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()

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. str='default')
    2. MPICommWrapper, method: str='default')
    3. MPICommWrapper, A:, method: str='default')
    4., method: str='default')

Invoked with: < 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.