xref: /petsc/src/snes/interface/snes.c (revision d132466e10dddff923c4add9c18d7fc5c6003d46)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*d132466eSBarry Smith static char vcid[] = "$Id: snes.c,v 1.182 1999/04/05 18:28:00 balay 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)) {
56*d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
57*d132466eSBarry Smith     ierr = SNESGetType(snes,&method);CHKERRQ(ierr);
58*d132466eSBarry 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     }
64*d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
65*d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n",
66*d132466eSBarry Smith                  snes->rtol, snes->atol, snes->trunctol, snes->xtol);CHKERRQ(ierr);
67*d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
68*d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
690ef38995SBarry Smith     if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
70*d132466eSBarry 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) {
75*d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
76*d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
77*d132466eSBarry 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
193*d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
194*d132466eSBarry 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);
273639f9d9dSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0);CHKERRQ(ierr);}
2743c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg); CHKERRQ(ierr);
2753f1db9ecSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0);  CHKERRQ(ierr);}
2763f1db9ecSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor",&flg); CHKERRQ(ierr);
2773f1db9ecSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0);  CHKERRQ(ierr);}
278*d132466eSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor_update",&flg); CHKERRQ(ierr);
279*d132466eSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitorUpdate,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);
285*d132466eSBarry 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);
2889b94acceSBarry Smith       ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr);
289f8c826e1SBarry Smith       snes->xmonitor = lg;
2909b94acceSBarry Smith       PLogObjectParent(snes,lg);
2919b94acceSBarry Smith     }
2929b94acceSBarry Smith   }
293e24b481bSBarry Smith 
2943c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);  CHKERRQ(ierr);
2953c7409f5SSatish Balay   if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
2969b94acceSBarry Smith     ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,
2979b94acceSBarry Smith                  SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr);
298981c4779SBarry Smith     PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
299981c4779SBarry Smith   } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
30031615d04SLois Curfman McInnes     ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,
30131615d04SLois Curfman McInnes                  SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr);
302d64ed03dSBarry Smith     PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
3039b94acceSBarry Smith   }
304639f9d9dSBarry Smith 
305639f9d9dSBarry Smith   for ( i=0; i<numberofsetfromoptions; i++ ) {
306639f9d9dSBarry Smith     ierr = (*othersetfromoptions[i])(snes); CHKERRQ(ierr);
307639f9d9dSBarry Smith   }
308639f9d9dSBarry Smith 
3099b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
3109b94acceSBarry Smith   ierr = SLESSetFromOptions(sles); CHKERRQ(ierr);
31193993e2dSLois Curfman McInnes 
312e24b481bSBarry Smith   /* set the special KSP monitor for matrix-free application */
313e24b481bSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg); CHKERRQ(ierr);
314e24b481bSBarry Smith   if (flg) {
315e24b481bSBarry Smith     KSP ksp;
316e24b481bSBarry Smith     ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
3175a655dc6SBarry Smith     ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL);CHKERRQ(ierr);
318e24b481bSBarry Smith   }
319e24b481bSBarry Smith 
32082bf6240SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr);
32182bf6240SBarry Smith   if (flg) { ierr = SNESPrintHelp(snes); CHKERRQ(ierr);}
32282bf6240SBarry Smith 
3233a40ed3dSBarry Smith   if (snes->setfromoptions) {
3243a40ed3dSBarry Smith     ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
3253a40ed3dSBarry Smith   }
3263a40ed3dSBarry Smith   PetscFunctionReturn(0);
3279b94acceSBarry Smith }
3289b94acceSBarry Smith 
329a847f771SSatish Balay 
3305615d1e5SSatish Balay #undef __FUNC__
331d4bb536fSBarry Smith #define __FUNC__ "SNESSetApplicationContext"
3329b94acceSBarry Smith /*@
3339b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3349b94acceSBarry Smith    the nonlinear solvers.
3359b94acceSBarry Smith 
336fee21e36SBarry Smith    Collective on SNES
337fee21e36SBarry Smith 
338c7afd0dbSLois Curfman McInnes    Input Parameters:
339c7afd0dbSLois Curfman McInnes +  snes - the SNES context
340c7afd0dbSLois Curfman McInnes -  usrP - optional user context
341c7afd0dbSLois Curfman McInnes 
34236851e7fSLois Curfman McInnes    Level: intermediate
34336851e7fSLois Curfman McInnes 
3449b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3459b94acceSBarry Smith 
3469b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3479b94acceSBarry Smith @*/
3489b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
3499b94acceSBarry Smith {
3503a40ed3dSBarry Smith   PetscFunctionBegin;
35177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3529b94acceSBarry Smith   snes->user		= usrP;
3533a40ed3dSBarry Smith   PetscFunctionReturn(0);
3549b94acceSBarry Smith }
35574679c65SBarry Smith 
3565615d1e5SSatish Balay #undef __FUNC__
357d4bb536fSBarry Smith #define __FUNC__ "SNESGetApplicationContext"
3589b94acceSBarry Smith /*@C
3599b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3609b94acceSBarry Smith    nonlinear solvers.
3619b94acceSBarry Smith 
362c7afd0dbSLois Curfman McInnes    Not Collective
363c7afd0dbSLois Curfman McInnes 
3649b94acceSBarry Smith    Input Parameter:
3659b94acceSBarry Smith .  snes - SNES context
3669b94acceSBarry Smith 
3679b94acceSBarry Smith    Output Parameter:
3689b94acceSBarry Smith .  usrP - user context
3699b94acceSBarry Smith 
37036851e7fSLois Curfman McInnes    Level: intermediate
37136851e7fSLois Curfman McInnes 
3729b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3739b94acceSBarry Smith 
3749b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3759b94acceSBarry Smith @*/
3769b94acceSBarry Smith int SNESGetApplicationContext( SNES snes,  void **usrP )
3779b94acceSBarry Smith {
3783a40ed3dSBarry Smith   PetscFunctionBegin;
37977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3809b94acceSBarry Smith   *usrP = snes->user;
3813a40ed3dSBarry Smith   PetscFunctionReturn(0);
3829b94acceSBarry Smith }
38374679c65SBarry Smith 
3845615d1e5SSatish Balay #undef __FUNC__
385d4bb536fSBarry Smith #define __FUNC__ "SNESGetIterationNumber"
3869b94acceSBarry Smith /*@
387c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
388c8228a4eSBarry Smith    at this time.
3899b94acceSBarry Smith 
390c7afd0dbSLois Curfman McInnes    Not Collective
391c7afd0dbSLois Curfman McInnes 
3929b94acceSBarry Smith    Input Parameter:
3939b94acceSBarry Smith .  snes - SNES context
3949b94acceSBarry Smith 
3959b94acceSBarry Smith    Output Parameter:
3969b94acceSBarry Smith .  iter - iteration number
3979b94acceSBarry Smith 
398c8228a4eSBarry Smith    Notes:
399c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
400c8228a4eSBarry Smith 
401c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
40208405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
40308405cd6SLois Curfman McInnes .vb
40408405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
40508405cd6SLois Curfman McInnes       if (!(it % 2)) {
40608405cd6SLois Curfman McInnes         [compute Jacobian here]
40708405cd6SLois Curfman McInnes       }
40808405cd6SLois Curfman McInnes .ve
409c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
41008405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
411c8228a4eSBarry Smith 
41236851e7fSLois Curfman McInnes    Level: intermediate
41336851e7fSLois Curfman McInnes 
4149b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
4159b94acceSBarry Smith @*/
4169b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
4179b94acceSBarry Smith {
4183a40ed3dSBarry Smith   PetscFunctionBegin;
41977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
42074679c65SBarry Smith   PetscValidIntPointer(iter);
4219b94acceSBarry Smith   *iter = snes->iter;
4223a40ed3dSBarry Smith   PetscFunctionReturn(0);
4239b94acceSBarry Smith }
42474679c65SBarry Smith 
4255615d1e5SSatish Balay #undef __FUNC__
4265615d1e5SSatish Balay #define __FUNC__ "SNESGetFunctionNorm"
4279b94acceSBarry Smith /*@
4289b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4299b94acceSBarry Smith    with SNESSSetFunction().
4309b94acceSBarry Smith 
431c7afd0dbSLois Curfman McInnes    Collective on SNES
432c7afd0dbSLois Curfman McInnes 
4339b94acceSBarry Smith    Input Parameter:
4349b94acceSBarry Smith .  snes - SNES context
4359b94acceSBarry Smith 
4369b94acceSBarry Smith    Output Parameter:
4379b94acceSBarry Smith .  fnorm - 2-norm of function
4389b94acceSBarry Smith 
4399b94acceSBarry Smith    Note:
4409b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
441a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
442a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
4439b94acceSBarry Smith 
44436851e7fSLois Curfman McInnes    Level: intermediate
44536851e7fSLois Curfman McInnes 
4469b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
447a86d99e1SLois Curfman McInnes 
44808405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
4499b94acceSBarry Smith @*/
4509b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm)
4519b94acceSBarry Smith {
4523a40ed3dSBarry Smith   PetscFunctionBegin;
45377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
45474679c65SBarry Smith   PetscValidScalarPointer(fnorm);
45574679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
456d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only");
45774679c65SBarry Smith   }
4589b94acceSBarry Smith   *fnorm = snes->norm;
4593a40ed3dSBarry Smith   PetscFunctionReturn(0);
4609b94acceSBarry Smith }
46174679c65SBarry Smith 
4625615d1e5SSatish Balay #undef __FUNC__
4635615d1e5SSatish Balay #define __FUNC__ "SNESGetGradientNorm"
4649b94acceSBarry Smith /*@
4659b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
4669b94acceSBarry Smith    with SNESSSetGradient().
4679b94acceSBarry Smith 
468c7afd0dbSLois Curfman McInnes    Collective on SNES
469c7afd0dbSLois Curfman McInnes 
4709b94acceSBarry Smith    Input Parameter:
4719b94acceSBarry Smith .  snes - SNES context
4729b94acceSBarry Smith 
4739b94acceSBarry Smith    Output Parameter:
4749b94acceSBarry Smith .  fnorm - 2-norm of gradient
4759b94acceSBarry Smith 
4769b94acceSBarry Smith    Note:
4779b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
478a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
479a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
4809b94acceSBarry Smith 
48136851e7fSLois Curfman McInnes    Level: intermediate
48236851e7fSLois Curfman McInnes 
4839b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
484a86d99e1SLois Curfman McInnes 
485a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
4869b94acceSBarry Smith @*/
4879b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm)
4889b94acceSBarry Smith {
4893a40ed3dSBarry Smith   PetscFunctionBegin;
49077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
49174679c65SBarry Smith   PetscValidScalarPointer(gnorm);
49274679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
493d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
49474679c65SBarry Smith   }
4959b94acceSBarry Smith   *gnorm = snes->norm;
4963a40ed3dSBarry Smith   PetscFunctionReturn(0);
4979b94acceSBarry Smith }
49874679c65SBarry Smith 
4995615d1e5SSatish Balay #undef __FUNC__
500d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberUnsuccessfulSteps"
5019b94acceSBarry Smith /*@
5029b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
5039b94acceSBarry Smith    attempted by the nonlinear solver.
5049b94acceSBarry Smith 
505c7afd0dbSLois Curfman McInnes    Not Collective
506c7afd0dbSLois Curfman McInnes 
5079b94acceSBarry Smith    Input Parameter:
5089b94acceSBarry Smith .  snes - SNES context
5099b94acceSBarry Smith 
5109b94acceSBarry Smith    Output Parameter:
5119b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
5129b94acceSBarry Smith 
513c96a6f78SLois Curfman McInnes    Notes:
514c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
515c96a6f78SLois Curfman McInnes 
51636851e7fSLois Curfman McInnes    Level: intermediate
51736851e7fSLois Curfman McInnes 
5189b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
5199b94acceSBarry Smith @*/
5209b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
5219b94acceSBarry Smith {
5223a40ed3dSBarry Smith   PetscFunctionBegin;
52377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
52474679c65SBarry Smith   PetscValidIntPointer(nfails);
5259b94acceSBarry Smith   *nfails = snes->nfailures;
5263a40ed3dSBarry Smith   PetscFunctionReturn(0);
5279b94acceSBarry Smith }
528a847f771SSatish Balay 
5295615d1e5SSatish Balay #undef __FUNC__
530d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberLinearIterations"
531c96a6f78SLois Curfman McInnes /*@
532c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
533c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
534c96a6f78SLois Curfman McInnes 
535c7afd0dbSLois Curfman McInnes    Not Collective
536c7afd0dbSLois Curfman McInnes 
537c96a6f78SLois Curfman McInnes    Input Parameter:
538c96a6f78SLois Curfman McInnes .  snes - SNES context
539c96a6f78SLois Curfman McInnes 
540c96a6f78SLois Curfman McInnes    Output Parameter:
541c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
542c96a6f78SLois Curfman McInnes 
543c96a6f78SLois Curfman McInnes    Notes:
544c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
545c96a6f78SLois Curfman McInnes 
54636851e7fSLois Curfman McInnes    Level: intermediate
54736851e7fSLois Curfman McInnes 
548c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
549c96a6f78SLois Curfman McInnes @*/
55086bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
551c96a6f78SLois Curfman McInnes {
5523a40ed3dSBarry Smith   PetscFunctionBegin;
553c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
554c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
555c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5563a40ed3dSBarry Smith   PetscFunctionReturn(0);
557c96a6f78SLois Curfman McInnes }
558c96a6f78SLois Curfman McInnes 
559c96a6f78SLois Curfman McInnes #undef __FUNC__
560d4bb536fSBarry Smith #define __FUNC__ "SNESGetSLES"
5619b94acceSBarry Smith /*@C
5629b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
5639b94acceSBarry Smith 
564c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
565c7afd0dbSLois Curfman McInnes 
5669b94acceSBarry Smith    Input Parameter:
5679b94acceSBarry Smith .  snes - the SNES context
5689b94acceSBarry Smith 
5699b94acceSBarry Smith    Output Parameter:
5709b94acceSBarry Smith .  sles - the SLES context
5719b94acceSBarry Smith 
5729b94acceSBarry Smith    Notes:
5739b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
5749b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
5759b94acceSBarry Smith    KSP and PC contexts as well.
5769b94acceSBarry Smith 
57736851e7fSLois Curfman McInnes    Level: beginner
57836851e7fSLois Curfman McInnes 
5799b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
5809b94acceSBarry Smith 
5819b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
5829b94acceSBarry Smith @*/
5839b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
5849b94acceSBarry Smith {
5853a40ed3dSBarry Smith   PetscFunctionBegin;
58677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5879b94acceSBarry Smith   *sles = snes->sles;
5883a40ed3dSBarry Smith   PetscFunctionReturn(0);
5899b94acceSBarry Smith }
59082bf6240SBarry Smith 
591e24b481bSBarry Smith #undef __FUNC__
592e24b481bSBarry Smith #define __FUNC__ "SNESPublish_Petsc"
593e24b481bSBarry Smith static int SNESPublish_Petsc(PetscObject object)
594e24b481bSBarry Smith {
595e24b481bSBarry Smith #if defined(HAVE_AMS)
596e24b481bSBarry Smith   SNES          v = (SNES) object;
597e24b481bSBarry Smith   int          ierr;
598e24b481bSBarry Smith 
599e24b481bSBarry Smith   PetscFunctionBegin;
600e24b481bSBarry Smith 
601e24b481bSBarry Smith   /* if it is already published then return */
602e24b481bSBarry Smith   if (v->amem >=0 ) PetscFunctionReturn(0);
603e24b481bSBarry Smith 
6043f1db9ecSBarry Smith   ierr = PetscObjectPublishBaseBegin(object);CHKERRQ(ierr);
605e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
606e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
607e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
608e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
609e24b481bSBarry Smith   ierr = PetscObjectPublishBaseEnd(object);CHKERRQ(ierr);
610e24b481bSBarry Smith #else
611e24b481bSBarry Smith   PetscFunctionBegin;
612e24b481bSBarry Smith #endif
613e24b481bSBarry Smith   PetscFunctionReturn(0);
614e24b481bSBarry Smith }
615e24b481bSBarry Smith 
6169b94acceSBarry Smith /* -----------------------------------------------------------*/
6175615d1e5SSatish Balay #undef __FUNC__
6185615d1e5SSatish Balay #define __FUNC__ "SNESCreate"
6199b94acceSBarry Smith /*@C
6209b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
6219b94acceSBarry Smith 
622c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
623c7afd0dbSLois Curfman McInnes 
624c7afd0dbSLois Curfman McInnes    Input Parameters:
625c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
626c7afd0dbSLois Curfman McInnes -  type - type of method, either
627c7afd0dbSLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
628c7afd0dbSLois Curfman McInnes    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
6299b94acceSBarry Smith 
6309b94acceSBarry Smith    Output Parameter:
6319b94acceSBarry Smith .  outsnes - the new SNES context
6329b94acceSBarry Smith 
633c7afd0dbSLois Curfman McInnes    Options Database Keys:
634c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
635c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
636c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
637c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
638c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
639c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
640c1f60f51SBarry Smith 
64136851e7fSLois Curfman McInnes    Level: beginner
64236851e7fSLois Curfman McInnes 
6439b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
6449b94acceSBarry Smith 
64563a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy()
6469b94acceSBarry Smith @*/
6474b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
6489b94acceSBarry Smith {
6499b94acceSBarry Smith   int                 ierr;
6509b94acceSBarry Smith   SNES                snes;
6519b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
65237fcc0dbSBarry Smith 
6533a40ed3dSBarry Smith   PetscFunctionBegin;
6549b94acceSBarry Smith   *outsnes = 0;
655d64ed03dSBarry Smith   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){
656d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type");
657d64ed03dSBarry Smith   }
6583f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
6599b94acceSBarry Smith   PLogObjectCreate(snes);
660e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6619b94acceSBarry Smith   snes->max_its           = 50;
6629750a799SBarry Smith   snes->max_funcs	  = 10000;
6639b94acceSBarry Smith   snes->norm		  = 0.0;
6645a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
665b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
666b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
6679b94acceSBarry Smith     snes->atol		  = 1.e-10;
668d64ed03dSBarry Smith   } else {
669b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
670b4874afaSBarry Smith     snes->ttol            = 0.0;
671b4874afaSBarry Smith     snes->atol		  = 1.e-50;
672b4874afaSBarry Smith   }
6739b94acceSBarry Smith   snes->xtol		  = 1.e-8;
674b18e04deSLois Curfman McInnes   snes->trunctol	  = 1.e-12; /* no longer used */
6759b94acceSBarry Smith   snes->nfuncs            = 0;
6769b94acceSBarry Smith   snes->nfailures         = 0;
6777a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
678639f9d9dSBarry Smith   snes->numbermonitors    = 0;
6799b94acceSBarry Smith   snes->data              = 0;
6809b94acceSBarry Smith   snes->view              = 0;
6819b94acceSBarry Smith   snes->computeumfunction = 0;
6829b94acceSBarry Smith   snes->umfunP            = 0;
6839b94acceSBarry Smith   snes->fc                = 0;
6849b94acceSBarry Smith   snes->deltatol          = 1.e-12;
6859b94acceSBarry Smith   snes->fmin              = -1.e30;
6869b94acceSBarry Smith   snes->method_class      = type;
6879b94acceSBarry Smith   snes->set_method_called = 0;
68882bf6240SBarry Smith   snes->setupcalled      = 0;
6899b94acceSBarry Smith   snes->ksp_ewconv        = 0;
6906f24a144SLois Curfman McInnes   snes->xmonitor          = 0;
6916f24a144SLois Curfman McInnes   snes->vwork             = 0;
6926f24a144SLois Curfman McInnes   snes->nwork             = 0;
693758f92a0SBarry Smith   snes->conv_hist_len     = 0;
694758f92a0SBarry Smith   snes->conv_hist_max     = 0;
695758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
696758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
697758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
6989b94acceSBarry Smith 
6999b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
7000452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx);
701eed86810SBarry Smith   PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
7029b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7039b94acceSBarry Smith   kctx->version     = 2;
7049b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7059b94acceSBarry Smith                              this was too large for some test cases */
7069b94acceSBarry Smith   kctx->rtol_last   = 0;
7079b94acceSBarry Smith   kctx->rtol_max    = .9;
7089b94acceSBarry Smith   kctx->gamma       = 1.0;
7099b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7109b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7119b94acceSBarry Smith   kctx->threshold   = .1;
7129b94acceSBarry Smith   kctx->lresid_last = 0;
7139b94acceSBarry Smith   kctx->norm_last   = 0;
7149b94acceSBarry Smith 
7159b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr);
7169b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
7175334005bSBarry Smith 
7189b94acceSBarry Smith   *outsnes = snes;
719e24b481bSBarry Smith   PetscPublishAll(snes);
7203a40ed3dSBarry Smith   PetscFunctionReturn(0);
7219b94acceSBarry Smith }
7229b94acceSBarry Smith 
7239b94acceSBarry Smith /* --------------------------------------------------------------- */
7245615d1e5SSatish Balay #undef __FUNC__
725d4bb536fSBarry Smith #define __FUNC__ "SNESSetFunction"
7269b94acceSBarry Smith /*@C
7279b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
7289b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
7299b94acceSBarry Smith    equations.
7309b94acceSBarry Smith 
731fee21e36SBarry Smith    Collective on SNES
732fee21e36SBarry Smith 
733c7afd0dbSLois Curfman McInnes    Input Parameters:
734c7afd0dbSLois Curfman McInnes +  snes - the SNES context
735c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
736c7afd0dbSLois Curfman McInnes .  r - vector to store function value
737c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
738c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7399b94acceSBarry Smith 
740c7afd0dbSLois Curfman McInnes    Calling sequence of func:
7418d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
742c7afd0dbSLois Curfman McInnes 
743313e4042SLois Curfman McInnes .  f - function vector
744c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
7459b94acceSBarry Smith 
7469b94acceSBarry Smith    Notes:
7479b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
7489b94acceSBarry Smith $      f'(x) x = -f(x),
749c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
7509b94acceSBarry Smith 
7519b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7529b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7539b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
7549b94acceSBarry Smith 
75536851e7fSLois Curfman McInnes    Level: beginner
75636851e7fSLois Curfman McInnes 
7579b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
7589b94acceSBarry Smith 
759a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
7609b94acceSBarry Smith @*/
7615334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx)
7629b94acceSBarry Smith {
7633a40ed3dSBarry Smith   PetscFunctionBegin;
76477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
765a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
766a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
767a8c6a408SBarry Smith   }
7689b94acceSBarry Smith   snes->computefunction     = func;
7699b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7709b94acceSBarry Smith   snes->funP                = ctx;
7713a40ed3dSBarry Smith   PetscFunctionReturn(0);
7729b94acceSBarry Smith }
7739b94acceSBarry Smith 
7745615d1e5SSatish Balay #undef __FUNC__
7755615d1e5SSatish Balay #define __FUNC__ "SNESComputeFunction"
7769b94acceSBarry Smith /*@
77736851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
7789b94acceSBarry Smith    SNESSetFunction().
7799b94acceSBarry Smith 
780c7afd0dbSLois Curfman McInnes    Collective on SNES
781c7afd0dbSLois Curfman McInnes 
7829b94acceSBarry Smith    Input Parameters:
783c7afd0dbSLois Curfman McInnes +  snes - the SNES context
784c7afd0dbSLois Curfman McInnes -  x - input vector
7859b94acceSBarry Smith 
7869b94acceSBarry Smith    Output Parameter:
7873638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
7889b94acceSBarry Smith 
7891bffabb2SLois Curfman McInnes    Notes:
7909b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7919b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7929b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
7939b94acceSBarry Smith 
79436851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
79536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
79636851e7fSLois Curfman McInnes    themselves.
79736851e7fSLois Curfman McInnes 
79836851e7fSLois Curfman McInnes    Level: developer
79936851e7fSLois Curfman McInnes 
8009b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
8019b94acceSBarry Smith 
802a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
8039b94acceSBarry Smith @*/
8049b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y)
8059b94acceSBarry Smith {
8069b94acceSBarry Smith   int    ierr;
8079b94acceSBarry Smith 
8083a40ed3dSBarry Smith   PetscFunctionBegin;
809d4bb536fSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
810a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
811d4bb536fSBarry Smith   }
8129b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
813d64ed03dSBarry Smith   PetscStackPush("SNES user function");
8149b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
815d64ed03dSBarry Smith   PetscStackPop;
816ae3c334cSLois Curfman McInnes   snes->nfuncs++;
8179b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
8183a40ed3dSBarry Smith   PetscFunctionReturn(0);
8199b94acceSBarry Smith }
8209b94acceSBarry Smith 
8215615d1e5SSatish Balay #undef __FUNC__
822d4bb536fSBarry Smith #define __FUNC__ "SNESSetMinimizationFunction"
8239b94acceSBarry Smith /*@C
8249b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
8259b94acceSBarry Smith    unconstrained minimization.
8269b94acceSBarry Smith 
827fee21e36SBarry Smith    Collective on SNES
828fee21e36SBarry Smith 
829c7afd0dbSLois Curfman McInnes    Input Parameters:
830c7afd0dbSLois Curfman McInnes +  snes - the SNES context
831c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
832c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
833c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8349b94acceSBarry Smith 
835c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8368d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,double *f,void *ctx);
837c7afd0dbSLois Curfman McInnes 
838c7afd0dbSLois Curfman McInnes +  x - input vector
8399b94acceSBarry Smith .  f - function
840c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined function context
8419b94acceSBarry Smith 
84236851e7fSLois Curfman McInnes    Level: beginner
84336851e7fSLois Curfman McInnes 
8449b94acceSBarry Smith    Notes:
8459b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
8469b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
8479b94acceSBarry Smith    SNESSetFunction().
8489b94acceSBarry Smith 
8499b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
8509b94acceSBarry Smith 
851a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
852a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
8539b94acceSBarry Smith @*/
8549b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),
8559b94acceSBarry Smith                       void *ctx)
8569b94acceSBarry Smith {
8573a40ed3dSBarry Smith   PetscFunctionBegin;
85877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
859a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
860a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
861a8c6a408SBarry Smith   }
8629b94acceSBarry Smith   snes->computeumfunction   = func;
8639b94acceSBarry Smith   snes->umfunP              = ctx;
8643a40ed3dSBarry Smith   PetscFunctionReturn(0);
8659b94acceSBarry Smith }
8669b94acceSBarry Smith 
8675615d1e5SSatish Balay #undef __FUNC__
8685615d1e5SSatish Balay #define __FUNC__ "SNESComputeMinimizationFunction"
8699b94acceSBarry Smith /*@
8709b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
8719b94acceSBarry Smith    set with SNESSetMinimizationFunction().
8729b94acceSBarry Smith 
873c7afd0dbSLois Curfman McInnes    Collective on SNES
874c7afd0dbSLois Curfman McInnes 
8759b94acceSBarry Smith    Input Parameters:
876c7afd0dbSLois Curfman McInnes +  snes - the SNES context
877c7afd0dbSLois Curfman McInnes -  x - input vector
8789b94acceSBarry Smith 
8799b94acceSBarry Smith    Output Parameter:
8809b94acceSBarry Smith .  y - function value
8819b94acceSBarry Smith 
8829b94acceSBarry Smith    Notes:
8839b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
8849b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
8859b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
886a86d99e1SLois Curfman McInnes 
88736851e7fSLois Curfman McInnes    SNESComputeMinimizationFunction() is typically used within minimization
88836851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
88936851e7fSLois Curfman McInnes    themselves.
89036851e7fSLois Curfman McInnes 
89136851e7fSLois Curfman McInnes    Level: developer
89236851e7fSLois Curfman McInnes 
893a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
894a86d99e1SLois Curfman McInnes 
895a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
896a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
8979b94acceSBarry Smith @*/
8989b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y)
8999b94acceSBarry Smith {
9009b94acceSBarry Smith   int    ierr;
9013a40ed3dSBarry Smith 
9023a40ed3dSBarry Smith   PetscFunctionBegin;
903a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
904a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
905a8c6a408SBarry Smith   }
9069b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
907d64ed03dSBarry Smith   PetscStackPush("SNES user minimzation function");
9089b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr);
909d64ed03dSBarry Smith   PetscStackPop;
910ae3c334cSLois Curfman McInnes   snes->nfuncs++;
9119b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
9123a40ed3dSBarry Smith   PetscFunctionReturn(0);
9139b94acceSBarry Smith }
9149b94acceSBarry Smith 
9155615d1e5SSatish Balay #undef __FUNC__
916d4bb536fSBarry Smith #define __FUNC__ "SNESSetGradient"
9179b94acceSBarry Smith /*@C
9189b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
9199b94acceSBarry Smith    vector for use by the SNES routines.
9209b94acceSBarry Smith 
921c7afd0dbSLois Curfman McInnes    Collective on SNES
922c7afd0dbSLois Curfman McInnes 
9239b94acceSBarry Smith    Input Parameters:
924c7afd0dbSLois Curfman McInnes +  snes - the SNES context
9259b94acceSBarry Smith .  func - function evaluation routine
926044dda88SLois Curfman McInnes .  ctx - optional user-defined context for private data for the
927044dda88SLois Curfman McInnes          gradient evaluation routine (may be PETSC_NULL)
928c7afd0dbSLois Curfman McInnes -  r - vector to store gradient value
929fee21e36SBarry Smith 
9309b94acceSBarry Smith    Calling sequence of func:
9318d76a1e5SLois Curfman McInnes $     func (SNES, Vec x, Vec g, void *ctx);
9329b94acceSBarry Smith 
933c7afd0dbSLois Curfman McInnes +  x - input vector
9349b94acceSBarry Smith .  g - gradient vector
935c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined gradient context
9369b94acceSBarry Smith 
9379b94acceSBarry Smith    Notes:
9389b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
9399b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
9409b94acceSBarry Smith    SNESSetFunction().
9419b94acceSBarry Smith 
94236851e7fSLois Curfman McInnes    Level: beginner
94336851e7fSLois Curfman McInnes 
9449b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9459b94acceSBarry Smith 
946a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
947a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
9489b94acceSBarry Smith @*/
94974679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
9509b94acceSBarry Smith {
9513a40ed3dSBarry Smith   PetscFunctionBegin;
95277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
953a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
954a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
955a8c6a408SBarry Smith   }
9569b94acceSBarry Smith   snes->computefunction     = func;
9579b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
9589b94acceSBarry Smith   snes->funP                = ctx;
9593a40ed3dSBarry Smith   PetscFunctionReturn(0);
9609b94acceSBarry Smith }
9619b94acceSBarry Smith 
9625615d1e5SSatish Balay #undef __FUNC__
9635615d1e5SSatish Balay #define __FUNC__ "SNESComputeGradient"
9649b94acceSBarry Smith /*@
965a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
966a86d99e1SLois Curfman McInnes    SNESSetGradient().
9679b94acceSBarry Smith 
968c7afd0dbSLois Curfman McInnes    Collective on SNES
969c7afd0dbSLois Curfman McInnes 
9709b94acceSBarry Smith    Input Parameters:
971c7afd0dbSLois Curfman McInnes +  snes - the SNES context
972c7afd0dbSLois Curfman McInnes -  x - input vector
9739b94acceSBarry Smith 
9749b94acceSBarry Smith    Output Parameter:
9759b94acceSBarry Smith .  y - gradient vector
9769b94acceSBarry Smith 
9779b94acceSBarry Smith    Notes:
9789b94acceSBarry Smith    SNESComputeGradient() is valid only for
9799b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
9809b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
981a86d99e1SLois Curfman McInnes 
98236851e7fSLois Curfman McInnes    SNESComputeGradient() is typically used within minimization
98336851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
98436851e7fSLois Curfman McInnes    themselves.
98536851e7fSLois Curfman McInnes 
98636851e7fSLois Curfman McInnes    Level: developer
98736851e7fSLois Curfman McInnes 
988a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
989a86d99e1SLois Curfman McInnes 
990a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
991a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
9929b94acceSBarry Smith @*/
9939b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y)
9949b94acceSBarry Smith {
9959b94acceSBarry Smith   int    ierr;
9963a40ed3dSBarry Smith 
9973a40ed3dSBarry Smith   PetscFunctionBegin;
9983a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
999a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
10003a40ed3dSBarry Smith   }
10019b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
1002d64ed03dSBarry Smith   PetscStackPush("SNES user gradient function");
10039b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
1004d64ed03dSBarry Smith   PetscStackPop;
10059b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
10063a40ed3dSBarry Smith   PetscFunctionReturn(0);
10079b94acceSBarry Smith }
10089b94acceSBarry Smith 
10095615d1e5SSatish Balay #undef __FUNC__
10105615d1e5SSatish Balay #define __FUNC__ "SNESComputeJacobian"
101162fef451SLois Curfman McInnes /*@
101262fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
101362fef451SLois Curfman McInnes    set with SNESSetJacobian().
101462fef451SLois Curfman McInnes 
1015c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1016c7afd0dbSLois Curfman McInnes 
101762fef451SLois Curfman McInnes    Input Parameters:
1018c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1019c7afd0dbSLois Curfman McInnes -  x - input vector
102062fef451SLois Curfman McInnes 
102162fef451SLois Curfman McInnes    Output Parameters:
1022c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
102362fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1024c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1025fee21e36SBarry Smith 
102662fef451SLois Curfman McInnes    Notes:
102762fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
102862fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
102962fef451SLois Curfman McInnes 
1030dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1031dc5a77f8SLois Curfman McInnes    flag parameter.
103262fef451SLois Curfman McInnes 
103362fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
103462fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
1035005c665bSBarry Smith    methods is SNESComputeHessian().
103662fef451SLois Curfman McInnes 
103736851e7fSLois Curfman McInnes    SNESComputeJacobian() is typically used within nonlinear solver
103836851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
103936851e7fSLois Curfman McInnes    themselves.
104036851e7fSLois Curfman McInnes 
104136851e7fSLois Curfman McInnes    Level: developer
104236851e7fSLois Curfman McInnes 
104362fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
104462fef451SLois Curfman McInnes 
104562fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
104662fef451SLois Curfman McInnes @*/
10479b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10489b94acceSBarry Smith {
10499b94acceSBarry Smith   int    ierr;
10503a40ed3dSBarry Smith 
10513a40ed3dSBarry Smith   PetscFunctionBegin;
10523a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1053a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
10543a40ed3dSBarry Smith   }
10553a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
10569b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
1057c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1058d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
10599b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr);
1060d64ed03dSBarry Smith   PetscStackPop;
10619b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
10626d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
106377c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
106477c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
10653a40ed3dSBarry Smith   PetscFunctionReturn(0);
10669b94acceSBarry Smith }
10679b94acceSBarry Smith 
10685615d1e5SSatish Balay #undef __FUNC__
10695615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian"
107062fef451SLois Curfman McInnes /*@
107162fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
107262fef451SLois Curfman McInnes    set with SNESSetHessian().
107362fef451SLois Curfman McInnes 
1074c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1075c7afd0dbSLois Curfman McInnes 
107662fef451SLois Curfman McInnes    Input Parameters:
1077c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1078c7afd0dbSLois Curfman McInnes -  x - input vector
107962fef451SLois Curfman McInnes 
108062fef451SLois Curfman McInnes    Output Parameters:
1081c7afd0dbSLois Curfman McInnes +  A - Hessian matrix
108262fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1083c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1084fee21e36SBarry Smith 
108562fef451SLois Curfman McInnes    Notes:
108662fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
108762fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
108862fef451SLois Curfman McInnes 
1089dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1090dc5a77f8SLois Curfman McInnes    flag parameter.
109162fef451SLois Curfman McInnes 
109262fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
109362fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
109462fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
109562fef451SLois Curfman McInnes 
109636851e7fSLois Curfman McInnes    SNESComputeHessian() is typically used within minimization
109736851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
109836851e7fSLois Curfman McInnes    themselves.
109936851e7fSLois Curfman McInnes 
110036851e7fSLois Curfman McInnes    Level: developer
110136851e7fSLois Curfman McInnes 
110262fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
110362fef451SLois Curfman McInnes 
1104a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
1105a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
110662fef451SLois Curfman McInnes @*/
110762fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
11089b94acceSBarry Smith {
11099b94acceSBarry Smith   int    ierr;
11103a40ed3dSBarry Smith 
11113a40ed3dSBarry Smith   PetscFunctionBegin;
11123a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1113a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
11143a40ed3dSBarry Smith   }
11153a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
111662fef451SLois Curfman McInnes   PLogEventBegin(SNES_HessianEval,snes,x,*A,*B);
11170f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
1118d64ed03dSBarry Smith   PetscStackPush("SNES user Hessian function");
111962fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr);
1120d64ed03dSBarry Smith   PetscStackPop;
112162fef451SLois Curfman McInnes   PLogEventEnd(SNES_HessianEval,snes,x,*A,*B);
11220f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
112377c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
112477c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11253a40ed3dSBarry Smith   PetscFunctionReturn(0);
11269b94acceSBarry Smith }
11279b94acceSBarry Smith 
11285615d1e5SSatish Balay #undef __FUNC__
1129d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian"
11309b94acceSBarry Smith /*@C
11319b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1132044dda88SLois Curfman McInnes    location to store the matrix.
11339b94acceSBarry Smith 
1134c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1135c7afd0dbSLois Curfman McInnes 
11369b94acceSBarry Smith    Input Parameters:
1137c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11389b94acceSBarry Smith .  A - Jacobian matrix
11399b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11409b94acceSBarry Smith .  func - Jacobian evaluation routine
1141c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11422cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11439b94acceSBarry Smith 
11449b94acceSBarry Smith    Calling sequence of func:
11458d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11469b94acceSBarry Smith 
1147c7afd0dbSLois Curfman McInnes +  x - input vector
11489b94acceSBarry Smith .  A - Jacobian matrix
11499b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1150ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1151ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1152c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11539b94acceSBarry Smith 
11549b94acceSBarry Smith    Notes:
1155dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
11562cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1157ac21db08SLois Curfman McInnes 
1158ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11599b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11609b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11619b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11629b94acceSBarry Smith    throughout the global iterations.
11639b94acceSBarry Smith 
116436851e7fSLois Curfman McInnes    Level: beginner
116536851e7fSLois Curfman McInnes 
11669b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11679b94acceSBarry Smith 
1168ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
11699b94acceSBarry Smith @*/
11709b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
11719b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
11729b94acceSBarry Smith {
11733a40ed3dSBarry Smith   PetscFunctionBegin;
117477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1175a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1176a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
1177a8c6a408SBarry Smith   }
11789b94acceSBarry Smith   snes->computejacobian = func;
11799b94acceSBarry Smith   snes->jacP            = ctx;
11809b94acceSBarry Smith   snes->jacobian        = A;
11819b94acceSBarry Smith   snes->jacobian_pre    = B;
11823a40ed3dSBarry Smith   PetscFunctionReturn(0);
11839b94acceSBarry Smith }
118462fef451SLois Curfman McInnes 
11855615d1e5SSatish Balay #undef __FUNC__
1186d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian"
1187b4fd4287SBarry Smith /*@
1188b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1189b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1190b4fd4287SBarry Smith 
1191c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1192c7afd0dbSLois Curfman McInnes 
1193b4fd4287SBarry Smith    Input Parameter:
1194b4fd4287SBarry Smith .  snes - the nonlinear solver context
1195b4fd4287SBarry Smith 
1196b4fd4287SBarry Smith    Output Parameters:
1197c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1198b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
1199c7afd0dbSLois Curfman McInnes -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1200fee21e36SBarry Smith 
120136851e7fSLois Curfman McInnes    Level: advanced
120236851e7fSLois Curfman McInnes 
1203b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1204b4fd4287SBarry Smith @*/
1205b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx)
1206b4fd4287SBarry Smith {
12073a40ed3dSBarry Smith   PetscFunctionBegin;
12083a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1209a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
12103a40ed3dSBarry Smith   }
1211b4fd4287SBarry Smith   if (A)   *A = snes->jacobian;
1212b4fd4287SBarry Smith   if (B)   *B = snes->jacobian_pre;
1213b4fd4287SBarry Smith   if (ctx) *ctx = snes->jacP;
12143a40ed3dSBarry Smith   PetscFunctionReturn(0);
1215b4fd4287SBarry Smith }
1216b4fd4287SBarry Smith 
12175615d1e5SSatish Balay #undef __FUNC__
1218d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian"
12199b94acceSBarry Smith /*@C
12209b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
1221044dda88SLois Curfman McInnes    location to store the matrix.
12229b94acceSBarry Smith 
1223c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1224c7afd0dbSLois Curfman McInnes 
12259b94acceSBarry Smith    Input Parameters:
1226c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12279b94acceSBarry Smith .  A - Hessian matrix
12289b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
12299b94acceSBarry Smith .  func - Jacobian evaluation routine
1230c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1231313e4042SLois Curfman McInnes          Hessian evaluation routine (may be PETSC_NULL)
12329b94acceSBarry Smith 
12339b94acceSBarry Smith    Calling sequence of func:
12348d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12359b94acceSBarry Smith 
1236c7afd0dbSLois Curfman McInnes +  x - input vector
12379b94acceSBarry Smith .  A - Hessian matrix
12389b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1239ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1240ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1241c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Hessian context
12429b94acceSBarry Smith 
12439b94acceSBarry Smith    Notes:
1244dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
12452cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1246ac21db08SLois Curfman McInnes 
12479b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
12489b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
12499b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12509b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12519b94acceSBarry Smith    throughout the global iterations.
12529b94acceSBarry Smith 
125336851e7fSLois Curfman McInnes    Level: beginner
125436851e7fSLois Curfman McInnes 
12559b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
12569b94acceSBarry Smith 
1257ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
12589b94acceSBarry Smith @*/
12599b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
12609b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
12619b94acceSBarry Smith {
12623a40ed3dSBarry Smith   PetscFunctionBegin;
126377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1264d4bb536fSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1265a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1266d4bb536fSBarry Smith   }
12679b94acceSBarry Smith   snes->computejacobian = func;
12689b94acceSBarry Smith   snes->jacP            = ctx;
12699b94acceSBarry Smith   snes->jacobian        = A;
12709b94acceSBarry Smith   snes->jacobian_pre    = B;
12713a40ed3dSBarry Smith   PetscFunctionReturn(0);
12729b94acceSBarry Smith }
12739b94acceSBarry Smith 
12745615d1e5SSatish Balay #undef __FUNC__
1275d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian"
127662fef451SLois Curfman McInnes /*@
127762fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
127862fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
127962fef451SLois Curfman McInnes 
1280c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object is parallel if SNES object is parallel
1281c7afd0dbSLois Curfman McInnes 
128262fef451SLois Curfman McInnes    Input Parameter:
128362fef451SLois Curfman McInnes .  snes - the nonlinear solver context
128462fef451SLois Curfman McInnes 
128562fef451SLois Curfman McInnes    Output Parameters:
1286c7afd0dbSLois Curfman McInnes +  A - location to stash Hessian matrix (or PETSC_NULL)
128762fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
1288c7afd0dbSLois Curfman McInnes -  ctx - location to stash Hessian ctx (or PETSC_NULL)
1289fee21e36SBarry Smith 
129036851e7fSLois Curfman McInnes    Level: advanced
129136851e7fSLois Curfman McInnes 
129262fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
1293c7afd0dbSLois Curfman McInnes 
1294c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian
129562fef451SLois Curfman McInnes @*/
129662fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx)
129762fef451SLois Curfman McInnes {
12983a40ed3dSBarry Smith   PetscFunctionBegin;
12993a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){
1300a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
13013a40ed3dSBarry Smith   }
130262fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
130362fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
130462fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
13053a40ed3dSBarry Smith   PetscFunctionReturn(0);
130662fef451SLois Curfman McInnes }
130762fef451SLois Curfman McInnes 
13089b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13099b94acceSBarry Smith 
13105615d1e5SSatish Balay #undef __FUNC__
13115615d1e5SSatish Balay #define __FUNC__ "SNESSetUp"
13129b94acceSBarry Smith /*@
13139b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1314272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13159b94acceSBarry Smith 
1316fee21e36SBarry Smith    Collective on SNES
1317fee21e36SBarry Smith 
1318c7afd0dbSLois Curfman McInnes    Input Parameters:
1319c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1320c7afd0dbSLois Curfman McInnes -  x - the solution vector
1321c7afd0dbSLois Curfman McInnes 
1322272ac6f2SLois Curfman McInnes    Notes:
1323272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1324272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1325272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1326272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1327272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1328272ac6f2SLois Curfman McInnes 
132936851e7fSLois Curfman McInnes    Level: advanced
133036851e7fSLois Curfman McInnes 
13319b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13329b94acceSBarry Smith 
13339b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
13349b94acceSBarry Smith @*/
13358ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
13369b94acceSBarry Smith {
1337272ac6f2SLois Curfman McInnes   int ierr, flg;
13383a40ed3dSBarry Smith 
13393a40ed3dSBarry Smith   PetscFunctionBegin;
134077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
134177c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
13428ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
13439b94acceSBarry Smith 
1344c1f60f51SBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);  CHKERRQ(ierr);
1345c1f60f51SBarry Smith   /*
1346c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1347dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1348c1f60f51SBarry Smith   */
1349c1f60f51SBarry Smith   if (flg) {
1350c1f60f51SBarry Smith     Mat J;
13515a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1352c1f60f51SBarry Smith     PLogObjectParent(snes,J);
1353c1f60f51SBarry Smith     snes->mfshell = J;
1354c1f60f51SBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1355c1f60f51SBarry Smith       snes->jacobian = J;
1356c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n");
1357d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
1358c1f60f51SBarry Smith       snes->jacobian = J;
1359c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n");
1360d4bb536fSBarry Smith     } else {
1361a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option");
1362d4bb536fSBarry Smith     }
13635a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1364c1f60f51SBarry Smith   }
1365272ac6f2SLois Curfman McInnes   ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);  CHKERRQ(ierr);
1366c1f60f51SBarry Smith   /*
1367dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1368c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1369c1f60f51SBarry Smith    */
137031615d04SLois Curfman McInnes   if (flg) {
1371272ac6f2SLois Curfman McInnes     Mat J;
13725a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1373272ac6f2SLois Curfman McInnes     PLogObjectParent(snes,J);
1374272ac6f2SLois Curfman McInnes     snes->mfshell = J;
137583e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
137683e56afdSLois Curfman McInnes       ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
137794a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n");
1378d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
137983e56afdSLois Curfman McInnes       ierr = SNESSetHessian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
138094a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n");
1381d4bb536fSBarry Smith     } else {
1382a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option");
1383d4bb536fSBarry Smith     }
13845a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1385272ac6f2SLois Curfman McInnes   }
13869b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
1387a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
1388a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
13895a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1390a8c6a408SBarry Smith     if (snes->vec_func == snes->vec_sol) {
1391a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector");
1392a8c6a408SBarry Smith     }
1393a703fe33SLois Curfman McInnes 
1394a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
139582bf6240SBarry Smith     if (snes->ksp_ewconv && PetscStrcmp(snes->type_name,SNES_EQ_TR)) {
1396a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1397a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
1398a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr);
13995a655dc6SBarry Smith       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes); CHKERRQ(ierr);
1400a703fe33SLois Curfman McInnes     }
14019b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
1402a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1403a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1404a8c6a408SBarry Smith     if (!snes->computeumfunction) {
1405a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first");
1406a8c6a408SBarry Smith     }
14075a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()");
1408d4bb536fSBarry Smith   } else {
1409a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class");
1410d4bb536fSBarry Smith   }
1411a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);}
141282bf6240SBarry Smith   snes->setupcalled = 1;
14133a40ed3dSBarry Smith   PetscFunctionReturn(0);
14149b94acceSBarry Smith }
14159b94acceSBarry Smith 
14165615d1e5SSatish Balay #undef __FUNC__
1417d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy"
14189b94acceSBarry Smith /*@C
14199b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
14209b94acceSBarry Smith    with SNESCreate().
14219b94acceSBarry Smith 
1422c7afd0dbSLois Curfman McInnes    Collective on SNES
1423c7afd0dbSLois Curfman McInnes 
14249b94acceSBarry Smith    Input Parameter:
14259b94acceSBarry Smith .  snes - the SNES context
14269b94acceSBarry Smith 
142736851e7fSLois Curfman McInnes    Level: beginner
142836851e7fSLois Curfman McInnes 
14299b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14309b94acceSBarry Smith 
143163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14329b94acceSBarry Smith @*/
14339b94acceSBarry Smith int SNESDestroy(SNES snes)
14349b94acceSBarry Smith {
14359b94acceSBarry Smith   int ierr;
14363a40ed3dSBarry Smith 
14373a40ed3dSBarry Smith   PetscFunctionBegin;
143877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
14393a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1440d4bb536fSBarry Smith 
1441e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes); CHKERRQ(ierr);}
14420452661fSBarry Smith   if (snes->kspconvctx) PetscFree(snes->kspconvctx);
1443522c5e43SBarry Smith   if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);}
14449b94acceSBarry Smith   ierr = SLESDestroy(snes->sles); CHKERRQ(ierr);
1445522c5e43SBarry Smith   if (snes->xmonitor) {ierr = SNESLGMonitorDestroy(snes->xmonitor);CHKERRQ(ierr);}
1446522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
14479b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
14480452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
14493a40ed3dSBarry Smith   PetscFunctionReturn(0);
14509b94acceSBarry Smith }
14519b94acceSBarry Smith 
14529b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14539b94acceSBarry Smith 
14545615d1e5SSatish Balay #undef __FUNC__
14555615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances"
14569b94acceSBarry Smith /*@
1457d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
14589b94acceSBarry Smith 
1459c7afd0dbSLois Curfman McInnes    Collective on SNES
1460c7afd0dbSLois Curfman McInnes 
14619b94acceSBarry Smith    Input Parameters:
1462c7afd0dbSLois Curfman McInnes +  snes - the SNES context
146333174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
146433174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
146533174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
146633174efeSLois Curfman McInnes            of the change in the solution between steps
146733174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1468c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1469fee21e36SBarry Smith 
147033174efeSLois Curfman McInnes    Options Database Keys:
1471c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1472c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1473c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1474c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1475c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
14769b94acceSBarry Smith 
1477d7a720efSLois Curfman McInnes    Notes:
14789b94acceSBarry Smith    The default maximum number of iterations is 50.
14799b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
14809b94acceSBarry Smith 
148136851e7fSLois Curfman McInnes    Level: intermediate
148236851e7fSLois Curfman McInnes 
148333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
14849b94acceSBarry Smith 
1485d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
14869b94acceSBarry Smith @*/
1487d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf)
14889b94acceSBarry Smith {
14893a40ed3dSBarry Smith   PetscFunctionBegin;
149077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1491d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1492d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1493d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1494d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1495d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
14963a40ed3dSBarry Smith   PetscFunctionReturn(0);
14979b94acceSBarry Smith }
14989b94acceSBarry Smith 
14995615d1e5SSatish Balay #undef __FUNC__
15005615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances"
15019b94acceSBarry Smith /*@
150233174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
150333174efeSLois Curfman McInnes 
1504c7afd0dbSLois Curfman McInnes    Not Collective
1505c7afd0dbSLois Curfman McInnes 
150633174efeSLois Curfman McInnes    Input Parameters:
1507c7afd0dbSLois Curfman McInnes +  snes - the SNES context
150833174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
150933174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
151033174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
151133174efeSLois Curfman McInnes            of the change in the solution between steps
151233174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1513c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1514fee21e36SBarry Smith 
151533174efeSLois Curfman McInnes    Notes:
151633174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
151733174efeSLois Curfman McInnes 
151836851e7fSLois Curfman McInnes    Level: intermediate
151936851e7fSLois Curfman McInnes 
152033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
152133174efeSLois Curfman McInnes 
152233174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
152333174efeSLois Curfman McInnes @*/
152433174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf)
152533174efeSLois Curfman McInnes {
15263a40ed3dSBarry Smith   PetscFunctionBegin;
152733174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
152833174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
152933174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
153033174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
153133174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
153233174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
15333a40ed3dSBarry Smith   PetscFunctionReturn(0);
153433174efeSLois Curfman McInnes }
153533174efeSLois Curfman McInnes 
15365615d1e5SSatish Balay #undef __FUNC__
15375615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance"
153833174efeSLois Curfman McInnes /*@
15399b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
15409b94acceSBarry Smith 
1541fee21e36SBarry Smith    Collective on SNES
1542fee21e36SBarry Smith 
1543c7afd0dbSLois Curfman McInnes    Input Parameters:
1544c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1545c7afd0dbSLois Curfman McInnes -  tol - tolerance
1546c7afd0dbSLois Curfman McInnes 
15479b94acceSBarry Smith    Options Database Key:
1548c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
15499b94acceSBarry Smith 
155036851e7fSLois Curfman McInnes    Level: intermediate
155136851e7fSLois Curfman McInnes 
15529b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
15539b94acceSBarry Smith 
1554d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
15559b94acceSBarry Smith @*/
15569b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol)
15579b94acceSBarry Smith {
15583a40ed3dSBarry Smith   PetscFunctionBegin;
155977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15609b94acceSBarry Smith   snes->deltatol = tol;
15613a40ed3dSBarry Smith   PetscFunctionReturn(0);
15629b94acceSBarry Smith }
15639b94acceSBarry Smith 
15645615d1e5SSatish Balay #undef __FUNC__
15655615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance"
15669b94acceSBarry Smith /*@
156777c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
15689b94acceSBarry Smith    for unconstrained minimization solvers.
15699b94acceSBarry Smith 
1570fee21e36SBarry Smith    Collective on SNES
1571fee21e36SBarry Smith 
1572c7afd0dbSLois Curfman McInnes    Input Parameters:
1573c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1574c7afd0dbSLois Curfman McInnes -  ftol - minimum function tolerance
1575c7afd0dbSLois Curfman McInnes 
15769b94acceSBarry Smith    Options Database Key:
1577c7afd0dbSLois Curfman McInnes .  -snes_fmin <ftol> - Sets ftol
15789b94acceSBarry Smith 
15799b94acceSBarry Smith    Note:
158077c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
15819b94acceSBarry Smith    methods only.
15829b94acceSBarry Smith 
158336851e7fSLois Curfman McInnes    Level: intermediate
158436851e7fSLois Curfman McInnes 
15859b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
15869b94acceSBarry Smith 
1587d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
15889b94acceSBarry Smith @*/
158977c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol)
15909b94acceSBarry Smith {
15913a40ed3dSBarry Smith   PetscFunctionBegin;
159277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15939b94acceSBarry Smith   snes->fmin = ftol;
15943a40ed3dSBarry Smith   PetscFunctionReturn(0);
15959b94acceSBarry Smith }
15969b94acceSBarry Smith 
1597ce1608b8SBarry Smith #undef __FUNC__
1598ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor"
1599ce1608b8SBarry Smith int SNESLGMonitor(SNES snes,int it,double norm,void *ctx)
1600ce1608b8SBarry Smith {
1601ce1608b8SBarry Smith   int ierr;
1602ce1608b8SBarry Smith 
1603ce1608b8SBarry Smith   PetscFunctionBegin;
1604ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1605ce1608b8SBarry Smith   PetscFunctionReturn(0);
1606ce1608b8SBarry Smith }
1607ce1608b8SBarry Smith 
16089b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
16099b94acceSBarry Smith 
16105615d1e5SSatish Balay #undef __FUNC__
1611d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor"
16129b94acceSBarry Smith /*@C
16135cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
16149b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
16159b94acceSBarry Smith    progress.
16169b94acceSBarry Smith 
1617fee21e36SBarry Smith    Collective on SNES
1618fee21e36SBarry Smith 
1619c7afd0dbSLois Curfman McInnes    Input Parameters:
1620c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1621c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1622c7afd0dbSLois Curfman McInnes -  mctx - [optional] user-defined context for private data for the
1623c7afd0dbSLois Curfman McInnes           monitor routine (may be PETSC_NULL)
16249b94acceSBarry Smith 
1625c7afd0dbSLois Curfman McInnes    Calling sequence of func:
162640a0c1c6SLois Curfman McInnes $     int func(SNES snes,int its, double norm,void *mctx)
1627c7afd0dbSLois Curfman McInnes 
1628c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1629c7afd0dbSLois Curfman McInnes .    its - iteration number
1630c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
1631c7afd0dbSLois Curfman McInnes             for SNES_NONLINEAR_EQUATIONS methods
163240a0c1c6SLois Curfman McInnes .    norm - 2-norm gradient value (may be estimated)
1633c7afd0dbSLois Curfman McInnes             for SNES_UNCONSTRAINED_MINIMIZATION methods
163440a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
16359b94acceSBarry Smith 
16369665c990SLois Curfman McInnes    Options Database Keys:
1637c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1638c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1639c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1640c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1641c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1642c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1643c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1644c7afd0dbSLois Curfman McInnes                             the options database.
16459665c990SLois Curfman McInnes 
1646639f9d9dSBarry Smith    Notes:
16476bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
16486bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
16496bc08f3fSLois Curfman McInnes    order in which they were set.
1650639f9d9dSBarry Smith 
165136851e7fSLois Curfman McInnes    Level: intermediate
165236851e7fSLois Curfman McInnes 
16539b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
16549b94acceSBarry Smith 
16555cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
16569b94acceSBarry Smith @*/
165774679c65SBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx )
16589b94acceSBarry Smith {
16593a40ed3dSBarry Smith   PetscFunctionBegin;
1660639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
1661a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set");
1662639f9d9dSBarry Smith   }
1663639f9d9dSBarry Smith 
1664639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1665639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
16663a40ed3dSBarry Smith   PetscFunctionReturn(0);
16679b94acceSBarry Smith }
16689b94acceSBarry Smith 
16695615d1e5SSatish Balay #undef __FUNC__
16705cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor"
16715cd90555SBarry Smith /*@C
16725cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
16735cd90555SBarry Smith 
1674c7afd0dbSLois Curfman McInnes    Collective on SNES
1675c7afd0dbSLois Curfman McInnes 
16765cd90555SBarry Smith    Input Parameters:
16775cd90555SBarry Smith .  snes - the SNES context
16785cd90555SBarry Smith 
16795cd90555SBarry Smith    Options Database:
1680c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1681c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1682c7afd0dbSLois Curfman McInnes     set via the options database
16835cd90555SBarry Smith 
16845cd90555SBarry Smith    Notes:
16855cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
16865cd90555SBarry Smith 
168736851e7fSLois Curfman McInnes    Level: intermediate
168836851e7fSLois Curfman McInnes 
16895cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
16905cd90555SBarry Smith 
16915cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
16925cd90555SBarry Smith @*/
16935cd90555SBarry Smith int SNESClearMonitor( SNES snes )
16945cd90555SBarry Smith {
16955cd90555SBarry Smith   PetscFunctionBegin;
16965cd90555SBarry Smith   snes->numbermonitors = 0;
16975cd90555SBarry Smith   PetscFunctionReturn(0);
16985cd90555SBarry Smith }
16995cd90555SBarry Smith 
17005cd90555SBarry Smith #undef __FUNC__
1701d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest"
17029b94acceSBarry Smith /*@C
17039b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
17049b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
17059b94acceSBarry Smith 
1706fee21e36SBarry Smith    Collective on SNES
1707fee21e36SBarry Smith 
1708c7afd0dbSLois Curfman McInnes    Input Parameters:
1709c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1710c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1711c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1712c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
17139b94acceSBarry Smith 
1714c7afd0dbSLois Curfman McInnes    Calling sequence of func:
17158d76a1e5SLois Curfman McInnes $     int func (SNES snes,double xnorm,double gnorm,double f,void *cctx)
1716c7afd0dbSLois Curfman McInnes 
1717c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1718c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1719c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1720c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods)
1721c7afd0dbSLois Curfman McInnes .    f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods)
1722c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods)
1723c7afd0dbSLois Curfman McInnes -    f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods)
17249b94acceSBarry Smith 
172536851e7fSLois Curfman McInnes    Level: advanced
172636851e7fSLois Curfman McInnes 
17279b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
17289b94acceSBarry Smith 
172940191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
173040191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
17319b94acceSBarry Smith @*/
173274679c65SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,void*),void *cctx)
17339b94acceSBarry Smith {
17343a40ed3dSBarry Smith   PetscFunctionBegin;
17359b94acceSBarry Smith   (snes)->converged = func;
17369b94acceSBarry Smith   (snes)->cnvP      = cctx;
17373a40ed3dSBarry Smith   PetscFunctionReturn(0);
17389b94acceSBarry Smith }
17399b94acceSBarry Smith 
17405615d1e5SSatish Balay #undef __FUNC__
1741d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory"
1742c9005455SLois Curfman McInnes /*@
1743c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1744c9005455SLois Curfman McInnes 
1745fee21e36SBarry Smith    Collective on SNES
1746fee21e36SBarry Smith 
1747c7afd0dbSLois Curfman McInnes    Input Parameters:
1748c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1749c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1750758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1751758f92a0SBarry Smith          negative if not converged) for each solve.
1752758f92a0SBarry Smith .  na  - size of a and its
1753758f92a0SBarry Smith -  reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero,
1754758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1755c7afd0dbSLois Curfman McInnes 
1756c9005455SLois Curfman McInnes    Notes:
1757c9005455SLois Curfman McInnes    If set, this array will contain the function norms (for
1758c9005455SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods) or gradient norms
1759c9005455SLois Curfman McInnes    (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed
1760c9005455SLois Curfman McInnes    at each step.
1761c9005455SLois Curfman McInnes 
1762c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1763c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1764c9005455SLois Curfman McInnes    during the section of code that is being timed.
1765c9005455SLois Curfman McInnes 
176636851e7fSLois Curfman McInnes    Level: intermediate
176736851e7fSLois Curfman McInnes 
1768c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1769758f92a0SBarry Smith 
177008405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1771758f92a0SBarry Smith 
1772c9005455SLois Curfman McInnes @*/
1773758f92a0SBarry Smith int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset)
1774c9005455SLois Curfman McInnes {
17753a40ed3dSBarry Smith   PetscFunctionBegin;
1776c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1777c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1778c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1779758f92a0SBarry Smith   snes->conv_hist_its   = its;
1780758f92a0SBarry Smith   snes->conv_hist_max   = na;
1781758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1782758f92a0SBarry Smith   PetscFunctionReturn(0);
1783758f92a0SBarry Smith }
1784758f92a0SBarry Smith 
1785758f92a0SBarry Smith #undef __FUNC__
1786758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory"
17870c4c9dddSBarry Smith /*@C
1788758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1789758f92a0SBarry Smith 
1790758f92a0SBarry Smith    Collective on SNES
1791758f92a0SBarry Smith 
1792758f92a0SBarry Smith    Input Parameter:
1793758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1794758f92a0SBarry Smith 
1795758f92a0SBarry Smith    Output Parameters:
1796758f92a0SBarry Smith .  a   - array to hold history
1797758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1798758f92a0SBarry Smith          negative if not converged) for each solve.
1799758f92a0SBarry Smith -  na  - size of a and its
1800758f92a0SBarry Smith 
1801758f92a0SBarry Smith    Notes:
1802758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1803758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1804758f92a0SBarry Smith 
1805758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1806758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1807758f92a0SBarry Smith    during the section of code that is being timed.
1808758f92a0SBarry Smith 
1809758f92a0SBarry Smith    Level: intermediate
1810758f92a0SBarry Smith 
1811758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1812758f92a0SBarry Smith 
1813758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1814758f92a0SBarry Smith 
1815758f92a0SBarry Smith @*/
1816758f92a0SBarry Smith int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na)
1817758f92a0SBarry Smith {
1818758f92a0SBarry Smith   PetscFunctionBegin;
1819758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1820758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1821758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1822758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
18233a40ed3dSBarry Smith   PetscFunctionReturn(0);
1824c9005455SLois Curfman McInnes }
1825c9005455SLois Curfman McInnes 
1826c9005455SLois Curfman McInnes #undef __FUNC__
18275615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private"
18289b94acceSBarry Smith /*
18299b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
18309b94acceSBarry Smith    positive parameter delta.
18319b94acceSBarry Smith 
18329b94acceSBarry Smith     Input Parameters:
1833c7afd0dbSLois Curfman McInnes +   snes - the SNES context
18349b94acceSBarry Smith .   y - approximate solution of linear system
18359b94acceSBarry Smith .   fnorm - 2-norm of current function
1836c7afd0dbSLois Curfman McInnes -   delta - trust region size
18379b94acceSBarry Smith 
18389b94acceSBarry Smith     Output Parameters:
1839c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
18409b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
18419b94acceSBarry Smith     region, and exceeds zero otherwise.
1842c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
18439b94acceSBarry Smith 
18449b94acceSBarry Smith     Note:
184540191667SLois Curfman McInnes     For non-trust region methods such as SNES_EQ_LS, the parameter delta
18469b94acceSBarry Smith     is set to be the maximum allowable step size.
18479b94acceSBarry Smith 
18489b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
18499b94acceSBarry Smith */
18509b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta,
18519b94acceSBarry Smith                           double *gpnorm,double *ynorm)
18529b94acceSBarry Smith {
18539b94acceSBarry Smith   double norm;
18549b94acceSBarry Smith   Scalar cnorm;
18553a40ed3dSBarry Smith   int    ierr;
18563a40ed3dSBarry Smith 
18573a40ed3dSBarry Smith   PetscFunctionBegin;
18583a40ed3dSBarry Smith   ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr);
18599b94acceSBarry Smith   if (norm > *delta) {
18609b94acceSBarry Smith      norm = *delta/norm;
18619b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
18629b94acceSBarry Smith      cnorm = norm;
18639b94acceSBarry Smith      VecScale( &cnorm, y );
18649b94acceSBarry Smith      *ynorm = *delta;
18659b94acceSBarry Smith   } else {
18669b94acceSBarry Smith      *gpnorm = 0.0;
18679b94acceSBarry Smith      *ynorm = norm;
18689b94acceSBarry Smith   }
18693a40ed3dSBarry Smith   PetscFunctionReturn(0);
18709b94acceSBarry Smith }
18719b94acceSBarry Smith 
18725615d1e5SSatish Balay #undef __FUNC__
18735615d1e5SSatish Balay #define __FUNC__ "SNESSolve"
18749b94acceSBarry Smith /*@
18759b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
187663a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
18779b94acceSBarry Smith 
1878c7afd0dbSLois Curfman McInnes    Collective on SNES
1879c7afd0dbSLois Curfman McInnes 
1880b2002411SLois Curfman McInnes    Input Parameters:
1881c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1882c7afd0dbSLois Curfman McInnes -  x - the solution vector
18839b94acceSBarry Smith 
18849b94acceSBarry Smith    Output Parameter:
1885b2002411SLois Curfman McInnes .  its - number of iterations until termination
18869b94acceSBarry Smith 
1887b2002411SLois Curfman McInnes    Notes:
18888ddd3da0SLois Curfman McInnes    The user should initialize the vector, x, with the initial guess
18898ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
18908ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
18918ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
18928ddd3da0SLois Curfman McInnes 
189336851e7fSLois Curfman McInnes    Level: beginner
189436851e7fSLois Curfman McInnes 
18959b94acceSBarry Smith .keywords: SNES, nonlinear, solve
18969b94acceSBarry Smith 
189763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
18989b94acceSBarry Smith @*/
18998ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
19009b94acceSBarry Smith {
19013c7409f5SSatish Balay   int ierr, flg;
1902052efed2SBarry Smith 
19033a40ed3dSBarry Smith   PetscFunctionBegin;
190477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
190574679c65SBarry Smith   PetscValidIntPointer(its);
190682bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);}
1907c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
1908758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
19099b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
1910c96a6f78SLois Curfman McInnes   snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0;
19119b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr);
19129b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
19133c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr);
19146d4a8577SBarry Smith   if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD); CHKERRQ(ierr); }
19153a40ed3dSBarry Smith   PetscFunctionReturn(0);
19169b94acceSBarry Smith }
19179b94acceSBarry Smith 
19189b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
19199b94acceSBarry Smith 
19205615d1e5SSatish Balay #undef __FUNC__
19215615d1e5SSatish Balay #define __FUNC__ "SNESSetType"
192282bf6240SBarry Smith /*@C
19234b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
19249b94acceSBarry Smith 
1925fee21e36SBarry Smith    Collective on SNES
1926fee21e36SBarry Smith 
1927c7afd0dbSLois Curfman McInnes    Input Parameters:
1928c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1929c7afd0dbSLois Curfman McInnes -  method - a known method
1930c7afd0dbSLois Curfman McInnes 
1931c7afd0dbSLois Curfman McInnes    Options Database Key:
1932c7afd0dbSLois Curfman McInnes .  -snes_type <method> - Sets the method; use -help for a list
1933c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1934ae12b187SLois Curfman McInnes 
19359b94acceSBarry Smith    Notes:
19369b94acceSBarry Smith    See "petsc/include/snes.h" for available methods (for instance)
193736851e7fSLois Curfman McInnes +    SNES_EQ_LS - Newton's method with line search
1938c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
1939c7afd0dbSLois Curfman McInnes .    SNES_EQ_TR - Newton's method with trust region
1940c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
1941c7afd0dbSLois Curfman McInnes .    SNES_UM_TR - Newton's method with trust region
1942c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
194336851e7fSLois Curfman McInnes -    SNES_UM_LS - Newton's method with line search
1944c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
19459b94acceSBarry Smith 
1946ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1947ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1948ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1949ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1950ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1951ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1952ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1953ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1954ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
195536851e7fSLois Curfman McInnes   appropriate method.  In other words, this routine is not for beginners.
195636851e7fSLois Curfman McInnes 
195736851e7fSLois Curfman McInnes   Level: intermediate
1958a703fe33SLois Curfman McInnes 
1959f536c699SSatish Balay .keywords: SNES, set, method
19609b94acceSBarry Smith @*/
19614b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method)
19629b94acceSBarry Smith {
196384cb2905SBarry Smith   int ierr;
19649b94acceSBarry Smith   int (*r)(SNES);
19653a40ed3dSBarry Smith 
19663a40ed3dSBarry Smith   PetscFunctionBegin;
196777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
196882bf6240SBarry Smith 
19693f1db9ecSBarry Smith   if (PetscTypeCompare(snes->type_name,method)) PetscFunctionReturn(0);
197082bf6240SBarry Smith 
197182bf6240SBarry Smith   if (snes->setupcalled) {
1972e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
197382bf6240SBarry Smith     snes->data = 0;
197482bf6240SBarry Smith   }
19757d1a2b2bSBarry Smith 
19769b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
197782bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
197882bf6240SBarry Smith 
1979488ecbafSBarry Smith   ierr =  FListFind(snes->comm, SNESList, method,(int (**)(void *)) &r );CHKERRQ(ierr);
198082bf6240SBarry Smith 
1981596552b5SBarry Smith   if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",method);
19821548b14aSBarry Smith 
19830452661fSBarry Smith   if (snes->data) PetscFree(snes->data);
198482bf6240SBarry Smith   snes->data = 0;
19853a40ed3dSBarry Smith   ierr = (*r)(snes); CHKERRQ(ierr);
198682bf6240SBarry Smith 
198782bf6240SBarry Smith   if (snes->type_name) PetscFree(snes->type_name);
198882bf6240SBarry Smith   snes->type_name = (char *) PetscMalloc((PetscStrlen(method)+1)*sizeof(char));CHKPTRQ(snes->type_name);
198982bf6240SBarry Smith   PetscStrcpy(snes->type_name,method);
199082bf6240SBarry Smith   snes->set_method_called = 1;
199182bf6240SBarry Smith 
19923a40ed3dSBarry Smith   PetscFunctionReturn(0);
19939b94acceSBarry Smith }
19949b94acceSBarry Smith 
1995a847f771SSatish Balay 
19969b94acceSBarry Smith /* --------------------------------------------------------------------- */
19975615d1e5SSatish Balay #undef __FUNC__
1998d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy"
19999b94acceSBarry Smith /*@C
20009b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
20019b94acceSBarry Smith    registered by SNESRegister().
20029b94acceSBarry Smith 
2003fee21e36SBarry Smith    Not Collective
2004fee21e36SBarry Smith 
200536851e7fSLois Curfman McInnes    Level: advanced
200636851e7fSLois Curfman McInnes 
20079b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
20089b94acceSBarry Smith 
20099b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
20109b94acceSBarry Smith @*/
2011cf256101SBarry Smith int SNESRegisterDestroy(void)
20129b94acceSBarry Smith {
201382bf6240SBarry Smith   int ierr;
201482bf6240SBarry Smith 
20153a40ed3dSBarry Smith   PetscFunctionBegin;
201682bf6240SBarry Smith   if (SNESList) {
2017488ecbafSBarry Smith     ierr = FListDestroy( SNESList );CHKERRQ(ierr);
201882bf6240SBarry Smith     SNESList = 0;
20199b94acceSBarry Smith   }
202084cb2905SBarry Smith   SNESRegisterAllCalled = 0;
20213a40ed3dSBarry Smith   PetscFunctionReturn(0);
20229b94acceSBarry Smith }
20239b94acceSBarry Smith 
20245615d1e5SSatish Balay #undef __FUNC__
2025d4bb536fSBarry Smith #define __FUNC__ "SNESGetType"
20269b94acceSBarry Smith /*@C
20279a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
20289b94acceSBarry Smith 
2029c7afd0dbSLois Curfman McInnes    Not Collective
2030c7afd0dbSLois Curfman McInnes 
20319b94acceSBarry Smith    Input Parameter:
20324b0e389bSBarry Smith .  snes - nonlinear solver context
20339b94acceSBarry Smith 
20349b94acceSBarry Smith    Output Parameter:
203582bf6240SBarry Smith .  method - SNES method (a charactor string)
20369b94acceSBarry Smith 
203736851e7fSLois Curfman McInnes    Level: intermediate
203836851e7fSLois Curfman McInnes 
20399b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name
20409b94acceSBarry Smith @*/
204182bf6240SBarry Smith int SNESGetType(SNES snes, SNESType *method)
20429b94acceSBarry Smith {
20433a40ed3dSBarry Smith   PetscFunctionBegin;
204482bf6240SBarry Smith   *method = snes->type_name;
20453a40ed3dSBarry Smith   PetscFunctionReturn(0);
20469b94acceSBarry Smith }
20479b94acceSBarry Smith 
20485615d1e5SSatish Balay #undef __FUNC__
2049d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution"
20509b94acceSBarry Smith /*@C
20519b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20529b94acceSBarry Smith    stored.
20539b94acceSBarry Smith 
2054c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2055c7afd0dbSLois Curfman McInnes 
20569b94acceSBarry Smith    Input Parameter:
20579b94acceSBarry Smith .  snes - the SNES context
20589b94acceSBarry Smith 
20599b94acceSBarry Smith    Output Parameter:
20609b94acceSBarry Smith .  x - the solution
20619b94acceSBarry Smith 
206236851e7fSLois Curfman McInnes    Level: advanced
206336851e7fSLois Curfman McInnes 
20649b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20659b94acceSBarry Smith 
2066a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
20679b94acceSBarry Smith @*/
20689b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
20699b94acceSBarry Smith {
20703a40ed3dSBarry Smith   PetscFunctionBegin;
207177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
20729b94acceSBarry Smith   *x = snes->vec_sol_always;
20733a40ed3dSBarry Smith   PetscFunctionReturn(0);
20749b94acceSBarry Smith }
20759b94acceSBarry Smith 
20765615d1e5SSatish Balay #undef __FUNC__
2077d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate"
20789b94acceSBarry Smith /*@C
20799b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
20809b94acceSBarry Smith    stored.
20819b94acceSBarry Smith 
2082c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2083c7afd0dbSLois Curfman McInnes 
20849b94acceSBarry Smith    Input Parameter:
20859b94acceSBarry Smith .  snes - the SNES context
20869b94acceSBarry Smith 
20879b94acceSBarry Smith    Output Parameter:
20889b94acceSBarry Smith .  x - the solution update
20899b94acceSBarry Smith 
209036851e7fSLois Curfman McInnes    Level: advanced
209136851e7fSLois Curfman McInnes 
20929b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
20939b94acceSBarry Smith 
20949b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
20959b94acceSBarry Smith @*/
20969b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
20979b94acceSBarry Smith {
20983a40ed3dSBarry Smith   PetscFunctionBegin;
209977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21009b94acceSBarry Smith   *x = snes->vec_sol_update_always;
21013a40ed3dSBarry Smith   PetscFunctionReturn(0);
21029b94acceSBarry Smith }
21039b94acceSBarry Smith 
21045615d1e5SSatish Balay #undef __FUNC__
2105d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction"
21069b94acceSBarry Smith /*@C
21073638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
21089b94acceSBarry Smith 
2109c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2110c7afd0dbSLois Curfman McInnes 
21119b94acceSBarry Smith    Input Parameter:
21129b94acceSBarry Smith .  snes - the SNES context
21139b94acceSBarry Smith 
21149b94acceSBarry Smith    Output Parameter:
21153638b69dSLois Curfman McInnes .  r - the function
21169b94acceSBarry Smith 
21179b94acceSBarry Smith    Notes:
21189b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
21199b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
21209b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
21219b94acceSBarry Smith 
212236851e7fSLois Curfman McInnes    Level: advanced
212336851e7fSLois Curfman McInnes 
2124a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
21259b94acceSBarry Smith 
212631615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
212731615d04SLois Curfman McInnes           SNESGetGradient()
21289b94acceSBarry Smith @*/
21299b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r)
21309b94acceSBarry Smith {
21313a40ed3dSBarry Smith   PetscFunctionBegin;
213277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2133a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
2134a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
2135a8c6a408SBarry Smith   }
21369b94acceSBarry Smith   *r = snes->vec_func_always;
21373a40ed3dSBarry Smith   PetscFunctionReturn(0);
21389b94acceSBarry Smith }
21399b94acceSBarry Smith 
21405615d1e5SSatish Balay #undef __FUNC__
2141d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient"
21429b94acceSBarry Smith /*@C
21433638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
21449b94acceSBarry Smith 
2145c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2146c7afd0dbSLois Curfman McInnes 
21479b94acceSBarry Smith    Input Parameter:
21489b94acceSBarry Smith .  snes - the SNES context
21499b94acceSBarry Smith 
21509b94acceSBarry Smith    Output Parameter:
21519b94acceSBarry Smith .  r - the gradient
21529b94acceSBarry Smith 
21539b94acceSBarry Smith    Notes:
21549b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
21559b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
21569b94acceSBarry Smith    SNESGetFunction().
21579b94acceSBarry Smith 
215836851e7fSLois Curfman McInnes    Level: advanced
215936851e7fSLois Curfman McInnes 
21609b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
21619b94acceSBarry Smith 
216231615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction()
21639b94acceSBarry Smith @*/
21649b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r)
21659b94acceSBarry Smith {
21663a40ed3dSBarry Smith   PetscFunctionBegin;
216777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21683a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2169a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
21703a40ed3dSBarry Smith   }
21719b94acceSBarry Smith   *r = snes->vec_func_always;
21723a40ed3dSBarry Smith   PetscFunctionReturn(0);
21739b94acceSBarry Smith }
21749b94acceSBarry Smith 
21755615d1e5SSatish Balay #undef __FUNC__
2176d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction"
21779b94acceSBarry Smith /*@
21789b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
21799b94acceSBarry Smith    unconstrained minimization problems.
21809b94acceSBarry Smith 
2181c7afd0dbSLois Curfman McInnes    Not Collective
2182c7afd0dbSLois Curfman McInnes 
21839b94acceSBarry Smith    Input Parameter:
21849b94acceSBarry Smith .  snes - the SNES context
21859b94acceSBarry Smith 
21869b94acceSBarry Smith    Output Parameter:
21879b94acceSBarry Smith .  r - the function
21889b94acceSBarry Smith 
21899b94acceSBarry Smith    Notes:
21909b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
21919b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
21929b94acceSBarry Smith    SNESGetFunction().
21939b94acceSBarry Smith 
219436851e7fSLois Curfman McInnes    Level: advanced
219536851e7fSLois Curfman McInnes 
21969b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
21979b94acceSBarry Smith 
219831615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction()
21999b94acceSBarry Smith @*/
22009b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r)
22019b94acceSBarry Smith {
22023a40ed3dSBarry Smith   PetscFunctionBegin;
220377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
220474679c65SBarry Smith   PetscValidScalarPointer(r);
22053a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2206a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
22073a40ed3dSBarry Smith   }
22089b94acceSBarry Smith   *r = snes->fc;
22093a40ed3dSBarry Smith   PetscFunctionReturn(0);
22109b94acceSBarry Smith }
22119b94acceSBarry Smith 
22125615d1e5SSatish Balay #undef __FUNC__
2213d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix"
22143c7409f5SSatish Balay /*@C
22153c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2216d850072dSLois Curfman McInnes    SNES options in the database.
22173c7409f5SSatish Balay 
2218fee21e36SBarry Smith    Collective on SNES
2219fee21e36SBarry Smith 
2220c7afd0dbSLois Curfman McInnes    Input Parameter:
2221c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2222c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2223c7afd0dbSLois Curfman McInnes 
2224d850072dSLois Curfman McInnes    Notes:
2225a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2226c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2227d850072dSLois Curfman McInnes 
222836851e7fSLois Curfman McInnes    Level: advanced
222936851e7fSLois Curfman McInnes 
22303c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2231a86d99e1SLois Curfman McInnes 
2232a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
22333c7409f5SSatish Balay @*/
22343c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
22353c7409f5SSatish Balay {
22363c7409f5SSatish Balay   int ierr;
22373c7409f5SSatish Balay 
22383a40ed3dSBarry Smith   PetscFunctionBegin;
223977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2240639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
22413c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
22423a40ed3dSBarry Smith   PetscFunctionReturn(0);
22433c7409f5SSatish Balay }
22443c7409f5SSatish Balay 
22455615d1e5SSatish Balay #undef __FUNC__
2246d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix"
22473c7409f5SSatish Balay /*@C
2248f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2249d850072dSLois Curfman McInnes    SNES options in the database.
22503c7409f5SSatish Balay 
2251fee21e36SBarry Smith    Collective on SNES
2252fee21e36SBarry Smith 
2253c7afd0dbSLois Curfman McInnes    Input Parameters:
2254c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2255c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2256c7afd0dbSLois Curfman McInnes 
2257d850072dSLois Curfman McInnes    Notes:
2258a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2259c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2260d850072dSLois Curfman McInnes 
226136851e7fSLois Curfman McInnes    Level: advanced
226236851e7fSLois Curfman McInnes 
22633c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2264a86d99e1SLois Curfman McInnes 
2265a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
22663c7409f5SSatish Balay @*/
22673c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
22683c7409f5SSatish Balay {
22693c7409f5SSatish Balay   int ierr;
22703c7409f5SSatish Balay 
22713a40ed3dSBarry Smith   PetscFunctionBegin;
227277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2273639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
22743c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
22753a40ed3dSBarry Smith   PetscFunctionReturn(0);
22763c7409f5SSatish Balay }
22773c7409f5SSatish Balay 
22785615d1e5SSatish Balay #undef __FUNC__
2279d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix"
22809ab63eb5SSatish Balay /*@C
22813c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
22823c7409f5SSatish Balay    SNES options in the database.
22833c7409f5SSatish Balay 
2284c7afd0dbSLois Curfman McInnes    Not Collective
2285c7afd0dbSLois Curfman McInnes 
22863c7409f5SSatish Balay    Input Parameter:
22873c7409f5SSatish Balay .  snes - the SNES context
22883c7409f5SSatish Balay 
22893c7409f5SSatish Balay    Output Parameter:
22903c7409f5SSatish Balay .  prefix - pointer to the prefix string used
22913c7409f5SSatish Balay 
22929ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
22939ab63eb5SSatish Balay    sufficient length to hold the prefix.
22949ab63eb5SSatish Balay 
229536851e7fSLois Curfman McInnes    Level: advanced
229636851e7fSLois Curfman McInnes 
22973c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2298a86d99e1SLois Curfman McInnes 
2299a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
23003c7409f5SSatish Balay @*/
23013c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
23023c7409f5SSatish Balay {
23033c7409f5SSatish Balay   int ierr;
23043c7409f5SSatish Balay 
23053a40ed3dSBarry Smith   PetscFunctionBegin;
230677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2307639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
23083a40ed3dSBarry Smith   PetscFunctionReturn(0);
23093c7409f5SSatish Balay }
23103c7409f5SSatish Balay 
2311ca161407SBarry Smith #undef __FUNC__
2312ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp"
2313ca161407SBarry Smith /*@
2314ca161407SBarry Smith    SNESPrintHelp - Prints all options for the SNES component.
2315ca161407SBarry Smith 
2316c7afd0dbSLois Curfman McInnes    Collective on SNES
2317c7afd0dbSLois Curfman McInnes 
2318ca161407SBarry Smith    Input Parameter:
2319ca161407SBarry Smith .  snes - the SNES context
2320ca161407SBarry Smith 
2321ca161407SBarry Smith    Options Database Keys:
2322c7afd0dbSLois Curfman McInnes +  -help - Prints SNES options
2323c7afd0dbSLois Curfman McInnes -  -h - Prints SNES options
2324ca161407SBarry Smith 
232536851e7fSLois Curfman McInnes    Level: beginner
232636851e7fSLois Curfman McInnes 
2327ca161407SBarry Smith .keywords: SNES, nonlinear, help
2328ca161407SBarry Smith 
2329ca161407SBarry Smith .seealso: SNESSetFromOptions()
2330ca161407SBarry Smith @*/
2331ca161407SBarry Smith int SNESPrintHelp(SNES snes)
2332ca161407SBarry Smith {
2333ca161407SBarry Smith   char                p[64];
2334ca161407SBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
2335ca161407SBarry Smith   int                 ierr;
2336ca161407SBarry Smith 
2337ca161407SBarry Smith   PetscFunctionBegin;
2338ca161407SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2339ca161407SBarry Smith 
2340ca161407SBarry Smith   PetscStrcpy(p,"-");
2341ca161407SBarry Smith   if (snes->prefix) PetscStrcat(p, snes->prefix);
2342ca161407SBarry Smith 
2343ca161407SBarry Smith   kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
2344ca161407SBarry Smith 
2345ebb8b11fSBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
2346*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr);
2347488ecbafSBarry Smith   ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr);
2348*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr);
2349*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr);
2350*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr);
2351*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr);
2352*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr);
2353*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr);
2354*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr);
2355*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr);
2356*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr);
2357*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_monitor: use default SNES convergence monitor, prints\n\
2358*d132466eSBarry Smith     residual norm at each iteration.\n",p);CHKERRQ(ierr);
2359*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_smonitor: same as the above, but prints fewer digits of the\n\
2360ca161407SBarry Smith     residual norm for small residual norms. This is useful to conceal\n\
2361*d132466eSBarry Smith     meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr);
2362*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr);
2363*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr);
2364*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr);
2365ca161407SBarry Smith   if (snes->type == SNES_NONLINEAR_EQUATIONS) {
2366*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2367*d132466eSBarry Smith      " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr);
2368*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr);
2369*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr);
2370*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr);
2371*d132466eSBarry 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);
2372*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr);
2373*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr);
2374*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2375*d132466eSBarry Smith      "     %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr);
2376*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2377*d132466eSBarry Smith      "     %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr);
2378*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2379*d132466eSBarry Smith      "     %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr);
2380*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2381*d132466eSBarry Smith      "     %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr);
2382*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2383*d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr);
2384*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2385*d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr);
2386*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2387*d132466eSBarry Smith      "     %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr);
2388ca161407SBarry Smith   } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) {
2389*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr);
2390*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr);
2391*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr);
2392*d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr);
2393ca161407SBarry Smith   }
2394*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <method> for help on ",p);CHKERRQ(ierr);
2395*d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr);
2396ca161407SBarry Smith   if (snes->printhelp) {
2397ca161407SBarry Smith     ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr);
2398ca161407SBarry Smith   }
2399ca161407SBarry Smith   PetscFunctionReturn(0);
2400ca161407SBarry Smith }
24013c7409f5SSatish Balay 
2402acb85ae6SSatish Balay /*MC
2403b2002411SLois Curfman McInnes    SNESRegister - Adds a method to the nonlinear solver package.
24049b94acceSBarry Smith 
2405b2002411SLois Curfman McInnes    Synopsis:
2406b2002411SLois Curfman McInnes    SNESRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
24079b94acceSBarry Smith 
24088d76a1e5SLois Curfman McInnes    Not collective
24098d76a1e5SLois Curfman McInnes 
2410b2002411SLois Curfman McInnes    Input Parameters:
2411c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2412b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2413b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2414c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
24159b94acceSBarry Smith 
2416b2002411SLois Curfman McInnes    Notes:
2417b2002411SLois Curfman McInnes    SNESRegister() may be called multiple times to add several user-defined solvers.
24183a40ed3dSBarry Smith 
2419b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2420b2002411SLois Curfman McInnes    is ignored.
2421b2002411SLois Curfman McInnes 
2422b2002411SLois Curfman McInnes    Sample usage:
242369225d78SLois Curfman McInnes .vb
2424b2002411SLois Curfman McInnes    SNESRegister("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2425b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
242669225d78SLois Curfman McInnes .ve
2427b2002411SLois Curfman McInnes 
2428b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2429b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2430b2002411SLois Curfman McInnes    or at runtime via the option
2431b2002411SLois Curfman McInnes $     -snes_type my_solver
2432b2002411SLois Curfman McInnes 
243336851e7fSLois Curfman McInnes    Level: advanced
243436851e7fSLois Curfman McInnes 
2435dd438238SBarry Smith    $PETSC_ARCH and $BOPT occuring in pathname will be replaced with appropriate values.
2436dd438238SBarry Smith 
2437b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2438b2002411SLois Curfman McInnes 
2439b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2440b2002411SLois Curfman McInnes M*/
2441b2002411SLois Curfman McInnes 
2442b2002411SLois Curfman McInnes #undef __FUNC__
2443b2002411SLois Curfman McInnes #define __FUNC__ "SNESRegister_Private"
2444b2002411SLois Curfman McInnes int SNESRegister_Private(char *sname,char *path,char *name,int (*function)(SNES))
2445b2002411SLois Curfman McInnes {
2446b2002411SLois Curfman McInnes   char fullname[256];
2447b2002411SLois Curfman McInnes   int  ierr;
2448b2002411SLois Curfman McInnes 
2449b2002411SLois Curfman McInnes   PetscFunctionBegin;
2450b2002411SLois Curfman McInnes   PetscStrcpy(fullname,path); PetscStrcat(fullname,":");PetscStrcat(fullname,name);
2451488ecbafSBarry Smith   ierr = FListAdd_Private(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr);
2452b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2453b2002411SLois Curfman McInnes }
2454