xref: /petsc/src/snes/interface/snes.c (revision 43d6d2cb785b30fc1a49e69d0bea9e51104b4e18)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*43d6d2cbSBarry Smith static char vcid[] = "$Id: snes.c,v 1.193 1999/09/20 18:20:17 bsmith Exp bsmith $";
39b94acceSBarry Smith #endif
49b94acceSBarry Smith 
570f55243SBarry Smith #include "src/snes/snesimpl.h"      /*I "snes.h"  I*/
69b94acceSBarry Smith 
784cb2905SBarry Smith int SNESRegisterAllCalled = 0;
8488ecbafSBarry Smith FList SNESList = 0;
982bf6240SBarry Smith 
105615d1e5SSatish Balay #undef __FUNC__
11d4bb536fSBarry Smith #define __FUNC__ "SNESView"
129b94acceSBarry Smith /*@
139b94acceSBarry Smith    SNESView - Prints the SNES data structure.
149b94acceSBarry Smith 
15fee21e36SBarry Smith    Collective on SNES, unless Viewer is VIEWER_STDOUT_SELF
16fee21e36SBarry Smith 
17c7afd0dbSLois Curfman McInnes    Input Parameters:
18c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
19c7afd0dbSLois Curfman McInnes -  viewer - visualization context
20c7afd0dbSLois Curfman McInnes 
219b94acceSBarry Smith    Options Database Key:
22c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
239b94acceSBarry Smith 
249b94acceSBarry Smith    Notes:
259b94acceSBarry Smith    The available visualization contexts include
26c7afd0dbSLois Curfman McInnes +     VIEWER_STDOUT_SELF - standard output (default)
27c7afd0dbSLois Curfman McInnes -     VIEWER_STDOUT_WORLD - synchronized standard
28c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
29c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
30c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
319b94acceSBarry Smith 
323e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
3377ed5343SBarry Smith    ViewerASCIIOpen() - output to a specified file.
349b94acceSBarry Smith 
3536851e7fSLois Curfman McInnes    Level: beginner
3636851e7fSLois Curfman McInnes 
379b94acceSBarry Smith .keywords: SNES, view
389b94acceSBarry Smith 
3977ed5343SBarry Smith .seealso: ViewerASCIIOpen()
409b94acceSBarry Smith @*/
419b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer)
429b94acceSBarry Smith {
439b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
449b94acceSBarry Smith   int                 ierr;
459b94acceSBarry Smith   SLES                sles;
469b94acceSBarry Smith   char                *method;
4719bcc07fSBarry Smith   ViewerType          vtype;
489b94acceSBarry Smith 
493a40ed3dSBarry Smith   PetscFunctionBegin;
5074679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5174679c65SBarry Smith   if (viewer) {PetscValidHeader(viewer);}
5274679c65SBarry Smith   else { viewer = VIEWER_STDOUT_SELF; }
5374679c65SBarry Smith 
5419bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype);CHKERRQ(ierr);
553f1db9ecSBarry Smith   if (PetscTypeCompare(vtype,ASCII_VIEWER)) {
56d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
57d132466eSBarry Smith     ierr = SNESGetType(snes,&method);CHKERRQ(ierr);
58d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  method: %s\n",method);CHKERRQ(ierr);
590ef38995SBarry Smith     if (snes->view) {
600ef38995SBarry Smith       ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
610ef38995SBarry Smith       ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr);
620ef38995SBarry Smith       ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
630ef38995SBarry Smith     }
64d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
65d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n",
66d132466eSBarry Smith                  snes->rtol, snes->atol, snes->trunctol, snes->xtol);CHKERRQ(ierr);
67d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
68d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
690ef38995SBarry Smith     if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
70d132466eSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"  min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr);
710ef38995SBarry Smith     }
729b94acceSBarry Smith     if (snes->ksp_ewconv) {
739b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
749b94acceSBarry Smith       if (kctx) {
75d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
76d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
77d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"    gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
789b94acceSBarry Smith       }
799b94acceSBarry Smith     }
803f1db9ecSBarry Smith   } else if (PetscTypeCompare(vtype,STRING_VIEWER)) {
810ef38995SBarry Smith     ierr = SNESGetType(snes,&method);CHKERRQ(ierr);
820ef38995SBarry Smith     ierr = ViewerStringSPrintf(viewer," %-3.3s",method);CHKERRQ(ierr);
8319bcc07fSBarry Smith   }
8477ed5343SBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
850ef38995SBarry Smith   ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
869b94acceSBarry Smith   ierr = SLESView(sles,viewer);CHKERRQ(ierr);
870ef38995SBarry Smith   ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
883a40ed3dSBarry Smith   PetscFunctionReturn(0);
899b94acceSBarry Smith }
909b94acceSBarry Smith 
91639f9d9dSBarry Smith /*
92639f9d9dSBarry Smith        We retain a list of functions that also take SNES command
93639f9d9dSBarry Smith     line options. These are called at the end SNESSetFromOptions()
94639f9d9dSBarry Smith */
95639f9d9dSBarry Smith #define MAXSETFROMOPTIONS 5
96639f9d9dSBarry Smith static int numberofsetfromoptions;
97639f9d9dSBarry Smith static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
98639f9d9dSBarry Smith 
995615d1e5SSatish Balay #undef __FUNC__
100d4bb536fSBarry Smith #define __FUNC__ "SNESAddOptionsChecker"
101639f9d9dSBarry Smith /*@
102639f9d9dSBarry Smith     SNESAddOptionsChecker - Adds an additional function to check for SNES options.
103639f9d9dSBarry Smith 
104c7afd0dbSLois Curfman McInnes     Not Collective
105c7afd0dbSLois Curfman McInnes 
106639f9d9dSBarry Smith     Input Parameter:
107639f9d9dSBarry Smith .   snescheck - function that checks for options
108639f9d9dSBarry Smith 
10936851e7fSLois Curfman McInnes     Level: developer
11036851e7fSLois Curfman McInnes 
111639f9d9dSBarry Smith .seealso: SNESSetFromOptions()
112639f9d9dSBarry Smith @*/
113639f9d9dSBarry Smith int SNESAddOptionsChecker(int (*snescheck)(SNES) )
114639f9d9dSBarry Smith {
1153a40ed3dSBarry Smith   PetscFunctionBegin;
116639f9d9dSBarry Smith   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
117a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many options checkers, only 5 allowed");
118639f9d9dSBarry Smith   }
119639f9d9dSBarry Smith 
120639f9d9dSBarry Smith   othersetfromoptions[numberofsetfromoptions++] = snescheck;
1213a40ed3dSBarry Smith   PetscFunctionReturn(0);
122639f9d9dSBarry Smith }
123639f9d9dSBarry Smith 
1245615d1e5SSatish Balay #undef __FUNC__
12515091d37SBarry Smith #define __FUNC__ "SNESSetTypeFromOptions"
12615091d37SBarry Smith /*@
12715091d37SBarry Smith    SNESSetTypeFromOptions - Sets the SNES solver type from the options database,
12815091d37SBarry Smith         or sets a default if none is give.
12915091d37SBarry Smith 
13015091d37SBarry Smith    Collective on SNES
13115091d37SBarry Smith 
13215091d37SBarry Smith    Input Parameter:
13315091d37SBarry Smith .  snes - the SNES context
13415091d37SBarry Smith 
13515091d37SBarry Smith    Options Database Keys:
13615091d37SBarry Smith .  -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc
13715091d37SBarry Smith 
13815091d37SBarry Smith    Level: beginner
13915091d37SBarry Smith 
14015091d37SBarry Smith .keywords: SNES, nonlinear, set, options, database
14115091d37SBarry Smith 
14215091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetFromOptions()
14315091d37SBarry Smith @*/
14415091d37SBarry Smith int SNESSetTypeFromOptions(SNES snes)
14515091d37SBarry Smith {
14615091d37SBarry Smith   char     method[256];
14715091d37SBarry Smith   int      ierr, flg;
14815091d37SBarry Smith 
14915091d37SBarry Smith   PetscFunctionBegin;
15015091d37SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15115091d37SBarry Smith   if (snes->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to SNESSetUp()");
152888f2ed8SSatish Balay   ierr = OptionsGetString(snes->prefix,"-snes_type",method,256,&flg);CHKERRQ(ierr);
15315091d37SBarry Smith   if (flg) {
15415091d37SBarry Smith     ierr = SNESSetType(snes,(SNESType) method);CHKERRQ(ierr);
15515091d37SBarry Smith   }
15615091d37SBarry Smith   /*
15715091d37SBarry Smith       If SNES type has not yet been set, set it now
15815091d37SBarry Smith   */
15915091d37SBarry Smith   if (!snes->type_name) {
16015091d37SBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
16115091d37SBarry Smith       ierr = SNESSetType(snes,SNES_EQ_LS);CHKERRQ(ierr);
16215091d37SBarry Smith     } else {
16315091d37SBarry Smith       ierr = SNESSetType(snes,SNES_UM_TR);CHKERRQ(ierr);
16415091d37SBarry Smith     }
16515091d37SBarry Smith   }
16615091d37SBarry Smith   PetscFunctionReturn(0);
16715091d37SBarry Smith }
16815091d37SBarry Smith 
16915091d37SBarry Smith #undef __FUNC__
1705615d1e5SSatish Balay #define __FUNC__ "SNESSetFromOptions"
1719b94acceSBarry Smith /*@
172682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
1739b94acceSBarry Smith 
174c7afd0dbSLois Curfman McInnes    Collective on SNES
175c7afd0dbSLois Curfman McInnes 
1769b94acceSBarry Smith    Input Parameter:
1779b94acceSBarry Smith .  snes - the SNES context
1789b94acceSBarry Smith 
17936851e7fSLois Curfman McInnes    Options Database Keys:
180b39c3a46SLois Curfman McInnes +  -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc
18182738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
18282738288SBarry Smith                 of the change in the solution between steps
183b39c3a46SLois Curfman McInnes .  -snes_atol <atol> - absolute tolerance of residual norm
184b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
185b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
186b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
187b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
18882738288SBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear or minimization
18982738288SBarry Smith                                solver; hence iterations will continue until max_it
1901fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
19182738288SBarry Smith                                of convergence test
19282738288SBarry Smith .  -snes_monitor - prints residual norm at each iteration
193d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
194d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
19582738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
196e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
19736851e7fSLois Curfman McInnes -  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
19882738288SBarry Smith 
19982738288SBarry Smith     Options Database for Eisenstat-Walker method:
20082738288SBarry Smith +  -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence
20182738288SBarry Smith .  -snes_ksp_eq_version ver - version of  Eisenstat-Walker method
20236851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
20336851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
20436851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
20536851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
20636851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
20736851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
20882738288SBarry Smith 
20911ca99fdSLois Curfman McInnes    Notes:
21011ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
21111ca99fdSLois Curfman McInnes    the users manual.
21283e2fdc7SBarry Smith 
21336851e7fSLois Curfman McInnes    Level: beginner
21436851e7fSLois Curfman McInnes 
2159b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2169b94acceSBarry Smith 
21715091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetTypeFromOptions()
2189b94acceSBarry Smith @*/
2199b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
2209b94acceSBarry Smith {
2219b94acceSBarry Smith   double   tmp;
2229b94acceSBarry Smith   SLES     sles;
22381f57ec7SBarry Smith   int      ierr, flg,i,loc[4],nmax = 4;
2243c7409f5SSatish Balay   int      version   = PETSC_DEFAULT;
2259b94acceSBarry Smith   double   rtol_0    = PETSC_DEFAULT;
2269b94acceSBarry Smith   double   rtol_max  = PETSC_DEFAULT;
2279b94acceSBarry Smith   double   gamma2    = PETSC_DEFAULT;
2289b94acceSBarry Smith   double   alpha     = PETSC_DEFAULT;
2299b94acceSBarry Smith   double   alpha2    = PETSC_DEFAULT;
2309b94acceSBarry Smith   double   threshold = PETSC_DEFAULT;
2319b94acceSBarry Smith 
2323a40ed3dSBarry Smith   PetscFunctionBegin;
23377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
23415091d37SBarry Smith   ierr = SNESSetTypeFromOptions(snes);CHKERRQ(ierr);
235ca161407SBarry Smith 
23615091d37SBarry Smith   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
2373c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg);CHKERRQ(ierr);
238d64ed03dSBarry Smith   if (flg) {
239d64ed03dSBarry Smith     ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
240d64ed03dSBarry Smith   }
2413c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg);CHKERRQ(ierr);
242d64ed03dSBarry Smith   if (flg) {
243d64ed03dSBarry Smith     ierr = SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
244d64ed03dSBarry Smith   }
2453c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg);CHKERRQ(ierr);
246d64ed03dSBarry Smith   if (flg) {
247d64ed03dSBarry Smith     ierr = SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
248d64ed03dSBarry Smith   }
2493c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg);CHKERRQ(ierr);
2503c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr);
251d7a720efSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg);CHKERRQ(ierr);
252d64ed03dSBarry Smith   if (flg) { ierr = SNESSetTrustRegionTolerance(snes,tmp);CHKERRQ(ierr); }
253d7a720efSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg);CHKERRQ(ierr);
254d64ed03dSBarry Smith   if (flg) { ierr = SNESSetMinimizationFunctionTolerance(snes,tmp);CHKERRQ(ierr);}
2553c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg);CHKERRQ(ierr);
2563c7409f5SSatish Balay   if (flg) { snes->ksp_ewconv = 1; }
257b18e04deSLois Curfman McInnes   ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg);CHKERRQ(ierr);
258b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg);CHKERRQ(ierr);
259b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr);
260b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg);CHKERRQ(ierr);
261b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg);CHKERRQ(ierr);
262b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg);CHKERRQ(ierr);
263b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr);
26493c39befSBarry Smith 
26593c39befSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_no_convergence_test",&flg);CHKERRQ(ierr);
26693c39befSBarry Smith   if (flg) {snes->converged = 0;}
26793c39befSBarry Smith 
2689b94acceSBarry Smith   ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha,
2699b94acceSBarry Smith                                   alpha2,threshold);CHKERRQ(ierr);
2705bbad29bSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_cancelmonitors",&flg);CHKERRQ(ierr);
2715cd90555SBarry Smith   if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
2723c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg);CHKERRQ(ierr);
273b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);}
2743c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg);CHKERRQ(ierr);
275b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);}
2763f1db9ecSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor",&flg);CHKERRQ(ierr);
277b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);}
278d132466eSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor_update",&flg);CHKERRQ(ierr);
279b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitorUpdate,0,0);CHKERRQ(ierr);}
28081f57ec7SBarry Smith   ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr);
2813c7409f5SSatish Balay   if (flg) {
28217699dbbSLois Curfman McInnes     int    rank = 0;
283d7e8b826SBarry Smith     DrawLG lg;
28417699dbbSLois Curfman McInnes     MPI_Initialized(&rank);
285d132466eSBarry Smith     if (rank) {ierr = MPI_Comm_rank(snes->comm,&rank);CHKERRQ(ierr);}
28617699dbbSLois Curfman McInnes     if (!rank) {
28781f57ec7SBarry Smith       ierr = SNESLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg);CHKERRQ(ierr);
288b8a78c4aSBarry Smith       ierr = SNESSetMonitor(snes,SNESLGMonitor,lg,( int (*)(void *))SNESLGMonitorDestroy);CHKERRQ(ierr);
2899b94acceSBarry Smith       PLogObjectParent(snes,lg);
2909b94acceSBarry Smith     }
2919b94acceSBarry Smith   }
292e24b481bSBarry Smith 
2933c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);CHKERRQ(ierr);
2943c7409f5SSatish Balay   if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
2959b94acceSBarry Smith     ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,
2969b94acceSBarry Smith                  SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
297981c4779SBarry Smith     PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
298981c4779SBarry Smith   } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
29931615d04SLois Curfman McInnes     ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,
30031615d04SLois Curfman McInnes                  SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr);
301d64ed03dSBarry Smith     PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
3029b94acceSBarry Smith   }
303639f9d9dSBarry Smith 
304639f9d9dSBarry Smith   for ( i=0; i<numberofsetfromoptions; i++ ) {
305639f9d9dSBarry Smith     ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
306639f9d9dSBarry Smith   }
307639f9d9dSBarry Smith 
3089b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
3099b94acceSBarry Smith   ierr = SLESSetFromOptions(sles);CHKERRQ(ierr);
31093993e2dSLois Curfman McInnes 
311e24b481bSBarry Smith   /* set the special KSP monitor for matrix-free application */
312e24b481bSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg);CHKERRQ(ierr);
313e24b481bSBarry Smith   if (flg) {
314e24b481bSBarry Smith     KSP ksp;
315e24b481bSBarry Smith     ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
316b8a78c4aSBarry Smith     ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr);
317e24b481bSBarry Smith   }
318e24b481bSBarry Smith 
31982bf6240SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help", &flg);CHKERRQ(ierr);
32082bf6240SBarry Smith   if (flg) { ierr = SNESPrintHelp(snes);CHKERRQ(ierr);}
32182bf6240SBarry Smith 
3223a40ed3dSBarry Smith   if (snes->setfromoptions) {
3233a40ed3dSBarry Smith     ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
3243a40ed3dSBarry Smith   }
3253a40ed3dSBarry Smith   PetscFunctionReturn(0);
3269b94acceSBarry Smith }
3279b94acceSBarry Smith 
328a847f771SSatish Balay 
3295615d1e5SSatish Balay #undef __FUNC__
330d4bb536fSBarry Smith #define __FUNC__ "SNESSetApplicationContext"
3319b94acceSBarry Smith /*@
3329b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3339b94acceSBarry Smith    the nonlinear solvers.
3349b94acceSBarry Smith 
335fee21e36SBarry Smith    Collective on SNES
336fee21e36SBarry Smith 
337c7afd0dbSLois Curfman McInnes    Input Parameters:
338c7afd0dbSLois Curfman McInnes +  snes - the SNES context
339c7afd0dbSLois Curfman McInnes -  usrP - optional user context
340c7afd0dbSLois Curfman McInnes 
34136851e7fSLois Curfman McInnes    Level: intermediate
34236851e7fSLois Curfman McInnes 
3439b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3449b94acceSBarry Smith 
3459b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3469b94acceSBarry Smith @*/
3479b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
3489b94acceSBarry Smith {
3493a40ed3dSBarry Smith   PetscFunctionBegin;
35077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3519b94acceSBarry Smith   snes->user		= usrP;
3523a40ed3dSBarry Smith   PetscFunctionReturn(0);
3539b94acceSBarry Smith }
35474679c65SBarry Smith 
3555615d1e5SSatish Balay #undef __FUNC__
356d4bb536fSBarry Smith #define __FUNC__ "SNESGetApplicationContext"
3579b94acceSBarry Smith /*@C
3589b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3599b94acceSBarry Smith    nonlinear solvers.
3609b94acceSBarry Smith 
361c7afd0dbSLois Curfman McInnes    Not Collective
362c7afd0dbSLois Curfman McInnes 
3639b94acceSBarry Smith    Input Parameter:
3649b94acceSBarry Smith .  snes - SNES context
3659b94acceSBarry Smith 
3669b94acceSBarry Smith    Output Parameter:
3679b94acceSBarry Smith .  usrP - user context
3689b94acceSBarry Smith 
36936851e7fSLois Curfman McInnes    Level: intermediate
37036851e7fSLois Curfman McInnes 
3719b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3729b94acceSBarry Smith 
3739b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3749b94acceSBarry Smith @*/
3759b94acceSBarry Smith int SNESGetApplicationContext( SNES snes,  void **usrP )
3769b94acceSBarry Smith {
3773a40ed3dSBarry Smith   PetscFunctionBegin;
37877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3799b94acceSBarry Smith   *usrP = snes->user;
3803a40ed3dSBarry Smith   PetscFunctionReturn(0);
3819b94acceSBarry Smith }
38274679c65SBarry Smith 
3835615d1e5SSatish Balay #undef __FUNC__
384d4bb536fSBarry Smith #define __FUNC__ "SNESGetIterationNumber"
3859b94acceSBarry Smith /*@
386c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
387c8228a4eSBarry Smith    at this time.
3889b94acceSBarry Smith 
389c7afd0dbSLois Curfman McInnes    Not Collective
390c7afd0dbSLois Curfman McInnes 
3919b94acceSBarry Smith    Input Parameter:
3929b94acceSBarry Smith .  snes - SNES context
3939b94acceSBarry Smith 
3949b94acceSBarry Smith    Output Parameter:
3959b94acceSBarry Smith .  iter - iteration number
3969b94acceSBarry Smith 
397c8228a4eSBarry Smith    Notes:
398c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
399c8228a4eSBarry Smith 
400c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
40108405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
40208405cd6SLois Curfman McInnes .vb
40308405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
40408405cd6SLois Curfman McInnes       if (!(it % 2)) {
40508405cd6SLois Curfman McInnes         [compute Jacobian here]
40608405cd6SLois Curfman McInnes       }
40708405cd6SLois Curfman McInnes .ve
408c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
40908405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
410c8228a4eSBarry Smith 
41136851e7fSLois Curfman McInnes    Level: intermediate
41236851e7fSLois Curfman McInnes 
4139b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
4149b94acceSBarry Smith @*/
4159b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
4169b94acceSBarry Smith {
4173a40ed3dSBarry Smith   PetscFunctionBegin;
41877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
41974679c65SBarry Smith   PetscValidIntPointer(iter);
4209b94acceSBarry Smith   *iter = snes->iter;
4213a40ed3dSBarry Smith   PetscFunctionReturn(0);
4229b94acceSBarry Smith }
42374679c65SBarry Smith 
4245615d1e5SSatish Balay #undef __FUNC__
4255615d1e5SSatish Balay #define __FUNC__ "SNESGetFunctionNorm"
4269b94acceSBarry Smith /*@
4279b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4289b94acceSBarry Smith    with SNESSSetFunction().
4299b94acceSBarry Smith 
430c7afd0dbSLois Curfman McInnes    Collective on SNES
431c7afd0dbSLois Curfman McInnes 
4329b94acceSBarry Smith    Input Parameter:
4339b94acceSBarry Smith .  snes - SNES context
4349b94acceSBarry Smith 
4359b94acceSBarry Smith    Output Parameter:
4369b94acceSBarry Smith .  fnorm - 2-norm of function
4379b94acceSBarry Smith 
4389b94acceSBarry Smith    Note:
4399b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
440a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
441a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
4429b94acceSBarry Smith 
44336851e7fSLois Curfman McInnes    Level: intermediate
44436851e7fSLois Curfman McInnes 
4459b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
446a86d99e1SLois Curfman McInnes 
44708405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
4489b94acceSBarry Smith @*/
4499b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm)
4509b94acceSBarry Smith {
4513a40ed3dSBarry Smith   PetscFunctionBegin;
45277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
45374679c65SBarry Smith   PetscValidScalarPointer(fnorm);
45474679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
455d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only");
45674679c65SBarry Smith   }
4579b94acceSBarry Smith   *fnorm = snes->norm;
4583a40ed3dSBarry Smith   PetscFunctionReturn(0);
4599b94acceSBarry Smith }
46074679c65SBarry Smith 
4615615d1e5SSatish Balay #undef __FUNC__
4625615d1e5SSatish Balay #define __FUNC__ "SNESGetGradientNorm"
4639b94acceSBarry Smith /*@
4649b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
4659b94acceSBarry Smith    with SNESSSetGradient().
4669b94acceSBarry Smith 
467c7afd0dbSLois Curfman McInnes    Collective on SNES
468c7afd0dbSLois Curfman McInnes 
4699b94acceSBarry Smith    Input Parameter:
4709b94acceSBarry Smith .  snes - SNES context
4719b94acceSBarry Smith 
4729b94acceSBarry Smith    Output Parameter:
4739b94acceSBarry Smith .  fnorm - 2-norm of gradient
4749b94acceSBarry Smith 
4759b94acceSBarry Smith    Note:
4769b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
477a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
478a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
4799b94acceSBarry Smith 
48036851e7fSLois Curfman McInnes    Level: intermediate
48136851e7fSLois Curfman McInnes 
4829b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
483a86d99e1SLois Curfman McInnes 
484a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
4859b94acceSBarry Smith @*/
4869b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm)
4879b94acceSBarry Smith {
4883a40ed3dSBarry Smith   PetscFunctionBegin;
48977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
49074679c65SBarry Smith   PetscValidScalarPointer(gnorm);
49174679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
492d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
49374679c65SBarry Smith   }
4949b94acceSBarry Smith   *gnorm = snes->norm;
4953a40ed3dSBarry Smith   PetscFunctionReturn(0);
4969b94acceSBarry Smith }
49774679c65SBarry Smith 
4985615d1e5SSatish Balay #undef __FUNC__
499d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberUnsuccessfulSteps"
5009b94acceSBarry Smith /*@
5019b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
5029b94acceSBarry Smith    attempted by the nonlinear solver.
5039b94acceSBarry Smith 
504c7afd0dbSLois Curfman McInnes    Not Collective
505c7afd0dbSLois Curfman McInnes 
5069b94acceSBarry Smith    Input Parameter:
5079b94acceSBarry Smith .  snes - SNES context
5089b94acceSBarry Smith 
5099b94acceSBarry Smith    Output Parameter:
5109b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
5119b94acceSBarry Smith 
512c96a6f78SLois Curfman McInnes    Notes:
513c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
514c96a6f78SLois Curfman McInnes 
51536851e7fSLois Curfman McInnes    Level: intermediate
51636851e7fSLois Curfman McInnes 
5179b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
5189b94acceSBarry Smith @*/
5199b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
5209b94acceSBarry Smith {
5213a40ed3dSBarry Smith   PetscFunctionBegin;
52277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
52374679c65SBarry Smith   PetscValidIntPointer(nfails);
5249b94acceSBarry Smith   *nfails = snes->nfailures;
5253a40ed3dSBarry Smith   PetscFunctionReturn(0);
5269b94acceSBarry Smith }
527a847f771SSatish Balay 
5285615d1e5SSatish Balay #undef __FUNC__
529d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberLinearIterations"
530c96a6f78SLois Curfman McInnes /*@
531c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
532c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
533c96a6f78SLois Curfman McInnes 
534c7afd0dbSLois Curfman McInnes    Not Collective
535c7afd0dbSLois Curfman McInnes 
536c96a6f78SLois Curfman McInnes    Input Parameter:
537c96a6f78SLois Curfman McInnes .  snes - SNES context
538c96a6f78SLois Curfman McInnes 
539c96a6f78SLois Curfman McInnes    Output Parameter:
540c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
541c96a6f78SLois Curfman McInnes 
542c96a6f78SLois Curfman McInnes    Notes:
543c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
544c96a6f78SLois Curfman McInnes 
54536851e7fSLois Curfman McInnes    Level: intermediate
54636851e7fSLois Curfman McInnes 
547c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
548c96a6f78SLois Curfman McInnes @*/
54986bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
550c96a6f78SLois Curfman McInnes {
5513a40ed3dSBarry Smith   PetscFunctionBegin;
552c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
553c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
554c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5553a40ed3dSBarry Smith   PetscFunctionReturn(0);
556c96a6f78SLois Curfman McInnes }
557c96a6f78SLois Curfman McInnes 
558c96a6f78SLois Curfman McInnes #undef __FUNC__
559d4bb536fSBarry Smith #define __FUNC__ "SNESGetSLES"
5609b94acceSBarry Smith /*@C
5619b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
5629b94acceSBarry Smith 
563c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
564c7afd0dbSLois Curfman McInnes 
5659b94acceSBarry Smith    Input Parameter:
5669b94acceSBarry Smith .  snes - the SNES context
5679b94acceSBarry Smith 
5689b94acceSBarry Smith    Output Parameter:
5699b94acceSBarry Smith .  sles - the SLES context
5709b94acceSBarry Smith 
5719b94acceSBarry Smith    Notes:
5729b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
5739b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
5749b94acceSBarry Smith    KSP and PC contexts as well.
5759b94acceSBarry Smith 
57636851e7fSLois Curfman McInnes    Level: beginner
57736851e7fSLois Curfman McInnes 
5789b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
5799b94acceSBarry Smith 
5809b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
5819b94acceSBarry Smith @*/
5829b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
5839b94acceSBarry Smith {
5843a40ed3dSBarry Smith   PetscFunctionBegin;
58577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5869b94acceSBarry Smith   *sles = snes->sles;
5873a40ed3dSBarry Smith   PetscFunctionReturn(0);
5889b94acceSBarry Smith }
58982bf6240SBarry Smith 
590e24b481bSBarry Smith #undef __FUNC__
591e24b481bSBarry Smith #define __FUNC__ "SNESPublish_Petsc"
592e24b481bSBarry Smith static int SNESPublish_Petsc(PetscObject object)
593e24b481bSBarry Smith {
594aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
595e24b481bSBarry Smith   SNES          v = (SNES) object;
596e24b481bSBarry Smith   int          ierr;
597*43d6d2cbSBarry Smith #endif
598e24b481bSBarry Smith 
599e24b481bSBarry Smith   PetscFunctionBegin;
600e24b481bSBarry Smith 
601*43d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
602e24b481bSBarry Smith   /* if it is already published then return */
603e24b481bSBarry Smith   if (v->amem >=0 ) PetscFunctionReturn(0);
604e24b481bSBarry Smith 
6053f1db9ecSBarry Smith   ierr = PetscObjectPublishBaseBegin(object);CHKERRQ(ierr);
606e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
607e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
608e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
609e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
610e24b481bSBarry Smith   ierr = PetscObjectPublishBaseEnd(object);CHKERRQ(ierr);
611e24b481bSBarry Smith   PetscFunctionReturn(0);
612e24b481bSBarry Smith }
613e24b481bSBarry Smith 
6149b94acceSBarry Smith /* -----------------------------------------------------------*/
6155615d1e5SSatish Balay #undef __FUNC__
6165615d1e5SSatish Balay #define __FUNC__ "SNESCreate"
6179b94acceSBarry Smith /*@C
6189b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
6199b94acceSBarry Smith 
620c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
621c7afd0dbSLois Curfman McInnes 
622c7afd0dbSLois Curfman McInnes    Input Parameters:
623c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
624c7afd0dbSLois Curfman McInnes -  type - type of method, either
625c7afd0dbSLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
626c7afd0dbSLois Curfman McInnes    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
6279b94acceSBarry Smith 
6289b94acceSBarry Smith    Output Parameter:
6299b94acceSBarry Smith .  outsnes - the new SNES context
6309b94acceSBarry Smith 
631c7afd0dbSLois Curfman McInnes    Options Database Keys:
632c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
633c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
634c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
635c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
636c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
637c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
638c1f60f51SBarry Smith 
63936851e7fSLois Curfman McInnes    Level: beginner
64036851e7fSLois Curfman McInnes 
6419b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
6429b94acceSBarry Smith 
64363a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy()
6449b94acceSBarry Smith @*/
6454b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
6469b94acceSBarry Smith {
6479b94acceSBarry Smith   int                 ierr;
6489b94acceSBarry Smith   SNES                snes;
6499b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
65037fcc0dbSBarry Smith 
6513a40ed3dSBarry Smith   PetscFunctionBegin;
6529b94acceSBarry Smith   *outsnes = 0;
653d64ed03dSBarry Smith   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){
654d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type");
655d64ed03dSBarry Smith   }
6563f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
6579b94acceSBarry Smith   PLogObjectCreate(snes);
658e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6599b94acceSBarry Smith   snes->max_its           = 50;
6609750a799SBarry Smith   snes->max_funcs	  = 10000;
6619b94acceSBarry Smith   snes->norm		  = 0.0;
6625a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
663b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
664b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
6659b94acceSBarry Smith     snes->atol		  = 1.e-10;
666d64ed03dSBarry Smith   } else {
667b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
668b4874afaSBarry Smith     snes->ttol            = 0.0;
669b4874afaSBarry Smith     snes->atol		  = 1.e-50;
670b4874afaSBarry Smith   }
6719b94acceSBarry Smith   snes->xtol		  = 1.e-8;
672b18e04deSLois Curfman McInnes   snes->trunctol	  = 1.e-12; /* no longer used */
6739b94acceSBarry Smith   snes->nfuncs            = 0;
6749b94acceSBarry Smith   snes->nfailures         = 0;
6757a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
676639f9d9dSBarry Smith   snes->numbermonitors    = 0;
6779b94acceSBarry Smith   snes->data              = 0;
6789b94acceSBarry Smith   snes->view              = 0;
6799b94acceSBarry Smith   snes->computeumfunction = 0;
6809b94acceSBarry Smith   snes->umfunP            = 0;
6819b94acceSBarry Smith   snes->fc                = 0;
6829b94acceSBarry Smith   snes->deltatol          = 1.e-12;
6839b94acceSBarry Smith   snes->fmin              = -1.e30;
6849b94acceSBarry Smith   snes->method_class      = type;
6859b94acceSBarry Smith   snes->set_method_called = 0;
68682bf6240SBarry Smith   snes->setupcalled      = 0;
6879b94acceSBarry Smith   snes->ksp_ewconv        = 0;
6886f24a144SLois Curfman McInnes   snes->vwork             = 0;
6896f24a144SLois Curfman McInnes   snes->nwork             = 0;
690758f92a0SBarry Smith   snes->conv_hist_len     = 0;
691758f92a0SBarry Smith   snes->conv_hist_max     = 0;
692758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
693758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
694758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
6959b94acceSBarry Smith 
6969b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
6970452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx);CHKPTRQ(kctx);
698eed86810SBarry Smith   PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
6999b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7009b94acceSBarry Smith   kctx->version     = 2;
7019b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7029b94acceSBarry Smith                              this was too large for some test cases */
7039b94acceSBarry Smith   kctx->rtol_last   = 0;
7049b94acceSBarry Smith   kctx->rtol_max    = .9;
7059b94acceSBarry Smith   kctx->gamma       = 1.0;
7069b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7079b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7089b94acceSBarry Smith   kctx->threshold   = .1;
7099b94acceSBarry Smith   kctx->lresid_last = 0;
7109b94acceSBarry Smith   kctx->norm_last   = 0;
7119b94acceSBarry Smith 
7129b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr);
7139b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
7145334005bSBarry Smith 
7159b94acceSBarry Smith   *outsnes = snes;
716e24b481bSBarry Smith   PetscPublishAll(snes);
7173a40ed3dSBarry Smith   PetscFunctionReturn(0);
7189b94acceSBarry Smith }
7199b94acceSBarry Smith 
7209b94acceSBarry Smith /* --------------------------------------------------------------- */
7215615d1e5SSatish Balay #undef __FUNC__
722d4bb536fSBarry Smith #define __FUNC__ "SNESSetFunction"
7239b94acceSBarry Smith /*@C
7249b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
7259b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
7269b94acceSBarry Smith    equations.
7279b94acceSBarry Smith 
728fee21e36SBarry Smith    Collective on SNES
729fee21e36SBarry Smith 
730c7afd0dbSLois Curfman McInnes    Input Parameters:
731c7afd0dbSLois Curfman McInnes +  snes - the SNES context
732c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
733c7afd0dbSLois Curfman McInnes .  r - vector to store function value
734c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
735c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7369b94acceSBarry Smith 
737c7afd0dbSLois Curfman McInnes    Calling sequence of func:
7388d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
739c7afd0dbSLois Curfman McInnes 
740313e4042SLois Curfman McInnes .  f - function vector
741c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
7429b94acceSBarry Smith 
7439b94acceSBarry Smith    Notes:
7449b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
7459b94acceSBarry Smith $      f'(x) x = -f(x),
746c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
7479b94acceSBarry Smith 
7489b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7499b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7509b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
7519b94acceSBarry Smith 
75236851e7fSLois Curfman McInnes    Level: beginner
75336851e7fSLois Curfman McInnes 
7549b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
7559b94acceSBarry Smith 
756a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
7579b94acceSBarry Smith @*/
7585334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx)
7599b94acceSBarry Smith {
7603a40ed3dSBarry Smith   PetscFunctionBegin;
76177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
762a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
763a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
764a8c6a408SBarry Smith   }
7659b94acceSBarry Smith   snes->computefunction     = func;
7669b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7679b94acceSBarry Smith   snes->funP                = ctx;
7683a40ed3dSBarry Smith   PetscFunctionReturn(0);
7699b94acceSBarry Smith }
7709b94acceSBarry Smith 
7715615d1e5SSatish Balay #undef __FUNC__
7725615d1e5SSatish Balay #define __FUNC__ "SNESComputeFunction"
7739b94acceSBarry Smith /*@
77436851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
7759b94acceSBarry Smith                          SNESSetFunction().
7769b94acceSBarry Smith 
777c7afd0dbSLois Curfman McInnes    Collective on SNES
778c7afd0dbSLois Curfman McInnes 
7799b94acceSBarry Smith    Input Parameters:
780c7afd0dbSLois Curfman McInnes +  snes - the SNES context
781c7afd0dbSLois Curfman McInnes -  x - input vector
7829b94acceSBarry Smith 
7839b94acceSBarry Smith    Output Parameter:
7843638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
7859b94acceSBarry Smith 
7861bffabb2SLois Curfman McInnes    Notes:
7879b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7889b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7899b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
7909b94acceSBarry Smith 
79136851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
79236851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
79336851e7fSLois Curfman McInnes    themselves.
79436851e7fSLois Curfman McInnes 
79536851e7fSLois Curfman McInnes    Level: developer
79636851e7fSLois Curfman McInnes 
7979b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
7989b94acceSBarry Smith 
799a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
8009b94acceSBarry Smith @*/
8019b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y)
8029b94acceSBarry Smith {
8039b94acceSBarry Smith   int    ierr;
8049b94acceSBarry Smith 
8053a40ed3dSBarry Smith   PetscFunctionBegin;
806d4bb536fSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
807a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
808d4bb536fSBarry Smith   }
8099b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
810d64ed03dSBarry Smith   PetscStackPush("SNES user function");
8119b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
812d64ed03dSBarry Smith   PetscStackPop;
813ae3c334cSLois Curfman McInnes   snes->nfuncs++;
8149b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
8153a40ed3dSBarry Smith   PetscFunctionReturn(0);
8169b94acceSBarry Smith }
8179b94acceSBarry Smith 
8185615d1e5SSatish Balay #undef __FUNC__
819d4bb536fSBarry Smith #define __FUNC__ "SNESSetMinimizationFunction"
8209b94acceSBarry Smith /*@C
8219b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
8229b94acceSBarry Smith    unconstrained minimization.
8239b94acceSBarry Smith 
824fee21e36SBarry Smith    Collective on SNES
825fee21e36SBarry Smith 
826c7afd0dbSLois Curfman McInnes    Input Parameters:
827c7afd0dbSLois Curfman McInnes +  snes - the SNES context
828c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
829c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
830c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8319b94acceSBarry Smith 
832c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8338d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,double *f,void *ctx);
834c7afd0dbSLois Curfman McInnes 
835c7afd0dbSLois Curfman McInnes +  x - input vector
8369b94acceSBarry Smith .  f - function
837c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined function context
8389b94acceSBarry Smith 
83936851e7fSLois Curfman McInnes    Level: beginner
84036851e7fSLois Curfman McInnes 
8419b94acceSBarry Smith    Notes:
8429b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
8439b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
8449b94acceSBarry Smith    SNESSetFunction().
8459b94acceSBarry Smith 
8469b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
8479b94acceSBarry Smith 
848a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
849a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
8509b94acceSBarry Smith @*/
8519b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),
8529b94acceSBarry Smith                       void *ctx)
8539b94acceSBarry Smith {
8543a40ed3dSBarry Smith   PetscFunctionBegin;
85577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
856a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
857a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
858a8c6a408SBarry Smith   }
8599b94acceSBarry Smith   snes->computeumfunction   = func;
8609b94acceSBarry Smith   snes->umfunP              = ctx;
8613a40ed3dSBarry Smith   PetscFunctionReturn(0);
8629b94acceSBarry Smith }
8639b94acceSBarry Smith 
8645615d1e5SSatish Balay #undef __FUNC__
8655615d1e5SSatish Balay #define __FUNC__ "SNESComputeMinimizationFunction"
8669b94acceSBarry Smith /*@
8679b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
8689b94acceSBarry Smith    set with SNESSetMinimizationFunction().
8699b94acceSBarry Smith 
870c7afd0dbSLois Curfman McInnes    Collective on SNES
871c7afd0dbSLois Curfman McInnes 
8729b94acceSBarry Smith    Input Parameters:
873c7afd0dbSLois Curfman McInnes +  snes - the SNES context
874c7afd0dbSLois Curfman McInnes -  x - input vector
8759b94acceSBarry Smith 
8769b94acceSBarry Smith    Output Parameter:
8779b94acceSBarry Smith .  y - function value
8789b94acceSBarry Smith 
8799b94acceSBarry Smith    Notes:
8809b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
8819b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
8829b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
883a86d99e1SLois Curfman McInnes 
88436851e7fSLois Curfman McInnes    SNESComputeMinimizationFunction() is typically used within minimization
88536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
88636851e7fSLois Curfman McInnes    themselves.
88736851e7fSLois Curfman McInnes 
88836851e7fSLois Curfman McInnes    Level: developer
88936851e7fSLois Curfman McInnes 
890a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
891a86d99e1SLois Curfman McInnes 
892a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
893a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
8949b94acceSBarry Smith @*/
8959b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y)
8969b94acceSBarry Smith {
8979b94acceSBarry Smith   int    ierr;
8983a40ed3dSBarry Smith 
8993a40ed3dSBarry Smith   PetscFunctionBegin;
900a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
901a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
902a8c6a408SBarry Smith   }
9039b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
904d64ed03dSBarry Smith   PetscStackPush("SNES user minimzation function");
9059b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr);
906d64ed03dSBarry Smith   PetscStackPop;
907ae3c334cSLois Curfman McInnes   snes->nfuncs++;
9089b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
9093a40ed3dSBarry Smith   PetscFunctionReturn(0);
9109b94acceSBarry Smith }
9119b94acceSBarry Smith 
9125615d1e5SSatish Balay #undef __FUNC__
913d4bb536fSBarry Smith #define __FUNC__ "SNESSetGradient"
9149b94acceSBarry Smith /*@C
9159b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
9169b94acceSBarry Smith    vector for use by the SNES routines.
9179b94acceSBarry Smith 
918c7afd0dbSLois Curfman McInnes    Collective on SNES
919c7afd0dbSLois Curfman McInnes 
9209b94acceSBarry Smith    Input Parameters:
921c7afd0dbSLois Curfman McInnes +  snes - the SNES context
9229b94acceSBarry Smith .  func - function evaluation routine
923044dda88SLois Curfman McInnes .  ctx - optional user-defined context for private data for the
924044dda88SLois Curfman McInnes          gradient evaluation routine (may be PETSC_NULL)
925c7afd0dbSLois Curfman McInnes -  r - vector to store gradient value
926fee21e36SBarry Smith 
9279b94acceSBarry Smith    Calling sequence of func:
9288d76a1e5SLois Curfman McInnes $     func (SNES, Vec x, Vec g, void *ctx);
9299b94acceSBarry Smith 
930c7afd0dbSLois Curfman McInnes +  x - input vector
9319b94acceSBarry Smith .  g - gradient vector
932c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined gradient context
9339b94acceSBarry Smith 
9349b94acceSBarry Smith    Notes:
9359b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
9369b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
9379b94acceSBarry Smith    SNESSetFunction().
9389b94acceSBarry Smith 
93936851e7fSLois Curfman McInnes    Level: beginner
94036851e7fSLois Curfman McInnes 
9419b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9429b94acceSBarry Smith 
943a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
944a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
9459b94acceSBarry Smith @*/
94674679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
9479b94acceSBarry Smith {
9483a40ed3dSBarry Smith   PetscFunctionBegin;
94977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
950a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
951a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
952a8c6a408SBarry Smith   }
9539b94acceSBarry Smith   snes->computefunction     = func;
9549b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
9559b94acceSBarry Smith   snes->funP                = ctx;
9563a40ed3dSBarry Smith   PetscFunctionReturn(0);
9579b94acceSBarry Smith }
9589b94acceSBarry Smith 
9595615d1e5SSatish Balay #undef __FUNC__
9605615d1e5SSatish Balay #define __FUNC__ "SNESComputeGradient"
9619b94acceSBarry Smith /*@
962a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
963a86d99e1SLois Curfman McInnes    SNESSetGradient().
9649b94acceSBarry Smith 
965c7afd0dbSLois Curfman McInnes    Collective on SNES
966c7afd0dbSLois Curfman McInnes 
9679b94acceSBarry Smith    Input Parameters:
968c7afd0dbSLois Curfman McInnes +  snes - the SNES context
969c7afd0dbSLois Curfman McInnes -  x - input vector
9709b94acceSBarry Smith 
9719b94acceSBarry Smith    Output Parameter:
9729b94acceSBarry Smith .  y - gradient vector
9739b94acceSBarry Smith 
9749b94acceSBarry Smith    Notes:
9759b94acceSBarry Smith    SNESComputeGradient() is valid only for
9769b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
9779b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
978a86d99e1SLois Curfman McInnes 
97936851e7fSLois Curfman McInnes    SNESComputeGradient() is typically used within minimization
98036851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
98136851e7fSLois Curfman McInnes    themselves.
98236851e7fSLois Curfman McInnes 
98336851e7fSLois Curfman McInnes    Level: developer
98436851e7fSLois Curfman McInnes 
985a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
986a86d99e1SLois Curfman McInnes 
987a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
988a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
9899b94acceSBarry Smith @*/
9909b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y)
9919b94acceSBarry Smith {
9929b94acceSBarry Smith   int    ierr;
9933a40ed3dSBarry Smith 
9943a40ed3dSBarry Smith   PetscFunctionBegin;
9953a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
996a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
9973a40ed3dSBarry Smith   }
9989b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
999d64ed03dSBarry Smith   PetscStackPush("SNES user gradient function");
10009b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1001d64ed03dSBarry Smith   PetscStackPop;
10029b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
10033a40ed3dSBarry Smith   PetscFunctionReturn(0);
10049b94acceSBarry Smith }
10059b94acceSBarry Smith 
10065615d1e5SSatish Balay #undef __FUNC__
10075615d1e5SSatish Balay #define __FUNC__ "SNESComputeJacobian"
100862fef451SLois Curfman McInnes /*@
100962fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
101062fef451SLois Curfman McInnes    set with SNESSetJacobian().
101162fef451SLois Curfman McInnes 
1012c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1013c7afd0dbSLois Curfman McInnes 
101462fef451SLois Curfman McInnes    Input Parameters:
1015c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1016c7afd0dbSLois Curfman McInnes -  x - input vector
101762fef451SLois Curfman McInnes 
101862fef451SLois Curfman McInnes    Output Parameters:
1019c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
102062fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1021c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1022fee21e36SBarry Smith 
102362fef451SLois Curfman McInnes    Notes:
102462fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
102562fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
102662fef451SLois Curfman McInnes 
1027dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1028dc5a77f8SLois Curfman McInnes    flag parameter.
102962fef451SLois Curfman McInnes 
103062fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
103162fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
1032005c665bSBarry Smith    methods is SNESComputeHessian().
103362fef451SLois Curfman McInnes 
103436851e7fSLois Curfman McInnes    SNESComputeJacobian() is typically used within nonlinear solver
103536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
103636851e7fSLois Curfman McInnes    themselves.
103736851e7fSLois Curfman McInnes 
103836851e7fSLois Curfman McInnes    Level: developer
103936851e7fSLois Curfman McInnes 
104062fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
104162fef451SLois Curfman McInnes 
104262fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
104362fef451SLois Curfman McInnes @*/
10449b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10459b94acceSBarry Smith {
10469b94acceSBarry Smith   int    ierr;
10473a40ed3dSBarry Smith 
10483a40ed3dSBarry Smith   PetscFunctionBegin;
10493a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1050a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
10513a40ed3dSBarry Smith   }
10523a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
10539b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
1054c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1055d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
10569b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1057d64ed03dSBarry Smith   PetscStackPop;
10589b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
10596d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
106077c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
106177c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
10623a40ed3dSBarry Smith   PetscFunctionReturn(0);
10639b94acceSBarry Smith }
10649b94acceSBarry Smith 
10655615d1e5SSatish Balay #undef __FUNC__
10665615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian"
106762fef451SLois Curfman McInnes /*@
106862fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
106962fef451SLois Curfman McInnes    set with SNESSetHessian().
107062fef451SLois Curfman McInnes 
1071c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1072c7afd0dbSLois Curfman McInnes 
107362fef451SLois Curfman McInnes    Input Parameters:
1074c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1075c7afd0dbSLois Curfman McInnes -  x - input vector
107662fef451SLois Curfman McInnes 
107762fef451SLois Curfman McInnes    Output Parameters:
1078c7afd0dbSLois Curfman McInnes +  A - Hessian matrix
107962fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1080c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1081fee21e36SBarry Smith 
108262fef451SLois Curfman McInnes    Notes:
108362fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
108462fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
108562fef451SLois Curfman McInnes 
1086dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1087dc5a77f8SLois Curfman McInnes    flag parameter.
108862fef451SLois Curfman McInnes 
108962fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
109062fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
109162fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
109262fef451SLois Curfman McInnes 
109336851e7fSLois Curfman McInnes    SNESComputeHessian() is typically used within minimization
109436851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
109536851e7fSLois Curfman McInnes    themselves.
109636851e7fSLois Curfman McInnes 
109736851e7fSLois Curfman McInnes    Level: developer
109836851e7fSLois Curfman McInnes 
109962fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
110062fef451SLois Curfman McInnes 
1101a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
1102a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
110362fef451SLois Curfman McInnes @*/
110462fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
11059b94acceSBarry Smith {
11069b94acceSBarry Smith   int    ierr;
11073a40ed3dSBarry Smith 
11083a40ed3dSBarry Smith   PetscFunctionBegin;
11093a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1110a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
11113a40ed3dSBarry Smith   }
11123a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
111362fef451SLois Curfman McInnes   PLogEventBegin(SNES_HessianEval,snes,x,*A,*B);
11140f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
1115d64ed03dSBarry Smith   PetscStackPush("SNES user Hessian function");
111662fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr);
1117d64ed03dSBarry Smith   PetscStackPop;
111862fef451SLois Curfman McInnes   PLogEventEnd(SNES_HessianEval,snes,x,*A,*B);
11190f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
112077c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
112177c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11223a40ed3dSBarry Smith   PetscFunctionReturn(0);
11239b94acceSBarry Smith }
11249b94acceSBarry Smith 
11255615d1e5SSatish Balay #undef __FUNC__
1126d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian"
11279b94acceSBarry Smith /*@C
11289b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1129044dda88SLois Curfman McInnes    location to store the matrix.
11309b94acceSBarry Smith 
1131c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1132c7afd0dbSLois Curfman McInnes 
11339b94acceSBarry Smith    Input Parameters:
1134c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11359b94acceSBarry Smith .  A - Jacobian matrix
11369b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11379b94acceSBarry Smith .  func - Jacobian evaluation routine
1138c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11392cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11409b94acceSBarry Smith 
11419b94acceSBarry Smith    Calling sequence of func:
11428d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11439b94acceSBarry Smith 
1144c7afd0dbSLois Curfman McInnes +  x - input vector
11459b94acceSBarry Smith .  A - Jacobian matrix
11469b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1147ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1148ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1149c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11509b94acceSBarry Smith 
11519b94acceSBarry Smith    Notes:
1152dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
11532cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1154ac21db08SLois Curfman McInnes 
1155ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11569b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11579b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11589b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11599b94acceSBarry Smith    throughout the global iterations.
11609b94acceSBarry Smith 
116136851e7fSLois Curfman McInnes    Level: beginner
116236851e7fSLois Curfman McInnes 
11639b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11649b94acceSBarry Smith 
1165ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
11669b94acceSBarry Smith @*/
11679b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
11689b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
11699b94acceSBarry Smith {
11703a40ed3dSBarry Smith   PetscFunctionBegin;
117177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1172a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1173a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
1174a8c6a408SBarry Smith   }
11759b94acceSBarry Smith   snes->computejacobian = func;
11769b94acceSBarry Smith   snes->jacP            = ctx;
11779b94acceSBarry Smith   snes->jacobian        = A;
11789b94acceSBarry Smith   snes->jacobian_pre    = B;
11793a40ed3dSBarry Smith   PetscFunctionReturn(0);
11809b94acceSBarry Smith }
118162fef451SLois Curfman McInnes 
11825615d1e5SSatish Balay #undef __FUNC__
1183d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian"
1184c2aafc4cSSatish Balay /*@C
1185b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1186b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1187b4fd4287SBarry Smith 
1188c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1189c7afd0dbSLois Curfman McInnes 
1190b4fd4287SBarry Smith    Input Parameter:
1191b4fd4287SBarry Smith .  snes - the nonlinear solver context
1192b4fd4287SBarry Smith 
1193b4fd4287SBarry Smith    Output Parameters:
1194c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1195b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
1196c7afd0dbSLois Curfman McInnes -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1197fee21e36SBarry Smith 
119836851e7fSLois Curfman McInnes    Level: advanced
119936851e7fSLois Curfman McInnes 
1200b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1201b4fd4287SBarry Smith @*/
1202b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx)
1203b4fd4287SBarry Smith {
12043a40ed3dSBarry Smith   PetscFunctionBegin;
12053a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1206a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
12073a40ed3dSBarry Smith   }
1208b4fd4287SBarry Smith   if (A)   *A = snes->jacobian;
1209b4fd4287SBarry Smith   if (B)   *B = snes->jacobian_pre;
1210b4fd4287SBarry Smith   if (ctx) *ctx = snes->jacP;
12113a40ed3dSBarry Smith   PetscFunctionReturn(0);
1212b4fd4287SBarry Smith }
1213b4fd4287SBarry Smith 
12145615d1e5SSatish Balay #undef __FUNC__
1215d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian"
12169b94acceSBarry Smith /*@C
12179b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
1218044dda88SLois Curfman McInnes    location to store the matrix.
12199b94acceSBarry Smith 
1220c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1221c7afd0dbSLois Curfman McInnes 
12229b94acceSBarry Smith    Input Parameters:
1223c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12249b94acceSBarry Smith .  A - Hessian matrix
12259b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
12269b94acceSBarry Smith .  func - Jacobian evaluation routine
1227c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1228313e4042SLois Curfman McInnes          Hessian evaluation routine (may be PETSC_NULL)
12299b94acceSBarry Smith 
12309b94acceSBarry Smith    Calling sequence of func:
12318d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12329b94acceSBarry Smith 
1233c7afd0dbSLois Curfman McInnes +  x - input vector
12349b94acceSBarry Smith .  A - Hessian matrix
12359b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1236ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1237ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1238c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Hessian context
12399b94acceSBarry Smith 
12409b94acceSBarry Smith    Notes:
1241dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
12422cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1243ac21db08SLois Curfman McInnes 
12449b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
12459b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
12469b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12479b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12489b94acceSBarry Smith    throughout the global iterations.
12499b94acceSBarry Smith 
125036851e7fSLois Curfman McInnes    Level: beginner
125136851e7fSLois Curfman McInnes 
12529b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
12539b94acceSBarry Smith 
1254ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
12559b94acceSBarry Smith @*/
12569b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
12579b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
12589b94acceSBarry Smith {
12593a40ed3dSBarry Smith   PetscFunctionBegin;
126077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1261d4bb536fSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1262a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1263d4bb536fSBarry Smith   }
12649b94acceSBarry Smith   snes->computejacobian = func;
12659b94acceSBarry Smith   snes->jacP            = ctx;
12669b94acceSBarry Smith   snes->jacobian        = A;
12679b94acceSBarry Smith   snes->jacobian_pre    = B;
12683a40ed3dSBarry Smith   PetscFunctionReturn(0);
12699b94acceSBarry Smith }
12709b94acceSBarry Smith 
12715615d1e5SSatish Balay #undef __FUNC__
1272d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian"
127362fef451SLois Curfman McInnes /*@
127462fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
127562fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
127662fef451SLois Curfman McInnes 
1277c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object is parallel if SNES object is parallel
1278c7afd0dbSLois Curfman McInnes 
127962fef451SLois Curfman McInnes    Input Parameter:
128062fef451SLois Curfman McInnes .  snes - the nonlinear solver context
128162fef451SLois Curfman McInnes 
128262fef451SLois Curfman McInnes    Output Parameters:
1283c7afd0dbSLois Curfman McInnes +  A - location to stash Hessian matrix (or PETSC_NULL)
128462fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
1285c7afd0dbSLois Curfman McInnes -  ctx - location to stash Hessian ctx (or PETSC_NULL)
1286fee21e36SBarry Smith 
128736851e7fSLois Curfman McInnes    Level: advanced
128836851e7fSLois Curfman McInnes 
128962fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
1290c7afd0dbSLois Curfman McInnes 
1291c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian
129262fef451SLois Curfman McInnes @*/
129362fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx)
129462fef451SLois Curfman McInnes {
12953a40ed3dSBarry Smith   PetscFunctionBegin;
12963a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){
1297a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
12983a40ed3dSBarry Smith   }
129962fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
130062fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
130162fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
13023a40ed3dSBarry Smith   PetscFunctionReturn(0);
130362fef451SLois Curfman McInnes }
130462fef451SLois Curfman McInnes 
13059b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13069b94acceSBarry Smith 
13075615d1e5SSatish Balay #undef __FUNC__
13085615d1e5SSatish Balay #define __FUNC__ "SNESSetUp"
13099b94acceSBarry Smith /*@
13109b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1311272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13129b94acceSBarry Smith 
1313fee21e36SBarry Smith    Collective on SNES
1314fee21e36SBarry Smith 
1315c7afd0dbSLois Curfman McInnes    Input Parameters:
1316c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1317c7afd0dbSLois Curfman McInnes -  x - the solution vector
1318c7afd0dbSLois Curfman McInnes 
1319272ac6f2SLois Curfman McInnes    Notes:
1320272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1321272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1322272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1323272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1324272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1325272ac6f2SLois Curfman McInnes 
132636851e7fSLois Curfman McInnes    Level: advanced
132736851e7fSLois Curfman McInnes 
13289b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13299b94acceSBarry Smith 
13309b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
13319b94acceSBarry Smith @*/
13328ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
13339b94acceSBarry Smith {
1334272ac6f2SLois Curfman McInnes   int ierr, flg;
13353a40ed3dSBarry Smith 
13363a40ed3dSBarry Smith   PetscFunctionBegin;
133777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
133877c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
13398ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
13409b94acceSBarry Smith 
1341c1f60f51SBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);CHKERRQ(ierr);
1342c1f60f51SBarry Smith   /*
1343c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1344dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1345c1f60f51SBarry Smith   */
1346c1f60f51SBarry Smith   if (flg) {
1347c1f60f51SBarry Smith     Mat J;
13485a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1349c1f60f51SBarry Smith     PLogObjectParent(snes,J);
1350c1f60f51SBarry Smith     snes->mfshell = J;
1351c1f60f51SBarry Smith     snes->jacobian = J;
1352c2aafc4cSSatish Balay     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1353c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n");
1354d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
1355c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n");
1356d4bb536fSBarry Smith     } else {
1357a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option");
1358d4bb536fSBarry Smith     }
13595a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1360c1f60f51SBarry Smith   }
1361272ac6f2SLois Curfman McInnes   ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);CHKERRQ(ierr);
1362c1f60f51SBarry Smith   /*
1363dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1364c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1365c1f60f51SBarry Smith    */
136631615d04SLois Curfman McInnes   if (flg) {
1367272ac6f2SLois Curfman McInnes     Mat J;
13685a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1369272ac6f2SLois Curfman McInnes     PLogObjectParent(snes,J);
1370272ac6f2SLois Curfman McInnes     snes->mfshell = J;
137183e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
137283e56afdSLois Curfman McInnes       ierr = SNESSetJacobian(snes,J,J,0,snes->funP);CHKERRQ(ierr);
137394a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n");
1374d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
137583e56afdSLois Curfman McInnes       ierr = SNESSetHessian(snes,J,J,0,snes->funP);CHKERRQ(ierr);
137694a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n");
1377d4bb536fSBarry Smith     } else {
1378a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option");
1379d4bb536fSBarry Smith     }
13805a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1381272ac6f2SLois Curfman McInnes   }
13829b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
1383a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
1384a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
13855a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1386a8c6a408SBarry Smith     if (snes->vec_func == snes->vec_sol) {
1387a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector");
1388a8c6a408SBarry Smith     }
1389a703fe33SLois Curfman McInnes 
1390a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
139182bf6240SBarry Smith     if (snes->ksp_ewconv && PetscStrcmp(snes->type_name,SNES_EQ_TR)) {
1392a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1393a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1394a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
13955a655dc6SBarry Smith       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr);
1396a703fe33SLois Curfman McInnes     }
13979b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
1398a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1399a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1400a8c6a408SBarry Smith     if (!snes->computeumfunction) {
1401a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first");
1402a8c6a408SBarry Smith     }
14035a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()");
1404d4bb536fSBarry Smith   } else {
1405a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class");
1406d4bb536fSBarry Smith   }
1407a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
140882bf6240SBarry Smith   snes->setupcalled = 1;
14093a40ed3dSBarry Smith   PetscFunctionReturn(0);
14109b94acceSBarry Smith }
14119b94acceSBarry Smith 
14125615d1e5SSatish Balay #undef __FUNC__
1413d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy"
14149b94acceSBarry Smith /*@C
14159b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
14169b94acceSBarry Smith    with SNESCreate().
14179b94acceSBarry Smith 
1418c7afd0dbSLois Curfman McInnes    Collective on SNES
1419c7afd0dbSLois Curfman McInnes 
14209b94acceSBarry Smith    Input Parameter:
14219b94acceSBarry Smith .  snes - the SNES context
14229b94acceSBarry Smith 
142336851e7fSLois Curfman McInnes    Level: beginner
142436851e7fSLois Curfman McInnes 
14259b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14269b94acceSBarry Smith 
142763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14289b94acceSBarry Smith @*/
14299b94acceSBarry Smith int SNESDestroy(SNES snes)
14309b94acceSBarry Smith {
1431b8a78c4aSBarry Smith   int i,ierr;
14323a40ed3dSBarry Smith 
14333a40ed3dSBarry Smith   PetscFunctionBegin;
143477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
14353a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1436d4bb536fSBarry Smith 
1437be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
1438be0abb6dSBarry Smith   ierr = PetscAMSDestroy(snes);CHKERRQ(ierr);
1439be0abb6dSBarry Smith 
1440e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1441606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
1442522c5e43SBarry Smith   if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);}
14439b94acceSBarry Smith   ierr = SLESDestroy(snes->sles);CHKERRQ(ierr);
1444522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1445b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++ ) {
1446b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1447b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1448b8a78c4aSBarry Smith     }
1449b8a78c4aSBarry Smith   }
14509b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
14510452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
14523a40ed3dSBarry Smith   PetscFunctionReturn(0);
14539b94acceSBarry Smith }
14549b94acceSBarry Smith 
14559b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14569b94acceSBarry Smith 
14575615d1e5SSatish Balay #undef __FUNC__
14585615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances"
14599b94acceSBarry Smith /*@
1460d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
14619b94acceSBarry Smith 
1462c7afd0dbSLois Curfman McInnes    Collective on SNES
1463c7afd0dbSLois Curfman McInnes 
14649b94acceSBarry Smith    Input Parameters:
1465c7afd0dbSLois Curfman McInnes +  snes - the SNES context
146633174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
146733174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
146833174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
146933174efeSLois Curfman McInnes            of the change in the solution between steps
147033174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1471c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1472fee21e36SBarry Smith 
147333174efeSLois Curfman McInnes    Options Database Keys:
1474c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1475c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1476c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1477c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1478c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
14799b94acceSBarry Smith 
1480d7a720efSLois Curfman McInnes    Notes:
14819b94acceSBarry Smith    The default maximum number of iterations is 50.
14829b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
14839b94acceSBarry Smith 
148436851e7fSLois Curfman McInnes    Level: intermediate
148536851e7fSLois Curfman McInnes 
148633174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
14879b94acceSBarry Smith 
1488d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
14899b94acceSBarry Smith @*/
1490d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf)
14919b94acceSBarry Smith {
14923a40ed3dSBarry Smith   PetscFunctionBegin;
149377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1494d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1495d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1496d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1497d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1498d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
14993a40ed3dSBarry Smith   PetscFunctionReturn(0);
15009b94acceSBarry Smith }
15019b94acceSBarry Smith 
15025615d1e5SSatish Balay #undef __FUNC__
15035615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances"
15049b94acceSBarry Smith /*@
150533174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
150633174efeSLois Curfman McInnes 
1507c7afd0dbSLois Curfman McInnes    Not Collective
1508c7afd0dbSLois Curfman McInnes 
150933174efeSLois Curfman McInnes    Input Parameters:
1510c7afd0dbSLois Curfman McInnes +  snes - the SNES context
151133174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
151233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
151333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
151433174efeSLois Curfman McInnes            of the change in the solution between steps
151533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1516c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1517fee21e36SBarry Smith 
151833174efeSLois Curfman McInnes    Notes:
151933174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
152033174efeSLois Curfman McInnes 
152136851e7fSLois Curfman McInnes    Level: intermediate
152236851e7fSLois Curfman McInnes 
152333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
152433174efeSLois Curfman McInnes 
152533174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
152633174efeSLois Curfman McInnes @*/
152733174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf)
152833174efeSLois Curfman McInnes {
15293a40ed3dSBarry Smith   PetscFunctionBegin;
153033174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
153133174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
153233174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
153333174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
153433174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
153533174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
15363a40ed3dSBarry Smith   PetscFunctionReturn(0);
153733174efeSLois Curfman McInnes }
153833174efeSLois Curfman McInnes 
15395615d1e5SSatish Balay #undef __FUNC__
15405615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance"
154133174efeSLois Curfman McInnes /*@
15429b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
15439b94acceSBarry Smith 
1544fee21e36SBarry Smith    Collective on SNES
1545fee21e36SBarry Smith 
1546c7afd0dbSLois Curfman McInnes    Input Parameters:
1547c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1548c7afd0dbSLois Curfman McInnes -  tol - tolerance
1549c7afd0dbSLois Curfman McInnes 
15509b94acceSBarry Smith    Options Database Key:
1551c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
15529b94acceSBarry Smith 
155336851e7fSLois Curfman McInnes    Level: intermediate
155436851e7fSLois Curfman McInnes 
15559b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
15569b94acceSBarry Smith 
1557d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
15589b94acceSBarry Smith @*/
15599b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol)
15609b94acceSBarry Smith {
15613a40ed3dSBarry Smith   PetscFunctionBegin;
156277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15639b94acceSBarry Smith   snes->deltatol = tol;
15643a40ed3dSBarry Smith   PetscFunctionReturn(0);
15659b94acceSBarry Smith }
15669b94acceSBarry Smith 
15675615d1e5SSatish Balay #undef __FUNC__
15685615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance"
15699b94acceSBarry Smith /*@
157077c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
15719b94acceSBarry Smith    for unconstrained minimization solvers.
15729b94acceSBarry Smith 
1573fee21e36SBarry Smith    Collective on SNES
1574fee21e36SBarry Smith 
1575c7afd0dbSLois Curfman McInnes    Input Parameters:
1576c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1577c7afd0dbSLois Curfman McInnes -  ftol - minimum function tolerance
1578c7afd0dbSLois Curfman McInnes 
15799b94acceSBarry Smith    Options Database Key:
1580c7afd0dbSLois Curfman McInnes .  -snes_fmin <ftol> - Sets ftol
15819b94acceSBarry Smith 
15829b94acceSBarry Smith    Note:
158377c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
15849b94acceSBarry Smith    methods only.
15859b94acceSBarry Smith 
158636851e7fSLois Curfman McInnes    Level: intermediate
158736851e7fSLois Curfman McInnes 
15889b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
15899b94acceSBarry Smith 
1590d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
15919b94acceSBarry Smith @*/
159277c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol)
15939b94acceSBarry Smith {
15943a40ed3dSBarry Smith   PetscFunctionBegin;
159577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15969b94acceSBarry Smith   snes->fmin = ftol;
15973a40ed3dSBarry Smith   PetscFunctionReturn(0);
15989b94acceSBarry Smith }
1599df9fa365SBarry Smith /*
1600df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1601df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1602df9fa365SBarry Smith    macros instead of functions
1603df9fa365SBarry Smith */
1604ce1608b8SBarry Smith #undef __FUNC__
1605ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor"
1606ce1608b8SBarry Smith int SNESLGMonitor(SNES snes,int it,double norm,void *ctx)
1607ce1608b8SBarry Smith {
1608ce1608b8SBarry Smith   int ierr;
1609ce1608b8SBarry Smith 
1610ce1608b8SBarry Smith   PetscFunctionBegin;
1611ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1612ce1608b8SBarry Smith   PetscFunctionReturn(0);
1613ce1608b8SBarry Smith }
1614ce1608b8SBarry Smith 
1615df9fa365SBarry Smith #undef __FUNC__
1616df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorCreate"
1617df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n, DrawLG *draw)
1618df9fa365SBarry Smith {
1619df9fa365SBarry Smith   int ierr;
1620df9fa365SBarry Smith 
1621df9fa365SBarry Smith   PetscFunctionBegin;
1622df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1623df9fa365SBarry Smith   PetscFunctionReturn(0);
1624df9fa365SBarry Smith }
1625df9fa365SBarry Smith 
1626df9fa365SBarry Smith #undef __FUNC__
1627df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorDestroy"
1628df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw)
1629df9fa365SBarry Smith {
1630df9fa365SBarry Smith   int ierr;
1631df9fa365SBarry Smith 
1632df9fa365SBarry Smith   PetscFunctionBegin;
1633df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1634df9fa365SBarry Smith   PetscFunctionReturn(0);
1635df9fa365SBarry Smith }
1636df9fa365SBarry Smith 
16379b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
16389b94acceSBarry Smith 
16395615d1e5SSatish Balay #undef __FUNC__
1640d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor"
16419b94acceSBarry Smith /*@C
16425cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
16439b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
16449b94acceSBarry Smith    progress.
16459b94acceSBarry Smith 
1646fee21e36SBarry Smith    Collective on SNES
1647fee21e36SBarry Smith 
1648c7afd0dbSLois Curfman McInnes    Input Parameters:
1649c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1650c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1651b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1652c7afd0dbSLois Curfman McInnes           monitor routine (may be PETSC_NULL)
1653b8a78c4aSBarry Smith -  monitordestroy - options routine that frees monitor context
16549b94acceSBarry Smith 
1655c7afd0dbSLois Curfman McInnes    Calling sequence of func:
165640a0c1c6SLois Curfman McInnes $     int func(SNES snes,int its, double norm,void *mctx)
1657c7afd0dbSLois Curfman McInnes 
1658c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1659c7afd0dbSLois Curfman McInnes .    its - iteration number
1660c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
1661c7afd0dbSLois Curfman McInnes             for SNES_NONLINEAR_EQUATIONS methods
166240a0c1c6SLois Curfman McInnes .    norm - 2-norm gradient value (may be estimated)
1663c7afd0dbSLois Curfman McInnes             for SNES_UNCONSTRAINED_MINIMIZATION methods
166440a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
16659b94acceSBarry Smith 
16669665c990SLois Curfman McInnes    Options Database Keys:
1667c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1668c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1669c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1670c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1671c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1672c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1673c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1674c7afd0dbSLois Curfman McInnes                             the options database.
16759665c990SLois Curfman McInnes 
1676639f9d9dSBarry Smith    Notes:
16776bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
16786bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
16796bc08f3fSLois Curfman McInnes    order in which they were set.
1680639f9d9dSBarry Smith 
168136851e7fSLois Curfman McInnes    Level: intermediate
168236851e7fSLois Curfman McInnes 
16839b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
16849b94acceSBarry Smith 
16855cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
16869b94acceSBarry Smith @*/
1687b8a78c4aSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx,int (*monitordestroy)(void *))
16889b94acceSBarry Smith {
16893a40ed3dSBarry Smith   PetscFunctionBegin;
1690639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
1691a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set");
1692639f9d9dSBarry Smith   }
1693639f9d9dSBarry Smith 
1694639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1695b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1696639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
16973a40ed3dSBarry Smith   PetscFunctionReturn(0);
16989b94acceSBarry Smith }
16999b94acceSBarry Smith 
17005615d1e5SSatish Balay #undef __FUNC__
17015cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor"
17025cd90555SBarry Smith /*@C
17035cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
17045cd90555SBarry Smith 
1705c7afd0dbSLois Curfman McInnes    Collective on SNES
1706c7afd0dbSLois Curfman McInnes 
17075cd90555SBarry Smith    Input Parameters:
17085cd90555SBarry Smith .  snes - the SNES context
17095cd90555SBarry Smith 
17105cd90555SBarry Smith    Options Database:
1711c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1712c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1713c7afd0dbSLois Curfman McInnes     set via the options database
17145cd90555SBarry Smith 
17155cd90555SBarry Smith    Notes:
17165cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
17175cd90555SBarry Smith 
171836851e7fSLois Curfman McInnes    Level: intermediate
171936851e7fSLois Curfman McInnes 
17205cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
17215cd90555SBarry Smith 
17225cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
17235cd90555SBarry Smith @*/
17245cd90555SBarry Smith int SNESClearMonitor( SNES snes )
17255cd90555SBarry Smith {
17265cd90555SBarry Smith   PetscFunctionBegin;
17275cd90555SBarry Smith   snes->numbermonitors = 0;
17285cd90555SBarry Smith   PetscFunctionReturn(0);
17295cd90555SBarry Smith }
17305cd90555SBarry Smith 
17315cd90555SBarry Smith #undef __FUNC__
1732d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest"
17339b94acceSBarry Smith /*@C
17349b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
17359b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
17369b94acceSBarry Smith 
1737fee21e36SBarry Smith    Collective on SNES
1738fee21e36SBarry Smith 
1739c7afd0dbSLois Curfman McInnes    Input Parameters:
1740c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1741c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1742c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1743c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
17449b94acceSBarry Smith 
1745c7afd0dbSLois Curfman McInnes    Calling sequence of func:
17468d76a1e5SLois Curfman McInnes $     int func (SNES snes,double xnorm,double gnorm,double f,void *cctx)
1747c7afd0dbSLois Curfman McInnes 
1748c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1749c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1750c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1751c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods)
1752c7afd0dbSLois Curfman McInnes .    f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods)
1753c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods)
1754c7afd0dbSLois Curfman McInnes -    f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods)
17559b94acceSBarry Smith 
175636851e7fSLois Curfman McInnes    Level: advanced
175736851e7fSLois Curfman McInnes 
17589b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
17599b94acceSBarry Smith 
176040191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
176140191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
17629b94acceSBarry Smith @*/
176374679c65SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,void*),void *cctx)
17649b94acceSBarry Smith {
17653a40ed3dSBarry Smith   PetscFunctionBegin;
17669b94acceSBarry Smith   (snes)->converged = func;
17679b94acceSBarry Smith   (snes)->cnvP      = cctx;
17683a40ed3dSBarry Smith   PetscFunctionReturn(0);
17699b94acceSBarry Smith }
17709b94acceSBarry Smith 
17715615d1e5SSatish Balay #undef __FUNC__
1772d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory"
1773c9005455SLois Curfman McInnes /*@
1774c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1775c9005455SLois Curfman McInnes 
1776fee21e36SBarry Smith    Collective on SNES
1777fee21e36SBarry Smith 
1778c7afd0dbSLois Curfman McInnes    Input Parameters:
1779c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1780c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1781758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1782758f92a0SBarry Smith          negative if not converged) for each solve.
1783758f92a0SBarry Smith .  na  - size of a and its
1784758f92a0SBarry Smith -  reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero,
1785758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1786c7afd0dbSLois Curfman McInnes 
1787c9005455SLois Curfman McInnes    Notes:
1788c9005455SLois Curfman McInnes    If set, this array will contain the function norms (for
1789c9005455SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods) or gradient norms
1790c9005455SLois Curfman McInnes    (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed
1791c9005455SLois Curfman McInnes    at each step.
1792c9005455SLois Curfman McInnes 
1793c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1794c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1795c9005455SLois Curfman McInnes    during the section of code that is being timed.
1796c9005455SLois Curfman McInnes 
179736851e7fSLois Curfman McInnes    Level: intermediate
179836851e7fSLois Curfman McInnes 
1799c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1800758f92a0SBarry Smith 
180108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1802758f92a0SBarry Smith 
1803c9005455SLois Curfman McInnes @*/
1804758f92a0SBarry Smith int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset)
1805c9005455SLois Curfman McInnes {
18063a40ed3dSBarry Smith   PetscFunctionBegin;
1807c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1808c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1809c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1810758f92a0SBarry Smith   snes->conv_hist_its   = its;
1811758f92a0SBarry Smith   snes->conv_hist_max   = na;
1812758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1813758f92a0SBarry Smith   PetscFunctionReturn(0);
1814758f92a0SBarry Smith }
1815758f92a0SBarry Smith 
1816758f92a0SBarry Smith #undef __FUNC__
1817758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory"
18180c4c9dddSBarry Smith /*@C
1819758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1820758f92a0SBarry Smith 
1821758f92a0SBarry Smith    Collective on SNES
1822758f92a0SBarry Smith 
1823758f92a0SBarry Smith    Input Parameter:
1824758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1825758f92a0SBarry Smith 
1826758f92a0SBarry Smith    Output Parameters:
1827758f92a0SBarry Smith .  a   - array to hold history
1828758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1829758f92a0SBarry Smith          negative if not converged) for each solve.
1830758f92a0SBarry Smith -  na  - size of a and its
1831758f92a0SBarry Smith 
1832758f92a0SBarry Smith    Notes:
1833758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1834758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1835758f92a0SBarry Smith 
1836758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1837758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1838758f92a0SBarry Smith    during the section of code that is being timed.
1839758f92a0SBarry Smith 
1840758f92a0SBarry Smith    Level: intermediate
1841758f92a0SBarry Smith 
1842758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1843758f92a0SBarry Smith 
1844758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1845758f92a0SBarry Smith 
1846758f92a0SBarry Smith @*/
1847758f92a0SBarry Smith int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na)
1848758f92a0SBarry Smith {
1849758f92a0SBarry Smith   PetscFunctionBegin;
1850758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1851758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1852758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1853758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
18543a40ed3dSBarry Smith   PetscFunctionReturn(0);
1855c9005455SLois Curfman McInnes }
1856c9005455SLois Curfman McInnes 
1857c9005455SLois Curfman McInnes #undef __FUNC__
18585615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private"
18599b94acceSBarry Smith /*
18609b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
18619b94acceSBarry Smith    positive parameter delta.
18629b94acceSBarry Smith 
18639b94acceSBarry Smith     Input Parameters:
1864c7afd0dbSLois Curfman McInnes +   snes - the SNES context
18659b94acceSBarry Smith .   y - approximate solution of linear system
18669b94acceSBarry Smith .   fnorm - 2-norm of current function
1867c7afd0dbSLois Curfman McInnes -   delta - trust region size
18689b94acceSBarry Smith 
18699b94acceSBarry Smith     Output Parameters:
1870c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
18719b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
18729b94acceSBarry Smith     region, and exceeds zero otherwise.
1873c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
18749b94acceSBarry Smith 
18759b94acceSBarry Smith     Note:
187640191667SLois Curfman McInnes     For non-trust region methods such as SNES_EQ_LS, the parameter delta
18779b94acceSBarry Smith     is set to be the maximum allowable step size.
18789b94acceSBarry Smith 
18799b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
18809b94acceSBarry Smith */
18819b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta,
18829b94acceSBarry Smith                           double *gpnorm,double *ynorm)
18839b94acceSBarry Smith {
18849b94acceSBarry Smith   double norm;
18859b94acceSBarry Smith   Scalar cnorm;
18863a40ed3dSBarry Smith   int    ierr;
18873a40ed3dSBarry Smith 
18883a40ed3dSBarry Smith   PetscFunctionBegin;
18893a40ed3dSBarry Smith   ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr);
18909b94acceSBarry Smith   if (norm > *delta) {
18919b94acceSBarry Smith      norm = *delta/norm;
18929b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
18939b94acceSBarry Smith      cnorm = norm;
18949b94acceSBarry Smith      VecScale( &cnorm, y );
18959b94acceSBarry Smith      *ynorm = *delta;
18969b94acceSBarry Smith   } else {
18979b94acceSBarry Smith      *gpnorm = 0.0;
18989b94acceSBarry Smith      *ynorm = norm;
18999b94acceSBarry Smith   }
19003a40ed3dSBarry Smith   PetscFunctionReturn(0);
19019b94acceSBarry Smith }
19029b94acceSBarry Smith 
19035615d1e5SSatish Balay #undef __FUNC__
19045615d1e5SSatish Balay #define __FUNC__ "SNESSolve"
19059b94acceSBarry Smith /*@
19069b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
190763a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
19089b94acceSBarry Smith 
1909c7afd0dbSLois Curfman McInnes    Collective on SNES
1910c7afd0dbSLois Curfman McInnes 
1911b2002411SLois Curfman McInnes    Input Parameters:
1912c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1913c7afd0dbSLois Curfman McInnes -  x - the solution vector
19149b94acceSBarry Smith 
19159b94acceSBarry Smith    Output Parameter:
1916b2002411SLois Curfman McInnes .  its - number of iterations until termination
19179b94acceSBarry Smith 
1918b2002411SLois Curfman McInnes    Notes:
19198ddd3da0SLois Curfman McInnes    The user should initialize the vector, x, with the initial guess
19208ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
19218ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
19228ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
19238ddd3da0SLois Curfman McInnes 
192436851e7fSLois Curfman McInnes    Level: beginner
192536851e7fSLois Curfman McInnes 
19269b94acceSBarry Smith .keywords: SNES, nonlinear, solve
19279b94acceSBarry Smith 
192863a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
19299b94acceSBarry Smith @*/
19308ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
19319b94acceSBarry Smith {
19323c7409f5SSatish Balay   int ierr, flg;
1933052efed2SBarry Smith 
19343a40ed3dSBarry Smith   PetscFunctionBegin;
193577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
193674679c65SBarry Smith   PetscValidIntPointer(its);
193782bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
1938c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
1939758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
19409b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
1941c96a6f78SLois Curfman McInnes   snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0;
19429b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr);
19439b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
19443c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg);CHKERRQ(ierr);
19456d4a8577SBarry Smith   if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); }
19463a40ed3dSBarry Smith   PetscFunctionReturn(0);
19479b94acceSBarry Smith }
19489b94acceSBarry Smith 
19499b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
19509b94acceSBarry Smith 
19515615d1e5SSatish Balay #undef __FUNC__
19525615d1e5SSatish Balay #define __FUNC__ "SNESSetType"
195382bf6240SBarry Smith /*@C
19544b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
19559b94acceSBarry Smith 
1956fee21e36SBarry Smith    Collective on SNES
1957fee21e36SBarry Smith 
1958c7afd0dbSLois Curfman McInnes    Input Parameters:
1959c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1960c7afd0dbSLois Curfman McInnes -  method - a known method
1961c7afd0dbSLois Curfman McInnes 
1962c7afd0dbSLois Curfman McInnes    Options Database Key:
1963c7afd0dbSLois Curfman McInnes .  -snes_type <method> - Sets the method; use -help for a list
1964c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1965ae12b187SLois Curfman McInnes 
19669b94acceSBarry Smith    Notes:
19679b94acceSBarry Smith    See "petsc/include/snes.h" for available methods (for instance)
196836851e7fSLois Curfman McInnes +    SNES_EQ_LS - Newton's method with line search
1969c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
1970c7afd0dbSLois Curfman McInnes .    SNES_EQ_TR - Newton's method with trust region
1971c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
1972c7afd0dbSLois Curfman McInnes .    SNES_UM_TR - Newton's method with trust region
1973c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
197436851e7fSLois Curfman McInnes -    SNES_UM_LS - Newton's method with line search
1975c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
19769b94acceSBarry Smith 
1977ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1978ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1979ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1980ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1981ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1982ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1983ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1984ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1985ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
198636851e7fSLois Curfman McInnes   appropriate method.  In other words, this routine is not for beginners.
198736851e7fSLois Curfman McInnes 
198836851e7fSLois Curfman McInnes   Level: intermediate
1989a703fe33SLois Curfman McInnes 
1990f536c699SSatish Balay .keywords: SNES, set, method
19919b94acceSBarry Smith @*/
19924b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method)
19939b94acceSBarry Smith {
199484cb2905SBarry Smith   int ierr;
19959b94acceSBarry Smith   int (*r)(SNES);
19963a40ed3dSBarry Smith 
19973a40ed3dSBarry Smith   PetscFunctionBegin;
199877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
199982bf6240SBarry Smith 
20003f1db9ecSBarry Smith   if (PetscTypeCompare(snes->type_name,method)) PetscFunctionReturn(0);
200182bf6240SBarry Smith 
200282bf6240SBarry Smith   if (snes->setupcalled) {
2003e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
200482bf6240SBarry Smith     snes->data = 0;
200582bf6240SBarry Smith   }
20067d1a2b2bSBarry Smith 
20079b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
200882bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
200982bf6240SBarry Smith 
2010488ecbafSBarry Smith   ierr =  FListFind(snes->comm, SNESList, method,(int (**)(void *)) &r );CHKERRQ(ierr);
201182bf6240SBarry Smith 
2012596552b5SBarry Smith   if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",method);
20131548b14aSBarry Smith 
2014606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
201582bf6240SBarry Smith   snes->data = 0;
20163a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
201782bf6240SBarry Smith 
2018be0abb6dSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,method);CHKERRQ(ierr);
201982bf6240SBarry Smith   snes->set_method_called = 1;
202082bf6240SBarry Smith 
20213a40ed3dSBarry Smith   PetscFunctionReturn(0);
20229b94acceSBarry Smith }
20239b94acceSBarry Smith 
2024a847f771SSatish Balay 
20259b94acceSBarry Smith /* --------------------------------------------------------------------- */
20265615d1e5SSatish Balay #undef __FUNC__
2027d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy"
20289b94acceSBarry Smith /*@C
20299b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
20309b94acceSBarry Smith    registered by SNESRegister().
20319b94acceSBarry Smith 
2032fee21e36SBarry Smith    Not Collective
2033fee21e36SBarry Smith 
203436851e7fSLois Curfman McInnes    Level: advanced
203536851e7fSLois Curfman McInnes 
20369b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
20379b94acceSBarry Smith 
20389b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
20399b94acceSBarry Smith @*/
2040cf256101SBarry Smith int SNESRegisterDestroy(void)
20419b94acceSBarry Smith {
204282bf6240SBarry Smith   int ierr;
204382bf6240SBarry Smith 
20443a40ed3dSBarry Smith   PetscFunctionBegin;
204582bf6240SBarry Smith   if (SNESList) {
2046488ecbafSBarry Smith     ierr = FListDestroy( SNESList );CHKERRQ(ierr);
204782bf6240SBarry Smith     SNESList = 0;
20489b94acceSBarry Smith   }
204984cb2905SBarry Smith   SNESRegisterAllCalled = 0;
20503a40ed3dSBarry Smith   PetscFunctionReturn(0);
20519b94acceSBarry Smith }
20529b94acceSBarry Smith 
20535615d1e5SSatish Balay #undef __FUNC__
2054d4bb536fSBarry Smith #define __FUNC__ "SNESGetType"
20559b94acceSBarry Smith /*@C
20569a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
20579b94acceSBarry Smith 
2058c7afd0dbSLois Curfman McInnes    Not Collective
2059c7afd0dbSLois Curfman McInnes 
20609b94acceSBarry Smith    Input Parameter:
20614b0e389bSBarry Smith .  snes - nonlinear solver context
20629b94acceSBarry Smith 
20639b94acceSBarry Smith    Output Parameter:
206482bf6240SBarry Smith .  method - SNES method (a charactor string)
20659b94acceSBarry Smith 
206636851e7fSLois Curfman McInnes    Level: intermediate
206736851e7fSLois Curfman McInnes 
20689b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name
20699b94acceSBarry Smith @*/
207082bf6240SBarry Smith int SNESGetType(SNES snes, SNESType *method)
20719b94acceSBarry Smith {
20723a40ed3dSBarry Smith   PetscFunctionBegin;
207382bf6240SBarry Smith   *method = snes->type_name;
20743a40ed3dSBarry Smith   PetscFunctionReturn(0);
20759b94acceSBarry Smith }
20769b94acceSBarry Smith 
20775615d1e5SSatish Balay #undef __FUNC__
2078d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution"
20799b94acceSBarry Smith /*@C
20809b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20819b94acceSBarry Smith    stored.
20829b94acceSBarry Smith 
2083c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2084c7afd0dbSLois Curfman McInnes 
20859b94acceSBarry Smith    Input Parameter:
20869b94acceSBarry Smith .  snes - the SNES context
20879b94acceSBarry Smith 
20889b94acceSBarry Smith    Output Parameter:
20899b94acceSBarry Smith .  x - the solution
20909b94acceSBarry Smith 
209136851e7fSLois Curfman McInnes    Level: advanced
209236851e7fSLois Curfman McInnes 
20939b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20949b94acceSBarry Smith 
2095a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
20969b94acceSBarry Smith @*/
20979b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
20989b94acceSBarry Smith {
20993a40ed3dSBarry Smith   PetscFunctionBegin;
210077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21019b94acceSBarry Smith   *x = snes->vec_sol_always;
21023a40ed3dSBarry Smith   PetscFunctionReturn(0);
21039b94acceSBarry Smith }
21049b94acceSBarry Smith 
21055615d1e5SSatish Balay #undef __FUNC__
2106d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate"
21079b94acceSBarry Smith /*@C
21089b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
21099b94acceSBarry Smith    stored.
21109b94acceSBarry Smith 
2111c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2112c7afd0dbSLois Curfman McInnes 
21139b94acceSBarry Smith    Input Parameter:
21149b94acceSBarry Smith .  snes - the SNES context
21159b94acceSBarry Smith 
21169b94acceSBarry Smith    Output Parameter:
21179b94acceSBarry Smith .  x - the solution update
21189b94acceSBarry Smith 
211936851e7fSLois Curfman McInnes    Level: advanced
212036851e7fSLois Curfman McInnes 
21219b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
21229b94acceSBarry Smith 
21239b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
21249b94acceSBarry Smith @*/
21259b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
21269b94acceSBarry Smith {
21273a40ed3dSBarry Smith   PetscFunctionBegin;
212877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21299b94acceSBarry Smith   *x = snes->vec_sol_update_always;
21303a40ed3dSBarry Smith   PetscFunctionReturn(0);
21319b94acceSBarry Smith }
21329b94acceSBarry Smith 
21335615d1e5SSatish Balay #undef __FUNC__
2134d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction"
21359b94acceSBarry Smith /*@C
21363638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
21379b94acceSBarry Smith 
2138c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2139c7afd0dbSLois Curfman McInnes 
21409b94acceSBarry Smith    Input Parameter:
21419b94acceSBarry Smith .  snes - the SNES context
21429b94acceSBarry Smith 
21439b94acceSBarry Smith    Output Parameter:
21447bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
21457bf4e008SBarry Smith -  ctx - the function context (or PETSC_NULL)
21469b94acceSBarry Smith 
21479b94acceSBarry Smith    Notes:
21489b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
21499b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
21509b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
21519b94acceSBarry Smith 
215236851e7fSLois Curfman McInnes    Level: advanced
215336851e7fSLois Curfman McInnes 
2154a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
21559b94acceSBarry Smith 
215631615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
215731615d04SLois Curfman McInnes           SNESGetGradient()
21587bf4e008SBarry Smith 
21599b94acceSBarry Smith @*/
21607bf4e008SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx)
21619b94acceSBarry Smith {
21623a40ed3dSBarry Smith   PetscFunctionBegin;
216377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2164a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
2165a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
2166a8c6a408SBarry Smith   }
21677bf4e008SBarry Smith   if (r)   *r = snes->vec_func_always;
21687bf4e008SBarry Smith   if (ctx) *ctx = snes->funP;
21693a40ed3dSBarry Smith   PetscFunctionReturn(0);
21709b94acceSBarry Smith }
21719b94acceSBarry Smith 
21725615d1e5SSatish Balay #undef __FUNC__
2173d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient"
21749b94acceSBarry Smith /*@C
21753638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
21769b94acceSBarry Smith 
2177c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2178c7afd0dbSLois Curfman McInnes 
21799b94acceSBarry Smith    Input Parameter:
21809b94acceSBarry Smith .  snes - the SNES context
21819b94acceSBarry Smith 
21829b94acceSBarry Smith    Output Parameter:
21837bf4e008SBarry Smith +  r - the gradient (or PETSC_NULL)
21847bf4e008SBarry Smith -  ctx - the gradient context (or PETSC_NULL)
21859b94acceSBarry Smith 
21869b94acceSBarry Smith    Notes:
21879b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
21889b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
21899b94acceSBarry Smith    SNESGetFunction().
21909b94acceSBarry Smith 
219136851e7fSLois Curfman McInnes    Level: advanced
219236851e7fSLois Curfman McInnes 
21939b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
21949b94acceSBarry Smith 
21957bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(),
21967bf4e008SBarry Smith           SNESSetGradient(), SNESSetFunction()
21977bf4e008SBarry Smith 
21989b94acceSBarry Smith @*/
21997bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx)
22009b94acceSBarry Smith {
22013a40ed3dSBarry Smith   PetscFunctionBegin;
220277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
22033a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2204a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
22053a40ed3dSBarry Smith   }
22067bf4e008SBarry Smith   if (r)   *r = snes->vec_func_always;
22077bf4e008SBarry Smith   if (ctx) *ctx = snes->funP;
22083a40ed3dSBarry Smith   PetscFunctionReturn(0);
22099b94acceSBarry Smith }
22109b94acceSBarry Smith 
22115615d1e5SSatish Balay #undef __FUNC__
2212d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction"
22137bf4e008SBarry Smith /*@C
22149b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
22159b94acceSBarry Smith    unconstrained minimization problems.
22169b94acceSBarry Smith 
2217c7afd0dbSLois Curfman McInnes    Not Collective
2218c7afd0dbSLois Curfman McInnes 
22199b94acceSBarry Smith    Input Parameter:
22209b94acceSBarry Smith .  snes - the SNES context
22219b94acceSBarry Smith 
22229b94acceSBarry Smith    Output Parameter:
22237bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
22247bf4e008SBarry Smith -  ctx - the function context (or PETSC_NULL)
22259b94acceSBarry Smith 
22269b94acceSBarry Smith    Notes:
22279b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
22289b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
22299b94acceSBarry Smith    SNESGetFunction().
22309b94acceSBarry Smith 
223136851e7fSLois Curfman McInnes    Level: advanced
223236851e7fSLois Curfman McInnes 
22339b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
22349b94acceSBarry Smith 
22357bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction()
22367bf4e008SBarry Smith 
22379b94acceSBarry Smith @*/
22387bf4e008SBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r,void **ctx)
22399b94acceSBarry Smith {
22403a40ed3dSBarry Smith   PetscFunctionBegin;
224177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
224274679c65SBarry Smith   PetscValidScalarPointer(r);
22433a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2244a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
22453a40ed3dSBarry Smith   }
22467bf4e008SBarry Smith   if (r)   *r = snes->fc;
22477bf4e008SBarry Smith   if (ctx) *ctx = snes->umfunP;
22483a40ed3dSBarry Smith   PetscFunctionReturn(0);
22499b94acceSBarry Smith }
22509b94acceSBarry Smith 
22515615d1e5SSatish Balay #undef __FUNC__
2252d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix"
22533c7409f5SSatish Balay /*@C
22543c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2255d850072dSLois Curfman McInnes    SNES options in the database.
22563c7409f5SSatish Balay 
2257fee21e36SBarry Smith    Collective on SNES
2258fee21e36SBarry Smith 
2259c7afd0dbSLois Curfman McInnes    Input Parameter:
2260c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2261c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2262c7afd0dbSLois Curfman McInnes 
2263d850072dSLois Curfman McInnes    Notes:
2264a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2265c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2266d850072dSLois Curfman McInnes 
226736851e7fSLois Curfman McInnes    Level: advanced
226836851e7fSLois Curfman McInnes 
22693c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2270a86d99e1SLois Curfman McInnes 
2271a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
22723c7409f5SSatish Balay @*/
22733c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
22743c7409f5SSatish Balay {
22753c7409f5SSatish Balay   int ierr;
22763c7409f5SSatish Balay 
22773a40ed3dSBarry Smith   PetscFunctionBegin;
227877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2279639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr);
22803c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
22813a40ed3dSBarry Smith   PetscFunctionReturn(0);
22823c7409f5SSatish Balay }
22833c7409f5SSatish Balay 
22845615d1e5SSatish Balay #undef __FUNC__
2285d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix"
22863c7409f5SSatish Balay /*@C
2287f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2288d850072dSLois Curfman McInnes    SNES options in the database.
22893c7409f5SSatish Balay 
2290fee21e36SBarry Smith    Collective on SNES
2291fee21e36SBarry Smith 
2292c7afd0dbSLois Curfman McInnes    Input Parameters:
2293c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2294c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2295c7afd0dbSLois Curfman McInnes 
2296d850072dSLois Curfman McInnes    Notes:
2297a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2298c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2299d850072dSLois Curfman McInnes 
230036851e7fSLois Curfman McInnes    Level: advanced
230136851e7fSLois Curfman McInnes 
23023c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2303a86d99e1SLois Curfman McInnes 
2304a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
23053c7409f5SSatish Balay @*/
23063c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
23073c7409f5SSatish Balay {
23083c7409f5SSatish Balay   int ierr;
23093c7409f5SSatish Balay 
23103a40ed3dSBarry Smith   PetscFunctionBegin;
231177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2312639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr);
23133c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
23143a40ed3dSBarry Smith   PetscFunctionReturn(0);
23153c7409f5SSatish Balay }
23163c7409f5SSatish Balay 
23175615d1e5SSatish Balay #undef __FUNC__
2318d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix"
23199ab63eb5SSatish Balay /*@C
23203c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
23213c7409f5SSatish Balay    SNES options in the database.
23223c7409f5SSatish Balay 
2323c7afd0dbSLois Curfman McInnes    Not Collective
2324c7afd0dbSLois Curfman McInnes 
23253c7409f5SSatish Balay    Input Parameter:
23263c7409f5SSatish Balay .  snes - the SNES context
23273c7409f5SSatish Balay 
23283c7409f5SSatish Balay    Output Parameter:
23293c7409f5SSatish Balay .  prefix - pointer to the prefix string used
23303c7409f5SSatish Balay 
23319ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
23329ab63eb5SSatish Balay    sufficient length to hold the prefix.
23339ab63eb5SSatish Balay 
233436851e7fSLois Curfman McInnes    Level: advanced
233536851e7fSLois Curfman McInnes 
23363c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2337a86d99e1SLois Curfman McInnes 
2338a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
23393c7409f5SSatish Balay @*/
23403c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
23413c7409f5SSatish Balay {
23423c7409f5SSatish Balay   int ierr;
23433c7409f5SSatish Balay 
23443a40ed3dSBarry Smith   PetscFunctionBegin;
234577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2346639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr);
23473a40ed3dSBarry Smith   PetscFunctionReturn(0);
23483c7409f5SSatish Balay }
23493c7409f5SSatish Balay 
2350ca161407SBarry Smith #undef __FUNC__
2351ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp"
2352ca161407SBarry Smith /*@
2353ca161407SBarry Smith    SNESPrintHelp - Prints all options for the SNES component.
2354ca161407SBarry Smith 
2355c7afd0dbSLois Curfman McInnes    Collective on SNES
2356c7afd0dbSLois Curfman McInnes 
2357ca161407SBarry Smith    Input Parameter:
2358ca161407SBarry Smith .  snes - the SNES context
2359ca161407SBarry Smith 
2360ca161407SBarry Smith    Options Database Keys:
2361c7afd0dbSLois Curfman McInnes +  -help - Prints SNES options
2362c7afd0dbSLois Curfman McInnes -  -h - Prints SNES options
2363ca161407SBarry Smith 
236436851e7fSLois Curfman McInnes    Level: beginner
236536851e7fSLois Curfman McInnes 
2366ca161407SBarry Smith .keywords: SNES, nonlinear, help
2367ca161407SBarry Smith 
2368ca161407SBarry Smith .seealso: SNESSetFromOptions()
2369ca161407SBarry Smith @*/
2370ca161407SBarry Smith int SNESPrintHelp(SNES snes)
2371ca161407SBarry Smith {
2372ca161407SBarry Smith   char                p[64];
2373ca161407SBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
2374ca161407SBarry Smith   int                 ierr;
2375ca161407SBarry Smith 
2376ca161407SBarry Smith   PetscFunctionBegin;
2377ca161407SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2378ca161407SBarry Smith 
2379549d3d68SSatish Balay   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
2380ca161407SBarry Smith   if (snes->prefix) PetscStrcat(p, snes->prefix);
2381ca161407SBarry Smith 
2382ca161407SBarry Smith   kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
2383ca161407SBarry Smith 
2384ebb8b11fSBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
2385d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr);
2386488ecbafSBarry Smith   ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr);
2387d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr);
2388d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr);
2389d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr);
2390d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr);
2391d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr);
2392d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr);
2393d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr);
2394d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr);
2395d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr);
2396d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_monitor: use default SNES convergence monitor, prints\n\
2397d132466eSBarry Smith     residual norm at each iteration.\n",p);CHKERRQ(ierr);
2398d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_smonitor: same as the above, but prints fewer digits of the\n\
2399ca161407SBarry Smith     residual norm for small residual norms. This is useful to conceal\n\
2400d132466eSBarry Smith     meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr);
2401d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr);
2402d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr);
2403d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr);
2404ca161407SBarry Smith   if (snes->type == SNES_NONLINEAR_EQUATIONS) {
2405d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2406d132466eSBarry Smith      " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr);
2407d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr);
2408d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr);
2409d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr);
2410d132466eSBarry 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);
2411d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr);
2412d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr);
2413d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2414d132466eSBarry Smith      "     %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr);
2415d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2416d132466eSBarry Smith      "     %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr);
2417d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2418d132466eSBarry Smith      "     %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr);
2419d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2420d132466eSBarry Smith      "     %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr);
2421d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2422d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr);
2423d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2424d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr);
2425d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2426d132466eSBarry Smith      "     %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr);
2427ca161407SBarry Smith   } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) {
2428d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr);
2429d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr);
2430d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr);
2431d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr);
2432ca161407SBarry Smith   }
2433d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <method> for help on ",p);CHKERRQ(ierr);
2434d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr);
2435ca161407SBarry Smith   if (snes->printhelp) {
2436ca161407SBarry Smith     ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr);
2437ca161407SBarry Smith   }
2438ca161407SBarry Smith   PetscFunctionReturn(0);
2439ca161407SBarry Smith }
24403c7409f5SSatish Balay 
2441acb85ae6SSatish Balay /*MC
2442b2002411SLois Curfman McInnes    SNESRegister - Adds a method to the nonlinear solver package.
24439b94acceSBarry Smith 
2444b2002411SLois Curfman McInnes    Synopsis:
2445b2002411SLois Curfman McInnes    SNESRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
24469b94acceSBarry Smith 
24478d76a1e5SLois Curfman McInnes    Not collective
24488d76a1e5SLois Curfman McInnes 
2449b2002411SLois Curfman McInnes    Input Parameters:
2450c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2451b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2452b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2453c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
24549b94acceSBarry Smith 
2455b2002411SLois Curfman McInnes    Notes:
2456b2002411SLois Curfman McInnes    SNESRegister() may be called multiple times to add several user-defined solvers.
24573a40ed3dSBarry Smith 
2458b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2459b2002411SLois Curfman McInnes    is ignored.
2460b2002411SLois Curfman McInnes 
2461b2002411SLois Curfman McInnes    Sample usage:
246269225d78SLois Curfman McInnes .vb
2463b2002411SLois Curfman McInnes    SNESRegister("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2464b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
246569225d78SLois Curfman McInnes .ve
2466b2002411SLois Curfman McInnes 
2467b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2468b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2469b2002411SLois Curfman McInnes    or at runtime via the option
2470b2002411SLois Curfman McInnes $     -snes_type my_solver
2471b2002411SLois Curfman McInnes 
247236851e7fSLois Curfman McInnes    Level: advanced
247336851e7fSLois Curfman McInnes 
247485614651SBarry Smith    $PETSC_ARCH, $PETSC_DIR, $PETSC_LDIR, and $BOPT occuring in pathname will be replaced with appropriate values.
2475dd438238SBarry Smith 
2476b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2477b2002411SLois Curfman McInnes 
2478b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2479b2002411SLois Curfman McInnes M*/
2480b2002411SLois Curfman McInnes 
2481b2002411SLois Curfman McInnes #undef __FUNC__
2482b2002411SLois Curfman McInnes #define __FUNC__ "SNESRegister_Private"
2483b2002411SLois Curfman McInnes int SNESRegister_Private(char *sname,char *path,char *name,int (*function)(SNES))
2484b2002411SLois Curfman McInnes {
2485b2002411SLois Curfman McInnes   char fullname[256];
2486b2002411SLois Curfman McInnes   int  ierr;
2487b2002411SLois Curfman McInnes 
2488b2002411SLois Curfman McInnes   PetscFunctionBegin;
2489549d3d68SSatish Balay   ierr = PetscStrcpy(fullname,path);CHKERRQ(ierr);
2490549d3d68SSatish Balay   PetscStrcat(fullname,":");PetscStrcat(fullname,name);
2491488ecbafSBarry Smith   ierr = FListAdd_Private(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr);
2492b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2493b2002411SLois Curfman McInnes }
2494