xref: /petsc/src/snes/interface/snes.c (revision c9780b6f0ebe9a8148f0ae06999feae3eb835510)
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;
68ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
78ba1e511SMatthew Knepley 
88ba1e511SMatthew Knepley /* Logging support */
943c77886SBarry Smith int SNES_COOKIE = 0;
1043c77886SBarry Smith int SNES_Solve = 0, SNES_LineSearch = 0, SNES_FunctionEval = 0, SNES_JacobianEval = 0;
11a09944afSBarry Smith 
12a09944afSBarry Smith #undef __FUNCT__
134a2ae208SSatish Balay #define __FUNCT__ "SNESView"
147e2c5f70SBarry Smith /*@C
159b94acceSBarry Smith    SNESView - Prints the SNES data structure.
169b94acceSBarry Smith 
174c49b128SBarry Smith    Collective on SNES
18fee21e36SBarry Smith 
19c7afd0dbSLois Curfman McInnes    Input Parameters:
20c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
21c7afd0dbSLois Curfman McInnes -  viewer - visualization context
22c7afd0dbSLois Curfman McInnes 
239b94acceSBarry Smith    Options Database Key:
24c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
259b94acceSBarry Smith 
269b94acceSBarry Smith    Notes:
279b94acceSBarry Smith    The available visualization contexts include
28b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
29b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
30c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
31c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
32c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
339b94acceSBarry Smith 
343e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
35b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
369b94acceSBarry Smith 
3736851e7fSLois Curfman McInnes    Level: beginner
3836851e7fSLois Curfman McInnes 
399b94acceSBarry Smith .keywords: SNES, view
409b94acceSBarry Smith 
41b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
429b94acceSBarry Smith @*/
43b0a32e0cSBarry Smith int SNESView(SNES snes,PetscViewer viewer)
449b94acceSBarry Smith {
459b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
469b94acceSBarry Smith   int                 ierr;
4794b7f48cSBarry Smith   KSP                 ksp;
48454a90a3SBarry Smith   char                *type;
496831982aSBarry Smith   PetscTruth          isascii,isstring;
509b94acceSBarry Smith 
513a40ed3dSBarry Smith   PetscFunctionBegin;
524482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
53b0a32e0cSBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(snes->comm);
544482741eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
55*c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
5674679c65SBarry Smith 
57b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
58b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
590f5bd95cSBarry Smith   if (isascii) {
603a7fca6bSBarry Smith     if (snes->prefix) {
613a7fca6bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr);
623a7fca6bSBarry Smith     } else {
63b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
643a7fca6bSBarry Smith     }
65454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
66454a90a3SBarry Smith     if (type) {
67b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
68184914b5SBarry Smith     } else {
69b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
70184914b5SBarry Smith     }
710ef38995SBarry Smith     if (snes->view) {
72b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
730ef38995SBarry Smith       ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr);
74b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
750ef38995SBarry Smith     }
76b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
7777d8c4bbSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, solution=%g\n",
7877d8c4bbSBarry Smith                  snes->rtol,snes->atol,snes->xtol);CHKERRQ(ierr);
79b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
80b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
819b94acceSBarry Smith     if (snes->ksp_ewconv) {
829b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
839b94acceSBarry Smith       if (kctx) {
84b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
85b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
86b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
879b94acceSBarry Smith       }
889b94acceSBarry Smith     }
890f5bd95cSBarry Smith   } else if (isstring) {
90454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
91b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
9219bcc07fSBarry Smith   }
9394b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
94b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
9594b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
96b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
973a40ed3dSBarry Smith   PetscFunctionReturn(0);
989b94acceSBarry Smith }
999b94acceSBarry Smith 
10076b2cf59SMatthew Knepley /*
10176b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
10276b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
10376b2cf59SMatthew Knepley */
10476b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
10576b2cf59SMatthew Knepley static int numberofsetfromoptions;
10676b2cf59SMatthew Knepley static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
10776b2cf59SMatthew Knepley 
108e74ef692SMatthew Knepley #undef __FUNCT__
109e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
110ac226902SBarry Smith /*@C
11176b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
11276b2cf59SMatthew Knepley 
11376b2cf59SMatthew Knepley   Not Collective
11476b2cf59SMatthew Knepley 
11576b2cf59SMatthew Knepley   Input Parameter:
11676b2cf59SMatthew Knepley . snescheck - function that checks for options
11776b2cf59SMatthew Knepley 
11876b2cf59SMatthew Knepley   Level: developer
11976b2cf59SMatthew Knepley 
12076b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
12176b2cf59SMatthew Knepley @*/
12276b2cf59SMatthew Knepley int SNESAddOptionsChecker(int (*snescheck)(SNES))
12376b2cf59SMatthew Knepley {
12476b2cf59SMatthew Knepley   PetscFunctionBegin;
12576b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
12676b2cf59SMatthew Knepley     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %d allowed", MAXSETFROMOPTIONS);
12776b2cf59SMatthew Knepley   }
12876b2cf59SMatthew Knepley 
12976b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
13076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
13176b2cf59SMatthew Knepley }
13276b2cf59SMatthew Knepley 
1334a2ae208SSatish Balay #undef __FUNCT__
1344a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1359b94acceSBarry Smith /*@
13694b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
1379b94acceSBarry Smith 
138c7afd0dbSLois Curfman McInnes    Collective on SNES
139c7afd0dbSLois Curfman McInnes 
1409b94acceSBarry Smith    Input Parameter:
1419b94acceSBarry Smith .  snes - the SNES context
1429b94acceSBarry Smith 
14336851e7fSLois Curfman McInnes    Options Database Keys:
1446831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
14582738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
14682738288SBarry Smith                 of the change in the solution between steps
147b39c3a46SLois Curfman McInnes .  -snes_atol <atol> - absolute tolerance of residual norm
148b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
149b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
150b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
15150ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
152b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
15382738288SBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear or minimization
15482738288SBarry Smith                                solver; hence iterations will continue until max_it
1551fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
15682738288SBarry Smith                                of convergence test
15782738288SBarry Smith .  -snes_monitor - prints residual norm at each iteration
158d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
159d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
16082738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
161e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
16236851e7fSLois Curfman McInnes -  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
16382738288SBarry Smith 
16482738288SBarry Smith     Options Database for Eisenstat-Walker method:
1654b27c08aSLois Curfman McInnes +  -snes_ksp_ew_conv - use Eisenstat-Walker method for determining linear system convergence
1664b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
16736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
16836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
16936851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
17036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
17136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
17236851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
17382738288SBarry Smith 
17411ca99fdSLois Curfman McInnes    Notes:
17511ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
17611ca99fdSLois Curfman McInnes    the users manual.
17783e2fdc7SBarry Smith 
17836851e7fSLois Curfman McInnes    Level: beginner
17936851e7fSLois Curfman McInnes 
1809b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
1819b94acceSBarry Smith 
18269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
1839b94acceSBarry Smith @*/
1849b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
1859b94acceSBarry Smith {
18694b7f48cSBarry Smith   KSP                 ksp;
187186905e3SBarry Smith   SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
188f1af5d2fSBarry Smith   PetscTruth          flg;
18976b2cf59SMatthew Knepley   int                 ierr, i;
1902fc52814SBarry Smith   const char          *deft;
1912fc52814SBarry Smith   char                type[256];
1929b94acceSBarry Smith 
1933a40ed3dSBarry Smith   PetscFunctionBegin;
1944482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
195ca161407SBarry Smith 
196b0a32e0cSBarry Smith   ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
197186905e3SBarry Smith     if (snes->type_name) {
198186905e3SBarry Smith       deft = snes->type_name;
199186905e3SBarry Smith     } else {
2004b27c08aSLois Curfman McInnes       deft = SNESLS;
201d64ed03dSBarry Smith     }
2024bbc92c1SBarry Smith 
203186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
204b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
205d64ed03dSBarry Smith     if (flg) {
206186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
207186905e3SBarry Smith     } else if (!snes->type_name) {
208186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
209d64ed03dSBarry Smith     }
210909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
21193c39befSBarry Smith 
21287828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
21387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->atol,&snes->atol,0);CHKERRQ(ierr);
214186905e3SBarry Smith 
21587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
216b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
217b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
21850ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
219186905e3SBarry Smith 
220b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_ksp_ew_conv","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetParametersEW",&snes->ksp_ewconv);CHKERRQ(ierr);
221186905e3SBarry Smith 
222b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1 or 2","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
22387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
22487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
22587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
22687828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
22787828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
22887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
229186905e3SBarry Smith 
230b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr);
23193c39befSBarry Smith     if (flg) {snes->converged = 0;}
232b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_cancelmonitors","Remove all monitors","SNESClearMonitor",&flg);CHKERRQ(ierr);
2335cd90555SBarry Smith     if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
234b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_monitor","Monitor norm of function","SNESDefaultMonitor",&flg);CHKERRQ(ierr);
235b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);}
2363a7fca6bSBarry Smith     ierr = PetscOptionsName("-snes_ratiomonitor","Monitor norm of function","SNESSetRatioMonitor",&flg);CHKERRQ(ierr);
2373a7fca6bSBarry Smith     if (flg) {ierr = SNESSetRatioMonitor(snes);CHKERRQ(ierr);}
238b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_smonitor","Monitor norm of function (fewer digits)","SNESDefaultSMonitor",&flg);CHKERRQ(ierr);
239b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);}
240b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor","Plot solution at each iteration","SNESVecViewMonitor",&flg);CHKERRQ(ierr);
241b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);}
242b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_update","Plot correction at each iteration","SNESVecViewUpdateMonitor",&flg);CHKERRQ(ierr);
2437c922b88SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewUpdateMonitor,0,0);CHKERRQ(ierr);}
2445ed2d596SBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_residual","Plot residual at each iteration","SNESVecViewResidualMonitor",&flg);CHKERRQ(ierr);
2455ed2d596SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewResidualMonitor,0,0);CHKERRQ(ierr);}
246b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_xmonitor","Plot function norm at each iteration","SNESLGMonitor",&flg);CHKERRQ(ierr);
247186905e3SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
248e24b481bSBarry Smith 
249b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
2504b27c08aSLois Curfman McInnes     if (flg) {
251186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
252b0a32e0cSBarry Smith       PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
2539b94acceSBarry Smith     }
254639f9d9dSBarry Smith 
25576b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
25676b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);                                                             CHKERRQ(ierr);
25776b2cf59SMatthew Knepley     }
25876b2cf59SMatthew Knepley 
259186905e3SBarry Smith     if (snes->setfromoptions) {
260186905e3SBarry Smith       ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
261639f9d9dSBarry Smith     }
262639f9d9dSBarry Smith 
263b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2644bbc92c1SBarry Smith 
26594b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
26694b7f48cSBarry Smith   ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
26793993e2dSLois Curfman McInnes 
2683a40ed3dSBarry Smith   PetscFunctionReturn(0);
2699b94acceSBarry Smith }
2709b94acceSBarry Smith 
271a847f771SSatish Balay 
2724a2ae208SSatish Balay #undef __FUNCT__
2734a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
2749b94acceSBarry Smith /*@
2759b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2769b94acceSBarry Smith    the nonlinear solvers.
2779b94acceSBarry Smith 
278fee21e36SBarry Smith    Collective on SNES
279fee21e36SBarry Smith 
280c7afd0dbSLois Curfman McInnes    Input Parameters:
281c7afd0dbSLois Curfman McInnes +  snes - the SNES context
282c7afd0dbSLois Curfman McInnes -  usrP - optional user context
283c7afd0dbSLois Curfman McInnes 
28436851e7fSLois Curfman McInnes    Level: intermediate
28536851e7fSLois Curfman McInnes 
2869b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
2879b94acceSBarry Smith 
2889b94acceSBarry Smith .seealso: SNESGetApplicationContext()
2899b94acceSBarry Smith @*/
2909b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
2919b94acceSBarry Smith {
2923a40ed3dSBarry Smith   PetscFunctionBegin;
2934482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2949b94acceSBarry Smith   snes->user		= usrP;
2953a40ed3dSBarry Smith   PetscFunctionReturn(0);
2969b94acceSBarry Smith }
29774679c65SBarry Smith 
2984a2ae208SSatish Balay #undef __FUNCT__
2994a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3009b94acceSBarry Smith /*@C
3019b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3029b94acceSBarry Smith    nonlinear solvers.
3039b94acceSBarry Smith 
304c7afd0dbSLois Curfman McInnes    Not Collective
305c7afd0dbSLois Curfman McInnes 
3069b94acceSBarry Smith    Input Parameter:
3079b94acceSBarry Smith .  snes - SNES context
3089b94acceSBarry Smith 
3099b94acceSBarry Smith    Output Parameter:
3109b94acceSBarry Smith .  usrP - user context
3119b94acceSBarry Smith 
31236851e7fSLois Curfman McInnes    Level: intermediate
31336851e7fSLois Curfman McInnes 
3149b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3159b94acceSBarry Smith 
3169b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3179b94acceSBarry Smith @*/
3189b94acceSBarry Smith int SNESGetApplicationContext(SNES snes,void **usrP)
3199b94acceSBarry Smith {
3203a40ed3dSBarry Smith   PetscFunctionBegin;
3214482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3229b94acceSBarry Smith   *usrP = snes->user;
3233a40ed3dSBarry Smith   PetscFunctionReturn(0);
3249b94acceSBarry Smith }
32574679c65SBarry Smith 
3264a2ae208SSatish Balay #undef __FUNCT__
3274a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3289b94acceSBarry Smith /*@
329c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
330c8228a4eSBarry Smith    at this time.
3319b94acceSBarry Smith 
332c7afd0dbSLois Curfman McInnes    Not Collective
333c7afd0dbSLois Curfman McInnes 
3349b94acceSBarry Smith    Input Parameter:
3359b94acceSBarry Smith .  snes - SNES context
3369b94acceSBarry Smith 
3379b94acceSBarry Smith    Output Parameter:
3389b94acceSBarry Smith .  iter - iteration number
3399b94acceSBarry Smith 
340c8228a4eSBarry Smith    Notes:
341c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
342c8228a4eSBarry Smith 
343c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
34408405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
34508405cd6SLois Curfman McInnes .vb
34608405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
34708405cd6SLois Curfman McInnes       if (!(it % 2)) {
34808405cd6SLois Curfman McInnes         [compute Jacobian here]
34908405cd6SLois Curfman McInnes       }
35008405cd6SLois Curfman McInnes .ve
351c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
35208405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
353c8228a4eSBarry Smith 
35436851e7fSLois Curfman McInnes    Level: intermediate
35536851e7fSLois Curfman McInnes 
3569b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3579b94acceSBarry Smith @*/
3589b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3599b94acceSBarry Smith {
3603a40ed3dSBarry Smith   PetscFunctionBegin;
3614482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3624482741eSBarry Smith   PetscValidIntPointer(iter,2);
3639b94acceSBarry Smith   *iter = snes->iter;
3643a40ed3dSBarry Smith   PetscFunctionReturn(0);
3659b94acceSBarry Smith }
36674679c65SBarry Smith 
3674a2ae208SSatish Balay #undef __FUNCT__
3684a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
3699b94acceSBarry Smith /*@
3709b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3719b94acceSBarry Smith    with SNESSSetFunction().
3729b94acceSBarry Smith 
373c7afd0dbSLois Curfman McInnes    Collective on SNES
374c7afd0dbSLois Curfman McInnes 
3759b94acceSBarry Smith    Input Parameter:
3769b94acceSBarry Smith .  snes - SNES context
3779b94acceSBarry Smith 
3789b94acceSBarry Smith    Output Parameter:
3799b94acceSBarry Smith .  fnorm - 2-norm of function
3809b94acceSBarry Smith 
38136851e7fSLois Curfman McInnes    Level: intermediate
38236851e7fSLois Curfman McInnes 
3839b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
384a86d99e1SLois Curfman McInnes 
38508405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
3869b94acceSBarry Smith @*/
38787828ca2SBarry Smith int SNESGetFunctionNorm(SNES snes,PetscScalar *fnorm)
3889b94acceSBarry Smith {
3893a40ed3dSBarry Smith   PetscFunctionBegin;
3904482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3914482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
3929b94acceSBarry Smith   *fnorm = snes->norm;
3933a40ed3dSBarry Smith   PetscFunctionReturn(0);
3949b94acceSBarry Smith }
39574679c65SBarry Smith 
3964a2ae208SSatish Balay #undef __FUNCT__
3974a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps"
3989b94acceSBarry Smith /*@
3999b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
4009b94acceSBarry Smith    attempted by the nonlinear solver.
4019b94acceSBarry Smith 
402c7afd0dbSLois Curfman McInnes    Not Collective
403c7afd0dbSLois Curfman McInnes 
4049b94acceSBarry Smith    Input Parameter:
4059b94acceSBarry Smith .  snes - SNES context
4069b94acceSBarry Smith 
4079b94acceSBarry Smith    Output Parameter:
4089b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4099b94acceSBarry Smith 
410c96a6f78SLois Curfman McInnes    Notes:
411c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
412c96a6f78SLois Curfman McInnes 
41336851e7fSLois Curfman McInnes    Level: intermediate
41436851e7fSLois Curfman McInnes 
4159b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4169b94acceSBarry Smith @*/
4179b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
4189b94acceSBarry Smith {
4193a40ed3dSBarry Smith   PetscFunctionBegin;
4204482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4214482741eSBarry Smith   PetscValidIntPointer(nfails,2);
42250ffb88aSMatthew Knepley   *nfails = snes->numFailures;
42350ffb88aSMatthew Knepley   PetscFunctionReturn(0);
42450ffb88aSMatthew Knepley }
42550ffb88aSMatthew Knepley 
42650ffb88aSMatthew Knepley #undef __FUNCT__
42750ffb88aSMatthew Knepley #define __FUNCT__ "SNESSetMaximumUnsuccessfulSteps"
42850ffb88aSMatthew Knepley /*@
42950ffb88aSMatthew Knepley    SNESSetMaximumUnsuccessfulSteps - Sets the maximum number of unsuccessful steps
43050ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
43150ffb88aSMatthew Knepley 
43250ffb88aSMatthew Knepley    Not Collective
43350ffb88aSMatthew Knepley 
43450ffb88aSMatthew Knepley    Input Parameters:
43550ffb88aSMatthew Knepley +  snes     - SNES context
43650ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
43750ffb88aSMatthew Knepley 
43850ffb88aSMatthew Knepley    Level: intermediate
43950ffb88aSMatthew Knepley 
44050ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
44150ffb88aSMatthew Knepley @*/
44250ffb88aSMatthew Knepley int SNESSetMaximumUnsuccessfulSteps(SNES snes, int maxFails)
44350ffb88aSMatthew Knepley {
44450ffb88aSMatthew Knepley   PetscFunctionBegin;
4454482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
44650ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
44750ffb88aSMatthew Knepley   PetscFunctionReturn(0);
44850ffb88aSMatthew Knepley }
44950ffb88aSMatthew Knepley 
45050ffb88aSMatthew Knepley #undef __FUNCT__
45150ffb88aSMatthew Knepley #define __FUNCT__ "SNESGetMaximumUnsuccessfulSteps"
45250ffb88aSMatthew Knepley /*@
45350ffb88aSMatthew Knepley    SNESGetMaximumUnsuccessfulSteps - Gets the maximum number of unsuccessful steps
45450ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
45550ffb88aSMatthew Knepley 
45650ffb88aSMatthew Knepley    Not Collective
45750ffb88aSMatthew Knepley 
45850ffb88aSMatthew Knepley    Input Parameter:
45950ffb88aSMatthew Knepley .  snes     - SNES context
46050ffb88aSMatthew Knepley 
46150ffb88aSMatthew Knepley    Output Parameter:
46250ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
46350ffb88aSMatthew Knepley 
46450ffb88aSMatthew Knepley    Level: intermediate
46550ffb88aSMatthew Knepley 
46650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
46750ffb88aSMatthew Knepley @*/
46850ffb88aSMatthew Knepley int SNESGetMaximumUnsuccessfulSteps(SNES snes, int *maxFails)
46950ffb88aSMatthew Knepley {
47050ffb88aSMatthew Knepley   PetscFunctionBegin;
4714482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4724482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
47350ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
4743a40ed3dSBarry Smith   PetscFunctionReturn(0);
4759b94acceSBarry Smith }
476a847f771SSatish Balay 
4774a2ae208SSatish Balay #undef __FUNCT__
4784a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations"
479c96a6f78SLois Curfman McInnes /*@
480c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
481c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
482c96a6f78SLois Curfman McInnes 
483c7afd0dbSLois Curfman McInnes    Not Collective
484c7afd0dbSLois Curfman McInnes 
485c96a6f78SLois Curfman McInnes    Input Parameter:
486c96a6f78SLois Curfman McInnes .  snes - SNES context
487c96a6f78SLois Curfman McInnes 
488c96a6f78SLois Curfman McInnes    Output Parameter:
489c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
490c96a6f78SLois Curfman McInnes 
491c96a6f78SLois Curfman McInnes    Notes:
492c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
493c96a6f78SLois Curfman McInnes 
49436851e7fSLois Curfman McInnes    Level: intermediate
49536851e7fSLois Curfman McInnes 
496c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
497c96a6f78SLois Curfman McInnes @*/
49886bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
499c96a6f78SLois Curfman McInnes {
5003a40ed3dSBarry Smith   PetscFunctionBegin;
5014482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5024482741eSBarry Smith   PetscValidIntPointer(lits,2);
503c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5043a40ed3dSBarry Smith   PetscFunctionReturn(0);
505c96a6f78SLois Curfman McInnes }
506c96a6f78SLois Curfman McInnes 
5074a2ae208SSatish Balay #undef __FUNCT__
50894b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
5099b94acceSBarry Smith /*@C
51094b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
5119b94acceSBarry Smith 
51294b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
513c7afd0dbSLois Curfman McInnes 
5149b94acceSBarry Smith    Input Parameter:
5159b94acceSBarry Smith .  snes - the SNES context
5169b94acceSBarry Smith 
5179b94acceSBarry Smith    Output Parameter:
51894b7f48cSBarry Smith .  ksp - the KSP context
5199b94acceSBarry Smith 
5209b94acceSBarry Smith    Notes:
52194b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
5229b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
5239b94acceSBarry Smith    KSP and PC contexts as well.
5249b94acceSBarry Smith 
52536851e7fSLois Curfman McInnes    Level: beginner
52636851e7fSLois Curfman McInnes 
52794b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
5289b94acceSBarry Smith 
52994b7f48cSBarry Smith .seealso: KSPGetPC()
5309b94acceSBarry Smith @*/
53194b7f48cSBarry Smith int SNESGetKSP(SNES snes,KSP *ksp)
5329b94acceSBarry Smith {
5333a40ed3dSBarry Smith   PetscFunctionBegin;
5344482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5354482741eSBarry Smith   PetscValidPointer(ksp,2);
53694b7f48cSBarry Smith   *ksp = snes->ksp;
5373a40ed3dSBarry Smith   PetscFunctionReturn(0);
5389b94acceSBarry Smith }
53982bf6240SBarry Smith 
5404a2ae208SSatish Balay #undef __FUNCT__
5414a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
542454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj)
543e24b481bSBarry Smith {
544aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
545454a90a3SBarry Smith   SNES          v = (SNES) obj;
546e24b481bSBarry Smith   int          ierr;
54743d6d2cbSBarry Smith #endif
548e24b481bSBarry Smith 
549e24b481bSBarry Smith   PetscFunctionBegin;
550e24b481bSBarry Smith 
55143d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
552e24b481bSBarry Smith   /* if it is already published then return */
553e24b481bSBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
554e24b481bSBarry Smith 
555454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
556e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
557e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
558e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
559e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
560454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
561433994e6SBarry Smith #endif
562e24b481bSBarry Smith   PetscFunctionReturn(0);
563e24b481bSBarry Smith }
564e24b481bSBarry Smith 
5659b94acceSBarry Smith /* -----------------------------------------------------------*/
5664a2ae208SSatish Balay #undef __FUNCT__
5674a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
5689b94acceSBarry Smith /*@C
5699b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
5709b94acceSBarry Smith 
571c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
572c7afd0dbSLois Curfman McInnes 
573c7afd0dbSLois Curfman McInnes    Input Parameters:
574c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
5759b94acceSBarry Smith 
5769b94acceSBarry Smith    Output Parameter:
5779b94acceSBarry Smith .  outsnes - the new SNES context
5789b94acceSBarry Smith 
579c7afd0dbSLois Curfman McInnes    Options Database Keys:
580c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
581c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
582c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
583c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
584c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
585c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
586c1f60f51SBarry Smith 
58736851e7fSLois Curfman McInnes    Level: beginner
58836851e7fSLois Curfman McInnes 
5899b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
5909b94acceSBarry Smith 
5914b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES
5929b94acceSBarry Smith @*/
5934b27c08aSLois Curfman McInnes int SNESCreate(MPI_Comm comm,SNES *outsnes)
5949b94acceSBarry Smith {
5959b94acceSBarry Smith   int                 ierr;
5969b94acceSBarry Smith   SNES                snes;
5979b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
59837fcc0dbSBarry Smith 
5993a40ed3dSBarry Smith   PetscFunctionBegin;
6004482741eSBarry Smith   PetscValidPointer(outsnes,1);
6018ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
6028ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
6038ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);                                                               CHKERRQ(ierr);
6048ba1e511SMatthew Knepley #endif
6058ba1e511SMatthew Knepley 
6063f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
607b0a32e0cSBarry Smith   PetscLogObjectCreate(snes);
608e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6099b94acceSBarry Smith   snes->max_its           = 50;
6109750a799SBarry Smith   snes->max_funcs	  = 10000;
6119b94acceSBarry Smith   snes->norm		  = 0.0;
612b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
613b4874afaSBarry Smith   snes->ttol              = 0.0;
614b4874afaSBarry Smith   snes->atol		  = 1.e-50;
6159b94acceSBarry Smith   snes->xtol		  = 1.e-8;
6164b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
6179b94acceSBarry Smith   snes->nfuncs            = 0;
61850ffb88aSMatthew Knepley   snes->numFailures       = 0;
61950ffb88aSMatthew Knepley   snes->maxFailures       = 1;
6207a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
621639f9d9dSBarry Smith   snes->numbermonitors    = 0;
6229b94acceSBarry Smith   snes->data              = 0;
6239b94acceSBarry Smith   snes->view              = 0;
62482bf6240SBarry Smith   snes->setupcalled       = 0;
625186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
6266f24a144SLois Curfman McInnes   snes->vwork             = 0;
6276f24a144SLois Curfman McInnes   snes->nwork             = 0;
628758f92a0SBarry Smith   snes->conv_hist_len     = 0;
629758f92a0SBarry Smith   snes->conv_hist_max     = 0;
630758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
631758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
632758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
633184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
6349b94acceSBarry Smith 
6359b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
636b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
637b0a32e0cSBarry Smith   PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
6389b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
6399b94acceSBarry Smith   kctx->version     = 2;
6409b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
6419b94acceSBarry Smith                              this was too large for some test cases */
6429b94acceSBarry Smith   kctx->rtol_last   = 0;
6439b94acceSBarry Smith   kctx->rtol_max    = .9;
6449b94acceSBarry Smith   kctx->gamma       = 1.0;
6459b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
6469b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
6479b94acceSBarry Smith   kctx->threshold   = .1;
6489b94acceSBarry Smith   kctx->lresid_last = 0;
6499b94acceSBarry Smith   kctx->norm_last   = 0;
6509b94acceSBarry Smith 
65194b7f48cSBarry Smith   ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr);
65294b7f48cSBarry Smith   PetscLogObjectParent(snes,snes->ksp)
6535334005bSBarry Smith 
6549b94acceSBarry Smith   *outsnes = snes;
65500036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
6563a40ed3dSBarry Smith   PetscFunctionReturn(0);
6579b94acceSBarry Smith }
6589b94acceSBarry Smith 
6599b94acceSBarry Smith /* --------------------------------------------------------------- */
6604a2ae208SSatish Balay #undef __FUNCT__
6614a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
6629b94acceSBarry Smith /*@C
6639b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
6649b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
6659b94acceSBarry Smith    equations.
6669b94acceSBarry Smith 
667fee21e36SBarry Smith    Collective on SNES
668fee21e36SBarry Smith 
669c7afd0dbSLois Curfman McInnes    Input Parameters:
670c7afd0dbSLois Curfman McInnes +  snes - the SNES context
671c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
672c7afd0dbSLois Curfman McInnes .  r - vector to store function value
673c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
674c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
6759b94acceSBarry Smith 
676c7afd0dbSLois Curfman McInnes    Calling sequence of func:
6778d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
678c7afd0dbSLois Curfman McInnes 
679313e4042SLois Curfman McInnes .  f - function vector
680c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
6819b94acceSBarry Smith 
6829b94acceSBarry Smith    Notes:
6839b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
6849b94acceSBarry Smith $      f'(x) x = -f(x),
685c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
6869b94acceSBarry Smith 
68736851e7fSLois Curfman McInnes    Level: beginner
68836851e7fSLois Curfman McInnes 
6899b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
6909b94acceSBarry Smith 
691a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
6929b94acceSBarry Smith @*/
6935334005bSBarry Smith int SNESSetFunction(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
6949b94acceSBarry Smith {
6953a40ed3dSBarry Smith   PetscFunctionBegin;
6964482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6974482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
698*c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
699184914b5SBarry Smith 
7009b94acceSBarry Smith   snes->computefunction     = func;
7019b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7029b94acceSBarry Smith   snes->funP                = ctx;
7033a40ed3dSBarry Smith   PetscFunctionReturn(0);
7049b94acceSBarry Smith }
7059b94acceSBarry Smith 
7064a2ae208SSatish Balay #undef __FUNCT__
7074a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
7089b94acceSBarry Smith /*@
70936851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
7109b94acceSBarry Smith                          SNESSetFunction().
7119b94acceSBarry Smith 
712c7afd0dbSLois Curfman McInnes    Collective on SNES
713c7afd0dbSLois Curfman McInnes 
7149b94acceSBarry Smith    Input Parameters:
715c7afd0dbSLois Curfman McInnes +  snes - the SNES context
716c7afd0dbSLois Curfman McInnes -  x - input vector
7179b94acceSBarry Smith 
7189b94acceSBarry Smith    Output Parameter:
7193638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
7209b94acceSBarry Smith 
7211bffabb2SLois Curfman McInnes    Notes:
72236851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
72336851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
72436851e7fSLois Curfman McInnes    themselves.
72536851e7fSLois Curfman McInnes 
72636851e7fSLois Curfman McInnes    Level: developer
72736851e7fSLois Curfman McInnes 
7289b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
7299b94acceSBarry Smith 
730a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
7319b94acceSBarry Smith @*/
7329b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x,Vec y)
7339b94acceSBarry Smith {
7349b94acceSBarry Smith   int    ierr;
7359b94acceSBarry Smith 
7363a40ed3dSBarry Smith   PetscFunctionBegin;
7374482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7384482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
7394482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
740*c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
741*c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
742184914b5SBarry Smith 
743d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
744d64ed03dSBarry Smith   PetscStackPush("SNES user function");
7459b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
746d64ed03dSBarry Smith   PetscStackPop;
747ae3c334cSLois Curfman McInnes   snes->nfuncs++;
748d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
7493a40ed3dSBarry Smith   PetscFunctionReturn(0);
7509b94acceSBarry Smith }
7519b94acceSBarry Smith 
7524a2ae208SSatish Balay #undef __FUNCT__
7534a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
75462fef451SLois Curfman McInnes /*@
75562fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
75662fef451SLois Curfman McInnes    set with SNESSetJacobian().
75762fef451SLois Curfman McInnes 
758c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
759c7afd0dbSLois Curfman McInnes 
76062fef451SLois Curfman McInnes    Input Parameters:
761c7afd0dbSLois Curfman McInnes +  snes - the SNES context
762c7afd0dbSLois Curfman McInnes -  x - input vector
76362fef451SLois Curfman McInnes 
76462fef451SLois Curfman McInnes    Output Parameters:
765c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
76662fef451SLois Curfman McInnes .  B - optional preconditioning matrix
767c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
768fee21e36SBarry Smith 
76962fef451SLois Curfman McInnes    Notes:
77062fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
77162fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
77262fef451SLois Curfman McInnes 
77394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
774dc5a77f8SLois Curfman McInnes    flag parameter.
77562fef451SLois Curfman McInnes 
77636851e7fSLois Curfman McInnes    Level: developer
77736851e7fSLois Curfman McInnes 
77862fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
77962fef451SLois Curfman McInnes 
78094b7f48cSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators()
78162fef451SLois Curfman McInnes @*/
7829b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
7839b94acceSBarry Smith {
7849b94acceSBarry Smith   int    ierr;
7853a40ed3dSBarry Smith 
7863a40ed3dSBarry Smith   PetscFunctionBegin;
7874482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7884482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
7894482741eSBarry Smith   PetscValidPointer(flg,5);
790*c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
7913a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
792d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
793c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
794d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
7959b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
796d64ed03dSBarry Smith   PetscStackPop;
797d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
7986d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
7994482741eSBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
8004482741eSBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE,4);
8013a40ed3dSBarry Smith   PetscFunctionReturn(0);
8029b94acceSBarry Smith }
8039b94acceSBarry Smith 
8044a2ae208SSatish Balay #undef __FUNCT__
8054a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
8069b94acceSBarry Smith /*@C
8079b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
808044dda88SLois Curfman McInnes    location to store the matrix.
8099b94acceSBarry Smith 
810c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
811c7afd0dbSLois Curfman McInnes 
8129b94acceSBarry Smith    Input Parameters:
813c7afd0dbSLois Curfman McInnes +  snes - the SNES context
8149b94acceSBarry Smith .  A - Jacobian matrix
8159b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
8169b94acceSBarry Smith .  func - Jacobian evaluation routine
817c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
8182cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
8199b94acceSBarry Smith 
8209b94acceSBarry Smith    Calling sequence of func:
8218d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
8229b94acceSBarry Smith 
823c7afd0dbSLois Curfman McInnes +  x - input vector
8249b94acceSBarry Smith .  A - Jacobian matrix
8259b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
826ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
82794b7f48cSBarry Smith    structure (same as flag in KSPSetOperators())
828c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
8299b94acceSBarry Smith 
8309b94acceSBarry Smith    Notes:
83194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
8322cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
833ac21db08SLois Curfman McInnes 
834ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
8359b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
8369b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
8379b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
8389b94acceSBarry Smith    throughout the global iterations.
8399b94acceSBarry Smith 
84036851e7fSLois Curfman McInnes    Level: beginner
84136851e7fSLois Curfman McInnes 
8429b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
8439b94acceSBarry Smith 
84494b7f48cSBarry Smith .seealso: KSPSetOperators(), SNESSetFunction()
8459b94acceSBarry Smith @*/
846454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
8479b94acceSBarry Smith {
8483a7fca6bSBarry Smith   int ierr;
8493a7fca6bSBarry Smith 
8503a40ed3dSBarry Smith   PetscFunctionBegin;
8514482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8524482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
8534482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
854*c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
855*c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
8563a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
8573a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
8583a7fca6bSBarry Smith   if (A) {
8593a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
8609b94acceSBarry Smith     snes->jacobian = A;
8613a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8623a7fca6bSBarry Smith   }
8633a7fca6bSBarry Smith   if (B) {
8643a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
8659b94acceSBarry Smith     snes->jacobian_pre = B;
8663a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8673a7fca6bSBarry Smith   }
8683a40ed3dSBarry Smith   PetscFunctionReturn(0);
8699b94acceSBarry Smith }
87062fef451SLois Curfman McInnes 
8714a2ae208SSatish Balay #undef __FUNCT__
8724a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
873c2aafc4cSSatish Balay /*@C
874b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
875b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
876b4fd4287SBarry Smith 
877c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
878c7afd0dbSLois Curfman McInnes 
879b4fd4287SBarry Smith    Input Parameter:
880b4fd4287SBarry Smith .  snes - the nonlinear solver context
881b4fd4287SBarry Smith 
882b4fd4287SBarry Smith    Output Parameters:
883c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
884b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
88500036973SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
88600036973SBarry Smith -  func - location to put Jacobian function (or PETSC_NULL)
887fee21e36SBarry Smith 
88836851e7fSLois Curfman McInnes    Level: advanced
88936851e7fSLois Curfman McInnes 
890b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
891b4fd4287SBarry Smith @*/
89200036973SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx,int (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*))
893b4fd4287SBarry Smith {
8943a40ed3dSBarry Smith   PetscFunctionBegin;
8954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
896b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
897b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
898b4fd4287SBarry Smith   if (ctx)  *ctx  = snes->jacP;
89900036973SBarry Smith   if (func) *func = snes->computejacobian;
9003a40ed3dSBarry Smith   PetscFunctionReturn(0);
901b4fd4287SBarry Smith }
902b4fd4287SBarry Smith 
9039b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
90445fc7adcSBarry Smith extern int SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
9059b94acceSBarry Smith 
9064a2ae208SSatish Balay #undef __FUNCT__
9074a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
9089b94acceSBarry Smith /*@
9099b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
910272ac6f2SLois Curfman McInnes    of a nonlinear solver.
9119b94acceSBarry Smith 
912fee21e36SBarry Smith    Collective on SNES
913fee21e36SBarry Smith 
914c7afd0dbSLois Curfman McInnes    Input Parameters:
915c7afd0dbSLois Curfman McInnes +  snes - the SNES context
916c7afd0dbSLois Curfman McInnes -  x - the solution vector
917c7afd0dbSLois Curfman McInnes 
918272ac6f2SLois Curfman McInnes    Notes:
919272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
920272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
921272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
922272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
923272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
924272ac6f2SLois Curfman McInnes 
92536851e7fSLois Curfman McInnes    Level: advanced
92636851e7fSLois Curfman McInnes 
9279b94acceSBarry Smith .keywords: SNES, nonlinear, setup
9289b94acceSBarry Smith 
9299b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
9309b94acceSBarry Smith @*/
9318ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
9329b94acceSBarry Smith {
933f1af5d2fSBarry Smith   int        ierr;
9344b27c08aSLois Curfman McInnes   PetscTruth flg, iseqtr;
9353a40ed3dSBarry Smith 
9363a40ed3dSBarry Smith   PetscFunctionBegin;
9374482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9384482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
939*c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
9408ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
9419b94acceSBarry Smith 
942b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
943c1f60f51SBarry Smith   /*
944c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
945dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
946c1f60f51SBarry Smith   */
947c1f60f51SBarry Smith   if (flg) {
948c1f60f51SBarry Smith     Mat J;
9495a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
9505a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
9513a7fca6bSBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator routines\n");
9523a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
9533a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
954c1f60f51SBarry Smith   }
95545fc7adcSBarry Smith 
956b4014bb2SMatthew Knepley #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE)
95745fc7adcSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
95845fc7adcSBarry Smith   if (flg) {
95945fc7adcSBarry Smith     Mat J;
96045fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
96145fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
96245fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
96345fc7adcSBarry Smith   }
96432a4b47aSMatthew Knepley #endif
96545fc7adcSBarry Smith 
966b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
967c1f60f51SBarry Smith   /*
968dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
969c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
970c1f60f51SBarry Smith    */
97131615d04SLois Curfman McInnes   if (flg) {
972272ac6f2SLois Curfman McInnes     Mat  J;
973b5d62d44SBarry Smith     KSP ksp;
97494b7f48cSBarry Smith     PC   pc;
975f3ef73ceSBarry Smith 
9765a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
9773a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
97893b98531SBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator and preconditioner routines\n");
9793a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
9803a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
9813a7fca6bSBarry Smith 
982f3ef73ceSBarry Smith     /* force no preconditioner */
98394b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
984b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
985a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
986a9815358SBarry Smith     if (!flg) {
987f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
988272ac6f2SLois Curfman McInnes     }
989a9815358SBarry Smith   }
990f3ef73ceSBarry Smith 
99129bbc08cSBarry Smith   if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
99229bbc08cSBarry Smith   if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
99329bbc08cSBarry Smith   if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
994a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
99529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
996a8c6a408SBarry Smith   }
997a703fe33SLois Curfman McInnes 
998a703fe33SLois Curfman McInnes   /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
9994b27c08aSLois Curfman McInnes   ierr = PetscTypeCompare((PetscObject)snes,SNESTR,&iseqtr);CHKERRQ(ierr);
10006831982aSBarry Smith   if (snes->ksp_ewconv && !iseqtr) {
100194b7f48cSBarry Smith     KSP ksp;
100294b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1003186905e3SBarry Smith     ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
1004a703fe33SLois Curfman McInnes   }
10054b27c08aSLois Curfman McInnes 
1006a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
100782bf6240SBarry Smith   snes->setupcalled = 1;
10083a40ed3dSBarry Smith   PetscFunctionReturn(0);
10099b94acceSBarry Smith }
10109b94acceSBarry Smith 
10114a2ae208SSatish Balay #undef __FUNCT__
10124a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
10139b94acceSBarry Smith /*@C
10149b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
10159b94acceSBarry Smith    with SNESCreate().
10169b94acceSBarry Smith 
1017c7afd0dbSLois Curfman McInnes    Collective on SNES
1018c7afd0dbSLois Curfman McInnes 
10199b94acceSBarry Smith    Input Parameter:
10209b94acceSBarry Smith .  snes - the SNES context
10219b94acceSBarry Smith 
102236851e7fSLois Curfman McInnes    Level: beginner
102336851e7fSLois Curfman McInnes 
10249b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
10259b94acceSBarry Smith 
102663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
10279b94acceSBarry Smith @*/
10289b94acceSBarry Smith int SNESDestroy(SNES snes)
10299b94acceSBarry Smith {
1030b8a78c4aSBarry Smith   int i,ierr;
10313a40ed3dSBarry Smith 
10323a40ed3dSBarry Smith   PetscFunctionBegin;
10334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10343a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1035d4bb536fSBarry Smith 
1036be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
10370f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1038be0abb6dSBarry Smith 
1039e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1040606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
10413a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
10423a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
104394b7f48cSBarry Smith   ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);
1044522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1045b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1046b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1047b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1048b8a78c4aSBarry Smith     }
1049b8a78c4aSBarry Smith   }
1050b0a32e0cSBarry Smith   PetscLogObjectDestroy((PetscObject)snes);
10510452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
10523a40ed3dSBarry Smith   PetscFunctionReturn(0);
10539b94acceSBarry Smith }
10549b94acceSBarry Smith 
10559b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
10569b94acceSBarry Smith 
10574a2ae208SSatish Balay #undef __FUNCT__
10584a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
10599b94acceSBarry Smith /*@
1060d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
10619b94acceSBarry Smith 
1062c7afd0dbSLois Curfman McInnes    Collective on SNES
1063c7afd0dbSLois Curfman McInnes 
10649b94acceSBarry Smith    Input Parameters:
1065c7afd0dbSLois Curfman McInnes +  snes - the SNES context
106633174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
106733174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
106833174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
106933174efeSLois Curfman McInnes            of the change in the solution between steps
107033174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1071c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1072fee21e36SBarry Smith 
107333174efeSLois Curfman McInnes    Options Database Keys:
1074c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1075c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1076c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1077c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1078c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
10799b94acceSBarry Smith 
1080d7a720efSLois Curfman McInnes    Notes:
10819b94acceSBarry Smith    The default maximum number of iterations is 50.
10829b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
10839b94acceSBarry Smith 
108436851e7fSLois Curfman McInnes    Level: intermediate
108536851e7fSLois Curfman McInnes 
108633174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
10879b94acceSBarry Smith 
1088d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
10899b94acceSBarry Smith @*/
1090329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf)
10919b94acceSBarry Smith {
10923a40ed3dSBarry Smith   PetscFunctionBegin;
10934482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1094d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1095d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1096d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1097d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1098d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
10993a40ed3dSBarry Smith   PetscFunctionReturn(0);
11009b94acceSBarry Smith }
11019b94acceSBarry Smith 
11024a2ae208SSatish Balay #undef __FUNCT__
11034a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
11049b94acceSBarry Smith /*@
110533174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
110633174efeSLois Curfman McInnes 
1107c7afd0dbSLois Curfman McInnes    Not Collective
1108c7afd0dbSLois Curfman McInnes 
110933174efeSLois Curfman McInnes    Input Parameters:
1110c7afd0dbSLois Curfman McInnes +  snes - the SNES context
111133174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
111233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
111333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
111433174efeSLois Curfman McInnes            of the change in the solution between steps
111533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1116c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1117fee21e36SBarry Smith 
111833174efeSLois Curfman McInnes    Notes:
111933174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
112033174efeSLois Curfman McInnes 
112136851e7fSLois Curfman McInnes    Level: intermediate
112236851e7fSLois Curfman McInnes 
112333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
112433174efeSLois Curfman McInnes 
112533174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
112633174efeSLois Curfman McInnes @*/
1127329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf)
112833174efeSLois Curfman McInnes {
11293a40ed3dSBarry Smith   PetscFunctionBegin;
11304482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
113133174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
113233174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
113333174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
113433174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
113533174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
11363a40ed3dSBarry Smith   PetscFunctionReturn(0);
113733174efeSLois Curfman McInnes }
113833174efeSLois Curfman McInnes 
11394a2ae208SSatish Balay #undef __FUNCT__
11404a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
114133174efeSLois Curfman McInnes /*@
11429b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
11439b94acceSBarry Smith 
1144fee21e36SBarry Smith    Collective on SNES
1145fee21e36SBarry Smith 
1146c7afd0dbSLois Curfman McInnes    Input Parameters:
1147c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1148c7afd0dbSLois Curfman McInnes -  tol - tolerance
1149c7afd0dbSLois Curfman McInnes 
11509b94acceSBarry Smith    Options Database Key:
1151c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
11529b94acceSBarry Smith 
115336851e7fSLois Curfman McInnes    Level: intermediate
115436851e7fSLois Curfman McInnes 
11559b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
11569b94acceSBarry Smith 
1157d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
11589b94acceSBarry Smith @*/
1159329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
11609b94acceSBarry Smith {
11613a40ed3dSBarry Smith   PetscFunctionBegin;
11624482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11639b94acceSBarry Smith   snes->deltatol = tol;
11643a40ed3dSBarry Smith   PetscFunctionReturn(0);
11659b94acceSBarry Smith }
11669b94acceSBarry Smith 
1167df9fa365SBarry Smith /*
1168df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1169df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1170df9fa365SBarry Smith    macros instead of functions
1171df9fa365SBarry Smith */
11724a2ae208SSatish Balay #undef __FUNCT__
11734a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitor"
1174329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx)
1175ce1608b8SBarry Smith {
1176ce1608b8SBarry Smith   int ierr;
1177ce1608b8SBarry Smith 
1178ce1608b8SBarry Smith   PetscFunctionBegin;
11794482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1180ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1181ce1608b8SBarry Smith   PetscFunctionReturn(0);
1182ce1608b8SBarry Smith }
1183ce1608b8SBarry Smith 
11844a2ae208SSatish Balay #undef __FUNCT__
11854a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorCreate"
11861836bdbcSSatish Balay int SNESLGMonitorCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1187df9fa365SBarry Smith {
1188df9fa365SBarry Smith   int ierr;
1189df9fa365SBarry Smith 
1190df9fa365SBarry Smith   PetscFunctionBegin;
1191df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1192df9fa365SBarry Smith   PetscFunctionReturn(0);
1193df9fa365SBarry Smith }
1194df9fa365SBarry Smith 
11954a2ae208SSatish Balay #undef __FUNCT__
11964a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorDestroy"
1197b0a32e0cSBarry Smith int SNESLGMonitorDestroy(PetscDrawLG draw)
1198df9fa365SBarry Smith {
1199df9fa365SBarry Smith   int ierr;
1200df9fa365SBarry Smith 
1201df9fa365SBarry Smith   PetscFunctionBegin;
1202df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1203df9fa365SBarry Smith   PetscFunctionReturn(0);
1204df9fa365SBarry Smith }
1205df9fa365SBarry Smith 
12069b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
12079b94acceSBarry Smith 
12084a2ae208SSatish Balay #undef __FUNCT__
12094a2ae208SSatish Balay #define __FUNCT__ "SNESSetMonitor"
12109b94acceSBarry Smith /*@C
12115cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
12129b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
12139b94acceSBarry Smith    progress.
12149b94acceSBarry Smith 
1215fee21e36SBarry Smith    Collective on SNES
1216fee21e36SBarry Smith 
1217c7afd0dbSLois Curfman McInnes    Input Parameters:
1218c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1219c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1220b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1221b3006f0bSLois Curfman McInnes           monitor routine (use PETSC_NULL if no context is desitre)
1222b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1223b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
12249b94acceSBarry Smith 
1225c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1226329f5518SBarry Smith $     int func(SNES snes,int its, PetscReal norm,void *mctx)
1227c7afd0dbSLois Curfman McInnes 
1228c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1229c7afd0dbSLois Curfman McInnes .    its - iteration number
1230c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
123140a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
12329b94acceSBarry Smith 
12339665c990SLois Curfman McInnes    Options Database Keys:
1234c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1235c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1236c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1237c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1238c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1239c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1240c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1241c7afd0dbSLois Curfman McInnes                             the options database.
12429665c990SLois Curfman McInnes 
1243639f9d9dSBarry Smith    Notes:
12446bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
12456bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
12466bc08f3fSLois Curfman McInnes    order in which they were set.
1247639f9d9dSBarry Smith 
124836851e7fSLois Curfman McInnes    Level: intermediate
124936851e7fSLois Curfman McInnes 
12509b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
12519b94acceSBarry Smith 
12525cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
12539b94acceSBarry Smith @*/
1254329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *))
12559b94acceSBarry Smith {
12563a40ed3dSBarry Smith   PetscFunctionBegin;
12574482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1258639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
125929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1260639f9d9dSBarry Smith   }
1261639f9d9dSBarry Smith 
1262639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1263b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1264639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
12653a40ed3dSBarry Smith   PetscFunctionReturn(0);
12669b94acceSBarry Smith }
12679b94acceSBarry Smith 
12684a2ae208SSatish Balay #undef __FUNCT__
12694a2ae208SSatish Balay #define __FUNCT__ "SNESClearMonitor"
12705cd90555SBarry Smith /*@C
12715cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
12725cd90555SBarry Smith 
1273c7afd0dbSLois Curfman McInnes    Collective on SNES
1274c7afd0dbSLois Curfman McInnes 
12755cd90555SBarry Smith    Input Parameters:
12765cd90555SBarry Smith .  snes - the SNES context
12775cd90555SBarry Smith 
12781a480d89SAdministrator    Options Database Key:
1279c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1280c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1281c7afd0dbSLois Curfman McInnes     set via the options database
12825cd90555SBarry Smith 
12835cd90555SBarry Smith    Notes:
12845cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
12855cd90555SBarry Smith 
128636851e7fSLois Curfman McInnes    Level: intermediate
128736851e7fSLois Curfman McInnes 
12885cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
12895cd90555SBarry Smith 
12905cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
12915cd90555SBarry Smith @*/
12925cd90555SBarry Smith int SNESClearMonitor(SNES snes)
12935cd90555SBarry Smith {
12945cd90555SBarry Smith   PetscFunctionBegin;
12954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12965cd90555SBarry Smith   snes->numbermonitors = 0;
12975cd90555SBarry Smith   PetscFunctionReturn(0);
12985cd90555SBarry Smith }
12995cd90555SBarry Smith 
13004a2ae208SSatish Balay #undef __FUNCT__
13014a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
13029b94acceSBarry Smith /*@C
13039b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
13049b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
13059b94acceSBarry Smith 
1306fee21e36SBarry Smith    Collective on SNES
1307fee21e36SBarry Smith 
1308c7afd0dbSLois Curfman McInnes    Input Parameters:
1309c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1310c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1311c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1312c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
13139b94acceSBarry Smith 
1314c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1315329f5518SBarry Smith $     int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1316c7afd0dbSLois Curfman McInnes 
1317c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1318c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1319184914b5SBarry Smith .    reason - reason for convergence/divergence
1320c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
13214b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
13224b27c08aSLois Curfman McInnes -    f - 2-norm of function
13239b94acceSBarry Smith 
132436851e7fSLois Curfman McInnes    Level: advanced
132536851e7fSLois Curfman McInnes 
13269b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
13279b94acceSBarry Smith 
13284b27c08aSLois Curfman McInnes .seealso: SNESConverged_LS(), SNESConverged_TR()
13299b94acceSBarry Smith @*/
1330329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
13319b94acceSBarry Smith {
13323a40ed3dSBarry Smith   PetscFunctionBegin;
13334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13349b94acceSBarry Smith   (snes)->converged = func;
13359b94acceSBarry Smith   (snes)->cnvP      = cctx;
13363a40ed3dSBarry Smith   PetscFunctionReturn(0);
13379b94acceSBarry Smith }
13389b94acceSBarry Smith 
13394a2ae208SSatish Balay #undef __FUNCT__
13404a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
1341184914b5SBarry Smith /*@C
1342184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1343184914b5SBarry Smith 
1344184914b5SBarry Smith    Not Collective
1345184914b5SBarry Smith 
1346184914b5SBarry Smith    Input Parameter:
1347184914b5SBarry Smith .  snes - the SNES context
1348184914b5SBarry Smith 
1349184914b5SBarry Smith    Output Parameter:
1350e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1351184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1352184914b5SBarry Smith 
1353184914b5SBarry Smith    Level: intermediate
1354184914b5SBarry Smith 
1355184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1356184914b5SBarry Smith 
1357184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1358184914b5SBarry Smith 
13594b27c08aSLois Curfman McInnes .seealso: SNESSetConvergenceTest(), SNESConverged_LS(), SNESConverged_TR(), SNESConvergedReason
1360184914b5SBarry Smith @*/
1361184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1362184914b5SBarry Smith {
1363184914b5SBarry Smith   PetscFunctionBegin;
13644482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13654482741eSBarry Smith   PetscValidPointer(reason,2);
1366184914b5SBarry Smith   *reason = snes->reason;
1367184914b5SBarry Smith   PetscFunctionReturn(0);
1368184914b5SBarry Smith }
1369184914b5SBarry Smith 
13704a2ae208SSatish Balay #undef __FUNCT__
13714a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1372c9005455SLois Curfman McInnes /*@
1373c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1374c9005455SLois Curfman McInnes 
1375fee21e36SBarry Smith    Collective on SNES
1376fee21e36SBarry Smith 
1377c7afd0dbSLois Curfman McInnes    Input Parameters:
1378c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1379c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1380cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1381758f92a0SBarry Smith .  na  - size of a and its
138264731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1383758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1384c7afd0dbSLois Curfman McInnes 
1385c9005455SLois Curfman McInnes    Notes:
13864b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1387c9005455SLois Curfman McInnes    at each step.
1388c9005455SLois Curfman McInnes 
1389c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1390c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1391c9005455SLois Curfman McInnes    during the section of code that is being timed.
1392c9005455SLois Curfman McInnes 
139336851e7fSLois Curfman McInnes    Level: intermediate
139436851e7fSLois Curfman McInnes 
1395c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1396758f92a0SBarry Smith 
139708405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1398758f92a0SBarry Smith 
1399c9005455SLois Curfman McInnes @*/
14001836bdbcSSatish Balay int SNESSetConvergenceHistory(SNES snes,PetscReal a[],int *its,int na,PetscTruth reset)
1401c9005455SLois Curfman McInnes {
14023a40ed3dSBarry Smith   PetscFunctionBegin;
14034482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
14044482741eSBarry Smith   if (na) PetscValidScalarPointer(a,2);
1405c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1406758f92a0SBarry Smith   snes->conv_hist_its   = its;
1407758f92a0SBarry Smith   snes->conv_hist_max   = na;
1408758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1409758f92a0SBarry Smith   PetscFunctionReturn(0);
1410758f92a0SBarry Smith }
1411758f92a0SBarry Smith 
14124a2ae208SSatish Balay #undef __FUNCT__
14134a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
14140c4c9dddSBarry Smith /*@C
1415758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1416758f92a0SBarry Smith 
1417758f92a0SBarry Smith    Collective on SNES
1418758f92a0SBarry Smith 
1419758f92a0SBarry Smith    Input Parameter:
1420758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1421758f92a0SBarry Smith 
1422758f92a0SBarry Smith    Output Parameters:
1423758f92a0SBarry Smith .  a   - array to hold history
1424758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1425758f92a0SBarry Smith          negative if not converged) for each solve.
1426758f92a0SBarry Smith -  na  - size of a and its
1427758f92a0SBarry Smith 
1428758f92a0SBarry Smith    Notes:
1429758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1430758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1431758f92a0SBarry Smith 
1432758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1433758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1434758f92a0SBarry Smith    during the section of code that is being timed.
1435758f92a0SBarry Smith 
1436758f92a0SBarry Smith    Level: intermediate
1437758f92a0SBarry Smith 
1438758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1439758f92a0SBarry Smith 
1440758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1441758f92a0SBarry Smith 
1442758f92a0SBarry Smith @*/
14431836bdbcSSatish Balay int SNESGetConvergenceHistory(SNES snes,PetscReal *a[],int *its[],int *na)
1444758f92a0SBarry Smith {
1445758f92a0SBarry Smith   PetscFunctionBegin;
14464482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1447758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1448758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1449758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
14503a40ed3dSBarry Smith   PetscFunctionReturn(0);
1451c9005455SLois Curfman McInnes }
1452c9005455SLois Curfman McInnes 
1453e74ef692SMatthew Knepley #undef __FUNCT__
1454e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetRhsBC"
1455ac226902SBarry Smith /*@C
145676b2cf59SMatthew Knepley   SNESSetRhsBC - Sets the function which applies boundary conditions
145776b2cf59SMatthew Knepley   to the Rhs of each system.
145876b2cf59SMatthew Knepley 
145976b2cf59SMatthew Knepley   Collective on SNES
146076b2cf59SMatthew Knepley 
146176b2cf59SMatthew Knepley   Input Parameters:
146276b2cf59SMatthew Knepley . snes - The nonlinear solver context
146376b2cf59SMatthew Knepley . func - The function
146476b2cf59SMatthew Knepley 
146576b2cf59SMatthew Knepley   Calling sequence of func:
146676b2cf59SMatthew Knepley . func (SNES snes, Vec rhs, void *ctx);
146776b2cf59SMatthew Knepley 
146876b2cf59SMatthew Knepley . rhs - The current rhs vector
146976b2cf59SMatthew Knepley . ctx - The user-context
147076b2cf59SMatthew Knepley 
147176b2cf59SMatthew Knepley   Level: intermediate
147276b2cf59SMatthew Knepley 
147376b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
147476b2cf59SMatthew Knepley .seealso SNESDefaultRhsBC(), SNESSetSolutionBC(), SNESSetUpdate()
147576b2cf59SMatthew Knepley @*/
147676b2cf59SMatthew Knepley int SNESSetRhsBC(SNES snes, int (*func)(SNES, Vec, void *))
147776b2cf59SMatthew Knepley {
147876b2cf59SMatthew Knepley   PetscFunctionBegin;
14794482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
148076b2cf59SMatthew Knepley   snes->applyrhsbc = func;
148176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
148276b2cf59SMatthew Knepley }
148376b2cf59SMatthew Knepley 
1484e74ef692SMatthew Knepley #undef __FUNCT__
1485e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultRhsBC"
148676b2cf59SMatthew Knepley /*@
148776b2cf59SMatthew Knepley   SNESDefaultRhsBC - The default boundary condition function which does nothing.
148876b2cf59SMatthew Knepley 
148976b2cf59SMatthew Knepley   Not collective
149076b2cf59SMatthew Knepley 
149176b2cf59SMatthew Knepley   Input Parameters:
149276b2cf59SMatthew Knepley . snes - The nonlinear solver context
149376b2cf59SMatthew Knepley . rhs  - The Rhs
149476b2cf59SMatthew Knepley . ctx  - The user-context
149576b2cf59SMatthew Knepley 
149676b2cf59SMatthew Knepley   Level: beginner
149776b2cf59SMatthew Knepley 
149876b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
149976b2cf59SMatthew Knepley .seealso SNESSetRhsBC(), SNESDefaultSolutionBC(), SNESDefaultUpdate()
150076b2cf59SMatthew Knepley @*/
150176b2cf59SMatthew Knepley int SNESDefaultRhsBC(SNES snes, Vec rhs, void *ctx)
150276b2cf59SMatthew Knepley {
150376b2cf59SMatthew Knepley   PetscFunctionBegin;
150476b2cf59SMatthew Knepley   PetscFunctionReturn(0);
150576b2cf59SMatthew Knepley }
150676b2cf59SMatthew Knepley 
1507e74ef692SMatthew Knepley #undef __FUNCT__
1508e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetSolutionBC"
1509ac226902SBarry Smith /*@C
151076b2cf59SMatthew Knepley   SNESSetSolutionBC - Sets the function which applies boundary conditions
151176b2cf59SMatthew Knepley   to the solution of each system.
151276b2cf59SMatthew Knepley 
151376b2cf59SMatthew Knepley   Collective on SNES
151476b2cf59SMatthew Knepley 
151576b2cf59SMatthew Knepley   Input Parameters:
151676b2cf59SMatthew Knepley . snes - The nonlinear solver context
151776b2cf59SMatthew Knepley . func - The function
151876b2cf59SMatthew Knepley 
151976b2cf59SMatthew Knepley   Calling sequence of func:
15209d06fb6bSMatthew Knepley . func (SNES snes, Vec rsol, void *ctx);
152176b2cf59SMatthew Knepley 
152276b2cf59SMatthew Knepley . sol - The current solution vector
152376b2cf59SMatthew Knepley . ctx - The user-context
152476b2cf59SMatthew Knepley 
152576b2cf59SMatthew Knepley   Level: intermediate
152676b2cf59SMatthew Knepley 
152776b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
152876b2cf59SMatthew Knepley .seealso SNESDefaultSolutionBC(), SNESSetRhsBC(), SNESSetUpdate()
152976b2cf59SMatthew Knepley @*/
153076b2cf59SMatthew Knepley int SNESSetSolutionBC(SNES snes, int (*func)(SNES, Vec, void *))
153176b2cf59SMatthew Knepley {
153276b2cf59SMatthew Knepley   PetscFunctionBegin;
15334482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
153476b2cf59SMatthew Knepley   snes->applysolbc = func;
153576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
153676b2cf59SMatthew Knepley }
153776b2cf59SMatthew Knepley 
1538e74ef692SMatthew Knepley #undef __FUNCT__
1539e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultSolutionBC"
154076b2cf59SMatthew Knepley /*@
154176b2cf59SMatthew Knepley   SNESDefaultSolutionBC - The default boundary condition function which does nothing.
154276b2cf59SMatthew Knepley 
154376b2cf59SMatthew Knepley   Not collective
154476b2cf59SMatthew Knepley 
154576b2cf59SMatthew Knepley   Input Parameters:
154676b2cf59SMatthew Knepley . snes - The nonlinear solver context
154776b2cf59SMatthew Knepley . sol  - The solution
154876b2cf59SMatthew Knepley . ctx  - The user-context
154976b2cf59SMatthew Knepley 
155076b2cf59SMatthew Knepley   Level: beginner
155176b2cf59SMatthew Knepley 
155276b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
155376b2cf59SMatthew Knepley .seealso SNESSetSolutionBC(), SNESDefaultRhsBC(), SNESDefaultUpdate()
155476b2cf59SMatthew Knepley @*/
155576b2cf59SMatthew Knepley int SNESDefaultSolutionBC(SNES snes, Vec sol, void *ctx)
155676b2cf59SMatthew Knepley {
155776b2cf59SMatthew Knepley   PetscFunctionBegin;
155876b2cf59SMatthew Knepley   PetscFunctionReturn(0);
155976b2cf59SMatthew Knepley }
156076b2cf59SMatthew Knepley 
1561e74ef692SMatthew Knepley #undef __FUNCT__
1562e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1563ac226902SBarry Smith /*@C
156476b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
156576b2cf59SMatthew Knepley   at the beginning of every step of the iteration.
156676b2cf59SMatthew Knepley 
156776b2cf59SMatthew Knepley   Collective on SNES
156876b2cf59SMatthew Knepley 
156976b2cf59SMatthew Knepley   Input Parameters:
157076b2cf59SMatthew Knepley . snes - The nonlinear solver context
157176b2cf59SMatthew Knepley . func - The function
157276b2cf59SMatthew Knepley 
157376b2cf59SMatthew Knepley   Calling sequence of func:
157476b2cf59SMatthew Knepley . func (TS ts, int step);
157576b2cf59SMatthew Knepley 
157676b2cf59SMatthew Knepley . step - The current step of the iteration
157776b2cf59SMatthew Knepley 
157876b2cf59SMatthew Knepley   Level: intermediate
157976b2cf59SMatthew Knepley 
158076b2cf59SMatthew Knepley .keywords: SNES, update
158176b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
158276b2cf59SMatthew Knepley @*/
158376b2cf59SMatthew Knepley int SNESSetUpdate(SNES snes, int (*func)(SNES, int))
158476b2cf59SMatthew Knepley {
158576b2cf59SMatthew Knepley   PetscFunctionBegin;
15864482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
158776b2cf59SMatthew Knepley   snes->update = func;
158876b2cf59SMatthew Knepley   PetscFunctionReturn(0);
158976b2cf59SMatthew Knepley }
159076b2cf59SMatthew Knepley 
1591e74ef692SMatthew Knepley #undef __FUNCT__
1592e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
159376b2cf59SMatthew Knepley /*@
159476b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
159576b2cf59SMatthew Knepley 
159676b2cf59SMatthew Knepley   Not collective
159776b2cf59SMatthew Knepley 
159876b2cf59SMatthew Knepley   Input Parameters:
159976b2cf59SMatthew Knepley . snes - The nonlinear solver context
160076b2cf59SMatthew Knepley . step - The current step of the iteration
160176b2cf59SMatthew Knepley 
1602205452f4SMatthew Knepley   Level: intermediate
1603205452f4SMatthew Knepley 
160476b2cf59SMatthew Knepley .keywords: SNES, update
160576b2cf59SMatthew Knepley .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultSolutionBC()
160676b2cf59SMatthew Knepley @*/
160776b2cf59SMatthew Knepley int SNESDefaultUpdate(SNES snes, int step)
160876b2cf59SMatthew Knepley {
160976b2cf59SMatthew Knepley   PetscFunctionBegin;
161076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
161176b2cf59SMatthew Knepley }
161276b2cf59SMatthew Knepley 
16134a2ae208SSatish Balay #undef __FUNCT__
16144a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
16159b94acceSBarry Smith /*
16169b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
16179b94acceSBarry Smith    positive parameter delta.
16189b94acceSBarry Smith 
16199b94acceSBarry Smith     Input Parameters:
1620c7afd0dbSLois Curfman McInnes +   snes - the SNES context
16219b94acceSBarry Smith .   y - approximate solution of linear system
16229b94acceSBarry Smith .   fnorm - 2-norm of current function
1623c7afd0dbSLois Curfman McInnes -   delta - trust region size
16249b94acceSBarry Smith 
16259b94acceSBarry Smith     Output Parameters:
1626c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
16279b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
16289b94acceSBarry Smith     region, and exceeds zero otherwise.
1629c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
16309b94acceSBarry Smith 
16319b94acceSBarry Smith     Note:
16324b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
16339b94acceSBarry Smith     is set to be the maximum allowable step size.
16349b94acceSBarry Smith 
16359b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
16369b94acceSBarry Smith */
1637064f8208SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
16389b94acceSBarry Smith {
1639064f8208SBarry Smith   PetscReal   nrm;
1640ea709b57SSatish Balay   PetscScalar cnorm;
16413a40ed3dSBarry Smith   int         ierr;
16423a40ed3dSBarry Smith 
16433a40ed3dSBarry Smith   PetscFunctionBegin;
16444482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16454482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
1646*c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
1647184914b5SBarry Smith 
1648064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
1649064f8208SBarry Smith   if (nrm > *delta) {
1650064f8208SBarry Smith      nrm = *delta/nrm;
1651064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
1652064f8208SBarry Smith      cnorm = nrm;
1653329f5518SBarry Smith      ierr = VecScale(&cnorm,y);CHKERRQ(ierr);
16549b94acceSBarry Smith      *ynorm = *delta;
16559b94acceSBarry Smith   } else {
16569b94acceSBarry Smith      *gpnorm = 0.0;
1657064f8208SBarry Smith      *ynorm = nrm;
16589b94acceSBarry Smith   }
16593a40ed3dSBarry Smith   PetscFunctionReturn(0);
16609b94acceSBarry Smith }
16619b94acceSBarry Smith 
16624a2ae208SSatish Balay #undef __FUNCT__
16634a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
16649b94acceSBarry Smith /*@
16659b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
166663a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
16679b94acceSBarry Smith 
1668c7afd0dbSLois Curfman McInnes    Collective on SNES
1669c7afd0dbSLois Curfman McInnes 
1670b2002411SLois Curfman McInnes    Input Parameters:
1671c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1672c7afd0dbSLois Curfman McInnes -  x - the solution vector
16739b94acceSBarry Smith 
1674b2002411SLois Curfman McInnes    Notes:
16758ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
16768ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
16778ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
16788ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
16798ddd3da0SLois Curfman McInnes 
168036851e7fSLois Curfman McInnes    Level: beginner
168136851e7fSLois Curfman McInnes 
16829b94acceSBarry Smith .keywords: SNES, nonlinear, solve
16839b94acceSBarry Smith 
168463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
16859b94acceSBarry Smith @*/
1686*c9780b6fSBarry Smith int SNESSolve(SNES snes,Vec x)
16879b94acceSBarry Smith {
1688f1af5d2fSBarry Smith   int        ierr;
1689f1af5d2fSBarry Smith   PetscTruth flg;
1690052efed2SBarry Smith 
16913a40ed3dSBarry Smith   PetscFunctionBegin;
16924482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16934482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
1694*c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
169529bbc08cSBarry Smith   if (!snes->solve) SETERRQ(1,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
169674637425SBarry Smith 
169782bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
1698c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
1699758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
1700d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
170150ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
1702*c9780b6fSBarry Smith   ierr = (*(snes)->solve)(snes);CHKERRQ(ierr);
1703d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
1704b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr);
17058b179ff8SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESView(snes,PETSC_VIEWER_STDOUT_(snes->comm));CHKERRQ(ierr); }
1706da9b6338SBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
1707da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
17083a40ed3dSBarry Smith   PetscFunctionReturn(0);
17099b94acceSBarry Smith }
17109b94acceSBarry Smith 
17119b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
17129b94acceSBarry Smith 
17134a2ae208SSatish Balay #undef __FUNCT__
17144a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
171582bf6240SBarry Smith /*@C
17164b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
17179b94acceSBarry Smith 
1718fee21e36SBarry Smith    Collective on SNES
1719fee21e36SBarry Smith 
1720c7afd0dbSLois Curfman McInnes    Input Parameters:
1721c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1722454a90a3SBarry Smith -  type - a known method
1723c7afd0dbSLois Curfman McInnes 
1724c7afd0dbSLois Curfman McInnes    Options Database Key:
1725454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1726c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1727ae12b187SLois Curfman McInnes 
17289b94acceSBarry Smith    Notes:
1729e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
17304b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
1731c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
17324b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
1733c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
17349b94acceSBarry Smith 
1735ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1736ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1737ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1738ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1739ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1740ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1741ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1742ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1743ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
1744b0a32e0cSBarry Smith   appropriate method.
174536851e7fSLois Curfman McInnes 
174636851e7fSLois Curfman McInnes   Level: intermediate
1747a703fe33SLois Curfman McInnes 
1748454a90a3SBarry Smith .keywords: SNES, set, type
1749435da068SBarry Smith 
1750435da068SBarry Smith .seealso: SNESType, SNESCreate()
1751435da068SBarry Smith 
17529b94acceSBarry Smith @*/
17530e33f6ddSBarry Smith int SNESSetType(SNES snes,const SNESType type)
17549b94acceSBarry Smith {
17550f5bd95cSBarry Smith   int        ierr,(*r)(SNES);
17566831982aSBarry Smith   PetscTruth match;
17573a40ed3dSBarry Smith 
17583a40ed3dSBarry Smith   PetscFunctionBegin;
17594482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
17604482741eSBarry Smith   PetscValidCharPointer(type,2);
176182bf6240SBarry Smith 
17626831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
17630f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
176482bf6240SBarry Smith 
176582bf6240SBarry Smith   if (snes->setupcalled) {
1766e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
176782bf6240SBarry Smith     snes->data = 0;
176882bf6240SBarry Smith   }
17697d1a2b2bSBarry Smith 
17709b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
177182bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
177282bf6240SBarry Smith 
1773b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
177482bf6240SBarry Smith 
177529bbc08cSBarry Smith   if (!r) SETERRQ1(1,"Unable to find requested SNES type %s",type);
17761548b14aSBarry Smith 
1777606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
177882bf6240SBarry Smith   snes->data = 0;
17793a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
1780454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
178182bf6240SBarry Smith 
17823a40ed3dSBarry Smith   PetscFunctionReturn(0);
17839b94acceSBarry Smith }
17849b94acceSBarry Smith 
1785a847f771SSatish Balay 
17869b94acceSBarry Smith /* --------------------------------------------------------------------- */
17874a2ae208SSatish Balay #undef __FUNCT__
17884a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
17899b94acceSBarry Smith /*@C
17909b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
1791f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
17929b94acceSBarry Smith 
1793fee21e36SBarry Smith    Not Collective
1794fee21e36SBarry Smith 
179536851e7fSLois Curfman McInnes    Level: advanced
179636851e7fSLois Curfman McInnes 
17979b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
17989b94acceSBarry Smith 
17999b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
18009b94acceSBarry Smith @*/
1801cf256101SBarry Smith int SNESRegisterDestroy(void)
18029b94acceSBarry Smith {
180382bf6240SBarry Smith   int ierr;
180482bf6240SBarry Smith 
18053a40ed3dSBarry Smith   PetscFunctionBegin;
180682bf6240SBarry Smith   if (SNESList) {
1807b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
180882bf6240SBarry Smith     SNESList = 0;
18099b94acceSBarry Smith   }
18104c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
18113a40ed3dSBarry Smith   PetscFunctionReturn(0);
18129b94acceSBarry Smith }
18139b94acceSBarry Smith 
18144a2ae208SSatish Balay #undef __FUNCT__
18154a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
18169b94acceSBarry Smith /*@C
18179a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
18189b94acceSBarry Smith 
1819c7afd0dbSLois Curfman McInnes    Not Collective
1820c7afd0dbSLois Curfman McInnes 
18219b94acceSBarry Smith    Input Parameter:
18224b0e389bSBarry Smith .  snes - nonlinear solver context
18239b94acceSBarry Smith 
18249b94acceSBarry Smith    Output Parameter:
18253a7fca6bSBarry Smith .  type - SNES method (a character string)
18269b94acceSBarry Smith 
182736851e7fSLois Curfman McInnes    Level: intermediate
182836851e7fSLois Curfman McInnes 
1829454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
18309b94acceSBarry Smith @*/
1831454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type)
18329b94acceSBarry Smith {
18333a40ed3dSBarry Smith   PetscFunctionBegin;
18344482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18354482741eSBarry Smith   PetscValidPointer(type,2);
1836454a90a3SBarry Smith   *type = snes->type_name;
18373a40ed3dSBarry Smith   PetscFunctionReturn(0);
18389b94acceSBarry Smith }
18399b94acceSBarry Smith 
18404a2ae208SSatish Balay #undef __FUNCT__
18414a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
18429b94acceSBarry Smith /*@C
18439b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
18449b94acceSBarry Smith    stored.
18459b94acceSBarry Smith 
1846c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1847c7afd0dbSLois Curfman McInnes 
18489b94acceSBarry Smith    Input Parameter:
18499b94acceSBarry Smith .  snes - the SNES context
18509b94acceSBarry Smith 
18519b94acceSBarry Smith    Output Parameter:
18529b94acceSBarry Smith .  x - the solution
18539b94acceSBarry Smith 
185436851e7fSLois Curfman McInnes    Level: advanced
185536851e7fSLois Curfman McInnes 
18569b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
18579b94acceSBarry Smith 
18584b27c08aSLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetSolutionUpdate()
18599b94acceSBarry Smith @*/
18609b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
18619b94acceSBarry Smith {
18623a40ed3dSBarry Smith   PetscFunctionBegin;
18634482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18644482741eSBarry Smith   PetscValidPointer(x,2);
18659b94acceSBarry Smith   *x = snes->vec_sol_always;
18663a40ed3dSBarry Smith   PetscFunctionReturn(0);
18679b94acceSBarry Smith }
18689b94acceSBarry Smith 
18694a2ae208SSatish Balay #undef __FUNCT__
18704a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
18719b94acceSBarry Smith /*@C
18729b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
18739b94acceSBarry Smith    stored.
18749b94acceSBarry Smith 
1875c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1876c7afd0dbSLois Curfman McInnes 
18779b94acceSBarry Smith    Input Parameter:
18789b94acceSBarry Smith .  snes - the SNES context
18799b94acceSBarry Smith 
18809b94acceSBarry Smith    Output Parameter:
18819b94acceSBarry Smith .  x - the solution update
18829b94acceSBarry Smith 
188336851e7fSLois Curfman McInnes    Level: advanced
188436851e7fSLois Curfman McInnes 
18859b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
18869b94acceSBarry Smith 
18879b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
18889b94acceSBarry Smith @*/
18899b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
18909b94acceSBarry Smith {
18913a40ed3dSBarry Smith   PetscFunctionBegin;
18924482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18934482741eSBarry Smith   PetscValidPointer(x,2);
18949b94acceSBarry Smith   *x = snes->vec_sol_update_always;
18953a40ed3dSBarry Smith   PetscFunctionReturn(0);
18969b94acceSBarry Smith }
18979b94acceSBarry Smith 
18984a2ae208SSatish Balay #undef __FUNCT__
18994a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
19009b94acceSBarry Smith /*@C
19013638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
19029b94acceSBarry Smith 
1903c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1904c7afd0dbSLois Curfman McInnes 
19059b94acceSBarry Smith    Input Parameter:
19069b94acceSBarry Smith .  snes - the SNES context
19079b94acceSBarry Smith 
19089b94acceSBarry Smith    Output Parameter:
19097bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
191000036973SBarry Smith .  ctx - the function context (or PETSC_NULL)
191100036973SBarry Smith -  func - the function (or PETSC_NULL)
19129b94acceSBarry Smith 
191336851e7fSLois Curfman McInnes    Level: advanced
191436851e7fSLois Curfman McInnes 
1915a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
19169b94acceSBarry Smith 
19174b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
19189b94acceSBarry Smith @*/
191900036973SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx,int (**func)(SNES,Vec,Vec,void*))
19209b94acceSBarry Smith {
19213a40ed3dSBarry Smith   PetscFunctionBegin;
19224482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19237bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
19247bf4e008SBarry Smith   if (ctx)  *ctx  = snes->funP;
192500036973SBarry Smith   if (func) *func = snes->computefunction;
19263a40ed3dSBarry Smith   PetscFunctionReturn(0);
19279b94acceSBarry Smith }
19289b94acceSBarry Smith 
19294a2ae208SSatish Balay #undef __FUNCT__
19304a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
19313c7409f5SSatish Balay /*@C
19323c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
1933d850072dSLois Curfman McInnes    SNES options in the database.
19343c7409f5SSatish Balay 
1935fee21e36SBarry Smith    Collective on SNES
1936fee21e36SBarry Smith 
1937c7afd0dbSLois Curfman McInnes    Input Parameter:
1938c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1939c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
1940c7afd0dbSLois Curfman McInnes 
1941d850072dSLois Curfman McInnes    Notes:
1942a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
1943c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
1944d850072dSLois Curfman McInnes 
194536851e7fSLois Curfman McInnes    Level: advanced
194636851e7fSLois Curfman McInnes 
19473c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
1948a86d99e1SLois Curfman McInnes 
1949a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
19503c7409f5SSatish Balay @*/
19511836bdbcSSatish Balay int SNESSetOptionsPrefix(SNES snes,const char prefix[])
19523c7409f5SSatish Balay {
19533c7409f5SSatish Balay   int ierr;
19543c7409f5SSatish Balay 
19553a40ed3dSBarry Smith   PetscFunctionBegin;
19564482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1957639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
195894b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
19593a40ed3dSBarry Smith   PetscFunctionReturn(0);
19603c7409f5SSatish Balay }
19613c7409f5SSatish Balay 
19624a2ae208SSatish Balay #undef __FUNCT__
19634a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
19643c7409f5SSatish Balay /*@C
1965f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
1966d850072dSLois Curfman McInnes    SNES options in the database.
19673c7409f5SSatish Balay 
1968fee21e36SBarry Smith    Collective on SNES
1969fee21e36SBarry Smith 
1970c7afd0dbSLois Curfman McInnes    Input Parameters:
1971c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1972c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
1973c7afd0dbSLois Curfman McInnes 
1974d850072dSLois Curfman McInnes    Notes:
1975a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
1976c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
1977d850072dSLois Curfman McInnes 
197836851e7fSLois Curfman McInnes    Level: advanced
197936851e7fSLois Curfman McInnes 
19803c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
1981a86d99e1SLois Curfman McInnes 
1982a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
19833c7409f5SSatish Balay @*/
19841836bdbcSSatish Balay int SNESAppendOptionsPrefix(SNES snes,const char prefix[])
19853c7409f5SSatish Balay {
19863c7409f5SSatish Balay   int ierr;
19873c7409f5SSatish Balay 
19883a40ed3dSBarry Smith   PetscFunctionBegin;
19894482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1990639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
199194b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
19923a40ed3dSBarry Smith   PetscFunctionReturn(0);
19933c7409f5SSatish Balay }
19943c7409f5SSatish Balay 
19954a2ae208SSatish Balay #undef __FUNCT__
19964a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
19979ab63eb5SSatish Balay /*@C
19983c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
19993c7409f5SSatish Balay    SNES options in the database.
20003c7409f5SSatish Balay 
2001c7afd0dbSLois Curfman McInnes    Not Collective
2002c7afd0dbSLois Curfman McInnes 
20033c7409f5SSatish Balay    Input Parameter:
20043c7409f5SSatish Balay .  snes - the SNES context
20053c7409f5SSatish Balay 
20063c7409f5SSatish Balay    Output Parameter:
20073c7409f5SSatish Balay .  prefix - pointer to the prefix string used
20083c7409f5SSatish Balay 
20099ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
20109ab63eb5SSatish Balay    sufficient length to hold the prefix.
20119ab63eb5SSatish Balay 
201236851e7fSLois Curfman McInnes    Level: advanced
201336851e7fSLois Curfman McInnes 
20143c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2015a86d99e1SLois Curfman McInnes 
2016a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
20173c7409f5SSatish Balay @*/
20181836bdbcSSatish Balay int SNESGetOptionsPrefix(SNES snes,char *prefix[])
20193c7409f5SSatish Balay {
20203c7409f5SSatish Balay   int ierr;
20213c7409f5SSatish Balay 
20223a40ed3dSBarry Smith   PetscFunctionBegin;
20234482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2024639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
20253a40ed3dSBarry Smith   PetscFunctionReturn(0);
20263c7409f5SSatish Balay }
20273c7409f5SSatish Balay 
2028b2002411SLois Curfman McInnes 
20294a2ae208SSatish Balay #undef __FUNCT__
20304a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
20313cea93caSBarry Smith /*@C
20323cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
20333cea93caSBarry Smith 
20347f6c08e0SMatthew Knepley   Level: advanced
20353cea93caSBarry Smith @*/
20361836bdbcSSatish Balay int SNESRegister(const char sname[],const char path[],const char name[],int (*function)(SNES))
2037b2002411SLois Curfman McInnes {
2038b2002411SLois Curfman McInnes   char fullname[256];
2039b2002411SLois Curfman McInnes   int  ierr;
2040b2002411SLois Curfman McInnes 
2041b2002411SLois Curfman McInnes   PetscFunctionBegin;
2042b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2043c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2044b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2045b2002411SLois Curfman McInnes }
2046da9b6338SBarry Smith 
2047da9b6338SBarry Smith #undef __FUNCT__
2048da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
2049da9b6338SBarry Smith int SNESTestLocalMin(SNES snes)
2050da9b6338SBarry Smith {
2051da9b6338SBarry Smith   int         ierr,N,i,j;
2052da9b6338SBarry Smith   Vec         u,uh,fh;
2053da9b6338SBarry Smith   PetscScalar value;
2054da9b6338SBarry Smith   PetscReal   norm;
2055da9b6338SBarry Smith 
2056da9b6338SBarry Smith   PetscFunctionBegin;
2057da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2058da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2059da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2060da9b6338SBarry Smith 
2061da9b6338SBarry Smith   /* currently only works for sequential */
2062da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2063da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2064da9b6338SBarry Smith   for (i=0; i<N; i++) {
2065da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
2066da9b6338SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %d\n",i);CHKERRQ(ierr);
2067da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2068ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2069da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2070da9b6338SBarry Smith       ierr  = (*snes->computefunction)(snes,uh,fh,snes->funP);CHKERRQ(ierr);
2071da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
2072da9b6338SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %d %18.16e\n",j,norm);CHKERRQ(ierr);
2073da9b6338SBarry Smith       value = -value;
2074da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2075da9b6338SBarry Smith     }
2076da9b6338SBarry Smith   }
2077da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2078da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2079da9b6338SBarry Smith   PetscFunctionReturn(0);
2080da9b6338SBarry Smith }
2081