The following code seems to work in 1.6.0 but gives an error in 1.4.0
(see also https://github.com/Juanlu001/fenics-recipes/issues/28)
Any pointers to where something could be wrong?
from dolfin import *
mesh = UnitSquareMesh(2, 2)
V = FunctionSpace(mesh, 'CG', 1)
u = TrialFunction(V)
v = TestFunction(V)
a = inner(grad(u), grad(v))* dx
f = Expression('x[0]*x[1]')
b = f * v * dx
p = a + u * v * dx
A = assemble(a)
B = assemble(b)
P = assemble(p)
class CompositeOperator(object):
def __init__(self, A, B, C):
self.A_mat = as_backend_type(A).mat()
self.B_vec = as_backend_type(B).vec()
self.C_vec = as_backend_type(C).vec()
def mult(self, mat, x, y):
self.A_mat.mult(x, y)
a = self.C_vec.dot(x)
y.axpy(a, self.B_vec)
def as_petscmatrix(self):
from petsc4py import PETSc
mat = PETSc.Mat().createPython(self.A_mat.getSizes(), comm = mpi_comm_world())
mat.setPythonContext(self)
return PETScMatrix(mat)
# Apply boundary conditions
bc = DirichletBC(V, 1., "x[1]<DOLFIN_EPS")
bc.apply(A)
bc.apply(P)
C = B.copy()
bc.homogenize()
bc.apply(B)
A_B = CompositeOperator(A, B, C).as_petscmatrix()
solver = PETScKrylovSolver()
solver.set_operators(A_B, P)
solution = Function(V)
solver.solve(solution.vector(), C)
The error:
Traceback (most recent call last):
File "<path>/temp.py", line 50, in <module>
solver.solve(solution.vector(), C)
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: 73.
*** Where: This error was encountered inside /home/juanlu/miniconda/conda-bld/work/dolfin-1.4.0/dolfin/la/PETScKrylovSolver.cpp.
*** Process: unknown
***
*** DOLFIN version: 1.4.0
*** Git changeset:
*** -------------------------------------------------------------------------
Error 73 is "object in argument is in wrong state, e.g. unassembled mat".
http://www.mcs.anl.gov/petsc/petsc-master/include/petscerror.h.html