163dd3a1aSKris Buschelman #define PETSCSNES_DLL 29b94acceSBarry Smith 3b9147fbbSdalcinl #include "include/private/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 */ 963dd3a1aSKris Buschelman PetscCookie PETSCSNES_DLLEXPORT SNES_COOKIE = 0; 106849ba73SBarry Smith PetscEvent 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 @*/ 4363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer) 449b94acceSBarry Smith { 45fa9f3622SBarry Smith SNESKSPEW *kctx; 46dfbe8321SBarry Smith PetscErrorCode ierr; 4794b7f48cSBarry Smith KSP ksp; 48e060cb09SBarry Smith SNESType type; 4932077d6dSBarry Smith PetscTruth iascii,isstring; 509b94acceSBarry Smith 513a40ed3dSBarry Smith PetscFunctionBegin; 524482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 533050cee2SBarry Smith if (!viewer) { 543050cee2SBarry Smith ierr = PetscViewerASCIIGetStdout(snes->comm,&viewer);CHKERRQ(ierr); 553050cee2SBarry Smith } 564482741eSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2); 57c9780b6fSBarry Smith PetscCheckSameComm(snes,1,viewer,2); 5874679c65SBarry Smith 5932077d6dSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 60b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr); 6132077d6dSBarry Smith if (iascii) { 623a7fca6bSBarry Smith if (snes->prefix) { 633a7fca6bSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr); 643a7fca6bSBarry Smith } else { 65b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr); 663a7fca6bSBarry Smith } 67454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 68454a90a3SBarry Smith if (type) { 69b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," type: %s\n",type);CHKERRQ(ierr); 70184914b5SBarry Smith } else { 71b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," type: not set yet\n");CHKERRQ(ierr); 72184914b5SBarry Smith } 73e7788613SBarry Smith if (snes->ops->view) { 74b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 75e7788613SBarry Smith ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 76b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 770ef38995SBarry Smith } 7877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 79a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, solution=%G\n", 8070441072SBarry Smith snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr); 8177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr); 8277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr); 839b94acceSBarry Smith if (snes->ksp_ewconv) { 84fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 859b94acceSBarry Smith if (kctx) { 8677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr); 87a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 88a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 899b94acceSBarry Smith } 909b94acceSBarry Smith } 910f5bd95cSBarry Smith } else if (isstring) { 92454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 93b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 9419bcc07fSBarry Smith } 9594b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 96b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 9794b7f48cSBarry Smith ierr = KSPView(ksp,viewer);CHKERRQ(ierr); 98b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 993a40ed3dSBarry Smith PetscFunctionReturn(0); 1009b94acceSBarry Smith } 1019b94acceSBarry Smith 10276b2cf59SMatthew Knepley /* 10376b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 10476b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 10576b2cf59SMatthew Knepley */ 10676b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 107a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 1086849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 10976b2cf59SMatthew Knepley 110e74ef692SMatthew Knepley #undef __FUNCT__ 111e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker" 112ac226902SBarry Smith /*@C 11376b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 11476b2cf59SMatthew Knepley 11576b2cf59SMatthew Knepley Not Collective 11676b2cf59SMatthew Knepley 11776b2cf59SMatthew Knepley Input Parameter: 11876b2cf59SMatthew Knepley . snescheck - function that checks for options 11976b2cf59SMatthew Knepley 12076b2cf59SMatthew Knepley Level: developer 12176b2cf59SMatthew Knepley 12276b2cf59SMatthew Knepley .seealso: SNESSetFromOptions() 12376b2cf59SMatthew Knepley @*/ 12463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 12576b2cf59SMatthew Knepley { 12676b2cf59SMatthew Knepley PetscFunctionBegin; 12776b2cf59SMatthew Knepley if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 12877431f27SBarry Smith SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS); 12976b2cf59SMatthew Knepley } 13076b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 13176b2cf59SMatthew Knepley PetscFunctionReturn(0); 13276b2cf59SMatthew Knepley } 13376b2cf59SMatthew Knepley 1344a2ae208SSatish Balay #undef __FUNCT__ 1354a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions" 1369b94acceSBarry Smith /*@ 13794b7f48cSBarry Smith SNESSetFromOptions - Sets various SNES and KSP parameters from user options. 1389b94acceSBarry Smith 139c7afd0dbSLois Curfman McInnes Collective on SNES 140c7afd0dbSLois Curfman McInnes 1419b94acceSBarry Smith Input Parameter: 1429b94acceSBarry Smith . snes - the SNES context 1439b94acceSBarry Smith 14436851e7fSLois Curfman McInnes Options Database Keys: 1456831982aSBarry Smith + -snes_type <type> - ls, tr, umls, umtr, test 14682738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 14782738288SBarry Smith of the change in the solution between steps 14870441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 149b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 150b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 151b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 15250ffb88aSMatthew Knepley . -snes_max_fail <max_fail> - maximum number of failures 153b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 1542492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 15582738288SBarry Smith solver; hence iterations will continue until max_it 1561fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 15782738288SBarry Smith of convergence test 158e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 159e8105e01SRichard Katz filename given prints to stdout 160a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 161a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 162a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 163a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 164e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 1655968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 166fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 16782738288SBarry Smith 16882738288SBarry Smith Options Database for Eisenstat-Walker method: 169fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 1704b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 17136851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 17236851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 17336851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 17436851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 17536851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 17636851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 17782738288SBarry Smith 17811ca99fdSLois Curfman McInnes Notes: 17911ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 18011ca99fdSLois Curfman McInnes the users manual. 18183e2fdc7SBarry Smith 18236851e7fSLois Curfman McInnes Level: beginner 18336851e7fSLois Curfman McInnes 1849b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 1859b94acceSBarry Smith 18669ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 1879b94acceSBarry Smith @*/ 18863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes) 1899b94acceSBarry Smith { 19094b7f48cSBarry Smith KSP ksp; 191fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW *)snes->kspconvctx; 192f1af5d2fSBarry Smith PetscTruth flg; 193dfbe8321SBarry Smith PetscErrorCode ierr; 19477431f27SBarry Smith PetscInt i; 1952fc52814SBarry Smith const char *deft; 196e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 19723d894e5SBarry Smith PetscViewerASCIIMonitor monviewer; 1989b94acceSBarry Smith 1993a40ed3dSBarry Smith PetscFunctionBegin; 2004482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 201ca161407SBarry Smith 202b0a32e0cSBarry Smith ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr); 203186905e3SBarry Smith if (snes->type_name) { 204186905e3SBarry Smith deft = snes->type_name; 205186905e3SBarry Smith } else { 2064b27c08aSLois Curfman McInnes deft = SNESLS; 207d64ed03dSBarry Smith } 2084bbc92c1SBarry Smith 209186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 210b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 211d64ed03dSBarry Smith if (flg) { 212186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 213186905e3SBarry Smith } else if (!snes->type_name) { 214186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 215d64ed03dSBarry Smith } 216909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 21793c39befSBarry Smith 21887828ca2SBarry Smith ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 21970441072SBarry Smith ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 220186905e3SBarry Smith 22187828ca2SBarry Smith ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 222b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 223b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 22450ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 2255968eb51SBarry Smith ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr); 2265968eb51SBarry Smith if (flg) { 2275968eb51SBarry Smith snes->printreason = PETSC_TRUE; 2285968eb51SBarry Smith } 229186905e3SBarry Smith 230fa9f3622SBarry Smith ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 231186905e3SBarry Smith 232fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 233fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 234fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 235fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 236fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 237fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 238fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 239186905e3SBarry Smith 240b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr); 241e7788613SBarry Smith if (flg) {snes->ops->converged = 0;} 242a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr); 243a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 244eabae89aSBarry Smith 245a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 246e8105e01SRichard Katz if (flg) { 24723d894e5SBarry Smith ierr = PetscViewerASCIIMonitorCreate(snes->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 24823d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 249e8105e01SRichard Katz } 250eabae89aSBarry Smith 251a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 252eabae89aSBarry Smith if (flg) { 25323d894e5SBarry Smith ierr = PetscViewerASCIIMonitorCreate(snes->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 254f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 255e8105e01SRichard Katz } 256eabae89aSBarry Smith 257a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 258eabae89aSBarry Smith if (flg) { 25923d894e5SBarry Smith ierr = PetscViewerASCIIMonitorCreate(snes->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 26023d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 261eabae89aSBarry Smith } 262eabae89aSBarry Smith 263a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr); 264a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 265a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr); 266a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 267a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr); 268a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 269a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr); 270a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 271e24b481bSBarry Smith 272b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr); 2734b27c08aSLois Curfman McInnes if (flg) { 274186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 275ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 2769b94acceSBarry Smith } 277639f9d9dSBarry Smith 27876b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 27976b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 28076b2cf59SMatthew Knepley } 28176b2cf59SMatthew Knepley 282e7788613SBarry Smith if (snes->ops->setfromoptions) { 283e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 284639f9d9dSBarry Smith } 285b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 2864bbc92c1SBarry Smith 28794b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 28894b7f48cSBarry Smith ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); 28993993e2dSLois Curfman McInnes 2903a40ed3dSBarry Smith PetscFunctionReturn(0); 2919b94acceSBarry Smith } 2929b94acceSBarry Smith 293a847f771SSatish Balay 2944a2ae208SSatish Balay #undef __FUNCT__ 2954a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 2969b94acceSBarry Smith /*@ 2979b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 2989b94acceSBarry Smith the nonlinear solvers. 2999b94acceSBarry Smith 300fee21e36SBarry Smith Collective on SNES 301fee21e36SBarry Smith 302c7afd0dbSLois Curfman McInnes Input Parameters: 303c7afd0dbSLois Curfman McInnes + snes - the SNES context 304c7afd0dbSLois Curfman McInnes - usrP - optional user context 305c7afd0dbSLois Curfman McInnes 30636851e7fSLois Curfman McInnes Level: intermediate 30736851e7fSLois Curfman McInnes 3089b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3099b94acceSBarry Smith 3109b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3119b94acceSBarry Smith @*/ 31263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP) 3139b94acceSBarry Smith { 3143a40ed3dSBarry Smith PetscFunctionBegin; 3154482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3169b94acceSBarry Smith snes->user = usrP; 3173a40ed3dSBarry Smith PetscFunctionReturn(0); 3189b94acceSBarry Smith } 31974679c65SBarry Smith 3204a2ae208SSatish Balay #undef __FUNCT__ 3214a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 3229b94acceSBarry Smith /*@C 3239b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3249b94acceSBarry Smith nonlinear solvers. 3259b94acceSBarry Smith 326c7afd0dbSLois Curfman McInnes Not Collective 327c7afd0dbSLois Curfman McInnes 3289b94acceSBarry Smith Input Parameter: 3299b94acceSBarry Smith . snes - SNES context 3309b94acceSBarry Smith 3319b94acceSBarry Smith Output Parameter: 3329b94acceSBarry Smith . usrP - user context 3339b94acceSBarry Smith 33436851e7fSLois Curfman McInnes Level: intermediate 33536851e7fSLois Curfman McInnes 3369b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3379b94acceSBarry Smith 3389b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3399b94acceSBarry Smith @*/ 34063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP) 3419b94acceSBarry Smith { 3423a40ed3dSBarry Smith PetscFunctionBegin; 3434482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3449b94acceSBarry Smith *usrP = snes->user; 3453a40ed3dSBarry Smith PetscFunctionReturn(0); 3469b94acceSBarry Smith } 34774679c65SBarry Smith 3484a2ae208SSatish Balay #undef __FUNCT__ 3494a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 3509b94acceSBarry Smith /*@ 351c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 352c8228a4eSBarry Smith at this time. 3539b94acceSBarry Smith 354c7afd0dbSLois Curfman McInnes Not Collective 355c7afd0dbSLois Curfman McInnes 3569b94acceSBarry Smith Input Parameter: 3579b94acceSBarry Smith . snes - SNES context 3589b94acceSBarry Smith 3599b94acceSBarry Smith Output Parameter: 3609b94acceSBarry Smith . iter - iteration number 3619b94acceSBarry Smith 362c8228a4eSBarry Smith Notes: 363c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 364c8228a4eSBarry Smith 365c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 36608405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 36708405cd6SLois Curfman McInnes .vb 36808405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 36908405cd6SLois Curfman McInnes if (!(it % 2)) { 37008405cd6SLois Curfman McInnes [compute Jacobian here] 37108405cd6SLois Curfman McInnes } 37208405cd6SLois Curfman McInnes .ve 373c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 37408405cd6SLois Curfman McInnes recomputed every second SNES iteration. 375c8228a4eSBarry Smith 37636851e7fSLois Curfman McInnes Level: intermediate 37736851e7fSLois Curfman McInnes 3782b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 3792b668275SBarry Smith 380b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 3819b94acceSBarry Smith @*/ 38263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter) 3839b94acceSBarry Smith { 3843a40ed3dSBarry Smith PetscFunctionBegin; 3854482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3864482741eSBarry Smith PetscValidIntPointer(iter,2); 3879b94acceSBarry Smith *iter = snes->iter; 3883a40ed3dSBarry Smith PetscFunctionReturn(0); 3899b94acceSBarry Smith } 39074679c65SBarry Smith 3914a2ae208SSatish Balay #undef __FUNCT__ 3924a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 3939b94acceSBarry Smith /*@ 3949b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 3959b94acceSBarry Smith with SNESSSetFunction(). 3969b94acceSBarry Smith 397c7afd0dbSLois Curfman McInnes Collective on SNES 398c7afd0dbSLois Curfman McInnes 3999b94acceSBarry Smith Input Parameter: 4009b94acceSBarry Smith . snes - SNES context 4019b94acceSBarry Smith 4029b94acceSBarry Smith Output Parameter: 4039b94acceSBarry Smith . fnorm - 2-norm of function 4049b94acceSBarry Smith 40536851e7fSLois Curfman McInnes Level: intermediate 40636851e7fSLois Curfman McInnes 4079b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 408a86d99e1SLois Curfman McInnes 409b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 4109b94acceSBarry Smith @*/ 41171f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 4129b94acceSBarry Smith { 4133a40ed3dSBarry Smith PetscFunctionBegin; 4144482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4154482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 4169b94acceSBarry Smith *fnorm = snes->norm; 4173a40ed3dSBarry Smith PetscFunctionReturn(0); 4189b94acceSBarry Smith } 41974679c65SBarry Smith 4204a2ae208SSatish Balay #undef __FUNCT__ 421b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 4229b94acceSBarry Smith /*@ 423b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 4249b94acceSBarry Smith attempted by the nonlinear solver. 4259b94acceSBarry Smith 426c7afd0dbSLois Curfman McInnes Not Collective 427c7afd0dbSLois Curfman McInnes 4289b94acceSBarry Smith Input Parameter: 4299b94acceSBarry Smith . snes - SNES context 4309b94acceSBarry Smith 4319b94acceSBarry Smith Output Parameter: 4329b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 4339b94acceSBarry Smith 434c96a6f78SLois Curfman McInnes Notes: 435c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 436c96a6f78SLois Curfman McInnes 43736851e7fSLois Curfman McInnes Level: intermediate 43836851e7fSLois Curfman McInnes 4399b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 4409b94acceSBarry Smith @*/ 441b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 4429b94acceSBarry Smith { 4433a40ed3dSBarry Smith PetscFunctionBegin; 4444482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4454482741eSBarry Smith PetscValidIntPointer(nfails,2); 44650ffb88aSMatthew Knepley *nfails = snes->numFailures; 44750ffb88aSMatthew Knepley PetscFunctionReturn(0); 44850ffb88aSMatthew Knepley } 44950ffb88aSMatthew Knepley 45050ffb88aSMatthew Knepley #undef __FUNCT__ 451b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 45250ffb88aSMatthew Knepley /*@ 453b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets 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 Parameters: 45950ffb88aSMatthew Knepley + snes - SNES context 46050ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 46150ffb88aSMatthew Knepley 46250ffb88aSMatthew Knepley Level: intermediate 46350ffb88aSMatthew Knepley 46450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 46550ffb88aSMatthew Knepley @*/ 466b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 46750ffb88aSMatthew Knepley { 46850ffb88aSMatthew Knepley PetscFunctionBegin; 4694482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 47050ffb88aSMatthew Knepley snes->maxFailures = maxFails; 47150ffb88aSMatthew Knepley PetscFunctionReturn(0); 47250ffb88aSMatthew Knepley } 47350ffb88aSMatthew Knepley 47450ffb88aSMatthew Knepley #undef __FUNCT__ 475b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 47650ffb88aSMatthew Knepley /*@ 477b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 47850ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 47950ffb88aSMatthew Knepley 48050ffb88aSMatthew Knepley Not Collective 48150ffb88aSMatthew Knepley 48250ffb88aSMatthew Knepley Input Parameter: 48350ffb88aSMatthew Knepley . snes - SNES context 48450ffb88aSMatthew Knepley 48550ffb88aSMatthew Knepley Output Parameter: 48650ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 48750ffb88aSMatthew Knepley 48850ffb88aSMatthew Knepley Level: intermediate 48950ffb88aSMatthew Knepley 49050ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 49150ffb88aSMatthew Knepley @*/ 492b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 49350ffb88aSMatthew Knepley { 49450ffb88aSMatthew Knepley PetscFunctionBegin; 4954482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4964482741eSBarry Smith PetscValidIntPointer(maxFails,2); 49750ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 4983a40ed3dSBarry Smith PetscFunctionReturn(0); 4999b94acceSBarry Smith } 500a847f771SSatish Balay 5014a2ae208SSatish Balay #undef __FUNCT__ 502*2541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 503*2541af92SBarry Smith /*@ 504*2541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 505*2541af92SBarry Smith done by SNES. 506*2541af92SBarry Smith 507*2541af92SBarry Smith Not Collective 508*2541af92SBarry Smith 509*2541af92SBarry Smith Input Parameter: 510*2541af92SBarry Smith . snes - SNES context 511*2541af92SBarry Smith 512*2541af92SBarry Smith Output Parameter: 513*2541af92SBarry Smith . nfuncs - number of evaluations 514*2541af92SBarry Smith 515*2541af92SBarry Smith Level: intermediate 516*2541af92SBarry Smith 517*2541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 518*2541af92SBarry Smith @*/ 519*2541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 520*2541af92SBarry Smith { 521*2541af92SBarry Smith PetscFunctionBegin; 522*2541af92SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 523*2541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 524*2541af92SBarry Smith *nfuncs = snes->nfuncs; 525*2541af92SBarry Smith PetscFunctionReturn(0); 526*2541af92SBarry Smith } 527*2541af92SBarry Smith 528*2541af92SBarry Smith #undef __FUNCT__ 5293d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 5303d4c4710SBarry Smith /*@ 5313d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 5323d4c4710SBarry Smith linear solvers. 5333d4c4710SBarry Smith 5343d4c4710SBarry Smith Not Collective 5353d4c4710SBarry Smith 5363d4c4710SBarry Smith Input Parameter: 5373d4c4710SBarry Smith . snes - SNES context 5383d4c4710SBarry Smith 5393d4c4710SBarry Smith Output Parameter: 5403d4c4710SBarry Smith . nfails - number of failed solves 5413d4c4710SBarry Smith 5423d4c4710SBarry Smith Notes: 5433d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 5443d4c4710SBarry Smith 5453d4c4710SBarry Smith Level: intermediate 5463d4c4710SBarry Smith 5473d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 5483d4c4710SBarry Smith @*/ 5493d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 5503d4c4710SBarry Smith { 5513d4c4710SBarry Smith PetscFunctionBegin; 5523d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5533d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 5543d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 5553d4c4710SBarry Smith PetscFunctionReturn(0); 5563d4c4710SBarry Smith } 5573d4c4710SBarry Smith 5583d4c4710SBarry Smith #undef __FUNCT__ 5593d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 5603d4c4710SBarry Smith /*@ 5613d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 5623d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 5633d4c4710SBarry Smith 5643d4c4710SBarry Smith Collective on SNES 5653d4c4710SBarry Smith 5663d4c4710SBarry Smith Input Parameters: 5673d4c4710SBarry Smith + snes - SNES context 5683d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 5693d4c4710SBarry Smith 5703d4c4710SBarry Smith Level: intermediate 5713d4c4710SBarry Smith 5723d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 5733d4c4710SBarry Smith 5743d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 5753d4c4710SBarry Smith 5763d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 5773d4c4710SBarry Smith @*/ 5783d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 5793d4c4710SBarry Smith { 5803d4c4710SBarry Smith PetscFunctionBegin; 5813d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5823d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 5833d4c4710SBarry Smith PetscFunctionReturn(0); 5843d4c4710SBarry Smith } 5853d4c4710SBarry Smith 5863d4c4710SBarry Smith #undef __FUNCT__ 5873d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 5883d4c4710SBarry Smith /*@ 5893d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 5903d4c4710SBarry Smith are allowed before SNES terminates 5913d4c4710SBarry Smith 5923d4c4710SBarry Smith Not Collective 5933d4c4710SBarry Smith 5943d4c4710SBarry Smith Input Parameter: 5953d4c4710SBarry Smith . snes - SNES context 5963d4c4710SBarry Smith 5973d4c4710SBarry Smith Output Parameter: 5983d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 5993d4c4710SBarry Smith 6003d4c4710SBarry Smith Level: intermediate 6013d4c4710SBarry Smith 6023d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 6033d4c4710SBarry Smith 6043d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 6053d4c4710SBarry Smith 6063d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 6073d4c4710SBarry Smith @*/ 6083d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 6093d4c4710SBarry Smith { 6103d4c4710SBarry Smith PetscFunctionBegin; 6113d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6123d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 6133d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 6143d4c4710SBarry Smith PetscFunctionReturn(0); 6153d4c4710SBarry Smith } 6163d4c4710SBarry Smith 6173d4c4710SBarry Smith #undef __FUNCT__ 618b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 619c96a6f78SLois Curfman McInnes /*@ 620b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 621c96a6f78SLois Curfman McInnes used by the nonlinear solver. 622c96a6f78SLois Curfman McInnes 623c7afd0dbSLois Curfman McInnes Not Collective 624c7afd0dbSLois Curfman McInnes 625c96a6f78SLois Curfman McInnes Input Parameter: 626c96a6f78SLois Curfman McInnes . snes - SNES context 627c96a6f78SLois Curfman McInnes 628c96a6f78SLois Curfman McInnes Output Parameter: 629c96a6f78SLois Curfman McInnes . lits - number of linear iterations 630c96a6f78SLois Curfman McInnes 631c96a6f78SLois Curfman McInnes Notes: 632c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 633c96a6f78SLois Curfman McInnes 63436851e7fSLois Curfman McInnes Level: intermediate 63536851e7fSLois Curfman McInnes 636c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 6372b668275SBarry Smith 6382b668275SBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm() 639c96a6f78SLois Curfman McInnes @*/ 640b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 641c96a6f78SLois Curfman McInnes { 6423a40ed3dSBarry Smith PetscFunctionBegin; 6434482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6444482741eSBarry Smith PetscValidIntPointer(lits,2); 645c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 6463a40ed3dSBarry Smith PetscFunctionReturn(0); 647c96a6f78SLois Curfman McInnes } 648c96a6f78SLois Curfman McInnes 6494a2ae208SSatish Balay #undef __FUNCT__ 65094b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 65152baeb72SSatish Balay /*@ 65294b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 6539b94acceSBarry Smith 65494b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 655c7afd0dbSLois Curfman McInnes 6569b94acceSBarry Smith Input Parameter: 6579b94acceSBarry Smith . snes - the SNES context 6589b94acceSBarry Smith 6599b94acceSBarry Smith Output Parameter: 66094b7f48cSBarry Smith . ksp - the KSP context 6619b94acceSBarry Smith 6629b94acceSBarry Smith Notes: 66394b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 6649b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 6652999313aSBarry Smith PC contexts as well. 6669b94acceSBarry Smith 66736851e7fSLois Curfman McInnes Level: beginner 66836851e7fSLois Curfman McInnes 66994b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 6709b94acceSBarry Smith 6712999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 6729b94acceSBarry Smith @*/ 67363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp) 6749b94acceSBarry Smith { 6753a40ed3dSBarry Smith PetscFunctionBegin; 6764482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6774482741eSBarry Smith PetscValidPointer(ksp,2); 67894b7f48cSBarry Smith *ksp = snes->ksp; 6793a40ed3dSBarry Smith PetscFunctionReturn(0); 6809b94acceSBarry Smith } 68182bf6240SBarry Smith 6824a2ae208SSatish Balay #undef __FUNCT__ 6832999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 6842999313aSBarry Smith /*@ 6852999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 6862999313aSBarry Smith 6872999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 6882999313aSBarry Smith 6892999313aSBarry Smith Input Parameters: 6902999313aSBarry Smith + snes - the SNES context 6912999313aSBarry Smith - ksp - the KSP context 6922999313aSBarry Smith 6932999313aSBarry Smith Notes: 6942999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 6952999313aSBarry Smith so this routine is rarely needed. 6962999313aSBarry Smith 6972999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 6982999313aSBarry Smith decreased by one. 6992999313aSBarry Smith 7002999313aSBarry Smith Level: developer 7012999313aSBarry Smith 7022999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7032999313aSBarry Smith 7042999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7052999313aSBarry Smith @*/ 7062999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp) 7072999313aSBarry Smith { 7082999313aSBarry Smith PetscErrorCode ierr; 7092999313aSBarry Smith 7102999313aSBarry Smith PetscFunctionBegin; 7112999313aSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7122999313aSBarry Smith PetscValidHeaderSpecific(ksp,KSP_COOKIE,2); 7132999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 7147dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 715906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 7162999313aSBarry Smith snes->ksp = ksp; 7172999313aSBarry Smith PetscFunctionReturn(0); 7182999313aSBarry Smith } 7192999313aSBarry Smith 7202999313aSBarry Smith #undef __FUNCT__ 7214a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 7226849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 723e24b481bSBarry Smith { 724e24b481bSBarry Smith PetscFunctionBegin; 725e24b481bSBarry Smith PetscFunctionReturn(0); 726e24b481bSBarry Smith } 727e24b481bSBarry Smith 7289b94acceSBarry Smith /* -----------------------------------------------------------*/ 7294a2ae208SSatish Balay #undef __FUNCT__ 7304a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 73152baeb72SSatish Balay /*@ 7329b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 7339b94acceSBarry Smith 734c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 735c7afd0dbSLois Curfman McInnes 736c7afd0dbSLois Curfman McInnes Input Parameters: 737906ed7ccSBarry Smith . comm - MPI communicator 7389b94acceSBarry Smith 7399b94acceSBarry Smith Output Parameter: 7409b94acceSBarry Smith . outsnes - the new SNES context 7419b94acceSBarry Smith 742c7afd0dbSLois Curfman McInnes Options Database Keys: 743c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 744c7afd0dbSLois Curfman McInnes and no preconditioning matrix 745c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 746c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 747c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 748c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 749c1f60f51SBarry Smith 75036851e7fSLois Curfman McInnes Level: beginner 75136851e7fSLois Curfman McInnes 7529b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 7539b94acceSBarry Smith 7544b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES 7559b94acceSBarry Smith @*/ 75663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes) 7579b94acceSBarry Smith { 758dfbe8321SBarry Smith PetscErrorCode ierr; 7599b94acceSBarry Smith SNES snes; 760fa9f3622SBarry Smith SNESKSPEW *kctx; 76137fcc0dbSBarry Smith 7623a40ed3dSBarry Smith PetscFunctionBegin; 763ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 7648ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 7658ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 7668ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 7678ba1e511SMatthew Knepley #endif 7688ba1e511SMatthew Knepley 769e7788613SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 770e24b481bSBarry Smith snes->bops->publish = SNESPublish_Petsc; 7719b94acceSBarry Smith snes->max_its = 50; 7729750a799SBarry Smith snes->max_funcs = 10000; 7739b94acceSBarry Smith snes->norm = 0.0; 774b4874afaSBarry Smith snes->rtol = 1.e-8; 775b4874afaSBarry Smith snes->ttol = 0.0; 77670441072SBarry Smith snes->abstol = 1.e-50; 7779b94acceSBarry Smith snes->xtol = 1.e-8; 7784b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 7799b94acceSBarry Smith snes->nfuncs = 0; 78050ffb88aSMatthew Knepley snes->numFailures = 0; 78150ffb88aSMatthew Knepley snes->maxFailures = 1; 7827a00f4a9SLois Curfman McInnes snes->linear_its = 0; 783639f9d9dSBarry Smith snes->numbermonitors = 0; 7849b94acceSBarry Smith snes->data = 0; 7854dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 786186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 7876f24a144SLois Curfman McInnes snes->vwork = 0; 7886f24a144SLois Curfman McInnes snes->nwork = 0; 789758f92a0SBarry Smith snes->conv_hist_len = 0; 790758f92a0SBarry Smith snes->conv_hist_max = 0; 791758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 792758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 793758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 794184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 7959b94acceSBarry Smith 7963d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 7973d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 7983d4c4710SBarry Smith 7999b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 80038f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 8019b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 8029b94acceSBarry Smith kctx->version = 2; 8039b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 8049b94acceSBarry Smith this was too large for some test cases */ 8059b94acceSBarry Smith kctx->rtol_last = 0; 8069b94acceSBarry Smith kctx->rtol_max = .9; 8079b94acceSBarry Smith kctx->gamma = 1.0; 80871f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 80971f87433Sdalcinl kctx->alpha2 = kctx->alpha; 8109b94acceSBarry Smith kctx->threshold = .1; 8119b94acceSBarry Smith kctx->lresid_last = 0; 8129b94acceSBarry Smith kctx->norm_last = 0; 8139b94acceSBarry Smith 81494b7f48cSBarry Smith ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr); 81552e6d16bSBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 8165334005bSBarry Smith 8179b94acceSBarry Smith *outsnes = snes; 81800036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 8193a40ed3dSBarry Smith PetscFunctionReturn(0); 8209b94acceSBarry Smith } 8219b94acceSBarry Smith 8224a2ae208SSatish Balay #undef __FUNCT__ 8234a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 8249b94acceSBarry Smith /*@C 8259b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 8269b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 8279b94acceSBarry Smith equations. 8289b94acceSBarry Smith 829fee21e36SBarry Smith Collective on SNES 830fee21e36SBarry Smith 831c7afd0dbSLois Curfman McInnes Input Parameters: 832c7afd0dbSLois Curfman McInnes + snes - the SNES context 833c7afd0dbSLois Curfman McInnes . r - vector to store function value 834de044059SHong Zhang . func - function evaluation routine 835c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 836c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8379b94acceSBarry Smith 838c7afd0dbSLois Curfman McInnes Calling sequence of func: 8398d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 840c7afd0dbSLois Curfman McInnes 841313e4042SLois Curfman McInnes . f - function vector 842c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 8439b94acceSBarry Smith 8449b94acceSBarry Smith Notes: 8459b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 8469b94acceSBarry Smith $ f'(x) x = -f(x), 847c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 8489b94acceSBarry Smith 84936851e7fSLois Curfman McInnes Level: beginner 85036851e7fSLois Curfman McInnes 8519b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 8529b94acceSBarry Smith 853a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 8549b94acceSBarry Smith @*/ 85563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 8569b94acceSBarry Smith { 8573a40ed3dSBarry Smith PetscFunctionBegin; 8584482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 8594482741eSBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE,2); 860c9780b6fSBarry Smith PetscCheckSameComm(snes,1,r,2); 861184914b5SBarry Smith 862e7788613SBarry Smith snes->ops->computefunction = func; 8639b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 8649b94acceSBarry Smith snes->funP = ctx; 8653a40ed3dSBarry Smith PetscFunctionReturn(0); 8669b94acceSBarry Smith } 8679b94acceSBarry Smith 8683ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 8693ab0aad5SBarry Smith #undef __FUNCT__ 8703ab0aad5SBarry Smith #define __FUNCT__ "SNESSetRhs" 8713ab0aad5SBarry Smith /*@C 8723ab0aad5SBarry Smith SNESSetRhs - Sets the vector for solving F(x) = rhs. If rhs is not set 8733ab0aad5SBarry Smith it assumes a zero right hand side. 8743ab0aad5SBarry Smith 8753ab0aad5SBarry Smith Collective on SNES 8763ab0aad5SBarry Smith 8773ab0aad5SBarry Smith Input Parameters: 8783ab0aad5SBarry Smith + snes - the SNES context 8793ab0aad5SBarry Smith - rhs - the right hand side vector or PETSC_NULL for a zero right hand side 8803ab0aad5SBarry Smith 8813ab0aad5SBarry Smith Level: intermediate 8823ab0aad5SBarry Smith 8833ab0aad5SBarry Smith .keywords: SNES, nonlinear, set, function, right hand side 8843ab0aad5SBarry Smith 8851096aae1SMatthew Knepley .seealso: SNESGetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 8863ab0aad5SBarry Smith @*/ 88763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetRhs(SNES snes,Vec rhs) 8883ab0aad5SBarry Smith { 889dfbe8321SBarry Smith PetscErrorCode ierr; 8903ab0aad5SBarry Smith 8913ab0aad5SBarry Smith PetscFunctionBegin; 8923ab0aad5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 8933ab0aad5SBarry Smith if (rhs) { 8943ab0aad5SBarry Smith PetscValidHeaderSpecific(rhs,VEC_COOKIE,2); 8953ab0aad5SBarry Smith PetscCheckSameComm(snes,1,rhs,2); 8963ab0aad5SBarry Smith ierr = PetscObjectReference((PetscObject)rhs);CHKERRQ(ierr); 8973ab0aad5SBarry Smith } 8983ab0aad5SBarry Smith if (snes->afine) { 8993ab0aad5SBarry Smith ierr = VecDestroy(snes->afine);CHKERRQ(ierr); 9003ab0aad5SBarry Smith } 9013ab0aad5SBarry Smith snes->afine = rhs; 9023ab0aad5SBarry Smith PetscFunctionReturn(0); 9033ab0aad5SBarry Smith } 9043ab0aad5SBarry Smith 9054a2ae208SSatish Balay #undef __FUNCT__ 9061096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 9071096aae1SMatthew Knepley /*@C 9081096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 9091096aae1SMatthew Knepley it assumes a zero right hand side. 9101096aae1SMatthew Knepley 9111096aae1SMatthew Knepley Collective on SNES 9121096aae1SMatthew Knepley 9131096aae1SMatthew Knepley Input Parameter: 9141096aae1SMatthew Knepley . snes - the SNES context 9151096aae1SMatthew Knepley 9161096aae1SMatthew Knepley Output Parameter: 9171096aae1SMatthew Knepley . rhs - the right hand side vector or PETSC_NULL for a zero right hand side 9181096aae1SMatthew Knepley 9191096aae1SMatthew Knepley Level: intermediate 9201096aae1SMatthew Knepley 9211096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 9221096aae1SMatthew Knepley 9231096aae1SMatthew Knepley .seealso: SNESSetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 9241096aae1SMatthew Knepley @*/ 9251096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs) 9261096aae1SMatthew Knepley { 9271096aae1SMatthew Knepley PetscFunctionBegin; 9281096aae1SMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9291096aae1SMatthew Knepley PetscValidPointer(rhs,2); 9301096aae1SMatthew Knepley *rhs = snes->afine; 9311096aae1SMatthew Knepley PetscFunctionReturn(0); 9321096aae1SMatthew Knepley } 9331096aae1SMatthew Knepley 9341096aae1SMatthew Knepley #undef __FUNCT__ 9354a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 9369b94acceSBarry Smith /*@ 93736851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 9389b94acceSBarry Smith SNESSetFunction(). 9399b94acceSBarry Smith 940c7afd0dbSLois Curfman McInnes Collective on SNES 941c7afd0dbSLois Curfman McInnes 9429b94acceSBarry Smith Input Parameters: 943c7afd0dbSLois Curfman McInnes + snes - the SNES context 944c7afd0dbSLois Curfman McInnes - x - input vector 9459b94acceSBarry Smith 9469b94acceSBarry Smith Output Parameter: 9473638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 9489b94acceSBarry Smith 9491bffabb2SLois Curfman McInnes Notes: 95036851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 95136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 95236851e7fSLois Curfman McInnes themselves. 95336851e7fSLois Curfman McInnes 95436851e7fSLois Curfman McInnes Level: developer 95536851e7fSLois Curfman McInnes 9569b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 9579b94acceSBarry Smith 958a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 9599b94acceSBarry Smith @*/ 96063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y) 9619b94acceSBarry Smith { 962dfbe8321SBarry Smith PetscErrorCode ierr; 9639b94acceSBarry Smith 9643a40ed3dSBarry Smith PetscFunctionBegin; 9654482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9664482741eSBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE,2); 9674482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,3); 968c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 969c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 970184914b5SBarry Smith 971d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 972e7788613SBarry Smith if (snes->ops->computefunction) { 973d64ed03dSBarry Smith PetscStackPush("SNES user function"); 974e9a2bbcdSBarry Smith CHKMEMQ; 975e7788613SBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP); 976e9a2bbcdSBarry Smith CHKMEMQ; 977d64ed03dSBarry Smith PetscStackPop; 978d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 97919717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr); 98019717074SBarry Smith } 981d5e45103SBarry Smith CHKERRQ(ierr); 9821096aae1SMatthew Knepley } else if (snes->afine) { 9831096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 9841096aae1SMatthew Knepley } else { 9851096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 9861096aae1SMatthew Knepley } 9873ab0aad5SBarry Smith if (snes->afine) { 988016dedfbSBarry Smith ierr = VecAXPY(y,-1.0,snes->afine);CHKERRQ(ierr); 9893ab0aad5SBarry Smith } 990ae3c334cSLois Curfman McInnes snes->nfuncs++; 991d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 9923a40ed3dSBarry Smith PetscFunctionReturn(0); 9939b94acceSBarry Smith } 9949b94acceSBarry Smith 9954a2ae208SSatish Balay #undef __FUNCT__ 9964a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 99762fef451SLois Curfman McInnes /*@ 99862fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 99962fef451SLois Curfman McInnes set with SNESSetJacobian(). 100062fef451SLois Curfman McInnes 1001c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1002c7afd0dbSLois Curfman McInnes 100362fef451SLois Curfman McInnes Input Parameters: 1004c7afd0dbSLois Curfman McInnes + snes - the SNES context 1005c7afd0dbSLois Curfman McInnes - x - input vector 100662fef451SLois Curfman McInnes 100762fef451SLois Curfman McInnes Output Parameters: 1008c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 100962fef451SLois Curfman McInnes . B - optional preconditioning matrix 10102b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1011fee21e36SBarry Smith 101262fef451SLois Curfman McInnes Notes: 101362fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 101462fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 101562fef451SLois Curfman McInnes 101694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1017dc5a77f8SLois Curfman McInnes flag parameter. 101862fef451SLois Curfman McInnes 101936851e7fSLois Curfman McInnes Level: developer 102036851e7fSLois Curfman McInnes 102162fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 102262fef451SLois Curfman McInnes 10232b668275SBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure 102462fef451SLois Curfman McInnes @*/ 102563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10269b94acceSBarry Smith { 1027dfbe8321SBarry Smith PetscErrorCode ierr; 10283a40ed3dSBarry Smith 10293a40ed3dSBarry Smith PetscFunctionBegin; 10304482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10314482741eSBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE,2); 10324482741eSBarry Smith PetscValidPointer(flg,5); 1033c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1034e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1035d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1036c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1037d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1038dc67937bSBarry Smith CHKMEMQ; 1039e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1040dc67937bSBarry Smith CHKMEMQ; 1041d64ed03dSBarry Smith PetscStackPop; 1042d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 10436d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 10446ce558aeSBarry Smith /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3); 10456ce558aeSBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE,4); */ 10463a40ed3dSBarry Smith PetscFunctionReturn(0); 10479b94acceSBarry Smith } 10489b94acceSBarry Smith 10494a2ae208SSatish Balay #undef __FUNCT__ 10504a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 10519b94acceSBarry Smith /*@C 10529b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1053044dda88SLois Curfman McInnes location to store the matrix. 10549b94acceSBarry Smith 1055c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1056c7afd0dbSLois Curfman McInnes 10579b94acceSBarry Smith Input Parameters: 1058c7afd0dbSLois Curfman McInnes + snes - the SNES context 10599b94acceSBarry Smith . A - Jacobian matrix 10609b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 10619b94acceSBarry Smith . func - Jacobian evaluation routine 1062c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 10632cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 10649b94acceSBarry Smith 10659b94acceSBarry Smith Calling sequence of func: 10668d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 10679b94acceSBarry Smith 1068c7afd0dbSLois Curfman McInnes + x - input vector 10699b94acceSBarry Smith . A - Jacobian matrix 10709b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1071ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 10722b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1073c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 10749b94acceSBarry Smith 10759b94acceSBarry Smith Notes: 107694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 10772cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1078ac21db08SLois Curfman McInnes 1079ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 10809b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 10819b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 10829b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 10839b94acceSBarry Smith throughout the global iterations. 10849b94acceSBarry Smith 108536851e7fSLois Curfman McInnes Level: beginner 108636851e7fSLois Curfman McInnes 10879b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 10889b94acceSBarry Smith 10893ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 10909b94acceSBarry Smith @*/ 109163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 10929b94acceSBarry Smith { 1093dfbe8321SBarry Smith PetscErrorCode ierr; 10943a7fca6bSBarry Smith 10953a40ed3dSBarry Smith PetscFunctionBegin; 10964482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10974482741eSBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2); 10984482741eSBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3); 1099c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 1100c9780b6fSBarry Smith if (B) PetscCheckSameComm(snes,1,B,2); 1101e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 11023a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 11033a7fca6bSBarry Smith if (A) { 11047dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 11053a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 11069b94acceSBarry Smith snes->jacobian = A; 11073a7fca6bSBarry Smith } 11083a7fca6bSBarry Smith if (B) { 11097dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 11103a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 11119b94acceSBarry Smith snes->jacobian_pre = B; 11123a7fca6bSBarry Smith } 11133a40ed3dSBarry Smith PetscFunctionReturn(0); 11149b94acceSBarry Smith } 111562fef451SLois Curfman McInnes 11164a2ae208SSatish Balay #undef __FUNCT__ 11174a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1118c2aafc4cSSatish Balay /*@C 1119b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1120b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1121b4fd4287SBarry Smith 1122c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1123c7afd0dbSLois Curfman McInnes 1124b4fd4287SBarry Smith Input Parameter: 1125b4fd4287SBarry Smith . snes - the nonlinear solver context 1126b4fd4287SBarry Smith 1127b4fd4287SBarry Smith Output Parameters: 1128c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1129b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 113070e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 113170e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1132fee21e36SBarry Smith 113336851e7fSLois Curfman McInnes Level: advanced 113436851e7fSLois Curfman McInnes 1135b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1136b4fd4287SBarry Smith @*/ 113770e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1138b4fd4287SBarry Smith { 11393a40ed3dSBarry Smith PetscFunctionBegin; 11404482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1141b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1142b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1143e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 114470e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 11453a40ed3dSBarry Smith PetscFunctionReturn(0); 1146b4fd4287SBarry Smith } 1147b4fd4287SBarry Smith 11489b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 114963dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 11509b94acceSBarry Smith 11514a2ae208SSatish Balay #undef __FUNCT__ 11524a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 11539b94acceSBarry Smith /*@ 11549b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1155272ac6f2SLois Curfman McInnes of a nonlinear solver. 11569b94acceSBarry Smith 1157fee21e36SBarry Smith Collective on SNES 1158fee21e36SBarry Smith 1159c7afd0dbSLois Curfman McInnes Input Parameters: 116070e92668SMatthew Knepley . snes - the SNES context 1161c7afd0dbSLois Curfman McInnes 1162272ac6f2SLois Curfman McInnes Notes: 1163272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1164272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1165272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1166272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1167272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1168272ac6f2SLois Curfman McInnes 116936851e7fSLois Curfman McInnes Level: advanced 117036851e7fSLois Curfman McInnes 11719b94acceSBarry Smith .keywords: SNES, nonlinear, setup 11729b94acceSBarry Smith 11739b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 11749b94acceSBarry Smith @*/ 117570e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes) 11769b94acceSBarry Smith { 1177dfbe8321SBarry Smith PetscErrorCode ierr; 117871f87433Sdalcinl PetscTruth flg; 11793a40ed3dSBarry Smith 11803a40ed3dSBarry Smith PetscFunctionBegin; 11814482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11824dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 11839b94acceSBarry Smith 1184b0a32e0cSBarry Smith ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1185c1f60f51SBarry Smith /* 1186c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1187dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1188c1f60f51SBarry Smith */ 1189c1f60f51SBarry Smith if (flg) { 1190c1f60f51SBarry Smith Mat J; 1191fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 11923ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 1193ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 11943a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 11953a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1196c1f60f51SBarry Smith } 119745fc7adcSBarry Smith 119803c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT) 119945fc7adcSBarry Smith ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr); 120045fc7adcSBarry Smith if (flg) { 120145fc7adcSBarry Smith Mat J; 120245fc7adcSBarry Smith ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr); 120375396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr); 120445fc7adcSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 120545fc7adcSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 120645fc7adcSBarry Smith } 120732a4b47aSMatthew Knepley #endif 120845fc7adcSBarry Smith 1209b0a32e0cSBarry Smith ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1210c1f60f51SBarry Smith /* 1211dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1212c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1213c1f60f51SBarry Smith */ 121431615d04SLois Curfman McInnes if (flg) { 1215272ac6f2SLois Curfman McInnes Mat J; 1216b5d62d44SBarry Smith KSP ksp; 121794b7f48cSBarry Smith PC pc; 121875396ef9SLisandro Dalcin /* create and set matrix-free operator */ 1219fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12203ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 122175396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12223ec795f1SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 12233a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1224f3ef73ceSBarry Smith /* force no preconditioner */ 122594b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 1226b5d62d44SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 1227a9815358SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 1228a9815358SBarry Smith if (!flg) { 122975396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 1230f3ef73ceSBarry Smith ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 1231272ac6f2SLois Curfman McInnes } 1232a9815358SBarry Smith } 1233f3ef73ceSBarry Smith 12341096aae1SMatthew Knepley if (!snes->vec_func && !snes->afine) { 12351096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12361096aae1SMatthew Knepley } 1237e7788613SBarry Smith if (!snes->ops->computefunction && !snes->afine) { 12381096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12391096aae1SMatthew Knepley } 124075396ef9SLisandro Dalcin if (!snes->jacobian) { 124175396ef9SLisandro Dalcin SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 124275396ef9SLisandro Dalcin } 1243a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 124429bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1245a8c6a408SBarry Smith } 1246a703fe33SLois Curfman McInnes 1247410397dcSLisandro Dalcin if (!snes->type_name) { 1248410397dcSLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 1249410397dcSLisandro Dalcin } 1250410397dcSLisandro Dalcin if (snes->ops->setup) { 1251410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1252410397dcSLisandro Dalcin } 12537aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 12543a40ed3dSBarry Smith PetscFunctionReturn(0); 12559b94acceSBarry Smith } 12569b94acceSBarry Smith 12574a2ae208SSatish Balay #undef __FUNCT__ 12584a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 125952baeb72SSatish Balay /*@ 12609b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 12619b94acceSBarry Smith with SNESCreate(). 12629b94acceSBarry Smith 1263c7afd0dbSLois Curfman McInnes Collective on SNES 1264c7afd0dbSLois Curfman McInnes 12659b94acceSBarry Smith Input Parameter: 12669b94acceSBarry Smith . snes - the SNES context 12679b94acceSBarry Smith 126836851e7fSLois Curfman McInnes Level: beginner 126936851e7fSLois Curfman McInnes 12709b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 12719b94acceSBarry Smith 127263a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 12739b94acceSBarry Smith @*/ 127463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes) 12759b94acceSBarry Smith { 12766849ba73SBarry Smith PetscErrorCode ierr; 12773a40ed3dSBarry Smith 12783a40ed3dSBarry Smith PetscFunctionBegin; 12794482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 12803a40ed3dSBarry Smith if (--snes->refct > 0) PetscFunctionReturn(0); 1281d4bb536fSBarry Smith 1282be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 12830f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1284be0abb6dSBarry Smith 1285e7788613SBarry Smith if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);} 128605b42c5fSBarry Smith ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr); 12873a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 12883a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 12893ab0aad5SBarry Smith if (snes->afine) {ierr = VecDestroy(snes->afine);CHKERRQ(ierr);} 129094b7f48cSBarry Smith ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr); 1291522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1292a6570f20SBarry Smith ierr = SNESMonitorCancel(snes);CHKERRQ(ierr); 1293a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 12943a40ed3dSBarry Smith PetscFunctionReturn(0); 12959b94acceSBarry Smith } 12969b94acceSBarry Smith 12979b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 12989b94acceSBarry Smith 12994a2ae208SSatish Balay #undef __FUNCT__ 13004a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 13019b94acceSBarry Smith /*@ 1302d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 13039b94acceSBarry Smith 1304c7afd0dbSLois Curfman McInnes Collective on SNES 1305c7afd0dbSLois Curfman McInnes 13069b94acceSBarry Smith Input Parameters: 1307c7afd0dbSLois Curfman McInnes + snes - the SNES context 130870441072SBarry Smith . abstol - absolute convergence tolerance 130933174efeSLois Curfman McInnes . rtol - relative convergence tolerance 131033174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 131133174efeSLois Curfman McInnes of the change in the solution between steps 131233174efeSLois Curfman McInnes . maxit - maximum number of iterations 1313c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1314fee21e36SBarry Smith 131533174efeSLois Curfman McInnes Options Database Keys: 131670441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1317c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1318c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1319c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1320c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 13219b94acceSBarry Smith 1322d7a720efSLois Curfman McInnes Notes: 13239b94acceSBarry Smith The default maximum number of iterations is 50. 13249b94acceSBarry Smith The default maximum number of function evaluations is 1000. 13259b94acceSBarry Smith 132636851e7fSLois Curfman McInnes Level: intermediate 132736851e7fSLois Curfman McInnes 132833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 13299b94acceSBarry Smith 13302492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 13319b94acceSBarry Smith @*/ 133263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 13339b94acceSBarry Smith { 13343a40ed3dSBarry Smith PetscFunctionBegin; 13354482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 133670441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1337d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1338d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1339d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1340d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 13413a40ed3dSBarry Smith PetscFunctionReturn(0); 13429b94acceSBarry Smith } 13439b94acceSBarry Smith 13444a2ae208SSatish Balay #undef __FUNCT__ 13454a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 13469b94acceSBarry Smith /*@ 134733174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 134833174efeSLois Curfman McInnes 1349c7afd0dbSLois Curfman McInnes Not Collective 1350c7afd0dbSLois Curfman McInnes 135133174efeSLois Curfman McInnes Input Parameters: 1352c7afd0dbSLois Curfman McInnes + snes - the SNES context 135370441072SBarry Smith . abstol - absolute convergence tolerance 135433174efeSLois Curfman McInnes . rtol - relative convergence tolerance 135533174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 135633174efeSLois Curfman McInnes of the change in the solution between steps 135733174efeSLois Curfman McInnes . maxit - maximum number of iterations 1358c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1359fee21e36SBarry Smith 136033174efeSLois Curfman McInnes Notes: 136133174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 136233174efeSLois Curfman McInnes 136336851e7fSLois Curfman McInnes Level: intermediate 136436851e7fSLois Curfman McInnes 136533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 136633174efeSLois Curfman McInnes 136733174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 136833174efeSLois Curfman McInnes @*/ 136963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *abstol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 137033174efeSLois Curfman McInnes { 13713a40ed3dSBarry Smith PetscFunctionBegin; 13724482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 137370441072SBarry Smith if (abstol) *abstol = snes->abstol; 137433174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 137533174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 137633174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 137733174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 13783a40ed3dSBarry Smith PetscFunctionReturn(0); 137933174efeSLois Curfman McInnes } 138033174efeSLois Curfman McInnes 13814a2ae208SSatish Balay #undef __FUNCT__ 13824a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 138333174efeSLois Curfman McInnes /*@ 13849b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 13859b94acceSBarry Smith 1386fee21e36SBarry Smith Collective on SNES 1387fee21e36SBarry Smith 1388c7afd0dbSLois Curfman McInnes Input Parameters: 1389c7afd0dbSLois Curfman McInnes + snes - the SNES context 1390c7afd0dbSLois Curfman McInnes - tol - tolerance 1391c7afd0dbSLois Curfman McInnes 13929b94acceSBarry Smith Options Database Key: 1393c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 13949b94acceSBarry Smith 139536851e7fSLois Curfman McInnes Level: intermediate 139636851e7fSLois Curfman McInnes 13979b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 13989b94acceSBarry Smith 13992492ecdbSBarry Smith .seealso: SNESSetTolerances() 14009b94acceSBarry Smith @*/ 140163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 14029b94acceSBarry Smith { 14033a40ed3dSBarry Smith PetscFunctionBegin; 14044482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 14059b94acceSBarry Smith snes->deltatol = tol; 14063a40ed3dSBarry Smith PetscFunctionReturn(0); 14079b94acceSBarry Smith } 14089b94acceSBarry Smith 1409df9fa365SBarry Smith /* 1410df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1411df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1412df9fa365SBarry Smith macros instead of functions 1413df9fa365SBarry Smith */ 14144a2ae208SSatish Balay #undef __FUNCT__ 1415a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 1416a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1417ce1608b8SBarry Smith { 1418dfbe8321SBarry Smith PetscErrorCode ierr; 1419ce1608b8SBarry Smith 1420ce1608b8SBarry Smith PetscFunctionBegin; 14214482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1422a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1423ce1608b8SBarry Smith PetscFunctionReturn(0); 1424ce1608b8SBarry Smith } 1425ce1608b8SBarry Smith 14264a2ae208SSatish Balay #undef __FUNCT__ 1427a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 1428a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1429df9fa365SBarry Smith { 1430dfbe8321SBarry Smith PetscErrorCode ierr; 1431df9fa365SBarry Smith 1432df9fa365SBarry Smith PetscFunctionBegin; 1433a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1434df9fa365SBarry Smith PetscFunctionReturn(0); 1435df9fa365SBarry Smith } 1436df9fa365SBarry Smith 14374a2ae208SSatish Balay #undef __FUNCT__ 1438a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 1439a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw) 1440df9fa365SBarry Smith { 1441dfbe8321SBarry Smith PetscErrorCode ierr; 1442df9fa365SBarry Smith 1443df9fa365SBarry Smith PetscFunctionBegin; 1444a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1445df9fa365SBarry Smith PetscFunctionReturn(0); 1446df9fa365SBarry Smith } 1447df9fa365SBarry Smith 14489b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 14499b94acceSBarry Smith 14504a2ae208SSatish Balay #undef __FUNCT__ 1451a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 14529b94acceSBarry Smith /*@C 1453a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 14549b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 14559b94acceSBarry Smith progress. 14569b94acceSBarry Smith 1457fee21e36SBarry Smith Collective on SNES 1458fee21e36SBarry Smith 1459c7afd0dbSLois Curfman McInnes Input Parameters: 1460c7afd0dbSLois Curfman McInnes + snes - the SNES context 1461c7afd0dbSLois Curfman McInnes . func - monitoring routine 1462b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1463e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 1464b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1465b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 14669b94acceSBarry Smith 1467c7afd0dbSLois Curfman McInnes Calling sequence of func: 1468a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 1469c7afd0dbSLois Curfman McInnes 1470c7afd0dbSLois Curfman McInnes + snes - the SNES context 1471c7afd0dbSLois Curfman McInnes . its - iteration number 1472c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 147340a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 14749b94acceSBarry Smith 14759665c990SLois Curfman McInnes Options Database Keys: 1476a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 1477a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 1478a6570f20SBarry Smith uses SNESMonitorLGCreate() 1479a6570f20SBarry Smith _ -snes_monitor_cancel - cancels all monitors that have 1480c7afd0dbSLois Curfman McInnes been hardwired into a code by 1481a6570f20SBarry Smith calls to SNESMonitorSet(), but 1482c7afd0dbSLois Curfman McInnes does not cancel those set via 1483c7afd0dbSLois Curfman McInnes the options database. 14849665c990SLois Curfman McInnes 1485639f9d9dSBarry Smith Notes: 14866bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 1487a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 14886bc08f3fSLois Curfman McInnes order in which they were set. 1489639f9d9dSBarry Smith 149036851e7fSLois Curfman McInnes Level: intermediate 149136851e7fSLois Curfman McInnes 14929b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 14939b94acceSBarry Smith 1494a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 14959b94acceSBarry Smith @*/ 1496b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 14979b94acceSBarry Smith { 1498b90d0a6eSBarry Smith PetscInt i; 1499b90d0a6eSBarry Smith 15003a40ed3dSBarry Smith PetscFunctionBegin; 15014482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1502639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 150329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1504639f9d9dSBarry Smith } 1505b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 1506b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 1507b90d0a6eSBarry Smith 1508b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 1509b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 1510b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 1511b90d0a6eSBarry Smith PetscErrorCode ierr; 1512b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 1513b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 1514b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 1515b90d0a6eSBarry Smith ierr = (*monitordestroy)(mctx);CHKERRQ(ierr); 1516b90d0a6eSBarry Smith PetscFunctionReturn(0); 1517b90d0a6eSBarry Smith } 1518b90d0a6eSBarry Smith } 1519b90d0a6eSBarry Smith } 1520b90d0a6eSBarry Smith } 1521b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 1522b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1523639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 15243a40ed3dSBarry Smith PetscFunctionReturn(0); 15259b94acceSBarry Smith } 15269b94acceSBarry Smith 15274a2ae208SSatish Balay #undef __FUNCT__ 1528a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 15295cd90555SBarry Smith /*@C 1530a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 15315cd90555SBarry Smith 1532c7afd0dbSLois Curfman McInnes Collective on SNES 1533c7afd0dbSLois Curfman McInnes 15345cd90555SBarry Smith Input Parameters: 15355cd90555SBarry Smith . snes - the SNES context 15365cd90555SBarry Smith 15371a480d89SAdministrator Options Database Key: 1538a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 1539a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 1540c7afd0dbSLois Curfman McInnes set via the options database 15415cd90555SBarry Smith 15425cd90555SBarry Smith Notes: 15435cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 15445cd90555SBarry Smith 154536851e7fSLois Curfman McInnes Level: intermediate 154636851e7fSLois Curfman McInnes 15475cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 15485cd90555SBarry Smith 1549a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 15505cd90555SBarry Smith @*/ 1551a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes) 15525cd90555SBarry Smith { 1553d952e501SBarry Smith PetscErrorCode ierr; 1554d952e501SBarry Smith PetscInt i; 1555d952e501SBarry Smith 15565cd90555SBarry Smith PetscFunctionBegin; 15574482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1558d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1559d952e501SBarry Smith if (snes->monitordestroy[i]) { 1560d952e501SBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1561d952e501SBarry Smith } 1562d952e501SBarry Smith } 15635cd90555SBarry Smith snes->numbermonitors = 0; 15645cd90555SBarry Smith PetscFunctionReturn(0); 15655cd90555SBarry Smith } 15665cd90555SBarry Smith 15674a2ae208SSatish Balay #undef __FUNCT__ 15684a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 15699b94acceSBarry Smith /*@C 15709b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 15719b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 15729b94acceSBarry Smith 1573fee21e36SBarry Smith Collective on SNES 1574fee21e36SBarry Smith 1575c7afd0dbSLois Curfman McInnes Input Parameters: 1576c7afd0dbSLois Curfman McInnes + snes - the SNES context 1577c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1578c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1579c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 15809b94acceSBarry Smith 1581c7afd0dbSLois Curfman McInnes Calling sequence of func: 158206ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1583c7afd0dbSLois Curfman McInnes 1584c7afd0dbSLois Curfman McInnes + snes - the SNES context 158506ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 1586c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1587184914b5SBarry Smith . reason - reason for convergence/divergence 1588c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 15894b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 15904b27c08aSLois Curfman McInnes - f - 2-norm of function 15919b94acceSBarry Smith 159236851e7fSLois Curfman McInnes Level: advanced 159336851e7fSLois Curfman McInnes 15949b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 15959b94acceSBarry Smith 15963f149594SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged(), SNESConverged_LS(), SNESConverged_TR() 15979b94acceSBarry Smith @*/ 159806ee9f85SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx) 15999b94acceSBarry Smith { 16003a40ed3dSBarry Smith PetscFunctionBegin; 16014482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1602e7788613SBarry Smith (snes)->ops->converged = func; 16039b94acceSBarry Smith (snes)->cnvP = cctx; 16043a40ed3dSBarry Smith PetscFunctionReturn(0); 16059b94acceSBarry Smith } 16069b94acceSBarry Smith 16074a2ae208SSatish Balay #undef __FUNCT__ 16084a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 160952baeb72SSatish Balay /*@ 1610184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1611184914b5SBarry Smith 1612184914b5SBarry Smith Not Collective 1613184914b5SBarry Smith 1614184914b5SBarry Smith Input Parameter: 1615184914b5SBarry Smith . snes - the SNES context 1616184914b5SBarry Smith 1617184914b5SBarry Smith Output Parameter: 1618e090d566SSatish Balay . reason - negative value indicates diverged, positive value converged, see petscsnes.h or the 1619184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1620184914b5SBarry Smith 1621184914b5SBarry Smith Level: intermediate 1622184914b5SBarry Smith 1623184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1624184914b5SBarry Smith 1625184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1626184914b5SBarry Smith 16274b27c08aSLois Curfman McInnes .seealso: SNESSetConvergenceTest(), SNESConverged_LS(), SNESConverged_TR(), SNESConvergedReason 1628184914b5SBarry Smith @*/ 162963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1630184914b5SBarry Smith { 1631184914b5SBarry Smith PetscFunctionBegin; 16324482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16334482741eSBarry Smith PetscValidPointer(reason,2); 1634184914b5SBarry Smith *reason = snes->reason; 1635184914b5SBarry Smith PetscFunctionReturn(0); 1636184914b5SBarry Smith } 1637184914b5SBarry Smith 16384a2ae208SSatish Balay #undef __FUNCT__ 16394a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 1640c9005455SLois Curfman McInnes /*@ 1641c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1642c9005455SLois Curfman McInnes 1643fee21e36SBarry Smith Collective on SNES 1644fee21e36SBarry Smith 1645c7afd0dbSLois Curfman McInnes Input Parameters: 1646c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1647c7afd0dbSLois Curfman McInnes . a - array to hold history 1648cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 1649758f92a0SBarry Smith . na - size of a and its 165064731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 1651758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1652c7afd0dbSLois Curfman McInnes 1653c9005455SLois Curfman McInnes Notes: 16544b27c08aSLois Curfman McInnes If set, this array will contain the function norms computed 1655c9005455SLois Curfman McInnes at each step. 1656c9005455SLois Curfman McInnes 1657c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1658c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1659c9005455SLois Curfman McInnes during the section of code that is being timed. 1660c9005455SLois Curfman McInnes 166136851e7fSLois Curfman McInnes Level: intermediate 166236851e7fSLois Curfman McInnes 1663c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1664758f92a0SBarry Smith 166508405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1666758f92a0SBarry Smith 1667c9005455SLois Curfman McInnes @*/ 1668a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset) 1669c9005455SLois Curfman McInnes { 16703a40ed3dSBarry Smith PetscFunctionBegin; 16714482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16724482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 1673a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 1674c9005455SLois Curfman McInnes snes->conv_hist = a; 1675758f92a0SBarry Smith snes->conv_hist_its = its; 1676758f92a0SBarry Smith snes->conv_hist_max = na; 1677758f92a0SBarry Smith snes->conv_hist_reset = reset; 1678758f92a0SBarry Smith PetscFunctionReturn(0); 1679758f92a0SBarry Smith } 1680758f92a0SBarry Smith 16814a2ae208SSatish Balay #undef __FUNCT__ 16824a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 16830c4c9dddSBarry Smith /*@C 1684758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1685758f92a0SBarry Smith 1686758f92a0SBarry Smith Collective on SNES 1687758f92a0SBarry Smith 1688758f92a0SBarry Smith Input Parameter: 1689758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1690758f92a0SBarry Smith 1691758f92a0SBarry Smith Output Parameters: 1692758f92a0SBarry Smith . a - array to hold history 1693758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1694758f92a0SBarry Smith negative if not converged) for each solve. 1695758f92a0SBarry Smith - na - size of a and its 1696758f92a0SBarry Smith 1697758f92a0SBarry Smith Notes: 1698758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1699758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1700758f92a0SBarry Smith 1701758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1702758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1703758f92a0SBarry Smith during the section of code that is being timed. 1704758f92a0SBarry Smith 1705758f92a0SBarry Smith Level: intermediate 1706758f92a0SBarry Smith 1707758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1708758f92a0SBarry Smith 1709758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1710758f92a0SBarry Smith 1711758f92a0SBarry Smith @*/ 171263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 1713758f92a0SBarry Smith { 1714758f92a0SBarry Smith PetscFunctionBegin; 17154482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1716758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1717758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1718758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 17193a40ed3dSBarry Smith PetscFunctionReturn(0); 1720c9005455SLois Curfman McInnes } 1721c9005455SLois Curfman McInnes 1722e74ef692SMatthew Knepley #undef __FUNCT__ 1723e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 1724ac226902SBarry Smith /*@C 172576b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 17267e4bb74cSBarry Smith at the beginning o every iteration of the nonlinear solve. Specifically 17277e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 172876b2cf59SMatthew Knepley 172976b2cf59SMatthew Knepley Collective on SNES 173076b2cf59SMatthew Knepley 173176b2cf59SMatthew Knepley Input Parameters: 173276b2cf59SMatthew Knepley . snes - The nonlinear solver context 173376b2cf59SMatthew Knepley . func - The function 173476b2cf59SMatthew Knepley 173576b2cf59SMatthew Knepley Calling sequence of func: 1736b5d30489SBarry Smith . func (SNES snes, PetscInt step); 173776b2cf59SMatthew Knepley 173876b2cf59SMatthew Knepley . step - The current step of the iteration 173976b2cf59SMatthew Knepley 174076b2cf59SMatthew Knepley Level: intermediate 174176b2cf59SMatthew Knepley 174276b2cf59SMatthew Knepley .keywords: SNES, update 1743b5d30489SBarry Smith 174476b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC() 174576b2cf59SMatthew Knepley @*/ 174663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 174776b2cf59SMatthew Knepley { 174876b2cf59SMatthew Knepley PetscFunctionBegin; 17494482741eSBarry Smith PetscValidHeaderSpecific(snes, SNES_COOKIE,1); 1750e7788613SBarry Smith snes->ops->update = func; 175176b2cf59SMatthew Knepley PetscFunctionReturn(0); 175276b2cf59SMatthew Knepley } 175376b2cf59SMatthew Knepley 1754e74ef692SMatthew Knepley #undef __FUNCT__ 1755e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 175676b2cf59SMatthew Knepley /*@ 175776b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 175876b2cf59SMatthew Knepley 175976b2cf59SMatthew Knepley Not collective 176076b2cf59SMatthew Knepley 176176b2cf59SMatthew Knepley Input Parameters: 176276b2cf59SMatthew Knepley . snes - The nonlinear solver context 176376b2cf59SMatthew Knepley . step - The current step of the iteration 176476b2cf59SMatthew Knepley 1765205452f4SMatthew Knepley Level: intermediate 1766205452f4SMatthew Knepley 176776b2cf59SMatthew Knepley .keywords: SNES, update 1768a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 176976b2cf59SMatthew Knepley @*/ 177063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step) 177176b2cf59SMatthew Knepley { 177276b2cf59SMatthew Knepley PetscFunctionBegin; 177376b2cf59SMatthew Knepley PetscFunctionReturn(0); 177476b2cf59SMatthew Knepley } 177576b2cf59SMatthew Knepley 17764a2ae208SSatish Balay #undef __FUNCT__ 17774a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 17789b94acceSBarry Smith /* 17799b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 17809b94acceSBarry Smith positive parameter delta. 17819b94acceSBarry Smith 17829b94acceSBarry Smith Input Parameters: 1783c7afd0dbSLois Curfman McInnes + snes - the SNES context 17849b94acceSBarry Smith . y - approximate solution of linear system 17859b94acceSBarry Smith . fnorm - 2-norm of current function 1786c7afd0dbSLois Curfman McInnes - delta - trust region size 17879b94acceSBarry Smith 17889b94acceSBarry Smith Output Parameters: 1789c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 17909b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 17919b94acceSBarry Smith region, and exceeds zero otherwise. 1792c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 17939b94acceSBarry Smith 17949b94acceSBarry Smith Note: 17954b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 17969b94acceSBarry Smith is set to be the maximum allowable step size. 17979b94acceSBarry Smith 17989b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 17999b94acceSBarry Smith */ 1800dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 18019b94acceSBarry Smith { 1802064f8208SBarry Smith PetscReal nrm; 1803ea709b57SSatish Balay PetscScalar cnorm; 1804dfbe8321SBarry Smith PetscErrorCode ierr; 18053a40ed3dSBarry Smith 18063a40ed3dSBarry Smith PetscFunctionBegin; 18074482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 18084482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,2); 1809c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 1810184914b5SBarry Smith 1811064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 1812064f8208SBarry Smith if (nrm > *delta) { 1813064f8208SBarry Smith nrm = *delta/nrm; 1814064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 1815064f8208SBarry Smith cnorm = nrm; 18162dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 18179b94acceSBarry Smith *ynorm = *delta; 18189b94acceSBarry Smith } else { 18199b94acceSBarry Smith *gpnorm = 0.0; 1820064f8208SBarry Smith *ynorm = nrm; 18219b94acceSBarry Smith } 18223a40ed3dSBarry Smith PetscFunctionReturn(0); 18239b94acceSBarry Smith } 18249b94acceSBarry Smith 18254a2ae208SSatish Balay #undef __FUNCT__ 18264a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 18276ce558aeSBarry Smith /*@C 1828f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 1829f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 18309b94acceSBarry Smith 1831c7afd0dbSLois Curfman McInnes Collective on SNES 1832c7afd0dbSLois Curfman McInnes 1833b2002411SLois Curfman McInnes Input Parameters: 1834c7afd0dbSLois Curfman McInnes + snes - the SNES context 1835f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 183670e92668SMatthew Knepley - x - the solution vector, or PETSC_NULL if it was set with SNESSetSolution() 18379b94acceSBarry Smith 1838b2002411SLois Curfman McInnes Notes: 18398ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 18408ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 18418ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 18428ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 18438ddd3da0SLois Curfman McInnes 184436851e7fSLois Curfman McInnes Level: beginner 184536851e7fSLois Curfman McInnes 18469b94acceSBarry Smith .keywords: SNES, nonlinear, solve 18479b94acceSBarry Smith 184870e92668SMatthew Knepley .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetRhs(), SNESSetSolution() 18499b94acceSBarry Smith @*/ 1850f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x) 18519b94acceSBarry Smith { 1852dfbe8321SBarry Smith PetscErrorCode ierr; 1853f1af5d2fSBarry Smith PetscTruth flg; 1854eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 1855eabae89aSBarry Smith PetscViewer viewer; 1856052efed2SBarry Smith 18573a40ed3dSBarry Smith PetscFunctionBegin; 18584482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1859e7788613SBarry Smith if (!snes->ops->solve) SETERRQ(PETSC_ERR_ORDER,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()"); 186074637425SBarry Smith 1861f69a0ea3SMatthew Knepley if (b) { 1862f69a0ea3SMatthew Knepley ierr = SNESSetRhs(snes, b); CHKERRQ(ierr); 18631096aae1SMatthew Knepley if (!snes->vec_func) { 18641096aae1SMatthew Knepley Vec r; 18651096aae1SMatthew Knepley 18661096aae1SMatthew Knepley ierr = VecDuplicate(b, &r); CHKERRQ(ierr); 18671096aae1SMatthew Knepley ierr = SNESSetFunction(snes, r, PETSC_NULL, PETSC_NULL); CHKERRQ(ierr); 18681096aae1SMatthew Knepley } 1869f69a0ea3SMatthew Knepley } 187070e92668SMatthew Knepley if (x) { 1871f69a0ea3SMatthew Knepley PetscValidHeaderSpecific(x,VEC_COOKIE,3); 1872f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 187370e92668SMatthew Knepley } else { 187470e92668SMatthew Knepley ierr = SNESGetSolution(snes, &x); CHKERRQ(ierr); 187570e92668SMatthew Knepley if (!x) { 187670e92668SMatthew Knepley ierr = VecDuplicate(snes->vec_func_always, &x); CHKERRQ(ierr); 187770e92668SMatthew Knepley } 187870e92668SMatthew Knepley } 187970e92668SMatthew Knepley snes->vec_sol = snes->vec_sol_always = x; 18803f149594SLisandro Dalcin 188170e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 18823f149594SLisandro Dalcin 1883abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 188450ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 1885d5e45103SBarry Smith 18863f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 18873f149594SLisandro Dalcin 1888e7788613SBarry Smith ierr = PetscExceptionTry1((*(snes)->ops->solve)(snes),PETSC_ERR_ARG_DOMAIN); 1889d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 189038f152feSBarry Smith /* this means that a caller above me has also tryed this exception so I don't handle it here, pass it up */ 189119717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(pierr); 1892c671dbe7SSatish Balay } else if (PetscExceptionCaught(ierr,PETSC_ERR_ARG_DOMAIN)) { 1893d5e45103SBarry Smith /* translate exception into SNES not converged reason */ 1894d5e45103SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 189538f152feSBarry Smith ierr = 0; 189638f152feSBarry Smith } 189738f152feSBarry Smith CHKERRQ(ierr); 18983f149594SLisandro Dalcin if (!snes->reason) { 18993f149594SLisandro Dalcin SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 19003f149594SLisandro Dalcin } 19013f149594SLisandro Dalcin if (!snes->ops->converged && snes->reason == SNES_DIVERGED_MAX_IT) { 19023f149594SLisandro Dalcin snes->reason = SNES_CONVERGED_ITS; 19033f149594SLisandro Dalcin } 1904d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 19053f149594SLisandro Dalcin 1906eabae89aSBarry Smith ierr = PetscOptionsGetString(snes->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1907eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 1908eabae89aSBarry Smith ierr = PetscViewerASCIIOpen(snes->comm,filename,&viewer);CHKERRQ(ierr); 1909eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 1910eabae89aSBarry Smith ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 1911eabae89aSBarry Smith } 1912eabae89aSBarry Smith 1913da9b6338SBarry Smith ierr = PetscOptionsHasName(snes->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr); 1914da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 19155968eb51SBarry Smith if (snes->printreason) { 19165968eb51SBarry Smith if (snes->reason > 0) { 19179dcbbd2bSBarry Smith ierr = PetscPrintf(snes->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 19185968eb51SBarry Smith } else { 19199dcbbd2bSBarry Smith ierr = PetscPrintf(snes->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 19205968eb51SBarry Smith } 19215968eb51SBarry Smith } 19225968eb51SBarry Smith 19233a40ed3dSBarry Smith PetscFunctionReturn(0); 19249b94acceSBarry Smith } 19259b94acceSBarry Smith 19269b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 19279b94acceSBarry Smith 19284a2ae208SSatish Balay #undef __FUNCT__ 19294a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 193082bf6240SBarry Smith /*@C 19314b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 19329b94acceSBarry Smith 1933fee21e36SBarry Smith Collective on SNES 1934fee21e36SBarry Smith 1935c7afd0dbSLois Curfman McInnes Input Parameters: 1936c7afd0dbSLois Curfman McInnes + snes - the SNES context 1937454a90a3SBarry Smith - type - a known method 1938c7afd0dbSLois Curfman McInnes 1939c7afd0dbSLois Curfman McInnes Options Database Key: 1940454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 1941c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 1942ae12b187SLois Curfman McInnes 19439b94acceSBarry Smith Notes: 1944e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 19454b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 1946c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 19474b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 1948c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 19499b94acceSBarry Smith 1950ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 1951ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 1952ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 1953ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 1954ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 1955ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 1956ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 1957ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 1958ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 1959b0a32e0cSBarry Smith appropriate method. 196036851e7fSLois Curfman McInnes 196136851e7fSLois Curfman McInnes Level: intermediate 1962a703fe33SLois Curfman McInnes 1963454a90a3SBarry Smith .keywords: SNES, set, type 1964435da068SBarry Smith 1965435da068SBarry Smith .seealso: SNESType, SNESCreate() 1966435da068SBarry Smith 19679b94acceSBarry Smith @*/ 1968e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,SNESType type) 19699b94acceSBarry Smith { 1970dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 19716831982aSBarry Smith PetscTruth match; 19723a40ed3dSBarry Smith 19733a40ed3dSBarry Smith PetscFunctionBegin; 19744482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 19754482741eSBarry Smith PetscValidCharPointer(type,2); 197682bf6240SBarry Smith 19776831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 19780f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 197992ff6ae8SBarry Smith 19801d280d73SBarry Smith ierr = PetscFListFind(SNESList,snes->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 1981958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 198275396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 198375396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 198475396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 198575396ef9SLisandro Dalcin snes->ops->converged = 0; 198675396ef9SLisandro Dalcin snes->ops->setup = 0; 198775396ef9SLisandro Dalcin snes->ops->solve = 0; 198875396ef9SLisandro Dalcin snes->ops->view = 0; 198975396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 199075396ef9SLisandro Dalcin snes->ops->destroy = 0; 199175396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 199275396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 19933a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 1994454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 19953a40ed3dSBarry Smith PetscFunctionReturn(0); 19969b94acceSBarry Smith } 19979b94acceSBarry Smith 1998a847f771SSatish Balay 19999b94acceSBarry Smith /* --------------------------------------------------------------------- */ 20004a2ae208SSatish Balay #undef __FUNCT__ 20014a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 200252baeb72SSatish Balay /*@ 20039b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2004f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 20059b94acceSBarry Smith 2006fee21e36SBarry Smith Not Collective 2007fee21e36SBarry Smith 200836851e7fSLois Curfman McInnes Level: advanced 200936851e7fSLois Curfman McInnes 20109b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 20119b94acceSBarry Smith 20129b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 20139b94acceSBarry Smith @*/ 201463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void) 20159b94acceSBarry Smith { 2016dfbe8321SBarry Smith PetscErrorCode ierr; 201782bf6240SBarry Smith 20183a40ed3dSBarry Smith PetscFunctionBegin; 20191441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 20204c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 20213a40ed3dSBarry Smith PetscFunctionReturn(0); 20229b94acceSBarry Smith } 20239b94acceSBarry Smith 20244a2ae208SSatish Balay #undef __FUNCT__ 20254a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 20269b94acceSBarry Smith /*@C 20279a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 20289b94acceSBarry Smith 2029c7afd0dbSLois Curfman McInnes Not Collective 2030c7afd0dbSLois Curfman McInnes 20319b94acceSBarry Smith Input Parameter: 20324b0e389bSBarry Smith . snes - nonlinear solver context 20339b94acceSBarry Smith 20349b94acceSBarry Smith Output Parameter: 20353a7fca6bSBarry Smith . type - SNES method (a character string) 20369b94acceSBarry Smith 203736851e7fSLois Curfman McInnes Level: intermediate 203836851e7fSLois Curfman McInnes 2039454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 20409b94acceSBarry Smith @*/ 204163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,SNESType *type) 20429b94acceSBarry Smith { 20433a40ed3dSBarry Smith PetscFunctionBegin; 20444482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20454482741eSBarry Smith PetscValidPointer(type,2); 2046454a90a3SBarry Smith *type = snes->type_name; 20473a40ed3dSBarry Smith PetscFunctionReturn(0); 20489b94acceSBarry Smith } 20499b94acceSBarry Smith 20504a2ae208SSatish Balay #undef __FUNCT__ 20514a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 205252baeb72SSatish Balay /*@ 20539b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 20549b94acceSBarry Smith stored. 20559b94acceSBarry Smith 2056c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2057c7afd0dbSLois Curfman McInnes 20589b94acceSBarry Smith Input Parameter: 20599b94acceSBarry Smith . snes - the SNES context 20609b94acceSBarry Smith 20619b94acceSBarry Smith Output Parameter: 20629b94acceSBarry Smith . x - the solution 20639b94acceSBarry Smith 206470e92668SMatthew Knepley Level: intermediate 206536851e7fSLois Curfman McInnes 20669b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 20679b94acceSBarry Smith 206870e92668SMatthew Knepley .seealso: SNESSetSolution(), SNESGetFunction(), SNESGetSolutionUpdate() 20699b94acceSBarry Smith @*/ 207063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x) 20719b94acceSBarry Smith { 20723a40ed3dSBarry Smith PetscFunctionBegin; 20734482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20744482741eSBarry Smith PetscValidPointer(x,2); 20759b94acceSBarry Smith *x = snes->vec_sol_always; 20763a40ed3dSBarry Smith PetscFunctionReturn(0); 20779b94acceSBarry Smith } 20789b94acceSBarry Smith 20794a2ae208SSatish Balay #undef __FUNCT__ 208070e92668SMatthew Knepley #define __FUNCT__ "SNESSetSolution" 20817e4bb74cSBarry Smith /*@ 208270e92668SMatthew Knepley SNESSetSolution - Sets the vector where the approximate solution is stored. 208370e92668SMatthew Knepley 208470e92668SMatthew Knepley Not Collective, but Vec is parallel if SNES is parallel 208570e92668SMatthew Knepley 208670e92668SMatthew Knepley Input Parameters: 208770e92668SMatthew Knepley + snes - the SNES context 208870e92668SMatthew Knepley - x - the solution 208970e92668SMatthew Knepley 209070e92668SMatthew Knepley Output Parameter: 209170e92668SMatthew Knepley 209270e92668SMatthew Knepley Level: intermediate 209370e92668SMatthew Knepley 209442219521SBarry Smith Notes: this is not normally used, rather one simply calls SNESSolve() with 209542219521SBarry Smith the appropriate solution vector. 209642219521SBarry Smith 209770e92668SMatthew Knepley .keywords: SNES, nonlinear, set, solution 209870e92668SMatthew Knepley 209970e92668SMatthew Knepley .seealso: SNESGetSolution(), SNESGetFunction(), SNESGetSolutionUpdate() 210070e92668SMatthew Knepley @*/ 210170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetSolution(SNES snes,Vec x) 210270e92668SMatthew Knepley { 210370e92668SMatthew Knepley PetscFunctionBegin; 210470e92668SMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 210570e92668SMatthew Knepley PetscValidHeaderSpecific(x,VEC_COOKIE,2); 210670e92668SMatthew Knepley PetscCheckSameComm(snes,1,x,2); 210770e92668SMatthew Knepley snes->vec_sol_always = x; 210870e92668SMatthew Knepley PetscFunctionReturn(0); 210970e92668SMatthew Knepley } 211070e92668SMatthew Knepley 211170e92668SMatthew Knepley #undef __FUNCT__ 21124a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 211352baeb72SSatish Balay /*@ 21149b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 21159b94acceSBarry Smith stored. 21169b94acceSBarry Smith 2117c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2118c7afd0dbSLois Curfman McInnes 21199b94acceSBarry Smith Input Parameter: 21209b94acceSBarry Smith . snes - the SNES context 21219b94acceSBarry Smith 21229b94acceSBarry Smith Output Parameter: 21239b94acceSBarry Smith . x - the solution update 21249b94acceSBarry Smith 212536851e7fSLois Curfman McInnes Level: advanced 212636851e7fSLois Curfman McInnes 21279b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 21289b94acceSBarry Smith 21299b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 21309b94acceSBarry Smith @*/ 213163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x) 21329b94acceSBarry Smith { 21333a40ed3dSBarry Smith PetscFunctionBegin; 21344482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 21354482741eSBarry Smith PetscValidPointer(x,2); 21369b94acceSBarry Smith *x = snes->vec_sol_update_always; 21373a40ed3dSBarry Smith PetscFunctionReturn(0); 21389b94acceSBarry Smith } 21399b94acceSBarry Smith 21404a2ae208SSatish Balay #undef __FUNCT__ 21414a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 21429b94acceSBarry Smith /*@C 21433638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 21449b94acceSBarry Smith 2145c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2146c7afd0dbSLois Curfman McInnes 21479b94acceSBarry Smith Input Parameter: 21489b94acceSBarry Smith . snes - the SNES context 21499b94acceSBarry Smith 21509b94acceSBarry Smith Output Parameter: 21517bf4e008SBarry Smith + r - the function (or PETSC_NULL) 215270e92668SMatthew Knepley . func - the function (or PETSC_NULL) 215370e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 21549b94acceSBarry Smith 215536851e7fSLois Curfman McInnes Level: advanced 215636851e7fSLois Curfman McInnes 2157a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 21589b94acceSBarry Smith 21594b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 21609b94acceSBarry Smith @*/ 216170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 21629b94acceSBarry Smith { 21633a40ed3dSBarry Smith PetscFunctionBegin; 21644482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 21657bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 2166e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 216770e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 21683a40ed3dSBarry Smith PetscFunctionReturn(0); 21699b94acceSBarry Smith } 21709b94acceSBarry Smith 21714a2ae208SSatish Balay #undef __FUNCT__ 21724a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 21733c7409f5SSatish Balay /*@C 21743c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2175d850072dSLois Curfman McInnes SNES options in the database. 21763c7409f5SSatish Balay 2177fee21e36SBarry Smith Collective on SNES 2178fee21e36SBarry Smith 2179c7afd0dbSLois Curfman McInnes Input Parameter: 2180c7afd0dbSLois Curfman McInnes + snes - the SNES context 2181c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2182c7afd0dbSLois Curfman McInnes 2183d850072dSLois Curfman McInnes Notes: 2184a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2185c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2186d850072dSLois Curfman McInnes 218736851e7fSLois Curfman McInnes Level: advanced 218836851e7fSLois Curfman McInnes 21893c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2190a86d99e1SLois Curfman McInnes 2191a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 21923c7409f5SSatish Balay @*/ 219363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[]) 21943c7409f5SSatish Balay { 2195dfbe8321SBarry Smith PetscErrorCode ierr; 21963c7409f5SSatish Balay 21973a40ed3dSBarry Smith PetscFunctionBegin; 21984482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2199639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 220094b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 22013a40ed3dSBarry Smith PetscFunctionReturn(0); 22023c7409f5SSatish Balay } 22033c7409f5SSatish Balay 22044a2ae208SSatish Balay #undef __FUNCT__ 22054a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 22063c7409f5SSatish Balay /*@C 2207f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2208d850072dSLois Curfman McInnes SNES options in the database. 22093c7409f5SSatish Balay 2210fee21e36SBarry Smith Collective on SNES 2211fee21e36SBarry Smith 2212c7afd0dbSLois Curfman McInnes Input Parameters: 2213c7afd0dbSLois Curfman McInnes + snes - the SNES context 2214c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2215c7afd0dbSLois Curfman McInnes 2216d850072dSLois Curfman McInnes Notes: 2217a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2218c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2219d850072dSLois Curfman McInnes 222036851e7fSLois Curfman McInnes Level: advanced 222136851e7fSLois Curfman McInnes 22223c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2223a86d99e1SLois Curfman McInnes 2224a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 22253c7409f5SSatish Balay @*/ 222663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 22273c7409f5SSatish Balay { 2228dfbe8321SBarry Smith PetscErrorCode ierr; 22293c7409f5SSatish Balay 22303a40ed3dSBarry Smith PetscFunctionBegin; 22314482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2232639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 223394b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 22343a40ed3dSBarry Smith PetscFunctionReturn(0); 22353c7409f5SSatish Balay } 22363c7409f5SSatish Balay 22374a2ae208SSatish Balay #undef __FUNCT__ 22384a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 22399ab63eb5SSatish Balay /*@C 22403c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 22413c7409f5SSatish Balay SNES options in the database. 22423c7409f5SSatish Balay 2243c7afd0dbSLois Curfman McInnes Not Collective 2244c7afd0dbSLois Curfman McInnes 22453c7409f5SSatish Balay Input Parameter: 22463c7409f5SSatish Balay . snes - the SNES context 22473c7409f5SSatish Balay 22483c7409f5SSatish Balay Output Parameter: 22493c7409f5SSatish Balay . prefix - pointer to the prefix string used 22503c7409f5SSatish Balay 22519ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 22529ab63eb5SSatish Balay sufficient length to hold the prefix. 22539ab63eb5SSatish Balay 225436851e7fSLois Curfman McInnes Level: advanced 225536851e7fSLois Curfman McInnes 22563c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2257a86d99e1SLois Curfman McInnes 2258a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 22593c7409f5SSatish Balay @*/ 2260e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 22613c7409f5SSatish Balay { 2262dfbe8321SBarry Smith PetscErrorCode ierr; 22633c7409f5SSatish Balay 22643a40ed3dSBarry Smith PetscFunctionBegin; 22654482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2266639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 22673a40ed3dSBarry Smith PetscFunctionReturn(0); 22683c7409f5SSatish Balay } 22693c7409f5SSatish Balay 2270b2002411SLois Curfman McInnes 22714a2ae208SSatish Balay #undef __FUNCT__ 22724a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 22733cea93caSBarry Smith /*@C 22743cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 22753cea93caSBarry Smith 22767f6c08e0SMatthew Knepley Level: advanced 22773cea93caSBarry Smith @*/ 227863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2279b2002411SLois Curfman McInnes { 2280e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2281dfbe8321SBarry Smith PetscErrorCode ierr; 2282b2002411SLois Curfman McInnes 2283b2002411SLois Curfman McInnes PetscFunctionBegin; 2284b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2285c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2286b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2287b2002411SLois Curfman McInnes } 2288da9b6338SBarry Smith 2289da9b6338SBarry Smith #undef __FUNCT__ 2290da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 229163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes) 2292da9b6338SBarry Smith { 2293dfbe8321SBarry Smith PetscErrorCode ierr; 229477431f27SBarry Smith PetscInt N,i,j; 2295da9b6338SBarry Smith Vec u,uh,fh; 2296da9b6338SBarry Smith PetscScalar value; 2297da9b6338SBarry Smith PetscReal norm; 2298da9b6338SBarry Smith 2299da9b6338SBarry Smith PetscFunctionBegin; 2300da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 2301da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 2302da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 2303da9b6338SBarry Smith 2304da9b6338SBarry Smith /* currently only works for sequential */ 2305da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 2306da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 2307da9b6338SBarry Smith for (i=0; i<N; i++) { 2308da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 230977431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 2310da9b6338SBarry Smith for (j=-10; j<11; j++) { 2311ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 2312da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 23133ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 2314da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 231577431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 2316da9b6338SBarry Smith value = -value; 2317da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 2318da9b6338SBarry Smith } 2319da9b6338SBarry Smith } 2320da9b6338SBarry Smith ierr = VecDestroy(uh);CHKERRQ(ierr); 2321da9b6338SBarry Smith ierr = VecDestroy(fh);CHKERRQ(ierr); 2322da9b6338SBarry Smith PetscFunctionReturn(0); 2323da9b6338SBarry Smith } 232471f87433Sdalcinl 232571f87433Sdalcinl #undef __FUNCT__ 2326fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 232771f87433Sdalcinl /*@ 2328fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 232971f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 233071f87433Sdalcinl Newton method. 233171f87433Sdalcinl 233271f87433Sdalcinl Collective on SNES 233371f87433Sdalcinl 233471f87433Sdalcinl Input Parameters: 233571f87433Sdalcinl + snes - SNES context 233671f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 233771f87433Sdalcinl 233871f87433Sdalcinl Notes: 233971f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 234071f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 234171f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 234271f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 234371f87433Sdalcinl solver. 234471f87433Sdalcinl 234571f87433Sdalcinl Level: advanced 234671f87433Sdalcinl 234771f87433Sdalcinl Reference: 234871f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 234971f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 235071f87433Sdalcinl 235171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 235271f87433Sdalcinl 2353fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 235471f87433Sdalcinl @*/ 2355fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag) 235671f87433Sdalcinl { 235771f87433Sdalcinl PetscFunctionBegin; 235871f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 235971f87433Sdalcinl snes->ksp_ewconv = flag; 236071f87433Sdalcinl PetscFunctionReturn(0); 236171f87433Sdalcinl } 236271f87433Sdalcinl 236371f87433Sdalcinl #undef __FUNCT__ 2364fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 236571f87433Sdalcinl /*@ 2366fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 236771f87433Sdalcinl for computing relative tolerance for linear solvers within an 236871f87433Sdalcinl inexact Newton method. 236971f87433Sdalcinl 237071f87433Sdalcinl Not Collective 237171f87433Sdalcinl 237271f87433Sdalcinl Input Parameter: 237371f87433Sdalcinl . snes - SNES context 237471f87433Sdalcinl 237571f87433Sdalcinl Output Parameter: 237671f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 237771f87433Sdalcinl 237871f87433Sdalcinl Notes: 237971f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 238071f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 238171f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 238271f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 238371f87433Sdalcinl solver. 238471f87433Sdalcinl 238571f87433Sdalcinl Level: advanced 238671f87433Sdalcinl 238771f87433Sdalcinl Reference: 238871f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 238971f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 239071f87433Sdalcinl 239171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 239271f87433Sdalcinl 2393fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 239471f87433Sdalcinl @*/ 2395fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag) 239671f87433Sdalcinl { 239771f87433Sdalcinl PetscFunctionBegin; 239871f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 239971f87433Sdalcinl PetscValidPointer(flag,2); 240071f87433Sdalcinl *flag = snes->ksp_ewconv; 240171f87433Sdalcinl PetscFunctionReturn(0); 240271f87433Sdalcinl } 240371f87433Sdalcinl 240471f87433Sdalcinl #undef __FUNCT__ 2405fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 240671f87433Sdalcinl /*@ 2407fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 240871f87433Sdalcinl convergence criteria for the linear solvers within an inexact 240971f87433Sdalcinl Newton method. 241071f87433Sdalcinl 241171f87433Sdalcinl Collective on SNES 241271f87433Sdalcinl 241371f87433Sdalcinl Input Parameters: 241471f87433Sdalcinl + snes - SNES context 241571f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 241671f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 241771f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 241871f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 241971f87433Sdalcinl (0 <= gamma2 <= 1) 242071f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 242171f87433Sdalcinl . alpha2 - power for safeguard 242271f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 242371f87433Sdalcinl 242471f87433Sdalcinl Note: 242571f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 242671f87433Sdalcinl 242771f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 242871f87433Sdalcinl 242971f87433Sdalcinl Level: advanced 243071f87433Sdalcinl 243171f87433Sdalcinl Reference: 243271f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 243371f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 243471f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 243571f87433Sdalcinl 243671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 243771f87433Sdalcinl 2438fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 243971f87433Sdalcinl @*/ 2440fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 244171f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 244271f87433Sdalcinl { 2443fa9f3622SBarry Smith SNESKSPEW *kctx; 244471f87433Sdalcinl PetscFunctionBegin; 244571f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2446fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 244771f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 244871f87433Sdalcinl 244971f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 245071f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 245171f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 245271f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 245371f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 245471f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 245571f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 245671f87433Sdalcinl 245771f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 245871f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 245971f87433Sdalcinl } 246071f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 246171f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 246271f87433Sdalcinl } 246371f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 246471f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 246571f87433Sdalcinl } 246671f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 246771f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 246871f87433Sdalcinl } 246971f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 247071f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 247171f87433Sdalcinl } 247271f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 247371f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 247471f87433Sdalcinl } 247571f87433Sdalcinl PetscFunctionReturn(0); 247671f87433Sdalcinl } 247771f87433Sdalcinl 247871f87433Sdalcinl #undef __FUNCT__ 2479fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 248071f87433Sdalcinl /*@ 2481fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 248271f87433Sdalcinl convergence criteria for the linear solvers within an inexact 248371f87433Sdalcinl Newton method. 248471f87433Sdalcinl 248571f87433Sdalcinl Not Collective 248671f87433Sdalcinl 248771f87433Sdalcinl Input Parameters: 248871f87433Sdalcinl snes - SNES context 248971f87433Sdalcinl 249071f87433Sdalcinl Output Parameters: 249171f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 249271f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 249371f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 249471f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 249571f87433Sdalcinl (0 <= gamma2 <= 1) 249671f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 249771f87433Sdalcinl . alpha2 - power for safeguard 249871f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 249971f87433Sdalcinl 250071f87433Sdalcinl Level: advanced 250171f87433Sdalcinl 250271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 250371f87433Sdalcinl 2504fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 250571f87433Sdalcinl @*/ 2506fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 250771f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 250871f87433Sdalcinl { 2509fa9f3622SBarry Smith SNESKSPEW *kctx; 251071f87433Sdalcinl PetscFunctionBegin; 251171f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2512fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 251371f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 251471f87433Sdalcinl if(version) *version = kctx->version; 251571f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 251671f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 251771f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 251871f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 251971f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 252071f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 252171f87433Sdalcinl PetscFunctionReturn(0); 252271f87433Sdalcinl } 252371f87433Sdalcinl 252471f87433Sdalcinl #undef __FUNCT__ 2525fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 2526fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 252771f87433Sdalcinl { 252871f87433Sdalcinl PetscErrorCode ierr; 2529fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 253071f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 253171f87433Sdalcinl 253271f87433Sdalcinl PetscFunctionBegin; 253371f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 253471f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 253571f87433Sdalcinl rtol = kctx->rtol_0; 253671f87433Sdalcinl } else { 253771f87433Sdalcinl if (kctx->version == 1) { 253871f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 253971f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 254071f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 254171f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 254271f87433Sdalcinl } else if (kctx->version == 2) { 254371f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 254471f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 254571f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 254671f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 254771f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 254871f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 254971f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 255071f87433Sdalcinl stol = PetscMax(rtol,stol); 255171f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 255271f87433Sdalcinl /* safeguard: avoid oversolving */ 255371f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 255471f87433Sdalcinl stol = PetscMax(rtol,stol); 255571f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 255671f87433Sdalcinl } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 255771f87433Sdalcinl } 255871f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 255971f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 256071f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 256171f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 256271f87433Sdalcinl PetscFunctionReturn(0); 256371f87433Sdalcinl } 256471f87433Sdalcinl 256571f87433Sdalcinl #undef __FUNCT__ 2566fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 2567fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 256871f87433Sdalcinl { 256971f87433Sdalcinl PetscErrorCode ierr; 2570fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 257171f87433Sdalcinl PCSide pcside; 257271f87433Sdalcinl Vec lres; 257371f87433Sdalcinl 257471f87433Sdalcinl PetscFunctionBegin; 257571f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 257671f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 257771f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 257871f87433Sdalcinl if (kctx->version == 1) { 257971f87433Sdalcinl ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr); 258071f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 258171f87433Sdalcinl /* KSP residual is true linear residual */ 258271f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 258371f87433Sdalcinl } else { 258471f87433Sdalcinl /* KSP residual is preconditioned residual */ 258571f87433Sdalcinl /* compute true linear residual norm */ 258671f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 258771f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 258871f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 258971f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 259071f87433Sdalcinl ierr = VecDestroy(lres);CHKERRQ(ierr); 259171f87433Sdalcinl } 259271f87433Sdalcinl } 259371f87433Sdalcinl PetscFunctionReturn(0); 259471f87433Sdalcinl } 259571f87433Sdalcinl 259671f87433Sdalcinl #undef __FUNCT__ 259771f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 259871f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 259971f87433Sdalcinl { 260071f87433Sdalcinl PetscErrorCode ierr; 260171f87433Sdalcinl 260271f87433Sdalcinl PetscFunctionBegin; 2603fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 260471f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 2605fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 260671f87433Sdalcinl PetscFunctionReturn(0); 260771f87433Sdalcinl } 2608