xref: /petsc/src/snes/interface/snes.c (revision e74ef6923b2c0251c8aab3a0671bbb2dfd6aaf97)
173f4d377SMatthew Knepley /*$Id: snes.c,v 1.235 2001/08/21 21:03:49 bsmith Exp $*/
29b94acceSBarry Smith 
3e090d566SSatish Balay #include "src/snes/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
6b0a32e0cSBarry Smith PetscFList      SNESList = 0;
782bf6240SBarry Smith 
84a2ae208SSatish Balay #undef __FUNCT__
9a09944afSBarry Smith #define __FUNCT__ "SNESGetProblemType"
10a09944afSBarry Smith /*@C
11a09944afSBarry Smith    SNESGetProblemType -Indicates if SNES is solving a nonlinear system or a minimization
12a09944afSBarry Smith 
13a09944afSBarry Smith    Not Collective
14a09944afSBarry Smith 
15a09944afSBarry Smith    Input Parameter:
16a09944afSBarry Smith .  SNES - the SNES context
17a09944afSBarry Smith 
18a09944afSBarry Smith    Output Parameter:
19a09944afSBarry Smith .   type - SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
20a09944afSBarry Smith    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
21a09944afSBarry Smith 
22a09944afSBarry Smith    Level: intermediate
23a09944afSBarry Smith 
24a09944afSBarry Smith .keywords: SNES, problem type
25a09944afSBarry Smith 
26a09944afSBarry Smith .seealso: SNESCreate()
27a09944afSBarry Smith @*/
28a09944afSBarry Smith int SNESGetProblemType(SNES snes,SNESProblemType *type)
29a09944afSBarry Smith {
30a09944afSBarry Smith   PetscFunctionBegin;
31a09944afSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
32a09944afSBarry Smith   *type = snes->method_class;
33a09944afSBarry Smith   PetscFunctionReturn(0);
34a09944afSBarry Smith }
35a09944afSBarry Smith 
36a09944afSBarry Smith #undef __FUNCT__
374a2ae208SSatish Balay #define __FUNCT__ "SNESView"
387e2c5f70SBarry Smith /*@C
399b94acceSBarry Smith    SNESView - Prints the SNES data structure.
409b94acceSBarry Smith 
414c49b128SBarry Smith    Collective on SNES
42fee21e36SBarry Smith 
43c7afd0dbSLois Curfman McInnes    Input Parameters:
44c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
45c7afd0dbSLois Curfman McInnes -  viewer - visualization context
46c7afd0dbSLois Curfman McInnes 
479b94acceSBarry Smith    Options Database Key:
48c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
499b94acceSBarry Smith 
509b94acceSBarry Smith    Notes:
519b94acceSBarry Smith    The available visualization contexts include
52b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
53b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
54c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
55c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
56c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
579b94acceSBarry Smith 
583e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
59b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
609b94acceSBarry Smith 
6136851e7fSLois Curfman McInnes    Level: beginner
6236851e7fSLois Curfman McInnes 
639b94acceSBarry Smith .keywords: SNES, view
649b94acceSBarry Smith 
65b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
669b94acceSBarry Smith @*/
67b0a32e0cSBarry Smith int SNESView(SNES snes,PetscViewer viewer)
689b94acceSBarry Smith {
699b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
709b94acceSBarry Smith   int                 ierr;
719b94acceSBarry Smith   SLES                sles;
72454a90a3SBarry Smith   char                *type;
736831982aSBarry Smith   PetscTruth          isascii,isstring;
749b94acceSBarry Smith 
753a40ed3dSBarry Smith   PetscFunctionBegin;
7674679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
77b0a32e0cSBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(snes->comm);
78b0a32e0cSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
796831982aSBarry Smith   PetscCheckSameComm(snes,viewer);
8074679c65SBarry Smith 
81b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
82b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
830f5bd95cSBarry Smith   if (isascii) {
843a7fca6bSBarry Smith     if (snes->prefix) {
853a7fca6bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr);
863a7fca6bSBarry Smith     } else {
87b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
883a7fca6bSBarry Smith     }
89454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
90454a90a3SBarry Smith     if (type) {
91b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
92184914b5SBarry Smith     } else {
93b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
94184914b5SBarry Smith     }
950ef38995SBarry Smith     if (snes->view) {
96b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
970ef38995SBarry Smith       ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr);
98b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
990ef38995SBarry Smith     }
100b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
10177d8c4bbSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, solution=%g\n",
10277d8c4bbSBarry Smith                  snes->rtol,snes->atol,snes->xtol);CHKERRQ(ierr);
103b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
104b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
1050ef38995SBarry Smith     if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
106b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr);
1070ef38995SBarry Smith     }
1089b94acceSBarry Smith     if (snes->ksp_ewconv) {
1099b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
1109b94acceSBarry Smith       if (kctx) {
111b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
112b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
113b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1149b94acceSBarry Smith       }
1159b94acceSBarry Smith     }
1160f5bd95cSBarry Smith   } else if (isstring) {
117454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
118b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
11919bcc07fSBarry Smith   }
12077ed5343SBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
121b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1229b94acceSBarry Smith   ierr = SLESView(sles,viewer);CHKERRQ(ierr);
123b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1243a40ed3dSBarry Smith   PetscFunctionReturn(0);
1259b94acceSBarry Smith }
1269b94acceSBarry Smith 
12776b2cf59SMatthew Knepley /*
12876b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
12976b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
13076b2cf59SMatthew Knepley */
13176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
13276b2cf59SMatthew Knepley static int numberofsetfromoptions;
13376b2cf59SMatthew Knepley static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
13476b2cf59SMatthew Knepley 
135*e74ef692SMatthew Knepley #undef __FUNCT__
136*e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
13776b2cf59SMatthew Knepley /*@
13876b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
13976b2cf59SMatthew Knepley 
14076b2cf59SMatthew Knepley   Not Collective
14176b2cf59SMatthew Knepley 
14276b2cf59SMatthew Knepley   Input Parameter:
14376b2cf59SMatthew Knepley . snescheck - function that checks for options
14476b2cf59SMatthew Knepley 
14576b2cf59SMatthew Knepley   Level: developer
14676b2cf59SMatthew Knepley 
14776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
14876b2cf59SMatthew Knepley @*/
14976b2cf59SMatthew Knepley int SNESAddOptionsChecker(int (*snescheck)(SNES))
15076b2cf59SMatthew Knepley {
15176b2cf59SMatthew Knepley   PetscFunctionBegin;
15276b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
15376b2cf59SMatthew Knepley     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %d allowed", MAXSETFROMOPTIONS);
15476b2cf59SMatthew Knepley   }
15576b2cf59SMatthew Knepley 
15676b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
15776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
15876b2cf59SMatthew Knepley }
15976b2cf59SMatthew Knepley 
1604a2ae208SSatish Balay #undef __FUNCT__
1614a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1629b94acceSBarry Smith /*@
163682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
1649b94acceSBarry Smith 
165c7afd0dbSLois Curfman McInnes    Collective on SNES
166c7afd0dbSLois Curfman McInnes 
1679b94acceSBarry Smith    Input Parameter:
1689b94acceSBarry Smith .  snes - the SNES context
1699b94acceSBarry Smith 
17036851e7fSLois Curfman McInnes    Options Database Keys:
1716831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
17282738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
17382738288SBarry Smith                 of the change in the solution between steps
174b39c3a46SLois Curfman McInnes .  -snes_atol <atol> - absolute tolerance of residual norm
175b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
176b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
177b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
178b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
17982738288SBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear or minimization
18082738288SBarry Smith                                solver; hence iterations will continue until max_it
1811fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
18282738288SBarry Smith                                of convergence test
18382738288SBarry Smith .  -snes_monitor - prints residual norm at each iteration
184d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
185d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
18682738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
187e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
18836851e7fSLois Curfman McInnes -  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
18982738288SBarry Smith 
19082738288SBarry Smith     Options Database for Eisenstat-Walker method:
19182738288SBarry Smith +  -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence
19282738288SBarry Smith .  -snes_ksp_eq_version ver - version of  Eisenstat-Walker method
19336851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
19436851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
19536851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
19636851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
19736851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
19836851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
19982738288SBarry Smith 
20011ca99fdSLois Curfman McInnes    Notes:
20111ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
20211ca99fdSLois Curfman McInnes    the users manual.
20383e2fdc7SBarry Smith 
20436851e7fSLois Curfman McInnes    Level: beginner
20536851e7fSLois Curfman McInnes 
2069b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2079b94acceSBarry Smith 
20869ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
2099b94acceSBarry Smith @*/
2109b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
2119b94acceSBarry Smith {
2129b94acceSBarry Smith   SLES                sles;
213186905e3SBarry Smith   SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
214f1af5d2fSBarry Smith   PetscTruth          flg;
21576b2cf59SMatthew Knepley   int                 ierr, i;
216186905e3SBarry Smith   char                *deft,type[256];
2179b94acceSBarry Smith 
2183a40ed3dSBarry Smith   PetscFunctionBegin;
21977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
220ca161407SBarry Smith 
221b0a32e0cSBarry Smith   ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
222186905e3SBarry Smith     if (snes->type_name) {
223186905e3SBarry Smith       deft = snes->type_name;
224186905e3SBarry Smith     } else {
225186905e3SBarry Smith       if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
226186905e3SBarry Smith         deft = SNESEQLS;
227186905e3SBarry Smith       } else {
228186905e3SBarry Smith         deft = SNESUMTR;
229d64ed03dSBarry Smith       }
230d64ed03dSBarry Smith     }
2314bbc92c1SBarry Smith 
232186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
233b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
234d64ed03dSBarry Smith     if (flg) {
235186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
236186905e3SBarry Smith     } else if (!snes->type_name) {
237186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
238d64ed03dSBarry Smith     }
23993c39befSBarry Smith 
24087828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
24187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->atol,&snes->atol,0);CHKERRQ(ierr);
242186905e3SBarry Smith 
24387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
244b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
245b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
24687828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_fmin","Minimization function tolerance","SNESSetMinimizationFunctionTolerance",snes->fmin,&snes->fmin,0);CHKERRQ(ierr);
247186905e3SBarry Smith 
248b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_ksp_ew_conv","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetParametersEW",&snes->ksp_ewconv);CHKERRQ(ierr);
249186905e3SBarry Smith 
250b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1 or 2","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
25187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
25287828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
25387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
25487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
25587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
25687828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
257186905e3SBarry Smith 
258b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr);
25993c39befSBarry Smith     if (flg) {snes->converged = 0;}
260b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_cancelmonitors","Remove all monitors","SNESClearMonitor",&flg);CHKERRQ(ierr);
2615cd90555SBarry Smith     if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
262b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_monitor","Monitor norm of function","SNESDefaultMonitor",&flg);CHKERRQ(ierr);
263b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);}
2643a7fca6bSBarry Smith     ierr = PetscOptionsName("-snes_ratiomonitor","Monitor norm of function","SNESSetRatioMonitor",&flg);CHKERRQ(ierr);
2653a7fca6bSBarry Smith     if (flg) {ierr = SNESSetRatioMonitor(snes);CHKERRQ(ierr);}
266b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_smonitor","Monitor norm of function (fewer digits)","SNESDefaultSMonitor",&flg);CHKERRQ(ierr);
267b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);}
268b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor","Plot solution at each iteration","SNESVecViewMonitor",&flg);CHKERRQ(ierr);
269b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);}
270b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_update","Plot correction at each iteration","SNESVecViewUpdateMonitor",&flg);CHKERRQ(ierr);
2717c922b88SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewUpdateMonitor,0,0);CHKERRQ(ierr);}
272b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_xmonitor","Plot function norm at each iteration","SNESLGMonitor",&flg);CHKERRQ(ierr);
273186905e3SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
274e24b481bSBarry Smith 
275b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
2763c7409f5SSatish Balay     if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
277186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
278b0a32e0cSBarry Smith       PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
279981c4779SBarry Smith     } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
280186905e3SBarry Smith       ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr);
281b0a32e0cSBarry Smith       PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
2829b94acceSBarry Smith     }
283639f9d9dSBarry Smith 
28476b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
28576b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);                                                             CHKERRQ(ierr);
28676b2cf59SMatthew Knepley     }
28776b2cf59SMatthew Knepley 
288186905e3SBarry Smith     if (snes->setfromoptions) {
289186905e3SBarry Smith       ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
290639f9d9dSBarry Smith     }
291639f9d9dSBarry Smith 
292b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2934bbc92c1SBarry Smith 
2949b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
2959b94acceSBarry Smith   ierr = SLESSetFromOptions(sles);CHKERRQ(ierr);
29693993e2dSLois Curfman McInnes 
2973a40ed3dSBarry Smith   PetscFunctionReturn(0);
2989b94acceSBarry Smith }
2999b94acceSBarry Smith 
300a847f771SSatish Balay 
3014a2ae208SSatish Balay #undef __FUNCT__
3024a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
3039b94acceSBarry Smith /*@
3049b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3059b94acceSBarry Smith    the nonlinear solvers.
3069b94acceSBarry Smith 
307fee21e36SBarry Smith    Collective on SNES
308fee21e36SBarry Smith 
309c7afd0dbSLois Curfman McInnes    Input Parameters:
310c7afd0dbSLois Curfman McInnes +  snes - the SNES context
311c7afd0dbSLois Curfman McInnes -  usrP - optional user context
312c7afd0dbSLois Curfman McInnes 
31336851e7fSLois Curfman McInnes    Level: intermediate
31436851e7fSLois Curfman McInnes 
3159b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3169b94acceSBarry Smith 
3179b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3189b94acceSBarry Smith @*/
3199b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
3209b94acceSBarry Smith {
3213a40ed3dSBarry Smith   PetscFunctionBegin;
32277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3239b94acceSBarry Smith   snes->user		= usrP;
3243a40ed3dSBarry Smith   PetscFunctionReturn(0);
3259b94acceSBarry Smith }
32674679c65SBarry Smith 
3274a2ae208SSatish Balay #undef __FUNCT__
3284a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3299b94acceSBarry Smith /*@C
3309b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3319b94acceSBarry Smith    nonlinear solvers.
3329b94acceSBarry Smith 
333c7afd0dbSLois Curfman McInnes    Not Collective
334c7afd0dbSLois Curfman McInnes 
3359b94acceSBarry Smith    Input Parameter:
3369b94acceSBarry Smith .  snes - SNES context
3379b94acceSBarry Smith 
3389b94acceSBarry Smith    Output Parameter:
3399b94acceSBarry Smith .  usrP - user context
3409b94acceSBarry Smith 
34136851e7fSLois Curfman McInnes    Level: intermediate
34236851e7fSLois Curfman McInnes 
3439b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3449b94acceSBarry Smith 
3459b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3469b94acceSBarry Smith @*/
3479b94acceSBarry Smith int SNESGetApplicationContext(SNES snes,void **usrP)
3489b94acceSBarry Smith {
3493a40ed3dSBarry Smith   PetscFunctionBegin;
35077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3519b94acceSBarry Smith   *usrP = snes->user;
3523a40ed3dSBarry Smith   PetscFunctionReturn(0);
3539b94acceSBarry Smith }
35474679c65SBarry Smith 
3554a2ae208SSatish Balay #undef __FUNCT__
3564a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3579b94acceSBarry Smith /*@
358c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
359c8228a4eSBarry Smith    at this time.
3609b94acceSBarry Smith 
361c7afd0dbSLois Curfman McInnes    Not Collective
362c7afd0dbSLois Curfman McInnes 
3639b94acceSBarry Smith    Input Parameter:
3649b94acceSBarry Smith .  snes - SNES context
3659b94acceSBarry Smith 
3669b94acceSBarry Smith    Output Parameter:
3679b94acceSBarry Smith .  iter - iteration number
3689b94acceSBarry Smith 
369c8228a4eSBarry Smith    Notes:
370c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
371c8228a4eSBarry Smith 
372c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
37308405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
37408405cd6SLois Curfman McInnes .vb
37508405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
37608405cd6SLois Curfman McInnes       if (!(it % 2)) {
37708405cd6SLois Curfman McInnes         [compute Jacobian here]
37808405cd6SLois Curfman McInnes       }
37908405cd6SLois Curfman McInnes .ve
380c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
38108405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
382c8228a4eSBarry Smith 
38336851e7fSLois Curfman McInnes    Level: intermediate
38436851e7fSLois Curfman McInnes 
3859b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3869b94acceSBarry Smith @*/
3879b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3889b94acceSBarry Smith {
3893a40ed3dSBarry Smith   PetscFunctionBegin;
39077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
39174679c65SBarry Smith   PetscValidIntPointer(iter);
3929b94acceSBarry Smith   *iter = snes->iter;
3933a40ed3dSBarry Smith   PetscFunctionReturn(0);
3949b94acceSBarry Smith }
39574679c65SBarry Smith 
3964a2ae208SSatish Balay #undef __FUNCT__
3974a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
3989b94acceSBarry Smith /*@
3999b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4009b94acceSBarry Smith    with SNESSSetFunction().
4019b94acceSBarry Smith 
402c7afd0dbSLois Curfman McInnes    Collective on SNES
403c7afd0dbSLois Curfman McInnes 
4049b94acceSBarry Smith    Input Parameter:
4059b94acceSBarry Smith .  snes - SNES context
4069b94acceSBarry Smith 
4079b94acceSBarry Smith    Output Parameter:
4089b94acceSBarry Smith .  fnorm - 2-norm of function
4099b94acceSBarry Smith 
4109b94acceSBarry Smith    Note:
4119b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
412a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
413a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
4149b94acceSBarry Smith 
41536851e7fSLois Curfman McInnes    Level: intermediate
41636851e7fSLois Curfman McInnes 
4179b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
418a86d99e1SLois Curfman McInnes 
41908405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
4209b94acceSBarry Smith @*/
42187828ca2SBarry Smith int SNESGetFunctionNorm(SNES snes,PetscScalar *fnorm)
4229b94acceSBarry Smith {
4233a40ed3dSBarry Smith   PetscFunctionBegin;
42477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
42574679c65SBarry Smith   PetscValidScalarPointer(fnorm);
42674679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
42729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"For SNES_NONLINEAR_EQUATIONS only");
42874679c65SBarry Smith   }
4299b94acceSBarry Smith   *fnorm = snes->norm;
4303a40ed3dSBarry Smith   PetscFunctionReturn(0);
4319b94acceSBarry Smith }
43274679c65SBarry Smith 
4334a2ae208SSatish Balay #undef __FUNCT__
4344a2ae208SSatish Balay #define __FUNCT__ "SNESGetGradientNorm"
4359b94acceSBarry Smith /*@
4369b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
4379b94acceSBarry Smith    with SNESSSetGradient().
4389b94acceSBarry Smith 
439c7afd0dbSLois Curfman McInnes    Collective on SNES
440c7afd0dbSLois Curfman McInnes 
4419b94acceSBarry Smith    Input Parameter:
4429b94acceSBarry Smith .  snes - SNES context
4439b94acceSBarry Smith 
4449b94acceSBarry Smith    Output Parameter:
4459b94acceSBarry Smith .  fnorm - 2-norm of gradient
4469b94acceSBarry Smith 
4479b94acceSBarry Smith    Note:
4489b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
449a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
450a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
4519b94acceSBarry Smith 
45236851e7fSLois Curfman McInnes    Level: intermediate
45336851e7fSLois Curfman McInnes 
4549b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
455a86d99e1SLois Curfman McInnes 
456a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
4579b94acceSBarry Smith @*/
45887828ca2SBarry Smith int SNESGetGradientNorm(SNES snes,PetscScalar *gnorm)
4599b94acceSBarry Smith {
4603a40ed3dSBarry Smith   PetscFunctionBegin;
46177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
46274679c65SBarry Smith   PetscValidScalarPointer(gnorm);
46374679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
46429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"For SNES_UNCONSTRAINED_MINIMIZATION only");
46574679c65SBarry Smith   }
4669b94acceSBarry Smith   *gnorm = snes->norm;
4673a40ed3dSBarry Smith   PetscFunctionReturn(0);
4689b94acceSBarry Smith }
46974679c65SBarry Smith 
4704a2ae208SSatish Balay #undef __FUNCT__
4714a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps"
4729b94acceSBarry Smith /*@
4739b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
4749b94acceSBarry Smith    attempted by the nonlinear solver.
4759b94acceSBarry Smith 
476c7afd0dbSLois Curfman McInnes    Not Collective
477c7afd0dbSLois Curfman McInnes 
4789b94acceSBarry Smith    Input Parameter:
4799b94acceSBarry Smith .  snes - SNES context
4809b94acceSBarry Smith 
4819b94acceSBarry Smith    Output Parameter:
4829b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4839b94acceSBarry Smith 
484c96a6f78SLois Curfman McInnes    Notes:
485c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
486c96a6f78SLois Curfman McInnes 
48736851e7fSLois Curfman McInnes    Level: intermediate
48836851e7fSLois Curfman McInnes 
4899b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4909b94acceSBarry Smith @*/
4919b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
4929b94acceSBarry Smith {
4933a40ed3dSBarry Smith   PetscFunctionBegin;
49477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
49574679c65SBarry Smith   PetscValidIntPointer(nfails);
4969b94acceSBarry Smith   *nfails = snes->nfailures;
4973a40ed3dSBarry Smith   PetscFunctionReturn(0);
4989b94acceSBarry Smith }
499a847f771SSatish Balay 
5004a2ae208SSatish Balay #undef __FUNCT__
5014a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations"
502c96a6f78SLois Curfman McInnes /*@
503c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
504c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
505c96a6f78SLois Curfman McInnes 
506c7afd0dbSLois Curfman McInnes    Not Collective
507c7afd0dbSLois Curfman McInnes 
508c96a6f78SLois Curfman McInnes    Input Parameter:
509c96a6f78SLois Curfman McInnes .  snes - SNES context
510c96a6f78SLois Curfman McInnes 
511c96a6f78SLois Curfman McInnes    Output Parameter:
512c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
513c96a6f78SLois Curfman McInnes 
514c96a6f78SLois Curfman McInnes    Notes:
515c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
516c96a6f78SLois Curfman McInnes 
51736851e7fSLois Curfman McInnes    Level: intermediate
51836851e7fSLois Curfman McInnes 
519c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
520c96a6f78SLois Curfman McInnes @*/
52186bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
522c96a6f78SLois Curfman McInnes {
5233a40ed3dSBarry Smith   PetscFunctionBegin;
524c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
525c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
526c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5273a40ed3dSBarry Smith   PetscFunctionReturn(0);
528c96a6f78SLois Curfman McInnes }
529c96a6f78SLois Curfman McInnes 
5304a2ae208SSatish Balay #undef __FUNCT__
5314a2ae208SSatish Balay #define __FUNCT__ "SNESGetSLES"
5329b94acceSBarry Smith /*@C
5339b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
5349b94acceSBarry Smith 
535c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
536c7afd0dbSLois Curfman McInnes 
5379b94acceSBarry Smith    Input Parameter:
5389b94acceSBarry Smith .  snes - the SNES context
5399b94acceSBarry Smith 
5409b94acceSBarry Smith    Output Parameter:
5419b94acceSBarry Smith .  sles - the SLES context
5429b94acceSBarry Smith 
5439b94acceSBarry Smith    Notes:
5449b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
5459b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
5469b94acceSBarry Smith    KSP and PC contexts as well.
5479b94acceSBarry Smith 
54836851e7fSLois Curfman McInnes    Level: beginner
54936851e7fSLois Curfman McInnes 
5509b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
5519b94acceSBarry Smith 
5529b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
5539b94acceSBarry Smith @*/
5549b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
5559b94acceSBarry Smith {
5563a40ed3dSBarry Smith   PetscFunctionBegin;
55777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5589b94acceSBarry Smith   *sles = snes->sles;
5593a40ed3dSBarry Smith   PetscFunctionReturn(0);
5609b94acceSBarry Smith }
56182bf6240SBarry Smith 
5624a2ae208SSatish Balay #undef __FUNCT__
5634a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
564454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj)
565e24b481bSBarry Smith {
566aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
567454a90a3SBarry Smith   SNES          v = (SNES) obj;
568e24b481bSBarry Smith   int          ierr;
56943d6d2cbSBarry Smith #endif
570e24b481bSBarry Smith 
571e24b481bSBarry Smith   PetscFunctionBegin;
572e24b481bSBarry Smith 
57343d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
574e24b481bSBarry Smith   /* if it is already published then return */
575e24b481bSBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
576e24b481bSBarry Smith 
577454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
578e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
579e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
580e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
581e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
582454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
583433994e6SBarry Smith #endif
584e24b481bSBarry Smith   PetscFunctionReturn(0);
585e24b481bSBarry Smith }
586e24b481bSBarry Smith 
5879b94acceSBarry Smith /* -----------------------------------------------------------*/
5884a2ae208SSatish Balay #undef __FUNCT__
5894a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
5909b94acceSBarry Smith /*@C
5919b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
5929b94acceSBarry Smith 
593c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
594c7afd0dbSLois Curfman McInnes 
595c7afd0dbSLois Curfman McInnes    Input Parameters:
596c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
597c7afd0dbSLois Curfman McInnes -  type - type of method, either
598c7afd0dbSLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
599c7afd0dbSLois Curfman McInnes    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
6009b94acceSBarry Smith 
6019b94acceSBarry Smith    Output Parameter:
6029b94acceSBarry Smith .  outsnes - the new SNES context
6039b94acceSBarry Smith 
604c7afd0dbSLois Curfman McInnes    Options Database Keys:
605c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
606c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
607c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
608c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
609c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
610c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
611c1f60f51SBarry Smith 
61236851e7fSLois Curfman McInnes    Level: beginner
61336851e7fSLois Curfman McInnes 
6149b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
6159b94acceSBarry Smith 
616435da068SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNESProblemType, SNES
6179b94acceSBarry Smith @*/
6184b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
6199b94acceSBarry Smith {
6209b94acceSBarry Smith   int                 ierr;
6219b94acceSBarry Smith   SNES                snes;
6229b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
62337fcc0dbSBarry Smith 
6243a40ed3dSBarry Smith   PetscFunctionBegin;
6259b94acceSBarry Smith   *outsnes = 0;
626d64ed03dSBarry Smith   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){
62729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"incorrect method type");
628d64ed03dSBarry Smith   }
6293f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
630b0a32e0cSBarry Smith   PetscLogObjectCreate(snes);
631e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6329b94acceSBarry Smith   snes->max_its           = 50;
6339750a799SBarry Smith   snes->max_funcs	  = 10000;
6349b94acceSBarry Smith   snes->norm		  = 0.0;
6355a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
636b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
637b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
6389b94acceSBarry Smith     snes->atol		  = 1.e-10;
639d64ed03dSBarry Smith   } else {
640b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
641b4874afaSBarry Smith     snes->ttol            = 0.0;
642b4874afaSBarry Smith     snes->atol		  = 1.e-50;
643b4874afaSBarry Smith   }
6449b94acceSBarry Smith   snes->xtol		  = 1.e-8;
6459b94acceSBarry Smith   snes->nfuncs            = 0;
6469b94acceSBarry Smith   snes->nfailures         = 0;
6477a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
648639f9d9dSBarry Smith   snes->numbermonitors    = 0;
6499b94acceSBarry Smith   snes->data              = 0;
6509b94acceSBarry Smith   snes->view              = 0;
6519b94acceSBarry Smith   snes->computeumfunction = 0;
6529b94acceSBarry Smith   snes->umfunP            = 0;
6539b94acceSBarry Smith   snes->fc                = 0;
6549b94acceSBarry Smith   snes->deltatol          = 1.e-12;
6559b94acceSBarry Smith   snes->fmin              = -1.e30;
6569b94acceSBarry Smith   snes->method_class      = type;
6579b94acceSBarry Smith   snes->set_method_called = 0;
65882bf6240SBarry Smith   snes->setupcalled       = 0;
659186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
6606f24a144SLois Curfman McInnes   snes->vwork             = 0;
6616f24a144SLois Curfman McInnes   snes->nwork             = 0;
662758f92a0SBarry Smith   snes->conv_hist_len     = 0;
663758f92a0SBarry Smith   snes->conv_hist_max     = 0;
664758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
665758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
666758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
667184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
6689b94acceSBarry Smith 
6699b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
670b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
671b0a32e0cSBarry Smith   PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
6729b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
6739b94acceSBarry Smith   kctx->version     = 2;
6749b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
6759b94acceSBarry Smith                              this was too large for some test cases */
6769b94acceSBarry Smith   kctx->rtol_last   = 0;
6779b94acceSBarry Smith   kctx->rtol_max    = .9;
6789b94acceSBarry Smith   kctx->gamma       = 1.0;
6799b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
6809b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
6819b94acceSBarry Smith   kctx->threshold   = .1;
6829b94acceSBarry Smith   kctx->lresid_last = 0;
6839b94acceSBarry Smith   kctx->norm_last   = 0;
6849b94acceSBarry Smith 
6859b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr);
686b0a32e0cSBarry Smith   PetscLogObjectParent(snes,snes->sles)
6875334005bSBarry Smith 
6889b94acceSBarry Smith   *outsnes = snes;
68900036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
6903a40ed3dSBarry Smith   PetscFunctionReturn(0);
6919b94acceSBarry Smith }
6929b94acceSBarry Smith 
6939b94acceSBarry Smith /* --------------------------------------------------------------- */
6944a2ae208SSatish Balay #undef __FUNCT__
6954a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
6969b94acceSBarry Smith /*@C
6979b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
6989b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
6999b94acceSBarry Smith    equations.
7009b94acceSBarry Smith 
701fee21e36SBarry Smith    Collective on SNES
702fee21e36SBarry Smith 
703c7afd0dbSLois Curfman McInnes    Input Parameters:
704c7afd0dbSLois Curfman McInnes +  snes - the SNES context
705c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
706c7afd0dbSLois Curfman McInnes .  r - vector to store function value
707c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
708c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7099b94acceSBarry Smith 
710c7afd0dbSLois Curfman McInnes    Calling sequence of func:
7118d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
712c7afd0dbSLois Curfman McInnes 
713313e4042SLois Curfman McInnes .  f - function vector
714c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
7159b94acceSBarry Smith 
7169b94acceSBarry Smith    Notes:
7179b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
7189b94acceSBarry Smith $      f'(x) x = -f(x),
719c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
7209b94acceSBarry Smith 
7219b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7229b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7239b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
7249b94acceSBarry Smith 
72536851e7fSLois Curfman McInnes    Level: beginner
72636851e7fSLois Curfman McInnes 
7279b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
7289b94acceSBarry Smith 
729a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
7309b94acceSBarry Smith @*/
7315334005bSBarry Smith int SNESSetFunction(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
7329b94acceSBarry Smith {
7333a40ed3dSBarry Smith   PetscFunctionBegin;
73477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
735184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
736184914b5SBarry Smith   PetscCheckSameComm(snes,r);
737a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
73829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
739a8c6a408SBarry Smith   }
740184914b5SBarry Smith 
7419b94acceSBarry Smith   snes->computefunction     = func;
7429b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7439b94acceSBarry Smith   snes->funP                = ctx;
7443a40ed3dSBarry Smith   PetscFunctionReturn(0);
7459b94acceSBarry Smith }
7469b94acceSBarry Smith 
7474a2ae208SSatish Balay #undef __FUNCT__
7484a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
7499b94acceSBarry Smith /*@
75036851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
7519b94acceSBarry Smith                          SNESSetFunction().
7529b94acceSBarry Smith 
753c7afd0dbSLois Curfman McInnes    Collective on SNES
754c7afd0dbSLois Curfman McInnes 
7559b94acceSBarry Smith    Input Parameters:
756c7afd0dbSLois Curfman McInnes +  snes - the SNES context
757c7afd0dbSLois Curfman McInnes -  x - input vector
7589b94acceSBarry Smith 
7599b94acceSBarry Smith    Output Parameter:
7603638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
7619b94acceSBarry Smith 
7621bffabb2SLois Curfman McInnes    Notes:
7639b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
7649b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
7659b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
7669b94acceSBarry Smith 
76736851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
76836851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
76936851e7fSLois Curfman McInnes    themselves.
77036851e7fSLois Curfman McInnes 
77136851e7fSLois Curfman McInnes    Level: developer
77236851e7fSLois Curfman McInnes 
7739b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
7749b94acceSBarry Smith 
775a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
7769b94acceSBarry Smith @*/
7779b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x,Vec y)
7789b94acceSBarry Smith {
7799b94acceSBarry Smith   int    ierr;
7809b94acceSBarry Smith 
7813a40ed3dSBarry Smith   PetscFunctionBegin;
782184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
783184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
784184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
785184914b5SBarry Smith   PetscCheckSameComm(snes,x);
786184914b5SBarry Smith   PetscCheckSameComm(snes,y);
787d4bb536fSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
78829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
789d4bb536fSBarry Smith   }
790184914b5SBarry Smith 
791b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
792d64ed03dSBarry Smith   PetscStackPush("SNES user function");
7939b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
794d64ed03dSBarry Smith   PetscStackPop;
795ae3c334cSLois Curfman McInnes   snes->nfuncs++;
796b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
7973a40ed3dSBarry Smith   PetscFunctionReturn(0);
7989b94acceSBarry Smith }
7999b94acceSBarry Smith 
8004a2ae208SSatish Balay #undef __FUNCT__
8014a2ae208SSatish Balay #define __FUNCT__ "SNESSetMinimizationFunction"
8029b94acceSBarry Smith /*@C
8039b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
8049b94acceSBarry Smith    unconstrained minimization.
8059b94acceSBarry Smith 
806fee21e36SBarry Smith    Collective on SNES
807fee21e36SBarry Smith 
808c7afd0dbSLois Curfman McInnes    Input Parameters:
809c7afd0dbSLois Curfman McInnes +  snes - the SNES context
810c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
811c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
812c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8139b94acceSBarry Smith 
814c7afd0dbSLois Curfman McInnes    Calling sequence of func:
815329f5518SBarry Smith $     func (SNES snes,Vec x,PetscReal *f,void *ctx);
816c7afd0dbSLois Curfman McInnes 
817c7afd0dbSLois Curfman McInnes +  x - input vector
8189b94acceSBarry Smith .  f - function
819c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined function context
8209b94acceSBarry Smith 
82136851e7fSLois Curfman McInnes    Level: beginner
82236851e7fSLois Curfman McInnes 
8239b94acceSBarry Smith    Notes:
8249b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
8259b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
8269b94acceSBarry Smith    SNESSetFunction().
8279b94acceSBarry Smith 
8289b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
8299b94acceSBarry Smith 
830a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
831a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
8329b94acceSBarry Smith @*/
833329f5518SBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,PetscReal*,void*),void *ctx)
8349b94acceSBarry Smith {
8353a40ed3dSBarry Smith   PetscFunctionBegin;
83677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
837a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
83829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
839a8c6a408SBarry Smith   }
8409b94acceSBarry Smith   snes->computeumfunction   = func;
8419b94acceSBarry Smith   snes->umfunP              = ctx;
8423a40ed3dSBarry Smith   PetscFunctionReturn(0);
8439b94acceSBarry Smith }
8449b94acceSBarry Smith 
8454a2ae208SSatish Balay #undef __FUNCT__
8464a2ae208SSatish Balay #define __FUNCT__ "SNESComputeMinimizationFunction"
8479b94acceSBarry Smith /*@
8489b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
8499b94acceSBarry Smith    set with SNESSetMinimizationFunction().
8509b94acceSBarry Smith 
851c7afd0dbSLois Curfman McInnes    Collective on SNES
852c7afd0dbSLois Curfman McInnes 
8539b94acceSBarry Smith    Input Parameters:
854c7afd0dbSLois Curfman McInnes +  snes - the SNES context
855c7afd0dbSLois Curfman McInnes -  x - input vector
8569b94acceSBarry Smith 
8579b94acceSBarry Smith    Output Parameter:
8589b94acceSBarry Smith .  y - function value
8599b94acceSBarry Smith 
8609b94acceSBarry Smith    Notes:
8619b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
8629b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
8639b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
864a86d99e1SLois Curfman McInnes 
86536851e7fSLois Curfman McInnes    SNESComputeMinimizationFunction() is typically used within minimization
86636851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
86736851e7fSLois Curfman McInnes    themselves.
86836851e7fSLois Curfman McInnes 
86936851e7fSLois Curfman McInnes    Level: developer
87036851e7fSLois Curfman McInnes 
871a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
872a86d99e1SLois Curfman McInnes 
873a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
874a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
8759b94acceSBarry Smith @*/
876329f5518SBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,PetscReal *y)
8779b94acceSBarry Smith {
8789b94acceSBarry Smith   int    ierr;
8793a40ed3dSBarry Smith 
8803a40ed3dSBarry Smith   PetscFunctionBegin;
881184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
882184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
883184914b5SBarry Smith   PetscCheckSameComm(snes,x);
884a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
88529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
886a8c6a408SBarry Smith   }
887184914b5SBarry Smith 
888b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);CHKERRQ(ierr);
889d64ed03dSBarry Smith   PetscStackPush("SNES user minimzation function");
8909b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr);
891d64ed03dSBarry Smith   PetscStackPop;
892ae3c334cSLois Curfman McInnes   snes->nfuncs++;
893b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);CHKERRQ(ierr);
8943a40ed3dSBarry Smith   PetscFunctionReturn(0);
8959b94acceSBarry Smith }
8969b94acceSBarry Smith 
8974a2ae208SSatish Balay #undef __FUNCT__
8984a2ae208SSatish Balay #define __FUNCT__ "SNESSetGradient"
8999b94acceSBarry Smith /*@C
9009b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
9019b94acceSBarry Smith    vector for use by the SNES routines.
9029b94acceSBarry Smith 
903c7afd0dbSLois Curfman McInnes    Collective on SNES
904c7afd0dbSLois Curfman McInnes 
9059b94acceSBarry Smith    Input Parameters:
906c7afd0dbSLois Curfman McInnes +  snes - the SNES context
9079b94acceSBarry Smith .  func - function evaluation routine
908044dda88SLois Curfman McInnes .  ctx - optional user-defined context for private data for the
909044dda88SLois Curfman McInnes          gradient evaluation routine (may be PETSC_NULL)
910c7afd0dbSLois Curfman McInnes -  r - vector to store gradient value
911fee21e36SBarry Smith 
9129b94acceSBarry Smith    Calling sequence of func:
9138d76a1e5SLois Curfman McInnes $     func (SNES, Vec x, Vec g, void *ctx);
9149b94acceSBarry Smith 
915c7afd0dbSLois Curfman McInnes +  x - input vector
9169b94acceSBarry Smith .  g - gradient vector
917c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined gradient context
9189b94acceSBarry Smith 
9199b94acceSBarry Smith    Notes:
9209b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
9219b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
9229b94acceSBarry Smith    SNESSetFunction().
9239b94acceSBarry Smith 
92436851e7fSLois Curfman McInnes    Level: beginner
92536851e7fSLois Curfman McInnes 
9269b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9279b94acceSBarry Smith 
928a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
929a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
9309b94acceSBarry Smith @*/
93174679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
9329b94acceSBarry Smith {
9333a40ed3dSBarry Smith   PetscFunctionBegin;
93477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
935184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
936184914b5SBarry Smith   PetscCheckSameComm(snes,r);
937a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
93829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
939a8c6a408SBarry Smith   }
9409b94acceSBarry Smith   snes->computefunction     = func;
9419b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
9429b94acceSBarry Smith   snes->funP                = ctx;
9433a40ed3dSBarry Smith   PetscFunctionReturn(0);
9449b94acceSBarry Smith }
9459b94acceSBarry Smith 
9464a2ae208SSatish Balay #undef __FUNCT__
9474a2ae208SSatish Balay #define __FUNCT__ "SNESComputeGradient"
9489b94acceSBarry Smith /*@
949a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
950a86d99e1SLois Curfman McInnes    SNESSetGradient().
9519b94acceSBarry Smith 
952c7afd0dbSLois Curfman McInnes    Collective on SNES
953c7afd0dbSLois Curfman McInnes 
9549b94acceSBarry Smith    Input Parameters:
955c7afd0dbSLois Curfman McInnes +  snes - the SNES context
956c7afd0dbSLois Curfman McInnes -  x - input vector
9579b94acceSBarry Smith 
9589b94acceSBarry Smith    Output Parameter:
9599b94acceSBarry Smith .  y - gradient vector
9609b94acceSBarry Smith 
9619b94acceSBarry Smith    Notes:
9629b94acceSBarry Smith    SNESComputeGradient() is valid only for
9639b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
9649b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
965a86d99e1SLois Curfman McInnes 
96636851e7fSLois Curfman McInnes    SNESComputeGradient() is typically used within minimization
96736851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
96836851e7fSLois Curfman McInnes    themselves.
96936851e7fSLois Curfman McInnes 
97036851e7fSLois Curfman McInnes    Level: developer
97136851e7fSLois Curfman McInnes 
972a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
973a86d99e1SLois Curfman McInnes 
974a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
975a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
9769b94acceSBarry Smith @*/
9779b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x,Vec y)
9789b94acceSBarry Smith {
9799b94acceSBarry Smith   int    ierr;
9803a40ed3dSBarry Smith 
9813a40ed3dSBarry Smith   PetscFunctionBegin;
982184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
983184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
984184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
985184914b5SBarry Smith   PetscCheckSameComm(snes,x);
986184914b5SBarry Smith   PetscCheckSameComm(snes,y);
9873a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
98829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
9893a40ed3dSBarry Smith   }
990b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(SNES_GradientEval,snes,x,y,0);CHKERRQ(ierr);
991d64ed03dSBarry Smith   PetscStackPush("SNES user gradient function");
9929b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
993d64ed03dSBarry Smith   PetscStackPop;
994b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(SNES_GradientEval,snes,x,y,0);CHKERRQ(ierr);
9953a40ed3dSBarry Smith   PetscFunctionReturn(0);
9969b94acceSBarry Smith }
9979b94acceSBarry Smith 
9984a2ae208SSatish Balay #undef __FUNCT__
9994a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
100062fef451SLois Curfman McInnes /*@
100162fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
100262fef451SLois Curfman McInnes    set with SNESSetJacobian().
100362fef451SLois Curfman McInnes 
1004c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1005c7afd0dbSLois Curfman McInnes 
100662fef451SLois Curfman McInnes    Input Parameters:
1007c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1008c7afd0dbSLois Curfman McInnes -  x - input vector
100962fef451SLois Curfman McInnes 
101062fef451SLois Curfman McInnes    Output Parameters:
1011c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
101262fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1013c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1014fee21e36SBarry Smith 
101562fef451SLois Curfman McInnes    Notes:
101662fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
101762fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
101862fef451SLois Curfman McInnes 
1019dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1020dc5a77f8SLois Curfman McInnes    flag parameter.
102162fef451SLois Curfman McInnes 
102262fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
102362fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
1024005c665bSBarry Smith    methods is SNESComputeHessian().
102562fef451SLois Curfman McInnes 
102636851e7fSLois Curfman McInnes    Level: developer
102736851e7fSLois Curfman McInnes 
102862fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
102962fef451SLois Curfman McInnes 
103062fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
103162fef451SLois Curfman McInnes @*/
10329b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10339b94acceSBarry Smith {
10349b94acceSBarry Smith   int    ierr;
10353a40ed3dSBarry Smith 
10363a40ed3dSBarry Smith   PetscFunctionBegin;
1037184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1038184914b5SBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE);
1039184914b5SBarry Smith   PetscCheckSameComm(snes,X);
10403a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
104129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
10423a40ed3dSBarry Smith   }
10433a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
1044b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1045c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1046d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
10479b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1048d64ed03dSBarry Smith   PetscStackPop;
1049b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
10506d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
105177c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
105277c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
10533a40ed3dSBarry Smith   PetscFunctionReturn(0);
10549b94acceSBarry Smith }
10559b94acceSBarry Smith 
10564a2ae208SSatish Balay #undef __FUNCT__
10574a2ae208SSatish Balay #define __FUNCT__ "SNESComputeHessian"
105862fef451SLois Curfman McInnes /*@
105962fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
106062fef451SLois Curfman McInnes    set with SNESSetHessian().
106162fef451SLois Curfman McInnes 
1062c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1063c7afd0dbSLois Curfman McInnes 
106462fef451SLois Curfman McInnes    Input Parameters:
1065c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1066c7afd0dbSLois Curfman McInnes -  x - input vector
106762fef451SLois Curfman McInnes 
106862fef451SLois Curfman McInnes    Output Parameters:
1069c7afd0dbSLois Curfman McInnes +  A - Hessian matrix
107062fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1071c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1072fee21e36SBarry Smith 
107362fef451SLois Curfman McInnes    Notes:
107462fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
107562fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
107662fef451SLois Curfman McInnes 
1077dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1078dc5a77f8SLois Curfman McInnes    flag parameter.
107962fef451SLois Curfman McInnes 
108062fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
108162fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
108262fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
108362fef451SLois Curfman McInnes 
108436851e7fSLois Curfman McInnes    SNESComputeHessian() is typically used within minimization
108536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
108636851e7fSLois Curfman McInnes    themselves.
108736851e7fSLois Curfman McInnes 
108836851e7fSLois Curfman McInnes    Level: developer
108936851e7fSLois Curfman McInnes 
109062fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
109162fef451SLois Curfman McInnes 
1092a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
1093a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
109462fef451SLois Curfman McInnes @*/
109562fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
10969b94acceSBarry Smith {
10979b94acceSBarry Smith   int    ierr;
10983a40ed3dSBarry Smith 
10993a40ed3dSBarry Smith   PetscFunctionBegin;
1100184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1101184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1102184914b5SBarry Smith   PetscCheckSameComm(snes,x);
11033a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
110429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
11053a40ed3dSBarry Smith   }
11063a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
1107b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(SNES_HessianEval,snes,x,*A,*B);CHKERRQ(ierr);
11080f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
1109d64ed03dSBarry Smith   PetscStackPush("SNES user Hessian function");
111062fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr);
1111d64ed03dSBarry Smith   PetscStackPop;
1112b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(SNES_HessianEval,snes,x,*A,*B);CHKERRQ(ierr);
11130f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
111477c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
111577c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
11163a40ed3dSBarry Smith   PetscFunctionReturn(0);
11179b94acceSBarry Smith }
11189b94acceSBarry Smith 
11194a2ae208SSatish Balay #undef __FUNCT__
11204a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
11219b94acceSBarry Smith /*@C
11229b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1123044dda88SLois Curfman McInnes    location to store the matrix.
11249b94acceSBarry Smith 
1125c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1126c7afd0dbSLois Curfman McInnes 
11279b94acceSBarry Smith    Input Parameters:
1128c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11299b94acceSBarry Smith .  A - Jacobian matrix
11309b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11319b94acceSBarry Smith .  func - Jacobian evaluation routine
1132c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11332cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11349b94acceSBarry Smith 
11359b94acceSBarry Smith    Calling sequence of func:
11368d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11379b94acceSBarry Smith 
1138c7afd0dbSLois Curfman McInnes +  x - input vector
11399b94acceSBarry Smith .  A - Jacobian matrix
11409b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1141ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1142ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1143c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11449b94acceSBarry Smith 
11459b94acceSBarry Smith    Notes:
1146dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
11472cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1148ac21db08SLois Curfman McInnes 
1149ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11509b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11519b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11529b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11539b94acceSBarry Smith    throughout the global iterations.
11549b94acceSBarry Smith 
115536851e7fSLois Curfman McInnes    Level: beginner
115636851e7fSLois Curfman McInnes 
11579b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11589b94acceSBarry Smith 
1159ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
11609b94acceSBarry Smith @*/
1161454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
11629b94acceSBarry Smith {
11633a7fca6bSBarry Smith   int ierr;
11643a7fca6bSBarry Smith 
11653a40ed3dSBarry Smith   PetscFunctionBegin;
116677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
116700036973SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE);
116800036973SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE);
116900036973SBarry Smith   if (A) PetscCheckSameComm(snes,A);
117000036973SBarry Smith   if (B) PetscCheckSameComm(snes,B);
1171a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
117229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
1173a8c6a408SBarry Smith   }
1174184914b5SBarry Smith 
11753a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
11763a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
11773a7fca6bSBarry Smith   if (A) {
11783a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
11799b94acceSBarry Smith     snes->jacobian = A;
11803a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
11813a7fca6bSBarry Smith   }
11823a7fca6bSBarry Smith   if (B) {
11833a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
11849b94acceSBarry Smith     snes->jacobian_pre = B;
11853a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
11863a7fca6bSBarry Smith   }
11873a40ed3dSBarry Smith   PetscFunctionReturn(0);
11889b94acceSBarry Smith }
118962fef451SLois Curfman McInnes 
11904a2ae208SSatish Balay #undef __FUNCT__
11914a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1192c2aafc4cSSatish Balay /*@C
1193b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1194b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1195b4fd4287SBarry Smith 
1196c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1197c7afd0dbSLois Curfman McInnes 
1198b4fd4287SBarry Smith    Input Parameter:
1199b4fd4287SBarry Smith .  snes - the nonlinear solver context
1200b4fd4287SBarry Smith 
1201b4fd4287SBarry Smith    Output Parameters:
1202c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1203b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
120400036973SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
120500036973SBarry Smith -  func - location to put Jacobian function (or PETSC_NULL)
1206fee21e36SBarry Smith 
120736851e7fSLois Curfman McInnes    Level: advanced
120836851e7fSLois Curfman McInnes 
1209b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1210b4fd4287SBarry Smith @*/
121100036973SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx,int (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*))
1212b4fd4287SBarry Smith {
12133a40ed3dSBarry Smith   PetscFunctionBegin;
1214184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
12153a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
121629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
12173a40ed3dSBarry Smith   }
1218b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1219b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1220b4fd4287SBarry Smith   if (ctx)  *ctx  = snes->jacP;
122100036973SBarry Smith   if (func) *func = snes->computejacobian;
12223a40ed3dSBarry Smith   PetscFunctionReturn(0);
1223b4fd4287SBarry Smith }
1224b4fd4287SBarry Smith 
12254a2ae208SSatish Balay #undef __FUNCT__
12264a2ae208SSatish Balay #define __FUNCT__ "SNESSetHessian"
12279b94acceSBarry Smith /*@C
12289b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
1229044dda88SLois Curfman McInnes    location to store the matrix.
12309b94acceSBarry Smith 
1231c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1232c7afd0dbSLois Curfman McInnes 
12339b94acceSBarry Smith    Input Parameters:
1234c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12359b94acceSBarry Smith .  A - Hessian matrix
12369b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
12379b94acceSBarry Smith .  func - Jacobian evaluation routine
1238c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1239313e4042SLois Curfman McInnes          Hessian evaluation routine (may be PETSC_NULL)
12409b94acceSBarry Smith 
12419b94acceSBarry Smith    Calling sequence of func:
12428d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12439b94acceSBarry Smith 
1244c7afd0dbSLois Curfman McInnes +  x - input vector
12459b94acceSBarry Smith .  A - Hessian matrix
12469b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1247ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1248ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1249c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Hessian context
12509b94acceSBarry Smith 
12519b94acceSBarry Smith    Notes:
1252dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
12532cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1254ac21db08SLois Curfman McInnes 
12559b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
12569b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
12579b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12589b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12599b94acceSBarry Smith    throughout the global iterations.
12609b94acceSBarry Smith 
126136851e7fSLois Curfman McInnes    Level: beginner
126236851e7fSLois Curfman McInnes 
12639b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
12649b94acceSBarry Smith 
1265ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
12669b94acceSBarry Smith @*/
1267454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
12689b94acceSBarry Smith {
12693a7fca6bSBarry Smith   int ierr;
12703a7fca6bSBarry Smith 
12713a40ed3dSBarry Smith   PetscFunctionBegin;
127277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1273184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
1274184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
1275184914b5SBarry Smith   PetscCheckSameComm(snes,A);
1276184914b5SBarry Smith   PetscCheckSameComm(snes,B);
1277d4bb536fSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
127829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1279d4bb536fSBarry Smith   }
12803a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
12813a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
12823a7fca6bSBarry Smith   if (A) {
12833a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12849b94acceSBarry Smith     snes->jacobian = A;
12853a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
12863a7fca6bSBarry Smith   }
12873a7fca6bSBarry Smith   if (B) {
12883a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12899b94acceSBarry Smith     snes->jacobian_pre = B;
12903a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
12913a7fca6bSBarry Smith   }
12923a40ed3dSBarry Smith   PetscFunctionReturn(0);
12939b94acceSBarry Smith }
12949b94acceSBarry Smith 
12954a2ae208SSatish Balay #undef __FUNCT__
12964a2ae208SSatish Balay #define __FUNCT__ "SNESGetHessian"
129762fef451SLois Curfman McInnes /*@
129862fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
129962fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
130062fef451SLois Curfman McInnes 
1301c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object is parallel if SNES object is parallel
1302c7afd0dbSLois Curfman McInnes 
130362fef451SLois Curfman McInnes    Input Parameter:
130462fef451SLois Curfman McInnes .  snes - the nonlinear solver context
130562fef451SLois Curfman McInnes 
130662fef451SLois Curfman McInnes    Output Parameters:
1307c7afd0dbSLois Curfman McInnes +  A - location to stash Hessian matrix (or PETSC_NULL)
130862fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
1309c7afd0dbSLois Curfman McInnes -  ctx - location to stash Hessian ctx (or PETSC_NULL)
1310fee21e36SBarry Smith 
131136851e7fSLois Curfman McInnes    Level: advanced
131236851e7fSLois Curfman McInnes 
131362fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
1314c7afd0dbSLois Curfman McInnes 
1315c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian
131662fef451SLois Curfman McInnes @*/
131762fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B,void **ctx)
131862fef451SLois Curfman McInnes {
13193a40ed3dSBarry Smith   PetscFunctionBegin;
1320184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
13213a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){
132229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
13233a40ed3dSBarry Smith   }
132462fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
132562fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
132662fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
13273a40ed3dSBarry Smith   PetscFunctionReturn(0);
132862fef451SLois Curfman McInnes }
132962fef451SLois Curfman McInnes 
13309b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13319b94acceSBarry Smith 
13324a2ae208SSatish Balay #undef __FUNCT__
13334a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
13349b94acceSBarry Smith /*@
13359b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1336272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13379b94acceSBarry Smith 
1338fee21e36SBarry Smith    Collective on SNES
1339fee21e36SBarry Smith 
1340c7afd0dbSLois Curfman McInnes    Input Parameters:
1341c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1342c7afd0dbSLois Curfman McInnes -  x - the solution vector
1343c7afd0dbSLois Curfman McInnes 
1344272ac6f2SLois Curfman McInnes    Notes:
1345272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1346272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1347272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1348272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1349272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1350272ac6f2SLois Curfman McInnes 
135136851e7fSLois Curfman McInnes    Level: advanced
135236851e7fSLois Curfman McInnes 
13539b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13549b94acceSBarry Smith 
13559b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
13569b94acceSBarry Smith @*/
13578ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
13589b94acceSBarry Smith {
1359f1af5d2fSBarry Smith   int        ierr;
1360f1af5d2fSBarry Smith   PetscTruth flg;
13613a40ed3dSBarry Smith 
13623a40ed3dSBarry Smith   PetscFunctionBegin;
136377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
136477c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1365184914b5SBarry Smith   PetscCheckSameComm(snes,x);
13668ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
13679b94acceSBarry Smith 
1368b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1369c1f60f51SBarry Smith   /*
1370c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1371dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1372c1f60f51SBarry Smith   */
1373c1f60f51SBarry Smith   if (flg) {
1374c1f60f51SBarry Smith     Mat J;
13755a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
13765a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
13773a7fca6bSBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator routines\n");
13783a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
13793a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1380c1f60f51SBarry Smith   }
1381b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1382c1f60f51SBarry Smith   /*
1383dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1384c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1385c1f60f51SBarry Smith    */
138631615d04SLois Curfman McInnes   if (flg) {
1387272ac6f2SLois Curfman McInnes     Mat  J;
1388f3ef73ceSBarry Smith     SLES sles;
1389f3ef73ceSBarry Smith     PC   pc;
1390f3ef73ceSBarry Smith 
13915a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
13923a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
139393b98531SBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator and preconditioner routines\n");
139483e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
13953a7fca6bSBarry Smith       ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
1396d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
13973a7fca6bSBarry Smith       ierr = SNESSetHessian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
1398d4bb536fSBarry Smith     } else {
139929bbc08cSBarry Smith       SETERRQ(PETSC_ERR_SUP,"Method class doesn't support matrix-free option");
1400d4bb536fSBarry Smith     }
14013a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
14023a7fca6bSBarry Smith 
1403f3ef73ceSBarry Smith     /* force no preconditioner */
1404f3ef73ceSBarry Smith     ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1405f3ef73ceSBarry Smith     ierr = SLESGetPC(sles,&pc);CHKERRQ(ierr);
1406f3ef73ceSBarry Smith     ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1407272ac6f2SLois Curfman McInnes   }
1408f3ef73ceSBarry Smith 
14099b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
14106831982aSBarry Smith     PetscTruth iseqtr;
14116831982aSBarry Smith 
141229bbc08cSBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
141329bbc08cSBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
141429bbc08cSBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1415a8c6a408SBarry Smith     if (snes->vec_func == snes->vec_sol) {
141629bbc08cSBarry Smith       SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1417a8c6a408SBarry Smith     }
1418a703fe33SLois Curfman McInnes 
1419a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
14206831982aSBarry Smith     ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr);
14216831982aSBarry Smith     if (snes->ksp_ewconv && !iseqtr) {
1422a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1423a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1424a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
1425186905e3SBarry Smith       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
1426a703fe33SLois Curfman McInnes     }
14279b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
142829bbc08cSBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetGradient() first");
142929bbc08cSBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetGradient() first");
1430a8c6a408SBarry Smith     if (!snes->computeumfunction) {
143129bbc08cSBarry Smith       SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetMinimizationFunction() first");
1432a8c6a408SBarry Smith     }
143329bbc08cSBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetHessian()");
1434d4bb536fSBarry Smith   } else {
143529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown method class");
1436d4bb536fSBarry Smith   }
1437a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
143882bf6240SBarry Smith   snes->setupcalled = 1;
14393a40ed3dSBarry Smith   PetscFunctionReturn(0);
14409b94acceSBarry Smith }
14419b94acceSBarry Smith 
14424a2ae208SSatish Balay #undef __FUNCT__
14434a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
14449b94acceSBarry Smith /*@C
14459b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
14469b94acceSBarry Smith    with SNESCreate().
14479b94acceSBarry Smith 
1448c7afd0dbSLois Curfman McInnes    Collective on SNES
1449c7afd0dbSLois Curfman McInnes 
14509b94acceSBarry Smith    Input Parameter:
14519b94acceSBarry Smith .  snes - the SNES context
14529b94acceSBarry Smith 
145336851e7fSLois Curfman McInnes    Level: beginner
145436851e7fSLois Curfman McInnes 
14559b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14569b94acceSBarry Smith 
145763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14589b94acceSBarry Smith @*/
14599b94acceSBarry Smith int SNESDestroy(SNES snes)
14609b94acceSBarry Smith {
1461b8a78c4aSBarry Smith   int i,ierr;
14623a40ed3dSBarry Smith 
14633a40ed3dSBarry Smith   PetscFunctionBegin;
146477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
14653a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1466d4bb536fSBarry Smith 
1467be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
14680f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1469be0abb6dSBarry Smith 
1470e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1471606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
14723a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
14733a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
14749b94acceSBarry Smith   ierr = SLESDestroy(snes->sles);CHKERRQ(ierr);
1475522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1476b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1477b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1478b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1479b8a78c4aSBarry Smith     }
1480b8a78c4aSBarry Smith   }
1481b0a32e0cSBarry Smith   PetscLogObjectDestroy((PetscObject)snes);
14820452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
14833a40ed3dSBarry Smith   PetscFunctionReturn(0);
14849b94acceSBarry Smith }
14859b94acceSBarry Smith 
14869b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14879b94acceSBarry Smith 
14884a2ae208SSatish Balay #undef __FUNCT__
14894a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
14909b94acceSBarry Smith /*@
1491d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
14929b94acceSBarry Smith 
1493c7afd0dbSLois Curfman McInnes    Collective on SNES
1494c7afd0dbSLois Curfman McInnes 
14959b94acceSBarry Smith    Input Parameters:
1496c7afd0dbSLois Curfman McInnes +  snes - the SNES context
149733174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
149833174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
149933174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
150033174efeSLois Curfman McInnes            of the change in the solution between steps
150133174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1502c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1503fee21e36SBarry Smith 
150433174efeSLois Curfman McInnes    Options Database Keys:
1505c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1506c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1507c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1508c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1509c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15109b94acceSBarry Smith 
1511d7a720efSLois Curfman McInnes    Notes:
15129b94acceSBarry Smith    The default maximum number of iterations is 50.
15139b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15149b94acceSBarry Smith 
151536851e7fSLois Curfman McInnes    Level: intermediate
151636851e7fSLois Curfman McInnes 
151733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15189b94acceSBarry Smith 
1519d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
15209b94acceSBarry Smith @*/
1521329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf)
15229b94acceSBarry Smith {
15233a40ed3dSBarry Smith   PetscFunctionBegin;
152477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1525d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1526d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1527d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1528d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1529d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15303a40ed3dSBarry Smith   PetscFunctionReturn(0);
15319b94acceSBarry Smith }
15329b94acceSBarry Smith 
15334a2ae208SSatish Balay #undef __FUNCT__
15344a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
15359b94acceSBarry Smith /*@
153633174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
153733174efeSLois Curfman McInnes 
1538c7afd0dbSLois Curfman McInnes    Not Collective
1539c7afd0dbSLois Curfman McInnes 
154033174efeSLois Curfman McInnes    Input Parameters:
1541c7afd0dbSLois Curfman McInnes +  snes - the SNES context
154233174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
154333174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
154433174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
154533174efeSLois Curfman McInnes            of the change in the solution between steps
154633174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1547c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1548fee21e36SBarry Smith 
154933174efeSLois Curfman McInnes    Notes:
155033174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
155133174efeSLois Curfman McInnes 
155236851e7fSLois Curfman McInnes    Level: intermediate
155336851e7fSLois Curfman McInnes 
155433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
155533174efeSLois Curfman McInnes 
155633174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
155733174efeSLois Curfman McInnes @*/
1558329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf)
155933174efeSLois Curfman McInnes {
15603a40ed3dSBarry Smith   PetscFunctionBegin;
156133174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
156233174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
156333174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
156433174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
156533174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
156633174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
15673a40ed3dSBarry Smith   PetscFunctionReturn(0);
156833174efeSLois Curfman McInnes }
156933174efeSLois Curfman McInnes 
15704a2ae208SSatish Balay #undef __FUNCT__
15714a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
157233174efeSLois Curfman McInnes /*@
15739b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
15749b94acceSBarry Smith 
1575fee21e36SBarry Smith    Collective on SNES
1576fee21e36SBarry Smith 
1577c7afd0dbSLois Curfman McInnes    Input Parameters:
1578c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1579c7afd0dbSLois Curfman McInnes -  tol - tolerance
1580c7afd0dbSLois Curfman McInnes 
15819b94acceSBarry Smith    Options Database Key:
1582c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
15839b94acceSBarry Smith 
158436851e7fSLois Curfman McInnes    Level: intermediate
158536851e7fSLois Curfman McInnes 
15869b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
15879b94acceSBarry Smith 
1588d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
15899b94acceSBarry Smith @*/
1590329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
15919b94acceSBarry Smith {
15923a40ed3dSBarry Smith   PetscFunctionBegin;
159377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15949b94acceSBarry Smith   snes->deltatol = tol;
15953a40ed3dSBarry Smith   PetscFunctionReturn(0);
15969b94acceSBarry Smith }
15979b94acceSBarry Smith 
15984a2ae208SSatish Balay #undef __FUNCT__
15994a2ae208SSatish Balay #define __FUNCT__ "SNESSetMinimizationFunctionTolerance"
16009b94acceSBarry Smith /*@
160177c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
16029b94acceSBarry Smith    for unconstrained minimization solvers.
16039b94acceSBarry Smith 
1604fee21e36SBarry Smith    Collective on SNES
1605fee21e36SBarry Smith 
1606c7afd0dbSLois Curfman McInnes    Input Parameters:
1607c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1608c7afd0dbSLois Curfman McInnes -  ftol - minimum function tolerance
1609c7afd0dbSLois Curfman McInnes 
16109b94acceSBarry Smith    Options Database Key:
1611c7afd0dbSLois Curfman McInnes .  -snes_fmin <ftol> - Sets ftol
16129b94acceSBarry Smith 
16139b94acceSBarry Smith    Note:
161477c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
16159b94acceSBarry Smith    methods only.
16169b94acceSBarry Smith 
161736851e7fSLois Curfman McInnes    Level: intermediate
161836851e7fSLois Curfman McInnes 
16199b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
16209b94acceSBarry Smith 
1621d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
16229b94acceSBarry Smith @*/
1623329f5518SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,PetscReal ftol)
16249b94acceSBarry Smith {
16253a40ed3dSBarry Smith   PetscFunctionBegin;
162677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16279b94acceSBarry Smith   snes->fmin = ftol;
16283a40ed3dSBarry Smith   PetscFunctionReturn(0);
16299b94acceSBarry Smith }
1630df9fa365SBarry Smith /*
1631df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1632df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1633df9fa365SBarry Smith    macros instead of functions
1634df9fa365SBarry Smith */
16354a2ae208SSatish Balay #undef __FUNCT__
16364a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitor"
1637329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx)
1638ce1608b8SBarry Smith {
1639ce1608b8SBarry Smith   int ierr;
1640ce1608b8SBarry Smith 
1641ce1608b8SBarry Smith   PetscFunctionBegin;
1642184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1643ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1644ce1608b8SBarry Smith   PetscFunctionReturn(0);
1645ce1608b8SBarry Smith }
1646ce1608b8SBarry Smith 
16474a2ae208SSatish Balay #undef __FUNCT__
16484a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorCreate"
1649b0a32e0cSBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,PetscDrawLG *draw)
1650df9fa365SBarry Smith {
1651df9fa365SBarry Smith   int ierr;
1652df9fa365SBarry Smith 
1653df9fa365SBarry Smith   PetscFunctionBegin;
1654df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1655df9fa365SBarry Smith   PetscFunctionReturn(0);
1656df9fa365SBarry Smith }
1657df9fa365SBarry Smith 
16584a2ae208SSatish Balay #undef __FUNCT__
16594a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorDestroy"
1660b0a32e0cSBarry Smith int SNESLGMonitorDestroy(PetscDrawLG draw)
1661df9fa365SBarry Smith {
1662df9fa365SBarry Smith   int ierr;
1663df9fa365SBarry Smith 
1664df9fa365SBarry Smith   PetscFunctionBegin;
1665df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1666df9fa365SBarry Smith   PetscFunctionReturn(0);
1667df9fa365SBarry Smith }
1668df9fa365SBarry Smith 
16699b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
16709b94acceSBarry Smith 
16714a2ae208SSatish Balay #undef __FUNCT__
16724a2ae208SSatish Balay #define __FUNCT__ "SNESSetMonitor"
16739b94acceSBarry Smith /*@C
16745cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
16759b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
16769b94acceSBarry Smith    progress.
16779b94acceSBarry Smith 
1678fee21e36SBarry Smith    Collective on SNES
1679fee21e36SBarry Smith 
1680c7afd0dbSLois Curfman McInnes    Input Parameters:
1681c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1682c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1683b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1684b3006f0bSLois Curfman McInnes           monitor routine (use PETSC_NULL if no context is desitre)
1685b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1686b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
16879b94acceSBarry Smith 
1688c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1689329f5518SBarry Smith $     int func(SNES snes,int its, PetscReal norm,void *mctx)
1690c7afd0dbSLois Curfman McInnes 
1691c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1692c7afd0dbSLois Curfman McInnes .    its - iteration number
1693c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
1694c7afd0dbSLois Curfman McInnes             for SNES_NONLINEAR_EQUATIONS methods
169540a0c1c6SLois Curfman McInnes .    norm - 2-norm gradient value (may be estimated)
1696c7afd0dbSLois Curfman McInnes             for SNES_UNCONSTRAINED_MINIMIZATION methods
169740a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
16989b94acceSBarry Smith 
16999665c990SLois Curfman McInnes    Options Database Keys:
1700c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1701c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1702c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1703c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1704c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1705c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1706c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1707c7afd0dbSLois Curfman McInnes                             the options database.
17089665c990SLois Curfman McInnes 
1709639f9d9dSBarry Smith    Notes:
17106bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
17116bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
17126bc08f3fSLois Curfman McInnes    order in which they were set.
1713639f9d9dSBarry Smith 
171436851e7fSLois Curfman McInnes    Level: intermediate
171536851e7fSLois Curfman McInnes 
17169b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
17179b94acceSBarry Smith 
17185cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
17199b94acceSBarry Smith @*/
1720329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *))
17219b94acceSBarry Smith {
17223a40ed3dSBarry Smith   PetscFunctionBegin;
1723184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1724639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
172529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1726639f9d9dSBarry Smith   }
1727639f9d9dSBarry Smith 
1728639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1729b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1730639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
17313a40ed3dSBarry Smith   PetscFunctionReturn(0);
17329b94acceSBarry Smith }
17339b94acceSBarry Smith 
17344a2ae208SSatish Balay #undef __FUNCT__
17354a2ae208SSatish Balay #define __FUNCT__ "SNESClearMonitor"
17365cd90555SBarry Smith /*@C
17375cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
17385cd90555SBarry Smith 
1739c7afd0dbSLois Curfman McInnes    Collective on SNES
1740c7afd0dbSLois Curfman McInnes 
17415cd90555SBarry Smith    Input Parameters:
17425cd90555SBarry Smith .  snes - the SNES context
17435cd90555SBarry Smith 
17445cd90555SBarry Smith    Options Database:
1745c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1746c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1747c7afd0dbSLois Curfman McInnes     set via the options database
17485cd90555SBarry Smith 
17495cd90555SBarry Smith    Notes:
17505cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
17515cd90555SBarry Smith 
175236851e7fSLois Curfman McInnes    Level: intermediate
175336851e7fSLois Curfman McInnes 
17545cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
17555cd90555SBarry Smith 
17565cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
17575cd90555SBarry Smith @*/
17585cd90555SBarry Smith int SNESClearMonitor(SNES snes)
17595cd90555SBarry Smith {
17605cd90555SBarry Smith   PetscFunctionBegin;
1761184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
17625cd90555SBarry Smith   snes->numbermonitors = 0;
17635cd90555SBarry Smith   PetscFunctionReturn(0);
17645cd90555SBarry Smith }
17655cd90555SBarry Smith 
17664a2ae208SSatish Balay #undef __FUNCT__
17674a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
17689b94acceSBarry Smith /*@C
17699b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
17709b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
17719b94acceSBarry Smith 
1772fee21e36SBarry Smith    Collective on SNES
1773fee21e36SBarry Smith 
1774c7afd0dbSLois Curfman McInnes    Input Parameters:
1775c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1776c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1777c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1778c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
17799b94acceSBarry Smith 
1780c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1781329f5518SBarry Smith $     int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1782c7afd0dbSLois Curfman McInnes 
1783c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1784c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1785184914b5SBarry Smith .    reason - reason for convergence/divergence
1786c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1787c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods)
1788c7afd0dbSLois Curfman McInnes .    f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods)
1789c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods)
1790c7afd0dbSLois Curfman McInnes -    f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods)
17919b94acceSBarry Smith 
179236851e7fSLois Curfman McInnes    Level: advanced
179336851e7fSLois Curfman McInnes 
17949b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
17959b94acceSBarry Smith 
179640191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
179740191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
17989b94acceSBarry Smith @*/
1799329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
18009b94acceSBarry Smith {
18013a40ed3dSBarry Smith   PetscFunctionBegin;
1802184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18039b94acceSBarry Smith   (snes)->converged = func;
18049b94acceSBarry Smith   (snes)->cnvP      = cctx;
18053a40ed3dSBarry Smith   PetscFunctionReturn(0);
18069b94acceSBarry Smith }
18079b94acceSBarry Smith 
18084a2ae208SSatish Balay #undef __FUNCT__
18094a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
1810184914b5SBarry Smith /*@C
1811184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1812184914b5SBarry Smith 
1813184914b5SBarry Smith    Not Collective
1814184914b5SBarry Smith 
1815184914b5SBarry Smith    Input Parameter:
1816184914b5SBarry Smith .  snes - the SNES context
1817184914b5SBarry Smith 
1818184914b5SBarry Smith    Output Parameter:
1819e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1820184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1821184914b5SBarry Smith 
1822184914b5SBarry Smith    Level: intermediate
1823184914b5SBarry Smith 
1824184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1825184914b5SBarry Smith 
1826184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1827184914b5SBarry Smith 
1828184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
1829435da068SBarry Smith           SNESConverged_UM_LS(), SNESConverged_UM_TR(), SNESConvergedReason
1830184914b5SBarry Smith @*/
1831184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1832184914b5SBarry Smith {
1833184914b5SBarry Smith   PetscFunctionBegin;
1834184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1835184914b5SBarry Smith   *reason = snes->reason;
1836184914b5SBarry Smith   PetscFunctionReturn(0);
1837184914b5SBarry Smith }
1838184914b5SBarry Smith 
18394a2ae208SSatish Balay #undef __FUNCT__
18404a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1841c9005455SLois Curfman McInnes /*@
1842c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1843c9005455SLois Curfman McInnes 
1844fee21e36SBarry Smith    Collective on SNES
1845fee21e36SBarry Smith 
1846c7afd0dbSLois Curfman McInnes    Input Parameters:
1847c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1848c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1849cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1850758f92a0SBarry Smith .  na  - size of a and its
185164731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1852758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1853c7afd0dbSLois Curfman McInnes 
1854c9005455SLois Curfman McInnes    Notes:
1855c9005455SLois Curfman McInnes    If set, this array will contain the function norms (for
1856c9005455SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods) or gradient norms
1857c9005455SLois Curfman McInnes    (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed
1858c9005455SLois Curfman McInnes    at each step.
1859c9005455SLois Curfman McInnes 
1860c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1861c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1862c9005455SLois Curfman McInnes    during the section of code that is being timed.
1863c9005455SLois Curfman McInnes 
186436851e7fSLois Curfman McInnes    Level: intermediate
186536851e7fSLois Curfman McInnes 
1866c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1867758f92a0SBarry Smith 
186808405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1869758f92a0SBarry Smith 
1870c9005455SLois Curfman McInnes @*/
1871329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset)
1872c9005455SLois Curfman McInnes {
18733a40ed3dSBarry Smith   PetscFunctionBegin;
1874c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1875c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1876c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1877758f92a0SBarry Smith   snes->conv_hist_its   = its;
1878758f92a0SBarry Smith   snes->conv_hist_max   = na;
1879758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1880758f92a0SBarry Smith   PetscFunctionReturn(0);
1881758f92a0SBarry Smith }
1882758f92a0SBarry Smith 
18834a2ae208SSatish Balay #undef __FUNCT__
18844a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
18850c4c9dddSBarry Smith /*@C
1886758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1887758f92a0SBarry Smith 
1888758f92a0SBarry Smith    Collective on SNES
1889758f92a0SBarry Smith 
1890758f92a0SBarry Smith    Input Parameter:
1891758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1892758f92a0SBarry Smith 
1893758f92a0SBarry Smith    Output Parameters:
1894758f92a0SBarry Smith .  a   - array to hold history
1895758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1896758f92a0SBarry Smith          negative if not converged) for each solve.
1897758f92a0SBarry Smith -  na  - size of a and its
1898758f92a0SBarry Smith 
1899758f92a0SBarry Smith    Notes:
1900758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1901758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1902758f92a0SBarry Smith 
1903758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1904758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1905758f92a0SBarry Smith    during the section of code that is being timed.
1906758f92a0SBarry Smith 
1907758f92a0SBarry Smith    Level: intermediate
1908758f92a0SBarry Smith 
1909758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1910758f92a0SBarry Smith 
1911758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1912758f92a0SBarry Smith 
1913758f92a0SBarry Smith @*/
1914329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na)
1915758f92a0SBarry Smith {
1916758f92a0SBarry Smith   PetscFunctionBegin;
1917758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1918758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1919758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1920758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
19213a40ed3dSBarry Smith   PetscFunctionReturn(0);
1922c9005455SLois Curfman McInnes }
1923c9005455SLois Curfman McInnes 
1924*e74ef692SMatthew Knepley #undef __FUNCT__
1925*e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetRhsBC"
192676b2cf59SMatthew Knepley /*@
192776b2cf59SMatthew Knepley   SNESSetRhsBC - Sets the function which applies boundary conditions
192876b2cf59SMatthew Knepley   to the Rhs of each system.
192976b2cf59SMatthew Knepley 
193076b2cf59SMatthew Knepley   Collective on SNES
193176b2cf59SMatthew Knepley 
193276b2cf59SMatthew Knepley   Input Parameters:
193376b2cf59SMatthew Knepley . snes - The nonlinear solver context
193476b2cf59SMatthew Knepley . func - The function
193576b2cf59SMatthew Knepley 
193676b2cf59SMatthew Knepley   Calling sequence of func:
193776b2cf59SMatthew Knepley . func (SNES snes, Vec rhs, void *ctx);
193876b2cf59SMatthew Knepley 
193976b2cf59SMatthew Knepley . rhs - The current rhs vector
194076b2cf59SMatthew Knepley . ctx - The user-context
194176b2cf59SMatthew Knepley 
194276b2cf59SMatthew Knepley   Level: intermediate
194376b2cf59SMatthew Knepley 
194476b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
194576b2cf59SMatthew Knepley .seealso SNESDefaultRhsBC(), SNESSetSolutionBC(), SNESSetUpdate()
194676b2cf59SMatthew Knepley @*/
194776b2cf59SMatthew Knepley int SNESSetRhsBC(SNES snes, int (*func)(SNES, Vec, void *))
194876b2cf59SMatthew Knepley {
194976b2cf59SMatthew Knepley   PetscFunctionBegin;
195076b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
195176b2cf59SMatthew Knepley   snes->applyrhsbc = func;
195276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
195376b2cf59SMatthew Knepley }
195476b2cf59SMatthew Knepley 
1955*e74ef692SMatthew Knepley #undef __FUNCT__
1956*e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultRhsBC"
195776b2cf59SMatthew Knepley /*@
195876b2cf59SMatthew Knepley   SNESDefaultRhsBC - The default boundary condition function which does nothing.
195976b2cf59SMatthew Knepley 
196076b2cf59SMatthew Knepley   Not collective
196176b2cf59SMatthew Knepley 
196276b2cf59SMatthew Knepley   Input Parameters:
196376b2cf59SMatthew Knepley . snes - The nonlinear solver context
196476b2cf59SMatthew Knepley . rhs  - The Rhs
196576b2cf59SMatthew Knepley . ctx  - The user-context
196676b2cf59SMatthew Knepley 
196776b2cf59SMatthew Knepley   Level: beginner
196876b2cf59SMatthew Knepley 
196976b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
197076b2cf59SMatthew Knepley .seealso SNESSetRhsBC(), SNESDefaultSolutionBC(), SNESDefaultUpdate()
197176b2cf59SMatthew Knepley @*/
197276b2cf59SMatthew Knepley int SNESDefaultRhsBC(SNES snes, Vec rhs, void *ctx)
197376b2cf59SMatthew Knepley {
197476b2cf59SMatthew Knepley   PetscFunctionBegin;
197576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
197676b2cf59SMatthew Knepley }
197776b2cf59SMatthew Knepley 
1978*e74ef692SMatthew Knepley #undef __FUNCT__
1979*e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetSolutionBC"
198076b2cf59SMatthew Knepley /*@
198176b2cf59SMatthew Knepley   SNESSetSolutionBC - Sets the function which applies boundary conditions
198276b2cf59SMatthew Knepley   to the solution of each system.
198376b2cf59SMatthew Knepley 
198476b2cf59SMatthew Knepley   Collective on SNES
198576b2cf59SMatthew Knepley 
198676b2cf59SMatthew Knepley   Input Parameters:
198776b2cf59SMatthew Knepley . snes - The nonlinear solver context
198876b2cf59SMatthew Knepley . func - The function
198976b2cf59SMatthew Knepley 
199076b2cf59SMatthew Knepley   Calling sequence of func:
199176b2cf59SMatthew Knepley . func (TS ts, Vec rsol, void *ctx);
199276b2cf59SMatthew Knepley 
199376b2cf59SMatthew Knepley . sol - The current solution vector
199476b2cf59SMatthew Knepley . ctx - The user-context
199576b2cf59SMatthew Knepley 
199676b2cf59SMatthew Knepley   Level: intermediate
199776b2cf59SMatthew Knepley 
199876b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
199976b2cf59SMatthew Knepley .seealso SNESDefaultSolutionBC(), SNESSetRhsBC(), SNESSetUpdate()
200076b2cf59SMatthew Knepley @*/
200176b2cf59SMatthew Knepley int SNESSetSolutionBC(SNES snes, int (*func)(SNES, Vec, void *))
200276b2cf59SMatthew Knepley {
200376b2cf59SMatthew Knepley   PetscFunctionBegin;
200476b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
200576b2cf59SMatthew Knepley   snes->applysolbc = func;
200676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
200776b2cf59SMatthew Knepley }
200876b2cf59SMatthew Knepley 
2009*e74ef692SMatthew Knepley #undef __FUNCT__
2010*e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultSolutionBC"
201176b2cf59SMatthew Knepley /*@
201276b2cf59SMatthew Knepley   SNESDefaultSolutionBC - The default boundary condition function which does nothing.
201376b2cf59SMatthew Knepley 
201476b2cf59SMatthew Knepley   Not collective
201576b2cf59SMatthew Knepley 
201676b2cf59SMatthew Knepley   Input Parameters:
201776b2cf59SMatthew Knepley . snes - The nonlinear solver context
201876b2cf59SMatthew Knepley . sol  - The solution
201976b2cf59SMatthew Knepley . ctx  - The user-context
202076b2cf59SMatthew Knepley 
202176b2cf59SMatthew Knepley   Level: beginner
202276b2cf59SMatthew Knepley 
202376b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
202476b2cf59SMatthew Knepley .seealso SNESSetSolutionBC(), SNESDefaultRhsBC(), SNESDefaultUpdate()
202576b2cf59SMatthew Knepley @*/
202676b2cf59SMatthew Knepley int SNESDefaultSolutionBC(SNES snes, Vec sol, void *ctx)
202776b2cf59SMatthew Knepley {
202876b2cf59SMatthew Knepley   PetscFunctionBegin;
202976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
203076b2cf59SMatthew Knepley }
203176b2cf59SMatthew Knepley 
2032*e74ef692SMatthew Knepley #undef __FUNCT__
2033*e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
203476b2cf59SMatthew Knepley /*@
203576b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
203676b2cf59SMatthew Knepley   at the beginning of every step of the iteration.
203776b2cf59SMatthew Knepley 
203876b2cf59SMatthew Knepley   Collective on SNES
203976b2cf59SMatthew Knepley 
204076b2cf59SMatthew Knepley   Input Parameters:
204176b2cf59SMatthew Knepley . snes - The nonlinear solver context
204276b2cf59SMatthew Knepley . func - The function
204376b2cf59SMatthew Knepley 
204476b2cf59SMatthew Knepley   Calling sequence of func:
204576b2cf59SMatthew Knepley . func (TS ts, int step);
204676b2cf59SMatthew Knepley 
204776b2cf59SMatthew Knepley . step - The current step of the iteration
204876b2cf59SMatthew Knepley 
204976b2cf59SMatthew Knepley   Level: intermediate
205076b2cf59SMatthew Knepley 
205176b2cf59SMatthew Knepley .keywords: SNES, update
205276b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
205376b2cf59SMatthew Knepley @*/
205476b2cf59SMatthew Knepley int SNESSetUpdate(SNES snes, int (*func)(SNES, int))
205576b2cf59SMatthew Knepley {
205676b2cf59SMatthew Knepley   PetscFunctionBegin;
205776b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
205876b2cf59SMatthew Knepley   snes->update = func;
205976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
206076b2cf59SMatthew Knepley }
206176b2cf59SMatthew Knepley 
2062*e74ef692SMatthew Knepley #undef __FUNCT__
2063*e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
206476b2cf59SMatthew Knepley /*@
206576b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
206676b2cf59SMatthew Knepley 
206776b2cf59SMatthew Knepley   Not collective
206876b2cf59SMatthew Knepley 
206976b2cf59SMatthew Knepley   Input Parameters:
207076b2cf59SMatthew Knepley . snes - The nonlinear solver context
207176b2cf59SMatthew Knepley . step - The current step of the iteration
207276b2cf59SMatthew Knepley 
207376b2cf59SMatthew Knepley .keywords: SNES, update
207476b2cf59SMatthew Knepley .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultSolutionBC()
207576b2cf59SMatthew Knepley @*/
207676b2cf59SMatthew Knepley int SNESDefaultUpdate(SNES snes, int step)
207776b2cf59SMatthew Knepley {
207876b2cf59SMatthew Knepley   PetscFunctionBegin;
207976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
208076b2cf59SMatthew Knepley }
208176b2cf59SMatthew Knepley 
20824a2ae208SSatish Balay #undef __FUNCT__
20834a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
20849b94acceSBarry Smith /*
20859b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
20869b94acceSBarry Smith    positive parameter delta.
20879b94acceSBarry Smith 
20889b94acceSBarry Smith     Input Parameters:
2089c7afd0dbSLois Curfman McInnes +   snes - the SNES context
20909b94acceSBarry Smith .   y - approximate solution of linear system
20919b94acceSBarry Smith .   fnorm - 2-norm of current function
2092c7afd0dbSLois Curfman McInnes -   delta - trust region size
20939b94acceSBarry Smith 
20949b94acceSBarry Smith     Output Parameters:
2095c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
20969b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
20979b94acceSBarry Smith     region, and exceeds zero otherwise.
2098c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
20999b94acceSBarry Smith 
21009b94acceSBarry Smith     Note:
21016831982aSBarry Smith     For non-trust region methods such as SNESEQLS, the parameter delta
21029b94acceSBarry Smith     is set to be the maximum allowable step size.
21039b94acceSBarry Smith 
21049b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
21059b94acceSBarry Smith */
2106329f5518SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,
2107329f5518SBarry Smith                           PetscReal *gpnorm,PetscReal *ynorm)
21089b94acceSBarry Smith {
2109329f5518SBarry Smith   PetscReal norm;
2110ea709b57SSatish Balay   PetscScalar cnorm;
21113a40ed3dSBarry Smith   int    ierr;
21123a40ed3dSBarry Smith 
21133a40ed3dSBarry Smith   PetscFunctionBegin;
2114184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2115184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
2116184914b5SBarry Smith   PetscCheckSameComm(snes,y);
2117184914b5SBarry Smith 
21183a40ed3dSBarry Smith   ierr = VecNorm(y,NORM_2,&norm);CHKERRQ(ierr);
21199b94acceSBarry Smith   if (norm > *delta) {
21209b94acceSBarry Smith      norm = *delta/norm;
21219b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
21229b94acceSBarry Smith      cnorm = norm;
2123329f5518SBarry Smith      ierr = VecScale(&cnorm,y);CHKERRQ(ierr);
21249b94acceSBarry Smith      *ynorm = *delta;
21259b94acceSBarry Smith   } else {
21269b94acceSBarry Smith      *gpnorm = 0.0;
21279b94acceSBarry Smith      *ynorm = norm;
21289b94acceSBarry Smith   }
21293a40ed3dSBarry Smith   PetscFunctionReturn(0);
21309b94acceSBarry Smith }
21319b94acceSBarry Smith 
21324a2ae208SSatish Balay #undef __FUNCT__
21334a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
21349b94acceSBarry Smith /*@
21359b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
213663a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
21379b94acceSBarry Smith 
2138c7afd0dbSLois Curfman McInnes    Collective on SNES
2139c7afd0dbSLois Curfman McInnes 
2140b2002411SLois Curfman McInnes    Input Parameters:
2141c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2142c7afd0dbSLois Curfman McInnes -  x - the solution vector
21439b94acceSBarry Smith 
21449b94acceSBarry Smith    Output Parameter:
2145b2002411SLois Curfman McInnes .  its - number of iterations until termination
21469b94acceSBarry Smith 
2147b2002411SLois Curfman McInnes    Notes:
21488ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
21498ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
21508ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
21518ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
21528ddd3da0SLois Curfman McInnes 
215336851e7fSLois Curfman McInnes    Level: beginner
215436851e7fSLois Curfman McInnes 
21559b94acceSBarry Smith .keywords: SNES, nonlinear, solve
21569b94acceSBarry Smith 
215763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
21589b94acceSBarry Smith @*/
21598ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
21609b94acceSBarry Smith {
2161f1af5d2fSBarry Smith   int        ierr;
2162f1af5d2fSBarry Smith   PetscTruth flg;
2163052efed2SBarry Smith 
21643a40ed3dSBarry Smith   PetscFunctionBegin;
216577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2166184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
2167184914b5SBarry Smith   PetscCheckSameComm(snes,x);
216874679c65SBarry Smith   PetscValidIntPointer(its);
216929bbc08cSBarry Smith   if (!snes->solve) SETERRQ(1,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
217074637425SBarry Smith 
217182bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
2172c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
2173758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
2174b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
2175c96a6f78SLois Curfman McInnes   snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0;
21769b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr);
2177b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
2178b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr);
217993b98531SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESView(snes,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); }
21803a40ed3dSBarry Smith   PetscFunctionReturn(0);
21819b94acceSBarry Smith }
21829b94acceSBarry Smith 
21839b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
21849b94acceSBarry Smith 
21854a2ae208SSatish Balay #undef __FUNCT__
21864a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
218782bf6240SBarry Smith /*@C
21884b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
21899b94acceSBarry Smith 
2190fee21e36SBarry Smith    Collective on SNES
2191fee21e36SBarry Smith 
2192c7afd0dbSLois Curfman McInnes    Input Parameters:
2193c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2194454a90a3SBarry Smith -  type - a known method
2195c7afd0dbSLois Curfman McInnes 
2196c7afd0dbSLois Curfman McInnes    Options Database Key:
2197454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2198c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2199ae12b187SLois Curfman McInnes 
22009b94acceSBarry Smith    Notes:
2201e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
22026831982aSBarry Smith +    SNESEQLS - Newton's method with line search
2203c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22046831982aSBarry Smith .    SNESEQTR - Newton's method with trust region
2205c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22066831982aSBarry Smith .    SNESUMTR - Newton's method with trust region
2207c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
22086831982aSBarry Smith -    SNESUMLS - Newton's method with line search
2209c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
22109b94acceSBarry Smith 
2211ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2212ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2213ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2214ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2215ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2216ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2217ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2218ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2219ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2220b0a32e0cSBarry Smith   appropriate method.
222136851e7fSLois Curfman McInnes 
222236851e7fSLois Curfman McInnes   Level: intermediate
2223a703fe33SLois Curfman McInnes 
2224454a90a3SBarry Smith .keywords: SNES, set, type
2225435da068SBarry Smith 
2226435da068SBarry Smith .seealso: SNESType, SNESCreate()
2227435da068SBarry Smith 
22289b94acceSBarry Smith @*/
2229454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type)
22309b94acceSBarry Smith {
22310f5bd95cSBarry Smith   int        ierr,(*r)(SNES);
22326831982aSBarry Smith   PetscTruth match;
22333a40ed3dSBarry Smith 
22343a40ed3dSBarry Smith   PetscFunctionBegin;
223577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
22360f5bd95cSBarry Smith   PetscValidCharPointer(type);
223782bf6240SBarry Smith 
22386831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
22390f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
224082bf6240SBarry Smith 
224182bf6240SBarry Smith   if (snes->setupcalled) {
2242e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
224382bf6240SBarry Smith     snes->data = 0;
224482bf6240SBarry Smith   }
22457d1a2b2bSBarry Smith 
22469b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
224782bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
224882bf6240SBarry Smith 
2249b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
225082bf6240SBarry Smith 
225129bbc08cSBarry Smith   if (!r) SETERRQ1(1,"Unable to find requested SNES type %s",type);
22521548b14aSBarry Smith 
2253606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
225482bf6240SBarry Smith   snes->data = 0;
22553a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
225682bf6240SBarry Smith 
2257454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
225882bf6240SBarry Smith   snes->set_method_called = 1;
225982bf6240SBarry Smith 
22603a40ed3dSBarry Smith   PetscFunctionReturn(0);
22619b94acceSBarry Smith }
22629b94acceSBarry Smith 
2263a847f771SSatish Balay 
22649b94acceSBarry Smith /* --------------------------------------------------------------------- */
22654a2ae208SSatish Balay #undef __FUNCT__
22664a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
22679b94acceSBarry Smith /*@C
22689b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2269f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
22709b94acceSBarry Smith 
2271fee21e36SBarry Smith    Not Collective
2272fee21e36SBarry Smith 
227336851e7fSLois Curfman McInnes    Level: advanced
227436851e7fSLois Curfman McInnes 
22759b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
22769b94acceSBarry Smith 
22779b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
22789b94acceSBarry Smith @*/
2279cf256101SBarry Smith int SNESRegisterDestroy(void)
22809b94acceSBarry Smith {
228182bf6240SBarry Smith   int ierr;
228282bf6240SBarry Smith 
22833a40ed3dSBarry Smith   PetscFunctionBegin;
228482bf6240SBarry Smith   if (SNESList) {
2285b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
228682bf6240SBarry Smith     SNESList = 0;
22879b94acceSBarry Smith   }
22884c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
22893a40ed3dSBarry Smith   PetscFunctionReturn(0);
22909b94acceSBarry Smith }
22919b94acceSBarry Smith 
22924a2ae208SSatish Balay #undef __FUNCT__
22934a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
22949b94acceSBarry Smith /*@C
22959a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
22969b94acceSBarry Smith 
2297c7afd0dbSLois Curfman McInnes    Not Collective
2298c7afd0dbSLois Curfman McInnes 
22999b94acceSBarry Smith    Input Parameter:
23004b0e389bSBarry Smith .  snes - nonlinear solver context
23019b94acceSBarry Smith 
23029b94acceSBarry Smith    Output Parameter:
23033a7fca6bSBarry Smith .  type - SNES method (a character string)
23049b94acceSBarry Smith 
230536851e7fSLois Curfman McInnes    Level: intermediate
230636851e7fSLois Curfman McInnes 
2307454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
23089b94acceSBarry Smith @*/
2309454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type)
23109b94acceSBarry Smith {
23113a40ed3dSBarry Smith   PetscFunctionBegin;
2312184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2313454a90a3SBarry Smith   *type = snes->type_name;
23143a40ed3dSBarry Smith   PetscFunctionReturn(0);
23159b94acceSBarry Smith }
23169b94acceSBarry Smith 
23174a2ae208SSatish Balay #undef __FUNCT__
23184a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
23199b94acceSBarry Smith /*@C
23209b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
23219b94acceSBarry Smith    stored.
23229b94acceSBarry Smith 
2323c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2324c7afd0dbSLois Curfman McInnes 
23259b94acceSBarry Smith    Input Parameter:
23269b94acceSBarry Smith .  snes - the SNES context
23279b94acceSBarry Smith 
23289b94acceSBarry Smith    Output Parameter:
23299b94acceSBarry Smith .  x - the solution
23309b94acceSBarry Smith 
233136851e7fSLois Curfman McInnes    Level: advanced
233236851e7fSLois Curfman McInnes 
23339b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
23349b94acceSBarry Smith 
2335a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
23369b94acceSBarry Smith @*/
23379b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
23389b94acceSBarry Smith {
23393a40ed3dSBarry Smith   PetscFunctionBegin;
234077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
23419b94acceSBarry Smith   *x = snes->vec_sol_always;
23423a40ed3dSBarry Smith   PetscFunctionReturn(0);
23439b94acceSBarry Smith }
23449b94acceSBarry Smith 
23454a2ae208SSatish Balay #undef __FUNCT__
23464a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
23479b94acceSBarry Smith /*@C
23489b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
23499b94acceSBarry Smith    stored.
23509b94acceSBarry Smith 
2351c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2352c7afd0dbSLois Curfman McInnes 
23539b94acceSBarry Smith    Input Parameter:
23549b94acceSBarry Smith .  snes - the SNES context
23559b94acceSBarry Smith 
23569b94acceSBarry Smith    Output Parameter:
23579b94acceSBarry Smith .  x - the solution update
23589b94acceSBarry Smith 
235936851e7fSLois Curfman McInnes    Level: advanced
236036851e7fSLois Curfman McInnes 
23619b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
23629b94acceSBarry Smith 
23639b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
23649b94acceSBarry Smith @*/
23659b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
23669b94acceSBarry Smith {
23673a40ed3dSBarry Smith   PetscFunctionBegin;
236877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
23699b94acceSBarry Smith   *x = snes->vec_sol_update_always;
23703a40ed3dSBarry Smith   PetscFunctionReturn(0);
23719b94acceSBarry Smith }
23729b94acceSBarry Smith 
23734a2ae208SSatish Balay #undef __FUNCT__
23744a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
23759b94acceSBarry Smith /*@C
23763638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
23779b94acceSBarry Smith 
2378c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2379c7afd0dbSLois Curfman McInnes 
23809b94acceSBarry Smith    Input Parameter:
23819b94acceSBarry Smith .  snes - the SNES context
23829b94acceSBarry Smith 
23839b94acceSBarry Smith    Output Parameter:
23847bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
238500036973SBarry Smith .  ctx - the function context (or PETSC_NULL)
238600036973SBarry Smith -  func - the function (or PETSC_NULL)
23879b94acceSBarry Smith 
23889b94acceSBarry Smith    Notes:
23899b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
23909b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
23919b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
23929b94acceSBarry Smith 
239336851e7fSLois Curfman McInnes    Level: advanced
239436851e7fSLois Curfman McInnes 
2395a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
23969b94acceSBarry Smith 
239731615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
239831615d04SLois Curfman McInnes           SNESGetGradient()
23997bf4e008SBarry Smith 
24009b94acceSBarry Smith @*/
240100036973SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx,int (**func)(SNES,Vec,Vec,void*))
24029b94acceSBarry Smith {
24033a40ed3dSBarry Smith   PetscFunctionBegin;
240477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2405a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
240629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only");
2407a8c6a408SBarry Smith   }
24087bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
24097bf4e008SBarry Smith   if (ctx)  *ctx  = snes->funP;
241000036973SBarry Smith   if (func) *func = snes->computefunction;
24113a40ed3dSBarry Smith   PetscFunctionReturn(0);
24129b94acceSBarry Smith }
24139b94acceSBarry Smith 
24144a2ae208SSatish Balay #undef __FUNCT__
24154a2ae208SSatish Balay #define __FUNCT__ "SNESGetGradient"
24169b94acceSBarry Smith /*@C
24173638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
24189b94acceSBarry Smith 
2419c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2420c7afd0dbSLois Curfman McInnes 
24219b94acceSBarry Smith    Input Parameter:
24229b94acceSBarry Smith .  snes - the SNES context
24239b94acceSBarry Smith 
24249b94acceSBarry Smith    Output Parameter:
24257bf4e008SBarry Smith +  r - the gradient (or PETSC_NULL)
24267bf4e008SBarry Smith -  ctx - the gradient context (or PETSC_NULL)
24279b94acceSBarry Smith 
24289b94acceSBarry Smith    Notes:
24299b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
24309b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
24319b94acceSBarry Smith    SNESGetFunction().
24329b94acceSBarry Smith 
243336851e7fSLois Curfman McInnes    Level: advanced
243436851e7fSLois Curfman McInnes 
24359b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
24369b94acceSBarry Smith 
24377bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(),
24387bf4e008SBarry Smith           SNESSetGradient(), SNESSetFunction()
24397bf4e008SBarry Smith 
24409b94acceSBarry Smith @*/
24417bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx)
24429b94acceSBarry Smith {
24433a40ed3dSBarry Smith   PetscFunctionBegin;
244477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
24453a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
244629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
24473a40ed3dSBarry Smith   }
24487bf4e008SBarry Smith   if (r)   *r = snes->vec_func_always;
24497bf4e008SBarry Smith   if (ctx) *ctx = snes->funP;
24503a40ed3dSBarry Smith   PetscFunctionReturn(0);
24519b94acceSBarry Smith }
24529b94acceSBarry Smith 
24534a2ae208SSatish Balay #undef __FUNCT__
24544a2ae208SSatish Balay #define __FUNCT__ "SNESGetMinimizationFunction"
24557bf4e008SBarry Smith /*@C
24569b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
24579b94acceSBarry Smith    unconstrained minimization problems.
24589b94acceSBarry Smith 
2459c7afd0dbSLois Curfman McInnes    Not Collective
2460c7afd0dbSLois Curfman McInnes 
24619b94acceSBarry Smith    Input Parameter:
24629b94acceSBarry Smith .  snes - the SNES context
24639b94acceSBarry Smith 
24649b94acceSBarry Smith    Output Parameter:
24657bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
24667bf4e008SBarry Smith -  ctx - the function context (or PETSC_NULL)
24679b94acceSBarry Smith 
24689b94acceSBarry Smith    Notes:
24699b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
24709b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
24719b94acceSBarry Smith    SNESGetFunction().
24729b94acceSBarry Smith 
247336851e7fSLois Curfman McInnes    Level: advanced
247436851e7fSLois Curfman McInnes 
24759b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
24769b94acceSBarry Smith 
24777bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction()
24787bf4e008SBarry Smith 
24799b94acceSBarry Smith @*/
2480329f5518SBarry Smith int SNESGetMinimizationFunction(SNES snes,PetscReal *r,void **ctx)
24819b94acceSBarry Smith {
24823a40ed3dSBarry Smith   PetscFunctionBegin;
248377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
248474679c65SBarry Smith   PetscValidScalarPointer(r);
24853a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
248629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only");
24873a40ed3dSBarry Smith   }
24887bf4e008SBarry Smith   if (r)   *r = snes->fc;
24897bf4e008SBarry Smith   if (ctx) *ctx = snes->umfunP;
24903a40ed3dSBarry Smith   PetscFunctionReturn(0);
24919b94acceSBarry Smith }
24929b94acceSBarry Smith 
24934a2ae208SSatish Balay #undef __FUNCT__
24944a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
24953c7409f5SSatish Balay /*@C
24963c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2497d850072dSLois Curfman McInnes    SNES options in the database.
24983c7409f5SSatish Balay 
2499fee21e36SBarry Smith    Collective on SNES
2500fee21e36SBarry Smith 
2501c7afd0dbSLois Curfman McInnes    Input Parameter:
2502c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2503c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2504c7afd0dbSLois Curfman McInnes 
2505d850072dSLois Curfman McInnes    Notes:
2506a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2507c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2508d850072dSLois Curfman McInnes 
250936851e7fSLois Curfman McInnes    Level: advanced
251036851e7fSLois Curfman McInnes 
25113c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2512a86d99e1SLois Curfman McInnes 
2513a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
25143c7409f5SSatish Balay @*/
25153c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
25163c7409f5SSatish Balay {
25173c7409f5SSatish Balay   int ierr;
25183c7409f5SSatish Balay 
25193a40ed3dSBarry Smith   PetscFunctionBegin;
252077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2521639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25223c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
25233a40ed3dSBarry Smith   PetscFunctionReturn(0);
25243c7409f5SSatish Balay }
25253c7409f5SSatish Balay 
25264a2ae208SSatish Balay #undef __FUNCT__
25274a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
25283c7409f5SSatish Balay /*@C
2529f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2530d850072dSLois Curfman McInnes    SNES options in the database.
25313c7409f5SSatish Balay 
2532fee21e36SBarry Smith    Collective on SNES
2533fee21e36SBarry Smith 
2534c7afd0dbSLois Curfman McInnes    Input Parameters:
2535c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2536c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2537c7afd0dbSLois Curfman McInnes 
2538d850072dSLois Curfman McInnes    Notes:
2539a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2540c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2541d850072dSLois Curfman McInnes 
254236851e7fSLois Curfman McInnes    Level: advanced
254336851e7fSLois Curfman McInnes 
25443c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2545a86d99e1SLois Curfman McInnes 
2546a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
25473c7409f5SSatish Balay @*/
25483c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
25493c7409f5SSatish Balay {
25503c7409f5SSatish Balay   int ierr;
25513c7409f5SSatish Balay 
25523a40ed3dSBarry Smith   PetscFunctionBegin;
255377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2554639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25553c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
25563a40ed3dSBarry Smith   PetscFunctionReturn(0);
25573c7409f5SSatish Balay }
25583c7409f5SSatish Balay 
25594a2ae208SSatish Balay #undef __FUNCT__
25604a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
25619ab63eb5SSatish Balay /*@C
25623c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
25633c7409f5SSatish Balay    SNES options in the database.
25643c7409f5SSatish Balay 
2565c7afd0dbSLois Curfman McInnes    Not Collective
2566c7afd0dbSLois Curfman McInnes 
25673c7409f5SSatish Balay    Input Parameter:
25683c7409f5SSatish Balay .  snes - the SNES context
25693c7409f5SSatish Balay 
25703c7409f5SSatish Balay    Output Parameter:
25713c7409f5SSatish Balay .  prefix - pointer to the prefix string used
25723c7409f5SSatish Balay 
25739ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
25749ab63eb5SSatish Balay    sufficient length to hold the prefix.
25759ab63eb5SSatish Balay 
257636851e7fSLois Curfman McInnes    Level: advanced
257736851e7fSLois Curfman McInnes 
25783c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2579a86d99e1SLois Curfman McInnes 
2580a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
25813c7409f5SSatish Balay @*/
25823c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
25833c7409f5SSatish Balay {
25843c7409f5SSatish Balay   int ierr;
25853c7409f5SSatish Balay 
25863a40ed3dSBarry Smith   PetscFunctionBegin;
258777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2588639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25893a40ed3dSBarry Smith   PetscFunctionReturn(0);
25903c7409f5SSatish Balay }
25913c7409f5SSatish Balay 
2592acb85ae6SSatish Balay /*MC
2593f1af5d2fSBarry Smith    SNESRegisterDynamic - Adds a method to the nonlinear solver package.
25949b94acceSBarry Smith 
2595b2002411SLois Curfman McInnes    Synopsis:
25963a7fca6bSBarry Smith    int SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
25979b94acceSBarry Smith 
25988d76a1e5SLois Curfman McInnes    Not collective
25998d76a1e5SLois Curfman McInnes 
2600b2002411SLois Curfman McInnes    Input Parameters:
2601c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2602b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2603b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2604c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
26059b94acceSBarry Smith 
2606b2002411SLois Curfman McInnes    Notes:
2607f1af5d2fSBarry Smith    SNESRegisterDynamic() may be called multiple times to add several user-defined solvers.
26083a40ed3dSBarry Smith 
2609b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2610b2002411SLois Curfman McInnes    is ignored.
2611b2002411SLois Curfman McInnes 
2612b9617806SBarry Smith    Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT},
26135ba88a07SLois Curfman McInnes    and others of the form ${any_environmental_variable} occuring in pathname will be
26145ba88a07SLois Curfman McInnes    replaced with appropriate values.
26155ba88a07SLois Curfman McInnes 
2616b2002411SLois Curfman McInnes    Sample usage:
261769225d78SLois Curfman McInnes .vb
2618f1af5d2fSBarry Smith    SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2619b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
262069225d78SLois Curfman McInnes .ve
2621b2002411SLois Curfman McInnes 
2622b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2623b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2624b2002411SLois Curfman McInnes    or at runtime via the option
2625b2002411SLois Curfman McInnes $     -snes_type my_solver
2626b2002411SLois Curfman McInnes 
262736851e7fSLois Curfman McInnes    Level: advanced
262836851e7fSLois Curfman McInnes 
2629b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2630b2002411SLois Curfman McInnes 
2631b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2632b2002411SLois Curfman McInnes M*/
2633b2002411SLois Curfman McInnes 
26344a2ae208SSatish Balay #undef __FUNCT__
26354a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
2636f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES))
2637b2002411SLois Curfman McInnes {
2638b2002411SLois Curfman McInnes   char fullname[256];
2639b2002411SLois Curfman McInnes   int  ierr;
2640b2002411SLois Curfman McInnes 
2641b2002411SLois Curfman McInnes   PetscFunctionBegin;
2642b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2643b9617806SBarry Smith   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)())function);CHKERRQ(ierr);
2644b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2645b2002411SLois Curfman McInnes }
2646