xref: /petsc/src/snes/interface/snes.c (revision 9d06fb6b2c85214d18459bd50a2c69b0a2710fee)
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;
104b27c08aSLois Curfman McInnes int SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval;
11a09944afSBarry Smith 
12a09944afSBarry Smith #undef __FUNCT__
134a2ae208SSatish Balay #define __FUNCT__ "SNESView"
147e2c5f70SBarry Smith /*@C
159b94acceSBarry Smith    SNESView - Prints the SNES data structure.
169b94acceSBarry Smith 
174c49b128SBarry Smith    Collective on SNES
18fee21e36SBarry Smith 
19c7afd0dbSLois Curfman McInnes    Input Parameters:
20c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
21c7afd0dbSLois Curfman McInnes -  viewer - visualization context
22c7afd0dbSLois Curfman McInnes 
239b94acceSBarry Smith    Options Database Key:
24c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
259b94acceSBarry Smith 
269b94acceSBarry Smith    Notes:
279b94acceSBarry Smith    The available visualization contexts include
28b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
29b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
30c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
31c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
32c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
339b94acceSBarry Smith 
343e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
35b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
369b94acceSBarry Smith 
3736851e7fSLois Curfman McInnes    Level: beginner
3836851e7fSLois Curfman McInnes 
399b94acceSBarry Smith .keywords: SNES, view
409b94acceSBarry Smith 
41b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
429b94acceSBarry Smith @*/
43b0a32e0cSBarry Smith int SNESView(SNES snes,PetscViewer viewer)
449b94acceSBarry Smith {
459b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
469b94acceSBarry Smith   int                 ierr;
479b94acceSBarry Smith   SLES                sles;
48454a90a3SBarry Smith   char                *type;
496831982aSBarry Smith   PetscTruth          isascii,isstring;
509b94acceSBarry Smith 
513a40ed3dSBarry Smith   PetscFunctionBegin;
5274679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
53b0a32e0cSBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(snes->comm);
54b0a32e0cSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
556831982aSBarry Smith   PetscCheckSameComm(snes,viewer);
5674679c65SBarry Smith 
57b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
58b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
590f5bd95cSBarry Smith   if (isascii) {
603a7fca6bSBarry Smith     if (snes->prefix) {
613a7fca6bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr);
623a7fca6bSBarry Smith     } else {
63b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
643a7fca6bSBarry Smith     }
65454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
66454a90a3SBarry Smith     if (type) {
67b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
68184914b5SBarry Smith     } else {
69b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
70184914b5SBarry Smith     }
710ef38995SBarry Smith     if (snes->view) {
72b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
730ef38995SBarry Smith       ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr);
74b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
750ef38995SBarry Smith     }
76b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
7777d8c4bbSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, solution=%g\n",
7877d8c4bbSBarry Smith                  snes->rtol,snes->atol,snes->xtol);CHKERRQ(ierr);
79b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
80b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
819b94acceSBarry Smith     if (snes->ksp_ewconv) {
829b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
839b94acceSBarry Smith       if (kctx) {
84b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
85b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
86b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
879b94acceSBarry Smith       }
889b94acceSBarry Smith     }
890f5bd95cSBarry Smith   } else if (isstring) {
90454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
91b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
9219bcc07fSBarry Smith   }
9377ed5343SBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
94b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
959b94acceSBarry Smith   ierr = SLESView(sles,viewer);CHKERRQ(ierr);
96b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
973a40ed3dSBarry Smith   PetscFunctionReturn(0);
989b94acceSBarry Smith }
999b94acceSBarry Smith 
10076b2cf59SMatthew Knepley /*
10176b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
10276b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
10376b2cf59SMatthew Knepley */
10476b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
10576b2cf59SMatthew Knepley static int numberofsetfromoptions;
10676b2cf59SMatthew Knepley static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
10776b2cf59SMatthew Knepley 
108e74ef692SMatthew Knepley #undef __FUNCT__
109e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
11076b2cf59SMatthew Knepley /*@
11176b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
11276b2cf59SMatthew Knepley 
11376b2cf59SMatthew Knepley   Not Collective
11476b2cf59SMatthew Knepley 
11576b2cf59SMatthew Knepley   Input Parameter:
11676b2cf59SMatthew Knepley . snescheck - function that checks for options
11776b2cf59SMatthew Knepley 
11876b2cf59SMatthew Knepley   Level: developer
11976b2cf59SMatthew Knepley 
12076b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
12176b2cf59SMatthew Knepley @*/
12276b2cf59SMatthew Knepley int SNESAddOptionsChecker(int (*snescheck)(SNES))
12376b2cf59SMatthew Knepley {
12476b2cf59SMatthew Knepley   PetscFunctionBegin;
12576b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
12676b2cf59SMatthew Knepley     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %d allowed", MAXSETFROMOPTIONS);
12776b2cf59SMatthew Knepley   }
12876b2cf59SMatthew Knepley 
12976b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
13076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
13176b2cf59SMatthew Knepley }
13276b2cf59SMatthew Knepley 
1334a2ae208SSatish Balay #undef __FUNCT__
1344a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1359b94acceSBarry Smith /*@
136682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
1379b94acceSBarry Smith 
138c7afd0dbSLois Curfman McInnes    Collective on SNES
139c7afd0dbSLois Curfman McInnes 
1409b94acceSBarry Smith    Input Parameter:
1419b94acceSBarry Smith .  snes - the SNES context
1429b94acceSBarry Smith 
14336851e7fSLois Curfman McInnes    Options Database Keys:
1446831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
14582738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
14682738288SBarry Smith                 of the change in the solution between steps
147b39c3a46SLois Curfman McInnes .  -snes_atol <atol> - absolute tolerance of residual norm
148b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
149b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
150b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
15150ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
152b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
15382738288SBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear or minimization
15482738288SBarry Smith                                solver; hence iterations will continue until max_it
1551fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
15682738288SBarry Smith                                of convergence test
15782738288SBarry Smith .  -snes_monitor - prints residual norm at each iteration
158d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
159d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
16082738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
161e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
16236851e7fSLois Curfman McInnes -  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
16382738288SBarry Smith 
16482738288SBarry Smith     Options Database for Eisenstat-Walker method:
1654b27c08aSLois Curfman McInnes +  -snes_ksp_ew_conv - use Eisenstat-Walker method for determining linear system convergence
1664b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
16736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
16836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
16936851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
17036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
17136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
17236851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
17382738288SBarry Smith 
17411ca99fdSLois Curfman McInnes    Notes:
17511ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
17611ca99fdSLois Curfman McInnes    the users manual.
17783e2fdc7SBarry Smith 
17836851e7fSLois Curfman McInnes    Level: beginner
17936851e7fSLois Curfman McInnes 
1809b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
1819b94acceSBarry Smith 
18269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
1839b94acceSBarry Smith @*/
1849b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
1859b94acceSBarry Smith {
1869b94acceSBarry Smith   SLES                sles;
187186905e3SBarry Smith   SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
188f1af5d2fSBarry Smith   PetscTruth          flg;
18976b2cf59SMatthew Knepley   int                 ierr, i;
190186905e3SBarry Smith   char                *deft,type[256];
1919b94acceSBarry Smith 
1923a40ed3dSBarry Smith   PetscFunctionBegin;
19377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
194ca161407SBarry Smith 
195b0a32e0cSBarry Smith   ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
196186905e3SBarry Smith     if (snes->type_name) {
197186905e3SBarry Smith       deft = snes->type_name;
198186905e3SBarry Smith     } else {
1994b27c08aSLois Curfman McInnes       deft = SNESLS;
200d64ed03dSBarry Smith     }
2014bbc92c1SBarry Smith 
202186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
203b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
204d64ed03dSBarry Smith     if (flg) {
205186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
206186905e3SBarry Smith     } else if (!snes->type_name) {
207186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
208d64ed03dSBarry Smith     }
209909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
21093c39befSBarry Smith 
21187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
21287828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->atol,&snes->atol,0);CHKERRQ(ierr);
213186905e3SBarry Smith 
21487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
215b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
216b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
21750ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
218186905e3SBarry Smith 
219b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_ksp_ew_conv","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetParametersEW",&snes->ksp_ewconv);CHKERRQ(ierr);
220186905e3SBarry Smith 
221b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1 or 2","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
22287828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
22387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
22487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
22587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
22687828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
22787828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
228186905e3SBarry Smith 
229b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr);
23093c39befSBarry Smith     if (flg) {snes->converged = 0;}
231b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_cancelmonitors","Remove all monitors","SNESClearMonitor",&flg);CHKERRQ(ierr);
2325cd90555SBarry Smith     if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
233b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_monitor","Monitor norm of function","SNESDefaultMonitor",&flg);CHKERRQ(ierr);
234b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);}
2353a7fca6bSBarry Smith     ierr = PetscOptionsName("-snes_ratiomonitor","Monitor norm of function","SNESSetRatioMonitor",&flg);CHKERRQ(ierr);
2363a7fca6bSBarry Smith     if (flg) {ierr = SNESSetRatioMonitor(snes);CHKERRQ(ierr);}
237b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_smonitor","Monitor norm of function (fewer digits)","SNESDefaultSMonitor",&flg);CHKERRQ(ierr);
238b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);}
239b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor","Plot solution at each iteration","SNESVecViewMonitor",&flg);CHKERRQ(ierr);
240b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);}
241b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_update","Plot correction at each iteration","SNESVecViewUpdateMonitor",&flg);CHKERRQ(ierr);
2427c922b88SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewUpdateMonitor,0,0);CHKERRQ(ierr);}
2435ed2d596SBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_residual","Plot residual at each iteration","SNESVecViewResidualMonitor",&flg);CHKERRQ(ierr);
2445ed2d596SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewResidualMonitor,0,0);CHKERRQ(ierr);}
245b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_xmonitor","Plot function norm at each iteration","SNESLGMonitor",&flg);CHKERRQ(ierr);
246186905e3SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
247e24b481bSBarry Smith 
248b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
2494b27c08aSLois Curfman McInnes     if (flg) {
250186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
251b0a32e0cSBarry Smith       PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
2529b94acceSBarry Smith     }
253639f9d9dSBarry Smith 
25476b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
25576b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);                                                             CHKERRQ(ierr);
25676b2cf59SMatthew Knepley     }
25776b2cf59SMatthew Knepley 
258186905e3SBarry Smith     if (snes->setfromoptions) {
259186905e3SBarry Smith       ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
260639f9d9dSBarry Smith     }
261639f9d9dSBarry Smith 
262b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2634bbc92c1SBarry Smith 
2649b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
2659b94acceSBarry Smith   ierr = SLESSetFromOptions(sles);CHKERRQ(ierr);
26693993e2dSLois Curfman McInnes 
2673a40ed3dSBarry Smith   PetscFunctionReturn(0);
2689b94acceSBarry Smith }
2699b94acceSBarry Smith 
270a847f771SSatish Balay 
2714a2ae208SSatish Balay #undef __FUNCT__
2724a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
2739b94acceSBarry Smith /*@
2749b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2759b94acceSBarry Smith    the nonlinear solvers.
2769b94acceSBarry Smith 
277fee21e36SBarry Smith    Collective on SNES
278fee21e36SBarry Smith 
279c7afd0dbSLois Curfman McInnes    Input Parameters:
280c7afd0dbSLois Curfman McInnes +  snes - the SNES context
281c7afd0dbSLois Curfman McInnes -  usrP - optional user context
282c7afd0dbSLois Curfman McInnes 
28336851e7fSLois Curfman McInnes    Level: intermediate
28436851e7fSLois Curfman McInnes 
2859b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
2869b94acceSBarry Smith 
2879b94acceSBarry Smith .seealso: SNESGetApplicationContext()
2889b94acceSBarry Smith @*/
2899b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
2909b94acceSBarry Smith {
2913a40ed3dSBarry Smith   PetscFunctionBegin;
29277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2939b94acceSBarry Smith   snes->user		= usrP;
2943a40ed3dSBarry Smith   PetscFunctionReturn(0);
2959b94acceSBarry Smith }
29674679c65SBarry Smith 
2974a2ae208SSatish Balay #undef __FUNCT__
2984a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
2999b94acceSBarry Smith /*@C
3009b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3019b94acceSBarry Smith    nonlinear solvers.
3029b94acceSBarry Smith 
303c7afd0dbSLois Curfman McInnes    Not Collective
304c7afd0dbSLois Curfman McInnes 
3059b94acceSBarry Smith    Input Parameter:
3069b94acceSBarry Smith .  snes - SNES context
3079b94acceSBarry Smith 
3089b94acceSBarry Smith    Output Parameter:
3099b94acceSBarry Smith .  usrP - user context
3109b94acceSBarry Smith 
31136851e7fSLois Curfman McInnes    Level: intermediate
31236851e7fSLois Curfman McInnes 
3139b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3149b94acceSBarry Smith 
3159b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3169b94acceSBarry Smith @*/
3179b94acceSBarry Smith int SNESGetApplicationContext(SNES snes,void **usrP)
3189b94acceSBarry Smith {
3193a40ed3dSBarry Smith   PetscFunctionBegin;
32077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3219b94acceSBarry Smith   *usrP = snes->user;
3223a40ed3dSBarry Smith   PetscFunctionReturn(0);
3239b94acceSBarry Smith }
32474679c65SBarry Smith 
3254a2ae208SSatish Balay #undef __FUNCT__
3264a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3279b94acceSBarry Smith /*@
328c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
329c8228a4eSBarry Smith    at this time.
3309b94acceSBarry Smith 
331c7afd0dbSLois Curfman McInnes    Not Collective
332c7afd0dbSLois Curfman McInnes 
3339b94acceSBarry Smith    Input Parameter:
3349b94acceSBarry Smith .  snes - SNES context
3359b94acceSBarry Smith 
3369b94acceSBarry Smith    Output Parameter:
3379b94acceSBarry Smith .  iter - iteration number
3389b94acceSBarry Smith 
339c8228a4eSBarry Smith    Notes:
340c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
341c8228a4eSBarry Smith 
342c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
34308405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
34408405cd6SLois Curfman McInnes .vb
34508405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
34608405cd6SLois Curfman McInnes       if (!(it % 2)) {
34708405cd6SLois Curfman McInnes         [compute Jacobian here]
34808405cd6SLois Curfman McInnes       }
34908405cd6SLois Curfman McInnes .ve
350c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
35108405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
352c8228a4eSBarry Smith 
35336851e7fSLois Curfman McInnes    Level: intermediate
35436851e7fSLois Curfman McInnes 
3559b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3569b94acceSBarry Smith @*/
3579b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3589b94acceSBarry Smith {
3593a40ed3dSBarry Smith   PetscFunctionBegin;
36077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
36174679c65SBarry Smith   PetscValidIntPointer(iter);
3629b94acceSBarry Smith   *iter = snes->iter;
3633a40ed3dSBarry Smith   PetscFunctionReturn(0);
3649b94acceSBarry Smith }
36574679c65SBarry Smith 
3664a2ae208SSatish Balay #undef __FUNCT__
3674a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
3689b94acceSBarry Smith /*@
3699b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3709b94acceSBarry Smith    with SNESSSetFunction().
3719b94acceSBarry Smith 
372c7afd0dbSLois Curfman McInnes    Collective on SNES
373c7afd0dbSLois Curfman McInnes 
3749b94acceSBarry Smith    Input Parameter:
3759b94acceSBarry Smith .  snes - SNES context
3769b94acceSBarry Smith 
3779b94acceSBarry Smith    Output Parameter:
3789b94acceSBarry Smith .  fnorm - 2-norm of function
3799b94acceSBarry Smith 
38036851e7fSLois Curfman McInnes    Level: intermediate
38136851e7fSLois Curfman McInnes 
3829b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
383a86d99e1SLois Curfman McInnes 
38408405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
3859b94acceSBarry Smith @*/
38687828ca2SBarry Smith int SNESGetFunctionNorm(SNES snes,PetscScalar *fnorm)
3879b94acceSBarry Smith {
3883a40ed3dSBarry Smith   PetscFunctionBegin;
38977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
39074679c65SBarry Smith   PetscValidScalarPointer(fnorm);
3919b94acceSBarry Smith   *fnorm = snes->norm;
3923a40ed3dSBarry Smith   PetscFunctionReturn(0);
3939b94acceSBarry Smith }
39474679c65SBarry Smith 
3954a2ae208SSatish Balay #undef __FUNCT__
3964a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps"
3979b94acceSBarry Smith /*@
3989b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
3999b94acceSBarry Smith    attempted by the nonlinear solver.
4009b94acceSBarry Smith 
401c7afd0dbSLois Curfman McInnes    Not Collective
402c7afd0dbSLois Curfman McInnes 
4039b94acceSBarry Smith    Input Parameter:
4049b94acceSBarry Smith .  snes - SNES context
4059b94acceSBarry Smith 
4069b94acceSBarry Smith    Output Parameter:
4079b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4089b94acceSBarry Smith 
409c96a6f78SLois Curfman McInnes    Notes:
410c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
411c96a6f78SLois Curfman McInnes 
41236851e7fSLois Curfman McInnes    Level: intermediate
41336851e7fSLois Curfman McInnes 
4149b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4159b94acceSBarry Smith @*/
4169b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
4179b94acceSBarry Smith {
4183a40ed3dSBarry Smith   PetscFunctionBegin;
41977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
42074679c65SBarry Smith   PetscValidIntPointer(nfails);
42150ffb88aSMatthew Knepley   *nfails = snes->numFailures;
42250ffb88aSMatthew Knepley   PetscFunctionReturn(0);
42350ffb88aSMatthew Knepley }
42450ffb88aSMatthew Knepley 
42550ffb88aSMatthew Knepley #undef __FUNCT__
42650ffb88aSMatthew Knepley #define __FUNCT__ "SNESSetMaximumUnsuccessfulSteps"
42750ffb88aSMatthew Knepley /*@
42850ffb88aSMatthew Knepley    SNESSetMaximumUnsuccessfulSteps - Sets the maximum number of unsuccessful steps
42950ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
43050ffb88aSMatthew Knepley 
43150ffb88aSMatthew Knepley    Not Collective
43250ffb88aSMatthew Knepley 
43350ffb88aSMatthew Knepley    Input Parameters:
43450ffb88aSMatthew Knepley +  snes     - SNES context
43550ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
43650ffb88aSMatthew Knepley 
43750ffb88aSMatthew Knepley    Level: intermediate
43850ffb88aSMatthew Knepley 
43950ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
44050ffb88aSMatthew Knepley @*/
44150ffb88aSMatthew Knepley int SNESSetMaximumUnsuccessfulSteps(SNES snes, int maxFails)
44250ffb88aSMatthew Knepley {
44350ffb88aSMatthew Knepley   PetscFunctionBegin;
44450ffb88aSMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE);
44550ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
44650ffb88aSMatthew Knepley   PetscFunctionReturn(0);
44750ffb88aSMatthew Knepley }
44850ffb88aSMatthew Knepley 
44950ffb88aSMatthew Knepley #undef __FUNCT__
45050ffb88aSMatthew Knepley #define __FUNCT__ "SNESGetMaximumUnsuccessfulSteps"
45150ffb88aSMatthew Knepley /*@
45250ffb88aSMatthew Knepley    SNESGetMaximumUnsuccessfulSteps - Gets the maximum number of unsuccessful steps
45350ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
45450ffb88aSMatthew Knepley 
45550ffb88aSMatthew Knepley    Not Collective
45650ffb88aSMatthew Knepley 
45750ffb88aSMatthew Knepley    Input Parameter:
45850ffb88aSMatthew Knepley .  snes     - SNES context
45950ffb88aSMatthew Knepley 
46050ffb88aSMatthew Knepley    Output Parameter:
46150ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
46250ffb88aSMatthew Knepley 
46350ffb88aSMatthew Knepley    Level: intermediate
46450ffb88aSMatthew Knepley 
46550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
46650ffb88aSMatthew Knepley @*/
46750ffb88aSMatthew Knepley int SNESGetMaximumUnsuccessfulSteps(SNES snes, int *maxFails)
46850ffb88aSMatthew Knepley {
46950ffb88aSMatthew Knepley   PetscFunctionBegin;
47050ffb88aSMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE);
47150ffb88aSMatthew Knepley   PetscValidIntPointer(maxFails);
47250ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
4733a40ed3dSBarry Smith   PetscFunctionReturn(0);
4749b94acceSBarry Smith }
475a847f771SSatish Balay 
4764a2ae208SSatish Balay #undef __FUNCT__
4774a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations"
478c96a6f78SLois Curfman McInnes /*@
479c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
480c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
481c96a6f78SLois Curfman McInnes 
482c7afd0dbSLois Curfman McInnes    Not Collective
483c7afd0dbSLois Curfman McInnes 
484c96a6f78SLois Curfman McInnes    Input Parameter:
485c96a6f78SLois Curfman McInnes .  snes - SNES context
486c96a6f78SLois Curfman McInnes 
487c96a6f78SLois Curfman McInnes    Output Parameter:
488c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
489c96a6f78SLois Curfman McInnes 
490c96a6f78SLois Curfman McInnes    Notes:
491c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
492c96a6f78SLois Curfman McInnes 
49336851e7fSLois Curfman McInnes    Level: intermediate
49436851e7fSLois Curfman McInnes 
495c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
496c96a6f78SLois Curfman McInnes @*/
49786bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
498c96a6f78SLois Curfman McInnes {
4993a40ed3dSBarry Smith   PetscFunctionBegin;
500c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
501c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
502c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5033a40ed3dSBarry Smith   PetscFunctionReturn(0);
504c96a6f78SLois Curfman McInnes }
505c96a6f78SLois Curfman McInnes 
5064a2ae208SSatish Balay #undef __FUNCT__
5074a2ae208SSatish Balay #define __FUNCT__ "SNESGetSLES"
5089b94acceSBarry Smith /*@C
5099b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
5109b94acceSBarry Smith 
511c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
512c7afd0dbSLois Curfman McInnes 
5139b94acceSBarry Smith    Input Parameter:
5149b94acceSBarry Smith .  snes - the SNES context
5159b94acceSBarry Smith 
5169b94acceSBarry Smith    Output Parameter:
5179b94acceSBarry Smith .  sles - the SLES context
5189b94acceSBarry Smith 
5199b94acceSBarry Smith    Notes:
5209b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
5219b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
5229b94acceSBarry Smith    KSP and PC contexts as well.
5239b94acceSBarry Smith 
52436851e7fSLois Curfman McInnes    Level: beginner
52536851e7fSLois Curfman McInnes 
5269b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
5279b94acceSBarry Smith 
5289b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
5299b94acceSBarry Smith @*/
5309b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
5319b94acceSBarry Smith {
5323a40ed3dSBarry Smith   PetscFunctionBegin;
53377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5349b94acceSBarry Smith   *sles = snes->sles;
5353a40ed3dSBarry Smith   PetscFunctionReturn(0);
5369b94acceSBarry Smith }
53782bf6240SBarry Smith 
5384a2ae208SSatish Balay #undef __FUNCT__
5394a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
540454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj)
541e24b481bSBarry Smith {
542aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
543454a90a3SBarry Smith   SNES          v = (SNES) obj;
544e24b481bSBarry Smith   int          ierr;
54543d6d2cbSBarry Smith #endif
546e24b481bSBarry Smith 
547e24b481bSBarry Smith   PetscFunctionBegin;
548e24b481bSBarry Smith 
54943d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
550e24b481bSBarry Smith   /* if it is already published then return */
551e24b481bSBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
552e24b481bSBarry Smith 
553454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
554e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
555e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
556e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
557e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
558454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
559433994e6SBarry Smith #endif
560e24b481bSBarry Smith   PetscFunctionReturn(0);
561e24b481bSBarry Smith }
562e24b481bSBarry Smith 
5639b94acceSBarry Smith /* -----------------------------------------------------------*/
5644a2ae208SSatish Balay #undef __FUNCT__
5654a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
5669b94acceSBarry Smith /*@C
5679b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
5689b94acceSBarry Smith 
569c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
570c7afd0dbSLois Curfman McInnes 
571c7afd0dbSLois Curfman McInnes    Input Parameters:
572c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
5739b94acceSBarry Smith 
5749b94acceSBarry Smith    Output Parameter:
5759b94acceSBarry Smith .  outsnes - the new SNES context
5769b94acceSBarry Smith 
577c7afd0dbSLois Curfman McInnes    Options Database Keys:
578c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
579c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
580c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
581c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
582c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
583c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
584c1f60f51SBarry Smith 
58536851e7fSLois Curfman McInnes    Level: beginner
58636851e7fSLois Curfman McInnes 
5879b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
5889b94acceSBarry Smith 
5894b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES
5909b94acceSBarry Smith @*/
5914b27c08aSLois Curfman McInnes int SNESCreate(MPI_Comm comm,SNES *outsnes)
5929b94acceSBarry Smith {
5939b94acceSBarry Smith   int                 ierr;
5949b94acceSBarry Smith   SNES                snes;
5959b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
59637fcc0dbSBarry Smith 
5973a40ed3dSBarry Smith   PetscFunctionBegin;
5988ba1e511SMatthew Knepley   PetscValidPointer(outsnes);
5998ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
6008ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
6018ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);                                                               CHKERRQ(ierr);
6028ba1e511SMatthew Knepley #endif
6038ba1e511SMatthew Knepley 
6043f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
605b0a32e0cSBarry Smith   PetscLogObjectCreate(snes);
606e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6079b94acceSBarry Smith   snes->max_its           = 50;
6089750a799SBarry Smith   snes->max_funcs	  = 10000;
6099b94acceSBarry Smith   snes->norm		  = 0.0;
610b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
611b4874afaSBarry Smith   snes->ttol              = 0.0;
612b4874afaSBarry Smith   snes->atol		  = 1.e-50;
6139b94acceSBarry Smith   snes->xtol		  = 1.e-8;
6144b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
6159b94acceSBarry Smith   snes->nfuncs            = 0;
61650ffb88aSMatthew Knepley   snes->numFailures       = 0;
61750ffb88aSMatthew Knepley   snes->maxFailures       = 1;
6187a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
619639f9d9dSBarry Smith   snes->numbermonitors    = 0;
6209b94acceSBarry Smith   snes->data              = 0;
6219b94acceSBarry Smith   snes->view              = 0;
62282bf6240SBarry Smith   snes->setupcalled       = 0;
623186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
6246f24a144SLois Curfman McInnes   snes->vwork             = 0;
6256f24a144SLois Curfman McInnes   snes->nwork             = 0;
626758f92a0SBarry Smith   snes->conv_hist_len     = 0;
627758f92a0SBarry Smith   snes->conv_hist_max     = 0;
628758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
629758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
630758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
631184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
6329b94acceSBarry Smith 
6339b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
634b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
635b0a32e0cSBarry Smith   PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
6369b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
6379b94acceSBarry Smith   kctx->version     = 2;
6389b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
6399b94acceSBarry Smith                              this was too large for some test cases */
6409b94acceSBarry Smith   kctx->rtol_last   = 0;
6419b94acceSBarry Smith   kctx->rtol_max    = .9;
6429b94acceSBarry Smith   kctx->gamma       = 1.0;
6439b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
6449b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
6459b94acceSBarry Smith   kctx->threshold   = .1;
6469b94acceSBarry Smith   kctx->lresid_last = 0;
6479b94acceSBarry Smith   kctx->norm_last   = 0;
6489b94acceSBarry Smith 
6499b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr);
650b0a32e0cSBarry Smith   PetscLogObjectParent(snes,snes->sles)
6515334005bSBarry Smith 
6529b94acceSBarry Smith   *outsnes = snes;
65300036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
6543a40ed3dSBarry Smith   PetscFunctionReturn(0);
6559b94acceSBarry Smith }
6569b94acceSBarry Smith 
6579b94acceSBarry Smith /* --------------------------------------------------------------- */
6584a2ae208SSatish Balay #undef __FUNCT__
6594a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
6609b94acceSBarry Smith /*@C
6619b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
6629b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
6639b94acceSBarry Smith    equations.
6649b94acceSBarry Smith 
665fee21e36SBarry Smith    Collective on SNES
666fee21e36SBarry Smith 
667c7afd0dbSLois Curfman McInnes    Input Parameters:
668c7afd0dbSLois Curfman McInnes +  snes - the SNES context
669c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
670c7afd0dbSLois Curfman McInnes .  r - vector to store function value
671c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
672c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
6739b94acceSBarry Smith 
674c7afd0dbSLois Curfman McInnes    Calling sequence of func:
6758d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
676c7afd0dbSLois Curfman McInnes 
677313e4042SLois Curfman McInnes .  f - function vector
678c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
6799b94acceSBarry Smith 
6809b94acceSBarry Smith    Notes:
6819b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
6829b94acceSBarry Smith $      f'(x) x = -f(x),
683c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
6849b94acceSBarry Smith 
68536851e7fSLois Curfman McInnes    Level: beginner
68636851e7fSLois Curfman McInnes 
6879b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
6889b94acceSBarry Smith 
689a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
6909b94acceSBarry Smith @*/
6915334005bSBarry Smith int SNESSetFunction(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
6929b94acceSBarry Smith {
6933a40ed3dSBarry Smith   PetscFunctionBegin;
69477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
695184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
696184914b5SBarry Smith   PetscCheckSameComm(snes,r);
697184914b5SBarry Smith 
6989b94acceSBarry Smith   snes->computefunction     = func;
6999b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7009b94acceSBarry Smith   snes->funP                = ctx;
7013a40ed3dSBarry Smith   PetscFunctionReturn(0);
7029b94acceSBarry Smith }
7039b94acceSBarry Smith 
7044a2ae208SSatish Balay #undef __FUNCT__
7054a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
7069b94acceSBarry Smith /*@
70736851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
7089b94acceSBarry Smith                          SNESSetFunction().
7099b94acceSBarry Smith 
710c7afd0dbSLois Curfman McInnes    Collective on SNES
711c7afd0dbSLois Curfman McInnes 
7129b94acceSBarry Smith    Input Parameters:
713c7afd0dbSLois Curfman McInnes +  snes - the SNES context
714c7afd0dbSLois Curfman McInnes -  x - input vector
7159b94acceSBarry Smith 
7169b94acceSBarry Smith    Output Parameter:
7173638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
7189b94acceSBarry Smith 
7191bffabb2SLois Curfman McInnes    Notes:
72036851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
72136851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
72236851e7fSLois Curfman McInnes    themselves.
72336851e7fSLois Curfman McInnes 
72436851e7fSLois Curfman McInnes    Level: developer
72536851e7fSLois Curfman McInnes 
7269b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
7279b94acceSBarry Smith 
728a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
7299b94acceSBarry Smith @*/
7309b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x,Vec y)
7319b94acceSBarry Smith {
7329b94acceSBarry Smith   int    ierr;
7339b94acceSBarry Smith 
7343a40ed3dSBarry Smith   PetscFunctionBegin;
735184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
736184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
737184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
738184914b5SBarry Smith   PetscCheckSameComm(snes,x);
739184914b5SBarry Smith   PetscCheckSameComm(snes,y);
740184914b5SBarry Smith 
741d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
742d64ed03dSBarry Smith   PetscStackPush("SNES user function");
7439b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
744d64ed03dSBarry Smith   PetscStackPop;
745ae3c334cSLois Curfman McInnes   snes->nfuncs++;
746d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
7473a40ed3dSBarry Smith   PetscFunctionReturn(0);
7489b94acceSBarry Smith }
7499b94acceSBarry Smith 
7504a2ae208SSatish Balay #undef __FUNCT__
7514a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
75262fef451SLois Curfman McInnes /*@
75362fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
75462fef451SLois Curfman McInnes    set with SNESSetJacobian().
75562fef451SLois Curfman McInnes 
756c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
757c7afd0dbSLois Curfman McInnes 
75862fef451SLois Curfman McInnes    Input Parameters:
759c7afd0dbSLois Curfman McInnes +  snes - the SNES context
760c7afd0dbSLois Curfman McInnes -  x - input vector
76162fef451SLois Curfman McInnes 
76262fef451SLois Curfman McInnes    Output Parameters:
763c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
76462fef451SLois Curfman McInnes .  B - optional preconditioning matrix
765c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
766fee21e36SBarry Smith 
76762fef451SLois Curfman McInnes    Notes:
76862fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
76962fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
77062fef451SLois Curfman McInnes 
771dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
772dc5a77f8SLois Curfman McInnes    flag parameter.
77362fef451SLois Curfman McInnes 
77436851e7fSLois Curfman McInnes    Level: developer
77536851e7fSLois Curfman McInnes 
77662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
77762fef451SLois Curfman McInnes 
77862fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
77962fef451SLois Curfman McInnes @*/
7809b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
7819b94acceSBarry Smith {
7829b94acceSBarry Smith   int    ierr;
7833a40ed3dSBarry Smith 
7843a40ed3dSBarry Smith   PetscFunctionBegin;
785184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
786184914b5SBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE);
787184914b5SBarry Smith   PetscCheckSameComm(snes,X);
7883a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
789d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
790c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
791d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
7929b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
793d64ed03dSBarry Smith   PetscStackPop;
794d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
7956d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
79677c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
79777c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
7983a40ed3dSBarry Smith   PetscFunctionReturn(0);
7999b94acceSBarry Smith }
8009b94acceSBarry Smith 
8014a2ae208SSatish Balay #undef __FUNCT__
8024a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
8039b94acceSBarry Smith /*@C
8049b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
805044dda88SLois Curfman McInnes    location to store the matrix.
8069b94acceSBarry Smith 
807c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
808c7afd0dbSLois Curfman McInnes 
8099b94acceSBarry Smith    Input Parameters:
810c7afd0dbSLois Curfman McInnes +  snes - the SNES context
8119b94acceSBarry Smith .  A - Jacobian matrix
8129b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
8139b94acceSBarry Smith .  func - Jacobian evaluation routine
814c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
8152cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
8169b94acceSBarry Smith 
8179b94acceSBarry Smith    Calling sequence of func:
8188d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
8199b94acceSBarry Smith 
820c7afd0dbSLois Curfman McInnes +  x - input vector
8219b94acceSBarry Smith .  A - Jacobian matrix
8229b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
823ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
824ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
825c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
8269b94acceSBarry Smith 
8279b94acceSBarry Smith    Notes:
828dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
8292cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
830ac21db08SLois Curfman McInnes 
831ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
8329b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
8339b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
8349b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
8359b94acceSBarry Smith    throughout the global iterations.
8369b94acceSBarry Smith 
83736851e7fSLois Curfman McInnes    Level: beginner
83836851e7fSLois Curfman McInnes 
8399b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
8409b94acceSBarry Smith 
841ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
8429b94acceSBarry Smith @*/
843454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
8449b94acceSBarry Smith {
8453a7fca6bSBarry Smith   int ierr;
8463a7fca6bSBarry Smith 
8473a40ed3dSBarry Smith   PetscFunctionBegin;
84877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
84900036973SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE);
85000036973SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE);
85100036973SBarry Smith   if (A) PetscCheckSameComm(snes,A);
85200036973SBarry Smith   if (B) PetscCheckSameComm(snes,B);
8533a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
8543a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
8553a7fca6bSBarry Smith   if (A) {
8563a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
8579b94acceSBarry Smith     snes->jacobian = A;
8583a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8593a7fca6bSBarry Smith   }
8603a7fca6bSBarry Smith   if (B) {
8613a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
8629b94acceSBarry Smith     snes->jacobian_pre = B;
8633a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8643a7fca6bSBarry Smith   }
8653a40ed3dSBarry Smith   PetscFunctionReturn(0);
8669b94acceSBarry Smith }
86762fef451SLois Curfman McInnes 
8684a2ae208SSatish Balay #undef __FUNCT__
8694a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
870c2aafc4cSSatish Balay /*@C
871b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
872b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
873b4fd4287SBarry Smith 
874c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
875c7afd0dbSLois Curfman McInnes 
876b4fd4287SBarry Smith    Input Parameter:
877b4fd4287SBarry Smith .  snes - the nonlinear solver context
878b4fd4287SBarry Smith 
879b4fd4287SBarry Smith    Output Parameters:
880c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
881b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
88200036973SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
88300036973SBarry Smith -  func - location to put Jacobian function (or PETSC_NULL)
884fee21e36SBarry Smith 
88536851e7fSLois Curfman McInnes    Level: advanced
88636851e7fSLois Curfman McInnes 
887b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
888b4fd4287SBarry Smith @*/
88900036973SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx,int (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*))
890b4fd4287SBarry Smith {
8913a40ed3dSBarry Smith   PetscFunctionBegin;
892184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
893b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
894b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
895b4fd4287SBarry Smith   if (ctx)  *ctx  = snes->jacP;
89600036973SBarry Smith   if (func) *func = snes->computejacobian;
8973a40ed3dSBarry Smith   PetscFunctionReturn(0);
898b4fd4287SBarry Smith }
899b4fd4287SBarry Smith 
9009b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
90145fc7adcSBarry Smith extern int SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
9029b94acceSBarry Smith 
9034a2ae208SSatish Balay #undef __FUNCT__
9044a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
9059b94acceSBarry Smith /*@
9069b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
907272ac6f2SLois Curfman McInnes    of a nonlinear solver.
9089b94acceSBarry Smith 
909fee21e36SBarry Smith    Collective on SNES
910fee21e36SBarry Smith 
911c7afd0dbSLois Curfman McInnes    Input Parameters:
912c7afd0dbSLois Curfman McInnes +  snes - the SNES context
913c7afd0dbSLois Curfman McInnes -  x - the solution vector
914c7afd0dbSLois Curfman McInnes 
915272ac6f2SLois Curfman McInnes    Notes:
916272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
917272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
918272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
919272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
920272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
921272ac6f2SLois Curfman McInnes 
92236851e7fSLois Curfman McInnes    Level: advanced
92336851e7fSLois Curfman McInnes 
9249b94acceSBarry Smith .keywords: SNES, nonlinear, setup
9259b94acceSBarry Smith 
9269b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
9279b94acceSBarry Smith @*/
9288ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
9299b94acceSBarry Smith {
930f1af5d2fSBarry Smith   int        ierr;
9314b27c08aSLois Curfman McInnes   PetscTruth flg, iseqtr;
9323a40ed3dSBarry Smith 
9333a40ed3dSBarry Smith   PetscFunctionBegin;
93477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
93577c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
936184914b5SBarry Smith   PetscCheckSameComm(snes,x);
9378ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
9389b94acceSBarry Smith 
939b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
940c1f60f51SBarry Smith   /*
941c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
942dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
943c1f60f51SBarry Smith   */
944c1f60f51SBarry Smith   if (flg) {
945c1f60f51SBarry Smith     Mat J;
9465a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
9475a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
9483a7fca6bSBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator routines\n");
9493a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
9503a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
951c1f60f51SBarry Smith   }
95245fc7adcSBarry Smith 
953b4014bb2SMatthew Knepley #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
95445fc7adcSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
95545fc7adcSBarry Smith   if (flg) {
95645fc7adcSBarry Smith     Mat J;
95745fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
95845fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
95945fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
96045fc7adcSBarry Smith   }
96132a4b47aSMatthew Knepley #endif
96245fc7adcSBarry Smith 
963b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
964c1f60f51SBarry Smith   /*
965dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
966c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
967c1f60f51SBarry Smith    */
96831615d04SLois Curfman McInnes   if (flg) {
969272ac6f2SLois Curfman McInnes     Mat  J;
970f3ef73ceSBarry Smith     SLES sles;
971f3ef73ceSBarry Smith     PC   pc;
972f3ef73ceSBarry Smith 
9735a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
9743a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
97593b98531SBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator and preconditioner routines\n");
9763a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
9773a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
9783a7fca6bSBarry Smith 
979f3ef73ceSBarry Smith     /* force no preconditioner */
980f3ef73ceSBarry Smith     ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
981f3ef73ceSBarry Smith     ierr = SLESGetPC(sles,&pc);CHKERRQ(ierr);
982f3ef73ceSBarry Smith     ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
983272ac6f2SLois Curfman McInnes   }
984f3ef73ceSBarry Smith 
98529bbc08cSBarry Smith   if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
98629bbc08cSBarry Smith   if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
98729bbc08cSBarry Smith   if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
988a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
98929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
990a8c6a408SBarry Smith   }
991a703fe33SLois Curfman McInnes 
992a703fe33SLois Curfman McInnes   /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
9934b27c08aSLois Curfman McInnes   ierr = PetscTypeCompare((PetscObject)snes,SNESTR,&iseqtr);CHKERRQ(ierr);
9946831982aSBarry Smith   if (snes->ksp_ewconv && !iseqtr) {
995a703fe33SLois Curfman McInnes     SLES sles; KSP ksp;
996a703fe33SLois Curfman McInnes     ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
997a703fe33SLois Curfman McInnes     ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
998186905e3SBarry Smith     ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
999a703fe33SLois Curfman McInnes   }
10004b27c08aSLois Curfman McInnes 
1001a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
100282bf6240SBarry Smith   snes->setupcalled = 1;
10033a40ed3dSBarry Smith   PetscFunctionReturn(0);
10049b94acceSBarry Smith }
10059b94acceSBarry Smith 
10064a2ae208SSatish Balay #undef __FUNCT__
10074a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
10089b94acceSBarry Smith /*@C
10099b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
10109b94acceSBarry Smith    with SNESCreate().
10119b94acceSBarry Smith 
1012c7afd0dbSLois Curfman McInnes    Collective on SNES
1013c7afd0dbSLois Curfman McInnes 
10149b94acceSBarry Smith    Input Parameter:
10159b94acceSBarry Smith .  snes - the SNES context
10169b94acceSBarry Smith 
101736851e7fSLois Curfman McInnes    Level: beginner
101836851e7fSLois Curfman McInnes 
10199b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
10209b94acceSBarry Smith 
102163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
10229b94acceSBarry Smith @*/
10239b94acceSBarry Smith int SNESDestroy(SNES snes)
10249b94acceSBarry Smith {
1025b8a78c4aSBarry Smith   int i,ierr;
10263a40ed3dSBarry Smith 
10273a40ed3dSBarry Smith   PetscFunctionBegin;
102877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
10293a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1030d4bb536fSBarry Smith 
1031be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
10320f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1033be0abb6dSBarry Smith 
1034e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1035606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
10363a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
10373a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
10389b94acceSBarry Smith   ierr = SLESDestroy(snes->sles);CHKERRQ(ierr);
1039522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1040b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1041b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1042b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1043b8a78c4aSBarry Smith     }
1044b8a78c4aSBarry Smith   }
1045b0a32e0cSBarry Smith   PetscLogObjectDestroy((PetscObject)snes);
10460452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
10473a40ed3dSBarry Smith   PetscFunctionReturn(0);
10489b94acceSBarry Smith }
10499b94acceSBarry Smith 
10509b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
10519b94acceSBarry Smith 
10524a2ae208SSatish Balay #undef __FUNCT__
10534a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
10549b94acceSBarry Smith /*@
1055d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
10569b94acceSBarry Smith 
1057c7afd0dbSLois Curfman McInnes    Collective on SNES
1058c7afd0dbSLois Curfman McInnes 
10599b94acceSBarry Smith    Input Parameters:
1060c7afd0dbSLois Curfman McInnes +  snes - the SNES context
106133174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
106233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
106333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
106433174efeSLois Curfman McInnes            of the change in the solution between steps
106533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1066c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1067fee21e36SBarry Smith 
106833174efeSLois Curfman McInnes    Options Database Keys:
1069c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1070c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1071c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1072c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1073c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
10749b94acceSBarry Smith 
1075d7a720efSLois Curfman McInnes    Notes:
10769b94acceSBarry Smith    The default maximum number of iterations is 50.
10779b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
10789b94acceSBarry Smith 
107936851e7fSLois Curfman McInnes    Level: intermediate
108036851e7fSLois Curfman McInnes 
108133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
10829b94acceSBarry Smith 
1083d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
10849b94acceSBarry Smith @*/
1085329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf)
10869b94acceSBarry Smith {
10873a40ed3dSBarry Smith   PetscFunctionBegin;
108877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1089d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1090d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1091d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1092d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1093d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
10943a40ed3dSBarry Smith   PetscFunctionReturn(0);
10959b94acceSBarry Smith }
10969b94acceSBarry Smith 
10974a2ae208SSatish Balay #undef __FUNCT__
10984a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
10999b94acceSBarry Smith /*@
110033174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
110133174efeSLois Curfman McInnes 
1102c7afd0dbSLois Curfman McInnes    Not Collective
1103c7afd0dbSLois Curfman McInnes 
110433174efeSLois Curfman McInnes    Input Parameters:
1105c7afd0dbSLois Curfman McInnes +  snes - the SNES context
110633174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
110733174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
110833174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
110933174efeSLois Curfman McInnes            of the change in the solution between steps
111033174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1111c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1112fee21e36SBarry Smith 
111333174efeSLois Curfman McInnes    Notes:
111433174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
111533174efeSLois Curfman McInnes 
111636851e7fSLois Curfman McInnes    Level: intermediate
111736851e7fSLois Curfman McInnes 
111833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
111933174efeSLois Curfman McInnes 
112033174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
112133174efeSLois Curfman McInnes @*/
1122329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf)
112333174efeSLois Curfman McInnes {
11243a40ed3dSBarry Smith   PetscFunctionBegin;
112533174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
112633174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
112733174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
112833174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
112933174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
113033174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
11313a40ed3dSBarry Smith   PetscFunctionReturn(0);
113233174efeSLois Curfman McInnes }
113333174efeSLois Curfman McInnes 
11344a2ae208SSatish Balay #undef __FUNCT__
11354a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
113633174efeSLois Curfman McInnes /*@
11379b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
11389b94acceSBarry Smith 
1139fee21e36SBarry Smith    Collective on SNES
1140fee21e36SBarry Smith 
1141c7afd0dbSLois Curfman McInnes    Input Parameters:
1142c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1143c7afd0dbSLois Curfman McInnes -  tol - tolerance
1144c7afd0dbSLois Curfman McInnes 
11459b94acceSBarry Smith    Options Database Key:
1146c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
11479b94acceSBarry Smith 
114836851e7fSLois Curfman McInnes    Level: intermediate
114936851e7fSLois Curfman McInnes 
11509b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
11519b94acceSBarry Smith 
1152d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
11539b94acceSBarry Smith @*/
1154329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
11559b94acceSBarry Smith {
11563a40ed3dSBarry Smith   PetscFunctionBegin;
115777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11589b94acceSBarry Smith   snes->deltatol = tol;
11593a40ed3dSBarry Smith   PetscFunctionReturn(0);
11609b94acceSBarry Smith }
11619b94acceSBarry Smith 
1162df9fa365SBarry Smith /*
1163df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1164df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1165df9fa365SBarry Smith    macros instead of functions
1166df9fa365SBarry Smith */
11674a2ae208SSatish Balay #undef __FUNCT__
11684a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitor"
1169329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx)
1170ce1608b8SBarry Smith {
1171ce1608b8SBarry Smith   int ierr;
1172ce1608b8SBarry Smith 
1173ce1608b8SBarry Smith   PetscFunctionBegin;
1174184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1175ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1176ce1608b8SBarry Smith   PetscFunctionReturn(0);
1177ce1608b8SBarry Smith }
1178ce1608b8SBarry Smith 
11794a2ae208SSatish Balay #undef __FUNCT__
11804a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorCreate"
1181b0a32e0cSBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,PetscDrawLG *draw)
1182df9fa365SBarry Smith {
1183df9fa365SBarry Smith   int ierr;
1184df9fa365SBarry Smith 
1185df9fa365SBarry Smith   PetscFunctionBegin;
1186df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1187df9fa365SBarry Smith   PetscFunctionReturn(0);
1188df9fa365SBarry Smith }
1189df9fa365SBarry Smith 
11904a2ae208SSatish Balay #undef __FUNCT__
11914a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorDestroy"
1192b0a32e0cSBarry Smith int SNESLGMonitorDestroy(PetscDrawLG draw)
1193df9fa365SBarry Smith {
1194df9fa365SBarry Smith   int ierr;
1195df9fa365SBarry Smith 
1196df9fa365SBarry Smith   PetscFunctionBegin;
1197df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1198df9fa365SBarry Smith   PetscFunctionReturn(0);
1199df9fa365SBarry Smith }
1200df9fa365SBarry Smith 
12019b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
12029b94acceSBarry Smith 
12034a2ae208SSatish Balay #undef __FUNCT__
12044a2ae208SSatish Balay #define __FUNCT__ "SNESSetMonitor"
12059b94acceSBarry Smith /*@C
12065cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
12079b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
12089b94acceSBarry Smith    progress.
12099b94acceSBarry Smith 
1210fee21e36SBarry Smith    Collective on SNES
1211fee21e36SBarry Smith 
1212c7afd0dbSLois Curfman McInnes    Input Parameters:
1213c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1214c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1215b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1216b3006f0bSLois Curfman McInnes           monitor routine (use PETSC_NULL if no context is desitre)
1217b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1218b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
12199b94acceSBarry Smith 
1220c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1221329f5518SBarry Smith $     int func(SNES snes,int its, PetscReal norm,void *mctx)
1222c7afd0dbSLois Curfman McInnes 
1223c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1224c7afd0dbSLois Curfman McInnes .    its - iteration number
1225c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
122640a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
12279b94acceSBarry Smith 
12289665c990SLois Curfman McInnes    Options Database Keys:
1229c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1230c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1231c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1232c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1233c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1234c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1235c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1236c7afd0dbSLois Curfman McInnes                             the options database.
12379665c990SLois Curfman McInnes 
1238639f9d9dSBarry Smith    Notes:
12396bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
12406bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
12416bc08f3fSLois Curfman McInnes    order in which they were set.
1242639f9d9dSBarry Smith 
124336851e7fSLois Curfman McInnes    Level: intermediate
124436851e7fSLois Curfman McInnes 
12459b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
12469b94acceSBarry Smith 
12475cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
12489b94acceSBarry Smith @*/
1249329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *))
12509b94acceSBarry Smith {
12513a40ed3dSBarry Smith   PetscFunctionBegin;
1252184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1253639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
125429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1255639f9d9dSBarry Smith   }
1256639f9d9dSBarry Smith 
1257639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1258b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1259639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
12603a40ed3dSBarry Smith   PetscFunctionReturn(0);
12619b94acceSBarry Smith }
12629b94acceSBarry Smith 
12634a2ae208SSatish Balay #undef __FUNCT__
12644a2ae208SSatish Balay #define __FUNCT__ "SNESClearMonitor"
12655cd90555SBarry Smith /*@C
12665cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
12675cd90555SBarry Smith 
1268c7afd0dbSLois Curfman McInnes    Collective on SNES
1269c7afd0dbSLois Curfman McInnes 
12705cd90555SBarry Smith    Input Parameters:
12715cd90555SBarry Smith .  snes - the SNES context
12725cd90555SBarry Smith 
12735cd90555SBarry Smith    Options Database:
1274c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1275c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1276c7afd0dbSLois Curfman McInnes     set via the options database
12775cd90555SBarry Smith 
12785cd90555SBarry Smith    Notes:
12795cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
12805cd90555SBarry Smith 
128136851e7fSLois Curfman McInnes    Level: intermediate
128236851e7fSLois Curfman McInnes 
12835cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
12845cd90555SBarry Smith 
12855cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
12865cd90555SBarry Smith @*/
12875cd90555SBarry Smith int SNESClearMonitor(SNES snes)
12885cd90555SBarry Smith {
12895cd90555SBarry Smith   PetscFunctionBegin;
1290184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
12915cd90555SBarry Smith   snes->numbermonitors = 0;
12925cd90555SBarry Smith   PetscFunctionReturn(0);
12935cd90555SBarry Smith }
12945cd90555SBarry Smith 
12954a2ae208SSatish Balay #undef __FUNCT__
12964a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
12979b94acceSBarry Smith /*@C
12989b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
12999b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
13009b94acceSBarry Smith 
1301fee21e36SBarry Smith    Collective on SNES
1302fee21e36SBarry Smith 
1303c7afd0dbSLois Curfman McInnes    Input Parameters:
1304c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1305c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1306c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1307c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
13089b94acceSBarry Smith 
1309c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1310329f5518SBarry Smith $     int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1311c7afd0dbSLois Curfman McInnes 
1312c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1313c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1314184914b5SBarry Smith .    reason - reason for convergence/divergence
1315c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
13164b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
13174b27c08aSLois Curfman McInnes -    f - 2-norm of function
13189b94acceSBarry Smith 
131936851e7fSLois Curfman McInnes    Level: advanced
132036851e7fSLois Curfman McInnes 
13219b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
13229b94acceSBarry Smith 
13234b27c08aSLois Curfman McInnes .seealso: SNESConverged_LS(), SNESConverged_TR()
13249b94acceSBarry Smith @*/
1325329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
13269b94acceSBarry Smith {
13273a40ed3dSBarry Smith   PetscFunctionBegin;
1328184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
13299b94acceSBarry Smith   (snes)->converged = func;
13309b94acceSBarry Smith   (snes)->cnvP      = cctx;
13313a40ed3dSBarry Smith   PetscFunctionReturn(0);
13329b94acceSBarry Smith }
13339b94acceSBarry Smith 
13344a2ae208SSatish Balay #undef __FUNCT__
13354a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
1336184914b5SBarry Smith /*@C
1337184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1338184914b5SBarry Smith 
1339184914b5SBarry Smith    Not Collective
1340184914b5SBarry Smith 
1341184914b5SBarry Smith    Input Parameter:
1342184914b5SBarry Smith .  snes - the SNES context
1343184914b5SBarry Smith 
1344184914b5SBarry Smith    Output Parameter:
1345e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1346184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1347184914b5SBarry Smith 
1348184914b5SBarry Smith    Level: intermediate
1349184914b5SBarry Smith 
1350184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1351184914b5SBarry Smith 
1352184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1353184914b5SBarry Smith 
13544b27c08aSLois Curfman McInnes .seealso: SNESSetConvergenceTest(), SNESConverged_LS(), SNESConverged_TR(), SNESConvergedReason
1355184914b5SBarry Smith @*/
1356184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1357184914b5SBarry Smith {
1358184914b5SBarry Smith   PetscFunctionBegin;
1359184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1360184914b5SBarry Smith   *reason = snes->reason;
1361184914b5SBarry Smith   PetscFunctionReturn(0);
1362184914b5SBarry Smith }
1363184914b5SBarry Smith 
13644a2ae208SSatish Balay #undef __FUNCT__
13654a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1366c9005455SLois Curfman McInnes /*@
1367c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1368c9005455SLois Curfman McInnes 
1369fee21e36SBarry Smith    Collective on SNES
1370fee21e36SBarry Smith 
1371c7afd0dbSLois Curfman McInnes    Input Parameters:
1372c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1373c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1374cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1375758f92a0SBarry Smith .  na  - size of a and its
137664731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1377758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1378c7afd0dbSLois Curfman McInnes 
1379c9005455SLois Curfman McInnes    Notes:
13804b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1381c9005455SLois Curfman McInnes    at each step.
1382c9005455SLois Curfman McInnes 
1383c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1384c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1385c9005455SLois Curfman McInnes    during the section of code that is being timed.
1386c9005455SLois Curfman McInnes 
138736851e7fSLois Curfman McInnes    Level: intermediate
138836851e7fSLois Curfman McInnes 
1389c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1390758f92a0SBarry Smith 
139108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1392758f92a0SBarry Smith 
1393c9005455SLois Curfman McInnes @*/
1394329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset)
1395c9005455SLois Curfman McInnes {
13963a40ed3dSBarry Smith   PetscFunctionBegin;
1397c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1398c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1399c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1400758f92a0SBarry Smith   snes->conv_hist_its   = its;
1401758f92a0SBarry Smith   snes->conv_hist_max   = na;
1402758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1403758f92a0SBarry Smith   PetscFunctionReturn(0);
1404758f92a0SBarry Smith }
1405758f92a0SBarry Smith 
14064a2ae208SSatish Balay #undef __FUNCT__
14074a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
14080c4c9dddSBarry Smith /*@C
1409758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1410758f92a0SBarry Smith 
1411758f92a0SBarry Smith    Collective on SNES
1412758f92a0SBarry Smith 
1413758f92a0SBarry Smith    Input Parameter:
1414758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1415758f92a0SBarry Smith 
1416758f92a0SBarry Smith    Output Parameters:
1417758f92a0SBarry Smith .  a   - array to hold history
1418758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1419758f92a0SBarry Smith          negative if not converged) for each solve.
1420758f92a0SBarry Smith -  na  - size of a and its
1421758f92a0SBarry Smith 
1422758f92a0SBarry Smith    Notes:
1423758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1424758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1425758f92a0SBarry Smith 
1426758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1427758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1428758f92a0SBarry Smith    during the section of code that is being timed.
1429758f92a0SBarry Smith 
1430758f92a0SBarry Smith    Level: intermediate
1431758f92a0SBarry Smith 
1432758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1433758f92a0SBarry Smith 
1434758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1435758f92a0SBarry Smith 
1436758f92a0SBarry Smith @*/
1437329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na)
1438758f92a0SBarry Smith {
1439758f92a0SBarry Smith   PetscFunctionBegin;
1440758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1441758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1442758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1443758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
14443a40ed3dSBarry Smith   PetscFunctionReturn(0);
1445c9005455SLois Curfman McInnes }
1446c9005455SLois Curfman McInnes 
1447e74ef692SMatthew Knepley #undef __FUNCT__
1448e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetRhsBC"
144976b2cf59SMatthew Knepley /*@
145076b2cf59SMatthew Knepley   SNESSetRhsBC - Sets the function which applies boundary conditions
145176b2cf59SMatthew Knepley   to the Rhs of each system.
145276b2cf59SMatthew Knepley 
145376b2cf59SMatthew Knepley   Collective on SNES
145476b2cf59SMatthew Knepley 
145576b2cf59SMatthew Knepley   Input Parameters:
145676b2cf59SMatthew Knepley . snes - The nonlinear solver context
145776b2cf59SMatthew Knepley . func - The function
145876b2cf59SMatthew Knepley 
145976b2cf59SMatthew Knepley   Calling sequence of func:
146076b2cf59SMatthew Knepley . func (SNES snes, Vec rhs, void *ctx);
146176b2cf59SMatthew Knepley 
146276b2cf59SMatthew Knepley . rhs - The current rhs vector
146376b2cf59SMatthew Knepley . ctx - The user-context
146476b2cf59SMatthew Knepley 
146576b2cf59SMatthew Knepley   Level: intermediate
146676b2cf59SMatthew Knepley 
146776b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
146876b2cf59SMatthew Knepley .seealso SNESDefaultRhsBC(), SNESSetSolutionBC(), SNESSetUpdate()
146976b2cf59SMatthew Knepley @*/
147076b2cf59SMatthew Knepley int SNESSetRhsBC(SNES snes, int (*func)(SNES, Vec, void *))
147176b2cf59SMatthew Knepley {
147276b2cf59SMatthew Knepley   PetscFunctionBegin;
147376b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
147476b2cf59SMatthew Knepley   snes->applyrhsbc = func;
147576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
147676b2cf59SMatthew Knepley }
147776b2cf59SMatthew Knepley 
1478e74ef692SMatthew Knepley #undef __FUNCT__
1479e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultRhsBC"
148076b2cf59SMatthew Knepley /*@
148176b2cf59SMatthew Knepley   SNESDefaultRhsBC - The default boundary condition function which does nothing.
148276b2cf59SMatthew Knepley 
148376b2cf59SMatthew Knepley   Not collective
148476b2cf59SMatthew Knepley 
148576b2cf59SMatthew Knepley   Input Parameters:
148676b2cf59SMatthew Knepley . snes - The nonlinear solver context
148776b2cf59SMatthew Knepley . rhs  - The Rhs
148876b2cf59SMatthew Knepley . ctx  - The user-context
148976b2cf59SMatthew Knepley 
149076b2cf59SMatthew Knepley   Level: beginner
149176b2cf59SMatthew Knepley 
149276b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
149376b2cf59SMatthew Knepley .seealso SNESSetRhsBC(), SNESDefaultSolutionBC(), SNESDefaultUpdate()
149476b2cf59SMatthew Knepley @*/
149576b2cf59SMatthew Knepley int SNESDefaultRhsBC(SNES snes, Vec rhs, void *ctx)
149676b2cf59SMatthew Knepley {
149776b2cf59SMatthew Knepley   PetscFunctionBegin;
149876b2cf59SMatthew Knepley   PetscFunctionReturn(0);
149976b2cf59SMatthew Knepley }
150076b2cf59SMatthew Knepley 
1501e74ef692SMatthew Knepley #undef __FUNCT__
1502e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetSolutionBC"
150376b2cf59SMatthew Knepley /*@
150476b2cf59SMatthew Knepley   SNESSetSolutionBC - Sets the function which applies boundary conditions
150576b2cf59SMatthew Knepley   to the solution of each system.
150676b2cf59SMatthew Knepley 
150776b2cf59SMatthew Knepley   Collective on SNES
150876b2cf59SMatthew Knepley 
150976b2cf59SMatthew Knepley   Input Parameters:
151076b2cf59SMatthew Knepley . snes - The nonlinear solver context
151176b2cf59SMatthew Knepley . func - The function
151276b2cf59SMatthew Knepley 
151376b2cf59SMatthew Knepley   Calling sequence of func:
1514*9d06fb6bSMatthew Knepley . func (SNES snes, Vec rsol, void *ctx);
151576b2cf59SMatthew Knepley 
151676b2cf59SMatthew Knepley . sol - The current solution vector
151776b2cf59SMatthew Knepley . ctx - The user-context
151876b2cf59SMatthew Knepley 
151976b2cf59SMatthew Knepley   Level: intermediate
152076b2cf59SMatthew Knepley 
152176b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
152276b2cf59SMatthew Knepley .seealso SNESDefaultSolutionBC(), SNESSetRhsBC(), SNESSetUpdate()
152376b2cf59SMatthew Knepley @*/
152476b2cf59SMatthew Knepley int SNESSetSolutionBC(SNES snes, int (*func)(SNES, Vec, void *))
152576b2cf59SMatthew Knepley {
152676b2cf59SMatthew Knepley   PetscFunctionBegin;
152776b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
152876b2cf59SMatthew Knepley   snes->applysolbc = func;
152976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
153076b2cf59SMatthew Knepley }
153176b2cf59SMatthew Knepley 
1532e74ef692SMatthew Knepley #undef __FUNCT__
1533e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultSolutionBC"
153476b2cf59SMatthew Knepley /*@
153576b2cf59SMatthew Knepley   SNESDefaultSolutionBC - The default boundary condition function which does nothing.
153676b2cf59SMatthew Knepley 
153776b2cf59SMatthew Knepley   Not collective
153876b2cf59SMatthew Knepley 
153976b2cf59SMatthew Knepley   Input Parameters:
154076b2cf59SMatthew Knepley . snes - The nonlinear solver context
154176b2cf59SMatthew Knepley . sol  - The solution
154276b2cf59SMatthew Knepley . ctx  - The user-context
154376b2cf59SMatthew Knepley 
154476b2cf59SMatthew Knepley   Level: beginner
154576b2cf59SMatthew Knepley 
154676b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
154776b2cf59SMatthew Knepley .seealso SNESSetSolutionBC(), SNESDefaultRhsBC(), SNESDefaultUpdate()
154876b2cf59SMatthew Knepley @*/
154976b2cf59SMatthew Knepley int SNESDefaultSolutionBC(SNES snes, Vec sol, void *ctx)
155076b2cf59SMatthew Knepley {
155176b2cf59SMatthew Knepley   PetscFunctionBegin;
155276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
155376b2cf59SMatthew Knepley }
155476b2cf59SMatthew Knepley 
1555e74ef692SMatthew Knepley #undef __FUNCT__
1556e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
155776b2cf59SMatthew Knepley /*@
155876b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
155976b2cf59SMatthew Knepley   at the beginning of every step of the iteration.
156076b2cf59SMatthew Knepley 
156176b2cf59SMatthew Knepley   Collective on SNES
156276b2cf59SMatthew Knepley 
156376b2cf59SMatthew Knepley   Input Parameters:
156476b2cf59SMatthew Knepley . snes - The nonlinear solver context
156576b2cf59SMatthew Knepley . func - The function
156676b2cf59SMatthew Knepley 
156776b2cf59SMatthew Knepley   Calling sequence of func:
156876b2cf59SMatthew Knepley . func (TS ts, int step);
156976b2cf59SMatthew Knepley 
157076b2cf59SMatthew Knepley . step - The current step of the iteration
157176b2cf59SMatthew Knepley 
157276b2cf59SMatthew Knepley   Level: intermediate
157376b2cf59SMatthew Knepley 
157476b2cf59SMatthew Knepley .keywords: SNES, update
157576b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
157676b2cf59SMatthew Knepley @*/
157776b2cf59SMatthew Knepley int SNESSetUpdate(SNES snes, int (*func)(SNES, int))
157876b2cf59SMatthew Knepley {
157976b2cf59SMatthew Knepley   PetscFunctionBegin;
158076b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
158176b2cf59SMatthew Knepley   snes->update = func;
158276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
158376b2cf59SMatthew Knepley }
158476b2cf59SMatthew Knepley 
1585e74ef692SMatthew Knepley #undef __FUNCT__
1586e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
158776b2cf59SMatthew Knepley /*@
158876b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
158976b2cf59SMatthew Knepley 
159076b2cf59SMatthew Knepley   Not collective
159176b2cf59SMatthew Knepley 
159276b2cf59SMatthew Knepley   Input Parameters:
159376b2cf59SMatthew Knepley . snes - The nonlinear solver context
159476b2cf59SMatthew Knepley . step - The current step of the iteration
159576b2cf59SMatthew Knepley 
1596205452f4SMatthew Knepley   Level: intermediate
1597205452f4SMatthew Knepley 
159876b2cf59SMatthew Knepley .keywords: SNES, update
159976b2cf59SMatthew Knepley .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultSolutionBC()
160076b2cf59SMatthew Knepley @*/
160176b2cf59SMatthew Knepley int SNESDefaultUpdate(SNES snes, int step)
160276b2cf59SMatthew Knepley {
160376b2cf59SMatthew Knepley   PetscFunctionBegin;
160476b2cf59SMatthew Knepley   PetscFunctionReturn(0);
160576b2cf59SMatthew Knepley }
160676b2cf59SMatthew Knepley 
16074a2ae208SSatish Balay #undef __FUNCT__
16084a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
16099b94acceSBarry Smith /*
16109b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
16119b94acceSBarry Smith    positive parameter delta.
16129b94acceSBarry Smith 
16139b94acceSBarry Smith     Input Parameters:
1614c7afd0dbSLois Curfman McInnes +   snes - the SNES context
16159b94acceSBarry Smith .   y - approximate solution of linear system
16169b94acceSBarry Smith .   fnorm - 2-norm of current function
1617c7afd0dbSLois Curfman McInnes -   delta - trust region size
16189b94acceSBarry Smith 
16199b94acceSBarry Smith     Output Parameters:
1620c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
16219b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
16229b94acceSBarry Smith     region, and exceeds zero otherwise.
1623c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
16249b94acceSBarry Smith 
16259b94acceSBarry Smith     Note:
16264b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
16279b94acceSBarry Smith     is set to be the maximum allowable step size.
16289b94acceSBarry Smith 
16299b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
16309b94acceSBarry Smith */
1631064f8208SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
16329b94acceSBarry Smith {
1633064f8208SBarry Smith   PetscReal   nrm;
1634ea709b57SSatish Balay   PetscScalar cnorm;
16353a40ed3dSBarry Smith   int         ierr;
16363a40ed3dSBarry Smith 
16373a40ed3dSBarry Smith   PetscFunctionBegin;
1638184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1639184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
1640184914b5SBarry Smith   PetscCheckSameComm(snes,y);
1641184914b5SBarry Smith 
1642064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
1643064f8208SBarry Smith   if (nrm > *delta) {
1644064f8208SBarry Smith      nrm = *delta/nrm;
1645064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
1646064f8208SBarry Smith      cnorm = nrm;
1647329f5518SBarry Smith      ierr = VecScale(&cnorm,y);CHKERRQ(ierr);
16489b94acceSBarry Smith      *ynorm = *delta;
16499b94acceSBarry Smith   } else {
16509b94acceSBarry Smith      *gpnorm = 0.0;
1651064f8208SBarry Smith      *ynorm = nrm;
16529b94acceSBarry Smith   }
16533a40ed3dSBarry Smith   PetscFunctionReturn(0);
16549b94acceSBarry Smith }
16559b94acceSBarry Smith 
16564a2ae208SSatish Balay #undef __FUNCT__
16574a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
16589b94acceSBarry Smith /*@
16599b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
166063a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
16619b94acceSBarry Smith 
1662c7afd0dbSLois Curfman McInnes    Collective on SNES
1663c7afd0dbSLois Curfman McInnes 
1664b2002411SLois Curfman McInnes    Input Parameters:
1665c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1666c7afd0dbSLois Curfman McInnes -  x - the solution vector
16679b94acceSBarry Smith 
16689b94acceSBarry Smith    Output Parameter:
1669b2002411SLois Curfman McInnes .  its - number of iterations until termination
16709b94acceSBarry Smith 
1671b2002411SLois Curfman McInnes    Notes:
16728ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
16738ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
16748ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
16758ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
16768ddd3da0SLois Curfman McInnes 
167736851e7fSLois Curfman McInnes    Level: beginner
167836851e7fSLois Curfman McInnes 
16799b94acceSBarry Smith .keywords: SNES, nonlinear, solve
16809b94acceSBarry Smith 
168163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
16829b94acceSBarry Smith @*/
16838ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
16849b94acceSBarry Smith {
1685f1af5d2fSBarry Smith   int        ierr;
1686f1af5d2fSBarry Smith   PetscTruth flg;
1687052efed2SBarry Smith 
16883a40ed3dSBarry Smith   PetscFunctionBegin;
168977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1690184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1691184914b5SBarry Smith   PetscCheckSameComm(snes,x);
169274679c65SBarry Smith   PetscValidIntPointer(its);
169329bbc08cSBarry Smith   if (!snes->solve) SETERRQ(1,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
169474637425SBarry Smith 
169582bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
1696c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
1697758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
1698d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
169950ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
17009b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr);
1701d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
1702b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr);
170393b98531SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESView(snes,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); }
17043a40ed3dSBarry Smith   PetscFunctionReturn(0);
17059b94acceSBarry Smith }
17069b94acceSBarry Smith 
17079b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
17089b94acceSBarry Smith 
17094a2ae208SSatish Balay #undef __FUNCT__
17104a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
171182bf6240SBarry Smith /*@C
17124b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
17139b94acceSBarry Smith 
1714fee21e36SBarry Smith    Collective on SNES
1715fee21e36SBarry Smith 
1716c7afd0dbSLois Curfman McInnes    Input Parameters:
1717c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1718454a90a3SBarry Smith -  type - a known method
1719c7afd0dbSLois Curfman McInnes 
1720c7afd0dbSLois Curfman McInnes    Options Database Key:
1721454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1722c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1723ae12b187SLois Curfman McInnes 
17249b94acceSBarry Smith    Notes:
1725e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
17264b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
1727c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
17284b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
1729c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
17309b94acceSBarry Smith 
1731ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1732ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1733ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1734ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1735ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1736ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1737ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1738ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1739ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
1740b0a32e0cSBarry Smith   appropriate method.
174136851e7fSLois Curfman McInnes 
174236851e7fSLois Curfman McInnes   Level: intermediate
1743a703fe33SLois Curfman McInnes 
1744454a90a3SBarry Smith .keywords: SNES, set, type
1745435da068SBarry Smith 
1746435da068SBarry Smith .seealso: SNESType, SNESCreate()
1747435da068SBarry Smith 
17489b94acceSBarry Smith @*/
1749454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type)
17509b94acceSBarry Smith {
17510f5bd95cSBarry Smith   int        ierr,(*r)(SNES);
17526831982aSBarry Smith   PetscTruth match;
17533a40ed3dSBarry Smith 
17543a40ed3dSBarry Smith   PetscFunctionBegin;
175577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
17560f5bd95cSBarry Smith   PetscValidCharPointer(type);
175782bf6240SBarry Smith 
17586831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
17590f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
176082bf6240SBarry Smith 
176182bf6240SBarry Smith   if (snes->setupcalled) {
1762e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
176382bf6240SBarry Smith     snes->data = 0;
176482bf6240SBarry Smith   }
17657d1a2b2bSBarry Smith 
17669b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
176782bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
176882bf6240SBarry Smith 
1769b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
177082bf6240SBarry Smith 
177129bbc08cSBarry Smith   if (!r) SETERRQ1(1,"Unable to find requested SNES type %s",type);
17721548b14aSBarry Smith 
1773606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
177482bf6240SBarry Smith   snes->data = 0;
17753a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
1776454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
177782bf6240SBarry Smith 
17783a40ed3dSBarry Smith   PetscFunctionReturn(0);
17799b94acceSBarry Smith }
17809b94acceSBarry Smith 
1781a847f771SSatish Balay 
17829b94acceSBarry Smith /* --------------------------------------------------------------------- */
17834a2ae208SSatish Balay #undef __FUNCT__
17844a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
17859b94acceSBarry Smith /*@C
17869b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
1787f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
17889b94acceSBarry Smith 
1789fee21e36SBarry Smith    Not Collective
1790fee21e36SBarry Smith 
179136851e7fSLois Curfman McInnes    Level: advanced
179236851e7fSLois Curfman McInnes 
17939b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
17949b94acceSBarry Smith 
17959b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
17969b94acceSBarry Smith @*/
1797cf256101SBarry Smith int SNESRegisterDestroy(void)
17989b94acceSBarry Smith {
179982bf6240SBarry Smith   int ierr;
180082bf6240SBarry Smith 
18013a40ed3dSBarry Smith   PetscFunctionBegin;
180282bf6240SBarry Smith   if (SNESList) {
1803b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
180482bf6240SBarry Smith     SNESList = 0;
18059b94acceSBarry Smith   }
18064c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
18073a40ed3dSBarry Smith   PetscFunctionReturn(0);
18089b94acceSBarry Smith }
18099b94acceSBarry Smith 
18104a2ae208SSatish Balay #undef __FUNCT__
18114a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
18129b94acceSBarry Smith /*@C
18139a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
18149b94acceSBarry Smith 
1815c7afd0dbSLois Curfman McInnes    Not Collective
1816c7afd0dbSLois Curfman McInnes 
18179b94acceSBarry Smith    Input Parameter:
18184b0e389bSBarry Smith .  snes - nonlinear solver context
18199b94acceSBarry Smith 
18209b94acceSBarry Smith    Output Parameter:
18213a7fca6bSBarry Smith .  type - SNES method (a character string)
18229b94acceSBarry Smith 
182336851e7fSLois Curfman McInnes    Level: intermediate
182436851e7fSLois Curfman McInnes 
1825454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
18269b94acceSBarry Smith @*/
1827454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type)
18289b94acceSBarry Smith {
18293a40ed3dSBarry Smith   PetscFunctionBegin;
1830184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1831454a90a3SBarry Smith   *type = snes->type_name;
18323a40ed3dSBarry Smith   PetscFunctionReturn(0);
18339b94acceSBarry Smith }
18349b94acceSBarry Smith 
18354a2ae208SSatish Balay #undef __FUNCT__
18364a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
18379b94acceSBarry Smith /*@C
18389b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
18399b94acceSBarry Smith    stored.
18409b94acceSBarry Smith 
1841c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1842c7afd0dbSLois Curfman McInnes 
18439b94acceSBarry Smith    Input Parameter:
18449b94acceSBarry Smith .  snes - the SNES context
18459b94acceSBarry Smith 
18469b94acceSBarry Smith    Output Parameter:
18479b94acceSBarry Smith .  x - the solution
18489b94acceSBarry Smith 
184936851e7fSLois Curfman McInnes    Level: advanced
185036851e7fSLois Curfman McInnes 
18519b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
18529b94acceSBarry Smith 
18534b27c08aSLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetSolutionUpdate()
18549b94acceSBarry Smith @*/
18559b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
18569b94acceSBarry Smith {
18573a40ed3dSBarry Smith   PetscFunctionBegin;
185877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18599b94acceSBarry Smith   *x = snes->vec_sol_always;
18603a40ed3dSBarry Smith   PetscFunctionReturn(0);
18619b94acceSBarry Smith }
18629b94acceSBarry Smith 
18634a2ae208SSatish Balay #undef __FUNCT__
18644a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
18659b94acceSBarry Smith /*@C
18669b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
18679b94acceSBarry Smith    stored.
18689b94acceSBarry Smith 
1869c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1870c7afd0dbSLois Curfman McInnes 
18719b94acceSBarry Smith    Input Parameter:
18729b94acceSBarry Smith .  snes - the SNES context
18739b94acceSBarry Smith 
18749b94acceSBarry Smith    Output Parameter:
18759b94acceSBarry Smith .  x - the solution update
18769b94acceSBarry Smith 
187736851e7fSLois Curfman McInnes    Level: advanced
187836851e7fSLois Curfman McInnes 
18799b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
18809b94acceSBarry Smith 
18819b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
18829b94acceSBarry Smith @*/
18839b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
18849b94acceSBarry Smith {
18853a40ed3dSBarry Smith   PetscFunctionBegin;
188677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18879b94acceSBarry Smith   *x = snes->vec_sol_update_always;
18883a40ed3dSBarry Smith   PetscFunctionReturn(0);
18899b94acceSBarry Smith }
18909b94acceSBarry Smith 
18914a2ae208SSatish Balay #undef __FUNCT__
18924a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
18939b94acceSBarry Smith /*@C
18943638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
18959b94acceSBarry Smith 
1896c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1897c7afd0dbSLois Curfman McInnes 
18989b94acceSBarry Smith    Input Parameter:
18999b94acceSBarry Smith .  snes - the SNES context
19009b94acceSBarry Smith 
19019b94acceSBarry Smith    Output Parameter:
19027bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
190300036973SBarry Smith .  ctx - the function context (or PETSC_NULL)
190400036973SBarry Smith -  func - the function (or PETSC_NULL)
19059b94acceSBarry Smith 
190636851e7fSLois Curfman McInnes    Level: advanced
190736851e7fSLois Curfman McInnes 
1908a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
19099b94acceSBarry Smith 
19104b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
19119b94acceSBarry Smith @*/
191200036973SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx,int (**func)(SNES,Vec,Vec,void*))
19139b94acceSBarry Smith {
19143a40ed3dSBarry Smith   PetscFunctionBegin;
191577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
19167bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
19177bf4e008SBarry Smith   if (ctx)  *ctx  = snes->funP;
191800036973SBarry Smith   if (func) *func = snes->computefunction;
19193a40ed3dSBarry Smith   PetscFunctionReturn(0);
19209b94acceSBarry Smith }
19219b94acceSBarry Smith 
19224a2ae208SSatish Balay #undef __FUNCT__
19234a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
19243c7409f5SSatish Balay /*@C
19253c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
1926d850072dSLois Curfman McInnes    SNES options in the database.
19273c7409f5SSatish Balay 
1928fee21e36SBarry Smith    Collective on SNES
1929fee21e36SBarry Smith 
1930c7afd0dbSLois Curfman McInnes    Input Parameter:
1931c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1932c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
1933c7afd0dbSLois Curfman McInnes 
1934d850072dSLois Curfman McInnes    Notes:
1935a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
1936c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
1937d850072dSLois Curfman McInnes 
193836851e7fSLois Curfman McInnes    Level: advanced
193936851e7fSLois Curfman McInnes 
19403c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
1941a86d99e1SLois Curfman McInnes 
1942a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
19433c7409f5SSatish Balay @*/
19443c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
19453c7409f5SSatish Balay {
19463c7409f5SSatish Balay   int ierr;
19473c7409f5SSatish Balay 
19483a40ed3dSBarry Smith   PetscFunctionBegin;
194977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1950639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
19513c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
19523a40ed3dSBarry Smith   PetscFunctionReturn(0);
19533c7409f5SSatish Balay }
19543c7409f5SSatish Balay 
19554a2ae208SSatish Balay #undef __FUNCT__
19564a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
19573c7409f5SSatish Balay /*@C
1958f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
1959d850072dSLois Curfman McInnes    SNES options in the database.
19603c7409f5SSatish Balay 
1961fee21e36SBarry Smith    Collective on SNES
1962fee21e36SBarry Smith 
1963c7afd0dbSLois Curfman McInnes    Input Parameters:
1964c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1965c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
1966c7afd0dbSLois Curfman McInnes 
1967d850072dSLois Curfman McInnes    Notes:
1968a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
1969c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
1970d850072dSLois Curfman McInnes 
197136851e7fSLois Curfman McInnes    Level: advanced
197236851e7fSLois Curfman McInnes 
19733c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
1974a86d99e1SLois Curfman McInnes 
1975a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
19763c7409f5SSatish Balay @*/
19773c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
19783c7409f5SSatish Balay {
19793c7409f5SSatish Balay   int ierr;
19803c7409f5SSatish Balay 
19813a40ed3dSBarry Smith   PetscFunctionBegin;
198277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1983639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
19843c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
19853a40ed3dSBarry Smith   PetscFunctionReturn(0);
19863c7409f5SSatish Balay }
19873c7409f5SSatish Balay 
19884a2ae208SSatish Balay #undef __FUNCT__
19894a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
19909ab63eb5SSatish Balay /*@C
19913c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
19923c7409f5SSatish Balay    SNES options in the database.
19933c7409f5SSatish Balay 
1994c7afd0dbSLois Curfman McInnes    Not Collective
1995c7afd0dbSLois Curfman McInnes 
19963c7409f5SSatish Balay    Input Parameter:
19973c7409f5SSatish Balay .  snes - the SNES context
19983c7409f5SSatish Balay 
19993c7409f5SSatish Balay    Output Parameter:
20003c7409f5SSatish Balay .  prefix - pointer to the prefix string used
20013c7409f5SSatish Balay 
20029ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
20039ab63eb5SSatish Balay    sufficient length to hold the prefix.
20049ab63eb5SSatish Balay 
200536851e7fSLois Curfman McInnes    Level: advanced
200636851e7fSLois Curfman McInnes 
20073c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2008a86d99e1SLois Curfman McInnes 
2009a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
20103c7409f5SSatish Balay @*/
20113c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
20123c7409f5SSatish Balay {
20133c7409f5SSatish Balay   int ierr;
20143c7409f5SSatish Balay 
20153a40ed3dSBarry Smith   PetscFunctionBegin;
201677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2017639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
20183a40ed3dSBarry Smith   PetscFunctionReturn(0);
20193c7409f5SSatish Balay }
20203c7409f5SSatish Balay 
2021acb85ae6SSatish Balay /*MC
2022f1af5d2fSBarry Smith    SNESRegisterDynamic - Adds a method to the nonlinear solver package.
20239b94acceSBarry Smith 
2024b2002411SLois Curfman McInnes    Synopsis:
20253a7fca6bSBarry Smith    int SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
20269b94acceSBarry Smith 
20278d76a1e5SLois Curfman McInnes    Not collective
20288d76a1e5SLois Curfman McInnes 
2029b2002411SLois Curfman McInnes    Input Parameters:
2030c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2031b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2032b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2033c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
20349b94acceSBarry Smith 
2035b2002411SLois Curfman McInnes    Notes:
2036f1af5d2fSBarry Smith    SNESRegisterDynamic() may be called multiple times to add several user-defined solvers.
20373a40ed3dSBarry Smith 
2038b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2039b2002411SLois Curfman McInnes    is ignored.
2040b2002411SLois Curfman McInnes 
2041b9617806SBarry Smith    Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT},
20425ba88a07SLois Curfman McInnes    and others of the form ${any_environmental_variable} occuring in pathname will be
20435ba88a07SLois Curfman McInnes    replaced with appropriate values.
20445ba88a07SLois Curfman McInnes 
2045b2002411SLois Curfman McInnes    Sample usage:
204669225d78SLois Curfman McInnes .vb
2047f1af5d2fSBarry Smith    SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2048b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
204969225d78SLois Curfman McInnes .ve
2050b2002411SLois Curfman McInnes 
2051b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2052b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2053b2002411SLois Curfman McInnes    or at runtime via the option
2054b2002411SLois Curfman McInnes $     -snes_type my_solver
2055b2002411SLois Curfman McInnes 
205636851e7fSLois Curfman McInnes    Level: advanced
205736851e7fSLois Curfman McInnes 
2058b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2059b2002411SLois Curfman McInnes 
2060b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2061b2002411SLois Curfman McInnes M*/
2062b2002411SLois Curfman McInnes 
20634a2ae208SSatish Balay #undef __FUNCT__
20644a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
2065f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES))
2066b2002411SLois Curfman McInnes {
2067b2002411SLois Curfman McInnes   char fullname[256];
2068b2002411SLois Curfman McInnes   int  ierr;
2069b2002411SLois Curfman McInnes 
2070b2002411SLois Curfman McInnes   PetscFunctionBegin;
2071b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2072c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2073b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2074b2002411SLois Curfman McInnes }
2075