xref: /petsc/src/binding/petsc4py/demo/legacy/wrap-cython/Bratu3D.pyx (revision 552edb6364df478b294b3111f33a8f37ca096b20)
1from petsc4py.PETSc cimport Vec,  PetscVec
2from petsc4py.PETSc cimport Mat,  PetscMat
3from petsc4py.PETSc cimport DM,   PetscDM
4from petsc4py.PETSc cimport SNES
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
15
16def formInitGuess(Vec x, DM da, double lambda_):
17    cdef int ierr
18    cdef Params p = {"lambda_" : lambda_}
19    ierr = FormInitGuess(da.dm, x.vec, &p)
20    if ierr != 0: raise Error(ierr)
21
22
23def formFunction(SNES snes, Vec x, Vec f, DM da, double lambda_):
24    cdef int ierr
25    cdef Params p = {"lambda_" : lambda_}
26    ierr = FormFunction(da.dm, x.vec, f.vec, &p)
27    if ierr != 0: raise Error(ierr)
28
29
30def formJacobian(SNES snes, Vec x, Mat J, Mat P, DM da, double lambda_):
31    cdef int ierr
32    cdef Params p = {"lambda_" : lambda_}
33    ierr = FormJacobian(da.dm, x.vec, P.mat, &p)
34    if ierr != 0: raise Error(ierr)
35    if J != P: J.assemble() # for matrix-free operator
36    return Mat.Structure.SAME_NONZERO_PATTERN
37