xref: /petsc/include/petscsnes.h (revision 6849ba73f22fecb8f92ef896a42e4e8bd4cd6965)
1 /*
2     User interface for the nonlinear solvers package.
3 */
4 #if !defined(__PETSCSNES_H)
5 #define __PETSCSNES_H
6 #include "petscksp.h"
7 PETSC_EXTERN_CXX_BEGIN
8 
9 /*S
10      SNES - Abstract PETSc object that manages all nonlinear solves
11 
12    Level: beginner
13 
14   Concepts: nonlinear solvers
15 
16 .seealso:  SNESCreate(), SNESSetType(), SNESType, TS, KSP, KSP, PC
17 S*/
18 typedef struct _p_SNES* SNES;
19 
20 /*E
21     SNESType - String with the name of a PETSc SNES method or the creation function
22        with an optional dynamic library name, for example
23        http://www.mcs.anl.gov/petsc/lib.a:mysnescreate()
24 
25    Level: beginner
26 
27 .seealso: SNESSetType(), SNES
28 E*/
29 #define SNESLS          "ls"
30 #define SNESTR          "tr"
31 #define SNESTEST        "test"
32 #define SNESType char*
33 
34 /* Logging support */
35 extern PetscCookie SNES_COOKIE;
36 extern PetscEvent    SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval;
37 
38 
39 EXTERN PetscErrorCode SNESInitializePackage(const char[]);
40 
41 EXTERN PetscErrorCode SNESCreate(MPI_Comm,SNES*);
42 EXTERN PetscErrorCode SNESDestroy(SNES);
43 EXTERN PetscErrorCode SNESSetType(SNES,const SNESType);
44 EXTERN PetscErrorCode SNESSetMonitor(SNES,PetscErrorCode(*)(SNES,int,PetscReal,void*),void *,PetscErrorCode (*)(void*));
45 EXTERN PetscErrorCode SNESClearMonitor(SNES);
46 EXTERN PetscErrorCode SNESSetConvergenceHistory(SNES,PetscReal[],int[],int,PetscTruth);
47 EXTERN PetscErrorCode SNESGetConvergenceHistory(SNES,PetscReal*[],int *[],int *);
48 EXTERN PetscErrorCode SNESSetUp(SNES,Vec);
49 EXTERN PetscErrorCode SNESSolve(SNES,Vec);
50 
51 EXTERN PetscErrorCode SNESAddOptionsChecker(PetscErrorCode (*)(SNES));
52 
53 EXTERN PetscErrorCode SNESSetRhsBC(SNES, PetscErrorCode (*)(SNES, Vec, void *));
54 EXTERN PetscErrorCode SNESDefaultRhsBC(SNES, Vec, void *);
55 EXTERN PetscErrorCode SNESSetSolutionBC(SNES, PetscErrorCode (*)(SNES, Vec, void *));
56 EXTERN PetscErrorCode SNESDefaultSolutionBC(SNES, Vec, void *);
57 EXTERN PetscErrorCode SNESSetUpdate(SNES, PetscErrorCode (*)(SNES, int));
58 EXTERN PetscErrorCode SNESDefaultUpdate(SNES, int);
59 
60 extern PetscFList SNESList;
61 EXTERN PetscErrorCode SNESRegisterDestroy(void);
62 EXTERN PetscErrorCode SNESRegisterAll(const char[]);
63 
64 EXTERN PetscErrorCode SNESRegister(const char[],const char[],const char[],PetscErrorCode (*)(SNES));
65 
66 /*MC
67    SNESRegisterDynamic - Adds a method to the nonlinear solver package.
68 
69    Synopsis:
70    int SNESRegisterDynamic(char *name_solver,char *path,char *name_create,PetscErrorCode (*routine_create)(SNES))
71 
72    Not collective
73 
74    Input Parameters:
75 +  name_solver - name of a new user-defined solver
76 .  path - path (either absolute or relative) the library containing this solver
77 .  name_create - name of routine to create method context
78 -  routine_create - routine to create method context
79 
80    Notes:
81    SNESRegisterDynamic() may be called multiple times to add several user-defined solvers.
82 
83    If dynamic libraries are used, then the fourth input argument (routine_create)
84    is ignored.
85 
86    Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT},
87    and others of the form ${any_environmental_variable} occuring in pathname will be
88    replaced with appropriate values.
89 
90    Sample usage:
91 .vb
92    SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
93                 "MySolverCreate",MySolverCreate);
94 .ve
95 
96    Then, your solver can be chosen with the procedural interface via
97 $     SNESSetType(snes,"my_solver")
98    or at runtime via the option
99 $     -snes_type my_solver
100 
101    Level: advanced
102 
103     Note: If your function is not being put into a shared library then use SNESRegister() instead
104 
105 .keywords: SNES, nonlinear, register
106 
107 .seealso: SNESRegisterAll(), SNESRegisterDestroy()
108 M*/
109 #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
110 #define SNESRegisterDynamic(a,b,c,d) SNESRegister(a,b,c,0)
111 #else
112 #define SNESRegisterDynamic(a,b,c,d) SNESRegister(a,b,c,d)
113 #endif
114 
115 EXTERN PetscErrorCode SNESGetKSP(SNES,KSP*);
116 EXTERN PetscErrorCode SNESGetSolution(SNES,Vec*);
117 EXTERN PetscErrorCode SNESGetSolutionUpdate(SNES,Vec*);
118 EXTERN PetscErrorCode SNESGetFunction(SNES,Vec*,void**,PetscErrorCode(**)(SNES,Vec,Vec,void*));
119 EXTERN PetscErrorCode SNESView(SNES,PetscViewer);
120 
121 EXTERN PetscErrorCode SNESSetOptionsPrefix(SNES,const char[]);
122 EXTERN PetscErrorCode SNESAppendOptionsPrefix(SNES,const char[]);
123 EXTERN PetscErrorCode SNESGetOptionsPrefix(SNES,char*[]);
124 EXTERN PetscErrorCode SNESSetFromOptions(SNES);
125 
126 EXTERN PetscErrorCode MatCreateSNESMF(SNES,Vec,Mat*);
127 EXTERN PetscErrorCode MatCreateMF(Vec,Mat*);
128 EXTERN PetscErrorCode MatSNESMFSetBase(Mat,Vec);
129 EXTERN PetscErrorCode MatSNESMFComputeJacobian(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
130 EXTERN PetscErrorCode MatSNESMFSetFunction(Mat,Vec,PetscErrorCode(*)(SNES,Vec,Vec,void*),void*);
131 EXTERN PetscErrorCode MatSNESMFSetFunctioni(Mat,PetscErrorCode (*)(int,Vec,PetscScalar*,void*));
132 EXTERN PetscErrorCode MatSNESMFSetFunctioniBase(Mat,PetscErrorCode (*)(Vec,void*));
133 EXTERN PetscErrorCode MatSNESMFAddNullSpace(Mat,MatNullSpace);
134 EXTERN PetscErrorCode MatSNESMFSetHHistory(Mat,PetscScalar[],int);
135 EXTERN PetscErrorCode MatSNESMFResetHHistory(Mat);
136 EXTERN PetscErrorCode MatSNESMFSetFunctionError(Mat,PetscReal);
137 EXTERN PetscErrorCode MatSNESMFSetPeriod(Mat,int);
138 EXTERN PetscErrorCode MatSNESMFGetH(Mat,PetscScalar *);
139 EXTERN PetscErrorCode MatSNESMFKSPMonitor(KSP,int,PetscReal,void *);
140 EXTERN PetscErrorCode MatSNESMFSetFromOptions(Mat);
141 EXTERN PetscErrorCode MatSNESMFCheckPositivity(Vec,Vec,PetscScalar*,void*);
142 EXTERN PetscErrorCode MatSNESMFSetCheckh(Mat,PetscErrorCode (*)(Vec,Vec,PetscScalar*,void*),void*);
143 
144 typedef struct _p_MatSNESMFCtx   *MatSNESMFCtx;
145 
146 #define MATSNESMF_DEFAULT "default"
147 #define MATSNESMF_WP      "wp"
148 #define MatSNESMFType char*
149 EXTERN PetscErrorCode MatSNESMFSetType(Mat,const MatSNESMFType);
150 EXTERN PetscErrorCode MatSNESMFRegister(const char[],const char[],const char[],PetscErrorCode (*)(MatSNESMFCtx));
151 
152 /*MC
153    MatSNESMFRegisterDynamic - Adds a method to the MatSNESMF registry.
154 
155    Synopsis:
156    int MatSNESMFRegisterDynamic(char *name_solver,char *path,char *name_create,PetscErrorCode (*routine_create)(MatSNESMF))
157 
158    Not Collective
159 
160    Input Parameters:
161 +  name_solver - name of a new user-defined compute-h module
162 .  path - path (either absolute or relative) the library containing this solver
163 .  name_create - name of routine to create method context
164 -  routine_create - routine to create method context
165 
166    Level: developer
167 
168    Notes:
169    MatSNESMFRegisterDynamic) may be called multiple times to add several user-defined solvers.
170 
171    If dynamic libraries are used, then the fourth input argument (routine_create)
172    is ignored.
173 
174    Sample usage:
175 .vb
176    MatSNESMFRegisterDynamic"my_h",/home/username/my_lib/lib/libO/solaris/mylib.a,
177                "MyHCreate",MyHCreate);
178 .ve
179 
180    Then, your solver can be chosen with the procedural interface via
181 $     MatSNESMFSetType(mfctx,"my_h")
182    or at runtime via the option
183 $     -snes_mf_type my_h
184 
185 .keywords: MatSNESMF, register
186 
187 .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy()
188 M*/
189 #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
190 #define MatSNESMFRegisterDynamic(a,b,c,d) MatSNESMFRegister(a,b,c,0)
191 #else
192 #define MatSNESMFRegisterDynamic(a,b,c,d) MatSNESMFRegister(a,b,c,d)
193 #endif
194 
195 EXTERN PetscErrorCode MatSNESMFRegisterAll(const char[]);
196 EXTERN PetscErrorCode MatSNESMFRegisterDestroy(void);
197 EXTERN PetscErrorCode MatSNESMFDefaultSetUmin(Mat,PetscReal);
198 EXTERN PetscErrorCode MatSNESMFWPSetComputeNormA(Mat,PetscTruth);
199 EXTERN PetscErrorCode MatSNESMFWPSetComputeNormU(Mat,PetscTruth);
200 
201 EXTERN PetscErrorCode MatDAADSetSNES(Mat,SNES);
202 
203 EXTERN PetscErrorCode SNESGetType(SNES,SNESType*);
204 EXTERN PetscErrorCode SNESDefaultMonitor(SNES,int,PetscReal,void *);
205 EXTERN PetscErrorCode SNESRatioMonitor(SNES,int,PetscReal,void *);
206 EXTERN PetscErrorCode SNESSetRatioMonitor(SNES);
207 EXTERN PetscErrorCode SNESVecViewMonitor(SNES,int,PetscReal,void *);
208 EXTERN PetscErrorCode SNESVecViewResidualMonitor(SNES,int,PetscReal,void *);
209 EXTERN PetscErrorCode SNESVecViewUpdateMonitor(SNES,int,PetscReal,void *);
210 EXTERN PetscErrorCode SNESDefaultSMonitor(SNES,int,PetscReal,void *);
211 EXTERN PetscErrorCode SNESSetTolerances(SNES,PetscReal,PetscReal,PetscReal,int,int);
212 EXTERN PetscErrorCode SNESGetTolerances(SNES,PetscReal*,PetscReal*,PetscReal*,int*,int*);
213 EXTERN PetscErrorCode SNESSetTrustRegionTolerance(SNES,PetscReal);
214 EXTERN PetscErrorCode SNESGetIterationNumber(SNES,int*);
215 EXTERN PetscErrorCode SNESGetFunctionNorm(SNES,PetscScalar*);
216 EXTERN PetscErrorCode SNESGetNumberUnsuccessfulSteps(SNES,int*);
217 EXTERN PetscErrorCode SNESSetMaximumUnsuccessfulSteps(SNES,int);
218 EXTERN PetscErrorCode SNESGetMaximumUnsuccessfulSteps(SNES,int*);
219 EXTERN PetscErrorCode SNESGetNumberLinearIterations(SNES,int*);
220 EXTERN PetscErrorCode SNES_KSP_SetParametersEW(SNES,int,PetscReal,PetscReal,PetscReal,PetscReal,PetscReal,PetscReal);
221 EXTERN PetscErrorCode SNES_KSP_SetConvergenceTestEW(SNES);
222 
223 /*
224      Reuse the default KSP monitor routines for SNES
225 */
226 EXTERN PetscErrorCode SNESLGMonitorCreate(const char[],const char[],int,int,int,int,PetscDrawLG*);
227 EXTERN PetscErrorCode SNESLGMonitor(SNES,int,PetscReal,void*);
228 EXTERN PetscErrorCode SNESLGMonitorDestroy(PetscDrawLG);
229 
230 EXTERN PetscErrorCode SNESSetApplicationContext(SNES,void *);
231 EXTERN PetscErrorCode SNESGetApplicationContext(SNES,void **);
232 
233 /*E
234     SNESConvergedReason - reason a SNES method was said to
235          have converged or diverged
236 
237    Level: beginner
238 
239    Notes: this must match finclude/petscsnes.h
240 
241    Developer note: The string versions of these are in
242      src/snes/interface/snes.c called convergedreasons.
243      If these enums are changed you much change those.
244 
245 .seealso: SNESSolve(), SNESGetConvergedReason(), KSPConvergedReason, SNESSetConvergenceTest()
246 E*/
247 typedef enum {/* converged */
248               SNES_CONVERGED_FNORM_ABS         =  2, /* F < F_minabs */
249               SNES_CONVERGED_FNORM_RELATIVE    =  3, /* F < F_mintol*F_initial */
250               SNES_CONVERGED_PNORM_RELATIVE    =  4, /* step size small */
251               SNES_CONVERGED_TR_DELTA          =  7,
252               /* diverged */
253               SNES_DIVERGED_FUNCTION_COUNT     = -2,
254               SNES_DIVERGED_FNORM_NAN          = -4,
255               SNES_DIVERGED_MAX_IT             = -5,
256               SNES_DIVERGED_LS_FAILURE         = -6,
257               SNES_DIVERGED_LOCAL_MIN          = -8,  /* || J^T b || is small, implies converged to local minimum of F() */
258               SNES_CONVERGED_ITERATING         =  0} SNESConvergedReason;
259 
260 EXTERN PetscErrorCode SNESSetConvergenceTest(SNES,PetscErrorCode (*)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void*);
261 EXTERN PetscErrorCode SNESConverged_LS(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*);
262 EXTERN PetscErrorCode SNESConverged_TR(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*);
263 EXTERN PetscErrorCode SNESGetConvergedReason(SNES,SNESConvergedReason*);
264 
265 EXTERN PetscErrorCode SNESDAFormFunction(SNES,Vec,Vec,void*);
266 EXTERN PetscErrorCode SNESDAComputeJacobianWithAdic(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
267 EXTERN PetscErrorCode SNESDAComputeJacobianWithAdifor(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
268 EXTERN PetscErrorCode SNESDAComputeJacobian(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
269 
270 /* --------- Solving systems of nonlinear equations --------------- */
271 EXTERN PetscErrorCode SNESSetFunction(SNES,Vec,PetscErrorCode(*)(SNES,Vec,Vec,void*),void *);
272 EXTERN PetscErrorCode SNESComputeFunction(SNES,Vec,Vec);
273 EXTERN PetscErrorCode SNESSetJacobian(SNES,Mat,Mat,PetscErrorCode(*)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *);
274 EXTERN PetscErrorCode SNESGetJacobian(SNES,Mat*,Mat*,void **,PetscErrorCode(**)(SNES,Vec,Mat*,Mat*,MatStructure*,void*));
275 EXTERN PetscErrorCode SNESDefaultComputeJacobian(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
276 EXTERN PetscErrorCode SNESDefaultComputeJacobianColor(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
277 EXTERN PetscErrorCode SNESSetRhs(SNES,Vec);
278 EXTERN PetscErrorCode SNESSetLineSearch(SNES,PetscErrorCode(*)(SNES,void*,Vec,Vec,Vec,Vec,Vec,PetscReal,PetscReal*,PetscReal*,int*),void*);
279 EXTERN PetscErrorCode SNESNoLineSearch(SNES,void*,Vec,Vec,Vec,Vec,Vec,PetscReal,PetscReal*,PetscReal*,int*);
280 EXTERN PetscErrorCode SNESNoLineSearchNoNorms(SNES,void*,Vec,Vec,Vec,Vec,Vec,PetscReal,PetscReal*,PetscReal*,int*);
281 EXTERN PetscErrorCode SNESCubicLineSearch(SNES,void*,Vec,Vec,Vec,Vec,Vec,PetscReal,PetscReal*,PetscReal*,int*);
282 EXTERN PetscErrorCode SNESQuadraticLineSearch(SNES,void*,Vec,Vec,Vec,Vec,Vec,PetscReal,PetscReal*,PetscReal*,int*);
283 
284 EXTERN PetscErrorCode SNESSetLineSearchCheck(SNES,PetscErrorCode(*)(SNES,void*,Vec,PetscTruth*),void*);
285 EXTERN PetscErrorCode SNESSetLineSearchParams(SNES,PetscReal,PetscReal,PetscReal);
286 EXTERN PetscErrorCode SNESGetLineSearchParams(SNES,PetscReal*,PetscReal*,PetscReal*);
287 
288 EXTERN PetscErrorCode SNESTestLocalMin(SNES snes);
289 
290 /* Should this routine be private? */
291 EXTERN PetscErrorCode SNESComputeJacobian(SNES,Vec,Mat*,Mat*,MatStructure*);
292 
293 PETSC_EXTERN_CXX_END
294 #endif
295