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

Can't define a matrix-free linear system when using a preconditioner in fenics' built-in Krylov solvers

0 votes

I'm trying to define a matrix-free solver. Here is a very simple example with the identity matrix:

from dolfin import *

class IdentityMatrix(LinearOperator):

    def __init__(self, V):
        u = Function(V)
        LinearOperator.__init__(self, u.vector(), u.vector())

    def mult(self, x, y):
        y.zero()
        y.axpy(1.0, x)

if __name__ == "__main__":
    mesh = UnitSquareMesh(10,10)
    V = FunctionSpace(mesh, 'Lagrange', 1)
    Id = IdentityMatrix(V)
    solver = PETScKrylovSolver("cg", "amg")
    solver.set_operator(Id)
    b = interpolate(Constant(1.0), V)
    x = Function(V)
    solver.solve(x.vector(), b.vector())

This crashes with the following error message:

$ python test_matrixfree.py 
Solving linear system of size 121 x 121 (PETSc Krylov solver).
Traceback (most recent call last):
  File "test_matrixfree.py", line 23, in <module>
    solver.solve(x.vector(), b.vector())
RuntimeError: 

*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
***
***     fenics@fenicsproject.org
***
*** Remember to include the error message listed below and, if possible,
*** include a *minimal* running example to reproduce the error.
***
*** -------------------------------------------------------------------------
*** Error:   Unable to successfully call PETSc function 'KSPSolve'.
*** Reason:  PETSc error code is: 56.
*** Where:   This error was encountered inside /build/dolfin-k_QrtL/dolfin-1.6.0/dolfin/la/PETScKrylovSolver.cpp.
*** Process: 0
*** 
*** DOLFIN version: 1.6.0
*** Git changeset:  unknown
*** -------------------------------------------------------------------------

Same error occurs with other preconditioners. However, this works no preconditioner is used,

    solver = PETScKrylovSolver("cg", "none")

Isn't there any way to define a preconditioner in that case?
I use the Ubuntu binaries for fenics 1.6.0, but I observed the same error with a compiled version of fenics 1.6.0.

Thanks for any help!

asked May 12, 2016 by BC FEniCS Novice (790 points)
edited May 12, 2016 by BC

1 Answer

+1 vote
 
Best answer

Preconditioners are usually built from matrices. You would need a custom made
preconditioner in your case.

answered May 13, 2016 by Kent-Andre Mardal FEniCS Expert (14,380 points)
selected May 29, 2016 by BC
...