Adding rows of Matrix

Hi,

Have you tried using the petsc4py API as mentioned here? Something like

from dolfin import *
import numpy as np
from petsc4py import PETSc
addMode = PETSc.InsertMode.ADD

mesh = UnitSquareMesh(5,5)

V = VectorFunctionSpace(mesh, 'CG', 2)

u = TrialFunction(V)
v = TestFunction(V)
A = assemble( inner(u, v) * dx )

row_block_0 = np.ndarray( ( 1, V.dim() ), dtype=np.float_)
row_block_1 = np.ndarray( ( 1, V.dim() ), dtype=np.float_)
identityRow = np.identity(V.dim())[1] #row to set to identity

A.get( row_block_0, [0], list(range(V.dim())) )
A.get( row_block_1, [1], list(range(V.dim())) )
print(A.array())  # original


Am = as_backend_type(A).mat()
Am.setOption(PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR, False) 

# setting the first row to the sum of first two rows
Am.setValues([0], list(range(V.dim())), row_block_0 + row_block_1)

# setting the row-1 to identity: assuming this is what A.ident([1]) does
Am.setValues([1], list(range(V.dim())), identityRow)

Am.assemble()

print(A.array())  # modified