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__ 134936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError" 144936397dSBarry Smith /*@C 154936397dSBarry Smith SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not 164936397dSBarry Smith in the functions domain. For example, negative pressure. 174936397dSBarry Smith 184936397dSBarry Smith Collective on SNES 194936397dSBarry Smith 204936397dSBarry Smith Input Parameters: 214936397dSBarry Smith . SNES - the SNES context 224936397dSBarry Smith 2328529972SSatish Balay Level: advanced 244936397dSBarry Smith 254936397dSBarry Smith .keywords: SNES, view 264936397dSBarry Smith 274936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction() 284936397dSBarry Smith @*/ 294936397dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunctionDomainError(SNES snes) 304936397dSBarry Smith { 314936397dSBarry Smith PetscFunctionBegin; 324936397dSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 334936397dSBarry Smith snes->domainerror = PETSC_TRUE; 344936397dSBarry Smith PetscFunctionReturn(0); 354936397dSBarry Smith } 364936397dSBarry Smith 374936397dSBarry Smith #undef __FUNCT__ 384a2ae208SSatish Balay #define __FUNCT__ "SNESView" 397e2c5f70SBarry Smith /*@C 409b94acceSBarry Smith SNESView - Prints the SNES data structure. 419b94acceSBarry Smith 424c49b128SBarry Smith Collective on SNES 43fee21e36SBarry Smith 44c7afd0dbSLois Curfman McInnes Input Parameters: 45c7afd0dbSLois Curfman McInnes + SNES - the SNES context 46c7afd0dbSLois Curfman McInnes - viewer - visualization context 47c7afd0dbSLois Curfman McInnes 489b94acceSBarry Smith Options Database Key: 49c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 509b94acceSBarry Smith 519b94acceSBarry Smith Notes: 529b94acceSBarry Smith The available visualization contexts include 53b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 54b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 55c8a8ba5cSLois Curfman McInnes output where only the first processor opens 56c8a8ba5cSLois Curfman McInnes the file. All other processors send their 57c8a8ba5cSLois Curfman McInnes data to the first processor to print. 589b94acceSBarry Smith 593e081fefSLois Curfman McInnes The user can open an alternative visualization context with 60b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 619b94acceSBarry Smith 6236851e7fSLois Curfman McInnes Level: beginner 6336851e7fSLois Curfman McInnes 649b94acceSBarry Smith .keywords: SNES, view 659b94acceSBarry Smith 66b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 679b94acceSBarry Smith @*/ 6863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer) 699b94acceSBarry Smith { 70fa9f3622SBarry Smith SNESKSPEW *kctx; 71dfbe8321SBarry Smith PetscErrorCode ierr; 7294b7f48cSBarry Smith KSP ksp; 73e060cb09SBarry Smith SNESType type; 7432077d6dSBarry Smith PetscTruth iascii,isstring; 759b94acceSBarry Smith 763a40ed3dSBarry Smith PetscFunctionBegin; 774482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 783050cee2SBarry Smith if (!viewer) { 797adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr); 803050cee2SBarry Smith } 814482741eSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2); 82c9780b6fSBarry Smith PetscCheckSameComm(snes,1,viewer,2); 8374679c65SBarry Smith 8432077d6dSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 85b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr); 8632077d6dSBarry Smith if (iascii) { 877adad957SLisandro Dalcin if (((PetscObject)snes)->prefix) { 887adad957SLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",((PetscObject)snes)->prefix);CHKERRQ(ierr); 893a7fca6bSBarry Smith } else { 90b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr); 913a7fca6bSBarry Smith } 92454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 93454a90a3SBarry Smith if (type) { 94b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," type: %s\n",type);CHKERRQ(ierr); 95184914b5SBarry Smith } else { 96b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," type: not set yet\n");CHKERRQ(ierr); 97184914b5SBarry Smith } 98e7788613SBarry Smith if (snes->ops->view) { 99b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 100e7788613SBarry Smith ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 101b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1020ef38995SBarry Smith } 10377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 104a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, solution=%G\n", 10570441072SBarry Smith snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr); 10677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr); 10777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr); 1089b94acceSBarry Smith if (snes->ksp_ewconv) { 109fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 1109b94acceSBarry Smith if (kctx) { 11177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr); 112a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 113a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 1149b94acceSBarry Smith } 1159b94acceSBarry Smith } 1160f5bd95cSBarry Smith } else if (isstring) { 117454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 118b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 11919bcc07fSBarry Smith } 12094b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 121b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 12294b7f48cSBarry Smith ierr = KSPView(ksp,viewer);CHKERRQ(ierr); 123b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1243a40ed3dSBarry Smith PetscFunctionReturn(0); 1259b94acceSBarry Smith } 1269b94acceSBarry Smith 12776b2cf59SMatthew Knepley /* 12876b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 12976b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 13076b2cf59SMatthew Knepley */ 13176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 132a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 1336849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 13476b2cf59SMatthew Knepley 135e74ef692SMatthew Knepley #undef __FUNCT__ 136e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker" 137ac226902SBarry Smith /*@C 13876b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 13976b2cf59SMatthew Knepley 14076b2cf59SMatthew Knepley Not Collective 14176b2cf59SMatthew Knepley 14276b2cf59SMatthew Knepley Input Parameter: 14376b2cf59SMatthew Knepley . snescheck - function that checks for options 14476b2cf59SMatthew Knepley 14576b2cf59SMatthew Knepley Level: developer 14676b2cf59SMatthew Knepley 14776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions() 14876b2cf59SMatthew Knepley @*/ 14963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 15076b2cf59SMatthew Knepley { 15176b2cf59SMatthew Knepley PetscFunctionBegin; 15276b2cf59SMatthew Knepley if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 15377431f27SBarry Smith SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS); 15476b2cf59SMatthew Knepley } 15576b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 15676b2cf59SMatthew Knepley PetscFunctionReturn(0); 15776b2cf59SMatthew Knepley } 15876b2cf59SMatthew Knepley 1594a2ae208SSatish Balay #undef __FUNCT__ 1604a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions" 1619b94acceSBarry Smith /*@ 16294b7f48cSBarry Smith SNESSetFromOptions - Sets various SNES and KSP parameters from user options. 1639b94acceSBarry Smith 164c7afd0dbSLois Curfman McInnes Collective on SNES 165c7afd0dbSLois Curfman McInnes 1669b94acceSBarry Smith Input Parameter: 1679b94acceSBarry Smith . snes - the SNES context 1689b94acceSBarry Smith 16936851e7fSLois Curfman McInnes Options Database Keys: 1706831982aSBarry Smith + -snes_type <type> - ls, tr, umls, umtr, test 17182738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 17282738288SBarry Smith of the change in the solution between steps 17370441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 174b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 175b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 176b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 17750ffb88aSMatthew Knepley . -snes_max_fail <max_fail> - maximum number of failures 178b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 1792492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 18082738288SBarry Smith solver; hence iterations will continue until max_it 1811fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 18282738288SBarry Smith of convergence test 183e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 184e8105e01SRichard Katz filename given prints to stdout 185a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 186a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 187a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 188a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 189e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 1905968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 191fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 19282738288SBarry Smith 19382738288SBarry Smith Options Database for Eisenstat-Walker method: 194fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 1954b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 19636851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 19736851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 19836851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 19936851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 20036851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 20136851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 20282738288SBarry Smith 20311ca99fdSLois Curfman McInnes Notes: 20411ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 20511ca99fdSLois Curfman McInnes the users manual. 20683e2fdc7SBarry Smith 20736851e7fSLois Curfman McInnes Level: beginner 20836851e7fSLois Curfman McInnes 2099b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 2109b94acceSBarry Smith 21169ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 2129b94acceSBarry Smith @*/ 21363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes) 2149b94acceSBarry Smith { 215f1af5d2fSBarry Smith PetscTruth flg; 21685385478SLisandro Dalcin PetscInt i,indx; 21785385478SLisandro Dalcin const char *deft = SNESLS; 21885385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 21985385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 220e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 22123d894e5SBarry Smith PetscViewerASCIIMonitor monviewer; 22285385478SLisandro Dalcin PetscErrorCode ierr; 2239b94acceSBarry Smith 2243a40ed3dSBarry Smith PetscFunctionBegin; 2254482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 226ca161407SBarry Smith 2277adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr); 228186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2297adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 230b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 231d64ed03dSBarry Smith if (flg) { 232186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 2337adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 234186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 235d64ed03dSBarry Smith } 236909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 23793c39befSBarry Smith 23887828ca2SBarry Smith ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 23970441072SBarry Smith ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 240186905e3SBarry Smith 24187828ca2SBarry Smith ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 242b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 243b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 24450ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 24585385478SLisandro Dalcin 24685385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 24785385478SLisandro Dalcin if (flg) { 24885385478SLisandro Dalcin switch (indx) { 24985385478SLisandro Dalcin case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL);CHKERRQ(ierr); break; 25085385478SLisandro Dalcin case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL);CHKERRQ(ierr); break; 25185385478SLisandro Dalcin } 25285385478SLisandro Dalcin } 25385385478SLisandro Dalcin 2545968eb51SBarry Smith ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr); 2555968eb51SBarry Smith if (flg) { 2565968eb51SBarry Smith snes->printreason = PETSC_TRUE; 2575968eb51SBarry Smith } 258186905e3SBarry Smith 25985385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 26085385478SLisandro Dalcin 261fa9f3622SBarry Smith ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 262186905e3SBarry Smith 263fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 264fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 265fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 266fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 267fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 268fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 269fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 270186905e3SBarry Smith 271a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr); 272a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 273eabae89aSBarry Smith 274a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 275e8105e01SRichard Katz if (flg) { 2767adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 27723d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 278e8105e01SRichard Katz } 279eabae89aSBarry Smith 280a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 281eabae89aSBarry Smith if (flg) { 2827adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 283f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 284e8105e01SRichard Katz } 285eabae89aSBarry Smith 286a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 287eabae89aSBarry Smith if (flg) { 2887adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 28923d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 290eabae89aSBarry Smith } 291eabae89aSBarry Smith 292a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr); 293a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 294a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr); 295a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 296a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr); 297a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 298a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr); 299a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 300e24b481bSBarry Smith 301b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr); 3024b27c08aSLois Curfman McInnes if (flg) { 303186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 304ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 3059b94acceSBarry Smith } 306639f9d9dSBarry Smith 30776b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 30876b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 30976b2cf59SMatthew Knepley } 31076b2cf59SMatthew Knepley 311e7788613SBarry Smith if (snes->ops->setfromoptions) { 312e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 313639f9d9dSBarry Smith } 314b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3154bbc92c1SBarry Smith 31685385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 31793993e2dSLois Curfman McInnes 3183a40ed3dSBarry Smith PetscFunctionReturn(0); 3199b94acceSBarry Smith } 3209b94acceSBarry Smith 321a847f771SSatish Balay 3224a2ae208SSatish Balay #undef __FUNCT__ 3234a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 3249b94acceSBarry Smith /*@ 3259b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3269b94acceSBarry Smith the nonlinear solvers. 3279b94acceSBarry Smith 328fee21e36SBarry Smith Collective on SNES 329fee21e36SBarry Smith 330c7afd0dbSLois Curfman McInnes Input Parameters: 331c7afd0dbSLois Curfman McInnes + snes - the SNES context 332c7afd0dbSLois Curfman McInnes - usrP - optional user context 333c7afd0dbSLois Curfman McInnes 33436851e7fSLois Curfman McInnes Level: intermediate 33536851e7fSLois Curfman McInnes 3369b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3379b94acceSBarry Smith 3389b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3399b94acceSBarry Smith @*/ 34063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP) 3419b94acceSBarry Smith { 3423a40ed3dSBarry Smith PetscFunctionBegin; 3434482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3449b94acceSBarry Smith snes->user = usrP; 3453a40ed3dSBarry Smith PetscFunctionReturn(0); 3469b94acceSBarry Smith } 34774679c65SBarry Smith 3484a2ae208SSatish Balay #undef __FUNCT__ 3494a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 3509b94acceSBarry Smith /*@C 3519b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3529b94acceSBarry Smith nonlinear solvers. 3539b94acceSBarry Smith 354c7afd0dbSLois Curfman McInnes Not Collective 355c7afd0dbSLois Curfman McInnes 3569b94acceSBarry Smith Input Parameter: 3579b94acceSBarry Smith . snes - SNES context 3589b94acceSBarry Smith 3599b94acceSBarry Smith Output Parameter: 3609b94acceSBarry Smith . usrP - user context 3619b94acceSBarry Smith 36236851e7fSLois Curfman McInnes Level: intermediate 36336851e7fSLois Curfman McInnes 3649b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3659b94acceSBarry Smith 3669b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3679b94acceSBarry Smith @*/ 36863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP) 3699b94acceSBarry Smith { 3703a40ed3dSBarry Smith PetscFunctionBegin; 3714482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3729b94acceSBarry Smith *usrP = snes->user; 3733a40ed3dSBarry Smith PetscFunctionReturn(0); 3749b94acceSBarry Smith } 37574679c65SBarry Smith 3764a2ae208SSatish Balay #undef __FUNCT__ 3774a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 3789b94acceSBarry Smith /*@ 379c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 380c8228a4eSBarry Smith at this time. 3819b94acceSBarry Smith 382c7afd0dbSLois Curfman McInnes Not Collective 383c7afd0dbSLois Curfman McInnes 3849b94acceSBarry Smith Input Parameter: 3859b94acceSBarry Smith . snes - SNES context 3869b94acceSBarry Smith 3879b94acceSBarry Smith Output Parameter: 3889b94acceSBarry Smith . iter - iteration number 3899b94acceSBarry Smith 390c8228a4eSBarry Smith Notes: 391c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 392c8228a4eSBarry Smith 393c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 39408405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 39508405cd6SLois Curfman McInnes .vb 39608405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 39708405cd6SLois Curfman McInnes if (!(it % 2)) { 39808405cd6SLois Curfman McInnes [compute Jacobian here] 39908405cd6SLois Curfman McInnes } 40008405cd6SLois Curfman McInnes .ve 401c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 40208405cd6SLois Curfman McInnes recomputed every second SNES iteration. 403c8228a4eSBarry Smith 40436851e7fSLois Curfman McInnes Level: intermediate 40536851e7fSLois Curfman McInnes 4062b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 4072b668275SBarry Smith 408b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 4099b94acceSBarry Smith @*/ 41063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter) 4119b94acceSBarry Smith { 4123a40ed3dSBarry Smith PetscFunctionBegin; 4134482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4144482741eSBarry Smith PetscValidIntPointer(iter,2); 4159b94acceSBarry Smith *iter = snes->iter; 4163a40ed3dSBarry Smith PetscFunctionReturn(0); 4179b94acceSBarry Smith } 41874679c65SBarry Smith 4194a2ae208SSatish Balay #undef __FUNCT__ 4204a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 4219b94acceSBarry Smith /*@ 4229b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4239b94acceSBarry Smith with SNESSSetFunction(). 4249b94acceSBarry Smith 425c7afd0dbSLois Curfman McInnes Collective on SNES 426c7afd0dbSLois Curfman McInnes 4279b94acceSBarry Smith Input Parameter: 4289b94acceSBarry Smith . snes - SNES context 4299b94acceSBarry Smith 4309b94acceSBarry Smith Output Parameter: 4319b94acceSBarry Smith . fnorm - 2-norm of function 4329b94acceSBarry Smith 43336851e7fSLois Curfman McInnes Level: intermediate 43436851e7fSLois Curfman McInnes 4359b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 436a86d99e1SLois Curfman McInnes 437b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 4389b94acceSBarry Smith @*/ 43971f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 4409b94acceSBarry Smith { 4413a40ed3dSBarry Smith PetscFunctionBegin; 4424482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4434482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 4449b94acceSBarry Smith *fnorm = snes->norm; 4453a40ed3dSBarry Smith PetscFunctionReturn(0); 4469b94acceSBarry Smith } 44774679c65SBarry Smith 4484a2ae208SSatish Balay #undef __FUNCT__ 449b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 4509b94acceSBarry Smith /*@ 451b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 4529b94acceSBarry Smith attempted by the nonlinear solver. 4539b94acceSBarry Smith 454c7afd0dbSLois Curfman McInnes Not Collective 455c7afd0dbSLois Curfman McInnes 4569b94acceSBarry Smith Input Parameter: 4579b94acceSBarry Smith . snes - SNES context 4589b94acceSBarry Smith 4599b94acceSBarry Smith Output Parameter: 4609b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 4619b94acceSBarry Smith 462c96a6f78SLois Curfman McInnes Notes: 463c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 464c96a6f78SLois Curfman McInnes 46536851e7fSLois Curfman McInnes Level: intermediate 46636851e7fSLois Curfman McInnes 4679b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 46858ebbce7SBarry Smith 46958ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 47058ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 4719b94acceSBarry Smith @*/ 472b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 4739b94acceSBarry Smith { 4743a40ed3dSBarry Smith PetscFunctionBegin; 4754482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4764482741eSBarry Smith PetscValidIntPointer(nfails,2); 47750ffb88aSMatthew Knepley *nfails = snes->numFailures; 47850ffb88aSMatthew Knepley PetscFunctionReturn(0); 47950ffb88aSMatthew Knepley } 48050ffb88aSMatthew Knepley 48150ffb88aSMatthew Knepley #undef __FUNCT__ 482b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 48350ffb88aSMatthew Knepley /*@ 484b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 48550ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 48650ffb88aSMatthew Knepley 48750ffb88aSMatthew Knepley Not Collective 48850ffb88aSMatthew Knepley 48950ffb88aSMatthew Knepley Input Parameters: 49050ffb88aSMatthew Knepley + snes - SNES context 49150ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 49250ffb88aSMatthew Knepley 49350ffb88aSMatthew Knepley Level: intermediate 49450ffb88aSMatthew Knepley 49550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 49658ebbce7SBarry Smith 49758ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 49858ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 49950ffb88aSMatthew Knepley @*/ 500b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 50150ffb88aSMatthew Knepley { 50250ffb88aSMatthew Knepley PetscFunctionBegin; 5034482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 50450ffb88aSMatthew Knepley snes->maxFailures = maxFails; 50550ffb88aSMatthew Knepley PetscFunctionReturn(0); 50650ffb88aSMatthew Knepley } 50750ffb88aSMatthew Knepley 50850ffb88aSMatthew Knepley #undef __FUNCT__ 509b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 51050ffb88aSMatthew Knepley /*@ 511b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 51250ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 51350ffb88aSMatthew Knepley 51450ffb88aSMatthew Knepley Not Collective 51550ffb88aSMatthew Knepley 51650ffb88aSMatthew Knepley Input Parameter: 51750ffb88aSMatthew Knepley . snes - SNES context 51850ffb88aSMatthew Knepley 51950ffb88aSMatthew Knepley Output Parameter: 52050ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 52150ffb88aSMatthew Knepley 52250ffb88aSMatthew Knepley Level: intermediate 52350ffb88aSMatthew Knepley 52450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 52558ebbce7SBarry Smith 52658ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 52758ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 52858ebbce7SBarry Smith 52950ffb88aSMatthew Knepley @*/ 530b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 53150ffb88aSMatthew Knepley { 53250ffb88aSMatthew Knepley PetscFunctionBegin; 5334482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5344482741eSBarry Smith PetscValidIntPointer(maxFails,2); 53550ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 5363a40ed3dSBarry Smith PetscFunctionReturn(0); 5379b94acceSBarry Smith } 538a847f771SSatish Balay 5394a2ae208SSatish Balay #undef __FUNCT__ 5402541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 5412541af92SBarry Smith /*@ 5422541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 5432541af92SBarry Smith done by SNES. 5442541af92SBarry Smith 5452541af92SBarry Smith Not Collective 5462541af92SBarry Smith 5472541af92SBarry Smith Input Parameter: 5482541af92SBarry Smith . snes - SNES context 5492541af92SBarry Smith 5502541af92SBarry Smith Output Parameter: 5512541af92SBarry Smith . nfuncs - number of evaluations 5522541af92SBarry Smith 5532541af92SBarry Smith Level: intermediate 5542541af92SBarry Smith 5552541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 55658ebbce7SBarry Smith 55758ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 5582541af92SBarry Smith @*/ 5592541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 5602541af92SBarry Smith { 5612541af92SBarry Smith PetscFunctionBegin; 5622541af92SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5632541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 5642541af92SBarry Smith *nfuncs = snes->nfuncs; 5652541af92SBarry Smith PetscFunctionReturn(0); 5662541af92SBarry Smith } 5672541af92SBarry Smith 5682541af92SBarry Smith #undef __FUNCT__ 5693d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 5703d4c4710SBarry Smith /*@ 5713d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 5723d4c4710SBarry Smith linear solvers. 5733d4c4710SBarry Smith 5743d4c4710SBarry Smith Not Collective 5753d4c4710SBarry Smith 5763d4c4710SBarry Smith Input Parameter: 5773d4c4710SBarry Smith . snes - SNES context 5783d4c4710SBarry Smith 5793d4c4710SBarry Smith Output Parameter: 5803d4c4710SBarry Smith . nfails - number of failed solves 5813d4c4710SBarry Smith 5823d4c4710SBarry Smith Notes: 5833d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 5843d4c4710SBarry Smith 5853d4c4710SBarry Smith Level: intermediate 5863d4c4710SBarry Smith 5873d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 58858ebbce7SBarry Smith 58958ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures() 5903d4c4710SBarry Smith @*/ 5913d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 5923d4c4710SBarry Smith { 5933d4c4710SBarry Smith PetscFunctionBegin; 5943d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5953d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 5963d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 5973d4c4710SBarry Smith PetscFunctionReturn(0); 5983d4c4710SBarry Smith } 5993d4c4710SBarry Smith 6003d4c4710SBarry Smith #undef __FUNCT__ 6013d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 6023d4c4710SBarry Smith /*@ 6033d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 6043d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 6053d4c4710SBarry Smith 6063d4c4710SBarry Smith Collective on SNES 6073d4c4710SBarry Smith 6083d4c4710SBarry Smith Input Parameters: 6093d4c4710SBarry Smith + snes - SNES context 6103d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 6113d4c4710SBarry Smith 6123d4c4710SBarry Smith Level: intermediate 6133d4c4710SBarry Smith 6143d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 6153d4c4710SBarry Smith 6163d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 6173d4c4710SBarry Smith 61858ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 6193d4c4710SBarry Smith @*/ 6203d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 6213d4c4710SBarry Smith { 6223d4c4710SBarry Smith PetscFunctionBegin; 6233d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6243d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 6253d4c4710SBarry Smith PetscFunctionReturn(0); 6263d4c4710SBarry Smith } 6273d4c4710SBarry Smith 6283d4c4710SBarry Smith #undef __FUNCT__ 6293d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 6303d4c4710SBarry Smith /*@ 6313d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 6323d4c4710SBarry Smith are allowed before SNES terminates 6333d4c4710SBarry Smith 6343d4c4710SBarry Smith Not Collective 6353d4c4710SBarry Smith 6363d4c4710SBarry Smith Input Parameter: 6373d4c4710SBarry Smith . snes - SNES context 6383d4c4710SBarry Smith 6393d4c4710SBarry Smith Output Parameter: 6403d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 6413d4c4710SBarry Smith 6423d4c4710SBarry Smith Level: intermediate 6433d4c4710SBarry Smith 6443d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 6453d4c4710SBarry Smith 6463d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 6473d4c4710SBarry Smith 64858ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), 6493d4c4710SBarry Smith @*/ 6503d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 6513d4c4710SBarry Smith { 6523d4c4710SBarry Smith PetscFunctionBegin; 6533d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6543d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 6553d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 6563d4c4710SBarry Smith PetscFunctionReturn(0); 6573d4c4710SBarry Smith } 6583d4c4710SBarry Smith 6593d4c4710SBarry Smith #undef __FUNCT__ 660b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 661c96a6f78SLois Curfman McInnes /*@ 662b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 663c96a6f78SLois Curfman McInnes used by the nonlinear solver. 664c96a6f78SLois Curfman McInnes 665c7afd0dbSLois Curfman McInnes Not Collective 666c7afd0dbSLois Curfman McInnes 667c96a6f78SLois Curfman McInnes Input Parameter: 668c96a6f78SLois Curfman McInnes . snes - SNES context 669c96a6f78SLois Curfman McInnes 670c96a6f78SLois Curfman McInnes Output Parameter: 671c96a6f78SLois Curfman McInnes . lits - number of linear iterations 672c96a6f78SLois Curfman McInnes 673c96a6f78SLois Curfman McInnes Notes: 674c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 675c96a6f78SLois Curfman McInnes 67636851e7fSLois Curfman McInnes Level: intermediate 67736851e7fSLois Curfman McInnes 678c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 6792b668275SBarry Smith 68058ebbce7SBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm()S, NESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 681c96a6f78SLois Curfman McInnes @*/ 682b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 683c96a6f78SLois Curfman McInnes { 6843a40ed3dSBarry Smith PetscFunctionBegin; 6854482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6864482741eSBarry Smith PetscValidIntPointer(lits,2); 687c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 6883a40ed3dSBarry Smith PetscFunctionReturn(0); 689c96a6f78SLois Curfman McInnes } 690c96a6f78SLois Curfman McInnes 6914a2ae208SSatish Balay #undef __FUNCT__ 69294b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 69352baeb72SSatish Balay /*@ 69494b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 6959b94acceSBarry Smith 69694b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 697c7afd0dbSLois Curfman McInnes 6989b94acceSBarry Smith Input Parameter: 6999b94acceSBarry Smith . snes - the SNES context 7009b94acceSBarry Smith 7019b94acceSBarry Smith Output Parameter: 70294b7f48cSBarry Smith . ksp - the KSP context 7039b94acceSBarry Smith 7049b94acceSBarry Smith Notes: 70594b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 7069b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 7072999313aSBarry Smith PC contexts as well. 7089b94acceSBarry Smith 70936851e7fSLois Curfman McInnes Level: beginner 71036851e7fSLois Curfman McInnes 71194b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7129b94acceSBarry Smith 7132999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7149b94acceSBarry Smith @*/ 71563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp) 7169b94acceSBarry Smith { 7173a40ed3dSBarry Smith PetscFunctionBegin; 7184482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7194482741eSBarry Smith PetscValidPointer(ksp,2); 72094b7f48cSBarry Smith *ksp = snes->ksp; 7213a40ed3dSBarry Smith PetscFunctionReturn(0); 7229b94acceSBarry Smith } 72382bf6240SBarry Smith 7244a2ae208SSatish Balay #undef __FUNCT__ 7252999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 7262999313aSBarry Smith /*@ 7272999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 7282999313aSBarry Smith 7292999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 7302999313aSBarry Smith 7312999313aSBarry Smith Input Parameters: 7322999313aSBarry Smith + snes - the SNES context 7332999313aSBarry Smith - ksp - the KSP context 7342999313aSBarry Smith 7352999313aSBarry Smith Notes: 7362999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 7372999313aSBarry Smith so this routine is rarely needed. 7382999313aSBarry Smith 7392999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 7402999313aSBarry Smith decreased by one. 7412999313aSBarry Smith 7422999313aSBarry Smith Level: developer 7432999313aSBarry Smith 7442999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7452999313aSBarry Smith 7462999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7472999313aSBarry Smith @*/ 7482999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp) 7492999313aSBarry Smith { 7502999313aSBarry Smith PetscErrorCode ierr; 7512999313aSBarry Smith 7522999313aSBarry Smith PetscFunctionBegin; 7532999313aSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7542999313aSBarry Smith PetscValidHeaderSpecific(ksp,KSP_COOKIE,2); 7552999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 7567dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 757906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 7582999313aSBarry Smith snes->ksp = ksp; 7592999313aSBarry Smith PetscFunctionReturn(0); 7602999313aSBarry Smith } 7612999313aSBarry Smith 7627adad957SLisandro Dalcin #if 0 7632999313aSBarry Smith #undef __FUNCT__ 7644a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 7656849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 766e24b481bSBarry Smith { 767e24b481bSBarry Smith PetscFunctionBegin; 768e24b481bSBarry Smith PetscFunctionReturn(0); 769e24b481bSBarry Smith } 7707adad957SLisandro Dalcin #endif 771e24b481bSBarry Smith 7729b94acceSBarry Smith /* -----------------------------------------------------------*/ 7734a2ae208SSatish Balay #undef __FUNCT__ 7744a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 77552baeb72SSatish Balay /*@ 7769b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 7779b94acceSBarry Smith 778c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 779c7afd0dbSLois Curfman McInnes 780c7afd0dbSLois Curfman McInnes Input Parameters: 781906ed7ccSBarry Smith . comm - MPI communicator 7829b94acceSBarry Smith 7839b94acceSBarry Smith Output Parameter: 7849b94acceSBarry Smith . outsnes - the new SNES context 7859b94acceSBarry Smith 786c7afd0dbSLois Curfman McInnes Options Database Keys: 787c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 788c7afd0dbSLois Curfman McInnes and no preconditioning matrix 789c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 790c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 791c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 792c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 793c1f60f51SBarry Smith 79436851e7fSLois Curfman McInnes Level: beginner 79536851e7fSLois Curfman McInnes 7969b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 7979b94acceSBarry Smith 7984b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES 7999b94acceSBarry Smith @*/ 80063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes) 8019b94acceSBarry Smith { 802dfbe8321SBarry Smith PetscErrorCode ierr; 8039b94acceSBarry Smith SNES snes; 804fa9f3622SBarry Smith SNESKSPEW *kctx; 80537fcc0dbSBarry Smith 8063a40ed3dSBarry Smith PetscFunctionBegin; 807ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 8088ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 8098ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 8108ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 8118ba1e511SMatthew Knepley #endif 8128ba1e511SMatthew Knepley 813e7788613SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 8147adad957SLisandro Dalcin 81585385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 8169b94acceSBarry Smith snes->max_its = 50; 8179750a799SBarry Smith snes->max_funcs = 10000; 8189b94acceSBarry Smith snes->norm = 0.0; 819b4874afaSBarry Smith snes->rtol = 1.e-8; 820b4874afaSBarry Smith snes->ttol = 0.0; 82170441072SBarry Smith snes->abstol = 1.e-50; 8229b94acceSBarry Smith snes->xtol = 1.e-8; 8234b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 8249b94acceSBarry Smith snes->nfuncs = 0; 82550ffb88aSMatthew Knepley snes->numFailures = 0; 82650ffb88aSMatthew Knepley snes->maxFailures = 1; 8277a00f4a9SLois Curfman McInnes snes->linear_its = 0; 828639f9d9dSBarry Smith snes->numbermonitors = 0; 8299b94acceSBarry Smith snes->data = 0; 8304dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 831186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 8326f24a144SLois Curfman McInnes snes->vwork = 0; 8336f24a144SLois Curfman McInnes snes->nwork = 0; 834758f92a0SBarry Smith snes->conv_hist_len = 0; 835758f92a0SBarry Smith snes->conv_hist_max = 0; 836758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 837758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 838758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 839184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 8409b94acceSBarry Smith 8413d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 8423d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 8433d4c4710SBarry Smith 8449b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 84538f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 8469b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 8479b94acceSBarry Smith kctx->version = 2; 8489b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 8499b94acceSBarry Smith this was too large for some test cases */ 8509b94acceSBarry Smith kctx->rtol_last = 0; 8519b94acceSBarry Smith kctx->rtol_max = .9; 8529b94acceSBarry Smith kctx->gamma = 1.0; 85371f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 85471f87433Sdalcinl kctx->alpha2 = kctx->alpha; 8559b94acceSBarry Smith kctx->threshold = .1; 8569b94acceSBarry Smith kctx->lresid_last = 0; 8579b94acceSBarry Smith kctx->norm_last = 0; 8589b94acceSBarry Smith 85994b7f48cSBarry Smith ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr); 86052e6d16bSBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 8615334005bSBarry Smith 8629b94acceSBarry Smith *outsnes = snes; 86300036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 8643a40ed3dSBarry Smith PetscFunctionReturn(0); 8659b94acceSBarry Smith } 8669b94acceSBarry Smith 8674a2ae208SSatish Balay #undef __FUNCT__ 8684a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 8699b94acceSBarry Smith /*@C 8709b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 8719b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 8729b94acceSBarry Smith equations. 8739b94acceSBarry Smith 874fee21e36SBarry Smith Collective on SNES 875fee21e36SBarry Smith 876c7afd0dbSLois Curfman McInnes Input Parameters: 877c7afd0dbSLois Curfman McInnes + snes - the SNES context 878c7afd0dbSLois Curfman McInnes . r - vector to store function value 879de044059SHong Zhang . func - function evaluation routine 880c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 881c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8829b94acceSBarry Smith 883c7afd0dbSLois Curfman McInnes Calling sequence of func: 8848d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 885c7afd0dbSLois Curfman McInnes 886313e4042SLois Curfman McInnes . f - function vector 887c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 8889b94acceSBarry Smith 8899b94acceSBarry Smith Notes: 8909b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 8919b94acceSBarry Smith $ f'(x) x = -f(x), 892c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 8939b94acceSBarry Smith 89436851e7fSLois Curfman McInnes Level: beginner 89536851e7fSLois Curfman McInnes 8969b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 8979b94acceSBarry Smith 898a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 8999b94acceSBarry Smith @*/ 90063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 9019b94acceSBarry Smith { 90285385478SLisandro Dalcin PetscErrorCode ierr; 9033a40ed3dSBarry Smith PetscFunctionBegin; 9044482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9054482741eSBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE,2); 906c9780b6fSBarry Smith PetscCheckSameComm(snes,1,r,2); 90785385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 90885385478SLisandro Dalcin if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); } 909e7788613SBarry Smith snes->ops->computefunction = func; 91085385478SLisandro Dalcin snes->vec_func = r; 9119b94acceSBarry Smith snes->funP = ctx; 9123a40ed3dSBarry Smith PetscFunctionReturn(0); 9139b94acceSBarry Smith } 9149b94acceSBarry Smith 9153ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 9163ab0aad5SBarry Smith #undef __FUNCT__ 9171096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 9181096aae1SMatthew Knepley /*@C 9191096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 9201096aae1SMatthew Knepley it assumes a zero right hand side. 9211096aae1SMatthew Knepley 9221096aae1SMatthew Knepley Collective on SNES 9231096aae1SMatthew Knepley 9241096aae1SMatthew Knepley Input Parameter: 9251096aae1SMatthew Knepley . snes - the SNES context 9261096aae1SMatthew Knepley 9271096aae1SMatthew Knepley Output Parameter: 928bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 9291096aae1SMatthew Knepley 9301096aae1SMatthew Knepley Level: intermediate 9311096aae1SMatthew Knepley 9321096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 9331096aae1SMatthew Knepley 93485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 9351096aae1SMatthew Knepley @*/ 9361096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs) 9371096aae1SMatthew Knepley { 9381096aae1SMatthew Knepley PetscFunctionBegin; 9391096aae1SMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9401096aae1SMatthew Knepley PetscValidPointer(rhs,2); 94185385478SLisandro Dalcin *rhs = snes->vec_rhs; 9421096aae1SMatthew Knepley PetscFunctionReturn(0); 9431096aae1SMatthew Knepley } 9441096aae1SMatthew Knepley 9451096aae1SMatthew Knepley #undef __FUNCT__ 9464a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 9479b94acceSBarry Smith /*@ 94836851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 9499b94acceSBarry Smith SNESSetFunction(). 9509b94acceSBarry Smith 951c7afd0dbSLois Curfman McInnes Collective on SNES 952c7afd0dbSLois Curfman McInnes 9539b94acceSBarry Smith Input Parameters: 954c7afd0dbSLois Curfman McInnes + snes - the SNES context 955c7afd0dbSLois Curfman McInnes - x - input vector 9569b94acceSBarry Smith 9579b94acceSBarry Smith Output Parameter: 9583638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 9599b94acceSBarry Smith 9601bffabb2SLois Curfman McInnes Notes: 96136851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 96236851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 96336851e7fSLois Curfman McInnes themselves. 96436851e7fSLois Curfman McInnes 96536851e7fSLois Curfman McInnes Level: developer 96636851e7fSLois Curfman McInnes 9679b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 9689b94acceSBarry Smith 969a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 9709b94acceSBarry Smith @*/ 97163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y) 9729b94acceSBarry Smith { 973dfbe8321SBarry Smith PetscErrorCode ierr; 9749b94acceSBarry Smith 9753a40ed3dSBarry Smith PetscFunctionBegin; 9764482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9774482741eSBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE,2); 9784482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,3); 979c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 980c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 981184914b5SBarry Smith 982d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 983e7788613SBarry Smith if (snes->ops->computefunction) { 984d64ed03dSBarry Smith PetscStackPush("SNES user function"); 985e9a2bbcdSBarry Smith CHKMEMQ; 986e7788613SBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP); 987e9a2bbcdSBarry Smith CHKMEMQ; 988d64ed03dSBarry Smith PetscStackPop; 989d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 99019717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr); 99119717074SBarry Smith } 992d5e45103SBarry Smith CHKERRQ(ierr); 99385385478SLisandro Dalcin } else if (snes->vec_rhs) { 9941096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 9951096aae1SMatthew Knepley } else { 9961096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 9971096aae1SMatthew Knepley } 99885385478SLisandro Dalcin if (snes->vec_rhs) { 99985385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 10003ab0aad5SBarry Smith } 1001ae3c334cSLois Curfman McInnes snes->nfuncs++; 1002d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 10033a40ed3dSBarry Smith PetscFunctionReturn(0); 10049b94acceSBarry Smith } 10059b94acceSBarry Smith 10064a2ae208SSatish Balay #undef __FUNCT__ 10074a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 100862fef451SLois Curfman McInnes /*@ 100962fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 101062fef451SLois Curfman McInnes set with SNESSetJacobian(). 101162fef451SLois Curfman McInnes 1012c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1013c7afd0dbSLois Curfman McInnes 101462fef451SLois Curfman McInnes Input Parameters: 1015c7afd0dbSLois Curfman McInnes + snes - the SNES context 1016c7afd0dbSLois Curfman McInnes - x - input vector 101762fef451SLois Curfman McInnes 101862fef451SLois Curfman McInnes Output Parameters: 1019c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 102062fef451SLois Curfman McInnes . B - optional preconditioning matrix 10212b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1022fee21e36SBarry Smith 102362fef451SLois Curfman McInnes Notes: 102462fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 102562fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 102662fef451SLois Curfman McInnes 102794b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1028dc5a77f8SLois Curfman McInnes flag parameter. 102962fef451SLois Curfman McInnes 103036851e7fSLois Curfman McInnes Level: developer 103136851e7fSLois Curfman McInnes 103262fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 103362fef451SLois Curfman McInnes 10342b668275SBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure 103562fef451SLois Curfman McInnes @*/ 103663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10379b94acceSBarry Smith { 1038dfbe8321SBarry Smith PetscErrorCode ierr; 10393a40ed3dSBarry Smith 10403a40ed3dSBarry Smith PetscFunctionBegin; 10414482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10424482741eSBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE,2); 10434482741eSBarry Smith PetscValidPointer(flg,5); 1044c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1045e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1046d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1047c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1048d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1049dc67937bSBarry Smith CHKMEMQ; 1050e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1051dc67937bSBarry Smith CHKMEMQ; 1052d64ed03dSBarry Smith PetscStackPop; 1053d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 10546d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 10556ce558aeSBarry Smith /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3); 10566ce558aeSBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE,4); */ 10573a40ed3dSBarry Smith PetscFunctionReturn(0); 10589b94acceSBarry Smith } 10599b94acceSBarry Smith 10604a2ae208SSatish Balay #undef __FUNCT__ 10614a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 10629b94acceSBarry Smith /*@C 10639b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1064044dda88SLois Curfman McInnes location to store the matrix. 10659b94acceSBarry Smith 1066c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1067c7afd0dbSLois Curfman McInnes 10689b94acceSBarry Smith Input Parameters: 1069c7afd0dbSLois Curfman McInnes + snes - the SNES context 10709b94acceSBarry Smith . A - Jacobian matrix 10719b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 10729b94acceSBarry Smith . func - Jacobian evaluation routine 1073c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 10742cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 10759b94acceSBarry Smith 10769b94acceSBarry Smith Calling sequence of func: 10778d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 10789b94acceSBarry Smith 1079c7afd0dbSLois Curfman McInnes + x - input vector 10809b94acceSBarry Smith . A - Jacobian matrix 10819b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1082ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 10832b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1084c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 10859b94acceSBarry Smith 10869b94acceSBarry Smith Notes: 108794b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 10882cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1089ac21db08SLois Curfman McInnes 1090ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 10919b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 10929b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 10939b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 10949b94acceSBarry Smith throughout the global iterations. 10959b94acceSBarry Smith 109636851e7fSLois Curfman McInnes Level: beginner 109736851e7fSLois Curfman McInnes 10989b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 10999b94acceSBarry Smith 11003ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 11019b94acceSBarry Smith @*/ 110263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 11039b94acceSBarry Smith { 1104dfbe8321SBarry Smith PetscErrorCode ierr; 11053a7fca6bSBarry Smith 11063a40ed3dSBarry Smith PetscFunctionBegin; 11074482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11084482741eSBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2); 11094482741eSBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3); 1110c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 1111c9780b6fSBarry Smith if (B) PetscCheckSameComm(snes,1,B,2); 1112e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 11133a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 11143a7fca6bSBarry Smith if (A) { 11157dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 11163a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 11179b94acceSBarry Smith snes->jacobian = A; 11183a7fca6bSBarry Smith } 11193a7fca6bSBarry Smith if (B) { 11207dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 11213a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 11229b94acceSBarry Smith snes->jacobian_pre = B; 11233a7fca6bSBarry Smith } 11243a40ed3dSBarry Smith PetscFunctionReturn(0); 11259b94acceSBarry Smith } 112662fef451SLois Curfman McInnes 11274a2ae208SSatish Balay #undef __FUNCT__ 11284a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1129c2aafc4cSSatish Balay /*@C 1130b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1131b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1132b4fd4287SBarry Smith 1133c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1134c7afd0dbSLois Curfman McInnes 1135b4fd4287SBarry Smith Input Parameter: 1136b4fd4287SBarry Smith . snes - the nonlinear solver context 1137b4fd4287SBarry Smith 1138b4fd4287SBarry Smith Output Parameters: 1139c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1140b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 114170e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 114270e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1143fee21e36SBarry Smith 114436851e7fSLois Curfman McInnes Level: advanced 114536851e7fSLois Curfman McInnes 1146b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1147b4fd4287SBarry Smith @*/ 114870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1149b4fd4287SBarry Smith { 11503a40ed3dSBarry Smith PetscFunctionBegin; 11514482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1152b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1153b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1154e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 115570e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 11563a40ed3dSBarry Smith PetscFunctionReturn(0); 1157b4fd4287SBarry Smith } 1158b4fd4287SBarry Smith 11599b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 116063dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 11619b94acceSBarry Smith 11624a2ae208SSatish Balay #undef __FUNCT__ 11634a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 11649b94acceSBarry Smith /*@ 11659b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1166272ac6f2SLois Curfman McInnes of a nonlinear solver. 11679b94acceSBarry Smith 1168fee21e36SBarry Smith Collective on SNES 1169fee21e36SBarry Smith 1170c7afd0dbSLois Curfman McInnes Input Parameters: 117170e92668SMatthew Knepley . snes - the SNES context 1172c7afd0dbSLois Curfman McInnes 1173272ac6f2SLois Curfman McInnes Notes: 1174272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1175272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1176272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1177272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1178272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1179272ac6f2SLois Curfman McInnes 118036851e7fSLois Curfman McInnes Level: advanced 118136851e7fSLois Curfman McInnes 11829b94acceSBarry Smith .keywords: SNES, nonlinear, setup 11839b94acceSBarry Smith 11849b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 11859b94acceSBarry Smith @*/ 118670e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes) 11879b94acceSBarry Smith { 1188dfbe8321SBarry Smith PetscErrorCode ierr; 118971f87433Sdalcinl PetscTruth flg; 11903a40ed3dSBarry Smith 11913a40ed3dSBarry Smith PetscFunctionBegin; 11924482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11934dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 11949b94acceSBarry Smith 11957adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 119685385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 119785385478SLisandro Dalcin } 119885385478SLisandro Dalcin 11997adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1200c1f60f51SBarry Smith /* 1201c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1202dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1203c1f60f51SBarry Smith */ 1204c1f60f51SBarry Smith if (flg) { 1205c1f60f51SBarry Smith Mat J; 1206fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12073ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 1208ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12093a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 12103a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1211c1f60f51SBarry Smith } 121245fc7adcSBarry Smith 121303c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT) 12147adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr); 121545fc7adcSBarry Smith if (flg) { 121645fc7adcSBarry Smith Mat J; 121745fc7adcSBarry Smith ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr); 121875396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr); 121945fc7adcSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 122045fc7adcSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 122145fc7adcSBarry Smith } 122232a4b47aSMatthew Knepley #endif 122345fc7adcSBarry Smith 12247adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1225c1f60f51SBarry Smith /* 1226dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1227c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1228c1f60f51SBarry Smith */ 122931615d04SLois Curfman McInnes if (flg) { 1230272ac6f2SLois Curfman McInnes Mat J; 1231b5d62d44SBarry Smith KSP ksp; 123294b7f48cSBarry Smith PC pc; 123375396ef9SLisandro Dalcin /* create and set matrix-free operator */ 1234fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12353ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 123675396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12373ec795f1SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 12383a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1239f3ef73ceSBarry Smith /* force no preconditioner */ 124094b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 1241b5d62d44SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 1242a9815358SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 1243a9815358SBarry Smith if (!flg) { 124475396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 1245f3ef73ceSBarry Smith ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 1246272ac6f2SLois Curfman McInnes } 1247a9815358SBarry Smith } 1248f3ef73ceSBarry Smith 124985385478SLisandro Dalcin if (!snes->vec_func && !snes->vec_rhs) { 12501096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12511096aae1SMatthew Knepley } 125285385478SLisandro Dalcin if (!snes->ops->computefunction && !snes->vec_rhs) { 12531096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12541096aae1SMatthew Knepley } 125575396ef9SLisandro Dalcin if (!snes->jacobian) { 125675396ef9SLisandro Dalcin SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 125775396ef9SLisandro Dalcin } 1258a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 125929bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1260a8c6a408SBarry Smith } 1261a703fe33SLois Curfman McInnes 1262410397dcSLisandro Dalcin if (snes->ops->setup) { 1263410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1264410397dcSLisandro Dalcin } 12657aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 12663a40ed3dSBarry Smith PetscFunctionReturn(0); 12679b94acceSBarry Smith } 12689b94acceSBarry Smith 12694a2ae208SSatish Balay #undef __FUNCT__ 12704a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 127152baeb72SSatish Balay /*@ 12729b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 12739b94acceSBarry Smith with SNESCreate(). 12749b94acceSBarry Smith 1275c7afd0dbSLois Curfman McInnes Collective on SNES 1276c7afd0dbSLois Curfman McInnes 12779b94acceSBarry Smith Input Parameter: 12789b94acceSBarry Smith . snes - the SNES context 12799b94acceSBarry Smith 128036851e7fSLois Curfman McInnes Level: beginner 128136851e7fSLois Curfman McInnes 12829b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 12839b94acceSBarry Smith 128463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 12859b94acceSBarry Smith @*/ 128663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes) 12879b94acceSBarry Smith { 12886849ba73SBarry Smith PetscErrorCode ierr; 12893a40ed3dSBarry Smith 12903a40ed3dSBarry Smith PetscFunctionBegin; 12914482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 12927adad957SLisandro Dalcin if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0); 1293d4bb536fSBarry Smith 1294be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 12950f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1296e7788613SBarry Smith if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);} 129785385478SLisandro Dalcin 129885385478SLisandro Dalcin if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);} 129985385478SLisandro Dalcin if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);} 130085385478SLisandro Dalcin if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);} 13013a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 13023a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 130394b7f48cSBarry Smith ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr); 130485385478SLisandro Dalcin ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr); 1305522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1306a6570f20SBarry Smith ierr = SNESMonitorCancel(snes);CHKERRQ(ierr); 1307a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 13083a40ed3dSBarry Smith PetscFunctionReturn(0); 13099b94acceSBarry Smith } 13109b94acceSBarry Smith 13119b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 13129b94acceSBarry Smith 13134a2ae208SSatish Balay #undef __FUNCT__ 13144a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 13159b94acceSBarry Smith /*@ 1316d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 13179b94acceSBarry Smith 1318c7afd0dbSLois Curfman McInnes Collective on SNES 1319c7afd0dbSLois Curfman McInnes 13209b94acceSBarry Smith Input Parameters: 1321c7afd0dbSLois Curfman McInnes + snes - the SNES context 132270441072SBarry Smith . abstol - absolute convergence tolerance 132333174efeSLois Curfman McInnes . rtol - relative convergence tolerance 132433174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 132533174efeSLois Curfman McInnes of the change in the solution between steps 132633174efeSLois Curfman McInnes . maxit - maximum number of iterations 1327c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1328fee21e36SBarry Smith 132933174efeSLois Curfman McInnes Options Database Keys: 133070441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1331c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1332c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1333c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1334c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 13359b94acceSBarry Smith 1336d7a720efSLois Curfman McInnes Notes: 13379b94acceSBarry Smith The default maximum number of iterations is 50. 13389b94acceSBarry Smith The default maximum number of function evaluations is 1000. 13399b94acceSBarry Smith 134036851e7fSLois Curfman McInnes Level: intermediate 134136851e7fSLois Curfman McInnes 134233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 13439b94acceSBarry Smith 13442492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 13459b94acceSBarry Smith @*/ 134663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 13479b94acceSBarry Smith { 13483a40ed3dSBarry Smith PetscFunctionBegin; 13494482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 135070441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1351d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1352d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1353d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1354d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 13553a40ed3dSBarry Smith PetscFunctionReturn(0); 13569b94acceSBarry Smith } 13579b94acceSBarry Smith 13584a2ae208SSatish Balay #undef __FUNCT__ 13594a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 13609b94acceSBarry Smith /*@ 136133174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 136233174efeSLois Curfman McInnes 1363c7afd0dbSLois Curfman McInnes Not Collective 1364c7afd0dbSLois Curfman McInnes 136533174efeSLois Curfman McInnes Input Parameters: 1366c7afd0dbSLois Curfman McInnes + snes - the SNES context 136785385478SLisandro Dalcin . atol - absolute convergence tolerance 136833174efeSLois Curfman McInnes . rtol - relative convergence tolerance 136933174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 137033174efeSLois Curfman McInnes of the change in the solution between steps 137133174efeSLois Curfman McInnes . maxit - maximum number of iterations 1372c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1373fee21e36SBarry Smith 137433174efeSLois Curfman McInnes Notes: 137533174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 137633174efeSLois Curfman McInnes 137736851e7fSLois Curfman McInnes Level: intermediate 137836851e7fSLois Curfman McInnes 137933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 138033174efeSLois Curfman McInnes 138133174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 138233174efeSLois Curfman McInnes @*/ 138385385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 138433174efeSLois Curfman McInnes { 13853a40ed3dSBarry Smith PetscFunctionBegin; 13864482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 138785385478SLisandro Dalcin if (atol) *atol = snes->abstol; 138833174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 138933174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 139033174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 139133174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 13923a40ed3dSBarry Smith PetscFunctionReturn(0); 139333174efeSLois Curfman McInnes } 139433174efeSLois Curfman McInnes 13954a2ae208SSatish Balay #undef __FUNCT__ 13964a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 139733174efeSLois Curfman McInnes /*@ 13989b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 13999b94acceSBarry Smith 1400fee21e36SBarry Smith Collective on SNES 1401fee21e36SBarry Smith 1402c7afd0dbSLois Curfman McInnes Input Parameters: 1403c7afd0dbSLois Curfman McInnes + snes - the SNES context 1404c7afd0dbSLois Curfman McInnes - tol - tolerance 1405c7afd0dbSLois Curfman McInnes 14069b94acceSBarry Smith Options Database Key: 1407c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 14089b94acceSBarry Smith 140936851e7fSLois Curfman McInnes Level: intermediate 141036851e7fSLois Curfman McInnes 14119b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 14129b94acceSBarry Smith 14132492ecdbSBarry Smith .seealso: SNESSetTolerances() 14149b94acceSBarry Smith @*/ 141563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 14169b94acceSBarry Smith { 14173a40ed3dSBarry Smith PetscFunctionBegin; 14184482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 14199b94acceSBarry Smith snes->deltatol = tol; 14203a40ed3dSBarry Smith PetscFunctionReturn(0); 14219b94acceSBarry Smith } 14229b94acceSBarry Smith 1423df9fa365SBarry Smith /* 1424df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1425df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1426df9fa365SBarry Smith macros instead of functions 1427df9fa365SBarry Smith */ 14284a2ae208SSatish Balay #undef __FUNCT__ 1429a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 1430a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1431ce1608b8SBarry Smith { 1432dfbe8321SBarry Smith PetscErrorCode ierr; 1433ce1608b8SBarry Smith 1434ce1608b8SBarry Smith PetscFunctionBegin; 14354482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1436a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1437ce1608b8SBarry Smith PetscFunctionReturn(0); 1438ce1608b8SBarry Smith } 1439ce1608b8SBarry Smith 14404a2ae208SSatish Balay #undef __FUNCT__ 1441a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 1442a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1443df9fa365SBarry Smith { 1444dfbe8321SBarry Smith PetscErrorCode ierr; 1445df9fa365SBarry Smith 1446df9fa365SBarry Smith PetscFunctionBegin; 1447a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1448df9fa365SBarry Smith PetscFunctionReturn(0); 1449df9fa365SBarry Smith } 1450df9fa365SBarry Smith 14514a2ae208SSatish Balay #undef __FUNCT__ 1452a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 1453a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw) 1454df9fa365SBarry Smith { 1455dfbe8321SBarry Smith PetscErrorCode ierr; 1456df9fa365SBarry Smith 1457df9fa365SBarry Smith PetscFunctionBegin; 1458a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1459df9fa365SBarry Smith PetscFunctionReturn(0); 1460df9fa365SBarry Smith } 1461df9fa365SBarry Smith 14629b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 14639b94acceSBarry Smith 14644a2ae208SSatish Balay #undef __FUNCT__ 1465a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 14669b94acceSBarry Smith /*@C 1467a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 14689b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 14699b94acceSBarry Smith progress. 14709b94acceSBarry Smith 1471fee21e36SBarry Smith Collective on SNES 1472fee21e36SBarry Smith 1473c7afd0dbSLois Curfman McInnes Input Parameters: 1474c7afd0dbSLois Curfman McInnes + snes - the SNES context 1475c7afd0dbSLois Curfman McInnes . func - monitoring routine 1476b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1477e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 1478b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1479b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 14809b94acceSBarry Smith 1481c7afd0dbSLois Curfman McInnes Calling sequence of func: 1482a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 1483c7afd0dbSLois Curfman McInnes 1484c7afd0dbSLois Curfman McInnes + snes - the SNES context 1485c7afd0dbSLois Curfman McInnes . its - iteration number 1486c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 148740a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 14889b94acceSBarry Smith 14899665c990SLois Curfman McInnes Options Database Keys: 1490a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 1491a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 1492a6570f20SBarry Smith uses SNESMonitorLGCreate() 1493a6570f20SBarry Smith _ -snes_monitor_cancel - cancels all monitors that have 1494c7afd0dbSLois Curfman McInnes been hardwired into a code by 1495a6570f20SBarry Smith calls to SNESMonitorSet(), but 1496c7afd0dbSLois Curfman McInnes does not cancel those set via 1497c7afd0dbSLois Curfman McInnes the options database. 14989665c990SLois Curfman McInnes 1499639f9d9dSBarry Smith Notes: 15006bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 1501a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 15026bc08f3fSLois Curfman McInnes order in which they were set. 1503639f9d9dSBarry Smith 1504*025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 1505*025f1a04SBarry Smith 150636851e7fSLois Curfman McInnes Level: intermediate 150736851e7fSLois Curfman McInnes 15089b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 15099b94acceSBarry Smith 1510a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 15119b94acceSBarry Smith @*/ 1512b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 15139b94acceSBarry Smith { 1514b90d0a6eSBarry Smith PetscInt i; 1515b90d0a6eSBarry Smith 15163a40ed3dSBarry Smith PetscFunctionBegin; 15174482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1518639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 151929bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1520639f9d9dSBarry Smith } 1521b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 1522b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 1523b90d0a6eSBarry Smith 1524b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 1525b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 1526b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 1527b90d0a6eSBarry Smith PetscErrorCode ierr; 1528b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 1529b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 1530b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 1531b90d0a6eSBarry Smith ierr = (*monitordestroy)(mctx);CHKERRQ(ierr); 1532b90d0a6eSBarry Smith PetscFunctionReturn(0); 1533b90d0a6eSBarry Smith } 1534b90d0a6eSBarry Smith } 1535b90d0a6eSBarry Smith } 1536b90d0a6eSBarry Smith } 1537b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 1538b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1539639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 15403a40ed3dSBarry Smith PetscFunctionReturn(0); 15419b94acceSBarry Smith } 15429b94acceSBarry Smith 15434a2ae208SSatish Balay #undef __FUNCT__ 1544a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 15455cd90555SBarry Smith /*@C 1546a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 15475cd90555SBarry Smith 1548c7afd0dbSLois Curfman McInnes Collective on SNES 1549c7afd0dbSLois Curfman McInnes 15505cd90555SBarry Smith Input Parameters: 15515cd90555SBarry Smith . snes - the SNES context 15525cd90555SBarry Smith 15531a480d89SAdministrator Options Database Key: 1554a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 1555a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 1556c7afd0dbSLois Curfman McInnes set via the options database 15575cd90555SBarry Smith 15585cd90555SBarry Smith Notes: 15595cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 15605cd90555SBarry Smith 156136851e7fSLois Curfman McInnes Level: intermediate 156236851e7fSLois Curfman McInnes 15635cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 15645cd90555SBarry Smith 1565a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 15665cd90555SBarry Smith @*/ 1567a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes) 15685cd90555SBarry Smith { 1569d952e501SBarry Smith PetscErrorCode ierr; 1570d952e501SBarry Smith PetscInt i; 1571d952e501SBarry Smith 15725cd90555SBarry Smith PetscFunctionBegin; 15734482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1574d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1575d952e501SBarry Smith if (snes->monitordestroy[i]) { 1576d952e501SBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1577d952e501SBarry Smith } 1578d952e501SBarry Smith } 15795cd90555SBarry Smith snes->numbermonitors = 0; 15805cd90555SBarry Smith PetscFunctionReturn(0); 15815cd90555SBarry Smith } 15825cd90555SBarry Smith 15834a2ae208SSatish Balay #undef __FUNCT__ 15844a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 15859b94acceSBarry Smith /*@C 15869b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 15879b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 15889b94acceSBarry Smith 1589fee21e36SBarry Smith Collective on SNES 1590fee21e36SBarry Smith 1591c7afd0dbSLois Curfman McInnes Input Parameters: 1592c7afd0dbSLois Curfman McInnes + snes - the SNES context 1593c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1594c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1595c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 15969b94acceSBarry Smith 1597c7afd0dbSLois Curfman McInnes Calling sequence of func: 159806ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1599c7afd0dbSLois Curfman McInnes 1600c7afd0dbSLois Curfman McInnes + snes - the SNES context 160106ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 1602c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1603184914b5SBarry Smith . reason - reason for convergence/divergence 1604c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 16054b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 16064b27c08aSLois Curfman McInnes - f - 2-norm of function 16079b94acceSBarry Smith 160836851e7fSLois Curfman McInnes Level: advanced 160936851e7fSLois Curfman McInnes 16109b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 16119b94acceSBarry Smith 161285385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 16139b94acceSBarry Smith @*/ 161406ee9f85SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx) 16159b94acceSBarry Smith { 16163a40ed3dSBarry Smith PetscFunctionBegin; 16174482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 161885385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 161985385478SLisandro Dalcin snes->ops->converged = func; 162085385478SLisandro Dalcin snes->cnvP = cctx; 16213a40ed3dSBarry Smith PetscFunctionReturn(0); 16229b94acceSBarry Smith } 16239b94acceSBarry Smith 16244a2ae208SSatish Balay #undef __FUNCT__ 16254a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 162652baeb72SSatish Balay /*@ 1627184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1628184914b5SBarry Smith 1629184914b5SBarry Smith Not Collective 1630184914b5SBarry Smith 1631184914b5SBarry Smith Input Parameter: 1632184914b5SBarry Smith . snes - the SNES context 1633184914b5SBarry Smith 1634184914b5SBarry Smith Output Parameter: 1635e090d566SSatish Balay . reason - negative value indicates diverged, positive value converged, see petscsnes.h or the 1636184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1637184914b5SBarry Smith 1638184914b5SBarry Smith Level: intermediate 1639184914b5SBarry Smith 1640184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1641184914b5SBarry Smith 1642184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1643184914b5SBarry Smith 164485385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 1645184914b5SBarry Smith @*/ 164663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1647184914b5SBarry Smith { 1648184914b5SBarry Smith PetscFunctionBegin; 16494482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16504482741eSBarry Smith PetscValidPointer(reason,2); 1651184914b5SBarry Smith *reason = snes->reason; 1652184914b5SBarry Smith PetscFunctionReturn(0); 1653184914b5SBarry Smith } 1654184914b5SBarry Smith 16554a2ae208SSatish Balay #undef __FUNCT__ 16564a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 1657c9005455SLois Curfman McInnes /*@ 1658c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1659c9005455SLois Curfman McInnes 1660fee21e36SBarry Smith Collective on SNES 1661fee21e36SBarry Smith 1662c7afd0dbSLois Curfman McInnes Input Parameters: 1663c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1664c7afd0dbSLois Curfman McInnes . a - array to hold history 1665cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 1666758f92a0SBarry Smith . na - size of a and its 166764731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 1668758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1669c7afd0dbSLois Curfman McInnes 1670c9005455SLois Curfman McInnes Notes: 16714b27c08aSLois Curfman McInnes If set, this array will contain the function norms computed 1672c9005455SLois Curfman McInnes at each step. 1673c9005455SLois Curfman McInnes 1674c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1675c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1676c9005455SLois Curfman McInnes during the section of code that is being timed. 1677c9005455SLois Curfman McInnes 167836851e7fSLois Curfman McInnes Level: intermediate 167936851e7fSLois Curfman McInnes 1680c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1681758f92a0SBarry Smith 168208405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1683758f92a0SBarry Smith 1684c9005455SLois Curfman McInnes @*/ 1685a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset) 1686c9005455SLois Curfman McInnes { 16873a40ed3dSBarry Smith PetscFunctionBegin; 16884482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16894482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 1690a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 1691c9005455SLois Curfman McInnes snes->conv_hist = a; 1692758f92a0SBarry Smith snes->conv_hist_its = its; 1693758f92a0SBarry Smith snes->conv_hist_max = na; 1694758f92a0SBarry Smith snes->conv_hist_reset = reset; 1695758f92a0SBarry Smith PetscFunctionReturn(0); 1696758f92a0SBarry Smith } 1697758f92a0SBarry Smith 16984a2ae208SSatish Balay #undef __FUNCT__ 16994a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 17000c4c9dddSBarry Smith /*@C 1701758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1702758f92a0SBarry Smith 1703758f92a0SBarry Smith Collective on SNES 1704758f92a0SBarry Smith 1705758f92a0SBarry Smith Input Parameter: 1706758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1707758f92a0SBarry Smith 1708758f92a0SBarry Smith Output Parameters: 1709758f92a0SBarry Smith . a - array to hold history 1710758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1711758f92a0SBarry Smith negative if not converged) for each solve. 1712758f92a0SBarry Smith - na - size of a and its 1713758f92a0SBarry Smith 1714758f92a0SBarry Smith Notes: 1715758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1716758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1717758f92a0SBarry Smith 1718758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1719758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1720758f92a0SBarry Smith during the section of code that is being timed. 1721758f92a0SBarry Smith 1722758f92a0SBarry Smith Level: intermediate 1723758f92a0SBarry Smith 1724758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1725758f92a0SBarry Smith 1726758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1727758f92a0SBarry Smith 1728758f92a0SBarry Smith @*/ 172963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 1730758f92a0SBarry Smith { 1731758f92a0SBarry Smith PetscFunctionBegin; 17324482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1733758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1734758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1735758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 17363a40ed3dSBarry Smith PetscFunctionReturn(0); 1737c9005455SLois Curfman McInnes } 1738c9005455SLois Curfman McInnes 1739e74ef692SMatthew Knepley #undef __FUNCT__ 1740e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 1741ac226902SBarry Smith /*@C 174276b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 17437e4bb74cSBarry Smith at the beginning o every iteration of the nonlinear solve. Specifically 17447e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 174576b2cf59SMatthew Knepley 174676b2cf59SMatthew Knepley Collective on SNES 174776b2cf59SMatthew Knepley 174876b2cf59SMatthew Knepley Input Parameters: 174976b2cf59SMatthew Knepley . snes - The nonlinear solver context 175076b2cf59SMatthew Knepley . func - The function 175176b2cf59SMatthew Knepley 175276b2cf59SMatthew Knepley Calling sequence of func: 1753b5d30489SBarry Smith . func (SNES snes, PetscInt step); 175476b2cf59SMatthew Knepley 175576b2cf59SMatthew Knepley . step - The current step of the iteration 175676b2cf59SMatthew Knepley 175776b2cf59SMatthew Knepley Level: intermediate 175876b2cf59SMatthew Knepley 175976b2cf59SMatthew Knepley .keywords: SNES, update 1760b5d30489SBarry Smith 176185385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 176276b2cf59SMatthew Knepley @*/ 176363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 176476b2cf59SMatthew Knepley { 176576b2cf59SMatthew Knepley PetscFunctionBegin; 17664482741eSBarry Smith PetscValidHeaderSpecific(snes, SNES_COOKIE,1); 1767e7788613SBarry Smith snes->ops->update = func; 176876b2cf59SMatthew Knepley PetscFunctionReturn(0); 176976b2cf59SMatthew Knepley } 177076b2cf59SMatthew Knepley 1771e74ef692SMatthew Knepley #undef __FUNCT__ 1772e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 177376b2cf59SMatthew Knepley /*@ 177476b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 177576b2cf59SMatthew Knepley 177676b2cf59SMatthew Knepley Not collective 177776b2cf59SMatthew Knepley 177876b2cf59SMatthew Knepley Input Parameters: 177976b2cf59SMatthew Knepley . snes - The nonlinear solver context 178076b2cf59SMatthew Knepley . step - The current step of the iteration 178176b2cf59SMatthew Knepley 1782205452f4SMatthew Knepley Level: intermediate 1783205452f4SMatthew Knepley 178476b2cf59SMatthew Knepley .keywords: SNES, update 1785a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 178676b2cf59SMatthew Knepley @*/ 178763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step) 178876b2cf59SMatthew Knepley { 178976b2cf59SMatthew Knepley PetscFunctionBegin; 179076b2cf59SMatthew Knepley PetscFunctionReturn(0); 179176b2cf59SMatthew Knepley } 179276b2cf59SMatthew Knepley 17934a2ae208SSatish Balay #undef __FUNCT__ 17944a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 17959b94acceSBarry Smith /* 17969b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 17979b94acceSBarry Smith positive parameter delta. 17989b94acceSBarry Smith 17999b94acceSBarry Smith Input Parameters: 1800c7afd0dbSLois Curfman McInnes + snes - the SNES context 18019b94acceSBarry Smith . y - approximate solution of linear system 18029b94acceSBarry Smith . fnorm - 2-norm of current function 1803c7afd0dbSLois Curfman McInnes - delta - trust region size 18049b94acceSBarry Smith 18059b94acceSBarry Smith Output Parameters: 1806c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 18079b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 18089b94acceSBarry Smith region, and exceeds zero otherwise. 1809c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 18109b94acceSBarry Smith 18119b94acceSBarry Smith Note: 18124b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 18139b94acceSBarry Smith is set to be the maximum allowable step size. 18149b94acceSBarry Smith 18159b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 18169b94acceSBarry Smith */ 1817dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 18189b94acceSBarry Smith { 1819064f8208SBarry Smith PetscReal nrm; 1820ea709b57SSatish Balay PetscScalar cnorm; 1821dfbe8321SBarry Smith PetscErrorCode ierr; 18223a40ed3dSBarry Smith 18233a40ed3dSBarry Smith PetscFunctionBegin; 18244482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 18254482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,2); 1826c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 1827184914b5SBarry Smith 1828064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 1829064f8208SBarry Smith if (nrm > *delta) { 1830064f8208SBarry Smith nrm = *delta/nrm; 1831064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 1832064f8208SBarry Smith cnorm = nrm; 18332dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 18349b94acceSBarry Smith *ynorm = *delta; 18359b94acceSBarry Smith } else { 18369b94acceSBarry Smith *gpnorm = 0.0; 1837064f8208SBarry Smith *ynorm = nrm; 18389b94acceSBarry Smith } 18393a40ed3dSBarry Smith PetscFunctionReturn(0); 18409b94acceSBarry Smith } 18419b94acceSBarry Smith 18424a2ae208SSatish Balay #undef __FUNCT__ 18434a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 18446ce558aeSBarry Smith /*@C 1845f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 1846f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 18479b94acceSBarry Smith 1848c7afd0dbSLois Curfman McInnes Collective on SNES 1849c7afd0dbSLois Curfman McInnes 1850b2002411SLois Curfman McInnes Input Parameters: 1851c7afd0dbSLois Curfman McInnes + snes - the SNES context 1852f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 185385385478SLisandro Dalcin - x - the solution vector. 18549b94acceSBarry Smith 1855b2002411SLois Curfman McInnes Notes: 18568ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 18578ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 18588ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 18598ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 18608ddd3da0SLois Curfman McInnes 186136851e7fSLois Curfman McInnes Level: beginner 186236851e7fSLois Curfman McInnes 18639b94acceSBarry Smith .keywords: SNES, nonlinear, solve 18649b94acceSBarry Smith 186585385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 18669b94acceSBarry Smith @*/ 1867f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x) 18689b94acceSBarry Smith { 1869dfbe8321SBarry Smith PetscErrorCode ierr; 1870f1af5d2fSBarry Smith PetscTruth flg; 1871eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 1872eabae89aSBarry Smith PetscViewer viewer; 1873052efed2SBarry Smith 18743a40ed3dSBarry Smith PetscFunctionBegin; 18754482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1876f69a0ea3SMatthew Knepley PetscValidHeaderSpecific(x,VEC_COOKIE,3); 1877f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 187885385478SLisandro Dalcin if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2); 187985385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 188085385478SLisandro Dalcin 188185385478SLisandro Dalcin /* set solution vector */ 188285385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 188385385478SLisandro Dalcin if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); } 188485385478SLisandro Dalcin snes->vec_sol = x; 188585385478SLisandro Dalcin /* set afine vector if provided */ 188685385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 188785385478SLisandro Dalcin if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); } 188885385478SLisandro Dalcin snes->vec_rhs = b; 188985385478SLisandro Dalcin 189085385478SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 189185385478SLisandro Dalcin ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr); 189270e92668SMatthew Knepley } 18933f149594SLisandro Dalcin 189470e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 18953f149594SLisandro Dalcin 1896abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 189750ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 1898d5e45103SBarry Smith 18993f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 19004936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 190185385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 19024936397dSBarry Smith if (snes->domainerror){ 19034936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 19044936397dSBarry Smith snes->domainerror = PETSC_FALSE; 19054936397dSBarry Smith } 190685385478SLisandro Dalcin 19073f149594SLisandro Dalcin if (!snes->reason) { 19083f149594SLisandro Dalcin SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 19093f149594SLisandro Dalcin } 19103f149594SLisandro Dalcin 19117adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1912eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 19137adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 1914eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 1915eabae89aSBarry Smith ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 1916eabae89aSBarry Smith } 1917eabae89aSBarry Smith 19187adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr); 1919da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 19205968eb51SBarry Smith if (snes->printreason) { 19215968eb51SBarry Smith if (snes->reason > 0) { 19227adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 19235968eb51SBarry Smith } else { 19247adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 19255968eb51SBarry Smith } 19265968eb51SBarry Smith } 19275968eb51SBarry Smith 19283a40ed3dSBarry Smith PetscFunctionReturn(0); 19299b94acceSBarry Smith } 19309b94acceSBarry Smith 19319b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 19329b94acceSBarry Smith 19334a2ae208SSatish Balay #undef __FUNCT__ 19344a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 193582bf6240SBarry Smith /*@C 19364b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 19379b94acceSBarry Smith 1938fee21e36SBarry Smith Collective on SNES 1939fee21e36SBarry Smith 1940c7afd0dbSLois Curfman McInnes Input Parameters: 1941c7afd0dbSLois Curfman McInnes + snes - the SNES context 1942454a90a3SBarry Smith - type - a known method 1943c7afd0dbSLois Curfman McInnes 1944c7afd0dbSLois Curfman McInnes Options Database Key: 1945454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 1946c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 1947ae12b187SLois Curfman McInnes 19489b94acceSBarry Smith Notes: 1949e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 19504b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 1951c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 19524b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 1953c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 19549b94acceSBarry Smith 1955ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 1956ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 1957ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 1958ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 1959ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 1960ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 1961ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 1962ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 1963ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 1964b0a32e0cSBarry Smith appropriate method. 196536851e7fSLois Curfman McInnes 196636851e7fSLois Curfman McInnes Level: intermediate 1967a703fe33SLois Curfman McInnes 1968454a90a3SBarry Smith .keywords: SNES, set, type 1969435da068SBarry Smith 1970435da068SBarry Smith .seealso: SNESType, SNESCreate() 1971435da068SBarry Smith 19729b94acceSBarry Smith @*/ 1973e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,SNESType type) 19749b94acceSBarry Smith { 1975dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 19766831982aSBarry Smith PetscTruth match; 19773a40ed3dSBarry Smith 19783a40ed3dSBarry Smith PetscFunctionBegin; 19794482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 19804482741eSBarry Smith PetscValidCharPointer(type,2); 198182bf6240SBarry Smith 19826831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 19830f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 198492ff6ae8SBarry Smith 19857adad957SLisandro Dalcin ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 1986958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 198775396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 198875396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 198975396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 199075396ef9SLisandro Dalcin snes->ops->setup = 0; 199175396ef9SLisandro Dalcin snes->ops->solve = 0; 199275396ef9SLisandro Dalcin snes->ops->view = 0; 199375396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 199475396ef9SLisandro Dalcin snes->ops->destroy = 0; 199575396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 199675396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 19973a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 1998454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 19993a40ed3dSBarry Smith PetscFunctionReturn(0); 20009b94acceSBarry Smith } 20019b94acceSBarry Smith 2002a847f771SSatish Balay 20039b94acceSBarry Smith /* --------------------------------------------------------------------- */ 20044a2ae208SSatish Balay #undef __FUNCT__ 20054a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 200652baeb72SSatish Balay /*@ 20079b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2008f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 20099b94acceSBarry Smith 2010fee21e36SBarry Smith Not Collective 2011fee21e36SBarry Smith 201236851e7fSLois Curfman McInnes Level: advanced 201336851e7fSLois Curfman McInnes 20149b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 20159b94acceSBarry Smith 20169b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 20179b94acceSBarry Smith @*/ 201863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void) 20199b94acceSBarry Smith { 2020dfbe8321SBarry Smith PetscErrorCode ierr; 202182bf6240SBarry Smith 20223a40ed3dSBarry Smith PetscFunctionBegin; 20231441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 20244c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 20253a40ed3dSBarry Smith PetscFunctionReturn(0); 20269b94acceSBarry Smith } 20279b94acceSBarry Smith 20284a2ae208SSatish Balay #undef __FUNCT__ 20294a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 20309b94acceSBarry Smith /*@C 20319a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 20329b94acceSBarry Smith 2033c7afd0dbSLois Curfman McInnes Not Collective 2034c7afd0dbSLois Curfman McInnes 20359b94acceSBarry Smith Input Parameter: 20364b0e389bSBarry Smith . snes - nonlinear solver context 20379b94acceSBarry Smith 20389b94acceSBarry Smith Output Parameter: 20393a7fca6bSBarry Smith . type - SNES method (a character string) 20409b94acceSBarry Smith 204136851e7fSLois Curfman McInnes Level: intermediate 204236851e7fSLois Curfman McInnes 2043454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 20449b94acceSBarry Smith @*/ 204563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,SNESType *type) 20469b94acceSBarry Smith { 20473a40ed3dSBarry Smith PetscFunctionBegin; 20484482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20494482741eSBarry Smith PetscValidPointer(type,2); 20507adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 20513a40ed3dSBarry Smith PetscFunctionReturn(0); 20529b94acceSBarry Smith } 20539b94acceSBarry Smith 20544a2ae208SSatish Balay #undef __FUNCT__ 20554a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 205652baeb72SSatish Balay /*@ 20579b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 20589b94acceSBarry Smith stored. 20599b94acceSBarry Smith 2060c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2061c7afd0dbSLois Curfman McInnes 20629b94acceSBarry Smith Input Parameter: 20639b94acceSBarry Smith . snes - the SNES context 20649b94acceSBarry Smith 20659b94acceSBarry Smith Output Parameter: 20669b94acceSBarry Smith . x - the solution 20679b94acceSBarry Smith 206870e92668SMatthew Knepley Level: intermediate 206936851e7fSLois Curfman McInnes 20709b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 20719b94acceSBarry Smith 207285385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 20739b94acceSBarry Smith @*/ 207463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x) 20759b94acceSBarry Smith { 20763a40ed3dSBarry Smith PetscFunctionBegin; 20774482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20784482741eSBarry Smith PetscValidPointer(x,2); 207985385478SLisandro Dalcin *x = snes->vec_sol; 208070e92668SMatthew Knepley PetscFunctionReturn(0); 208170e92668SMatthew Knepley } 208270e92668SMatthew Knepley 208370e92668SMatthew Knepley #undef __FUNCT__ 20844a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 208552baeb72SSatish Balay /*@ 20869b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 20879b94acceSBarry Smith stored. 20889b94acceSBarry Smith 2089c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2090c7afd0dbSLois Curfman McInnes 20919b94acceSBarry Smith Input Parameter: 20929b94acceSBarry Smith . snes - the SNES context 20939b94acceSBarry Smith 20949b94acceSBarry Smith Output Parameter: 20959b94acceSBarry Smith . x - the solution update 20969b94acceSBarry Smith 209736851e7fSLois Curfman McInnes Level: advanced 209836851e7fSLois Curfman McInnes 20999b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 21009b94acceSBarry Smith 210185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 21029b94acceSBarry Smith @*/ 210363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x) 21049b94acceSBarry Smith { 21053a40ed3dSBarry Smith PetscFunctionBegin; 21064482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 21074482741eSBarry Smith PetscValidPointer(x,2); 210885385478SLisandro Dalcin *x = snes->vec_sol_update; 21093a40ed3dSBarry Smith PetscFunctionReturn(0); 21109b94acceSBarry Smith } 21119b94acceSBarry Smith 21124a2ae208SSatish Balay #undef __FUNCT__ 21134a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 21149b94acceSBarry Smith /*@C 21153638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 21169b94acceSBarry Smith 2117c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2118c7afd0dbSLois Curfman McInnes 21199b94acceSBarry Smith Input Parameter: 21209b94acceSBarry Smith . snes - the SNES context 21219b94acceSBarry Smith 21229b94acceSBarry Smith Output Parameter: 21237bf4e008SBarry Smith + r - the function (or PETSC_NULL) 212470e92668SMatthew Knepley . func - the function (or PETSC_NULL) 212570e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 21269b94acceSBarry Smith 212736851e7fSLois Curfman McInnes Level: advanced 212836851e7fSLois Curfman McInnes 2129a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 21309b94acceSBarry Smith 21314b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 21329b94acceSBarry Smith @*/ 213370e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 21349b94acceSBarry Smith { 21353a40ed3dSBarry Smith PetscFunctionBegin; 21364482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 213785385478SLisandro Dalcin if (r) *r = snes->vec_func; 2138e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 213970e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 21403a40ed3dSBarry Smith PetscFunctionReturn(0); 21419b94acceSBarry Smith } 21429b94acceSBarry Smith 21434a2ae208SSatish Balay #undef __FUNCT__ 21444a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 21453c7409f5SSatish Balay /*@C 21463c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2147d850072dSLois Curfman McInnes SNES options in the database. 21483c7409f5SSatish Balay 2149fee21e36SBarry Smith Collective on SNES 2150fee21e36SBarry Smith 2151c7afd0dbSLois Curfman McInnes Input Parameter: 2152c7afd0dbSLois Curfman McInnes + snes - the SNES context 2153c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2154c7afd0dbSLois Curfman McInnes 2155d850072dSLois Curfman McInnes Notes: 2156a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2157c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2158d850072dSLois Curfman McInnes 215936851e7fSLois Curfman McInnes Level: advanced 216036851e7fSLois Curfman McInnes 21613c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2162a86d99e1SLois Curfman McInnes 2163a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 21643c7409f5SSatish Balay @*/ 216563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[]) 21663c7409f5SSatish Balay { 2167dfbe8321SBarry Smith PetscErrorCode ierr; 21683c7409f5SSatish Balay 21693a40ed3dSBarry Smith PetscFunctionBegin; 21704482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2171639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 217294b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 21733a40ed3dSBarry Smith PetscFunctionReturn(0); 21743c7409f5SSatish Balay } 21753c7409f5SSatish Balay 21764a2ae208SSatish Balay #undef __FUNCT__ 21774a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 21783c7409f5SSatish Balay /*@C 2179f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2180d850072dSLois Curfman McInnes SNES options in the database. 21813c7409f5SSatish Balay 2182fee21e36SBarry Smith Collective on SNES 2183fee21e36SBarry Smith 2184c7afd0dbSLois Curfman McInnes Input Parameters: 2185c7afd0dbSLois Curfman McInnes + snes - the SNES context 2186c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2187c7afd0dbSLois Curfman McInnes 2188d850072dSLois Curfman McInnes Notes: 2189a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2190c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2191d850072dSLois Curfman McInnes 219236851e7fSLois Curfman McInnes Level: advanced 219336851e7fSLois Curfman McInnes 21943c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2195a86d99e1SLois Curfman McInnes 2196a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 21973c7409f5SSatish Balay @*/ 219863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 21993c7409f5SSatish Balay { 2200dfbe8321SBarry Smith PetscErrorCode ierr; 22013c7409f5SSatish Balay 22023a40ed3dSBarry Smith PetscFunctionBegin; 22034482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2204639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 220594b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 22063a40ed3dSBarry Smith PetscFunctionReturn(0); 22073c7409f5SSatish Balay } 22083c7409f5SSatish Balay 22094a2ae208SSatish Balay #undef __FUNCT__ 22104a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 22119ab63eb5SSatish Balay /*@C 22123c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 22133c7409f5SSatish Balay SNES options in the database. 22143c7409f5SSatish Balay 2215c7afd0dbSLois Curfman McInnes Not Collective 2216c7afd0dbSLois Curfman McInnes 22173c7409f5SSatish Balay Input Parameter: 22183c7409f5SSatish Balay . snes - the SNES context 22193c7409f5SSatish Balay 22203c7409f5SSatish Balay Output Parameter: 22213c7409f5SSatish Balay . prefix - pointer to the prefix string used 22223c7409f5SSatish Balay 22239ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 22249ab63eb5SSatish Balay sufficient length to hold the prefix. 22259ab63eb5SSatish Balay 222636851e7fSLois Curfman McInnes Level: advanced 222736851e7fSLois Curfman McInnes 22283c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2229a86d99e1SLois Curfman McInnes 2230a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 22313c7409f5SSatish Balay @*/ 2232e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 22333c7409f5SSatish Balay { 2234dfbe8321SBarry Smith PetscErrorCode ierr; 22353c7409f5SSatish Balay 22363a40ed3dSBarry Smith PetscFunctionBegin; 22374482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2238639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 22393a40ed3dSBarry Smith PetscFunctionReturn(0); 22403c7409f5SSatish Balay } 22413c7409f5SSatish Balay 2242b2002411SLois Curfman McInnes 22434a2ae208SSatish Balay #undef __FUNCT__ 22444a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 22453cea93caSBarry Smith /*@C 22463cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 22473cea93caSBarry Smith 22487f6c08e0SMatthew Knepley Level: advanced 22493cea93caSBarry Smith @*/ 225063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2251b2002411SLois Curfman McInnes { 2252e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2253dfbe8321SBarry Smith PetscErrorCode ierr; 2254b2002411SLois Curfman McInnes 2255b2002411SLois Curfman McInnes PetscFunctionBegin; 2256b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2257c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2258b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2259b2002411SLois Curfman McInnes } 2260da9b6338SBarry Smith 2261da9b6338SBarry Smith #undef __FUNCT__ 2262da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 226363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes) 2264da9b6338SBarry Smith { 2265dfbe8321SBarry Smith PetscErrorCode ierr; 226677431f27SBarry Smith PetscInt N,i,j; 2267da9b6338SBarry Smith Vec u,uh,fh; 2268da9b6338SBarry Smith PetscScalar value; 2269da9b6338SBarry Smith PetscReal norm; 2270da9b6338SBarry Smith 2271da9b6338SBarry Smith PetscFunctionBegin; 2272da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 2273da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 2274da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 2275da9b6338SBarry Smith 2276da9b6338SBarry Smith /* currently only works for sequential */ 2277da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 2278da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 2279da9b6338SBarry Smith for (i=0; i<N; i++) { 2280da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 228177431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 2282da9b6338SBarry Smith for (j=-10; j<11; j++) { 2283ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 2284da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 22853ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 2286da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 228777431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 2288da9b6338SBarry Smith value = -value; 2289da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 2290da9b6338SBarry Smith } 2291da9b6338SBarry Smith } 2292da9b6338SBarry Smith ierr = VecDestroy(uh);CHKERRQ(ierr); 2293da9b6338SBarry Smith ierr = VecDestroy(fh);CHKERRQ(ierr); 2294da9b6338SBarry Smith PetscFunctionReturn(0); 2295da9b6338SBarry Smith } 229671f87433Sdalcinl 229771f87433Sdalcinl #undef __FUNCT__ 2298fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 229971f87433Sdalcinl /*@ 2300fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 230171f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 230271f87433Sdalcinl Newton method. 230371f87433Sdalcinl 230471f87433Sdalcinl Collective on SNES 230571f87433Sdalcinl 230671f87433Sdalcinl Input Parameters: 230771f87433Sdalcinl + snes - SNES context 230871f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 230971f87433Sdalcinl 231071f87433Sdalcinl Notes: 231171f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 231271f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 231371f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 231471f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 231571f87433Sdalcinl solver. 231671f87433Sdalcinl 231771f87433Sdalcinl Level: advanced 231871f87433Sdalcinl 231971f87433Sdalcinl Reference: 232071f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 232171f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 232271f87433Sdalcinl 232371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 232471f87433Sdalcinl 2325fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 232671f87433Sdalcinl @*/ 2327fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag) 232871f87433Sdalcinl { 232971f87433Sdalcinl PetscFunctionBegin; 233071f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 233171f87433Sdalcinl snes->ksp_ewconv = flag; 233271f87433Sdalcinl PetscFunctionReturn(0); 233371f87433Sdalcinl } 233471f87433Sdalcinl 233571f87433Sdalcinl #undef __FUNCT__ 2336fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 233771f87433Sdalcinl /*@ 2338fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 233971f87433Sdalcinl for computing relative tolerance for linear solvers within an 234071f87433Sdalcinl inexact Newton method. 234171f87433Sdalcinl 234271f87433Sdalcinl Not Collective 234371f87433Sdalcinl 234471f87433Sdalcinl Input Parameter: 234571f87433Sdalcinl . snes - SNES context 234671f87433Sdalcinl 234771f87433Sdalcinl Output Parameter: 234871f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 234971f87433Sdalcinl 235071f87433Sdalcinl Notes: 235171f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 235271f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 235371f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 235471f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 235571f87433Sdalcinl solver. 235671f87433Sdalcinl 235771f87433Sdalcinl Level: advanced 235871f87433Sdalcinl 235971f87433Sdalcinl Reference: 236071f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 236171f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 236271f87433Sdalcinl 236371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 236471f87433Sdalcinl 2365fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 236671f87433Sdalcinl @*/ 2367fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag) 236871f87433Sdalcinl { 236971f87433Sdalcinl PetscFunctionBegin; 237071f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 237171f87433Sdalcinl PetscValidPointer(flag,2); 237271f87433Sdalcinl *flag = snes->ksp_ewconv; 237371f87433Sdalcinl PetscFunctionReturn(0); 237471f87433Sdalcinl } 237571f87433Sdalcinl 237671f87433Sdalcinl #undef __FUNCT__ 2377fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 237871f87433Sdalcinl /*@ 2379fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 238071f87433Sdalcinl convergence criteria for the linear solvers within an inexact 238171f87433Sdalcinl Newton method. 238271f87433Sdalcinl 238371f87433Sdalcinl Collective on SNES 238471f87433Sdalcinl 238571f87433Sdalcinl Input Parameters: 238671f87433Sdalcinl + snes - SNES context 238771f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 238871f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 238971f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 239071f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 239171f87433Sdalcinl (0 <= gamma2 <= 1) 239271f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 239371f87433Sdalcinl . alpha2 - power for safeguard 239471f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 239571f87433Sdalcinl 239671f87433Sdalcinl Note: 239771f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 239871f87433Sdalcinl 239971f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 240071f87433Sdalcinl 240171f87433Sdalcinl Level: advanced 240271f87433Sdalcinl 240371f87433Sdalcinl Reference: 240471f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 240571f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 240671f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 240771f87433Sdalcinl 240871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 240971f87433Sdalcinl 2410fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 241171f87433Sdalcinl @*/ 2412fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 241371f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 241471f87433Sdalcinl { 2415fa9f3622SBarry Smith SNESKSPEW *kctx; 241671f87433Sdalcinl PetscFunctionBegin; 241771f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2418fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 241971f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 242071f87433Sdalcinl 242171f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 242271f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 242371f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 242471f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 242571f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 242671f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 242771f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 242871f87433Sdalcinl 242971f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 243071f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 243171f87433Sdalcinl } 243271f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 243371f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 243471f87433Sdalcinl } 243571f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 243671f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 243771f87433Sdalcinl } 243871f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 243971f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 244071f87433Sdalcinl } 244171f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 244271f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 244371f87433Sdalcinl } 244471f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 244571f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 244671f87433Sdalcinl } 244771f87433Sdalcinl PetscFunctionReturn(0); 244871f87433Sdalcinl } 244971f87433Sdalcinl 245071f87433Sdalcinl #undef __FUNCT__ 2451fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 245271f87433Sdalcinl /*@ 2453fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 245471f87433Sdalcinl convergence criteria for the linear solvers within an inexact 245571f87433Sdalcinl Newton method. 245671f87433Sdalcinl 245771f87433Sdalcinl Not Collective 245871f87433Sdalcinl 245971f87433Sdalcinl Input Parameters: 246071f87433Sdalcinl snes - SNES context 246171f87433Sdalcinl 246271f87433Sdalcinl Output Parameters: 246371f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 246471f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 246571f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 246671f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 246771f87433Sdalcinl (0 <= gamma2 <= 1) 246871f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 246971f87433Sdalcinl . alpha2 - power for safeguard 247071f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 247171f87433Sdalcinl 247271f87433Sdalcinl Level: advanced 247371f87433Sdalcinl 247471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 247571f87433Sdalcinl 2476fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 247771f87433Sdalcinl @*/ 2478fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 247971f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 248071f87433Sdalcinl { 2481fa9f3622SBarry Smith SNESKSPEW *kctx; 248271f87433Sdalcinl PetscFunctionBegin; 248371f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2484fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 248571f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 248671f87433Sdalcinl if(version) *version = kctx->version; 248771f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 248871f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 248971f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 249071f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 249171f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 249271f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 249371f87433Sdalcinl PetscFunctionReturn(0); 249471f87433Sdalcinl } 249571f87433Sdalcinl 249671f87433Sdalcinl #undef __FUNCT__ 2497fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 2498fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 249971f87433Sdalcinl { 250071f87433Sdalcinl PetscErrorCode ierr; 2501fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 250271f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 250371f87433Sdalcinl 250471f87433Sdalcinl PetscFunctionBegin; 250571f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 250671f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 250771f87433Sdalcinl rtol = kctx->rtol_0; 250871f87433Sdalcinl } else { 250971f87433Sdalcinl if (kctx->version == 1) { 251071f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 251171f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 251271f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 251371f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 251471f87433Sdalcinl } else if (kctx->version == 2) { 251571f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 251671f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 251771f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 251871f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 251971f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 252071f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 252171f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 252271f87433Sdalcinl stol = PetscMax(rtol,stol); 252371f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 252471f87433Sdalcinl /* safeguard: avoid oversolving */ 252571f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 252671f87433Sdalcinl stol = PetscMax(rtol,stol); 252771f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 252871f87433Sdalcinl } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 252971f87433Sdalcinl } 253071f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 253171f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 253271f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 253371f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 253471f87433Sdalcinl PetscFunctionReturn(0); 253571f87433Sdalcinl } 253671f87433Sdalcinl 253771f87433Sdalcinl #undef __FUNCT__ 2538fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 2539fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 254071f87433Sdalcinl { 254171f87433Sdalcinl PetscErrorCode ierr; 2542fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 254371f87433Sdalcinl PCSide pcside; 254471f87433Sdalcinl Vec lres; 254571f87433Sdalcinl 254671f87433Sdalcinl PetscFunctionBegin; 254771f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 254871f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 254971f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 255071f87433Sdalcinl if (kctx->version == 1) { 255171f87433Sdalcinl ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr); 255271f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 255371f87433Sdalcinl /* KSP residual is true linear residual */ 255471f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 255571f87433Sdalcinl } else { 255671f87433Sdalcinl /* KSP residual is preconditioned residual */ 255771f87433Sdalcinl /* compute true linear residual norm */ 255871f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 255971f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 256071f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 256171f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 256271f87433Sdalcinl ierr = VecDestroy(lres);CHKERRQ(ierr); 256371f87433Sdalcinl } 256471f87433Sdalcinl } 256571f87433Sdalcinl PetscFunctionReturn(0); 256671f87433Sdalcinl } 256771f87433Sdalcinl 256871f87433Sdalcinl #undef __FUNCT__ 256971f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 257071f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 257171f87433Sdalcinl { 257271f87433Sdalcinl PetscErrorCode ierr; 257371f87433Sdalcinl 257471f87433Sdalcinl PetscFunctionBegin; 2575fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 257671f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 2577fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 257871f87433Sdalcinl PetscFunctionReturn(0); 257971f87433Sdalcinl } 2580