xref: /petsc/src/snes/interface/snes.c (revision be0abb6ddea392ceaee217d3645fed7c6ea71822)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*be0abb6dSBarry Smith static char vcid[] = "$Id: snes.c,v 1.191 1999/06/30 23:54:03 balay Exp bsmith $";
39b94acceSBarry Smith #endif
49b94acceSBarry Smith 
570f55243SBarry Smith #include "src/snes/snesimpl.h"      /*I "snes.h"  I*/
69b94acceSBarry Smith 
784cb2905SBarry Smith int SNESRegisterAllCalled = 0;
8488ecbafSBarry Smith FList SNESList = 0;
982bf6240SBarry Smith 
105615d1e5SSatish Balay #undef __FUNC__
11d4bb536fSBarry Smith #define __FUNC__ "SNESView"
129b94acceSBarry Smith /*@
139b94acceSBarry Smith    SNESView - Prints the SNES data structure.
149b94acceSBarry Smith 
15fee21e36SBarry Smith    Collective on SNES, unless Viewer is VIEWER_STDOUT_SELF
16fee21e36SBarry Smith 
17c7afd0dbSLois Curfman McInnes    Input Parameters:
18c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
19c7afd0dbSLois Curfman McInnes -  viewer - visualization context
20c7afd0dbSLois Curfman McInnes 
219b94acceSBarry Smith    Options Database Key:
22c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
239b94acceSBarry Smith 
249b94acceSBarry Smith    Notes:
259b94acceSBarry Smith    The available visualization contexts include
26c7afd0dbSLois Curfman McInnes +     VIEWER_STDOUT_SELF - standard output (default)
27c7afd0dbSLois Curfman McInnes -     VIEWER_STDOUT_WORLD - synchronized standard
28c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
29c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
30c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
319b94acceSBarry Smith 
323e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
3377ed5343SBarry Smith    ViewerASCIIOpen() - output to a specified file.
349b94acceSBarry Smith 
3536851e7fSLois Curfman McInnes    Level: beginner
3636851e7fSLois Curfman McInnes 
379b94acceSBarry Smith .keywords: SNES, view
389b94acceSBarry Smith 
3977ed5343SBarry Smith .seealso: ViewerASCIIOpen()
409b94acceSBarry Smith @*/
419b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer)
429b94acceSBarry Smith {
439b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
449b94acceSBarry Smith   int                 ierr;
459b94acceSBarry Smith   SLES                sles;
469b94acceSBarry Smith   char                *method;
4719bcc07fSBarry Smith   ViewerType          vtype;
489b94acceSBarry Smith 
493a40ed3dSBarry Smith   PetscFunctionBegin;
5074679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5174679c65SBarry Smith   if (viewer) {PetscValidHeader(viewer);}
5274679c65SBarry Smith   else { viewer = VIEWER_STDOUT_SELF; }
5374679c65SBarry Smith 
5419bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype);CHKERRQ(ierr);
553f1db9ecSBarry Smith   if (PetscTypeCompare(vtype,ASCII_VIEWER)) {
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;
597e24b481bSBarry Smith 
598e24b481bSBarry Smith   PetscFunctionBegin;
599e24b481bSBarry Smith 
600e24b481bSBarry Smith   /* if it is already published then return */
601e24b481bSBarry Smith   if (v->amem >=0 ) PetscFunctionReturn(0);
602e24b481bSBarry Smith 
6033f1db9ecSBarry Smith   ierr = PetscObjectPublishBaseBegin(object);CHKERRQ(ierr);
604e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
605e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
606e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
607e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
608e24b481bSBarry Smith   ierr = PetscObjectPublishBaseEnd(object);CHKERRQ(ierr);
609e24b481bSBarry Smith #else
610e24b481bSBarry Smith   PetscFunctionBegin;
611e24b481bSBarry Smith #endif
612e24b481bSBarry Smith   PetscFunctionReturn(0);
613e24b481bSBarry Smith }
614e24b481bSBarry Smith 
6159b94acceSBarry Smith /* -----------------------------------------------------------*/
6165615d1e5SSatish Balay #undef __FUNC__
6175615d1e5SSatish Balay #define __FUNC__ "SNESCreate"
6189b94acceSBarry Smith /*@C
6199b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
6209b94acceSBarry Smith 
621c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
622c7afd0dbSLois Curfman McInnes 
623c7afd0dbSLois Curfman McInnes    Input Parameters:
624c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
625c7afd0dbSLois Curfman McInnes -  type - type of method, either
626c7afd0dbSLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
627c7afd0dbSLois Curfman McInnes    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
6289b94acceSBarry Smith 
6299b94acceSBarry Smith    Output Parameter:
6309b94acceSBarry Smith .  outsnes - the new SNES context
6319b94acceSBarry Smith 
632c7afd0dbSLois Curfman McInnes    Options Database Keys:
633c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
634c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
635c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
636c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
637c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
638c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
639c1f60f51SBarry Smith 
64036851e7fSLois Curfman McInnes    Level: beginner
64136851e7fSLois Curfman McInnes 
6429b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
6439b94acceSBarry Smith 
64463a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy()
6459b94acceSBarry Smith @*/
6464b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
6479b94acceSBarry Smith {
6489b94acceSBarry Smith   int                 ierr;
6499b94acceSBarry Smith   SNES                snes;
6509b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
65137fcc0dbSBarry Smith 
6523a40ed3dSBarry Smith   PetscFunctionBegin;
6539b94acceSBarry Smith   *outsnes = 0;
654d64ed03dSBarry Smith   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){
655d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type");
656d64ed03dSBarry Smith   }
6573f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
6589b94acceSBarry Smith   PLogObjectCreate(snes);
659e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6609b94acceSBarry Smith   snes->max_its           = 50;
6619750a799SBarry Smith   snes->max_funcs	  = 10000;
6629b94acceSBarry Smith   snes->norm		  = 0.0;
6635a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
664b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
665b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
6669b94acceSBarry Smith     snes->atol		  = 1.e-10;
667d64ed03dSBarry Smith   } else {
668b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
669b4874afaSBarry Smith     snes->ttol            = 0.0;
670b4874afaSBarry Smith     snes->atol		  = 1.e-50;
671b4874afaSBarry Smith   }
6729b94acceSBarry Smith   snes->xtol		  = 1.e-8;
673b18e04deSLois Curfman McInnes   snes->trunctol	  = 1.e-12; /* no longer used */
6749b94acceSBarry Smith   snes->nfuncs            = 0;
6759b94acceSBarry Smith   snes->nfailures         = 0;
6767a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
677639f9d9dSBarry Smith   snes->numbermonitors    = 0;
6789b94acceSBarry Smith   snes->data              = 0;
6799b94acceSBarry Smith   snes->view              = 0;
6809b94acceSBarry Smith   snes->computeumfunction = 0;
6819b94acceSBarry Smith   snes->umfunP            = 0;
6829b94acceSBarry Smith   snes->fc                = 0;
6839b94acceSBarry Smith   snes->deltatol          = 1.e-12;
6849b94acceSBarry Smith   snes->fmin              = -1.e30;
6859b94acceSBarry Smith   snes->method_class      = type;
6869b94acceSBarry Smith   snes->set_method_called = 0;
68782bf6240SBarry Smith   snes->setupcalled      = 0;
6889b94acceSBarry Smith   snes->ksp_ewconv        = 0;
6896f24a144SLois Curfman McInnes   snes->vwork             = 0;
6906f24a144SLois Curfman McInnes   snes->nwork             = 0;
691758f92a0SBarry Smith   snes->conv_hist_len     = 0;
692758f92a0SBarry Smith   snes->conv_hist_max     = 0;
693758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
694758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
695758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
6969b94acceSBarry Smith 
6979b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
6980452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx);CHKPTRQ(kctx);
699eed86810SBarry Smith   PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
7009b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7019b94acceSBarry Smith   kctx->version     = 2;
7029b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7039b94acceSBarry Smith                              this was too large for some test cases */
7049b94acceSBarry Smith   kctx->rtol_last   = 0;
7059b94acceSBarry Smith   kctx->rtol_max    = .9;
7069b94acceSBarry Smith   kctx->gamma       = 1.0;
7079b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7089b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7099b94acceSBarry Smith   kctx->threshold   = .1;
7109b94acceSBarry Smith   kctx->lresid_last = 0;
7119b94acceSBarry Smith   kctx->norm_last   = 0;
7129b94acceSBarry Smith 
7139b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr);
7149b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
7155334005bSBarry Smith 
7169b94acceSBarry Smith   *outsnes = snes;
717e24b481bSBarry Smith   PetscPublishAll(snes);
7183a40ed3dSBarry Smith   PetscFunctionReturn(0);
7199b94acceSBarry Smith }
7209b94acceSBarry Smith 
7219b94acceSBarry Smith /* --------------------------------------------------------------- */
7225615d1e5SSatish Balay #undef __FUNC__
723d4bb536fSBarry Smith #define __FUNC__ "SNESSetFunction"
7249b94acceSBarry Smith /*@C
7259b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
7269b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
7279b94acceSBarry Smith    equations.
7289b94acceSBarry Smith 
729fee21e36SBarry Smith    Collective on SNES
730fee21e36SBarry Smith 
731c7afd0dbSLois Curfman McInnes    Input Parameters:
732c7afd0dbSLois Curfman McInnes +  snes - the SNES context
733c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
734c7afd0dbSLois Curfman McInnes .  r - vector to store function value
735c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
736c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7379b94acceSBarry Smith 
738c7afd0dbSLois Curfman McInnes    Calling sequence of func:
7398d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
740c7afd0dbSLois Curfman McInnes 
741313e4042SLois Curfman McInnes .  f - function vector
742c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
7439b94acceSBarry Smith 
7449b94acceSBarry Smith    Notes:
7459b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
7469b94acceSBarry Smith $      f'(x) x = -f(x),
747c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
7489b94acceSBarry Smith 
7499b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7509b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7519b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
7529b94acceSBarry Smith 
75336851e7fSLois Curfman McInnes    Level: beginner
75436851e7fSLois Curfman McInnes 
7559b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
7569b94acceSBarry Smith 
757a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
7589b94acceSBarry Smith @*/
7595334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx)
7609b94acceSBarry Smith {
7613a40ed3dSBarry Smith   PetscFunctionBegin;
76277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
763a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
764a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
765a8c6a408SBarry Smith   }
7669b94acceSBarry Smith   snes->computefunction     = func;
7679b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7689b94acceSBarry Smith   snes->funP                = ctx;
7693a40ed3dSBarry Smith   PetscFunctionReturn(0);
7709b94acceSBarry Smith }
7719b94acceSBarry Smith 
7725615d1e5SSatish Balay #undef __FUNC__
7735615d1e5SSatish Balay #define __FUNC__ "SNESComputeFunction"
7749b94acceSBarry Smith /*@
77536851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
7769b94acceSBarry Smith                          SNESSetFunction().
7779b94acceSBarry Smith 
778c7afd0dbSLois Curfman McInnes    Collective on SNES
779c7afd0dbSLois Curfman McInnes 
7809b94acceSBarry Smith    Input Parameters:
781c7afd0dbSLois Curfman McInnes +  snes - the SNES context
782c7afd0dbSLois Curfman McInnes -  x - input vector
7839b94acceSBarry Smith 
7849b94acceSBarry Smith    Output Parameter:
7853638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
7869b94acceSBarry Smith 
7871bffabb2SLois Curfman McInnes    Notes:
7889b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7899b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7909b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
7919b94acceSBarry Smith 
79236851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
79336851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
79436851e7fSLois Curfman McInnes    themselves.
79536851e7fSLois Curfman McInnes 
79636851e7fSLois Curfman McInnes    Level: developer
79736851e7fSLois Curfman McInnes 
7989b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
7999b94acceSBarry Smith 
800a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
8019b94acceSBarry Smith @*/
8029b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y)
8039b94acceSBarry Smith {
8049b94acceSBarry Smith   int    ierr;
8059b94acceSBarry Smith 
8063a40ed3dSBarry Smith   PetscFunctionBegin;
807d4bb536fSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
808a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
809d4bb536fSBarry Smith   }
8109b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
811d64ed03dSBarry Smith   PetscStackPush("SNES user function");
8129b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
813d64ed03dSBarry Smith   PetscStackPop;
814ae3c334cSLois Curfman McInnes   snes->nfuncs++;
8159b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
8163a40ed3dSBarry Smith   PetscFunctionReturn(0);
8179b94acceSBarry Smith }
8189b94acceSBarry Smith 
8195615d1e5SSatish Balay #undef __FUNC__
820d4bb536fSBarry Smith #define __FUNC__ "SNESSetMinimizationFunction"
8219b94acceSBarry Smith /*@C
8229b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
8239b94acceSBarry Smith    unconstrained minimization.
8249b94acceSBarry Smith 
825fee21e36SBarry Smith    Collective on SNES
826fee21e36SBarry Smith 
827c7afd0dbSLois Curfman McInnes    Input Parameters:
828c7afd0dbSLois Curfman McInnes +  snes - the SNES context
829c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
830c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
831c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8329b94acceSBarry Smith 
833c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8348d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,double *f,void *ctx);
835c7afd0dbSLois Curfman McInnes 
836c7afd0dbSLois Curfman McInnes +  x - input vector
8379b94acceSBarry Smith .  f - function
838c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined function context
8399b94acceSBarry Smith 
84036851e7fSLois Curfman McInnes    Level: beginner
84136851e7fSLois Curfman McInnes 
8429b94acceSBarry Smith    Notes:
8439b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
8449b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
8459b94acceSBarry Smith    SNESSetFunction().
8469b94acceSBarry Smith 
8479b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
8489b94acceSBarry Smith 
849a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
850a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
8519b94acceSBarry Smith @*/
8529b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),
8539b94acceSBarry Smith                       void *ctx)
8549b94acceSBarry Smith {
8553a40ed3dSBarry Smith   PetscFunctionBegin;
85677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
857a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
858a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
859a8c6a408SBarry Smith   }
8609b94acceSBarry Smith   snes->computeumfunction   = func;
8619b94acceSBarry Smith   snes->umfunP              = ctx;
8623a40ed3dSBarry Smith   PetscFunctionReturn(0);
8639b94acceSBarry Smith }
8649b94acceSBarry Smith 
8655615d1e5SSatish Balay #undef __FUNC__
8665615d1e5SSatish Balay #define __FUNC__ "SNESComputeMinimizationFunction"
8679b94acceSBarry Smith /*@
8689b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
8699b94acceSBarry Smith    set with SNESSetMinimizationFunction().
8709b94acceSBarry Smith 
871c7afd0dbSLois Curfman McInnes    Collective on SNES
872c7afd0dbSLois Curfman McInnes 
8739b94acceSBarry Smith    Input Parameters:
874c7afd0dbSLois Curfman McInnes +  snes - the SNES context
875c7afd0dbSLois Curfman McInnes -  x - input vector
8769b94acceSBarry Smith 
8779b94acceSBarry Smith    Output Parameter:
8789b94acceSBarry Smith .  y - function value
8799b94acceSBarry Smith 
8809b94acceSBarry Smith    Notes:
8819b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
8829b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
8839b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
884a86d99e1SLois Curfman McInnes 
88536851e7fSLois Curfman McInnes    SNESComputeMinimizationFunction() is typically used within minimization
88636851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
88736851e7fSLois Curfman McInnes    themselves.
88836851e7fSLois Curfman McInnes 
88936851e7fSLois Curfman McInnes    Level: developer
89036851e7fSLois Curfman McInnes 
891a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
892a86d99e1SLois Curfman McInnes 
893a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
894a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
8959b94acceSBarry Smith @*/
8969b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y)
8979b94acceSBarry Smith {
8989b94acceSBarry Smith   int    ierr;
8993a40ed3dSBarry Smith 
9003a40ed3dSBarry Smith   PetscFunctionBegin;
901a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
902a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
903a8c6a408SBarry Smith   }
9049b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
905d64ed03dSBarry Smith   PetscStackPush("SNES user minimzation function");
9069b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr);
907d64ed03dSBarry Smith   PetscStackPop;
908ae3c334cSLois Curfman McInnes   snes->nfuncs++;
9099b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
9103a40ed3dSBarry Smith   PetscFunctionReturn(0);
9119b94acceSBarry Smith }
9129b94acceSBarry Smith 
9135615d1e5SSatish Balay #undef __FUNC__
914d4bb536fSBarry Smith #define __FUNC__ "SNESSetGradient"
9159b94acceSBarry Smith /*@C
9169b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
9179b94acceSBarry Smith    vector for use by the SNES routines.
9189b94acceSBarry Smith 
919c7afd0dbSLois Curfman McInnes    Collective on SNES
920c7afd0dbSLois Curfman McInnes 
9219b94acceSBarry Smith    Input Parameters:
922c7afd0dbSLois Curfman McInnes +  snes - the SNES context
9239b94acceSBarry Smith .  func - function evaluation routine
924044dda88SLois Curfman McInnes .  ctx - optional user-defined context for private data for the
925044dda88SLois Curfman McInnes          gradient evaluation routine (may be PETSC_NULL)
926c7afd0dbSLois Curfman McInnes -  r - vector to store gradient value
927fee21e36SBarry Smith 
9289b94acceSBarry Smith    Calling sequence of func:
9298d76a1e5SLois Curfman McInnes $     func (SNES, Vec x, Vec g, void *ctx);
9309b94acceSBarry Smith 
931c7afd0dbSLois Curfman McInnes +  x - input vector
9329b94acceSBarry Smith .  g - gradient vector
933c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined gradient context
9349b94acceSBarry Smith 
9359b94acceSBarry Smith    Notes:
9369b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
9379b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
9389b94acceSBarry Smith    SNESSetFunction().
9399b94acceSBarry Smith 
94036851e7fSLois Curfman McInnes    Level: beginner
94136851e7fSLois Curfman McInnes 
9429b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9439b94acceSBarry Smith 
944a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
945a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
9469b94acceSBarry Smith @*/
94774679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
9489b94acceSBarry Smith {
9493a40ed3dSBarry Smith   PetscFunctionBegin;
95077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
951a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
952a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
953a8c6a408SBarry Smith   }
9549b94acceSBarry Smith   snes->computefunction     = func;
9559b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
9569b94acceSBarry Smith   snes->funP                = ctx;
9573a40ed3dSBarry Smith   PetscFunctionReturn(0);
9589b94acceSBarry Smith }
9599b94acceSBarry Smith 
9605615d1e5SSatish Balay #undef __FUNC__
9615615d1e5SSatish Balay #define __FUNC__ "SNESComputeGradient"
9629b94acceSBarry Smith /*@
963a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
964a86d99e1SLois Curfman McInnes    SNESSetGradient().
9659b94acceSBarry Smith 
966c7afd0dbSLois Curfman McInnes    Collective on SNES
967c7afd0dbSLois Curfman McInnes 
9689b94acceSBarry Smith    Input Parameters:
969c7afd0dbSLois Curfman McInnes +  snes - the SNES context
970c7afd0dbSLois Curfman McInnes -  x - input vector
9719b94acceSBarry Smith 
9729b94acceSBarry Smith    Output Parameter:
9739b94acceSBarry Smith .  y - gradient vector
9749b94acceSBarry Smith 
9759b94acceSBarry Smith    Notes:
9769b94acceSBarry Smith    SNESComputeGradient() is valid only for
9779b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
9789b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
979a86d99e1SLois Curfman McInnes 
98036851e7fSLois Curfman McInnes    SNESComputeGradient() is typically used within minimization
98136851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
98236851e7fSLois Curfman McInnes    themselves.
98336851e7fSLois Curfman McInnes 
98436851e7fSLois Curfman McInnes    Level: developer
98536851e7fSLois Curfman McInnes 
986a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
987a86d99e1SLois Curfman McInnes 
988a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
989a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
9909b94acceSBarry Smith @*/
9919b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y)
9929b94acceSBarry Smith {
9939b94acceSBarry Smith   int    ierr;
9943a40ed3dSBarry Smith 
9953a40ed3dSBarry Smith   PetscFunctionBegin;
9963a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
997a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
9983a40ed3dSBarry Smith   }
9999b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
1000d64ed03dSBarry Smith   PetscStackPush("SNES user gradient function");
10019b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1002d64ed03dSBarry Smith   PetscStackPop;
10039b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
10043a40ed3dSBarry Smith   PetscFunctionReturn(0);
10059b94acceSBarry Smith }
10069b94acceSBarry Smith 
10075615d1e5SSatish Balay #undef __FUNC__
10085615d1e5SSatish Balay #define __FUNC__ "SNESComputeJacobian"
100962fef451SLois Curfman McInnes /*@
101062fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
101162fef451SLois Curfman McInnes    set with SNESSetJacobian().
101262fef451SLois Curfman McInnes 
1013c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1014c7afd0dbSLois Curfman McInnes 
101562fef451SLois Curfman McInnes    Input Parameters:
1016c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1017c7afd0dbSLois Curfman McInnes -  x - input vector
101862fef451SLois Curfman McInnes 
101962fef451SLois Curfman McInnes    Output Parameters:
1020c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
102162fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1022c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1023fee21e36SBarry Smith 
102462fef451SLois Curfman McInnes    Notes:
102562fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
102662fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
102762fef451SLois Curfman McInnes 
1028dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1029dc5a77f8SLois Curfman McInnes    flag parameter.
103062fef451SLois Curfman McInnes 
103162fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
103262fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
1033005c665bSBarry Smith    methods is SNESComputeHessian().
103462fef451SLois Curfman McInnes 
103536851e7fSLois Curfman McInnes    SNESComputeJacobian() is typically used within nonlinear solver
103636851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
103736851e7fSLois Curfman McInnes    themselves.
103836851e7fSLois Curfman McInnes 
103936851e7fSLois Curfman McInnes    Level: developer
104036851e7fSLois Curfman McInnes 
104162fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
104262fef451SLois Curfman McInnes 
104362fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
104462fef451SLois Curfman McInnes @*/
10459b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10469b94acceSBarry Smith {
10479b94acceSBarry Smith   int    ierr;
10483a40ed3dSBarry Smith 
10493a40ed3dSBarry Smith   PetscFunctionBegin;
10503a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1051a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
10523a40ed3dSBarry Smith   }
10533a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
10549b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
1055c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1056d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
10579b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1058d64ed03dSBarry Smith   PetscStackPop;
10599b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
10606d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
106177c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
106277c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
10633a40ed3dSBarry Smith   PetscFunctionReturn(0);
10649b94acceSBarry Smith }
10659b94acceSBarry Smith 
10665615d1e5SSatish Balay #undef __FUNC__
10675615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian"
106862fef451SLois Curfman McInnes /*@
106962fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
107062fef451SLois Curfman McInnes    set with SNESSetHessian().
107162fef451SLois Curfman McInnes 
1072c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1073c7afd0dbSLois Curfman McInnes 
107462fef451SLois Curfman McInnes    Input Parameters:
1075c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1076c7afd0dbSLois Curfman McInnes -  x - input vector
107762fef451SLois Curfman McInnes 
107862fef451SLois Curfman McInnes    Output Parameters:
1079c7afd0dbSLois Curfman McInnes +  A - Hessian matrix
108062fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1081c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1082fee21e36SBarry Smith 
108362fef451SLois Curfman McInnes    Notes:
108462fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
108562fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
108662fef451SLois Curfman McInnes 
1087dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1088dc5a77f8SLois Curfman McInnes    flag parameter.
108962fef451SLois Curfman McInnes 
109062fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
109162fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
109262fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
109362fef451SLois Curfman McInnes 
109436851e7fSLois Curfman McInnes    SNESComputeHessian() is typically used within minimization
109536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
109636851e7fSLois Curfman McInnes    themselves.
109736851e7fSLois Curfman McInnes 
109836851e7fSLois Curfman McInnes    Level: developer
109936851e7fSLois Curfman McInnes 
110062fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
110162fef451SLois Curfman McInnes 
1102a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
1103a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
110462fef451SLois Curfman McInnes @*/
110562fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
11069b94acceSBarry Smith {
11079b94acceSBarry Smith   int    ierr;
11083a40ed3dSBarry Smith 
11093a40ed3dSBarry Smith   PetscFunctionBegin;
11103a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1111a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
11123a40ed3dSBarry Smith   }
11133a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
111462fef451SLois Curfman McInnes   PLogEventBegin(SNES_HessianEval,snes,x,*A,*B);
11150f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
1116d64ed03dSBarry Smith   PetscStackPush("SNES user Hessian function");
111762fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr);
1118d64ed03dSBarry Smith   PetscStackPop;
111962fef451SLois Curfman McInnes   PLogEventEnd(SNES_HessianEval,snes,x,*A,*B);
11200f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
112177c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
112277c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11233a40ed3dSBarry Smith   PetscFunctionReturn(0);
11249b94acceSBarry Smith }
11259b94acceSBarry Smith 
11265615d1e5SSatish Balay #undef __FUNC__
1127d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian"
11289b94acceSBarry Smith /*@C
11299b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1130044dda88SLois Curfman McInnes    location to store the matrix.
11319b94acceSBarry Smith 
1132c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1133c7afd0dbSLois Curfman McInnes 
11349b94acceSBarry Smith    Input Parameters:
1135c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11369b94acceSBarry Smith .  A - Jacobian matrix
11379b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11389b94acceSBarry Smith .  func - Jacobian evaluation routine
1139c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11402cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11419b94acceSBarry Smith 
11429b94acceSBarry Smith    Calling sequence of func:
11438d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11449b94acceSBarry Smith 
1145c7afd0dbSLois Curfman McInnes +  x - input vector
11469b94acceSBarry Smith .  A - Jacobian matrix
11479b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1148ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1149ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1150c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11519b94acceSBarry Smith 
11529b94acceSBarry Smith    Notes:
1153dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
11542cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1155ac21db08SLois Curfman McInnes 
1156ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11579b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11589b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11599b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11609b94acceSBarry Smith    throughout the global iterations.
11619b94acceSBarry Smith 
116236851e7fSLois Curfman McInnes    Level: beginner
116336851e7fSLois Curfman McInnes 
11649b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11659b94acceSBarry Smith 
1166ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
11679b94acceSBarry Smith @*/
11689b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
11699b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
11709b94acceSBarry Smith {
11713a40ed3dSBarry Smith   PetscFunctionBegin;
117277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1173a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1174a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
1175a8c6a408SBarry Smith   }
11769b94acceSBarry Smith   snes->computejacobian = func;
11779b94acceSBarry Smith   snes->jacP            = ctx;
11789b94acceSBarry Smith   snes->jacobian        = A;
11799b94acceSBarry Smith   snes->jacobian_pre    = B;
11803a40ed3dSBarry Smith   PetscFunctionReturn(0);
11819b94acceSBarry Smith }
118262fef451SLois Curfman McInnes 
11835615d1e5SSatish Balay #undef __FUNC__
1184d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian"
1185c2aafc4cSSatish Balay /*@C
1186b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1187b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1188b4fd4287SBarry Smith 
1189c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1190c7afd0dbSLois Curfman McInnes 
1191b4fd4287SBarry Smith    Input Parameter:
1192b4fd4287SBarry Smith .  snes - the nonlinear solver context
1193b4fd4287SBarry Smith 
1194b4fd4287SBarry Smith    Output Parameters:
1195c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1196b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
1197c7afd0dbSLois Curfman McInnes -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1198fee21e36SBarry Smith 
119936851e7fSLois Curfman McInnes    Level: advanced
120036851e7fSLois Curfman McInnes 
1201b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1202b4fd4287SBarry Smith @*/
1203b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx)
1204b4fd4287SBarry Smith {
12053a40ed3dSBarry Smith   PetscFunctionBegin;
12063a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1207a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
12083a40ed3dSBarry Smith   }
1209b4fd4287SBarry Smith   if (A)   *A = snes->jacobian;
1210b4fd4287SBarry Smith   if (B)   *B = snes->jacobian_pre;
1211b4fd4287SBarry Smith   if (ctx) *ctx = snes->jacP;
12123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1213b4fd4287SBarry Smith }
1214b4fd4287SBarry Smith 
12155615d1e5SSatish Balay #undef __FUNC__
1216d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian"
12179b94acceSBarry Smith /*@C
12189b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
1219044dda88SLois Curfman McInnes    location to store the matrix.
12209b94acceSBarry Smith 
1221c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1222c7afd0dbSLois Curfman McInnes 
12239b94acceSBarry Smith    Input Parameters:
1224c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12259b94acceSBarry Smith .  A - Hessian matrix
12269b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
12279b94acceSBarry Smith .  func - Jacobian evaluation routine
1228c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1229313e4042SLois Curfman McInnes          Hessian evaluation routine (may be PETSC_NULL)
12309b94acceSBarry Smith 
12319b94acceSBarry Smith    Calling sequence of func:
12328d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12339b94acceSBarry Smith 
1234c7afd0dbSLois Curfman McInnes +  x - input vector
12359b94acceSBarry Smith .  A - Hessian matrix
12369b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1237ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1238ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1239c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Hessian context
12409b94acceSBarry Smith 
12419b94acceSBarry Smith    Notes:
1242dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
12432cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1244ac21db08SLois Curfman McInnes 
12459b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
12469b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
12479b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12489b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12499b94acceSBarry Smith    throughout the global iterations.
12509b94acceSBarry Smith 
125136851e7fSLois Curfman McInnes    Level: beginner
125236851e7fSLois Curfman McInnes 
12539b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
12549b94acceSBarry Smith 
1255ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
12569b94acceSBarry Smith @*/
12579b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
12589b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
12599b94acceSBarry Smith {
12603a40ed3dSBarry Smith   PetscFunctionBegin;
126177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1262d4bb536fSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1263a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1264d4bb536fSBarry Smith   }
12659b94acceSBarry Smith   snes->computejacobian = func;
12669b94acceSBarry Smith   snes->jacP            = ctx;
12679b94acceSBarry Smith   snes->jacobian        = A;
12689b94acceSBarry Smith   snes->jacobian_pre    = B;
12693a40ed3dSBarry Smith   PetscFunctionReturn(0);
12709b94acceSBarry Smith }
12719b94acceSBarry Smith 
12725615d1e5SSatish Balay #undef __FUNC__
1273d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian"
127462fef451SLois Curfman McInnes /*@
127562fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
127662fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
127762fef451SLois Curfman McInnes 
1278c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object is parallel if SNES object is parallel
1279c7afd0dbSLois Curfman McInnes 
128062fef451SLois Curfman McInnes    Input Parameter:
128162fef451SLois Curfman McInnes .  snes - the nonlinear solver context
128262fef451SLois Curfman McInnes 
128362fef451SLois Curfman McInnes    Output Parameters:
1284c7afd0dbSLois Curfman McInnes +  A - location to stash Hessian matrix (or PETSC_NULL)
128562fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
1286c7afd0dbSLois Curfman McInnes -  ctx - location to stash Hessian ctx (or PETSC_NULL)
1287fee21e36SBarry Smith 
128836851e7fSLois Curfman McInnes    Level: advanced
128936851e7fSLois Curfman McInnes 
129062fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
1291c7afd0dbSLois Curfman McInnes 
1292c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian
129362fef451SLois Curfman McInnes @*/
129462fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx)
129562fef451SLois Curfman McInnes {
12963a40ed3dSBarry Smith   PetscFunctionBegin;
12973a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){
1298a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
12993a40ed3dSBarry Smith   }
130062fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
130162fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
130262fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
13033a40ed3dSBarry Smith   PetscFunctionReturn(0);
130462fef451SLois Curfman McInnes }
130562fef451SLois Curfman McInnes 
13069b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13079b94acceSBarry Smith 
13085615d1e5SSatish Balay #undef __FUNC__
13095615d1e5SSatish Balay #define __FUNC__ "SNESSetUp"
13109b94acceSBarry Smith /*@
13119b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1312272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13139b94acceSBarry Smith 
1314fee21e36SBarry Smith    Collective on SNES
1315fee21e36SBarry Smith 
1316c7afd0dbSLois Curfman McInnes    Input Parameters:
1317c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1318c7afd0dbSLois Curfman McInnes -  x - the solution vector
1319c7afd0dbSLois Curfman McInnes 
1320272ac6f2SLois Curfman McInnes    Notes:
1321272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1322272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1323272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1324272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1325272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1326272ac6f2SLois Curfman McInnes 
132736851e7fSLois Curfman McInnes    Level: advanced
132836851e7fSLois Curfman McInnes 
13299b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13309b94acceSBarry Smith 
13319b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
13329b94acceSBarry Smith @*/
13338ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
13349b94acceSBarry Smith {
1335272ac6f2SLois Curfman McInnes   int ierr, flg;
13363a40ed3dSBarry Smith 
13373a40ed3dSBarry Smith   PetscFunctionBegin;
133877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
133977c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
13408ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
13419b94acceSBarry Smith 
1342c1f60f51SBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);CHKERRQ(ierr);
1343c1f60f51SBarry Smith   /*
1344c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1345dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1346c1f60f51SBarry Smith   */
1347c1f60f51SBarry Smith   if (flg) {
1348c1f60f51SBarry Smith     Mat J;
13495a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1350c1f60f51SBarry Smith     PLogObjectParent(snes,J);
1351c1f60f51SBarry Smith     snes->mfshell = J;
1352c1f60f51SBarry Smith     snes->jacobian = J;
1353c2aafc4cSSatish Balay     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1354c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n");
1355d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
1356c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n");
1357d4bb536fSBarry Smith     } else {
1358a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option");
1359d4bb536fSBarry Smith     }
13605a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1361c1f60f51SBarry Smith   }
1362272ac6f2SLois Curfman McInnes   ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);CHKERRQ(ierr);
1363c1f60f51SBarry Smith   /*
1364dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1365c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1366c1f60f51SBarry Smith    */
136731615d04SLois Curfman McInnes   if (flg) {
1368272ac6f2SLois Curfman McInnes     Mat J;
13695a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1370272ac6f2SLois Curfman McInnes     PLogObjectParent(snes,J);
1371272ac6f2SLois Curfman McInnes     snes->mfshell = J;
137283e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
137383e56afdSLois Curfman McInnes       ierr = SNESSetJacobian(snes,J,J,0,snes->funP);CHKERRQ(ierr);
137494a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n");
1375d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
137683e56afdSLois Curfman McInnes       ierr = SNESSetHessian(snes,J,J,0,snes->funP);CHKERRQ(ierr);
137794a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n");
1378d4bb536fSBarry Smith     } else {
1379a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option");
1380d4bb536fSBarry Smith     }
13815a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1382272ac6f2SLois Curfman McInnes   }
13839b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
1384a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
1385a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
13865a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1387a8c6a408SBarry Smith     if (snes->vec_func == snes->vec_sol) {
1388a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector");
1389a8c6a408SBarry Smith     }
1390a703fe33SLois Curfman McInnes 
1391a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
139282bf6240SBarry Smith     if (snes->ksp_ewconv && PetscStrcmp(snes->type_name,SNES_EQ_TR)) {
1393a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1394a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1395a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
13965a655dc6SBarry Smith       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr);
1397a703fe33SLois Curfman McInnes     }
13989b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
1399a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1400a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1401a8c6a408SBarry Smith     if (!snes->computeumfunction) {
1402a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first");
1403a8c6a408SBarry Smith     }
14045a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()");
1405d4bb536fSBarry Smith   } else {
1406a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class");
1407d4bb536fSBarry Smith   }
1408a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
140982bf6240SBarry Smith   snes->setupcalled = 1;
14103a40ed3dSBarry Smith   PetscFunctionReturn(0);
14119b94acceSBarry Smith }
14129b94acceSBarry Smith 
14135615d1e5SSatish Balay #undef __FUNC__
1414d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy"
14159b94acceSBarry Smith /*@C
14169b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
14179b94acceSBarry Smith    with SNESCreate().
14189b94acceSBarry Smith 
1419c7afd0dbSLois Curfman McInnes    Collective on SNES
1420c7afd0dbSLois Curfman McInnes 
14219b94acceSBarry Smith    Input Parameter:
14229b94acceSBarry Smith .  snes - the SNES context
14239b94acceSBarry Smith 
142436851e7fSLois Curfman McInnes    Level: beginner
142536851e7fSLois Curfman McInnes 
14269b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14279b94acceSBarry Smith 
142863a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14299b94acceSBarry Smith @*/
14309b94acceSBarry Smith int SNESDestroy(SNES snes)
14319b94acceSBarry Smith {
1432b8a78c4aSBarry Smith   int i,ierr;
14333a40ed3dSBarry Smith 
14343a40ed3dSBarry Smith   PetscFunctionBegin;
143577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
14363a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1437d4bb536fSBarry Smith 
1438*be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
1439*be0abb6dSBarry Smith   ierr = PetscAMSDestroy(snes);CHKERRQ(ierr);
1440*be0abb6dSBarry Smith 
1441e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1442606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
1443522c5e43SBarry Smith   if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);}
14449b94acceSBarry Smith   ierr = SLESDestroy(snes->sles);CHKERRQ(ierr);
1445522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1446b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++ ) {
1447b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1448b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1449b8a78c4aSBarry Smith     }
1450b8a78c4aSBarry Smith   }
14519b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
14520452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
14533a40ed3dSBarry Smith   PetscFunctionReturn(0);
14549b94acceSBarry Smith }
14559b94acceSBarry Smith 
14569b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14579b94acceSBarry Smith 
14585615d1e5SSatish Balay #undef __FUNC__
14595615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances"
14609b94acceSBarry Smith /*@
1461d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
14629b94acceSBarry Smith 
1463c7afd0dbSLois Curfman McInnes    Collective on SNES
1464c7afd0dbSLois Curfman McInnes 
14659b94acceSBarry Smith    Input Parameters:
1466c7afd0dbSLois Curfman McInnes +  snes - the SNES context
146733174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
146833174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
146933174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
147033174efeSLois Curfman McInnes            of the change in the solution between steps
147133174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1472c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1473fee21e36SBarry Smith 
147433174efeSLois Curfman McInnes    Options Database Keys:
1475c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1476c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1477c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1478c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1479c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
14809b94acceSBarry Smith 
1481d7a720efSLois Curfman McInnes    Notes:
14829b94acceSBarry Smith    The default maximum number of iterations is 50.
14839b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
14849b94acceSBarry Smith 
148536851e7fSLois Curfman McInnes    Level: intermediate
148636851e7fSLois Curfman McInnes 
148733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
14889b94acceSBarry Smith 
1489d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
14909b94acceSBarry Smith @*/
1491d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf)
14929b94acceSBarry Smith {
14933a40ed3dSBarry Smith   PetscFunctionBegin;
149477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1495d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1496d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1497d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1498d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1499d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15003a40ed3dSBarry Smith   PetscFunctionReturn(0);
15019b94acceSBarry Smith }
15029b94acceSBarry Smith 
15035615d1e5SSatish Balay #undef __FUNC__
15045615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances"
15059b94acceSBarry Smith /*@
150633174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
150733174efeSLois Curfman McInnes 
1508c7afd0dbSLois Curfman McInnes    Not Collective
1509c7afd0dbSLois Curfman McInnes 
151033174efeSLois Curfman McInnes    Input Parameters:
1511c7afd0dbSLois Curfman McInnes +  snes - the SNES context
151233174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
151333174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
151433174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
151533174efeSLois Curfman McInnes            of the change in the solution between steps
151633174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1517c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1518fee21e36SBarry Smith 
151933174efeSLois Curfman McInnes    Notes:
152033174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
152133174efeSLois Curfman McInnes 
152236851e7fSLois Curfman McInnes    Level: intermediate
152336851e7fSLois Curfman McInnes 
152433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
152533174efeSLois Curfman McInnes 
152633174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
152733174efeSLois Curfman McInnes @*/
152833174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf)
152933174efeSLois Curfman McInnes {
15303a40ed3dSBarry Smith   PetscFunctionBegin;
153133174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
153233174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
153333174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
153433174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
153533174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
153633174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
15373a40ed3dSBarry Smith   PetscFunctionReturn(0);
153833174efeSLois Curfman McInnes }
153933174efeSLois Curfman McInnes 
15405615d1e5SSatish Balay #undef __FUNC__
15415615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance"
154233174efeSLois Curfman McInnes /*@
15439b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
15449b94acceSBarry Smith 
1545fee21e36SBarry Smith    Collective on SNES
1546fee21e36SBarry Smith 
1547c7afd0dbSLois Curfman McInnes    Input Parameters:
1548c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1549c7afd0dbSLois Curfman McInnes -  tol - tolerance
1550c7afd0dbSLois Curfman McInnes 
15519b94acceSBarry Smith    Options Database Key:
1552c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
15539b94acceSBarry Smith 
155436851e7fSLois Curfman McInnes    Level: intermediate
155536851e7fSLois Curfman McInnes 
15569b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
15579b94acceSBarry Smith 
1558d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
15599b94acceSBarry Smith @*/
15609b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol)
15619b94acceSBarry Smith {
15623a40ed3dSBarry Smith   PetscFunctionBegin;
156377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15649b94acceSBarry Smith   snes->deltatol = tol;
15653a40ed3dSBarry Smith   PetscFunctionReturn(0);
15669b94acceSBarry Smith }
15679b94acceSBarry Smith 
15685615d1e5SSatish Balay #undef __FUNC__
15695615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance"
15709b94acceSBarry Smith /*@
157177c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
15729b94acceSBarry Smith    for unconstrained minimization solvers.
15739b94acceSBarry Smith 
1574fee21e36SBarry Smith    Collective on SNES
1575fee21e36SBarry Smith 
1576c7afd0dbSLois Curfman McInnes    Input Parameters:
1577c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1578c7afd0dbSLois Curfman McInnes -  ftol - minimum function tolerance
1579c7afd0dbSLois Curfman McInnes 
15809b94acceSBarry Smith    Options Database Key:
1581c7afd0dbSLois Curfman McInnes .  -snes_fmin <ftol> - Sets ftol
15829b94acceSBarry Smith 
15839b94acceSBarry Smith    Note:
158477c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
15859b94acceSBarry Smith    methods only.
15869b94acceSBarry Smith 
158736851e7fSLois Curfman McInnes    Level: intermediate
158836851e7fSLois Curfman McInnes 
15899b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
15909b94acceSBarry Smith 
1591d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
15929b94acceSBarry Smith @*/
159377c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol)
15949b94acceSBarry Smith {
15953a40ed3dSBarry Smith   PetscFunctionBegin;
159677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15979b94acceSBarry Smith   snes->fmin = ftol;
15983a40ed3dSBarry Smith   PetscFunctionReturn(0);
15999b94acceSBarry Smith }
1600df9fa365SBarry Smith /*
1601df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1602df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1603df9fa365SBarry Smith    macros instead of functions
1604df9fa365SBarry Smith */
1605ce1608b8SBarry Smith #undef __FUNC__
1606ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor"
1607ce1608b8SBarry Smith int SNESLGMonitor(SNES snes,int it,double norm,void *ctx)
1608ce1608b8SBarry Smith {
1609ce1608b8SBarry Smith   int ierr;
1610ce1608b8SBarry Smith 
1611ce1608b8SBarry Smith   PetscFunctionBegin;
1612ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1613ce1608b8SBarry Smith   PetscFunctionReturn(0);
1614ce1608b8SBarry Smith }
1615ce1608b8SBarry Smith 
1616df9fa365SBarry Smith #undef __FUNC__
1617df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorCreate"
1618df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n, DrawLG *draw)
1619df9fa365SBarry Smith {
1620df9fa365SBarry Smith   int ierr;
1621df9fa365SBarry Smith 
1622df9fa365SBarry Smith   PetscFunctionBegin;
1623df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1624df9fa365SBarry Smith   PetscFunctionReturn(0);
1625df9fa365SBarry Smith }
1626df9fa365SBarry Smith 
1627df9fa365SBarry Smith #undef __FUNC__
1628df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorDestroy"
1629df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw)
1630df9fa365SBarry Smith {
1631df9fa365SBarry Smith   int ierr;
1632df9fa365SBarry Smith 
1633df9fa365SBarry Smith   PetscFunctionBegin;
1634df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1635df9fa365SBarry Smith   PetscFunctionReturn(0);
1636df9fa365SBarry Smith }
1637df9fa365SBarry Smith 
16389b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
16399b94acceSBarry Smith 
16405615d1e5SSatish Balay #undef __FUNC__
1641d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor"
16429b94acceSBarry Smith /*@C
16435cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
16449b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
16459b94acceSBarry Smith    progress.
16469b94acceSBarry Smith 
1647fee21e36SBarry Smith    Collective on SNES
1648fee21e36SBarry Smith 
1649c7afd0dbSLois Curfman McInnes    Input Parameters:
1650c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1651c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1652b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1653c7afd0dbSLois Curfman McInnes           monitor routine (may be PETSC_NULL)
1654b8a78c4aSBarry Smith -  monitordestroy - options routine that frees monitor context
16559b94acceSBarry Smith 
1656c7afd0dbSLois Curfman McInnes    Calling sequence of func:
165740a0c1c6SLois Curfman McInnes $     int func(SNES snes,int its, double norm,void *mctx)
1658c7afd0dbSLois Curfman McInnes 
1659c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1660c7afd0dbSLois Curfman McInnes .    its - iteration number
1661c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
1662c7afd0dbSLois Curfman McInnes             for SNES_NONLINEAR_EQUATIONS methods
166340a0c1c6SLois Curfman McInnes .    norm - 2-norm gradient value (may be estimated)
1664c7afd0dbSLois Curfman McInnes             for SNES_UNCONSTRAINED_MINIMIZATION methods
166540a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
16669b94acceSBarry Smith 
16679665c990SLois Curfman McInnes    Options Database Keys:
1668c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1669c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1670c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1671c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1672c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1673c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1674c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1675c7afd0dbSLois Curfman McInnes                             the options database.
16769665c990SLois Curfman McInnes 
1677639f9d9dSBarry Smith    Notes:
16786bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
16796bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
16806bc08f3fSLois Curfman McInnes    order in which they were set.
1681639f9d9dSBarry Smith 
168236851e7fSLois Curfman McInnes    Level: intermediate
168336851e7fSLois Curfman McInnes 
16849b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
16859b94acceSBarry Smith 
16865cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
16879b94acceSBarry Smith @*/
1688b8a78c4aSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx,int (*monitordestroy)(void *))
16899b94acceSBarry Smith {
16903a40ed3dSBarry Smith   PetscFunctionBegin;
1691639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
1692a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set");
1693639f9d9dSBarry Smith   }
1694639f9d9dSBarry Smith 
1695639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1696b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1697639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
16983a40ed3dSBarry Smith   PetscFunctionReturn(0);
16999b94acceSBarry Smith }
17009b94acceSBarry Smith 
17015615d1e5SSatish Balay #undef __FUNC__
17025cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor"
17035cd90555SBarry Smith /*@C
17045cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
17055cd90555SBarry Smith 
1706c7afd0dbSLois Curfman McInnes    Collective on SNES
1707c7afd0dbSLois Curfman McInnes 
17085cd90555SBarry Smith    Input Parameters:
17095cd90555SBarry Smith .  snes - the SNES context
17105cd90555SBarry Smith 
17115cd90555SBarry Smith    Options Database:
1712c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1713c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1714c7afd0dbSLois Curfman McInnes     set via the options database
17155cd90555SBarry Smith 
17165cd90555SBarry Smith    Notes:
17175cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
17185cd90555SBarry Smith 
171936851e7fSLois Curfman McInnes    Level: intermediate
172036851e7fSLois Curfman McInnes 
17215cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
17225cd90555SBarry Smith 
17235cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
17245cd90555SBarry Smith @*/
17255cd90555SBarry Smith int SNESClearMonitor( SNES snes )
17265cd90555SBarry Smith {
17275cd90555SBarry Smith   PetscFunctionBegin;
17285cd90555SBarry Smith   snes->numbermonitors = 0;
17295cd90555SBarry Smith   PetscFunctionReturn(0);
17305cd90555SBarry Smith }
17315cd90555SBarry Smith 
17325cd90555SBarry Smith #undef __FUNC__
1733d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest"
17349b94acceSBarry Smith /*@C
17359b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
17369b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
17379b94acceSBarry Smith 
1738fee21e36SBarry Smith    Collective on SNES
1739fee21e36SBarry Smith 
1740c7afd0dbSLois Curfman McInnes    Input Parameters:
1741c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1742c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1743c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1744c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
17459b94acceSBarry Smith 
1746c7afd0dbSLois Curfman McInnes    Calling sequence of func:
17478d76a1e5SLois Curfman McInnes $     int func (SNES snes,double xnorm,double gnorm,double f,void *cctx)
1748c7afd0dbSLois Curfman McInnes 
1749c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1750c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1751c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1752c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods)
1753c7afd0dbSLois Curfman McInnes .    f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods)
1754c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods)
1755c7afd0dbSLois Curfman McInnes -    f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods)
17569b94acceSBarry Smith 
175736851e7fSLois Curfman McInnes    Level: advanced
175836851e7fSLois Curfman McInnes 
17599b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
17609b94acceSBarry Smith 
176140191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
176240191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
17639b94acceSBarry Smith @*/
176474679c65SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,void*),void *cctx)
17659b94acceSBarry Smith {
17663a40ed3dSBarry Smith   PetscFunctionBegin;
17679b94acceSBarry Smith   (snes)->converged = func;
17689b94acceSBarry Smith   (snes)->cnvP      = cctx;
17693a40ed3dSBarry Smith   PetscFunctionReturn(0);
17709b94acceSBarry Smith }
17719b94acceSBarry Smith 
17725615d1e5SSatish Balay #undef __FUNC__
1773d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory"
1774c9005455SLois Curfman McInnes /*@
1775c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1776c9005455SLois Curfman McInnes 
1777fee21e36SBarry Smith    Collective on SNES
1778fee21e36SBarry Smith 
1779c7afd0dbSLois Curfman McInnes    Input Parameters:
1780c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1781c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1782758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1783758f92a0SBarry Smith          negative if not converged) for each solve.
1784758f92a0SBarry Smith .  na  - size of a and its
1785758f92a0SBarry Smith -  reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero,
1786758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1787c7afd0dbSLois Curfman McInnes 
1788c9005455SLois Curfman McInnes    Notes:
1789c9005455SLois Curfman McInnes    If set, this array will contain the function norms (for
1790c9005455SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods) or gradient norms
1791c9005455SLois Curfman McInnes    (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed
1792c9005455SLois Curfman McInnes    at each step.
1793c9005455SLois Curfman McInnes 
1794c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1795c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1796c9005455SLois Curfman McInnes    during the section of code that is being timed.
1797c9005455SLois Curfman McInnes 
179836851e7fSLois Curfman McInnes    Level: intermediate
179936851e7fSLois Curfman McInnes 
1800c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1801758f92a0SBarry Smith 
180208405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1803758f92a0SBarry Smith 
1804c9005455SLois Curfman McInnes @*/
1805758f92a0SBarry Smith int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset)
1806c9005455SLois Curfman McInnes {
18073a40ed3dSBarry Smith   PetscFunctionBegin;
1808c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1809c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1810c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1811758f92a0SBarry Smith   snes->conv_hist_its   = its;
1812758f92a0SBarry Smith   snes->conv_hist_max   = na;
1813758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1814758f92a0SBarry Smith   PetscFunctionReturn(0);
1815758f92a0SBarry Smith }
1816758f92a0SBarry Smith 
1817758f92a0SBarry Smith #undef __FUNC__
1818758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory"
18190c4c9dddSBarry Smith /*@C
1820758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1821758f92a0SBarry Smith 
1822758f92a0SBarry Smith    Collective on SNES
1823758f92a0SBarry Smith 
1824758f92a0SBarry Smith    Input Parameter:
1825758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1826758f92a0SBarry Smith 
1827758f92a0SBarry Smith    Output Parameters:
1828758f92a0SBarry Smith .  a   - array to hold history
1829758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1830758f92a0SBarry Smith          negative if not converged) for each solve.
1831758f92a0SBarry Smith -  na  - size of a and its
1832758f92a0SBarry Smith 
1833758f92a0SBarry Smith    Notes:
1834758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1835758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1836758f92a0SBarry Smith 
1837758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1838758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1839758f92a0SBarry Smith    during the section of code that is being timed.
1840758f92a0SBarry Smith 
1841758f92a0SBarry Smith    Level: intermediate
1842758f92a0SBarry Smith 
1843758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1844758f92a0SBarry Smith 
1845758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1846758f92a0SBarry Smith 
1847758f92a0SBarry Smith @*/
1848758f92a0SBarry Smith int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na)
1849758f92a0SBarry Smith {
1850758f92a0SBarry Smith   PetscFunctionBegin;
1851758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1852758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1853758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1854758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
18553a40ed3dSBarry Smith   PetscFunctionReturn(0);
1856c9005455SLois Curfman McInnes }
1857c9005455SLois Curfman McInnes 
1858c9005455SLois Curfman McInnes #undef __FUNC__
18595615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private"
18609b94acceSBarry Smith /*
18619b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
18629b94acceSBarry Smith    positive parameter delta.
18639b94acceSBarry Smith 
18649b94acceSBarry Smith     Input Parameters:
1865c7afd0dbSLois Curfman McInnes +   snes - the SNES context
18669b94acceSBarry Smith .   y - approximate solution of linear system
18679b94acceSBarry Smith .   fnorm - 2-norm of current function
1868c7afd0dbSLois Curfman McInnes -   delta - trust region size
18699b94acceSBarry Smith 
18709b94acceSBarry Smith     Output Parameters:
1871c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
18729b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
18739b94acceSBarry Smith     region, and exceeds zero otherwise.
1874c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
18759b94acceSBarry Smith 
18769b94acceSBarry Smith     Note:
187740191667SLois Curfman McInnes     For non-trust region methods such as SNES_EQ_LS, the parameter delta
18789b94acceSBarry Smith     is set to be the maximum allowable step size.
18799b94acceSBarry Smith 
18809b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
18819b94acceSBarry Smith */
18829b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta,
18839b94acceSBarry Smith                           double *gpnorm,double *ynorm)
18849b94acceSBarry Smith {
18859b94acceSBarry Smith   double norm;
18869b94acceSBarry Smith   Scalar cnorm;
18873a40ed3dSBarry Smith   int    ierr;
18883a40ed3dSBarry Smith 
18893a40ed3dSBarry Smith   PetscFunctionBegin;
18903a40ed3dSBarry Smith   ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr);
18919b94acceSBarry Smith   if (norm > *delta) {
18929b94acceSBarry Smith      norm = *delta/norm;
18939b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
18949b94acceSBarry Smith      cnorm = norm;
18959b94acceSBarry Smith      VecScale( &cnorm, y );
18969b94acceSBarry Smith      *ynorm = *delta;
18979b94acceSBarry Smith   } else {
18989b94acceSBarry Smith      *gpnorm = 0.0;
18999b94acceSBarry Smith      *ynorm = norm;
19009b94acceSBarry Smith   }
19013a40ed3dSBarry Smith   PetscFunctionReturn(0);
19029b94acceSBarry Smith }
19039b94acceSBarry Smith 
19045615d1e5SSatish Balay #undef __FUNC__
19055615d1e5SSatish Balay #define __FUNC__ "SNESSolve"
19069b94acceSBarry Smith /*@
19079b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
190863a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
19099b94acceSBarry Smith 
1910c7afd0dbSLois Curfman McInnes    Collective on SNES
1911c7afd0dbSLois Curfman McInnes 
1912b2002411SLois Curfman McInnes    Input Parameters:
1913c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1914c7afd0dbSLois Curfman McInnes -  x - the solution vector
19159b94acceSBarry Smith 
19169b94acceSBarry Smith    Output Parameter:
1917b2002411SLois Curfman McInnes .  its - number of iterations until termination
19189b94acceSBarry Smith 
1919b2002411SLois Curfman McInnes    Notes:
19208ddd3da0SLois Curfman McInnes    The user should initialize the vector, x, with the initial guess
19218ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
19228ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
19238ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
19248ddd3da0SLois Curfman McInnes 
192536851e7fSLois Curfman McInnes    Level: beginner
192636851e7fSLois Curfman McInnes 
19279b94acceSBarry Smith .keywords: SNES, nonlinear, solve
19289b94acceSBarry Smith 
192963a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
19309b94acceSBarry Smith @*/
19318ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
19329b94acceSBarry Smith {
19333c7409f5SSatish Balay   int ierr, flg;
1934052efed2SBarry Smith 
19353a40ed3dSBarry Smith   PetscFunctionBegin;
193677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
193774679c65SBarry Smith   PetscValidIntPointer(its);
193882bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
1939c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
1940758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
19419b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
1942c96a6f78SLois Curfman McInnes   snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0;
19439b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr);
19449b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
19453c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg);CHKERRQ(ierr);
19466d4a8577SBarry Smith   if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); }
19473a40ed3dSBarry Smith   PetscFunctionReturn(0);
19489b94acceSBarry Smith }
19499b94acceSBarry Smith 
19509b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
19519b94acceSBarry Smith 
19525615d1e5SSatish Balay #undef __FUNC__
19535615d1e5SSatish Balay #define __FUNC__ "SNESSetType"
195482bf6240SBarry Smith /*@C
19554b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
19569b94acceSBarry Smith 
1957fee21e36SBarry Smith    Collective on SNES
1958fee21e36SBarry Smith 
1959c7afd0dbSLois Curfman McInnes    Input Parameters:
1960c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1961c7afd0dbSLois Curfman McInnes -  method - a known method
1962c7afd0dbSLois Curfman McInnes 
1963c7afd0dbSLois Curfman McInnes    Options Database Key:
1964c7afd0dbSLois Curfman McInnes .  -snes_type <method> - Sets the method; use -help for a list
1965c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1966ae12b187SLois Curfman McInnes 
19679b94acceSBarry Smith    Notes:
19689b94acceSBarry Smith    See "petsc/include/snes.h" for available methods (for instance)
196936851e7fSLois Curfman McInnes +    SNES_EQ_LS - Newton's method with line search
1970c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
1971c7afd0dbSLois Curfman McInnes .    SNES_EQ_TR - Newton's method with trust region
1972c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
1973c7afd0dbSLois Curfman McInnes .    SNES_UM_TR - Newton's method with trust region
1974c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
197536851e7fSLois Curfman McInnes -    SNES_UM_LS - Newton's method with line search
1976c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
19779b94acceSBarry Smith 
1978ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1979ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1980ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1981ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1982ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1983ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1984ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1985ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1986ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
198736851e7fSLois Curfman McInnes   appropriate method.  In other words, this routine is not for beginners.
198836851e7fSLois Curfman McInnes 
198936851e7fSLois Curfman McInnes   Level: intermediate
1990a703fe33SLois Curfman McInnes 
1991f536c699SSatish Balay .keywords: SNES, set, method
19929b94acceSBarry Smith @*/
19934b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method)
19949b94acceSBarry Smith {
199584cb2905SBarry Smith   int ierr;
19969b94acceSBarry Smith   int (*r)(SNES);
19973a40ed3dSBarry Smith 
19983a40ed3dSBarry Smith   PetscFunctionBegin;
199977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
200082bf6240SBarry Smith 
20013f1db9ecSBarry Smith   if (PetscTypeCompare(snes->type_name,method)) PetscFunctionReturn(0);
200282bf6240SBarry Smith 
200382bf6240SBarry Smith   if (snes->setupcalled) {
2004e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
200582bf6240SBarry Smith     snes->data = 0;
200682bf6240SBarry Smith   }
20077d1a2b2bSBarry Smith 
20089b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
200982bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
201082bf6240SBarry Smith 
2011488ecbafSBarry Smith   ierr =  FListFind(snes->comm, SNESList, method,(int (**)(void *)) &r );CHKERRQ(ierr);
201282bf6240SBarry Smith 
2013596552b5SBarry Smith   if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",method);
20141548b14aSBarry Smith 
2015606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
201682bf6240SBarry Smith   snes->data = 0;
20173a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
201882bf6240SBarry Smith 
2019*be0abb6dSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,method);CHKERRQ(ierr);
202082bf6240SBarry Smith   snes->set_method_called = 1;
202182bf6240SBarry Smith 
20223a40ed3dSBarry Smith   PetscFunctionReturn(0);
20239b94acceSBarry Smith }
20249b94acceSBarry Smith 
2025a847f771SSatish Balay 
20269b94acceSBarry Smith /* --------------------------------------------------------------------- */
20275615d1e5SSatish Balay #undef __FUNC__
2028d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy"
20299b94acceSBarry Smith /*@C
20309b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
20319b94acceSBarry Smith    registered by SNESRegister().
20329b94acceSBarry Smith 
2033fee21e36SBarry Smith    Not Collective
2034fee21e36SBarry Smith 
203536851e7fSLois Curfman McInnes    Level: advanced
203636851e7fSLois Curfman McInnes 
20379b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
20389b94acceSBarry Smith 
20399b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
20409b94acceSBarry Smith @*/
2041cf256101SBarry Smith int SNESRegisterDestroy(void)
20429b94acceSBarry Smith {
204382bf6240SBarry Smith   int ierr;
204482bf6240SBarry Smith 
20453a40ed3dSBarry Smith   PetscFunctionBegin;
204682bf6240SBarry Smith   if (SNESList) {
2047488ecbafSBarry Smith     ierr = FListDestroy( SNESList );CHKERRQ(ierr);
204882bf6240SBarry Smith     SNESList = 0;
20499b94acceSBarry Smith   }
205084cb2905SBarry Smith   SNESRegisterAllCalled = 0;
20513a40ed3dSBarry Smith   PetscFunctionReturn(0);
20529b94acceSBarry Smith }
20539b94acceSBarry Smith 
20545615d1e5SSatish Balay #undef __FUNC__
2055d4bb536fSBarry Smith #define __FUNC__ "SNESGetType"
20569b94acceSBarry Smith /*@C
20579a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
20589b94acceSBarry Smith 
2059c7afd0dbSLois Curfman McInnes    Not Collective
2060c7afd0dbSLois Curfman McInnes 
20619b94acceSBarry Smith    Input Parameter:
20624b0e389bSBarry Smith .  snes - nonlinear solver context
20639b94acceSBarry Smith 
20649b94acceSBarry Smith    Output Parameter:
206582bf6240SBarry Smith .  method - SNES method (a charactor string)
20669b94acceSBarry Smith 
206736851e7fSLois Curfman McInnes    Level: intermediate
206836851e7fSLois Curfman McInnes 
20699b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name
20709b94acceSBarry Smith @*/
207182bf6240SBarry Smith int SNESGetType(SNES snes, SNESType *method)
20729b94acceSBarry Smith {
20733a40ed3dSBarry Smith   PetscFunctionBegin;
207482bf6240SBarry Smith   *method = snes->type_name;
20753a40ed3dSBarry Smith   PetscFunctionReturn(0);
20769b94acceSBarry Smith }
20779b94acceSBarry Smith 
20785615d1e5SSatish Balay #undef __FUNC__
2079d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution"
20809b94acceSBarry Smith /*@C
20819b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20829b94acceSBarry Smith    stored.
20839b94acceSBarry Smith 
2084c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2085c7afd0dbSLois Curfman McInnes 
20869b94acceSBarry Smith    Input Parameter:
20879b94acceSBarry Smith .  snes - the SNES context
20889b94acceSBarry Smith 
20899b94acceSBarry Smith    Output Parameter:
20909b94acceSBarry Smith .  x - the solution
20919b94acceSBarry Smith 
209236851e7fSLois Curfman McInnes    Level: advanced
209336851e7fSLois Curfman McInnes 
20949b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20959b94acceSBarry Smith 
2096a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
20979b94acceSBarry Smith @*/
20989b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
20999b94acceSBarry Smith {
21003a40ed3dSBarry Smith   PetscFunctionBegin;
210177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21029b94acceSBarry Smith   *x = snes->vec_sol_always;
21033a40ed3dSBarry Smith   PetscFunctionReturn(0);
21049b94acceSBarry Smith }
21059b94acceSBarry Smith 
21065615d1e5SSatish Balay #undef __FUNC__
2107d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate"
21089b94acceSBarry Smith /*@C
21099b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
21109b94acceSBarry Smith    stored.
21119b94acceSBarry Smith 
2112c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2113c7afd0dbSLois Curfman McInnes 
21149b94acceSBarry Smith    Input Parameter:
21159b94acceSBarry Smith .  snes - the SNES context
21169b94acceSBarry Smith 
21179b94acceSBarry Smith    Output Parameter:
21189b94acceSBarry Smith .  x - the solution update
21199b94acceSBarry Smith 
212036851e7fSLois Curfman McInnes    Level: advanced
212136851e7fSLois Curfman McInnes 
21229b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
21239b94acceSBarry Smith 
21249b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
21259b94acceSBarry Smith @*/
21269b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
21279b94acceSBarry Smith {
21283a40ed3dSBarry Smith   PetscFunctionBegin;
212977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21309b94acceSBarry Smith   *x = snes->vec_sol_update_always;
21313a40ed3dSBarry Smith   PetscFunctionReturn(0);
21329b94acceSBarry Smith }
21339b94acceSBarry Smith 
21345615d1e5SSatish Balay #undef __FUNC__
2135d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction"
21369b94acceSBarry Smith /*@C
21373638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
21389b94acceSBarry Smith 
2139c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2140c7afd0dbSLois Curfman McInnes 
21419b94acceSBarry Smith    Input Parameter:
21429b94acceSBarry Smith .  snes - the SNES context
21439b94acceSBarry Smith 
21449b94acceSBarry Smith    Output Parameter:
21453638b69dSLois Curfman McInnes .  r - the function
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()
21589b94acceSBarry Smith @*/
21599b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r)
21609b94acceSBarry Smith {
21613a40ed3dSBarry Smith   PetscFunctionBegin;
216277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2163a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
2164a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
2165a8c6a408SBarry Smith   }
21669b94acceSBarry Smith   *r = snes->vec_func_always;
21673a40ed3dSBarry Smith   PetscFunctionReturn(0);
21689b94acceSBarry Smith }
21699b94acceSBarry Smith 
21705615d1e5SSatish Balay #undef __FUNC__
2171d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient"
21729b94acceSBarry Smith /*@C
21733638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
21749b94acceSBarry Smith 
2175c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2176c7afd0dbSLois Curfman McInnes 
21779b94acceSBarry Smith    Input Parameter:
21789b94acceSBarry Smith .  snes - the SNES context
21799b94acceSBarry Smith 
21809b94acceSBarry Smith    Output Parameter:
21819b94acceSBarry Smith .  r - the gradient
21829b94acceSBarry Smith 
21839b94acceSBarry Smith    Notes:
21849b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
21859b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
21869b94acceSBarry Smith    SNESGetFunction().
21879b94acceSBarry Smith 
218836851e7fSLois Curfman McInnes    Level: advanced
218936851e7fSLois Curfman McInnes 
21909b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
21919b94acceSBarry Smith 
219231615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction()
21939b94acceSBarry Smith @*/
21949b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r)
21959b94acceSBarry Smith {
21963a40ed3dSBarry Smith   PetscFunctionBegin;
219777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21983a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2199a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
22003a40ed3dSBarry Smith   }
22019b94acceSBarry Smith   *r = snes->vec_func_always;
22023a40ed3dSBarry Smith   PetscFunctionReturn(0);
22039b94acceSBarry Smith }
22049b94acceSBarry Smith 
22055615d1e5SSatish Balay #undef __FUNC__
2206d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction"
22079b94acceSBarry Smith /*@
22089b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
22099b94acceSBarry Smith    unconstrained minimization problems.
22109b94acceSBarry Smith 
2211c7afd0dbSLois Curfman McInnes    Not Collective
2212c7afd0dbSLois Curfman McInnes 
22139b94acceSBarry Smith    Input Parameter:
22149b94acceSBarry Smith .  snes - the SNES context
22159b94acceSBarry Smith 
22169b94acceSBarry Smith    Output Parameter:
22179b94acceSBarry Smith .  r - the function
22189b94acceSBarry Smith 
22199b94acceSBarry Smith    Notes:
22209b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
22219b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
22229b94acceSBarry Smith    SNESGetFunction().
22239b94acceSBarry Smith 
222436851e7fSLois Curfman McInnes    Level: advanced
222536851e7fSLois Curfman McInnes 
22269b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
22279b94acceSBarry Smith 
222831615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction()
22299b94acceSBarry Smith @*/
22309b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r)
22319b94acceSBarry Smith {
22323a40ed3dSBarry Smith   PetscFunctionBegin;
223377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
223474679c65SBarry Smith   PetscValidScalarPointer(r);
22353a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2236a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
22373a40ed3dSBarry Smith   }
22389b94acceSBarry Smith   *r = snes->fc;
22393a40ed3dSBarry Smith   PetscFunctionReturn(0);
22409b94acceSBarry Smith }
22419b94acceSBarry Smith 
22425615d1e5SSatish Balay #undef __FUNC__
2243d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix"
22443c7409f5SSatish Balay /*@C
22453c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2246d850072dSLois Curfman McInnes    SNES options in the database.
22473c7409f5SSatish Balay 
2248fee21e36SBarry Smith    Collective on SNES
2249fee21e36SBarry Smith 
2250c7afd0dbSLois Curfman McInnes    Input Parameter:
2251c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2252c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2253c7afd0dbSLois Curfman McInnes 
2254d850072dSLois Curfman McInnes    Notes:
2255a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2256c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2257d850072dSLois Curfman McInnes 
225836851e7fSLois Curfman McInnes    Level: advanced
225936851e7fSLois Curfman McInnes 
22603c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2261a86d99e1SLois Curfman McInnes 
2262a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
22633c7409f5SSatish Balay @*/
22643c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
22653c7409f5SSatish Balay {
22663c7409f5SSatish Balay   int ierr;
22673c7409f5SSatish Balay 
22683a40ed3dSBarry Smith   PetscFunctionBegin;
226977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2270639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr);
22713c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
22723a40ed3dSBarry Smith   PetscFunctionReturn(0);
22733c7409f5SSatish Balay }
22743c7409f5SSatish Balay 
22755615d1e5SSatish Balay #undef __FUNC__
2276d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix"
22773c7409f5SSatish Balay /*@C
2278f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2279d850072dSLois Curfman McInnes    SNES options in the database.
22803c7409f5SSatish Balay 
2281fee21e36SBarry Smith    Collective on SNES
2282fee21e36SBarry Smith 
2283c7afd0dbSLois Curfman McInnes    Input Parameters:
2284c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2285c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2286c7afd0dbSLois Curfman McInnes 
2287d850072dSLois Curfman McInnes    Notes:
2288a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2289c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2290d850072dSLois Curfman McInnes 
229136851e7fSLois Curfman McInnes    Level: advanced
229236851e7fSLois Curfman McInnes 
22933c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2294a86d99e1SLois Curfman McInnes 
2295a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
22963c7409f5SSatish Balay @*/
22973c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
22983c7409f5SSatish Balay {
22993c7409f5SSatish Balay   int ierr;
23003c7409f5SSatish Balay 
23013a40ed3dSBarry Smith   PetscFunctionBegin;
230277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2303639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr);
23043c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
23053a40ed3dSBarry Smith   PetscFunctionReturn(0);
23063c7409f5SSatish Balay }
23073c7409f5SSatish Balay 
23085615d1e5SSatish Balay #undef __FUNC__
2309d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix"
23109ab63eb5SSatish Balay /*@C
23113c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
23123c7409f5SSatish Balay    SNES options in the database.
23133c7409f5SSatish Balay 
2314c7afd0dbSLois Curfman McInnes    Not Collective
2315c7afd0dbSLois Curfman McInnes 
23163c7409f5SSatish Balay    Input Parameter:
23173c7409f5SSatish Balay .  snes - the SNES context
23183c7409f5SSatish Balay 
23193c7409f5SSatish Balay    Output Parameter:
23203c7409f5SSatish Balay .  prefix - pointer to the prefix string used
23213c7409f5SSatish Balay 
23229ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
23239ab63eb5SSatish Balay    sufficient length to hold the prefix.
23249ab63eb5SSatish Balay 
232536851e7fSLois Curfman McInnes    Level: advanced
232636851e7fSLois Curfman McInnes 
23273c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2328a86d99e1SLois Curfman McInnes 
2329a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
23303c7409f5SSatish Balay @*/
23313c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
23323c7409f5SSatish Balay {
23333c7409f5SSatish Balay   int ierr;
23343c7409f5SSatish Balay 
23353a40ed3dSBarry Smith   PetscFunctionBegin;
233677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2337639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr);
23383a40ed3dSBarry Smith   PetscFunctionReturn(0);
23393c7409f5SSatish Balay }
23403c7409f5SSatish Balay 
2341ca161407SBarry Smith #undef __FUNC__
2342ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp"
2343ca161407SBarry Smith /*@
2344ca161407SBarry Smith    SNESPrintHelp - Prints all options for the SNES component.
2345ca161407SBarry Smith 
2346c7afd0dbSLois Curfman McInnes    Collective on SNES
2347c7afd0dbSLois Curfman McInnes 
2348ca161407SBarry Smith    Input Parameter:
2349ca161407SBarry Smith .  snes - the SNES context
2350ca161407SBarry Smith 
2351ca161407SBarry Smith    Options Database Keys:
2352c7afd0dbSLois Curfman McInnes +  -help - Prints SNES options
2353c7afd0dbSLois Curfman McInnes -  -h - Prints SNES options
2354ca161407SBarry Smith 
235536851e7fSLois Curfman McInnes    Level: beginner
235636851e7fSLois Curfman McInnes 
2357ca161407SBarry Smith .keywords: SNES, nonlinear, help
2358ca161407SBarry Smith 
2359ca161407SBarry Smith .seealso: SNESSetFromOptions()
2360ca161407SBarry Smith @*/
2361ca161407SBarry Smith int SNESPrintHelp(SNES snes)
2362ca161407SBarry Smith {
2363ca161407SBarry Smith   char                p[64];
2364ca161407SBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
2365ca161407SBarry Smith   int                 ierr;
2366ca161407SBarry Smith 
2367ca161407SBarry Smith   PetscFunctionBegin;
2368ca161407SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2369ca161407SBarry Smith 
2370549d3d68SSatish Balay   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
2371ca161407SBarry Smith   if (snes->prefix) PetscStrcat(p, snes->prefix);
2372ca161407SBarry Smith 
2373ca161407SBarry Smith   kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
2374ca161407SBarry Smith 
2375ebb8b11fSBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
2376d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr);
2377488ecbafSBarry Smith   ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr);
2378d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr);
2379d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr);
2380d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr);
2381d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr);
2382d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr);
2383d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr);
2384d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr);
2385d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr);
2386d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr);
2387d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_monitor: use default SNES convergence monitor, prints\n\
2388d132466eSBarry Smith     residual norm at each iteration.\n",p);CHKERRQ(ierr);
2389d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_smonitor: same as the above, but prints fewer digits of the\n\
2390ca161407SBarry Smith     residual norm for small residual norms. This is useful to conceal\n\
2391d132466eSBarry Smith     meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr);
2392d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr);
2393d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr);
2394d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr);
2395ca161407SBarry Smith   if (snes->type == SNES_NONLINEAR_EQUATIONS) {
2396d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2397d132466eSBarry Smith      " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr);
2398d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr);
2399d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr);
2400d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr);
2401d132466eSBarry 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);
2402d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr);
2403d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr);
2404d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2405d132466eSBarry Smith      "     %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr);
2406d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2407d132466eSBarry Smith      "     %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr);
2408d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2409d132466eSBarry Smith      "     %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr);
2410d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2411d132466eSBarry Smith      "     %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr);
2412d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2413d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr);
2414d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2415d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr);
2416d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2417d132466eSBarry Smith      "     %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr);
2418ca161407SBarry Smith   } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) {
2419d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr);
2420d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr);
2421d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr);
2422d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr);
2423ca161407SBarry Smith   }
2424d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <method> for help on ",p);CHKERRQ(ierr);
2425d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr);
2426ca161407SBarry Smith   if (snes->printhelp) {
2427ca161407SBarry Smith     ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr);
2428ca161407SBarry Smith   }
2429ca161407SBarry Smith   PetscFunctionReturn(0);
2430ca161407SBarry Smith }
24313c7409f5SSatish Balay 
2432acb85ae6SSatish Balay /*MC
2433b2002411SLois Curfman McInnes    SNESRegister - Adds a method to the nonlinear solver package.
24349b94acceSBarry Smith 
2435b2002411SLois Curfman McInnes    Synopsis:
2436b2002411SLois Curfman McInnes    SNESRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
24379b94acceSBarry Smith 
24388d76a1e5SLois Curfman McInnes    Not collective
24398d76a1e5SLois Curfman McInnes 
2440b2002411SLois Curfman McInnes    Input Parameters:
2441c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2442b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2443b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2444c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
24459b94acceSBarry Smith 
2446b2002411SLois Curfman McInnes    Notes:
2447b2002411SLois Curfman McInnes    SNESRegister() may be called multiple times to add several user-defined solvers.
24483a40ed3dSBarry Smith 
2449b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2450b2002411SLois Curfman McInnes    is ignored.
2451b2002411SLois Curfman McInnes 
2452b2002411SLois Curfman McInnes    Sample usage:
245369225d78SLois Curfman McInnes .vb
2454b2002411SLois Curfman McInnes    SNESRegister("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2455b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
245669225d78SLois Curfman McInnes .ve
2457b2002411SLois Curfman McInnes 
2458b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2459b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2460b2002411SLois Curfman McInnes    or at runtime via the option
2461b2002411SLois Curfman McInnes $     -snes_type my_solver
2462b2002411SLois Curfman McInnes 
246336851e7fSLois Curfman McInnes    Level: advanced
246436851e7fSLois Curfman McInnes 
246585614651SBarry Smith    $PETSC_ARCH, $PETSC_DIR, $PETSC_LDIR, and $BOPT occuring in pathname will be replaced with appropriate values.
2466dd438238SBarry Smith 
2467b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2468b2002411SLois Curfman McInnes 
2469b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2470b2002411SLois Curfman McInnes M*/
2471b2002411SLois Curfman McInnes 
2472b2002411SLois Curfman McInnes #undef __FUNC__
2473b2002411SLois Curfman McInnes #define __FUNC__ "SNESRegister_Private"
2474b2002411SLois Curfman McInnes int SNESRegister_Private(char *sname,char *path,char *name,int (*function)(SNES))
2475b2002411SLois Curfman McInnes {
2476b2002411SLois Curfman McInnes   char fullname[256];
2477b2002411SLois Curfman McInnes   int  ierr;
2478b2002411SLois Curfman McInnes 
2479b2002411SLois Curfman McInnes   PetscFunctionBegin;
2480549d3d68SSatish Balay   ierr = PetscStrcpy(fullname,path);CHKERRQ(ierr);
2481549d3d68SSatish Balay   PetscStrcat(fullname,":");PetscStrcat(fullname,name);
2482488ecbafSBarry Smith   ierr = FListAdd_Private(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr);
2483b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2484b2002411SLois Curfman McInnes }
2485