1from petsc4py.PETSc cimport Vec,  PetscVec
2from petsc4py.PETSc cimport Mat,  PetscMat
3from petsc4py.PETSc cimport DM,   PetscDM
4from petsc4py.PETSc cimport SNES, PetscSNES
5
6from petsc4py.PETSc import Error
7
8cdef extern from "Bratu3Dimpl.h":
9    ctypedef struct Params:
10        double lambda_
11    int FormInitGuess(PetscDM da, PetscVec x, Params *p)
12    int FormFunction (PetscDM da, PetscVec x, PetscVec F, Params *p)
13    int FormJacobian (PetscDM da, PetscVec x, PetscMat J, Params *p)
14
15def formInitGuess(Vec x, DM da, double lambda_):
16    cdef int ierr
17    cdef Params p = {"lambda_" : lambda_}
18    ierr = FormInitGuess(da.dm, x.vec, &p)
19    if ierr != 0: raise Error(ierr)
20
21def formFunction(SNES snes, Vec x, Vec f, DM da, double lambda_):
22    cdef int ierr
23    cdef Params p = {"lambda_" : lambda_}
24    ierr = FormFunction(da.dm, x.vec, f.vec, &p)
25    if ierr != 0: raise Error(ierr)
26
27def formJacobian(SNES snes, Vec x, Mat J, Mat P, DM da, double lambda_):
28    cdef int ierr
29    cdef Params p = {"lambda_" : lambda_}
30    ierr = FormJacobian(da.dm, x.vec, P.mat, &p)
31    if ierr != 0: raise Error(ierr)
32    if J != P: J.assemble() # for matrix-free operator
33    return Mat.Structure.SAME_NONZERO_PATTERN
34