xref: /petsc/src/snes/interface/snes.c (revision 6831982abb6453c2f3e25efecb5d0951d333371e) !
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*6831982aSBarry Smith static char vcid[] = "$Id: snes.c,v 1.199 1999/10/13 20:38:25 bsmith Exp bsmith $";
39b94acceSBarry Smith #endif
49b94acceSBarry Smith 
570f55243SBarry Smith #include "src/snes/snesimpl.h"      /*I "snes.h"  I*/
69b94acceSBarry Smith 
784cb2905SBarry Smith int SNESRegisterAllCalled = 0;
8488ecbafSBarry Smith FList SNESList = 0;
982bf6240SBarry Smith 
105615d1e5SSatish Balay #undef __FUNC__
11d4bb536fSBarry Smith #define __FUNC__ "SNESView"
129b94acceSBarry Smith /*@
139b94acceSBarry Smith    SNESView - Prints the SNES data structure.
149b94acceSBarry Smith 
15fee21e36SBarry Smith    Collective on SNES, unless Viewer is VIEWER_STDOUT_SELF
16fee21e36SBarry Smith 
17c7afd0dbSLois Curfman McInnes    Input Parameters:
18c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
19c7afd0dbSLois Curfman McInnes -  viewer - visualization context
20c7afd0dbSLois Curfman McInnes 
219b94acceSBarry Smith    Options Database Key:
22c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
239b94acceSBarry Smith 
249b94acceSBarry Smith    Notes:
259b94acceSBarry Smith    The available visualization contexts include
26c7afd0dbSLois Curfman McInnes +     VIEWER_STDOUT_SELF - standard output (default)
27c7afd0dbSLois Curfman McInnes -     VIEWER_STDOUT_WORLD - synchronized standard
28c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
29c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
30c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
319b94acceSBarry Smith 
323e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
3377ed5343SBarry Smith    ViewerASCIIOpen() - output to a specified file.
349b94acceSBarry Smith 
3536851e7fSLois Curfman McInnes    Level: beginner
3636851e7fSLois Curfman McInnes 
379b94acceSBarry Smith .keywords: SNES, view
389b94acceSBarry Smith 
3977ed5343SBarry Smith .seealso: ViewerASCIIOpen()
409b94acceSBarry Smith @*/
419b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer)
429b94acceSBarry Smith {
439b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
449b94acceSBarry Smith   int                 ierr;
459b94acceSBarry Smith   SLES                sles;
46454a90a3SBarry Smith   char                *type;
47*6831982aSBarry Smith   PetscTruth          isascii,isstring;
489b94acceSBarry Smith 
493a40ed3dSBarry Smith   PetscFunctionBegin;
5074679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
510f5bd95cSBarry Smith   if (!viewer) viewer = VIEWER_STDOUT_SELF;
520f5bd95cSBarry Smith   PetscValidHeaderSpecific(viewer,VIEWER_COOKIE);
53*6831982aSBarry Smith   PetscCheckSameComm(snes,viewer);
5474679c65SBarry Smith 
55*6831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr);
56*6831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,STRING_VIEWER,&isstring);CHKERRQ(ierr);
570f5bd95cSBarry Smith   if (isascii) {
58d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
59454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
60454a90a3SBarry Smith     if (type) {
61454a90a3SBarry Smith       ierr = ViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
62184914b5SBarry Smith     } else {
63454a90a3SBarry Smith       ierr = ViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
64184914b5SBarry Smith     }
650ef38995SBarry Smith     if (snes->view) {
660ef38995SBarry Smith       ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
670ef38995SBarry Smith       ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr);
680ef38995SBarry Smith       ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
690ef38995SBarry Smith     }
70d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
71d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n",
72d132466eSBarry Smith                  snes->rtol, snes->atol, snes->trunctol, snes->xtol);CHKERRQ(ierr);
73d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
74d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
750ef38995SBarry Smith     if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
76d132466eSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"  min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr);
770ef38995SBarry Smith     }
789b94acceSBarry Smith     if (snes->ksp_ewconv) {
799b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
809b94acceSBarry Smith       if (kctx) {
81d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
82d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
83d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"    gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
849b94acceSBarry Smith       }
859b94acceSBarry Smith     }
860f5bd95cSBarry Smith   } else if (isstring) {
87454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
88454a90a3SBarry Smith     ierr = ViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
8919bcc07fSBarry Smith   }
9077ed5343SBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
910ef38995SBarry Smith   ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
929b94acceSBarry Smith   ierr = SLESView(sles,viewer);CHKERRQ(ierr);
930ef38995SBarry Smith   ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
943a40ed3dSBarry Smith   PetscFunctionReturn(0);
959b94acceSBarry Smith }
969b94acceSBarry Smith 
97639f9d9dSBarry Smith /*
98639f9d9dSBarry Smith        We retain a list of functions that also take SNES command
99639f9d9dSBarry Smith     line options. These are called at the end SNESSetFromOptions()
100639f9d9dSBarry Smith */
101639f9d9dSBarry Smith #define MAXSETFROMOPTIONS 5
102639f9d9dSBarry Smith static int numberofsetfromoptions;
103639f9d9dSBarry Smith static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
104639f9d9dSBarry Smith 
1055615d1e5SSatish Balay #undef __FUNC__
106d4bb536fSBarry Smith #define __FUNC__ "SNESAddOptionsChecker"
107639f9d9dSBarry Smith /*@
108639f9d9dSBarry Smith     SNESAddOptionsChecker - Adds an additional function to check for SNES options.
109639f9d9dSBarry Smith 
110c7afd0dbSLois Curfman McInnes     Not Collective
111c7afd0dbSLois Curfman McInnes 
112639f9d9dSBarry Smith     Input Parameter:
113639f9d9dSBarry Smith .   snescheck - function that checks for options
114639f9d9dSBarry Smith 
11536851e7fSLois Curfman McInnes     Level: developer
11636851e7fSLois Curfman McInnes 
117639f9d9dSBarry Smith .seealso: SNESSetFromOptions()
118639f9d9dSBarry Smith @*/
119639f9d9dSBarry Smith int SNESAddOptionsChecker(int (*snescheck)(SNES) )
120639f9d9dSBarry Smith {
1213a40ed3dSBarry Smith   PetscFunctionBegin;
122639f9d9dSBarry Smith   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
123a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many options checkers, only 5 allowed");
124639f9d9dSBarry Smith   }
125639f9d9dSBarry Smith 
126639f9d9dSBarry Smith   othersetfromoptions[numberofsetfromoptions++] = snescheck;
1273a40ed3dSBarry Smith   PetscFunctionReturn(0);
128639f9d9dSBarry Smith }
129639f9d9dSBarry Smith 
1305615d1e5SSatish Balay #undef __FUNC__
13115091d37SBarry Smith #define __FUNC__ "SNESSetTypeFromOptions"
13215091d37SBarry Smith /*@
13315091d37SBarry Smith    SNESSetTypeFromOptions - Sets the SNES solver type from the options database,
13415091d37SBarry Smith         or sets a default if none is give.
13515091d37SBarry Smith 
13615091d37SBarry Smith    Collective on SNES
13715091d37SBarry Smith 
13815091d37SBarry Smith    Input Parameter:
13915091d37SBarry Smith .  snes - the SNES context
14015091d37SBarry Smith 
14115091d37SBarry Smith    Options Database Keys:
142*6831982aSBarry Smith .  -snes_type <type> - ls, tr, umls, umtr, test
14315091d37SBarry Smith 
14415091d37SBarry Smith    Level: beginner
14515091d37SBarry Smith 
14615091d37SBarry Smith .keywords: SNES, nonlinear, set, options, database
14715091d37SBarry Smith 
14815091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetFromOptions()
14915091d37SBarry Smith @*/
15015091d37SBarry Smith int SNESSetTypeFromOptions(SNES snes)
15115091d37SBarry Smith {
152454a90a3SBarry Smith   char     type[256];
15315091d37SBarry Smith   int      ierr, flg;
15415091d37SBarry Smith 
15515091d37SBarry Smith   PetscFunctionBegin;
15615091d37SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15715091d37SBarry Smith   if (snes->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to SNESSetUp()");
158454a90a3SBarry Smith   ierr = OptionsGetString(snes->prefix,"-snes_type",type,256,&flg);CHKERRQ(ierr);
15915091d37SBarry Smith   if (flg) {
160454a90a3SBarry Smith     ierr = SNESSetType(snes,(SNESType) type);CHKERRQ(ierr);
16115091d37SBarry Smith   }
16215091d37SBarry Smith   /*
16315091d37SBarry Smith       If SNES type has not yet been set, set it now
16415091d37SBarry Smith   */
16515091d37SBarry Smith   if (!snes->type_name) {
16615091d37SBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
167*6831982aSBarry Smith       ierr = SNESSetType(snes,SNESEQLS);CHKERRQ(ierr);
16815091d37SBarry Smith     } else {
169*6831982aSBarry Smith       ierr = SNESSetType(snes,SNESUMTR);CHKERRQ(ierr);
17015091d37SBarry Smith     }
17115091d37SBarry Smith   }
17215091d37SBarry Smith   PetscFunctionReturn(0);
17315091d37SBarry Smith }
17415091d37SBarry Smith 
17515091d37SBarry Smith #undef __FUNC__
1765615d1e5SSatish Balay #define __FUNC__ "SNESSetFromOptions"
1779b94acceSBarry Smith /*@
178682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
1799b94acceSBarry Smith 
180c7afd0dbSLois Curfman McInnes    Collective on SNES
181c7afd0dbSLois Curfman McInnes 
1829b94acceSBarry Smith    Input Parameter:
1839b94acceSBarry Smith .  snes - the SNES context
1849b94acceSBarry Smith 
18536851e7fSLois Curfman McInnes    Options Database Keys:
186*6831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
18782738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
18882738288SBarry Smith                 of the change in the solution between steps
189b39c3a46SLois Curfman McInnes .  -snes_atol <atol> - absolute tolerance of residual norm
190b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
191b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
192b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
193b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
19482738288SBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear or minimization
19582738288SBarry Smith                                solver; hence iterations will continue until max_it
1961fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
19782738288SBarry Smith                                of convergence test
19882738288SBarry Smith .  -snes_monitor - prints residual norm at each iteration
199d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
200d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
20182738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
202e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
20336851e7fSLois Curfman McInnes -  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
20482738288SBarry Smith 
20582738288SBarry Smith     Options Database for Eisenstat-Walker method:
20682738288SBarry Smith +  -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence
20782738288SBarry Smith .  -snes_ksp_eq_version ver - version of  Eisenstat-Walker method
20836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
20936851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
21036851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
21136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
21236851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
21336851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
21482738288SBarry Smith 
21511ca99fdSLois Curfman McInnes    Notes:
21611ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
21711ca99fdSLois Curfman McInnes    the users manual.
21883e2fdc7SBarry Smith 
21936851e7fSLois Curfman McInnes    Level: beginner
22036851e7fSLois Curfman McInnes 
2219b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2229b94acceSBarry Smith 
22315091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetTypeFromOptions()
2249b94acceSBarry Smith @*/
2259b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
2269b94acceSBarry Smith {
2279b94acceSBarry Smith   double   tmp;
2289b94acceSBarry Smith   SLES     sles;
22981f57ec7SBarry Smith   int      ierr, flg,i,loc[4],nmax = 4;
2303c7409f5SSatish Balay   int      version   = PETSC_DEFAULT;
2319b94acceSBarry Smith   double   rtol_0    = PETSC_DEFAULT;
2329b94acceSBarry Smith   double   rtol_max  = PETSC_DEFAULT;
2339b94acceSBarry Smith   double   gamma2    = PETSC_DEFAULT;
2349b94acceSBarry Smith   double   alpha     = PETSC_DEFAULT;
2359b94acceSBarry Smith   double   alpha2    = PETSC_DEFAULT;
2369b94acceSBarry Smith   double   threshold = PETSC_DEFAULT;
2379b94acceSBarry Smith 
2383a40ed3dSBarry Smith   PetscFunctionBegin;
23977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
24015091d37SBarry Smith   ierr = SNESSetTypeFromOptions(snes);CHKERRQ(ierr);
241ca161407SBarry Smith 
24215091d37SBarry Smith   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
2433c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg);CHKERRQ(ierr);
244d64ed03dSBarry Smith   if (flg) {
245d64ed03dSBarry Smith     ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
246d64ed03dSBarry Smith   }
2473c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg);CHKERRQ(ierr);
248d64ed03dSBarry Smith   if (flg) {
249d64ed03dSBarry Smith     ierr = SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
250d64ed03dSBarry Smith   }
2513c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg);CHKERRQ(ierr);
252d64ed03dSBarry Smith   if (flg) {
253d64ed03dSBarry Smith     ierr = SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
254d64ed03dSBarry Smith   }
2553c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg);CHKERRQ(ierr);
2563c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr);
257d7a720efSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg);CHKERRQ(ierr);
258d64ed03dSBarry Smith   if (flg) { ierr = SNESSetTrustRegionTolerance(snes,tmp);CHKERRQ(ierr); }
259d7a720efSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg);CHKERRQ(ierr);
260d64ed03dSBarry Smith   if (flg) { ierr = SNESSetMinimizationFunctionTolerance(snes,tmp);CHKERRQ(ierr);}
2613c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg);CHKERRQ(ierr);
2623c7409f5SSatish Balay   if (flg) { snes->ksp_ewconv = 1; }
263b18e04deSLois Curfman McInnes   ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg);CHKERRQ(ierr);
264b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg);CHKERRQ(ierr);
265b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr);
266b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg);CHKERRQ(ierr);
267b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg);CHKERRQ(ierr);
268b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg);CHKERRQ(ierr);
269b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr);
27093c39befSBarry Smith 
27193c39befSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_no_convergence_test",&flg);CHKERRQ(ierr);
27293c39befSBarry Smith   if (flg) {snes->converged = 0;}
27393c39befSBarry Smith 
2749b94acceSBarry Smith   ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha,
2759b94acceSBarry Smith                                   alpha2,threshold);CHKERRQ(ierr);
2765bbad29bSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_cancelmonitors",&flg);CHKERRQ(ierr);
2775cd90555SBarry Smith   if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
2783c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg);CHKERRQ(ierr);
279b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);}
2803c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg);CHKERRQ(ierr);
281b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);}
2823f1db9ecSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor",&flg);CHKERRQ(ierr);
283b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);}
284d132466eSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor_update",&flg);CHKERRQ(ierr);
285b8a78c4aSBarry Smith   if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitorUpdate,0,0);CHKERRQ(ierr);}
28681f57ec7SBarry Smith   ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr);
2873c7409f5SSatish Balay   if (flg) {
28817699dbbSLois Curfman McInnes     int    rank = 0;
289d7e8b826SBarry Smith     DrawLG lg;
29017699dbbSLois Curfman McInnes     MPI_Initialized(&rank);
291d132466eSBarry Smith     if (rank) {ierr = MPI_Comm_rank(snes->comm,&rank);CHKERRQ(ierr);}
29217699dbbSLois Curfman McInnes     if (!rank) {
29381f57ec7SBarry Smith       ierr = SNESLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg);CHKERRQ(ierr);
294b8a78c4aSBarry Smith       ierr = SNESSetMonitor(snes,SNESLGMonitor,lg,( int (*)(void *))SNESLGMonitorDestroy);CHKERRQ(ierr);
2959b94acceSBarry Smith       PLogObjectParent(snes,lg);
2969b94acceSBarry Smith     }
2979b94acceSBarry Smith   }
298e24b481bSBarry Smith 
2993c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);CHKERRQ(ierr);
3003c7409f5SSatish Balay   if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
3019b94acceSBarry Smith     ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,
3029b94acceSBarry Smith                  SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
303981c4779SBarry Smith     PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
304981c4779SBarry Smith   } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
30531615d04SLois Curfman McInnes     ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,
30631615d04SLois Curfman McInnes                  SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr);
307d64ed03dSBarry Smith     PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
3089b94acceSBarry Smith   }
309639f9d9dSBarry Smith 
310639f9d9dSBarry Smith   for ( i=0; i<numberofsetfromoptions; i++ ) {
311639f9d9dSBarry Smith     ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
312639f9d9dSBarry Smith   }
313639f9d9dSBarry Smith 
3149b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
3159b94acceSBarry Smith   ierr = SLESSetFromOptions(sles);CHKERRQ(ierr);
31693993e2dSLois Curfman McInnes 
317e24b481bSBarry Smith   /* set the special KSP monitor for matrix-free application */
318e24b481bSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg);CHKERRQ(ierr);
319e24b481bSBarry Smith   if (flg) {
320e24b481bSBarry Smith     KSP ksp;
321e24b481bSBarry Smith     ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
322b8a78c4aSBarry Smith     ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr);
323e24b481bSBarry Smith   }
324e24b481bSBarry Smith 
32582bf6240SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help", &flg);CHKERRQ(ierr);
32682bf6240SBarry Smith   if (flg) { ierr = SNESPrintHelp(snes);CHKERRQ(ierr);}
32782bf6240SBarry Smith 
3283a40ed3dSBarry Smith   if (snes->setfromoptions) {
3293a40ed3dSBarry Smith     ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
3303a40ed3dSBarry Smith   }
3313a40ed3dSBarry Smith   PetscFunctionReturn(0);
3329b94acceSBarry Smith }
3339b94acceSBarry Smith 
334a847f771SSatish Balay 
3355615d1e5SSatish Balay #undef __FUNC__
336d4bb536fSBarry Smith #define __FUNC__ "SNESSetApplicationContext"
3379b94acceSBarry Smith /*@
3389b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3399b94acceSBarry Smith    the nonlinear solvers.
3409b94acceSBarry Smith 
341fee21e36SBarry Smith    Collective on SNES
342fee21e36SBarry Smith 
343c7afd0dbSLois Curfman McInnes    Input Parameters:
344c7afd0dbSLois Curfman McInnes +  snes - the SNES context
345c7afd0dbSLois Curfman McInnes -  usrP - optional user context
346c7afd0dbSLois Curfman McInnes 
34736851e7fSLois Curfman McInnes    Level: intermediate
34836851e7fSLois Curfman McInnes 
3499b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3509b94acceSBarry Smith 
3519b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3529b94acceSBarry Smith @*/
3539b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
3549b94acceSBarry Smith {
3553a40ed3dSBarry Smith   PetscFunctionBegin;
35677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3579b94acceSBarry Smith   snes->user		= usrP;
3583a40ed3dSBarry Smith   PetscFunctionReturn(0);
3599b94acceSBarry Smith }
36074679c65SBarry Smith 
3615615d1e5SSatish Balay #undef __FUNC__
362d4bb536fSBarry Smith #define __FUNC__ "SNESGetApplicationContext"
3639b94acceSBarry Smith /*@C
3649b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3659b94acceSBarry Smith    nonlinear solvers.
3669b94acceSBarry Smith 
367c7afd0dbSLois Curfman McInnes    Not Collective
368c7afd0dbSLois Curfman McInnes 
3699b94acceSBarry Smith    Input Parameter:
3709b94acceSBarry Smith .  snes - SNES context
3719b94acceSBarry Smith 
3729b94acceSBarry Smith    Output Parameter:
3739b94acceSBarry Smith .  usrP - user context
3749b94acceSBarry Smith 
37536851e7fSLois Curfman McInnes    Level: intermediate
37636851e7fSLois Curfman McInnes 
3779b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3789b94acceSBarry Smith 
3799b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3809b94acceSBarry Smith @*/
3819b94acceSBarry Smith int SNESGetApplicationContext( SNES snes,  void **usrP )
3829b94acceSBarry Smith {
3833a40ed3dSBarry Smith   PetscFunctionBegin;
38477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3859b94acceSBarry Smith   *usrP = snes->user;
3863a40ed3dSBarry Smith   PetscFunctionReturn(0);
3879b94acceSBarry Smith }
38874679c65SBarry Smith 
3895615d1e5SSatish Balay #undef __FUNC__
390d4bb536fSBarry Smith #define __FUNC__ "SNESGetIterationNumber"
3919b94acceSBarry Smith /*@
392c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
393c8228a4eSBarry Smith    at this time.
3949b94acceSBarry Smith 
395c7afd0dbSLois Curfman McInnes    Not Collective
396c7afd0dbSLois Curfman McInnes 
3979b94acceSBarry Smith    Input Parameter:
3989b94acceSBarry Smith .  snes - SNES context
3999b94acceSBarry Smith 
4009b94acceSBarry Smith    Output Parameter:
4019b94acceSBarry Smith .  iter - iteration number
4029b94acceSBarry Smith 
403c8228a4eSBarry Smith    Notes:
404c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
405c8228a4eSBarry Smith 
406c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
40708405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
40808405cd6SLois Curfman McInnes .vb
40908405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
41008405cd6SLois Curfman McInnes       if (!(it % 2)) {
41108405cd6SLois Curfman McInnes         [compute Jacobian here]
41208405cd6SLois Curfman McInnes       }
41308405cd6SLois Curfman McInnes .ve
414c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
41508405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
416c8228a4eSBarry Smith 
41736851e7fSLois Curfman McInnes    Level: intermediate
41836851e7fSLois Curfman McInnes 
4199b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
4209b94acceSBarry Smith @*/
4219b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
4229b94acceSBarry Smith {
4233a40ed3dSBarry Smith   PetscFunctionBegin;
42477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
42574679c65SBarry Smith   PetscValidIntPointer(iter);
4269b94acceSBarry Smith   *iter = snes->iter;
4273a40ed3dSBarry Smith   PetscFunctionReturn(0);
4289b94acceSBarry Smith }
42974679c65SBarry Smith 
4305615d1e5SSatish Balay #undef __FUNC__
4315615d1e5SSatish Balay #define __FUNC__ "SNESGetFunctionNorm"
4329b94acceSBarry Smith /*@
4339b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4349b94acceSBarry Smith    with SNESSSetFunction().
4359b94acceSBarry Smith 
436c7afd0dbSLois Curfman McInnes    Collective on SNES
437c7afd0dbSLois Curfman McInnes 
4389b94acceSBarry Smith    Input Parameter:
4399b94acceSBarry Smith .  snes - SNES context
4409b94acceSBarry Smith 
4419b94acceSBarry Smith    Output Parameter:
4429b94acceSBarry Smith .  fnorm - 2-norm of function
4439b94acceSBarry Smith 
4449b94acceSBarry Smith    Note:
4459b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
446a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
447a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
4489b94acceSBarry Smith 
44936851e7fSLois Curfman McInnes    Level: intermediate
45036851e7fSLois Curfman McInnes 
4519b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
452a86d99e1SLois Curfman McInnes 
45308405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
4549b94acceSBarry Smith @*/
4559b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm)
4569b94acceSBarry Smith {
4573a40ed3dSBarry Smith   PetscFunctionBegin;
45877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
45974679c65SBarry Smith   PetscValidScalarPointer(fnorm);
46074679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
461d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only");
46274679c65SBarry Smith   }
4639b94acceSBarry Smith   *fnorm = snes->norm;
4643a40ed3dSBarry Smith   PetscFunctionReturn(0);
4659b94acceSBarry Smith }
46674679c65SBarry Smith 
4675615d1e5SSatish Balay #undef __FUNC__
4685615d1e5SSatish Balay #define __FUNC__ "SNESGetGradientNorm"
4699b94acceSBarry Smith /*@
4709b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
4719b94acceSBarry Smith    with SNESSSetGradient().
4729b94acceSBarry Smith 
473c7afd0dbSLois Curfman McInnes    Collective on SNES
474c7afd0dbSLois Curfman McInnes 
4759b94acceSBarry Smith    Input Parameter:
4769b94acceSBarry Smith .  snes - SNES context
4779b94acceSBarry Smith 
4789b94acceSBarry Smith    Output Parameter:
4799b94acceSBarry Smith .  fnorm - 2-norm of gradient
4809b94acceSBarry Smith 
4819b94acceSBarry Smith    Note:
4829b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
483a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
484a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
4859b94acceSBarry Smith 
48636851e7fSLois Curfman McInnes    Level: intermediate
48736851e7fSLois Curfman McInnes 
4889b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
489a86d99e1SLois Curfman McInnes 
490a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
4919b94acceSBarry Smith @*/
4929b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm)
4939b94acceSBarry Smith {
4943a40ed3dSBarry Smith   PetscFunctionBegin;
49577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
49674679c65SBarry Smith   PetscValidScalarPointer(gnorm);
49774679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
498d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
49974679c65SBarry Smith   }
5009b94acceSBarry Smith   *gnorm = snes->norm;
5013a40ed3dSBarry Smith   PetscFunctionReturn(0);
5029b94acceSBarry Smith }
50374679c65SBarry Smith 
5045615d1e5SSatish Balay #undef __FUNC__
505d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberUnsuccessfulSteps"
5069b94acceSBarry Smith /*@
5079b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
5089b94acceSBarry Smith    attempted by the nonlinear solver.
5099b94acceSBarry Smith 
510c7afd0dbSLois Curfman McInnes    Not Collective
511c7afd0dbSLois Curfman McInnes 
5129b94acceSBarry Smith    Input Parameter:
5139b94acceSBarry Smith .  snes - SNES context
5149b94acceSBarry Smith 
5159b94acceSBarry Smith    Output Parameter:
5169b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
5179b94acceSBarry Smith 
518c96a6f78SLois Curfman McInnes    Notes:
519c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
520c96a6f78SLois Curfman McInnes 
52136851e7fSLois Curfman McInnes    Level: intermediate
52236851e7fSLois Curfman McInnes 
5239b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
5249b94acceSBarry Smith @*/
5259b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
5269b94acceSBarry Smith {
5273a40ed3dSBarry Smith   PetscFunctionBegin;
52877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
52974679c65SBarry Smith   PetscValidIntPointer(nfails);
5309b94acceSBarry Smith   *nfails = snes->nfailures;
5313a40ed3dSBarry Smith   PetscFunctionReturn(0);
5329b94acceSBarry Smith }
533a847f771SSatish Balay 
5345615d1e5SSatish Balay #undef __FUNC__
535d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberLinearIterations"
536c96a6f78SLois Curfman McInnes /*@
537c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
538c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
539c96a6f78SLois Curfman McInnes 
540c7afd0dbSLois Curfman McInnes    Not Collective
541c7afd0dbSLois Curfman McInnes 
542c96a6f78SLois Curfman McInnes    Input Parameter:
543c96a6f78SLois Curfman McInnes .  snes - SNES context
544c96a6f78SLois Curfman McInnes 
545c96a6f78SLois Curfman McInnes    Output Parameter:
546c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
547c96a6f78SLois Curfman McInnes 
548c96a6f78SLois Curfman McInnes    Notes:
549c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
550c96a6f78SLois Curfman McInnes 
55136851e7fSLois Curfman McInnes    Level: intermediate
55236851e7fSLois Curfman McInnes 
553c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
554c96a6f78SLois Curfman McInnes @*/
55586bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
556c96a6f78SLois Curfman McInnes {
5573a40ed3dSBarry Smith   PetscFunctionBegin;
558c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
559c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
560c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5613a40ed3dSBarry Smith   PetscFunctionReturn(0);
562c96a6f78SLois Curfman McInnes }
563c96a6f78SLois Curfman McInnes 
564c96a6f78SLois Curfman McInnes #undef __FUNC__
565d4bb536fSBarry Smith #define __FUNC__ "SNESGetSLES"
5669b94acceSBarry Smith /*@C
5679b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
5689b94acceSBarry Smith 
569c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
570c7afd0dbSLois Curfman McInnes 
5719b94acceSBarry Smith    Input Parameter:
5729b94acceSBarry Smith .  snes - the SNES context
5739b94acceSBarry Smith 
5749b94acceSBarry Smith    Output Parameter:
5759b94acceSBarry Smith .  sles - the SLES context
5769b94acceSBarry Smith 
5779b94acceSBarry Smith    Notes:
5789b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
5799b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
5809b94acceSBarry Smith    KSP and PC contexts as well.
5819b94acceSBarry Smith 
58236851e7fSLois Curfman McInnes    Level: beginner
58336851e7fSLois Curfman McInnes 
5849b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
5859b94acceSBarry Smith 
5869b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
5879b94acceSBarry Smith @*/
5889b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
5899b94acceSBarry Smith {
5903a40ed3dSBarry Smith   PetscFunctionBegin;
59177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5929b94acceSBarry Smith   *sles = snes->sles;
5933a40ed3dSBarry Smith   PetscFunctionReturn(0);
5949b94acceSBarry Smith }
59582bf6240SBarry Smith 
596e24b481bSBarry Smith #undef __FUNC__
597e24b481bSBarry Smith #define __FUNC__ "SNESPublish_Petsc"
598454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj)
599e24b481bSBarry Smith {
600aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
601454a90a3SBarry Smith   SNES          v = (SNES) obj;
602e24b481bSBarry Smith   int          ierr;
60343d6d2cbSBarry Smith #endif
604e24b481bSBarry Smith 
605e24b481bSBarry Smith   PetscFunctionBegin;
606e24b481bSBarry Smith 
60743d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
608e24b481bSBarry Smith   /* if it is already published then return */
609e24b481bSBarry Smith   if (v->amem >=0 ) PetscFunctionReturn(0);
610e24b481bSBarry Smith 
611454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
612e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
613e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
614e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
615e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
616454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
617433994e6SBarry Smith #endif
618e24b481bSBarry Smith   PetscFunctionReturn(0);
619e24b481bSBarry Smith }
620e24b481bSBarry Smith 
6219b94acceSBarry Smith /* -----------------------------------------------------------*/
6225615d1e5SSatish Balay #undef __FUNC__
6235615d1e5SSatish Balay #define __FUNC__ "SNESCreate"
6249b94acceSBarry Smith /*@C
6259b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
6269b94acceSBarry Smith 
627c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
628c7afd0dbSLois Curfman McInnes 
629c7afd0dbSLois Curfman McInnes    Input Parameters:
630c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
631c7afd0dbSLois Curfman McInnes -  type - type of method, either
632c7afd0dbSLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
633c7afd0dbSLois Curfman McInnes    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
6349b94acceSBarry Smith 
6359b94acceSBarry Smith    Output Parameter:
6369b94acceSBarry Smith .  outsnes - the new SNES context
6379b94acceSBarry Smith 
638c7afd0dbSLois Curfman McInnes    Options Database Keys:
639c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
640c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
641c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
642c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
643c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
644c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
645c1f60f51SBarry Smith 
64636851e7fSLois Curfman McInnes    Level: beginner
64736851e7fSLois Curfman McInnes 
6489b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
6499b94acceSBarry Smith 
65063a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy()
6519b94acceSBarry Smith @*/
6524b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
6539b94acceSBarry Smith {
6549b94acceSBarry Smith   int                 ierr;
6559b94acceSBarry Smith   SNES                snes;
6569b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
65737fcc0dbSBarry Smith 
6583a40ed3dSBarry Smith   PetscFunctionBegin;
6599b94acceSBarry Smith   *outsnes = 0;
660d64ed03dSBarry Smith   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){
661d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type");
662d64ed03dSBarry Smith   }
6633f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
6649b94acceSBarry Smith   PLogObjectCreate(snes);
665e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6669b94acceSBarry Smith   snes->max_its           = 50;
6679750a799SBarry Smith   snes->max_funcs	  = 10000;
6689b94acceSBarry Smith   snes->norm		  = 0.0;
6695a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
670b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
671b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
6729b94acceSBarry Smith     snes->atol		  = 1.e-10;
673d64ed03dSBarry Smith   } else {
674b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
675b4874afaSBarry Smith     snes->ttol            = 0.0;
676b4874afaSBarry Smith     snes->atol		  = 1.e-50;
677b4874afaSBarry Smith   }
6789b94acceSBarry Smith   snes->xtol		  = 1.e-8;
679b18e04deSLois Curfman McInnes   snes->trunctol	  = 1.e-12; /* no longer used */
6809b94acceSBarry Smith   snes->nfuncs            = 0;
6819b94acceSBarry Smith   snes->nfailures         = 0;
6827a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
683639f9d9dSBarry Smith   snes->numbermonitors    = 0;
6849b94acceSBarry Smith   snes->data              = 0;
6859b94acceSBarry Smith   snes->view              = 0;
6869b94acceSBarry Smith   snes->computeumfunction = 0;
6879b94acceSBarry Smith   snes->umfunP            = 0;
6889b94acceSBarry Smith   snes->fc                = 0;
6899b94acceSBarry Smith   snes->deltatol          = 1.e-12;
6909b94acceSBarry Smith   snes->fmin              = -1.e30;
6919b94acceSBarry Smith   snes->method_class      = type;
6929b94acceSBarry Smith   snes->set_method_called = 0;
69382bf6240SBarry Smith   snes->setupcalled      = 0;
6949b94acceSBarry Smith   snes->ksp_ewconv        = 0;
6956f24a144SLois Curfman McInnes   snes->vwork             = 0;
6966f24a144SLois Curfman McInnes   snes->nwork             = 0;
697758f92a0SBarry Smith   snes->conv_hist_len     = 0;
698758f92a0SBarry Smith   snes->conv_hist_max     = 0;
699758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
700758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
701758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
702184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
7039b94acceSBarry Smith 
7049b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
7050452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx);CHKPTRQ(kctx);
706eed86810SBarry Smith   PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
7079b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7089b94acceSBarry Smith   kctx->version     = 2;
7099b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7109b94acceSBarry Smith                              this was too large for some test cases */
7119b94acceSBarry Smith   kctx->rtol_last   = 0;
7129b94acceSBarry Smith   kctx->rtol_max    = .9;
7139b94acceSBarry Smith   kctx->gamma       = 1.0;
7149b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7159b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7169b94acceSBarry Smith   kctx->threshold   = .1;
7179b94acceSBarry Smith   kctx->lresid_last = 0;
7189b94acceSBarry Smith   kctx->norm_last   = 0;
7199b94acceSBarry Smith 
7209b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr);
7219b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
7225334005bSBarry Smith 
7239b94acceSBarry Smith   *outsnes = snes;
724e24b481bSBarry Smith   PetscPublishAll(snes);
7253a40ed3dSBarry Smith   PetscFunctionReturn(0);
7269b94acceSBarry Smith }
7279b94acceSBarry Smith 
7289b94acceSBarry Smith /* --------------------------------------------------------------- */
7295615d1e5SSatish Balay #undef __FUNC__
730d4bb536fSBarry Smith #define __FUNC__ "SNESSetFunction"
7319b94acceSBarry Smith /*@C
7329b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
7339b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
7349b94acceSBarry Smith    equations.
7359b94acceSBarry Smith 
736fee21e36SBarry Smith    Collective on SNES
737fee21e36SBarry Smith 
738c7afd0dbSLois Curfman McInnes    Input Parameters:
739c7afd0dbSLois Curfman McInnes +  snes - the SNES context
740c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
741c7afd0dbSLois Curfman McInnes .  r - vector to store function value
742c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
743c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7449b94acceSBarry Smith 
745c7afd0dbSLois Curfman McInnes    Calling sequence of func:
7468d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
747c7afd0dbSLois Curfman McInnes 
748313e4042SLois Curfman McInnes .  f - function vector
749c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
7509b94acceSBarry Smith 
7519b94acceSBarry Smith    Notes:
7529b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
7539b94acceSBarry Smith $      f'(x) x = -f(x),
754c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
7559b94acceSBarry Smith 
7569b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7579b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7589b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
7599b94acceSBarry Smith 
76036851e7fSLois Curfman McInnes    Level: beginner
76136851e7fSLois Curfman McInnes 
7629b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
7639b94acceSBarry Smith 
764a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
7659b94acceSBarry Smith @*/
7665334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx)
7679b94acceSBarry Smith {
7683a40ed3dSBarry Smith   PetscFunctionBegin;
76977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
770184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
771184914b5SBarry Smith   PetscCheckSameComm(snes,r);
772a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
773a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
774a8c6a408SBarry Smith   }
775184914b5SBarry Smith 
7769b94acceSBarry Smith   snes->computefunction     = func;
7779b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7789b94acceSBarry Smith   snes->funP                = ctx;
7793a40ed3dSBarry Smith   PetscFunctionReturn(0);
7809b94acceSBarry Smith }
7819b94acceSBarry Smith 
7825615d1e5SSatish Balay #undef __FUNC__
7835615d1e5SSatish Balay #define __FUNC__ "SNESComputeFunction"
7849b94acceSBarry Smith /*@
78536851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
7869b94acceSBarry Smith                          SNESSetFunction().
7879b94acceSBarry Smith 
788c7afd0dbSLois Curfman McInnes    Collective on SNES
789c7afd0dbSLois Curfman McInnes 
7909b94acceSBarry Smith    Input Parameters:
791c7afd0dbSLois Curfman McInnes +  snes - the SNES context
792c7afd0dbSLois Curfman McInnes -  x - input vector
7939b94acceSBarry Smith 
7949b94acceSBarry Smith    Output Parameter:
7953638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
7969b94acceSBarry Smith 
7971bffabb2SLois Curfman McInnes    Notes:
7989b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7999b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
8009b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
8019b94acceSBarry Smith 
80236851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
80336851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
80436851e7fSLois Curfman McInnes    themselves.
80536851e7fSLois Curfman McInnes 
80636851e7fSLois Curfman McInnes    Level: developer
80736851e7fSLois Curfman McInnes 
8089b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
8099b94acceSBarry Smith 
810a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
8119b94acceSBarry Smith @*/
8129b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y)
8139b94acceSBarry Smith {
8149b94acceSBarry Smith   int    ierr;
8159b94acceSBarry Smith 
8163a40ed3dSBarry Smith   PetscFunctionBegin;
817184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
818184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
819184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
820184914b5SBarry Smith   PetscCheckSameComm(snes,x);
821184914b5SBarry Smith   PetscCheckSameComm(snes,y);
822d4bb536fSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
823a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
824d4bb536fSBarry Smith   }
825184914b5SBarry Smith 
8269b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
827d64ed03dSBarry Smith   PetscStackPush("SNES user function");
8289b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
829d64ed03dSBarry Smith   PetscStackPop;
830ae3c334cSLois Curfman McInnes   snes->nfuncs++;
8319b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
8323a40ed3dSBarry Smith   PetscFunctionReturn(0);
8339b94acceSBarry Smith }
8349b94acceSBarry Smith 
8355615d1e5SSatish Balay #undef __FUNC__
836d4bb536fSBarry Smith #define __FUNC__ "SNESSetMinimizationFunction"
8379b94acceSBarry Smith /*@C
8389b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
8399b94acceSBarry Smith    unconstrained minimization.
8409b94acceSBarry Smith 
841fee21e36SBarry Smith    Collective on SNES
842fee21e36SBarry Smith 
843c7afd0dbSLois Curfman McInnes    Input Parameters:
844c7afd0dbSLois Curfman McInnes +  snes - the SNES context
845c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
846c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
847c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8489b94acceSBarry Smith 
849c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8508d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,double *f,void *ctx);
851c7afd0dbSLois Curfman McInnes 
852c7afd0dbSLois Curfman McInnes +  x - input vector
8539b94acceSBarry Smith .  f - function
854c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined function context
8559b94acceSBarry Smith 
85636851e7fSLois Curfman McInnes    Level: beginner
85736851e7fSLois Curfman McInnes 
8589b94acceSBarry Smith    Notes:
8599b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
8609b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
8619b94acceSBarry Smith    SNESSetFunction().
8629b94acceSBarry Smith 
8639b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
8649b94acceSBarry Smith 
865a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
866a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
8679b94acceSBarry Smith @*/
868454a90a3SBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),void *ctx)
8699b94acceSBarry Smith {
8703a40ed3dSBarry Smith   PetscFunctionBegin;
87177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
872a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
873a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
874a8c6a408SBarry Smith   }
8759b94acceSBarry Smith   snes->computeumfunction   = func;
8769b94acceSBarry Smith   snes->umfunP              = ctx;
8773a40ed3dSBarry Smith   PetscFunctionReturn(0);
8789b94acceSBarry Smith }
8799b94acceSBarry Smith 
8805615d1e5SSatish Balay #undef __FUNC__
8815615d1e5SSatish Balay #define __FUNC__ "SNESComputeMinimizationFunction"
8829b94acceSBarry Smith /*@
8839b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
8849b94acceSBarry Smith    set with SNESSetMinimizationFunction().
8859b94acceSBarry Smith 
886c7afd0dbSLois Curfman McInnes    Collective on SNES
887c7afd0dbSLois Curfman McInnes 
8889b94acceSBarry Smith    Input Parameters:
889c7afd0dbSLois Curfman McInnes +  snes - the SNES context
890c7afd0dbSLois Curfman McInnes -  x - input vector
8919b94acceSBarry Smith 
8929b94acceSBarry Smith    Output Parameter:
8939b94acceSBarry Smith .  y - function value
8949b94acceSBarry Smith 
8959b94acceSBarry Smith    Notes:
8969b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
8979b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
8989b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
899a86d99e1SLois Curfman McInnes 
90036851e7fSLois Curfman McInnes    SNESComputeMinimizationFunction() is typically used within minimization
90136851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
90236851e7fSLois Curfman McInnes    themselves.
90336851e7fSLois Curfman McInnes 
90436851e7fSLois Curfman McInnes    Level: developer
90536851e7fSLois Curfman McInnes 
906a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
907a86d99e1SLois Curfman McInnes 
908a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
909a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
9109b94acceSBarry Smith @*/
9119b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y)
9129b94acceSBarry Smith {
9139b94acceSBarry Smith   int    ierr;
9143a40ed3dSBarry Smith 
9153a40ed3dSBarry Smith   PetscFunctionBegin;
916184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
917184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
918184914b5SBarry Smith   PetscCheckSameComm(snes,x);
919a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
920a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
921a8c6a408SBarry Smith   }
922184914b5SBarry Smith 
9239b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
924d64ed03dSBarry Smith   PetscStackPush("SNES user minimzation function");
9259b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr);
926d64ed03dSBarry Smith   PetscStackPop;
927ae3c334cSLois Curfman McInnes   snes->nfuncs++;
9289b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
9293a40ed3dSBarry Smith   PetscFunctionReturn(0);
9309b94acceSBarry Smith }
9319b94acceSBarry Smith 
9325615d1e5SSatish Balay #undef __FUNC__
933d4bb536fSBarry Smith #define __FUNC__ "SNESSetGradient"
9349b94acceSBarry Smith /*@C
9359b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
9369b94acceSBarry Smith    vector for use by the SNES routines.
9379b94acceSBarry Smith 
938c7afd0dbSLois Curfman McInnes    Collective on SNES
939c7afd0dbSLois Curfman McInnes 
9409b94acceSBarry Smith    Input Parameters:
941c7afd0dbSLois Curfman McInnes +  snes - the SNES context
9429b94acceSBarry Smith .  func - function evaluation routine
943044dda88SLois Curfman McInnes .  ctx - optional user-defined context for private data for the
944044dda88SLois Curfman McInnes          gradient evaluation routine (may be PETSC_NULL)
945c7afd0dbSLois Curfman McInnes -  r - vector to store gradient value
946fee21e36SBarry Smith 
9479b94acceSBarry Smith    Calling sequence of func:
9488d76a1e5SLois Curfman McInnes $     func (SNES, Vec x, Vec g, void *ctx);
9499b94acceSBarry Smith 
950c7afd0dbSLois Curfman McInnes +  x - input vector
9519b94acceSBarry Smith .  g - gradient vector
952c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined gradient context
9539b94acceSBarry Smith 
9549b94acceSBarry Smith    Notes:
9559b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
9569b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
9579b94acceSBarry Smith    SNESSetFunction().
9589b94acceSBarry Smith 
95936851e7fSLois Curfman McInnes    Level: beginner
96036851e7fSLois Curfman McInnes 
9619b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9629b94acceSBarry Smith 
963a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
964a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
9659b94acceSBarry Smith @*/
96674679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
9679b94acceSBarry Smith {
9683a40ed3dSBarry Smith   PetscFunctionBegin;
96977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
970184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
971184914b5SBarry Smith   PetscCheckSameComm(snes,r);
972a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
973a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
974a8c6a408SBarry Smith   }
9759b94acceSBarry Smith   snes->computefunction     = func;
9769b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
9779b94acceSBarry Smith   snes->funP                = ctx;
9783a40ed3dSBarry Smith   PetscFunctionReturn(0);
9799b94acceSBarry Smith }
9809b94acceSBarry Smith 
9815615d1e5SSatish Balay #undef __FUNC__
9825615d1e5SSatish Balay #define __FUNC__ "SNESComputeGradient"
9839b94acceSBarry Smith /*@
984a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
985a86d99e1SLois Curfman McInnes    SNESSetGradient().
9869b94acceSBarry Smith 
987c7afd0dbSLois Curfman McInnes    Collective on SNES
988c7afd0dbSLois Curfman McInnes 
9899b94acceSBarry Smith    Input Parameters:
990c7afd0dbSLois Curfman McInnes +  snes - the SNES context
991c7afd0dbSLois Curfman McInnes -  x - input vector
9929b94acceSBarry Smith 
9939b94acceSBarry Smith    Output Parameter:
9949b94acceSBarry Smith .  y - gradient vector
9959b94acceSBarry Smith 
9969b94acceSBarry Smith    Notes:
9979b94acceSBarry Smith    SNESComputeGradient() is valid only for
9989b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
9999b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
1000a86d99e1SLois Curfman McInnes 
100136851e7fSLois Curfman McInnes    SNESComputeGradient() is typically used within minimization
100236851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
100336851e7fSLois Curfman McInnes    themselves.
100436851e7fSLois Curfman McInnes 
100536851e7fSLois Curfman McInnes    Level: developer
100636851e7fSLois Curfman McInnes 
1007a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
1008a86d99e1SLois Curfman McInnes 
1009a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
1010a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
10119b94acceSBarry Smith @*/
10129b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y)
10139b94acceSBarry Smith {
10149b94acceSBarry Smith   int    ierr;
10153a40ed3dSBarry Smith 
10163a40ed3dSBarry Smith   PetscFunctionBegin;
1017184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1018184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1019184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
1020184914b5SBarry Smith   PetscCheckSameComm(snes,x);
1021184914b5SBarry Smith   PetscCheckSameComm(snes,y);
10223a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1023a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
10243a40ed3dSBarry Smith   }
10259b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
1026d64ed03dSBarry Smith   PetscStackPush("SNES user gradient function");
10279b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
1028d64ed03dSBarry Smith   PetscStackPop;
10299b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
10303a40ed3dSBarry Smith   PetscFunctionReturn(0);
10319b94acceSBarry Smith }
10329b94acceSBarry Smith 
10335615d1e5SSatish Balay #undef __FUNC__
10345615d1e5SSatish Balay #define __FUNC__ "SNESComputeJacobian"
103562fef451SLois Curfman McInnes /*@
103662fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
103762fef451SLois Curfman McInnes    set with SNESSetJacobian().
103862fef451SLois Curfman McInnes 
1039c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1040c7afd0dbSLois Curfman McInnes 
104162fef451SLois Curfman McInnes    Input Parameters:
1042c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1043c7afd0dbSLois Curfman McInnes -  x - input vector
104462fef451SLois Curfman McInnes 
104562fef451SLois Curfman McInnes    Output Parameters:
1046c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
104762fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1048c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1049fee21e36SBarry Smith 
105062fef451SLois Curfman McInnes    Notes:
105162fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
105262fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
105362fef451SLois Curfman McInnes 
1054dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1055dc5a77f8SLois Curfman McInnes    flag parameter.
105662fef451SLois Curfman McInnes 
105762fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
105862fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
1059005c665bSBarry Smith    methods is SNESComputeHessian().
106062fef451SLois Curfman McInnes 
106136851e7fSLois Curfman McInnes    SNESComputeJacobian() is typically used within nonlinear solver
106236851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
106336851e7fSLois Curfman McInnes    themselves.
106436851e7fSLois Curfman McInnes 
106536851e7fSLois Curfman McInnes    Level: developer
106636851e7fSLois Curfman McInnes 
106762fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
106862fef451SLois Curfman McInnes 
106962fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
107062fef451SLois Curfman McInnes @*/
10719b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10729b94acceSBarry Smith {
10739b94acceSBarry Smith   int    ierr;
10743a40ed3dSBarry Smith 
10753a40ed3dSBarry Smith   PetscFunctionBegin;
1076184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1077184914b5SBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE);
1078184914b5SBarry Smith   PetscCheckSameComm(snes,X);
10793a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1080a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
10813a40ed3dSBarry Smith   }
10823a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
10839b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
1084c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1085d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
10869b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1087d64ed03dSBarry Smith   PetscStackPop;
10889b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
10896d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
109077c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
109177c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
10923a40ed3dSBarry Smith   PetscFunctionReturn(0);
10939b94acceSBarry Smith }
10949b94acceSBarry Smith 
10955615d1e5SSatish Balay #undef __FUNC__
10965615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian"
109762fef451SLois Curfman McInnes /*@
109862fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
109962fef451SLois Curfman McInnes    set with SNESSetHessian().
110062fef451SLois Curfman McInnes 
1101c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1102c7afd0dbSLois Curfman McInnes 
110362fef451SLois Curfman McInnes    Input Parameters:
1104c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1105c7afd0dbSLois Curfman McInnes -  x - input vector
110662fef451SLois Curfman McInnes 
110762fef451SLois Curfman McInnes    Output Parameters:
1108c7afd0dbSLois Curfman McInnes +  A - Hessian matrix
110962fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1110c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1111fee21e36SBarry Smith 
111262fef451SLois Curfman McInnes    Notes:
111362fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
111462fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
111562fef451SLois Curfman McInnes 
1116dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1117dc5a77f8SLois Curfman McInnes    flag parameter.
111862fef451SLois Curfman McInnes 
111962fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
112062fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
112162fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
112262fef451SLois Curfman McInnes 
112336851e7fSLois Curfman McInnes    SNESComputeHessian() is typically used within minimization
112436851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
112536851e7fSLois Curfman McInnes    themselves.
112636851e7fSLois Curfman McInnes 
112736851e7fSLois Curfman McInnes    Level: developer
112836851e7fSLois Curfman McInnes 
112962fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
113062fef451SLois Curfman McInnes 
1131a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
1132a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
113362fef451SLois Curfman McInnes @*/
113462fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
11359b94acceSBarry Smith {
11369b94acceSBarry Smith   int    ierr;
11373a40ed3dSBarry Smith 
11383a40ed3dSBarry Smith   PetscFunctionBegin;
1139184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1140184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1141184914b5SBarry Smith   PetscCheckSameComm(snes,x);
11423a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1143a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
11443a40ed3dSBarry Smith   }
11453a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
114662fef451SLois Curfman McInnes   PLogEventBegin(SNES_HessianEval,snes,x,*A,*B);
11470f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
1148d64ed03dSBarry Smith   PetscStackPush("SNES user Hessian function");
114962fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr);
1150d64ed03dSBarry Smith   PetscStackPop;
115162fef451SLois Curfman McInnes   PLogEventEnd(SNES_HessianEval,snes,x,*A,*B);
11520f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
115377c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
115477c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11553a40ed3dSBarry Smith   PetscFunctionReturn(0);
11569b94acceSBarry Smith }
11579b94acceSBarry Smith 
11585615d1e5SSatish Balay #undef __FUNC__
1159d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian"
11609b94acceSBarry Smith /*@C
11619b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1162044dda88SLois Curfman McInnes    location to store the matrix.
11639b94acceSBarry Smith 
1164c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1165c7afd0dbSLois Curfman McInnes 
11669b94acceSBarry Smith    Input Parameters:
1167c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11689b94acceSBarry Smith .  A - Jacobian matrix
11699b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11709b94acceSBarry Smith .  func - Jacobian evaluation routine
1171c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11722cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11739b94acceSBarry Smith 
11749b94acceSBarry Smith    Calling sequence of func:
11758d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11769b94acceSBarry Smith 
1177c7afd0dbSLois Curfman McInnes +  x - input vector
11789b94acceSBarry Smith .  A - Jacobian matrix
11799b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1180ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1181ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1182c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11839b94acceSBarry Smith 
11849b94acceSBarry Smith    Notes:
1185dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
11862cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1187ac21db08SLois Curfman McInnes 
1188ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11899b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11909b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11919b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11929b94acceSBarry Smith    throughout the global iterations.
11939b94acceSBarry Smith 
119436851e7fSLois Curfman McInnes    Level: beginner
119536851e7fSLois Curfman McInnes 
11969b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11979b94acceSBarry Smith 
1198ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
11999b94acceSBarry Smith @*/
1200454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
12019b94acceSBarry Smith {
12023a40ed3dSBarry Smith   PetscFunctionBegin;
120377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1204184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
1205184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
1206184914b5SBarry Smith   PetscCheckSameComm(snes,A);
1207184914b5SBarry Smith   PetscCheckSameComm(snes,B);
1208a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1209a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
1210a8c6a408SBarry Smith   }
1211184914b5SBarry Smith 
12129b94acceSBarry Smith   snes->computejacobian = func;
12139b94acceSBarry Smith   snes->jacP            = ctx;
12149b94acceSBarry Smith   snes->jacobian        = A;
12159b94acceSBarry Smith   snes->jacobian_pre    = B;
12163a40ed3dSBarry Smith   PetscFunctionReturn(0);
12179b94acceSBarry Smith }
121862fef451SLois Curfman McInnes 
12195615d1e5SSatish Balay #undef __FUNC__
1220d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian"
1221c2aafc4cSSatish Balay /*@C
1222b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1223b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1224b4fd4287SBarry Smith 
1225c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1226c7afd0dbSLois Curfman McInnes 
1227b4fd4287SBarry Smith    Input Parameter:
1228b4fd4287SBarry Smith .  snes - the nonlinear solver context
1229b4fd4287SBarry Smith 
1230b4fd4287SBarry Smith    Output Parameters:
1231c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1232b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
1233c7afd0dbSLois Curfman McInnes -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1234fee21e36SBarry Smith 
123536851e7fSLois Curfman McInnes    Level: advanced
123636851e7fSLois Curfman McInnes 
1237b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1238b4fd4287SBarry Smith @*/
1239b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx)
1240b4fd4287SBarry Smith {
12413a40ed3dSBarry Smith   PetscFunctionBegin;
1242184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
12433a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1244a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
12453a40ed3dSBarry Smith   }
1246b4fd4287SBarry Smith   if (A)   *A = snes->jacobian;
1247b4fd4287SBarry Smith   if (B)   *B = snes->jacobian_pre;
1248b4fd4287SBarry Smith   if (ctx) *ctx = snes->jacP;
12493a40ed3dSBarry Smith   PetscFunctionReturn(0);
1250b4fd4287SBarry Smith }
1251b4fd4287SBarry Smith 
12525615d1e5SSatish Balay #undef __FUNC__
1253d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian"
12549b94acceSBarry Smith /*@C
12559b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
1256044dda88SLois Curfman McInnes    location to store the matrix.
12579b94acceSBarry Smith 
1258c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1259c7afd0dbSLois Curfman McInnes 
12609b94acceSBarry Smith    Input Parameters:
1261c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12629b94acceSBarry Smith .  A - Hessian matrix
12639b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
12649b94acceSBarry Smith .  func - Jacobian evaluation routine
1265c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1266313e4042SLois Curfman McInnes          Hessian evaluation routine (may be PETSC_NULL)
12679b94acceSBarry Smith 
12689b94acceSBarry Smith    Calling sequence of func:
12698d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12709b94acceSBarry Smith 
1271c7afd0dbSLois Curfman McInnes +  x - input vector
12729b94acceSBarry Smith .  A - Hessian matrix
12739b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1274ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1275ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1276c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Hessian context
12779b94acceSBarry Smith 
12789b94acceSBarry Smith    Notes:
1279dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
12802cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1281ac21db08SLois Curfman McInnes 
12829b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
12839b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
12849b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12859b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12869b94acceSBarry Smith    throughout the global iterations.
12879b94acceSBarry Smith 
128836851e7fSLois Curfman McInnes    Level: beginner
128936851e7fSLois Curfman McInnes 
12909b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
12919b94acceSBarry Smith 
1292ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
12939b94acceSBarry Smith @*/
1294454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
12959b94acceSBarry Smith {
12963a40ed3dSBarry Smith   PetscFunctionBegin;
129777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1298184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
1299184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
1300184914b5SBarry Smith   PetscCheckSameComm(snes,A);
1301184914b5SBarry Smith   PetscCheckSameComm(snes,B);
1302d4bb536fSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1303a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1304d4bb536fSBarry Smith   }
13059b94acceSBarry Smith   snes->computejacobian = func;
13069b94acceSBarry Smith   snes->jacP            = ctx;
13079b94acceSBarry Smith   snes->jacobian        = A;
13089b94acceSBarry Smith   snes->jacobian_pre    = B;
13093a40ed3dSBarry Smith   PetscFunctionReturn(0);
13109b94acceSBarry Smith }
13119b94acceSBarry Smith 
13125615d1e5SSatish Balay #undef __FUNC__
1313d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian"
131462fef451SLois Curfman McInnes /*@
131562fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
131662fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
131762fef451SLois Curfman McInnes 
1318c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object is parallel if SNES object is parallel
1319c7afd0dbSLois Curfman McInnes 
132062fef451SLois Curfman McInnes    Input Parameter:
132162fef451SLois Curfman McInnes .  snes - the nonlinear solver context
132262fef451SLois Curfman McInnes 
132362fef451SLois Curfman McInnes    Output Parameters:
1324c7afd0dbSLois Curfman McInnes +  A - location to stash Hessian matrix (or PETSC_NULL)
132562fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
1326c7afd0dbSLois Curfman McInnes -  ctx - location to stash Hessian ctx (or PETSC_NULL)
1327fee21e36SBarry Smith 
132836851e7fSLois Curfman McInnes    Level: advanced
132936851e7fSLois Curfman McInnes 
133062fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
1331c7afd0dbSLois Curfman McInnes 
1332c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian
133362fef451SLois Curfman McInnes @*/
133462fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx)
133562fef451SLois Curfman McInnes {
13363a40ed3dSBarry Smith   PetscFunctionBegin;
1337184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
13383a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){
1339a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
13403a40ed3dSBarry Smith   }
134162fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
134262fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
134362fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
13443a40ed3dSBarry Smith   PetscFunctionReturn(0);
134562fef451SLois Curfman McInnes }
134662fef451SLois Curfman McInnes 
13479b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13489b94acceSBarry Smith 
13495615d1e5SSatish Balay #undef __FUNC__
13505615d1e5SSatish Balay #define __FUNC__ "SNESSetUp"
13519b94acceSBarry Smith /*@
13529b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1353272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13549b94acceSBarry Smith 
1355fee21e36SBarry Smith    Collective on SNES
1356fee21e36SBarry Smith 
1357c7afd0dbSLois Curfman McInnes    Input Parameters:
1358c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1359c7afd0dbSLois Curfman McInnes -  x - the solution vector
1360c7afd0dbSLois Curfman McInnes 
1361272ac6f2SLois Curfman McInnes    Notes:
1362272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1363272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1364272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1365272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1366272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1367272ac6f2SLois Curfman McInnes 
136836851e7fSLois Curfman McInnes    Level: advanced
136936851e7fSLois Curfman McInnes 
13709b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13719b94acceSBarry Smith 
13729b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
13739b94acceSBarry Smith @*/
13748ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
13759b94acceSBarry Smith {
1376272ac6f2SLois Curfman McInnes   int ierr, flg;
13773a40ed3dSBarry Smith 
13783a40ed3dSBarry Smith   PetscFunctionBegin;
137977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
138077c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1381184914b5SBarry Smith   PetscCheckSameComm(snes,x);
13828ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
13839b94acceSBarry Smith 
1384c1f60f51SBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);CHKERRQ(ierr);
1385c1f60f51SBarry Smith   /*
1386c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1387dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1388c1f60f51SBarry Smith   */
1389c1f60f51SBarry Smith   if (flg) {
1390c1f60f51SBarry Smith     Mat J;
13915a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1392c1f60f51SBarry Smith     PLogObjectParent(snes,J);
1393c1f60f51SBarry Smith     snes->mfshell = J;
1394c1f60f51SBarry Smith     snes->jacobian = J;
1395c2aafc4cSSatish Balay     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1396c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n");
1397d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
1398c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n");
1399d4bb536fSBarry Smith     } else {
1400a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option");
1401d4bb536fSBarry Smith     }
14025a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1403c1f60f51SBarry Smith   }
1404272ac6f2SLois Curfman McInnes   ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);CHKERRQ(ierr);
1405c1f60f51SBarry Smith   /*
1406dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1407c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1408c1f60f51SBarry Smith    */
140931615d04SLois Curfman McInnes   if (flg) {
1410272ac6f2SLois Curfman McInnes     Mat J;
14115a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1412272ac6f2SLois Curfman McInnes     PLogObjectParent(snes,J);
1413272ac6f2SLois Curfman McInnes     snes->mfshell = J;
141483e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
141583e56afdSLois Curfman McInnes       ierr = SNESSetJacobian(snes,J,J,0,snes->funP);CHKERRQ(ierr);
141694a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n");
1417d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
141883e56afdSLois Curfman McInnes       ierr = SNESSetHessian(snes,J,J,0,snes->funP);CHKERRQ(ierr);
141994a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n");
1420d4bb536fSBarry Smith     } else {
1421a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option");
1422d4bb536fSBarry Smith     }
14235a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1424272ac6f2SLois Curfman McInnes   }
14259b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
1426*6831982aSBarry Smith     PetscTruth iseqtr;
1427*6831982aSBarry Smith 
1428a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
1429a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
14305a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1431a8c6a408SBarry Smith     if (snes->vec_func == snes->vec_sol) {
1432a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector");
1433a8c6a408SBarry Smith     }
1434a703fe33SLois Curfman McInnes 
1435a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
1436*6831982aSBarry Smith     ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr);
1437*6831982aSBarry Smith     if (snes->ksp_ewconv && !iseqtr) {
1438a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1439a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1440a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
14415a655dc6SBarry Smith       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr);
1442a703fe33SLois Curfman McInnes     }
14439b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
1444a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1445a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1446a8c6a408SBarry Smith     if (!snes->computeumfunction) {
1447a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first");
1448a8c6a408SBarry Smith     }
14495a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()");
1450d4bb536fSBarry Smith   } else {
1451a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class");
1452d4bb536fSBarry Smith   }
1453a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
145482bf6240SBarry Smith   snes->setupcalled = 1;
14553a40ed3dSBarry Smith   PetscFunctionReturn(0);
14569b94acceSBarry Smith }
14579b94acceSBarry Smith 
14585615d1e5SSatish Balay #undef __FUNC__
1459d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy"
14609b94acceSBarry Smith /*@C
14619b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
14629b94acceSBarry Smith    with SNESCreate().
14639b94acceSBarry Smith 
1464c7afd0dbSLois Curfman McInnes    Collective on SNES
1465c7afd0dbSLois Curfman McInnes 
14669b94acceSBarry Smith    Input Parameter:
14679b94acceSBarry Smith .  snes - the SNES context
14689b94acceSBarry Smith 
146936851e7fSLois Curfman McInnes    Level: beginner
147036851e7fSLois Curfman McInnes 
14719b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14729b94acceSBarry Smith 
147363a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14749b94acceSBarry Smith @*/
14759b94acceSBarry Smith int SNESDestroy(SNES snes)
14769b94acceSBarry Smith {
1477b8a78c4aSBarry Smith   int i,ierr;
14783a40ed3dSBarry Smith 
14793a40ed3dSBarry Smith   PetscFunctionBegin;
148077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
14813a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1482d4bb536fSBarry Smith 
1483be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
14840f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1485be0abb6dSBarry Smith 
1486e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1487606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
1488522c5e43SBarry Smith   if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);}
14899b94acceSBarry Smith   ierr = SLESDestroy(snes->sles);CHKERRQ(ierr);
1490522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1491b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++ ) {
1492b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1493b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1494b8a78c4aSBarry Smith     }
1495b8a78c4aSBarry Smith   }
14969b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
14970452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
14983a40ed3dSBarry Smith   PetscFunctionReturn(0);
14999b94acceSBarry Smith }
15009b94acceSBarry Smith 
15019b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
15029b94acceSBarry Smith 
15035615d1e5SSatish Balay #undef __FUNC__
15045615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances"
15059b94acceSBarry Smith /*@
1506d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15079b94acceSBarry Smith 
1508c7afd0dbSLois Curfman McInnes    Collective on SNES
1509c7afd0dbSLois Curfman McInnes 
15109b94acceSBarry Smith    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    Options Database Keys:
1520c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1521c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1522c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1523c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1524c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15259b94acceSBarry Smith 
1526d7a720efSLois Curfman McInnes    Notes:
15279b94acceSBarry Smith    The default maximum number of iterations is 50.
15289b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15299b94acceSBarry Smith 
153036851e7fSLois Curfman McInnes    Level: intermediate
153136851e7fSLois Curfman McInnes 
153233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15339b94acceSBarry Smith 
1534d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
15359b94acceSBarry Smith @*/
1536d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf)
15379b94acceSBarry Smith {
15383a40ed3dSBarry Smith   PetscFunctionBegin;
153977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1540d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1541d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1542d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1543d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1544d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15453a40ed3dSBarry Smith   PetscFunctionReturn(0);
15469b94acceSBarry Smith }
15479b94acceSBarry Smith 
15485615d1e5SSatish Balay #undef __FUNC__
15495615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances"
15509b94acceSBarry Smith /*@
155133174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
155233174efeSLois Curfman McInnes 
1553c7afd0dbSLois Curfman McInnes    Not Collective
1554c7afd0dbSLois Curfman McInnes 
155533174efeSLois Curfman McInnes    Input Parameters:
1556c7afd0dbSLois Curfman McInnes +  snes - the SNES context
155733174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
155833174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
155933174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
156033174efeSLois Curfman McInnes            of the change in the solution between steps
156133174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1562c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1563fee21e36SBarry Smith 
156433174efeSLois Curfman McInnes    Notes:
156533174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
156633174efeSLois Curfman McInnes 
156736851e7fSLois Curfman McInnes    Level: intermediate
156836851e7fSLois Curfman McInnes 
156933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
157033174efeSLois Curfman McInnes 
157133174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
157233174efeSLois Curfman McInnes @*/
157333174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf)
157433174efeSLois Curfman McInnes {
15753a40ed3dSBarry Smith   PetscFunctionBegin;
157633174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
157733174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
157833174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
157933174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
158033174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
158133174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
15823a40ed3dSBarry Smith   PetscFunctionReturn(0);
158333174efeSLois Curfman McInnes }
158433174efeSLois Curfman McInnes 
15855615d1e5SSatish Balay #undef __FUNC__
15865615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance"
158733174efeSLois Curfman McInnes /*@
15889b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
15899b94acceSBarry Smith 
1590fee21e36SBarry Smith    Collective on SNES
1591fee21e36SBarry Smith 
1592c7afd0dbSLois Curfman McInnes    Input Parameters:
1593c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1594c7afd0dbSLois Curfman McInnes -  tol - tolerance
1595c7afd0dbSLois Curfman McInnes 
15969b94acceSBarry Smith    Options Database Key:
1597c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
15989b94acceSBarry Smith 
159936851e7fSLois Curfman McInnes    Level: intermediate
160036851e7fSLois Curfman McInnes 
16019b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16029b94acceSBarry Smith 
1603d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
16049b94acceSBarry Smith @*/
16059b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol)
16069b94acceSBarry Smith {
16073a40ed3dSBarry Smith   PetscFunctionBegin;
160877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16099b94acceSBarry Smith   snes->deltatol = tol;
16103a40ed3dSBarry Smith   PetscFunctionReturn(0);
16119b94acceSBarry Smith }
16129b94acceSBarry Smith 
16135615d1e5SSatish Balay #undef __FUNC__
16145615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance"
16159b94acceSBarry Smith /*@
161677c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
16179b94acceSBarry Smith    for unconstrained minimization solvers.
16189b94acceSBarry Smith 
1619fee21e36SBarry Smith    Collective on SNES
1620fee21e36SBarry Smith 
1621c7afd0dbSLois Curfman McInnes    Input Parameters:
1622c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1623c7afd0dbSLois Curfman McInnes -  ftol - minimum function tolerance
1624c7afd0dbSLois Curfman McInnes 
16259b94acceSBarry Smith    Options Database Key:
1626c7afd0dbSLois Curfman McInnes .  -snes_fmin <ftol> - Sets ftol
16279b94acceSBarry Smith 
16289b94acceSBarry Smith    Note:
162977c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
16309b94acceSBarry Smith    methods only.
16319b94acceSBarry Smith 
163236851e7fSLois Curfman McInnes    Level: intermediate
163336851e7fSLois Curfman McInnes 
16349b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
16359b94acceSBarry Smith 
1636d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
16379b94acceSBarry Smith @*/
163877c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol)
16399b94acceSBarry Smith {
16403a40ed3dSBarry Smith   PetscFunctionBegin;
164177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16429b94acceSBarry Smith   snes->fmin = ftol;
16433a40ed3dSBarry Smith   PetscFunctionReturn(0);
16449b94acceSBarry Smith }
1645df9fa365SBarry Smith /*
1646df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1647df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1648df9fa365SBarry Smith    macros instead of functions
1649df9fa365SBarry Smith */
1650ce1608b8SBarry Smith #undef __FUNC__
1651ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor"
1652ce1608b8SBarry Smith int SNESLGMonitor(SNES snes,int it,double norm,void *ctx)
1653ce1608b8SBarry Smith {
1654ce1608b8SBarry Smith   int ierr;
1655ce1608b8SBarry Smith 
1656ce1608b8SBarry Smith   PetscFunctionBegin;
1657184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1658ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1659ce1608b8SBarry Smith   PetscFunctionReturn(0);
1660ce1608b8SBarry Smith }
1661ce1608b8SBarry Smith 
1662df9fa365SBarry Smith #undef __FUNC__
1663df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorCreate"
1664df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n, DrawLG *draw)
1665df9fa365SBarry Smith {
1666df9fa365SBarry Smith   int ierr;
1667df9fa365SBarry Smith 
1668df9fa365SBarry Smith   PetscFunctionBegin;
1669df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1670df9fa365SBarry Smith   PetscFunctionReturn(0);
1671df9fa365SBarry Smith }
1672df9fa365SBarry Smith 
1673df9fa365SBarry Smith #undef __FUNC__
1674df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorDestroy"
1675df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw)
1676df9fa365SBarry Smith {
1677df9fa365SBarry Smith   int ierr;
1678df9fa365SBarry Smith 
1679df9fa365SBarry Smith   PetscFunctionBegin;
1680df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1681df9fa365SBarry Smith   PetscFunctionReturn(0);
1682df9fa365SBarry Smith }
1683df9fa365SBarry Smith 
16849b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
16859b94acceSBarry Smith 
16865615d1e5SSatish Balay #undef __FUNC__
1687d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor"
16889b94acceSBarry Smith /*@C
16895cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
16909b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
16919b94acceSBarry Smith    progress.
16929b94acceSBarry Smith 
1693fee21e36SBarry Smith    Collective on SNES
1694fee21e36SBarry Smith 
1695c7afd0dbSLois Curfman McInnes    Input Parameters:
1696c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1697c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1698b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1699c7afd0dbSLois Curfman McInnes           monitor routine (may be PETSC_NULL)
1700b8a78c4aSBarry Smith -  monitordestroy - options routine that frees monitor context
17019b94acceSBarry Smith 
1702c7afd0dbSLois Curfman McInnes    Calling sequence of func:
170340a0c1c6SLois Curfman McInnes $     int func(SNES snes,int its, double norm,void *mctx)
1704c7afd0dbSLois Curfman McInnes 
1705c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1706c7afd0dbSLois Curfman McInnes .    its - iteration number
1707c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
1708c7afd0dbSLois Curfman McInnes             for SNES_NONLINEAR_EQUATIONS methods
170940a0c1c6SLois Curfman McInnes .    norm - 2-norm gradient value (may be estimated)
1710c7afd0dbSLois Curfman McInnes             for SNES_UNCONSTRAINED_MINIMIZATION methods
171140a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
17129b94acceSBarry Smith 
17139665c990SLois Curfman McInnes    Options Database Keys:
1714c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1715c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1716c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1717c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1718c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1719c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1720c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1721c7afd0dbSLois Curfman McInnes                             the options database.
17229665c990SLois Curfman McInnes 
1723639f9d9dSBarry Smith    Notes:
17246bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
17256bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
17266bc08f3fSLois Curfman McInnes    order in which they were set.
1727639f9d9dSBarry Smith 
172836851e7fSLois Curfman McInnes    Level: intermediate
172936851e7fSLois Curfman McInnes 
17309b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
17319b94acceSBarry Smith 
17325cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
17339b94acceSBarry Smith @*/
1734b8a78c4aSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx,int (*monitordestroy)(void *))
17359b94acceSBarry Smith {
17363a40ed3dSBarry Smith   PetscFunctionBegin;
1737184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1738639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
1739a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set");
1740639f9d9dSBarry Smith   }
1741639f9d9dSBarry Smith 
1742639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1743b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1744639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
17453a40ed3dSBarry Smith   PetscFunctionReturn(0);
17469b94acceSBarry Smith }
17479b94acceSBarry Smith 
17485615d1e5SSatish Balay #undef __FUNC__
17495cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor"
17505cd90555SBarry Smith /*@C
17515cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
17525cd90555SBarry Smith 
1753c7afd0dbSLois Curfman McInnes    Collective on SNES
1754c7afd0dbSLois Curfman McInnes 
17555cd90555SBarry Smith    Input Parameters:
17565cd90555SBarry Smith .  snes - the SNES context
17575cd90555SBarry Smith 
17585cd90555SBarry Smith    Options Database:
1759c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1760c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1761c7afd0dbSLois Curfman McInnes     set via the options database
17625cd90555SBarry Smith 
17635cd90555SBarry Smith    Notes:
17645cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
17655cd90555SBarry Smith 
176636851e7fSLois Curfman McInnes    Level: intermediate
176736851e7fSLois Curfman McInnes 
17685cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
17695cd90555SBarry Smith 
17705cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
17715cd90555SBarry Smith @*/
17725cd90555SBarry Smith int SNESClearMonitor( SNES snes )
17735cd90555SBarry Smith {
17745cd90555SBarry Smith   PetscFunctionBegin;
1775184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
17765cd90555SBarry Smith   snes->numbermonitors = 0;
17775cd90555SBarry Smith   PetscFunctionReturn(0);
17785cd90555SBarry Smith }
17795cd90555SBarry Smith 
17805cd90555SBarry Smith #undef __FUNC__
1781d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest"
17829b94acceSBarry Smith /*@C
17839b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
17849b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
17859b94acceSBarry Smith 
1786fee21e36SBarry Smith    Collective on SNES
1787fee21e36SBarry Smith 
1788c7afd0dbSLois Curfman McInnes    Input Parameters:
1789c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1790c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1791c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1792c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
17939b94acceSBarry Smith 
1794c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1795184914b5SBarry Smith $     int func (SNES snes,double xnorm,double gnorm,double f,SNESConvergedReason *reason,void *cctx)
1796c7afd0dbSLois Curfman McInnes 
1797c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1798c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1799184914b5SBarry Smith .    reason - reason for convergence/divergence
1800c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1801c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods)
1802c7afd0dbSLois Curfman McInnes .    f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods)
1803c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods)
1804c7afd0dbSLois Curfman McInnes -    f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods)
18059b94acceSBarry Smith 
180636851e7fSLois Curfman McInnes    Level: advanced
180736851e7fSLois Curfman McInnes 
18089b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
18099b94acceSBarry Smith 
181040191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
181140191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
18129b94acceSBarry Smith @*/
1813184914b5SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,SNESConvergedReason*,void*),void *cctx)
18149b94acceSBarry Smith {
18153a40ed3dSBarry Smith   PetscFunctionBegin;
1816184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18179b94acceSBarry Smith   (snes)->converged = func;
18189b94acceSBarry Smith   (snes)->cnvP      = cctx;
18193a40ed3dSBarry Smith   PetscFunctionReturn(0);
18209b94acceSBarry Smith }
18219b94acceSBarry Smith 
18225615d1e5SSatish Balay #undef __FUNC__
1823184914b5SBarry Smith #define __FUNC__ "SNESGetConvergedReason"
1824184914b5SBarry Smith /*@C
1825184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1826184914b5SBarry Smith 
1827184914b5SBarry Smith    Not Collective
1828184914b5SBarry Smith 
1829184914b5SBarry Smith    Input Parameter:
1830184914b5SBarry Smith .  snes - the SNES context
1831184914b5SBarry Smith 
1832184914b5SBarry Smith    Output Parameter:
1833184914b5SBarry Smith .  reason - negative value indicates diverged, positive value converged, see snes.h or the
1834184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1835184914b5SBarry Smith 
1836184914b5SBarry Smith    Level: intermediate
1837184914b5SBarry Smith 
1838184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1839184914b5SBarry Smith 
1840184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1841184914b5SBarry Smith 
1842184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
1843184914b5SBarry Smith           SNESConverged_UM_LS(), SNESConverged_UM_TR()
1844184914b5SBarry Smith @*/
1845184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1846184914b5SBarry Smith {
1847184914b5SBarry Smith   PetscFunctionBegin;
1848184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1849184914b5SBarry Smith   *reason = snes->reason;
1850184914b5SBarry Smith   PetscFunctionReturn(0);
1851184914b5SBarry Smith }
1852184914b5SBarry Smith 
1853184914b5SBarry Smith #undef __FUNC__
1854d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory"
1855c9005455SLois Curfman McInnes /*@
1856c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1857c9005455SLois Curfman McInnes 
1858fee21e36SBarry Smith    Collective on SNES
1859fee21e36SBarry Smith 
1860c7afd0dbSLois Curfman McInnes    Input Parameters:
1861c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1862c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1863758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1864758f92a0SBarry Smith          negative if not converged) for each solve.
1865758f92a0SBarry Smith .  na  - size of a and its
1866758f92a0SBarry Smith -  reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero,
1867758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1868c7afd0dbSLois Curfman McInnes 
1869c9005455SLois Curfman McInnes    Notes:
1870c9005455SLois Curfman McInnes    If set, this array will contain the function norms (for
1871c9005455SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods) or gradient norms
1872c9005455SLois Curfman McInnes    (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed
1873c9005455SLois Curfman McInnes    at each step.
1874c9005455SLois Curfman McInnes 
1875c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1876c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1877c9005455SLois Curfman McInnes    during the section of code that is being timed.
1878c9005455SLois Curfman McInnes 
187936851e7fSLois Curfman McInnes    Level: intermediate
188036851e7fSLois Curfman McInnes 
1881c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1882758f92a0SBarry Smith 
188308405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1884758f92a0SBarry Smith 
1885c9005455SLois Curfman McInnes @*/
1886758f92a0SBarry Smith int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset)
1887c9005455SLois Curfman McInnes {
18883a40ed3dSBarry Smith   PetscFunctionBegin;
1889c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1890c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1891c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1892758f92a0SBarry Smith   snes->conv_hist_its   = its;
1893758f92a0SBarry Smith   snes->conv_hist_max   = na;
1894758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1895758f92a0SBarry Smith   PetscFunctionReturn(0);
1896758f92a0SBarry Smith }
1897758f92a0SBarry Smith 
1898758f92a0SBarry Smith #undef __FUNC__
1899758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory"
19000c4c9dddSBarry Smith /*@C
1901758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1902758f92a0SBarry Smith 
1903758f92a0SBarry Smith    Collective on SNES
1904758f92a0SBarry Smith 
1905758f92a0SBarry Smith    Input Parameter:
1906758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1907758f92a0SBarry Smith 
1908758f92a0SBarry Smith    Output Parameters:
1909758f92a0SBarry Smith .  a   - array to hold history
1910758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1911758f92a0SBarry Smith          negative if not converged) for each solve.
1912758f92a0SBarry Smith -  na  - size of a and its
1913758f92a0SBarry Smith 
1914758f92a0SBarry Smith    Notes:
1915758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1916758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1917758f92a0SBarry Smith 
1918758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1919758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1920758f92a0SBarry Smith    during the section of code that is being timed.
1921758f92a0SBarry Smith 
1922758f92a0SBarry Smith    Level: intermediate
1923758f92a0SBarry Smith 
1924758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1925758f92a0SBarry Smith 
1926758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1927758f92a0SBarry Smith 
1928758f92a0SBarry Smith @*/
1929758f92a0SBarry Smith int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na)
1930758f92a0SBarry Smith {
1931758f92a0SBarry Smith   PetscFunctionBegin;
1932758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1933758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1934758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1935758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
19363a40ed3dSBarry Smith   PetscFunctionReturn(0);
1937c9005455SLois Curfman McInnes }
1938c9005455SLois Curfman McInnes 
1939c9005455SLois Curfman McInnes #undef __FUNC__
19405615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private"
19419b94acceSBarry Smith /*
19429b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
19439b94acceSBarry Smith    positive parameter delta.
19449b94acceSBarry Smith 
19459b94acceSBarry Smith     Input Parameters:
1946c7afd0dbSLois Curfman McInnes +   snes - the SNES context
19479b94acceSBarry Smith .   y - approximate solution of linear system
19489b94acceSBarry Smith .   fnorm - 2-norm of current function
1949c7afd0dbSLois Curfman McInnes -   delta - trust region size
19509b94acceSBarry Smith 
19519b94acceSBarry Smith     Output Parameters:
1952c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
19539b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
19549b94acceSBarry Smith     region, and exceeds zero otherwise.
1955c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
19569b94acceSBarry Smith 
19579b94acceSBarry Smith     Note:
1958*6831982aSBarry Smith     For non-trust region methods such as SNESEQLS, the parameter delta
19599b94acceSBarry Smith     is set to be the maximum allowable step size.
19609b94acceSBarry Smith 
19619b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
19629b94acceSBarry Smith */
19639b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta,
19649b94acceSBarry Smith                           double *gpnorm,double *ynorm)
19659b94acceSBarry Smith {
19669b94acceSBarry Smith   double norm;
19679b94acceSBarry Smith   Scalar cnorm;
19683a40ed3dSBarry Smith   int    ierr;
19693a40ed3dSBarry Smith 
19703a40ed3dSBarry Smith   PetscFunctionBegin;
1971184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1972184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
1973184914b5SBarry Smith   PetscCheckSameComm(snes,y);
1974184914b5SBarry Smith 
19753a40ed3dSBarry Smith   ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr);
19769b94acceSBarry Smith   if (norm > *delta) {
19779b94acceSBarry Smith      norm = *delta/norm;
19789b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
19799b94acceSBarry Smith      cnorm = norm;
19809b94acceSBarry Smith      VecScale( &cnorm, y );
19819b94acceSBarry Smith      *ynorm = *delta;
19829b94acceSBarry Smith   } else {
19839b94acceSBarry Smith      *gpnorm = 0.0;
19849b94acceSBarry Smith      *ynorm = norm;
19859b94acceSBarry Smith   }
19863a40ed3dSBarry Smith   PetscFunctionReturn(0);
19879b94acceSBarry Smith }
19889b94acceSBarry Smith 
19895615d1e5SSatish Balay #undef __FUNC__
19905615d1e5SSatish Balay #define __FUNC__ "SNESSolve"
19919b94acceSBarry Smith /*@
19929b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
199363a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
19949b94acceSBarry Smith 
1995c7afd0dbSLois Curfman McInnes    Collective on SNES
1996c7afd0dbSLois Curfman McInnes 
1997b2002411SLois Curfman McInnes    Input Parameters:
1998c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1999c7afd0dbSLois Curfman McInnes -  x - the solution vector
20009b94acceSBarry Smith 
20019b94acceSBarry Smith    Output Parameter:
2002b2002411SLois Curfman McInnes .  its - number of iterations until termination
20039b94acceSBarry Smith 
2004b2002411SLois Curfman McInnes    Notes:
20058ddd3da0SLois Curfman McInnes    The user should initialize the vector, x, with the initial guess
20068ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
20078ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
20088ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
20098ddd3da0SLois Curfman McInnes 
201036851e7fSLois Curfman McInnes    Level: beginner
201136851e7fSLois Curfman McInnes 
20129b94acceSBarry Smith .keywords: SNES, nonlinear, solve
20139b94acceSBarry Smith 
201463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
20159b94acceSBarry Smith @*/
20168ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
20179b94acceSBarry Smith {
20183c7409f5SSatish Balay   int ierr, flg;
2019052efed2SBarry Smith 
20203a40ed3dSBarry Smith   PetscFunctionBegin;
202177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2022184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
2023184914b5SBarry Smith   PetscCheckSameComm(snes,x);
202474679c65SBarry Smith   PetscValidIntPointer(its);
202582bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
2026c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
2027758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
20289b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
2029c96a6f78SLois Curfman McInnes   snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0;
20309b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr);
20319b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
20320f5bd95cSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_view", &flg);CHKERRQ(ierr);
20336d4a8577SBarry Smith   if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); }
20343a40ed3dSBarry Smith   PetscFunctionReturn(0);
20359b94acceSBarry Smith }
20369b94acceSBarry Smith 
20379b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
20389b94acceSBarry Smith 
20395615d1e5SSatish Balay #undef __FUNC__
20405615d1e5SSatish Balay #define __FUNC__ "SNESSetType"
204182bf6240SBarry Smith /*@C
20424b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
20439b94acceSBarry Smith 
2044fee21e36SBarry Smith    Collective on SNES
2045fee21e36SBarry Smith 
2046c7afd0dbSLois Curfman McInnes    Input Parameters:
2047c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2048454a90a3SBarry Smith -  type - a known method
2049c7afd0dbSLois Curfman McInnes 
2050c7afd0dbSLois Curfman McInnes    Options Database Key:
2051454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2052c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2053ae12b187SLois Curfman McInnes 
20549b94acceSBarry Smith    Notes:
20559b94acceSBarry Smith    See "petsc/include/snes.h" for available methods (for instance)
2056*6831982aSBarry Smith +    SNESEQLS - Newton's method with line search
2057c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
2058*6831982aSBarry Smith .    SNESEQTR - Newton's method with trust region
2059c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
2060*6831982aSBarry Smith .    SNESUMTR - Newton's method with trust region
2061c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
2062*6831982aSBarry Smith -    SNESUMLS - Newton's method with line search
2063c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
20649b94acceSBarry Smith 
2065ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2066ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2067ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2068ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2069ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2070ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2071ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2072ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2073ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
207436851e7fSLois Curfman McInnes   appropriate method.  In other words, this routine is not for beginners.
207536851e7fSLois Curfman McInnes 
207636851e7fSLois Curfman McInnes   Level: intermediate
2077a703fe33SLois Curfman McInnes 
2078454a90a3SBarry Smith .keywords: SNES, set, type
20799b94acceSBarry Smith @*/
2080454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type)
20819b94acceSBarry Smith {
20820f5bd95cSBarry Smith   int        ierr, (*r)(SNES);
2083*6831982aSBarry Smith   PetscTruth match;
20843a40ed3dSBarry Smith 
20853a40ed3dSBarry Smith   PetscFunctionBegin;
208677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
20870f5bd95cSBarry Smith   PetscValidCharPointer(type);
208882bf6240SBarry Smith 
2089*6831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
20900f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
209182bf6240SBarry Smith 
209282bf6240SBarry Smith   if (snes->setupcalled) {
2093e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
209482bf6240SBarry Smith     snes->data = 0;
209582bf6240SBarry Smith   }
20967d1a2b2bSBarry Smith 
20979b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
209882bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
209982bf6240SBarry Smith 
2100454a90a3SBarry Smith   ierr =  FListFind(snes->comm, SNESList, type,(int (**)(void *)) &r );CHKERRQ(ierr);
210182bf6240SBarry Smith 
2102454a90a3SBarry Smith   if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",type);
21031548b14aSBarry Smith 
2104606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
210582bf6240SBarry Smith   snes->data = 0;
21063a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
210782bf6240SBarry Smith 
2108454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
210982bf6240SBarry Smith   snes->set_method_called = 1;
211082bf6240SBarry Smith 
21113a40ed3dSBarry Smith   PetscFunctionReturn(0);
21129b94acceSBarry Smith }
21139b94acceSBarry Smith 
2114a847f771SSatish Balay 
21159b94acceSBarry Smith /* --------------------------------------------------------------------- */
21165615d1e5SSatish Balay #undef __FUNC__
2117d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy"
21189b94acceSBarry Smith /*@C
21199b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
21209b94acceSBarry Smith    registered by SNESRegister().
21219b94acceSBarry Smith 
2122fee21e36SBarry Smith    Not Collective
2123fee21e36SBarry Smith 
212436851e7fSLois Curfman McInnes    Level: advanced
212536851e7fSLois Curfman McInnes 
21269b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
21279b94acceSBarry Smith 
21289b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
21299b94acceSBarry Smith @*/
2130cf256101SBarry Smith int SNESRegisterDestroy(void)
21319b94acceSBarry Smith {
213282bf6240SBarry Smith   int ierr;
213382bf6240SBarry Smith 
21343a40ed3dSBarry Smith   PetscFunctionBegin;
213582bf6240SBarry Smith   if (SNESList) {
2136488ecbafSBarry Smith     ierr = FListDestroy( SNESList );CHKERRQ(ierr);
213782bf6240SBarry Smith     SNESList = 0;
21389b94acceSBarry Smith   }
213984cb2905SBarry Smith   SNESRegisterAllCalled = 0;
21403a40ed3dSBarry Smith   PetscFunctionReturn(0);
21419b94acceSBarry Smith }
21429b94acceSBarry Smith 
21435615d1e5SSatish Balay #undef __FUNC__
2144d4bb536fSBarry Smith #define __FUNC__ "SNESGetType"
21459b94acceSBarry Smith /*@C
21469a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
21479b94acceSBarry Smith 
2148c7afd0dbSLois Curfman McInnes    Not Collective
2149c7afd0dbSLois Curfman McInnes 
21509b94acceSBarry Smith    Input Parameter:
21514b0e389bSBarry Smith .  snes - nonlinear solver context
21529b94acceSBarry Smith 
21539b94acceSBarry Smith    Output Parameter:
2154454a90a3SBarry Smith .  type - SNES method (a charactor string)
21559b94acceSBarry Smith 
215636851e7fSLois Curfman McInnes    Level: intermediate
215736851e7fSLois Curfman McInnes 
2158454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
21599b94acceSBarry Smith @*/
2160454a90a3SBarry Smith int SNESGetType(SNES snes, SNESType *type)
21619b94acceSBarry Smith {
21623a40ed3dSBarry Smith   PetscFunctionBegin;
2163184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2164454a90a3SBarry Smith   *type = snes->type_name;
21653a40ed3dSBarry Smith   PetscFunctionReturn(0);
21669b94acceSBarry Smith }
21679b94acceSBarry Smith 
21685615d1e5SSatish Balay #undef __FUNC__
2169d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution"
21709b94acceSBarry Smith /*@C
21719b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
21729b94acceSBarry Smith    stored.
21739b94acceSBarry Smith 
2174c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2175c7afd0dbSLois Curfman McInnes 
21769b94acceSBarry Smith    Input Parameter:
21779b94acceSBarry Smith .  snes - the SNES context
21789b94acceSBarry Smith 
21799b94acceSBarry Smith    Output Parameter:
21809b94acceSBarry Smith .  x - the solution
21819b94acceSBarry Smith 
218236851e7fSLois Curfman McInnes    Level: advanced
218336851e7fSLois Curfman McInnes 
21849b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
21859b94acceSBarry Smith 
2186a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
21879b94acceSBarry Smith @*/
21889b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
21899b94acceSBarry Smith {
21903a40ed3dSBarry Smith   PetscFunctionBegin;
219177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21929b94acceSBarry Smith   *x = snes->vec_sol_always;
21933a40ed3dSBarry Smith   PetscFunctionReturn(0);
21949b94acceSBarry Smith }
21959b94acceSBarry Smith 
21965615d1e5SSatish Balay #undef __FUNC__
2197d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate"
21989b94acceSBarry Smith /*@C
21999b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
22009b94acceSBarry Smith    stored.
22019b94acceSBarry Smith 
2202c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2203c7afd0dbSLois Curfman McInnes 
22049b94acceSBarry Smith    Input Parameter:
22059b94acceSBarry Smith .  snes - the SNES context
22069b94acceSBarry Smith 
22079b94acceSBarry Smith    Output Parameter:
22089b94acceSBarry Smith .  x - the solution update
22099b94acceSBarry Smith 
221036851e7fSLois Curfman McInnes    Level: advanced
221136851e7fSLois Curfman McInnes 
22129b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
22139b94acceSBarry Smith 
22149b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
22159b94acceSBarry Smith @*/
22169b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
22179b94acceSBarry Smith {
22183a40ed3dSBarry Smith   PetscFunctionBegin;
221977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
22209b94acceSBarry Smith   *x = snes->vec_sol_update_always;
22213a40ed3dSBarry Smith   PetscFunctionReturn(0);
22229b94acceSBarry Smith }
22239b94acceSBarry Smith 
22245615d1e5SSatish Balay #undef __FUNC__
2225d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction"
22269b94acceSBarry Smith /*@C
22273638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
22289b94acceSBarry Smith 
2229c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2230c7afd0dbSLois Curfman McInnes 
22319b94acceSBarry Smith    Input Parameter:
22329b94acceSBarry Smith .  snes - the SNES context
22339b94acceSBarry Smith 
22349b94acceSBarry Smith    Output Parameter:
22357bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
22367bf4e008SBarry Smith -  ctx - the function context (or PETSC_NULL)
22379b94acceSBarry Smith 
22389b94acceSBarry Smith    Notes:
22399b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
22409b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
22419b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
22429b94acceSBarry Smith 
224336851e7fSLois Curfman McInnes    Level: advanced
224436851e7fSLois Curfman McInnes 
2245a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
22469b94acceSBarry Smith 
224731615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
224831615d04SLois Curfman McInnes           SNESGetGradient()
22497bf4e008SBarry Smith 
22509b94acceSBarry Smith @*/
22517bf4e008SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx)
22529b94acceSBarry Smith {
22533a40ed3dSBarry Smith   PetscFunctionBegin;
225477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2255a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
2256a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
2257a8c6a408SBarry Smith   }
22587bf4e008SBarry Smith   if (r)   *r = snes->vec_func_always;
22597bf4e008SBarry Smith   if (ctx) *ctx = snes->funP;
22603a40ed3dSBarry Smith   PetscFunctionReturn(0);
22619b94acceSBarry Smith }
22629b94acceSBarry Smith 
22635615d1e5SSatish Balay #undef __FUNC__
2264d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient"
22659b94acceSBarry Smith /*@C
22663638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
22679b94acceSBarry Smith 
2268c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2269c7afd0dbSLois Curfman McInnes 
22709b94acceSBarry Smith    Input Parameter:
22719b94acceSBarry Smith .  snes - the SNES context
22729b94acceSBarry Smith 
22739b94acceSBarry Smith    Output Parameter:
22747bf4e008SBarry Smith +  r - the gradient (or PETSC_NULL)
22757bf4e008SBarry Smith -  ctx - the gradient context (or PETSC_NULL)
22769b94acceSBarry Smith 
22779b94acceSBarry Smith    Notes:
22789b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
22799b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
22809b94acceSBarry Smith    SNESGetFunction().
22819b94acceSBarry Smith 
228236851e7fSLois Curfman McInnes    Level: advanced
228336851e7fSLois Curfman McInnes 
22849b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
22859b94acceSBarry Smith 
22867bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(),
22877bf4e008SBarry Smith           SNESSetGradient(), SNESSetFunction()
22887bf4e008SBarry Smith 
22899b94acceSBarry Smith @*/
22907bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx)
22919b94acceSBarry Smith {
22923a40ed3dSBarry Smith   PetscFunctionBegin;
229377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
22943a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2295a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
22963a40ed3dSBarry Smith   }
22977bf4e008SBarry Smith   if (r)   *r = snes->vec_func_always;
22987bf4e008SBarry Smith   if (ctx) *ctx = snes->funP;
22993a40ed3dSBarry Smith   PetscFunctionReturn(0);
23009b94acceSBarry Smith }
23019b94acceSBarry Smith 
23025615d1e5SSatish Balay #undef __FUNC__
2303d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction"
23047bf4e008SBarry Smith /*@C
23059b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
23069b94acceSBarry Smith    unconstrained minimization problems.
23079b94acceSBarry Smith 
2308c7afd0dbSLois Curfman McInnes    Not Collective
2309c7afd0dbSLois Curfman McInnes 
23109b94acceSBarry Smith    Input Parameter:
23119b94acceSBarry Smith .  snes - the SNES context
23129b94acceSBarry Smith 
23139b94acceSBarry Smith    Output Parameter:
23147bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
23157bf4e008SBarry Smith -  ctx - the function context (or PETSC_NULL)
23169b94acceSBarry Smith 
23179b94acceSBarry Smith    Notes:
23189b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
23199b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
23209b94acceSBarry Smith    SNESGetFunction().
23219b94acceSBarry Smith 
232236851e7fSLois Curfman McInnes    Level: advanced
232336851e7fSLois Curfman McInnes 
23249b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
23259b94acceSBarry Smith 
23267bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction()
23277bf4e008SBarry Smith 
23289b94acceSBarry Smith @*/
23297bf4e008SBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r,void **ctx)
23309b94acceSBarry Smith {
23313a40ed3dSBarry Smith   PetscFunctionBegin;
233277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
233374679c65SBarry Smith   PetscValidScalarPointer(r);
23343a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2335a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
23363a40ed3dSBarry Smith   }
23377bf4e008SBarry Smith   if (r)   *r = snes->fc;
23387bf4e008SBarry Smith   if (ctx) *ctx = snes->umfunP;
23393a40ed3dSBarry Smith   PetscFunctionReturn(0);
23409b94acceSBarry Smith }
23419b94acceSBarry Smith 
23425615d1e5SSatish Balay #undef __FUNC__
2343d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix"
23443c7409f5SSatish Balay /*@C
23453c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2346d850072dSLois Curfman McInnes    SNES options in the database.
23473c7409f5SSatish Balay 
2348fee21e36SBarry Smith    Collective on SNES
2349fee21e36SBarry Smith 
2350c7afd0dbSLois Curfman McInnes    Input Parameter:
2351c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2352c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2353c7afd0dbSLois Curfman McInnes 
2354d850072dSLois Curfman McInnes    Notes:
2355a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2356c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2357d850072dSLois Curfman McInnes 
235836851e7fSLois Curfman McInnes    Level: advanced
235936851e7fSLois Curfman McInnes 
23603c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2361a86d99e1SLois Curfman McInnes 
2362a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
23633c7409f5SSatish Balay @*/
23643c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
23653c7409f5SSatish Balay {
23663c7409f5SSatish Balay   int ierr;
23673c7409f5SSatish Balay 
23683a40ed3dSBarry Smith   PetscFunctionBegin;
236977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2370639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr);
23713c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
23723a40ed3dSBarry Smith   PetscFunctionReturn(0);
23733c7409f5SSatish Balay }
23743c7409f5SSatish Balay 
23755615d1e5SSatish Balay #undef __FUNC__
2376d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix"
23773c7409f5SSatish Balay /*@C
2378f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2379d850072dSLois Curfman McInnes    SNES options in the database.
23803c7409f5SSatish Balay 
2381fee21e36SBarry Smith    Collective on SNES
2382fee21e36SBarry Smith 
2383c7afd0dbSLois Curfman McInnes    Input Parameters:
2384c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2385c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2386c7afd0dbSLois Curfman McInnes 
2387d850072dSLois Curfman McInnes    Notes:
2388a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2389c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2390d850072dSLois Curfman McInnes 
239136851e7fSLois Curfman McInnes    Level: advanced
239236851e7fSLois Curfman McInnes 
23933c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2394a86d99e1SLois Curfman McInnes 
2395a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
23963c7409f5SSatish Balay @*/
23973c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
23983c7409f5SSatish Balay {
23993c7409f5SSatish Balay   int ierr;
24003c7409f5SSatish Balay 
24013a40ed3dSBarry Smith   PetscFunctionBegin;
240277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2403639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr);
24043c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
24053a40ed3dSBarry Smith   PetscFunctionReturn(0);
24063c7409f5SSatish Balay }
24073c7409f5SSatish Balay 
24085615d1e5SSatish Balay #undef __FUNC__
2409d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix"
24109ab63eb5SSatish Balay /*@C
24113c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
24123c7409f5SSatish Balay    SNES options in the database.
24133c7409f5SSatish Balay 
2414c7afd0dbSLois Curfman McInnes    Not Collective
2415c7afd0dbSLois Curfman McInnes 
24163c7409f5SSatish Balay    Input Parameter:
24173c7409f5SSatish Balay .  snes - the SNES context
24183c7409f5SSatish Balay 
24193c7409f5SSatish Balay    Output Parameter:
24203c7409f5SSatish Balay .  prefix - pointer to the prefix string used
24213c7409f5SSatish Balay 
24229ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
24239ab63eb5SSatish Balay    sufficient length to hold the prefix.
24249ab63eb5SSatish Balay 
242536851e7fSLois Curfman McInnes    Level: advanced
242636851e7fSLois Curfman McInnes 
24273c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2428a86d99e1SLois Curfman McInnes 
2429a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
24303c7409f5SSatish Balay @*/
24313c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
24323c7409f5SSatish Balay {
24333c7409f5SSatish Balay   int ierr;
24343c7409f5SSatish Balay 
24353a40ed3dSBarry Smith   PetscFunctionBegin;
243677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2437639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr);
24383a40ed3dSBarry Smith   PetscFunctionReturn(0);
24393c7409f5SSatish Balay }
24403c7409f5SSatish Balay 
2441ca161407SBarry Smith #undef __FUNC__
2442ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp"
2443ca161407SBarry Smith /*@
2444ca161407SBarry Smith    SNESPrintHelp - Prints all options for the SNES component.
2445ca161407SBarry Smith 
2446c7afd0dbSLois Curfman McInnes    Collective on SNES
2447c7afd0dbSLois Curfman McInnes 
2448ca161407SBarry Smith    Input Parameter:
2449ca161407SBarry Smith .  snes - the SNES context
2450ca161407SBarry Smith 
2451ca161407SBarry Smith    Options Database Keys:
2452c7afd0dbSLois Curfman McInnes +  -help - Prints SNES options
2453c7afd0dbSLois Curfman McInnes -  -h - Prints SNES options
2454ca161407SBarry Smith 
245536851e7fSLois Curfman McInnes    Level: beginner
245636851e7fSLois Curfman McInnes 
2457ca161407SBarry Smith .keywords: SNES, nonlinear, help
2458ca161407SBarry Smith 
2459ca161407SBarry Smith .seealso: SNESSetFromOptions()
2460ca161407SBarry Smith @*/
2461ca161407SBarry Smith int SNESPrintHelp(SNES snes)
2462ca161407SBarry Smith {
2463ca161407SBarry Smith   char                p[64];
2464ca161407SBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
2465ca161407SBarry Smith   int                 ierr;
2466ca161407SBarry Smith 
2467ca161407SBarry Smith   PetscFunctionBegin;
2468ca161407SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2469ca161407SBarry Smith 
2470549d3d68SSatish Balay   ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
2471ca161407SBarry Smith   if (snes->prefix) PetscStrcat(p, snes->prefix);
2472ca161407SBarry Smith 
2473ca161407SBarry Smith   kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
2474ca161407SBarry Smith 
2475ebb8b11fSBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
2476d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr);
2477488ecbafSBarry Smith   ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr);
2478d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr);
2479d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr);
2480d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr);
2481d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr);
2482d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr);
2483d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr);
2484d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr);
2485d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr);
2486d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr);
2487d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_monitor: use default SNES convergence monitor, prints\n\
2488d132466eSBarry Smith     residual norm at each iteration.\n",p);CHKERRQ(ierr);
2489d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_smonitor: same as the above, but prints fewer digits of the\n\
2490ca161407SBarry Smith     residual norm for small residual norms. This is useful to conceal\n\
2491d132466eSBarry Smith     meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr);
2492d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr);
2493d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr);
2494d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr);
2495ca161407SBarry Smith   if (snes->type == SNES_NONLINEAR_EQUATIONS) {
2496d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2497d132466eSBarry Smith      " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr);
2498d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr);
2499d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr);
2500d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr);
2501d132466eSBarry 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);
2502d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr);
2503d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr);
2504d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2505d132466eSBarry Smith      "     %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr);
2506d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2507d132466eSBarry Smith      "     %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr);
2508d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2509d132466eSBarry Smith      "     %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr);
2510d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2511d132466eSBarry Smith      "     %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr);
2512d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2513d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr);
2514d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2515d132466eSBarry Smith      "     %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr);
2516d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,
2517d132466eSBarry Smith      "     %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr);
2518ca161407SBarry Smith   } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) {
2519d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr);
2520d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr);
2521d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr);
2522d132466eSBarry Smith     ierr = (*PetscHelpPrintf)(snes->comm,"   %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr);
2523ca161407SBarry Smith   }
2524454a90a3SBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <type> for help on ",p);CHKERRQ(ierr);
2525d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr);
2526ca161407SBarry Smith   if (snes->printhelp) {
2527ca161407SBarry Smith     ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr);
2528ca161407SBarry Smith   }
2529ca161407SBarry Smith   PetscFunctionReturn(0);
2530ca161407SBarry Smith }
25313c7409f5SSatish Balay 
2532acb85ae6SSatish Balay /*MC
2533b2002411SLois Curfman McInnes    SNESRegister - Adds a method to the nonlinear solver package.
25349b94acceSBarry Smith 
2535b2002411SLois Curfman McInnes    Synopsis:
2536b2002411SLois Curfman McInnes    SNESRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
25379b94acceSBarry Smith 
25388d76a1e5SLois Curfman McInnes    Not collective
25398d76a1e5SLois Curfman McInnes 
2540b2002411SLois Curfman McInnes    Input Parameters:
2541c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2542b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2543b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2544c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
25459b94acceSBarry Smith 
2546b2002411SLois Curfman McInnes    Notes:
2547b2002411SLois Curfman McInnes    SNESRegister() may be called multiple times to add several user-defined solvers.
25483a40ed3dSBarry Smith 
2549b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2550b2002411SLois Curfman McInnes    is ignored.
2551b2002411SLois Curfman McInnes 
2552b2002411SLois Curfman McInnes    Sample usage:
255369225d78SLois Curfman McInnes .vb
2554b2002411SLois Curfman McInnes    SNESRegister("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2555b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
255669225d78SLois Curfman McInnes .ve
2557b2002411SLois Curfman McInnes 
2558b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2559b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2560b2002411SLois Curfman McInnes    or at runtime via the option
2561b2002411SLois Curfman McInnes $     -snes_type my_solver
2562b2002411SLois Curfman McInnes 
256336851e7fSLois Curfman McInnes    Level: advanced
256436851e7fSLois Curfman McInnes 
256585614651SBarry Smith    $PETSC_ARCH, $PETSC_DIR, $PETSC_LDIR, and $BOPT occuring in pathname will be replaced with appropriate values.
2566dd438238SBarry Smith 
2567b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2568b2002411SLois Curfman McInnes 
2569b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2570b2002411SLois Curfman McInnes M*/
2571b2002411SLois Curfman McInnes 
2572b2002411SLois Curfman McInnes #undef __FUNC__
2573b2002411SLois Curfman McInnes #define __FUNC__ "SNESRegister_Private"
2574b2002411SLois Curfman McInnes int SNESRegister_Private(char *sname,char *path,char *name,int (*function)(SNES))
2575b2002411SLois Curfman McInnes {
2576b2002411SLois Curfman McInnes   char fullname[256];
2577b2002411SLois Curfman McInnes   int  ierr;
2578b2002411SLois Curfman McInnes 
2579b2002411SLois Curfman McInnes   PetscFunctionBegin;
2580250ffb4eSSatish Balay   ierr = FListConcat_Private(path,name,fullname); CHKERRQ(ierr);
2581488ecbafSBarry Smith   ierr = FListAdd_Private(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr);
2582b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2583b2002411SLois Curfman McInnes }
2584