xref: /petsc/src/snes/interface/snes.c (revision 909c8a9f0ce5b6e30fd18fd91539f25f7b9715c4)
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     }
246*909c8a9fSBarry 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);}
281b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_xmonitor","Plot function norm at each iteration","SNESLGMonitor",&flg);CHKERRQ(ierr);
282186905e3SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
283e24b481bSBarry Smith 
284b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
2853c7409f5SSatish Balay     if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
286186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
287b0a32e0cSBarry Smith       PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
288981c4779SBarry Smith     } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
289186905e3SBarry Smith       ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr);
290b0a32e0cSBarry Smith       PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
2919b94acceSBarry Smith     }
292639f9d9dSBarry Smith 
29376b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
29476b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);                                                             CHKERRQ(ierr);
29576b2cf59SMatthew Knepley     }
29676b2cf59SMatthew Knepley 
297186905e3SBarry Smith     if (snes->setfromoptions) {
298186905e3SBarry Smith       ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
299639f9d9dSBarry Smith     }
300639f9d9dSBarry Smith 
301b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3024bbc92c1SBarry Smith 
3039b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
3049b94acceSBarry Smith   ierr = SLESSetFromOptions(sles);CHKERRQ(ierr);
30593993e2dSLois Curfman McInnes 
3063a40ed3dSBarry Smith   PetscFunctionReturn(0);
3079b94acceSBarry Smith }
3089b94acceSBarry Smith 
309a847f771SSatish Balay 
3104a2ae208SSatish Balay #undef __FUNCT__
3114a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
3129b94acceSBarry Smith /*@
3139b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3149b94acceSBarry Smith    the nonlinear solvers.
3159b94acceSBarry Smith 
316fee21e36SBarry Smith    Collective on SNES
317fee21e36SBarry Smith 
318c7afd0dbSLois Curfman McInnes    Input Parameters:
319c7afd0dbSLois Curfman McInnes +  snes - the SNES context
320c7afd0dbSLois Curfman McInnes -  usrP - optional user context
321c7afd0dbSLois Curfman McInnes 
32236851e7fSLois Curfman McInnes    Level: intermediate
32336851e7fSLois Curfman McInnes 
3249b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3259b94acceSBarry Smith 
3269b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3279b94acceSBarry Smith @*/
3289b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
3299b94acceSBarry Smith {
3303a40ed3dSBarry Smith   PetscFunctionBegin;
33177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3329b94acceSBarry Smith   snes->user		= usrP;
3333a40ed3dSBarry Smith   PetscFunctionReturn(0);
3349b94acceSBarry Smith }
33574679c65SBarry Smith 
3364a2ae208SSatish Balay #undef __FUNCT__
3374a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3389b94acceSBarry Smith /*@C
3399b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3409b94acceSBarry Smith    nonlinear solvers.
3419b94acceSBarry Smith 
342c7afd0dbSLois Curfman McInnes    Not Collective
343c7afd0dbSLois Curfman McInnes 
3449b94acceSBarry Smith    Input Parameter:
3459b94acceSBarry Smith .  snes - SNES context
3469b94acceSBarry Smith 
3479b94acceSBarry Smith    Output Parameter:
3489b94acceSBarry Smith .  usrP - user context
3499b94acceSBarry Smith 
35036851e7fSLois Curfman McInnes    Level: intermediate
35136851e7fSLois Curfman McInnes 
3529b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3539b94acceSBarry Smith 
3549b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3559b94acceSBarry Smith @*/
3569b94acceSBarry Smith int SNESGetApplicationContext(SNES snes,void **usrP)
3579b94acceSBarry Smith {
3583a40ed3dSBarry Smith   PetscFunctionBegin;
35977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3609b94acceSBarry Smith   *usrP = snes->user;
3613a40ed3dSBarry Smith   PetscFunctionReturn(0);
3629b94acceSBarry Smith }
36374679c65SBarry Smith 
3644a2ae208SSatish Balay #undef __FUNCT__
3654a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3669b94acceSBarry Smith /*@
367c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
368c8228a4eSBarry Smith    at this time.
3699b94acceSBarry Smith 
370c7afd0dbSLois Curfman McInnes    Not Collective
371c7afd0dbSLois Curfman McInnes 
3729b94acceSBarry Smith    Input Parameter:
3739b94acceSBarry Smith .  snes - SNES context
3749b94acceSBarry Smith 
3759b94acceSBarry Smith    Output Parameter:
3769b94acceSBarry Smith .  iter - iteration number
3779b94acceSBarry Smith 
378c8228a4eSBarry Smith    Notes:
379c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
380c8228a4eSBarry Smith 
381c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
38208405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
38308405cd6SLois Curfman McInnes .vb
38408405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
38508405cd6SLois Curfman McInnes       if (!(it % 2)) {
38608405cd6SLois Curfman McInnes         [compute Jacobian here]
38708405cd6SLois Curfman McInnes       }
38808405cd6SLois Curfman McInnes .ve
389c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
39008405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
391c8228a4eSBarry Smith 
39236851e7fSLois Curfman McInnes    Level: intermediate
39336851e7fSLois Curfman McInnes 
3949b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3959b94acceSBarry Smith @*/
3969b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3979b94acceSBarry Smith {
3983a40ed3dSBarry Smith   PetscFunctionBegin;
39977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
40074679c65SBarry Smith   PetscValidIntPointer(iter);
4019b94acceSBarry Smith   *iter = snes->iter;
4023a40ed3dSBarry Smith   PetscFunctionReturn(0);
4039b94acceSBarry Smith }
40474679c65SBarry Smith 
4054a2ae208SSatish Balay #undef __FUNCT__
4064a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
4079b94acceSBarry Smith /*@
4089b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4099b94acceSBarry Smith    with SNESSSetFunction().
4109b94acceSBarry Smith 
411c7afd0dbSLois Curfman McInnes    Collective on SNES
412c7afd0dbSLois Curfman McInnes 
4139b94acceSBarry Smith    Input Parameter:
4149b94acceSBarry Smith .  snes - SNES context
4159b94acceSBarry Smith 
4169b94acceSBarry Smith    Output Parameter:
4179b94acceSBarry Smith .  fnorm - 2-norm of function
4189b94acceSBarry Smith 
4199b94acceSBarry Smith    Note:
4209b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
421a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
422a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
4239b94acceSBarry Smith 
42436851e7fSLois Curfman McInnes    Level: intermediate
42536851e7fSLois Curfman McInnes 
4269b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
427a86d99e1SLois Curfman McInnes 
42808405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
4299b94acceSBarry Smith @*/
43087828ca2SBarry Smith int SNESGetFunctionNorm(SNES snes,PetscScalar *fnorm)
4319b94acceSBarry Smith {
4323a40ed3dSBarry Smith   PetscFunctionBegin;
43377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
43474679c65SBarry Smith   PetscValidScalarPointer(fnorm);
43574679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
43629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"For SNES_NONLINEAR_EQUATIONS only");
43774679c65SBarry Smith   }
4389b94acceSBarry Smith   *fnorm = snes->norm;
4393a40ed3dSBarry Smith   PetscFunctionReturn(0);
4409b94acceSBarry Smith }
44174679c65SBarry Smith 
4424a2ae208SSatish Balay #undef __FUNCT__
4434a2ae208SSatish Balay #define __FUNCT__ "SNESGetGradientNorm"
4449b94acceSBarry Smith /*@
4459b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
4469b94acceSBarry Smith    with SNESSSetGradient().
4479b94acceSBarry Smith 
448c7afd0dbSLois Curfman McInnes    Collective on SNES
449c7afd0dbSLois Curfman McInnes 
4509b94acceSBarry Smith    Input Parameter:
4519b94acceSBarry Smith .  snes - SNES context
4529b94acceSBarry Smith 
4539b94acceSBarry Smith    Output Parameter:
4549b94acceSBarry Smith .  fnorm - 2-norm of gradient
4559b94acceSBarry Smith 
4569b94acceSBarry Smith    Note:
4579b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
458a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
459a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
4609b94acceSBarry Smith 
46136851e7fSLois Curfman McInnes    Level: intermediate
46236851e7fSLois Curfman McInnes 
4639b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
464a86d99e1SLois Curfman McInnes 
465a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
4669b94acceSBarry Smith @*/
46787828ca2SBarry Smith int SNESGetGradientNorm(SNES snes,PetscScalar *gnorm)
4689b94acceSBarry Smith {
4693a40ed3dSBarry Smith   PetscFunctionBegin;
47077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
47174679c65SBarry Smith   PetscValidScalarPointer(gnorm);
47274679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
47329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"For SNES_UNCONSTRAINED_MINIMIZATION only");
47474679c65SBarry Smith   }
4759b94acceSBarry Smith   *gnorm = snes->norm;
4763a40ed3dSBarry Smith   PetscFunctionReturn(0);
4779b94acceSBarry Smith }
47874679c65SBarry Smith 
4794a2ae208SSatish Balay #undef __FUNCT__
4804a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps"
4819b94acceSBarry Smith /*@
4829b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
4839b94acceSBarry Smith    attempted by the nonlinear solver.
4849b94acceSBarry Smith 
485c7afd0dbSLois Curfman McInnes    Not Collective
486c7afd0dbSLois Curfman McInnes 
4879b94acceSBarry Smith    Input Parameter:
4889b94acceSBarry Smith .  snes - SNES context
4899b94acceSBarry Smith 
4909b94acceSBarry Smith    Output Parameter:
4919b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4929b94acceSBarry Smith 
493c96a6f78SLois Curfman McInnes    Notes:
494c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
495c96a6f78SLois Curfman McInnes 
49636851e7fSLois Curfman McInnes    Level: intermediate
49736851e7fSLois Curfman McInnes 
4989b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4999b94acceSBarry Smith @*/
5009b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
5019b94acceSBarry Smith {
5023a40ed3dSBarry Smith   PetscFunctionBegin;
50377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
50474679c65SBarry Smith   PetscValidIntPointer(nfails);
50550ffb88aSMatthew Knepley   *nfails = snes->numFailures;
50650ffb88aSMatthew Knepley   PetscFunctionReturn(0);
50750ffb88aSMatthew Knepley }
50850ffb88aSMatthew Knepley 
50950ffb88aSMatthew Knepley #undef __FUNCT__
51050ffb88aSMatthew Knepley #define __FUNCT__ "SNESSetMaximumUnsuccessfulSteps"
51150ffb88aSMatthew Knepley /*@
51250ffb88aSMatthew Knepley    SNESSetMaximumUnsuccessfulSteps - Sets the maximum number of unsuccessful steps
51350ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
51450ffb88aSMatthew Knepley 
51550ffb88aSMatthew Knepley    Not Collective
51650ffb88aSMatthew Knepley 
51750ffb88aSMatthew Knepley    Input Parameters:
51850ffb88aSMatthew Knepley +  snes     - SNES context
51950ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
52050ffb88aSMatthew Knepley 
52150ffb88aSMatthew Knepley    Level: intermediate
52250ffb88aSMatthew Knepley 
52350ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
52450ffb88aSMatthew Knepley @*/
52550ffb88aSMatthew Knepley int SNESSetMaximumUnsuccessfulSteps(SNES snes, int maxFails)
52650ffb88aSMatthew Knepley {
52750ffb88aSMatthew Knepley   PetscFunctionBegin;
52850ffb88aSMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE);
52950ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
53050ffb88aSMatthew Knepley   PetscFunctionReturn(0);
53150ffb88aSMatthew Knepley }
53250ffb88aSMatthew Knepley 
53350ffb88aSMatthew Knepley #undef __FUNCT__
53450ffb88aSMatthew Knepley #define __FUNCT__ "SNESGetMaximumUnsuccessfulSteps"
53550ffb88aSMatthew Knepley /*@
53650ffb88aSMatthew Knepley    SNESGetMaximumUnsuccessfulSteps - Gets the maximum number of unsuccessful steps
53750ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
53850ffb88aSMatthew Knepley 
53950ffb88aSMatthew Knepley    Not Collective
54050ffb88aSMatthew Knepley 
54150ffb88aSMatthew Knepley    Input Parameter:
54250ffb88aSMatthew Knepley .  snes     - SNES context
54350ffb88aSMatthew Knepley 
54450ffb88aSMatthew Knepley    Output Parameter:
54550ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
54650ffb88aSMatthew Knepley 
54750ffb88aSMatthew Knepley    Level: intermediate
54850ffb88aSMatthew Knepley 
54950ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
55050ffb88aSMatthew Knepley @*/
55150ffb88aSMatthew Knepley int SNESGetMaximumUnsuccessfulSteps(SNES snes, int *maxFails)
55250ffb88aSMatthew Knepley {
55350ffb88aSMatthew Knepley   PetscFunctionBegin;
55450ffb88aSMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE);
55550ffb88aSMatthew Knepley   PetscValidIntPointer(maxFails);
55650ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
5573a40ed3dSBarry Smith   PetscFunctionReturn(0);
5589b94acceSBarry Smith }
559a847f771SSatish Balay 
5604a2ae208SSatish Balay #undef __FUNCT__
5614a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations"
562c96a6f78SLois Curfman McInnes /*@
563c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
564c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
565c96a6f78SLois Curfman McInnes 
566c7afd0dbSLois Curfman McInnes    Not Collective
567c7afd0dbSLois Curfman McInnes 
568c96a6f78SLois Curfman McInnes    Input Parameter:
569c96a6f78SLois Curfman McInnes .  snes - SNES context
570c96a6f78SLois Curfman McInnes 
571c96a6f78SLois Curfman McInnes    Output Parameter:
572c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
573c96a6f78SLois Curfman McInnes 
574c96a6f78SLois Curfman McInnes    Notes:
575c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
576c96a6f78SLois Curfman McInnes 
57736851e7fSLois Curfman McInnes    Level: intermediate
57836851e7fSLois Curfman McInnes 
579c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
580c96a6f78SLois Curfman McInnes @*/
58186bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
582c96a6f78SLois Curfman McInnes {
5833a40ed3dSBarry Smith   PetscFunctionBegin;
584c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
585c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
586c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5873a40ed3dSBarry Smith   PetscFunctionReturn(0);
588c96a6f78SLois Curfman McInnes }
589c96a6f78SLois Curfman McInnes 
5904a2ae208SSatish Balay #undef __FUNCT__
5914a2ae208SSatish Balay #define __FUNCT__ "SNESGetSLES"
5929b94acceSBarry Smith /*@C
5939b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
5949b94acceSBarry Smith 
595c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
596c7afd0dbSLois Curfman McInnes 
5979b94acceSBarry Smith    Input Parameter:
5989b94acceSBarry Smith .  snes - the SNES context
5999b94acceSBarry Smith 
6009b94acceSBarry Smith    Output Parameter:
6019b94acceSBarry Smith .  sles - the SLES context
6029b94acceSBarry Smith 
6039b94acceSBarry Smith    Notes:
6049b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
6059b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
6069b94acceSBarry Smith    KSP and PC contexts as well.
6079b94acceSBarry Smith 
60836851e7fSLois Curfman McInnes    Level: beginner
60936851e7fSLois Curfman McInnes 
6109b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
6119b94acceSBarry Smith 
6129b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
6139b94acceSBarry Smith @*/
6149b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
6159b94acceSBarry Smith {
6163a40ed3dSBarry Smith   PetscFunctionBegin;
61777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
6189b94acceSBarry Smith   *sles = snes->sles;
6193a40ed3dSBarry Smith   PetscFunctionReturn(0);
6209b94acceSBarry Smith }
62182bf6240SBarry Smith 
6224a2ae208SSatish Balay #undef __FUNCT__
6234a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
624454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj)
625e24b481bSBarry Smith {
626aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
627454a90a3SBarry Smith   SNES          v = (SNES) obj;
628e24b481bSBarry Smith   int          ierr;
62943d6d2cbSBarry Smith #endif
630e24b481bSBarry Smith 
631e24b481bSBarry Smith   PetscFunctionBegin;
632e24b481bSBarry Smith 
63343d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
634e24b481bSBarry Smith   /* if it is already published then return */
635e24b481bSBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
636e24b481bSBarry Smith 
637454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
638e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
639e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
640e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
641e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
642454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
643433994e6SBarry Smith #endif
644e24b481bSBarry Smith   PetscFunctionReturn(0);
645e24b481bSBarry Smith }
646e24b481bSBarry Smith 
6479b94acceSBarry Smith /* -----------------------------------------------------------*/
6484a2ae208SSatish Balay #undef __FUNCT__
6494a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
6509b94acceSBarry Smith /*@C
6519b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
6529b94acceSBarry Smith 
653c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
654c7afd0dbSLois Curfman McInnes 
655c7afd0dbSLois Curfman McInnes    Input Parameters:
656c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
657c7afd0dbSLois Curfman McInnes -  type - type of method, either
658c7afd0dbSLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
659c7afd0dbSLois Curfman McInnes    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
6609b94acceSBarry Smith 
6619b94acceSBarry Smith    Output Parameter:
6629b94acceSBarry Smith .  outsnes - the new SNES context
6639b94acceSBarry Smith 
664c7afd0dbSLois Curfman McInnes    Options Database Keys:
665c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
666c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
667c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
668c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
669c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
670c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
671c1f60f51SBarry Smith 
67236851e7fSLois Curfman McInnes    Level: beginner
67336851e7fSLois Curfman McInnes 
6749b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
6759b94acceSBarry Smith 
676435da068SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNESProblemType, SNES
6779b94acceSBarry Smith @*/
6784b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
6799b94acceSBarry Smith {
6809b94acceSBarry Smith   int                 ierr;
6819b94acceSBarry Smith   SNES                snes;
6829b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
68337fcc0dbSBarry Smith 
6843a40ed3dSBarry Smith   PetscFunctionBegin;
6858ba1e511SMatthew Knepley   PetscValidPointer(outsnes);
6868ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
6878ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
6888ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);                                                               CHKERRQ(ierr);
6898ba1e511SMatthew Knepley #endif
6908ba1e511SMatthew Knepley 
691d64ed03dSBarry Smith   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){
69229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"incorrect method type");
693d64ed03dSBarry Smith   }
6943f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
695b0a32e0cSBarry Smith   PetscLogObjectCreate(snes);
696e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6979b94acceSBarry Smith   snes->max_its           = 50;
6989750a799SBarry Smith   snes->max_funcs	  = 10000;
6999b94acceSBarry Smith   snes->norm		  = 0.0;
7005a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
701b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
702b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
7039b94acceSBarry Smith     snes->atol		  = 1.e-10;
704d64ed03dSBarry Smith   } else {
705b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
706b4874afaSBarry Smith     snes->ttol            = 0.0;
707b4874afaSBarry Smith     snes->atol		  = 1.e-50;
708b4874afaSBarry Smith   }
7099b94acceSBarry Smith   snes->xtol		  = 1.e-8;
7109b94acceSBarry Smith   snes->nfuncs            = 0;
71150ffb88aSMatthew Knepley   snes->numFailures       = 0;
71250ffb88aSMatthew Knepley   snes->maxFailures       = 1;
7137a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
714639f9d9dSBarry Smith   snes->numbermonitors    = 0;
7159b94acceSBarry Smith   snes->data              = 0;
7169b94acceSBarry Smith   snes->view              = 0;
7179b94acceSBarry Smith   snes->computeumfunction = 0;
7189b94acceSBarry Smith   snes->umfunP            = 0;
7199b94acceSBarry Smith   snes->fc                = 0;
7209b94acceSBarry Smith   snes->deltatol          = 1.e-12;
7219b94acceSBarry Smith   snes->fmin              = -1.e30;
7229b94acceSBarry Smith   snes->method_class      = type;
7239b94acceSBarry Smith   snes->set_method_called = 0;
72482bf6240SBarry Smith   snes->setupcalled       = 0;
725186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
7266f24a144SLois Curfman McInnes   snes->vwork             = 0;
7276f24a144SLois Curfman McInnes   snes->nwork             = 0;
728758f92a0SBarry Smith   snes->conv_hist_len     = 0;
729758f92a0SBarry Smith   snes->conv_hist_max     = 0;
730758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
731758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
732758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
733184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
7349b94acceSBarry Smith 
7359b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
736b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
737b0a32e0cSBarry Smith   PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
7389b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7399b94acceSBarry Smith   kctx->version     = 2;
7409b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7419b94acceSBarry Smith                              this was too large for some test cases */
7429b94acceSBarry Smith   kctx->rtol_last   = 0;
7439b94acceSBarry Smith   kctx->rtol_max    = .9;
7449b94acceSBarry Smith   kctx->gamma       = 1.0;
7459b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7469b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7479b94acceSBarry Smith   kctx->threshold   = .1;
7489b94acceSBarry Smith   kctx->lresid_last = 0;
7499b94acceSBarry Smith   kctx->norm_last   = 0;
7509b94acceSBarry Smith 
7519b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr);
752b0a32e0cSBarry Smith   PetscLogObjectParent(snes,snes->sles)
7535334005bSBarry Smith 
7549b94acceSBarry Smith   *outsnes = snes;
75500036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
7563a40ed3dSBarry Smith   PetscFunctionReturn(0);
7579b94acceSBarry Smith }
7589b94acceSBarry Smith 
7599b94acceSBarry Smith /* --------------------------------------------------------------- */
7604a2ae208SSatish Balay #undef __FUNCT__
7614a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
7629b94acceSBarry Smith /*@C
7639b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
7649b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
7659b94acceSBarry Smith    equations.
7669b94acceSBarry Smith 
767fee21e36SBarry Smith    Collective on SNES
768fee21e36SBarry Smith 
769c7afd0dbSLois Curfman McInnes    Input Parameters:
770c7afd0dbSLois Curfman McInnes +  snes - the SNES context
771c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
772c7afd0dbSLois Curfman McInnes .  r - vector to store function value
773c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
774c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7759b94acceSBarry Smith 
776c7afd0dbSLois Curfman McInnes    Calling sequence of func:
7778d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
778c7afd0dbSLois Curfman McInnes 
779313e4042SLois Curfman McInnes .  f - function vector
780c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
7819b94acceSBarry Smith 
7829b94acceSBarry Smith    Notes:
7839b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
7849b94acceSBarry Smith $      f'(x) x = -f(x),
785c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
7869b94acceSBarry Smith 
7879b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7889b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7899b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
7909b94acceSBarry Smith 
79136851e7fSLois Curfman McInnes    Level: beginner
79236851e7fSLois Curfman McInnes 
7939b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
7949b94acceSBarry Smith 
795a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
7969b94acceSBarry Smith @*/
7975334005bSBarry Smith int SNESSetFunction(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
7989b94acceSBarry Smith {
7993a40ed3dSBarry Smith   PetscFunctionBegin;
80077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
801184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
802184914b5SBarry Smith   PetscCheckSameComm(snes,r);
803a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
80429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
805a8c6a408SBarry Smith   }
806184914b5SBarry Smith 
8079b94acceSBarry Smith   snes->computefunction     = func;
8089b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
8099b94acceSBarry Smith   snes->funP                = ctx;
8103a40ed3dSBarry Smith   PetscFunctionReturn(0);
8119b94acceSBarry Smith }
8129b94acceSBarry Smith 
8134a2ae208SSatish Balay #undef __FUNCT__
8144a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
8159b94acceSBarry Smith /*@
81636851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
8179b94acceSBarry Smith                          SNESSetFunction().
8189b94acceSBarry Smith 
819c7afd0dbSLois Curfman McInnes    Collective on SNES
820c7afd0dbSLois Curfman McInnes 
8219b94acceSBarry Smith    Input Parameters:
822c7afd0dbSLois Curfman McInnes +  snes - the SNES context
823c7afd0dbSLois Curfman McInnes -  x - input vector
8249b94acceSBarry Smith 
8259b94acceSBarry Smith    Output Parameter:
8263638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
8279b94acceSBarry Smith 
8281bffabb2SLois Curfman McInnes    Notes:
8299b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
8309b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
8319b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
8329b94acceSBarry Smith 
83336851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
83436851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
83536851e7fSLois Curfman McInnes    themselves.
83636851e7fSLois Curfman McInnes 
83736851e7fSLois Curfman McInnes    Level: developer
83836851e7fSLois Curfman McInnes 
8399b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
8409b94acceSBarry Smith 
841a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
8429b94acceSBarry Smith @*/
8439b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x,Vec y)
8449b94acceSBarry Smith {
8459b94acceSBarry Smith   int    ierr;
8469b94acceSBarry Smith 
8473a40ed3dSBarry Smith   PetscFunctionBegin;
848184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
849184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
850184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
851184914b5SBarry Smith   PetscCheckSameComm(snes,x);
852184914b5SBarry Smith   PetscCheckSameComm(snes,y);
853d4bb536fSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
85429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
855d4bb536fSBarry Smith   }
856184914b5SBarry Smith 
857d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
858d64ed03dSBarry Smith   PetscStackPush("SNES user function");
8599b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
860d64ed03dSBarry Smith   PetscStackPop;
861ae3c334cSLois Curfman McInnes   snes->nfuncs++;
862d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
8633a40ed3dSBarry Smith   PetscFunctionReturn(0);
8649b94acceSBarry Smith }
8659b94acceSBarry Smith 
8664a2ae208SSatish Balay #undef __FUNCT__
8674a2ae208SSatish Balay #define __FUNCT__ "SNESSetMinimizationFunction"
8689b94acceSBarry Smith /*@C
8699b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
8709b94acceSBarry Smith    unconstrained minimization.
8719b94acceSBarry Smith 
872fee21e36SBarry Smith    Collective on SNES
873fee21e36SBarry Smith 
874c7afd0dbSLois Curfman McInnes    Input Parameters:
875c7afd0dbSLois Curfman McInnes +  snes - the SNES context
876c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
877c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
878c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8799b94acceSBarry Smith 
880c7afd0dbSLois Curfman McInnes    Calling sequence of func:
881329f5518SBarry Smith $     func (SNES snes,Vec x,PetscReal *f,void *ctx);
882c7afd0dbSLois Curfman McInnes 
883c7afd0dbSLois Curfman McInnes +  x - input vector
8849b94acceSBarry Smith .  f - function
885c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined function context
8869b94acceSBarry Smith 
88736851e7fSLois Curfman McInnes    Level: beginner
88836851e7fSLois Curfman McInnes 
8899b94acceSBarry Smith    Notes:
8909b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
8919b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
8929b94acceSBarry Smith    SNESSetFunction().
8939b94acceSBarry Smith 
8949b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
8959b94acceSBarry Smith 
896a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
897a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
8989b94acceSBarry Smith @*/
899329f5518SBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,PetscReal*,void*),void *ctx)
9009b94acceSBarry Smith {
9013a40ed3dSBarry Smith   PetscFunctionBegin;
90277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
903a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
90429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
905a8c6a408SBarry Smith   }
9069b94acceSBarry Smith   snes->computeumfunction   = func;
9079b94acceSBarry Smith   snes->umfunP              = ctx;
9083a40ed3dSBarry Smith   PetscFunctionReturn(0);
9099b94acceSBarry Smith }
9109b94acceSBarry Smith 
9114a2ae208SSatish Balay #undef __FUNCT__
9124a2ae208SSatish Balay #define __FUNCT__ "SNESComputeMinimizationFunction"
9139b94acceSBarry Smith /*@
9149b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
9159b94acceSBarry Smith    set with SNESSetMinimizationFunction().
9169b94acceSBarry Smith 
917c7afd0dbSLois Curfman McInnes    Collective on SNES
918c7afd0dbSLois Curfman McInnes 
9199b94acceSBarry Smith    Input Parameters:
920c7afd0dbSLois Curfman McInnes +  snes - the SNES context
921c7afd0dbSLois Curfman McInnes -  x - input vector
9229b94acceSBarry Smith 
9239b94acceSBarry Smith    Output Parameter:
9249b94acceSBarry Smith .  y - function value
9259b94acceSBarry Smith 
9269b94acceSBarry Smith    Notes:
9279b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
9289b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
9299b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
930a86d99e1SLois Curfman McInnes 
93136851e7fSLois Curfman McInnes    SNESComputeMinimizationFunction() is typically used within minimization
93236851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
93336851e7fSLois Curfman McInnes    themselves.
93436851e7fSLois Curfman McInnes 
93536851e7fSLois Curfman McInnes    Level: developer
93636851e7fSLois Curfman McInnes 
937a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
938a86d99e1SLois Curfman McInnes 
939a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
940a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
9419b94acceSBarry Smith @*/
942329f5518SBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,PetscReal *y)
9439b94acceSBarry Smith {
9449b94acceSBarry Smith   int    ierr;
9453a40ed3dSBarry Smith 
9463a40ed3dSBarry Smith   PetscFunctionBegin;
947184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
948184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
949184914b5SBarry Smith   PetscCheckSameComm(snes,x);
950a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
95129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
952a8c6a408SBarry Smith   }
953184914b5SBarry Smith 
954d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);CHKERRQ(ierr);
955d64ed03dSBarry Smith   PetscStackPush("SNES user minimzation function");
9569b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr);
957d64ed03dSBarry Smith   PetscStackPop;
958ae3c334cSLois Curfman McInnes   snes->nfuncs++;
959d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);CHKERRQ(ierr);
9603a40ed3dSBarry Smith   PetscFunctionReturn(0);
9619b94acceSBarry Smith }
9629b94acceSBarry Smith 
9634a2ae208SSatish Balay #undef __FUNCT__
9644a2ae208SSatish Balay #define __FUNCT__ "SNESSetGradient"
9659b94acceSBarry Smith /*@C
9669b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
9679b94acceSBarry Smith    vector for use by the SNES routines.
9689b94acceSBarry Smith 
969c7afd0dbSLois Curfman McInnes    Collective on SNES
970c7afd0dbSLois Curfman McInnes 
9719b94acceSBarry Smith    Input Parameters:
972c7afd0dbSLois Curfman McInnes +  snes - the SNES context
9739b94acceSBarry Smith .  func - function evaluation routine
974044dda88SLois Curfman McInnes .  ctx - optional user-defined context for private data for the
975044dda88SLois Curfman McInnes          gradient evaluation routine (may be PETSC_NULL)
976c7afd0dbSLois Curfman McInnes -  r - vector to store gradient value
977fee21e36SBarry Smith 
9789b94acceSBarry Smith    Calling sequence of func:
9798d76a1e5SLois Curfman McInnes $     func (SNES, Vec x, Vec g, void *ctx);
9809b94acceSBarry Smith 
981c7afd0dbSLois Curfman McInnes +  x - input vector
9829b94acceSBarry Smith .  g - gradient vector
983c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined gradient context
9849b94acceSBarry Smith 
9859b94acceSBarry Smith    Notes:
9869b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
9879b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
9889b94acceSBarry Smith    SNESSetFunction().
9899b94acceSBarry Smith 
99036851e7fSLois Curfman McInnes    Level: beginner
99136851e7fSLois Curfman McInnes 
9929b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9939b94acceSBarry Smith 
994a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
995a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
9969b94acceSBarry Smith @*/
99774679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
9989b94acceSBarry Smith {
9993a40ed3dSBarry Smith   PetscFunctionBegin;
100077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1001184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
1002184914b5SBarry Smith   PetscCheckSameComm(snes,r);
1003a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
100429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1005a8c6a408SBarry Smith   }
10069b94acceSBarry Smith   snes->computefunction     = func;
10079b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
10089b94acceSBarry Smith   snes->funP                = ctx;
10093a40ed3dSBarry Smith   PetscFunctionReturn(0);
10109b94acceSBarry Smith }
10119b94acceSBarry Smith 
10124a2ae208SSatish Balay #undef __FUNCT__
10134a2ae208SSatish Balay #define __FUNCT__ "SNESComputeGradient"
10149b94acceSBarry Smith /*@
1015a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
1016a86d99e1SLois Curfman McInnes    SNESSetGradient().
10179b94acceSBarry Smith 
1018c7afd0dbSLois Curfman McInnes    Collective on SNES
1019c7afd0dbSLois Curfman McInnes 
10209b94acceSBarry Smith    Input Parameters:
1021c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1022c7afd0dbSLois Curfman McInnes -  x - input vector
10239b94acceSBarry Smith 
10249b94acceSBarry Smith    Output Parameter:
10259b94acceSBarry Smith .  y - gradient vector
10269b94acceSBarry Smith 
10279b94acceSBarry Smith    Notes:
10289b94acceSBarry Smith    SNESComputeGradient() is valid only for
10299b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
10309b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
1031a86d99e1SLois Curfman McInnes 
103236851e7fSLois Curfman McInnes    SNESComputeGradient() is typically used within minimization
103336851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
103436851e7fSLois Curfman McInnes    themselves.
103536851e7fSLois Curfman McInnes 
103636851e7fSLois Curfman McInnes    Level: developer
103736851e7fSLois Curfman McInnes 
1038a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
1039a86d99e1SLois Curfman McInnes 
1040a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
1041a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
10429b94acceSBarry Smith @*/
10439b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x,Vec y)
10449b94acceSBarry Smith {
10459b94acceSBarry Smith   int    ierr;
10463a40ed3dSBarry Smith 
10473a40ed3dSBarry Smith   PetscFunctionBegin;
1048184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1049184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1050184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
1051184914b5SBarry Smith   PetscCheckSameComm(snes,x);
1052184914b5SBarry Smith   PetscCheckSameComm(snes,y);
10533a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
105429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
10553a40ed3dSBarry Smith   }
1056d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_GradientEval,snes,x,y,0);CHKERRQ(ierr);
1057d64ed03dSBarry Smith   PetscStackPush("SNES user gradient function");
10589b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1059d64ed03dSBarry Smith   PetscStackPop;
1060d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_GradientEval,snes,x,y,0);CHKERRQ(ierr);
10613a40ed3dSBarry Smith   PetscFunctionReturn(0);
10629b94acceSBarry Smith }
10639b94acceSBarry Smith 
10644a2ae208SSatish Balay #undef __FUNCT__
10654a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
106662fef451SLois Curfman McInnes /*@
106762fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
106862fef451SLois Curfman McInnes    set with SNESSetJacobian().
106962fef451SLois Curfman McInnes 
1070c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1071c7afd0dbSLois Curfman McInnes 
107262fef451SLois Curfman McInnes    Input Parameters:
1073c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1074c7afd0dbSLois Curfman McInnes -  x - input vector
107562fef451SLois Curfman McInnes 
107662fef451SLois Curfman McInnes    Output Parameters:
1077c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
107862fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1079c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1080fee21e36SBarry Smith 
108162fef451SLois Curfman McInnes    Notes:
108262fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
108362fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
108462fef451SLois Curfman McInnes 
1085dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1086dc5a77f8SLois Curfman McInnes    flag parameter.
108762fef451SLois Curfman McInnes 
108862fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
108962fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
1090005c665bSBarry Smith    methods is SNESComputeHessian().
109162fef451SLois Curfman McInnes 
109236851e7fSLois Curfman McInnes    Level: developer
109336851e7fSLois Curfman McInnes 
109462fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
109562fef451SLois Curfman McInnes 
109662fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
109762fef451SLois Curfman McInnes @*/
10989b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10999b94acceSBarry Smith {
11009b94acceSBarry Smith   int    ierr;
11013a40ed3dSBarry Smith 
11023a40ed3dSBarry Smith   PetscFunctionBegin;
1103184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1104184914b5SBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE);
1105184914b5SBarry Smith   PetscCheckSameComm(snes,X);
11063a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
110729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
11083a40ed3dSBarry Smith   }
11093a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
1110d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1111c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1112d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
11139b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1114d64ed03dSBarry Smith   PetscStackPop;
1115d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
11166d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
111777c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
111877c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11193a40ed3dSBarry Smith   PetscFunctionReturn(0);
11209b94acceSBarry Smith }
11219b94acceSBarry Smith 
11224a2ae208SSatish Balay #undef __FUNCT__
11234a2ae208SSatish Balay #define __FUNCT__ "SNESComputeHessian"
112462fef451SLois Curfman McInnes /*@
112562fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
112662fef451SLois Curfman McInnes    set with SNESSetHessian().
112762fef451SLois Curfman McInnes 
1128c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1129c7afd0dbSLois Curfman McInnes 
113062fef451SLois Curfman McInnes    Input Parameters:
1131c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1132c7afd0dbSLois Curfman McInnes -  x - input vector
113362fef451SLois Curfman McInnes 
113462fef451SLois Curfman McInnes    Output Parameters:
1135c7afd0dbSLois Curfman McInnes +  A - Hessian matrix
113662fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1137c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1138fee21e36SBarry Smith 
113962fef451SLois Curfman McInnes    Notes:
114062fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
114162fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
114262fef451SLois Curfman McInnes 
1143dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1144dc5a77f8SLois Curfman McInnes    flag parameter.
114562fef451SLois Curfman McInnes 
114662fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
114762fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
114862fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
114962fef451SLois Curfman McInnes 
115036851e7fSLois Curfman McInnes    SNESComputeHessian() is typically used within minimization
115136851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
115236851e7fSLois Curfman McInnes    themselves.
115336851e7fSLois Curfman McInnes 
115436851e7fSLois Curfman McInnes    Level: developer
115536851e7fSLois Curfman McInnes 
115662fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
115762fef451SLois Curfman McInnes 
1158a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
1159a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
116062fef451SLois Curfman McInnes @*/
116162fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
11629b94acceSBarry Smith {
11639b94acceSBarry Smith   int    ierr;
11643a40ed3dSBarry Smith 
11653a40ed3dSBarry Smith   PetscFunctionBegin;
1166184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1167184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1168184914b5SBarry Smith   PetscCheckSameComm(snes,x);
11693a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
117029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
11713a40ed3dSBarry Smith   }
11723a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
1173d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_HessianEval,snes,x,*A,*B);CHKERRQ(ierr);
11740f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
1175d64ed03dSBarry Smith   PetscStackPush("SNES user Hessian function");
117662fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr);
1177d64ed03dSBarry Smith   PetscStackPop;
1178d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_HessianEval,snes,x,*A,*B);CHKERRQ(ierr);
11790f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
118077c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
118177c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11823a40ed3dSBarry Smith   PetscFunctionReturn(0);
11839b94acceSBarry Smith }
11849b94acceSBarry Smith 
11854a2ae208SSatish Balay #undef __FUNCT__
11864a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
11879b94acceSBarry Smith /*@C
11889b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1189044dda88SLois Curfman McInnes    location to store the matrix.
11909b94acceSBarry Smith 
1191c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1192c7afd0dbSLois Curfman McInnes 
11939b94acceSBarry Smith    Input Parameters:
1194c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11959b94acceSBarry Smith .  A - Jacobian matrix
11969b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11979b94acceSBarry Smith .  func - Jacobian evaluation routine
1198c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11992cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
12009b94acceSBarry Smith 
12019b94acceSBarry Smith    Calling sequence of func:
12028d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12039b94acceSBarry Smith 
1204c7afd0dbSLois Curfman McInnes +  x - input vector
12059b94acceSBarry Smith .  A - Jacobian matrix
12069b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1207ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1208ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1209c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
12109b94acceSBarry Smith 
12119b94acceSBarry Smith    Notes:
1212dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
12132cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1214ac21db08SLois Curfman McInnes 
1215ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
12169b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
12179b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12189b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12199b94acceSBarry Smith    throughout the global iterations.
12209b94acceSBarry Smith 
122136851e7fSLois Curfman McInnes    Level: beginner
122236851e7fSLois Curfman McInnes 
12239b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
12249b94acceSBarry Smith 
1225ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
12269b94acceSBarry Smith @*/
1227454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
12289b94acceSBarry Smith {
12293a7fca6bSBarry Smith   int ierr;
12303a7fca6bSBarry Smith 
12313a40ed3dSBarry Smith   PetscFunctionBegin;
123277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
123300036973SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE);
123400036973SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE);
123500036973SBarry Smith   if (A) PetscCheckSameComm(snes,A);
123600036973SBarry Smith   if (B) PetscCheckSameComm(snes,B);
1237a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
123829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
1239a8c6a408SBarry Smith   }
1240184914b5SBarry Smith 
12413a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
12423a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
12433a7fca6bSBarry Smith   if (A) {
12443a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12459b94acceSBarry Smith     snes->jacobian = A;
12463a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
12473a7fca6bSBarry Smith   }
12483a7fca6bSBarry Smith   if (B) {
12493a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12509b94acceSBarry Smith     snes->jacobian_pre = B;
12513a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
12523a7fca6bSBarry Smith   }
12533a40ed3dSBarry Smith   PetscFunctionReturn(0);
12549b94acceSBarry Smith }
125562fef451SLois Curfman McInnes 
12564a2ae208SSatish Balay #undef __FUNCT__
12574a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1258c2aafc4cSSatish Balay /*@C
1259b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1260b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1261b4fd4287SBarry Smith 
1262c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1263c7afd0dbSLois Curfman McInnes 
1264b4fd4287SBarry Smith    Input Parameter:
1265b4fd4287SBarry Smith .  snes - the nonlinear solver context
1266b4fd4287SBarry Smith 
1267b4fd4287SBarry Smith    Output Parameters:
1268c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1269b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
127000036973SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
127100036973SBarry Smith -  func - location to put Jacobian function (or PETSC_NULL)
1272fee21e36SBarry Smith 
127336851e7fSLois Curfman McInnes    Level: advanced
127436851e7fSLois Curfman McInnes 
1275b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1276b4fd4287SBarry Smith @*/
127700036973SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx,int (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*))
1278b4fd4287SBarry Smith {
12793a40ed3dSBarry Smith   PetscFunctionBegin;
1280184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
12813a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
128229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
12833a40ed3dSBarry Smith   }
1284b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1285b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1286b4fd4287SBarry Smith   if (ctx)  *ctx  = snes->jacP;
128700036973SBarry Smith   if (func) *func = snes->computejacobian;
12883a40ed3dSBarry Smith   PetscFunctionReturn(0);
1289b4fd4287SBarry Smith }
1290b4fd4287SBarry Smith 
12914a2ae208SSatish Balay #undef __FUNCT__
12924a2ae208SSatish Balay #define __FUNCT__ "SNESSetHessian"
12939b94acceSBarry Smith /*@C
12949b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
1295044dda88SLois Curfman McInnes    location to store the matrix.
12969b94acceSBarry Smith 
1297c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1298c7afd0dbSLois Curfman McInnes 
12999b94acceSBarry Smith    Input Parameters:
1300c7afd0dbSLois Curfman McInnes +  snes - the SNES context
13019b94acceSBarry Smith .  A - Hessian matrix
13029b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
13039b94acceSBarry Smith .  func - Jacobian evaluation routine
1304c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1305313e4042SLois Curfman McInnes          Hessian evaluation routine (may be PETSC_NULL)
13069b94acceSBarry Smith 
13079b94acceSBarry Smith    Calling sequence of func:
13088d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
13099b94acceSBarry Smith 
1310c7afd0dbSLois Curfman McInnes +  x - input vector
13119b94acceSBarry Smith .  A - Hessian matrix
13129b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1313ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1314ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1315c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Hessian context
13169b94acceSBarry Smith 
13179b94acceSBarry Smith    Notes:
1318dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
13192cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1320ac21db08SLois Curfman McInnes 
13219b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
13229b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
13239b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
13249b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
13259b94acceSBarry Smith    throughout the global iterations.
13269b94acceSBarry Smith 
132736851e7fSLois Curfman McInnes    Level: beginner
132836851e7fSLois Curfman McInnes 
13299b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
13309b94acceSBarry Smith 
1331ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
13329b94acceSBarry Smith @*/
1333454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
13349b94acceSBarry Smith {
13353a7fca6bSBarry Smith   int ierr;
13363a7fca6bSBarry Smith 
13373a40ed3dSBarry Smith   PetscFunctionBegin;
133877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1339184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
1340184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
1341184914b5SBarry Smith   PetscCheckSameComm(snes,A);
1342184914b5SBarry Smith   PetscCheckSameComm(snes,B);
1343d4bb536fSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
134429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1345d4bb536fSBarry Smith   }
13463a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
13473a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
13483a7fca6bSBarry Smith   if (A) {
13493a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13509b94acceSBarry Smith     snes->jacobian = A;
13513a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
13523a7fca6bSBarry Smith   }
13533a7fca6bSBarry Smith   if (B) {
13543a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13559b94acceSBarry Smith     snes->jacobian_pre = B;
13563a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
13573a7fca6bSBarry Smith   }
13583a40ed3dSBarry Smith   PetscFunctionReturn(0);
13599b94acceSBarry Smith }
13609b94acceSBarry Smith 
13614a2ae208SSatish Balay #undef __FUNCT__
13624a2ae208SSatish Balay #define __FUNCT__ "SNESGetHessian"
136362fef451SLois Curfman McInnes /*@
136462fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
136562fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
136662fef451SLois Curfman McInnes 
1367c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object is parallel if SNES object is parallel
1368c7afd0dbSLois Curfman McInnes 
136962fef451SLois Curfman McInnes    Input Parameter:
137062fef451SLois Curfman McInnes .  snes - the nonlinear solver context
137162fef451SLois Curfman McInnes 
137262fef451SLois Curfman McInnes    Output Parameters:
1373c7afd0dbSLois Curfman McInnes +  A - location to stash Hessian matrix (or PETSC_NULL)
137462fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
1375c7afd0dbSLois Curfman McInnes -  ctx - location to stash Hessian ctx (or PETSC_NULL)
1376fee21e36SBarry Smith 
137736851e7fSLois Curfman McInnes    Level: advanced
137836851e7fSLois Curfman McInnes 
137962fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
1380c7afd0dbSLois Curfman McInnes 
1381c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian
138262fef451SLois Curfman McInnes @*/
138362fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B,void **ctx)
138462fef451SLois Curfman McInnes {
13853a40ed3dSBarry Smith   PetscFunctionBegin;
1386184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
13873a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){
138829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
13893a40ed3dSBarry Smith   }
139062fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
139162fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
139262fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
13933a40ed3dSBarry Smith   PetscFunctionReturn(0);
139462fef451SLois Curfman McInnes }
139562fef451SLois Curfman McInnes 
13969b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13979b94acceSBarry Smith 
13984a2ae208SSatish Balay #undef __FUNCT__
13994a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
14009b94acceSBarry Smith /*@
14019b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1402272ac6f2SLois Curfman McInnes    of a nonlinear solver.
14039b94acceSBarry Smith 
1404fee21e36SBarry Smith    Collective on SNES
1405fee21e36SBarry Smith 
1406c7afd0dbSLois Curfman McInnes    Input Parameters:
1407c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1408c7afd0dbSLois Curfman McInnes -  x - the solution vector
1409c7afd0dbSLois Curfman McInnes 
1410272ac6f2SLois Curfman McInnes    Notes:
1411272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1412272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1413272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1414272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1415272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1416272ac6f2SLois Curfman McInnes 
141736851e7fSLois Curfman McInnes    Level: advanced
141836851e7fSLois Curfman McInnes 
14199b94acceSBarry Smith .keywords: SNES, nonlinear, setup
14209b94acceSBarry Smith 
14219b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
14229b94acceSBarry Smith @*/
14238ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
14249b94acceSBarry Smith {
1425f1af5d2fSBarry Smith   int        ierr;
1426f1af5d2fSBarry Smith   PetscTruth flg;
14273a40ed3dSBarry Smith 
14283a40ed3dSBarry Smith   PetscFunctionBegin;
142977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
143077c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1431184914b5SBarry Smith   PetscCheckSameComm(snes,x);
14328ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
14339b94acceSBarry Smith 
1434b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1435c1f60f51SBarry Smith   /*
1436c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1437dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1438c1f60f51SBarry Smith   */
1439c1f60f51SBarry Smith   if (flg) {
1440c1f60f51SBarry Smith     Mat J;
14415a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
14425a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
14433a7fca6bSBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator routines\n");
14443a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
14453a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1446c1f60f51SBarry Smith   }
1447b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1448c1f60f51SBarry Smith   /*
1449dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1450c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1451c1f60f51SBarry Smith    */
145231615d04SLois Curfman McInnes   if (flg) {
1453272ac6f2SLois Curfman McInnes     Mat  J;
1454f3ef73ceSBarry Smith     SLES sles;
1455f3ef73ceSBarry Smith     PC   pc;
1456f3ef73ceSBarry Smith 
14575a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
14583a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
145993b98531SBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator and preconditioner routines\n");
146083e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
14613a7fca6bSBarry Smith       ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
1462d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
14633a7fca6bSBarry Smith       ierr = SNESSetHessian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
1464d4bb536fSBarry Smith     } else {
146529bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Method class doesn't support matrix-free option");
1466d4bb536fSBarry Smith     }
14673a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
14683a7fca6bSBarry Smith 
1469f3ef73ceSBarry Smith     /* force no preconditioner */
1470f3ef73ceSBarry Smith     ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1471f3ef73ceSBarry Smith     ierr = SLESGetPC(sles,&pc);CHKERRQ(ierr);
1472f3ef73ceSBarry Smith     ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1473272ac6f2SLois Curfman McInnes   }
1474f3ef73ceSBarry Smith 
14759b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
14766831982aSBarry Smith     PetscTruth iseqtr;
14776831982aSBarry Smith 
147829bbc08cSBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
147929bbc08cSBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
148029bbc08cSBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1481a8c6a408SBarry Smith     if (snes->vec_func == snes->vec_sol) {
148229bbc08cSBarry Smith       SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1483a8c6a408SBarry Smith     }
1484a703fe33SLois Curfman McInnes 
1485a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
14866831982aSBarry Smith     ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr);
14876831982aSBarry Smith     if (snes->ksp_ewconv && !iseqtr) {
1488a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1489a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1490a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
1491186905e3SBarry Smith       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
1492a703fe33SLois Curfman McInnes     }
14939b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
149429bbc08cSBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetGradient() first");
149529bbc08cSBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetGradient() first");
1496a8c6a408SBarry Smith     if (!snes->computeumfunction) {
149729bbc08cSBarry Smith       SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetMinimizationFunction() first");
1498a8c6a408SBarry Smith     }
149929bbc08cSBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetHessian()");
1500d4bb536fSBarry Smith   } else {
150129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown method class");
1502d4bb536fSBarry Smith   }
1503a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
150482bf6240SBarry Smith   snes->setupcalled = 1;
15053a40ed3dSBarry Smith   PetscFunctionReturn(0);
15069b94acceSBarry Smith }
15079b94acceSBarry Smith 
15084a2ae208SSatish Balay #undef __FUNCT__
15094a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
15109b94acceSBarry Smith /*@C
15119b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
15129b94acceSBarry Smith    with SNESCreate().
15139b94acceSBarry Smith 
1514c7afd0dbSLois Curfman McInnes    Collective on SNES
1515c7afd0dbSLois Curfman McInnes 
15169b94acceSBarry Smith    Input Parameter:
15179b94acceSBarry Smith .  snes - the SNES context
15189b94acceSBarry Smith 
151936851e7fSLois Curfman McInnes    Level: beginner
152036851e7fSLois Curfman McInnes 
15219b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
15229b94acceSBarry Smith 
152363a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
15249b94acceSBarry Smith @*/
15259b94acceSBarry Smith int SNESDestroy(SNES snes)
15269b94acceSBarry Smith {
1527b8a78c4aSBarry Smith   int i,ierr;
15283a40ed3dSBarry Smith 
15293a40ed3dSBarry Smith   PetscFunctionBegin;
153077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15313a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1532d4bb536fSBarry Smith 
1533be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
15340f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1535be0abb6dSBarry Smith 
1536e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1537606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
15383a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
15393a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
15409b94acceSBarry Smith   ierr = SLESDestroy(snes->sles);CHKERRQ(ierr);
1541522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1542b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1543b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1544b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1545b8a78c4aSBarry Smith     }
1546b8a78c4aSBarry Smith   }
1547b0a32e0cSBarry Smith   PetscLogObjectDestroy((PetscObject)snes);
15480452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
15493a40ed3dSBarry Smith   PetscFunctionReturn(0);
15509b94acceSBarry Smith }
15519b94acceSBarry Smith 
15529b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
15539b94acceSBarry Smith 
15544a2ae208SSatish Balay #undef __FUNCT__
15554a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15569b94acceSBarry Smith /*@
1557d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15589b94acceSBarry Smith 
1559c7afd0dbSLois Curfman McInnes    Collective on SNES
1560c7afd0dbSLois Curfman McInnes 
15619b94acceSBarry Smith    Input Parameters:
1562c7afd0dbSLois Curfman McInnes +  snes - the SNES context
156333174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
156433174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
156533174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
156633174efeSLois Curfman McInnes            of the change in the solution between steps
156733174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1568c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1569fee21e36SBarry Smith 
157033174efeSLois Curfman McInnes    Options Database Keys:
1571c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1572c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1573c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1574c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1575c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15769b94acceSBarry Smith 
1577d7a720efSLois Curfman McInnes    Notes:
15789b94acceSBarry Smith    The default maximum number of iterations is 50.
15799b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15809b94acceSBarry Smith 
158136851e7fSLois Curfman McInnes    Level: intermediate
158236851e7fSLois Curfman McInnes 
158333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15849b94acceSBarry Smith 
1585d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
15869b94acceSBarry Smith @*/
1587329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf)
15889b94acceSBarry Smith {
15893a40ed3dSBarry Smith   PetscFunctionBegin;
159077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1591d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1592d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1593d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1594d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1595d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15963a40ed3dSBarry Smith   PetscFunctionReturn(0);
15979b94acceSBarry Smith }
15989b94acceSBarry Smith 
15994a2ae208SSatish Balay #undef __FUNCT__
16004a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
16019b94acceSBarry Smith /*@
160233174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
160333174efeSLois Curfman McInnes 
1604c7afd0dbSLois Curfman McInnes    Not Collective
1605c7afd0dbSLois Curfman McInnes 
160633174efeSLois Curfman McInnes    Input Parameters:
1607c7afd0dbSLois Curfman McInnes +  snes - the SNES context
160833174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
160933174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
161033174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
161133174efeSLois Curfman McInnes            of the change in the solution between steps
161233174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1613c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1614fee21e36SBarry Smith 
161533174efeSLois Curfman McInnes    Notes:
161633174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
161733174efeSLois Curfman McInnes 
161836851e7fSLois Curfman McInnes    Level: intermediate
161936851e7fSLois Curfman McInnes 
162033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
162133174efeSLois Curfman McInnes 
162233174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
162333174efeSLois Curfman McInnes @*/
1624329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf)
162533174efeSLois Curfman McInnes {
16263a40ed3dSBarry Smith   PetscFunctionBegin;
162733174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
162833174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
162933174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
163033174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
163133174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
163233174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
16333a40ed3dSBarry Smith   PetscFunctionReturn(0);
163433174efeSLois Curfman McInnes }
163533174efeSLois Curfman McInnes 
16364a2ae208SSatish Balay #undef __FUNCT__
16374a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
163833174efeSLois Curfman McInnes /*@
16399b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16409b94acceSBarry Smith 
1641fee21e36SBarry Smith    Collective on SNES
1642fee21e36SBarry Smith 
1643c7afd0dbSLois Curfman McInnes    Input Parameters:
1644c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1645c7afd0dbSLois Curfman McInnes -  tol - tolerance
1646c7afd0dbSLois Curfman McInnes 
16479b94acceSBarry Smith    Options Database Key:
1648c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16499b94acceSBarry Smith 
165036851e7fSLois Curfman McInnes    Level: intermediate
165136851e7fSLois Curfman McInnes 
16529b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16539b94acceSBarry Smith 
1654d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
16559b94acceSBarry Smith @*/
1656329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16579b94acceSBarry Smith {
16583a40ed3dSBarry Smith   PetscFunctionBegin;
165977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16609b94acceSBarry Smith   snes->deltatol = tol;
16613a40ed3dSBarry Smith   PetscFunctionReturn(0);
16629b94acceSBarry Smith }
16639b94acceSBarry Smith 
16644a2ae208SSatish Balay #undef __FUNCT__
16654a2ae208SSatish Balay #define __FUNCT__ "SNESSetMinimizationFunctionTolerance"
16669b94acceSBarry Smith /*@
166777c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
16689b94acceSBarry Smith    for unconstrained minimization solvers.
16699b94acceSBarry Smith 
1670fee21e36SBarry Smith    Collective on SNES
1671fee21e36SBarry Smith 
1672c7afd0dbSLois Curfman McInnes    Input Parameters:
1673c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1674c7afd0dbSLois Curfman McInnes -  ftol - minimum function tolerance
1675c7afd0dbSLois Curfman McInnes 
16769b94acceSBarry Smith    Options Database Key:
1677c7afd0dbSLois Curfman McInnes .  -snes_fmin <ftol> - Sets ftol
16789b94acceSBarry Smith 
16799b94acceSBarry Smith    Note:
168077c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
16819b94acceSBarry Smith    methods only.
16829b94acceSBarry Smith 
168336851e7fSLois Curfman McInnes    Level: intermediate
168436851e7fSLois Curfman McInnes 
16859b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
16869b94acceSBarry Smith 
1687d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
16889b94acceSBarry Smith @*/
1689329f5518SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,PetscReal ftol)
16909b94acceSBarry Smith {
16913a40ed3dSBarry Smith   PetscFunctionBegin;
169277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16939b94acceSBarry Smith   snes->fmin = ftol;
16943a40ed3dSBarry Smith   PetscFunctionReturn(0);
16959b94acceSBarry Smith }
1696df9fa365SBarry Smith /*
1697df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1698df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1699df9fa365SBarry Smith    macros instead of functions
1700df9fa365SBarry Smith */
17014a2ae208SSatish Balay #undef __FUNCT__
17024a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitor"
1703329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx)
1704ce1608b8SBarry Smith {
1705ce1608b8SBarry Smith   int ierr;
1706ce1608b8SBarry Smith 
1707ce1608b8SBarry Smith   PetscFunctionBegin;
1708184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1709ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1710ce1608b8SBarry Smith   PetscFunctionReturn(0);
1711ce1608b8SBarry Smith }
1712ce1608b8SBarry Smith 
17134a2ae208SSatish Balay #undef __FUNCT__
17144a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorCreate"
1715b0a32e0cSBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,PetscDrawLG *draw)
1716df9fa365SBarry Smith {
1717df9fa365SBarry Smith   int ierr;
1718df9fa365SBarry Smith 
1719df9fa365SBarry Smith   PetscFunctionBegin;
1720df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1721df9fa365SBarry Smith   PetscFunctionReturn(0);
1722df9fa365SBarry Smith }
1723df9fa365SBarry Smith 
17244a2ae208SSatish Balay #undef __FUNCT__
17254a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorDestroy"
1726b0a32e0cSBarry Smith int SNESLGMonitorDestroy(PetscDrawLG draw)
1727df9fa365SBarry Smith {
1728df9fa365SBarry Smith   int ierr;
1729df9fa365SBarry Smith 
1730df9fa365SBarry Smith   PetscFunctionBegin;
1731df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1732df9fa365SBarry Smith   PetscFunctionReturn(0);
1733df9fa365SBarry Smith }
1734df9fa365SBarry Smith 
17359b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
17369b94acceSBarry Smith 
17374a2ae208SSatish Balay #undef __FUNCT__
17384a2ae208SSatish Balay #define __FUNCT__ "SNESSetMonitor"
17399b94acceSBarry Smith /*@C
17405cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
17419b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
17429b94acceSBarry Smith    progress.
17439b94acceSBarry Smith 
1744fee21e36SBarry Smith    Collective on SNES
1745fee21e36SBarry Smith 
1746c7afd0dbSLois Curfman McInnes    Input Parameters:
1747c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1748c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1749b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1750b3006f0bSLois Curfman McInnes           monitor routine (use PETSC_NULL if no context is desitre)
1751b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1752b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
17539b94acceSBarry Smith 
1754c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1755329f5518SBarry Smith $     int func(SNES snes,int its, PetscReal norm,void *mctx)
1756c7afd0dbSLois Curfman McInnes 
1757c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1758c7afd0dbSLois Curfman McInnes .    its - iteration number
1759c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
1760c7afd0dbSLois Curfman McInnes             for SNES_NONLINEAR_EQUATIONS methods
176140a0c1c6SLois Curfman McInnes .    norm - 2-norm gradient value (may be estimated)
1762c7afd0dbSLois Curfman McInnes             for SNES_UNCONSTRAINED_MINIMIZATION methods
176340a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
17649b94acceSBarry Smith 
17659665c990SLois Curfman McInnes    Options Database Keys:
1766c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1767c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1768c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1769c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1770c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1771c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1772c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1773c7afd0dbSLois Curfman McInnes                             the options database.
17749665c990SLois Curfman McInnes 
1775639f9d9dSBarry Smith    Notes:
17766bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
17776bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
17786bc08f3fSLois Curfman McInnes    order in which they were set.
1779639f9d9dSBarry Smith 
178036851e7fSLois Curfman McInnes    Level: intermediate
178136851e7fSLois Curfman McInnes 
17829b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
17839b94acceSBarry Smith 
17845cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
17859b94acceSBarry Smith @*/
1786329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *))
17879b94acceSBarry Smith {
17883a40ed3dSBarry Smith   PetscFunctionBegin;
1789184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1790639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
179129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1792639f9d9dSBarry Smith   }
1793639f9d9dSBarry Smith 
1794639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1795b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1796639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
17973a40ed3dSBarry Smith   PetscFunctionReturn(0);
17989b94acceSBarry Smith }
17999b94acceSBarry Smith 
18004a2ae208SSatish Balay #undef __FUNCT__
18014a2ae208SSatish Balay #define __FUNCT__ "SNESClearMonitor"
18025cd90555SBarry Smith /*@C
18035cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
18045cd90555SBarry Smith 
1805c7afd0dbSLois Curfman McInnes    Collective on SNES
1806c7afd0dbSLois Curfman McInnes 
18075cd90555SBarry Smith    Input Parameters:
18085cd90555SBarry Smith .  snes - the SNES context
18095cd90555SBarry Smith 
18105cd90555SBarry Smith    Options Database:
1811c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1812c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1813c7afd0dbSLois Curfman McInnes     set via the options database
18145cd90555SBarry Smith 
18155cd90555SBarry Smith    Notes:
18165cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
18175cd90555SBarry Smith 
181836851e7fSLois Curfman McInnes    Level: intermediate
181936851e7fSLois Curfman McInnes 
18205cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
18215cd90555SBarry Smith 
18225cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
18235cd90555SBarry Smith @*/
18245cd90555SBarry Smith int SNESClearMonitor(SNES snes)
18255cd90555SBarry Smith {
18265cd90555SBarry Smith   PetscFunctionBegin;
1827184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18285cd90555SBarry Smith   snes->numbermonitors = 0;
18295cd90555SBarry Smith   PetscFunctionReturn(0);
18305cd90555SBarry Smith }
18315cd90555SBarry Smith 
18324a2ae208SSatish Balay #undef __FUNCT__
18334a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
18349b94acceSBarry Smith /*@C
18359b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
18369b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
18379b94acceSBarry Smith 
1838fee21e36SBarry Smith    Collective on SNES
1839fee21e36SBarry Smith 
1840c7afd0dbSLois Curfman McInnes    Input Parameters:
1841c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1842c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1843c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1844c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
18459b94acceSBarry Smith 
1846c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1847329f5518SBarry Smith $     int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1848c7afd0dbSLois Curfman McInnes 
1849c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1850c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1851184914b5SBarry Smith .    reason - reason for convergence/divergence
1852c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1853c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods)
1854c7afd0dbSLois Curfman McInnes .    f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods)
1855c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods)
1856c7afd0dbSLois Curfman McInnes -    f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods)
18579b94acceSBarry Smith 
185836851e7fSLois Curfman McInnes    Level: advanced
185936851e7fSLois Curfman McInnes 
18609b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
18619b94acceSBarry Smith 
186240191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
186340191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
18649b94acceSBarry Smith @*/
1865329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
18669b94acceSBarry Smith {
18673a40ed3dSBarry Smith   PetscFunctionBegin;
1868184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18699b94acceSBarry Smith   (snes)->converged = func;
18709b94acceSBarry Smith   (snes)->cnvP      = cctx;
18713a40ed3dSBarry Smith   PetscFunctionReturn(0);
18729b94acceSBarry Smith }
18739b94acceSBarry Smith 
18744a2ae208SSatish Balay #undef __FUNCT__
18754a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
1876184914b5SBarry Smith /*@C
1877184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1878184914b5SBarry Smith 
1879184914b5SBarry Smith    Not Collective
1880184914b5SBarry Smith 
1881184914b5SBarry Smith    Input Parameter:
1882184914b5SBarry Smith .  snes - the SNES context
1883184914b5SBarry Smith 
1884184914b5SBarry Smith    Output Parameter:
1885e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1886184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1887184914b5SBarry Smith 
1888184914b5SBarry Smith    Level: intermediate
1889184914b5SBarry Smith 
1890184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1891184914b5SBarry Smith 
1892184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1893184914b5SBarry Smith 
1894184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
1895435da068SBarry Smith           SNESConverged_UM_LS(), SNESConverged_UM_TR(), SNESConvergedReason
1896184914b5SBarry Smith @*/
1897184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1898184914b5SBarry Smith {
1899184914b5SBarry Smith   PetscFunctionBegin;
1900184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1901184914b5SBarry Smith   *reason = snes->reason;
1902184914b5SBarry Smith   PetscFunctionReturn(0);
1903184914b5SBarry Smith }
1904184914b5SBarry Smith 
19054a2ae208SSatish Balay #undef __FUNCT__
19064a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1907c9005455SLois Curfman McInnes /*@
1908c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1909c9005455SLois Curfman McInnes 
1910fee21e36SBarry Smith    Collective on SNES
1911fee21e36SBarry Smith 
1912c7afd0dbSLois Curfman McInnes    Input Parameters:
1913c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1914c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1915cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1916758f92a0SBarry Smith .  na  - size of a and its
191764731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1918758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1919c7afd0dbSLois Curfman McInnes 
1920c9005455SLois Curfman McInnes    Notes:
1921c9005455SLois Curfman McInnes    If set, this array will contain the function norms (for
1922c9005455SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods) or gradient norms
1923c9005455SLois Curfman McInnes    (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed
1924c9005455SLois Curfman McInnes    at each step.
1925c9005455SLois Curfman McInnes 
1926c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1927c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1928c9005455SLois Curfman McInnes    during the section of code that is being timed.
1929c9005455SLois Curfman McInnes 
193036851e7fSLois Curfman McInnes    Level: intermediate
193136851e7fSLois Curfman McInnes 
1932c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1933758f92a0SBarry Smith 
193408405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1935758f92a0SBarry Smith 
1936c9005455SLois Curfman McInnes @*/
1937329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset)
1938c9005455SLois Curfman McInnes {
19393a40ed3dSBarry Smith   PetscFunctionBegin;
1940c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1941c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1942c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1943758f92a0SBarry Smith   snes->conv_hist_its   = its;
1944758f92a0SBarry Smith   snes->conv_hist_max   = na;
1945758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1946758f92a0SBarry Smith   PetscFunctionReturn(0);
1947758f92a0SBarry Smith }
1948758f92a0SBarry Smith 
19494a2ae208SSatish Balay #undef __FUNCT__
19504a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
19510c4c9dddSBarry Smith /*@C
1952758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1953758f92a0SBarry Smith 
1954758f92a0SBarry Smith    Collective on SNES
1955758f92a0SBarry Smith 
1956758f92a0SBarry Smith    Input Parameter:
1957758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1958758f92a0SBarry Smith 
1959758f92a0SBarry Smith    Output Parameters:
1960758f92a0SBarry Smith .  a   - array to hold history
1961758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1962758f92a0SBarry Smith          negative if not converged) for each solve.
1963758f92a0SBarry Smith -  na  - size of a and its
1964758f92a0SBarry Smith 
1965758f92a0SBarry Smith    Notes:
1966758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1967758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1968758f92a0SBarry Smith 
1969758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1970758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1971758f92a0SBarry Smith    during the section of code that is being timed.
1972758f92a0SBarry Smith 
1973758f92a0SBarry Smith    Level: intermediate
1974758f92a0SBarry Smith 
1975758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1976758f92a0SBarry Smith 
1977758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1978758f92a0SBarry Smith 
1979758f92a0SBarry Smith @*/
1980329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na)
1981758f92a0SBarry Smith {
1982758f92a0SBarry Smith   PetscFunctionBegin;
1983758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1984758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1985758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1986758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
19873a40ed3dSBarry Smith   PetscFunctionReturn(0);
1988c9005455SLois Curfman McInnes }
1989c9005455SLois Curfman McInnes 
1990e74ef692SMatthew Knepley #undef __FUNCT__
1991e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetRhsBC"
199276b2cf59SMatthew Knepley /*@
199376b2cf59SMatthew Knepley   SNESSetRhsBC - Sets the function which applies boundary conditions
199476b2cf59SMatthew Knepley   to the Rhs of each system.
199576b2cf59SMatthew Knepley 
199676b2cf59SMatthew Knepley   Collective on SNES
199776b2cf59SMatthew Knepley 
199876b2cf59SMatthew Knepley   Input Parameters:
199976b2cf59SMatthew Knepley . snes - The nonlinear solver context
200076b2cf59SMatthew Knepley . func - The function
200176b2cf59SMatthew Knepley 
200276b2cf59SMatthew Knepley   Calling sequence of func:
200376b2cf59SMatthew Knepley . func (SNES snes, Vec rhs, void *ctx);
200476b2cf59SMatthew Knepley 
200576b2cf59SMatthew Knepley . rhs - The current rhs vector
200676b2cf59SMatthew Knepley . ctx - The user-context
200776b2cf59SMatthew Knepley 
200876b2cf59SMatthew Knepley   Level: intermediate
200976b2cf59SMatthew Knepley 
201076b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
201176b2cf59SMatthew Knepley .seealso SNESDefaultRhsBC(), SNESSetSolutionBC(), SNESSetUpdate()
201276b2cf59SMatthew Knepley @*/
201376b2cf59SMatthew Knepley int SNESSetRhsBC(SNES snes, int (*func)(SNES, Vec, void *))
201476b2cf59SMatthew Knepley {
201576b2cf59SMatthew Knepley   PetscFunctionBegin;
201676b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
201776b2cf59SMatthew Knepley   snes->applyrhsbc = func;
201876b2cf59SMatthew Knepley   PetscFunctionReturn(0);
201976b2cf59SMatthew Knepley }
202076b2cf59SMatthew Knepley 
2021e74ef692SMatthew Knepley #undef __FUNCT__
2022e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultRhsBC"
202376b2cf59SMatthew Knepley /*@
202476b2cf59SMatthew Knepley   SNESDefaultRhsBC - The default boundary condition function which does nothing.
202576b2cf59SMatthew Knepley 
202676b2cf59SMatthew Knepley   Not collective
202776b2cf59SMatthew Knepley 
202876b2cf59SMatthew Knepley   Input Parameters:
202976b2cf59SMatthew Knepley . snes - The nonlinear solver context
203076b2cf59SMatthew Knepley . rhs  - The Rhs
203176b2cf59SMatthew Knepley . ctx  - The user-context
203276b2cf59SMatthew Knepley 
203376b2cf59SMatthew Knepley   Level: beginner
203476b2cf59SMatthew Knepley 
203576b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
203676b2cf59SMatthew Knepley .seealso SNESSetRhsBC(), SNESDefaultSolutionBC(), SNESDefaultUpdate()
203776b2cf59SMatthew Knepley @*/
203876b2cf59SMatthew Knepley int SNESDefaultRhsBC(SNES snes, Vec rhs, void *ctx)
203976b2cf59SMatthew Knepley {
204076b2cf59SMatthew Knepley   PetscFunctionBegin;
204176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
204276b2cf59SMatthew Knepley }
204376b2cf59SMatthew Knepley 
2044e74ef692SMatthew Knepley #undef __FUNCT__
2045e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetSolutionBC"
204676b2cf59SMatthew Knepley /*@
204776b2cf59SMatthew Knepley   SNESSetSolutionBC - Sets the function which applies boundary conditions
204876b2cf59SMatthew Knepley   to the solution of each system.
204976b2cf59SMatthew Knepley 
205076b2cf59SMatthew Knepley   Collective on SNES
205176b2cf59SMatthew Knepley 
205276b2cf59SMatthew Knepley   Input Parameters:
205376b2cf59SMatthew Knepley . snes - The nonlinear solver context
205476b2cf59SMatthew Knepley . func - The function
205576b2cf59SMatthew Knepley 
205676b2cf59SMatthew Knepley   Calling sequence of func:
205776b2cf59SMatthew Knepley . func (TS ts, Vec rsol, void *ctx);
205876b2cf59SMatthew Knepley 
205976b2cf59SMatthew Knepley . sol - The current solution vector
206076b2cf59SMatthew Knepley . ctx - The user-context
206176b2cf59SMatthew Knepley 
206276b2cf59SMatthew Knepley   Level: intermediate
206376b2cf59SMatthew Knepley 
206476b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
206576b2cf59SMatthew Knepley .seealso SNESDefaultSolutionBC(), SNESSetRhsBC(), SNESSetUpdate()
206676b2cf59SMatthew Knepley @*/
206776b2cf59SMatthew Knepley int SNESSetSolutionBC(SNES snes, int (*func)(SNES, Vec, void *))
206876b2cf59SMatthew Knepley {
206976b2cf59SMatthew Knepley   PetscFunctionBegin;
207076b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
207176b2cf59SMatthew Knepley   snes->applysolbc = func;
207276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
207376b2cf59SMatthew Knepley }
207476b2cf59SMatthew Knepley 
2075e74ef692SMatthew Knepley #undef __FUNCT__
2076e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultSolutionBC"
207776b2cf59SMatthew Knepley /*@
207876b2cf59SMatthew Knepley   SNESDefaultSolutionBC - The default boundary condition function which does nothing.
207976b2cf59SMatthew Knepley 
208076b2cf59SMatthew Knepley   Not collective
208176b2cf59SMatthew Knepley 
208276b2cf59SMatthew Knepley   Input Parameters:
208376b2cf59SMatthew Knepley . snes - The nonlinear solver context
208476b2cf59SMatthew Knepley . sol  - The solution
208576b2cf59SMatthew Knepley . ctx  - The user-context
208676b2cf59SMatthew Knepley 
208776b2cf59SMatthew Knepley   Level: beginner
208876b2cf59SMatthew Knepley 
208976b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
209076b2cf59SMatthew Knepley .seealso SNESSetSolutionBC(), SNESDefaultRhsBC(), SNESDefaultUpdate()
209176b2cf59SMatthew Knepley @*/
209276b2cf59SMatthew Knepley int SNESDefaultSolutionBC(SNES snes, Vec sol, void *ctx)
209376b2cf59SMatthew Knepley {
209476b2cf59SMatthew Knepley   PetscFunctionBegin;
209576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
209676b2cf59SMatthew Knepley }
209776b2cf59SMatthew Knepley 
2098e74ef692SMatthew Knepley #undef __FUNCT__
2099e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
210076b2cf59SMatthew Knepley /*@
210176b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
210276b2cf59SMatthew Knepley   at the beginning of every step of the iteration.
210376b2cf59SMatthew Knepley 
210476b2cf59SMatthew Knepley   Collective on SNES
210576b2cf59SMatthew Knepley 
210676b2cf59SMatthew Knepley   Input Parameters:
210776b2cf59SMatthew Knepley . snes - The nonlinear solver context
210876b2cf59SMatthew Knepley . func - The function
210976b2cf59SMatthew Knepley 
211076b2cf59SMatthew Knepley   Calling sequence of func:
211176b2cf59SMatthew Knepley . func (TS ts, int step);
211276b2cf59SMatthew Knepley 
211376b2cf59SMatthew Knepley . step - The current step of the iteration
211476b2cf59SMatthew Knepley 
211576b2cf59SMatthew Knepley   Level: intermediate
211676b2cf59SMatthew Knepley 
211776b2cf59SMatthew Knepley .keywords: SNES, update
211876b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
211976b2cf59SMatthew Knepley @*/
212076b2cf59SMatthew Knepley int SNESSetUpdate(SNES snes, int (*func)(SNES, int))
212176b2cf59SMatthew Knepley {
212276b2cf59SMatthew Knepley   PetscFunctionBegin;
212376b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
212476b2cf59SMatthew Knepley   snes->update = func;
212576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
212676b2cf59SMatthew Knepley }
212776b2cf59SMatthew Knepley 
2128e74ef692SMatthew Knepley #undef __FUNCT__
2129e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
213076b2cf59SMatthew Knepley /*@
213176b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
213276b2cf59SMatthew Knepley 
213376b2cf59SMatthew Knepley   Not collective
213476b2cf59SMatthew Knepley 
213576b2cf59SMatthew Knepley   Input Parameters:
213676b2cf59SMatthew Knepley . snes - The nonlinear solver context
213776b2cf59SMatthew Knepley . step - The current step of the iteration
213876b2cf59SMatthew Knepley 
2139205452f4SMatthew Knepley   Level: intermediate
2140205452f4SMatthew Knepley 
214176b2cf59SMatthew Knepley .keywords: SNES, update
214276b2cf59SMatthew Knepley .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultSolutionBC()
214376b2cf59SMatthew Knepley @*/
214476b2cf59SMatthew Knepley int SNESDefaultUpdate(SNES snes, int step)
214576b2cf59SMatthew Knepley {
214676b2cf59SMatthew Knepley   PetscFunctionBegin;
214776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
214876b2cf59SMatthew Knepley }
214976b2cf59SMatthew Knepley 
21504a2ae208SSatish Balay #undef __FUNCT__
21514a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
21529b94acceSBarry Smith /*
21539b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
21549b94acceSBarry Smith    positive parameter delta.
21559b94acceSBarry Smith 
21569b94acceSBarry Smith     Input Parameters:
2157c7afd0dbSLois Curfman McInnes +   snes - the SNES context
21589b94acceSBarry Smith .   y - approximate solution of linear system
21599b94acceSBarry Smith .   fnorm - 2-norm of current function
2160c7afd0dbSLois Curfman McInnes -   delta - trust region size
21619b94acceSBarry Smith 
21629b94acceSBarry Smith     Output Parameters:
2163c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
21649b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
21659b94acceSBarry Smith     region, and exceeds zero otherwise.
2166c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
21679b94acceSBarry Smith 
21689b94acceSBarry Smith     Note:
21696831982aSBarry Smith     For non-trust region methods such as SNESEQLS, the parameter delta
21709b94acceSBarry Smith     is set to be the maximum allowable step size.
21719b94acceSBarry Smith 
21729b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
21739b94acceSBarry Smith */
2174064f8208SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
21759b94acceSBarry Smith {
2176064f8208SBarry Smith   PetscReal   nrm;
2177ea709b57SSatish Balay   PetscScalar cnorm;
21783a40ed3dSBarry Smith   int         ierr;
21793a40ed3dSBarry Smith 
21803a40ed3dSBarry Smith   PetscFunctionBegin;
2181184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2182184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
2183184914b5SBarry Smith   PetscCheckSameComm(snes,y);
2184184914b5SBarry Smith 
2185064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2186064f8208SBarry Smith   if (nrm > *delta) {
2187064f8208SBarry Smith      nrm = *delta/nrm;
2188064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2189064f8208SBarry Smith      cnorm = nrm;
2190329f5518SBarry Smith      ierr = VecScale(&cnorm,y);CHKERRQ(ierr);
21919b94acceSBarry Smith      *ynorm = *delta;
21929b94acceSBarry Smith   } else {
21939b94acceSBarry Smith      *gpnorm = 0.0;
2194064f8208SBarry Smith      *ynorm = nrm;
21959b94acceSBarry Smith   }
21963a40ed3dSBarry Smith   PetscFunctionReturn(0);
21979b94acceSBarry Smith }
21989b94acceSBarry Smith 
21994a2ae208SSatish Balay #undef __FUNCT__
22004a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
22019b94acceSBarry Smith /*@
22029b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
220363a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
22049b94acceSBarry Smith 
2205c7afd0dbSLois Curfman McInnes    Collective on SNES
2206c7afd0dbSLois Curfman McInnes 
2207b2002411SLois Curfman McInnes    Input Parameters:
2208c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2209c7afd0dbSLois Curfman McInnes -  x - the solution vector
22109b94acceSBarry Smith 
22119b94acceSBarry Smith    Output Parameter:
2212b2002411SLois Curfman McInnes .  its - number of iterations until termination
22139b94acceSBarry Smith 
2214b2002411SLois Curfman McInnes    Notes:
22158ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
22168ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
22178ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
22188ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
22198ddd3da0SLois Curfman McInnes 
222036851e7fSLois Curfman McInnes    Level: beginner
222136851e7fSLois Curfman McInnes 
22229b94acceSBarry Smith .keywords: SNES, nonlinear, solve
22239b94acceSBarry Smith 
222463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
22259b94acceSBarry Smith @*/
22268ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
22279b94acceSBarry Smith {
2228f1af5d2fSBarry Smith   int        ierr;
2229f1af5d2fSBarry Smith   PetscTruth flg;
2230052efed2SBarry Smith 
22313a40ed3dSBarry Smith   PetscFunctionBegin;
223277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2233184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
2234184914b5SBarry Smith   PetscCheckSameComm(snes,x);
223574679c65SBarry Smith   PetscValidIntPointer(its);
223629bbc08cSBarry Smith   if (!snes->solve) SETERRQ(1,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
223774637425SBarry Smith 
223882bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
2239c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
2240758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
2241d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
224250ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
22439b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr);
2244d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
2245b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr);
224693b98531SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESView(snes,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); }
22473a40ed3dSBarry Smith   PetscFunctionReturn(0);
22489b94acceSBarry Smith }
22499b94acceSBarry Smith 
22509b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
22519b94acceSBarry Smith 
22524a2ae208SSatish Balay #undef __FUNCT__
22534a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
225482bf6240SBarry Smith /*@C
22554b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
22569b94acceSBarry Smith 
2257fee21e36SBarry Smith    Collective on SNES
2258fee21e36SBarry Smith 
2259c7afd0dbSLois Curfman McInnes    Input Parameters:
2260c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2261454a90a3SBarry Smith -  type - a known method
2262c7afd0dbSLois Curfman McInnes 
2263c7afd0dbSLois Curfman McInnes    Options Database Key:
2264454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2265c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2266ae12b187SLois Curfman McInnes 
22679b94acceSBarry Smith    Notes:
2268e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
22696831982aSBarry Smith +    SNESEQLS - Newton's method with line search
2270c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22716831982aSBarry Smith .    SNESEQTR - Newton's method with trust region
2272c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22736831982aSBarry Smith .    SNESUMTR - Newton's method with trust region
2274c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
22756831982aSBarry Smith -    SNESUMLS - Newton's method with line search
2276c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
22779b94acceSBarry Smith 
2278ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2279ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2280ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2281ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2282ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2283ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2284ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2285ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2286ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2287b0a32e0cSBarry Smith   appropriate method.
228836851e7fSLois Curfman McInnes 
228936851e7fSLois Curfman McInnes   Level: intermediate
2290a703fe33SLois Curfman McInnes 
2291454a90a3SBarry Smith .keywords: SNES, set, type
2292435da068SBarry Smith 
2293435da068SBarry Smith .seealso: SNESType, SNESCreate()
2294435da068SBarry Smith 
22959b94acceSBarry Smith @*/
2296454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type)
22979b94acceSBarry Smith {
22980f5bd95cSBarry Smith   int        ierr,(*r)(SNES);
22996831982aSBarry Smith   PetscTruth match;
23003a40ed3dSBarry Smith 
23013a40ed3dSBarry Smith   PetscFunctionBegin;
230277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
23030f5bd95cSBarry Smith   PetscValidCharPointer(type);
230482bf6240SBarry Smith 
23056831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
23060f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
230782bf6240SBarry Smith 
230882bf6240SBarry Smith   if (snes->setupcalled) {
2309e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
231082bf6240SBarry Smith     snes->data = 0;
231182bf6240SBarry Smith   }
23127d1a2b2bSBarry Smith 
23139b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
231482bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
231582bf6240SBarry Smith 
2316b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
231782bf6240SBarry Smith 
231829bbc08cSBarry Smith   if (!r) SETERRQ1(1,"Unable to find requested SNES type %s",type);
23191548b14aSBarry Smith 
2320606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
232182bf6240SBarry Smith   snes->data = 0;
23223a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
232382bf6240SBarry Smith 
2324454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
232582bf6240SBarry Smith   snes->set_method_called = 1;
232682bf6240SBarry Smith 
23273a40ed3dSBarry Smith   PetscFunctionReturn(0);
23289b94acceSBarry Smith }
23299b94acceSBarry Smith 
2330a847f771SSatish Balay 
23319b94acceSBarry Smith /* --------------------------------------------------------------------- */
23324a2ae208SSatish Balay #undef __FUNCT__
23334a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
23349b94acceSBarry Smith /*@C
23359b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2336f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
23379b94acceSBarry Smith 
2338fee21e36SBarry Smith    Not Collective
2339fee21e36SBarry Smith 
234036851e7fSLois Curfman McInnes    Level: advanced
234136851e7fSLois Curfman McInnes 
23429b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
23439b94acceSBarry Smith 
23449b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
23459b94acceSBarry Smith @*/
2346cf256101SBarry Smith int SNESRegisterDestroy(void)
23479b94acceSBarry Smith {
234882bf6240SBarry Smith   int ierr;
234982bf6240SBarry Smith 
23503a40ed3dSBarry Smith   PetscFunctionBegin;
235182bf6240SBarry Smith   if (SNESList) {
2352b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
235382bf6240SBarry Smith     SNESList = 0;
23549b94acceSBarry Smith   }
23554c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
23563a40ed3dSBarry Smith   PetscFunctionReturn(0);
23579b94acceSBarry Smith }
23589b94acceSBarry Smith 
23594a2ae208SSatish Balay #undef __FUNCT__
23604a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
23619b94acceSBarry Smith /*@C
23629a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
23639b94acceSBarry Smith 
2364c7afd0dbSLois Curfman McInnes    Not Collective
2365c7afd0dbSLois Curfman McInnes 
23669b94acceSBarry Smith    Input Parameter:
23674b0e389bSBarry Smith .  snes - nonlinear solver context
23689b94acceSBarry Smith 
23699b94acceSBarry Smith    Output Parameter:
23703a7fca6bSBarry Smith .  type - SNES method (a character string)
23719b94acceSBarry Smith 
237236851e7fSLois Curfman McInnes    Level: intermediate
237336851e7fSLois Curfman McInnes 
2374454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
23759b94acceSBarry Smith @*/
2376454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type)
23779b94acceSBarry Smith {
23783a40ed3dSBarry Smith   PetscFunctionBegin;
2379184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2380454a90a3SBarry Smith   *type = snes->type_name;
23813a40ed3dSBarry Smith   PetscFunctionReturn(0);
23829b94acceSBarry Smith }
23839b94acceSBarry Smith 
23844a2ae208SSatish Balay #undef __FUNCT__
23854a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
23869b94acceSBarry Smith /*@C
23879b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
23889b94acceSBarry Smith    stored.
23899b94acceSBarry Smith 
2390c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2391c7afd0dbSLois Curfman McInnes 
23929b94acceSBarry Smith    Input Parameter:
23939b94acceSBarry Smith .  snes - the SNES context
23949b94acceSBarry Smith 
23959b94acceSBarry Smith    Output Parameter:
23969b94acceSBarry Smith .  x - the solution
23979b94acceSBarry Smith 
239836851e7fSLois Curfman McInnes    Level: advanced
239936851e7fSLois Curfman McInnes 
24009b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
24019b94acceSBarry Smith 
2402a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
24039b94acceSBarry Smith @*/
24049b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
24059b94acceSBarry Smith {
24063a40ed3dSBarry Smith   PetscFunctionBegin;
240777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
24089b94acceSBarry Smith   *x = snes->vec_sol_always;
24093a40ed3dSBarry Smith   PetscFunctionReturn(0);
24109b94acceSBarry Smith }
24119b94acceSBarry Smith 
24124a2ae208SSatish Balay #undef __FUNCT__
24134a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
24149b94acceSBarry Smith /*@C
24159b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
24169b94acceSBarry Smith    stored.
24179b94acceSBarry Smith 
2418c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2419c7afd0dbSLois Curfman McInnes 
24209b94acceSBarry Smith    Input Parameter:
24219b94acceSBarry Smith .  snes - the SNES context
24229b94acceSBarry Smith 
24239b94acceSBarry Smith    Output Parameter:
24249b94acceSBarry Smith .  x - the solution update
24259b94acceSBarry Smith 
242636851e7fSLois Curfman McInnes    Level: advanced
242736851e7fSLois Curfman McInnes 
24289b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
24299b94acceSBarry Smith 
24309b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
24319b94acceSBarry Smith @*/
24329b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
24339b94acceSBarry Smith {
24343a40ed3dSBarry Smith   PetscFunctionBegin;
243577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
24369b94acceSBarry Smith   *x = snes->vec_sol_update_always;
24373a40ed3dSBarry Smith   PetscFunctionReturn(0);
24389b94acceSBarry Smith }
24399b94acceSBarry Smith 
24404a2ae208SSatish Balay #undef __FUNCT__
24414a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
24429b94acceSBarry Smith /*@C
24433638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
24449b94acceSBarry Smith 
2445c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2446c7afd0dbSLois Curfman McInnes 
24479b94acceSBarry Smith    Input Parameter:
24489b94acceSBarry Smith .  snes - the SNES context
24499b94acceSBarry Smith 
24509b94acceSBarry Smith    Output Parameter:
24517bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
245200036973SBarry Smith .  ctx - the function context (or PETSC_NULL)
245300036973SBarry Smith -  func - the function (or PETSC_NULL)
24549b94acceSBarry Smith 
24559b94acceSBarry Smith    Notes:
24569b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
24579b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
24589b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
24599b94acceSBarry Smith 
246036851e7fSLois Curfman McInnes    Level: advanced
246136851e7fSLois Curfman McInnes 
2462a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
24639b94acceSBarry Smith 
246431615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
246531615d04SLois Curfman McInnes           SNESGetGradient()
24667bf4e008SBarry Smith 
24679b94acceSBarry Smith @*/
246800036973SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx,int (**func)(SNES,Vec,Vec,void*))
24699b94acceSBarry Smith {
24703a40ed3dSBarry Smith   PetscFunctionBegin;
247177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2472a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
247329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
2474a8c6a408SBarry Smith   }
24757bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
24767bf4e008SBarry Smith   if (ctx)  *ctx  = snes->funP;
247700036973SBarry Smith   if (func) *func = snes->computefunction;
24783a40ed3dSBarry Smith   PetscFunctionReturn(0);
24799b94acceSBarry Smith }
24809b94acceSBarry Smith 
24814a2ae208SSatish Balay #undef __FUNCT__
24824a2ae208SSatish Balay #define __FUNCT__ "SNESGetGradient"
24839b94acceSBarry Smith /*@C
24843638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
24859b94acceSBarry Smith 
2486c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2487c7afd0dbSLois Curfman McInnes 
24889b94acceSBarry Smith    Input Parameter:
24899b94acceSBarry Smith .  snes - the SNES context
24909b94acceSBarry Smith 
24919b94acceSBarry Smith    Output Parameter:
24927bf4e008SBarry Smith +  r - the gradient (or PETSC_NULL)
24937bf4e008SBarry Smith -  ctx - the gradient context (or PETSC_NULL)
24949b94acceSBarry Smith 
24959b94acceSBarry Smith    Notes:
24969b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
24979b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
24989b94acceSBarry Smith    SNESGetFunction().
24999b94acceSBarry Smith 
250036851e7fSLois Curfman McInnes    Level: advanced
250136851e7fSLois Curfman McInnes 
25029b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
25039b94acceSBarry Smith 
25047bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(),
25057bf4e008SBarry Smith           SNESSetGradient(), SNESSetFunction()
25067bf4e008SBarry Smith 
25079b94acceSBarry Smith @*/
25087bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx)
25099b94acceSBarry Smith {
25103a40ed3dSBarry Smith   PetscFunctionBegin;
251177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
25123a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
251329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
25143a40ed3dSBarry Smith   }
25157bf4e008SBarry Smith   if (r)   *r = snes->vec_func_always;
25167bf4e008SBarry Smith   if (ctx) *ctx = snes->funP;
25173a40ed3dSBarry Smith   PetscFunctionReturn(0);
25189b94acceSBarry Smith }
25199b94acceSBarry Smith 
25204a2ae208SSatish Balay #undef __FUNCT__
25214a2ae208SSatish Balay #define __FUNCT__ "SNESGetMinimizationFunction"
25227bf4e008SBarry Smith /*@C
25239b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
25249b94acceSBarry Smith    unconstrained minimization problems.
25259b94acceSBarry Smith 
2526c7afd0dbSLois Curfman McInnes    Not Collective
2527c7afd0dbSLois Curfman McInnes 
25289b94acceSBarry Smith    Input Parameter:
25299b94acceSBarry Smith .  snes - the SNES context
25309b94acceSBarry Smith 
25319b94acceSBarry Smith    Output Parameter:
25327bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
25337bf4e008SBarry Smith -  ctx - the function context (or PETSC_NULL)
25349b94acceSBarry Smith 
25359b94acceSBarry Smith    Notes:
25369b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
25379b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
25389b94acceSBarry Smith    SNESGetFunction().
25399b94acceSBarry Smith 
254036851e7fSLois Curfman McInnes    Level: advanced
254136851e7fSLois Curfman McInnes 
25429b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
25439b94acceSBarry Smith 
25447bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction()
25457bf4e008SBarry Smith 
25469b94acceSBarry Smith @*/
2547329f5518SBarry Smith int SNESGetMinimizationFunction(SNES snes,PetscReal *r,void **ctx)
25489b94acceSBarry Smith {
25493a40ed3dSBarry Smith   PetscFunctionBegin;
255077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
255174679c65SBarry Smith   PetscValidScalarPointer(r);
25523a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
255329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
25543a40ed3dSBarry Smith   }
25557bf4e008SBarry Smith   if (r)   *r = snes->fc;
25567bf4e008SBarry Smith   if (ctx) *ctx = snes->umfunP;
25573a40ed3dSBarry Smith   PetscFunctionReturn(0);
25589b94acceSBarry Smith }
25599b94acceSBarry Smith 
25604a2ae208SSatish Balay #undef __FUNCT__
25614a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
25623c7409f5SSatish Balay /*@C
25633c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2564d850072dSLois Curfman McInnes    SNES options in the database.
25653c7409f5SSatish Balay 
2566fee21e36SBarry Smith    Collective on SNES
2567fee21e36SBarry Smith 
2568c7afd0dbSLois Curfman McInnes    Input Parameter:
2569c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2570c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2571c7afd0dbSLois Curfman McInnes 
2572d850072dSLois Curfman McInnes    Notes:
2573a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2574c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2575d850072dSLois Curfman McInnes 
257636851e7fSLois Curfman McInnes    Level: advanced
257736851e7fSLois Curfman McInnes 
25783c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2579a86d99e1SLois Curfman McInnes 
2580a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
25813c7409f5SSatish Balay @*/
25823c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
25833c7409f5SSatish Balay {
25843c7409f5SSatish Balay   int ierr;
25853c7409f5SSatish Balay 
25863a40ed3dSBarry Smith   PetscFunctionBegin;
258777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2588639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25893c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
25903a40ed3dSBarry Smith   PetscFunctionReturn(0);
25913c7409f5SSatish Balay }
25923c7409f5SSatish Balay 
25934a2ae208SSatish Balay #undef __FUNCT__
25944a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
25953c7409f5SSatish Balay /*@C
2596f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2597d850072dSLois Curfman McInnes    SNES options in the database.
25983c7409f5SSatish Balay 
2599fee21e36SBarry Smith    Collective on SNES
2600fee21e36SBarry Smith 
2601c7afd0dbSLois Curfman McInnes    Input Parameters:
2602c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2603c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2604c7afd0dbSLois Curfman McInnes 
2605d850072dSLois Curfman McInnes    Notes:
2606a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2607c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2608d850072dSLois Curfman McInnes 
260936851e7fSLois Curfman McInnes    Level: advanced
261036851e7fSLois Curfman McInnes 
26113c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2612a86d99e1SLois Curfman McInnes 
2613a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
26143c7409f5SSatish Balay @*/
26153c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
26163c7409f5SSatish Balay {
26173c7409f5SSatish Balay   int ierr;
26183c7409f5SSatish Balay 
26193a40ed3dSBarry Smith   PetscFunctionBegin;
262077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2621639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26223c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
26233a40ed3dSBarry Smith   PetscFunctionReturn(0);
26243c7409f5SSatish Balay }
26253c7409f5SSatish Balay 
26264a2ae208SSatish Balay #undef __FUNCT__
26274a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
26289ab63eb5SSatish Balay /*@C
26293c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
26303c7409f5SSatish Balay    SNES options in the database.
26313c7409f5SSatish Balay 
2632c7afd0dbSLois Curfman McInnes    Not Collective
2633c7afd0dbSLois Curfman McInnes 
26343c7409f5SSatish Balay    Input Parameter:
26353c7409f5SSatish Balay .  snes - the SNES context
26363c7409f5SSatish Balay 
26373c7409f5SSatish Balay    Output Parameter:
26383c7409f5SSatish Balay .  prefix - pointer to the prefix string used
26393c7409f5SSatish Balay 
26409ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
26419ab63eb5SSatish Balay    sufficient length to hold the prefix.
26429ab63eb5SSatish Balay 
264336851e7fSLois Curfman McInnes    Level: advanced
264436851e7fSLois Curfman McInnes 
26453c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2646a86d99e1SLois Curfman McInnes 
2647a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
26483c7409f5SSatish Balay @*/
26493c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
26503c7409f5SSatish Balay {
26513c7409f5SSatish Balay   int ierr;
26523c7409f5SSatish Balay 
26533a40ed3dSBarry Smith   PetscFunctionBegin;
265477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2655639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26563a40ed3dSBarry Smith   PetscFunctionReturn(0);
26573c7409f5SSatish Balay }
26583c7409f5SSatish Balay 
2659acb85ae6SSatish Balay /*MC
2660f1af5d2fSBarry Smith    SNESRegisterDynamic - Adds a method to the nonlinear solver package.
26619b94acceSBarry Smith 
2662b2002411SLois Curfman McInnes    Synopsis:
26633a7fca6bSBarry Smith    int SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
26649b94acceSBarry Smith 
26658d76a1e5SLois Curfman McInnes    Not collective
26668d76a1e5SLois Curfman McInnes 
2667b2002411SLois Curfman McInnes    Input Parameters:
2668c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2669b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2670b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2671c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
26729b94acceSBarry Smith 
2673b2002411SLois Curfman McInnes    Notes:
2674f1af5d2fSBarry Smith    SNESRegisterDynamic() may be called multiple times to add several user-defined solvers.
26753a40ed3dSBarry Smith 
2676b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2677b2002411SLois Curfman McInnes    is ignored.
2678b2002411SLois Curfman McInnes 
2679b9617806SBarry Smith    Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT},
26805ba88a07SLois Curfman McInnes    and others of the form ${any_environmental_variable} occuring in pathname will be
26815ba88a07SLois Curfman McInnes    replaced with appropriate values.
26825ba88a07SLois Curfman McInnes 
2683b2002411SLois Curfman McInnes    Sample usage:
268469225d78SLois Curfman McInnes .vb
2685f1af5d2fSBarry Smith    SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2686b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
268769225d78SLois Curfman McInnes .ve
2688b2002411SLois Curfman McInnes 
2689b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2690b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2691b2002411SLois Curfman McInnes    or at runtime via the option
2692b2002411SLois Curfman McInnes $     -snes_type my_solver
2693b2002411SLois Curfman McInnes 
269436851e7fSLois Curfman McInnes    Level: advanced
269536851e7fSLois Curfman McInnes 
2696b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2697b2002411SLois Curfman McInnes 
2698b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2699b2002411SLois Curfman McInnes M*/
2700b2002411SLois Curfman McInnes 
27014a2ae208SSatish Balay #undef __FUNCT__
27024a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
2703f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES))
2704b2002411SLois Curfman McInnes {
2705b2002411SLois Curfman McInnes   char fullname[256];
2706b2002411SLois Curfman McInnes   int  ierr;
2707b2002411SLois Curfman McInnes 
2708b2002411SLois Curfman McInnes   PetscFunctionBegin;
2709b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2710c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2711b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2712b2002411SLois Curfman McInnes }
2713