xref: /petsc/src/snes/interface/snes.c (revision 5ed2d596fd9914de76abbaed56c65f4792ae57ae)
173f4d377SMatthew Knepley /*$Id: snes.c,v 1.235 2001/08/21 21:03:49 bsmith Exp $*/
29b94acceSBarry Smith 
3e090d566SSatish Balay #include "src/snes/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
68ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
78ba1e511SMatthew Knepley 
88ba1e511SMatthew Knepley /* Logging support */
98ba1e511SMatthew Knepley int SNES_COOKIE;
108ba1e511SMatthew Knepley int MATSNESMFCTX_COOKIE;
11d5ba7fb7SMatthew Knepley int SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval, SNES_MinimizationFunctionEval, SNES_GradientEval;
12d5ba7fb7SMatthew Knepley int SNES_HessianEval;
1382bf6240SBarry Smith 
144a2ae208SSatish Balay #undef __FUNCT__
15a09944afSBarry Smith #define __FUNCT__ "SNESGetProblemType"
16a09944afSBarry Smith /*@C
17a09944afSBarry Smith    SNESGetProblemType -Indicates if SNES is solving a nonlinear system or a minimization
18a09944afSBarry Smith 
19a09944afSBarry Smith    Not Collective
20a09944afSBarry Smith 
21a09944afSBarry Smith    Input Parameter:
22a09944afSBarry Smith .  SNES - the SNES context
23a09944afSBarry Smith 
24a09944afSBarry Smith    Output Parameter:
25a09944afSBarry Smith .   type - SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
26a09944afSBarry Smith    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
27a09944afSBarry Smith 
28a09944afSBarry Smith    Level: intermediate
29a09944afSBarry Smith 
30a09944afSBarry Smith .keywords: SNES, problem type
31a09944afSBarry Smith 
32a09944afSBarry Smith .seealso: SNESCreate()
33a09944afSBarry Smith @*/
34a09944afSBarry Smith int SNESGetProblemType(SNES snes,SNESProblemType *type)
35a09944afSBarry Smith {
36a09944afSBarry Smith   PetscFunctionBegin;
37a09944afSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
38a09944afSBarry Smith   *type = snes->method_class;
39a09944afSBarry Smith   PetscFunctionReturn(0);
40a09944afSBarry Smith }
41a09944afSBarry Smith 
42a09944afSBarry Smith #undef __FUNCT__
434a2ae208SSatish Balay #define __FUNCT__ "SNESView"
447e2c5f70SBarry Smith /*@C
459b94acceSBarry Smith    SNESView - Prints the SNES data structure.
469b94acceSBarry Smith 
474c49b128SBarry Smith    Collective on SNES
48fee21e36SBarry Smith 
49c7afd0dbSLois Curfman McInnes    Input Parameters:
50c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
51c7afd0dbSLois Curfman McInnes -  viewer - visualization context
52c7afd0dbSLois Curfman McInnes 
539b94acceSBarry Smith    Options Database Key:
54c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
559b94acceSBarry Smith 
569b94acceSBarry Smith    Notes:
579b94acceSBarry Smith    The available visualization contexts include
58b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
59b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
60c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
61c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
62c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
639b94acceSBarry Smith 
643e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
65b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
669b94acceSBarry Smith 
6736851e7fSLois Curfman McInnes    Level: beginner
6836851e7fSLois Curfman McInnes 
699b94acceSBarry Smith .keywords: SNES, view
709b94acceSBarry Smith 
71b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
729b94acceSBarry Smith @*/
73b0a32e0cSBarry Smith int SNESView(SNES snes,PetscViewer viewer)
749b94acceSBarry Smith {
759b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
769b94acceSBarry Smith   int                 ierr;
779b94acceSBarry Smith   SLES                sles;
78454a90a3SBarry Smith   char                *type;
796831982aSBarry Smith   PetscTruth          isascii,isstring;
809b94acceSBarry Smith 
813a40ed3dSBarry Smith   PetscFunctionBegin;
8274679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
83b0a32e0cSBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(snes->comm);
84b0a32e0cSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
856831982aSBarry Smith   PetscCheckSameComm(snes,viewer);
8674679c65SBarry Smith 
87b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
88b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
890f5bd95cSBarry Smith   if (isascii) {
903a7fca6bSBarry Smith     if (snes->prefix) {
913a7fca6bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr);
923a7fca6bSBarry Smith     } else {
93b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
943a7fca6bSBarry Smith     }
95454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
96454a90a3SBarry Smith     if (type) {
97b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
98184914b5SBarry Smith     } else {
99b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
100184914b5SBarry Smith     }
1010ef38995SBarry Smith     if (snes->view) {
102b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1030ef38995SBarry Smith       ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr);
104b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1050ef38995SBarry Smith     }
106b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
10777d8c4bbSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, solution=%g\n",
10877d8c4bbSBarry Smith                  snes->rtol,snes->atol,snes->xtol);CHKERRQ(ierr);
109b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
110b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
1110ef38995SBarry Smith     if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
112b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr);
1130ef38995SBarry Smith     }
1149b94acceSBarry Smith     if (snes->ksp_ewconv) {
1159b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
1169b94acceSBarry Smith       if (kctx) {
117b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
118b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
119b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1209b94acceSBarry Smith       }
1219b94acceSBarry Smith     }
1220f5bd95cSBarry Smith   } else if (isstring) {
123454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
124b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
12519bcc07fSBarry Smith   }
12677ed5343SBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
127b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1289b94acceSBarry Smith   ierr = SLESView(sles,viewer);CHKERRQ(ierr);
129b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1303a40ed3dSBarry Smith   PetscFunctionReturn(0);
1319b94acceSBarry Smith }
1329b94acceSBarry Smith 
13376b2cf59SMatthew Knepley /*
13476b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
13576b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
13676b2cf59SMatthew Knepley */
13776b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
13876b2cf59SMatthew Knepley static int numberofsetfromoptions;
13976b2cf59SMatthew Knepley static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
14076b2cf59SMatthew Knepley 
141e74ef692SMatthew Knepley #undef __FUNCT__
142e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
14376b2cf59SMatthew Knepley /*@
14476b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
14576b2cf59SMatthew Knepley 
14676b2cf59SMatthew Knepley   Not Collective
14776b2cf59SMatthew Knepley 
14876b2cf59SMatthew Knepley   Input Parameter:
14976b2cf59SMatthew Knepley . snescheck - function that checks for options
15076b2cf59SMatthew Knepley 
15176b2cf59SMatthew Knepley   Level: developer
15276b2cf59SMatthew Knepley 
15376b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
15476b2cf59SMatthew Knepley @*/
15576b2cf59SMatthew Knepley int SNESAddOptionsChecker(int (*snescheck)(SNES))
15676b2cf59SMatthew Knepley {
15776b2cf59SMatthew Knepley   PetscFunctionBegin;
15876b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
15976b2cf59SMatthew Knepley     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %d allowed", MAXSETFROMOPTIONS);
16076b2cf59SMatthew Knepley   }
16176b2cf59SMatthew Knepley 
16276b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
16376b2cf59SMatthew Knepley   PetscFunctionReturn(0);
16476b2cf59SMatthew Knepley }
16576b2cf59SMatthew Knepley 
1664a2ae208SSatish Balay #undef __FUNCT__
1674a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1689b94acceSBarry Smith /*@
169682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
1709b94acceSBarry Smith 
171c7afd0dbSLois Curfman McInnes    Collective on SNES
172c7afd0dbSLois Curfman McInnes 
1739b94acceSBarry Smith    Input Parameter:
1749b94acceSBarry Smith .  snes - the SNES context
1759b94acceSBarry Smith 
17636851e7fSLois Curfman McInnes    Options Database Keys:
1776831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
17882738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
17982738288SBarry Smith                 of the change in the solution between steps
180b39c3a46SLois Curfman McInnes .  -snes_atol <atol> - absolute tolerance of residual norm
181b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
182b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
183b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
18450ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
185b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
18682738288SBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear or minimization
18782738288SBarry Smith                                solver; hence iterations will continue until max_it
1881fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
18982738288SBarry Smith                                of convergence test
19082738288SBarry Smith .  -snes_monitor - prints residual norm at each iteration
191d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
192d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
19382738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
194e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
19536851e7fSLois Curfman McInnes -  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
19682738288SBarry Smith 
19782738288SBarry Smith     Options Database for Eisenstat-Walker method:
19882738288SBarry Smith +  -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence
19982738288SBarry Smith .  -snes_ksp_eq_version ver - version of  Eisenstat-Walker method
20036851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
20136851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
20236851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
20336851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
20436851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
20536851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
20682738288SBarry Smith 
20711ca99fdSLois Curfman McInnes    Notes:
20811ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
20911ca99fdSLois Curfman McInnes    the users manual.
21083e2fdc7SBarry Smith 
21136851e7fSLois Curfman McInnes    Level: beginner
21236851e7fSLois Curfman McInnes 
2139b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2149b94acceSBarry Smith 
21569ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
2169b94acceSBarry Smith @*/
2179b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
2189b94acceSBarry Smith {
2199b94acceSBarry Smith   SLES                sles;
220186905e3SBarry Smith   SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
221f1af5d2fSBarry Smith   PetscTruth          flg;
22276b2cf59SMatthew Knepley   int                 ierr, i;
223186905e3SBarry Smith   char                *deft,type[256];
2249b94acceSBarry Smith 
2253a40ed3dSBarry Smith   PetscFunctionBegin;
22677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
227ca161407SBarry Smith 
228b0a32e0cSBarry Smith   ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
229186905e3SBarry Smith     if (snes->type_name) {
230186905e3SBarry Smith       deft = snes->type_name;
231186905e3SBarry Smith     } else {
232186905e3SBarry Smith       if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
233186905e3SBarry Smith         deft = SNESEQLS;
234186905e3SBarry Smith       } else {
235186905e3SBarry Smith         deft = SNESUMTR;
236d64ed03dSBarry Smith       }
237d64ed03dSBarry Smith     }
2384bbc92c1SBarry Smith 
239186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
240b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
241d64ed03dSBarry Smith     if (flg) {
242186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
243186905e3SBarry Smith     } else if (!snes->type_name) {
244186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
245d64ed03dSBarry Smith     }
246909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
24793c39befSBarry Smith 
24887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
24987828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->atol,&snes->atol,0);CHKERRQ(ierr);
250186905e3SBarry Smith 
25187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
252b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
253b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
25450ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
25587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_fmin","Minimization function tolerance","SNESSetMinimizationFunctionTolerance",snes->fmin,&snes->fmin,0);CHKERRQ(ierr);
256186905e3SBarry Smith 
257b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_ksp_ew_conv","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetParametersEW",&snes->ksp_ewconv);CHKERRQ(ierr);
258186905e3SBarry Smith 
259b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1 or 2","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
26087828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
26187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
26287828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
26387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
26487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
26587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
266186905e3SBarry Smith 
267b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr);
26893c39befSBarry Smith     if (flg) {snes->converged = 0;}
269b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_cancelmonitors","Remove all monitors","SNESClearMonitor",&flg);CHKERRQ(ierr);
2705cd90555SBarry Smith     if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
271b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_monitor","Monitor norm of function","SNESDefaultMonitor",&flg);CHKERRQ(ierr);
272b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);}
2733a7fca6bSBarry Smith     ierr = PetscOptionsName("-snes_ratiomonitor","Monitor norm of function","SNESSetRatioMonitor",&flg);CHKERRQ(ierr);
2743a7fca6bSBarry Smith     if (flg) {ierr = SNESSetRatioMonitor(snes);CHKERRQ(ierr);}
275b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_smonitor","Monitor norm of function (fewer digits)","SNESDefaultSMonitor",&flg);CHKERRQ(ierr);
276b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);}
277b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor","Plot solution at each iteration","SNESVecViewMonitor",&flg);CHKERRQ(ierr);
278b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);}
279b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_update","Plot correction at each iteration","SNESVecViewUpdateMonitor",&flg);CHKERRQ(ierr);
2807c922b88SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewUpdateMonitor,0,0);CHKERRQ(ierr);}
281*5ed2d596SBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_residual","Plot residual at each iteration","SNESVecViewResidualMonitor",&flg);CHKERRQ(ierr);
282*5ed2d596SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewResidualMonitor,0,0);CHKERRQ(ierr);}
283b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_xmonitor","Plot function norm at each iteration","SNESLGMonitor",&flg);CHKERRQ(ierr);
284186905e3SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
285e24b481bSBarry Smith 
286b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
2873c7409f5SSatish Balay     if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
288186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
289b0a32e0cSBarry Smith       PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
290981c4779SBarry Smith     } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
291186905e3SBarry Smith       ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr);
292b0a32e0cSBarry Smith       PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
2939b94acceSBarry Smith     }
294639f9d9dSBarry Smith 
29576b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
29676b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);                                                             CHKERRQ(ierr);
29776b2cf59SMatthew Knepley     }
29876b2cf59SMatthew Knepley 
299186905e3SBarry Smith     if (snes->setfromoptions) {
300186905e3SBarry Smith       ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
301639f9d9dSBarry Smith     }
302639f9d9dSBarry Smith 
303b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3044bbc92c1SBarry Smith 
3059b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
3069b94acceSBarry Smith   ierr = SLESSetFromOptions(sles);CHKERRQ(ierr);
30793993e2dSLois Curfman McInnes 
3083a40ed3dSBarry Smith   PetscFunctionReturn(0);
3099b94acceSBarry Smith }
3109b94acceSBarry Smith 
311a847f771SSatish Balay 
3124a2ae208SSatish Balay #undef __FUNCT__
3134a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
3149b94acceSBarry Smith /*@
3159b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3169b94acceSBarry Smith    the nonlinear solvers.
3179b94acceSBarry Smith 
318fee21e36SBarry Smith    Collective on SNES
319fee21e36SBarry Smith 
320c7afd0dbSLois Curfman McInnes    Input Parameters:
321c7afd0dbSLois Curfman McInnes +  snes - the SNES context
322c7afd0dbSLois Curfman McInnes -  usrP - optional user context
323c7afd0dbSLois Curfman McInnes 
32436851e7fSLois Curfman McInnes    Level: intermediate
32536851e7fSLois Curfman McInnes 
3269b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3279b94acceSBarry Smith 
3289b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3299b94acceSBarry Smith @*/
3309b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
3319b94acceSBarry Smith {
3323a40ed3dSBarry Smith   PetscFunctionBegin;
33377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3349b94acceSBarry Smith   snes->user		= usrP;
3353a40ed3dSBarry Smith   PetscFunctionReturn(0);
3369b94acceSBarry Smith }
33774679c65SBarry Smith 
3384a2ae208SSatish Balay #undef __FUNCT__
3394a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3409b94acceSBarry Smith /*@C
3419b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3429b94acceSBarry Smith    nonlinear solvers.
3439b94acceSBarry Smith 
344c7afd0dbSLois Curfman McInnes    Not Collective
345c7afd0dbSLois Curfman McInnes 
3469b94acceSBarry Smith    Input Parameter:
3479b94acceSBarry Smith .  snes - SNES context
3489b94acceSBarry Smith 
3499b94acceSBarry Smith    Output Parameter:
3509b94acceSBarry Smith .  usrP - user context
3519b94acceSBarry Smith 
35236851e7fSLois Curfman McInnes    Level: intermediate
35336851e7fSLois Curfman McInnes 
3549b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3559b94acceSBarry Smith 
3569b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3579b94acceSBarry Smith @*/
3589b94acceSBarry Smith int SNESGetApplicationContext(SNES snes,void **usrP)
3599b94acceSBarry Smith {
3603a40ed3dSBarry Smith   PetscFunctionBegin;
36177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3629b94acceSBarry Smith   *usrP = snes->user;
3633a40ed3dSBarry Smith   PetscFunctionReturn(0);
3649b94acceSBarry Smith }
36574679c65SBarry Smith 
3664a2ae208SSatish Balay #undef __FUNCT__
3674a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3689b94acceSBarry Smith /*@
369c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
370c8228a4eSBarry Smith    at this time.
3719b94acceSBarry Smith 
372c7afd0dbSLois Curfman McInnes    Not Collective
373c7afd0dbSLois Curfman McInnes 
3749b94acceSBarry Smith    Input Parameter:
3759b94acceSBarry Smith .  snes - SNES context
3769b94acceSBarry Smith 
3779b94acceSBarry Smith    Output Parameter:
3789b94acceSBarry Smith .  iter - iteration number
3799b94acceSBarry Smith 
380c8228a4eSBarry Smith    Notes:
381c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
382c8228a4eSBarry Smith 
383c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
38408405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
38508405cd6SLois Curfman McInnes .vb
38608405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
38708405cd6SLois Curfman McInnes       if (!(it % 2)) {
38808405cd6SLois Curfman McInnes         [compute Jacobian here]
38908405cd6SLois Curfman McInnes       }
39008405cd6SLois Curfman McInnes .ve
391c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
39208405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
393c8228a4eSBarry Smith 
39436851e7fSLois Curfman McInnes    Level: intermediate
39536851e7fSLois Curfman McInnes 
3969b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3979b94acceSBarry Smith @*/
3989b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3999b94acceSBarry Smith {
4003a40ed3dSBarry Smith   PetscFunctionBegin;
40177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
40274679c65SBarry Smith   PetscValidIntPointer(iter);
4039b94acceSBarry Smith   *iter = snes->iter;
4043a40ed3dSBarry Smith   PetscFunctionReturn(0);
4059b94acceSBarry Smith }
40674679c65SBarry Smith 
4074a2ae208SSatish Balay #undef __FUNCT__
4084a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
4099b94acceSBarry Smith /*@
4109b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4119b94acceSBarry Smith    with SNESSSetFunction().
4129b94acceSBarry Smith 
413c7afd0dbSLois Curfman McInnes    Collective on SNES
414c7afd0dbSLois Curfman McInnes 
4159b94acceSBarry Smith    Input Parameter:
4169b94acceSBarry Smith .  snes - SNES context
4179b94acceSBarry Smith 
4189b94acceSBarry Smith    Output Parameter:
4199b94acceSBarry Smith .  fnorm - 2-norm of function
4209b94acceSBarry Smith 
4219b94acceSBarry Smith    Note:
4229b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
423a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
424a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
4259b94acceSBarry Smith 
42636851e7fSLois Curfman McInnes    Level: intermediate
42736851e7fSLois Curfman McInnes 
4289b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
429a86d99e1SLois Curfman McInnes 
43008405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
4319b94acceSBarry Smith @*/
43287828ca2SBarry Smith int SNESGetFunctionNorm(SNES snes,PetscScalar *fnorm)
4339b94acceSBarry Smith {
4343a40ed3dSBarry Smith   PetscFunctionBegin;
43577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
43674679c65SBarry Smith   PetscValidScalarPointer(fnorm);
43774679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
43829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"For SNES_NONLINEAR_EQUATIONS only");
43974679c65SBarry Smith   }
4409b94acceSBarry Smith   *fnorm = snes->norm;
4413a40ed3dSBarry Smith   PetscFunctionReturn(0);
4429b94acceSBarry Smith }
44374679c65SBarry Smith 
4444a2ae208SSatish Balay #undef __FUNCT__
4454a2ae208SSatish Balay #define __FUNCT__ "SNESGetGradientNorm"
4469b94acceSBarry Smith /*@
4479b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
4489b94acceSBarry Smith    with SNESSSetGradient().
4499b94acceSBarry Smith 
450c7afd0dbSLois Curfman McInnes    Collective on SNES
451c7afd0dbSLois Curfman McInnes 
4529b94acceSBarry Smith    Input Parameter:
4539b94acceSBarry Smith .  snes - SNES context
4549b94acceSBarry Smith 
4559b94acceSBarry Smith    Output Parameter:
4569b94acceSBarry Smith .  fnorm - 2-norm of gradient
4579b94acceSBarry Smith 
4589b94acceSBarry Smith    Note:
4599b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
460a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
461a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
4629b94acceSBarry Smith 
46336851e7fSLois Curfman McInnes    Level: intermediate
46436851e7fSLois Curfman McInnes 
4659b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
466a86d99e1SLois Curfman McInnes 
467a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
4689b94acceSBarry Smith @*/
46987828ca2SBarry Smith int SNESGetGradientNorm(SNES snes,PetscScalar *gnorm)
4709b94acceSBarry Smith {
4713a40ed3dSBarry Smith   PetscFunctionBegin;
47277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
47374679c65SBarry Smith   PetscValidScalarPointer(gnorm);
47474679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
47529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"For SNES_UNCONSTRAINED_MINIMIZATION only");
47674679c65SBarry Smith   }
4779b94acceSBarry Smith   *gnorm = snes->norm;
4783a40ed3dSBarry Smith   PetscFunctionReturn(0);
4799b94acceSBarry Smith }
48074679c65SBarry Smith 
4814a2ae208SSatish Balay #undef __FUNCT__
4824a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps"
4839b94acceSBarry Smith /*@
4849b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
4859b94acceSBarry Smith    attempted by the nonlinear solver.
4869b94acceSBarry Smith 
487c7afd0dbSLois Curfman McInnes    Not Collective
488c7afd0dbSLois Curfman McInnes 
4899b94acceSBarry Smith    Input Parameter:
4909b94acceSBarry Smith .  snes - SNES context
4919b94acceSBarry Smith 
4929b94acceSBarry Smith    Output Parameter:
4939b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4949b94acceSBarry Smith 
495c96a6f78SLois Curfman McInnes    Notes:
496c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
497c96a6f78SLois Curfman McInnes 
49836851e7fSLois Curfman McInnes    Level: intermediate
49936851e7fSLois Curfman McInnes 
5009b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
5019b94acceSBarry Smith @*/
5029b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
5039b94acceSBarry Smith {
5043a40ed3dSBarry Smith   PetscFunctionBegin;
50577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
50674679c65SBarry Smith   PetscValidIntPointer(nfails);
50750ffb88aSMatthew Knepley   *nfails = snes->numFailures;
50850ffb88aSMatthew Knepley   PetscFunctionReturn(0);
50950ffb88aSMatthew Knepley }
51050ffb88aSMatthew Knepley 
51150ffb88aSMatthew Knepley #undef __FUNCT__
51250ffb88aSMatthew Knepley #define __FUNCT__ "SNESSetMaximumUnsuccessfulSteps"
51350ffb88aSMatthew Knepley /*@
51450ffb88aSMatthew Knepley    SNESSetMaximumUnsuccessfulSteps - Sets the maximum number of unsuccessful steps
51550ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
51650ffb88aSMatthew Knepley 
51750ffb88aSMatthew Knepley    Not Collective
51850ffb88aSMatthew Knepley 
51950ffb88aSMatthew Knepley    Input Parameters:
52050ffb88aSMatthew Knepley +  snes     - SNES context
52150ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
52250ffb88aSMatthew Knepley 
52350ffb88aSMatthew Knepley    Level: intermediate
52450ffb88aSMatthew Knepley 
52550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
52650ffb88aSMatthew Knepley @*/
52750ffb88aSMatthew Knepley int SNESSetMaximumUnsuccessfulSteps(SNES snes, int maxFails)
52850ffb88aSMatthew Knepley {
52950ffb88aSMatthew Knepley   PetscFunctionBegin;
53050ffb88aSMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE);
53150ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
53250ffb88aSMatthew Knepley   PetscFunctionReturn(0);
53350ffb88aSMatthew Knepley }
53450ffb88aSMatthew Knepley 
53550ffb88aSMatthew Knepley #undef __FUNCT__
53650ffb88aSMatthew Knepley #define __FUNCT__ "SNESGetMaximumUnsuccessfulSteps"
53750ffb88aSMatthew Knepley /*@
53850ffb88aSMatthew Knepley    SNESGetMaximumUnsuccessfulSteps - Gets the maximum number of unsuccessful steps
53950ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
54050ffb88aSMatthew Knepley 
54150ffb88aSMatthew Knepley    Not Collective
54250ffb88aSMatthew Knepley 
54350ffb88aSMatthew Knepley    Input Parameter:
54450ffb88aSMatthew Knepley .  snes     - SNES context
54550ffb88aSMatthew Knepley 
54650ffb88aSMatthew Knepley    Output Parameter:
54750ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
54850ffb88aSMatthew Knepley 
54950ffb88aSMatthew Knepley    Level: intermediate
55050ffb88aSMatthew Knepley 
55150ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
55250ffb88aSMatthew Knepley @*/
55350ffb88aSMatthew Knepley int SNESGetMaximumUnsuccessfulSteps(SNES snes, int *maxFails)
55450ffb88aSMatthew Knepley {
55550ffb88aSMatthew Knepley   PetscFunctionBegin;
55650ffb88aSMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE);
55750ffb88aSMatthew Knepley   PetscValidIntPointer(maxFails);
55850ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
5593a40ed3dSBarry Smith   PetscFunctionReturn(0);
5609b94acceSBarry Smith }
561a847f771SSatish Balay 
5624a2ae208SSatish Balay #undef __FUNCT__
5634a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations"
564c96a6f78SLois Curfman McInnes /*@
565c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
566c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
567c96a6f78SLois Curfman McInnes 
568c7afd0dbSLois Curfman McInnes    Not Collective
569c7afd0dbSLois Curfman McInnes 
570c96a6f78SLois Curfman McInnes    Input Parameter:
571c96a6f78SLois Curfman McInnes .  snes - SNES context
572c96a6f78SLois Curfman McInnes 
573c96a6f78SLois Curfman McInnes    Output Parameter:
574c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
575c96a6f78SLois Curfman McInnes 
576c96a6f78SLois Curfman McInnes    Notes:
577c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
578c96a6f78SLois Curfman McInnes 
57936851e7fSLois Curfman McInnes    Level: intermediate
58036851e7fSLois Curfman McInnes 
581c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
582c96a6f78SLois Curfman McInnes @*/
58386bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
584c96a6f78SLois Curfman McInnes {
5853a40ed3dSBarry Smith   PetscFunctionBegin;
586c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
587c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
588c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5893a40ed3dSBarry Smith   PetscFunctionReturn(0);
590c96a6f78SLois Curfman McInnes }
591c96a6f78SLois Curfman McInnes 
5924a2ae208SSatish Balay #undef __FUNCT__
5934a2ae208SSatish Balay #define __FUNCT__ "SNESGetSLES"
5949b94acceSBarry Smith /*@C
5959b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
5969b94acceSBarry Smith 
597c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
598c7afd0dbSLois Curfman McInnes 
5999b94acceSBarry Smith    Input Parameter:
6009b94acceSBarry Smith .  snes - the SNES context
6019b94acceSBarry Smith 
6029b94acceSBarry Smith    Output Parameter:
6039b94acceSBarry Smith .  sles - the SLES context
6049b94acceSBarry Smith 
6059b94acceSBarry Smith    Notes:
6069b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
6079b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
6089b94acceSBarry Smith    KSP and PC contexts as well.
6099b94acceSBarry Smith 
61036851e7fSLois Curfman McInnes    Level: beginner
61136851e7fSLois Curfman McInnes 
6129b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
6139b94acceSBarry Smith 
6149b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
6159b94acceSBarry Smith @*/
6169b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
6179b94acceSBarry Smith {
6183a40ed3dSBarry Smith   PetscFunctionBegin;
61977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
6209b94acceSBarry Smith   *sles = snes->sles;
6213a40ed3dSBarry Smith   PetscFunctionReturn(0);
6229b94acceSBarry Smith }
62382bf6240SBarry Smith 
6244a2ae208SSatish Balay #undef __FUNCT__
6254a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
626454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj)
627e24b481bSBarry Smith {
628aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
629454a90a3SBarry Smith   SNES          v = (SNES) obj;
630e24b481bSBarry Smith   int          ierr;
63143d6d2cbSBarry Smith #endif
632e24b481bSBarry Smith 
633e24b481bSBarry Smith   PetscFunctionBegin;
634e24b481bSBarry Smith 
63543d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
636e24b481bSBarry Smith   /* if it is already published then return */
637e24b481bSBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
638e24b481bSBarry Smith 
639454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
640e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
641e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
642e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
643e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
644454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
645433994e6SBarry Smith #endif
646e24b481bSBarry Smith   PetscFunctionReturn(0);
647e24b481bSBarry Smith }
648e24b481bSBarry Smith 
6499b94acceSBarry Smith /* -----------------------------------------------------------*/
6504a2ae208SSatish Balay #undef __FUNCT__
6514a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
6529b94acceSBarry Smith /*@C
6539b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
6549b94acceSBarry Smith 
655c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
656c7afd0dbSLois Curfman McInnes 
657c7afd0dbSLois Curfman McInnes    Input Parameters:
658c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
659c7afd0dbSLois Curfman McInnes -  type - type of method, either
660c7afd0dbSLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
661c7afd0dbSLois Curfman McInnes    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
6629b94acceSBarry Smith 
6639b94acceSBarry Smith    Output Parameter:
6649b94acceSBarry Smith .  outsnes - the new SNES context
6659b94acceSBarry Smith 
666c7afd0dbSLois Curfman McInnes    Options Database Keys:
667c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
668c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
669c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
670c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
671c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
672c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
673c1f60f51SBarry Smith 
67436851e7fSLois Curfman McInnes    Level: beginner
67536851e7fSLois Curfman McInnes 
6769b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
6779b94acceSBarry Smith 
678435da068SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNESProblemType, SNES
6799b94acceSBarry Smith @*/
6804b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
6819b94acceSBarry Smith {
6829b94acceSBarry Smith   int                 ierr;
6839b94acceSBarry Smith   SNES                snes;
6849b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
68537fcc0dbSBarry Smith 
6863a40ed3dSBarry Smith   PetscFunctionBegin;
6878ba1e511SMatthew Knepley   PetscValidPointer(outsnes);
6888ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
6898ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
6908ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);                                                               CHKERRQ(ierr);
6918ba1e511SMatthew Knepley #endif
6928ba1e511SMatthew Knepley 
693d64ed03dSBarry Smith   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){
69429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"incorrect method type");
695d64ed03dSBarry Smith   }
6963f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
697b0a32e0cSBarry Smith   PetscLogObjectCreate(snes);
698e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6999b94acceSBarry Smith   snes->max_its           = 50;
7009750a799SBarry Smith   snes->max_funcs	  = 10000;
7019b94acceSBarry Smith   snes->norm		  = 0.0;
7025a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
703b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
704b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
7059b94acceSBarry Smith     snes->atol		  = 1.e-10;
706d64ed03dSBarry Smith   } else {
707b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
708b4874afaSBarry Smith     snes->ttol            = 0.0;
709b4874afaSBarry Smith     snes->atol		  = 1.e-50;
710b4874afaSBarry Smith   }
7119b94acceSBarry Smith   snes->xtol		  = 1.e-8;
7129b94acceSBarry Smith   snes->nfuncs            = 0;
71350ffb88aSMatthew Knepley   snes->numFailures       = 0;
71450ffb88aSMatthew Knepley   snes->maxFailures       = 1;
7157a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
716639f9d9dSBarry Smith   snes->numbermonitors    = 0;
7179b94acceSBarry Smith   snes->data              = 0;
7189b94acceSBarry Smith   snes->view              = 0;
7199b94acceSBarry Smith   snes->computeumfunction = 0;
7209b94acceSBarry Smith   snes->umfunP            = 0;
7219b94acceSBarry Smith   snes->fc                = 0;
7229b94acceSBarry Smith   snes->deltatol          = 1.e-12;
7239b94acceSBarry Smith   snes->fmin              = -1.e30;
7249b94acceSBarry Smith   snes->method_class      = type;
7259b94acceSBarry Smith   snes->set_method_called = 0;
72682bf6240SBarry Smith   snes->setupcalled       = 0;
727186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
7286f24a144SLois Curfman McInnes   snes->vwork             = 0;
7296f24a144SLois Curfman McInnes   snes->nwork             = 0;
730758f92a0SBarry Smith   snes->conv_hist_len     = 0;
731758f92a0SBarry Smith   snes->conv_hist_max     = 0;
732758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
733758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
734758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
735184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
7369b94acceSBarry Smith 
7379b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
738b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
739b0a32e0cSBarry Smith   PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
7409b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7419b94acceSBarry Smith   kctx->version     = 2;
7429b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7439b94acceSBarry Smith                              this was too large for some test cases */
7449b94acceSBarry Smith   kctx->rtol_last   = 0;
7459b94acceSBarry Smith   kctx->rtol_max    = .9;
7469b94acceSBarry Smith   kctx->gamma       = 1.0;
7479b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7489b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7499b94acceSBarry Smith   kctx->threshold   = .1;
7509b94acceSBarry Smith   kctx->lresid_last = 0;
7519b94acceSBarry Smith   kctx->norm_last   = 0;
7529b94acceSBarry Smith 
7539b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr);
754b0a32e0cSBarry Smith   PetscLogObjectParent(snes,snes->sles)
7555334005bSBarry Smith 
7569b94acceSBarry Smith   *outsnes = snes;
75700036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
7583a40ed3dSBarry Smith   PetscFunctionReturn(0);
7599b94acceSBarry Smith }
7609b94acceSBarry Smith 
7619b94acceSBarry Smith /* --------------------------------------------------------------- */
7624a2ae208SSatish Balay #undef __FUNCT__
7634a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
7649b94acceSBarry Smith /*@C
7659b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
7669b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
7679b94acceSBarry Smith    equations.
7689b94acceSBarry Smith 
769fee21e36SBarry Smith    Collective on SNES
770fee21e36SBarry Smith 
771c7afd0dbSLois Curfman McInnes    Input Parameters:
772c7afd0dbSLois Curfman McInnes +  snes - the SNES context
773c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
774c7afd0dbSLois Curfman McInnes .  r - vector to store function value
775c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
776c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7779b94acceSBarry Smith 
778c7afd0dbSLois Curfman McInnes    Calling sequence of func:
7798d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
780c7afd0dbSLois Curfman McInnes 
781313e4042SLois Curfman McInnes .  f - function vector
782c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
7839b94acceSBarry Smith 
7849b94acceSBarry Smith    Notes:
7859b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
7869b94acceSBarry Smith $      f'(x) x = -f(x),
787c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
7889b94acceSBarry Smith 
7899b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7909b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7919b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
7929b94acceSBarry Smith 
79336851e7fSLois Curfman McInnes    Level: beginner
79436851e7fSLois Curfman McInnes 
7959b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
7969b94acceSBarry Smith 
797a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
7989b94acceSBarry Smith @*/
7995334005bSBarry Smith int SNESSetFunction(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
8009b94acceSBarry Smith {
8013a40ed3dSBarry Smith   PetscFunctionBegin;
80277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
803184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
804184914b5SBarry Smith   PetscCheckSameComm(snes,r);
805a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
80629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
807a8c6a408SBarry Smith   }
808184914b5SBarry Smith 
8099b94acceSBarry Smith   snes->computefunction     = func;
8109b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
8119b94acceSBarry Smith   snes->funP                = ctx;
8123a40ed3dSBarry Smith   PetscFunctionReturn(0);
8139b94acceSBarry Smith }
8149b94acceSBarry Smith 
8154a2ae208SSatish Balay #undef __FUNCT__
8164a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
8179b94acceSBarry Smith /*@
81836851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
8199b94acceSBarry Smith                          SNESSetFunction().
8209b94acceSBarry Smith 
821c7afd0dbSLois Curfman McInnes    Collective on SNES
822c7afd0dbSLois Curfman McInnes 
8239b94acceSBarry Smith    Input Parameters:
824c7afd0dbSLois Curfman McInnes +  snes - the SNES context
825c7afd0dbSLois Curfman McInnes -  x - input vector
8269b94acceSBarry Smith 
8279b94acceSBarry Smith    Output Parameter:
8283638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
8299b94acceSBarry Smith 
8301bffabb2SLois Curfman McInnes    Notes:
8319b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
8329b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
8339b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
8349b94acceSBarry Smith 
83536851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
83636851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
83736851e7fSLois Curfman McInnes    themselves.
83836851e7fSLois Curfman McInnes 
83936851e7fSLois Curfman McInnes    Level: developer
84036851e7fSLois Curfman McInnes 
8419b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
8429b94acceSBarry Smith 
843a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
8449b94acceSBarry Smith @*/
8459b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x,Vec y)
8469b94acceSBarry Smith {
8479b94acceSBarry Smith   int    ierr;
8489b94acceSBarry Smith 
8493a40ed3dSBarry Smith   PetscFunctionBegin;
850184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
851184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
852184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
853184914b5SBarry Smith   PetscCheckSameComm(snes,x);
854184914b5SBarry Smith   PetscCheckSameComm(snes,y);
855d4bb536fSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
85629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
857d4bb536fSBarry Smith   }
858184914b5SBarry Smith 
859d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
860d64ed03dSBarry Smith   PetscStackPush("SNES user function");
8619b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
862d64ed03dSBarry Smith   PetscStackPop;
863ae3c334cSLois Curfman McInnes   snes->nfuncs++;
864d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
8653a40ed3dSBarry Smith   PetscFunctionReturn(0);
8669b94acceSBarry Smith }
8679b94acceSBarry Smith 
8684a2ae208SSatish Balay #undef __FUNCT__
8694a2ae208SSatish Balay #define __FUNCT__ "SNESSetMinimizationFunction"
8709b94acceSBarry Smith /*@C
8719b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
8729b94acceSBarry Smith    unconstrained minimization.
8739b94acceSBarry Smith 
874fee21e36SBarry Smith    Collective on SNES
875fee21e36SBarry Smith 
876c7afd0dbSLois Curfman McInnes    Input Parameters:
877c7afd0dbSLois Curfman McInnes +  snes - the SNES context
878c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
879c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
880c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8819b94acceSBarry Smith 
882c7afd0dbSLois Curfman McInnes    Calling sequence of func:
883329f5518SBarry Smith $     func (SNES snes,Vec x,PetscReal *f,void *ctx);
884c7afd0dbSLois Curfman McInnes 
885c7afd0dbSLois Curfman McInnes +  x - input vector
8869b94acceSBarry Smith .  f - function
887c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined function context
8889b94acceSBarry Smith 
88936851e7fSLois Curfman McInnes    Level: beginner
89036851e7fSLois Curfman McInnes 
8919b94acceSBarry Smith    Notes:
8929b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
8939b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
8949b94acceSBarry Smith    SNESSetFunction().
8959b94acceSBarry Smith 
8969b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
8979b94acceSBarry Smith 
898a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
899a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
9009b94acceSBarry Smith @*/
901329f5518SBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,PetscReal*,void*),void *ctx)
9029b94acceSBarry Smith {
9033a40ed3dSBarry Smith   PetscFunctionBegin;
90477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
905a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
90629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
907a8c6a408SBarry Smith   }
9089b94acceSBarry Smith   snes->computeumfunction   = func;
9099b94acceSBarry Smith   snes->umfunP              = ctx;
9103a40ed3dSBarry Smith   PetscFunctionReturn(0);
9119b94acceSBarry Smith }
9129b94acceSBarry Smith 
9134a2ae208SSatish Balay #undef __FUNCT__
9144a2ae208SSatish Balay #define __FUNCT__ "SNESComputeMinimizationFunction"
9159b94acceSBarry Smith /*@
9169b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
9179b94acceSBarry Smith    set with SNESSetMinimizationFunction().
9189b94acceSBarry Smith 
919c7afd0dbSLois Curfman McInnes    Collective on SNES
920c7afd0dbSLois Curfman McInnes 
9219b94acceSBarry Smith    Input Parameters:
922c7afd0dbSLois Curfman McInnes +  snes - the SNES context
923c7afd0dbSLois Curfman McInnes -  x - input vector
9249b94acceSBarry Smith 
9259b94acceSBarry Smith    Output Parameter:
9269b94acceSBarry Smith .  y - function value
9279b94acceSBarry Smith 
9289b94acceSBarry Smith    Notes:
9299b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
9309b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
9319b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
932a86d99e1SLois Curfman McInnes 
93336851e7fSLois Curfman McInnes    SNESComputeMinimizationFunction() is typically used within minimization
93436851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
93536851e7fSLois Curfman McInnes    themselves.
93636851e7fSLois Curfman McInnes 
93736851e7fSLois Curfman McInnes    Level: developer
93836851e7fSLois Curfman McInnes 
939a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
940a86d99e1SLois Curfman McInnes 
941a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
942a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
9439b94acceSBarry Smith @*/
944329f5518SBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,PetscReal *y)
9459b94acceSBarry Smith {
9469b94acceSBarry Smith   int    ierr;
9473a40ed3dSBarry Smith 
9483a40ed3dSBarry Smith   PetscFunctionBegin;
949184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
950184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
951184914b5SBarry Smith   PetscCheckSameComm(snes,x);
952a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
95329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
954a8c6a408SBarry Smith   }
955184914b5SBarry Smith 
956d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);CHKERRQ(ierr);
957d64ed03dSBarry Smith   PetscStackPush("SNES user minimzation function");
9589b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr);
959d64ed03dSBarry Smith   PetscStackPop;
960ae3c334cSLois Curfman McInnes   snes->nfuncs++;
961d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);CHKERRQ(ierr);
9623a40ed3dSBarry Smith   PetscFunctionReturn(0);
9639b94acceSBarry Smith }
9649b94acceSBarry Smith 
9654a2ae208SSatish Balay #undef __FUNCT__
9664a2ae208SSatish Balay #define __FUNCT__ "SNESSetGradient"
9679b94acceSBarry Smith /*@C
9689b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
9699b94acceSBarry Smith    vector for use by the SNES routines.
9709b94acceSBarry Smith 
971c7afd0dbSLois Curfman McInnes    Collective on SNES
972c7afd0dbSLois Curfman McInnes 
9739b94acceSBarry Smith    Input Parameters:
974c7afd0dbSLois Curfman McInnes +  snes - the SNES context
9759b94acceSBarry Smith .  func - function evaluation routine
976044dda88SLois Curfman McInnes .  ctx - optional user-defined context for private data for the
977044dda88SLois Curfman McInnes          gradient evaluation routine (may be PETSC_NULL)
978c7afd0dbSLois Curfman McInnes -  r - vector to store gradient value
979fee21e36SBarry Smith 
9809b94acceSBarry Smith    Calling sequence of func:
9818d76a1e5SLois Curfman McInnes $     func (SNES, Vec x, Vec g, void *ctx);
9829b94acceSBarry Smith 
983c7afd0dbSLois Curfman McInnes +  x - input vector
9849b94acceSBarry Smith .  g - gradient vector
985c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined gradient context
9869b94acceSBarry Smith 
9879b94acceSBarry Smith    Notes:
9889b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
9899b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
9909b94acceSBarry Smith    SNESSetFunction().
9919b94acceSBarry Smith 
99236851e7fSLois Curfman McInnes    Level: beginner
99336851e7fSLois Curfman McInnes 
9949b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9959b94acceSBarry Smith 
996a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
997a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
9989b94acceSBarry Smith @*/
99974679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
10009b94acceSBarry Smith {
10013a40ed3dSBarry Smith   PetscFunctionBegin;
100277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1003184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
1004184914b5SBarry Smith   PetscCheckSameComm(snes,r);
1005a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
100629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1007a8c6a408SBarry Smith   }
10089b94acceSBarry Smith   snes->computefunction     = func;
10099b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
10109b94acceSBarry Smith   snes->funP                = ctx;
10113a40ed3dSBarry Smith   PetscFunctionReturn(0);
10129b94acceSBarry Smith }
10139b94acceSBarry Smith 
10144a2ae208SSatish Balay #undef __FUNCT__
10154a2ae208SSatish Balay #define __FUNCT__ "SNESComputeGradient"
10169b94acceSBarry Smith /*@
1017a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
1018a86d99e1SLois Curfman McInnes    SNESSetGradient().
10199b94acceSBarry Smith 
1020c7afd0dbSLois Curfman McInnes    Collective on SNES
1021c7afd0dbSLois Curfman McInnes 
10229b94acceSBarry Smith    Input Parameters:
1023c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1024c7afd0dbSLois Curfman McInnes -  x - input vector
10259b94acceSBarry Smith 
10269b94acceSBarry Smith    Output Parameter:
10279b94acceSBarry Smith .  y - gradient vector
10289b94acceSBarry Smith 
10299b94acceSBarry Smith    Notes:
10309b94acceSBarry Smith    SNESComputeGradient() is valid only for
10319b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
10329b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
1033a86d99e1SLois Curfman McInnes 
103436851e7fSLois Curfman McInnes    SNESComputeGradient() is typically used within minimization
103536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
103636851e7fSLois Curfman McInnes    themselves.
103736851e7fSLois Curfman McInnes 
103836851e7fSLois Curfman McInnes    Level: developer
103936851e7fSLois Curfman McInnes 
1040a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
1041a86d99e1SLois Curfman McInnes 
1042a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
1043a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
10449b94acceSBarry Smith @*/
10459b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x,Vec y)
10469b94acceSBarry Smith {
10479b94acceSBarry Smith   int    ierr;
10483a40ed3dSBarry Smith 
10493a40ed3dSBarry Smith   PetscFunctionBegin;
1050184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1051184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1052184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
1053184914b5SBarry Smith   PetscCheckSameComm(snes,x);
1054184914b5SBarry Smith   PetscCheckSameComm(snes,y);
10553a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
105629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
10573a40ed3dSBarry Smith   }
1058d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_GradientEval,snes,x,y,0);CHKERRQ(ierr);
1059d64ed03dSBarry Smith   PetscStackPush("SNES user gradient function");
10609b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1061d64ed03dSBarry Smith   PetscStackPop;
1062d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_GradientEval,snes,x,y,0);CHKERRQ(ierr);
10633a40ed3dSBarry Smith   PetscFunctionReturn(0);
10649b94acceSBarry Smith }
10659b94acceSBarry Smith 
10664a2ae208SSatish Balay #undef __FUNCT__
10674a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
106862fef451SLois Curfman McInnes /*@
106962fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
107062fef451SLois Curfman McInnes    set with SNESSetJacobian().
107162fef451SLois Curfman McInnes 
1072c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1073c7afd0dbSLois Curfman McInnes 
107462fef451SLois Curfman McInnes    Input Parameters:
1075c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1076c7afd0dbSLois Curfman McInnes -  x - input vector
107762fef451SLois Curfman McInnes 
107862fef451SLois Curfman McInnes    Output Parameters:
1079c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
108062fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1081c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1082fee21e36SBarry Smith 
108362fef451SLois Curfman McInnes    Notes:
108462fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
108562fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
108662fef451SLois Curfman McInnes 
1087dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1088dc5a77f8SLois Curfman McInnes    flag parameter.
108962fef451SLois Curfman McInnes 
109062fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
109162fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
1092005c665bSBarry Smith    methods is SNESComputeHessian().
109362fef451SLois Curfman McInnes 
109436851e7fSLois Curfman McInnes    Level: developer
109536851e7fSLois Curfman McInnes 
109662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
109762fef451SLois Curfman McInnes 
109862fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
109962fef451SLois Curfman McInnes @*/
11009b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
11019b94acceSBarry Smith {
11029b94acceSBarry Smith   int    ierr;
11033a40ed3dSBarry Smith 
11043a40ed3dSBarry Smith   PetscFunctionBegin;
1105184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1106184914b5SBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE);
1107184914b5SBarry Smith   PetscCheckSameComm(snes,X);
11083a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
110929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
11103a40ed3dSBarry Smith   }
11113a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
1112d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1113c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1114d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
11159b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1116d64ed03dSBarry Smith   PetscStackPop;
1117d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
11186d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
111977c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
112077c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11213a40ed3dSBarry Smith   PetscFunctionReturn(0);
11229b94acceSBarry Smith }
11239b94acceSBarry Smith 
11244a2ae208SSatish Balay #undef __FUNCT__
11254a2ae208SSatish Balay #define __FUNCT__ "SNESComputeHessian"
112662fef451SLois Curfman McInnes /*@
112762fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
112862fef451SLois Curfman McInnes    set with SNESSetHessian().
112962fef451SLois Curfman McInnes 
1130c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1131c7afd0dbSLois Curfman McInnes 
113262fef451SLois Curfman McInnes    Input Parameters:
1133c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1134c7afd0dbSLois Curfman McInnes -  x - input vector
113562fef451SLois Curfman McInnes 
113662fef451SLois Curfman McInnes    Output Parameters:
1137c7afd0dbSLois Curfman McInnes +  A - Hessian matrix
113862fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1139c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1140fee21e36SBarry Smith 
114162fef451SLois Curfman McInnes    Notes:
114262fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
114362fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
114462fef451SLois Curfman McInnes 
1145dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1146dc5a77f8SLois Curfman McInnes    flag parameter.
114762fef451SLois Curfman McInnes 
114862fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
114962fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
115062fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
115162fef451SLois Curfman McInnes 
115236851e7fSLois Curfman McInnes    SNESComputeHessian() is typically used within minimization
115336851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
115436851e7fSLois Curfman McInnes    themselves.
115536851e7fSLois Curfman McInnes 
115636851e7fSLois Curfman McInnes    Level: developer
115736851e7fSLois Curfman McInnes 
115862fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
115962fef451SLois Curfman McInnes 
1160a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
1161a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
116262fef451SLois Curfman McInnes @*/
116362fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
11649b94acceSBarry Smith {
11659b94acceSBarry Smith   int    ierr;
11663a40ed3dSBarry Smith 
11673a40ed3dSBarry Smith   PetscFunctionBegin;
1168184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1169184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1170184914b5SBarry Smith   PetscCheckSameComm(snes,x);
11713a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
117229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
11733a40ed3dSBarry Smith   }
11743a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
1175d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_HessianEval,snes,x,*A,*B);CHKERRQ(ierr);
11760f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
1177d64ed03dSBarry Smith   PetscStackPush("SNES user Hessian function");
117862fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr);
1179d64ed03dSBarry Smith   PetscStackPop;
1180d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_HessianEval,snes,x,*A,*B);CHKERRQ(ierr);
11810f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
118277c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
118377c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11843a40ed3dSBarry Smith   PetscFunctionReturn(0);
11859b94acceSBarry Smith }
11869b94acceSBarry Smith 
11874a2ae208SSatish Balay #undef __FUNCT__
11884a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
11899b94acceSBarry Smith /*@C
11909b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1191044dda88SLois Curfman McInnes    location to store the matrix.
11929b94acceSBarry Smith 
1193c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1194c7afd0dbSLois Curfman McInnes 
11959b94acceSBarry Smith    Input Parameters:
1196c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11979b94acceSBarry Smith .  A - Jacobian matrix
11989b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11999b94acceSBarry Smith .  func - Jacobian evaluation routine
1200c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
12012cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
12029b94acceSBarry Smith 
12039b94acceSBarry Smith    Calling sequence of func:
12048d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12059b94acceSBarry Smith 
1206c7afd0dbSLois Curfman McInnes +  x - input vector
12079b94acceSBarry Smith .  A - Jacobian matrix
12089b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1209ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1210ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1211c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
12129b94acceSBarry Smith 
12139b94acceSBarry Smith    Notes:
1214dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
12152cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1216ac21db08SLois Curfman McInnes 
1217ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
12189b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
12199b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12209b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12219b94acceSBarry Smith    throughout the global iterations.
12229b94acceSBarry Smith 
122336851e7fSLois Curfman McInnes    Level: beginner
122436851e7fSLois Curfman McInnes 
12259b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
12269b94acceSBarry Smith 
1227ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
12289b94acceSBarry Smith @*/
1229454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
12309b94acceSBarry Smith {
12313a7fca6bSBarry Smith   int ierr;
12323a7fca6bSBarry Smith 
12333a40ed3dSBarry Smith   PetscFunctionBegin;
123477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
123500036973SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE);
123600036973SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE);
123700036973SBarry Smith   if (A) PetscCheckSameComm(snes,A);
123800036973SBarry Smith   if (B) PetscCheckSameComm(snes,B);
1239a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
124029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
1241a8c6a408SBarry Smith   }
1242184914b5SBarry Smith 
12433a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
12443a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
12453a7fca6bSBarry Smith   if (A) {
12463a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12479b94acceSBarry Smith     snes->jacobian = A;
12483a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
12493a7fca6bSBarry Smith   }
12503a7fca6bSBarry Smith   if (B) {
12513a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12529b94acceSBarry Smith     snes->jacobian_pre = B;
12533a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
12543a7fca6bSBarry Smith   }
12553a40ed3dSBarry Smith   PetscFunctionReturn(0);
12569b94acceSBarry Smith }
125762fef451SLois Curfman McInnes 
12584a2ae208SSatish Balay #undef __FUNCT__
12594a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1260c2aafc4cSSatish Balay /*@C
1261b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1262b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1263b4fd4287SBarry Smith 
1264c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1265c7afd0dbSLois Curfman McInnes 
1266b4fd4287SBarry Smith    Input Parameter:
1267b4fd4287SBarry Smith .  snes - the nonlinear solver context
1268b4fd4287SBarry Smith 
1269b4fd4287SBarry Smith    Output Parameters:
1270c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1271b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
127200036973SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
127300036973SBarry Smith -  func - location to put Jacobian function (or PETSC_NULL)
1274fee21e36SBarry Smith 
127536851e7fSLois Curfman McInnes    Level: advanced
127636851e7fSLois Curfman McInnes 
1277b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1278b4fd4287SBarry Smith @*/
127900036973SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx,int (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*))
1280b4fd4287SBarry Smith {
12813a40ed3dSBarry Smith   PetscFunctionBegin;
1282184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
12833a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
128429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
12853a40ed3dSBarry Smith   }
1286b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1287b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1288b4fd4287SBarry Smith   if (ctx)  *ctx  = snes->jacP;
128900036973SBarry Smith   if (func) *func = snes->computejacobian;
12903a40ed3dSBarry Smith   PetscFunctionReturn(0);
1291b4fd4287SBarry Smith }
1292b4fd4287SBarry Smith 
12934a2ae208SSatish Balay #undef __FUNCT__
12944a2ae208SSatish Balay #define __FUNCT__ "SNESSetHessian"
12959b94acceSBarry Smith /*@C
12969b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
1297044dda88SLois Curfman McInnes    location to store the matrix.
12989b94acceSBarry Smith 
1299c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1300c7afd0dbSLois Curfman McInnes 
13019b94acceSBarry Smith    Input Parameters:
1302c7afd0dbSLois Curfman McInnes +  snes - the SNES context
13039b94acceSBarry Smith .  A - Hessian matrix
13049b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
13059b94acceSBarry Smith .  func - Jacobian evaluation routine
1306c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1307313e4042SLois Curfman McInnes          Hessian evaluation routine (may be PETSC_NULL)
13089b94acceSBarry Smith 
13099b94acceSBarry Smith    Calling sequence of func:
13108d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
13119b94acceSBarry Smith 
1312c7afd0dbSLois Curfman McInnes +  x - input vector
13139b94acceSBarry Smith .  A - Hessian matrix
13149b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1315ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1316ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1317c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Hessian context
13189b94acceSBarry Smith 
13199b94acceSBarry Smith    Notes:
1320dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
13212cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1322ac21db08SLois Curfman McInnes 
13239b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
13249b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
13259b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
13269b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
13279b94acceSBarry Smith    throughout the global iterations.
13289b94acceSBarry Smith 
132936851e7fSLois Curfman McInnes    Level: beginner
133036851e7fSLois Curfman McInnes 
13319b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
13329b94acceSBarry Smith 
1333ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
13349b94acceSBarry Smith @*/
1335454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
13369b94acceSBarry Smith {
13373a7fca6bSBarry Smith   int ierr;
13383a7fca6bSBarry Smith 
13393a40ed3dSBarry Smith   PetscFunctionBegin;
134077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1341184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
1342184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
1343184914b5SBarry Smith   PetscCheckSameComm(snes,A);
1344184914b5SBarry Smith   PetscCheckSameComm(snes,B);
1345d4bb536fSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
134629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1347d4bb536fSBarry Smith   }
13483a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
13493a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
13503a7fca6bSBarry Smith   if (A) {
13513a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13529b94acceSBarry Smith     snes->jacobian = A;
13533a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
13543a7fca6bSBarry Smith   }
13553a7fca6bSBarry Smith   if (B) {
13563a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13579b94acceSBarry Smith     snes->jacobian_pre = B;
13583a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
13593a7fca6bSBarry Smith   }
13603a40ed3dSBarry Smith   PetscFunctionReturn(0);
13619b94acceSBarry Smith }
13629b94acceSBarry Smith 
13634a2ae208SSatish Balay #undef __FUNCT__
13644a2ae208SSatish Balay #define __FUNCT__ "SNESGetHessian"
136562fef451SLois Curfman McInnes /*@
136662fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
136762fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
136862fef451SLois Curfman McInnes 
1369c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object is parallel if SNES object is parallel
1370c7afd0dbSLois Curfman McInnes 
137162fef451SLois Curfman McInnes    Input Parameter:
137262fef451SLois Curfman McInnes .  snes - the nonlinear solver context
137362fef451SLois Curfman McInnes 
137462fef451SLois Curfman McInnes    Output Parameters:
1375c7afd0dbSLois Curfman McInnes +  A - location to stash Hessian matrix (or PETSC_NULL)
137662fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
1377c7afd0dbSLois Curfman McInnes -  ctx - location to stash Hessian ctx (or PETSC_NULL)
1378fee21e36SBarry Smith 
137936851e7fSLois Curfman McInnes    Level: advanced
138036851e7fSLois Curfman McInnes 
138162fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
1382c7afd0dbSLois Curfman McInnes 
1383c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian
138462fef451SLois Curfman McInnes @*/
138562fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B,void **ctx)
138662fef451SLois Curfman McInnes {
13873a40ed3dSBarry Smith   PetscFunctionBegin;
1388184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
13893a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){
139029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
13913a40ed3dSBarry Smith   }
139262fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
139362fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
139462fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
13953a40ed3dSBarry Smith   PetscFunctionReturn(0);
139662fef451SLois Curfman McInnes }
139762fef451SLois Curfman McInnes 
13989b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13999b94acceSBarry Smith 
14004a2ae208SSatish Balay #undef __FUNCT__
14014a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
14029b94acceSBarry Smith /*@
14039b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1404272ac6f2SLois Curfman McInnes    of a nonlinear solver.
14059b94acceSBarry Smith 
1406fee21e36SBarry Smith    Collective on SNES
1407fee21e36SBarry Smith 
1408c7afd0dbSLois Curfman McInnes    Input Parameters:
1409c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1410c7afd0dbSLois Curfman McInnes -  x - the solution vector
1411c7afd0dbSLois Curfman McInnes 
1412272ac6f2SLois Curfman McInnes    Notes:
1413272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1414272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1415272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1416272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1417272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1418272ac6f2SLois Curfman McInnes 
141936851e7fSLois Curfman McInnes    Level: advanced
142036851e7fSLois Curfman McInnes 
14219b94acceSBarry Smith .keywords: SNES, nonlinear, setup
14229b94acceSBarry Smith 
14239b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
14249b94acceSBarry Smith @*/
14258ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
14269b94acceSBarry Smith {
1427f1af5d2fSBarry Smith   int        ierr;
1428f1af5d2fSBarry Smith   PetscTruth flg;
14293a40ed3dSBarry Smith 
14303a40ed3dSBarry Smith   PetscFunctionBegin;
143177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
143277c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1433184914b5SBarry Smith   PetscCheckSameComm(snes,x);
14348ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
14359b94acceSBarry Smith 
1436b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1437c1f60f51SBarry Smith   /*
1438c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1439dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1440c1f60f51SBarry Smith   */
1441c1f60f51SBarry Smith   if (flg) {
1442c1f60f51SBarry Smith     Mat J;
14435a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
14445a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
14453a7fca6bSBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator routines\n");
14463a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
14473a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1448c1f60f51SBarry Smith   }
1449b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1450c1f60f51SBarry Smith   /*
1451dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1452c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1453c1f60f51SBarry Smith    */
145431615d04SLois Curfman McInnes   if (flg) {
1455272ac6f2SLois Curfman McInnes     Mat  J;
1456f3ef73ceSBarry Smith     SLES sles;
1457f3ef73ceSBarry Smith     PC   pc;
1458f3ef73ceSBarry Smith 
14595a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
14603a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
146193b98531SBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator and preconditioner routines\n");
146283e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
14633a7fca6bSBarry Smith       ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
1464d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
14653a7fca6bSBarry Smith       ierr = SNESSetHessian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
1466d4bb536fSBarry Smith     } else {
146729bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Method class doesn't support matrix-free option");
1468d4bb536fSBarry Smith     }
14693a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
14703a7fca6bSBarry Smith 
1471f3ef73ceSBarry Smith     /* force no preconditioner */
1472f3ef73ceSBarry Smith     ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1473f3ef73ceSBarry Smith     ierr = SLESGetPC(sles,&pc);CHKERRQ(ierr);
1474f3ef73ceSBarry Smith     ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1475272ac6f2SLois Curfman McInnes   }
1476f3ef73ceSBarry Smith 
14779b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
14786831982aSBarry Smith     PetscTruth iseqtr;
14796831982aSBarry Smith 
148029bbc08cSBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
148129bbc08cSBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
148229bbc08cSBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1483a8c6a408SBarry Smith     if (snes->vec_func == snes->vec_sol) {
148429bbc08cSBarry Smith       SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1485a8c6a408SBarry Smith     }
1486a703fe33SLois Curfman McInnes 
1487a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
14886831982aSBarry Smith     ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr);
14896831982aSBarry Smith     if (snes->ksp_ewconv && !iseqtr) {
1490a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1491a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1492a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
1493186905e3SBarry Smith       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
1494a703fe33SLois Curfman McInnes     }
14959b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
149629bbc08cSBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetGradient() first");
149729bbc08cSBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetGradient() first");
1498a8c6a408SBarry Smith     if (!snes->computeumfunction) {
149929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetMinimizationFunction() first");
1500a8c6a408SBarry Smith     }
150129bbc08cSBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetHessian()");
1502d4bb536fSBarry Smith   } else {
150329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown method class");
1504d4bb536fSBarry Smith   }
1505a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
150682bf6240SBarry Smith   snes->setupcalled = 1;
15073a40ed3dSBarry Smith   PetscFunctionReturn(0);
15089b94acceSBarry Smith }
15099b94acceSBarry Smith 
15104a2ae208SSatish Balay #undef __FUNCT__
15114a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
15129b94acceSBarry Smith /*@C
15139b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
15149b94acceSBarry Smith    with SNESCreate().
15159b94acceSBarry Smith 
1516c7afd0dbSLois Curfman McInnes    Collective on SNES
1517c7afd0dbSLois Curfman McInnes 
15189b94acceSBarry Smith    Input Parameter:
15199b94acceSBarry Smith .  snes - the SNES context
15209b94acceSBarry Smith 
152136851e7fSLois Curfman McInnes    Level: beginner
152236851e7fSLois Curfman McInnes 
15239b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
15249b94acceSBarry Smith 
152563a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
15269b94acceSBarry Smith @*/
15279b94acceSBarry Smith int SNESDestroy(SNES snes)
15289b94acceSBarry Smith {
1529b8a78c4aSBarry Smith   int i,ierr;
15303a40ed3dSBarry Smith 
15313a40ed3dSBarry Smith   PetscFunctionBegin;
153277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15333a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1534d4bb536fSBarry Smith 
1535be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
15360f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1537be0abb6dSBarry Smith 
1538e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1539606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
15403a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
15413a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
15429b94acceSBarry Smith   ierr = SLESDestroy(snes->sles);CHKERRQ(ierr);
1543522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1544b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1545b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1546b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1547b8a78c4aSBarry Smith     }
1548b8a78c4aSBarry Smith   }
1549b0a32e0cSBarry Smith   PetscLogObjectDestroy((PetscObject)snes);
15500452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
15513a40ed3dSBarry Smith   PetscFunctionReturn(0);
15529b94acceSBarry Smith }
15539b94acceSBarry Smith 
15549b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
15559b94acceSBarry Smith 
15564a2ae208SSatish Balay #undef __FUNCT__
15574a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15589b94acceSBarry Smith /*@
1559d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15609b94acceSBarry Smith 
1561c7afd0dbSLois Curfman McInnes    Collective on SNES
1562c7afd0dbSLois Curfman McInnes 
15639b94acceSBarry Smith    Input Parameters:
1564c7afd0dbSLois Curfman McInnes +  snes - the SNES context
156533174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
156633174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
156733174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
156833174efeSLois Curfman McInnes            of the change in the solution between steps
156933174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1570c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1571fee21e36SBarry Smith 
157233174efeSLois Curfman McInnes    Options Database Keys:
1573c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1574c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1575c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1576c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1577c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15789b94acceSBarry Smith 
1579d7a720efSLois Curfman McInnes    Notes:
15809b94acceSBarry Smith    The default maximum number of iterations is 50.
15819b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15829b94acceSBarry Smith 
158336851e7fSLois Curfman McInnes    Level: intermediate
158436851e7fSLois Curfman McInnes 
158533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15869b94acceSBarry Smith 
1587d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
15889b94acceSBarry Smith @*/
1589329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf)
15909b94acceSBarry Smith {
15913a40ed3dSBarry Smith   PetscFunctionBegin;
159277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1593d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1594d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1595d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1596d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1597d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15983a40ed3dSBarry Smith   PetscFunctionReturn(0);
15999b94acceSBarry Smith }
16009b94acceSBarry Smith 
16014a2ae208SSatish Balay #undef __FUNCT__
16024a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
16039b94acceSBarry Smith /*@
160433174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
160533174efeSLois Curfman McInnes 
1606c7afd0dbSLois Curfman McInnes    Not Collective
1607c7afd0dbSLois Curfman McInnes 
160833174efeSLois Curfman McInnes    Input Parameters:
1609c7afd0dbSLois Curfman McInnes +  snes - the SNES context
161033174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
161133174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
161233174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
161333174efeSLois Curfman McInnes            of the change in the solution between steps
161433174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1615c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1616fee21e36SBarry Smith 
161733174efeSLois Curfman McInnes    Notes:
161833174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
161933174efeSLois Curfman McInnes 
162036851e7fSLois Curfman McInnes    Level: intermediate
162136851e7fSLois Curfman McInnes 
162233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
162333174efeSLois Curfman McInnes 
162433174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
162533174efeSLois Curfman McInnes @*/
1626329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf)
162733174efeSLois Curfman McInnes {
16283a40ed3dSBarry Smith   PetscFunctionBegin;
162933174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
163033174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
163133174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
163233174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
163333174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
163433174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
16353a40ed3dSBarry Smith   PetscFunctionReturn(0);
163633174efeSLois Curfman McInnes }
163733174efeSLois Curfman McInnes 
16384a2ae208SSatish Balay #undef __FUNCT__
16394a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
164033174efeSLois Curfman McInnes /*@
16419b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16429b94acceSBarry Smith 
1643fee21e36SBarry Smith    Collective on SNES
1644fee21e36SBarry Smith 
1645c7afd0dbSLois Curfman McInnes    Input Parameters:
1646c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1647c7afd0dbSLois Curfman McInnes -  tol - tolerance
1648c7afd0dbSLois Curfman McInnes 
16499b94acceSBarry Smith    Options Database Key:
1650c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16519b94acceSBarry Smith 
165236851e7fSLois Curfman McInnes    Level: intermediate
165336851e7fSLois Curfman McInnes 
16549b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16559b94acceSBarry Smith 
1656d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
16579b94acceSBarry Smith @*/
1658329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16599b94acceSBarry Smith {
16603a40ed3dSBarry Smith   PetscFunctionBegin;
166177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16629b94acceSBarry Smith   snes->deltatol = tol;
16633a40ed3dSBarry Smith   PetscFunctionReturn(0);
16649b94acceSBarry Smith }
16659b94acceSBarry Smith 
16664a2ae208SSatish Balay #undef __FUNCT__
16674a2ae208SSatish Balay #define __FUNCT__ "SNESSetMinimizationFunctionTolerance"
16689b94acceSBarry Smith /*@
166977c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
16709b94acceSBarry Smith    for unconstrained minimization solvers.
16719b94acceSBarry Smith 
1672fee21e36SBarry Smith    Collective on SNES
1673fee21e36SBarry Smith 
1674c7afd0dbSLois Curfman McInnes    Input Parameters:
1675c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1676c7afd0dbSLois Curfman McInnes -  ftol - minimum function tolerance
1677c7afd0dbSLois Curfman McInnes 
16789b94acceSBarry Smith    Options Database Key:
1679c7afd0dbSLois Curfman McInnes .  -snes_fmin <ftol> - Sets ftol
16809b94acceSBarry Smith 
16819b94acceSBarry Smith    Note:
168277c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
16839b94acceSBarry Smith    methods only.
16849b94acceSBarry Smith 
168536851e7fSLois Curfman McInnes    Level: intermediate
168636851e7fSLois Curfman McInnes 
16879b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
16889b94acceSBarry Smith 
1689d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
16909b94acceSBarry Smith @*/
1691329f5518SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,PetscReal ftol)
16929b94acceSBarry Smith {
16933a40ed3dSBarry Smith   PetscFunctionBegin;
169477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16959b94acceSBarry Smith   snes->fmin = ftol;
16963a40ed3dSBarry Smith   PetscFunctionReturn(0);
16979b94acceSBarry Smith }
1698df9fa365SBarry Smith /*
1699df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1700df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1701df9fa365SBarry Smith    macros instead of functions
1702df9fa365SBarry Smith */
17034a2ae208SSatish Balay #undef __FUNCT__
17044a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitor"
1705329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx)
1706ce1608b8SBarry Smith {
1707ce1608b8SBarry Smith   int ierr;
1708ce1608b8SBarry Smith 
1709ce1608b8SBarry Smith   PetscFunctionBegin;
1710184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1711ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1712ce1608b8SBarry Smith   PetscFunctionReturn(0);
1713ce1608b8SBarry Smith }
1714ce1608b8SBarry Smith 
17154a2ae208SSatish Balay #undef __FUNCT__
17164a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorCreate"
1717b0a32e0cSBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,PetscDrawLG *draw)
1718df9fa365SBarry Smith {
1719df9fa365SBarry Smith   int ierr;
1720df9fa365SBarry Smith 
1721df9fa365SBarry Smith   PetscFunctionBegin;
1722df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1723df9fa365SBarry Smith   PetscFunctionReturn(0);
1724df9fa365SBarry Smith }
1725df9fa365SBarry Smith 
17264a2ae208SSatish Balay #undef __FUNCT__
17274a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorDestroy"
1728b0a32e0cSBarry Smith int SNESLGMonitorDestroy(PetscDrawLG draw)
1729df9fa365SBarry Smith {
1730df9fa365SBarry Smith   int ierr;
1731df9fa365SBarry Smith 
1732df9fa365SBarry Smith   PetscFunctionBegin;
1733df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1734df9fa365SBarry Smith   PetscFunctionReturn(0);
1735df9fa365SBarry Smith }
1736df9fa365SBarry Smith 
17379b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
17389b94acceSBarry Smith 
17394a2ae208SSatish Balay #undef __FUNCT__
17404a2ae208SSatish Balay #define __FUNCT__ "SNESSetMonitor"
17419b94acceSBarry Smith /*@C
17425cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
17439b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
17449b94acceSBarry Smith    progress.
17459b94acceSBarry Smith 
1746fee21e36SBarry Smith    Collective on SNES
1747fee21e36SBarry Smith 
1748c7afd0dbSLois Curfman McInnes    Input Parameters:
1749c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1750c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1751b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1752b3006f0bSLois Curfman McInnes           monitor routine (use PETSC_NULL if no context is desitre)
1753b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1754b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
17559b94acceSBarry Smith 
1756c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1757329f5518SBarry Smith $     int func(SNES snes,int its, PetscReal norm,void *mctx)
1758c7afd0dbSLois Curfman McInnes 
1759c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1760c7afd0dbSLois Curfman McInnes .    its - iteration number
1761c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
1762c7afd0dbSLois Curfman McInnes             for SNES_NONLINEAR_EQUATIONS methods
176340a0c1c6SLois Curfman McInnes .    norm - 2-norm gradient value (may be estimated)
1764c7afd0dbSLois Curfman McInnes             for SNES_UNCONSTRAINED_MINIMIZATION methods
176540a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
17669b94acceSBarry Smith 
17679665c990SLois Curfman McInnes    Options Database Keys:
1768c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1769c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1770c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1771c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1772c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1773c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1774c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1775c7afd0dbSLois Curfman McInnes                             the options database.
17769665c990SLois Curfman McInnes 
1777639f9d9dSBarry Smith    Notes:
17786bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
17796bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
17806bc08f3fSLois Curfman McInnes    order in which they were set.
1781639f9d9dSBarry Smith 
178236851e7fSLois Curfman McInnes    Level: intermediate
178336851e7fSLois Curfman McInnes 
17849b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
17859b94acceSBarry Smith 
17865cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
17879b94acceSBarry Smith @*/
1788329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *))
17899b94acceSBarry Smith {
17903a40ed3dSBarry Smith   PetscFunctionBegin;
1791184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1792639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
179329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1794639f9d9dSBarry Smith   }
1795639f9d9dSBarry Smith 
1796639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1797b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1798639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
17993a40ed3dSBarry Smith   PetscFunctionReturn(0);
18009b94acceSBarry Smith }
18019b94acceSBarry Smith 
18024a2ae208SSatish Balay #undef __FUNCT__
18034a2ae208SSatish Balay #define __FUNCT__ "SNESClearMonitor"
18045cd90555SBarry Smith /*@C
18055cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
18065cd90555SBarry Smith 
1807c7afd0dbSLois Curfman McInnes    Collective on SNES
1808c7afd0dbSLois Curfman McInnes 
18095cd90555SBarry Smith    Input Parameters:
18105cd90555SBarry Smith .  snes - the SNES context
18115cd90555SBarry Smith 
18125cd90555SBarry Smith    Options Database:
1813c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1814c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1815c7afd0dbSLois Curfman McInnes     set via the options database
18165cd90555SBarry Smith 
18175cd90555SBarry Smith    Notes:
18185cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
18195cd90555SBarry Smith 
182036851e7fSLois Curfman McInnes    Level: intermediate
182136851e7fSLois Curfman McInnes 
18225cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
18235cd90555SBarry Smith 
18245cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
18255cd90555SBarry Smith @*/
18265cd90555SBarry Smith int SNESClearMonitor(SNES snes)
18275cd90555SBarry Smith {
18285cd90555SBarry Smith   PetscFunctionBegin;
1829184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18305cd90555SBarry Smith   snes->numbermonitors = 0;
18315cd90555SBarry Smith   PetscFunctionReturn(0);
18325cd90555SBarry Smith }
18335cd90555SBarry Smith 
18344a2ae208SSatish Balay #undef __FUNCT__
18354a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
18369b94acceSBarry Smith /*@C
18379b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
18389b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
18399b94acceSBarry Smith 
1840fee21e36SBarry Smith    Collective on SNES
1841fee21e36SBarry Smith 
1842c7afd0dbSLois Curfman McInnes    Input Parameters:
1843c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1844c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1845c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1846c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
18479b94acceSBarry Smith 
1848c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1849329f5518SBarry Smith $     int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1850c7afd0dbSLois Curfman McInnes 
1851c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1852c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1853184914b5SBarry Smith .    reason - reason for convergence/divergence
1854c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1855c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods)
1856c7afd0dbSLois Curfman McInnes .    f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods)
1857c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods)
1858c7afd0dbSLois Curfman McInnes -    f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods)
18599b94acceSBarry Smith 
186036851e7fSLois Curfman McInnes    Level: advanced
186136851e7fSLois Curfman McInnes 
18629b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
18639b94acceSBarry Smith 
186440191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
186540191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
18669b94acceSBarry Smith @*/
1867329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
18689b94acceSBarry Smith {
18693a40ed3dSBarry Smith   PetscFunctionBegin;
1870184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18719b94acceSBarry Smith   (snes)->converged = func;
18729b94acceSBarry Smith   (snes)->cnvP      = cctx;
18733a40ed3dSBarry Smith   PetscFunctionReturn(0);
18749b94acceSBarry Smith }
18759b94acceSBarry Smith 
18764a2ae208SSatish Balay #undef __FUNCT__
18774a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
1878184914b5SBarry Smith /*@C
1879184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1880184914b5SBarry Smith 
1881184914b5SBarry Smith    Not Collective
1882184914b5SBarry Smith 
1883184914b5SBarry Smith    Input Parameter:
1884184914b5SBarry Smith .  snes - the SNES context
1885184914b5SBarry Smith 
1886184914b5SBarry Smith    Output Parameter:
1887e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1888184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1889184914b5SBarry Smith 
1890184914b5SBarry Smith    Level: intermediate
1891184914b5SBarry Smith 
1892184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1893184914b5SBarry Smith 
1894184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1895184914b5SBarry Smith 
1896184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
1897435da068SBarry Smith           SNESConverged_UM_LS(), SNESConverged_UM_TR(), SNESConvergedReason
1898184914b5SBarry Smith @*/
1899184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1900184914b5SBarry Smith {
1901184914b5SBarry Smith   PetscFunctionBegin;
1902184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1903184914b5SBarry Smith   *reason = snes->reason;
1904184914b5SBarry Smith   PetscFunctionReturn(0);
1905184914b5SBarry Smith }
1906184914b5SBarry Smith 
19074a2ae208SSatish Balay #undef __FUNCT__
19084a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1909c9005455SLois Curfman McInnes /*@
1910c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1911c9005455SLois Curfman McInnes 
1912fee21e36SBarry Smith    Collective on SNES
1913fee21e36SBarry Smith 
1914c7afd0dbSLois Curfman McInnes    Input Parameters:
1915c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1916c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1917cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1918758f92a0SBarry Smith .  na  - size of a and its
191964731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1920758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1921c7afd0dbSLois Curfman McInnes 
1922c9005455SLois Curfman McInnes    Notes:
1923c9005455SLois Curfman McInnes    If set, this array will contain the function norms (for
1924c9005455SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods) or gradient norms
1925c9005455SLois Curfman McInnes    (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed
1926c9005455SLois Curfman McInnes    at each step.
1927c9005455SLois Curfman McInnes 
1928c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1929c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1930c9005455SLois Curfman McInnes    during the section of code that is being timed.
1931c9005455SLois Curfman McInnes 
193236851e7fSLois Curfman McInnes    Level: intermediate
193336851e7fSLois Curfman McInnes 
1934c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1935758f92a0SBarry Smith 
193608405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1937758f92a0SBarry Smith 
1938c9005455SLois Curfman McInnes @*/
1939329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset)
1940c9005455SLois Curfman McInnes {
19413a40ed3dSBarry Smith   PetscFunctionBegin;
1942c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1943c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1944c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1945758f92a0SBarry Smith   snes->conv_hist_its   = its;
1946758f92a0SBarry Smith   snes->conv_hist_max   = na;
1947758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1948758f92a0SBarry Smith   PetscFunctionReturn(0);
1949758f92a0SBarry Smith }
1950758f92a0SBarry Smith 
19514a2ae208SSatish Balay #undef __FUNCT__
19524a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
19530c4c9dddSBarry Smith /*@C
1954758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1955758f92a0SBarry Smith 
1956758f92a0SBarry Smith    Collective on SNES
1957758f92a0SBarry Smith 
1958758f92a0SBarry Smith    Input Parameter:
1959758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1960758f92a0SBarry Smith 
1961758f92a0SBarry Smith    Output Parameters:
1962758f92a0SBarry Smith .  a   - array to hold history
1963758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1964758f92a0SBarry Smith          negative if not converged) for each solve.
1965758f92a0SBarry Smith -  na  - size of a and its
1966758f92a0SBarry Smith 
1967758f92a0SBarry Smith    Notes:
1968758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1969758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1970758f92a0SBarry Smith 
1971758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1972758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1973758f92a0SBarry Smith    during the section of code that is being timed.
1974758f92a0SBarry Smith 
1975758f92a0SBarry Smith    Level: intermediate
1976758f92a0SBarry Smith 
1977758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1978758f92a0SBarry Smith 
1979758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1980758f92a0SBarry Smith 
1981758f92a0SBarry Smith @*/
1982329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na)
1983758f92a0SBarry Smith {
1984758f92a0SBarry Smith   PetscFunctionBegin;
1985758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1986758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1987758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1988758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
19893a40ed3dSBarry Smith   PetscFunctionReturn(0);
1990c9005455SLois Curfman McInnes }
1991c9005455SLois Curfman McInnes 
1992e74ef692SMatthew Knepley #undef __FUNCT__
1993e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetRhsBC"
199476b2cf59SMatthew Knepley /*@
199576b2cf59SMatthew Knepley   SNESSetRhsBC - Sets the function which applies boundary conditions
199676b2cf59SMatthew Knepley   to the Rhs of each system.
199776b2cf59SMatthew Knepley 
199876b2cf59SMatthew Knepley   Collective on SNES
199976b2cf59SMatthew Knepley 
200076b2cf59SMatthew Knepley   Input Parameters:
200176b2cf59SMatthew Knepley . snes - The nonlinear solver context
200276b2cf59SMatthew Knepley . func - The function
200376b2cf59SMatthew Knepley 
200476b2cf59SMatthew Knepley   Calling sequence of func:
200576b2cf59SMatthew Knepley . func (SNES snes, Vec rhs, void *ctx);
200676b2cf59SMatthew Knepley 
200776b2cf59SMatthew Knepley . rhs - The current rhs vector
200876b2cf59SMatthew Knepley . ctx - The user-context
200976b2cf59SMatthew Knepley 
201076b2cf59SMatthew Knepley   Level: intermediate
201176b2cf59SMatthew Knepley 
201276b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
201376b2cf59SMatthew Knepley .seealso SNESDefaultRhsBC(), SNESSetSolutionBC(), SNESSetUpdate()
201476b2cf59SMatthew Knepley @*/
201576b2cf59SMatthew Knepley int SNESSetRhsBC(SNES snes, int (*func)(SNES, Vec, void *))
201676b2cf59SMatthew Knepley {
201776b2cf59SMatthew Knepley   PetscFunctionBegin;
201876b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
201976b2cf59SMatthew Knepley   snes->applyrhsbc = func;
202076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
202176b2cf59SMatthew Knepley }
202276b2cf59SMatthew Knepley 
2023e74ef692SMatthew Knepley #undef __FUNCT__
2024e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultRhsBC"
202576b2cf59SMatthew Knepley /*@
202676b2cf59SMatthew Knepley   SNESDefaultRhsBC - The default boundary condition function which does nothing.
202776b2cf59SMatthew Knepley 
202876b2cf59SMatthew Knepley   Not collective
202976b2cf59SMatthew Knepley 
203076b2cf59SMatthew Knepley   Input Parameters:
203176b2cf59SMatthew Knepley . snes - The nonlinear solver context
203276b2cf59SMatthew Knepley . rhs  - The Rhs
203376b2cf59SMatthew Knepley . ctx  - The user-context
203476b2cf59SMatthew Knepley 
203576b2cf59SMatthew Knepley   Level: beginner
203676b2cf59SMatthew Knepley 
203776b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
203876b2cf59SMatthew Knepley .seealso SNESSetRhsBC(), SNESDefaultSolutionBC(), SNESDefaultUpdate()
203976b2cf59SMatthew Knepley @*/
204076b2cf59SMatthew Knepley int SNESDefaultRhsBC(SNES snes, Vec rhs, void *ctx)
204176b2cf59SMatthew Knepley {
204276b2cf59SMatthew Knepley   PetscFunctionBegin;
204376b2cf59SMatthew Knepley   PetscFunctionReturn(0);
204476b2cf59SMatthew Knepley }
204576b2cf59SMatthew Knepley 
2046e74ef692SMatthew Knepley #undef __FUNCT__
2047e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetSolutionBC"
204876b2cf59SMatthew Knepley /*@
204976b2cf59SMatthew Knepley   SNESSetSolutionBC - Sets the function which applies boundary conditions
205076b2cf59SMatthew Knepley   to the solution of each system.
205176b2cf59SMatthew Knepley 
205276b2cf59SMatthew Knepley   Collective on SNES
205376b2cf59SMatthew Knepley 
205476b2cf59SMatthew Knepley   Input Parameters:
205576b2cf59SMatthew Knepley . snes - The nonlinear solver context
205676b2cf59SMatthew Knepley . func - The function
205776b2cf59SMatthew Knepley 
205876b2cf59SMatthew Knepley   Calling sequence of func:
205976b2cf59SMatthew Knepley . func (TS ts, Vec rsol, void *ctx);
206076b2cf59SMatthew Knepley 
206176b2cf59SMatthew Knepley . sol - The current solution vector
206276b2cf59SMatthew Knepley . ctx - The user-context
206376b2cf59SMatthew Knepley 
206476b2cf59SMatthew Knepley   Level: intermediate
206576b2cf59SMatthew Knepley 
206676b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
206776b2cf59SMatthew Knepley .seealso SNESDefaultSolutionBC(), SNESSetRhsBC(), SNESSetUpdate()
206876b2cf59SMatthew Knepley @*/
206976b2cf59SMatthew Knepley int SNESSetSolutionBC(SNES snes, int (*func)(SNES, Vec, void *))
207076b2cf59SMatthew Knepley {
207176b2cf59SMatthew Knepley   PetscFunctionBegin;
207276b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
207376b2cf59SMatthew Knepley   snes->applysolbc = func;
207476b2cf59SMatthew Knepley   PetscFunctionReturn(0);
207576b2cf59SMatthew Knepley }
207676b2cf59SMatthew Knepley 
2077e74ef692SMatthew Knepley #undef __FUNCT__
2078e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultSolutionBC"
207976b2cf59SMatthew Knepley /*@
208076b2cf59SMatthew Knepley   SNESDefaultSolutionBC - The default boundary condition function which does nothing.
208176b2cf59SMatthew Knepley 
208276b2cf59SMatthew Knepley   Not collective
208376b2cf59SMatthew Knepley 
208476b2cf59SMatthew Knepley   Input Parameters:
208576b2cf59SMatthew Knepley . snes - The nonlinear solver context
208676b2cf59SMatthew Knepley . sol  - The solution
208776b2cf59SMatthew Knepley . ctx  - The user-context
208876b2cf59SMatthew Knepley 
208976b2cf59SMatthew Knepley   Level: beginner
209076b2cf59SMatthew Knepley 
209176b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
209276b2cf59SMatthew Knepley .seealso SNESSetSolutionBC(), SNESDefaultRhsBC(), SNESDefaultUpdate()
209376b2cf59SMatthew Knepley @*/
209476b2cf59SMatthew Knepley int SNESDefaultSolutionBC(SNES snes, Vec sol, void *ctx)
209576b2cf59SMatthew Knepley {
209676b2cf59SMatthew Knepley   PetscFunctionBegin;
209776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
209876b2cf59SMatthew Knepley }
209976b2cf59SMatthew Knepley 
2100e74ef692SMatthew Knepley #undef __FUNCT__
2101e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
210276b2cf59SMatthew Knepley /*@
210376b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
210476b2cf59SMatthew Knepley   at the beginning of every step of the iteration.
210576b2cf59SMatthew Knepley 
210676b2cf59SMatthew Knepley   Collective on SNES
210776b2cf59SMatthew Knepley 
210876b2cf59SMatthew Knepley   Input Parameters:
210976b2cf59SMatthew Knepley . snes - The nonlinear solver context
211076b2cf59SMatthew Knepley . func - The function
211176b2cf59SMatthew Knepley 
211276b2cf59SMatthew Knepley   Calling sequence of func:
211376b2cf59SMatthew Knepley . func (TS ts, int step);
211476b2cf59SMatthew Knepley 
211576b2cf59SMatthew Knepley . step - The current step of the iteration
211676b2cf59SMatthew Knepley 
211776b2cf59SMatthew Knepley   Level: intermediate
211876b2cf59SMatthew Knepley 
211976b2cf59SMatthew Knepley .keywords: SNES, update
212076b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
212176b2cf59SMatthew Knepley @*/
212276b2cf59SMatthew Knepley int SNESSetUpdate(SNES snes, int (*func)(SNES, int))
212376b2cf59SMatthew Knepley {
212476b2cf59SMatthew Knepley   PetscFunctionBegin;
212576b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
212676b2cf59SMatthew Knepley   snes->update = func;
212776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
212876b2cf59SMatthew Knepley }
212976b2cf59SMatthew Knepley 
2130e74ef692SMatthew Knepley #undef __FUNCT__
2131e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
213276b2cf59SMatthew Knepley /*@
213376b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
213476b2cf59SMatthew Knepley 
213576b2cf59SMatthew Knepley   Not collective
213676b2cf59SMatthew Knepley 
213776b2cf59SMatthew Knepley   Input Parameters:
213876b2cf59SMatthew Knepley . snes - The nonlinear solver context
213976b2cf59SMatthew Knepley . step - The current step of the iteration
214076b2cf59SMatthew Knepley 
2141205452f4SMatthew Knepley   Level: intermediate
2142205452f4SMatthew Knepley 
214376b2cf59SMatthew Knepley .keywords: SNES, update
214476b2cf59SMatthew Knepley .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultSolutionBC()
214576b2cf59SMatthew Knepley @*/
214676b2cf59SMatthew Knepley int SNESDefaultUpdate(SNES snes, int step)
214776b2cf59SMatthew Knepley {
214876b2cf59SMatthew Knepley   PetscFunctionBegin;
214976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
215076b2cf59SMatthew Knepley }
215176b2cf59SMatthew Knepley 
21524a2ae208SSatish Balay #undef __FUNCT__
21534a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
21549b94acceSBarry Smith /*
21559b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
21569b94acceSBarry Smith    positive parameter delta.
21579b94acceSBarry Smith 
21589b94acceSBarry Smith     Input Parameters:
2159c7afd0dbSLois Curfman McInnes +   snes - the SNES context
21609b94acceSBarry Smith .   y - approximate solution of linear system
21619b94acceSBarry Smith .   fnorm - 2-norm of current function
2162c7afd0dbSLois Curfman McInnes -   delta - trust region size
21639b94acceSBarry Smith 
21649b94acceSBarry Smith     Output Parameters:
2165c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
21669b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
21679b94acceSBarry Smith     region, and exceeds zero otherwise.
2168c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
21699b94acceSBarry Smith 
21709b94acceSBarry Smith     Note:
21716831982aSBarry Smith     For non-trust region methods such as SNESEQLS, the parameter delta
21729b94acceSBarry Smith     is set to be the maximum allowable step size.
21739b94acceSBarry Smith 
21749b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
21759b94acceSBarry Smith */
2176064f8208SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
21779b94acceSBarry Smith {
2178064f8208SBarry Smith   PetscReal   nrm;
2179ea709b57SSatish Balay   PetscScalar cnorm;
21803a40ed3dSBarry Smith   int         ierr;
21813a40ed3dSBarry Smith 
21823a40ed3dSBarry Smith   PetscFunctionBegin;
2183184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2184184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
2185184914b5SBarry Smith   PetscCheckSameComm(snes,y);
2186184914b5SBarry Smith 
2187064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2188064f8208SBarry Smith   if (nrm > *delta) {
2189064f8208SBarry Smith      nrm = *delta/nrm;
2190064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2191064f8208SBarry Smith      cnorm = nrm;
2192329f5518SBarry Smith      ierr = VecScale(&cnorm,y);CHKERRQ(ierr);
21939b94acceSBarry Smith      *ynorm = *delta;
21949b94acceSBarry Smith   } else {
21959b94acceSBarry Smith      *gpnorm = 0.0;
2196064f8208SBarry Smith      *ynorm = nrm;
21979b94acceSBarry Smith   }
21983a40ed3dSBarry Smith   PetscFunctionReturn(0);
21999b94acceSBarry Smith }
22009b94acceSBarry Smith 
22014a2ae208SSatish Balay #undef __FUNCT__
22024a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
22039b94acceSBarry Smith /*@
22049b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
220563a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
22069b94acceSBarry Smith 
2207c7afd0dbSLois Curfman McInnes    Collective on SNES
2208c7afd0dbSLois Curfman McInnes 
2209b2002411SLois Curfman McInnes    Input Parameters:
2210c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2211c7afd0dbSLois Curfman McInnes -  x - the solution vector
22129b94acceSBarry Smith 
22139b94acceSBarry Smith    Output Parameter:
2214b2002411SLois Curfman McInnes .  its - number of iterations until termination
22159b94acceSBarry Smith 
2216b2002411SLois Curfman McInnes    Notes:
22178ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
22188ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
22198ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
22208ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
22218ddd3da0SLois Curfman McInnes 
222236851e7fSLois Curfman McInnes    Level: beginner
222336851e7fSLois Curfman McInnes 
22249b94acceSBarry Smith .keywords: SNES, nonlinear, solve
22259b94acceSBarry Smith 
222663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
22279b94acceSBarry Smith @*/
22288ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
22299b94acceSBarry Smith {
2230f1af5d2fSBarry Smith   int        ierr;
2231f1af5d2fSBarry Smith   PetscTruth flg;
2232052efed2SBarry Smith 
22333a40ed3dSBarry Smith   PetscFunctionBegin;
223477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2235184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
2236184914b5SBarry Smith   PetscCheckSameComm(snes,x);
223774679c65SBarry Smith   PetscValidIntPointer(its);
223829bbc08cSBarry Smith   if (!snes->solve) SETERRQ(1,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
223974637425SBarry Smith 
224082bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
2241c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
2242758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
2243d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
224450ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
22459b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr);
2246d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
2247b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr);
224893b98531SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESView(snes,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); }
22493a40ed3dSBarry Smith   PetscFunctionReturn(0);
22509b94acceSBarry Smith }
22519b94acceSBarry Smith 
22529b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
22539b94acceSBarry Smith 
22544a2ae208SSatish Balay #undef __FUNCT__
22554a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
225682bf6240SBarry Smith /*@C
22574b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
22589b94acceSBarry Smith 
2259fee21e36SBarry Smith    Collective on SNES
2260fee21e36SBarry Smith 
2261c7afd0dbSLois Curfman McInnes    Input Parameters:
2262c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2263454a90a3SBarry Smith -  type - a known method
2264c7afd0dbSLois Curfman McInnes 
2265c7afd0dbSLois Curfman McInnes    Options Database Key:
2266454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2267c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2268ae12b187SLois Curfman McInnes 
22699b94acceSBarry Smith    Notes:
2270e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
22716831982aSBarry Smith +    SNESEQLS - Newton's method with line search
2272c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22736831982aSBarry Smith .    SNESEQTR - Newton's method with trust region
2274c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22756831982aSBarry Smith .    SNESUMTR - Newton's method with trust region
2276c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
22776831982aSBarry Smith -    SNESUMLS - Newton's method with line search
2278c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
22799b94acceSBarry Smith 
2280ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2281ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2282ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2283ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2284ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2285ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2286ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2287ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2288ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2289b0a32e0cSBarry Smith   appropriate method.
229036851e7fSLois Curfman McInnes 
229136851e7fSLois Curfman McInnes   Level: intermediate
2292a703fe33SLois Curfman McInnes 
2293454a90a3SBarry Smith .keywords: SNES, set, type
2294435da068SBarry Smith 
2295435da068SBarry Smith .seealso: SNESType, SNESCreate()
2296435da068SBarry Smith 
22979b94acceSBarry Smith @*/
2298454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type)
22999b94acceSBarry Smith {
23000f5bd95cSBarry Smith   int        ierr,(*r)(SNES);
23016831982aSBarry Smith   PetscTruth match;
23023a40ed3dSBarry Smith 
23033a40ed3dSBarry Smith   PetscFunctionBegin;
230477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
23050f5bd95cSBarry Smith   PetscValidCharPointer(type);
230682bf6240SBarry Smith 
23076831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
23080f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
230982bf6240SBarry Smith 
231082bf6240SBarry Smith   if (snes->setupcalled) {
2311e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
231282bf6240SBarry Smith     snes->data = 0;
231382bf6240SBarry Smith   }
23147d1a2b2bSBarry Smith 
23159b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
231682bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
231782bf6240SBarry Smith 
2318b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
231982bf6240SBarry Smith 
232029bbc08cSBarry Smith   if (!r) SETERRQ1(1,"Unable to find requested SNES type %s",type);
23211548b14aSBarry Smith 
2322606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
232382bf6240SBarry Smith   snes->data = 0;
23243a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
232582bf6240SBarry Smith 
2326454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
232782bf6240SBarry Smith   snes->set_method_called = 1;
232882bf6240SBarry Smith 
23293a40ed3dSBarry Smith   PetscFunctionReturn(0);
23309b94acceSBarry Smith }
23319b94acceSBarry Smith 
2332a847f771SSatish Balay 
23339b94acceSBarry Smith /* --------------------------------------------------------------------- */
23344a2ae208SSatish Balay #undef __FUNCT__
23354a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
23369b94acceSBarry Smith /*@C
23379b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2338f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
23399b94acceSBarry Smith 
2340fee21e36SBarry Smith    Not Collective
2341fee21e36SBarry Smith 
234236851e7fSLois Curfman McInnes    Level: advanced
234336851e7fSLois Curfman McInnes 
23449b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
23459b94acceSBarry Smith 
23469b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
23479b94acceSBarry Smith @*/
2348cf256101SBarry Smith int SNESRegisterDestroy(void)
23499b94acceSBarry Smith {
235082bf6240SBarry Smith   int ierr;
235182bf6240SBarry Smith 
23523a40ed3dSBarry Smith   PetscFunctionBegin;
235382bf6240SBarry Smith   if (SNESList) {
2354b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
235582bf6240SBarry Smith     SNESList = 0;
23569b94acceSBarry Smith   }
23574c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
23583a40ed3dSBarry Smith   PetscFunctionReturn(0);
23599b94acceSBarry Smith }
23609b94acceSBarry Smith 
23614a2ae208SSatish Balay #undef __FUNCT__
23624a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
23639b94acceSBarry Smith /*@C
23649a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
23659b94acceSBarry Smith 
2366c7afd0dbSLois Curfman McInnes    Not Collective
2367c7afd0dbSLois Curfman McInnes 
23689b94acceSBarry Smith    Input Parameter:
23694b0e389bSBarry Smith .  snes - nonlinear solver context
23709b94acceSBarry Smith 
23719b94acceSBarry Smith    Output Parameter:
23723a7fca6bSBarry Smith .  type - SNES method (a character string)
23739b94acceSBarry Smith 
237436851e7fSLois Curfman McInnes    Level: intermediate
237536851e7fSLois Curfman McInnes 
2376454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
23779b94acceSBarry Smith @*/
2378454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type)
23799b94acceSBarry Smith {
23803a40ed3dSBarry Smith   PetscFunctionBegin;
2381184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2382454a90a3SBarry Smith   *type = snes->type_name;
23833a40ed3dSBarry Smith   PetscFunctionReturn(0);
23849b94acceSBarry Smith }
23859b94acceSBarry Smith 
23864a2ae208SSatish Balay #undef __FUNCT__
23874a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
23889b94acceSBarry Smith /*@C
23899b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
23909b94acceSBarry Smith    stored.
23919b94acceSBarry Smith 
2392c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2393c7afd0dbSLois Curfman McInnes 
23949b94acceSBarry Smith    Input Parameter:
23959b94acceSBarry Smith .  snes - the SNES context
23969b94acceSBarry Smith 
23979b94acceSBarry Smith    Output Parameter:
23989b94acceSBarry Smith .  x - the solution
23999b94acceSBarry Smith 
240036851e7fSLois Curfman McInnes    Level: advanced
240136851e7fSLois Curfman McInnes 
24029b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
24039b94acceSBarry Smith 
2404a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
24059b94acceSBarry Smith @*/
24069b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
24079b94acceSBarry Smith {
24083a40ed3dSBarry Smith   PetscFunctionBegin;
240977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
24109b94acceSBarry Smith   *x = snes->vec_sol_always;
24113a40ed3dSBarry Smith   PetscFunctionReturn(0);
24129b94acceSBarry Smith }
24139b94acceSBarry Smith 
24144a2ae208SSatish Balay #undef __FUNCT__
24154a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
24169b94acceSBarry Smith /*@C
24179b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
24189b94acceSBarry Smith    stored.
24199b94acceSBarry Smith 
2420c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2421c7afd0dbSLois Curfman McInnes 
24229b94acceSBarry Smith    Input Parameter:
24239b94acceSBarry Smith .  snes - the SNES context
24249b94acceSBarry Smith 
24259b94acceSBarry Smith    Output Parameter:
24269b94acceSBarry Smith .  x - the solution update
24279b94acceSBarry Smith 
242836851e7fSLois Curfman McInnes    Level: advanced
242936851e7fSLois Curfman McInnes 
24309b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
24319b94acceSBarry Smith 
24329b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
24339b94acceSBarry Smith @*/
24349b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
24359b94acceSBarry Smith {
24363a40ed3dSBarry Smith   PetscFunctionBegin;
243777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
24389b94acceSBarry Smith   *x = snes->vec_sol_update_always;
24393a40ed3dSBarry Smith   PetscFunctionReturn(0);
24409b94acceSBarry Smith }
24419b94acceSBarry Smith 
24424a2ae208SSatish Balay #undef __FUNCT__
24434a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
24449b94acceSBarry Smith /*@C
24453638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
24469b94acceSBarry Smith 
2447c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2448c7afd0dbSLois Curfman McInnes 
24499b94acceSBarry Smith    Input Parameter:
24509b94acceSBarry Smith .  snes - the SNES context
24519b94acceSBarry Smith 
24529b94acceSBarry Smith    Output Parameter:
24537bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
245400036973SBarry Smith .  ctx - the function context (or PETSC_NULL)
245500036973SBarry Smith -  func - the function (or PETSC_NULL)
24569b94acceSBarry Smith 
24579b94acceSBarry Smith    Notes:
24589b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
24599b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
24609b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
24619b94acceSBarry Smith 
246236851e7fSLois Curfman McInnes    Level: advanced
246336851e7fSLois Curfman McInnes 
2464a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
24659b94acceSBarry Smith 
246631615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
246731615d04SLois Curfman McInnes           SNESGetGradient()
24687bf4e008SBarry Smith 
24699b94acceSBarry Smith @*/
247000036973SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx,int (**func)(SNES,Vec,Vec,void*))
24719b94acceSBarry Smith {
24723a40ed3dSBarry Smith   PetscFunctionBegin;
247377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2474a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
247529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
2476a8c6a408SBarry Smith   }
24777bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
24787bf4e008SBarry Smith   if (ctx)  *ctx  = snes->funP;
247900036973SBarry Smith   if (func) *func = snes->computefunction;
24803a40ed3dSBarry Smith   PetscFunctionReturn(0);
24819b94acceSBarry Smith }
24829b94acceSBarry Smith 
24834a2ae208SSatish Balay #undef __FUNCT__
24844a2ae208SSatish Balay #define __FUNCT__ "SNESGetGradient"
24859b94acceSBarry Smith /*@C
24863638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
24879b94acceSBarry Smith 
2488c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2489c7afd0dbSLois Curfman McInnes 
24909b94acceSBarry Smith    Input Parameter:
24919b94acceSBarry Smith .  snes - the SNES context
24929b94acceSBarry Smith 
24939b94acceSBarry Smith    Output Parameter:
24947bf4e008SBarry Smith +  r - the gradient (or PETSC_NULL)
24957bf4e008SBarry Smith -  ctx - the gradient context (or PETSC_NULL)
24969b94acceSBarry Smith 
24979b94acceSBarry Smith    Notes:
24989b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
24999b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
25009b94acceSBarry Smith    SNESGetFunction().
25019b94acceSBarry Smith 
250236851e7fSLois Curfman McInnes    Level: advanced
250336851e7fSLois Curfman McInnes 
25049b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
25059b94acceSBarry Smith 
25067bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(),
25077bf4e008SBarry Smith           SNESSetGradient(), SNESSetFunction()
25087bf4e008SBarry Smith 
25099b94acceSBarry Smith @*/
25107bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx)
25119b94acceSBarry Smith {
25123a40ed3dSBarry Smith   PetscFunctionBegin;
251377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
25143a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
251529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
25163a40ed3dSBarry Smith   }
25177bf4e008SBarry Smith   if (r)   *r = snes->vec_func_always;
25187bf4e008SBarry Smith   if (ctx) *ctx = snes->funP;
25193a40ed3dSBarry Smith   PetscFunctionReturn(0);
25209b94acceSBarry Smith }
25219b94acceSBarry Smith 
25224a2ae208SSatish Balay #undef __FUNCT__
25234a2ae208SSatish Balay #define __FUNCT__ "SNESGetMinimizationFunction"
25247bf4e008SBarry Smith /*@C
25259b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
25269b94acceSBarry Smith    unconstrained minimization problems.
25279b94acceSBarry Smith 
2528c7afd0dbSLois Curfman McInnes    Not Collective
2529c7afd0dbSLois Curfman McInnes 
25309b94acceSBarry Smith    Input Parameter:
25319b94acceSBarry Smith .  snes - the SNES context
25329b94acceSBarry Smith 
25339b94acceSBarry Smith    Output Parameter:
25347bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
25357bf4e008SBarry Smith -  ctx - the function context (or PETSC_NULL)
25369b94acceSBarry Smith 
25379b94acceSBarry Smith    Notes:
25389b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
25399b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
25409b94acceSBarry Smith    SNESGetFunction().
25419b94acceSBarry Smith 
254236851e7fSLois Curfman McInnes    Level: advanced
254336851e7fSLois Curfman McInnes 
25449b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
25459b94acceSBarry Smith 
25467bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction()
25477bf4e008SBarry Smith 
25489b94acceSBarry Smith @*/
2549329f5518SBarry Smith int SNESGetMinimizationFunction(SNES snes,PetscReal *r,void **ctx)
25509b94acceSBarry Smith {
25513a40ed3dSBarry Smith   PetscFunctionBegin;
255277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
255374679c65SBarry Smith   PetscValidScalarPointer(r);
25543a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
255529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
25563a40ed3dSBarry Smith   }
25577bf4e008SBarry Smith   if (r)   *r = snes->fc;
25587bf4e008SBarry Smith   if (ctx) *ctx = snes->umfunP;
25593a40ed3dSBarry Smith   PetscFunctionReturn(0);
25609b94acceSBarry Smith }
25619b94acceSBarry Smith 
25624a2ae208SSatish Balay #undef __FUNCT__
25634a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
25643c7409f5SSatish Balay /*@C
25653c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2566d850072dSLois Curfman McInnes    SNES options in the database.
25673c7409f5SSatish Balay 
2568fee21e36SBarry Smith    Collective on SNES
2569fee21e36SBarry Smith 
2570c7afd0dbSLois Curfman McInnes    Input Parameter:
2571c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2572c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2573c7afd0dbSLois Curfman McInnes 
2574d850072dSLois Curfman McInnes    Notes:
2575a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2576c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2577d850072dSLois Curfman McInnes 
257836851e7fSLois Curfman McInnes    Level: advanced
257936851e7fSLois Curfman McInnes 
25803c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2581a86d99e1SLois Curfman McInnes 
2582a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
25833c7409f5SSatish Balay @*/
25843c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
25853c7409f5SSatish Balay {
25863c7409f5SSatish Balay   int ierr;
25873c7409f5SSatish Balay 
25883a40ed3dSBarry Smith   PetscFunctionBegin;
258977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2590639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25913c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
25923a40ed3dSBarry Smith   PetscFunctionReturn(0);
25933c7409f5SSatish Balay }
25943c7409f5SSatish Balay 
25954a2ae208SSatish Balay #undef __FUNCT__
25964a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
25973c7409f5SSatish Balay /*@C
2598f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2599d850072dSLois Curfman McInnes    SNES options in the database.
26003c7409f5SSatish Balay 
2601fee21e36SBarry Smith    Collective on SNES
2602fee21e36SBarry Smith 
2603c7afd0dbSLois Curfman McInnes    Input Parameters:
2604c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2605c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2606c7afd0dbSLois Curfman McInnes 
2607d850072dSLois Curfman McInnes    Notes:
2608a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2609c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2610d850072dSLois Curfman McInnes 
261136851e7fSLois Curfman McInnes    Level: advanced
261236851e7fSLois Curfman McInnes 
26133c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2614a86d99e1SLois Curfman McInnes 
2615a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
26163c7409f5SSatish Balay @*/
26173c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
26183c7409f5SSatish Balay {
26193c7409f5SSatish Balay   int ierr;
26203c7409f5SSatish Balay 
26213a40ed3dSBarry Smith   PetscFunctionBegin;
262277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2623639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26243c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
26253a40ed3dSBarry Smith   PetscFunctionReturn(0);
26263c7409f5SSatish Balay }
26273c7409f5SSatish Balay 
26284a2ae208SSatish Balay #undef __FUNCT__
26294a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
26309ab63eb5SSatish Balay /*@C
26313c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
26323c7409f5SSatish Balay    SNES options in the database.
26333c7409f5SSatish Balay 
2634c7afd0dbSLois Curfman McInnes    Not Collective
2635c7afd0dbSLois Curfman McInnes 
26363c7409f5SSatish Balay    Input Parameter:
26373c7409f5SSatish Balay .  snes - the SNES context
26383c7409f5SSatish Balay 
26393c7409f5SSatish Balay    Output Parameter:
26403c7409f5SSatish Balay .  prefix - pointer to the prefix string used
26413c7409f5SSatish Balay 
26429ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
26439ab63eb5SSatish Balay    sufficient length to hold the prefix.
26449ab63eb5SSatish Balay 
264536851e7fSLois Curfman McInnes    Level: advanced
264636851e7fSLois Curfman McInnes 
26473c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2648a86d99e1SLois Curfman McInnes 
2649a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
26503c7409f5SSatish Balay @*/
26513c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
26523c7409f5SSatish Balay {
26533c7409f5SSatish Balay   int ierr;
26543c7409f5SSatish Balay 
26553a40ed3dSBarry Smith   PetscFunctionBegin;
265677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2657639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26583a40ed3dSBarry Smith   PetscFunctionReturn(0);
26593c7409f5SSatish Balay }
26603c7409f5SSatish Balay 
2661acb85ae6SSatish Balay /*MC
2662f1af5d2fSBarry Smith    SNESRegisterDynamic - Adds a method to the nonlinear solver package.
26639b94acceSBarry Smith 
2664b2002411SLois Curfman McInnes    Synopsis:
26653a7fca6bSBarry Smith    int SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
26669b94acceSBarry Smith 
26678d76a1e5SLois Curfman McInnes    Not collective
26688d76a1e5SLois Curfman McInnes 
2669b2002411SLois Curfman McInnes    Input Parameters:
2670c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2671b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2672b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2673c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
26749b94acceSBarry Smith 
2675b2002411SLois Curfman McInnes    Notes:
2676f1af5d2fSBarry Smith    SNESRegisterDynamic() may be called multiple times to add several user-defined solvers.
26773a40ed3dSBarry Smith 
2678b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2679b2002411SLois Curfman McInnes    is ignored.
2680b2002411SLois Curfman McInnes 
2681b9617806SBarry Smith    Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT},
26825ba88a07SLois Curfman McInnes    and others of the form ${any_environmental_variable} occuring in pathname will be
26835ba88a07SLois Curfman McInnes    replaced with appropriate values.
26845ba88a07SLois Curfman McInnes 
2685b2002411SLois Curfman McInnes    Sample usage:
268669225d78SLois Curfman McInnes .vb
2687f1af5d2fSBarry Smith    SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2688b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
268969225d78SLois Curfman McInnes .ve
2690b2002411SLois Curfman McInnes 
2691b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2692b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2693b2002411SLois Curfman McInnes    or at runtime via the option
2694b2002411SLois Curfman McInnes $     -snes_type my_solver
2695b2002411SLois Curfman McInnes 
269636851e7fSLois Curfman McInnes    Level: advanced
269736851e7fSLois Curfman McInnes 
2698b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2699b2002411SLois Curfman McInnes 
2700b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2701b2002411SLois Curfman McInnes M*/
2702b2002411SLois Curfman McInnes 
27034a2ae208SSatish Balay #undef __FUNCT__
27044a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
2705f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES))
2706b2002411SLois Curfman McInnes {
2707b2002411SLois Curfman McInnes   char fullname[256];
2708b2002411SLois Curfman McInnes   int  ierr;
2709b2002411SLois Curfman McInnes 
2710b2002411SLois Curfman McInnes   PetscFunctionBegin;
2711b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2712c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2713b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2714b2002411SLois Curfman McInnes }
2715