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 */ 9166c7f25SBarry Smith PetscCookie PETSCSNES_DLLEXPORT SNES_COOKIE; 10166c7f25SBarry Smith PetscLogEvent SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval; 11a09944afSBarry Smith 12a09944afSBarry Smith #undef __FUNCT__ 134936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError" 14e725d27bSBarry Smith /*@ 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; 73a313700dSBarry Smith const 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 178ddf469c8SBarry Smith . -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops 179b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 1802492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 18182738288SBarry Smith solver; hence iterations will continue until max_it 1821fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 18382738288SBarry Smith of convergence test 184e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 185e8105e01SRichard Katz filename given prints to stdout 186a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 187a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 188a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 189a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 190e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 1915968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 192fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 19382738288SBarry Smith 19482738288SBarry Smith Options Database for Eisenstat-Walker method: 195fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 1964b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 19736851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 19836851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 19936851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 20036851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 20136851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 20236851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 20382738288SBarry Smith 20411ca99fdSLois Curfman McInnes Notes: 20511ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 20611ca99fdSLois Curfman McInnes the users manual. 20783e2fdc7SBarry Smith 20836851e7fSLois Curfman McInnes Level: beginner 20936851e7fSLois Curfman McInnes 2109b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 2119b94acceSBarry Smith 21269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 2139b94acceSBarry Smith @*/ 21463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes) 2159b94acceSBarry Smith { 216f1af5d2fSBarry Smith PetscTruth flg; 21785385478SLisandro Dalcin PetscInt i,indx; 21885385478SLisandro Dalcin const char *deft = SNESLS; 21985385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 22085385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 221e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 22223d894e5SBarry Smith PetscViewerASCIIMonitor monviewer; 22385385478SLisandro Dalcin PetscErrorCode ierr; 2249b94acceSBarry Smith 2253a40ed3dSBarry Smith PetscFunctionBegin; 2264482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 227ca161407SBarry Smith 2287adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr); 229186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2307adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 231b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 232d64ed03dSBarry Smith if (flg) { 233186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 2347adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 235186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 236d64ed03dSBarry Smith } 237909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 23893c39befSBarry Smith 23987828ca2SBarry Smith ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 24070441072SBarry Smith ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 241186905e3SBarry Smith 24287828ca2SBarry Smith ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 243b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 244b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 24550ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 246ddf469c8SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr); 24785385478SLisandro Dalcin 24885385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 24985385478SLisandro Dalcin if (flg) { 25085385478SLisandro Dalcin switch (indx) { 2517f7931b9SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 2527f7931b9SBarry Smith case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 25385385478SLisandro Dalcin } 25485385478SLisandro Dalcin } 25585385478SLisandro Dalcin 2565968eb51SBarry Smith ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr); 2575968eb51SBarry Smith if (flg) { 2585968eb51SBarry Smith snes->printreason = PETSC_TRUE; 2595968eb51SBarry Smith } 260186905e3SBarry Smith 26185385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 26285385478SLisandro Dalcin 263fa9f3622SBarry Smith ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 264186905e3SBarry Smith 265fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 266fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 267fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 268fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 269fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 270fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 271fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 272186905e3SBarry Smith 273a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr); 274a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 275eabae89aSBarry Smith 276a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 277e8105e01SRichard Katz if (flg) { 2787adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 27923d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 280e8105e01SRichard Katz } 281eabae89aSBarry Smith 282a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 283eabae89aSBarry Smith if (flg) { 2847adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 285f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 286e8105e01SRichard Katz } 287eabae89aSBarry Smith 288a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 289eabae89aSBarry Smith if (flg) { 2907adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 29123d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 292eabae89aSBarry Smith } 293eabae89aSBarry Smith 294a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr); 295a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 296a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr); 297a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 298a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr); 299a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 300a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr); 301a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 302e24b481bSBarry Smith 303b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr); 3044b27c08aSLois Curfman McInnes if (flg) { 305186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 306ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 3079b94acceSBarry Smith } 308639f9d9dSBarry Smith 30976b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 31076b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 31176b2cf59SMatthew Knepley } 31276b2cf59SMatthew Knepley 313e7788613SBarry Smith if (snes->ops->setfromoptions) { 314e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 315639f9d9dSBarry Smith } 316b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3174bbc92c1SBarry Smith 31885385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 31993993e2dSLois Curfman McInnes 3203a40ed3dSBarry Smith PetscFunctionReturn(0); 3219b94acceSBarry Smith } 3229b94acceSBarry Smith 323a847f771SSatish Balay 3244a2ae208SSatish Balay #undef __FUNCT__ 3254a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 3269b94acceSBarry Smith /*@ 3279b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3289b94acceSBarry Smith the nonlinear solvers. 3299b94acceSBarry Smith 330fee21e36SBarry Smith Collective on SNES 331fee21e36SBarry Smith 332c7afd0dbSLois Curfman McInnes Input Parameters: 333c7afd0dbSLois Curfman McInnes + snes - the SNES context 334c7afd0dbSLois Curfman McInnes - usrP - optional user context 335c7afd0dbSLois Curfman McInnes 33636851e7fSLois Curfman McInnes Level: intermediate 33736851e7fSLois Curfman McInnes 3389b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3399b94acceSBarry Smith 3409b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3419b94acceSBarry Smith @*/ 34263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP) 3439b94acceSBarry Smith { 3443a40ed3dSBarry Smith PetscFunctionBegin; 3454482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3469b94acceSBarry Smith snes->user = usrP; 3473a40ed3dSBarry Smith PetscFunctionReturn(0); 3489b94acceSBarry Smith } 34974679c65SBarry Smith 3504a2ae208SSatish Balay #undef __FUNCT__ 3514a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 3529b94acceSBarry Smith /*@C 3539b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3549b94acceSBarry Smith nonlinear solvers. 3559b94acceSBarry Smith 356c7afd0dbSLois Curfman McInnes Not Collective 357c7afd0dbSLois Curfman McInnes 3589b94acceSBarry Smith Input Parameter: 3599b94acceSBarry Smith . snes - SNES context 3609b94acceSBarry Smith 3619b94acceSBarry Smith Output Parameter: 3629b94acceSBarry Smith . usrP - user context 3639b94acceSBarry Smith 36436851e7fSLois Curfman McInnes Level: intermediate 36536851e7fSLois Curfman McInnes 3669b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3679b94acceSBarry Smith 3689b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3699b94acceSBarry Smith @*/ 37063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP) 3719b94acceSBarry Smith { 3723a40ed3dSBarry Smith PetscFunctionBegin; 3734482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3749b94acceSBarry Smith *usrP = snes->user; 3753a40ed3dSBarry Smith PetscFunctionReturn(0); 3769b94acceSBarry Smith } 37774679c65SBarry Smith 3784a2ae208SSatish Balay #undef __FUNCT__ 3794a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 3809b94acceSBarry Smith /*@ 381c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 382c8228a4eSBarry Smith at this time. 3839b94acceSBarry Smith 384c7afd0dbSLois Curfman McInnes Not Collective 385c7afd0dbSLois Curfman McInnes 3869b94acceSBarry Smith Input Parameter: 3879b94acceSBarry Smith . snes - SNES context 3889b94acceSBarry Smith 3899b94acceSBarry Smith Output Parameter: 3909b94acceSBarry Smith . iter - iteration number 3919b94acceSBarry Smith 392c8228a4eSBarry Smith Notes: 393c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 394c8228a4eSBarry Smith 395c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 39608405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 39708405cd6SLois Curfman McInnes .vb 39808405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 39908405cd6SLois Curfman McInnes if (!(it % 2)) { 40008405cd6SLois Curfman McInnes [compute Jacobian here] 40108405cd6SLois Curfman McInnes } 40208405cd6SLois Curfman McInnes .ve 403c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 40408405cd6SLois Curfman McInnes recomputed every second SNES iteration. 405c8228a4eSBarry Smith 40636851e7fSLois Curfman McInnes Level: intermediate 40736851e7fSLois Curfman McInnes 4082b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 4092b668275SBarry Smith 410b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 4119b94acceSBarry Smith @*/ 41263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter) 4139b94acceSBarry Smith { 4143a40ed3dSBarry Smith PetscFunctionBegin; 4154482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4164482741eSBarry Smith PetscValidIntPointer(iter,2); 4179b94acceSBarry Smith *iter = snes->iter; 4183a40ed3dSBarry Smith PetscFunctionReturn(0); 4199b94acceSBarry Smith } 42074679c65SBarry Smith 4214a2ae208SSatish Balay #undef __FUNCT__ 4224a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 4239b94acceSBarry Smith /*@ 4249b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4259b94acceSBarry Smith with SNESSSetFunction(). 4269b94acceSBarry Smith 427c7afd0dbSLois Curfman McInnes Collective on SNES 428c7afd0dbSLois Curfman McInnes 4299b94acceSBarry Smith Input Parameter: 4309b94acceSBarry Smith . snes - SNES context 4319b94acceSBarry Smith 4329b94acceSBarry Smith Output Parameter: 4339b94acceSBarry Smith . fnorm - 2-norm of function 4349b94acceSBarry Smith 43536851e7fSLois Curfman McInnes Level: intermediate 43636851e7fSLois Curfman McInnes 4379b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 438a86d99e1SLois Curfman McInnes 439b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 4409b94acceSBarry Smith @*/ 44171f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 4429b94acceSBarry Smith { 4433a40ed3dSBarry Smith PetscFunctionBegin; 4444482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4454482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 4469b94acceSBarry Smith *fnorm = snes->norm; 4473a40ed3dSBarry Smith PetscFunctionReturn(0); 4489b94acceSBarry Smith } 44974679c65SBarry Smith 4504a2ae208SSatish Balay #undef __FUNCT__ 451b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 4529b94acceSBarry Smith /*@ 453b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 4549b94acceSBarry Smith attempted by the nonlinear solver. 4559b94acceSBarry Smith 456c7afd0dbSLois Curfman McInnes Not Collective 457c7afd0dbSLois Curfman McInnes 4589b94acceSBarry Smith Input Parameter: 4599b94acceSBarry Smith . snes - SNES context 4609b94acceSBarry Smith 4619b94acceSBarry Smith Output Parameter: 4629b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 4639b94acceSBarry Smith 464c96a6f78SLois Curfman McInnes Notes: 465c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 466c96a6f78SLois Curfman McInnes 46736851e7fSLois Curfman McInnes Level: intermediate 46836851e7fSLois Curfman McInnes 4699b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 47058ebbce7SBarry Smith 471*e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 47258ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 4739b94acceSBarry Smith @*/ 474b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 4759b94acceSBarry Smith { 4763a40ed3dSBarry Smith PetscFunctionBegin; 4774482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4784482741eSBarry Smith PetscValidIntPointer(nfails,2); 47950ffb88aSMatthew Knepley *nfails = snes->numFailures; 48050ffb88aSMatthew Knepley PetscFunctionReturn(0); 48150ffb88aSMatthew Knepley } 48250ffb88aSMatthew Knepley 48350ffb88aSMatthew Knepley #undef __FUNCT__ 484b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 48550ffb88aSMatthew Knepley /*@ 486b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 48750ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 48850ffb88aSMatthew Knepley 48950ffb88aSMatthew Knepley Not Collective 49050ffb88aSMatthew Knepley 49150ffb88aSMatthew Knepley Input Parameters: 49250ffb88aSMatthew Knepley + snes - SNES context 49350ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 49450ffb88aSMatthew Knepley 49550ffb88aSMatthew Knepley Level: intermediate 49650ffb88aSMatthew Knepley 49750ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 49858ebbce7SBarry Smith 499*e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 50058ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 50150ffb88aSMatthew Knepley @*/ 502b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 50350ffb88aSMatthew Knepley { 50450ffb88aSMatthew Knepley PetscFunctionBegin; 5054482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 50650ffb88aSMatthew Knepley snes->maxFailures = maxFails; 50750ffb88aSMatthew Knepley PetscFunctionReturn(0); 50850ffb88aSMatthew Knepley } 50950ffb88aSMatthew Knepley 51050ffb88aSMatthew Knepley #undef __FUNCT__ 511b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 51250ffb88aSMatthew Knepley /*@ 513b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 51450ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 51550ffb88aSMatthew Knepley 51650ffb88aSMatthew Knepley Not Collective 51750ffb88aSMatthew Knepley 51850ffb88aSMatthew Knepley Input Parameter: 51950ffb88aSMatthew Knepley . snes - SNES context 52050ffb88aSMatthew Knepley 52150ffb88aSMatthew Knepley Output Parameter: 52250ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 52350ffb88aSMatthew Knepley 52450ffb88aSMatthew Knepley Level: intermediate 52550ffb88aSMatthew Knepley 52650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 52758ebbce7SBarry Smith 528*e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 52958ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 53058ebbce7SBarry Smith 53150ffb88aSMatthew Knepley @*/ 532b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 53350ffb88aSMatthew Knepley { 53450ffb88aSMatthew Knepley PetscFunctionBegin; 5354482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5364482741eSBarry Smith PetscValidIntPointer(maxFails,2); 53750ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 5383a40ed3dSBarry Smith PetscFunctionReturn(0); 5399b94acceSBarry Smith } 540a847f771SSatish Balay 5414a2ae208SSatish Balay #undef __FUNCT__ 5422541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 5432541af92SBarry Smith /*@ 5442541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 5452541af92SBarry Smith done by SNES. 5462541af92SBarry Smith 5472541af92SBarry Smith Not Collective 5482541af92SBarry Smith 5492541af92SBarry Smith Input Parameter: 5502541af92SBarry Smith . snes - SNES context 5512541af92SBarry Smith 5522541af92SBarry Smith Output Parameter: 5532541af92SBarry Smith . nfuncs - number of evaluations 5542541af92SBarry Smith 5552541af92SBarry Smith Level: intermediate 5562541af92SBarry Smith 5572541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 55858ebbce7SBarry Smith 559*e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 5602541af92SBarry Smith @*/ 5612541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 5622541af92SBarry Smith { 5632541af92SBarry Smith PetscFunctionBegin; 5642541af92SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5652541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 5662541af92SBarry Smith *nfuncs = snes->nfuncs; 5672541af92SBarry Smith PetscFunctionReturn(0); 5682541af92SBarry Smith } 5692541af92SBarry Smith 5702541af92SBarry Smith #undef __FUNCT__ 5713d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 5723d4c4710SBarry Smith /*@ 5733d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 5743d4c4710SBarry Smith linear solvers. 5753d4c4710SBarry Smith 5763d4c4710SBarry Smith Not Collective 5773d4c4710SBarry Smith 5783d4c4710SBarry Smith Input Parameter: 5793d4c4710SBarry Smith . snes - SNES context 5803d4c4710SBarry Smith 5813d4c4710SBarry Smith Output Parameter: 5823d4c4710SBarry Smith . nfails - number of failed solves 5833d4c4710SBarry Smith 5843d4c4710SBarry Smith Notes: 5853d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 5863d4c4710SBarry Smith 5873d4c4710SBarry Smith Level: intermediate 5883d4c4710SBarry Smith 5893d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 59058ebbce7SBarry Smith 591*e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 5923d4c4710SBarry Smith @*/ 5933d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 5943d4c4710SBarry Smith { 5953d4c4710SBarry Smith PetscFunctionBegin; 5963d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5973d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 5983d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 5993d4c4710SBarry Smith PetscFunctionReturn(0); 6003d4c4710SBarry Smith } 6013d4c4710SBarry Smith 6023d4c4710SBarry Smith #undef __FUNCT__ 6033d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 6043d4c4710SBarry Smith /*@ 6053d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 6063d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 6073d4c4710SBarry Smith 6083d4c4710SBarry Smith Collective on SNES 6093d4c4710SBarry Smith 6103d4c4710SBarry Smith Input Parameters: 6113d4c4710SBarry Smith + snes - SNES context 6123d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 6133d4c4710SBarry Smith 6143d4c4710SBarry Smith Level: intermediate 6153d4c4710SBarry Smith 616a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 6173d4c4710SBarry Smith 6183d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 6193d4c4710SBarry Smith 62058ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 6213d4c4710SBarry Smith @*/ 6223d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 6233d4c4710SBarry Smith { 6243d4c4710SBarry Smith PetscFunctionBegin; 6253d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6263d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 6273d4c4710SBarry Smith PetscFunctionReturn(0); 6283d4c4710SBarry Smith } 6293d4c4710SBarry Smith 6303d4c4710SBarry Smith #undef __FUNCT__ 6313d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 6323d4c4710SBarry Smith /*@ 6333d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 6343d4c4710SBarry Smith are allowed before SNES terminates 6353d4c4710SBarry Smith 6363d4c4710SBarry Smith Not Collective 6373d4c4710SBarry Smith 6383d4c4710SBarry Smith Input Parameter: 6393d4c4710SBarry Smith . snes - SNES context 6403d4c4710SBarry Smith 6413d4c4710SBarry Smith Output Parameter: 6423d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 6433d4c4710SBarry Smith 6443d4c4710SBarry Smith Level: intermediate 6453d4c4710SBarry Smith 6463d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 6473d4c4710SBarry Smith 6483d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 6493d4c4710SBarry Smith 650*e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 6513d4c4710SBarry Smith @*/ 6523d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 6533d4c4710SBarry Smith { 6543d4c4710SBarry Smith PetscFunctionBegin; 6553d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6563d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 6573d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 6583d4c4710SBarry Smith PetscFunctionReturn(0); 6593d4c4710SBarry Smith } 6603d4c4710SBarry Smith 6613d4c4710SBarry Smith #undef __FUNCT__ 662b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 663c96a6f78SLois Curfman McInnes /*@ 664b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 665c96a6f78SLois Curfman McInnes used by the nonlinear solver. 666c96a6f78SLois Curfman McInnes 667c7afd0dbSLois Curfman McInnes Not Collective 668c7afd0dbSLois Curfman McInnes 669c96a6f78SLois Curfman McInnes Input Parameter: 670c96a6f78SLois Curfman McInnes . snes - SNES context 671c96a6f78SLois Curfman McInnes 672c96a6f78SLois Curfman McInnes Output Parameter: 673c96a6f78SLois Curfman McInnes . lits - number of linear iterations 674c96a6f78SLois Curfman McInnes 675c96a6f78SLois Curfman McInnes Notes: 676c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 677c96a6f78SLois Curfman McInnes 67836851e7fSLois Curfman McInnes Level: intermediate 67936851e7fSLois Curfman McInnes 680c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 6812b668275SBarry Smith 68258ebbce7SBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm()S, NESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 683c96a6f78SLois Curfman McInnes @*/ 684b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 685c96a6f78SLois Curfman McInnes { 6863a40ed3dSBarry Smith PetscFunctionBegin; 6874482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6884482741eSBarry Smith PetscValidIntPointer(lits,2); 689c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 6903a40ed3dSBarry Smith PetscFunctionReturn(0); 691c96a6f78SLois Curfman McInnes } 692c96a6f78SLois Curfman McInnes 6934a2ae208SSatish Balay #undef __FUNCT__ 69494b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 69552baeb72SSatish Balay /*@ 69694b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 6979b94acceSBarry Smith 69894b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 699c7afd0dbSLois Curfman McInnes 7009b94acceSBarry Smith Input Parameter: 7019b94acceSBarry Smith . snes - the SNES context 7029b94acceSBarry Smith 7039b94acceSBarry Smith Output Parameter: 70494b7f48cSBarry Smith . ksp - the KSP context 7059b94acceSBarry Smith 7069b94acceSBarry Smith Notes: 70794b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 7089b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 7092999313aSBarry Smith PC contexts as well. 7109b94acceSBarry Smith 71136851e7fSLois Curfman McInnes Level: beginner 71236851e7fSLois Curfman McInnes 71394b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7149b94acceSBarry Smith 7152999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7169b94acceSBarry Smith @*/ 71763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp) 7189b94acceSBarry Smith { 7193a40ed3dSBarry Smith PetscFunctionBegin; 7204482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7214482741eSBarry Smith PetscValidPointer(ksp,2); 72294b7f48cSBarry Smith *ksp = snes->ksp; 7233a40ed3dSBarry Smith PetscFunctionReturn(0); 7249b94acceSBarry Smith } 72582bf6240SBarry Smith 7264a2ae208SSatish Balay #undef __FUNCT__ 7272999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 7282999313aSBarry Smith /*@ 7292999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 7302999313aSBarry Smith 7312999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 7322999313aSBarry Smith 7332999313aSBarry Smith Input Parameters: 7342999313aSBarry Smith + snes - the SNES context 7352999313aSBarry Smith - ksp - the KSP context 7362999313aSBarry Smith 7372999313aSBarry Smith Notes: 7382999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 7392999313aSBarry Smith so this routine is rarely needed. 7402999313aSBarry Smith 7412999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 7422999313aSBarry Smith decreased by one. 7432999313aSBarry Smith 7442999313aSBarry Smith Level: developer 7452999313aSBarry Smith 7462999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7472999313aSBarry Smith 7482999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7492999313aSBarry Smith @*/ 7502999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp) 7512999313aSBarry Smith { 7522999313aSBarry Smith PetscErrorCode ierr; 7532999313aSBarry Smith 7542999313aSBarry Smith PetscFunctionBegin; 7552999313aSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7562999313aSBarry Smith PetscValidHeaderSpecific(ksp,KSP_COOKIE,2); 7572999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 7587dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 759906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 7602999313aSBarry Smith snes->ksp = ksp; 7612999313aSBarry Smith PetscFunctionReturn(0); 7622999313aSBarry Smith } 7632999313aSBarry Smith 7647adad957SLisandro Dalcin #if 0 7652999313aSBarry Smith #undef __FUNCT__ 7664a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 7676849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 768e24b481bSBarry Smith { 769e24b481bSBarry Smith PetscFunctionBegin; 770e24b481bSBarry Smith PetscFunctionReturn(0); 771e24b481bSBarry Smith } 7727adad957SLisandro Dalcin #endif 773e24b481bSBarry Smith 7749b94acceSBarry Smith /* -----------------------------------------------------------*/ 7754a2ae208SSatish Balay #undef __FUNCT__ 7764a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 77752baeb72SSatish Balay /*@ 7789b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 7799b94acceSBarry Smith 780c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 781c7afd0dbSLois Curfman McInnes 782c7afd0dbSLois Curfman McInnes Input Parameters: 783906ed7ccSBarry Smith . comm - MPI communicator 7849b94acceSBarry Smith 7859b94acceSBarry Smith Output Parameter: 7869b94acceSBarry Smith . outsnes - the new SNES context 7879b94acceSBarry Smith 788c7afd0dbSLois Curfman McInnes Options Database Keys: 789c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 790c7afd0dbSLois Curfman McInnes and no preconditioning matrix 791c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 792c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 793c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 794c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 795c1f60f51SBarry Smith 79636851e7fSLois Curfman McInnes Level: beginner 79736851e7fSLois Curfman McInnes 7989b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 7999b94acceSBarry Smith 8004b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES 8019b94acceSBarry Smith @*/ 80263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes) 8039b94acceSBarry Smith { 804dfbe8321SBarry Smith PetscErrorCode ierr; 8059b94acceSBarry Smith SNES snes; 806fa9f3622SBarry Smith SNESKSPEW *kctx; 80737fcc0dbSBarry Smith 8083a40ed3dSBarry Smith PetscFunctionBegin; 809ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 8108ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 8118ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 8128ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 8138ba1e511SMatthew Knepley #endif 8148ba1e511SMatthew Knepley 815e7788613SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 8167adad957SLisandro Dalcin 81785385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 8189b94acceSBarry Smith snes->max_its = 50; 8199750a799SBarry Smith snes->max_funcs = 10000; 8209b94acceSBarry Smith snes->norm = 0.0; 821b4874afaSBarry Smith snes->rtol = 1.e-8; 822b4874afaSBarry Smith snes->ttol = 0.0; 82370441072SBarry Smith snes->abstol = 1.e-50; 8249b94acceSBarry Smith snes->xtol = 1.e-8; 8254b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 8269b94acceSBarry Smith snes->nfuncs = 0; 82750ffb88aSMatthew Knepley snes->numFailures = 0; 82850ffb88aSMatthew Knepley snes->maxFailures = 1; 8297a00f4a9SLois Curfman McInnes snes->linear_its = 0; 830639f9d9dSBarry Smith snes->numbermonitors = 0; 8319b94acceSBarry Smith snes->data = 0; 8324dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 833186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 8346f24a144SLois Curfman McInnes snes->vwork = 0; 8356f24a144SLois Curfman McInnes snes->nwork = 0; 836758f92a0SBarry Smith snes->conv_hist_len = 0; 837758f92a0SBarry Smith snes->conv_hist_max = 0; 838758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 839758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 840758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 841184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 8429b94acceSBarry Smith 8433d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 8443d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 8453d4c4710SBarry Smith 8469b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 84738f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 8489b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 8499b94acceSBarry Smith kctx->version = 2; 8509b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 8519b94acceSBarry Smith this was too large for some test cases */ 8529b94acceSBarry Smith kctx->rtol_last = 0; 8539b94acceSBarry Smith kctx->rtol_max = .9; 8549b94acceSBarry Smith kctx->gamma = 1.0; 85571f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 85671f87433Sdalcinl kctx->alpha2 = kctx->alpha; 8579b94acceSBarry Smith kctx->threshold = .1; 8589b94acceSBarry Smith kctx->lresid_last = 0; 8599b94acceSBarry Smith kctx->norm_last = 0; 8609b94acceSBarry Smith 86194b7f48cSBarry Smith ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr); 86252e6d16bSBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 8635334005bSBarry Smith 8649b94acceSBarry Smith *outsnes = snes; 86500036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 8663a40ed3dSBarry Smith PetscFunctionReturn(0); 8679b94acceSBarry Smith } 8689b94acceSBarry Smith 8694a2ae208SSatish Balay #undef __FUNCT__ 8704a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 8719b94acceSBarry Smith /*@C 8729b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 8739b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 8749b94acceSBarry Smith equations. 8759b94acceSBarry Smith 876fee21e36SBarry Smith Collective on SNES 877fee21e36SBarry Smith 878c7afd0dbSLois Curfman McInnes Input Parameters: 879c7afd0dbSLois Curfman McInnes + snes - the SNES context 880c7afd0dbSLois Curfman McInnes . r - vector to store function value 881de044059SHong Zhang . func - function evaluation routine 882c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 883c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8849b94acceSBarry Smith 885c7afd0dbSLois Curfman McInnes Calling sequence of func: 8868d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 887c7afd0dbSLois Curfman McInnes 888313e4042SLois Curfman McInnes . f - function vector 889c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 8909b94acceSBarry Smith 8919b94acceSBarry Smith Notes: 8929b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 8939b94acceSBarry Smith $ f'(x) x = -f(x), 894c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 8959b94acceSBarry Smith 89636851e7fSLois Curfman McInnes Level: beginner 89736851e7fSLois Curfman McInnes 8989b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 8999b94acceSBarry Smith 900a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 9019b94acceSBarry Smith @*/ 90263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 9039b94acceSBarry Smith { 90485385478SLisandro Dalcin PetscErrorCode ierr; 9053a40ed3dSBarry Smith PetscFunctionBegin; 9064482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9074482741eSBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE,2); 908c9780b6fSBarry Smith PetscCheckSameComm(snes,1,r,2); 90985385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 91085385478SLisandro Dalcin if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); } 911e7788613SBarry Smith snes->ops->computefunction = func; 91285385478SLisandro Dalcin snes->vec_func = r; 9139b94acceSBarry Smith snes->funP = ctx; 9143a40ed3dSBarry Smith PetscFunctionReturn(0); 9159b94acceSBarry Smith } 9169b94acceSBarry Smith 9173ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 9183ab0aad5SBarry Smith #undef __FUNCT__ 9191096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 9201096aae1SMatthew Knepley /*@C 9211096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 9221096aae1SMatthew Knepley it assumes a zero right hand side. 9231096aae1SMatthew Knepley 9241096aae1SMatthew Knepley Collective on SNES 9251096aae1SMatthew Knepley 9261096aae1SMatthew Knepley Input Parameter: 9271096aae1SMatthew Knepley . snes - the SNES context 9281096aae1SMatthew Knepley 9291096aae1SMatthew Knepley Output Parameter: 930bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 9311096aae1SMatthew Knepley 9321096aae1SMatthew Knepley Level: intermediate 9331096aae1SMatthew Knepley 9341096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 9351096aae1SMatthew Knepley 93685385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 9371096aae1SMatthew Knepley @*/ 9381096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs) 9391096aae1SMatthew Knepley { 9401096aae1SMatthew Knepley PetscFunctionBegin; 9411096aae1SMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9421096aae1SMatthew Knepley PetscValidPointer(rhs,2); 94385385478SLisandro Dalcin *rhs = snes->vec_rhs; 9441096aae1SMatthew Knepley PetscFunctionReturn(0); 9451096aae1SMatthew Knepley } 9461096aae1SMatthew Knepley 9471096aae1SMatthew Knepley #undef __FUNCT__ 9484a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 9499b94acceSBarry Smith /*@ 95036851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 9519b94acceSBarry Smith SNESSetFunction(). 9529b94acceSBarry Smith 953c7afd0dbSLois Curfman McInnes Collective on SNES 954c7afd0dbSLois Curfman McInnes 9559b94acceSBarry Smith Input Parameters: 956c7afd0dbSLois Curfman McInnes + snes - the SNES context 957c7afd0dbSLois Curfman McInnes - x - input vector 9589b94acceSBarry Smith 9599b94acceSBarry Smith Output Parameter: 9603638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 9619b94acceSBarry Smith 9621bffabb2SLois Curfman McInnes Notes: 96336851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 96436851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 96536851e7fSLois Curfman McInnes themselves. 96636851e7fSLois Curfman McInnes 96736851e7fSLois Curfman McInnes Level: developer 96836851e7fSLois Curfman McInnes 9699b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 9709b94acceSBarry Smith 971a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 9729b94acceSBarry Smith @*/ 97363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y) 9749b94acceSBarry Smith { 975dfbe8321SBarry Smith PetscErrorCode ierr; 9769b94acceSBarry Smith 9773a40ed3dSBarry Smith PetscFunctionBegin; 9784482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9794482741eSBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE,2); 9804482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,3); 981c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 982c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 983184914b5SBarry Smith 984d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 985e7788613SBarry Smith if (snes->ops->computefunction) { 986d64ed03dSBarry Smith PetscStackPush("SNES user function"); 987e9a2bbcdSBarry Smith CHKMEMQ; 988e7788613SBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP); 989e9a2bbcdSBarry Smith CHKMEMQ; 990d64ed03dSBarry Smith PetscStackPop; 991d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 99219717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr); 99319717074SBarry Smith } 994d5e45103SBarry Smith CHKERRQ(ierr); 99585385478SLisandro Dalcin } else if (snes->vec_rhs) { 9961096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 9971096aae1SMatthew Knepley } else { 9981096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 9991096aae1SMatthew Knepley } 100085385478SLisandro Dalcin if (snes->vec_rhs) { 100185385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 10023ab0aad5SBarry Smith } 1003ae3c334cSLois Curfman McInnes snes->nfuncs++; 1004d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 10053a40ed3dSBarry Smith PetscFunctionReturn(0); 10069b94acceSBarry Smith } 10079b94acceSBarry Smith 10084a2ae208SSatish Balay #undef __FUNCT__ 10094a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 101062fef451SLois Curfman McInnes /*@ 101162fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 101262fef451SLois Curfman McInnes set with SNESSetJacobian(). 101362fef451SLois Curfman McInnes 1014c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1015c7afd0dbSLois Curfman McInnes 101662fef451SLois Curfman McInnes Input Parameters: 1017c7afd0dbSLois Curfman McInnes + snes - the SNES context 1018c7afd0dbSLois Curfman McInnes - x - input vector 101962fef451SLois Curfman McInnes 102062fef451SLois Curfman McInnes Output Parameters: 1021c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 102262fef451SLois Curfman McInnes . B - optional preconditioning matrix 10232b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1024fee21e36SBarry Smith 102562fef451SLois Curfman McInnes Notes: 102662fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 102762fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 102862fef451SLois Curfman McInnes 102994b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1030dc5a77f8SLois Curfman McInnes flag parameter. 103162fef451SLois Curfman McInnes 103236851e7fSLois Curfman McInnes Level: developer 103336851e7fSLois Curfman McInnes 103462fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 103562fef451SLois Curfman McInnes 10362b668275SBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure 103762fef451SLois Curfman McInnes @*/ 103863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10399b94acceSBarry Smith { 1040dfbe8321SBarry Smith PetscErrorCode ierr; 10413a40ed3dSBarry Smith 10423a40ed3dSBarry Smith PetscFunctionBegin; 10434482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10444482741eSBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE,2); 10454482741eSBarry Smith PetscValidPointer(flg,5); 1046c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1047e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1048d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1049c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1050d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1051dc67937bSBarry Smith CHKMEMQ; 1052e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1053dc67937bSBarry Smith CHKMEMQ; 1054d64ed03dSBarry Smith PetscStackPop; 1055d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 10566d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 10576ce558aeSBarry Smith /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3); 10586ce558aeSBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE,4); */ 10593a40ed3dSBarry Smith PetscFunctionReturn(0); 10609b94acceSBarry Smith } 10619b94acceSBarry Smith 10624a2ae208SSatish Balay #undef __FUNCT__ 10634a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 10649b94acceSBarry Smith /*@C 10659b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1066044dda88SLois Curfman McInnes location to store the matrix. 10679b94acceSBarry Smith 1068c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1069c7afd0dbSLois Curfman McInnes 10709b94acceSBarry Smith Input Parameters: 1071c7afd0dbSLois Curfman McInnes + snes - the SNES context 10729b94acceSBarry Smith . A - Jacobian matrix 10739b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 10749b94acceSBarry Smith . func - Jacobian evaluation routine 1075c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 10762cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 10779b94acceSBarry Smith 10789b94acceSBarry Smith Calling sequence of func: 10798d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 10809b94acceSBarry Smith 1081c7afd0dbSLois Curfman McInnes + x - input vector 10829b94acceSBarry Smith . A - Jacobian matrix 10839b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1084ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 10852b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1086c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 10879b94acceSBarry Smith 10889b94acceSBarry Smith Notes: 108994b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 10902cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1091ac21db08SLois Curfman McInnes 1092ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 10939b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 10949b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 10959b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 10969b94acceSBarry Smith throughout the global iterations. 10979b94acceSBarry Smith 109816913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 109916913363SBarry Smith each matrix. 110016913363SBarry Smith 110136851e7fSLois Curfman McInnes Level: beginner 110236851e7fSLois Curfman McInnes 11039b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11049b94acceSBarry Smith 11053ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 11069b94acceSBarry Smith @*/ 110763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 11089b94acceSBarry Smith { 1109dfbe8321SBarry Smith PetscErrorCode ierr; 11103a7fca6bSBarry Smith 11113a40ed3dSBarry Smith PetscFunctionBegin; 11124482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11134482741eSBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2); 11144482741eSBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3); 1115c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 1116c9780b6fSBarry Smith if (B) PetscCheckSameComm(snes,1,B,2); 1117e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 11183a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 11193a7fca6bSBarry Smith if (A) { 11207dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 11213a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 11229b94acceSBarry Smith snes->jacobian = A; 11233a7fca6bSBarry Smith } 11243a7fca6bSBarry Smith if (B) { 11257dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 11263a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 11279b94acceSBarry Smith snes->jacobian_pre = B; 11283a7fca6bSBarry Smith } 11293a40ed3dSBarry Smith PetscFunctionReturn(0); 11309b94acceSBarry Smith } 113162fef451SLois Curfman McInnes 11324a2ae208SSatish Balay #undef __FUNCT__ 11334a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1134c2aafc4cSSatish Balay /*@C 1135b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1136b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1137b4fd4287SBarry Smith 1138c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1139c7afd0dbSLois Curfman McInnes 1140b4fd4287SBarry Smith Input Parameter: 1141b4fd4287SBarry Smith . snes - the nonlinear solver context 1142b4fd4287SBarry Smith 1143b4fd4287SBarry Smith Output Parameters: 1144c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1145b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 114670e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 114770e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1148fee21e36SBarry Smith 114936851e7fSLois Curfman McInnes Level: advanced 115036851e7fSLois Curfman McInnes 1151b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1152b4fd4287SBarry Smith @*/ 115370e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1154b4fd4287SBarry Smith { 11553a40ed3dSBarry Smith PetscFunctionBegin; 11564482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1157b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1158b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1159e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 116070e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 11613a40ed3dSBarry Smith PetscFunctionReturn(0); 1162b4fd4287SBarry Smith } 1163b4fd4287SBarry Smith 11649b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 116563dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 11669b94acceSBarry Smith 11674a2ae208SSatish Balay #undef __FUNCT__ 11684a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 11699b94acceSBarry Smith /*@ 11709b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1171272ac6f2SLois Curfman McInnes of a nonlinear solver. 11729b94acceSBarry Smith 1173fee21e36SBarry Smith Collective on SNES 1174fee21e36SBarry Smith 1175c7afd0dbSLois Curfman McInnes Input Parameters: 117670e92668SMatthew Knepley . snes - the SNES context 1177c7afd0dbSLois Curfman McInnes 1178272ac6f2SLois Curfman McInnes Notes: 1179272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1180272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1181272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1182272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1183272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1184272ac6f2SLois Curfman McInnes 118536851e7fSLois Curfman McInnes Level: advanced 118636851e7fSLois Curfman McInnes 11879b94acceSBarry Smith .keywords: SNES, nonlinear, setup 11889b94acceSBarry Smith 11899b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 11909b94acceSBarry Smith @*/ 119170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes) 11929b94acceSBarry Smith { 1193dfbe8321SBarry Smith PetscErrorCode ierr; 119471f87433Sdalcinl PetscTruth flg; 11953a40ed3dSBarry Smith 11963a40ed3dSBarry Smith PetscFunctionBegin; 11974482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11984dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 11999b94acceSBarry Smith 12007adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 120185385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 120285385478SLisandro Dalcin } 120385385478SLisandro Dalcin 12047adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1205c1f60f51SBarry Smith /* 1206c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1207dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1208c1f60f51SBarry Smith */ 1209c1f60f51SBarry Smith if (flg) { 1210c1f60f51SBarry Smith Mat J; 1211fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12123ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 1213ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12143a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 12153a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1216c1f60f51SBarry Smith } 121745fc7adcSBarry Smith 121803c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT) 12197adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr); 122045fc7adcSBarry Smith if (flg) { 122145fc7adcSBarry Smith Mat J; 122245fc7adcSBarry Smith ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr); 122375396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr); 122445fc7adcSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 122545fc7adcSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 122645fc7adcSBarry Smith } 122732a4b47aSMatthew Knepley #endif 122845fc7adcSBarry Smith 12297adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1230c1f60f51SBarry Smith /* 1231dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1232c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1233c1f60f51SBarry Smith */ 123431615d04SLois Curfman McInnes if (flg) { 1235272ac6f2SLois Curfman McInnes Mat J; 1236b5d62d44SBarry Smith KSP ksp; 123794b7f48cSBarry Smith PC pc; 123875396ef9SLisandro Dalcin /* create and set matrix-free operator */ 1239fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12403ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 124175396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12423ec795f1SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 12433a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1244f3ef73ceSBarry Smith /* force no preconditioner */ 124594b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 1246b5d62d44SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 1247a9815358SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 1248a9815358SBarry Smith if (!flg) { 124975396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 1250f3ef73ceSBarry Smith ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 1251272ac6f2SLois Curfman McInnes } 1252a9815358SBarry Smith } 1253f3ef73ceSBarry Smith 125485385478SLisandro Dalcin if (!snes->vec_func && !snes->vec_rhs) { 12551096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12561096aae1SMatthew Knepley } 125785385478SLisandro Dalcin if (!snes->ops->computefunction && !snes->vec_rhs) { 12581096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12591096aae1SMatthew Knepley } 126075396ef9SLisandro Dalcin if (!snes->jacobian) { 126175396ef9SLisandro Dalcin SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 126275396ef9SLisandro Dalcin } 1263a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 126429bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1265a8c6a408SBarry Smith } 1266a703fe33SLois Curfman McInnes 1267410397dcSLisandro Dalcin if (snes->ops->setup) { 1268410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1269410397dcSLisandro Dalcin } 12707aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 12713a40ed3dSBarry Smith PetscFunctionReturn(0); 12729b94acceSBarry Smith } 12739b94acceSBarry Smith 12744a2ae208SSatish Balay #undef __FUNCT__ 12754a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 127652baeb72SSatish Balay /*@ 12779b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 12789b94acceSBarry Smith with SNESCreate(). 12799b94acceSBarry Smith 1280c7afd0dbSLois Curfman McInnes Collective on SNES 1281c7afd0dbSLois Curfman McInnes 12829b94acceSBarry Smith Input Parameter: 12839b94acceSBarry Smith . snes - the SNES context 12849b94acceSBarry Smith 128536851e7fSLois Curfman McInnes Level: beginner 128636851e7fSLois Curfman McInnes 12879b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 12889b94acceSBarry Smith 128963a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 12909b94acceSBarry Smith @*/ 129163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes) 12929b94acceSBarry Smith { 12936849ba73SBarry Smith PetscErrorCode ierr; 12943a40ed3dSBarry Smith 12953a40ed3dSBarry Smith PetscFunctionBegin; 12964482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 12977adad957SLisandro Dalcin if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0); 1298d4bb536fSBarry Smith 1299be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 13000f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1301e7788613SBarry Smith if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);} 130285385478SLisandro Dalcin 130385385478SLisandro Dalcin if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);} 130485385478SLisandro Dalcin if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);} 130585385478SLisandro Dalcin if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);} 13063a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 13073a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 130894b7f48cSBarry Smith ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr); 130985385478SLisandro Dalcin ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr); 1310522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1311a6570f20SBarry Smith ierr = SNESMonitorCancel(snes);CHKERRQ(ierr); 13127f7931b9SBarry Smith if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);} 1313a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 13143a40ed3dSBarry Smith PetscFunctionReturn(0); 13159b94acceSBarry Smith } 13169b94acceSBarry Smith 13179b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 13189b94acceSBarry Smith 13194a2ae208SSatish Balay #undef __FUNCT__ 13204a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 13219b94acceSBarry Smith /*@ 1322d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 13239b94acceSBarry Smith 1324c7afd0dbSLois Curfman McInnes Collective on SNES 1325c7afd0dbSLois Curfman McInnes 13269b94acceSBarry Smith Input Parameters: 1327c7afd0dbSLois Curfman McInnes + snes - the SNES context 132870441072SBarry Smith . abstol - absolute convergence tolerance 132933174efeSLois Curfman McInnes . rtol - relative convergence tolerance 133033174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 133133174efeSLois Curfman McInnes of the change in the solution between steps 133233174efeSLois Curfman McInnes . maxit - maximum number of iterations 1333c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1334fee21e36SBarry Smith 133533174efeSLois Curfman McInnes Options Database Keys: 133670441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1337c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1338c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1339c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1340c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 13419b94acceSBarry Smith 1342d7a720efSLois Curfman McInnes Notes: 13439b94acceSBarry Smith The default maximum number of iterations is 50. 13449b94acceSBarry Smith The default maximum number of function evaluations is 1000. 13459b94acceSBarry Smith 134636851e7fSLois Curfman McInnes Level: intermediate 134736851e7fSLois Curfman McInnes 134833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 13499b94acceSBarry Smith 13502492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 13519b94acceSBarry Smith @*/ 135263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 13539b94acceSBarry Smith { 13543a40ed3dSBarry Smith PetscFunctionBegin; 13554482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 135670441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1357d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1358d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1359d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1360d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 13613a40ed3dSBarry Smith PetscFunctionReturn(0); 13629b94acceSBarry Smith } 13639b94acceSBarry Smith 13644a2ae208SSatish Balay #undef __FUNCT__ 13654a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 13669b94acceSBarry Smith /*@ 136733174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 136833174efeSLois Curfman McInnes 1369c7afd0dbSLois Curfman McInnes Not Collective 1370c7afd0dbSLois Curfman McInnes 137133174efeSLois Curfman McInnes Input Parameters: 1372c7afd0dbSLois Curfman McInnes + snes - the SNES context 137385385478SLisandro Dalcin . atol - absolute convergence tolerance 137433174efeSLois Curfman McInnes . rtol - relative convergence tolerance 137533174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 137633174efeSLois Curfman McInnes of the change in the solution between steps 137733174efeSLois Curfman McInnes . maxit - maximum number of iterations 1378c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1379fee21e36SBarry Smith 138033174efeSLois Curfman McInnes Notes: 138133174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 138233174efeSLois Curfman McInnes 138336851e7fSLois Curfman McInnes Level: intermediate 138436851e7fSLois Curfman McInnes 138533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 138633174efeSLois Curfman McInnes 138733174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 138833174efeSLois Curfman McInnes @*/ 138985385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 139033174efeSLois Curfman McInnes { 13913a40ed3dSBarry Smith PetscFunctionBegin; 13924482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 139385385478SLisandro Dalcin if (atol) *atol = snes->abstol; 139433174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 139533174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 139633174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 139733174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 13983a40ed3dSBarry Smith PetscFunctionReturn(0); 139933174efeSLois Curfman McInnes } 140033174efeSLois Curfman McInnes 14014a2ae208SSatish Balay #undef __FUNCT__ 14024a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 140333174efeSLois Curfman McInnes /*@ 14049b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 14059b94acceSBarry Smith 1406fee21e36SBarry Smith Collective on SNES 1407fee21e36SBarry Smith 1408c7afd0dbSLois Curfman McInnes Input Parameters: 1409c7afd0dbSLois Curfman McInnes + snes - the SNES context 1410c7afd0dbSLois Curfman McInnes - tol - tolerance 1411c7afd0dbSLois Curfman McInnes 14129b94acceSBarry Smith Options Database Key: 1413c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 14149b94acceSBarry Smith 141536851e7fSLois Curfman McInnes Level: intermediate 141636851e7fSLois Curfman McInnes 14179b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 14189b94acceSBarry Smith 14192492ecdbSBarry Smith .seealso: SNESSetTolerances() 14209b94acceSBarry Smith @*/ 142163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 14229b94acceSBarry Smith { 14233a40ed3dSBarry Smith PetscFunctionBegin; 14244482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 14259b94acceSBarry Smith snes->deltatol = tol; 14263a40ed3dSBarry Smith PetscFunctionReturn(0); 14279b94acceSBarry Smith } 14289b94acceSBarry Smith 1429df9fa365SBarry Smith /* 1430df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1431df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1432df9fa365SBarry Smith macros instead of functions 1433df9fa365SBarry Smith */ 14344a2ae208SSatish Balay #undef __FUNCT__ 1435a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 1436a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1437ce1608b8SBarry Smith { 1438dfbe8321SBarry Smith PetscErrorCode ierr; 1439ce1608b8SBarry Smith 1440ce1608b8SBarry Smith PetscFunctionBegin; 14414482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1442a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1443ce1608b8SBarry Smith PetscFunctionReturn(0); 1444ce1608b8SBarry Smith } 1445ce1608b8SBarry Smith 14464a2ae208SSatish Balay #undef __FUNCT__ 1447a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 1448a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1449df9fa365SBarry Smith { 1450dfbe8321SBarry Smith PetscErrorCode ierr; 1451df9fa365SBarry Smith 1452df9fa365SBarry Smith PetscFunctionBegin; 1453a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1454df9fa365SBarry Smith PetscFunctionReturn(0); 1455df9fa365SBarry Smith } 1456df9fa365SBarry Smith 14574a2ae208SSatish Balay #undef __FUNCT__ 1458a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 1459a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw) 1460df9fa365SBarry Smith { 1461dfbe8321SBarry Smith PetscErrorCode ierr; 1462df9fa365SBarry Smith 1463df9fa365SBarry Smith PetscFunctionBegin; 1464a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1465df9fa365SBarry Smith PetscFunctionReturn(0); 1466df9fa365SBarry Smith } 1467df9fa365SBarry Smith 14689b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 14699b94acceSBarry Smith 14704a2ae208SSatish Balay #undef __FUNCT__ 1471a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 14729b94acceSBarry Smith /*@C 1473a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 14749b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 14759b94acceSBarry Smith progress. 14769b94acceSBarry Smith 1477fee21e36SBarry Smith Collective on SNES 1478fee21e36SBarry Smith 1479c7afd0dbSLois Curfman McInnes Input Parameters: 1480c7afd0dbSLois Curfman McInnes + snes - the SNES context 1481c7afd0dbSLois Curfman McInnes . func - monitoring routine 1482b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1483e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 1484b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1485b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 14869b94acceSBarry Smith 1487c7afd0dbSLois Curfman McInnes Calling sequence of func: 1488a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 1489c7afd0dbSLois Curfman McInnes 1490c7afd0dbSLois Curfman McInnes + snes - the SNES context 1491c7afd0dbSLois Curfman McInnes . its - iteration number 1492c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 149340a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 14949b94acceSBarry Smith 14959665c990SLois Curfman McInnes Options Database Keys: 1496a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 1497a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 1498a6570f20SBarry Smith uses SNESMonitorLGCreate() 1499a6570f20SBarry Smith _ -snes_monitor_cancel - cancels all monitors that have 1500c7afd0dbSLois Curfman McInnes been hardwired into a code by 1501a6570f20SBarry Smith calls to SNESMonitorSet(), but 1502c7afd0dbSLois Curfman McInnes does not cancel those set via 1503c7afd0dbSLois Curfman McInnes the options database. 15049665c990SLois Curfman McInnes 1505639f9d9dSBarry Smith Notes: 15066bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 1507a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 15086bc08f3fSLois Curfman McInnes order in which they were set. 1509639f9d9dSBarry Smith 1510025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 1511025f1a04SBarry Smith 151236851e7fSLois Curfman McInnes Level: intermediate 151336851e7fSLois Curfman McInnes 15149b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 15159b94acceSBarry Smith 1516a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 15179b94acceSBarry Smith @*/ 1518b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 15199b94acceSBarry Smith { 1520b90d0a6eSBarry Smith PetscInt i; 1521b90d0a6eSBarry Smith 15223a40ed3dSBarry Smith PetscFunctionBegin; 15234482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1524639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 152529bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1526639f9d9dSBarry Smith } 1527b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 1528b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 1529b90d0a6eSBarry Smith 1530b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 1531b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 1532b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 1533b90d0a6eSBarry Smith PetscErrorCode ierr; 1534b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 1535b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 1536b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 1537b90d0a6eSBarry Smith ierr = (*monitordestroy)(mctx);CHKERRQ(ierr); 1538b90d0a6eSBarry Smith PetscFunctionReturn(0); 1539b90d0a6eSBarry Smith } 1540b90d0a6eSBarry Smith } 1541b90d0a6eSBarry Smith } 1542b90d0a6eSBarry Smith } 1543b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 1544b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1545639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 15463a40ed3dSBarry Smith PetscFunctionReturn(0); 15479b94acceSBarry Smith } 15489b94acceSBarry Smith 15494a2ae208SSatish Balay #undef __FUNCT__ 1550a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 15515cd90555SBarry Smith /*@C 1552a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 15535cd90555SBarry Smith 1554c7afd0dbSLois Curfman McInnes Collective on SNES 1555c7afd0dbSLois Curfman McInnes 15565cd90555SBarry Smith Input Parameters: 15575cd90555SBarry Smith . snes - the SNES context 15585cd90555SBarry Smith 15591a480d89SAdministrator Options Database Key: 1560a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 1561a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 1562c7afd0dbSLois Curfman McInnes set via the options database 15635cd90555SBarry Smith 15645cd90555SBarry Smith Notes: 15655cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 15665cd90555SBarry Smith 156736851e7fSLois Curfman McInnes Level: intermediate 156836851e7fSLois Curfman McInnes 15695cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 15705cd90555SBarry Smith 1571a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 15725cd90555SBarry Smith @*/ 1573a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes) 15745cd90555SBarry Smith { 1575d952e501SBarry Smith PetscErrorCode ierr; 1576d952e501SBarry Smith PetscInt i; 1577d952e501SBarry Smith 15785cd90555SBarry Smith PetscFunctionBegin; 15794482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1580d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1581d952e501SBarry Smith if (snes->monitordestroy[i]) { 1582d952e501SBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1583d952e501SBarry Smith } 1584d952e501SBarry Smith } 15855cd90555SBarry Smith snes->numbermonitors = 0; 15865cd90555SBarry Smith PetscFunctionReturn(0); 15875cd90555SBarry Smith } 15885cd90555SBarry Smith 15894a2ae208SSatish Balay #undef __FUNCT__ 15904a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 15919b94acceSBarry Smith /*@C 15929b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 15939b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 15949b94acceSBarry Smith 1595fee21e36SBarry Smith Collective on SNES 1596fee21e36SBarry Smith 1597c7afd0dbSLois Curfman McInnes Input Parameters: 1598c7afd0dbSLois Curfman McInnes + snes - the SNES context 1599c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 16007f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 16017f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 16029b94acceSBarry Smith 1603c7afd0dbSLois Curfman McInnes Calling sequence of func: 160406ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1605c7afd0dbSLois Curfman McInnes 1606c7afd0dbSLois Curfman McInnes + snes - the SNES context 160706ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 1608c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1609184914b5SBarry Smith . reason - reason for convergence/divergence 1610c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 16114b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 16124b27c08aSLois Curfman McInnes - f - 2-norm of function 16139b94acceSBarry Smith 161436851e7fSLois Curfman McInnes Level: advanced 161536851e7fSLois Curfman McInnes 16169b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 16179b94acceSBarry Smith 161885385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 16199b94acceSBarry Smith @*/ 16207f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 16219b94acceSBarry Smith { 16227f7931b9SBarry Smith PetscErrorCode ierr; 16237f7931b9SBarry Smith 16243a40ed3dSBarry Smith PetscFunctionBegin; 16254482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 162685385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 16277f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 16287f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 16297f7931b9SBarry Smith } 163085385478SLisandro Dalcin snes->ops->converged = func; 16317f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 163285385478SLisandro Dalcin snes->cnvP = cctx; 16333a40ed3dSBarry Smith PetscFunctionReturn(0); 16349b94acceSBarry Smith } 16359b94acceSBarry Smith 16364a2ae208SSatish Balay #undef __FUNCT__ 16374a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 163852baeb72SSatish Balay /*@ 1639184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1640184914b5SBarry Smith 1641184914b5SBarry Smith Not Collective 1642184914b5SBarry Smith 1643184914b5SBarry Smith Input Parameter: 1644184914b5SBarry Smith . snes - the SNES context 1645184914b5SBarry Smith 1646184914b5SBarry Smith Output Parameter: 1647e090d566SSatish Balay . reason - negative value indicates diverged, positive value converged, see petscsnes.h or the 1648184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1649184914b5SBarry Smith 1650184914b5SBarry Smith Level: intermediate 1651184914b5SBarry Smith 1652184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1653184914b5SBarry Smith 1654184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1655184914b5SBarry Smith 165685385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 1657184914b5SBarry Smith @*/ 165863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1659184914b5SBarry Smith { 1660184914b5SBarry Smith PetscFunctionBegin; 16614482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16624482741eSBarry Smith PetscValidPointer(reason,2); 1663184914b5SBarry Smith *reason = snes->reason; 1664184914b5SBarry Smith PetscFunctionReturn(0); 1665184914b5SBarry Smith } 1666184914b5SBarry Smith 16674a2ae208SSatish Balay #undef __FUNCT__ 16684a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 1669c9005455SLois Curfman McInnes /*@ 1670c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1671c9005455SLois Curfman McInnes 1672fee21e36SBarry Smith Collective on SNES 1673fee21e36SBarry Smith 1674c7afd0dbSLois Curfman McInnes Input Parameters: 1675c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1676c7afd0dbSLois Curfman McInnes . a - array to hold history 1677cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 1678758f92a0SBarry Smith . na - size of a and its 167964731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 1680758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1681c7afd0dbSLois Curfman McInnes 1682c9005455SLois Curfman McInnes Notes: 16834b27c08aSLois Curfman McInnes If set, this array will contain the function norms computed 1684c9005455SLois Curfman McInnes at each step. 1685c9005455SLois Curfman McInnes 1686c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1687c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1688c9005455SLois Curfman McInnes during the section of code that is being timed. 1689c9005455SLois Curfman McInnes 169036851e7fSLois Curfman McInnes Level: intermediate 169136851e7fSLois Curfman McInnes 1692c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1693758f92a0SBarry Smith 169408405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1695758f92a0SBarry Smith 1696c9005455SLois Curfman McInnes @*/ 1697a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset) 1698c9005455SLois Curfman McInnes { 16993a40ed3dSBarry Smith PetscFunctionBegin; 17004482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 17014482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 1702a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 1703c9005455SLois Curfman McInnes snes->conv_hist = a; 1704758f92a0SBarry Smith snes->conv_hist_its = its; 1705758f92a0SBarry Smith snes->conv_hist_max = na; 1706a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 1707758f92a0SBarry Smith snes->conv_hist_reset = reset; 1708758f92a0SBarry Smith PetscFunctionReturn(0); 1709758f92a0SBarry Smith } 1710758f92a0SBarry Smith 17114a2ae208SSatish Balay #undef __FUNCT__ 17124a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 17130c4c9dddSBarry Smith /*@C 1714758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1715758f92a0SBarry Smith 1716758f92a0SBarry Smith Collective on SNES 1717758f92a0SBarry Smith 1718758f92a0SBarry Smith Input Parameter: 1719758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1720758f92a0SBarry Smith 1721758f92a0SBarry Smith Output Parameters: 1722758f92a0SBarry Smith . a - array to hold history 1723758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1724758f92a0SBarry Smith negative if not converged) for each solve. 1725758f92a0SBarry Smith - na - size of a and its 1726758f92a0SBarry Smith 1727758f92a0SBarry Smith Notes: 1728758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1729758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1730758f92a0SBarry Smith 1731758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1732758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1733758f92a0SBarry Smith during the section of code that is being timed. 1734758f92a0SBarry Smith 1735758f92a0SBarry Smith Level: intermediate 1736758f92a0SBarry Smith 1737758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1738758f92a0SBarry Smith 1739758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1740758f92a0SBarry Smith 1741758f92a0SBarry Smith @*/ 174263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 1743758f92a0SBarry Smith { 1744758f92a0SBarry Smith PetscFunctionBegin; 17454482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1746758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1747758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1748758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 17493a40ed3dSBarry Smith PetscFunctionReturn(0); 1750c9005455SLois Curfman McInnes } 1751c9005455SLois Curfman McInnes 1752e74ef692SMatthew Knepley #undef __FUNCT__ 1753e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 1754ac226902SBarry Smith /*@C 175576b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 17567e4bb74cSBarry Smith at the beginning o every iteration of the nonlinear solve. Specifically 17577e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 175876b2cf59SMatthew Knepley 175976b2cf59SMatthew Knepley Collective on SNES 176076b2cf59SMatthew Knepley 176176b2cf59SMatthew Knepley Input Parameters: 176276b2cf59SMatthew Knepley . snes - The nonlinear solver context 176376b2cf59SMatthew Knepley . func - The function 176476b2cf59SMatthew Knepley 176576b2cf59SMatthew Knepley Calling sequence of func: 1766b5d30489SBarry Smith . func (SNES snes, PetscInt step); 176776b2cf59SMatthew Knepley 176876b2cf59SMatthew Knepley . step - The current step of the iteration 176976b2cf59SMatthew Knepley 177076b2cf59SMatthew Knepley Level: intermediate 177176b2cf59SMatthew Knepley 177276b2cf59SMatthew Knepley .keywords: SNES, update 1773b5d30489SBarry Smith 177485385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 177576b2cf59SMatthew Knepley @*/ 177663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 177776b2cf59SMatthew Knepley { 177876b2cf59SMatthew Knepley PetscFunctionBegin; 17794482741eSBarry Smith PetscValidHeaderSpecific(snes, SNES_COOKIE,1); 1780e7788613SBarry Smith snes->ops->update = func; 178176b2cf59SMatthew Knepley PetscFunctionReturn(0); 178276b2cf59SMatthew Knepley } 178376b2cf59SMatthew Knepley 1784e74ef692SMatthew Knepley #undef __FUNCT__ 1785e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 178676b2cf59SMatthew Knepley /*@ 178776b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 178876b2cf59SMatthew Knepley 178976b2cf59SMatthew Knepley Not collective 179076b2cf59SMatthew Knepley 179176b2cf59SMatthew Knepley Input Parameters: 179276b2cf59SMatthew Knepley . snes - The nonlinear solver context 179376b2cf59SMatthew Knepley . step - The current step of the iteration 179476b2cf59SMatthew Knepley 1795205452f4SMatthew Knepley Level: intermediate 1796205452f4SMatthew Knepley 179776b2cf59SMatthew Knepley .keywords: SNES, update 1798a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 179976b2cf59SMatthew Knepley @*/ 180063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step) 180176b2cf59SMatthew Knepley { 180276b2cf59SMatthew Knepley PetscFunctionBegin; 180376b2cf59SMatthew Knepley PetscFunctionReturn(0); 180476b2cf59SMatthew Knepley } 180576b2cf59SMatthew Knepley 18064a2ae208SSatish Balay #undef __FUNCT__ 18074a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 18089b94acceSBarry Smith /* 18099b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 18109b94acceSBarry Smith positive parameter delta. 18119b94acceSBarry Smith 18129b94acceSBarry Smith Input Parameters: 1813c7afd0dbSLois Curfman McInnes + snes - the SNES context 18149b94acceSBarry Smith . y - approximate solution of linear system 18159b94acceSBarry Smith . fnorm - 2-norm of current function 1816c7afd0dbSLois Curfman McInnes - delta - trust region size 18179b94acceSBarry Smith 18189b94acceSBarry Smith Output Parameters: 1819c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 18209b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 18219b94acceSBarry Smith region, and exceeds zero otherwise. 1822c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 18239b94acceSBarry Smith 18249b94acceSBarry Smith Note: 18254b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 18269b94acceSBarry Smith is set to be the maximum allowable step size. 18279b94acceSBarry Smith 18289b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 18299b94acceSBarry Smith */ 1830dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 18319b94acceSBarry Smith { 1832064f8208SBarry Smith PetscReal nrm; 1833ea709b57SSatish Balay PetscScalar cnorm; 1834dfbe8321SBarry Smith PetscErrorCode ierr; 18353a40ed3dSBarry Smith 18363a40ed3dSBarry Smith PetscFunctionBegin; 18374482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 18384482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,2); 1839c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 1840184914b5SBarry Smith 1841064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 1842064f8208SBarry Smith if (nrm > *delta) { 1843064f8208SBarry Smith nrm = *delta/nrm; 1844064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 1845064f8208SBarry Smith cnorm = nrm; 18462dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 18479b94acceSBarry Smith *ynorm = *delta; 18489b94acceSBarry Smith } else { 18499b94acceSBarry Smith *gpnorm = 0.0; 1850064f8208SBarry Smith *ynorm = nrm; 18519b94acceSBarry Smith } 18523a40ed3dSBarry Smith PetscFunctionReturn(0); 18539b94acceSBarry Smith } 18549b94acceSBarry Smith 18554a2ae208SSatish Balay #undef __FUNCT__ 18564a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 18576ce558aeSBarry Smith /*@C 1858f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 1859f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 18609b94acceSBarry Smith 1861c7afd0dbSLois Curfman McInnes Collective on SNES 1862c7afd0dbSLois Curfman McInnes 1863b2002411SLois Curfman McInnes Input Parameters: 1864c7afd0dbSLois Curfman McInnes + snes - the SNES context 1865f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 186685385478SLisandro Dalcin - x - the solution vector. 18679b94acceSBarry Smith 1868b2002411SLois Curfman McInnes Notes: 18698ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 18708ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 18718ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 18728ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 18738ddd3da0SLois Curfman McInnes 187436851e7fSLois Curfman McInnes Level: beginner 187536851e7fSLois Curfman McInnes 18769b94acceSBarry Smith .keywords: SNES, nonlinear, solve 18779b94acceSBarry Smith 187885385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 18799b94acceSBarry Smith @*/ 1880f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x) 18819b94acceSBarry Smith { 1882dfbe8321SBarry Smith PetscErrorCode ierr; 1883f1af5d2fSBarry Smith PetscTruth flg; 1884eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 1885eabae89aSBarry Smith PetscViewer viewer; 1886052efed2SBarry Smith 18873a40ed3dSBarry Smith PetscFunctionBegin; 18884482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1889f69a0ea3SMatthew Knepley PetscValidHeaderSpecific(x,VEC_COOKIE,3); 1890f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 189185385478SLisandro Dalcin if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2); 189285385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 189385385478SLisandro Dalcin 189485385478SLisandro Dalcin /* set solution vector */ 189585385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 189685385478SLisandro Dalcin if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); } 189785385478SLisandro Dalcin snes->vec_sol = x; 189885385478SLisandro Dalcin /* set afine vector if provided */ 189985385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 190085385478SLisandro Dalcin if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); } 190185385478SLisandro Dalcin snes->vec_rhs = b; 190285385478SLisandro Dalcin 190385385478SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 190485385478SLisandro Dalcin ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr); 190570e92668SMatthew Knepley } 19063f149594SLisandro Dalcin 190770e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 19083f149594SLisandro Dalcin 1909abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 191050ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 1911d5e45103SBarry Smith 19123f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 19134936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 191485385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 19154936397dSBarry Smith if (snes->domainerror){ 19164936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 19174936397dSBarry Smith snes->domainerror = PETSC_FALSE; 19184936397dSBarry Smith } 191985385478SLisandro Dalcin 19203f149594SLisandro Dalcin if (!snes->reason) { 19213f149594SLisandro Dalcin SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 19223f149594SLisandro Dalcin } 19233f149594SLisandro Dalcin 19247adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1925eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 19267adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 1927eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 1928eabae89aSBarry Smith ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 1929eabae89aSBarry Smith } 1930eabae89aSBarry Smith 19317adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr); 1932da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 19335968eb51SBarry Smith if (snes->printreason) { 19345968eb51SBarry Smith if (snes->reason > 0) { 19357adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 19365968eb51SBarry Smith } else { 19377adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 19385968eb51SBarry Smith } 19395968eb51SBarry Smith } 19405968eb51SBarry Smith 19413a40ed3dSBarry Smith PetscFunctionReturn(0); 19429b94acceSBarry Smith } 19439b94acceSBarry Smith 19449b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 19459b94acceSBarry Smith 19464a2ae208SSatish Balay #undef __FUNCT__ 19474a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 194882bf6240SBarry Smith /*@C 19494b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 19509b94acceSBarry Smith 1951fee21e36SBarry Smith Collective on SNES 1952fee21e36SBarry Smith 1953c7afd0dbSLois Curfman McInnes Input Parameters: 1954c7afd0dbSLois Curfman McInnes + snes - the SNES context 1955454a90a3SBarry Smith - type - a known method 1956c7afd0dbSLois Curfman McInnes 1957c7afd0dbSLois Curfman McInnes Options Database Key: 1958454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 1959c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 1960ae12b187SLois Curfman McInnes 19619b94acceSBarry Smith Notes: 1962e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 19634b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 1964c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 19654b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 1966c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 19679b94acceSBarry Smith 1968ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 1969ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 1970ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 1971ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 1972ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 1973ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 1974ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 1975ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 1976ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 1977b0a32e0cSBarry Smith appropriate method. 197836851e7fSLois Curfman McInnes 197936851e7fSLois Curfman McInnes Level: intermediate 1980a703fe33SLois Curfman McInnes 1981454a90a3SBarry Smith .keywords: SNES, set, type 1982435da068SBarry Smith 1983435da068SBarry Smith .seealso: SNESType, SNESCreate() 1984435da068SBarry Smith 19859b94acceSBarry Smith @*/ 1986a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type) 19879b94acceSBarry Smith { 1988dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 19896831982aSBarry Smith PetscTruth match; 19903a40ed3dSBarry Smith 19913a40ed3dSBarry Smith PetscFunctionBegin; 19924482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 19934482741eSBarry Smith PetscValidCharPointer(type,2); 199482bf6240SBarry Smith 19956831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 19960f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 199792ff6ae8SBarry Smith 19987adad957SLisandro Dalcin ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 1999958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 200075396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 200175396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 200275396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 200375396ef9SLisandro Dalcin snes->ops->setup = 0; 200475396ef9SLisandro Dalcin snes->ops->solve = 0; 200575396ef9SLisandro Dalcin snes->ops->view = 0; 200675396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 200775396ef9SLisandro Dalcin snes->ops->destroy = 0; 200875396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 200975396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 20103a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 2011454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 20123a40ed3dSBarry Smith PetscFunctionReturn(0); 20139b94acceSBarry Smith } 20149b94acceSBarry Smith 2015a847f771SSatish Balay 20169b94acceSBarry Smith /* --------------------------------------------------------------------- */ 20174a2ae208SSatish Balay #undef __FUNCT__ 20184a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 201952baeb72SSatish Balay /*@ 20209b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2021f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 20229b94acceSBarry Smith 2023fee21e36SBarry Smith Not Collective 2024fee21e36SBarry Smith 202536851e7fSLois Curfman McInnes Level: advanced 202636851e7fSLois Curfman McInnes 20279b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 20289b94acceSBarry Smith 20299b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 20309b94acceSBarry Smith @*/ 203163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void) 20329b94acceSBarry Smith { 2033dfbe8321SBarry Smith PetscErrorCode ierr; 203482bf6240SBarry Smith 20353a40ed3dSBarry Smith PetscFunctionBegin; 20361441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 20374c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 20383a40ed3dSBarry Smith PetscFunctionReturn(0); 20399b94acceSBarry Smith } 20409b94acceSBarry Smith 20414a2ae208SSatish Balay #undef __FUNCT__ 20424a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 20439b94acceSBarry Smith /*@C 20449a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 20459b94acceSBarry Smith 2046c7afd0dbSLois Curfman McInnes Not Collective 2047c7afd0dbSLois Curfman McInnes 20489b94acceSBarry Smith Input Parameter: 20494b0e389bSBarry Smith . snes - nonlinear solver context 20509b94acceSBarry Smith 20519b94acceSBarry Smith Output Parameter: 20523a7fca6bSBarry Smith . type - SNES method (a character string) 20539b94acceSBarry Smith 205436851e7fSLois Curfman McInnes Level: intermediate 205536851e7fSLois Curfman McInnes 2056454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 20579b94acceSBarry Smith @*/ 2058a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type) 20599b94acceSBarry Smith { 20603a40ed3dSBarry Smith PetscFunctionBegin; 20614482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20624482741eSBarry Smith PetscValidPointer(type,2); 20637adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 20643a40ed3dSBarry Smith PetscFunctionReturn(0); 20659b94acceSBarry Smith } 20669b94acceSBarry Smith 20674a2ae208SSatish Balay #undef __FUNCT__ 20684a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 206952baeb72SSatish Balay /*@ 20709b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 20719b94acceSBarry Smith stored. 20729b94acceSBarry Smith 2073c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2074c7afd0dbSLois Curfman McInnes 20759b94acceSBarry Smith Input Parameter: 20769b94acceSBarry Smith . snes - the SNES context 20779b94acceSBarry Smith 20789b94acceSBarry Smith Output Parameter: 20799b94acceSBarry Smith . x - the solution 20809b94acceSBarry Smith 208170e92668SMatthew Knepley Level: intermediate 208236851e7fSLois Curfman McInnes 20839b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 20849b94acceSBarry Smith 208585385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 20869b94acceSBarry Smith @*/ 208763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x) 20889b94acceSBarry Smith { 20893a40ed3dSBarry Smith PetscFunctionBegin; 20904482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20914482741eSBarry Smith PetscValidPointer(x,2); 209285385478SLisandro Dalcin *x = snes->vec_sol; 209370e92668SMatthew Knepley PetscFunctionReturn(0); 209470e92668SMatthew Knepley } 209570e92668SMatthew Knepley 209670e92668SMatthew Knepley #undef __FUNCT__ 20974a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 209852baeb72SSatish Balay /*@ 20999b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 21009b94acceSBarry Smith stored. 21019b94acceSBarry Smith 2102c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2103c7afd0dbSLois Curfman McInnes 21049b94acceSBarry Smith Input Parameter: 21059b94acceSBarry Smith . snes - the SNES context 21069b94acceSBarry Smith 21079b94acceSBarry Smith Output Parameter: 21089b94acceSBarry Smith . x - the solution update 21099b94acceSBarry Smith 211036851e7fSLois Curfman McInnes Level: advanced 211136851e7fSLois Curfman McInnes 21129b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 21139b94acceSBarry Smith 211485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 21159b94acceSBarry Smith @*/ 211663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x) 21179b94acceSBarry Smith { 21183a40ed3dSBarry Smith PetscFunctionBegin; 21194482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 21204482741eSBarry Smith PetscValidPointer(x,2); 212185385478SLisandro Dalcin *x = snes->vec_sol_update; 21223a40ed3dSBarry Smith PetscFunctionReturn(0); 21239b94acceSBarry Smith } 21249b94acceSBarry Smith 21254a2ae208SSatish Balay #undef __FUNCT__ 21264a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 21279b94acceSBarry Smith /*@C 21283638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 21299b94acceSBarry Smith 2130c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2131c7afd0dbSLois Curfman McInnes 21329b94acceSBarry Smith Input Parameter: 21339b94acceSBarry Smith . snes - the SNES context 21349b94acceSBarry Smith 21359b94acceSBarry Smith Output Parameter: 21367bf4e008SBarry Smith + r - the function (or PETSC_NULL) 213770e92668SMatthew Knepley . func - the function (or PETSC_NULL) 213870e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 21399b94acceSBarry Smith 214036851e7fSLois Curfman McInnes Level: advanced 214136851e7fSLois Curfman McInnes 2142a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 21439b94acceSBarry Smith 21444b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 21459b94acceSBarry Smith @*/ 214670e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 21479b94acceSBarry Smith { 21483a40ed3dSBarry Smith PetscFunctionBegin; 21494482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 215085385478SLisandro Dalcin if (r) *r = snes->vec_func; 2151e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 215270e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 21533a40ed3dSBarry Smith PetscFunctionReturn(0); 21549b94acceSBarry Smith } 21559b94acceSBarry Smith 21564a2ae208SSatish Balay #undef __FUNCT__ 21574a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 21583c7409f5SSatish Balay /*@C 21593c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2160d850072dSLois Curfman McInnes SNES options in the database. 21613c7409f5SSatish Balay 2162fee21e36SBarry Smith Collective on SNES 2163fee21e36SBarry Smith 2164c7afd0dbSLois Curfman McInnes Input Parameter: 2165c7afd0dbSLois Curfman McInnes + snes - the SNES context 2166c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2167c7afd0dbSLois Curfman McInnes 2168d850072dSLois Curfman McInnes Notes: 2169a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2170c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2171d850072dSLois Curfman McInnes 217236851e7fSLois Curfman McInnes Level: advanced 217336851e7fSLois Curfman McInnes 21743c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2175a86d99e1SLois Curfman McInnes 2176a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 21773c7409f5SSatish Balay @*/ 217863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[]) 21793c7409f5SSatish Balay { 2180dfbe8321SBarry Smith PetscErrorCode ierr; 21813c7409f5SSatish Balay 21823a40ed3dSBarry Smith PetscFunctionBegin; 21834482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2184639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 218594b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 21863a40ed3dSBarry Smith PetscFunctionReturn(0); 21873c7409f5SSatish Balay } 21883c7409f5SSatish Balay 21894a2ae208SSatish Balay #undef __FUNCT__ 21904a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 21913c7409f5SSatish Balay /*@C 2192f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2193d850072dSLois Curfman McInnes SNES options in the database. 21943c7409f5SSatish Balay 2195fee21e36SBarry Smith Collective on SNES 2196fee21e36SBarry Smith 2197c7afd0dbSLois Curfman McInnes Input Parameters: 2198c7afd0dbSLois Curfman McInnes + snes - the SNES context 2199c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2200c7afd0dbSLois Curfman McInnes 2201d850072dSLois Curfman McInnes Notes: 2202a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2203c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2204d850072dSLois Curfman McInnes 220536851e7fSLois Curfman McInnes Level: advanced 220636851e7fSLois Curfman McInnes 22073c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2208a86d99e1SLois Curfman McInnes 2209a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 22103c7409f5SSatish Balay @*/ 221163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 22123c7409f5SSatish Balay { 2213dfbe8321SBarry Smith PetscErrorCode ierr; 22143c7409f5SSatish Balay 22153a40ed3dSBarry Smith PetscFunctionBegin; 22164482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2217639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 221894b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 22193a40ed3dSBarry Smith PetscFunctionReturn(0); 22203c7409f5SSatish Balay } 22213c7409f5SSatish Balay 22224a2ae208SSatish Balay #undef __FUNCT__ 22234a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 22249ab63eb5SSatish Balay /*@C 22253c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 22263c7409f5SSatish Balay SNES options in the database. 22273c7409f5SSatish Balay 2228c7afd0dbSLois Curfman McInnes Not Collective 2229c7afd0dbSLois Curfman McInnes 22303c7409f5SSatish Balay Input Parameter: 22313c7409f5SSatish Balay . snes - the SNES context 22323c7409f5SSatish Balay 22333c7409f5SSatish Balay Output Parameter: 22343c7409f5SSatish Balay . prefix - pointer to the prefix string used 22353c7409f5SSatish Balay 22364ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 22379ab63eb5SSatish Balay sufficient length to hold the prefix. 22389ab63eb5SSatish Balay 223936851e7fSLois Curfman McInnes Level: advanced 224036851e7fSLois Curfman McInnes 22413c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2242a86d99e1SLois Curfman McInnes 2243a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 22443c7409f5SSatish Balay @*/ 2245e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 22463c7409f5SSatish Balay { 2247dfbe8321SBarry Smith PetscErrorCode ierr; 22483c7409f5SSatish Balay 22493a40ed3dSBarry Smith PetscFunctionBegin; 22504482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2251639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 22523a40ed3dSBarry Smith PetscFunctionReturn(0); 22533c7409f5SSatish Balay } 22543c7409f5SSatish Balay 2255b2002411SLois Curfman McInnes 22564a2ae208SSatish Balay #undef __FUNCT__ 22574a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 22583cea93caSBarry Smith /*@C 22593cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 22603cea93caSBarry Smith 22617f6c08e0SMatthew Knepley Level: advanced 22623cea93caSBarry Smith @*/ 226363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2264b2002411SLois Curfman McInnes { 2265e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2266dfbe8321SBarry Smith PetscErrorCode ierr; 2267b2002411SLois Curfman McInnes 2268b2002411SLois Curfman McInnes PetscFunctionBegin; 2269b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2270c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2271b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2272b2002411SLois Curfman McInnes } 2273da9b6338SBarry Smith 2274da9b6338SBarry Smith #undef __FUNCT__ 2275da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 227663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes) 2277da9b6338SBarry Smith { 2278dfbe8321SBarry Smith PetscErrorCode ierr; 227977431f27SBarry Smith PetscInt N,i,j; 2280da9b6338SBarry Smith Vec u,uh,fh; 2281da9b6338SBarry Smith PetscScalar value; 2282da9b6338SBarry Smith PetscReal norm; 2283da9b6338SBarry Smith 2284da9b6338SBarry Smith PetscFunctionBegin; 2285da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 2286da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 2287da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 2288da9b6338SBarry Smith 2289da9b6338SBarry Smith /* currently only works for sequential */ 2290da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 2291da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 2292da9b6338SBarry Smith for (i=0; i<N; i++) { 2293da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 229477431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 2295da9b6338SBarry Smith for (j=-10; j<11; j++) { 2296ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 2297da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 22983ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 2299da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 230077431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 2301da9b6338SBarry Smith value = -value; 2302da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 2303da9b6338SBarry Smith } 2304da9b6338SBarry Smith } 2305da9b6338SBarry Smith ierr = VecDestroy(uh);CHKERRQ(ierr); 2306da9b6338SBarry Smith ierr = VecDestroy(fh);CHKERRQ(ierr); 2307da9b6338SBarry Smith PetscFunctionReturn(0); 2308da9b6338SBarry Smith } 230971f87433Sdalcinl 231071f87433Sdalcinl #undef __FUNCT__ 2311fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 231271f87433Sdalcinl /*@ 2313fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 231471f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 231571f87433Sdalcinl Newton method. 231671f87433Sdalcinl 231771f87433Sdalcinl Collective on SNES 231871f87433Sdalcinl 231971f87433Sdalcinl Input Parameters: 232071f87433Sdalcinl + snes - SNES context 232171f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 232271f87433Sdalcinl 232371f87433Sdalcinl Notes: 232471f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 232571f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 232671f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 232771f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 232871f87433Sdalcinl solver. 232971f87433Sdalcinl 233071f87433Sdalcinl Level: advanced 233171f87433Sdalcinl 233271f87433Sdalcinl Reference: 233371f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 233471f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 233571f87433Sdalcinl 233671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 233771f87433Sdalcinl 2338fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 233971f87433Sdalcinl @*/ 2340fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag) 234171f87433Sdalcinl { 234271f87433Sdalcinl PetscFunctionBegin; 234371f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 234471f87433Sdalcinl snes->ksp_ewconv = flag; 234571f87433Sdalcinl PetscFunctionReturn(0); 234671f87433Sdalcinl } 234771f87433Sdalcinl 234871f87433Sdalcinl #undef __FUNCT__ 2349fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 235071f87433Sdalcinl /*@ 2351fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 235271f87433Sdalcinl for computing relative tolerance for linear solvers within an 235371f87433Sdalcinl inexact Newton method. 235471f87433Sdalcinl 235571f87433Sdalcinl Not Collective 235671f87433Sdalcinl 235771f87433Sdalcinl Input Parameter: 235871f87433Sdalcinl . snes - SNES context 235971f87433Sdalcinl 236071f87433Sdalcinl Output Parameter: 236171f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 236271f87433Sdalcinl 236371f87433Sdalcinl Notes: 236471f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 236571f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 236671f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 236771f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 236871f87433Sdalcinl solver. 236971f87433Sdalcinl 237071f87433Sdalcinl Level: advanced 237171f87433Sdalcinl 237271f87433Sdalcinl Reference: 237371f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 237471f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 237571f87433Sdalcinl 237671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 237771f87433Sdalcinl 2378fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 237971f87433Sdalcinl @*/ 2380fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag) 238171f87433Sdalcinl { 238271f87433Sdalcinl PetscFunctionBegin; 238371f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 238471f87433Sdalcinl PetscValidPointer(flag,2); 238571f87433Sdalcinl *flag = snes->ksp_ewconv; 238671f87433Sdalcinl PetscFunctionReturn(0); 238771f87433Sdalcinl } 238871f87433Sdalcinl 238971f87433Sdalcinl #undef __FUNCT__ 2390fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 239171f87433Sdalcinl /*@ 2392fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 239371f87433Sdalcinl convergence criteria for the linear solvers within an inexact 239471f87433Sdalcinl Newton method. 239571f87433Sdalcinl 239671f87433Sdalcinl Collective on SNES 239771f87433Sdalcinl 239871f87433Sdalcinl Input Parameters: 239971f87433Sdalcinl + snes - SNES context 240071f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 240171f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 240271f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 240371f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 240471f87433Sdalcinl (0 <= gamma2 <= 1) 240571f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 240671f87433Sdalcinl . alpha2 - power for safeguard 240771f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 240871f87433Sdalcinl 240971f87433Sdalcinl Note: 241071f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 241171f87433Sdalcinl 241271f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 241371f87433Sdalcinl 241471f87433Sdalcinl Level: advanced 241571f87433Sdalcinl 241671f87433Sdalcinl Reference: 241771f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 241871f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 241971f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 242071f87433Sdalcinl 242171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 242271f87433Sdalcinl 2423fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 242471f87433Sdalcinl @*/ 2425fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 242671f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 242771f87433Sdalcinl { 2428fa9f3622SBarry Smith SNESKSPEW *kctx; 242971f87433Sdalcinl PetscFunctionBegin; 243071f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2431fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 243271f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 243371f87433Sdalcinl 243471f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 243571f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 243671f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 243771f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 243871f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 243971f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 244071f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 244171f87433Sdalcinl 244271f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 244371f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 244471f87433Sdalcinl } 244571f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 244671f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 244771f87433Sdalcinl } 244871f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 244971f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 245071f87433Sdalcinl } 245171f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 245271f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 245371f87433Sdalcinl } 245471f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 245571f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 245671f87433Sdalcinl } 245771f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 245871f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 245971f87433Sdalcinl } 246071f87433Sdalcinl PetscFunctionReturn(0); 246171f87433Sdalcinl } 246271f87433Sdalcinl 246371f87433Sdalcinl #undef __FUNCT__ 2464fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 246571f87433Sdalcinl /*@ 2466fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 246771f87433Sdalcinl convergence criteria for the linear solvers within an inexact 246871f87433Sdalcinl Newton method. 246971f87433Sdalcinl 247071f87433Sdalcinl Not Collective 247171f87433Sdalcinl 247271f87433Sdalcinl Input Parameters: 247371f87433Sdalcinl snes - SNES context 247471f87433Sdalcinl 247571f87433Sdalcinl Output Parameters: 247671f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 247771f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 247871f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 247971f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 248071f87433Sdalcinl (0 <= gamma2 <= 1) 248171f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 248271f87433Sdalcinl . alpha2 - power for safeguard 248371f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 248471f87433Sdalcinl 248571f87433Sdalcinl Level: advanced 248671f87433Sdalcinl 248771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 248871f87433Sdalcinl 2489fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 249071f87433Sdalcinl @*/ 2491fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 249271f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 249371f87433Sdalcinl { 2494fa9f3622SBarry Smith SNESKSPEW *kctx; 249571f87433Sdalcinl PetscFunctionBegin; 249671f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2497fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 249871f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 249971f87433Sdalcinl if(version) *version = kctx->version; 250071f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 250171f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 250271f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 250371f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 250471f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 250571f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 250671f87433Sdalcinl PetscFunctionReturn(0); 250771f87433Sdalcinl } 250871f87433Sdalcinl 250971f87433Sdalcinl #undef __FUNCT__ 2510fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 2511fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 251271f87433Sdalcinl { 251371f87433Sdalcinl PetscErrorCode ierr; 2514fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 251571f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 251671f87433Sdalcinl 251771f87433Sdalcinl PetscFunctionBegin; 251871f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 251971f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 252071f87433Sdalcinl rtol = kctx->rtol_0; 252171f87433Sdalcinl } else { 252271f87433Sdalcinl if (kctx->version == 1) { 252371f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 252471f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 252571f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 252671f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 252771f87433Sdalcinl } else if (kctx->version == 2) { 252871f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 252971f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 253071f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 253171f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 253271f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 253371f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 253471f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 253571f87433Sdalcinl stol = PetscMax(rtol,stol); 253671f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 253771f87433Sdalcinl /* safeguard: avoid oversolving */ 253871f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 253971f87433Sdalcinl stol = PetscMax(rtol,stol); 254071f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 254171f87433Sdalcinl } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 254271f87433Sdalcinl } 254371f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 254471f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 254571f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 254671f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 254771f87433Sdalcinl PetscFunctionReturn(0); 254871f87433Sdalcinl } 254971f87433Sdalcinl 255071f87433Sdalcinl #undef __FUNCT__ 2551fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 2552fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 255371f87433Sdalcinl { 255471f87433Sdalcinl PetscErrorCode ierr; 2555fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 255671f87433Sdalcinl PCSide pcside; 255771f87433Sdalcinl Vec lres; 255871f87433Sdalcinl 255971f87433Sdalcinl PetscFunctionBegin; 256071f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 256171f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 256271f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 256371f87433Sdalcinl if (kctx->version == 1) { 256471f87433Sdalcinl ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr); 256571f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 256671f87433Sdalcinl /* KSP residual is true linear residual */ 256771f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 256871f87433Sdalcinl } else { 256971f87433Sdalcinl /* KSP residual is preconditioned residual */ 257071f87433Sdalcinl /* compute true linear residual norm */ 257171f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 257271f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 257371f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 257471f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 257571f87433Sdalcinl ierr = VecDestroy(lres);CHKERRQ(ierr); 257671f87433Sdalcinl } 257771f87433Sdalcinl } 257871f87433Sdalcinl PetscFunctionReturn(0); 257971f87433Sdalcinl } 258071f87433Sdalcinl 258171f87433Sdalcinl #undef __FUNCT__ 258271f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 258371f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 258471f87433Sdalcinl { 258571f87433Sdalcinl PetscErrorCode ierr; 258671f87433Sdalcinl 258771f87433Sdalcinl PetscFunctionBegin; 2588fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 258971f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 2590fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 259171f87433Sdalcinl PetscFunctionReturn(0); 259271f87433Sdalcinl } 2593