xref: /petsc/src/snes/interface/snes.c (revision b8a78c4a112c1f7cf1d4f71aab23e55422181bbb)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*b8a78c4aSBarry Smith static char vcid[] = "$Id: snes.c,v 1.183 1999/04/19 22:15:28 bsmith Exp bsmith $";
39b94acceSBarry Smith #endif
49b94acceSBarry Smith 
570f55243SBarry Smith #include "src/snes/snesimpl.h"      /*I "snes.h"  I*/
69b94acceSBarry Smith 
784cb2905SBarry Smith int SNESRegisterAllCalled = 0;
8488ecbafSBarry Smith FList SNESList = 0;
982bf6240SBarry Smith 
105615d1e5SSatish Balay #undef __FUNC__
11d4bb536fSBarry Smith #define __FUNC__ "SNESView"
129b94acceSBarry Smith /*@
139b94acceSBarry Smith    SNESView - Prints the SNES data structure.
149b94acceSBarry Smith 
15fee21e36SBarry Smith    Collective on SNES, unless Viewer is VIEWER_STDOUT_SELF
16fee21e36SBarry Smith 
17c7afd0dbSLois Curfman McInnes    Input Parameters:
18c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
19c7afd0dbSLois Curfman McInnes -  viewer - visualization context
20c7afd0dbSLois Curfman McInnes 
219b94acceSBarry Smith    Options Database Key:
22c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
239b94acceSBarry Smith 
249b94acceSBarry Smith    Notes:
259b94acceSBarry Smith    The available visualization contexts include
26c7afd0dbSLois Curfman McInnes +     VIEWER_STDOUT_SELF - standard output (default)
27c7afd0dbSLois Curfman McInnes -     VIEWER_STDOUT_WORLD - synchronized standard
28c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
29c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
30c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
319b94acceSBarry Smith 
323e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
3377ed5343SBarry Smith    ViewerASCIIOpen() - output to a specified file.
349b94acceSBarry Smith 
3536851e7fSLois Curfman McInnes    Level: beginner
3636851e7fSLois Curfman McInnes 
379b94acceSBarry Smith .keywords: SNES, view
389b94acceSBarry Smith 
3977ed5343SBarry Smith .seealso: ViewerASCIIOpen()
409b94acceSBarry Smith @*/
419b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer)
429b94acceSBarry Smith {
439b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
449b94acceSBarry Smith   int                 ierr;
459b94acceSBarry Smith   SLES                sles;
469b94acceSBarry Smith   char                *method;
4719bcc07fSBarry Smith   ViewerType          vtype;
489b94acceSBarry Smith 
493a40ed3dSBarry Smith   PetscFunctionBegin;
5074679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5174679c65SBarry Smith   if (viewer) {PetscValidHeader(viewer);}
5274679c65SBarry Smith   else { viewer = VIEWER_STDOUT_SELF; }
5374679c65SBarry Smith 
5419bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
553f1db9ecSBarry Smith   if (PetscTypeCompare(vtype,ASCII_VIEWER)) {
56d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
57d132466eSBarry Smith     ierr = SNESGetType(snes,&method);CHKERRQ(ierr);
58d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  method: %s\n",method);CHKERRQ(ierr);
590ef38995SBarry Smith     if (snes->view) {
600ef38995SBarry Smith       ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
610ef38995SBarry Smith       ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr);
620ef38995SBarry Smith       ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
630ef38995SBarry Smith     }
64d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
65d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n",
66d132466eSBarry Smith                  snes->rtol, snes->atol, snes->trunctol, snes->xtol);CHKERRQ(ierr);
67d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
68d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
690ef38995SBarry Smith     if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
70d132466eSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"  min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr);
710ef38995SBarry Smith     }
729b94acceSBarry Smith     if (snes->ksp_ewconv) {
739b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
749b94acceSBarry Smith       if (kctx) {
75d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
76d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
77d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"    gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
789b94acceSBarry Smith       }
799b94acceSBarry Smith     }
803f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,STRING_VIEWER)) {
810ef38995SBarry Smith     ierr = SNESGetType(snes,&method);CHKERRQ(ierr);
820ef38995SBarry Smith     ierr = ViewerStringSPrintf(viewer," %-3.3s",method);CHKERRQ(ierr);
8319bcc07fSBarry Smith   }
8477ed5343SBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
850ef38995SBarry Smith   ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
869b94acceSBarry Smith   ierr = SLESView(sles,viewer); CHKERRQ(ierr);
870ef38995SBarry Smith   ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
883a40ed3dSBarry Smith   PetscFunctionReturn(0);
899b94acceSBarry Smith }
909b94acceSBarry Smith 
91639f9d9dSBarry Smith /*
92639f9d9dSBarry Smith        We retain a list of functions that also take SNES command
93639f9d9dSBarry Smith     line options. These are called at the end SNESSetFromOptions()
94639f9d9dSBarry Smith */
95639f9d9dSBarry Smith #define MAXSETFROMOPTIONS 5
96639f9d9dSBarry Smith static int numberofsetfromoptions;
97639f9d9dSBarry Smith static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
98639f9d9dSBarry Smith 
995615d1e5SSatish Balay #undef __FUNC__
100d4bb536fSBarry Smith #define __FUNC__ "SNESAddOptionsChecker"
101639f9d9dSBarry Smith /*@
102639f9d9dSBarry Smith     SNESAddOptionsChecker - Adds an additional function to check for SNES options.
103639f9d9dSBarry Smith 
104c7afd0dbSLois Curfman McInnes     Not Collective
105c7afd0dbSLois Curfman McInnes 
106639f9d9dSBarry Smith     Input Parameter:
107639f9d9dSBarry Smith .   snescheck - function that checks for options
108639f9d9dSBarry Smith 
10936851e7fSLois Curfman McInnes     Level: developer
11036851e7fSLois Curfman McInnes 
111639f9d9dSBarry Smith .seealso: SNESSetFromOptions()
112639f9d9dSBarry Smith @*/
113639f9d9dSBarry Smith int SNESAddOptionsChecker(int (*snescheck)(SNES) )
114639f9d9dSBarry Smith {
1153a40ed3dSBarry Smith   PetscFunctionBegin;
116639f9d9dSBarry Smith   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
117a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many options checkers, only 5 allowed");
118639f9d9dSBarry Smith   }
119639f9d9dSBarry Smith 
120639f9d9dSBarry Smith   othersetfromoptions[numberofsetfromoptions++] = snescheck;
1213a40ed3dSBarry Smith   PetscFunctionReturn(0);
122639f9d9dSBarry Smith }
123639f9d9dSBarry Smith 
1245615d1e5SSatish Balay #undef __FUNC__
12515091d37SBarry Smith #define __FUNC__ "SNESSetTypeFromOptions"
12615091d37SBarry Smith /*@
12715091d37SBarry Smith    SNESSetTypeFromOptions - Sets the SNES solver type from the options database,
12815091d37SBarry Smith         or sets a default if none is give.
12915091d37SBarry Smith 
13015091d37SBarry Smith    Collective on SNES
13115091d37SBarry Smith 
13215091d37SBarry Smith    Input Parameter:
13315091d37SBarry Smith .  snes - the SNES context
13415091d37SBarry Smith 
13515091d37SBarry Smith    Options Database Keys:
13615091d37SBarry Smith .  -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc
13715091d37SBarry Smith 
13815091d37SBarry Smith    Level: beginner
13915091d37SBarry Smith 
14015091d37SBarry Smith .keywords: SNES, nonlinear, set, options, database
14115091d37SBarry Smith 
14215091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetFromOptions()
14315091d37SBarry Smith @*/
14415091d37SBarry Smith int SNESSetTypeFromOptions(SNES snes)
14515091d37SBarry Smith {
14615091d37SBarry Smith   char     method[256];
14715091d37SBarry Smith   int      ierr, flg;
14815091d37SBarry Smith 
14915091d37SBarry Smith   PetscFunctionBegin;
15015091d37SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15115091d37SBarry Smith   if (snes->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to SNESSetUp()");
15215091d37SBarry Smith   ierr = OptionsGetString(snes->prefix,"-snes_type",method,256,&flg);
15315091d37SBarry Smith   if (flg) {
15415091d37SBarry Smith     ierr = SNESSetType(snes,(SNESType) method); CHKERRQ(ierr);
15515091d37SBarry Smith   }
15615091d37SBarry Smith   /*
15715091d37SBarry Smith       If SNES type has not yet been set, set it now
15815091d37SBarry Smith   */
15915091d37SBarry Smith   if (!snes->type_name) {
16015091d37SBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
16115091d37SBarry Smith       ierr = SNESSetType(snes,SNES_EQ_LS); CHKERRQ(ierr);
16215091d37SBarry Smith     } else {
16315091d37SBarry Smith       ierr = SNESSetType(snes,SNES_UM_TR); CHKERRQ(ierr);
16415091d37SBarry Smith     }
16515091d37SBarry Smith   }
16615091d37SBarry Smith   PetscFunctionReturn(0);
16715091d37SBarry Smith }
16815091d37SBarry Smith 
16915091d37SBarry Smith #undef __FUNC__
1705615d1e5SSatish Balay #define __FUNC__ "SNESSetFromOptions"
1719b94acceSBarry Smith /*@
172682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
1739b94acceSBarry Smith 
174c7afd0dbSLois Curfman McInnes    Collective on SNES
175c7afd0dbSLois Curfman McInnes 
1769b94acceSBarry Smith    Input Parameter:
1779b94acceSBarry Smith .  snes - the SNES context
1789b94acceSBarry Smith 
17936851e7fSLois Curfman McInnes    Options Database Keys:
180b39c3a46SLois Curfman McInnes +  -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc
18182738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
18282738288SBarry Smith                 of the change in the solution between steps
183b39c3a46SLois Curfman McInnes .  -snes_atol <atol> - absolute tolerance of residual norm
184b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
185b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
186b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
187b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
18882738288SBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear or minimization
18982738288SBarry Smith                                solver; hence iterations will continue until max_it
1901fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
19182738288SBarry Smith                                of convergence test
19282738288SBarry Smith .  -snes_monitor - prints residual norm at each iteration
193d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
194d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
19582738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
196e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
19736851e7fSLois Curfman McInnes -  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
19882738288SBarry Smith 
19982738288SBarry Smith     Options Database for Eisenstat-Walker method:
20082738288SBarry Smith +  -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence
20182738288SBarry Smith .  -snes_ksp_eq_version ver - version of  Eisenstat-Walker method
20236851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
20336851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
20436851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
20536851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
20636851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
20736851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
20882738288SBarry Smith 
20911ca99fdSLois Curfman McInnes    Notes:
21011ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
21111ca99fdSLois Curfman McInnes    the users manual.
21283e2fdc7SBarry Smith 
21336851e7fSLois Curfman McInnes    Level: beginner
21436851e7fSLois Curfman McInnes 
2159b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2169b94acceSBarry Smith 
21715091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetTypeFromOptions()
2189b94acceSBarry Smith @*/
2199b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
2209b94acceSBarry Smith {
2219b94acceSBarry Smith   double   tmp;
2229b94acceSBarry Smith   SLES     sles;
22381f57ec7SBarry Smith   int      ierr, flg,i,loc[4],nmax = 4;
2243c7409f5SSatish Balay   int      version   = PETSC_DEFAULT;
2259b94acceSBarry Smith   double   rtol_0    = PETSC_DEFAULT;
2269b94acceSBarry Smith   double   rtol_max  = PETSC_DEFAULT;
2279b94acceSBarry Smith   double   gamma2    = PETSC_DEFAULT;
2289b94acceSBarry Smith   double   alpha     = PETSC_DEFAULT;
2299b94acceSBarry Smith   double   alpha2    = PETSC_DEFAULT;
2309b94acceSBarry Smith   double   threshold = PETSC_DEFAULT;
2319b94acceSBarry Smith 
2323a40ed3dSBarry Smith   PetscFunctionBegin;
23377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
23415091d37SBarry Smith   ierr = SNESSetTypeFromOptions(snes);CHKERRQ(ierr);
235ca161407SBarry Smith 
23615091d37SBarry Smith   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
2373c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg); CHKERRQ(ierr);
238d64ed03dSBarry Smith   if (flg) {
239d64ed03dSBarry Smith     ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
240d64ed03dSBarry Smith   }
2413c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg); CHKERRQ(ierr);
242d64ed03dSBarry Smith   if (flg) {
243d64ed03dSBarry Smith     ierr = SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
244d64ed03dSBarry Smith   }
2453c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg);  CHKERRQ(ierr);
246d64ed03dSBarry Smith   if (flg) {
247d64ed03dSBarry Smith     ierr = SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
248d64ed03dSBarry Smith   }
2493c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg); CHKERRQ(ierr);
2503c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr);
251d7a720efSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg); CHKERRQ(ierr);
252d64ed03dSBarry Smith   if (flg) { ierr = SNESSetTrustRegionTolerance(snes,tmp); CHKERRQ(ierr); }
253d7a720efSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg); CHKERRQ(ierr);
254d64ed03dSBarry Smith   if (flg) { ierr = SNESSetMinimizationFunctionTolerance(snes,tmp);  CHKERRQ(ierr);}
2553c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg); CHKERRQ(ierr);
2563c7409f5SSatish Balay   if (flg) { snes->ksp_ewconv = 1; }
257b18e04deSLois Curfman McInnes   ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg); CHKERRQ(ierr);
258b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg); CHKERRQ(ierr);
259b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr);
260b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg); CHKERRQ(ierr);
261b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg); CHKERRQ(ierr);
262b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg); CHKERRQ(ierr);
263b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr);
26493c39befSBarry Smith 
26593c39befSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_no_convergence_test",&flg); CHKERRQ(ierr);
26693c39befSBarry Smith   if (flg) {snes->converged = 0;}
26793c39befSBarry Smith 
2689b94acceSBarry Smith   ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha,
2699b94acceSBarry Smith                                   alpha2,threshold); CHKERRQ(ierr);
2705bbad29bSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_cancelmonitors",&flg); CHKERRQ(ierr);
2715cd90555SBarry Smith   if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
2723c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg); CHKERRQ(ierr);
273*b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);}
2743c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg); CHKERRQ(ierr);
275*b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);  CHKERRQ(ierr);}
2763f1db9ecSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor",&flg); CHKERRQ(ierr);
277*b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);  CHKERRQ(ierr);}
278d132466eSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor_update",&flg); CHKERRQ(ierr);
279*b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitorUpdate,0,0);  CHKERRQ(ierr);}
28081f57ec7SBarry Smith   ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr);
2813c7409f5SSatish Balay   if (flg) {
28217699dbbSLois Curfman McInnes     int    rank = 0;
283d7e8b826SBarry Smith     DrawLG lg;
28417699dbbSLois Curfman McInnes     MPI_Initialized(&rank);
285d132466eSBarry Smith     if (rank) {ierr = MPI_Comm_rank(snes->comm,&rank);CHKERRQ(ierr);}
28617699dbbSLois Curfman McInnes     if (!rank) {
28781f57ec7SBarry Smith       ierr = SNESLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr);
288*b8a78c4aSBarry Smith       ierr = SNESSetMonitor(snes,SNESLGMonitor,lg,( int (*)(void *))SNESLGMonitorDestroy); CHKERRQ(ierr);
2899b94acceSBarry Smith       PLogObjectParent(snes,lg);
2909b94acceSBarry Smith     }
2919b94acceSBarry Smith   }
292e24b481bSBarry Smith 
2933c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);  CHKERRQ(ierr);
2943c7409f5SSatish Balay   if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
2959b94acceSBarry Smith     ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,
2969b94acceSBarry Smith                  SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr);
297981c4779SBarry Smith     PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
298981c4779SBarry Smith   } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
29931615d04SLois Curfman McInnes     ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,
30031615d04SLois Curfman McInnes                  SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr);
301d64ed03dSBarry Smith     PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
3029b94acceSBarry Smith   }
303639f9d9dSBarry Smith 
304639f9d9dSBarry Smith   for ( i=0; i<numberofsetfromoptions; i++ ) {
305639f9d9dSBarry Smith     ierr = (*othersetfromoptions[i])(snes); CHKERRQ(ierr);
306639f9d9dSBarry Smith   }
307639f9d9dSBarry Smith 
3089b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
3099b94acceSBarry Smith   ierr = SLESSetFromOptions(sles); CHKERRQ(ierr);
31093993e2dSLois Curfman McInnes 
311e24b481bSBarry Smith   /* set the special KSP monitor for matrix-free application */
312e24b481bSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg); CHKERRQ(ierr);
313e24b481bSBarry Smith   if (flg) {
314e24b481bSBarry Smith     KSP ksp;
315e24b481bSBarry Smith     ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
316*b8a78c4aSBarry Smith     ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr);
317e24b481bSBarry Smith   }
318e24b481bSBarry Smith 
31982bf6240SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr);
32082bf6240SBarry Smith   if (flg) { ierr = SNESPrintHelp(snes); CHKERRQ(ierr);}
32182bf6240SBarry Smith 
3223a40ed3dSBarry Smith   if (snes->setfromoptions) {
3233a40ed3dSBarry Smith     ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
3243a40ed3dSBarry Smith   }
3253a40ed3dSBarry Smith   PetscFunctionReturn(0);
3269b94acceSBarry Smith }
3279b94acceSBarry Smith 
328a847f771SSatish Balay 
3295615d1e5SSatish Balay #undef __FUNC__
330d4bb536fSBarry Smith #define __FUNC__ "SNESSetApplicationContext"
3319b94acceSBarry Smith /*@
3329b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3339b94acceSBarry Smith    the nonlinear solvers.
3349b94acceSBarry Smith 
335fee21e36SBarry Smith    Collective on SNES
336fee21e36SBarry Smith 
337c7afd0dbSLois Curfman McInnes    Input Parameters:
338c7afd0dbSLois Curfman McInnes +  snes - the SNES context
339c7afd0dbSLois Curfman McInnes -  usrP - optional user context
340c7afd0dbSLois Curfman McInnes 
34136851e7fSLois Curfman McInnes    Level: intermediate
34236851e7fSLois Curfman McInnes 
3439b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3449b94acceSBarry Smith 
3459b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3469b94acceSBarry Smith @*/
3479b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
3489b94acceSBarry Smith {
3493a40ed3dSBarry Smith   PetscFunctionBegin;
35077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3519b94acceSBarry Smith   snes->user		= usrP;
3523a40ed3dSBarry Smith   PetscFunctionReturn(0);
3539b94acceSBarry Smith }
35474679c65SBarry Smith 
3555615d1e5SSatish Balay #undef __FUNC__
356d4bb536fSBarry Smith #define __FUNC__ "SNESGetApplicationContext"
3579b94acceSBarry Smith /*@C
3589b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3599b94acceSBarry Smith    nonlinear solvers.
3609b94acceSBarry Smith 
361c7afd0dbSLois Curfman McInnes    Not Collective
362c7afd0dbSLois Curfman McInnes 
3639b94acceSBarry Smith    Input Parameter:
3649b94acceSBarry Smith .  snes - SNES context
3659b94acceSBarry Smith 
3669b94acceSBarry Smith    Output Parameter:
3679b94acceSBarry Smith .  usrP - user context
3689b94acceSBarry Smith 
36936851e7fSLois Curfman McInnes    Level: intermediate
37036851e7fSLois Curfman McInnes 
3719b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3729b94acceSBarry Smith 
3739b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3749b94acceSBarry Smith @*/
3759b94acceSBarry Smith int SNESGetApplicationContext( SNES snes,  void **usrP )
3769b94acceSBarry Smith {
3773a40ed3dSBarry Smith   PetscFunctionBegin;
37877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3799b94acceSBarry Smith   *usrP = snes->user;
3803a40ed3dSBarry Smith   PetscFunctionReturn(0);
3819b94acceSBarry Smith }
38274679c65SBarry Smith 
3835615d1e5SSatish Balay #undef __FUNC__
384d4bb536fSBarry Smith #define __FUNC__ "SNESGetIterationNumber"
3859b94acceSBarry Smith /*@
386c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
387c8228a4eSBarry Smith    at this time.
3889b94acceSBarry Smith 
389c7afd0dbSLois Curfman McInnes    Not Collective
390c7afd0dbSLois Curfman McInnes 
3919b94acceSBarry Smith    Input Parameter:
3929b94acceSBarry Smith .  snes - SNES context
3939b94acceSBarry Smith 
3949b94acceSBarry Smith    Output Parameter:
3959b94acceSBarry Smith .  iter - iteration number
3969b94acceSBarry Smith 
397c8228a4eSBarry Smith    Notes:
398c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
399c8228a4eSBarry Smith 
400c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
40108405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
40208405cd6SLois Curfman McInnes .vb
40308405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
40408405cd6SLois Curfman McInnes       if (!(it % 2)) {
40508405cd6SLois Curfman McInnes         [compute Jacobian here]
40608405cd6SLois Curfman McInnes       }
40708405cd6SLois Curfman McInnes .ve
408c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
40908405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
410c8228a4eSBarry Smith 
41136851e7fSLois Curfman McInnes    Level: intermediate
41236851e7fSLois Curfman McInnes 
4139b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
4149b94acceSBarry Smith @*/
4159b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
4169b94acceSBarry Smith {
4173a40ed3dSBarry Smith   PetscFunctionBegin;
41877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
41974679c65SBarry Smith   PetscValidIntPointer(iter);
4209b94acceSBarry Smith   *iter = snes->iter;
4213a40ed3dSBarry Smith   PetscFunctionReturn(0);
4229b94acceSBarry Smith }
42374679c65SBarry Smith 
4245615d1e5SSatish Balay #undef __FUNC__
4255615d1e5SSatish Balay #define __FUNC__ "SNESGetFunctionNorm"
4269b94acceSBarry Smith /*@
4279b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4289b94acceSBarry Smith    with SNESSSetFunction().
4299b94acceSBarry Smith 
430c7afd0dbSLois Curfman McInnes    Collective on SNES
431c7afd0dbSLois Curfman McInnes 
4329b94acceSBarry Smith    Input Parameter:
4339b94acceSBarry Smith .  snes - SNES context
4349b94acceSBarry Smith 
4359b94acceSBarry Smith    Output Parameter:
4369b94acceSBarry Smith .  fnorm - 2-norm of function
4379b94acceSBarry Smith 
4389b94acceSBarry Smith    Note:
4399b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
440a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
441a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
4429b94acceSBarry Smith 
44336851e7fSLois Curfman McInnes    Level: intermediate
44436851e7fSLois Curfman McInnes 
4459b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
446a86d99e1SLois Curfman McInnes 
44708405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
4489b94acceSBarry Smith @*/
4499b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm)
4509b94acceSBarry Smith {
4513a40ed3dSBarry Smith   PetscFunctionBegin;
45277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
45374679c65SBarry Smith   PetscValidScalarPointer(fnorm);
45474679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
455d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only");
45674679c65SBarry Smith   }
4579b94acceSBarry Smith   *fnorm = snes->norm;
4583a40ed3dSBarry Smith   PetscFunctionReturn(0);
4599b94acceSBarry Smith }
46074679c65SBarry Smith 
4615615d1e5SSatish Balay #undef __FUNC__
4625615d1e5SSatish Balay #define __FUNC__ "SNESGetGradientNorm"
4639b94acceSBarry Smith /*@
4649b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
4659b94acceSBarry Smith    with SNESSSetGradient().
4669b94acceSBarry Smith 
467c7afd0dbSLois Curfman McInnes    Collective on SNES
468c7afd0dbSLois Curfman McInnes 
4699b94acceSBarry Smith    Input Parameter:
4709b94acceSBarry Smith .  snes - SNES context
4719b94acceSBarry Smith 
4729b94acceSBarry Smith    Output Parameter:
4739b94acceSBarry Smith .  fnorm - 2-norm of gradient
4749b94acceSBarry Smith 
4759b94acceSBarry Smith    Note:
4769b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
477a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
478a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
4799b94acceSBarry Smith 
48036851e7fSLois Curfman McInnes    Level: intermediate
48136851e7fSLois Curfman McInnes 
4829b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
483a86d99e1SLois Curfman McInnes 
484a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
4859b94acceSBarry Smith @*/
4869b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm)
4879b94acceSBarry Smith {
4883a40ed3dSBarry Smith   PetscFunctionBegin;
48977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
49074679c65SBarry Smith   PetscValidScalarPointer(gnorm);
49174679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
492d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
49374679c65SBarry Smith   }
4949b94acceSBarry Smith   *gnorm = snes->norm;
4953a40ed3dSBarry Smith   PetscFunctionReturn(0);
4969b94acceSBarry Smith }
49774679c65SBarry Smith 
4985615d1e5SSatish Balay #undef __FUNC__
499d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberUnsuccessfulSteps"
5009b94acceSBarry Smith /*@
5019b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
5029b94acceSBarry Smith    attempted by the nonlinear solver.
5039b94acceSBarry Smith 
504c7afd0dbSLois Curfman McInnes    Not Collective
505c7afd0dbSLois Curfman McInnes 
5069b94acceSBarry Smith    Input Parameter:
5079b94acceSBarry Smith .  snes - SNES context
5089b94acceSBarry Smith 
5099b94acceSBarry Smith    Output Parameter:
5109b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
5119b94acceSBarry Smith 
512c96a6f78SLois Curfman McInnes    Notes:
513c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
514c96a6f78SLois Curfman McInnes 
51536851e7fSLois Curfman McInnes    Level: intermediate
51636851e7fSLois Curfman McInnes 
5179b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
5189b94acceSBarry Smith @*/
5199b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
5209b94acceSBarry Smith {
5213a40ed3dSBarry Smith   PetscFunctionBegin;
52277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
52374679c65SBarry Smith   PetscValidIntPointer(nfails);
5249b94acceSBarry Smith   *nfails = snes->nfailures;
5253a40ed3dSBarry Smith   PetscFunctionReturn(0);
5269b94acceSBarry Smith }
527a847f771SSatish Balay 
5285615d1e5SSatish Balay #undef __FUNC__
529d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberLinearIterations"
530c96a6f78SLois Curfman McInnes /*@
531c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
532c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
533c96a6f78SLois Curfman McInnes 
534c7afd0dbSLois Curfman McInnes    Not Collective
535c7afd0dbSLois Curfman McInnes 
536c96a6f78SLois Curfman McInnes    Input Parameter:
537c96a6f78SLois Curfman McInnes .  snes - SNES context
538c96a6f78SLois Curfman McInnes 
539c96a6f78SLois Curfman McInnes    Output Parameter:
540c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
541c96a6f78SLois Curfman McInnes 
542c96a6f78SLois Curfman McInnes    Notes:
543c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
544c96a6f78SLois Curfman McInnes 
54536851e7fSLois Curfman McInnes    Level: intermediate
54636851e7fSLois Curfman McInnes 
547c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
548c96a6f78SLois Curfman McInnes @*/
54986bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
550c96a6f78SLois Curfman McInnes {
5513a40ed3dSBarry Smith   PetscFunctionBegin;
552c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
553c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
554c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5553a40ed3dSBarry Smith   PetscFunctionReturn(0);
556c96a6f78SLois Curfman McInnes }
557c96a6f78SLois Curfman McInnes 
558c96a6f78SLois Curfman McInnes #undef __FUNC__
559d4bb536fSBarry Smith #define __FUNC__ "SNESGetSLES"
5609b94acceSBarry Smith /*@C
5619b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
5629b94acceSBarry Smith 
563c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
564c7afd0dbSLois Curfman McInnes 
5659b94acceSBarry Smith    Input Parameter:
5669b94acceSBarry Smith .  snes - the SNES context
5679b94acceSBarry Smith 
5689b94acceSBarry Smith    Output Parameter:
5699b94acceSBarry Smith .  sles - the SLES context
5709b94acceSBarry Smith 
5719b94acceSBarry Smith    Notes:
5729b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
5739b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
5749b94acceSBarry Smith    KSP and PC contexts as well.
5759b94acceSBarry Smith 
57636851e7fSLois Curfman McInnes    Level: beginner
57736851e7fSLois Curfman McInnes 
5789b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
5799b94acceSBarry Smith 
5809b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
5819b94acceSBarry Smith @*/
5829b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
5839b94acceSBarry Smith {
5843a40ed3dSBarry Smith   PetscFunctionBegin;
58577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5869b94acceSBarry Smith   *sles = snes->sles;
5873a40ed3dSBarry Smith   PetscFunctionReturn(0);
5889b94acceSBarry Smith }
58982bf6240SBarry Smith 
590e24b481bSBarry Smith #undef __FUNC__
591e24b481bSBarry Smith #define __FUNC__ "SNESPublish_Petsc"
592e24b481bSBarry Smith static int SNESPublish_Petsc(PetscObject object)
593e24b481bSBarry Smith {
594e24b481bSBarry Smith #if defined(HAVE_AMS)
595e24b481bSBarry Smith   SNES          v = (SNES) object;
596e24b481bSBarry Smith   int          ierr;
597e24b481bSBarry Smith 
598e24b481bSBarry Smith   PetscFunctionBegin;
599e24b481bSBarry Smith 
600e24b481bSBarry Smith   /* if it is already published then return */
601e24b481bSBarry Smith   if (v->amem >=0 ) PetscFunctionReturn(0);
602e24b481bSBarry Smith 
6033f1db9ecSBarry Smith   ierr = PetscObjectPublishBaseBegin(object);CHKERRQ(ierr);
604e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
605e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
606e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
607e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
608e24b481bSBarry Smith   ierr = PetscObjectPublishBaseEnd(object);CHKERRQ(ierr);
609e24b481bSBarry Smith #else
610e24b481bSBarry Smith   PetscFunctionBegin;
611e24b481bSBarry Smith #endif
612e24b481bSBarry Smith   PetscFunctionReturn(0);
613e24b481bSBarry Smith }
614e24b481bSBarry Smith 
6159b94acceSBarry Smith /* -----------------------------------------------------------*/
6165615d1e5SSatish Balay #undef __FUNC__
6175615d1e5SSatish Balay #define __FUNC__ "SNESCreate"
6189b94acceSBarry Smith /*@C
6199b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
6209b94acceSBarry Smith 
621c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
622c7afd0dbSLois Curfman McInnes 
623c7afd0dbSLois Curfman McInnes    Input Parameters:
624c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
625c7afd0dbSLois Curfman McInnes -  type - type of method, either
626c7afd0dbSLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
627c7afd0dbSLois Curfman McInnes    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
6289b94acceSBarry Smith 
6299b94acceSBarry Smith    Output Parameter:
6309b94acceSBarry Smith .  outsnes - the new SNES context
6319b94acceSBarry Smith 
632c7afd0dbSLois Curfman McInnes    Options Database Keys:
633c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
634c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
635c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
636c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
637c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
638c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
639c1f60f51SBarry Smith 
64036851e7fSLois Curfman McInnes    Level: beginner
64136851e7fSLois Curfman McInnes 
6429b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
6439b94acceSBarry Smith 
64463a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy()
6459b94acceSBarry Smith @*/
6464b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
6479b94acceSBarry Smith {
6489b94acceSBarry Smith   int                 ierr;
6499b94acceSBarry Smith   SNES                snes;
6509b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
65137fcc0dbSBarry Smith 
6523a40ed3dSBarry Smith   PetscFunctionBegin;
6539b94acceSBarry Smith   *outsnes = 0;
654d64ed03dSBarry Smith   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){
655d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type");
656d64ed03dSBarry Smith   }
6573f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
6589b94acceSBarry Smith   PLogObjectCreate(snes);
659e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6609b94acceSBarry Smith   snes->max_its           = 50;
6619750a799SBarry Smith   snes->max_funcs	  = 10000;
6629b94acceSBarry Smith   snes->norm		  = 0.0;
6635a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
664b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
665b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
6669b94acceSBarry Smith     snes->atol		  = 1.e-10;
667d64ed03dSBarry Smith   } else {
668b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
669b4874afaSBarry Smith     snes->ttol            = 0.0;
670b4874afaSBarry Smith     snes->atol		  = 1.e-50;
671b4874afaSBarry Smith   }
6729b94acceSBarry Smith   snes->xtol		  = 1.e-8;
673b18e04deSLois Curfman McInnes   snes->trunctol	  = 1.e-12; /* no longer used */
6749b94acceSBarry Smith   snes->nfuncs            = 0;
6759b94acceSBarry Smith   snes->nfailures         = 0;
6767a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
677639f9d9dSBarry Smith   snes->numbermonitors    = 0;
6789b94acceSBarry Smith   snes->data              = 0;
6799b94acceSBarry Smith   snes->view              = 0;
6809b94acceSBarry Smith   snes->computeumfunction = 0;
6819b94acceSBarry Smith   snes->umfunP            = 0;
6829b94acceSBarry Smith   snes->fc                = 0;
6839b94acceSBarry Smith   snes->deltatol          = 1.e-12;
6849b94acceSBarry Smith   snes->fmin              = -1.e30;
6859b94acceSBarry Smith   snes->method_class      = type;
6869b94acceSBarry Smith   snes->set_method_called = 0;
68782bf6240SBarry Smith   snes->setupcalled      = 0;
6889b94acceSBarry Smith   snes->ksp_ewconv        = 0;
6896f24a144SLois Curfman McInnes   snes->vwork             = 0;
6906f24a144SLois Curfman McInnes   snes->nwork             = 0;
691758f92a0SBarry Smith   snes->conv_hist_len     = 0;
692758f92a0SBarry Smith   snes->conv_hist_max     = 0;
693758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
694758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
695758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
6969b94acceSBarry Smith 
6979b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
6980452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx);
699eed86810SBarry Smith   PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
7009b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7019b94acceSBarry Smith   kctx->version     = 2;
7029b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7039b94acceSBarry Smith                              this was too large for some test cases */
7049b94acceSBarry Smith   kctx->rtol_last   = 0;
7059b94acceSBarry Smith   kctx->rtol_max    = .9;
7069b94acceSBarry Smith   kctx->gamma       = 1.0;
7079b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7089b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7099b94acceSBarry Smith   kctx->threshold   = .1;
7109b94acceSBarry Smith   kctx->lresid_last = 0;
7119b94acceSBarry Smith   kctx->norm_last   = 0;
7129b94acceSBarry Smith 
7139b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr);
7149b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
7155334005bSBarry Smith 
7169b94acceSBarry Smith   *outsnes = snes;
717e24b481bSBarry Smith   PetscPublishAll(snes);
7183a40ed3dSBarry Smith   PetscFunctionReturn(0);
7199b94acceSBarry Smith }
7209b94acceSBarry Smith 
7219b94acceSBarry Smith /* --------------------------------------------------------------- */
7225615d1e5SSatish Balay #undef __FUNC__
723d4bb536fSBarry Smith #define __FUNC__ "SNESSetFunction"
7249b94acceSBarry Smith /*@C
7259b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
7269b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
7279b94acceSBarry Smith    equations.
7289b94acceSBarry Smith 
729fee21e36SBarry Smith    Collective on SNES
730fee21e36SBarry Smith 
731c7afd0dbSLois Curfman McInnes    Input Parameters:
732c7afd0dbSLois Curfman McInnes +  snes - the SNES context
733c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
734c7afd0dbSLois Curfman McInnes .  r - vector to store function value
735c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
736c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7379b94acceSBarry Smith 
738c7afd0dbSLois Curfman McInnes    Calling sequence of func:
7398d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
740c7afd0dbSLois Curfman McInnes 
741313e4042SLois Curfman McInnes .  f - function vector
742c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
7439b94acceSBarry Smith 
7449b94acceSBarry Smith    Notes:
7459b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
7469b94acceSBarry Smith $      f'(x) x = -f(x),
747c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
7489b94acceSBarry Smith 
7499b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7509b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7519b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
7529b94acceSBarry Smith 
75336851e7fSLois Curfman McInnes    Level: beginner
75436851e7fSLois Curfman McInnes 
7559b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
7569b94acceSBarry Smith 
757a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
7589b94acceSBarry Smith @*/
7595334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx)
7609b94acceSBarry Smith {
7613a40ed3dSBarry Smith   PetscFunctionBegin;
76277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
763a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
764a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
765a8c6a408SBarry Smith   }
7669b94acceSBarry Smith   snes->computefunction     = func;
7679b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7689b94acceSBarry Smith   snes->funP                = ctx;
7693a40ed3dSBarry Smith   PetscFunctionReturn(0);
7709b94acceSBarry Smith }
7719b94acceSBarry Smith 
7725615d1e5SSatish Balay #undef __FUNC__
7735615d1e5SSatish Balay #define __FUNC__ "SNESComputeFunction"
7749b94acceSBarry Smith /*@
77536851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
7769b94acceSBarry Smith    SNESSetFunction().
7779b94acceSBarry Smith 
778c7afd0dbSLois Curfman McInnes    Collective on SNES
779c7afd0dbSLois Curfman McInnes 
7809b94acceSBarry Smith    Input Parameters:
781c7afd0dbSLois Curfman McInnes +  snes - the SNES context
782c7afd0dbSLois Curfman McInnes -  x - input vector
7839b94acceSBarry Smith 
7849b94acceSBarry Smith    Output Parameter:
7853638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
7869b94acceSBarry Smith 
7871bffabb2SLois Curfman McInnes    Notes:
7889b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7899b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7909b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
7919b94acceSBarry Smith 
79236851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
79336851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
79436851e7fSLois Curfman McInnes    themselves.
79536851e7fSLois Curfman McInnes 
79636851e7fSLois Curfman McInnes    Level: developer
79736851e7fSLois Curfman McInnes 
7989b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
7999b94acceSBarry Smith 
800a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
8019b94acceSBarry Smith @*/
8029b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y)
8039b94acceSBarry Smith {
8049b94acceSBarry Smith   int    ierr;
8059b94acceSBarry Smith 
8063a40ed3dSBarry Smith   PetscFunctionBegin;
807d4bb536fSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
808a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
809d4bb536fSBarry Smith   }
8109b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
811d64ed03dSBarry Smith   PetscStackPush("SNES user function");
8129b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
813d64ed03dSBarry Smith   PetscStackPop;
814ae3c334cSLois Curfman McInnes   snes->nfuncs++;
8159b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
8163a40ed3dSBarry Smith   PetscFunctionReturn(0);
8179b94acceSBarry Smith }
8189b94acceSBarry Smith 
8195615d1e5SSatish Balay #undef __FUNC__
820d4bb536fSBarry Smith #define __FUNC__ "SNESSetMinimizationFunction"
8219b94acceSBarry Smith /*@C
8229b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
8239b94acceSBarry Smith    unconstrained minimization.
8249b94acceSBarry Smith 
825fee21e36SBarry Smith    Collective on SNES
826fee21e36SBarry Smith 
827c7afd0dbSLois Curfman McInnes    Input Parameters:
828c7afd0dbSLois Curfman McInnes +  snes - the SNES context
829c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
830c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
831c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8329b94acceSBarry Smith 
833c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8348d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,double *f,void *ctx);
835c7afd0dbSLois Curfman McInnes 
836c7afd0dbSLois Curfman McInnes +  x - input vector
8379b94acceSBarry Smith .  f - function
838c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined function context
8399b94acceSBarry Smith 
84036851e7fSLois Curfman McInnes    Level: beginner
84136851e7fSLois Curfman McInnes 
8429b94acceSBarry Smith    Notes:
8439b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
8449b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
8459b94acceSBarry Smith    SNESSetFunction().
8469b94acceSBarry Smith 
8479b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
8489b94acceSBarry Smith 
849a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
850a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
8519b94acceSBarry Smith @*/
8529b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),
8539b94acceSBarry Smith                       void *ctx)
8549b94acceSBarry Smith {
8553a40ed3dSBarry Smith   PetscFunctionBegin;
85677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
857a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
858a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
859a8c6a408SBarry Smith   }
8609b94acceSBarry Smith   snes->computeumfunction   = func;
8619b94acceSBarry Smith   snes->umfunP              = ctx;
8623a40ed3dSBarry Smith   PetscFunctionReturn(0);
8639b94acceSBarry Smith }
8649b94acceSBarry Smith 
8655615d1e5SSatish Balay #undef __FUNC__
8665615d1e5SSatish Balay #define __FUNC__ "SNESComputeMinimizationFunction"
8679b94acceSBarry Smith /*@
8689b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
8699b94acceSBarry Smith    set with SNESSetMinimizationFunction().
8709b94acceSBarry Smith 
871c7afd0dbSLois Curfman McInnes    Collective on SNES
872c7afd0dbSLois Curfman McInnes 
8739b94acceSBarry Smith    Input Parameters:
874c7afd0dbSLois Curfman McInnes +  snes - the SNES context
875c7afd0dbSLois Curfman McInnes -  x - input vector
8769b94acceSBarry Smith 
8779b94acceSBarry Smith    Output Parameter:
8789b94acceSBarry Smith .  y - function value
8799b94acceSBarry Smith 
8809b94acceSBarry Smith    Notes:
8819b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
8829b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
8839b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
884a86d99e1SLois Curfman McInnes 
88536851e7fSLois Curfman McInnes    SNESComputeMinimizationFunction() is typically used within minimization
88636851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
88736851e7fSLois Curfman McInnes    themselves.
88836851e7fSLois Curfman McInnes 
88936851e7fSLois Curfman McInnes    Level: developer
89036851e7fSLois Curfman McInnes 
891a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
892a86d99e1SLois Curfman McInnes 
893a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
894a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
8959b94acceSBarry Smith @*/
8969b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y)
8979b94acceSBarry Smith {
8989b94acceSBarry Smith   int    ierr;
8993a40ed3dSBarry Smith 
9003a40ed3dSBarry Smith   PetscFunctionBegin;
901a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
902a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
903a8c6a408SBarry Smith   }
9049b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
905d64ed03dSBarry Smith   PetscStackPush("SNES user minimzation function");
9069b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr);
907d64ed03dSBarry Smith   PetscStackPop;
908ae3c334cSLois Curfman McInnes   snes->nfuncs++;
9099b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
9103a40ed3dSBarry Smith   PetscFunctionReturn(0);
9119b94acceSBarry Smith }
9129b94acceSBarry Smith 
9135615d1e5SSatish Balay #undef __FUNC__
914d4bb536fSBarry Smith #define __FUNC__ "SNESSetGradient"
9159b94acceSBarry Smith /*@C
9169b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
9179b94acceSBarry Smith    vector for use by the SNES routines.
9189b94acceSBarry Smith 
919c7afd0dbSLois Curfman McInnes    Collective on SNES
920c7afd0dbSLois Curfman McInnes 
9219b94acceSBarry Smith    Input Parameters:
922c7afd0dbSLois Curfman McInnes +  snes - the SNES context
9239b94acceSBarry Smith .  func - function evaluation routine
924044dda88SLois Curfman McInnes .  ctx - optional user-defined context for private data for the
925044dda88SLois Curfman McInnes          gradient evaluation routine (may be PETSC_NULL)
926c7afd0dbSLois Curfman McInnes -  r - vector to store gradient value
927fee21e36SBarry Smith 
9289b94acceSBarry Smith    Calling sequence of func:
9298d76a1e5SLois Curfman McInnes $     func (SNES, Vec x, Vec g, void *ctx);
9309b94acceSBarry Smith 
931c7afd0dbSLois Curfman McInnes +  x - input vector
9329b94acceSBarry Smith .  g - gradient vector
933c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined gradient context
9349b94acceSBarry Smith 
9359b94acceSBarry Smith    Notes:
9369b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
9379b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
9389b94acceSBarry Smith    SNESSetFunction().
9399b94acceSBarry Smith 
94036851e7fSLois Curfman McInnes    Level: beginner
94136851e7fSLois Curfman McInnes 
9429b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9439b94acceSBarry Smith 
944a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
945a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
9469b94acceSBarry Smith @*/
94774679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
9489b94acceSBarry Smith {
9493a40ed3dSBarry Smith   PetscFunctionBegin;
95077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
951a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
952a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
953a8c6a408SBarry Smith   }
9549b94acceSBarry Smith   snes->computefunction     = func;
9559b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
9569b94acceSBarry Smith   snes->funP                = ctx;
9573a40ed3dSBarry Smith   PetscFunctionReturn(0);
9589b94acceSBarry Smith }
9599b94acceSBarry Smith 
9605615d1e5SSatish Balay #undef __FUNC__
9615615d1e5SSatish Balay #define __FUNC__ "SNESComputeGradient"
9629b94acceSBarry Smith /*@
963a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
964a86d99e1SLois Curfman McInnes    SNESSetGradient().
9659b94acceSBarry Smith 
966c7afd0dbSLois Curfman McInnes    Collective on SNES
967c7afd0dbSLois Curfman McInnes 
9689b94acceSBarry Smith    Input Parameters:
969c7afd0dbSLois Curfman McInnes +  snes - the SNES context
970c7afd0dbSLois Curfman McInnes -  x - input vector
9719b94acceSBarry Smith 
9729b94acceSBarry Smith    Output Parameter:
9739b94acceSBarry Smith .  y - gradient vector
9749b94acceSBarry Smith 
9759b94acceSBarry Smith    Notes:
9769b94acceSBarry Smith    SNESComputeGradient() is valid only for
9779b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
9789b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
979a86d99e1SLois Curfman McInnes 
98036851e7fSLois Curfman McInnes    SNESComputeGradient() is typically used within minimization
98136851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
98236851e7fSLois Curfman McInnes    themselves.
98336851e7fSLois Curfman McInnes 
98436851e7fSLois Curfman McInnes    Level: developer
98536851e7fSLois Curfman McInnes 
986a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
987a86d99e1SLois Curfman McInnes 
988a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
989a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
9909b94acceSBarry Smith @*/
9919b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y)
9929b94acceSBarry Smith {
9939b94acceSBarry Smith   int    ierr;
9943a40ed3dSBarry Smith 
9953a40ed3dSBarry Smith   PetscFunctionBegin;
9963a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
997a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
9983a40ed3dSBarry Smith   }
9999b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
1000d64ed03dSBarry Smith   PetscStackPush("SNES user gradient function");
10019b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
1002d64ed03dSBarry Smith   PetscStackPop;
10039b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
10043a40ed3dSBarry Smith   PetscFunctionReturn(0);
10059b94acceSBarry Smith }
10069b94acceSBarry Smith 
10075615d1e5SSatish Balay #undef __FUNC__
10085615d1e5SSatish Balay #define __FUNC__ "SNESComputeJacobian"
100962fef451SLois Curfman McInnes /*@
101062fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
101162fef451SLois Curfman McInnes    set with SNESSetJacobian().
101262fef451SLois Curfman McInnes 
1013c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1014c7afd0dbSLois Curfman McInnes 
101562fef451SLois Curfman McInnes    Input Parameters:
1016c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1017c7afd0dbSLois Curfman McInnes -  x - input vector
101862fef451SLois Curfman McInnes 
101962fef451SLois Curfman McInnes    Output Parameters:
1020c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
102162fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1022c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1023fee21e36SBarry Smith 
102462fef451SLois Curfman McInnes    Notes:
102562fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
102662fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
102762fef451SLois Curfman McInnes 
1028dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1029dc5a77f8SLois Curfman McInnes    flag parameter.
103062fef451SLois Curfman McInnes 
103162fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
103262fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
1033005c665bSBarry Smith    methods is SNESComputeHessian().
103462fef451SLois Curfman McInnes 
103536851e7fSLois Curfman McInnes    SNESComputeJacobian() is typically used within nonlinear solver
103636851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
103736851e7fSLois Curfman McInnes    themselves.
103836851e7fSLois Curfman McInnes 
103936851e7fSLois Curfman McInnes    Level: developer
104036851e7fSLois Curfman McInnes 
104162fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
104262fef451SLois Curfman McInnes 
104362fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
104462fef451SLois Curfman McInnes @*/
10459b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10469b94acceSBarry Smith {
10479b94acceSBarry Smith   int    ierr;
10483a40ed3dSBarry Smith 
10493a40ed3dSBarry Smith   PetscFunctionBegin;
10503a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1051a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
10523a40ed3dSBarry Smith   }
10533a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
10549b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
1055c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1056d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
10579b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr);
1058d64ed03dSBarry Smith   PetscStackPop;
10599b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
10606d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
106177c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
106277c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
10633a40ed3dSBarry Smith   PetscFunctionReturn(0);
10649b94acceSBarry Smith }
10659b94acceSBarry Smith 
10665615d1e5SSatish Balay #undef __FUNC__
10675615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian"
106862fef451SLois Curfman McInnes /*@
106962fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
107062fef451SLois Curfman McInnes    set with SNESSetHessian().
107162fef451SLois Curfman McInnes 
1072c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1073c7afd0dbSLois Curfman McInnes 
107462fef451SLois Curfman McInnes    Input Parameters:
1075c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1076c7afd0dbSLois Curfman McInnes -  x - input vector
107762fef451SLois Curfman McInnes 
107862fef451SLois Curfman McInnes    Output Parameters:
1079c7afd0dbSLois Curfman McInnes +  A - Hessian matrix
108062fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1081c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1082fee21e36SBarry Smith 
108362fef451SLois Curfman McInnes    Notes:
108462fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
108562fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
108662fef451SLois Curfman McInnes 
1087dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1088dc5a77f8SLois Curfman McInnes    flag parameter.
108962fef451SLois Curfman McInnes 
109062fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
109162fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
109262fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
109362fef451SLois Curfman McInnes 
109436851e7fSLois Curfman McInnes    SNESComputeHessian() is typically used within minimization
109536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
109636851e7fSLois Curfman McInnes    themselves.
109736851e7fSLois Curfman McInnes 
109836851e7fSLois Curfman McInnes    Level: developer
109936851e7fSLois Curfman McInnes 
110062fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
110162fef451SLois Curfman McInnes 
1102a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
1103a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
110462fef451SLois Curfman McInnes @*/
110562fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
11069b94acceSBarry Smith {
11079b94acceSBarry Smith   int    ierr;
11083a40ed3dSBarry Smith 
11093a40ed3dSBarry Smith   PetscFunctionBegin;
11103a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1111a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
11123a40ed3dSBarry Smith   }
11133a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
111462fef451SLois Curfman McInnes   PLogEventBegin(SNES_HessianEval,snes,x,*A,*B);
11150f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
1116d64ed03dSBarry Smith   PetscStackPush("SNES user Hessian function");
111762fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr);
1118d64ed03dSBarry Smith   PetscStackPop;
111962fef451SLois Curfman McInnes   PLogEventEnd(SNES_HessianEval,snes,x,*A,*B);
11200f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
112177c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
112277c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11233a40ed3dSBarry Smith   PetscFunctionReturn(0);
11249b94acceSBarry Smith }
11259b94acceSBarry Smith 
11265615d1e5SSatish Balay #undef __FUNC__
1127d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian"
11289b94acceSBarry Smith /*@C
11299b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1130044dda88SLois Curfman McInnes    location to store the matrix.
11319b94acceSBarry Smith 
1132c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1133c7afd0dbSLois Curfman McInnes 
11349b94acceSBarry Smith    Input Parameters:
1135c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11369b94acceSBarry Smith .  A - Jacobian matrix
11379b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11389b94acceSBarry Smith .  func - Jacobian evaluation routine
1139c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11402cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11419b94acceSBarry Smith 
11429b94acceSBarry Smith    Calling sequence of func:
11438d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11449b94acceSBarry Smith 
1145c7afd0dbSLois Curfman McInnes +  x - input vector
11469b94acceSBarry Smith .  A - Jacobian matrix
11479b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1148ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1149ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1150c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11519b94acceSBarry Smith 
11529b94acceSBarry Smith    Notes:
1153dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
11542cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1155ac21db08SLois Curfman McInnes 
1156ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11579b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11589b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11599b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11609b94acceSBarry Smith    throughout the global iterations.
11619b94acceSBarry Smith 
116236851e7fSLois Curfman McInnes    Level: beginner
116336851e7fSLois Curfman McInnes 
11649b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11659b94acceSBarry Smith 
1166ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
11679b94acceSBarry Smith @*/
11689b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
11699b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
11709b94acceSBarry Smith {
11713a40ed3dSBarry Smith   PetscFunctionBegin;
117277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1173a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1174a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
1175a8c6a408SBarry Smith   }
11769b94acceSBarry Smith   snes->computejacobian = func;
11779b94acceSBarry Smith   snes->jacP            = ctx;
11789b94acceSBarry Smith   snes->jacobian        = A;
11799b94acceSBarry Smith   snes->jacobian_pre    = B;
11803a40ed3dSBarry Smith   PetscFunctionReturn(0);
11819b94acceSBarry Smith }
118262fef451SLois Curfman McInnes 
11835615d1e5SSatish Balay #undef __FUNC__
1184d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian"
1185b4fd4287SBarry Smith /*@
1186b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1187b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1188b4fd4287SBarry Smith 
1189c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1190c7afd0dbSLois Curfman McInnes 
1191b4fd4287SBarry Smith    Input Parameter:
1192b4fd4287SBarry Smith .  snes - the nonlinear solver context
1193b4fd4287SBarry Smith 
1194b4fd4287SBarry Smith    Output Parameters:
1195c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1196b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
1197c7afd0dbSLois Curfman McInnes -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1198fee21e36SBarry Smith 
119936851e7fSLois Curfman McInnes    Level: advanced
120036851e7fSLois Curfman McInnes 
1201b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1202b4fd4287SBarry Smith @*/
1203b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx)
1204b4fd4287SBarry Smith {
12053a40ed3dSBarry Smith   PetscFunctionBegin;
12063a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1207a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
12083a40ed3dSBarry Smith   }
1209b4fd4287SBarry Smith   if (A)   *A = snes->jacobian;
1210b4fd4287SBarry Smith   if (B)   *B = snes->jacobian_pre;
1211b4fd4287SBarry Smith   if (ctx) *ctx = snes->jacP;
12123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1213b4fd4287SBarry Smith }
1214b4fd4287SBarry Smith 
12155615d1e5SSatish Balay #undef __FUNC__
1216d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian"
12179b94acceSBarry Smith /*@C
12189b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
1219044dda88SLois Curfman McInnes    location to store the matrix.
12209b94acceSBarry Smith 
1221c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1222c7afd0dbSLois Curfman McInnes 
12239b94acceSBarry Smith    Input Parameters:
1224c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12259b94acceSBarry Smith .  A - Hessian matrix
12269b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
12279b94acceSBarry Smith .  func - Jacobian evaluation routine
1228c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1229313e4042SLois Curfman McInnes          Hessian evaluation routine (may be PETSC_NULL)
12309b94acceSBarry Smith 
12319b94acceSBarry Smith    Calling sequence of func:
12328d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12339b94acceSBarry Smith 
1234c7afd0dbSLois Curfman McInnes +  x - input vector
12359b94acceSBarry Smith .  A - Hessian matrix
12369b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1237ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1238ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1239c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Hessian context
12409b94acceSBarry Smith 
12419b94acceSBarry Smith    Notes:
1242dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
12432cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1244ac21db08SLois Curfman McInnes 
12459b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
12469b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
12479b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12489b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12499b94acceSBarry Smith    throughout the global iterations.
12509b94acceSBarry Smith 
125136851e7fSLois Curfman McInnes    Level: beginner
125236851e7fSLois Curfman McInnes 
12539b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
12549b94acceSBarry Smith 
1255ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
12569b94acceSBarry Smith @*/
12579b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
12589b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
12599b94acceSBarry Smith {
12603a40ed3dSBarry Smith   PetscFunctionBegin;
126177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1262d4bb536fSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1263a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1264d4bb536fSBarry Smith   }
12659b94acceSBarry Smith   snes->computejacobian = func;
12669b94acceSBarry Smith   snes->jacP            = ctx;
12679b94acceSBarry Smith   snes->jacobian        = A;
12689b94acceSBarry Smith   snes->jacobian_pre    = B;
12693a40ed3dSBarry Smith   PetscFunctionReturn(0);
12709b94acceSBarry Smith }
12719b94acceSBarry Smith 
12725615d1e5SSatish Balay #undef __FUNC__
1273d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian"
127462fef451SLois Curfman McInnes /*@
127562fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
127662fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
127762fef451SLois Curfman McInnes 
1278c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object is parallel if SNES object is parallel
1279c7afd0dbSLois Curfman McInnes 
128062fef451SLois Curfman McInnes    Input Parameter:
128162fef451SLois Curfman McInnes .  snes - the nonlinear solver context
128262fef451SLois Curfman McInnes 
128362fef451SLois Curfman McInnes    Output Parameters:
1284c7afd0dbSLois Curfman McInnes +  A - location to stash Hessian matrix (or PETSC_NULL)
128562fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
1286c7afd0dbSLois Curfman McInnes -  ctx - location to stash Hessian ctx (or PETSC_NULL)
1287fee21e36SBarry Smith 
128836851e7fSLois Curfman McInnes    Level: advanced
128936851e7fSLois Curfman McInnes 
129062fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
1291c7afd0dbSLois Curfman McInnes 
1292c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian
129362fef451SLois Curfman McInnes @*/
129462fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx)
129562fef451SLois Curfman McInnes {
12963a40ed3dSBarry Smith   PetscFunctionBegin;
12973a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){
1298a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
12993a40ed3dSBarry Smith   }
130062fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
130162fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
130262fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
13033a40ed3dSBarry Smith   PetscFunctionReturn(0);
130462fef451SLois Curfman McInnes }
130562fef451SLois Curfman McInnes 
13069b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13079b94acceSBarry Smith 
13085615d1e5SSatish Balay #undef __FUNC__
13095615d1e5SSatish Balay #define __FUNC__ "SNESSetUp"
13109b94acceSBarry Smith /*@
13119b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1312272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13139b94acceSBarry Smith 
1314fee21e36SBarry Smith    Collective on SNES
1315fee21e36SBarry Smith 
1316c7afd0dbSLois Curfman McInnes    Input Parameters:
1317c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1318c7afd0dbSLois Curfman McInnes -  x - the solution vector
1319c7afd0dbSLois Curfman McInnes 
1320272ac6f2SLois Curfman McInnes    Notes:
1321272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1322272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1323272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1324272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1325272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1326272ac6f2SLois Curfman McInnes 
132736851e7fSLois Curfman McInnes    Level: advanced
132836851e7fSLois Curfman McInnes 
13299b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13309b94acceSBarry Smith 
13319b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
13329b94acceSBarry Smith @*/
13338ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
13349b94acceSBarry Smith {
1335272ac6f2SLois Curfman McInnes   int ierr, flg;
13363a40ed3dSBarry Smith 
13373a40ed3dSBarry Smith   PetscFunctionBegin;
133877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
133977c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
13408ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
13419b94acceSBarry Smith 
1342c1f60f51SBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);  CHKERRQ(ierr);
1343c1f60f51SBarry Smith   /*
1344c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1345dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1346c1f60f51SBarry Smith   */
1347c1f60f51SBarry Smith   if (flg) {
1348c1f60f51SBarry Smith     Mat J;
13495a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1350c1f60f51SBarry Smith     PLogObjectParent(snes,J);
1351c1f60f51SBarry Smith     snes->mfshell = J;
1352c1f60f51SBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1353c1f60f51SBarry Smith       snes->jacobian = J;
1354c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n");
1355d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
1356c1f60f51SBarry Smith       snes->jacobian = J;
1357c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n");
1358d4bb536fSBarry Smith     } else {
1359a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option");
1360d4bb536fSBarry Smith     }
13615a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1362c1f60f51SBarry Smith   }
1363272ac6f2SLois Curfman McInnes   ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);  CHKERRQ(ierr);
1364c1f60f51SBarry Smith   /*
1365dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1366c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1367c1f60f51SBarry Smith    */
136831615d04SLois Curfman McInnes   if (flg) {
1369272ac6f2SLois Curfman McInnes     Mat J;
13705a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1371272ac6f2SLois Curfman McInnes     PLogObjectParent(snes,J);
1372272ac6f2SLois Curfman McInnes     snes->mfshell = J;
137383e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
137483e56afdSLois Curfman McInnes       ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
137594a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n");
1376d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
137783e56afdSLois Curfman McInnes       ierr = SNESSetHessian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
137894a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n");
1379d4bb536fSBarry Smith     } else {
1380a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option");
1381d4bb536fSBarry Smith     }
13825a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1383272ac6f2SLois Curfman McInnes   }
13849b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
1385a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
1386a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
13875a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1388a8c6a408SBarry Smith     if (snes->vec_func == snes->vec_sol) {
1389a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector");
1390a8c6a408SBarry Smith     }
1391a703fe33SLois Curfman McInnes 
1392a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
139382bf6240SBarry Smith     if (snes->ksp_ewconv && PetscStrcmp(snes->type_name,SNES_EQ_TR)) {
1394a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1395a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
1396a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr);
13975a655dc6SBarry Smith       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes); CHKERRQ(ierr);
1398a703fe33SLois Curfman McInnes     }
13999b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
1400a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1401a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1402a8c6a408SBarry Smith     if (!snes->computeumfunction) {
1403a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first");
1404a8c6a408SBarry Smith     }
14055a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()");
1406d4bb536fSBarry Smith   } else {
1407a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class");
1408d4bb536fSBarry Smith   }
1409a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);}
141082bf6240SBarry Smith   snes->setupcalled = 1;
14113a40ed3dSBarry Smith   PetscFunctionReturn(0);
14129b94acceSBarry Smith }
14139b94acceSBarry Smith 
14145615d1e5SSatish Balay #undef __FUNC__
1415d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy"
14169b94acceSBarry Smith /*@C
14179b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
14189b94acceSBarry Smith    with SNESCreate().
14199b94acceSBarry Smith 
1420c7afd0dbSLois Curfman McInnes    Collective on SNES
1421c7afd0dbSLois Curfman McInnes 
14229b94acceSBarry Smith    Input Parameter:
14239b94acceSBarry Smith .  snes - the SNES context
14249b94acceSBarry Smith 
142536851e7fSLois Curfman McInnes    Level: beginner
142636851e7fSLois Curfman McInnes 
14279b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14289b94acceSBarry Smith 
142963a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14309b94acceSBarry Smith @*/
14319b94acceSBarry Smith int SNESDestroy(SNES snes)
14329b94acceSBarry Smith {
1433*b8a78c4aSBarry Smith   int i,ierr;
14343a40ed3dSBarry Smith 
14353a40ed3dSBarry Smith   PetscFunctionBegin;
143677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
14373a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1438d4bb536fSBarry Smith 
1439e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes); CHKERRQ(ierr);}
14400452661fSBarry Smith   if (snes->kspconvctx) PetscFree(snes->kspconvctx);
1441522c5e43SBarry Smith   if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);}
14429b94acceSBarry Smith   ierr = SLESDestroy(snes->sles); CHKERRQ(ierr);
1443522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1444*b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++ ) {
1445*b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1446*b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1447*b8a78c4aSBarry Smith     }
1448*b8a78c4aSBarry Smith   }
14499b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
14500452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
14513a40ed3dSBarry Smith   PetscFunctionReturn(0);
14529b94acceSBarry Smith }
14539b94acceSBarry Smith 
14549b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14559b94acceSBarry Smith 
14565615d1e5SSatish Balay #undef __FUNC__
14575615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances"
14589b94acceSBarry Smith /*@
1459d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
14609b94acceSBarry Smith 
1461c7afd0dbSLois Curfman McInnes    Collective on SNES
1462c7afd0dbSLois Curfman McInnes 
14639b94acceSBarry Smith    Input Parameters:
1464c7afd0dbSLois Curfman McInnes +  snes - the SNES context
146533174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
146633174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
146733174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
146833174efeSLois Curfman McInnes            of the change in the solution between steps
146933174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1470c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1471fee21e36SBarry Smith 
147233174efeSLois Curfman McInnes    Options Database Keys:
1473c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1474c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1475c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1476c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1477c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
14789b94acceSBarry Smith 
1479d7a720efSLois Curfman McInnes    Notes:
14809b94acceSBarry Smith    The default maximum number of iterations is 50.
14819b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
14829b94acceSBarry Smith 
148336851e7fSLois Curfman McInnes    Level: intermediate
148436851e7fSLois Curfman McInnes 
148533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
14869b94acceSBarry Smith 
1487d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
14889b94acceSBarry Smith @*/
1489d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf)
14909b94acceSBarry Smith {
14913a40ed3dSBarry Smith   PetscFunctionBegin;
149277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1493d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1494d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1495d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1496d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1497d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
14983a40ed3dSBarry Smith   PetscFunctionReturn(0);
14999b94acceSBarry Smith }
15009b94acceSBarry Smith 
15015615d1e5SSatish Balay #undef __FUNC__
15025615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances"
15039b94acceSBarry Smith /*@
150433174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
150533174efeSLois Curfman McInnes 
1506c7afd0dbSLois Curfman McInnes    Not Collective
1507c7afd0dbSLois Curfman McInnes 
150833174efeSLois Curfman McInnes    Input Parameters:
1509c7afd0dbSLois Curfman McInnes +  snes - the SNES context
151033174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
151133174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
151233174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
151333174efeSLois Curfman McInnes            of the change in the solution between steps
151433174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1515c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1516fee21e36SBarry Smith 
151733174efeSLois Curfman McInnes    Notes:
151833174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
151933174efeSLois Curfman McInnes 
152036851e7fSLois Curfman McInnes    Level: intermediate
152136851e7fSLois Curfman McInnes 
152233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
152333174efeSLois Curfman McInnes 
152433174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
152533174efeSLois Curfman McInnes @*/
152633174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf)
152733174efeSLois Curfman McInnes {
15283a40ed3dSBarry Smith   PetscFunctionBegin;
152933174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
153033174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
153133174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
153233174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
153333174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
153433174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
15353a40ed3dSBarry Smith   PetscFunctionReturn(0);
153633174efeSLois Curfman McInnes }
153733174efeSLois Curfman McInnes 
15385615d1e5SSatish Balay #undef __FUNC__
15395615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance"
154033174efeSLois Curfman McInnes /*@
15419b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
15429b94acceSBarry Smith 
1543fee21e36SBarry Smith    Collective on SNES
1544fee21e36SBarry Smith 
1545c7afd0dbSLois Curfman McInnes    Input Parameters:
1546c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1547c7afd0dbSLois Curfman McInnes -  tol - tolerance
1548c7afd0dbSLois Curfman McInnes 
15499b94acceSBarry Smith    Options Database Key:
1550c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
15519b94acceSBarry Smith 
155236851e7fSLois Curfman McInnes    Level: intermediate
155336851e7fSLois Curfman McInnes 
15549b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
15559b94acceSBarry Smith 
1556d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
15579b94acceSBarry Smith @*/
15589b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol)
15599b94acceSBarry Smith {
15603a40ed3dSBarry Smith   PetscFunctionBegin;
156177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15629b94acceSBarry Smith   snes->deltatol = tol;
15633a40ed3dSBarry Smith   PetscFunctionReturn(0);
15649b94acceSBarry Smith }
15659b94acceSBarry Smith 
15665615d1e5SSatish Balay #undef __FUNC__
15675615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance"
15689b94acceSBarry Smith /*@
156977c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
15709b94acceSBarry Smith    for unconstrained minimization solvers.
15719b94acceSBarry Smith 
1572fee21e36SBarry Smith    Collective on SNES
1573fee21e36SBarry Smith 
1574c7afd0dbSLois Curfman McInnes    Input Parameters:
1575c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1576c7afd0dbSLois Curfman McInnes -  ftol - minimum function tolerance
1577c7afd0dbSLois Curfman McInnes 
15789b94acceSBarry Smith    Options Database Key:
1579c7afd0dbSLois Curfman McInnes .  -snes_fmin <ftol> - Sets ftol
15809b94acceSBarry Smith 
15819b94acceSBarry Smith    Note:
158277c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
15839b94acceSBarry Smith    methods only.
15849b94acceSBarry Smith 
158536851e7fSLois Curfman McInnes    Level: intermediate
158636851e7fSLois Curfman McInnes 
15879b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
15889b94acceSBarry Smith 
1589d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
15909b94acceSBarry Smith @*/
159177c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol)
15929b94acceSBarry Smith {
15933a40ed3dSBarry Smith   PetscFunctionBegin;
159477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15959b94acceSBarry Smith   snes->fmin = ftol;
15963a40ed3dSBarry Smith   PetscFunctionReturn(0);
15979b94acceSBarry Smith }
15989b94acceSBarry Smith 
1599ce1608b8SBarry Smith #undef __FUNC__
1600ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor"
1601ce1608b8SBarry Smith int SNESLGMonitor(SNES snes,int it,double norm,void *ctx)
1602ce1608b8SBarry Smith {
1603ce1608b8SBarry Smith   int ierr;
1604ce1608b8SBarry Smith 
1605ce1608b8SBarry Smith   PetscFunctionBegin;
1606ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1607ce1608b8SBarry Smith   PetscFunctionReturn(0);
1608ce1608b8SBarry Smith }
1609ce1608b8SBarry Smith 
16109b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
16119b94acceSBarry Smith 
16125615d1e5SSatish Balay #undef __FUNC__
1613d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor"
16149b94acceSBarry Smith /*@C
16155cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
16169b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
16179b94acceSBarry Smith    progress.
16189b94acceSBarry Smith 
1619fee21e36SBarry Smith    Collective on SNES
1620fee21e36SBarry Smith 
1621c7afd0dbSLois Curfman McInnes    Input Parameters:
1622c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1623c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1624*b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1625c7afd0dbSLois Curfman McInnes           monitor routine (may be PETSC_NULL)
1626*b8a78c4aSBarry Smith -  monitordestroy - options routine that frees monitor context
16279b94acceSBarry Smith 
1628c7afd0dbSLois Curfman McInnes    Calling sequence of func:
162940a0c1c6SLois Curfman McInnes $     int func(SNES snes,int its, double norm,void *mctx)
1630c7afd0dbSLois Curfman McInnes 
1631c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1632c7afd0dbSLois Curfman McInnes .    its - iteration number
1633c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
1634c7afd0dbSLois Curfman McInnes             for SNES_NONLINEAR_EQUATIONS methods
163540a0c1c6SLois Curfman McInnes .    norm - 2-norm gradient value (may be estimated)
1636c7afd0dbSLois Curfman McInnes             for SNES_UNCONSTRAINED_MINIMIZATION methods
163740a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
16389b94acceSBarry Smith 
16399665c990SLois Curfman McInnes    Options Database Keys:
1640c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1641c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1642c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1643c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1644c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1645c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1646c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1647c7afd0dbSLois Curfman McInnes                             the options database.
16489665c990SLois Curfman McInnes 
1649639f9d9dSBarry Smith    Notes:
16506bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
16516bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
16526bc08f3fSLois Curfman McInnes    order in which they were set.
1653639f9d9dSBarry Smith 
165436851e7fSLois Curfman McInnes    Level: intermediate
165536851e7fSLois Curfman McInnes 
16569b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
16579b94acceSBarry Smith 
16585cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
16599b94acceSBarry Smith @*/
1660*b8a78c4aSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx,int (*monitordestroy)(void *))
16619b94acceSBarry Smith {
16623a40ed3dSBarry Smith   PetscFunctionBegin;
1663639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
1664a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set");
1665639f9d9dSBarry Smith   }
1666639f9d9dSBarry Smith 
1667639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1668*b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1669639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
16703a40ed3dSBarry Smith   PetscFunctionReturn(0);
16719b94acceSBarry Smith }
16729b94acceSBarry Smith 
16735615d1e5SSatish Balay #undef __FUNC__
16745cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor"
16755cd90555SBarry Smith /*@C
16765cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
16775cd90555SBarry Smith 
1678c7afd0dbSLois Curfman McInnes    Collective on SNES
1679c7afd0dbSLois Curfman McInnes 
16805cd90555SBarry Smith    Input Parameters:
16815cd90555SBarry Smith .  snes - the SNES context
16825cd90555SBarry Smith 
16835cd90555SBarry Smith    Options Database:
1684c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1685c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1686c7afd0dbSLois Curfman McInnes     set via the options database
16875cd90555SBarry Smith 
16885cd90555SBarry Smith    Notes:
16895cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
16905cd90555SBarry Smith 
169136851e7fSLois Curfman McInnes    Level: intermediate
169236851e7fSLois Curfman McInnes 
16935cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
16945cd90555SBarry Smith 
16955cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
16965cd90555SBarry Smith @*/
16975cd90555SBarry Smith int SNESClearMonitor( SNES snes )
16985cd90555SBarry Smith {
16995cd90555SBarry Smith   PetscFunctionBegin;
17005cd90555SBarry Smith   snes->numbermonitors = 0;
17015cd90555SBarry Smith   PetscFunctionReturn(0);
17025cd90555SBarry Smith }
17035cd90555SBarry Smith 
17045cd90555SBarry Smith #undef __FUNC__
1705d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest"
17069b94acceSBarry Smith /*@C
17079b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
17089b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
17099b94acceSBarry Smith 
1710fee21e36SBarry Smith    Collective on SNES
1711fee21e36SBarry Smith 
1712c7afd0dbSLois Curfman McInnes    Input Parameters:
1713c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1714c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1715c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1716c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
17179b94acceSBarry Smith 
1718c7afd0dbSLois Curfman McInnes    Calling sequence of func:
17198d76a1e5SLois Curfman McInnes $     int func (SNES snes,double xnorm,double gnorm,double f,void *cctx)
1720c7afd0dbSLois Curfman McInnes 
1721c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1722c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1723c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1724c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods)
1725c7afd0dbSLois Curfman McInnes .    f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods)
1726c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods)
1727c7afd0dbSLois Curfman McInnes -    f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods)
17289b94acceSBarry Smith 
172936851e7fSLois Curfman McInnes    Level: advanced
173036851e7fSLois Curfman McInnes 
17319b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
17329b94acceSBarry Smith 
173340191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
173440191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
17359b94acceSBarry Smith @*/
173674679c65SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,void*),void *cctx)
17379b94acceSBarry Smith {
17383a40ed3dSBarry Smith   PetscFunctionBegin;
17399b94acceSBarry Smith   (snes)->converged = func;
17409b94acceSBarry Smith   (snes)->cnvP      = cctx;
17413a40ed3dSBarry Smith   PetscFunctionReturn(0);
17429b94acceSBarry Smith }
17439b94acceSBarry Smith 
17445615d1e5SSatish Balay #undef __FUNC__
1745d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory"
1746c9005455SLois Curfman McInnes /*@
1747c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1748c9005455SLois Curfman McInnes 
1749fee21e36SBarry Smith    Collective on SNES
1750fee21e36SBarry Smith 
1751c7afd0dbSLois Curfman McInnes    Input Parameters:
1752c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1753c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1754758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1755758f92a0SBarry Smith          negative if not converged) for each solve.
1756758f92a0SBarry Smith .  na  - size of a and its
1757758f92a0SBarry Smith -  reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero,
1758758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1759c7afd0dbSLois Curfman McInnes 
1760c9005455SLois Curfman McInnes    Notes:
1761c9005455SLois Curfman McInnes    If set, this array will contain the function norms (for
1762c9005455SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods) or gradient norms
1763c9005455SLois Curfman McInnes    (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed
1764c9005455SLois Curfman McInnes    at each step.
1765c9005455SLois Curfman McInnes 
1766c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1767c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1768c9005455SLois Curfman McInnes    during the section of code that is being timed.
1769c9005455SLois Curfman McInnes 
177036851e7fSLois Curfman McInnes    Level: intermediate
177136851e7fSLois Curfman McInnes 
1772c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1773758f92a0SBarry Smith 
177408405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1775758f92a0SBarry Smith 
1776c9005455SLois Curfman McInnes @*/
1777758f92a0SBarry Smith int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset)
1778c9005455SLois Curfman McInnes {
17793a40ed3dSBarry Smith   PetscFunctionBegin;
1780c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1781c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1782c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1783758f92a0SBarry Smith   snes->conv_hist_its   = its;
1784758f92a0SBarry Smith   snes->conv_hist_max   = na;
1785758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1786758f92a0SBarry Smith   PetscFunctionReturn(0);
1787758f92a0SBarry Smith }
1788758f92a0SBarry Smith 
1789758f92a0SBarry Smith #undef __FUNC__
1790758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory"
17910c4c9dddSBarry Smith /*@C
1792758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1793758f92a0SBarry Smith 
1794758f92a0SBarry Smith    Collective on SNES
1795758f92a0SBarry Smith 
1796758f92a0SBarry Smith    Input Parameter:
1797758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1798758f92a0SBarry Smith 
1799758f92a0SBarry Smith    Output Parameters:
1800758f92a0SBarry Smith .  a   - array to hold history
1801758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1802758f92a0SBarry Smith          negative if not converged) for each solve.
1803758f92a0SBarry Smith -  na  - size of a and its
1804758f92a0SBarry Smith 
1805758f92a0SBarry Smith    Notes:
1806758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1807758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1808758f92a0SBarry Smith 
1809758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1810758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1811758f92a0SBarry Smith    during the section of code that is being timed.
1812758f92a0SBarry Smith 
1813758f92a0SBarry Smith    Level: intermediate
1814758f92a0SBarry Smith 
1815758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1816758f92a0SBarry Smith 
1817758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1818758f92a0SBarry Smith 
1819758f92a0SBarry Smith @*/
1820758f92a0SBarry Smith int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na)
1821758f92a0SBarry Smith {
1822758f92a0SBarry Smith   PetscFunctionBegin;
1823758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1824758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1825758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1826758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
18273a40ed3dSBarry Smith   PetscFunctionReturn(0);
1828c9005455SLois Curfman McInnes }
1829c9005455SLois Curfman McInnes 
1830c9005455SLois Curfman McInnes #undef __FUNC__
18315615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private"
18329b94acceSBarry Smith /*
18339b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
18349b94acceSBarry Smith    positive parameter delta.
18359b94acceSBarry Smith 
18369b94acceSBarry Smith     Input Parameters:
1837c7afd0dbSLois Curfman McInnes +   snes - the SNES context
18389b94acceSBarry Smith .   y - approximate solution of linear system
18399b94acceSBarry Smith .   fnorm - 2-norm of current function
1840c7afd0dbSLois Curfman McInnes -   delta - trust region size
18419b94acceSBarry Smith 
18429b94acceSBarry Smith     Output Parameters:
1843c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
18449b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
18459b94acceSBarry Smith     region, and exceeds zero otherwise.
1846c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
18479b94acceSBarry Smith 
18489b94acceSBarry Smith     Note:
184940191667SLois Curfman McInnes     For non-trust region methods such as SNES_EQ_LS, the parameter delta
18509b94acceSBarry Smith     is set to be the maximum allowable step size.
18519b94acceSBarry Smith 
18529b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
18539b94acceSBarry Smith */
18549b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta,
18559b94acceSBarry Smith                           double *gpnorm,double *ynorm)
18569b94acceSBarry Smith {
18579b94acceSBarry Smith   double norm;
18589b94acceSBarry Smith   Scalar cnorm;
18593a40ed3dSBarry Smith   int    ierr;
18603a40ed3dSBarry Smith 
18613a40ed3dSBarry Smith   PetscFunctionBegin;
18623a40ed3dSBarry Smith   ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr);
18639b94acceSBarry Smith   if (norm > *delta) {
18649b94acceSBarry Smith      norm = *delta/norm;
18659b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
18669b94acceSBarry Smith      cnorm = norm;
18679b94acceSBarry Smith      VecScale( &cnorm, y );
18689b94acceSBarry Smith      *ynorm = *delta;
18699b94acceSBarry Smith   } else {
18709b94acceSBarry Smith      *gpnorm = 0.0;
18719b94acceSBarry Smith      *ynorm = norm;
18729b94acceSBarry Smith   }
18733a40ed3dSBarry Smith   PetscFunctionReturn(0);
18749b94acceSBarry Smith }
18759b94acceSBarry Smith 
18765615d1e5SSatish Balay #undef __FUNC__
18775615d1e5SSatish Balay #define __FUNC__ "SNESSolve"
18789b94acceSBarry Smith /*@
18799b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
188063a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
18819b94acceSBarry Smith 
1882c7afd0dbSLois Curfman McInnes    Collective on SNES
1883c7afd0dbSLois Curfman McInnes 
1884b2002411SLois Curfman McInnes    Input Parameters:
1885c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1886c7afd0dbSLois Curfman McInnes -  x - the solution vector
18879b94acceSBarry Smith 
18889b94acceSBarry Smith    Output Parameter:
1889b2002411SLois Curfman McInnes .  its - number of iterations until termination
18909b94acceSBarry Smith 
1891b2002411SLois Curfman McInnes    Notes:
18928ddd3da0SLois Curfman McInnes    The user should initialize the vector, x, with the initial guess
18938ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
18948ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
18958ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
18968ddd3da0SLois Curfman McInnes 
189736851e7fSLois Curfman McInnes    Level: beginner
189836851e7fSLois Curfman McInnes 
18999b94acceSBarry Smith .keywords: SNES, nonlinear, solve
19009b94acceSBarry Smith 
190163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
19029b94acceSBarry Smith @*/
19038ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
19049b94acceSBarry Smith {
19053c7409f5SSatish Balay   int ierr, flg;
1906052efed2SBarry Smith 
19073a40ed3dSBarry Smith   PetscFunctionBegin;
190877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
190974679c65SBarry Smith   PetscValidIntPointer(its);
191082bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);}
1911c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
1912758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
19139b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
1914c96a6f78SLois Curfman McInnes   snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0;
19159b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr);
19169b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
19173c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr);
19186d4a8577SBarry Smith   if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD); CHKERRQ(ierr); }
19193a40ed3dSBarry Smith   PetscFunctionReturn(0);
19209b94acceSBarry Smith }
19219b94acceSBarry Smith 
19229b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
19239b94acceSBarry Smith 
19245615d1e5SSatish Balay #undef __FUNC__
19255615d1e5SSatish Balay #define __FUNC__ "SNESSetType"
192682bf6240SBarry Smith /*@C
19274b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
19289b94acceSBarry Smith 
1929fee21e36SBarry Smith    Collective on SNES
1930fee21e36SBarry Smith 
1931c7afd0dbSLois Curfman McInnes    Input Parameters:
1932c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1933c7afd0dbSLois Curfman McInnes -  method - a known method
1934c7afd0dbSLois Curfman McInnes 
1935c7afd0dbSLois Curfman McInnes    Options Database Key:
1936c7afd0dbSLois Curfman McInnes .  -snes_type <method> - Sets the method; use -help for a list
1937c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1938ae12b187SLois Curfman McInnes 
19399b94acceSBarry Smith    Notes:
19409b94acceSBarry Smith    See "petsc/include/snes.h" for available methods (for instance)
194136851e7fSLois Curfman McInnes +    SNES_EQ_LS - Newton's method with line search
1942c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
1943c7afd0dbSLois Curfman McInnes .    SNES_EQ_TR - Newton's method with trust region
1944c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
1945c7afd0dbSLois Curfman McInnes .    SNES_UM_TR - Newton's method with trust region
1946c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
194736851e7fSLois Curfman McInnes -    SNES_UM_LS - Newton's method with line search
1948c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
19499b94acceSBarry Smith 
1950ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1951ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1952ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1953ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1954ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1955ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1956ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1957ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1958ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
195936851e7fSLois Curfman McInnes   appropriate method.  In other words, this routine is not for beginners.
196036851e7fSLois Curfman McInnes 
196136851e7fSLois Curfman McInnes   Level: intermediate
1962a703fe33SLois Curfman McInnes 
1963f536c699SSatish Balay .keywords: SNES, set, method
19649b94acceSBarry Smith @*/
19654b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method)
19669b94acceSBarry Smith {
196784cb2905SBarry Smith   int ierr;
19689b94acceSBarry Smith   int (*r)(SNES);
19693a40ed3dSBarry Smith 
19703a40ed3dSBarry Smith   PetscFunctionBegin;
197177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
197282bf6240SBarry Smith 
19733f1db9ecSBarry Smith   if (PetscTypeCompare(snes->type_name,method)) PetscFunctionReturn(0);
197482bf6240SBarry Smith 
197582bf6240SBarry Smith   if (snes->setupcalled) {
1976e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
197782bf6240SBarry Smith     snes->data = 0;
197882bf6240SBarry Smith   }
19797d1a2b2bSBarry Smith 
19809b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
198182bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
198282bf6240SBarry Smith 
1983488ecbafSBarry Smith   ierr =  FListFind(snes->comm, SNESList, method,(int (**)(void *)) &r );CHKERRQ(ierr);
198482bf6240SBarry Smith 
1985596552b5SBarry Smith   if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",method);
19861548b14aSBarry Smith 
19870452661fSBarry Smith   if (snes->data) PetscFree(snes->data);
198882bf6240SBarry Smith   snes->data = 0;
19893a40ed3dSBarry Smith   ierr = (*r)(snes); CHKERRQ(ierr);
199082bf6240SBarry Smith 
199182bf6240SBarry Smith   if (snes->type_name) PetscFree(snes->type_name);
199282bf6240SBarry Smith   snes->type_name = (char *) PetscMalloc((PetscStrlen(method)+1)*sizeof(char));CHKPTRQ(snes->type_name);
199382bf6240SBarry Smith   PetscStrcpy(snes->type_name,method);
199482bf6240SBarry Smith   snes->set_method_called = 1;
199582bf6240SBarry Smith 
19963a40ed3dSBarry Smith   PetscFunctionReturn(0);
19979b94acceSBarry Smith }
19989b94acceSBarry Smith 
1999a847f771SSatish Balay 
20009b94acceSBarry Smith /* --------------------------------------------------------------------- */
20015615d1e5SSatish Balay #undef __FUNC__
2002d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy"
20039b94acceSBarry Smith /*@C
20049b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
20059b94acceSBarry Smith    registered by SNESRegister().
20069b94acceSBarry Smith 
2007fee21e36SBarry Smith    Not Collective
2008fee21e36SBarry Smith 
200936851e7fSLois Curfman McInnes    Level: advanced
201036851e7fSLois Curfman McInnes 
20119b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
20129b94acceSBarry Smith 
20139b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
20149b94acceSBarry Smith @*/
2015cf256101SBarry Smith int SNESRegisterDestroy(void)
20169b94acceSBarry Smith {
201782bf6240SBarry Smith   int ierr;
201882bf6240SBarry Smith 
20193a40ed3dSBarry Smith   PetscFunctionBegin;
202082bf6240SBarry Smith   if (SNESList) {
2021488ecbafSBarry Smith     ierr = FListDestroy( SNESList );CHKERRQ(ierr);
202282bf6240SBarry Smith     SNESList = 0;
20239b94acceSBarry Smith   }
202484cb2905SBarry Smith   SNESRegisterAllCalled = 0;
20253a40ed3dSBarry Smith   PetscFunctionReturn(0);
20269b94acceSBarry Smith }
20279b94acceSBarry Smith 
20285615d1e5SSatish Balay #undef __FUNC__
2029d4bb536fSBarry Smith #define __FUNC__ "SNESGetType"
20309b94acceSBarry Smith /*@C
20319a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
20329b94acceSBarry Smith 
2033c7afd0dbSLois Curfman McInnes    Not Collective
2034c7afd0dbSLois Curfman McInnes 
20359b94acceSBarry Smith    Input Parameter:
20364b0e389bSBarry Smith .  snes - nonlinear solver context
20379b94acceSBarry Smith 
20389b94acceSBarry Smith    Output Parameter:
203982bf6240SBarry Smith .  method - SNES method (a charactor string)
20409b94acceSBarry Smith 
204136851e7fSLois Curfman McInnes    Level: intermediate
204236851e7fSLois Curfman McInnes 
20439b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name
20449b94acceSBarry Smith @*/
204582bf6240SBarry Smith int SNESGetType(SNES snes, SNESType *method)
20469b94acceSBarry Smith {
20473a40ed3dSBarry Smith   PetscFunctionBegin;
204882bf6240SBarry Smith   *method = snes->type_name;
20493a40ed3dSBarry Smith   PetscFunctionReturn(0);
20509b94acceSBarry Smith }
20519b94acceSBarry Smith 
20525615d1e5SSatish Balay #undef __FUNC__
2053d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution"
20549b94acceSBarry Smith /*@C
20559b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20569b94acceSBarry Smith    stored.
20579b94acceSBarry Smith 
2058c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2059c7afd0dbSLois Curfman McInnes 
20609b94acceSBarry Smith    Input Parameter:
20619b94acceSBarry Smith .  snes - the SNES context
20629b94acceSBarry Smith 
20639b94acceSBarry Smith    Output Parameter:
20649b94acceSBarry Smith .  x - the solution
20659b94acceSBarry Smith 
206636851e7fSLois Curfman McInnes    Level: advanced
206736851e7fSLois Curfman McInnes 
20689b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20699b94acceSBarry Smith 
2070a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
20719b94acceSBarry Smith @*/
20729b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
20739b94acceSBarry Smith {
20743a40ed3dSBarry Smith   PetscFunctionBegin;
207577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
20769b94acceSBarry Smith   *x = snes->vec_sol_always;
20773a40ed3dSBarry Smith   PetscFunctionReturn(0);
20789b94acceSBarry Smith }
20799b94acceSBarry Smith 
20805615d1e5SSatish Balay #undef __FUNC__
2081d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate"
20829b94acceSBarry Smith /*@C
20839b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
20849b94acceSBarry Smith    stored.
20859b94acceSBarry Smith 
2086c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2087c7afd0dbSLois Curfman McInnes 
20889b94acceSBarry Smith    Input Parameter:
20899b94acceSBarry Smith .  snes - the SNES context
20909b94acceSBarry Smith 
20919b94acceSBarry Smith    Output Parameter:
20929b94acceSBarry Smith .  x - the solution update
20939b94acceSBarry Smith 
209436851e7fSLois Curfman McInnes    Level: advanced
209536851e7fSLois Curfman McInnes 
20969b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
20979b94acceSBarry Smith 
20989b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
20999b94acceSBarry Smith @*/
21009b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
21019b94acceSBarry Smith {
21023a40ed3dSBarry Smith   PetscFunctionBegin;
210377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21049b94acceSBarry Smith   *x = snes->vec_sol_update_always;
21053a40ed3dSBarry Smith   PetscFunctionReturn(0);
21069b94acceSBarry Smith }
21079b94acceSBarry Smith 
21085615d1e5SSatish Balay #undef __FUNC__
2109d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction"
21109b94acceSBarry Smith /*@C
21113638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
21129b94acceSBarry Smith 
2113c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2114c7afd0dbSLois Curfman McInnes 
21159b94acceSBarry Smith    Input Parameter:
21169b94acceSBarry Smith .  snes - the SNES context
21179b94acceSBarry Smith 
21189b94acceSBarry Smith    Output Parameter:
21193638b69dSLois Curfman McInnes .  r - the function
21209b94acceSBarry Smith 
21219b94acceSBarry Smith    Notes:
21229b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
21239b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
21249b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
21259b94acceSBarry Smith 
212636851e7fSLois Curfman McInnes    Level: advanced
212736851e7fSLois Curfman McInnes 
2128a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
21299b94acceSBarry Smith 
213031615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
213131615d04SLois Curfman McInnes           SNESGetGradient()
21329b94acceSBarry Smith @*/
21339b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r)
21349b94acceSBarry Smith {
21353a40ed3dSBarry Smith   PetscFunctionBegin;
213677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2137a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
2138a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
2139a8c6a408SBarry Smith   }
21409b94acceSBarry Smith   *r = snes->vec_func_always;
21413a40ed3dSBarry Smith   PetscFunctionReturn(0);
21429b94acceSBarry Smith }
21439b94acceSBarry Smith 
21445615d1e5SSatish Balay #undef __FUNC__
2145d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient"
21469b94acceSBarry Smith /*@C
21473638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
21489b94acceSBarry Smith 
2149c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2150c7afd0dbSLois Curfman McInnes 
21519b94acceSBarry Smith    Input Parameter:
21529b94acceSBarry Smith .  snes - the SNES context
21539b94acceSBarry Smith 
21549b94acceSBarry Smith    Output Parameter:
21559b94acceSBarry Smith .  r - the gradient
21569b94acceSBarry Smith 
21579b94acceSBarry Smith    Notes:
21589b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
21599b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
21609b94acceSBarry Smith    SNESGetFunction().
21619b94acceSBarry Smith 
216236851e7fSLois Curfman McInnes    Level: advanced
216336851e7fSLois Curfman McInnes 
21649b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
21659b94acceSBarry Smith 
216631615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction()
21679b94acceSBarry Smith @*/
21689b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r)
21699b94acceSBarry Smith {
21703a40ed3dSBarry Smith   PetscFunctionBegin;
217177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21723a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2173a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
21743a40ed3dSBarry Smith   }
21759b94acceSBarry Smith   *r = snes->vec_func_always;
21763a40ed3dSBarry Smith   PetscFunctionReturn(0);
21779b94acceSBarry Smith }
21789b94acceSBarry Smith 
21795615d1e5SSatish Balay #undef __FUNC__
2180d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction"
21819b94acceSBarry Smith /*@
21829b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
21839b94acceSBarry Smith    unconstrained minimization problems.
21849b94acceSBarry Smith 
2185c7afd0dbSLois Curfman McInnes    Not Collective
2186c7afd0dbSLois Curfman McInnes 
21879b94acceSBarry Smith    Input Parameter:
21889b94acceSBarry Smith .  snes - the SNES context
21899b94acceSBarry Smith 
21909b94acceSBarry Smith    Output Parameter:
21919b94acceSBarry Smith .  r - the function
21929b94acceSBarry Smith 
21939b94acceSBarry Smith    Notes:
21949b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
21959b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
21969b94acceSBarry Smith    SNESGetFunction().
21979b94acceSBarry Smith 
219836851e7fSLois Curfman McInnes    Level: advanced
219936851e7fSLois Curfman McInnes 
22009b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
22019b94acceSBarry Smith 
220231615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction()
22039b94acceSBarry Smith @*/
22049b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r)
22059b94acceSBarry Smith {
22063a40ed3dSBarry Smith   PetscFunctionBegin;
220777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
220874679c65SBarry Smith   PetscValidScalarPointer(r);
22093a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2210a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
22113a40ed3dSBarry Smith   }
22129b94acceSBarry Smith   *r = snes->fc;
22133a40ed3dSBarry Smith   PetscFunctionReturn(0);
22149b94acceSBarry Smith }
22159b94acceSBarry Smith 
22165615d1e5SSatish Balay #undef __FUNC__
2217d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix"
22183c7409f5SSatish Balay /*@C
22193c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2220d850072dSLois Curfman McInnes    SNES options in the database.
22213c7409f5SSatish Balay 
2222fee21e36SBarry Smith    Collective on SNES
2223fee21e36SBarry Smith 
2224c7afd0dbSLois Curfman McInnes    Input Parameter:
2225c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2226c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2227c7afd0dbSLois Curfman McInnes 
2228d850072dSLois Curfman McInnes    Notes:
2229a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2230c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2231d850072dSLois Curfman McInnes 
223236851e7fSLois Curfman McInnes    Level: advanced
223336851e7fSLois Curfman McInnes 
22343c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2235a86d99e1SLois Curfman McInnes 
2236a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
22373c7409f5SSatish Balay @*/
22383c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
22393c7409f5SSatish Balay {
22403c7409f5SSatish Balay   int ierr;
22413c7409f5SSatish Balay 
22423a40ed3dSBarry Smith   PetscFunctionBegin;
224377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2244639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
22453c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
22463a40ed3dSBarry Smith   PetscFunctionReturn(0);
22473c7409f5SSatish Balay }
22483c7409f5SSatish Balay 
22495615d1e5SSatish Balay #undef __FUNC__
2250d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix"
22513c7409f5SSatish Balay /*@C
2252f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2253d850072dSLois Curfman McInnes    SNES options in the database.
22543c7409f5SSatish Balay 
2255fee21e36SBarry Smith    Collective on SNES
2256fee21e36SBarry Smith 
2257c7afd0dbSLois Curfman McInnes    Input Parameters:
2258c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2259c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2260c7afd0dbSLois Curfman McInnes 
2261d850072dSLois Curfman McInnes    Notes:
2262a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2263c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2264d850072dSLois Curfman McInnes 
226536851e7fSLois Curfman McInnes    Level: advanced
226636851e7fSLois Curfman McInnes 
22673c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2268a86d99e1SLois Curfman McInnes 
2269a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
22703c7409f5SSatish Balay @*/
22713c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
22723c7409f5SSatish Balay {
22733c7409f5SSatish Balay   int ierr;
22743c7409f5SSatish Balay 
22753a40ed3dSBarry Smith   PetscFunctionBegin;
227677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2277639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
22783c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
22793a40ed3dSBarry Smith   PetscFunctionReturn(0);
22803c7409f5SSatish Balay }
22813c7409f5SSatish Balay 
22825615d1e5SSatish Balay #undef __FUNC__
2283d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix"
22849ab63eb5SSatish Balay /*@C
22853c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
22863c7409f5SSatish Balay    SNES options in the database.
22873c7409f5SSatish Balay 
2288c7afd0dbSLois Curfman McInnes    Not Collective
2289c7afd0dbSLois Curfman McInnes 
22903c7409f5SSatish Balay    Input Parameter:
22913c7409f5SSatish Balay .  snes - the SNES context
22923c7409f5SSatish Balay 
22933c7409f5SSatish Balay    Output Parameter:
22943c7409f5SSatish Balay .  prefix - pointer to the prefix string used
22953c7409f5SSatish Balay 
22969ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
22979ab63eb5SSatish Balay    sufficient length to hold the prefix.
22989ab63eb5SSatish Balay 
229936851e7fSLois Curfman McInnes    Level: advanced
230036851e7fSLois Curfman McInnes 
23013c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2302a86d99e1SLois Curfman McInnes 
2303a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
23043c7409f5SSatish Balay @*/
23053c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
23063c7409f5SSatish Balay {
23073c7409f5SSatish Balay   int ierr;
23083c7409f5SSatish Balay 
23093a40ed3dSBarry Smith   PetscFunctionBegin;
231077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2311639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
23123a40ed3dSBarry Smith   PetscFunctionReturn(0);
23133c7409f5SSatish Balay }
23143c7409f5SSatish Balay 
2315ca161407SBarry Smith #undef __FUNC__
2316ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp"
2317ca161407SBarry Smith /*@
2318ca161407SBarry Smith    SNESPrintHelp - Prints all options for the SNES component.
2319ca161407SBarry Smith 
2320c7afd0dbSLois Curfman McInnes    Collective on SNES
2321c7afd0dbSLois Curfman McInnes 
2322ca161407SBarry Smith    Input Parameter:
2323ca161407SBarry Smith .  snes - the SNES context
2324ca161407SBarry Smith 
2325ca161407SBarry Smith    Options Database Keys:
2326c7afd0dbSLois Curfman McInnes +  -help - Prints SNES options
2327c7afd0dbSLois Curfman McInnes -  -h - Prints SNES options
2328ca161407SBarry Smith 
232936851e7fSLois Curfman McInnes    Level: beginner
233036851e7fSLois Curfman McInnes 
2331ca161407SBarry Smith .keywords: SNES, nonlinear, help
2332ca161407SBarry Smith 
2333ca161407SBarry Smith .seealso: SNESSetFromOptions()
2334ca161407SBarry Smith @*/
2335ca161407SBarry Smith int SNESPrintHelp(SNES snes)
2336ca161407SBarry Smith {
2337ca161407SBarry Smith   char                p[64];
2338ca161407SBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
2339ca161407SBarry Smith   int                 ierr;
2340ca161407SBarry Smith 
2341ca161407SBarry Smith   PetscFunctionBegin;
2342ca161407SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2343ca161407SBarry Smith 
2344ca161407SBarry Smith   PetscStrcpy(p,"-");
2345ca161407SBarry Smith   if (snes->prefix) PetscStrcat(p, snes->prefix);
2346ca161407SBarry Smith 
2347ca161407SBarry Smith   kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
2348ca161407SBarry Smith 
2349ebb8b11fSBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
2350d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr);
2351488ecbafSBarry Smith   ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr);
2352d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr);
2353d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr);
2354d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr);
2355d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr);
2356d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr);
2357d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr);
2358d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr);
2359d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr);
2360d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr);
2361d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_monitor: use default SNES convergence monitor, prints\n\
2362d132466eSBarry Smith     residual norm at each iteration.\n",p);CHKERRQ(ierr);
2363d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_smonitor: same as the above, but prints fewer digits of the\n\
2364ca161407SBarry Smith     residual norm for small residual norms. This is useful to conceal\n\
2365d132466eSBarry Smith     meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr);
2366d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr);
2367d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr);
2368d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr);
2369ca161407SBarry Smith   if (snes->type == SNES_NONLINEAR_EQUATIONS) {
2370d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2371d132466eSBarry Smith      " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr);
2372d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr);
2373d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr);
2374d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr);
2375d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf_ksp_monitor - if using matrix-free multiply then prints h at each KSP iteration\n",p);CHKERRQ(ierr);
2376d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr);
2377d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr);
2378d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2379d132466eSBarry Smith      "     %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr);
2380d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2381d132466eSBarry Smith      "     %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr);
2382d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2383d132466eSBarry Smith      "     %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr);
2384d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2385d132466eSBarry Smith      "     %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr);
2386d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2387d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr);
2388d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2389d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr);
2390d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2391d132466eSBarry Smith      "     %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr);
2392ca161407SBarry Smith   } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) {
2393d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr);
2394d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr);
2395d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr);
2396d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr);
2397ca161407SBarry Smith   }
2398d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <method> for help on ",p);CHKERRQ(ierr);
2399d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr);
2400ca161407SBarry Smith   if (snes->printhelp) {
2401ca161407SBarry Smith     ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr);
2402ca161407SBarry Smith   }
2403ca161407SBarry Smith   PetscFunctionReturn(0);
2404ca161407SBarry Smith }
24053c7409f5SSatish Balay 
2406acb85ae6SSatish Balay /*MC
2407b2002411SLois Curfman McInnes    SNESRegister - Adds a method to the nonlinear solver package.
24089b94acceSBarry Smith 
2409b2002411SLois Curfman McInnes    Synopsis:
2410b2002411SLois Curfman McInnes    SNESRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
24119b94acceSBarry Smith 
24128d76a1e5SLois Curfman McInnes    Not collective
24138d76a1e5SLois Curfman McInnes 
2414b2002411SLois Curfman McInnes    Input Parameters:
2415c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2416b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2417b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2418c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
24199b94acceSBarry Smith 
2420b2002411SLois Curfman McInnes    Notes:
2421b2002411SLois Curfman McInnes    SNESRegister() may be called multiple times to add several user-defined solvers.
24223a40ed3dSBarry Smith 
2423b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2424b2002411SLois Curfman McInnes    is ignored.
2425b2002411SLois Curfman McInnes 
2426b2002411SLois Curfman McInnes    Sample usage:
242769225d78SLois Curfman McInnes .vb
2428b2002411SLois Curfman McInnes    SNESRegister("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2429b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
243069225d78SLois Curfman McInnes .ve
2431b2002411SLois Curfman McInnes 
2432b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2433b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2434b2002411SLois Curfman McInnes    or at runtime via the option
2435b2002411SLois Curfman McInnes $     -snes_type my_solver
2436b2002411SLois Curfman McInnes 
243736851e7fSLois Curfman McInnes    Level: advanced
243836851e7fSLois Curfman McInnes 
2439dd438238SBarry Smith    $PETSC_ARCH and $BOPT occuring in pathname will be replaced with appropriate values.
2440dd438238SBarry Smith 
2441b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2442b2002411SLois Curfman McInnes 
2443b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2444b2002411SLois Curfman McInnes M*/
2445b2002411SLois Curfman McInnes 
2446b2002411SLois Curfman McInnes #undef __FUNC__
2447b2002411SLois Curfman McInnes #define __FUNC__ "SNESRegister_Private"
2448b2002411SLois Curfman McInnes int SNESRegister_Private(char *sname,char *path,char *name,int (*function)(SNES))
2449b2002411SLois Curfman McInnes {
2450b2002411SLois Curfman McInnes   char fullname[256];
2451b2002411SLois Curfman McInnes   int  ierr;
2452b2002411SLois Curfman McInnes 
2453b2002411SLois Curfman McInnes   PetscFunctionBegin;
2454b2002411SLois Curfman McInnes   PetscStrcpy(fullname,path); PetscStrcat(fullname,":");PetscStrcat(fullname,name);
2455488ecbafSBarry Smith   ierr = FListAdd_Private(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr);
2456b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2457b2002411SLois Curfman McInnes }
2458