I'm using petsc4py, and getting an exception I don't understand. I define the following function:
def tsIJacobian(self, ts, t, u, udot, shift, A, B):
self.setup_problem()
psol = fe.as_backend_type(self.sol.vector()).vec()
pA = fe.as_backend_type(self.A).mat()
u.copy(psol)
JU = fe.assemble(fe.derivative(ksdg.U_terms, ksdg.U))
Jrho = fe.assemble(fe.derivative(ksdg.rho_terms, ksdg.rho))
pJU = fe.as_backend_type(JU).mat()
pJrho = fe.as_backend_type(Jrho).mat()
pA.copy(A)
A.scale(shift)
A.axpy(1.0, pJU)
A.axpy(1.0, pJrho)
A.assemble()
if not (A is B):
A.copy(B)
B.assemble()
then try the following. (pA has already been defined as a 48x48 PETSc.Mat elsewhere, and assembled. ksdg is an instance of a class I'm working on, of which this will eventually be a member function, if I can get it to work):
J = pA.duplicate()
B = pA.duplicate()
tsIJacobian(ksdg, ts, 0, psol, pdsol, 0.1, J, B)
This throws the following exception:
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-24-2a71f7ccf0af> in <module>()
----> 1 tsIJacobian(ksdg, ts, 0, psol, pdsol, 0.1, J, B)
<ipython-input-22-14579c08d6ae> in tsIJacobian(self, ts, t, u, udot, shift, A, B)
15 A.assemble()
16 if not (A is B):
---> 17 A.copy(B)
18 B.assemble()
PETSc/Mat.pyx in petsc4py.PETSc.Mat.copy (src/petsc4py.PETSc.c:118071)()
Error: error code 63
Looking at petscerror.h.html
, 63 is PETSC_ERR_ARG_OUTOFRANGE 63 /* input argument, out of range */
.
If anyone understands why PETSc won't let me copy matrix A to B, I'd appreciate an explanation. Thanks.
OK, I have sort of figured it out. The error 63 exception is raised when one attempts to create a new nonzero element in a sparse matrix, if the option
PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR
is True
. Thus, if I execute
B.setOption(PETSc.Mat.Option.NEW_NONZERO_ALLOCATION_ERR, False)
immediately after creation of B
, no exception is raised in tsIJacobian. What I don't understand is why I don't get the same exception for J
.