xref: /petsc/src/binding/petsc4py/src/petsc4py/PETSc/petscsnes.pxi (revision bd89dbf26d8a5efecb980364933175da61864cd7)
1cdef extern from * nogil:
2
3    ctypedef const char* PetscSNESType "SNESType"
4    PetscSNESType SNESNEWTONLS
5    PetscSNESType SNESNEWTONTR
6    PetscSNESType SNESNEWTONAL
7    PetscSNESType SNESPYTHON
8    PetscSNESType SNESNRICHARDSON
9    PetscSNESType SNESKSPONLY
10    PetscSNESType SNESKSPTRANSPOSEONLY
11    PetscSNESType SNESVINEWTONRSLS
12    PetscSNESType SNESVINEWTONSSLS
13    PetscSNESType SNESNGMRES
14    PetscSNESType SNESQN
15    PetscSNESType SNESSHELL
16    PetscSNESType SNESNGS
17    PetscSNESType SNESNCG
18    PetscSNESType SNESFAS
19    PetscSNESType SNESMS
20    PetscSNESType SNESNASM
21    PetscSNESType SNESANDERSON
22    PetscSNESType SNESASPIN
23    PetscSNESType SNESCOMPOSITE
24    PetscSNESType SNESPATCH
25
26    ctypedef enum PetscSNESNormSchedule "SNESNormSchedule":
27        SNES_NORM_DEFAULT
28        SNES_NORM_NONE
29        SNES_NORM_ALWAYS
30        SNES_NORM_INITIAL_ONLY
31        SNES_NORM_FINAL_ONLY
32        SNES_NORM_INITIAL_FINAL_ONLY
33
34    ctypedef enum PetscSNESConvergedReason "SNESConvergedReason":
35        # iterating
36        SNES_CONVERGED_ITERATING
37        # converged
38        SNES_CONVERGED_FNORM_ABS
39        SNES_CONVERGED_FNORM_RELATIVE
40        SNES_CONVERGED_SNORM_RELATIVE
41        SNES_CONVERGED_ITS
42        # diverged
43        SNES_DIVERGED_FUNCTION_DOMAIN
44        SNES_DIVERGED_FUNCTION_NANORINF
45        SNES_DIVERGED_OBJECTIVE_DOMAIN
46        SNES_DIVERGED_OBJECTIVE_NANORINF
47        SNES_DIVERGED_JACOBIAN_DOMAIN
48        SNES_DIVERGED_FUNCTION_COUNT
49        SNES_DIVERGED_LINEAR_SOLVE
50        SNES_DIVERGED_MAX_IT
51        SNES_DIVERGED_LINE_SEARCH
52        SNES_DIVERGED_INNER
53        SNES_DIVERGED_LOCAL_MIN
54        SNES_DIVERGED_DTOL
55        SNES_DIVERGED_TR_DELTA
56
57    ctypedef PetscErrorCode (*PetscSNESCtxDel)(void**)
58
59    ctypedef PetscErrorCode (*PetscSNESInitialGuessFunction)(PetscSNES,
60                                                             PetscVec,
61                                                             void*) except PETSC_ERR_PYTHON
62    ctypedef PetscErrorCode (*PetscSNESFunctionFunction)(PetscSNES,
63                                                         PetscVec,
64                                                         PetscVec,
65                                                         void*) except PETSC_ERR_PYTHON
66
67    ctypedef PetscErrorCode (*PetscSNESUpdateFunction)(PetscSNES,
68                                                       PetscInt) except PETSC_ERR_PYTHON
69
70    ctypedef PetscErrorCode (*PetscSNESJacobianFunction)(PetscSNES,
71                                                         PetscVec,
72                                                         PetscMat,
73                                                         PetscMat,
74                                                         void*) except PETSC_ERR_PYTHON
75
76    ctypedef PetscErrorCode (*PetscSNESObjectiveFunction)(PetscSNES,
77                                                          PetscVec,
78                                                          PetscReal*,
79                                                          void*) except PETSC_ERR_PYTHON
80
81    ctypedef PetscErrorCode (*PetscSNESConvergedFunction)(PetscSNES,
82                                                          PetscInt,
83                                                          PetscReal,
84                                                          PetscReal,
85                                                          PetscReal,
86                                                          PetscSNESConvergedReason*,
87                                                          void*) except PETSC_ERR_PYTHON
88
89    ctypedef PetscErrorCode (*PetscSNESMonitorFunction)(PetscSNES,
90                                                        PetscInt,
91                                                        PetscReal,
92                                                        void*) except PETSC_ERR_PYTHON
93
94    PetscErrorCode SNESCreate(MPI_Comm, PetscSNES*)
95    PetscErrorCode SNESParametersInitialize(PetscSNES)
96    PetscErrorCode SNESDestroy(PetscSNES*)
97    PetscErrorCode SNESView(PetscSNES, PetscViewer)
98
99    PetscErrorCode SNESSetType(PetscSNES, PetscSNESType)
100    PetscErrorCode SNESGetType(PetscSNES, PetscSNESType*)
101    PetscErrorCode SNESSetOptionsPrefix(PetscSNES, char[])
102    PetscErrorCode SNESAppendOptionsPrefix(PetscSNES, char[])
103    PetscErrorCode SNESGetOptionsPrefix(PetscSNES, char*[])
104    PetscErrorCode SNESSetFromOptions(PetscSNES)
105    PetscErrorCode SNESSetApplicationContext(PetscSNES, void*)
106    PetscErrorCode SNESGetApplicationContext(PetscSNES, void*)
107
108    PetscErrorCode SNESGetKSP(PetscSNES, PetscKSP*)
109    PetscErrorCode SNESSetKSP(PetscSNES, PetscKSP)
110
111    PetscErrorCode SNESGetDM(PetscSNES, PetscDM*)
112    PetscErrorCode SNESSetDM(PetscSNES, PetscDM)
113
114    PetscErrorCode SNESFASSetInterpolation(PetscSNES, PetscInt, PetscMat)
115    PetscErrorCode SNESFASGetInterpolation(PetscSNES, PetscInt, PetscMat*)
116    PetscErrorCode SNESFASSetRestriction(PetscSNES, PetscInt, PetscMat)
117    PetscErrorCode SNESFASGetRestriction(PetscSNES, PetscInt, PetscMat*)
118    PetscErrorCode SNESFASSetInjection(PetscSNES, PetscInt, PetscMat)
119    PetscErrorCode SNESFASGetInjection(PetscSNES, PetscInt, PetscMat*)
120    PetscErrorCode SNESFASSetRScale(PetscSNES, PetscInt, PetscVec)
121    PetscErrorCode SNESFASSetLevels(PetscSNES, PetscInt, MPI_Comm[])
122    PetscErrorCode SNESFASGetLevels(PetscSNES, PetscInt*)
123    PetscErrorCode SNESFASGetCycleSNES(PetscSNES, PetscInt, PetscSNES*)
124    PetscErrorCode SNESFASGetCoarseSolve(PetscSNES, PetscSNES*)
125    PetscErrorCode SNESFASGetSmoother(PetscSNES, PetscInt, PetscSNES*)
126    PetscErrorCode SNESFASGetSmootherDown(PetscSNES, PetscInt, PetscSNES*)
127    PetscErrorCode SNESFASGetSmootherUp(PetscSNES, PetscInt, PetscSNES*)
128
129    PetscErrorCode SNESGetNPC(PetscSNES, PetscSNES*)
130    PetscErrorCode SNESHasNPC(PetscSNES, PetscBool*)
131    PetscErrorCode SNESSetNPC(PetscSNES, PetscSNES)
132    PetscErrorCode SNESSetNPCSide(PetscSNES, PetscPCSide)
133    PetscErrorCode SNESGetNPCSide(PetscSNES, PetscPCSide*)
134
135    PetscErrorCode SNESGetRhs(PetscSNES, PetscVec*)
136    PetscErrorCode SNESGetSolution(PetscSNES, PetscVec*)
137    PetscErrorCode SNESSetSolution(PetscSNES, PetscVec)
138    PetscErrorCode SNESGetSolutionUpdate(PetscSNES, PetscVec*)
139
140    PetscErrorCode SNESSetComputeInitialGuess(PetscSNES, PetscSNESInitialGuessFunction, void*)
141    PetscErrorCode SNESSetFunction(PetscSNES, PetscVec, PetscSNESFunctionFunction, void*)
142    PetscErrorCode SNESGetFunction(PetscSNES, PetscVec*, void*, void**)
143    PetscErrorCode SNESSetUpdate(PetscSNES, PetscSNESUpdateFunction)
144    PetscErrorCode SNESSetJacobian(PetscSNES, PetscMat, PetscMat, PetscSNESJacobianFunction, void*)
145    PetscErrorCode SNESGetJacobian(PetscSNES, PetscMat*, PetscMat*, PetscSNESJacobianFunction*, void**)
146    PetscErrorCode SNESSetObjective(PetscSNES, PetscSNESObjectiveFunction, void*)
147    PetscErrorCode SNESGetObjective(PetscSNES, PetscSNESObjectiveFunction*, void**)
148
149    PetscErrorCode SNESComputeFunction(PetscSNES, PetscVec, PetscVec)
150    PetscErrorCode SNESComputeJacobian(PetscSNES, PetscVec, PetscMat, PetscMat)
151    PetscErrorCode SNESComputeObjective(PetscSNES, PetscVec, PetscReal*)
152
153    ctypedef PetscErrorCode (*PetscSNESNGSFunction)(PetscSNES,
154                                                    PetscVec,
155                                                    PetscVec,
156                                                    void*) except PETSC_ERR_PYTHON
157    PetscErrorCode SNESSetNGS(PetscSNES, PetscSNESNGSFunction, void*)
158    PetscErrorCode SNESGetNGS(PetscSNES, PetscSNESNGSFunction*, void**)
159    PetscErrorCode SNESComputeNGS(PetscSNES, PetscVec, PetscVec)
160
161    PetscErrorCode SNESSetNormSchedule(PetscSNES, PetscSNESNormSchedule)
162    PetscErrorCode SNESGetNormSchedule(PetscSNES, PetscSNESNormSchedule*)
163
164    PetscErrorCode SNESSetTolerances(PetscSNES, PetscReal, PetscReal, PetscReal, PetscInt, PetscInt)
165    PetscErrorCode SNESGetTolerances(PetscSNES, PetscReal*, PetscReal*, PetscReal*, PetscInt*, PetscInt*)
166    PetscErrorCode SNESSetDivergenceTolerance(PetscSNES, PetscReal)
167    PetscErrorCode SNESGetDivergenceTolerance(PetscSNES, PetscReal*)
168
169    PetscErrorCode SNESConverged(PetscSNES, PetscInt, PetscReal, PetscReal, PetscReal)
170    PetscErrorCode SNESSetConvergenceTest(PetscSNES, PetscSNESConvergedFunction, void*, PetscSNESCtxDel*)
171    PetscErrorCode SNESConvergedDefault(PetscSNES, PetscInt, PetscReal, PetscReal, PetscReal,
172                                        PetscSNESConvergedReason*, void*) except PETSC_ERR_PYTHON
173    PetscErrorCode SNESConvergedSkip(PetscSNES, PetscInt, PetscReal, PetscReal, PetscReal,
174                                     PetscSNESConvergedReason*, void*) except PETSC_ERR_PYTHON
175    PetscErrorCode SNESSetConvergenceHistory(PetscSNES, PetscReal[], PetscInt[], PetscInt, PetscBool)
176    PetscErrorCode SNESGetConvergenceHistory(PetscSNES, PetscReal*[], PetscInt*[], PetscInt*)
177    PetscErrorCode SNESLogConvergenceHistory(PetscSNES, PetscReal, PetscInt)
178
179    PetscErrorCode SNESMonitorSet(PetscSNES, PetscSNESMonitorFunction, void*, PetscSNESCtxDel)
180    PetscErrorCode SNESMonitorCancel(PetscSNES)
181    PetscErrorCode SNESMonitor(PetscSNES, PetscInt, PetscReal)
182
183    PetscErrorCode SNESSetUp(PetscSNES)
184    PetscErrorCode SNESSetUpMatrices(PetscSNES)
185    PetscErrorCode SNESReset(PetscSNES)
186    PetscErrorCode SNESSolve(PetscSNES, PetscVec, PetscVec)
187
188    PetscErrorCode SNESSetConvergedReason(PetscSNES, PetscSNESConvergedReason)
189    PetscErrorCode SNESGetConvergedReason(PetscSNES, PetscSNESConvergedReason*)
190    PetscErrorCode SNESSetErrorIfNotConverged(PetscSNES, PetscBool)
191    PetscErrorCode SNESGetErrorIfNotConverged(PetscSNES, PetscBool*)
192    PetscErrorCode SNESSetIterationNumber(PetscSNES, PetscInt)
193    PetscErrorCode SNESGetIterationNumber(PetscSNES, PetscInt*)
194    PetscErrorCode SNESSetForceIteration(PetscSNES, PetscBool)
195    PetscErrorCode SNESSetFunctionNorm(PetscSNES, PetscReal)
196    PetscErrorCode SNESGetFunctionNorm(PetscSNES, PetscReal*)
197    PetscErrorCode SNESGetLinearSolveIterations(PetscSNES, PetscInt*)
198    PetscErrorCode SNESSetCountersReset(PetscSNES, PetscBool)
199
200    PetscErrorCode SNESGetNumberFunctionEvals(PetscSNES, PetscInt*)
201    PetscErrorCode SNESSetMaxNonlinearStepFailures(PetscSNES, PetscInt)
202    PetscErrorCode SNESGetMaxNonlinearStepFailures(PetscSNES, PetscInt*)
203    PetscErrorCode SNESGetNonlinearStepFailures(PetscSNES, PetscInt*)
204    PetscErrorCode SNESSetMaxLinearSolveFailures(PetscSNES, PetscInt)
205    PetscErrorCode SNESGetMaxLinearSolveFailures(PetscSNES, PetscInt*)
206    PetscErrorCode SNESGetLinearSolveFailures(PetscSNES, PetscInt*)
207
208    PetscErrorCode SNESKSPSetUseEW(PetscSNES, PetscBool)
209    PetscErrorCode SNESKSPGetUseEW(PetscSNES, PetscBool*)
210    PetscErrorCode SNESKSPSetParametersEW(PetscSNES, PetscInt, PetscReal, PetscReal,
211                                          PetscReal, PetscReal, PetscReal, PetscReal)
212    PetscErrorCode SNESKSPGetParametersEW(PetscSNES, PetscInt*, PetscReal*, PetscReal*,
213                                          PetscReal*, PetscReal*, PetscReal*, PetscReal*)
214
215    PetscErrorCode SNESVISetVariableBounds(PetscSNES, PetscVec, PetscVec)
216    PetscErrorCode SNESVIGetVariableBounds(PetscSNES, PetscVec*, PetscVec*)
217    PetscErrorCode SNESVIGetInactiveSet(PetscSNES, PetscIS*)
218
219    PetscErrorCode SNESCompositeGetSNES(PetscSNES, PetscInt, PetscSNES*)
220    PetscErrorCode SNESCompositeGetNumber(PetscSNES, PetscInt*)
221    PetscErrorCode SNESNASMGetSNES(PetscSNES, PetscInt, PetscSNES*)
222    PetscErrorCode SNESNASMGetNumber(PetscSNES, PetscInt*)
223
224    PetscErrorCode SNESPatchSetCellNumbering(PetscSNES, PetscSection)
225    PetscErrorCode SNESPatchSetDiscretisationInfo(PetscSNES, PetscInt, PetscDM*, PetscInt*, PetscInt*, const PetscInt**, const PetscInt*, PetscInt, const PetscInt*, PetscInt, const PetscInt*)
226    PetscErrorCode SNESPatchSetComputeOperator(PetscSNES, PetscPCPatchComputeOperator, void*)
227    PetscErrorCode SNESPatchSetComputeFunction(PetscSNES, PetscPCPatchComputeFunction, void*)
228    PetscErrorCode SNESPatchSetConstructType(PetscSNES, PetscPCPatchConstructType, PetscPCPatchConstructOperator, void*)
229
230    PetscErrorCode SNESPythonSetType(PetscSNES, char[])
231    PetscErrorCode SNESPythonGetType(PetscSNES, char*[])
232
233    PetscErrorCode SNESNewtonTRSetTolerances(PetscSNES, PetscReal, PetscReal, PetscReal)
234    PetscErrorCode SNESNewtonTRGetTolerances(PetscSNES, PetscReal*, PetscReal*, PetscReal*)
235    PetscErrorCode SNESNewtonTRSetUpdateParameters(PetscSNES, PetscReal, PetscReal, PetscReal, PetscReal, PetscReal)
236    PetscErrorCode SNESNewtonTRGetUpdateParameters(PetscSNES, PetscReal*, PetscReal*, PetscReal*, PetscReal*, PetscReal*)
237
238    ctypedef const char* PetscSNESLineSearchType "SNESLineSearchType"
239    PetscSNESLineSearchType SNESLINESEARCHBT
240    PetscSNESLineSearchType SNESLINESEARCHNLEQERR
241    PetscSNESLineSearchType SNESLINESEARCHBASIC
242    PetscSNESLineSearchType SNESLINESEARCHNONE
243    PetscSNESLineSearchType SNESLINESEARCHSECANT
244    PetscSNESLineSearchType SNESLINESEARCHCP
245    PetscSNESLineSearchType SNESLINESEARCHSHELL
246    PetscSNESLineSearchType SNESLINESEARCHNCGLINEAR
247    PetscSNESLineSearchType SNESLINESEARCHBISECTION
248    PetscErrorCode SNESGetLineSearch(PetscSNES, PetscSNESLineSearch*)
249    PetscErrorCode SNESSetLineSearch(PetscSNES, PetscSNESLineSearch)
250    PetscErrorCode SNESLineSearchCreate(MPI_Comm, PetscSNESLineSearch*)
251    PetscErrorCode SNESLineSearchDestroy(PetscSNESLineSearch*)
252    PetscErrorCode SNESLineSearchSetFromOptions(PetscSNESLineSearch)
253    PetscErrorCode SNESLineSearchView(PetscSNESLineSearch, PetscViewer)
254    PetscErrorCode SNESLineSearchSetType(PetscSNESLineSearch, PetscSNESLineSearchType)
255    PetscErrorCode SNESLineSearchGetType(PetscSNESLineSearch, PetscSNESLineSearchType*)
256    PetscErrorCode SNESLineSearchSetTolerances(PetscSNESLineSearch, PetscReal, PetscReal, PetscReal, PetscReal, PetscReal, PetscInt)
257    PetscErrorCode SNESLineSearchGetTolerances(PetscSNESLineSearch, PetscReal*, PetscReal*, PetscReal*, PetscReal*, PetscReal*, PetscInt*)
258    PetscErrorCode SNESLineSearchSetOrder(PetscSNESLineSearch, PetscInt)
259    PetscErrorCode SNESLineSearchGetOrder(PetscSNESLineSearch, PetscInt*)
260
261    PetscErrorCode SNESLineSearchApply(PetscSNESLineSearch, PetscVec, PetscVec, PetscReal*, PetscVec)
262    PetscErrorCode SNESLineSearchGetNorms(PetscSNESLineSearch, PetscReal*, PetscReal*, PetscReal*)
263    PetscErrorCode SNESLineSearchDestroy(PetscSNESLineSearch*)
264
265    ctypedef PetscErrorCode (*PetscSNESPreCheckFunction)(PetscSNESLineSearch,
266                                                         PetscVec, PetscVec,
267                                                         PetscBool*,
268                                                         void*) except PETSC_ERR_PYTHON
269    PetscErrorCode SNESLineSearchSetPreCheck(PetscSNESLineSearch, PetscSNESPreCheckFunction, void*)
270    PetscErrorCode SNESLineSearchGetSNES(PetscSNESLineSearch, PetscSNES*)
271
272    ctypedef enum PetscSNESNewtonALCorrectionType "SNESNewtonALCorrectionType":
273        SNES_NEWTONAL_CORRECTION_EXACT
274        SNES_NEWTONAL_CORRECTION_NORMAL
275    PetscErrorCode SNESNewtonALSetFunction(PetscSNES, PetscSNESFunctionFunction, void*)
276    PetscErrorCode SNESNewtonALGetLoadParameter(PetscSNES, PetscReal*)
277    PetscErrorCode SNESNewtonALSetCorrectionType(PetscSNES, PetscSNESNewtonALCorrectionType)
278
279cdef extern from * nogil: # custom.h
280    PetscErrorCode SNESSetUseMFFD(PetscSNES, PetscBool)
281    PetscErrorCode SNESGetUseMFFD(PetscSNES, PetscBool*)
282
283    PetscErrorCode SNESSetUseFDColoring(PetscSNES, PetscBool)
284    PetscErrorCode SNESGetUseFDColoring(PetscSNES, PetscBool*)
285
286    PetscErrorCode SNESSetUseKSP(PetscSNES, PetscBool)
287    PetscErrorCode SNESGetUseKSP(PetscSNES, PetscBool*)
288
289    PetscErrorCode SNESConvergenceTestCall(PetscSNES, PetscInt,
290                                           PetscReal, PetscReal, PetscReal,
291                                           PetscSNESConvergedReason*)
292
293# -----------------------------------------------------------------------------
294
295cdef inline SNES ref_SNES(PetscSNES snes):
296    cdef SNES ob = <SNES> SNES()
297    ob.snes = snes
298    CHKERR(PetscINCREF(ob.obj))
299    return ob
300
301# -----------------------------------------------------------------------------
302
303cdef PetscErrorCode SNES_InitialGuess(
304    PetscSNES snes,
305    PetscVec  x,
306    void      *ctx,
307   ) except PETSC_ERR_PYTHON with gil:
308    cdef SNES Snes = ref_SNES(snes)
309    cdef Vec  Xvec = ref_Vec(x)
310    cdef object context = Snes.get_attr('__initialguess__')
311    if context is None and ctx != NULL: context = <object>ctx
312    assert context is not None and type(context) is tuple # sanity check
313    (initialguess, args, kargs) = context
314    initialguess(Snes, Xvec, *args, **kargs)
315    return PETSC_SUCCESS
316
317# -----------------------------------------------------------------------------
318
319cdef PetscErrorCode SNES_PreCheck(
320    PetscSNESLineSearch linesearch,
321    PetscVec  x,
322    PetscVec  y,
323    PetscBool *changed,
324    void      *ctx
325   ) except PETSC_ERR_PYTHON with gil:
326    cdef PetscSNES snes = NULL
327    CHKERR(SNESLineSearchGetSNES(linesearch, &snes))
328    cdef object b = False
329    cdef SNES Snes = ref_SNES(snes)
330    cdef Vec  Xvec = ref_Vec(x)
331    cdef Vec  Yvec = ref_Vec(y)
332    cdef object context = Snes.get_attr('__precheck__')
333    if context is None and ctx != NULL: context = <object>ctx
334    assert context is not None and type(context) is tuple # sanity check
335    (precheck, args, kargs) = context
336    b = precheck(Xvec, Yvec, *args, **kargs)
337    changed[0] = asBool(b)
338    return PETSC_SUCCESS
339
340# -----------------------------------------------------------------------------
341
342cdef PetscErrorCode SNES_Function(
343    PetscSNES snes,
344    PetscVec  x,
345    PetscVec  f,
346    void      *ctx,
347   ) except PETSC_ERR_PYTHON with gil:
348    cdef SNES Snes = ref_SNES(snes)
349    cdef Vec  Xvec = ref_Vec(x)
350    cdef Vec  Fvec = ref_Vec(f)
351    cdef object context = Snes.get_attr('__function__')
352    if context is None and ctx != NULL: context = <object>ctx
353    assert context is not None and type(context) is tuple # sanity check
354    (function, args, kargs) = context
355    function(Snes, Xvec, Fvec, *args, **kargs)
356    return PETSC_SUCCESS
357
358# -----------------------------------------------------------------------------
359
360cdef PetscErrorCode SNES_Update(
361    PetscSNES snes,
362    PetscInt  its,
363   ) except PETSC_ERR_PYTHON with gil:
364    cdef SNES Snes = ref_SNES(snes)
365    cdef object context = Snes.get_attr('__update__')
366    assert context is not None and type(context) is tuple # sanity check
367    (update, args, kargs) = context
368    update(Snes, toInt(its), *args, **kargs)
369    return PETSC_SUCCESS
370
371# -----------------------------------------------------------------------------
372
373cdef PetscErrorCode SNES_Jacobian(
374    PetscSNES snes,
375    PetscVec  x,
376    PetscMat  J,
377    PetscMat  P,
378    void      *ctx,
379   ) except PETSC_ERR_PYTHON with gil:
380    cdef SNES Snes = ref_SNES(snes)
381    cdef Vec  Xvec = ref_Vec(x)
382    cdef Mat  Jmat = ref_Mat(J)
383    cdef Mat  Pmat = ref_Mat(P)
384    cdef object context = Snes.get_attr('__jacobian__')
385    if context is None and ctx != NULL: context = <object>ctx
386    assert context is not None and type(context) is tuple # sanity check
387    (jacobian, args, kargs) = context
388    jacobian(Snes, Xvec, Jmat, Pmat, *args, **kargs)
389    return PETSC_SUCCESS
390
391# -----------------------------------------------------------------------------
392
393cdef PetscErrorCode SNES_Objective(
394    PetscSNES snes,
395    PetscVec  x,
396    PetscReal *o,
397    void      *ctx,
398   ) except PETSC_ERR_PYTHON with gil:
399    cdef SNES Snes = ref_SNES(snes)
400    cdef Vec  Xvec = ref_Vec(x)
401    cdef object context = Snes.get_attr('__objective__')
402    if context is None and ctx != NULL: context = <object>ctx
403    assert context is not None and type(context) is tuple # sanity check
404    (objective, args, kargs) = context
405    obj = objective(Snes, Xvec, *args, **kargs)
406    o[0] = asReal(obj)
407    return PETSC_SUCCESS
408
409# -----------------------------------------------------------------------------
410
411cdef PetscErrorCode SNES_NGS(
412    PetscSNES snes,
413    PetscVec  x,
414    PetscVec  b,
415    void      *ctx,
416   ) except PETSC_ERR_PYTHON with gil:
417    cdef SNES Snes = ref_SNES(snes)
418    cdef Vec  Xvec = ref_Vec(x)
419    cdef Vec  Bvec = ref_Vec(b)
420    cdef object context = Snes.get_attr('__ngs__')
421    if context is None and ctx != NULL: context = <object>ctx
422    assert context is not None and type(context) is tuple # sanity check
423    (ngs, args, kargs) = context
424    ngs(Snes, Xvec, Bvec, *args, **kargs)
425    return PETSC_SUCCESS
426
427# -----------------------------------------------------------------------------
428
429cdef PetscErrorCode SNES_Converged(
430    PetscSNES  snes,
431    PetscInt   iters,
432    PetscReal  xnorm,
433    PetscReal  gnorm,
434    PetscReal  fnorm,
435    PetscSNESConvergedReason *r,
436    void       *ctx,
437   ) except PETSC_ERR_PYTHON with gil:
438    cdef SNES Snes = ref_SNES(snes)
439    cdef object it = toInt(iters)
440    cdef object xn = toReal(xnorm)
441    cdef object gn = toReal(gnorm)
442    cdef object fn = toReal(fnorm)
443    cdef object context = Snes.get_attr('__converged__')
444    if context is None and ctx != NULL: context = <object>ctx
445    assert context is not None and type(context) is tuple # sanity check
446    (converged, args, kargs) = context
447    reason = converged(Snes, it, (xn, gn, fn), *args, **kargs)
448    if   reason is None:  r[0] = SNES_CONVERGED_ITERATING
449    elif reason is False: r[0] = SNES_CONVERGED_ITERATING
450    elif reason is True:  r[0] = SNES_CONVERGED_ITS # XXX ?
451    else:                 r[0] = reason
452    return PETSC_SUCCESS
453
454# -----------------------------------------------------------------------------
455
456cdef PetscErrorCode SNES_Monitor(
457    PetscSNES snes,
458    PetscInt  iters,
459    PetscReal rnorm,
460    void      *ctx,
461   ) except PETSC_ERR_PYTHON with gil:
462    cdef SNES Snes = ref_SNES(snes)
463    cdef object monitorlist = Snes.get_attr('__monitor__')
464    if monitorlist is None: return PETSC_SUCCESS
465    cdef object it = toInt(iters)
466    cdef object rn = toReal(rnorm)
467    for (monitor, args, kargs) in monitorlist:
468        monitor(Snes, it, rn, *args, **kargs)
469    return PETSC_SUCCESS
470
471# -----------------------------------------------------------------------------
472
473cdef PetscErrorCode SNES_NewtonALFunction(
474    PetscSNES snes,
475    PetscVec  x,
476    PetscVec  f,
477    void      *ctx,
478   ) except PETSC_ERR_PYTHON with gil:
479    cdef SNES Snes = ref_SNES(snes)
480    cdef Vec  Xvec = ref_Vec(x)
481    cdef Vec  Fvec = ref_Vec(f)
482    cdef object context = Snes.get_attr('__newtonal_function__')
483    if context is None and ctx != NULL: context = <object>ctx
484    assert context is not None and type(context) is tuple # sanity check
485    (function, args, kargs) = context
486    function(Snes, Xvec, Fvec, *args, **kargs)
487    return PETSC_SUCCESS
488
489
490# -----------------------------------------------------------------------------
491