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) 180e35cf81dSBarry Smith . -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild) 181b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 1822492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 18382738288SBarry Smith solver; hence iterations will continue until max_it 1841fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 18582738288SBarry Smith of convergence test 186e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 187e8105e01SRichard Katz filename given prints to stdout 188a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 189a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 190a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 191a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 192e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 1935968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 194fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 19582738288SBarry Smith 19682738288SBarry Smith Options Database for Eisenstat-Walker method: 197fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 1984b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 19936851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 20036851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 20136851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 20236851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 20336851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 20436851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 20582738288SBarry Smith 20611ca99fdSLois Curfman McInnes Notes: 20711ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 20811ca99fdSLois Curfman McInnes the users manual. 20983e2fdc7SBarry Smith 21036851e7fSLois Curfman McInnes Level: beginner 21136851e7fSLois Curfman McInnes 2129b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 2139b94acceSBarry Smith 21469ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 2159b94acceSBarry Smith @*/ 21663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes) 2179b94acceSBarry Smith { 218f1af5d2fSBarry Smith PetscTruth flg; 219a8054027SBarry Smith PetscInt i,indx,lag; 22085385478SLisandro Dalcin const char *deft = SNESLS; 22185385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 22285385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 223e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 22423d894e5SBarry Smith PetscViewerASCIIMonitor monviewer; 22585385478SLisandro Dalcin PetscErrorCode ierr; 2269b94acceSBarry Smith 2273a40ed3dSBarry Smith PetscFunctionBegin; 2284482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 229ca161407SBarry Smith 2307adad957SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr); 231186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2327adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 233b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 234d64ed03dSBarry Smith if (flg) { 235186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 2367adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 237186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 238d64ed03dSBarry Smith } 239909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 24093c39befSBarry Smith 24187828ca2SBarry Smith ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 24270441072SBarry Smith ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 243186905e3SBarry Smith 24487828ca2SBarry Smith ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 245b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 246b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 24750ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 248ddf469c8SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr); 24985385478SLisandro Dalcin 250a8054027SBarry Smith ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr); 251a8054027SBarry Smith if (flg) { 252a8054027SBarry Smith ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr); 253a8054027SBarry Smith } 254e35cf81dSBarry Smith ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr); 255e35cf81dSBarry Smith if (flg) { 256e35cf81dSBarry Smith ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr); 257e35cf81dSBarry Smith } 258a8054027SBarry Smith 25985385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 26085385478SLisandro Dalcin if (flg) { 26185385478SLisandro Dalcin switch (indx) { 2627f7931b9SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 2637f7931b9SBarry Smith case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 26485385478SLisandro Dalcin } 26585385478SLisandro Dalcin } 26685385478SLisandro Dalcin 2675968eb51SBarry Smith ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr); 2685968eb51SBarry Smith if (flg) { 2695968eb51SBarry Smith snes->printreason = PETSC_TRUE; 2705968eb51SBarry Smith } 271186905e3SBarry Smith 27285385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 27385385478SLisandro Dalcin 274fa9f3622SBarry Smith ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 275186905e3SBarry Smith 276fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 277fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 278fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 279fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 280fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 281fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 282fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 283186905e3SBarry Smith 284a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr); 285a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 286eabae89aSBarry Smith 287a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 288e8105e01SRichard Katz if (flg) { 289050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 29023d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 291e8105e01SRichard Katz } 292eabae89aSBarry Smith 293*b271bb04SBarry Smith ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 294*b271bb04SBarry Smith if (flg) { 295*b271bb04SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr); 296*b271bb04SBarry Smith } 297*b271bb04SBarry Smith 298a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 299eabae89aSBarry Smith if (flg) { 300050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 301f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 302e8105e01SRichard Katz } 303eabae89aSBarry Smith 304a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 305eabae89aSBarry Smith if (flg) { 306050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 30723d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 308eabae89aSBarry Smith } 309eabae89aSBarry Smith 310a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr); 311a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 312a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr); 313a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 314a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr); 315a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 316a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr); 317a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 318*b271bb04SBarry Smith ierr = PetscOptionsName("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr); 319*b271bb04SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 320e24b481bSBarry Smith 321b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr); 3224b27c08aSLois Curfman McInnes if (flg) { 323186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 324ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 3259b94acceSBarry Smith } 326639f9d9dSBarry Smith 32776b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 32876b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 32976b2cf59SMatthew Knepley } 33076b2cf59SMatthew Knepley 331e7788613SBarry Smith if (snes->ops->setfromoptions) { 332e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 333639f9d9dSBarry Smith } 334b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3354bbc92c1SBarry Smith 3361cee3971SBarry Smith 3371cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 33885385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 33993993e2dSLois Curfman McInnes 3403a40ed3dSBarry Smith PetscFunctionReturn(0); 3419b94acceSBarry Smith } 3429b94acceSBarry Smith 343a847f771SSatish Balay 3444a2ae208SSatish Balay #undef __FUNCT__ 3454a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 3469b94acceSBarry Smith /*@ 3479b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3489b94acceSBarry Smith the nonlinear solvers. 3499b94acceSBarry Smith 350fee21e36SBarry Smith Collective on SNES 351fee21e36SBarry Smith 352c7afd0dbSLois Curfman McInnes Input Parameters: 353c7afd0dbSLois Curfman McInnes + snes - the SNES context 354c7afd0dbSLois Curfman McInnes - usrP - optional user context 355c7afd0dbSLois Curfman McInnes 35636851e7fSLois Curfman McInnes Level: intermediate 35736851e7fSLois Curfman McInnes 3589b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3599b94acceSBarry Smith 3609b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3619b94acceSBarry Smith @*/ 36263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP) 3639b94acceSBarry Smith { 3643a40ed3dSBarry Smith PetscFunctionBegin; 3654482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3669b94acceSBarry Smith snes->user = usrP; 3673a40ed3dSBarry Smith PetscFunctionReturn(0); 3689b94acceSBarry Smith } 36974679c65SBarry Smith 3704a2ae208SSatish Balay #undef __FUNCT__ 3714a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 3729b94acceSBarry Smith /*@C 3739b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3749b94acceSBarry Smith nonlinear solvers. 3759b94acceSBarry Smith 376c7afd0dbSLois Curfman McInnes Not Collective 377c7afd0dbSLois Curfman McInnes 3789b94acceSBarry Smith Input Parameter: 3799b94acceSBarry Smith . snes - SNES context 3809b94acceSBarry Smith 3819b94acceSBarry Smith Output Parameter: 3829b94acceSBarry Smith . usrP - user context 3839b94acceSBarry Smith 38436851e7fSLois Curfman McInnes Level: intermediate 38536851e7fSLois Curfman McInnes 3869b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3879b94acceSBarry Smith 3889b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3899b94acceSBarry Smith @*/ 39063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP) 3919b94acceSBarry Smith { 3923a40ed3dSBarry Smith PetscFunctionBegin; 3934482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3949b94acceSBarry Smith *usrP = snes->user; 3953a40ed3dSBarry Smith PetscFunctionReturn(0); 3969b94acceSBarry Smith } 39774679c65SBarry Smith 3984a2ae208SSatish Balay #undef __FUNCT__ 3994a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 4009b94acceSBarry Smith /*@ 401c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 402c8228a4eSBarry Smith at this time. 4039b94acceSBarry Smith 404c7afd0dbSLois Curfman McInnes Not Collective 405c7afd0dbSLois Curfman McInnes 4069b94acceSBarry Smith Input Parameter: 4079b94acceSBarry Smith . snes - SNES context 4089b94acceSBarry Smith 4099b94acceSBarry Smith Output Parameter: 4109b94acceSBarry Smith . iter - iteration number 4119b94acceSBarry Smith 412c8228a4eSBarry Smith Notes: 413c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 414c8228a4eSBarry Smith 415c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 41608405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 41708405cd6SLois Curfman McInnes .vb 41808405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 41908405cd6SLois Curfman McInnes if (!(it % 2)) { 42008405cd6SLois Curfman McInnes [compute Jacobian here] 42108405cd6SLois Curfman McInnes } 42208405cd6SLois Curfman McInnes .ve 423c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 42408405cd6SLois Curfman McInnes recomputed every second SNES iteration. 425c8228a4eSBarry Smith 42636851e7fSLois Curfman McInnes Level: intermediate 42736851e7fSLois Curfman McInnes 4282b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 4292b668275SBarry Smith 430b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 4319b94acceSBarry Smith @*/ 43263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter) 4339b94acceSBarry Smith { 4343a40ed3dSBarry Smith PetscFunctionBegin; 4354482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4364482741eSBarry Smith PetscValidIntPointer(iter,2); 4379b94acceSBarry Smith *iter = snes->iter; 4383a40ed3dSBarry Smith PetscFunctionReturn(0); 4399b94acceSBarry Smith } 44074679c65SBarry Smith 4414a2ae208SSatish Balay #undef __FUNCT__ 4424a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 4439b94acceSBarry Smith /*@ 4449b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4459b94acceSBarry Smith with SNESSSetFunction(). 4469b94acceSBarry Smith 447c7afd0dbSLois Curfman McInnes Collective on SNES 448c7afd0dbSLois Curfman McInnes 4499b94acceSBarry Smith Input Parameter: 4509b94acceSBarry Smith . snes - SNES context 4519b94acceSBarry Smith 4529b94acceSBarry Smith Output Parameter: 4539b94acceSBarry Smith . fnorm - 2-norm of function 4549b94acceSBarry Smith 45536851e7fSLois Curfman McInnes Level: intermediate 45636851e7fSLois Curfman McInnes 4579b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 458a86d99e1SLois Curfman McInnes 459b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 4609b94acceSBarry Smith @*/ 46171f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 4629b94acceSBarry Smith { 4633a40ed3dSBarry Smith PetscFunctionBegin; 4644482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4654482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 4669b94acceSBarry Smith *fnorm = snes->norm; 4673a40ed3dSBarry Smith PetscFunctionReturn(0); 4689b94acceSBarry Smith } 46974679c65SBarry Smith 4704a2ae208SSatish Balay #undef __FUNCT__ 471b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 4729b94acceSBarry Smith /*@ 473b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 4749b94acceSBarry Smith attempted by the nonlinear solver. 4759b94acceSBarry Smith 476c7afd0dbSLois Curfman McInnes Not Collective 477c7afd0dbSLois Curfman McInnes 4789b94acceSBarry Smith Input Parameter: 4799b94acceSBarry Smith . snes - SNES context 4809b94acceSBarry Smith 4819b94acceSBarry Smith Output Parameter: 4829b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 4839b94acceSBarry Smith 484c96a6f78SLois Curfman McInnes Notes: 485c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 486c96a6f78SLois Curfman McInnes 48736851e7fSLois Curfman McInnes Level: intermediate 48836851e7fSLois Curfman McInnes 4899b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 49058ebbce7SBarry Smith 491e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 49258ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 4939b94acceSBarry Smith @*/ 494b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 4959b94acceSBarry Smith { 4963a40ed3dSBarry Smith PetscFunctionBegin; 4974482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4984482741eSBarry Smith PetscValidIntPointer(nfails,2); 49950ffb88aSMatthew Knepley *nfails = snes->numFailures; 50050ffb88aSMatthew Knepley PetscFunctionReturn(0); 50150ffb88aSMatthew Knepley } 50250ffb88aSMatthew Knepley 50350ffb88aSMatthew Knepley #undef __FUNCT__ 504b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 50550ffb88aSMatthew Knepley /*@ 506b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 50750ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 50850ffb88aSMatthew Knepley 50950ffb88aSMatthew Knepley Not Collective 51050ffb88aSMatthew Knepley 51150ffb88aSMatthew Knepley Input Parameters: 51250ffb88aSMatthew Knepley + snes - SNES context 51350ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 51450ffb88aSMatthew Knepley 51550ffb88aSMatthew Knepley Level: intermediate 51650ffb88aSMatthew Knepley 51750ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 51858ebbce7SBarry Smith 519e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 52058ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 52150ffb88aSMatthew Knepley @*/ 522b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 52350ffb88aSMatthew Knepley { 52450ffb88aSMatthew Knepley PetscFunctionBegin; 5254482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 52650ffb88aSMatthew Knepley snes->maxFailures = maxFails; 52750ffb88aSMatthew Knepley PetscFunctionReturn(0); 52850ffb88aSMatthew Knepley } 52950ffb88aSMatthew Knepley 53050ffb88aSMatthew Knepley #undef __FUNCT__ 531b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 53250ffb88aSMatthew Knepley /*@ 533b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 53450ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 53550ffb88aSMatthew Knepley 53650ffb88aSMatthew Knepley Not Collective 53750ffb88aSMatthew Knepley 53850ffb88aSMatthew Knepley Input Parameter: 53950ffb88aSMatthew Knepley . snes - SNES context 54050ffb88aSMatthew Knepley 54150ffb88aSMatthew Knepley Output Parameter: 54250ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 54350ffb88aSMatthew Knepley 54450ffb88aSMatthew Knepley Level: intermediate 54550ffb88aSMatthew Knepley 54650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 54758ebbce7SBarry Smith 548e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 54958ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 55058ebbce7SBarry Smith 55150ffb88aSMatthew Knepley @*/ 552b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 55350ffb88aSMatthew Knepley { 55450ffb88aSMatthew Knepley PetscFunctionBegin; 5554482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5564482741eSBarry Smith PetscValidIntPointer(maxFails,2); 55750ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 5583a40ed3dSBarry Smith PetscFunctionReturn(0); 5599b94acceSBarry Smith } 560a847f771SSatish Balay 5614a2ae208SSatish Balay #undef __FUNCT__ 5622541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 5632541af92SBarry Smith /*@ 5642541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 5652541af92SBarry Smith done by SNES. 5662541af92SBarry Smith 5672541af92SBarry Smith Not Collective 5682541af92SBarry Smith 5692541af92SBarry Smith Input Parameter: 5702541af92SBarry Smith . snes - SNES context 5712541af92SBarry Smith 5722541af92SBarry Smith Output Parameter: 5732541af92SBarry Smith . nfuncs - number of evaluations 5742541af92SBarry Smith 5752541af92SBarry Smith Level: intermediate 5762541af92SBarry Smith 5772541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 57858ebbce7SBarry Smith 579e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 5802541af92SBarry Smith @*/ 5812541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 5822541af92SBarry Smith { 5832541af92SBarry Smith PetscFunctionBegin; 5842541af92SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5852541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 5862541af92SBarry Smith *nfuncs = snes->nfuncs; 5872541af92SBarry Smith PetscFunctionReturn(0); 5882541af92SBarry Smith } 5892541af92SBarry Smith 5902541af92SBarry Smith #undef __FUNCT__ 5913d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 5923d4c4710SBarry Smith /*@ 5933d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 5943d4c4710SBarry Smith linear solvers. 5953d4c4710SBarry Smith 5963d4c4710SBarry Smith Not Collective 5973d4c4710SBarry Smith 5983d4c4710SBarry Smith Input Parameter: 5993d4c4710SBarry Smith . snes - SNES context 6003d4c4710SBarry Smith 6013d4c4710SBarry Smith Output Parameter: 6023d4c4710SBarry Smith . nfails - number of failed solves 6033d4c4710SBarry Smith 6043d4c4710SBarry Smith Notes: 6053d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 6063d4c4710SBarry Smith 6073d4c4710SBarry Smith Level: intermediate 6083d4c4710SBarry Smith 6093d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 61058ebbce7SBarry Smith 611e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 6123d4c4710SBarry Smith @*/ 6133d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 6143d4c4710SBarry Smith { 6153d4c4710SBarry Smith PetscFunctionBegin; 6163d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6173d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 6183d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 6193d4c4710SBarry Smith PetscFunctionReturn(0); 6203d4c4710SBarry Smith } 6213d4c4710SBarry Smith 6223d4c4710SBarry Smith #undef __FUNCT__ 6233d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 6243d4c4710SBarry Smith /*@ 6253d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 6263d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 6273d4c4710SBarry Smith 6283d4c4710SBarry Smith Collective on SNES 6293d4c4710SBarry Smith 6303d4c4710SBarry Smith Input Parameters: 6313d4c4710SBarry Smith + snes - SNES context 6323d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 6333d4c4710SBarry Smith 6343d4c4710SBarry Smith Level: intermediate 6353d4c4710SBarry Smith 636a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 6373d4c4710SBarry Smith 6383d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 6393d4c4710SBarry Smith 64058ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 6413d4c4710SBarry Smith @*/ 6423d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 6433d4c4710SBarry Smith { 6443d4c4710SBarry Smith PetscFunctionBegin; 6453d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6463d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 6473d4c4710SBarry Smith PetscFunctionReturn(0); 6483d4c4710SBarry Smith } 6493d4c4710SBarry Smith 6503d4c4710SBarry Smith #undef __FUNCT__ 6513d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 6523d4c4710SBarry Smith /*@ 6533d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 6543d4c4710SBarry Smith are allowed before SNES terminates 6553d4c4710SBarry Smith 6563d4c4710SBarry Smith Not Collective 6573d4c4710SBarry Smith 6583d4c4710SBarry Smith Input Parameter: 6593d4c4710SBarry Smith . snes - SNES context 6603d4c4710SBarry Smith 6613d4c4710SBarry Smith Output Parameter: 6623d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 6633d4c4710SBarry Smith 6643d4c4710SBarry Smith Level: intermediate 6653d4c4710SBarry Smith 6663d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 6673d4c4710SBarry Smith 6683d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 6693d4c4710SBarry Smith 670e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 6713d4c4710SBarry Smith @*/ 6723d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 6733d4c4710SBarry Smith { 6743d4c4710SBarry Smith PetscFunctionBegin; 6753d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6763d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 6773d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 6783d4c4710SBarry Smith PetscFunctionReturn(0); 6793d4c4710SBarry Smith } 6803d4c4710SBarry Smith 6813d4c4710SBarry Smith #undef __FUNCT__ 682b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 683c96a6f78SLois Curfman McInnes /*@ 684b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 685c96a6f78SLois Curfman McInnes used by the nonlinear solver. 686c96a6f78SLois Curfman McInnes 687c7afd0dbSLois Curfman McInnes Not Collective 688c7afd0dbSLois Curfman McInnes 689c96a6f78SLois Curfman McInnes Input Parameter: 690c96a6f78SLois Curfman McInnes . snes - SNES context 691c96a6f78SLois Curfman McInnes 692c96a6f78SLois Curfman McInnes Output Parameter: 693c96a6f78SLois Curfman McInnes . lits - number of linear iterations 694c96a6f78SLois Curfman McInnes 695c96a6f78SLois Curfman McInnes Notes: 696c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 697c96a6f78SLois Curfman McInnes 69836851e7fSLois Curfman McInnes Level: intermediate 69936851e7fSLois Curfman McInnes 700c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 7012b668275SBarry Smith 70258ebbce7SBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm()S, NESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 703c96a6f78SLois Curfman McInnes @*/ 704b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 705c96a6f78SLois Curfman McInnes { 7063a40ed3dSBarry Smith PetscFunctionBegin; 7074482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7084482741eSBarry Smith PetscValidIntPointer(lits,2); 709c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 7103a40ed3dSBarry Smith PetscFunctionReturn(0); 711c96a6f78SLois Curfman McInnes } 712c96a6f78SLois Curfman McInnes 7134a2ae208SSatish Balay #undef __FUNCT__ 71494b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 71552baeb72SSatish Balay /*@ 71694b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 7179b94acceSBarry Smith 71894b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 719c7afd0dbSLois Curfman McInnes 7209b94acceSBarry Smith Input Parameter: 7219b94acceSBarry Smith . snes - the SNES context 7229b94acceSBarry Smith 7239b94acceSBarry Smith Output Parameter: 72494b7f48cSBarry Smith . ksp - the KSP context 7259b94acceSBarry Smith 7269b94acceSBarry Smith Notes: 72794b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 7289b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 7292999313aSBarry Smith PC contexts as well. 7309b94acceSBarry Smith 73136851e7fSLois Curfman McInnes Level: beginner 73236851e7fSLois Curfman McInnes 73394b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7349b94acceSBarry Smith 7352999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7369b94acceSBarry Smith @*/ 73763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp) 7389b94acceSBarry Smith { 7391cee3971SBarry Smith PetscErrorCode ierr; 7401cee3971SBarry Smith 7413a40ed3dSBarry Smith PetscFunctionBegin; 7424482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7434482741eSBarry Smith PetscValidPointer(ksp,2); 7441cee3971SBarry Smith 7451cee3971SBarry Smith if (!snes->ksp) { 7461cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 7471cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 7481cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 7491cee3971SBarry Smith } 75094b7f48cSBarry Smith *ksp = snes->ksp; 7513a40ed3dSBarry Smith PetscFunctionReturn(0); 7529b94acceSBarry Smith } 75382bf6240SBarry Smith 7544a2ae208SSatish Balay #undef __FUNCT__ 7552999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 7562999313aSBarry Smith /*@ 7572999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 7582999313aSBarry Smith 7592999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 7602999313aSBarry Smith 7612999313aSBarry Smith Input Parameters: 7622999313aSBarry Smith + snes - the SNES context 7632999313aSBarry Smith - ksp - the KSP context 7642999313aSBarry Smith 7652999313aSBarry Smith Notes: 7662999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 7672999313aSBarry Smith so this routine is rarely needed. 7682999313aSBarry Smith 7692999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 7702999313aSBarry Smith decreased by one. 7712999313aSBarry Smith 7722999313aSBarry Smith Level: developer 7732999313aSBarry Smith 7742999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7752999313aSBarry Smith 7762999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7772999313aSBarry Smith @*/ 7782999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp) 7792999313aSBarry Smith { 7802999313aSBarry Smith PetscErrorCode ierr; 7812999313aSBarry Smith 7822999313aSBarry Smith PetscFunctionBegin; 7832999313aSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7842999313aSBarry Smith PetscValidHeaderSpecific(ksp,KSP_COOKIE,2); 7852999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 7867dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 787906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 7882999313aSBarry Smith snes->ksp = ksp; 7892999313aSBarry Smith PetscFunctionReturn(0); 7902999313aSBarry Smith } 7912999313aSBarry Smith 7927adad957SLisandro Dalcin #if 0 7932999313aSBarry Smith #undef __FUNCT__ 7944a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 7956849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 796e24b481bSBarry Smith { 797e24b481bSBarry Smith PetscFunctionBegin; 798e24b481bSBarry Smith PetscFunctionReturn(0); 799e24b481bSBarry Smith } 8007adad957SLisandro Dalcin #endif 801e24b481bSBarry Smith 8029b94acceSBarry Smith /* -----------------------------------------------------------*/ 8034a2ae208SSatish Balay #undef __FUNCT__ 8044a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 80552baeb72SSatish Balay /*@ 8069b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 8079b94acceSBarry Smith 808c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 809c7afd0dbSLois Curfman McInnes 810c7afd0dbSLois Curfman McInnes Input Parameters: 811906ed7ccSBarry Smith . comm - MPI communicator 8129b94acceSBarry Smith 8139b94acceSBarry Smith Output Parameter: 8149b94acceSBarry Smith . outsnes - the new SNES context 8159b94acceSBarry Smith 816c7afd0dbSLois Curfman McInnes Options Database Keys: 817c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 818c7afd0dbSLois Curfman McInnes and no preconditioning matrix 819c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 820c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 821c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 822c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 823c1f60f51SBarry Smith 82436851e7fSLois Curfman McInnes Level: beginner 82536851e7fSLois Curfman McInnes 8269b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 8279b94acceSBarry Smith 828a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 829a8054027SBarry Smith 8309b94acceSBarry Smith @*/ 83163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes) 8329b94acceSBarry Smith { 833dfbe8321SBarry Smith PetscErrorCode ierr; 8349b94acceSBarry Smith SNES snes; 835fa9f3622SBarry Smith SNESKSPEW *kctx; 83637fcc0dbSBarry Smith 8373a40ed3dSBarry Smith PetscFunctionBegin; 838ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 8398ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 8408ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 8418ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 8428ba1e511SMatthew Knepley #endif 8438ba1e511SMatthew Knepley 844e7788613SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 8457adad957SLisandro Dalcin 84685385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 8479b94acceSBarry Smith snes->max_its = 50; 8489750a799SBarry Smith snes->max_funcs = 10000; 8499b94acceSBarry Smith snes->norm = 0.0; 850b4874afaSBarry Smith snes->rtol = 1.e-8; 851b4874afaSBarry Smith snes->ttol = 0.0; 85270441072SBarry Smith snes->abstol = 1.e-50; 8539b94acceSBarry Smith snes->xtol = 1.e-8; 8544b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 8559b94acceSBarry Smith snes->nfuncs = 0; 85650ffb88aSMatthew Knepley snes->numFailures = 0; 85750ffb88aSMatthew Knepley snes->maxFailures = 1; 8587a00f4a9SLois Curfman McInnes snes->linear_its = 0; 859e35cf81dSBarry Smith snes->lagjacobian = 1; 860a8054027SBarry Smith snes->lagpreconditioner = 1; 861639f9d9dSBarry Smith snes->numbermonitors = 0; 8629b94acceSBarry Smith snes->data = 0; 8634dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 864186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 8656f24a144SLois Curfman McInnes snes->vwork = 0; 8666f24a144SLois Curfman McInnes snes->nwork = 0; 867758f92a0SBarry Smith snes->conv_hist_len = 0; 868758f92a0SBarry Smith snes->conv_hist_max = 0; 869758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 870758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 871758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 872184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 8739b94acceSBarry Smith 8743d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 8753d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 8763d4c4710SBarry Smith 8779b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 87838f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 8799b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 8809b94acceSBarry Smith kctx->version = 2; 8819b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 8829b94acceSBarry Smith this was too large for some test cases */ 8839b94acceSBarry Smith kctx->rtol_last = 0; 8849b94acceSBarry Smith kctx->rtol_max = .9; 8859b94acceSBarry Smith kctx->gamma = 1.0; 88671f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 88771f87433Sdalcinl kctx->alpha2 = kctx->alpha; 8889b94acceSBarry Smith kctx->threshold = .1; 8899b94acceSBarry Smith kctx->lresid_last = 0; 8909b94acceSBarry Smith kctx->norm_last = 0; 8919b94acceSBarry Smith 8929b94acceSBarry Smith *outsnes = snes; 89300036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 8943a40ed3dSBarry Smith PetscFunctionReturn(0); 8959b94acceSBarry Smith } 8969b94acceSBarry Smith 8974a2ae208SSatish Balay #undef __FUNCT__ 8984a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 8999b94acceSBarry Smith /*@C 9009b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 9019b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 9029b94acceSBarry Smith equations. 9039b94acceSBarry Smith 904fee21e36SBarry Smith Collective on SNES 905fee21e36SBarry Smith 906c7afd0dbSLois Curfman McInnes Input Parameters: 907c7afd0dbSLois Curfman McInnes + snes - the SNES context 908c7afd0dbSLois Curfman McInnes . r - vector to store function value 909de044059SHong Zhang . func - function evaluation routine 910c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 911c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 9129b94acceSBarry Smith 913c7afd0dbSLois Curfman McInnes Calling sequence of func: 9148d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 915c7afd0dbSLois Curfman McInnes 916313e4042SLois Curfman McInnes . f - function vector 917c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 9189b94acceSBarry Smith 9199b94acceSBarry Smith Notes: 9209b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 9219b94acceSBarry Smith $ f'(x) x = -f(x), 922c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 9239b94acceSBarry Smith 92436851e7fSLois Curfman McInnes Level: beginner 92536851e7fSLois Curfman McInnes 9269b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9279b94acceSBarry Smith 928a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 9299b94acceSBarry Smith @*/ 93063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 9319b94acceSBarry Smith { 93285385478SLisandro Dalcin PetscErrorCode ierr; 9333a40ed3dSBarry Smith PetscFunctionBegin; 9344482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9354482741eSBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE,2); 936c9780b6fSBarry Smith PetscCheckSameComm(snes,1,r,2); 93785385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 93885385478SLisandro Dalcin if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); } 939e7788613SBarry Smith snes->ops->computefunction = func; 94085385478SLisandro Dalcin snes->vec_func = r; 9419b94acceSBarry Smith snes->funP = ctx; 9423a40ed3dSBarry Smith PetscFunctionReturn(0); 9439b94acceSBarry Smith } 9449b94acceSBarry Smith 9453ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 9463ab0aad5SBarry Smith #undef __FUNCT__ 9471096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 9481096aae1SMatthew Knepley /*@C 9491096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 9501096aae1SMatthew Knepley it assumes a zero right hand side. 9511096aae1SMatthew Knepley 9521096aae1SMatthew Knepley Collective on SNES 9531096aae1SMatthew Knepley 9541096aae1SMatthew Knepley Input Parameter: 9551096aae1SMatthew Knepley . snes - the SNES context 9561096aae1SMatthew Knepley 9571096aae1SMatthew Knepley Output Parameter: 958bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 9591096aae1SMatthew Knepley 9601096aae1SMatthew Knepley Level: intermediate 9611096aae1SMatthew Knepley 9621096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 9631096aae1SMatthew Knepley 96485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 9651096aae1SMatthew Knepley @*/ 9661096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs) 9671096aae1SMatthew Knepley { 9681096aae1SMatthew Knepley PetscFunctionBegin; 9691096aae1SMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9701096aae1SMatthew Knepley PetscValidPointer(rhs,2); 97185385478SLisandro Dalcin *rhs = snes->vec_rhs; 9721096aae1SMatthew Knepley PetscFunctionReturn(0); 9731096aae1SMatthew Knepley } 9741096aae1SMatthew Knepley 9751096aae1SMatthew Knepley #undef __FUNCT__ 9764a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 9779b94acceSBarry Smith /*@ 97836851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 9799b94acceSBarry Smith SNESSetFunction(). 9809b94acceSBarry Smith 981c7afd0dbSLois Curfman McInnes Collective on SNES 982c7afd0dbSLois Curfman McInnes 9839b94acceSBarry Smith Input Parameters: 984c7afd0dbSLois Curfman McInnes + snes - the SNES context 985c7afd0dbSLois Curfman McInnes - x - input vector 9869b94acceSBarry Smith 9879b94acceSBarry Smith Output Parameter: 9883638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 9899b94acceSBarry Smith 9901bffabb2SLois Curfman McInnes Notes: 99136851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 99236851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 99336851e7fSLois Curfman McInnes themselves. 99436851e7fSLois Curfman McInnes 99536851e7fSLois Curfman McInnes Level: developer 99636851e7fSLois Curfman McInnes 9979b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 9989b94acceSBarry Smith 999a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 10009b94acceSBarry Smith @*/ 100163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y) 10029b94acceSBarry Smith { 1003dfbe8321SBarry Smith PetscErrorCode ierr; 10049b94acceSBarry Smith 10053a40ed3dSBarry Smith PetscFunctionBegin; 10064482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10074482741eSBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE,2); 10084482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1009c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1010c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 1011184914b5SBarry Smith 1012d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1013e7788613SBarry Smith if (snes->ops->computefunction) { 1014d64ed03dSBarry Smith PetscStackPush("SNES user function"); 1015e9a2bbcdSBarry Smith CHKMEMQ; 1016e7788613SBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP); 1017e9a2bbcdSBarry Smith CHKMEMQ; 1018d64ed03dSBarry Smith PetscStackPop; 1019d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 102019717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr); 102119717074SBarry Smith } 1022d5e45103SBarry Smith CHKERRQ(ierr); 102385385478SLisandro Dalcin } else if (snes->vec_rhs) { 10241096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 10251096aae1SMatthew Knepley } else { 10261096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 10271096aae1SMatthew Knepley } 102885385478SLisandro Dalcin if (snes->vec_rhs) { 102985385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 10303ab0aad5SBarry Smith } 1031ae3c334cSLois Curfman McInnes snes->nfuncs++; 1032d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 10333a40ed3dSBarry Smith PetscFunctionReturn(0); 10349b94acceSBarry Smith } 10359b94acceSBarry Smith 10364a2ae208SSatish Balay #undef __FUNCT__ 10374a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 103862fef451SLois Curfman McInnes /*@ 103962fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 104062fef451SLois Curfman McInnes set with SNESSetJacobian(). 104162fef451SLois Curfman McInnes 1042c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1043c7afd0dbSLois Curfman McInnes 104462fef451SLois Curfman McInnes Input Parameters: 1045c7afd0dbSLois Curfman McInnes + snes - the SNES context 1046c7afd0dbSLois Curfman McInnes - x - input vector 104762fef451SLois Curfman McInnes 104862fef451SLois Curfman McInnes Output Parameters: 1049c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 105062fef451SLois Curfman McInnes . B - optional preconditioning matrix 10512b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1052fee21e36SBarry Smith 1053e35cf81dSBarry Smith Options Database Keys: 1054e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1055e35cf81dSBarry Smith - -snes_lag_jacobian <lag> 1056e35cf81dSBarry Smith 105762fef451SLois Curfman McInnes Notes: 105862fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 105962fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 106062fef451SLois Curfman McInnes 106194b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1062dc5a77f8SLois Curfman McInnes flag parameter. 106362fef451SLois Curfman McInnes 106436851e7fSLois Curfman McInnes Level: developer 106536851e7fSLois Curfman McInnes 106662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 106762fef451SLois Curfman McInnes 1068e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 106962fef451SLois Curfman McInnes @*/ 107063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10719b94acceSBarry Smith { 1072dfbe8321SBarry Smith PetscErrorCode ierr; 1073ebd3b9afSBarry Smith PetscTruth flag; 10743a40ed3dSBarry Smith 10753a40ed3dSBarry Smith PetscFunctionBegin; 10764482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10774482741eSBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE,2); 10784482741eSBarry Smith PetscValidPointer(flg,5); 1079c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1080e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1081ebd3b9afSBarry Smith 1082ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1083ebd3b9afSBarry Smith 1084fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1085fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1086fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1087fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1088e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1089e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1090ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1091ebd3b9afSBarry Smith if (flag) { 1092ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1093ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1094ebd3b9afSBarry Smith } 1095e35cf81dSBarry Smith PetscFunctionReturn(0); 1096e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1097e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1098e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1099ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1100ebd3b9afSBarry Smith if (flag) { 1101ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1102ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1103ebd3b9afSBarry Smith } 1104e35cf81dSBarry Smith PetscFunctionReturn(0); 1105e35cf81dSBarry Smith } 1106e35cf81dSBarry Smith 1107c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1108e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1109d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1110dc67937bSBarry Smith CHKMEMQ; 1111e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1112dc67937bSBarry Smith CHKMEMQ; 1113d64ed03dSBarry Smith PetscStackPop; 1114d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1115a8054027SBarry Smith 11163b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 11173b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 11183b4f5425SBarry Smith snes->lagpreconditioner = -1; 11193b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1120a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1121a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1122a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1123a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1124a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1125a8054027SBarry Smith } 1126a8054027SBarry Smith 11276d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 11286ce558aeSBarry Smith /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3); 11296ce558aeSBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE,4); */ 11303a40ed3dSBarry Smith PetscFunctionReturn(0); 11319b94acceSBarry Smith } 11329b94acceSBarry Smith 11334a2ae208SSatish Balay #undef __FUNCT__ 11344a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 11359b94acceSBarry Smith /*@C 11369b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1137044dda88SLois Curfman McInnes location to store the matrix. 11389b94acceSBarry Smith 1139c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1140c7afd0dbSLois Curfman McInnes 11419b94acceSBarry Smith Input Parameters: 1142c7afd0dbSLois Curfman McInnes + snes - the SNES context 11439b94acceSBarry Smith . A - Jacobian matrix 11449b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11459b94acceSBarry Smith . func - Jacobian evaluation routine 1146c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11472cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11489b94acceSBarry Smith 11499b94acceSBarry Smith Calling sequence of func: 11508d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11519b94acceSBarry Smith 1152c7afd0dbSLois Curfman McInnes + x - input vector 11539b94acceSBarry Smith . A - Jacobian matrix 11549b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1155ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 11562b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1157c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11589b94acceSBarry Smith 11599b94acceSBarry Smith Notes: 116094b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 11612cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1162ac21db08SLois Curfman McInnes 1163ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11649b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11659b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11669b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11679b94acceSBarry Smith throughout the global iterations. 11689b94acceSBarry Smith 116916913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 117016913363SBarry Smith each matrix. 117116913363SBarry Smith 117236851e7fSLois Curfman McInnes Level: beginner 117336851e7fSLois Curfman McInnes 11749b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11759b94acceSBarry Smith 11763ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 11779b94acceSBarry Smith @*/ 117863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 11799b94acceSBarry Smith { 1180dfbe8321SBarry Smith PetscErrorCode ierr; 11813a7fca6bSBarry Smith 11823a40ed3dSBarry Smith PetscFunctionBegin; 11834482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11844482741eSBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2); 11854482741eSBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3); 1186c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 1187c9780b6fSBarry Smith if (B) PetscCheckSameComm(snes,1,B,2); 1188e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 11893a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 11903a7fca6bSBarry Smith if (A) { 11917dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 11923a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 11939b94acceSBarry Smith snes->jacobian = A; 11943a7fca6bSBarry Smith } 11953a7fca6bSBarry Smith if (B) { 11967dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 11973a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 11989b94acceSBarry Smith snes->jacobian_pre = B; 11993a7fca6bSBarry Smith } 12003a40ed3dSBarry Smith PetscFunctionReturn(0); 12019b94acceSBarry Smith } 120262fef451SLois Curfman McInnes 12034a2ae208SSatish Balay #undef __FUNCT__ 12044a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1205c2aafc4cSSatish Balay /*@C 1206b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1207b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1208b4fd4287SBarry Smith 1209c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1210c7afd0dbSLois Curfman McInnes 1211b4fd4287SBarry Smith Input Parameter: 1212b4fd4287SBarry Smith . snes - the nonlinear solver context 1213b4fd4287SBarry Smith 1214b4fd4287SBarry Smith Output Parameters: 1215c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1216b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 121770e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 121870e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1219fee21e36SBarry Smith 122036851e7fSLois Curfman McInnes Level: advanced 122136851e7fSLois Curfman McInnes 1222b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1223b4fd4287SBarry Smith @*/ 122470e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1225b4fd4287SBarry Smith { 12263a40ed3dSBarry Smith PetscFunctionBegin; 12274482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1228b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1229b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1230e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 123170e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 12323a40ed3dSBarry Smith PetscFunctionReturn(0); 1233b4fd4287SBarry Smith } 1234b4fd4287SBarry Smith 12359b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 123663dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 12379b94acceSBarry Smith 12384a2ae208SSatish Balay #undef __FUNCT__ 12394a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 12409b94acceSBarry Smith /*@ 12419b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1242272ac6f2SLois Curfman McInnes of a nonlinear solver. 12439b94acceSBarry Smith 1244fee21e36SBarry Smith Collective on SNES 1245fee21e36SBarry Smith 1246c7afd0dbSLois Curfman McInnes Input Parameters: 124770e92668SMatthew Knepley . snes - the SNES context 1248c7afd0dbSLois Curfman McInnes 1249272ac6f2SLois Curfman McInnes Notes: 1250272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1251272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1252272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1253272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1254272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1255272ac6f2SLois Curfman McInnes 125636851e7fSLois Curfman McInnes Level: advanced 125736851e7fSLois Curfman McInnes 12589b94acceSBarry Smith .keywords: SNES, nonlinear, setup 12599b94acceSBarry Smith 12609b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 12619b94acceSBarry Smith @*/ 126270e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes) 12639b94acceSBarry Smith { 1264dfbe8321SBarry Smith PetscErrorCode ierr; 126571f87433Sdalcinl PetscTruth flg; 12663a40ed3dSBarry Smith 12673a40ed3dSBarry Smith PetscFunctionBegin; 12684482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 12694dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 12709b94acceSBarry Smith 12717adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 127285385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 127385385478SLisandro Dalcin } 127485385478SLisandro Dalcin 12757adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1276c1f60f51SBarry Smith /* 1277c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1278dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1279c1f60f51SBarry Smith */ 1280c1f60f51SBarry Smith if (flg) { 1281c1f60f51SBarry Smith Mat J; 1282fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12833ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 1284ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12853a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 12863a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1287c1f60f51SBarry Smith } 128845fc7adcSBarry Smith 128903c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT) 12907adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr); 129145fc7adcSBarry Smith if (flg) { 129245fc7adcSBarry Smith Mat J; 129345fc7adcSBarry Smith ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr); 129475396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr); 129545fc7adcSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 129645fc7adcSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 129745fc7adcSBarry Smith } 129832a4b47aSMatthew Knepley #endif 129945fc7adcSBarry Smith 13007adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1301c1f60f51SBarry Smith /* 1302dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1303c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1304c1f60f51SBarry Smith */ 130531615d04SLois Curfman McInnes if (flg) { 1306272ac6f2SLois Curfman McInnes Mat J; 1307b5d62d44SBarry Smith KSP ksp; 130894b7f48cSBarry Smith PC pc; 130975396ef9SLisandro Dalcin /* create and set matrix-free operator */ 1310fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 13113ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 131275396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 13133ec795f1SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 13143a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1315f3ef73ceSBarry Smith /* force no preconditioner */ 131694b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 1317b5d62d44SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 1318a9815358SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 1319a9815358SBarry Smith if (!flg) { 132075396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 1321f3ef73ceSBarry Smith ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 1322272ac6f2SLois Curfman McInnes } 1323a9815358SBarry Smith } 1324f3ef73ceSBarry Smith 132585385478SLisandro Dalcin if (!snes->vec_func && !snes->vec_rhs) { 13261096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 13271096aae1SMatthew Knepley } 132885385478SLisandro Dalcin if (!snes->ops->computefunction && !snes->vec_rhs) { 13291096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 13301096aae1SMatthew Knepley } 133175396ef9SLisandro Dalcin if (!snes->jacobian) { 133275396ef9SLisandro Dalcin SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 133375396ef9SLisandro Dalcin } 1334a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 133529bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1336a8c6a408SBarry Smith } 1337a703fe33SLois Curfman McInnes 1338410397dcSLisandro Dalcin if (snes->ops->setup) { 1339410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1340410397dcSLisandro Dalcin } 13417aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 13423a40ed3dSBarry Smith PetscFunctionReturn(0); 13439b94acceSBarry Smith } 13449b94acceSBarry Smith 13454a2ae208SSatish Balay #undef __FUNCT__ 13464a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 134752baeb72SSatish Balay /*@ 13489b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 13499b94acceSBarry Smith with SNESCreate(). 13509b94acceSBarry Smith 1351c7afd0dbSLois Curfman McInnes Collective on SNES 1352c7afd0dbSLois Curfman McInnes 13539b94acceSBarry Smith Input Parameter: 13549b94acceSBarry Smith . snes - the SNES context 13559b94acceSBarry Smith 135636851e7fSLois Curfman McInnes Level: beginner 135736851e7fSLois Curfman McInnes 13589b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 13599b94acceSBarry Smith 136063a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 13619b94acceSBarry Smith @*/ 136263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes) 13639b94acceSBarry Smith { 13646849ba73SBarry Smith PetscErrorCode ierr; 13653a40ed3dSBarry Smith 13663a40ed3dSBarry Smith PetscFunctionBegin; 13674482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 13687adad957SLisandro Dalcin if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0); 1369d4bb536fSBarry Smith 1370be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 13710f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1372e7788613SBarry Smith if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);} 137385385478SLisandro Dalcin 137485385478SLisandro Dalcin if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);} 137585385478SLisandro Dalcin if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);} 137685385478SLisandro Dalcin if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);} 13773a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 13783a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 13791cee3971SBarry Smith if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);} 138085385478SLisandro Dalcin ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr); 1381522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1382a6570f20SBarry Smith ierr = SNESMonitorCancel(snes);CHKERRQ(ierr); 13837f7931b9SBarry Smith if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);} 1384a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 13853a40ed3dSBarry Smith PetscFunctionReturn(0); 13869b94acceSBarry Smith } 13879b94acceSBarry Smith 13889b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 13899b94acceSBarry Smith 13904a2ae208SSatish Balay #undef __FUNCT__ 1391a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1392a8054027SBarry Smith /*@ 1393a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1394a8054027SBarry Smith 1395a8054027SBarry Smith Collective on SNES 1396a8054027SBarry Smith 1397a8054027SBarry Smith Input Parameters: 1398a8054027SBarry Smith + snes - the SNES context 1399a8054027SBarry 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 14003b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1401a8054027SBarry Smith 1402a8054027SBarry Smith Options Database Keys: 1403a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1404a8054027SBarry Smith 1405a8054027SBarry Smith Notes: 1406a8054027SBarry Smith The default is 1 1407a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1408a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1409a8054027SBarry Smith 1410a8054027SBarry Smith Level: intermediate 1411a8054027SBarry Smith 1412a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1413a8054027SBarry Smith 1414e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1415a8054027SBarry Smith 1416a8054027SBarry Smith @*/ 1417a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1418a8054027SBarry Smith { 1419a8054027SBarry Smith PetscFunctionBegin; 1420a8054027SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 14213b4f5425SBarry Smith if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1422a8054027SBarry Smith if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1423a8054027SBarry Smith snes->lagpreconditioner = lag; 1424a8054027SBarry Smith PetscFunctionReturn(0); 1425a8054027SBarry Smith } 1426a8054027SBarry Smith 1427a8054027SBarry Smith #undef __FUNCT__ 1428a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1429a8054027SBarry Smith /*@ 1430a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1431a8054027SBarry Smith 1432a8054027SBarry Smith Collective on SNES 1433a8054027SBarry Smith 1434a8054027SBarry Smith Input Parameter: 1435a8054027SBarry Smith . snes - the SNES context 1436a8054027SBarry Smith 1437a8054027SBarry Smith Output Parameter: 1438a8054027SBarry 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 14393b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1440a8054027SBarry Smith 1441a8054027SBarry Smith Options Database Keys: 1442a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1443a8054027SBarry Smith 1444a8054027SBarry Smith Notes: 1445a8054027SBarry Smith The default is 1 1446a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1447a8054027SBarry Smith 1448a8054027SBarry Smith Level: intermediate 1449a8054027SBarry Smith 1450a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1451a8054027SBarry Smith 1452a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1453a8054027SBarry Smith 1454a8054027SBarry Smith @*/ 1455a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1456a8054027SBarry Smith { 1457a8054027SBarry Smith PetscFunctionBegin; 1458a8054027SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1459a8054027SBarry Smith *lag = snes->lagpreconditioner; 1460a8054027SBarry Smith PetscFunctionReturn(0); 1461a8054027SBarry Smith } 1462a8054027SBarry Smith 1463a8054027SBarry Smith #undef __FUNCT__ 1464e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 1465e35cf81dSBarry Smith /*@ 1466e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 1467e35cf81dSBarry Smith often the preconditioner is rebuilt. 1468e35cf81dSBarry Smith 1469e35cf81dSBarry Smith Collective on SNES 1470e35cf81dSBarry Smith 1471e35cf81dSBarry Smith Input Parameters: 1472e35cf81dSBarry Smith + snes - the SNES context 1473e35cf81dSBarry 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 1474fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 1475e35cf81dSBarry Smith 1476e35cf81dSBarry Smith Options Database Keys: 1477e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1478e35cf81dSBarry Smith 1479e35cf81dSBarry Smith Notes: 1480e35cf81dSBarry Smith The default is 1 1481e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1482fe3ffe1eSBarry Smith If -1 is used before the very first nonlinear solve the CODE WILL FAIL! because no Jacobian is used, use -2 to indicate you want it recomputed 1483fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 1484e35cf81dSBarry Smith 1485e35cf81dSBarry Smith Level: intermediate 1486e35cf81dSBarry Smith 1487e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1488e35cf81dSBarry Smith 1489e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 1490e35cf81dSBarry Smith 1491e35cf81dSBarry Smith @*/ 1492e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag) 1493e35cf81dSBarry Smith { 1494e35cf81dSBarry Smith PetscFunctionBegin; 1495e35cf81dSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1496fe3ffe1eSBarry Smith if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1497e35cf81dSBarry Smith if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1498e35cf81dSBarry Smith snes->lagjacobian = lag; 1499e35cf81dSBarry Smith PetscFunctionReturn(0); 1500e35cf81dSBarry Smith } 1501e35cf81dSBarry Smith 1502e35cf81dSBarry Smith #undef __FUNCT__ 1503e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 1504e35cf81dSBarry Smith /*@ 1505e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 1506e35cf81dSBarry Smith 1507e35cf81dSBarry Smith Collective on SNES 1508e35cf81dSBarry Smith 1509e35cf81dSBarry Smith Input Parameter: 1510e35cf81dSBarry Smith . snes - the SNES context 1511e35cf81dSBarry Smith 1512e35cf81dSBarry Smith Output Parameter: 1513e35cf81dSBarry 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 1514e35cf81dSBarry Smith the Jacobian is built etc. 1515e35cf81dSBarry Smith 1516e35cf81dSBarry Smith Options Database Keys: 1517e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1518e35cf81dSBarry Smith 1519e35cf81dSBarry Smith Notes: 1520e35cf81dSBarry Smith The default is 1 1521e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1522e35cf81dSBarry Smith 1523e35cf81dSBarry Smith Level: intermediate 1524e35cf81dSBarry Smith 1525e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1526e35cf81dSBarry Smith 1527e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 1528e35cf81dSBarry Smith 1529e35cf81dSBarry Smith @*/ 1530e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag) 1531e35cf81dSBarry Smith { 1532e35cf81dSBarry Smith PetscFunctionBegin; 1533e35cf81dSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1534e35cf81dSBarry Smith *lag = snes->lagjacobian; 1535e35cf81dSBarry Smith PetscFunctionReturn(0); 1536e35cf81dSBarry Smith } 1537e35cf81dSBarry Smith 1538e35cf81dSBarry Smith #undef __FUNCT__ 15394a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 15409b94acceSBarry Smith /*@ 1541d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 15429b94acceSBarry Smith 1543c7afd0dbSLois Curfman McInnes Collective on SNES 1544c7afd0dbSLois Curfman McInnes 15459b94acceSBarry Smith Input Parameters: 1546c7afd0dbSLois Curfman McInnes + snes - the SNES context 154770441072SBarry Smith . abstol - absolute convergence tolerance 154833174efeSLois Curfman McInnes . rtol - relative convergence tolerance 154933174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 155033174efeSLois Curfman McInnes of the change in the solution between steps 155133174efeSLois Curfman McInnes . maxit - maximum number of iterations 1552c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1553fee21e36SBarry Smith 155433174efeSLois Curfman McInnes Options Database Keys: 155570441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1556c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1557c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1558c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1559c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15609b94acceSBarry Smith 1561d7a720efSLois Curfman McInnes Notes: 15629b94acceSBarry Smith The default maximum number of iterations is 50. 15639b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15649b94acceSBarry Smith 156536851e7fSLois Curfman McInnes Level: intermediate 156636851e7fSLois Curfman McInnes 156733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15689b94acceSBarry Smith 15692492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 15709b94acceSBarry Smith @*/ 157163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 15729b94acceSBarry Smith { 15733a40ed3dSBarry Smith PetscFunctionBegin; 15744482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 157570441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1576d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1577d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1578d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1579d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15803a40ed3dSBarry Smith PetscFunctionReturn(0); 15819b94acceSBarry Smith } 15829b94acceSBarry Smith 15834a2ae208SSatish Balay #undef __FUNCT__ 15844a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 15859b94acceSBarry Smith /*@ 158633174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 158733174efeSLois Curfman McInnes 1588c7afd0dbSLois Curfman McInnes Not Collective 1589c7afd0dbSLois Curfman McInnes 159033174efeSLois Curfman McInnes Input Parameters: 1591c7afd0dbSLois Curfman McInnes + snes - the SNES context 159285385478SLisandro Dalcin . atol - absolute convergence tolerance 159333174efeSLois Curfman McInnes . rtol - relative convergence tolerance 159433174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 159533174efeSLois Curfman McInnes of the change in the solution between steps 159633174efeSLois Curfman McInnes . maxit - maximum number of iterations 1597c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1598fee21e36SBarry Smith 159933174efeSLois Curfman McInnes Notes: 160033174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 160133174efeSLois Curfman McInnes 160236851e7fSLois Curfman McInnes Level: intermediate 160336851e7fSLois Curfman McInnes 160433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 160533174efeSLois Curfman McInnes 160633174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 160733174efeSLois Curfman McInnes @*/ 160885385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 160933174efeSLois Curfman McInnes { 16103a40ed3dSBarry Smith PetscFunctionBegin; 16114482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 161285385478SLisandro Dalcin if (atol) *atol = snes->abstol; 161333174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 161433174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 161533174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 161633174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 16173a40ed3dSBarry Smith PetscFunctionReturn(0); 161833174efeSLois Curfman McInnes } 161933174efeSLois Curfman McInnes 16204a2ae208SSatish Balay #undef __FUNCT__ 16214a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 162233174efeSLois Curfman McInnes /*@ 16239b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 16249b94acceSBarry Smith 1625fee21e36SBarry Smith Collective on SNES 1626fee21e36SBarry Smith 1627c7afd0dbSLois Curfman McInnes Input Parameters: 1628c7afd0dbSLois Curfman McInnes + snes - the SNES context 1629c7afd0dbSLois Curfman McInnes - tol - tolerance 1630c7afd0dbSLois Curfman McInnes 16319b94acceSBarry Smith Options Database Key: 1632c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 16339b94acceSBarry Smith 163436851e7fSLois Curfman McInnes Level: intermediate 163536851e7fSLois Curfman McInnes 16369b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 16379b94acceSBarry Smith 16382492ecdbSBarry Smith .seealso: SNESSetTolerances() 16399b94acceSBarry Smith @*/ 164063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 16419b94acceSBarry Smith { 16423a40ed3dSBarry Smith PetscFunctionBegin; 16434482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16449b94acceSBarry Smith snes->deltatol = tol; 16453a40ed3dSBarry Smith PetscFunctionReturn(0); 16469b94acceSBarry Smith } 16479b94acceSBarry Smith 1648df9fa365SBarry Smith /* 1649df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1650df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1651df9fa365SBarry Smith macros instead of functions 1652df9fa365SBarry Smith */ 16534a2ae208SSatish Balay #undef __FUNCT__ 1654a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 1655a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1656ce1608b8SBarry Smith { 1657dfbe8321SBarry Smith PetscErrorCode ierr; 1658ce1608b8SBarry Smith 1659ce1608b8SBarry Smith PetscFunctionBegin; 16604482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1661a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1662ce1608b8SBarry Smith PetscFunctionReturn(0); 1663ce1608b8SBarry Smith } 1664ce1608b8SBarry Smith 16654a2ae208SSatish Balay #undef __FUNCT__ 1666a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 1667a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1668df9fa365SBarry Smith { 1669dfbe8321SBarry Smith PetscErrorCode ierr; 1670df9fa365SBarry Smith 1671df9fa365SBarry Smith PetscFunctionBegin; 1672a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1673df9fa365SBarry Smith PetscFunctionReturn(0); 1674df9fa365SBarry Smith } 1675df9fa365SBarry Smith 16764a2ae208SSatish Balay #undef __FUNCT__ 1677a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 1678a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw) 1679df9fa365SBarry Smith { 1680dfbe8321SBarry Smith PetscErrorCode ierr; 1681df9fa365SBarry Smith 1682df9fa365SBarry Smith PetscFunctionBegin; 1683a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1684df9fa365SBarry Smith PetscFunctionReturn(0); 1685df9fa365SBarry Smith } 1686df9fa365SBarry Smith 1687*b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 1688*b271bb04SBarry Smith #undef __FUNCT__ 1689*b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 1690*b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 1691*b271bb04SBarry Smith { 1692*b271bb04SBarry Smith PetscDrawLG lg; 1693*b271bb04SBarry Smith PetscErrorCode ierr; 1694*b271bb04SBarry Smith PetscReal x,y,per; 1695*b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 1696*b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 1697*b271bb04SBarry Smith PetscDraw draw; 1698*b271bb04SBarry Smith PetscFunctionBegin; 1699*b271bb04SBarry Smith if (!monctx) { 1700*b271bb04SBarry Smith MPI_Comm comm; 1701*b271bb04SBarry Smith 1702*b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 1703*b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 1704*b271bb04SBarry Smith } 1705*b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 1706*b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1707*b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1708*b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 1709*b271bb04SBarry Smith x = (PetscReal) n; 1710*b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 1711*b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1712*b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1713*b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1714*b271bb04SBarry Smith } 1715*b271bb04SBarry Smith 1716*b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 1717*b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1718*b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1719*b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 1720*b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 1721*b271bb04SBarry Smith x = (PetscReal) n; 1722*b271bb04SBarry Smith y = 100.0*per; 1723*b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1724*b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1725*b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1726*b271bb04SBarry Smith } 1727*b271bb04SBarry Smith 1728*b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 1729*b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1730*b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1731*b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 1732*b271bb04SBarry Smith x = (PetscReal) n; 1733*b271bb04SBarry Smith y = (prev - rnorm)/prev; 1734*b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1735*b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1736*b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1737*b271bb04SBarry Smith } 1738*b271bb04SBarry Smith 1739*b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 1740*b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1741*b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1742*b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 1743*b271bb04SBarry Smith x = (PetscReal) n; 1744*b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 1745*b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 1746*b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1747*b271bb04SBarry Smith } 1748*b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1749*b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1750*b271bb04SBarry Smith } 1751*b271bb04SBarry Smith prev = rnorm; 1752*b271bb04SBarry Smith PetscFunctionReturn(0); 1753*b271bb04SBarry Smith } 1754*b271bb04SBarry Smith 1755*b271bb04SBarry Smith #undef __FUNCT__ 1756*b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 1757*b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1758*b271bb04SBarry Smith { 1759*b271bb04SBarry Smith PetscErrorCode ierr; 1760*b271bb04SBarry Smith 1761*b271bb04SBarry Smith PetscFunctionBegin; 1762*b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1763*b271bb04SBarry Smith PetscFunctionReturn(0); 1764*b271bb04SBarry Smith } 1765*b271bb04SBarry Smith 1766*b271bb04SBarry Smith #undef __FUNCT__ 1767*b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 1768*b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw) 1769*b271bb04SBarry Smith { 1770*b271bb04SBarry Smith PetscErrorCode ierr; 1771*b271bb04SBarry Smith 1772*b271bb04SBarry Smith PetscFunctionBegin; 1773*b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1774*b271bb04SBarry Smith PetscFunctionReturn(0); 1775*b271bb04SBarry Smith } 1776*b271bb04SBarry Smith 17779b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 17789b94acceSBarry Smith 17794a2ae208SSatish Balay #undef __FUNCT__ 1780a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 17819b94acceSBarry Smith /*@C 1782a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 17839b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 17849b94acceSBarry Smith progress. 17859b94acceSBarry Smith 1786fee21e36SBarry Smith Collective on SNES 1787fee21e36SBarry Smith 1788c7afd0dbSLois Curfman McInnes Input Parameters: 1789c7afd0dbSLois Curfman McInnes + snes - the SNES context 1790c7afd0dbSLois Curfman McInnes . func - monitoring routine 1791b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1792e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 1793b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1794b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 17959b94acceSBarry Smith 1796c7afd0dbSLois Curfman McInnes Calling sequence of func: 1797a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 1798c7afd0dbSLois Curfman McInnes 1799c7afd0dbSLois Curfman McInnes + snes - the SNES context 1800c7afd0dbSLois Curfman McInnes . its - iteration number 1801c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 180240a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 18039b94acceSBarry Smith 18049665c990SLois Curfman McInnes Options Database Keys: 1805a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 1806a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 1807a6570f20SBarry Smith uses SNESMonitorLGCreate() 1808a6570f20SBarry Smith _ -snes_monitor_cancel - cancels all monitors that have 1809c7afd0dbSLois Curfman McInnes been hardwired into a code by 1810a6570f20SBarry Smith calls to SNESMonitorSet(), but 1811c7afd0dbSLois Curfman McInnes does not cancel those set via 1812c7afd0dbSLois Curfman McInnes the options database. 18139665c990SLois Curfman McInnes 1814639f9d9dSBarry Smith Notes: 18156bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 1816a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 18176bc08f3fSLois Curfman McInnes order in which they were set. 1818639f9d9dSBarry Smith 1819025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 1820025f1a04SBarry Smith 182136851e7fSLois Curfman McInnes Level: intermediate 182236851e7fSLois Curfman McInnes 18239b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 18249b94acceSBarry Smith 1825a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 18269b94acceSBarry Smith @*/ 1827b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 18289b94acceSBarry Smith { 1829b90d0a6eSBarry Smith PetscInt i; 1830b90d0a6eSBarry Smith 18313a40ed3dSBarry Smith PetscFunctionBegin; 18324482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1833639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 183429bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1835639f9d9dSBarry Smith } 1836b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 1837b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 1838b90d0a6eSBarry Smith 1839b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 1840b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 1841b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 1842b90d0a6eSBarry Smith PetscErrorCode ierr; 1843b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 1844b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 1845b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 1846b90d0a6eSBarry Smith ierr = (*monitordestroy)(mctx);CHKERRQ(ierr); 1847b90d0a6eSBarry Smith PetscFunctionReturn(0); 1848b90d0a6eSBarry Smith } 1849b90d0a6eSBarry Smith } 1850b90d0a6eSBarry Smith } 1851b90d0a6eSBarry Smith } 1852b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 1853b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1854639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 18553a40ed3dSBarry Smith PetscFunctionReturn(0); 18569b94acceSBarry Smith } 18579b94acceSBarry Smith 18584a2ae208SSatish Balay #undef __FUNCT__ 1859a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 18605cd90555SBarry Smith /*@C 1861a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 18625cd90555SBarry Smith 1863c7afd0dbSLois Curfman McInnes Collective on SNES 1864c7afd0dbSLois Curfman McInnes 18655cd90555SBarry Smith Input Parameters: 18665cd90555SBarry Smith . snes - the SNES context 18675cd90555SBarry Smith 18681a480d89SAdministrator Options Database Key: 1869a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 1870a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 1871c7afd0dbSLois Curfman McInnes set via the options database 18725cd90555SBarry Smith 18735cd90555SBarry Smith Notes: 18745cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 18755cd90555SBarry Smith 187636851e7fSLois Curfman McInnes Level: intermediate 187736851e7fSLois Curfman McInnes 18785cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 18795cd90555SBarry Smith 1880a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 18815cd90555SBarry Smith @*/ 1882a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes) 18835cd90555SBarry Smith { 1884d952e501SBarry Smith PetscErrorCode ierr; 1885d952e501SBarry Smith PetscInt i; 1886d952e501SBarry Smith 18875cd90555SBarry Smith PetscFunctionBegin; 18884482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1889d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1890d952e501SBarry Smith if (snes->monitordestroy[i]) { 1891d952e501SBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1892d952e501SBarry Smith } 1893d952e501SBarry Smith } 18945cd90555SBarry Smith snes->numbermonitors = 0; 18955cd90555SBarry Smith PetscFunctionReturn(0); 18965cd90555SBarry Smith } 18975cd90555SBarry Smith 18984a2ae208SSatish Balay #undef __FUNCT__ 18994a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 19009b94acceSBarry Smith /*@C 19019b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 19029b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 19039b94acceSBarry Smith 1904fee21e36SBarry Smith Collective on SNES 1905fee21e36SBarry Smith 1906c7afd0dbSLois Curfman McInnes Input Parameters: 1907c7afd0dbSLois Curfman McInnes + snes - the SNES context 1908c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 19097f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 19107f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 19119b94acceSBarry Smith 1912c7afd0dbSLois Curfman McInnes Calling sequence of func: 191306ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1914c7afd0dbSLois Curfman McInnes 1915c7afd0dbSLois Curfman McInnes + snes - the SNES context 191606ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 1917c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1918184914b5SBarry Smith . reason - reason for convergence/divergence 1919c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 19204b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 19214b27c08aSLois Curfman McInnes - f - 2-norm of function 19229b94acceSBarry Smith 192336851e7fSLois Curfman McInnes Level: advanced 192436851e7fSLois Curfman McInnes 19259b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 19269b94acceSBarry Smith 192785385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 19289b94acceSBarry Smith @*/ 19297f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 19309b94acceSBarry Smith { 19317f7931b9SBarry Smith PetscErrorCode ierr; 19327f7931b9SBarry Smith 19333a40ed3dSBarry Smith PetscFunctionBegin; 19344482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 193585385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 19367f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 19377f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 19387f7931b9SBarry Smith } 193985385478SLisandro Dalcin snes->ops->converged = func; 19407f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 194185385478SLisandro Dalcin snes->cnvP = cctx; 19423a40ed3dSBarry Smith PetscFunctionReturn(0); 19439b94acceSBarry Smith } 19449b94acceSBarry Smith 19454a2ae208SSatish Balay #undef __FUNCT__ 19464a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 194752baeb72SSatish Balay /*@ 1948184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1949184914b5SBarry Smith 1950184914b5SBarry Smith Not Collective 1951184914b5SBarry Smith 1952184914b5SBarry Smith Input Parameter: 1953184914b5SBarry Smith . snes - the SNES context 1954184914b5SBarry Smith 1955184914b5SBarry Smith Output Parameter: 19564d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 1957184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1958184914b5SBarry Smith 1959184914b5SBarry Smith Level: intermediate 1960184914b5SBarry Smith 1961184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1962184914b5SBarry Smith 1963184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1964184914b5SBarry Smith 196585385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 1966184914b5SBarry Smith @*/ 196763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1968184914b5SBarry Smith { 1969184914b5SBarry Smith PetscFunctionBegin; 19704482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 19714482741eSBarry Smith PetscValidPointer(reason,2); 1972184914b5SBarry Smith *reason = snes->reason; 1973184914b5SBarry Smith PetscFunctionReturn(0); 1974184914b5SBarry Smith } 1975184914b5SBarry Smith 19764a2ae208SSatish Balay #undef __FUNCT__ 19774a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 1978c9005455SLois Curfman McInnes /*@ 1979c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1980c9005455SLois Curfman McInnes 1981fee21e36SBarry Smith Collective on SNES 1982fee21e36SBarry Smith 1983c7afd0dbSLois Curfman McInnes Input Parameters: 1984c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1985c7afd0dbSLois Curfman McInnes . a - array to hold history 1986cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 1987758f92a0SBarry Smith . na - size of a and its 198864731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 1989758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1990c7afd0dbSLois Curfman McInnes 1991c9005455SLois Curfman McInnes Notes: 19924b27c08aSLois Curfman McInnes If set, this array will contain the function norms computed 1993c9005455SLois Curfman McInnes at each step. 1994c9005455SLois Curfman McInnes 1995c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1996c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1997c9005455SLois Curfman McInnes during the section of code that is being timed. 1998c9005455SLois Curfman McInnes 199936851e7fSLois Curfman McInnes Level: intermediate 200036851e7fSLois Curfman McInnes 2001c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2002758f92a0SBarry Smith 200308405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2004758f92a0SBarry Smith 2005c9005455SLois Curfman McInnes @*/ 2006a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset) 2007c9005455SLois Curfman McInnes { 20083a40ed3dSBarry Smith PetscFunctionBegin; 20094482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20104482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2011a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2012c9005455SLois Curfman McInnes snes->conv_hist = a; 2013758f92a0SBarry Smith snes->conv_hist_its = its; 2014758f92a0SBarry Smith snes->conv_hist_max = na; 2015a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2016758f92a0SBarry Smith snes->conv_hist_reset = reset; 2017758f92a0SBarry Smith PetscFunctionReturn(0); 2018758f92a0SBarry Smith } 2019758f92a0SBarry Smith 20204a2ae208SSatish Balay #undef __FUNCT__ 20214a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 20220c4c9dddSBarry Smith /*@C 2023758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 2024758f92a0SBarry Smith 2025758f92a0SBarry Smith Collective on SNES 2026758f92a0SBarry Smith 2027758f92a0SBarry Smith Input Parameter: 2028758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 2029758f92a0SBarry Smith 2030758f92a0SBarry Smith Output Parameters: 2031758f92a0SBarry Smith . a - array to hold history 2032758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 2033758f92a0SBarry Smith negative if not converged) for each solve. 2034758f92a0SBarry Smith - na - size of a and its 2035758f92a0SBarry Smith 2036758f92a0SBarry Smith Notes: 2037758f92a0SBarry Smith The calling sequence for this routine in Fortran is 2038758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 2039758f92a0SBarry Smith 2040758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 2041758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 2042758f92a0SBarry Smith during the section of code that is being timed. 2043758f92a0SBarry Smith 2044758f92a0SBarry Smith Level: intermediate 2045758f92a0SBarry Smith 2046758f92a0SBarry Smith .keywords: SNES, get, convergence, history 2047758f92a0SBarry Smith 2048758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 2049758f92a0SBarry Smith 2050758f92a0SBarry Smith @*/ 205163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 2052758f92a0SBarry Smith { 2053758f92a0SBarry Smith PetscFunctionBegin; 20544482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2055758f92a0SBarry Smith if (a) *a = snes->conv_hist; 2056758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 2057758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 20583a40ed3dSBarry Smith PetscFunctionReturn(0); 2059c9005455SLois Curfman McInnes } 2060c9005455SLois Curfman McInnes 2061e74ef692SMatthew Knepley #undef __FUNCT__ 2062e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 2063ac226902SBarry Smith /*@C 206476b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 20657e4bb74cSBarry Smith at the beginning o every iteration of the nonlinear solve. Specifically 20667e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 206776b2cf59SMatthew Knepley 206876b2cf59SMatthew Knepley Collective on SNES 206976b2cf59SMatthew Knepley 207076b2cf59SMatthew Knepley Input Parameters: 207176b2cf59SMatthew Knepley . snes - The nonlinear solver context 207276b2cf59SMatthew Knepley . func - The function 207376b2cf59SMatthew Knepley 207476b2cf59SMatthew Knepley Calling sequence of func: 2075b5d30489SBarry Smith . func (SNES snes, PetscInt step); 207676b2cf59SMatthew Knepley 207776b2cf59SMatthew Knepley . step - The current step of the iteration 207876b2cf59SMatthew Knepley 207976b2cf59SMatthew Knepley Level: intermediate 208076b2cf59SMatthew Knepley 208176b2cf59SMatthew Knepley .keywords: SNES, update 2082b5d30489SBarry Smith 208385385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 208476b2cf59SMatthew Knepley @*/ 208563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 208676b2cf59SMatthew Knepley { 208776b2cf59SMatthew Knepley PetscFunctionBegin; 20884482741eSBarry Smith PetscValidHeaderSpecific(snes, SNES_COOKIE,1); 2089e7788613SBarry Smith snes->ops->update = func; 209076b2cf59SMatthew Knepley PetscFunctionReturn(0); 209176b2cf59SMatthew Knepley } 209276b2cf59SMatthew Knepley 2093e74ef692SMatthew Knepley #undef __FUNCT__ 2094e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 209576b2cf59SMatthew Knepley /*@ 209676b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 209776b2cf59SMatthew Knepley 209876b2cf59SMatthew Knepley Not collective 209976b2cf59SMatthew Knepley 210076b2cf59SMatthew Knepley Input Parameters: 210176b2cf59SMatthew Knepley . snes - The nonlinear solver context 210276b2cf59SMatthew Knepley . step - The current step of the iteration 210376b2cf59SMatthew Knepley 2104205452f4SMatthew Knepley Level: intermediate 2105205452f4SMatthew Knepley 210676b2cf59SMatthew Knepley .keywords: SNES, update 2107a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 210876b2cf59SMatthew Knepley @*/ 210963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step) 211076b2cf59SMatthew Knepley { 211176b2cf59SMatthew Knepley PetscFunctionBegin; 211276b2cf59SMatthew Knepley PetscFunctionReturn(0); 211376b2cf59SMatthew Knepley } 211476b2cf59SMatthew Knepley 21154a2ae208SSatish Balay #undef __FUNCT__ 21164a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 21179b94acceSBarry Smith /* 21189b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 21199b94acceSBarry Smith positive parameter delta. 21209b94acceSBarry Smith 21219b94acceSBarry Smith Input Parameters: 2122c7afd0dbSLois Curfman McInnes + snes - the SNES context 21239b94acceSBarry Smith . y - approximate solution of linear system 21249b94acceSBarry Smith . fnorm - 2-norm of current function 2125c7afd0dbSLois Curfman McInnes - delta - trust region size 21269b94acceSBarry Smith 21279b94acceSBarry Smith Output Parameters: 2128c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 21299b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 21309b94acceSBarry Smith region, and exceeds zero otherwise. 2131c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 21329b94acceSBarry Smith 21339b94acceSBarry Smith Note: 21344b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 21359b94acceSBarry Smith is set to be the maximum allowable step size. 21369b94acceSBarry Smith 21379b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 21389b94acceSBarry Smith */ 2139dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 21409b94acceSBarry Smith { 2141064f8208SBarry Smith PetscReal nrm; 2142ea709b57SSatish Balay PetscScalar cnorm; 2143dfbe8321SBarry Smith PetscErrorCode ierr; 21443a40ed3dSBarry Smith 21453a40ed3dSBarry Smith PetscFunctionBegin; 21464482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 21474482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,2); 2148c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 2149184914b5SBarry Smith 2150064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 2151064f8208SBarry Smith if (nrm > *delta) { 2152064f8208SBarry Smith nrm = *delta/nrm; 2153064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 2154064f8208SBarry Smith cnorm = nrm; 21552dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 21569b94acceSBarry Smith *ynorm = *delta; 21579b94acceSBarry Smith } else { 21589b94acceSBarry Smith *gpnorm = 0.0; 2159064f8208SBarry Smith *ynorm = nrm; 21609b94acceSBarry Smith } 21613a40ed3dSBarry Smith PetscFunctionReturn(0); 21629b94acceSBarry Smith } 21639b94acceSBarry Smith 21644a2ae208SSatish Balay #undef __FUNCT__ 21654a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 21666ce558aeSBarry Smith /*@C 2167f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 2168f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 21699b94acceSBarry Smith 2170c7afd0dbSLois Curfman McInnes Collective on SNES 2171c7afd0dbSLois Curfman McInnes 2172b2002411SLois Curfman McInnes Input Parameters: 2173c7afd0dbSLois Curfman McInnes + snes - the SNES context 2174f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 217585385478SLisandro Dalcin - x - the solution vector. 21769b94acceSBarry Smith 2177b2002411SLois Curfman McInnes Notes: 21788ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 21798ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 21808ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 21818ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 21828ddd3da0SLois Curfman McInnes 218336851e7fSLois Curfman McInnes Level: beginner 218436851e7fSLois Curfman McInnes 21859b94acceSBarry Smith .keywords: SNES, nonlinear, solve 21869b94acceSBarry Smith 218785385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 21889b94acceSBarry Smith @*/ 2189f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x) 21909b94acceSBarry Smith { 2191dfbe8321SBarry Smith PetscErrorCode ierr; 2192f1af5d2fSBarry Smith PetscTruth flg; 2193eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 2194eabae89aSBarry Smith PetscViewer viewer; 2195052efed2SBarry Smith 21963a40ed3dSBarry Smith PetscFunctionBegin; 21974482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2198f69a0ea3SMatthew Knepley PetscValidHeaderSpecific(x,VEC_COOKIE,3); 2199f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 220085385478SLisandro Dalcin if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2); 220185385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 220285385478SLisandro Dalcin 220385385478SLisandro Dalcin /* set solution vector */ 220485385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 220585385478SLisandro Dalcin if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); } 220685385478SLisandro Dalcin snes->vec_sol = x; 220785385478SLisandro Dalcin /* set afine vector if provided */ 220885385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 220985385478SLisandro Dalcin if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); } 221085385478SLisandro Dalcin snes->vec_rhs = b; 221185385478SLisandro Dalcin 221285385478SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 221385385478SLisandro Dalcin ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr); 221470e92668SMatthew Knepley } 22153f149594SLisandro Dalcin 221670e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 22173f149594SLisandro Dalcin 2218abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 221950ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2220d5e45103SBarry Smith 22213f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 22224936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 222385385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 22244936397dSBarry Smith if (snes->domainerror){ 22254936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 22264936397dSBarry Smith snes->domainerror = PETSC_FALSE; 22274936397dSBarry Smith } 222885385478SLisandro Dalcin 22293f149594SLisandro Dalcin if (!snes->reason) { 22303f149594SLisandro Dalcin SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 22313f149594SLisandro Dalcin } 22323f149594SLisandro Dalcin 22337adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2234eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 22357adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2236eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 2237eabae89aSBarry Smith ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 2238eabae89aSBarry Smith } 2239eabae89aSBarry Smith 22407adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr); 2241da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 22425968eb51SBarry Smith if (snes->printreason) { 22435968eb51SBarry Smith if (snes->reason > 0) { 22447adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 22455968eb51SBarry Smith } else { 22467adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 22475968eb51SBarry Smith } 22485968eb51SBarry Smith } 22495968eb51SBarry Smith 22503a40ed3dSBarry Smith PetscFunctionReturn(0); 22519b94acceSBarry Smith } 22529b94acceSBarry Smith 22539b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 22549b94acceSBarry Smith 22554a2ae208SSatish Balay #undef __FUNCT__ 22564a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 225782bf6240SBarry Smith /*@C 22584b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 22599b94acceSBarry Smith 2260fee21e36SBarry Smith Collective on SNES 2261fee21e36SBarry Smith 2262c7afd0dbSLois Curfman McInnes Input Parameters: 2263c7afd0dbSLois Curfman McInnes + snes - the SNES context 2264454a90a3SBarry Smith - type - a known method 2265c7afd0dbSLois Curfman McInnes 2266c7afd0dbSLois Curfman McInnes Options Database Key: 2267454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2268c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2269ae12b187SLois Curfman McInnes 22709b94acceSBarry Smith Notes: 2271e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 22724b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2273c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 22744b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2275c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 22769b94acceSBarry Smith 2277ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2278ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2279ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2280ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2281ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2282ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2283ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2284ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2285ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2286b0a32e0cSBarry Smith appropriate method. 228736851e7fSLois Curfman McInnes 228836851e7fSLois Curfman McInnes Level: intermediate 2289a703fe33SLois Curfman McInnes 2290454a90a3SBarry Smith .keywords: SNES, set, type 2291435da068SBarry Smith 2292435da068SBarry Smith .seealso: SNESType, SNESCreate() 2293435da068SBarry Smith 22949b94acceSBarry Smith @*/ 2295a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type) 22969b94acceSBarry Smith { 2297dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 22986831982aSBarry Smith PetscTruth match; 22993a40ed3dSBarry Smith 23003a40ed3dSBarry Smith PetscFunctionBegin; 23014482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 23024482741eSBarry Smith PetscValidCharPointer(type,2); 230382bf6240SBarry Smith 23046831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 23050f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 230692ff6ae8SBarry Smith 23077adad957SLisandro Dalcin ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 2308958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 230975396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 231075396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 231175396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 231275396ef9SLisandro Dalcin snes->ops->setup = 0; 231375396ef9SLisandro Dalcin snes->ops->solve = 0; 231475396ef9SLisandro Dalcin snes->ops->view = 0; 231575396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 231675396ef9SLisandro Dalcin snes->ops->destroy = 0; 231775396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 231875396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 23193a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 2320454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 23213a40ed3dSBarry Smith PetscFunctionReturn(0); 23229b94acceSBarry Smith } 23239b94acceSBarry Smith 2324a847f771SSatish Balay 23259b94acceSBarry Smith /* --------------------------------------------------------------------- */ 23264a2ae208SSatish Balay #undef __FUNCT__ 23274a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 232852baeb72SSatish Balay /*@ 23299b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2330f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 23319b94acceSBarry Smith 2332fee21e36SBarry Smith Not Collective 2333fee21e36SBarry Smith 233436851e7fSLois Curfman McInnes Level: advanced 233536851e7fSLois Curfman McInnes 23369b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 23379b94acceSBarry Smith 23389b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 23399b94acceSBarry Smith @*/ 234063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void) 23419b94acceSBarry Smith { 2342dfbe8321SBarry Smith PetscErrorCode ierr; 234382bf6240SBarry Smith 23443a40ed3dSBarry Smith PetscFunctionBegin; 23451441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 23464c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 23473a40ed3dSBarry Smith PetscFunctionReturn(0); 23489b94acceSBarry Smith } 23499b94acceSBarry Smith 23504a2ae208SSatish Balay #undef __FUNCT__ 23514a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 23529b94acceSBarry Smith /*@C 23539a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 23549b94acceSBarry Smith 2355c7afd0dbSLois Curfman McInnes Not Collective 2356c7afd0dbSLois Curfman McInnes 23579b94acceSBarry Smith Input Parameter: 23584b0e389bSBarry Smith . snes - nonlinear solver context 23599b94acceSBarry Smith 23609b94acceSBarry Smith Output Parameter: 23613a7fca6bSBarry Smith . type - SNES method (a character string) 23629b94acceSBarry Smith 236336851e7fSLois Curfman McInnes Level: intermediate 236436851e7fSLois Curfman McInnes 2365454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 23669b94acceSBarry Smith @*/ 2367a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type) 23689b94acceSBarry Smith { 23693a40ed3dSBarry Smith PetscFunctionBegin; 23704482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 23714482741eSBarry Smith PetscValidPointer(type,2); 23727adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 23733a40ed3dSBarry Smith PetscFunctionReturn(0); 23749b94acceSBarry Smith } 23759b94acceSBarry Smith 23764a2ae208SSatish Balay #undef __FUNCT__ 23774a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 237852baeb72SSatish Balay /*@ 23799b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 23809b94acceSBarry Smith stored. 23819b94acceSBarry Smith 2382c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2383c7afd0dbSLois Curfman McInnes 23849b94acceSBarry Smith Input Parameter: 23859b94acceSBarry Smith . snes - the SNES context 23869b94acceSBarry Smith 23879b94acceSBarry Smith Output Parameter: 23889b94acceSBarry Smith . x - the solution 23899b94acceSBarry Smith 239070e92668SMatthew Knepley Level: intermediate 239136851e7fSLois Curfman McInnes 23929b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 23939b94acceSBarry Smith 239485385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 23959b94acceSBarry Smith @*/ 239663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x) 23979b94acceSBarry Smith { 23983a40ed3dSBarry Smith PetscFunctionBegin; 23994482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 24004482741eSBarry Smith PetscValidPointer(x,2); 240185385478SLisandro Dalcin *x = snes->vec_sol; 240270e92668SMatthew Knepley PetscFunctionReturn(0); 240370e92668SMatthew Knepley } 240470e92668SMatthew Knepley 240570e92668SMatthew Knepley #undef __FUNCT__ 24064a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 240752baeb72SSatish Balay /*@ 24089b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 24099b94acceSBarry Smith stored. 24109b94acceSBarry Smith 2411c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2412c7afd0dbSLois Curfman McInnes 24139b94acceSBarry Smith Input Parameter: 24149b94acceSBarry Smith . snes - the SNES context 24159b94acceSBarry Smith 24169b94acceSBarry Smith Output Parameter: 24179b94acceSBarry Smith . x - the solution update 24189b94acceSBarry Smith 241936851e7fSLois Curfman McInnes Level: advanced 242036851e7fSLois Curfman McInnes 24219b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 24229b94acceSBarry Smith 242385385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 24249b94acceSBarry Smith @*/ 242563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x) 24269b94acceSBarry Smith { 24273a40ed3dSBarry Smith PetscFunctionBegin; 24284482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 24294482741eSBarry Smith PetscValidPointer(x,2); 243085385478SLisandro Dalcin *x = snes->vec_sol_update; 24313a40ed3dSBarry Smith PetscFunctionReturn(0); 24329b94acceSBarry Smith } 24339b94acceSBarry Smith 24344a2ae208SSatish Balay #undef __FUNCT__ 24354a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 24369b94acceSBarry Smith /*@C 24373638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 24389b94acceSBarry Smith 2439c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2440c7afd0dbSLois Curfman McInnes 24419b94acceSBarry Smith Input Parameter: 24429b94acceSBarry Smith . snes - the SNES context 24439b94acceSBarry Smith 24449b94acceSBarry Smith Output Parameter: 24457bf4e008SBarry Smith + r - the function (or PETSC_NULL) 244670e92668SMatthew Knepley . func - the function (or PETSC_NULL) 244770e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 24489b94acceSBarry Smith 244936851e7fSLois Curfman McInnes Level: advanced 245036851e7fSLois Curfman McInnes 2451a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 24529b94acceSBarry Smith 24534b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 24549b94acceSBarry Smith @*/ 245570e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 24569b94acceSBarry Smith { 24573a40ed3dSBarry Smith PetscFunctionBegin; 24584482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 245985385478SLisandro Dalcin if (r) *r = snes->vec_func; 2460e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 246170e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 24623a40ed3dSBarry Smith PetscFunctionReturn(0); 24639b94acceSBarry Smith } 24649b94acceSBarry Smith 24654a2ae208SSatish Balay #undef __FUNCT__ 24664a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 24673c7409f5SSatish Balay /*@C 24683c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2469d850072dSLois Curfman McInnes SNES options in the database. 24703c7409f5SSatish Balay 2471fee21e36SBarry Smith Collective on SNES 2472fee21e36SBarry Smith 2473c7afd0dbSLois Curfman McInnes Input Parameter: 2474c7afd0dbSLois Curfman McInnes + snes - the SNES context 2475c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2476c7afd0dbSLois Curfman McInnes 2477d850072dSLois Curfman McInnes Notes: 2478a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2479c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2480d850072dSLois Curfman McInnes 248136851e7fSLois Curfman McInnes Level: advanced 248236851e7fSLois Curfman McInnes 24833c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2484a86d99e1SLois Curfman McInnes 2485a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 24863c7409f5SSatish Balay @*/ 248763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[]) 24883c7409f5SSatish Balay { 2489dfbe8321SBarry Smith PetscErrorCode ierr; 24903c7409f5SSatish Balay 24913a40ed3dSBarry Smith PetscFunctionBegin; 24924482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2493639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 24941cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 249594b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 24963a40ed3dSBarry Smith PetscFunctionReturn(0); 24973c7409f5SSatish Balay } 24983c7409f5SSatish Balay 24994a2ae208SSatish Balay #undef __FUNCT__ 25004a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 25013c7409f5SSatish Balay /*@C 2502f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2503d850072dSLois Curfman McInnes SNES options in the database. 25043c7409f5SSatish Balay 2505fee21e36SBarry Smith Collective on SNES 2506fee21e36SBarry Smith 2507c7afd0dbSLois Curfman McInnes Input Parameters: 2508c7afd0dbSLois Curfman McInnes + snes - the SNES context 2509c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2510c7afd0dbSLois Curfman McInnes 2511d850072dSLois Curfman McInnes Notes: 2512a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2513c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2514d850072dSLois Curfman McInnes 251536851e7fSLois Curfman McInnes Level: advanced 251636851e7fSLois Curfman McInnes 25173c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2518a86d99e1SLois Curfman McInnes 2519a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 25203c7409f5SSatish Balay @*/ 252163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 25223c7409f5SSatish Balay { 2523dfbe8321SBarry Smith PetscErrorCode ierr; 25243c7409f5SSatish Balay 25253a40ed3dSBarry Smith PetscFunctionBegin; 25264482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2527639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 25281cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 252994b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 25303a40ed3dSBarry Smith PetscFunctionReturn(0); 25313c7409f5SSatish Balay } 25323c7409f5SSatish Balay 25334a2ae208SSatish Balay #undef __FUNCT__ 25344a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 25359ab63eb5SSatish Balay /*@C 25363c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 25373c7409f5SSatish Balay SNES options in the database. 25383c7409f5SSatish Balay 2539c7afd0dbSLois Curfman McInnes Not Collective 2540c7afd0dbSLois Curfman McInnes 25413c7409f5SSatish Balay Input Parameter: 25423c7409f5SSatish Balay . snes - the SNES context 25433c7409f5SSatish Balay 25443c7409f5SSatish Balay Output Parameter: 25453c7409f5SSatish Balay . prefix - pointer to the prefix string used 25463c7409f5SSatish Balay 25474ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 25489ab63eb5SSatish Balay sufficient length to hold the prefix. 25499ab63eb5SSatish Balay 255036851e7fSLois Curfman McInnes Level: advanced 255136851e7fSLois Curfman McInnes 25523c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2553a86d99e1SLois Curfman McInnes 2554a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 25553c7409f5SSatish Balay @*/ 2556e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 25573c7409f5SSatish Balay { 2558dfbe8321SBarry Smith PetscErrorCode ierr; 25593c7409f5SSatish Balay 25603a40ed3dSBarry Smith PetscFunctionBegin; 25614482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2562639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 25633a40ed3dSBarry Smith PetscFunctionReturn(0); 25643c7409f5SSatish Balay } 25653c7409f5SSatish Balay 2566b2002411SLois Curfman McInnes 25674a2ae208SSatish Balay #undef __FUNCT__ 25684a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 25693cea93caSBarry Smith /*@C 25703cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 25713cea93caSBarry Smith 25727f6c08e0SMatthew Knepley Level: advanced 25733cea93caSBarry Smith @*/ 257463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2575b2002411SLois Curfman McInnes { 2576e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2577dfbe8321SBarry Smith PetscErrorCode ierr; 2578b2002411SLois Curfman McInnes 2579b2002411SLois Curfman McInnes PetscFunctionBegin; 2580b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2581c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2582b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2583b2002411SLois Curfman McInnes } 2584da9b6338SBarry Smith 2585da9b6338SBarry Smith #undef __FUNCT__ 2586da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 258763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes) 2588da9b6338SBarry Smith { 2589dfbe8321SBarry Smith PetscErrorCode ierr; 259077431f27SBarry Smith PetscInt N,i,j; 2591da9b6338SBarry Smith Vec u,uh,fh; 2592da9b6338SBarry Smith PetscScalar value; 2593da9b6338SBarry Smith PetscReal norm; 2594da9b6338SBarry Smith 2595da9b6338SBarry Smith PetscFunctionBegin; 2596da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 2597da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 2598da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 2599da9b6338SBarry Smith 2600da9b6338SBarry Smith /* currently only works for sequential */ 2601da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 2602da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 2603da9b6338SBarry Smith for (i=0; i<N; i++) { 2604da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 260577431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 2606da9b6338SBarry Smith for (j=-10; j<11; j++) { 2607ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 2608da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 26093ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 2610da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 261177431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 2612da9b6338SBarry Smith value = -value; 2613da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 2614da9b6338SBarry Smith } 2615da9b6338SBarry Smith } 2616da9b6338SBarry Smith ierr = VecDestroy(uh);CHKERRQ(ierr); 2617da9b6338SBarry Smith ierr = VecDestroy(fh);CHKERRQ(ierr); 2618da9b6338SBarry Smith PetscFunctionReturn(0); 2619da9b6338SBarry Smith } 262071f87433Sdalcinl 262171f87433Sdalcinl #undef __FUNCT__ 2622fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 262371f87433Sdalcinl /*@ 2624fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 262571f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 262671f87433Sdalcinl Newton method. 262771f87433Sdalcinl 262871f87433Sdalcinl Collective on SNES 262971f87433Sdalcinl 263071f87433Sdalcinl Input Parameters: 263171f87433Sdalcinl + snes - SNES context 263271f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 263371f87433Sdalcinl 263471f87433Sdalcinl Notes: 263571f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 263671f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 263771f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 263871f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 263971f87433Sdalcinl solver. 264071f87433Sdalcinl 264171f87433Sdalcinl Level: advanced 264271f87433Sdalcinl 264371f87433Sdalcinl Reference: 264471f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 264571f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 264671f87433Sdalcinl 264771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 264871f87433Sdalcinl 2649fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 265071f87433Sdalcinl @*/ 2651fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag) 265271f87433Sdalcinl { 265371f87433Sdalcinl PetscFunctionBegin; 265471f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 265571f87433Sdalcinl snes->ksp_ewconv = flag; 265671f87433Sdalcinl PetscFunctionReturn(0); 265771f87433Sdalcinl } 265871f87433Sdalcinl 265971f87433Sdalcinl #undef __FUNCT__ 2660fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 266171f87433Sdalcinl /*@ 2662fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 266371f87433Sdalcinl for computing relative tolerance for linear solvers within an 266471f87433Sdalcinl inexact Newton method. 266571f87433Sdalcinl 266671f87433Sdalcinl Not Collective 266771f87433Sdalcinl 266871f87433Sdalcinl Input Parameter: 266971f87433Sdalcinl . snes - SNES context 267071f87433Sdalcinl 267171f87433Sdalcinl Output Parameter: 267271f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 267371f87433Sdalcinl 267471f87433Sdalcinl Notes: 267571f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 267671f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 267771f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 267871f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 267971f87433Sdalcinl solver. 268071f87433Sdalcinl 268171f87433Sdalcinl Level: advanced 268271f87433Sdalcinl 268371f87433Sdalcinl Reference: 268471f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 268571f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 268671f87433Sdalcinl 268771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 268871f87433Sdalcinl 2689fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 269071f87433Sdalcinl @*/ 2691fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag) 269271f87433Sdalcinl { 269371f87433Sdalcinl PetscFunctionBegin; 269471f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 269571f87433Sdalcinl PetscValidPointer(flag,2); 269671f87433Sdalcinl *flag = snes->ksp_ewconv; 269771f87433Sdalcinl PetscFunctionReturn(0); 269871f87433Sdalcinl } 269971f87433Sdalcinl 270071f87433Sdalcinl #undef __FUNCT__ 2701fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 270271f87433Sdalcinl /*@ 2703fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 270471f87433Sdalcinl convergence criteria for the linear solvers within an inexact 270571f87433Sdalcinl Newton method. 270671f87433Sdalcinl 270771f87433Sdalcinl Collective on SNES 270871f87433Sdalcinl 270971f87433Sdalcinl Input Parameters: 271071f87433Sdalcinl + snes - SNES context 271171f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 271271f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 271371f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 271471f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 271571f87433Sdalcinl (0 <= gamma2 <= 1) 271671f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 271771f87433Sdalcinl . alpha2 - power for safeguard 271871f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 271971f87433Sdalcinl 272071f87433Sdalcinl Note: 272171f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 272271f87433Sdalcinl 272371f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 272471f87433Sdalcinl 272571f87433Sdalcinl Level: advanced 272671f87433Sdalcinl 272771f87433Sdalcinl Reference: 272871f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 272971f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 273071f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 273171f87433Sdalcinl 273271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 273371f87433Sdalcinl 2734fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 273571f87433Sdalcinl @*/ 2736fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 273771f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 273871f87433Sdalcinl { 2739fa9f3622SBarry Smith SNESKSPEW *kctx; 274071f87433Sdalcinl PetscFunctionBegin; 274171f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2742fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 274371f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 274471f87433Sdalcinl 274571f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 274671f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 274771f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 274871f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 274971f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 275071f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 275171f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 275271f87433Sdalcinl 275371f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 275471f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 275571f87433Sdalcinl } 275671f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 275771f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 275871f87433Sdalcinl } 275971f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 276071f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 276171f87433Sdalcinl } 276271f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 276371f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 276471f87433Sdalcinl } 276571f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 276671f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 276771f87433Sdalcinl } 276871f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 276971f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 277071f87433Sdalcinl } 277171f87433Sdalcinl PetscFunctionReturn(0); 277271f87433Sdalcinl } 277371f87433Sdalcinl 277471f87433Sdalcinl #undef __FUNCT__ 2775fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 277671f87433Sdalcinl /*@ 2777fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 277871f87433Sdalcinl convergence criteria for the linear solvers within an inexact 277971f87433Sdalcinl Newton method. 278071f87433Sdalcinl 278171f87433Sdalcinl Not Collective 278271f87433Sdalcinl 278371f87433Sdalcinl Input Parameters: 278471f87433Sdalcinl snes - SNES context 278571f87433Sdalcinl 278671f87433Sdalcinl Output Parameters: 278771f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 278871f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 278971f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 279071f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 279171f87433Sdalcinl (0 <= gamma2 <= 1) 279271f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 279371f87433Sdalcinl . alpha2 - power for safeguard 279471f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 279571f87433Sdalcinl 279671f87433Sdalcinl Level: advanced 279771f87433Sdalcinl 279871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 279971f87433Sdalcinl 2800fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 280171f87433Sdalcinl @*/ 2802fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 280371f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 280471f87433Sdalcinl { 2805fa9f3622SBarry Smith SNESKSPEW *kctx; 280671f87433Sdalcinl PetscFunctionBegin; 280771f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2808fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 280971f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 281071f87433Sdalcinl if(version) *version = kctx->version; 281171f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 281271f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 281371f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 281471f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 281571f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 281671f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 281771f87433Sdalcinl PetscFunctionReturn(0); 281871f87433Sdalcinl } 281971f87433Sdalcinl 282071f87433Sdalcinl #undef __FUNCT__ 2821fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 2822fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 282371f87433Sdalcinl { 282471f87433Sdalcinl PetscErrorCode ierr; 2825fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 282671f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 282771f87433Sdalcinl 282871f87433Sdalcinl PetscFunctionBegin; 282971f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 283071f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 283171f87433Sdalcinl rtol = kctx->rtol_0; 283271f87433Sdalcinl } else { 283371f87433Sdalcinl if (kctx->version == 1) { 283471f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 283571f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 283671f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 283771f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 283871f87433Sdalcinl } else if (kctx->version == 2) { 283971f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 284071f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 284171f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 284271f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 284371f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 284471f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 284571f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 284671f87433Sdalcinl stol = PetscMax(rtol,stol); 284771f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 284871f87433Sdalcinl /* safeguard: avoid oversolving */ 284971f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 285071f87433Sdalcinl stol = PetscMax(rtol,stol); 285171f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 285271f87433Sdalcinl } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 285371f87433Sdalcinl } 285471f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 285571f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 285671f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 285771f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 285871f87433Sdalcinl PetscFunctionReturn(0); 285971f87433Sdalcinl } 286071f87433Sdalcinl 286171f87433Sdalcinl #undef __FUNCT__ 2862fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 2863fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 286471f87433Sdalcinl { 286571f87433Sdalcinl PetscErrorCode ierr; 2866fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 286771f87433Sdalcinl PCSide pcside; 286871f87433Sdalcinl Vec lres; 286971f87433Sdalcinl 287071f87433Sdalcinl PetscFunctionBegin; 287171f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 287271f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 287371f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 287471f87433Sdalcinl if (kctx->version == 1) { 287571f87433Sdalcinl ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr); 287671f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 287771f87433Sdalcinl /* KSP residual is true linear residual */ 287871f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 287971f87433Sdalcinl } else { 288071f87433Sdalcinl /* KSP residual is preconditioned residual */ 288171f87433Sdalcinl /* compute true linear residual norm */ 288271f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 288371f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 288471f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 288571f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 288671f87433Sdalcinl ierr = VecDestroy(lres);CHKERRQ(ierr); 288771f87433Sdalcinl } 288871f87433Sdalcinl } 288971f87433Sdalcinl PetscFunctionReturn(0); 289071f87433Sdalcinl } 289171f87433Sdalcinl 289271f87433Sdalcinl #undef __FUNCT__ 289371f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 289471f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 289571f87433Sdalcinl { 289671f87433Sdalcinl PetscErrorCode ierr; 289771f87433Sdalcinl 289871f87433Sdalcinl PetscFunctionBegin; 2899fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 290071f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 2901fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 290271f87433Sdalcinl PetscFunctionReturn(0); 290371f87433Sdalcinl } 2904