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 179a8054027SBarry Smith . -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild) 180b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 1812492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 18282738288SBarry Smith solver; hence iterations will continue until max_it 1831fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 18482738288SBarry Smith of convergence test 185e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 186e8105e01SRichard Katz filename given prints to stdout 187a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 188a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 189a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 190a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 191e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 1925968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 193fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 19482738288SBarry Smith 19582738288SBarry Smith Options Database for Eisenstat-Walker method: 196fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 1974b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 19836851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 19936851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 20036851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 20136851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 20236851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 20336851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 20482738288SBarry Smith 20511ca99fdSLois Curfman McInnes Notes: 20611ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 20711ca99fdSLois Curfman McInnes the users manual. 20883e2fdc7SBarry Smith 20936851e7fSLois Curfman McInnes Level: beginner 21036851e7fSLois Curfman McInnes 2119b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 2129b94acceSBarry Smith 21369ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 2149b94acceSBarry Smith @*/ 21563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes) 2169b94acceSBarry Smith { 217f1af5d2fSBarry Smith PetscTruth flg; 218a8054027SBarry Smith PetscInt i,indx,lag; 21985385478SLisandro Dalcin const char *deft = SNESLS; 22085385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 22185385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 222e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 22323d894e5SBarry Smith PetscViewerASCIIMonitor monviewer; 22485385478SLisandro Dalcin PetscErrorCode ierr; 2259b94acceSBarry Smith 2263a40ed3dSBarry Smith PetscFunctionBegin; 2274482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 228ca161407SBarry Smith 2297adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr); 230186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2317adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 232b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 233d64ed03dSBarry Smith if (flg) { 234186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 2357adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 236186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 237d64ed03dSBarry Smith } 238909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 23993c39befSBarry Smith 24087828ca2SBarry Smith ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 24170441072SBarry Smith ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 242186905e3SBarry Smith 24387828ca2SBarry Smith ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 244b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 245b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 24650ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 247ddf469c8SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr); 24885385478SLisandro Dalcin 249a8054027SBarry Smith ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr); 250a8054027SBarry Smith if (flg) { 251a8054027SBarry Smith ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr); 252a8054027SBarry Smith } 253a8054027SBarry Smith 25485385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 25585385478SLisandro Dalcin if (flg) { 25685385478SLisandro Dalcin switch (indx) { 2577f7931b9SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 2587f7931b9SBarry Smith case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 25985385478SLisandro Dalcin } 26085385478SLisandro Dalcin } 26185385478SLisandro Dalcin 2625968eb51SBarry Smith ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr); 2635968eb51SBarry Smith if (flg) { 2645968eb51SBarry Smith snes->printreason = PETSC_TRUE; 2655968eb51SBarry Smith } 266186905e3SBarry Smith 26785385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 26885385478SLisandro Dalcin 269fa9f3622SBarry Smith ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 270186905e3SBarry Smith 271fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 272fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 273fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 274fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 275fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 276fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 277fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 278186905e3SBarry Smith 279a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr); 280a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 281eabae89aSBarry Smith 282a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 283e8105e01SRichard Katz if (flg) { 2847adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 28523d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 286e8105e01SRichard Katz } 287eabae89aSBarry Smith 288a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","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); 291f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 292e8105e01SRichard Katz } 293eabae89aSBarry Smith 294a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 295eabae89aSBarry Smith if (flg) { 2967adad957SLisandro Dalcin ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr); 29723d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 298eabae89aSBarry Smith } 299eabae89aSBarry Smith 300a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr); 301a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 302a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr); 303a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 304a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr); 305a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 306a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr); 307a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 308e24b481bSBarry Smith 309b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr); 3104b27c08aSLois Curfman McInnes if (flg) { 311186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 312ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 3139b94acceSBarry Smith } 314639f9d9dSBarry Smith 31576b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 31676b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 31776b2cf59SMatthew Knepley } 31876b2cf59SMatthew Knepley 319e7788613SBarry Smith if (snes->ops->setfromoptions) { 320e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 321639f9d9dSBarry Smith } 322b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3234bbc92c1SBarry Smith 324*1cee3971SBarry Smith 325*1cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 32685385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 32793993e2dSLois Curfman McInnes 3283a40ed3dSBarry Smith PetscFunctionReturn(0); 3299b94acceSBarry Smith } 3309b94acceSBarry Smith 331a847f771SSatish Balay 3324a2ae208SSatish Balay #undef __FUNCT__ 3334a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 3349b94acceSBarry Smith /*@ 3359b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3369b94acceSBarry Smith the nonlinear solvers. 3379b94acceSBarry Smith 338fee21e36SBarry Smith Collective on SNES 339fee21e36SBarry Smith 340c7afd0dbSLois Curfman McInnes Input Parameters: 341c7afd0dbSLois Curfman McInnes + snes - the SNES context 342c7afd0dbSLois Curfman McInnes - usrP - optional user context 343c7afd0dbSLois Curfman McInnes 34436851e7fSLois Curfman McInnes Level: intermediate 34536851e7fSLois Curfman McInnes 3469b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3479b94acceSBarry Smith 3489b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3499b94acceSBarry Smith @*/ 35063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP) 3519b94acceSBarry Smith { 3523a40ed3dSBarry Smith PetscFunctionBegin; 3534482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3549b94acceSBarry Smith snes->user = usrP; 3553a40ed3dSBarry Smith PetscFunctionReturn(0); 3569b94acceSBarry Smith } 35774679c65SBarry Smith 3584a2ae208SSatish Balay #undef __FUNCT__ 3594a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 3609b94acceSBarry Smith /*@C 3619b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3629b94acceSBarry Smith nonlinear solvers. 3639b94acceSBarry Smith 364c7afd0dbSLois Curfman McInnes Not Collective 365c7afd0dbSLois Curfman McInnes 3669b94acceSBarry Smith Input Parameter: 3679b94acceSBarry Smith . snes - SNES context 3689b94acceSBarry Smith 3699b94acceSBarry Smith Output Parameter: 3709b94acceSBarry Smith . usrP - user context 3719b94acceSBarry Smith 37236851e7fSLois Curfman McInnes Level: intermediate 37336851e7fSLois Curfman McInnes 3749b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3759b94acceSBarry Smith 3769b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3779b94acceSBarry Smith @*/ 37863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP) 3799b94acceSBarry Smith { 3803a40ed3dSBarry Smith PetscFunctionBegin; 3814482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3829b94acceSBarry Smith *usrP = snes->user; 3833a40ed3dSBarry Smith PetscFunctionReturn(0); 3849b94acceSBarry Smith } 38574679c65SBarry Smith 3864a2ae208SSatish Balay #undef __FUNCT__ 3874a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 3889b94acceSBarry Smith /*@ 389c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 390c8228a4eSBarry Smith at this time. 3919b94acceSBarry Smith 392c7afd0dbSLois Curfman McInnes Not Collective 393c7afd0dbSLois Curfman McInnes 3949b94acceSBarry Smith Input Parameter: 3959b94acceSBarry Smith . snes - SNES context 3969b94acceSBarry Smith 3979b94acceSBarry Smith Output Parameter: 3989b94acceSBarry Smith . iter - iteration number 3999b94acceSBarry Smith 400c8228a4eSBarry Smith Notes: 401c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 402c8228a4eSBarry Smith 403c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 40408405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 40508405cd6SLois Curfman McInnes .vb 40608405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 40708405cd6SLois Curfman McInnes if (!(it % 2)) { 40808405cd6SLois Curfman McInnes [compute Jacobian here] 40908405cd6SLois Curfman McInnes } 41008405cd6SLois Curfman McInnes .ve 411c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 41208405cd6SLois Curfman McInnes recomputed every second SNES iteration. 413c8228a4eSBarry Smith 41436851e7fSLois Curfman McInnes Level: intermediate 41536851e7fSLois Curfman McInnes 4162b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 4172b668275SBarry Smith 418b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 4199b94acceSBarry Smith @*/ 42063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter) 4219b94acceSBarry Smith { 4223a40ed3dSBarry Smith PetscFunctionBegin; 4234482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4244482741eSBarry Smith PetscValidIntPointer(iter,2); 4259b94acceSBarry Smith *iter = snes->iter; 4263a40ed3dSBarry Smith PetscFunctionReturn(0); 4279b94acceSBarry Smith } 42874679c65SBarry Smith 4294a2ae208SSatish Balay #undef __FUNCT__ 4304a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 4319b94acceSBarry Smith /*@ 4329b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4339b94acceSBarry Smith with SNESSSetFunction(). 4349b94acceSBarry Smith 435c7afd0dbSLois Curfman McInnes Collective on SNES 436c7afd0dbSLois Curfman McInnes 4379b94acceSBarry Smith Input Parameter: 4389b94acceSBarry Smith . snes - SNES context 4399b94acceSBarry Smith 4409b94acceSBarry Smith Output Parameter: 4419b94acceSBarry Smith . fnorm - 2-norm of function 4429b94acceSBarry Smith 44336851e7fSLois Curfman McInnes Level: intermediate 44436851e7fSLois Curfman McInnes 4459b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 446a86d99e1SLois Curfman McInnes 447b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 4489b94acceSBarry Smith @*/ 44971f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 4509b94acceSBarry Smith { 4513a40ed3dSBarry Smith PetscFunctionBegin; 4524482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4534482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 4549b94acceSBarry Smith *fnorm = snes->norm; 4553a40ed3dSBarry Smith PetscFunctionReturn(0); 4569b94acceSBarry Smith } 45774679c65SBarry Smith 4584a2ae208SSatish Balay #undef __FUNCT__ 459b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 4609b94acceSBarry Smith /*@ 461b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 4629b94acceSBarry Smith attempted by the nonlinear solver. 4639b94acceSBarry Smith 464c7afd0dbSLois Curfman McInnes Not Collective 465c7afd0dbSLois Curfman McInnes 4669b94acceSBarry Smith Input Parameter: 4679b94acceSBarry Smith . snes - SNES context 4689b94acceSBarry Smith 4699b94acceSBarry Smith Output Parameter: 4709b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 4719b94acceSBarry Smith 472c96a6f78SLois Curfman McInnes Notes: 473c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 474c96a6f78SLois Curfman McInnes 47536851e7fSLois Curfman McInnes Level: intermediate 47636851e7fSLois Curfman McInnes 4779b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 47858ebbce7SBarry Smith 479e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 48058ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 4819b94acceSBarry Smith @*/ 482b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 4839b94acceSBarry Smith { 4843a40ed3dSBarry Smith PetscFunctionBegin; 4854482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4864482741eSBarry Smith PetscValidIntPointer(nfails,2); 48750ffb88aSMatthew Knepley *nfails = snes->numFailures; 48850ffb88aSMatthew Knepley PetscFunctionReturn(0); 48950ffb88aSMatthew Knepley } 49050ffb88aSMatthew Knepley 49150ffb88aSMatthew Knepley #undef __FUNCT__ 492b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 49350ffb88aSMatthew Knepley /*@ 494b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 49550ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 49650ffb88aSMatthew Knepley 49750ffb88aSMatthew Knepley Not Collective 49850ffb88aSMatthew Knepley 49950ffb88aSMatthew Knepley Input Parameters: 50050ffb88aSMatthew Knepley + snes - SNES context 50150ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 50250ffb88aSMatthew Knepley 50350ffb88aSMatthew Knepley Level: intermediate 50450ffb88aSMatthew Knepley 50550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 50658ebbce7SBarry Smith 507e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 50858ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 50950ffb88aSMatthew Knepley @*/ 510b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 51150ffb88aSMatthew Knepley { 51250ffb88aSMatthew Knepley PetscFunctionBegin; 5134482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 51450ffb88aSMatthew Knepley snes->maxFailures = maxFails; 51550ffb88aSMatthew Knepley PetscFunctionReturn(0); 51650ffb88aSMatthew Knepley } 51750ffb88aSMatthew Knepley 51850ffb88aSMatthew Knepley #undef __FUNCT__ 519b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 52050ffb88aSMatthew Knepley /*@ 521b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 52250ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 52350ffb88aSMatthew Knepley 52450ffb88aSMatthew Knepley Not Collective 52550ffb88aSMatthew Knepley 52650ffb88aSMatthew Knepley Input Parameter: 52750ffb88aSMatthew Knepley . snes - SNES context 52850ffb88aSMatthew Knepley 52950ffb88aSMatthew Knepley Output Parameter: 53050ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 53150ffb88aSMatthew Knepley 53250ffb88aSMatthew Knepley Level: intermediate 53350ffb88aSMatthew Knepley 53450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 53558ebbce7SBarry Smith 536e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 53758ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 53858ebbce7SBarry Smith 53950ffb88aSMatthew Knepley @*/ 540b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 54150ffb88aSMatthew Knepley { 54250ffb88aSMatthew Knepley PetscFunctionBegin; 5434482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5444482741eSBarry Smith PetscValidIntPointer(maxFails,2); 54550ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 5463a40ed3dSBarry Smith PetscFunctionReturn(0); 5479b94acceSBarry Smith } 548a847f771SSatish Balay 5494a2ae208SSatish Balay #undef __FUNCT__ 5502541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 5512541af92SBarry Smith /*@ 5522541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 5532541af92SBarry Smith done by SNES. 5542541af92SBarry Smith 5552541af92SBarry Smith Not Collective 5562541af92SBarry Smith 5572541af92SBarry Smith Input Parameter: 5582541af92SBarry Smith . snes - SNES context 5592541af92SBarry Smith 5602541af92SBarry Smith Output Parameter: 5612541af92SBarry Smith . nfuncs - number of evaluations 5622541af92SBarry Smith 5632541af92SBarry Smith Level: intermediate 5642541af92SBarry Smith 5652541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 56658ebbce7SBarry Smith 567e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 5682541af92SBarry Smith @*/ 5692541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 5702541af92SBarry Smith { 5712541af92SBarry Smith PetscFunctionBegin; 5722541af92SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5732541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 5742541af92SBarry Smith *nfuncs = snes->nfuncs; 5752541af92SBarry Smith PetscFunctionReturn(0); 5762541af92SBarry Smith } 5772541af92SBarry Smith 5782541af92SBarry Smith #undef __FUNCT__ 5793d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 5803d4c4710SBarry Smith /*@ 5813d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 5823d4c4710SBarry Smith linear solvers. 5833d4c4710SBarry Smith 5843d4c4710SBarry Smith Not Collective 5853d4c4710SBarry Smith 5863d4c4710SBarry Smith Input Parameter: 5873d4c4710SBarry Smith . snes - SNES context 5883d4c4710SBarry Smith 5893d4c4710SBarry Smith Output Parameter: 5903d4c4710SBarry Smith . nfails - number of failed solves 5913d4c4710SBarry Smith 5923d4c4710SBarry Smith Notes: 5933d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 5943d4c4710SBarry Smith 5953d4c4710SBarry Smith Level: intermediate 5963d4c4710SBarry Smith 5973d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 59858ebbce7SBarry Smith 599e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 6003d4c4710SBarry Smith @*/ 6013d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 6023d4c4710SBarry Smith { 6033d4c4710SBarry Smith PetscFunctionBegin; 6043d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6053d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 6063d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 6073d4c4710SBarry Smith PetscFunctionReturn(0); 6083d4c4710SBarry Smith } 6093d4c4710SBarry Smith 6103d4c4710SBarry Smith #undef __FUNCT__ 6113d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 6123d4c4710SBarry Smith /*@ 6133d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 6143d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 6153d4c4710SBarry Smith 6163d4c4710SBarry Smith Collective on SNES 6173d4c4710SBarry Smith 6183d4c4710SBarry Smith Input Parameters: 6193d4c4710SBarry Smith + snes - SNES context 6203d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 6213d4c4710SBarry Smith 6223d4c4710SBarry Smith Level: intermediate 6233d4c4710SBarry Smith 624a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 6253d4c4710SBarry Smith 6263d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 6273d4c4710SBarry Smith 62858ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 6293d4c4710SBarry Smith @*/ 6303d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 6313d4c4710SBarry Smith { 6323d4c4710SBarry Smith PetscFunctionBegin; 6333d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6343d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 6353d4c4710SBarry Smith PetscFunctionReturn(0); 6363d4c4710SBarry Smith } 6373d4c4710SBarry Smith 6383d4c4710SBarry Smith #undef __FUNCT__ 6393d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 6403d4c4710SBarry Smith /*@ 6413d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 6423d4c4710SBarry Smith are allowed before SNES terminates 6433d4c4710SBarry Smith 6443d4c4710SBarry Smith Not Collective 6453d4c4710SBarry Smith 6463d4c4710SBarry Smith Input Parameter: 6473d4c4710SBarry Smith . snes - SNES context 6483d4c4710SBarry Smith 6493d4c4710SBarry Smith Output Parameter: 6503d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 6513d4c4710SBarry Smith 6523d4c4710SBarry Smith Level: intermediate 6533d4c4710SBarry Smith 6543d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 6553d4c4710SBarry Smith 6563d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 6573d4c4710SBarry Smith 658e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 6593d4c4710SBarry Smith @*/ 6603d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 6613d4c4710SBarry Smith { 6623d4c4710SBarry Smith PetscFunctionBegin; 6633d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6643d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 6653d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 6663d4c4710SBarry Smith PetscFunctionReturn(0); 6673d4c4710SBarry Smith } 6683d4c4710SBarry Smith 6693d4c4710SBarry Smith #undef __FUNCT__ 670b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 671c96a6f78SLois Curfman McInnes /*@ 672b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 673c96a6f78SLois Curfman McInnes used by the nonlinear solver. 674c96a6f78SLois Curfman McInnes 675c7afd0dbSLois Curfman McInnes Not Collective 676c7afd0dbSLois Curfman McInnes 677c96a6f78SLois Curfman McInnes Input Parameter: 678c96a6f78SLois Curfman McInnes . snes - SNES context 679c96a6f78SLois Curfman McInnes 680c96a6f78SLois Curfman McInnes Output Parameter: 681c96a6f78SLois Curfman McInnes . lits - number of linear iterations 682c96a6f78SLois Curfman McInnes 683c96a6f78SLois Curfman McInnes Notes: 684c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 685c96a6f78SLois Curfman McInnes 68636851e7fSLois Curfman McInnes Level: intermediate 68736851e7fSLois Curfman McInnes 688c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 6892b668275SBarry Smith 69058ebbce7SBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm()S, NESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 691c96a6f78SLois Curfman McInnes @*/ 692b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 693c96a6f78SLois Curfman McInnes { 6943a40ed3dSBarry Smith PetscFunctionBegin; 6954482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6964482741eSBarry Smith PetscValidIntPointer(lits,2); 697c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 6983a40ed3dSBarry Smith PetscFunctionReturn(0); 699c96a6f78SLois Curfman McInnes } 700c96a6f78SLois Curfman McInnes 7014a2ae208SSatish Balay #undef __FUNCT__ 70294b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 70352baeb72SSatish Balay /*@ 70494b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 7059b94acceSBarry Smith 70694b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 707c7afd0dbSLois Curfman McInnes 7089b94acceSBarry Smith Input Parameter: 7099b94acceSBarry Smith . snes - the SNES context 7109b94acceSBarry Smith 7119b94acceSBarry Smith Output Parameter: 71294b7f48cSBarry Smith . ksp - the KSP context 7139b94acceSBarry Smith 7149b94acceSBarry Smith Notes: 71594b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 7169b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 7172999313aSBarry Smith PC contexts as well. 7189b94acceSBarry Smith 71936851e7fSLois Curfman McInnes Level: beginner 72036851e7fSLois Curfman McInnes 72194b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7229b94acceSBarry Smith 7232999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7249b94acceSBarry Smith @*/ 72563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp) 7269b94acceSBarry Smith { 727*1cee3971SBarry Smith PetscErrorCode ierr; 728*1cee3971SBarry Smith 7293a40ed3dSBarry Smith PetscFunctionBegin; 7304482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7314482741eSBarry Smith PetscValidPointer(ksp,2); 732*1cee3971SBarry Smith 733*1cee3971SBarry Smith if (!snes->ksp) { 734*1cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 735*1cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 736*1cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 737*1cee3971SBarry Smith } 73894b7f48cSBarry Smith *ksp = snes->ksp; 7393a40ed3dSBarry Smith PetscFunctionReturn(0); 7409b94acceSBarry Smith } 74182bf6240SBarry Smith 7424a2ae208SSatish Balay #undef __FUNCT__ 7432999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 7442999313aSBarry Smith /*@ 7452999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 7462999313aSBarry Smith 7472999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 7482999313aSBarry Smith 7492999313aSBarry Smith Input Parameters: 7502999313aSBarry Smith + snes - the SNES context 7512999313aSBarry Smith - ksp - the KSP context 7522999313aSBarry Smith 7532999313aSBarry Smith Notes: 7542999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 7552999313aSBarry Smith so this routine is rarely needed. 7562999313aSBarry Smith 7572999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 7582999313aSBarry Smith decreased by one. 7592999313aSBarry Smith 7602999313aSBarry Smith Level: developer 7612999313aSBarry Smith 7622999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7632999313aSBarry Smith 7642999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7652999313aSBarry Smith @*/ 7662999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp) 7672999313aSBarry Smith { 7682999313aSBarry Smith PetscErrorCode ierr; 7692999313aSBarry Smith 7702999313aSBarry Smith PetscFunctionBegin; 7712999313aSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7722999313aSBarry Smith PetscValidHeaderSpecific(ksp,KSP_COOKIE,2); 7732999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 7747dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 775906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 7762999313aSBarry Smith snes->ksp = ksp; 7772999313aSBarry Smith PetscFunctionReturn(0); 7782999313aSBarry Smith } 7792999313aSBarry Smith 7807adad957SLisandro Dalcin #if 0 7812999313aSBarry Smith #undef __FUNCT__ 7824a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 7836849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 784e24b481bSBarry Smith { 785e24b481bSBarry Smith PetscFunctionBegin; 786e24b481bSBarry Smith PetscFunctionReturn(0); 787e24b481bSBarry Smith } 7887adad957SLisandro Dalcin #endif 789e24b481bSBarry Smith 7909b94acceSBarry Smith /* -----------------------------------------------------------*/ 7914a2ae208SSatish Balay #undef __FUNCT__ 7924a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 79352baeb72SSatish Balay /*@ 7949b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 7959b94acceSBarry Smith 796c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 797c7afd0dbSLois Curfman McInnes 798c7afd0dbSLois Curfman McInnes Input Parameters: 799906ed7ccSBarry Smith . comm - MPI communicator 8009b94acceSBarry Smith 8019b94acceSBarry Smith Output Parameter: 8029b94acceSBarry Smith . outsnes - the new SNES context 8039b94acceSBarry Smith 804c7afd0dbSLois Curfman McInnes Options Database Keys: 805c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 806c7afd0dbSLois Curfman McInnes and no preconditioning matrix 807c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 808c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 809c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 810c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 811c1f60f51SBarry Smith 81236851e7fSLois Curfman McInnes Level: beginner 81336851e7fSLois Curfman McInnes 8149b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 8159b94acceSBarry Smith 816a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 817a8054027SBarry Smith 8189b94acceSBarry Smith @*/ 81963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes) 8209b94acceSBarry Smith { 821dfbe8321SBarry Smith PetscErrorCode ierr; 8229b94acceSBarry Smith SNES snes; 823fa9f3622SBarry Smith SNESKSPEW *kctx; 82437fcc0dbSBarry Smith 8253a40ed3dSBarry Smith PetscFunctionBegin; 826ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 8278ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 8288ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 8298ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 8308ba1e511SMatthew Knepley #endif 8318ba1e511SMatthew Knepley 832e7788613SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 8337adad957SLisandro Dalcin 83485385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 8359b94acceSBarry Smith snes->max_its = 50; 8369750a799SBarry Smith snes->max_funcs = 10000; 8379b94acceSBarry Smith snes->norm = 0.0; 838b4874afaSBarry Smith snes->rtol = 1.e-8; 839b4874afaSBarry Smith snes->ttol = 0.0; 84070441072SBarry Smith snes->abstol = 1.e-50; 8419b94acceSBarry Smith snes->xtol = 1.e-8; 8424b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 8439b94acceSBarry Smith snes->nfuncs = 0; 84450ffb88aSMatthew Knepley snes->numFailures = 0; 84550ffb88aSMatthew Knepley snes->maxFailures = 1; 8467a00f4a9SLois Curfman McInnes snes->linear_its = 0; 847a8054027SBarry Smith snes->lagpreconditioner = 1; 848639f9d9dSBarry Smith snes->numbermonitors = 0; 8499b94acceSBarry Smith snes->data = 0; 8504dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 851186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 8526f24a144SLois Curfman McInnes snes->vwork = 0; 8536f24a144SLois Curfman McInnes snes->nwork = 0; 854758f92a0SBarry Smith snes->conv_hist_len = 0; 855758f92a0SBarry Smith snes->conv_hist_max = 0; 856758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 857758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 858758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 859184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 8609b94acceSBarry Smith 8613d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 8623d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 8633d4c4710SBarry Smith 8649b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 86538f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 8669b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 8679b94acceSBarry Smith kctx->version = 2; 8689b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 8699b94acceSBarry Smith this was too large for some test cases */ 8709b94acceSBarry Smith kctx->rtol_last = 0; 8719b94acceSBarry Smith kctx->rtol_max = .9; 8729b94acceSBarry Smith kctx->gamma = 1.0; 87371f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 87471f87433Sdalcinl kctx->alpha2 = kctx->alpha; 8759b94acceSBarry Smith kctx->threshold = .1; 8769b94acceSBarry Smith kctx->lresid_last = 0; 8779b94acceSBarry Smith kctx->norm_last = 0; 8789b94acceSBarry Smith 8799b94acceSBarry Smith *outsnes = snes; 88000036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 8813a40ed3dSBarry Smith PetscFunctionReturn(0); 8829b94acceSBarry Smith } 8839b94acceSBarry Smith 8844a2ae208SSatish Balay #undef __FUNCT__ 8854a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 8869b94acceSBarry Smith /*@C 8879b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 8889b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 8899b94acceSBarry Smith equations. 8909b94acceSBarry Smith 891fee21e36SBarry Smith Collective on SNES 892fee21e36SBarry Smith 893c7afd0dbSLois Curfman McInnes Input Parameters: 894c7afd0dbSLois Curfman McInnes + snes - the SNES context 895c7afd0dbSLois Curfman McInnes . r - vector to store function value 896de044059SHong Zhang . func - function evaluation routine 897c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 898c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8999b94acceSBarry Smith 900c7afd0dbSLois Curfman McInnes Calling sequence of func: 9018d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 902c7afd0dbSLois Curfman McInnes 903313e4042SLois Curfman McInnes . f - function vector 904c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 9059b94acceSBarry Smith 9069b94acceSBarry Smith Notes: 9079b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 9089b94acceSBarry Smith $ f'(x) x = -f(x), 909c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 9109b94acceSBarry Smith 91136851e7fSLois Curfman McInnes Level: beginner 91236851e7fSLois Curfman McInnes 9139b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9149b94acceSBarry Smith 915a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 9169b94acceSBarry Smith @*/ 91763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 9189b94acceSBarry Smith { 91985385478SLisandro Dalcin PetscErrorCode ierr; 9203a40ed3dSBarry Smith PetscFunctionBegin; 9214482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9224482741eSBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE,2); 923c9780b6fSBarry Smith PetscCheckSameComm(snes,1,r,2); 92485385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 92585385478SLisandro Dalcin if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); } 926e7788613SBarry Smith snes->ops->computefunction = func; 92785385478SLisandro Dalcin snes->vec_func = r; 9289b94acceSBarry Smith snes->funP = ctx; 9293a40ed3dSBarry Smith PetscFunctionReturn(0); 9309b94acceSBarry Smith } 9319b94acceSBarry Smith 9323ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 9333ab0aad5SBarry Smith #undef __FUNCT__ 9341096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 9351096aae1SMatthew Knepley /*@C 9361096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 9371096aae1SMatthew Knepley it assumes a zero right hand side. 9381096aae1SMatthew Knepley 9391096aae1SMatthew Knepley Collective on SNES 9401096aae1SMatthew Knepley 9411096aae1SMatthew Knepley Input Parameter: 9421096aae1SMatthew Knepley . snes - the SNES context 9431096aae1SMatthew Knepley 9441096aae1SMatthew Knepley Output Parameter: 945bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 9461096aae1SMatthew Knepley 9471096aae1SMatthew Knepley Level: intermediate 9481096aae1SMatthew Knepley 9491096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 9501096aae1SMatthew Knepley 95185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 9521096aae1SMatthew Knepley @*/ 9531096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs) 9541096aae1SMatthew Knepley { 9551096aae1SMatthew Knepley PetscFunctionBegin; 9561096aae1SMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9571096aae1SMatthew Knepley PetscValidPointer(rhs,2); 95885385478SLisandro Dalcin *rhs = snes->vec_rhs; 9591096aae1SMatthew Knepley PetscFunctionReturn(0); 9601096aae1SMatthew Knepley } 9611096aae1SMatthew Knepley 9621096aae1SMatthew Knepley #undef __FUNCT__ 9634a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 9649b94acceSBarry Smith /*@ 96536851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 9669b94acceSBarry Smith SNESSetFunction(). 9679b94acceSBarry Smith 968c7afd0dbSLois Curfman McInnes Collective on SNES 969c7afd0dbSLois Curfman McInnes 9709b94acceSBarry Smith Input Parameters: 971c7afd0dbSLois Curfman McInnes + snes - the SNES context 972c7afd0dbSLois Curfman McInnes - x - input vector 9739b94acceSBarry Smith 9749b94acceSBarry Smith Output Parameter: 9753638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 9769b94acceSBarry Smith 9771bffabb2SLois Curfman McInnes Notes: 97836851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 97936851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 98036851e7fSLois Curfman McInnes themselves. 98136851e7fSLois Curfman McInnes 98236851e7fSLois Curfman McInnes Level: developer 98336851e7fSLois Curfman McInnes 9849b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 9859b94acceSBarry Smith 986a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 9879b94acceSBarry Smith @*/ 98863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y) 9899b94acceSBarry Smith { 990dfbe8321SBarry Smith PetscErrorCode ierr; 9919b94acceSBarry Smith 9923a40ed3dSBarry Smith PetscFunctionBegin; 9934482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9944482741eSBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE,2); 9954482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,3); 996c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 997c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 998184914b5SBarry Smith 999d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1000e7788613SBarry Smith if (snes->ops->computefunction) { 1001d64ed03dSBarry Smith PetscStackPush("SNES user function"); 1002e9a2bbcdSBarry Smith CHKMEMQ; 1003e7788613SBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP); 1004e9a2bbcdSBarry Smith CHKMEMQ; 1005d64ed03dSBarry Smith PetscStackPop; 1006d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 100719717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr); 100819717074SBarry Smith } 1009d5e45103SBarry Smith CHKERRQ(ierr); 101085385478SLisandro Dalcin } else if (snes->vec_rhs) { 10111096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 10121096aae1SMatthew Knepley } else { 10131096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 10141096aae1SMatthew Knepley } 101585385478SLisandro Dalcin if (snes->vec_rhs) { 101685385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 10173ab0aad5SBarry Smith } 1018ae3c334cSLois Curfman McInnes snes->nfuncs++; 1019d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 10203a40ed3dSBarry Smith PetscFunctionReturn(0); 10219b94acceSBarry Smith } 10229b94acceSBarry Smith 10234a2ae208SSatish Balay #undef __FUNCT__ 10244a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 102562fef451SLois Curfman McInnes /*@ 102662fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 102762fef451SLois Curfman McInnes set with SNESSetJacobian(). 102862fef451SLois Curfman McInnes 1029c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1030c7afd0dbSLois Curfman McInnes 103162fef451SLois Curfman McInnes Input Parameters: 1032c7afd0dbSLois Curfman McInnes + snes - the SNES context 1033c7afd0dbSLois Curfman McInnes - x - input vector 103462fef451SLois Curfman McInnes 103562fef451SLois Curfman McInnes Output Parameters: 1036c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 103762fef451SLois Curfman McInnes . B - optional preconditioning matrix 10382b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1039fee21e36SBarry Smith 104062fef451SLois Curfman McInnes Notes: 104162fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 104262fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 104362fef451SLois Curfman McInnes 104494b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1045dc5a77f8SLois Curfman McInnes flag parameter. 104662fef451SLois Curfman McInnes 104736851e7fSLois Curfman McInnes Level: developer 104836851e7fSLois Curfman McInnes 104962fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 105062fef451SLois Curfman McInnes 10512b668275SBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure 105262fef451SLois Curfman McInnes @*/ 105363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10549b94acceSBarry Smith { 1055dfbe8321SBarry Smith PetscErrorCode ierr; 10563a40ed3dSBarry Smith 10573a40ed3dSBarry Smith PetscFunctionBegin; 10584482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10594482741eSBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE,2); 10604482741eSBarry Smith PetscValidPointer(flg,5); 1061c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1062e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1063d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1064c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1065d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1066dc67937bSBarry Smith CHKMEMQ; 1067e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1068dc67937bSBarry Smith CHKMEMQ; 1069d64ed03dSBarry Smith PetscStackPop; 1070d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1071a8054027SBarry Smith 1072a8054027SBarry Smith if (snes->lagpreconditioner == -1) { 1073a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1074a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1075a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1076a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1077a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1078a8054027SBarry Smith } 1079a8054027SBarry Smith 10806d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 10816ce558aeSBarry Smith /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3); 10826ce558aeSBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE,4); */ 10833a40ed3dSBarry Smith PetscFunctionReturn(0); 10849b94acceSBarry Smith } 10859b94acceSBarry Smith 10864a2ae208SSatish Balay #undef __FUNCT__ 10874a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 10889b94acceSBarry Smith /*@C 10899b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1090044dda88SLois Curfman McInnes location to store the matrix. 10919b94acceSBarry Smith 1092c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1093c7afd0dbSLois Curfman McInnes 10949b94acceSBarry Smith Input Parameters: 1095c7afd0dbSLois Curfman McInnes + snes - the SNES context 10969b94acceSBarry Smith . A - Jacobian matrix 10979b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 10989b94acceSBarry Smith . func - Jacobian evaluation routine 1099c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11002cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11019b94acceSBarry Smith 11029b94acceSBarry Smith Calling sequence of func: 11038d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11049b94acceSBarry Smith 1105c7afd0dbSLois Curfman McInnes + x - input vector 11069b94acceSBarry Smith . A - Jacobian matrix 11079b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1108ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 11092b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1110c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11119b94acceSBarry Smith 11129b94acceSBarry Smith Notes: 111394b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 11142cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1115ac21db08SLois Curfman McInnes 1116ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11179b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11189b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11199b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11209b94acceSBarry Smith throughout the global iterations. 11219b94acceSBarry Smith 112216913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 112316913363SBarry Smith each matrix. 112416913363SBarry Smith 112536851e7fSLois Curfman McInnes Level: beginner 112636851e7fSLois Curfman McInnes 11279b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11289b94acceSBarry Smith 11293ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 11309b94acceSBarry Smith @*/ 113163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 11329b94acceSBarry Smith { 1133dfbe8321SBarry Smith PetscErrorCode ierr; 11343a7fca6bSBarry Smith 11353a40ed3dSBarry Smith PetscFunctionBegin; 11364482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11374482741eSBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2); 11384482741eSBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3); 1139c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 1140c9780b6fSBarry Smith if (B) PetscCheckSameComm(snes,1,B,2); 1141e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 11423a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 11433a7fca6bSBarry Smith if (A) { 11447dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 11453a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 11469b94acceSBarry Smith snes->jacobian = A; 11473a7fca6bSBarry Smith } 11483a7fca6bSBarry Smith if (B) { 11497dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 11503a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 11519b94acceSBarry Smith snes->jacobian_pre = B; 11523a7fca6bSBarry Smith } 11533a40ed3dSBarry Smith PetscFunctionReturn(0); 11549b94acceSBarry Smith } 115562fef451SLois Curfman McInnes 11564a2ae208SSatish Balay #undef __FUNCT__ 11574a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1158c2aafc4cSSatish Balay /*@C 1159b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1160b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1161b4fd4287SBarry Smith 1162c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1163c7afd0dbSLois Curfman McInnes 1164b4fd4287SBarry Smith Input Parameter: 1165b4fd4287SBarry Smith . snes - the nonlinear solver context 1166b4fd4287SBarry Smith 1167b4fd4287SBarry Smith Output Parameters: 1168c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1169b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 117070e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 117170e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1172fee21e36SBarry Smith 117336851e7fSLois Curfman McInnes Level: advanced 117436851e7fSLois Curfman McInnes 1175b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1176b4fd4287SBarry Smith @*/ 117770e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1178b4fd4287SBarry Smith { 11793a40ed3dSBarry Smith PetscFunctionBegin; 11804482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1181b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1182b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1183e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 118470e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 11853a40ed3dSBarry Smith PetscFunctionReturn(0); 1186b4fd4287SBarry Smith } 1187b4fd4287SBarry Smith 11889b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 118963dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 11909b94acceSBarry Smith 11914a2ae208SSatish Balay #undef __FUNCT__ 11924a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 11939b94acceSBarry Smith /*@ 11949b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1195272ac6f2SLois Curfman McInnes of a nonlinear solver. 11969b94acceSBarry Smith 1197fee21e36SBarry Smith Collective on SNES 1198fee21e36SBarry Smith 1199c7afd0dbSLois Curfman McInnes Input Parameters: 120070e92668SMatthew Knepley . snes - the SNES context 1201c7afd0dbSLois Curfman McInnes 1202272ac6f2SLois Curfman McInnes Notes: 1203272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1204272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1205272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1206272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1207272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1208272ac6f2SLois Curfman McInnes 120936851e7fSLois Curfman McInnes Level: advanced 121036851e7fSLois Curfman McInnes 12119b94acceSBarry Smith .keywords: SNES, nonlinear, setup 12129b94acceSBarry Smith 12139b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 12149b94acceSBarry Smith @*/ 121570e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes) 12169b94acceSBarry Smith { 1217dfbe8321SBarry Smith PetscErrorCode ierr; 121871f87433Sdalcinl PetscTruth flg; 12193a40ed3dSBarry Smith 12203a40ed3dSBarry Smith PetscFunctionBegin; 12214482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 12224dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 12239b94acceSBarry Smith 12247adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 122585385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 122685385478SLisandro Dalcin } 122785385478SLisandro Dalcin 12287adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1229c1f60f51SBarry Smith /* 1230c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1231dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1232c1f60f51SBarry Smith */ 1233c1f60f51SBarry Smith if (flg) { 1234c1f60f51SBarry Smith Mat J; 1235fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12363ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 1237ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12383a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 12393a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1240c1f60f51SBarry Smith } 124145fc7adcSBarry Smith 124203c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT) 12437adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr); 124445fc7adcSBarry Smith if (flg) { 124545fc7adcSBarry Smith Mat J; 124645fc7adcSBarry Smith ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr); 124775396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr); 124845fc7adcSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 124945fc7adcSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 125045fc7adcSBarry Smith } 125132a4b47aSMatthew Knepley #endif 125245fc7adcSBarry Smith 12537adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1254c1f60f51SBarry Smith /* 1255dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1256c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1257c1f60f51SBarry Smith */ 125831615d04SLois Curfman McInnes if (flg) { 1259272ac6f2SLois Curfman McInnes Mat J; 1260b5d62d44SBarry Smith KSP ksp; 126194b7f48cSBarry Smith PC pc; 126275396ef9SLisandro Dalcin /* create and set matrix-free operator */ 1263fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12643ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 126575396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12663ec795f1SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 12673a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1268f3ef73ceSBarry Smith /* force no preconditioner */ 126994b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 1270b5d62d44SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 1271a9815358SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 1272a9815358SBarry Smith if (!flg) { 127375396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 1274f3ef73ceSBarry Smith ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 1275272ac6f2SLois Curfman McInnes } 1276a9815358SBarry Smith } 1277f3ef73ceSBarry Smith 127885385478SLisandro Dalcin if (!snes->vec_func && !snes->vec_rhs) { 12791096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12801096aae1SMatthew Knepley } 128185385478SLisandro Dalcin if (!snes->ops->computefunction && !snes->vec_rhs) { 12821096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 12831096aae1SMatthew Knepley } 128475396ef9SLisandro Dalcin if (!snes->jacobian) { 128575396ef9SLisandro Dalcin SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 128675396ef9SLisandro Dalcin } 1287a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 128829bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1289a8c6a408SBarry Smith } 1290a703fe33SLois Curfman McInnes 1291410397dcSLisandro Dalcin if (snes->ops->setup) { 1292410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1293410397dcSLisandro Dalcin } 12947aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 12953a40ed3dSBarry Smith PetscFunctionReturn(0); 12969b94acceSBarry Smith } 12979b94acceSBarry Smith 12984a2ae208SSatish Balay #undef __FUNCT__ 12994a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 130052baeb72SSatish Balay /*@ 13019b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 13029b94acceSBarry Smith with SNESCreate(). 13039b94acceSBarry Smith 1304c7afd0dbSLois Curfman McInnes Collective on SNES 1305c7afd0dbSLois Curfman McInnes 13069b94acceSBarry Smith Input Parameter: 13079b94acceSBarry Smith . snes - the SNES context 13089b94acceSBarry Smith 130936851e7fSLois Curfman McInnes Level: beginner 131036851e7fSLois Curfman McInnes 13119b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 13129b94acceSBarry Smith 131363a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 13149b94acceSBarry Smith @*/ 131563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes) 13169b94acceSBarry Smith { 13176849ba73SBarry Smith PetscErrorCode ierr; 13183a40ed3dSBarry Smith 13193a40ed3dSBarry Smith PetscFunctionBegin; 13204482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 13217adad957SLisandro Dalcin if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0); 1322d4bb536fSBarry Smith 1323be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 13240f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1325e7788613SBarry Smith if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);} 132685385478SLisandro Dalcin 132785385478SLisandro Dalcin if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);} 132885385478SLisandro Dalcin if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);} 132985385478SLisandro Dalcin if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);} 13303a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 13313a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 1332*1cee3971SBarry Smith if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);} 133385385478SLisandro Dalcin ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr); 1334522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1335a6570f20SBarry Smith ierr = SNESMonitorCancel(snes);CHKERRQ(ierr); 13367f7931b9SBarry Smith if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);} 1337a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 13383a40ed3dSBarry Smith PetscFunctionReturn(0); 13399b94acceSBarry Smith } 13409b94acceSBarry Smith 13419b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 13429b94acceSBarry Smith 13434a2ae208SSatish Balay #undef __FUNCT__ 1344a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1345a8054027SBarry Smith /*@ 1346a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1347a8054027SBarry Smith 1348a8054027SBarry Smith Collective on SNES 1349a8054027SBarry Smith 1350a8054027SBarry Smith Input Parameters: 1351a8054027SBarry Smith + snes - the SNES context 1352a8054027SBarry Smith - lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time 1353a8054027SBarry Smith the Jacobian is built etc. 1354a8054027SBarry Smith 1355a8054027SBarry Smith Options Database Keys: 1356a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1357a8054027SBarry Smith 1358a8054027SBarry Smith Notes: 1359a8054027SBarry Smith The default is 1 1360a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1361a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1362a8054027SBarry Smith 1363a8054027SBarry Smith Level: intermediate 1364a8054027SBarry Smith 1365a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1366a8054027SBarry Smith 1367a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner() 1368a8054027SBarry Smith 1369a8054027SBarry Smith @*/ 1370a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1371a8054027SBarry Smith { 1372a8054027SBarry Smith PetscFunctionBegin; 1373a8054027SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1374a8054027SBarry Smith if (lag < -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -1, 1 or greater"); 1375a8054027SBarry Smith if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1376a8054027SBarry Smith snes->lagpreconditioner = lag; 1377a8054027SBarry Smith PetscFunctionReturn(0); 1378a8054027SBarry Smith } 1379a8054027SBarry Smith 1380a8054027SBarry Smith #undef __FUNCT__ 1381a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1382a8054027SBarry Smith /*@ 1383a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1384a8054027SBarry Smith 1385a8054027SBarry Smith Collective on SNES 1386a8054027SBarry Smith 1387a8054027SBarry Smith Input Parameter: 1388a8054027SBarry Smith . snes - the SNES context 1389a8054027SBarry Smith 1390a8054027SBarry Smith Output Parameter: 1391a8054027SBarry Smith . lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time 1392a8054027SBarry Smith the Jacobian is built etc. 1393a8054027SBarry Smith 1394a8054027SBarry Smith Options Database Keys: 1395a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1396a8054027SBarry Smith 1397a8054027SBarry Smith Notes: 1398a8054027SBarry Smith The default is 1 1399a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1400a8054027SBarry Smith 1401a8054027SBarry Smith Level: intermediate 1402a8054027SBarry Smith 1403a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1404a8054027SBarry Smith 1405a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1406a8054027SBarry Smith 1407a8054027SBarry Smith @*/ 1408a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1409a8054027SBarry Smith { 1410a8054027SBarry Smith PetscFunctionBegin; 1411a8054027SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1412a8054027SBarry Smith *lag = snes->lagpreconditioner; 1413a8054027SBarry Smith PetscFunctionReturn(0); 1414a8054027SBarry Smith } 1415a8054027SBarry Smith 1416a8054027SBarry Smith #undef __FUNCT__ 14174a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 14189b94acceSBarry Smith /*@ 1419d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 14209b94acceSBarry Smith 1421c7afd0dbSLois Curfman McInnes Collective on SNES 1422c7afd0dbSLois Curfman McInnes 14239b94acceSBarry Smith Input Parameters: 1424c7afd0dbSLois Curfman McInnes + snes - the SNES context 142570441072SBarry Smith . abstol - absolute convergence tolerance 142633174efeSLois Curfman McInnes . rtol - relative convergence tolerance 142733174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 142833174efeSLois Curfman McInnes of the change in the solution between steps 142933174efeSLois Curfman McInnes . maxit - maximum number of iterations 1430c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1431fee21e36SBarry Smith 143233174efeSLois Curfman McInnes Options Database Keys: 143370441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1434c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1435c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1436c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1437c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 14389b94acceSBarry Smith 1439d7a720efSLois Curfman McInnes Notes: 14409b94acceSBarry Smith The default maximum number of iterations is 50. 14419b94acceSBarry Smith The default maximum number of function evaluations is 1000. 14429b94acceSBarry Smith 144336851e7fSLois Curfman McInnes Level: intermediate 144436851e7fSLois Curfman McInnes 144533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 14469b94acceSBarry Smith 14472492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 14489b94acceSBarry Smith @*/ 144963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 14509b94acceSBarry Smith { 14513a40ed3dSBarry Smith PetscFunctionBegin; 14524482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 145370441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1454d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1455d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1456d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1457d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 14583a40ed3dSBarry Smith PetscFunctionReturn(0); 14599b94acceSBarry Smith } 14609b94acceSBarry Smith 14614a2ae208SSatish Balay #undef __FUNCT__ 14624a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 14639b94acceSBarry Smith /*@ 146433174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 146533174efeSLois Curfman McInnes 1466c7afd0dbSLois Curfman McInnes Not Collective 1467c7afd0dbSLois Curfman McInnes 146833174efeSLois Curfman McInnes Input Parameters: 1469c7afd0dbSLois Curfman McInnes + snes - the SNES context 147085385478SLisandro Dalcin . atol - absolute convergence tolerance 147133174efeSLois Curfman McInnes . rtol - relative convergence tolerance 147233174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 147333174efeSLois Curfman McInnes of the change in the solution between steps 147433174efeSLois Curfman McInnes . maxit - maximum number of iterations 1475c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1476fee21e36SBarry Smith 147733174efeSLois Curfman McInnes Notes: 147833174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 147933174efeSLois Curfman McInnes 148036851e7fSLois Curfman McInnes Level: intermediate 148136851e7fSLois Curfman McInnes 148233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 148333174efeSLois Curfman McInnes 148433174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 148533174efeSLois Curfman McInnes @*/ 148685385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 148733174efeSLois Curfman McInnes { 14883a40ed3dSBarry Smith PetscFunctionBegin; 14894482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 149085385478SLisandro Dalcin if (atol) *atol = snes->abstol; 149133174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 149233174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 149333174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 149433174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 14953a40ed3dSBarry Smith PetscFunctionReturn(0); 149633174efeSLois Curfman McInnes } 149733174efeSLois Curfman McInnes 14984a2ae208SSatish Balay #undef __FUNCT__ 14994a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 150033174efeSLois Curfman McInnes /*@ 15019b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 15029b94acceSBarry Smith 1503fee21e36SBarry Smith Collective on SNES 1504fee21e36SBarry Smith 1505c7afd0dbSLois Curfman McInnes Input Parameters: 1506c7afd0dbSLois Curfman McInnes + snes - the SNES context 1507c7afd0dbSLois Curfman McInnes - tol - tolerance 1508c7afd0dbSLois Curfman McInnes 15099b94acceSBarry Smith Options Database Key: 1510c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 15119b94acceSBarry Smith 151236851e7fSLois Curfman McInnes Level: intermediate 151336851e7fSLois Curfman McInnes 15149b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 15159b94acceSBarry Smith 15162492ecdbSBarry Smith .seealso: SNESSetTolerances() 15179b94acceSBarry Smith @*/ 151863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 15199b94acceSBarry Smith { 15203a40ed3dSBarry Smith PetscFunctionBegin; 15214482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 15229b94acceSBarry Smith snes->deltatol = tol; 15233a40ed3dSBarry Smith PetscFunctionReturn(0); 15249b94acceSBarry Smith } 15259b94acceSBarry Smith 1526df9fa365SBarry Smith /* 1527df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1528df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1529df9fa365SBarry Smith macros instead of functions 1530df9fa365SBarry Smith */ 15314a2ae208SSatish Balay #undef __FUNCT__ 1532a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 1533a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1534ce1608b8SBarry Smith { 1535dfbe8321SBarry Smith PetscErrorCode ierr; 1536ce1608b8SBarry Smith 1537ce1608b8SBarry Smith PetscFunctionBegin; 15384482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1539a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1540ce1608b8SBarry Smith PetscFunctionReturn(0); 1541ce1608b8SBarry Smith } 1542ce1608b8SBarry Smith 15434a2ae208SSatish Balay #undef __FUNCT__ 1544a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 1545a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1546df9fa365SBarry Smith { 1547dfbe8321SBarry Smith PetscErrorCode ierr; 1548df9fa365SBarry Smith 1549df9fa365SBarry Smith PetscFunctionBegin; 1550a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1551df9fa365SBarry Smith PetscFunctionReturn(0); 1552df9fa365SBarry Smith } 1553df9fa365SBarry Smith 15544a2ae208SSatish Balay #undef __FUNCT__ 1555a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 1556a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw) 1557df9fa365SBarry Smith { 1558dfbe8321SBarry Smith PetscErrorCode ierr; 1559df9fa365SBarry Smith 1560df9fa365SBarry Smith PetscFunctionBegin; 1561a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1562df9fa365SBarry Smith PetscFunctionReturn(0); 1563df9fa365SBarry Smith } 1564df9fa365SBarry Smith 15659b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 15669b94acceSBarry Smith 15674a2ae208SSatish Balay #undef __FUNCT__ 1568a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 15699b94acceSBarry Smith /*@C 1570a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 15719b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 15729b94acceSBarry Smith progress. 15739b94acceSBarry Smith 1574fee21e36SBarry Smith Collective on SNES 1575fee21e36SBarry Smith 1576c7afd0dbSLois Curfman McInnes Input Parameters: 1577c7afd0dbSLois Curfman McInnes + snes - the SNES context 1578c7afd0dbSLois Curfman McInnes . func - monitoring routine 1579b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1580e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 1581b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1582b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 15839b94acceSBarry Smith 1584c7afd0dbSLois Curfman McInnes Calling sequence of func: 1585a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 1586c7afd0dbSLois Curfman McInnes 1587c7afd0dbSLois Curfman McInnes + snes - the SNES context 1588c7afd0dbSLois Curfman McInnes . its - iteration number 1589c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 159040a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 15919b94acceSBarry Smith 15929665c990SLois Curfman McInnes Options Database Keys: 1593a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 1594a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 1595a6570f20SBarry Smith uses SNESMonitorLGCreate() 1596a6570f20SBarry Smith _ -snes_monitor_cancel - cancels all monitors that have 1597c7afd0dbSLois Curfman McInnes been hardwired into a code by 1598a6570f20SBarry Smith calls to SNESMonitorSet(), but 1599c7afd0dbSLois Curfman McInnes does not cancel those set via 1600c7afd0dbSLois Curfman McInnes the options database. 16019665c990SLois Curfman McInnes 1602639f9d9dSBarry Smith Notes: 16036bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 1604a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 16056bc08f3fSLois Curfman McInnes order in which they were set. 1606639f9d9dSBarry Smith 1607025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 1608025f1a04SBarry Smith 160936851e7fSLois Curfman McInnes Level: intermediate 161036851e7fSLois Curfman McInnes 16119b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 16129b94acceSBarry Smith 1613a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 16149b94acceSBarry Smith @*/ 1615b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 16169b94acceSBarry Smith { 1617b90d0a6eSBarry Smith PetscInt i; 1618b90d0a6eSBarry Smith 16193a40ed3dSBarry Smith PetscFunctionBegin; 16204482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1621639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 162229bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1623639f9d9dSBarry Smith } 1624b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 1625b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 1626b90d0a6eSBarry Smith 1627b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 1628b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 1629b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 1630b90d0a6eSBarry Smith PetscErrorCode ierr; 1631b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 1632b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 1633b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 1634b90d0a6eSBarry Smith ierr = (*monitordestroy)(mctx);CHKERRQ(ierr); 1635b90d0a6eSBarry Smith PetscFunctionReturn(0); 1636b90d0a6eSBarry Smith } 1637b90d0a6eSBarry Smith } 1638b90d0a6eSBarry Smith } 1639b90d0a6eSBarry Smith } 1640b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 1641b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1642639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 16433a40ed3dSBarry Smith PetscFunctionReturn(0); 16449b94acceSBarry Smith } 16459b94acceSBarry Smith 16464a2ae208SSatish Balay #undef __FUNCT__ 1647a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 16485cd90555SBarry Smith /*@C 1649a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 16505cd90555SBarry Smith 1651c7afd0dbSLois Curfman McInnes Collective on SNES 1652c7afd0dbSLois Curfman McInnes 16535cd90555SBarry Smith Input Parameters: 16545cd90555SBarry Smith . snes - the SNES context 16555cd90555SBarry Smith 16561a480d89SAdministrator Options Database Key: 1657a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 1658a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 1659c7afd0dbSLois Curfman McInnes set via the options database 16605cd90555SBarry Smith 16615cd90555SBarry Smith Notes: 16625cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 16635cd90555SBarry Smith 166436851e7fSLois Curfman McInnes Level: intermediate 166536851e7fSLois Curfman McInnes 16665cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 16675cd90555SBarry Smith 1668a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 16695cd90555SBarry Smith @*/ 1670a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes) 16715cd90555SBarry Smith { 1672d952e501SBarry Smith PetscErrorCode ierr; 1673d952e501SBarry Smith PetscInt i; 1674d952e501SBarry Smith 16755cd90555SBarry Smith PetscFunctionBegin; 16764482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1677d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1678d952e501SBarry Smith if (snes->monitordestroy[i]) { 1679d952e501SBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1680d952e501SBarry Smith } 1681d952e501SBarry Smith } 16825cd90555SBarry Smith snes->numbermonitors = 0; 16835cd90555SBarry Smith PetscFunctionReturn(0); 16845cd90555SBarry Smith } 16855cd90555SBarry Smith 16864a2ae208SSatish Balay #undef __FUNCT__ 16874a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 16889b94acceSBarry Smith /*@C 16899b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 16909b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 16919b94acceSBarry Smith 1692fee21e36SBarry Smith Collective on SNES 1693fee21e36SBarry Smith 1694c7afd0dbSLois Curfman McInnes Input Parameters: 1695c7afd0dbSLois Curfman McInnes + snes - the SNES context 1696c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 16977f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 16987f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 16999b94acceSBarry Smith 1700c7afd0dbSLois Curfman McInnes Calling sequence of func: 170106ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1702c7afd0dbSLois Curfman McInnes 1703c7afd0dbSLois Curfman McInnes + snes - the SNES context 170406ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 1705c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1706184914b5SBarry Smith . reason - reason for convergence/divergence 1707c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 17084b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 17094b27c08aSLois Curfman McInnes - f - 2-norm of function 17109b94acceSBarry Smith 171136851e7fSLois Curfman McInnes Level: advanced 171236851e7fSLois Curfman McInnes 17139b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 17149b94acceSBarry Smith 171585385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 17169b94acceSBarry Smith @*/ 17177f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 17189b94acceSBarry Smith { 17197f7931b9SBarry Smith PetscErrorCode ierr; 17207f7931b9SBarry Smith 17213a40ed3dSBarry Smith PetscFunctionBegin; 17224482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 172385385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 17247f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 17257f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 17267f7931b9SBarry Smith } 172785385478SLisandro Dalcin snes->ops->converged = func; 17287f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 172985385478SLisandro Dalcin snes->cnvP = cctx; 17303a40ed3dSBarry Smith PetscFunctionReturn(0); 17319b94acceSBarry Smith } 17329b94acceSBarry Smith 17334a2ae208SSatish Balay #undef __FUNCT__ 17344a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 173552baeb72SSatish Balay /*@ 1736184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1737184914b5SBarry Smith 1738184914b5SBarry Smith Not Collective 1739184914b5SBarry Smith 1740184914b5SBarry Smith Input Parameter: 1741184914b5SBarry Smith . snes - the SNES context 1742184914b5SBarry Smith 1743184914b5SBarry Smith Output Parameter: 17444d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 1745184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1746184914b5SBarry Smith 1747184914b5SBarry Smith Level: intermediate 1748184914b5SBarry Smith 1749184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1750184914b5SBarry Smith 1751184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1752184914b5SBarry Smith 175385385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 1754184914b5SBarry Smith @*/ 175563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1756184914b5SBarry Smith { 1757184914b5SBarry Smith PetscFunctionBegin; 17584482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 17594482741eSBarry Smith PetscValidPointer(reason,2); 1760184914b5SBarry Smith *reason = snes->reason; 1761184914b5SBarry Smith PetscFunctionReturn(0); 1762184914b5SBarry Smith } 1763184914b5SBarry Smith 17644a2ae208SSatish Balay #undef __FUNCT__ 17654a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 1766c9005455SLois Curfman McInnes /*@ 1767c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1768c9005455SLois Curfman McInnes 1769fee21e36SBarry Smith Collective on SNES 1770fee21e36SBarry Smith 1771c7afd0dbSLois Curfman McInnes Input Parameters: 1772c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1773c7afd0dbSLois Curfman McInnes . a - array to hold history 1774cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 1775758f92a0SBarry Smith . na - size of a and its 177664731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 1777758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1778c7afd0dbSLois Curfman McInnes 1779c9005455SLois Curfman McInnes Notes: 17804b27c08aSLois Curfman McInnes If set, this array will contain the function norms computed 1781c9005455SLois Curfman McInnes at each step. 1782c9005455SLois Curfman McInnes 1783c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1784c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1785c9005455SLois Curfman McInnes during the section of code that is being timed. 1786c9005455SLois Curfman McInnes 178736851e7fSLois Curfman McInnes Level: intermediate 178836851e7fSLois Curfman McInnes 1789c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1790758f92a0SBarry Smith 179108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1792758f92a0SBarry Smith 1793c9005455SLois Curfman McInnes @*/ 1794a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset) 1795c9005455SLois Curfman McInnes { 17963a40ed3dSBarry Smith PetscFunctionBegin; 17974482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 17984482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 1799a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 1800c9005455SLois Curfman McInnes snes->conv_hist = a; 1801758f92a0SBarry Smith snes->conv_hist_its = its; 1802758f92a0SBarry Smith snes->conv_hist_max = na; 1803a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 1804758f92a0SBarry Smith snes->conv_hist_reset = reset; 1805758f92a0SBarry Smith PetscFunctionReturn(0); 1806758f92a0SBarry Smith } 1807758f92a0SBarry Smith 18084a2ae208SSatish Balay #undef __FUNCT__ 18094a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 18100c4c9dddSBarry Smith /*@C 1811758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1812758f92a0SBarry Smith 1813758f92a0SBarry Smith Collective on SNES 1814758f92a0SBarry Smith 1815758f92a0SBarry Smith Input Parameter: 1816758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1817758f92a0SBarry Smith 1818758f92a0SBarry Smith Output Parameters: 1819758f92a0SBarry Smith . a - array to hold history 1820758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1821758f92a0SBarry Smith negative if not converged) for each solve. 1822758f92a0SBarry Smith - na - size of a and its 1823758f92a0SBarry Smith 1824758f92a0SBarry Smith Notes: 1825758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1826758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1827758f92a0SBarry Smith 1828758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1829758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1830758f92a0SBarry Smith during the section of code that is being timed. 1831758f92a0SBarry Smith 1832758f92a0SBarry Smith Level: intermediate 1833758f92a0SBarry Smith 1834758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1835758f92a0SBarry Smith 1836758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1837758f92a0SBarry Smith 1838758f92a0SBarry Smith @*/ 183963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 1840758f92a0SBarry Smith { 1841758f92a0SBarry Smith PetscFunctionBegin; 18424482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1843758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1844758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1845758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 18463a40ed3dSBarry Smith PetscFunctionReturn(0); 1847c9005455SLois Curfman McInnes } 1848c9005455SLois Curfman McInnes 1849e74ef692SMatthew Knepley #undef __FUNCT__ 1850e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 1851ac226902SBarry Smith /*@C 185276b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 18537e4bb74cSBarry Smith at the beginning o every iteration of the nonlinear solve. Specifically 18547e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 185576b2cf59SMatthew Knepley 185676b2cf59SMatthew Knepley Collective on SNES 185776b2cf59SMatthew Knepley 185876b2cf59SMatthew Knepley Input Parameters: 185976b2cf59SMatthew Knepley . snes - The nonlinear solver context 186076b2cf59SMatthew Knepley . func - The function 186176b2cf59SMatthew Knepley 186276b2cf59SMatthew Knepley Calling sequence of func: 1863b5d30489SBarry Smith . func (SNES snes, PetscInt step); 186476b2cf59SMatthew Knepley 186576b2cf59SMatthew Knepley . step - The current step of the iteration 186676b2cf59SMatthew Knepley 186776b2cf59SMatthew Knepley Level: intermediate 186876b2cf59SMatthew Knepley 186976b2cf59SMatthew Knepley .keywords: SNES, update 1870b5d30489SBarry Smith 187185385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 187276b2cf59SMatthew Knepley @*/ 187363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 187476b2cf59SMatthew Knepley { 187576b2cf59SMatthew Knepley PetscFunctionBegin; 18764482741eSBarry Smith PetscValidHeaderSpecific(snes, SNES_COOKIE,1); 1877e7788613SBarry Smith snes->ops->update = func; 187876b2cf59SMatthew Knepley PetscFunctionReturn(0); 187976b2cf59SMatthew Knepley } 188076b2cf59SMatthew Knepley 1881e74ef692SMatthew Knepley #undef __FUNCT__ 1882e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 188376b2cf59SMatthew Knepley /*@ 188476b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 188576b2cf59SMatthew Knepley 188676b2cf59SMatthew Knepley Not collective 188776b2cf59SMatthew Knepley 188876b2cf59SMatthew Knepley Input Parameters: 188976b2cf59SMatthew Knepley . snes - The nonlinear solver context 189076b2cf59SMatthew Knepley . step - The current step of the iteration 189176b2cf59SMatthew Knepley 1892205452f4SMatthew Knepley Level: intermediate 1893205452f4SMatthew Knepley 189476b2cf59SMatthew Knepley .keywords: SNES, update 1895a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 189676b2cf59SMatthew Knepley @*/ 189763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step) 189876b2cf59SMatthew Knepley { 189976b2cf59SMatthew Knepley PetscFunctionBegin; 190076b2cf59SMatthew Knepley PetscFunctionReturn(0); 190176b2cf59SMatthew Knepley } 190276b2cf59SMatthew Knepley 19034a2ae208SSatish Balay #undef __FUNCT__ 19044a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 19059b94acceSBarry Smith /* 19069b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 19079b94acceSBarry Smith positive parameter delta. 19089b94acceSBarry Smith 19099b94acceSBarry Smith Input Parameters: 1910c7afd0dbSLois Curfman McInnes + snes - the SNES context 19119b94acceSBarry Smith . y - approximate solution of linear system 19129b94acceSBarry Smith . fnorm - 2-norm of current function 1913c7afd0dbSLois Curfman McInnes - delta - trust region size 19149b94acceSBarry Smith 19159b94acceSBarry Smith Output Parameters: 1916c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 19179b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 19189b94acceSBarry Smith region, and exceeds zero otherwise. 1919c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 19209b94acceSBarry Smith 19219b94acceSBarry Smith Note: 19224b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 19239b94acceSBarry Smith is set to be the maximum allowable step size. 19249b94acceSBarry Smith 19259b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 19269b94acceSBarry Smith */ 1927dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 19289b94acceSBarry Smith { 1929064f8208SBarry Smith PetscReal nrm; 1930ea709b57SSatish Balay PetscScalar cnorm; 1931dfbe8321SBarry Smith PetscErrorCode ierr; 19323a40ed3dSBarry Smith 19333a40ed3dSBarry Smith PetscFunctionBegin; 19344482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 19354482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,2); 1936c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 1937184914b5SBarry Smith 1938064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 1939064f8208SBarry Smith if (nrm > *delta) { 1940064f8208SBarry Smith nrm = *delta/nrm; 1941064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 1942064f8208SBarry Smith cnorm = nrm; 19432dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 19449b94acceSBarry Smith *ynorm = *delta; 19459b94acceSBarry Smith } else { 19469b94acceSBarry Smith *gpnorm = 0.0; 1947064f8208SBarry Smith *ynorm = nrm; 19489b94acceSBarry Smith } 19493a40ed3dSBarry Smith PetscFunctionReturn(0); 19509b94acceSBarry Smith } 19519b94acceSBarry Smith 19524a2ae208SSatish Balay #undef __FUNCT__ 19534a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 19546ce558aeSBarry Smith /*@C 1955f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 1956f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 19579b94acceSBarry Smith 1958c7afd0dbSLois Curfman McInnes Collective on SNES 1959c7afd0dbSLois Curfman McInnes 1960b2002411SLois Curfman McInnes Input Parameters: 1961c7afd0dbSLois Curfman McInnes + snes - the SNES context 1962f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 196385385478SLisandro Dalcin - x - the solution vector. 19649b94acceSBarry Smith 1965b2002411SLois Curfman McInnes Notes: 19668ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 19678ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 19688ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 19698ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 19708ddd3da0SLois Curfman McInnes 197136851e7fSLois Curfman McInnes Level: beginner 197236851e7fSLois Curfman McInnes 19739b94acceSBarry Smith .keywords: SNES, nonlinear, solve 19749b94acceSBarry Smith 197585385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 19769b94acceSBarry Smith @*/ 1977f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x) 19789b94acceSBarry Smith { 1979dfbe8321SBarry Smith PetscErrorCode ierr; 1980f1af5d2fSBarry Smith PetscTruth flg; 1981eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 1982eabae89aSBarry Smith PetscViewer viewer; 1983052efed2SBarry Smith 19843a40ed3dSBarry Smith PetscFunctionBegin; 19854482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1986f69a0ea3SMatthew Knepley PetscValidHeaderSpecific(x,VEC_COOKIE,3); 1987f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 198885385478SLisandro Dalcin if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2); 198985385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 199085385478SLisandro Dalcin 199185385478SLisandro Dalcin /* set solution vector */ 199285385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 199385385478SLisandro Dalcin if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); } 199485385478SLisandro Dalcin snes->vec_sol = x; 199585385478SLisandro Dalcin /* set afine vector if provided */ 199685385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 199785385478SLisandro Dalcin if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); } 199885385478SLisandro Dalcin snes->vec_rhs = b; 199985385478SLisandro Dalcin 200085385478SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 200185385478SLisandro Dalcin ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr); 200270e92668SMatthew Knepley } 20033f149594SLisandro Dalcin 200470e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 20053f149594SLisandro Dalcin 2006abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 200750ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2008d5e45103SBarry Smith 20093f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 20104936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 201185385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 20124936397dSBarry Smith if (snes->domainerror){ 20134936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 20144936397dSBarry Smith snes->domainerror = PETSC_FALSE; 20154936397dSBarry Smith } 201685385478SLisandro Dalcin 20173f149594SLisandro Dalcin if (!snes->reason) { 20183f149594SLisandro Dalcin SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 20193f149594SLisandro Dalcin } 20203f149594SLisandro Dalcin 20217adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2022eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 20237adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2024eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 2025eabae89aSBarry Smith ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 2026eabae89aSBarry Smith } 2027eabae89aSBarry Smith 20287adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr); 2029da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 20305968eb51SBarry Smith if (snes->printreason) { 20315968eb51SBarry Smith if (snes->reason > 0) { 20327adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 20335968eb51SBarry Smith } else { 20347adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 20355968eb51SBarry Smith } 20365968eb51SBarry Smith } 20375968eb51SBarry Smith 20383a40ed3dSBarry Smith PetscFunctionReturn(0); 20399b94acceSBarry Smith } 20409b94acceSBarry Smith 20419b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 20429b94acceSBarry Smith 20434a2ae208SSatish Balay #undef __FUNCT__ 20444a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 204582bf6240SBarry Smith /*@C 20464b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 20479b94acceSBarry Smith 2048fee21e36SBarry Smith Collective on SNES 2049fee21e36SBarry Smith 2050c7afd0dbSLois Curfman McInnes Input Parameters: 2051c7afd0dbSLois Curfman McInnes + snes - the SNES context 2052454a90a3SBarry Smith - type - a known method 2053c7afd0dbSLois Curfman McInnes 2054c7afd0dbSLois Curfman McInnes Options Database Key: 2055454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2056c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2057ae12b187SLois Curfman McInnes 20589b94acceSBarry Smith Notes: 2059e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 20604b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2061c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20624b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2063c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20649b94acceSBarry Smith 2065ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2066ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2067ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2068ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2069ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2070ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2071ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2072ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2073ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2074b0a32e0cSBarry Smith appropriate method. 207536851e7fSLois Curfman McInnes 207636851e7fSLois Curfman McInnes Level: intermediate 2077a703fe33SLois Curfman McInnes 2078454a90a3SBarry Smith .keywords: SNES, set, type 2079435da068SBarry Smith 2080435da068SBarry Smith .seealso: SNESType, SNESCreate() 2081435da068SBarry Smith 20829b94acceSBarry Smith @*/ 2083a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type) 20849b94acceSBarry Smith { 2085dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 20866831982aSBarry Smith PetscTruth match; 20873a40ed3dSBarry Smith 20883a40ed3dSBarry Smith PetscFunctionBegin; 20894482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20904482741eSBarry Smith PetscValidCharPointer(type,2); 209182bf6240SBarry Smith 20926831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 20930f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 209492ff6ae8SBarry Smith 20957adad957SLisandro Dalcin ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 2096958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 209775396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 209875396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 209975396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 210075396ef9SLisandro Dalcin snes->ops->setup = 0; 210175396ef9SLisandro Dalcin snes->ops->solve = 0; 210275396ef9SLisandro Dalcin snes->ops->view = 0; 210375396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 210475396ef9SLisandro Dalcin snes->ops->destroy = 0; 210575396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 210675396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 21073a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 2108454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 21093a40ed3dSBarry Smith PetscFunctionReturn(0); 21109b94acceSBarry Smith } 21119b94acceSBarry Smith 2112a847f771SSatish Balay 21139b94acceSBarry Smith /* --------------------------------------------------------------------- */ 21144a2ae208SSatish Balay #undef __FUNCT__ 21154a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 211652baeb72SSatish Balay /*@ 21179b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2118f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 21199b94acceSBarry Smith 2120fee21e36SBarry Smith Not Collective 2121fee21e36SBarry Smith 212236851e7fSLois Curfman McInnes Level: advanced 212336851e7fSLois Curfman McInnes 21249b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 21259b94acceSBarry Smith 21269b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 21279b94acceSBarry Smith @*/ 212863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void) 21299b94acceSBarry Smith { 2130dfbe8321SBarry Smith PetscErrorCode ierr; 213182bf6240SBarry Smith 21323a40ed3dSBarry Smith PetscFunctionBegin; 21331441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 21344c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 21353a40ed3dSBarry Smith PetscFunctionReturn(0); 21369b94acceSBarry Smith } 21379b94acceSBarry Smith 21384a2ae208SSatish Balay #undef __FUNCT__ 21394a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 21409b94acceSBarry Smith /*@C 21419a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 21429b94acceSBarry Smith 2143c7afd0dbSLois Curfman McInnes Not Collective 2144c7afd0dbSLois Curfman McInnes 21459b94acceSBarry Smith Input Parameter: 21464b0e389bSBarry Smith . snes - nonlinear solver context 21479b94acceSBarry Smith 21489b94acceSBarry Smith Output Parameter: 21493a7fca6bSBarry Smith . type - SNES method (a character string) 21509b94acceSBarry Smith 215136851e7fSLois Curfman McInnes Level: intermediate 215236851e7fSLois Curfman McInnes 2153454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 21549b94acceSBarry Smith @*/ 2155a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type) 21569b94acceSBarry Smith { 21573a40ed3dSBarry Smith PetscFunctionBegin; 21584482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 21594482741eSBarry Smith PetscValidPointer(type,2); 21607adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 21613a40ed3dSBarry Smith PetscFunctionReturn(0); 21629b94acceSBarry Smith } 21639b94acceSBarry Smith 21644a2ae208SSatish Balay #undef __FUNCT__ 21654a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 216652baeb72SSatish Balay /*@ 21679b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 21689b94acceSBarry Smith stored. 21699b94acceSBarry Smith 2170c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2171c7afd0dbSLois Curfman McInnes 21729b94acceSBarry Smith Input Parameter: 21739b94acceSBarry Smith . snes - the SNES context 21749b94acceSBarry Smith 21759b94acceSBarry Smith Output Parameter: 21769b94acceSBarry Smith . x - the solution 21779b94acceSBarry Smith 217870e92668SMatthew Knepley Level: intermediate 217936851e7fSLois Curfman McInnes 21809b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 21819b94acceSBarry Smith 218285385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 21839b94acceSBarry Smith @*/ 218463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x) 21859b94acceSBarry Smith { 21863a40ed3dSBarry Smith PetscFunctionBegin; 21874482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 21884482741eSBarry Smith PetscValidPointer(x,2); 218985385478SLisandro Dalcin *x = snes->vec_sol; 219070e92668SMatthew Knepley PetscFunctionReturn(0); 219170e92668SMatthew Knepley } 219270e92668SMatthew Knepley 219370e92668SMatthew Knepley #undef __FUNCT__ 21944a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 219552baeb72SSatish Balay /*@ 21969b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 21979b94acceSBarry Smith stored. 21989b94acceSBarry Smith 2199c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2200c7afd0dbSLois Curfman McInnes 22019b94acceSBarry Smith Input Parameter: 22029b94acceSBarry Smith . snes - the SNES context 22039b94acceSBarry Smith 22049b94acceSBarry Smith Output Parameter: 22059b94acceSBarry Smith . x - the solution update 22069b94acceSBarry Smith 220736851e7fSLois Curfman McInnes Level: advanced 220836851e7fSLois Curfman McInnes 22099b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 22109b94acceSBarry Smith 221185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 22129b94acceSBarry Smith @*/ 221363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x) 22149b94acceSBarry Smith { 22153a40ed3dSBarry Smith PetscFunctionBegin; 22164482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 22174482741eSBarry Smith PetscValidPointer(x,2); 221885385478SLisandro Dalcin *x = snes->vec_sol_update; 22193a40ed3dSBarry Smith PetscFunctionReturn(0); 22209b94acceSBarry Smith } 22219b94acceSBarry Smith 22224a2ae208SSatish Balay #undef __FUNCT__ 22234a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 22249b94acceSBarry Smith /*@C 22253638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 22269b94acceSBarry Smith 2227c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2228c7afd0dbSLois Curfman McInnes 22299b94acceSBarry Smith Input Parameter: 22309b94acceSBarry Smith . snes - the SNES context 22319b94acceSBarry Smith 22329b94acceSBarry Smith Output Parameter: 22337bf4e008SBarry Smith + r - the function (or PETSC_NULL) 223470e92668SMatthew Knepley . func - the function (or PETSC_NULL) 223570e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 22369b94acceSBarry Smith 223736851e7fSLois Curfman McInnes Level: advanced 223836851e7fSLois Curfman McInnes 2239a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 22409b94acceSBarry Smith 22414b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 22429b94acceSBarry Smith @*/ 224370e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 22449b94acceSBarry Smith { 22453a40ed3dSBarry Smith PetscFunctionBegin; 22464482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 224785385478SLisandro Dalcin if (r) *r = snes->vec_func; 2248e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 224970e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 22503a40ed3dSBarry Smith PetscFunctionReturn(0); 22519b94acceSBarry Smith } 22529b94acceSBarry Smith 22534a2ae208SSatish Balay #undef __FUNCT__ 22544a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 22553c7409f5SSatish Balay /*@C 22563c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2257d850072dSLois Curfman McInnes SNES options in the database. 22583c7409f5SSatish Balay 2259fee21e36SBarry Smith Collective on SNES 2260fee21e36SBarry Smith 2261c7afd0dbSLois Curfman McInnes Input Parameter: 2262c7afd0dbSLois Curfman McInnes + snes - the SNES context 2263c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2264c7afd0dbSLois Curfman McInnes 2265d850072dSLois Curfman McInnes Notes: 2266a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2267c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2268d850072dSLois Curfman McInnes 226936851e7fSLois Curfman McInnes Level: advanced 227036851e7fSLois Curfman McInnes 22713c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2272a86d99e1SLois Curfman McInnes 2273a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 22743c7409f5SSatish Balay @*/ 227563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[]) 22763c7409f5SSatish Balay { 2277dfbe8321SBarry Smith PetscErrorCode ierr; 22783c7409f5SSatish Balay 22793a40ed3dSBarry Smith PetscFunctionBegin; 22804482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2281639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 2282*1cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 228394b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 22843a40ed3dSBarry Smith PetscFunctionReturn(0); 22853c7409f5SSatish Balay } 22863c7409f5SSatish Balay 22874a2ae208SSatish Balay #undef __FUNCT__ 22884a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 22893c7409f5SSatish Balay /*@C 2290f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2291d850072dSLois Curfman McInnes SNES options in the database. 22923c7409f5SSatish Balay 2293fee21e36SBarry Smith Collective on SNES 2294fee21e36SBarry Smith 2295c7afd0dbSLois Curfman McInnes Input Parameters: 2296c7afd0dbSLois Curfman McInnes + snes - the SNES context 2297c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2298c7afd0dbSLois Curfman McInnes 2299d850072dSLois Curfman McInnes Notes: 2300a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2301c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2302d850072dSLois Curfman McInnes 230336851e7fSLois Curfman McInnes Level: advanced 230436851e7fSLois Curfman McInnes 23053c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2306a86d99e1SLois Curfman McInnes 2307a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 23083c7409f5SSatish Balay @*/ 230963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 23103c7409f5SSatish Balay { 2311dfbe8321SBarry Smith PetscErrorCode ierr; 23123c7409f5SSatish Balay 23133a40ed3dSBarry Smith PetscFunctionBegin; 23144482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2315639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 2316*1cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 231794b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 23183a40ed3dSBarry Smith PetscFunctionReturn(0); 23193c7409f5SSatish Balay } 23203c7409f5SSatish Balay 23214a2ae208SSatish Balay #undef __FUNCT__ 23224a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 23239ab63eb5SSatish Balay /*@C 23243c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 23253c7409f5SSatish Balay SNES options in the database. 23263c7409f5SSatish Balay 2327c7afd0dbSLois Curfman McInnes Not Collective 2328c7afd0dbSLois Curfman McInnes 23293c7409f5SSatish Balay Input Parameter: 23303c7409f5SSatish Balay . snes - the SNES context 23313c7409f5SSatish Balay 23323c7409f5SSatish Balay Output Parameter: 23333c7409f5SSatish Balay . prefix - pointer to the prefix string used 23343c7409f5SSatish Balay 23354ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 23369ab63eb5SSatish Balay sufficient length to hold the prefix. 23379ab63eb5SSatish Balay 233836851e7fSLois Curfman McInnes Level: advanced 233936851e7fSLois Curfman McInnes 23403c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2341a86d99e1SLois Curfman McInnes 2342a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 23433c7409f5SSatish Balay @*/ 2344e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 23453c7409f5SSatish Balay { 2346dfbe8321SBarry Smith PetscErrorCode ierr; 23473c7409f5SSatish Balay 23483a40ed3dSBarry Smith PetscFunctionBegin; 23494482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2350639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 23513a40ed3dSBarry Smith PetscFunctionReturn(0); 23523c7409f5SSatish Balay } 23533c7409f5SSatish Balay 2354b2002411SLois Curfman McInnes 23554a2ae208SSatish Balay #undef __FUNCT__ 23564a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 23573cea93caSBarry Smith /*@C 23583cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 23593cea93caSBarry Smith 23607f6c08e0SMatthew Knepley Level: advanced 23613cea93caSBarry Smith @*/ 236263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2363b2002411SLois Curfman McInnes { 2364e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2365dfbe8321SBarry Smith PetscErrorCode ierr; 2366b2002411SLois Curfman McInnes 2367b2002411SLois Curfman McInnes PetscFunctionBegin; 2368b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2369c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2370b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2371b2002411SLois Curfman McInnes } 2372da9b6338SBarry Smith 2373da9b6338SBarry Smith #undef __FUNCT__ 2374da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 237563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes) 2376da9b6338SBarry Smith { 2377dfbe8321SBarry Smith PetscErrorCode ierr; 237877431f27SBarry Smith PetscInt N,i,j; 2379da9b6338SBarry Smith Vec u,uh,fh; 2380da9b6338SBarry Smith PetscScalar value; 2381da9b6338SBarry Smith PetscReal norm; 2382da9b6338SBarry Smith 2383da9b6338SBarry Smith PetscFunctionBegin; 2384da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 2385da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 2386da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 2387da9b6338SBarry Smith 2388da9b6338SBarry Smith /* currently only works for sequential */ 2389da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 2390da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 2391da9b6338SBarry Smith for (i=0; i<N; i++) { 2392da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 239377431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 2394da9b6338SBarry Smith for (j=-10; j<11; j++) { 2395ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 2396da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 23973ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 2398da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 239977431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 2400da9b6338SBarry Smith value = -value; 2401da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 2402da9b6338SBarry Smith } 2403da9b6338SBarry Smith } 2404da9b6338SBarry Smith ierr = VecDestroy(uh);CHKERRQ(ierr); 2405da9b6338SBarry Smith ierr = VecDestroy(fh);CHKERRQ(ierr); 2406da9b6338SBarry Smith PetscFunctionReturn(0); 2407da9b6338SBarry Smith } 240871f87433Sdalcinl 240971f87433Sdalcinl #undef __FUNCT__ 2410fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 241171f87433Sdalcinl /*@ 2412fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 241371f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 241471f87433Sdalcinl Newton method. 241571f87433Sdalcinl 241671f87433Sdalcinl Collective on SNES 241771f87433Sdalcinl 241871f87433Sdalcinl Input Parameters: 241971f87433Sdalcinl + snes - SNES context 242071f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 242171f87433Sdalcinl 242271f87433Sdalcinl Notes: 242371f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 242471f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 242571f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 242671f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 242771f87433Sdalcinl solver. 242871f87433Sdalcinl 242971f87433Sdalcinl Level: advanced 243071f87433Sdalcinl 243171f87433Sdalcinl Reference: 243271f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 243371f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 243471f87433Sdalcinl 243571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 243671f87433Sdalcinl 2437fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 243871f87433Sdalcinl @*/ 2439fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag) 244071f87433Sdalcinl { 244171f87433Sdalcinl PetscFunctionBegin; 244271f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 244371f87433Sdalcinl snes->ksp_ewconv = flag; 244471f87433Sdalcinl PetscFunctionReturn(0); 244571f87433Sdalcinl } 244671f87433Sdalcinl 244771f87433Sdalcinl #undef __FUNCT__ 2448fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 244971f87433Sdalcinl /*@ 2450fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 245171f87433Sdalcinl for computing relative tolerance for linear solvers within an 245271f87433Sdalcinl inexact Newton method. 245371f87433Sdalcinl 245471f87433Sdalcinl Not Collective 245571f87433Sdalcinl 245671f87433Sdalcinl Input Parameter: 245771f87433Sdalcinl . snes - SNES context 245871f87433Sdalcinl 245971f87433Sdalcinl Output Parameter: 246071f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 246171f87433Sdalcinl 246271f87433Sdalcinl Notes: 246371f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 246471f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 246571f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 246671f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 246771f87433Sdalcinl solver. 246871f87433Sdalcinl 246971f87433Sdalcinl Level: advanced 247071f87433Sdalcinl 247171f87433Sdalcinl Reference: 247271f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 247371f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 247471f87433Sdalcinl 247571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 247671f87433Sdalcinl 2477fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 247871f87433Sdalcinl @*/ 2479fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag) 248071f87433Sdalcinl { 248171f87433Sdalcinl PetscFunctionBegin; 248271f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 248371f87433Sdalcinl PetscValidPointer(flag,2); 248471f87433Sdalcinl *flag = snes->ksp_ewconv; 248571f87433Sdalcinl PetscFunctionReturn(0); 248671f87433Sdalcinl } 248771f87433Sdalcinl 248871f87433Sdalcinl #undef __FUNCT__ 2489fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 249071f87433Sdalcinl /*@ 2491fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 249271f87433Sdalcinl convergence criteria for the linear solvers within an inexact 249371f87433Sdalcinl Newton method. 249471f87433Sdalcinl 249571f87433Sdalcinl Collective on SNES 249671f87433Sdalcinl 249771f87433Sdalcinl Input Parameters: 249871f87433Sdalcinl + snes - SNES context 249971f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 250071f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 250171f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 250271f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 250371f87433Sdalcinl (0 <= gamma2 <= 1) 250471f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 250571f87433Sdalcinl . alpha2 - power for safeguard 250671f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 250771f87433Sdalcinl 250871f87433Sdalcinl Note: 250971f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 251071f87433Sdalcinl 251171f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 251271f87433Sdalcinl 251371f87433Sdalcinl Level: advanced 251471f87433Sdalcinl 251571f87433Sdalcinl Reference: 251671f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 251771f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 251871f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 251971f87433Sdalcinl 252071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 252171f87433Sdalcinl 2522fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 252371f87433Sdalcinl @*/ 2524fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 252571f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 252671f87433Sdalcinl { 2527fa9f3622SBarry Smith SNESKSPEW *kctx; 252871f87433Sdalcinl PetscFunctionBegin; 252971f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2530fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 253171f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 253271f87433Sdalcinl 253371f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 253471f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 253571f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 253671f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 253771f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 253871f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 253971f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 254071f87433Sdalcinl 254171f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 254271f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 254371f87433Sdalcinl } 254471f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 254571f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 254671f87433Sdalcinl } 254771f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 254871f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 254971f87433Sdalcinl } 255071f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 255171f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 255271f87433Sdalcinl } 255371f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 255471f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 255571f87433Sdalcinl } 255671f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 255771f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 255871f87433Sdalcinl } 255971f87433Sdalcinl PetscFunctionReturn(0); 256071f87433Sdalcinl } 256171f87433Sdalcinl 256271f87433Sdalcinl #undef __FUNCT__ 2563fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 256471f87433Sdalcinl /*@ 2565fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 256671f87433Sdalcinl convergence criteria for the linear solvers within an inexact 256771f87433Sdalcinl Newton method. 256871f87433Sdalcinl 256971f87433Sdalcinl Not Collective 257071f87433Sdalcinl 257171f87433Sdalcinl Input Parameters: 257271f87433Sdalcinl snes - SNES context 257371f87433Sdalcinl 257471f87433Sdalcinl Output Parameters: 257571f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 257671f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 257771f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 257871f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 257971f87433Sdalcinl (0 <= gamma2 <= 1) 258071f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 258171f87433Sdalcinl . alpha2 - power for safeguard 258271f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 258371f87433Sdalcinl 258471f87433Sdalcinl Level: advanced 258571f87433Sdalcinl 258671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 258771f87433Sdalcinl 2588fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 258971f87433Sdalcinl @*/ 2590fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 259171f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 259271f87433Sdalcinl { 2593fa9f3622SBarry Smith SNESKSPEW *kctx; 259471f87433Sdalcinl PetscFunctionBegin; 259571f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2596fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 259771f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 259871f87433Sdalcinl if(version) *version = kctx->version; 259971f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 260071f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 260171f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 260271f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 260371f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 260471f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 260571f87433Sdalcinl PetscFunctionReturn(0); 260671f87433Sdalcinl } 260771f87433Sdalcinl 260871f87433Sdalcinl #undef __FUNCT__ 2609fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 2610fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 261171f87433Sdalcinl { 261271f87433Sdalcinl PetscErrorCode ierr; 2613fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 261471f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 261571f87433Sdalcinl 261671f87433Sdalcinl PetscFunctionBegin; 261771f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 261871f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 261971f87433Sdalcinl rtol = kctx->rtol_0; 262071f87433Sdalcinl } else { 262171f87433Sdalcinl if (kctx->version == 1) { 262271f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 262371f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 262471f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 262571f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 262671f87433Sdalcinl } else if (kctx->version == 2) { 262771f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 262871f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 262971f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 263071f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 263171f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 263271f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 263371f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 263471f87433Sdalcinl stol = PetscMax(rtol,stol); 263571f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 263671f87433Sdalcinl /* safeguard: avoid oversolving */ 263771f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 263871f87433Sdalcinl stol = PetscMax(rtol,stol); 263971f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 264071f87433Sdalcinl } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 264171f87433Sdalcinl } 264271f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 264371f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 264471f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 264571f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 264671f87433Sdalcinl PetscFunctionReturn(0); 264771f87433Sdalcinl } 264871f87433Sdalcinl 264971f87433Sdalcinl #undef __FUNCT__ 2650fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 2651fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 265271f87433Sdalcinl { 265371f87433Sdalcinl PetscErrorCode ierr; 2654fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 265571f87433Sdalcinl PCSide pcside; 265671f87433Sdalcinl Vec lres; 265771f87433Sdalcinl 265871f87433Sdalcinl PetscFunctionBegin; 265971f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 266071f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 266171f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 266271f87433Sdalcinl if (kctx->version == 1) { 266371f87433Sdalcinl ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr); 266471f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 266571f87433Sdalcinl /* KSP residual is true linear residual */ 266671f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 266771f87433Sdalcinl } else { 266871f87433Sdalcinl /* KSP residual is preconditioned residual */ 266971f87433Sdalcinl /* compute true linear residual norm */ 267071f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 267171f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 267271f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 267371f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 267471f87433Sdalcinl ierr = VecDestroy(lres);CHKERRQ(ierr); 267571f87433Sdalcinl } 267671f87433Sdalcinl } 267771f87433Sdalcinl PetscFunctionReturn(0); 267871f87433Sdalcinl } 267971f87433Sdalcinl 268071f87433Sdalcinl #undef __FUNCT__ 268171f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 268271f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 268371f87433Sdalcinl { 268471f87433Sdalcinl PetscErrorCode ierr; 268571f87433Sdalcinl 268671f87433Sdalcinl PetscFunctionBegin; 2687fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 268871f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 2689fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 269071f87433Sdalcinl PetscFunctionReturn(0); 269171f87433Sdalcinl } 2692