This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Transforming a matrix into a Petsc Matrix

0 votes

Hi!
Is there a easy way to obtain a Petsc matrix from a "normal" one?

This is what I have: I assemble a mass matrix and I lump it, but I need to change some of its values. Then I need to convert it back to a PETSc matrix in order to use the solve function and apply the bc.

Thanks,
Cristina

asked Apr 22, 2015 by MCri FEniCS User (1,120 points)
edited Apr 22, 2015 by MCri

1 Answer

+1 vote
       # convert matrix to petsc
    from petsc4py import PETSc
    fullA = self.fullA.tocsr()
    fullE = self.fullE.tocsc()
    fullAT = self.fullA.T.tocsr()
    fullET = self.fullE.T.tocsc()

    fullApetsc = PETSc.Mat().createAIJ(size=fullA.shape, csr=(fullA.indptr, fullA.indices, fullA.data))
    fullEpetsc = PETSc.Mat().createAIJ(size=fullE.shape, csr=(fullE.indptr, fullE.indices, fullE.data))
    fullATpetsc = PETSc.Mat().createAIJ(size=fullAT.shape, csr=(fullAT.indptr, fullAT.indices, fullAT.data))
    fullETpetsc = PETSc.Mat().createAIJ(size=fullET.shape, csr=(fullET.indptr, fullET.indices, fullET.data))
    fullApetsc.assemble()
    fullEpetsc.assemble()
    fullATpetsc.assemble()
    fullETpetsc.assemble()

the way from scipy to petsc

answered Apr 23, 2015 by maxb90 FEniCS Novice (770 points)

Thanks for the answer and sorry for the delay.. I was thinking about a different way to solve may problem but I didn't find anything..
I tried with your suggestion but I got
from petsc4py import PETSc
ImportError: No module named petsc4py

Why?
and my second question is: How do the commands work? Why four commands?
Thanks

...