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) { 54*7adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)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) { 62*7adad957SLisandro Dalcin if (((PetscObject)snes)->prefix) { 63*7adad957SLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",((PetscObject)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 { 190f1af5d2fSBarry Smith PetscTruth flg; 19185385478SLisandro Dalcin PetscInt i,indx; 19285385478SLisandro Dalcin const char *deft = SNESLS; 19385385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 19485385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 195e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 19623d894e5SBarry Smith PetscViewerASCIIMonitor monviewer; 19785385478SLisandro Dalcin PetscErrorCode ierr; 1989b94acceSBarry Smith 1993a40ed3dSBarry Smith PetscFunctionBegin; 2004482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 201ca161407SBarry Smith 202*7adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr); 203186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 204*7adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 205b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 206d64ed03dSBarry Smith if (flg) { 207186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 208*7adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 209186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 210d64ed03dSBarry Smith } 211909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 21293c39befSBarry Smith 21387828ca2SBarry Smith ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 21470441072SBarry Smith ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 215186905e3SBarry Smith 21687828ca2SBarry Smith ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 217b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 218b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 21950ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 22085385478SLisandro Dalcin 22185385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 22285385478SLisandro Dalcin if (flg) { 22385385478SLisandro Dalcin switch (indx) { 22485385478SLisandro Dalcin case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL);CHKERRQ(ierr); break; 22585385478SLisandro Dalcin case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL);CHKERRQ(ierr); break; 22685385478SLisandro Dalcin } 22785385478SLisandro Dalcin } 22885385478SLisandro Dalcin 2295968eb51SBarry Smith ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr); 2305968eb51SBarry Smith if (flg) { 2315968eb51SBarry Smith snes->printreason = PETSC_TRUE; 2325968eb51SBarry Smith } 233186905e3SBarry Smith 23485385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 23585385478SLisandro Dalcin 236fa9f3622SBarry Smith ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 237186905e3SBarry Smith 238fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 239fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 240fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 241fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 242fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 243fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 244fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 245186905e3SBarry Smith 246a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr); 247a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 248eabae89aSBarry Smith 249a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 250e8105e01SRichard Katz if (flg) { 251*7adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 25223d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 253e8105e01SRichard Katz } 254eabae89aSBarry Smith 255a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 256eabae89aSBarry Smith if (flg) { 257*7adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 258f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 259e8105e01SRichard Katz } 260eabae89aSBarry Smith 261a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 262eabae89aSBarry Smith if (flg) { 263*7adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 26423d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 265eabae89aSBarry Smith } 266eabae89aSBarry Smith 267a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr); 268a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 269a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr); 270a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 271a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr); 272a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 273a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr); 274a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 275e24b481bSBarry Smith 276b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr); 2774b27c08aSLois Curfman McInnes if (flg) { 278186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 279ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 2809b94acceSBarry Smith } 281639f9d9dSBarry Smith 28276b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 28376b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 28476b2cf59SMatthew Knepley } 28576b2cf59SMatthew Knepley 286e7788613SBarry Smith if (snes->ops->setfromoptions) { 287e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 288639f9d9dSBarry Smith } 289b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 2904bbc92c1SBarry Smith 29185385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 29293993e2dSLois Curfman McInnes 2933a40ed3dSBarry Smith PetscFunctionReturn(0); 2949b94acceSBarry Smith } 2959b94acceSBarry Smith 296a847f771SSatish Balay 2974a2ae208SSatish Balay #undef __FUNCT__ 2984a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 2999b94acceSBarry Smith /*@ 3009b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3019b94acceSBarry Smith the nonlinear solvers. 3029b94acceSBarry Smith 303fee21e36SBarry Smith Collective on SNES 304fee21e36SBarry Smith 305c7afd0dbSLois Curfman McInnes Input Parameters: 306c7afd0dbSLois Curfman McInnes + snes - the SNES context 307c7afd0dbSLois Curfman McInnes - usrP - optional user context 308c7afd0dbSLois Curfman McInnes 30936851e7fSLois Curfman McInnes Level: intermediate 31036851e7fSLois Curfman McInnes 3119b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3129b94acceSBarry Smith 3139b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3149b94acceSBarry Smith @*/ 31563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP) 3169b94acceSBarry Smith { 3173a40ed3dSBarry Smith PetscFunctionBegin; 3184482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3199b94acceSBarry Smith snes->user = usrP; 3203a40ed3dSBarry Smith PetscFunctionReturn(0); 3219b94acceSBarry Smith } 32274679c65SBarry Smith 3234a2ae208SSatish Balay #undef __FUNCT__ 3244a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 3259b94acceSBarry Smith /*@C 3269b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3279b94acceSBarry Smith nonlinear solvers. 3289b94acceSBarry Smith 329c7afd0dbSLois Curfman McInnes Not Collective 330c7afd0dbSLois Curfman McInnes 3319b94acceSBarry Smith Input Parameter: 3329b94acceSBarry Smith . snes - SNES context 3339b94acceSBarry Smith 3349b94acceSBarry Smith Output Parameter: 3359b94acceSBarry Smith . usrP - user context 3369b94acceSBarry Smith 33736851e7fSLois Curfman McInnes Level: intermediate 33836851e7fSLois Curfman McInnes 3399b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3409b94acceSBarry Smith 3419b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3429b94acceSBarry Smith @*/ 34363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP) 3449b94acceSBarry Smith { 3453a40ed3dSBarry Smith PetscFunctionBegin; 3464482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3479b94acceSBarry Smith *usrP = snes->user; 3483a40ed3dSBarry Smith PetscFunctionReturn(0); 3499b94acceSBarry Smith } 35074679c65SBarry Smith 3514a2ae208SSatish Balay #undef __FUNCT__ 3524a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 3539b94acceSBarry Smith /*@ 354c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 355c8228a4eSBarry Smith at this time. 3569b94acceSBarry Smith 357c7afd0dbSLois Curfman McInnes Not Collective 358c7afd0dbSLois Curfman McInnes 3599b94acceSBarry Smith Input Parameter: 3609b94acceSBarry Smith . snes - SNES context 3619b94acceSBarry Smith 3629b94acceSBarry Smith Output Parameter: 3639b94acceSBarry Smith . iter - iteration number 3649b94acceSBarry Smith 365c8228a4eSBarry Smith Notes: 366c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 367c8228a4eSBarry Smith 368c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 36908405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 37008405cd6SLois Curfman McInnes .vb 37108405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 37208405cd6SLois Curfman McInnes if (!(it % 2)) { 37308405cd6SLois Curfman McInnes [compute Jacobian here] 37408405cd6SLois Curfman McInnes } 37508405cd6SLois Curfman McInnes .ve 376c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 37708405cd6SLois Curfman McInnes recomputed every second SNES iteration. 378c8228a4eSBarry Smith 37936851e7fSLois Curfman McInnes Level: intermediate 38036851e7fSLois Curfman McInnes 3812b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 3822b668275SBarry Smith 383b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 3849b94acceSBarry Smith @*/ 38563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter) 3869b94acceSBarry Smith { 3873a40ed3dSBarry Smith PetscFunctionBegin; 3884482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3894482741eSBarry Smith PetscValidIntPointer(iter,2); 3909b94acceSBarry Smith *iter = snes->iter; 3913a40ed3dSBarry Smith PetscFunctionReturn(0); 3929b94acceSBarry Smith } 39374679c65SBarry Smith 3944a2ae208SSatish Balay #undef __FUNCT__ 3954a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 3969b94acceSBarry Smith /*@ 3979b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 3989b94acceSBarry Smith with SNESSSetFunction(). 3999b94acceSBarry Smith 400c7afd0dbSLois Curfman McInnes Collective on SNES 401c7afd0dbSLois Curfman McInnes 4029b94acceSBarry Smith Input Parameter: 4039b94acceSBarry Smith . snes - SNES context 4049b94acceSBarry Smith 4059b94acceSBarry Smith Output Parameter: 4069b94acceSBarry Smith . fnorm - 2-norm of function 4079b94acceSBarry Smith 40836851e7fSLois Curfman McInnes Level: intermediate 40936851e7fSLois Curfman McInnes 4109b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 411a86d99e1SLois Curfman McInnes 412b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 4139b94acceSBarry Smith @*/ 41471f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 4159b94acceSBarry Smith { 4163a40ed3dSBarry Smith PetscFunctionBegin; 4174482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4184482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 4199b94acceSBarry Smith *fnorm = snes->norm; 4203a40ed3dSBarry Smith PetscFunctionReturn(0); 4219b94acceSBarry Smith } 42274679c65SBarry Smith 4234a2ae208SSatish Balay #undef __FUNCT__ 424b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 4259b94acceSBarry Smith /*@ 426b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 4279b94acceSBarry Smith attempted by the nonlinear solver. 4289b94acceSBarry Smith 429c7afd0dbSLois Curfman McInnes Not Collective 430c7afd0dbSLois Curfman McInnes 4319b94acceSBarry Smith Input Parameter: 4329b94acceSBarry Smith . snes - SNES context 4339b94acceSBarry Smith 4349b94acceSBarry Smith Output Parameter: 4359b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 4369b94acceSBarry Smith 437c96a6f78SLois Curfman McInnes Notes: 438c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 439c96a6f78SLois Curfman McInnes 44036851e7fSLois Curfman McInnes Level: intermediate 44136851e7fSLois Curfman McInnes 4429b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 4439b94acceSBarry Smith @*/ 444b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 4459b94acceSBarry Smith { 4463a40ed3dSBarry Smith PetscFunctionBegin; 4474482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4484482741eSBarry Smith PetscValidIntPointer(nfails,2); 44950ffb88aSMatthew Knepley *nfails = snes->numFailures; 45050ffb88aSMatthew Knepley PetscFunctionReturn(0); 45150ffb88aSMatthew Knepley } 45250ffb88aSMatthew Knepley 45350ffb88aSMatthew Knepley #undef __FUNCT__ 454b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 45550ffb88aSMatthew Knepley /*@ 456b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 45750ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 45850ffb88aSMatthew Knepley 45950ffb88aSMatthew Knepley Not Collective 46050ffb88aSMatthew Knepley 46150ffb88aSMatthew Knepley Input Parameters: 46250ffb88aSMatthew Knepley + snes - SNES context 46350ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 46450ffb88aSMatthew Knepley 46550ffb88aSMatthew Knepley Level: intermediate 46650ffb88aSMatthew Knepley 46750ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 46850ffb88aSMatthew Knepley @*/ 469b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 47050ffb88aSMatthew Knepley { 47150ffb88aSMatthew Knepley PetscFunctionBegin; 4724482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 47350ffb88aSMatthew Knepley snes->maxFailures = maxFails; 47450ffb88aSMatthew Knepley PetscFunctionReturn(0); 47550ffb88aSMatthew Knepley } 47650ffb88aSMatthew Knepley 47750ffb88aSMatthew Knepley #undef __FUNCT__ 478b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 47950ffb88aSMatthew Knepley /*@ 480b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 48150ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 48250ffb88aSMatthew Knepley 48350ffb88aSMatthew Knepley Not Collective 48450ffb88aSMatthew Knepley 48550ffb88aSMatthew Knepley Input Parameter: 48650ffb88aSMatthew Knepley . snes - SNES context 48750ffb88aSMatthew Knepley 48850ffb88aSMatthew Knepley Output Parameter: 48950ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 49050ffb88aSMatthew Knepley 49150ffb88aSMatthew Knepley Level: intermediate 49250ffb88aSMatthew Knepley 49350ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 49450ffb88aSMatthew Knepley @*/ 495b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 49650ffb88aSMatthew Knepley { 49750ffb88aSMatthew Knepley PetscFunctionBegin; 4984482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4994482741eSBarry Smith PetscValidIntPointer(maxFails,2); 50050ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 5013a40ed3dSBarry Smith PetscFunctionReturn(0); 5029b94acceSBarry Smith } 503a847f771SSatish Balay 5044a2ae208SSatish Balay #undef __FUNCT__ 5052541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 5062541af92SBarry Smith /*@ 5072541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 5082541af92SBarry Smith done by SNES. 5092541af92SBarry Smith 5102541af92SBarry Smith Not Collective 5112541af92SBarry Smith 5122541af92SBarry Smith Input Parameter: 5132541af92SBarry Smith . snes - SNES context 5142541af92SBarry Smith 5152541af92SBarry Smith Output Parameter: 5162541af92SBarry Smith . nfuncs - number of evaluations 5172541af92SBarry Smith 5182541af92SBarry Smith Level: intermediate 5192541af92SBarry Smith 5202541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 5212541af92SBarry Smith @*/ 5222541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 5232541af92SBarry Smith { 5242541af92SBarry Smith PetscFunctionBegin; 5252541af92SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5262541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 5272541af92SBarry Smith *nfuncs = snes->nfuncs; 5282541af92SBarry Smith PetscFunctionReturn(0); 5292541af92SBarry Smith } 5302541af92SBarry Smith 5312541af92SBarry Smith #undef __FUNCT__ 5323d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 5333d4c4710SBarry Smith /*@ 5343d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 5353d4c4710SBarry Smith linear solvers. 5363d4c4710SBarry Smith 5373d4c4710SBarry Smith Not Collective 5383d4c4710SBarry Smith 5393d4c4710SBarry Smith Input Parameter: 5403d4c4710SBarry Smith . snes - SNES context 5413d4c4710SBarry Smith 5423d4c4710SBarry Smith Output Parameter: 5433d4c4710SBarry Smith . nfails - number of failed solves 5443d4c4710SBarry Smith 5453d4c4710SBarry Smith Notes: 5463d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 5473d4c4710SBarry Smith 5483d4c4710SBarry Smith Level: intermediate 5493d4c4710SBarry Smith 5503d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 5513d4c4710SBarry Smith @*/ 5523d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 5533d4c4710SBarry Smith { 5543d4c4710SBarry Smith PetscFunctionBegin; 5553d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5563d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 5573d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 5583d4c4710SBarry Smith PetscFunctionReturn(0); 5593d4c4710SBarry Smith } 5603d4c4710SBarry Smith 5613d4c4710SBarry Smith #undef __FUNCT__ 5623d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 5633d4c4710SBarry Smith /*@ 5643d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 5653d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 5663d4c4710SBarry Smith 5673d4c4710SBarry Smith Collective on SNES 5683d4c4710SBarry Smith 5693d4c4710SBarry Smith Input Parameters: 5703d4c4710SBarry Smith + snes - SNES context 5713d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 5723d4c4710SBarry Smith 5733d4c4710SBarry Smith Level: intermediate 5743d4c4710SBarry Smith 5753d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 5763d4c4710SBarry Smith 5773d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 5783d4c4710SBarry Smith 5793d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 5803d4c4710SBarry Smith @*/ 5813d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 5823d4c4710SBarry Smith { 5833d4c4710SBarry Smith PetscFunctionBegin; 5843d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5853d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 5863d4c4710SBarry Smith PetscFunctionReturn(0); 5873d4c4710SBarry Smith } 5883d4c4710SBarry Smith 5893d4c4710SBarry Smith #undef __FUNCT__ 5903d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 5913d4c4710SBarry Smith /*@ 5923d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 5933d4c4710SBarry Smith are allowed before SNES terminates 5943d4c4710SBarry Smith 5953d4c4710SBarry Smith Not Collective 5963d4c4710SBarry Smith 5973d4c4710SBarry Smith Input Parameter: 5983d4c4710SBarry Smith . snes - SNES context 5993d4c4710SBarry Smith 6003d4c4710SBarry Smith Output Parameter: 6013d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 6023d4c4710SBarry Smith 6033d4c4710SBarry Smith Level: intermediate 6043d4c4710SBarry Smith 6053d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 6063d4c4710SBarry Smith 6073d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 6083d4c4710SBarry Smith 6093d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 6103d4c4710SBarry Smith @*/ 6113d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 6123d4c4710SBarry Smith { 6133d4c4710SBarry Smith PetscFunctionBegin; 6143d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6153d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 6163d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 6173d4c4710SBarry Smith PetscFunctionReturn(0); 6183d4c4710SBarry Smith } 6193d4c4710SBarry Smith 6203d4c4710SBarry Smith #undef __FUNCT__ 621b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 622c96a6f78SLois Curfman McInnes /*@ 623b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 624c96a6f78SLois Curfman McInnes used by the nonlinear solver. 625c96a6f78SLois Curfman McInnes 626c7afd0dbSLois Curfman McInnes Not Collective 627c7afd0dbSLois Curfman McInnes 628c96a6f78SLois Curfman McInnes Input Parameter: 629c96a6f78SLois Curfman McInnes . snes - SNES context 630c96a6f78SLois Curfman McInnes 631c96a6f78SLois Curfman McInnes Output Parameter: 632c96a6f78SLois Curfman McInnes . lits - number of linear iterations 633c96a6f78SLois Curfman McInnes 634c96a6f78SLois Curfman McInnes Notes: 635c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 636c96a6f78SLois Curfman McInnes 63736851e7fSLois Curfman McInnes Level: intermediate 63836851e7fSLois Curfman McInnes 639c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 6402b668275SBarry Smith 6412b668275SBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm() 642c96a6f78SLois Curfman McInnes @*/ 643b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 644c96a6f78SLois Curfman McInnes { 6453a40ed3dSBarry Smith PetscFunctionBegin; 6464482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6474482741eSBarry Smith PetscValidIntPointer(lits,2); 648c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 6493a40ed3dSBarry Smith PetscFunctionReturn(0); 650c96a6f78SLois Curfman McInnes } 651c96a6f78SLois Curfman McInnes 6524a2ae208SSatish Balay #undef __FUNCT__ 65394b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 65452baeb72SSatish Balay /*@ 65594b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 6569b94acceSBarry Smith 65794b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 658c7afd0dbSLois Curfman McInnes 6599b94acceSBarry Smith Input Parameter: 6609b94acceSBarry Smith . snes - the SNES context 6619b94acceSBarry Smith 6629b94acceSBarry Smith Output Parameter: 66394b7f48cSBarry Smith . ksp - the KSP context 6649b94acceSBarry Smith 6659b94acceSBarry Smith Notes: 66694b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 6679b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 6682999313aSBarry Smith PC contexts as well. 6699b94acceSBarry Smith 67036851e7fSLois Curfman McInnes Level: beginner 67136851e7fSLois Curfman McInnes 67294b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 6739b94acceSBarry Smith 6742999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 6759b94acceSBarry Smith @*/ 67663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp) 6779b94acceSBarry Smith { 6783a40ed3dSBarry Smith PetscFunctionBegin; 6794482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6804482741eSBarry Smith PetscValidPointer(ksp,2); 68194b7f48cSBarry Smith *ksp = snes->ksp; 6823a40ed3dSBarry Smith PetscFunctionReturn(0); 6839b94acceSBarry Smith } 68482bf6240SBarry Smith 6854a2ae208SSatish Balay #undef __FUNCT__ 6862999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 6872999313aSBarry Smith /*@ 6882999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 6892999313aSBarry Smith 6902999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 6912999313aSBarry Smith 6922999313aSBarry Smith Input Parameters: 6932999313aSBarry Smith + snes - the SNES context 6942999313aSBarry Smith - ksp - the KSP context 6952999313aSBarry Smith 6962999313aSBarry Smith Notes: 6972999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 6982999313aSBarry Smith so this routine is rarely needed. 6992999313aSBarry Smith 7002999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 7012999313aSBarry Smith decreased by one. 7022999313aSBarry Smith 7032999313aSBarry Smith Level: developer 7042999313aSBarry Smith 7052999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7062999313aSBarry Smith 7072999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7082999313aSBarry Smith @*/ 7092999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp) 7102999313aSBarry Smith { 7112999313aSBarry Smith PetscErrorCode ierr; 7122999313aSBarry Smith 7132999313aSBarry Smith PetscFunctionBegin; 7142999313aSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7152999313aSBarry Smith PetscValidHeaderSpecific(ksp,KSP_COOKIE,2); 7162999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 7177dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 718906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 7192999313aSBarry Smith snes->ksp = ksp; 7202999313aSBarry Smith PetscFunctionReturn(0); 7212999313aSBarry Smith } 7222999313aSBarry Smith 723*7adad957SLisandro Dalcin #if 0 7242999313aSBarry Smith #undef __FUNCT__ 7254a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 7266849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 727e24b481bSBarry Smith { 728e24b481bSBarry Smith PetscFunctionBegin; 729e24b481bSBarry Smith PetscFunctionReturn(0); 730e24b481bSBarry Smith } 731*7adad957SLisandro Dalcin #endif 732e24b481bSBarry Smith 7339b94acceSBarry Smith /* -----------------------------------------------------------*/ 7344a2ae208SSatish Balay #undef __FUNCT__ 7354a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 73652baeb72SSatish Balay /*@ 7379b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 7389b94acceSBarry Smith 739c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 740c7afd0dbSLois Curfman McInnes 741c7afd0dbSLois Curfman McInnes Input Parameters: 742906ed7ccSBarry Smith . comm - MPI communicator 7439b94acceSBarry Smith 7449b94acceSBarry Smith Output Parameter: 7459b94acceSBarry Smith . outsnes - the new SNES context 7469b94acceSBarry Smith 747c7afd0dbSLois Curfman McInnes Options Database Keys: 748c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 749c7afd0dbSLois Curfman McInnes and no preconditioning matrix 750c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 751c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 752c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 753c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 754c1f60f51SBarry Smith 75536851e7fSLois Curfman McInnes Level: beginner 75636851e7fSLois Curfman McInnes 7579b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 7589b94acceSBarry Smith 7594b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES 7609b94acceSBarry Smith @*/ 76163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes) 7629b94acceSBarry Smith { 763dfbe8321SBarry Smith PetscErrorCode ierr; 7649b94acceSBarry Smith SNES snes; 765fa9f3622SBarry Smith SNESKSPEW *kctx; 76637fcc0dbSBarry Smith 7673a40ed3dSBarry Smith PetscFunctionBegin; 768ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 7698ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 7708ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 7718ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 7728ba1e511SMatthew Knepley #endif 7738ba1e511SMatthew Knepley 774e7788613SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 775*7adad957SLisandro Dalcin 77685385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 7779b94acceSBarry Smith snes->max_its = 50; 7789750a799SBarry Smith snes->max_funcs = 10000; 7799b94acceSBarry Smith snes->norm = 0.0; 780b4874afaSBarry Smith snes->rtol = 1.e-8; 781b4874afaSBarry Smith snes->ttol = 0.0; 78270441072SBarry Smith snes->abstol = 1.e-50; 7839b94acceSBarry Smith snes->xtol = 1.e-8; 7844b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 7859b94acceSBarry Smith snes->nfuncs = 0; 78650ffb88aSMatthew Knepley snes->numFailures = 0; 78750ffb88aSMatthew Knepley snes->maxFailures = 1; 7887a00f4a9SLois Curfman McInnes snes->linear_its = 0; 789639f9d9dSBarry Smith snes->numbermonitors = 0; 7909b94acceSBarry Smith snes->data = 0; 7914dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 792186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 7936f24a144SLois Curfman McInnes snes->vwork = 0; 7946f24a144SLois Curfman McInnes snes->nwork = 0; 795758f92a0SBarry Smith snes->conv_hist_len = 0; 796758f92a0SBarry Smith snes->conv_hist_max = 0; 797758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 798758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 799758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 800184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 8019b94acceSBarry Smith 8023d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 8033d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 8043d4c4710SBarry Smith 8059b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 80638f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 8079b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 8089b94acceSBarry Smith kctx->version = 2; 8099b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 8109b94acceSBarry Smith this was too large for some test cases */ 8119b94acceSBarry Smith kctx->rtol_last = 0; 8129b94acceSBarry Smith kctx->rtol_max = .9; 8139b94acceSBarry Smith kctx->gamma = 1.0; 81471f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 81571f87433Sdalcinl kctx->alpha2 = kctx->alpha; 8169b94acceSBarry Smith kctx->threshold = .1; 8179b94acceSBarry Smith kctx->lresid_last = 0; 8189b94acceSBarry Smith kctx->norm_last = 0; 8199b94acceSBarry Smith 82094b7f48cSBarry Smith ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr); 82152e6d16bSBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 8225334005bSBarry Smith 8239b94acceSBarry Smith *outsnes = snes; 82400036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 8253a40ed3dSBarry Smith PetscFunctionReturn(0); 8269b94acceSBarry Smith } 8279b94acceSBarry Smith 8284a2ae208SSatish Balay #undef __FUNCT__ 8294a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 8309b94acceSBarry Smith /*@C 8319b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 8329b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 8339b94acceSBarry Smith equations. 8349b94acceSBarry Smith 835fee21e36SBarry Smith Collective on SNES 836fee21e36SBarry Smith 837c7afd0dbSLois Curfman McInnes Input Parameters: 838c7afd0dbSLois Curfman McInnes + snes - the SNES context 839c7afd0dbSLois Curfman McInnes . r - vector to store function value 840de044059SHong Zhang . func - function evaluation routine 841c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 842c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8439b94acceSBarry Smith 844c7afd0dbSLois Curfman McInnes Calling sequence of func: 8458d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 846c7afd0dbSLois Curfman McInnes 847313e4042SLois Curfman McInnes . f - function vector 848c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 8499b94acceSBarry Smith 8509b94acceSBarry Smith Notes: 8519b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 8529b94acceSBarry Smith $ f'(x) x = -f(x), 853c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 8549b94acceSBarry Smith 85536851e7fSLois Curfman McInnes Level: beginner 85636851e7fSLois Curfman McInnes 8579b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 8589b94acceSBarry Smith 859a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 8609b94acceSBarry Smith @*/ 86163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 8629b94acceSBarry Smith { 86385385478SLisandro Dalcin PetscErrorCode ierr; 8643a40ed3dSBarry Smith PetscFunctionBegin; 8654482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 8664482741eSBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE,2); 867c9780b6fSBarry Smith PetscCheckSameComm(snes,1,r,2); 86885385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 86985385478SLisandro Dalcin if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); } 870e7788613SBarry Smith snes->ops->computefunction = func; 87185385478SLisandro Dalcin snes->vec_func = r; 8729b94acceSBarry Smith snes->funP = ctx; 8733a40ed3dSBarry Smith PetscFunctionReturn(0); 8749b94acceSBarry Smith } 8759b94acceSBarry Smith 8763ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 8773ab0aad5SBarry Smith #undef __FUNCT__ 8781096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 8791096aae1SMatthew Knepley /*@C 8801096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 8811096aae1SMatthew Knepley it assumes a zero right hand side. 8821096aae1SMatthew Knepley 8831096aae1SMatthew Knepley Collective on SNES 8841096aae1SMatthew Knepley 8851096aae1SMatthew Knepley Input Parameter: 8861096aae1SMatthew Knepley . snes - the SNES context 8871096aae1SMatthew Knepley 8881096aae1SMatthew Knepley Output Parameter: 8891096aae1SMatthew Knepley . rhs - the right hand side vector or PETSC_NULL for a zero right hand side 8901096aae1SMatthew Knepley 8911096aae1SMatthew Knepley Level: intermediate 8921096aae1SMatthew Knepley 8931096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 8941096aae1SMatthew Knepley 89585385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 8961096aae1SMatthew Knepley @*/ 8971096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs) 8981096aae1SMatthew Knepley { 8991096aae1SMatthew Knepley PetscFunctionBegin; 9001096aae1SMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9011096aae1SMatthew Knepley PetscValidPointer(rhs,2); 90285385478SLisandro Dalcin *rhs = snes->vec_rhs; 9031096aae1SMatthew Knepley PetscFunctionReturn(0); 9041096aae1SMatthew Knepley } 9051096aae1SMatthew Knepley 9061096aae1SMatthew Knepley #undef __FUNCT__ 9074a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 9089b94acceSBarry Smith /*@ 90936851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 9109b94acceSBarry Smith SNESSetFunction(). 9119b94acceSBarry Smith 912c7afd0dbSLois Curfman McInnes Collective on SNES 913c7afd0dbSLois Curfman McInnes 9149b94acceSBarry Smith Input Parameters: 915c7afd0dbSLois Curfman McInnes + snes - the SNES context 916c7afd0dbSLois Curfman McInnes - x - input vector 9179b94acceSBarry Smith 9189b94acceSBarry Smith Output Parameter: 9193638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 9209b94acceSBarry Smith 9211bffabb2SLois Curfman McInnes Notes: 92236851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 92336851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 92436851e7fSLois Curfman McInnes themselves. 92536851e7fSLois Curfman McInnes 92636851e7fSLois Curfman McInnes Level: developer 92736851e7fSLois Curfman McInnes 9289b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 9299b94acceSBarry Smith 930a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 9319b94acceSBarry Smith @*/ 93263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y) 9339b94acceSBarry Smith { 934dfbe8321SBarry Smith PetscErrorCode ierr; 9359b94acceSBarry Smith 9363a40ed3dSBarry Smith PetscFunctionBegin; 9374482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9384482741eSBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE,2); 9394482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,3); 940c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 941c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 942184914b5SBarry Smith 943d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 944e7788613SBarry Smith if (snes->ops->computefunction) { 945d64ed03dSBarry Smith PetscStackPush("SNES user function"); 946e9a2bbcdSBarry Smith CHKMEMQ; 947e7788613SBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP); 948e9a2bbcdSBarry Smith CHKMEMQ; 949d64ed03dSBarry Smith PetscStackPop; 950d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 95119717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr); 95219717074SBarry Smith } 953d5e45103SBarry Smith CHKERRQ(ierr); 95485385478SLisandro Dalcin } else if (snes->vec_rhs) { 9551096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 9561096aae1SMatthew Knepley } else { 9571096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 9581096aae1SMatthew Knepley } 95985385478SLisandro Dalcin if (snes->vec_rhs) { 96085385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 9613ab0aad5SBarry Smith } 962ae3c334cSLois Curfman McInnes snes->nfuncs++; 963d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 9643a40ed3dSBarry Smith PetscFunctionReturn(0); 9659b94acceSBarry Smith } 9669b94acceSBarry Smith 9674a2ae208SSatish Balay #undef __FUNCT__ 9684a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 96962fef451SLois Curfman McInnes /*@ 97062fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 97162fef451SLois Curfman McInnes set with SNESSetJacobian(). 97262fef451SLois Curfman McInnes 973c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 974c7afd0dbSLois Curfman McInnes 97562fef451SLois Curfman McInnes Input Parameters: 976c7afd0dbSLois Curfman McInnes + snes - the SNES context 977c7afd0dbSLois Curfman McInnes - x - input vector 97862fef451SLois Curfman McInnes 97962fef451SLois Curfman McInnes Output Parameters: 980c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 98162fef451SLois Curfman McInnes . B - optional preconditioning matrix 9822b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 983fee21e36SBarry Smith 98462fef451SLois Curfman McInnes Notes: 98562fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 98662fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 98762fef451SLois Curfman McInnes 98894b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 989dc5a77f8SLois Curfman McInnes flag parameter. 99062fef451SLois Curfman McInnes 99136851e7fSLois Curfman McInnes Level: developer 99236851e7fSLois Curfman McInnes 99362fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 99462fef451SLois Curfman McInnes 9952b668275SBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure 99662fef451SLois Curfman McInnes @*/ 99763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 9989b94acceSBarry Smith { 999dfbe8321SBarry Smith PetscErrorCode ierr; 10003a40ed3dSBarry Smith 10013a40ed3dSBarry Smith PetscFunctionBegin; 10024482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10034482741eSBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE,2); 10044482741eSBarry Smith PetscValidPointer(flg,5); 1005c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1006e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1007d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1008c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1009d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1010dc67937bSBarry Smith CHKMEMQ; 1011e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1012dc67937bSBarry Smith CHKMEMQ; 1013d64ed03dSBarry Smith PetscStackPop; 1014d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 10156d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 10166ce558aeSBarry Smith /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3); 10176ce558aeSBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE,4); */ 10183a40ed3dSBarry Smith PetscFunctionReturn(0); 10199b94acceSBarry Smith } 10209b94acceSBarry Smith 10214a2ae208SSatish Balay #undef __FUNCT__ 10224a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 10239b94acceSBarry Smith /*@C 10249b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1025044dda88SLois Curfman McInnes location to store the matrix. 10269b94acceSBarry Smith 1027c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1028c7afd0dbSLois Curfman McInnes 10299b94acceSBarry Smith Input Parameters: 1030c7afd0dbSLois Curfman McInnes + snes - the SNES context 10319b94acceSBarry Smith . A - Jacobian matrix 10329b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 10339b94acceSBarry Smith . func - Jacobian evaluation routine 1034c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 10352cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 10369b94acceSBarry Smith 10379b94acceSBarry Smith Calling sequence of func: 10388d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 10399b94acceSBarry Smith 1040c7afd0dbSLois Curfman McInnes + x - input vector 10419b94acceSBarry Smith . A - Jacobian matrix 10429b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1043ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 10442b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1045c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 10469b94acceSBarry Smith 10479b94acceSBarry Smith Notes: 104894b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 10492cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1050ac21db08SLois Curfman McInnes 1051ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 10529b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 10539b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 10549b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 10559b94acceSBarry Smith throughout the global iterations. 10569b94acceSBarry Smith 105736851e7fSLois Curfman McInnes Level: beginner 105836851e7fSLois Curfman McInnes 10599b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 10609b94acceSBarry Smith 10613ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 10629b94acceSBarry Smith @*/ 106363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 10649b94acceSBarry Smith { 1065dfbe8321SBarry Smith PetscErrorCode ierr; 10663a7fca6bSBarry Smith 10673a40ed3dSBarry Smith PetscFunctionBegin; 10684482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10694482741eSBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2); 10704482741eSBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3); 1071c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 1072c9780b6fSBarry Smith if (B) PetscCheckSameComm(snes,1,B,2); 1073e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 10743a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 10753a7fca6bSBarry Smith if (A) { 10767dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 10773a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 10789b94acceSBarry Smith snes->jacobian = A; 10793a7fca6bSBarry Smith } 10803a7fca6bSBarry Smith if (B) { 10817dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 10823a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 10839b94acceSBarry Smith snes->jacobian_pre = B; 10843a7fca6bSBarry Smith } 10853a40ed3dSBarry Smith PetscFunctionReturn(0); 10869b94acceSBarry Smith } 108762fef451SLois Curfman McInnes 10884a2ae208SSatish Balay #undef __FUNCT__ 10894a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1090c2aafc4cSSatish Balay /*@C 1091b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1092b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1093b4fd4287SBarry Smith 1094c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1095c7afd0dbSLois Curfman McInnes 1096b4fd4287SBarry Smith Input Parameter: 1097b4fd4287SBarry Smith . snes - the nonlinear solver context 1098b4fd4287SBarry Smith 1099b4fd4287SBarry Smith Output Parameters: 1100c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1101b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 110270e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 110370e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1104fee21e36SBarry Smith 110536851e7fSLois Curfman McInnes Level: advanced 110636851e7fSLois Curfman McInnes 1107b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1108b4fd4287SBarry Smith @*/ 110970e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1110b4fd4287SBarry Smith { 11113a40ed3dSBarry Smith PetscFunctionBegin; 11124482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1113b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1114b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1115e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 111670e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 11173a40ed3dSBarry Smith PetscFunctionReturn(0); 1118b4fd4287SBarry Smith } 1119b4fd4287SBarry Smith 11209b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 112163dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 11229b94acceSBarry Smith 11234a2ae208SSatish Balay #undef __FUNCT__ 11244a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 11259b94acceSBarry Smith /*@ 11269b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1127272ac6f2SLois Curfman McInnes of a nonlinear solver. 11289b94acceSBarry Smith 1129fee21e36SBarry Smith Collective on SNES 1130fee21e36SBarry Smith 1131c7afd0dbSLois Curfman McInnes Input Parameters: 113270e92668SMatthew Knepley . snes - the SNES context 1133c7afd0dbSLois Curfman McInnes 1134272ac6f2SLois Curfman McInnes Notes: 1135272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1136272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1137272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1138272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1139272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1140272ac6f2SLois Curfman McInnes 114136851e7fSLois Curfman McInnes Level: advanced 114236851e7fSLois Curfman McInnes 11439b94acceSBarry Smith .keywords: SNES, nonlinear, setup 11449b94acceSBarry Smith 11459b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 11469b94acceSBarry Smith @*/ 114770e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes) 11489b94acceSBarry Smith { 1149dfbe8321SBarry Smith PetscErrorCode ierr; 115071f87433Sdalcinl PetscTruth flg; 11513a40ed3dSBarry Smith 11523a40ed3dSBarry Smith PetscFunctionBegin; 11534482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11544dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 11559b94acceSBarry Smith 1156*7adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 115785385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 115885385478SLisandro Dalcin } 115985385478SLisandro Dalcin 1160*7adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1161c1f60f51SBarry Smith /* 1162c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1163dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1164c1f60f51SBarry Smith */ 1165c1f60f51SBarry Smith if (flg) { 1166c1f60f51SBarry Smith Mat J; 1167fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 11683ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 1169ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 11703a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 11713a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1172c1f60f51SBarry Smith } 117345fc7adcSBarry Smith 117403c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT) 1175*7adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr); 117645fc7adcSBarry Smith if (flg) { 117745fc7adcSBarry Smith Mat J; 117845fc7adcSBarry Smith ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr); 117975396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr); 118045fc7adcSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 118145fc7adcSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 118245fc7adcSBarry Smith } 118332a4b47aSMatthew Knepley #endif 118445fc7adcSBarry Smith 1185*7adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1186c1f60f51SBarry Smith /* 1187dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1188c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1189c1f60f51SBarry Smith */ 119031615d04SLois Curfman McInnes if (flg) { 1191272ac6f2SLois Curfman McInnes Mat J; 1192b5d62d44SBarry Smith KSP ksp; 119394b7f48cSBarry Smith PC pc; 119475396ef9SLisandro Dalcin /* create and set matrix-free operator */ 1195fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 11963ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 119775396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 11983ec795f1SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 11993a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1200f3ef73ceSBarry Smith /* force no preconditioner */ 120194b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 1202b5d62d44SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 1203a9815358SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 1204a9815358SBarry Smith if (!flg) { 120575396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 1206f3ef73ceSBarry Smith ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 1207272ac6f2SLois Curfman McInnes } 1208a9815358SBarry Smith } 1209f3ef73ceSBarry Smith 121085385478SLisandro Dalcin if (!snes->vec_func && !snes->vec_rhs) { 12111096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12121096aae1SMatthew Knepley } 121385385478SLisandro Dalcin if (!snes->ops->computefunction && !snes->vec_rhs) { 12141096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12151096aae1SMatthew Knepley } 121675396ef9SLisandro Dalcin if (!snes->jacobian) { 121775396ef9SLisandro Dalcin SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 121875396ef9SLisandro Dalcin } 1219a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 122029bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1221a8c6a408SBarry Smith } 1222a703fe33SLois Curfman McInnes 1223410397dcSLisandro Dalcin if (snes->ops->setup) { 1224410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1225410397dcSLisandro Dalcin } 12267aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 12273a40ed3dSBarry Smith PetscFunctionReturn(0); 12289b94acceSBarry Smith } 12299b94acceSBarry Smith 12304a2ae208SSatish Balay #undef __FUNCT__ 12314a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 123252baeb72SSatish Balay /*@ 12339b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 12349b94acceSBarry Smith with SNESCreate(). 12359b94acceSBarry Smith 1236c7afd0dbSLois Curfman McInnes Collective on SNES 1237c7afd0dbSLois Curfman McInnes 12389b94acceSBarry Smith Input Parameter: 12399b94acceSBarry Smith . snes - the SNES context 12409b94acceSBarry Smith 124136851e7fSLois Curfman McInnes Level: beginner 124236851e7fSLois Curfman McInnes 12439b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 12449b94acceSBarry Smith 124563a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 12469b94acceSBarry Smith @*/ 124763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes) 12489b94acceSBarry Smith { 12496849ba73SBarry Smith PetscErrorCode ierr; 12503a40ed3dSBarry Smith 12513a40ed3dSBarry Smith PetscFunctionBegin; 12524482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1253*7adad957SLisandro Dalcin if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0); 1254d4bb536fSBarry Smith 1255be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 12560f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1257e7788613SBarry Smith if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);} 125885385478SLisandro Dalcin 125985385478SLisandro Dalcin if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);} 126085385478SLisandro Dalcin if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);} 126185385478SLisandro Dalcin if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);} 12623a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 12633a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 126494b7f48cSBarry Smith ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr); 126585385478SLisandro Dalcin ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr); 1266522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1267a6570f20SBarry Smith ierr = SNESMonitorCancel(snes);CHKERRQ(ierr); 1268a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 12693a40ed3dSBarry Smith PetscFunctionReturn(0); 12709b94acceSBarry Smith } 12719b94acceSBarry Smith 12729b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 12739b94acceSBarry Smith 12744a2ae208SSatish Balay #undef __FUNCT__ 12754a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 12769b94acceSBarry Smith /*@ 1277d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 12789b94acceSBarry Smith 1279c7afd0dbSLois Curfman McInnes Collective on SNES 1280c7afd0dbSLois Curfman McInnes 12819b94acceSBarry Smith Input Parameters: 1282c7afd0dbSLois Curfman McInnes + snes - the SNES context 128370441072SBarry Smith . abstol - absolute convergence tolerance 128433174efeSLois Curfman McInnes . rtol - relative convergence tolerance 128533174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 128633174efeSLois Curfman McInnes of the change in the solution between steps 128733174efeSLois Curfman McInnes . maxit - maximum number of iterations 1288c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1289fee21e36SBarry Smith 129033174efeSLois Curfman McInnes Options Database Keys: 129170441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1292c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1293c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1294c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1295c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 12969b94acceSBarry Smith 1297d7a720efSLois Curfman McInnes Notes: 12989b94acceSBarry Smith The default maximum number of iterations is 50. 12999b94acceSBarry Smith The default maximum number of function evaluations is 1000. 13009b94acceSBarry Smith 130136851e7fSLois Curfman McInnes Level: intermediate 130236851e7fSLois Curfman McInnes 130333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 13049b94acceSBarry Smith 13052492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 13069b94acceSBarry Smith @*/ 130763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 13089b94acceSBarry Smith { 13093a40ed3dSBarry Smith PetscFunctionBegin; 13104482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 131170441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1312d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1313d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1314d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1315d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 13163a40ed3dSBarry Smith PetscFunctionReturn(0); 13179b94acceSBarry Smith } 13189b94acceSBarry Smith 13194a2ae208SSatish Balay #undef __FUNCT__ 13204a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 13219b94acceSBarry Smith /*@ 132233174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 132333174efeSLois Curfman McInnes 1324c7afd0dbSLois Curfman McInnes Not Collective 1325c7afd0dbSLois Curfman McInnes 132633174efeSLois Curfman McInnes Input Parameters: 1327c7afd0dbSLois Curfman McInnes + snes - the SNES context 132885385478SLisandro Dalcin . atol - absolute convergence tolerance 132933174efeSLois Curfman McInnes . rtol - relative convergence tolerance 133033174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 133133174efeSLois Curfman McInnes of the change in the solution between steps 133233174efeSLois Curfman McInnes . maxit - maximum number of iterations 1333c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1334fee21e36SBarry Smith 133533174efeSLois Curfman McInnes Notes: 133633174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 133733174efeSLois Curfman McInnes 133836851e7fSLois Curfman McInnes Level: intermediate 133936851e7fSLois Curfman McInnes 134033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 134133174efeSLois Curfman McInnes 134233174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 134333174efeSLois Curfman McInnes @*/ 134485385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 134533174efeSLois Curfman McInnes { 13463a40ed3dSBarry Smith PetscFunctionBegin; 13474482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 134885385478SLisandro Dalcin if (atol) *atol = snes->abstol; 134933174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 135033174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 135133174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 135233174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 13533a40ed3dSBarry Smith PetscFunctionReturn(0); 135433174efeSLois Curfman McInnes } 135533174efeSLois Curfman McInnes 13564a2ae208SSatish Balay #undef __FUNCT__ 13574a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 135833174efeSLois Curfman McInnes /*@ 13599b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 13609b94acceSBarry Smith 1361fee21e36SBarry Smith Collective on SNES 1362fee21e36SBarry Smith 1363c7afd0dbSLois Curfman McInnes Input Parameters: 1364c7afd0dbSLois Curfman McInnes + snes - the SNES context 1365c7afd0dbSLois Curfman McInnes - tol - tolerance 1366c7afd0dbSLois Curfman McInnes 13679b94acceSBarry Smith Options Database Key: 1368c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 13699b94acceSBarry Smith 137036851e7fSLois Curfman McInnes Level: intermediate 137136851e7fSLois Curfman McInnes 13729b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 13739b94acceSBarry Smith 13742492ecdbSBarry Smith .seealso: SNESSetTolerances() 13759b94acceSBarry Smith @*/ 137663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 13779b94acceSBarry Smith { 13783a40ed3dSBarry Smith PetscFunctionBegin; 13794482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 13809b94acceSBarry Smith snes->deltatol = tol; 13813a40ed3dSBarry Smith PetscFunctionReturn(0); 13829b94acceSBarry Smith } 13839b94acceSBarry Smith 1384df9fa365SBarry Smith /* 1385df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1386df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1387df9fa365SBarry Smith macros instead of functions 1388df9fa365SBarry Smith */ 13894a2ae208SSatish Balay #undef __FUNCT__ 1390a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 1391a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1392ce1608b8SBarry Smith { 1393dfbe8321SBarry Smith PetscErrorCode ierr; 1394ce1608b8SBarry Smith 1395ce1608b8SBarry Smith PetscFunctionBegin; 13964482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1397a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1398ce1608b8SBarry Smith PetscFunctionReturn(0); 1399ce1608b8SBarry Smith } 1400ce1608b8SBarry Smith 14014a2ae208SSatish Balay #undef __FUNCT__ 1402a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 1403a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1404df9fa365SBarry Smith { 1405dfbe8321SBarry Smith PetscErrorCode ierr; 1406df9fa365SBarry Smith 1407df9fa365SBarry Smith PetscFunctionBegin; 1408a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1409df9fa365SBarry Smith PetscFunctionReturn(0); 1410df9fa365SBarry Smith } 1411df9fa365SBarry Smith 14124a2ae208SSatish Balay #undef __FUNCT__ 1413a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 1414a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw) 1415df9fa365SBarry Smith { 1416dfbe8321SBarry Smith PetscErrorCode ierr; 1417df9fa365SBarry Smith 1418df9fa365SBarry Smith PetscFunctionBegin; 1419a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1420df9fa365SBarry Smith PetscFunctionReturn(0); 1421df9fa365SBarry Smith } 1422df9fa365SBarry Smith 14239b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 14249b94acceSBarry Smith 14254a2ae208SSatish Balay #undef __FUNCT__ 1426a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 14279b94acceSBarry Smith /*@C 1428a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 14299b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 14309b94acceSBarry Smith progress. 14319b94acceSBarry Smith 1432fee21e36SBarry Smith Collective on SNES 1433fee21e36SBarry Smith 1434c7afd0dbSLois Curfman McInnes Input Parameters: 1435c7afd0dbSLois Curfman McInnes + snes - the SNES context 1436c7afd0dbSLois Curfman McInnes . func - monitoring routine 1437b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1438e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 1439b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1440b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 14419b94acceSBarry Smith 1442c7afd0dbSLois Curfman McInnes Calling sequence of func: 1443a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 1444c7afd0dbSLois Curfman McInnes 1445c7afd0dbSLois Curfman McInnes + snes - the SNES context 1446c7afd0dbSLois Curfman McInnes . its - iteration number 1447c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 144840a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 14499b94acceSBarry Smith 14509665c990SLois Curfman McInnes Options Database Keys: 1451a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 1452a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 1453a6570f20SBarry Smith uses SNESMonitorLGCreate() 1454a6570f20SBarry Smith _ -snes_monitor_cancel - cancels all monitors that have 1455c7afd0dbSLois Curfman McInnes been hardwired into a code by 1456a6570f20SBarry Smith calls to SNESMonitorSet(), but 1457c7afd0dbSLois Curfman McInnes does not cancel those set via 1458c7afd0dbSLois Curfman McInnes the options database. 14599665c990SLois Curfman McInnes 1460639f9d9dSBarry Smith Notes: 14616bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 1462a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 14636bc08f3fSLois Curfman McInnes order in which they were set. 1464639f9d9dSBarry Smith 146536851e7fSLois Curfman McInnes Level: intermediate 146636851e7fSLois Curfman McInnes 14679b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 14689b94acceSBarry Smith 1469a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 14709b94acceSBarry Smith @*/ 1471b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 14729b94acceSBarry Smith { 1473b90d0a6eSBarry Smith PetscInt i; 1474b90d0a6eSBarry Smith 14753a40ed3dSBarry Smith PetscFunctionBegin; 14764482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1477639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 147829bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1479639f9d9dSBarry Smith } 1480b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 1481b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 1482b90d0a6eSBarry Smith 1483b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 1484b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 1485b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 1486b90d0a6eSBarry Smith PetscErrorCode ierr; 1487b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 1488b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 1489b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 1490b90d0a6eSBarry Smith ierr = (*monitordestroy)(mctx);CHKERRQ(ierr); 1491b90d0a6eSBarry Smith PetscFunctionReturn(0); 1492b90d0a6eSBarry Smith } 1493b90d0a6eSBarry Smith } 1494b90d0a6eSBarry Smith } 1495b90d0a6eSBarry Smith } 1496b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 1497b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1498639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 14993a40ed3dSBarry Smith PetscFunctionReturn(0); 15009b94acceSBarry Smith } 15019b94acceSBarry Smith 15024a2ae208SSatish Balay #undef __FUNCT__ 1503a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 15045cd90555SBarry Smith /*@C 1505a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 15065cd90555SBarry Smith 1507c7afd0dbSLois Curfman McInnes Collective on SNES 1508c7afd0dbSLois Curfman McInnes 15095cd90555SBarry Smith Input Parameters: 15105cd90555SBarry Smith . snes - the SNES context 15115cd90555SBarry Smith 15121a480d89SAdministrator Options Database Key: 1513a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 1514a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 1515c7afd0dbSLois Curfman McInnes set via the options database 15165cd90555SBarry Smith 15175cd90555SBarry Smith Notes: 15185cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 15195cd90555SBarry Smith 152036851e7fSLois Curfman McInnes Level: intermediate 152136851e7fSLois Curfman McInnes 15225cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 15235cd90555SBarry Smith 1524a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 15255cd90555SBarry Smith @*/ 1526a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes) 15275cd90555SBarry Smith { 1528d952e501SBarry Smith PetscErrorCode ierr; 1529d952e501SBarry Smith PetscInt i; 1530d952e501SBarry Smith 15315cd90555SBarry Smith PetscFunctionBegin; 15324482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1533d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1534d952e501SBarry Smith if (snes->monitordestroy[i]) { 1535d952e501SBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1536d952e501SBarry Smith } 1537d952e501SBarry Smith } 15385cd90555SBarry Smith snes->numbermonitors = 0; 15395cd90555SBarry Smith PetscFunctionReturn(0); 15405cd90555SBarry Smith } 15415cd90555SBarry Smith 15424a2ae208SSatish Balay #undef __FUNCT__ 15434a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 15449b94acceSBarry Smith /*@C 15459b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 15469b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 15479b94acceSBarry Smith 1548fee21e36SBarry Smith Collective on SNES 1549fee21e36SBarry Smith 1550c7afd0dbSLois Curfman McInnes Input Parameters: 1551c7afd0dbSLois Curfman McInnes + snes - the SNES context 1552c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1553c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1554c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 15559b94acceSBarry Smith 1556c7afd0dbSLois Curfman McInnes Calling sequence of func: 155706ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1558c7afd0dbSLois Curfman McInnes 1559c7afd0dbSLois Curfman McInnes + snes - the SNES context 156006ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 1561c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1562184914b5SBarry Smith . reason - reason for convergence/divergence 1563c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 15644b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 15654b27c08aSLois Curfman McInnes - f - 2-norm of function 15669b94acceSBarry Smith 156736851e7fSLois Curfman McInnes Level: advanced 156836851e7fSLois Curfman McInnes 15699b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 15709b94acceSBarry Smith 157185385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 15729b94acceSBarry Smith @*/ 157306ee9f85SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx) 15749b94acceSBarry Smith { 15753a40ed3dSBarry Smith PetscFunctionBegin; 15764482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 157785385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 157885385478SLisandro Dalcin snes->ops->converged = func; 157985385478SLisandro Dalcin snes->cnvP = cctx; 15803a40ed3dSBarry Smith PetscFunctionReturn(0); 15819b94acceSBarry Smith } 15829b94acceSBarry Smith 15834a2ae208SSatish Balay #undef __FUNCT__ 15844a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 158552baeb72SSatish Balay /*@ 1586184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1587184914b5SBarry Smith 1588184914b5SBarry Smith Not Collective 1589184914b5SBarry Smith 1590184914b5SBarry Smith Input Parameter: 1591184914b5SBarry Smith . snes - the SNES context 1592184914b5SBarry Smith 1593184914b5SBarry Smith Output Parameter: 1594e090d566SSatish Balay . reason - negative value indicates diverged, positive value converged, see petscsnes.h or the 1595184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1596184914b5SBarry Smith 1597184914b5SBarry Smith Level: intermediate 1598184914b5SBarry Smith 1599184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1600184914b5SBarry Smith 1601184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1602184914b5SBarry Smith 160385385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 1604184914b5SBarry Smith @*/ 160563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1606184914b5SBarry Smith { 1607184914b5SBarry Smith PetscFunctionBegin; 16084482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16094482741eSBarry Smith PetscValidPointer(reason,2); 1610184914b5SBarry Smith *reason = snes->reason; 1611184914b5SBarry Smith PetscFunctionReturn(0); 1612184914b5SBarry Smith } 1613184914b5SBarry Smith 16144a2ae208SSatish Balay #undef __FUNCT__ 16154a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 1616c9005455SLois Curfman McInnes /*@ 1617c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1618c9005455SLois Curfman McInnes 1619fee21e36SBarry Smith Collective on SNES 1620fee21e36SBarry Smith 1621c7afd0dbSLois Curfman McInnes Input Parameters: 1622c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1623c7afd0dbSLois Curfman McInnes . a - array to hold history 1624cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 1625758f92a0SBarry Smith . na - size of a and its 162664731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 1627758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1628c7afd0dbSLois Curfman McInnes 1629c9005455SLois Curfman McInnes Notes: 16304b27c08aSLois Curfman McInnes If set, this array will contain the function norms computed 1631c9005455SLois Curfman McInnes at each step. 1632c9005455SLois Curfman McInnes 1633c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1634c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1635c9005455SLois Curfman McInnes during the section of code that is being timed. 1636c9005455SLois Curfman McInnes 163736851e7fSLois Curfman McInnes Level: intermediate 163836851e7fSLois Curfman McInnes 1639c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1640758f92a0SBarry Smith 164108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1642758f92a0SBarry Smith 1643c9005455SLois Curfman McInnes @*/ 1644a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset) 1645c9005455SLois Curfman McInnes { 16463a40ed3dSBarry Smith PetscFunctionBegin; 16474482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16484482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 1649a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 1650c9005455SLois Curfman McInnes snes->conv_hist = a; 1651758f92a0SBarry Smith snes->conv_hist_its = its; 1652758f92a0SBarry Smith snes->conv_hist_max = na; 1653758f92a0SBarry Smith snes->conv_hist_reset = reset; 1654758f92a0SBarry Smith PetscFunctionReturn(0); 1655758f92a0SBarry Smith } 1656758f92a0SBarry Smith 16574a2ae208SSatish Balay #undef __FUNCT__ 16584a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 16590c4c9dddSBarry Smith /*@C 1660758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1661758f92a0SBarry Smith 1662758f92a0SBarry Smith Collective on SNES 1663758f92a0SBarry Smith 1664758f92a0SBarry Smith Input Parameter: 1665758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1666758f92a0SBarry Smith 1667758f92a0SBarry Smith Output Parameters: 1668758f92a0SBarry Smith . a - array to hold history 1669758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1670758f92a0SBarry Smith negative if not converged) for each solve. 1671758f92a0SBarry Smith - na - size of a and its 1672758f92a0SBarry Smith 1673758f92a0SBarry Smith Notes: 1674758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1675758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1676758f92a0SBarry Smith 1677758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1678758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1679758f92a0SBarry Smith during the section of code that is being timed. 1680758f92a0SBarry Smith 1681758f92a0SBarry Smith Level: intermediate 1682758f92a0SBarry Smith 1683758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1684758f92a0SBarry Smith 1685758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1686758f92a0SBarry Smith 1687758f92a0SBarry Smith @*/ 168863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 1689758f92a0SBarry Smith { 1690758f92a0SBarry Smith PetscFunctionBegin; 16914482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1692758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1693758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1694758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 16953a40ed3dSBarry Smith PetscFunctionReturn(0); 1696c9005455SLois Curfman McInnes } 1697c9005455SLois Curfman McInnes 1698e74ef692SMatthew Knepley #undef __FUNCT__ 1699e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 1700ac226902SBarry Smith /*@C 170176b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 17027e4bb74cSBarry Smith at the beginning o every iteration of the nonlinear solve. Specifically 17037e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 170476b2cf59SMatthew Knepley 170576b2cf59SMatthew Knepley Collective on SNES 170676b2cf59SMatthew Knepley 170776b2cf59SMatthew Knepley Input Parameters: 170876b2cf59SMatthew Knepley . snes - The nonlinear solver context 170976b2cf59SMatthew Knepley . func - The function 171076b2cf59SMatthew Knepley 171176b2cf59SMatthew Knepley Calling sequence of func: 1712b5d30489SBarry Smith . func (SNES snes, PetscInt step); 171376b2cf59SMatthew Knepley 171476b2cf59SMatthew Knepley . step - The current step of the iteration 171576b2cf59SMatthew Knepley 171676b2cf59SMatthew Knepley Level: intermediate 171776b2cf59SMatthew Knepley 171876b2cf59SMatthew Knepley .keywords: SNES, update 1719b5d30489SBarry Smith 172085385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 172176b2cf59SMatthew Knepley @*/ 172263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 172376b2cf59SMatthew Knepley { 172476b2cf59SMatthew Knepley PetscFunctionBegin; 17254482741eSBarry Smith PetscValidHeaderSpecific(snes, SNES_COOKIE,1); 1726e7788613SBarry Smith snes->ops->update = func; 172776b2cf59SMatthew Knepley PetscFunctionReturn(0); 172876b2cf59SMatthew Knepley } 172976b2cf59SMatthew Knepley 1730e74ef692SMatthew Knepley #undef __FUNCT__ 1731e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 173276b2cf59SMatthew Knepley /*@ 173376b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 173476b2cf59SMatthew Knepley 173576b2cf59SMatthew Knepley Not collective 173676b2cf59SMatthew Knepley 173776b2cf59SMatthew Knepley Input Parameters: 173876b2cf59SMatthew Knepley . snes - The nonlinear solver context 173976b2cf59SMatthew Knepley . step - The current step of the iteration 174076b2cf59SMatthew Knepley 1741205452f4SMatthew Knepley Level: intermediate 1742205452f4SMatthew Knepley 174376b2cf59SMatthew Knepley .keywords: SNES, update 1744a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 174576b2cf59SMatthew Knepley @*/ 174663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step) 174776b2cf59SMatthew Knepley { 174876b2cf59SMatthew Knepley PetscFunctionBegin; 174976b2cf59SMatthew Knepley PetscFunctionReturn(0); 175076b2cf59SMatthew Knepley } 175176b2cf59SMatthew Knepley 17524a2ae208SSatish Balay #undef __FUNCT__ 17534a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 17549b94acceSBarry Smith /* 17559b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 17569b94acceSBarry Smith positive parameter delta. 17579b94acceSBarry Smith 17589b94acceSBarry Smith Input Parameters: 1759c7afd0dbSLois Curfman McInnes + snes - the SNES context 17609b94acceSBarry Smith . y - approximate solution of linear system 17619b94acceSBarry Smith . fnorm - 2-norm of current function 1762c7afd0dbSLois Curfman McInnes - delta - trust region size 17639b94acceSBarry Smith 17649b94acceSBarry Smith Output Parameters: 1765c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 17669b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 17679b94acceSBarry Smith region, and exceeds zero otherwise. 1768c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 17699b94acceSBarry Smith 17709b94acceSBarry Smith Note: 17714b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 17729b94acceSBarry Smith is set to be the maximum allowable step size. 17739b94acceSBarry Smith 17749b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 17759b94acceSBarry Smith */ 1776dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 17779b94acceSBarry Smith { 1778064f8208SBarry Smith PetscReal nrm; 1779ea709b57SSatish Balay PetscScalar cnorm; 1780dfbe8321SBarry Smith PetscErrorCode ierr; 17813a40ed3dSBarry Smith 17823a40ed3dSBarry Smith PetscFunctionBegin; 17834482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 17844482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,2); 1785c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 1786184914b5SBarry Smith 1787064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 1788064f8208SBarry Smith if (nrm > *delta) { 1789064f8208SBarry Smith nrm = *delta/nrm; 1790064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 1791064f8208SBarry Smith cnorm = nrm; 17922dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 17939b94acceSBarry Smith *ynorm = *delta; 17949b94acceSBarry Smith } else { 17959b94acceSBarry Smith *gpnorm = 0.0; 1796064f8208SBarry Smith *ynorm = nrm; 17979b94acceSBarry Smith } 17983a40ed3dSBarry Smith PetscFunctionReturn(0); 17999b94acceSBarry Smith } 18009b94acceSBarry Smith 18014a2ae208SSatish Balay #undef __FUNCT__ 18024a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 18036ce558aeSBarry Smith /*@C 1804f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 1805f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 18069b94acceSBarry Smith 1807c7afd0dbSLois Curfman McInnes Collective on SNES 1808c7afd0dbSLois Curfman McInnes 1809b2002411SLois Curfman McInnes Input Parameters: 1810c7afd0dbSLois Curfman McInnes + snes - the SNES context 1811f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 181285385478SLisandro Dalcin - x - the solution vector. 18139b94acceSBarry Smith 1814b2002411SLois Curfman McInnes Notes: 18158ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 18168ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 18178ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 18188ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 18198ddd3da0SLois Curfman McInnes 182036851e7fSLois Curfman McInnes Level: beginner 182136851e7fSLois Curfman McInnes 18229b94acceSBarry Smith .keywords: SNES, nonlinear, solve 18239b94acceSBarry Smith 182485385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 18259b94acceSBarry Smith @*/ 1826f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x) 18279b94acceSBarry Smith { 1828dfbe8321SBarry Smith PetscErrorCode ierr; 1829f1af5d2fSBarry Smith PetscTruth flg; 1830eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 1831eabae89aSBarry Smith PetscViewer viewer; 1832052efed2SBarry Smith 18333a40ed3dSBarry Smith PetscFunctionBegin; 18344482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1835f69a0ea3SMatthew Knepley PetscValidHeaderSpecific(x,VEC_COOKIE,3); 1836f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 183785385478SLisandro Dalcin if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2); 183885385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 183985385478SLisandro Dalcin 184085385478SLisandro Dalcin /* set solution vector */ 184185385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 184285385478SLisandro Dalcin if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); } 184385385478SLisandro Dalcin snes->vec_sol = x; 184485385478SLisandro Dalcin /* set afine vector if provided */ 184585385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 184685385478SLisandro Dalcin if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); } 184785385478SLisandro Dalcin snes->vec_rhs = b; 184885385478SLisandro Dalcin 184985385478SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 185085385478SLisandro Dalcin ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr); 185170e92668SMatthew Knepley } 18523f149594SLisandro Dalcin 185370e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 18543f149594SLisandro Dalcin 1855abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 185650ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 1857d5e45103SBarry Smith 18583f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 18593f149594SLisandro Dalcin 1860e7788613SBarry Smith ierr = PetscExceptionTry1((*(snes)->ops->solve)(snes),PETSC_ERR_ARG_DOMAIN); 1861d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 186238f152feSBarry Smith /* this means that a caller above me has also tryed this exception so I don't handle it here, pass it up */ 186319717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(pierr); 1864c671dbe7SSatish Balay } else if (PetscExceptionCaught(ierr,PETSC_ERR_ARG_DOMAIN)) { 1865d5e45103SBarry Smith /* translate exception into SNES not converged reason */ 1866d5e45103SBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 186738f152feSBarry Smith ierr = 0; 186838f152feSBarry Smith } 186938f152feSBarry Smith CHKERRQ(ierr); 187085385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 187185385478SLisandro Dalcin 18723f149594SLisandro Dalcin if (!snes->reason) { 18733f149594SLisandro Dalcin SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 18743f149594SLisandro Dalcin } 18753f149594SLisandro Dalcin 1876*7adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1877eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 1878*7adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 1879eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 1880eabae89aSBarry Smith ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 1881eabae89aSBarry Smith } 1882eabae89aSBarry Smith 1883*7adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr); 1884da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 18855968eb51SBarry Smith if (snes->printreason) { 18865968eb51SBarry Smith if (snes->reason > 0) { 1887*7adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 18885968eb51SBarry Smith } else { 1889*7adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 18905968eb51SBarry Smith } 18915968eb51SBarry Smith } 18925968eb51SBarry Smith 18933a40ed3dSBarry Smith PetscFunctionReturn(0); 18949b94acceSBarry Smith } 18959b94acceSBarry Smith 18969b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 18979b94acceSBarry Smith 18984a2ae208SSatish Balay #undef __FUNCT__ 18994a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 190082bf6240SBarry Smith /*@C 19014b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 19029b94acceSBarry Smith 1903fee21e36SBarry Smith Collective on SNES 1904fee21e36SBarry Smith 1905c7afd0dbSLois Curfman McInnes Input Parameters: 1906c7afd0dbSLois Curfman McInnes + snes - the SNES context 1907454a90a3SBarry Smith - type - a known method 1908c7afd0dbSLois Curfman McInnes 1909c7afd0dbSLois Curfman McInnes Options Database Key: 1910454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 1911c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 1912ae12b187SLois Curfman McInnes 19139b94acceSBarry Smith Notes: 1914e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 19154b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 1916c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 19174b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 1918c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 19199b94acceSBarry Smith 1920ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 1921ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 1922ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 1923ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 1924ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 1925ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 1926ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 1927ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 1928ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 1929b0a32e0cSBarry Smith appropriate method. 193036851e7fSLois Curfman McInnes 193136851e7fSLois Curfman McInnes Level: intermediate 1932a703fe33SLois Curfman McInnes 1933454a90a3SBarry Smith .keywords: SNES, set, type 1934435da068SBarry Smith 1935435da068SBarry Smith .seealso: SNESType, SNESCreate() 1936435da068SBarry Smith 19379b94acceSBarry Smith @*/ 1938e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,SNESType type) 19399b94acceSBarry Smith { 1940dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 19416831982aSBarry Smith PetscTruth match; 19423a40ed3dSBarry Smith 19433a40ed3dSBarry Smith PetscFunctionBegin; 19444482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 19454482741eSBarry Smith PetscValidCharPointer(type,2); 194682bf6240SBarry Smith 19476831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 19480f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 194992ff6ae8SBarry Smith 1950*7adad957SLisandro Dalcin ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 1951958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 195275396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 195375396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 195475396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 195575396ef9SLisandro Dalcin snes->ops->setup = 0; 195675396ef9SLisandro Dalcin snes->ops->solve = 0; 195775396ef9SLisandro Dalcin snes->ops->view = 0; 195875396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 195975396ef9SLisandro Dalcin snes->ops->destroy = 0; 196075396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 196175396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 19623a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 1963454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 19643a40ed3dSBarry Smith PetscFunctionReturn(0); 19659b94acceSBarry Smith } 19669b94acceSBarry Smith 1967a847f771SSatish Balay 19689b94acceSBarry Smith /* --------------------------------------------------------------------- */ 19694a2ae208SSatish Balay #undef __FUNCT__ 19704a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 197152baeb72SSatish Balay /*@ 19729b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 1973f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 19749b94acceSBarry Smith 1975fee21e36SBarry Smith Not Collective 1976fee21e36SBarry Smith 197736851e7fSLois Curfman McInnes Level: advanced 197836851e7fSLois Curfman McInnes 19799b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 19809b94acceSBarry Smith 19819b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 19829b94acceSBarry Smith @*/ 198363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void) 19849b94acceSBarry Smith { 1985dfbe8321SBarry Smith PetscErrorCode ierr; 198682bf6240SBarry Smith 19873a40ed3dSBarry Smith PetscFunctionBegin; 19881441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 19894c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 19903a40ed3dSBarry Smith PetscFunctionReturn(0); 19919b94acceSBarry Smith } 19929b94acceSBarry Smith 19934a2ae208SSatish Balay #undef __FUNCT__ 19944a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 19959b94acceSBarry Smith /*@C 19969a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 19979b94acceSBarry Smith 1998c7afd0dbSLois Curfman McInnes Not Collective 1999c7afd0dbSLois Curfman McInnes 20009b94acceSBarry Smith Input Parameter: 20014b0e389bSBarry Smith . snes - nonlinear solver context 20029b94acceSBarry Smith 20039b94acceSBarry Smith Output Parameter: 20043a7fca6bSBarry Smith . type - SNES method (a character string) 20059b94acceSBarry Smith 200636851e7fSLois Curfman McInnes Level: intermediate 200736851e7fSLois Curfman McInnes 2008454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 20099b94acceSBarry Smith @*/ 201063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,SNESType *type) 20119b94acceSBarry Smith { 20123a40ed3dSBarry Smith PetscFunctionBegin; 20134482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20144482741eSBarry Smith PetscValidPointer(type,2); 2015*7adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 20163a40ed3dSBarry Smith PetscFunctionReturn(0); 20179b94acceSBarry Smith } 20189b94acceSBarry Smith 20194a2ae208SSatish Balay #undef __FUNCT__ 20204a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 202152baeb72SSatish Balay /*@ 20229b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 20239b94acceSBarry Smith stored. 20249b94acceSBarry Smith 2025c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2026c7afd0dbSLois Curfman McInnes 20279b94acceSBarry Smith Input Parameter: 20289b94acceSBarry Smith . snes - the SNES context 20299b94acceSBarry Smith 20309b94acceSBarry Smith Output Parameter: 20319b94acceSBarry Smith . x - the solution 20329b94acceSBarry Smith 203370e92668SMatthew Knepley Level: intermediate 203436851e7fSLois Curfman McInnes 20359b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 20369b94acceSBarry Smith 203785385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 20389b94acceSBarry Smith @*/ 203963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x) 20409b94acceSBarry Smith { 20413a40ed3dSBarry Smith PetscFunctionBegin; 20424482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20434482741eSBarry Smith PetscValidPointer(x,2); 204485385478SLisandro Dalcin *x = snes->vec_sol; 204570e92668SMatthew Knepley PetscFunctionReturn(0); 204670e92668SMatthew Knepley } 204770e92668SMatthew Knepley 204870e92668SMatthew Knepley #undef __FUNCT__ 20494a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 205052baeb72SSatish Balay /*@ 20519b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 20529b94acceSBarry Smith stored. 20539b94acceSBarry Smith 2054c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2055c7afd0dbSLois Curfman McInnes 20569b94acceSBarry Smith Input Parameter: 20579b94acceSBarry Smith . snes - the SNES context 20589b94acceSBarry Smith 20599b94acceSBarry Smith Output Parameter: 20609b94acceSBarry Smith . x - the solution update 20619b94acceSBarry Smith 206236851e7fSLois Curfman McInnes Level: advanced 206336851e7fSLois Curfman McInnes 20649b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 20659b94acceSBarry Smith 206685385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 20679b94acceSBarry Smith @*/ 206863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x) 20699b94acceSBarry Smith { 20703a40ed3dSBarry Smith PetscFunctionBegin; 20714482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20724482741eSBarry Smith PetscValidPointer(x,2); 207385385478SLisandro Dalcin *x = snes->vec_sol_update; 20743a40ed3dSBarry Smith PetscFunctionReturn(0); 20759b94acceSBarry Smith } 20769b94acceSBarry Smith 20774a2ae208SSatish Balay #undef __FUNCT__ 20784a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 20799b94acceSBarry Smith /*@C 20803638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 20819b94acceSBarry Smith 2082c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2083c7afd0dbSLois Curfman McInnes 20849b94acceSBarry Smith Input Parameter: 20859b94acceSBarry Smith . snes - the SNES context 20869b94acceSBarry Smith 20879b94acceSBarry Smith Output Parameter: 20887bf4e008SBarry Smith + r - the function (or PETSC_NULL) 208970e92668SMatthew Knepley . func - the function (or PETSC_NULL) 209070e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 20919b94acceSBarry Smith 209236851e7fSLois Curfman McInnes Level: advanced 209336851e7fSLois Curfman McInnes 2094a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 20959b94acceSBarry Smith 20964b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 20979b94acceSBarry Smith @*/ 209870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 20999b94acceSBarry Smith { 21003a40ed3dSBarry Smith PetscFunctionBegin; 21014482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 210285385478SLisandro Dalcin if (r) *r = snes->vec_func; 2103e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 210470e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 21053a40ed3dSBarry Smith PetscFunctionReturn(0); 21069b94acceSBarry Smith } 21079b94acceSBarry Smith 21084a2ae208SSatish Balay #undef __FUNCT__ 21094a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 21103c7409f5SSatish Balay /*@C 21113c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2112d850072dSLois Curfman McInnes SNES options in the database. 21133c7409f5SSatish Balay 2114fee21e36SBarry Smith Collective on SNES 2115fee21e36SBarry Smith 2116c7afd0dbSLois Curfman McInnes Input Parameter: 2117c7afd0dbSLois Curfman McInnes + snes - the SNES context 2118c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2119c7afd0dbSLois Curfman McInnes 2120d850072dSLois Curfman McInnes Notes: 2121a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2122c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2123d850072dSLois Curfman McInnes 212436851e7fSLois Curfman McInnes Level: advanced 212536851e7fSLois Curfman McInnes 21263c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2127a86d99e1SLois Curfman McInnes 2128a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 21293c7409f5SSatish Balay @*/ 213063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[]) 21313c7409f5SSatish Balay { 2132dfbe8321SBarry Smith PetscErrorCode ierr; 21333c7409f5SSatish Balay 21343a40ed3dSBarry Smith PetscFunctionBegin; 21354482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2136639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 213794b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 21383a40ed3dSBarry Smith PetscFunctionReturn(0); 21393c7409f5SSatish Balay } 21403c7409f5SSatish Balay 21414a2ae208SSatish Balay #undef __FUNCT__ 21424a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 21433c7409f5SSatish Balay /*@C 2144f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2145d850072dSLois Curfman McInnes SNES options in the database. 21463c7409f5SSatish Balay 2147fee21e36SBarry Smith Collective on SNES 2148fee21e36SBarry Smith 2149c7afd0dbSLois Curfman McInnes Input Parameters: 2150c7afd0dbSLois Curfman McInnes + snes - the SNES context 2151c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2152c7afd0dbSLois Curfman McInnes 2153d850072dSLois Curfman McInnes Notes: 2154a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2155c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2156d850072dSLois Curfman McInnes 215736851e7fSLois Curfman McInnes Level: advanced 215836851e7fSLois Curfman McInnes 21593c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2160a86d99e1SLois Curfman McInnes 2161a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 21623c7409f5SSatish Balay @*/ 216363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 21643c7409f5SSatish Balay { 2165dfbe8321SBarry Smith PetscErrorCode ierr; 21663c7409f5SSatish Balay 21673a40ed3dSBarry Smith PetscFunctionBegin; 21684482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2169639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 217094b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 21713a40ed3dSBarry Smith PetscFunctionReturn(0); 21723c7409f5SSatish Balay } 21733c7409f5SSatish Balay 21744a2ae208SSatish Balay #undef __FUNCT__ 21754a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 21769ab63eb5SSatish Balay /*@C 21773c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 21783c7409f5SSatish Balay SNES options in the database. 21793c7409f5SSatish Balay 2180c7afd0dbSLois Curfman McInnes Not Collective 2181c7afd0dbSLois Curfman McInnes 21823c7409f5SSatish Balay Input Parameter: 21833c7409f5SSatish Balay . snes - the SNES context 21843c7409f5SSatish Balay 21853c7409f5SSatish Balay Output Parameter: 21863c7409f5SSatish Balay . prefix - pointer to the prefix string used 21873c7409f5SSatish Balay 21889ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 21899ab63eb5SSatish Balay sufficient length to hold the prefix. 21909ab63eb5SSatish Balay 219136851e7fSLois Curfman McInnes Level: advanced 219236851e7fSLois Curfman McInnes 21933c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2194a86d99e1SLois Curfman McInnes 2195a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 21963c7409f5SSatish Balay @*/ 2197e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 21983c7409f5SSatish Balay { 2199dfbe8321SBarry Smith PetscErrorCode ierr; 22003c7409f5SSatish Balay 22013a40ed3dSBarry Smith PetscFunctionBegin; 22024482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2203639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 22043a40ed3dSBarry Smith PetscFunctionReturn(0); 22053c7409f5SSatish Balay } 22063c7409f5SSatish Balay 2207b2002411SLois Curfman McInnes 22084a2ae208SSatish Balay #undef __FUNCT__ 22094a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 22103cea93caSBarry Smith /*@C 22113cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 22123cea93caSBarry Smith 22137f6c08e0SMatthew Knepley Level: advanced 22143cea93caSBarry Smith @*/ 221563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2216b2002411SLois Curfman McInnes { 2217e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2218dfbe8321SBarry Smith PetscErrorCode ierr; 2219b2002411SLois Curfman McInnes 2220b2002411SLois Curfman McInnes PetscFunctionBegin; 2221b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2222c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2223b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2224b2002411SLois Curfman McInnes } 2225da9b6338SBarry Smith 2226da9b6338SBarry Smith #undef __FUNCT__ 2227da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 222863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes) 2229da9b6338SBarry Smith { 2230dfbe8321SBarry Smith PetscErrorCode ierr; 223177431f27SBarry Smith PetscInt N,i,j; 2232da9b6338SBarry Smith Vec u,uh,fh; 2233da9b6338SBarry Smith PetscScalar value; 2234da9b6338SBarry Smith PetscReal norm; 2235da9b6338SBarry Smith 2236da9b6338SBarry Smith PetscFunctionBegin; 2237da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 2238da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 2239da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 2240da9b6338SBarry Smith 2241da9b6338SBarry Smith /* currently only works for sequential */ 2242da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 2243da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 2244da9b6338SBarry Smith for (i=0; i<N; i++) { 2245da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 224677431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 2247da9b6338SBarry Smith for (j=-10; j<11; j++) { 2248ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 2249da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 22503ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 2251da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 225277431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 2253da9b6338SBarry Smith value = -value; 2254da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 2255da9b6338SBarry Smith } 2256da9b6338SBarry Smith } 2257da9b6338SBarry Smith ierr = VecDestroy(uh);CHKERRQ(ierr); 2258da9b6338SBarry Smith ierr = VecDestroy(fh);CHKERRQ(ierr); 2259da9b6338SBarry Smith PetscFunctionReturn(0); 2260da9b6338SBarry Smith } 226171f87433Sdalcinl 226271f87433Sdalcinl #undef __FUNCT__ 2263fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 226471f87433Sdalcinl /*@ 2265fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 226671f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 226771f87433Sdalcinl Newton method. 226871f87433Sdalcinl 226971f87433Sdalcinl Collective on SNES 227071f87433Sdalcinl 227171f87433Sdalcinl Input Parameters: 227271f87433Sdalcinl + snes - SNES context 227371f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 227471f87433Sdalcinl 227571f87433Sdalcinl Notes: 227671f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 227771f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 227871f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 227971f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 228071f87433Sdalcinl solver. 228171f87433Sdalcinl 228271f87433Sdalcinl Level: advanced 228371f87433Sdalcinl 228471f87433Sdalcinl Reference: 228571f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 228671f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 228771f87433Sdalcinl 228871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 228971f87433Sdalcinl 2290fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 229171f87433Sdalcinl @*/ 2292fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag) 229371f87433Sdalcinl { 229471f87433Sdalcinl PetscFunctionBegin; 229571f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 229671f87433Sdalcinl snes->ksp_ewconv = flag; 229771f87433Sdalcinl PetscFunctionReturn(0); 229871f87433Sdalcinl } 229971f87433Sdalcinl 230071f87433Sdalcinl #undef __FUNCT__ 2301fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 230271f87433Sdalcinl /*@ 2303fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 230471f87433Sdalcinl for computing relative tolerance for linear solvers within an 230571f87433Sdalcinl inexact Newton method. 230671f87433Sdalcinl 230771f87433Sdalcinl Not Collective 230871f87433Sdalcinl 230971f87433Sdalcinl Input Parameter: 231071f87433Sdalcinl . snes - SNES context 231171f87433Sdalcinl 231271f87433Sdalcinl Output Parameter: 231371f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 231471f87433Sdalcinl 231571f87433Sdalcinl Notes: 231671f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 231771f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 231871f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 231971f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 232071f87433Sdalcinl solver. 232171f87433Sdalcinl 232271f87433Sdalcinl Level: advanced 232371f87433Sdalcinl 232471f87433Sdalcinl Reference: 232571f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 232671f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 232771f87433Sdalcinl 232871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 232971f87433Sdalcinl 2330fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 233171f87433Sdalcinl @*/ 2332fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag) 233371f87433Sdalcinl { 233471f87433Sdalcinl PetscFunctionBegin; 233571f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 233671f87433Sdalcinl PetscValidPointer(flag,2); 233771f87433Sdalcinl *flag = snes->ksp_ewconv; 233871f87433Sdalcinl PetscFunctionReturn(0); 233971f87433Sdalcinl } 234071f87433Sdalcinl 234171f87433Sdalcinl #undef __FUNCT__ 2342fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 234371f87433Sdalcinl /*@ 2344fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 234571f87433Sdalcinl convergence criteria for the linear solvers within an inexact 234671f87433Sdalcinl Newton method. 234771f87433Sdalcinl 234871f87433Sdalcinl Collective on SNES 234971f87433Sdalcinl 235071f87433Sdalcinl Input Parameters: 235171f87433Sdalcinl + snes - SNES context 235271f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 235371f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 235471f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 235571f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 235671f87433Sdalcinl (0 <= gamma2 <= 1) 235771f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 235871f87433Sdalcinl . alpha2 - power for safeguard 235971f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 236071f87433Sdalcinl 236171f87433Sdalcinl Note: 236271f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 236371f87433Sdalcinl 236471f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 236571f87433Sdalcinl 236671f87433Sdalcinl Level: advanced 236771f87433Sdalcinl 236871f87433Sdalcinl Reference: 236971f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 237071f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 237171f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 237271f87433Sdalcinl 237371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 237471f87433Sdalcinl 2375fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 237671f87433Sdalcinl @*/ 2377fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 237871f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 237971f87433Sdalcinl { 2380fa9f3622SBarry Smith SNESKSPEW *kctx; 238171f87433Sdalcinl PetscFunctionBegin; 238271f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2383fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 238471f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 238571f87433Sdalcinl 238671f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 238771f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 238871f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 238971f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 239071f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 239171f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 239271f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 239371f87433Sdalcinl 239471f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 239571f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 239671f87433Sdalcinl } 239771f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 239871f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 239971f87433Sdalcinl } 240071f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 240171f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 240271f87433Sdalcinl } 240371f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 240471f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 240571f87433Sdalcinl } 240671f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 240771f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 240871f87433Sdalcinl } 240971f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 241071f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 241171f87433Sdalcinl } 241271f87433Sdalcinl PetscFunctionReturn(0); 241371f87433Sdalcinl } 241471f87433Sdalcinl 241571f87433Sdalcinl #undef __FUNCT__ 2416fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 241771f87433Sdalcinl /*@ 2418fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 241971f87433Sdalcinl convergence criteria for the linear solvers within an inexact 242071f87433Sdalcinl Newton method. 242171f87433Sdalcinl 242271f87433Sdalcinl Not Collective 242371f87433Sdalcinl 242471f87433Sdalcinl Input Parameters: 242571f87433Sdalcinl snes - SNES context 242671f87433Sdalcinl 242771f87433Sdalcinl Output Parameters: 242871f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 242971f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 243071f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 243171f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 243271f87433Sdalcinl (0 <= gamma2 <= 1) 243371f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 243471f87433Sdalcinl . alpha2 - power for safeguard 243571f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 243671f87433Sdalcinl 243771f87433Sdalcinl Level: advanced 243871f87433Sdalcinl 243971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 244071f87433Sdalcinl 2441fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 244271f87433Sdalcinl @*/ 2443fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 244471f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 244571f87433Sdalcinl { 2446fa9f3622SBarry Smith SNESKSPEW *kctx; 244771f87433Sdalcinl PetscFunctionBegin; 244871f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2449fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 245071f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 245171f87433Sdalcinl if(version) *version = kctx->version; 245271f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 245371f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 245471f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 245571f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 245671f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 245771f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 245871f87433Sdalcinl PetscFunctionReturn(0); 245971f87433Sdalcinl } 246071f87433Sdalcinl 246171f87433Sdalcinl #undef __FUNCT__ 2462fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 2463fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 246471f87433Sdalcinl { 246571f87433Sdalcinl PetscErrorCode ierr; 2466fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 246771f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 246871f87433Sdalcinl 246971f87433Sdalcinl PetscFunctionBegin; 247071f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 247171f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 247271f87433Sdalcinl rtol = kctx->rtol_0; 247371f87433Sdalcinl } else { 247471f87433Sdalcinl if (kctx->version == 1) { 247571f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 247671f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 247771f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 247871f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 247971f87433Sdalcinl } else if (kctx->version == 2) { 248071f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 248171f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 248271f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 248371f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 248471f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 248571f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 248671f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 248771f87433Sdalcinl stol = PetscMax(rtol,stol); 248871f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 248971f87433Sdalcinl /* safeguard: avoid oversolving */ 249071f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 249171f87433Sdalcinl stol = PetscMax(rtol,stol); 249271f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 249371f87433Sdalcinl } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 249471f87433Sdalcinl } 249571f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 249671f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 249771f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 249871f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 249971f87433Sdalcinl PetscFunctionReturn(0); 250071f87433Sdalcinl } 250171f87433Sdalcinl 250271f87433Sdalcinl #undef __FUNCT__ 2503fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 2504fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 250571f87433Sdalcinl { 250671f87433Sdalcinl PetscErrorCode ierr; 2507fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 250871f87433Sdalcinl PCSide pcside; 250971f87433Sdalcinl Vec lres; 251071f87433Sdalcinl 251171f87433Sdalcinl PetscFunctionBegin; 251271f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 251371f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 251471f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 251571f87433Sdalcinl if (kctx->version == 1) { 251671f87433Sdalcinl ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr); 251771f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 251871f87433Sdalcinl /* KSP residual is true linear residual */ 251971f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 252071f87433Sdalcinl } else { 252171f87433Sdalcinl /* KSP residual is preconditioned residual */ 252271f87433Sdalcinl /* compute true linear residual norm */ 252371f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 252471f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 252571f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 252671f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 252771f87433Sdalcinl ierr = VecDestroy(lres);CHKERRQ(ierr); 252871f87433Sdalcinl } 252971f87433Sdalcinl } 253071f87433Sdalcinl PetscFunctionReturn(0); 253171f87433Sdalcinl } 253271f87433Sdalcinl 253371f87433Sdalcinl #undef __FUNCT__ 253471f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 253571f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 253671f87433Sdalcinl { 253771f87433Sdalcinl PetscErrorCode ierr; 253871f87433Sdalcinl 253971f87433Sdalcinl PetscFunctionBegin; 2540fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 254171f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 2542fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 254371f87433Sdalcinl PetscFunctionReturn(0); 254471f87433Sdalcinl } 2545