Convert to la.Matrix to PETsCMatrix

Hi, I want to make use of PETsCLUSolver, and I found some reference code from older version of fenics by doing

Ainv = PETsCLUSolver()
Ainv.set_operator(A)

in which A is a la.Matrix object. However I realised in the new version of fenics, set_operator() is no longer defined. One post I read says to do the following instead

Ainv = PETsCLUSolver(A)

However I get the error that PETsCMatrix object need to be supplied, while A is a la.Matrix.
Is there any easy ways to conver the la.Matrix to PETsCMatrix?

    self.Ainv = PETScLUSolver(A)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. dolfin.cpp.la.PETScLUSolver(method: str='default')
    2. dolfin.cpp.la.PETScLUSolver(comm: MPICommWrapper, method: str='default')
    3. dolfin.cpp.la.PETScLUSolver(comm: MPICommWrapper, A: dolfin.cpp.la.PETScMatrix, method: str='default')
    4. dolfin.cpp.la.PETScLUSolver(A: dolfin.cpp.la.PETScMatrix, method: str='default')

Invoked with: <dolfin.cpp.la.Matrix object at 0x7fba5d393308>

You can try

A_PETSc = as_backend_type(A)

then your line

Ainv = PETScLUSolver(A_PETSc)

should work.

1 Like

Thank you volkerk,

Earlier, I found Ainv = PETScLUSolver(A.instance()) also works.