I would like to explicitly populate a PETScMatrix from given entries (not via automatic assembly in dolfin). Looking through some older question, the consensus seems to be that it's best to use petsc4py
for this. I understand that I can get the underlying petsc4py.PETSc.Mat
object as follows:
import dolfin as df
A = df.PETScMatrix()
B = A.mat() # B is a petsc4py.PETSc.Mat object
print(type(B))
Output:
<type 'petsc4py.PETSc.Mat'>
Now, I can fill the matrix B with values using the regular petsc4py functionality, for example:
B.create()
B.setSizes((3, 3))
B.setType('aij')
B.setUp()
B.setValue(0, 2, 42.0)
B.assemble()
print B.getValues(range(3), range(3))
Output:
[[ 0., 0., 42.],
[ 0., 0., 0.],
[ 0., 0., 0.]]
However, these changes don't seem to be propagated into A
:
print A.array()
Output:
array([], shape=(0, 0), dtype=float64)
So it seems I'm missing the step to "push" the changes made in B
back into A
. How can I do this?
Many thanks!
P.S.: In the question at [1] Garth Wells suggested an alternative option to construct a TensorLayout
object to specify a sparsity pattern for A
. However, there doesn't seem to be a Python interface for TensorLayout
. Is this possible to do from Python (and if so, is there example code somewhere), or is it best to use petsc4py
directly?
[1] http://fenicsproject.org/qa/2008/how-to-initialize-a-petscmatrix-with-given-sizes?show=2074#a2074