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 293a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 294eabae89aSBarry Smith if (flg) { 295050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 296f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 297e8105e01SRichard Katz } 298eabae89aSBarry Smith 299a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 300eabae89aSBarry Smith if (flg) { 301050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 30223d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 303eabae89aSBarry Smith } 304eabae89aSBarry Smith 305a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr); 306a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 307a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr); 308a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 309a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr); 310a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 311a6570f20SBarry Smith ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr); 312a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 313e24b481bSBarry Smith 314b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr); 3154b27c08aSLois Curfman McInnes if (flg) { 316186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 317ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 3189b94acceSBarry Smith } 319639f9d9dSBarry Smith 32076b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 32176b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 32276b2cf59SMatthew Knepley } 32376b2cf59SMatthew Knepley 324e7788613SBarry Smith if (snes->ops->setfromoptions) { 325e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 326639f9d9dSBarry Smith } 327b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3284bbc92c1SBarry Smith 3291cee3971SBarry Smith 3301cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 33185385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 33293993e2dSLois Curfman McInnes 3333a40ed3dSBarry Smith PetscFunctionReturn(0); 3349b94acceSBarry Smith } 3359b94acceSBarry Smith 336a847f771SSatish Balay 3374a2ae208SSatish Balay #undef __FUNCT__ 3384a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 3399b94acceSBarry Smith /*@ 3409b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3419b94acceSBarry Smith the nonlinear solvers. 3429b94acceSBarry Smith 343fee21e36SBarry Smith Collective on SNES 344fee21e36SBarry Smith 345c7afd0dbSLois Curfman McInnes Input Parameters: 346c7afd0dbSLois Curfman McInnes + snes - the SNES context 347c7afd0dbSLois Curfman McInnes - usrP - optional user context 348c7afd0dbSLois Curfman McInnes 34936851e7fSLois Curfman McInnes Level: intermediate 35036851e7fSLois Curfman McInnes 3519b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3529b94acceSBarry Smith 3539b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3549b94acceSBarry Smith @*/ 35563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP) 3569b94acceSBarry Smith { 3573a40ed3dSBarry Smith PetscFunctionBegin; 3584482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3599b94acceSBarry Smith snes->user = usrP; 3603a40ed3dSBarry Smith PetscFunctionReturn(0); 3619b94acceSBarry Smith } 36274679c65SBarry Smith 3634a2ae208SSatish Balay #undef __FUNCT__ 3644a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 3659b94acceSBarry Smith /*@C 3669b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3679b94acceSBarry Smith nonlinear solvers. 3689b94acceSBarry Smith 369c7afd0dbSLois Curfman McInnes Not Collective 370c7afd0dbSLois Curfman McInnes 3719b94acceSBarry Smith Input Parameter: 3729b94acceSBarry Smith . snes - SNES context 3739b94acceSBarry Smith 3749b94acceSBarry Smith Output Parameter: 3759b94acceSBarry Smith . usrP - user context 3769b94acceSBarry Smith 37736851e7fSLois Curfman McInnes Level: intermediate 37836851e7fSLois Curfman McInnes 3799b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3809b94acceSBarry Smith 3819b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3829b94acceSBarry Smith @*/ 38363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP) 3849b94acceSBarry Smith { 3853a40ed3dSBarry Smith PetscFunctionBegin; 3864482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3879b94acceSBarry Smith *usrP = snes->user; 3883a40ed3dSBarry Smith PetscFunctionReturn(0); 3899b94acceSBarry Smith } 39074679c65SBarry Smith 3914a2ae208SSatish Balay #undef __FUNCT__ 3924a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 3939b94acceSBarry Smith /*@ 394c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 395c8228a4eSBarry Smith at this time. 3969b94acceSBarry Smith 397c7afd0dbSLois Curfman McInnes Not Collective 398c7afd0dbSLois Curfman McInnes 3999b94acceSBarry Smith Input Parameter: 4009b94acceSBarry Smith . snes - SNES context 4019b94acceSBarry Smith 4029b94acceSBarry Smith Output Parameter: 4039b94acceSBarry Smith . iter - iteration number 4049b94acceSBarry Smith 405c8228a4eSBarry Smith Notes: 406c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 407c8228a4eSBarry Smith 408c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 40908405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 41008405cd6SLois Curfman McInnes .vb 41108405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 41208405cd6SLois Curfman McInnes if (!(it % 2)) { 41308405cd6SLois Curfman McInnes [compute Jacobian here] 41408405cd6SLois Curfman McInnes } 41508405cd6SLois Curfman McInnes .ve 416c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 41708405cd6SLois Curfman McInnes recomputed every second SNES iteration. 418c8228a4eSBarry Smith 41936851e7fSLois Curfman McInnes Level: intermediate 42036851e7fSLois Curfman McInnes 4212b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 4222b668275SBarry Smith 423b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 4249b94acceSBarry Smith @*/ 42563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter) 4269b94acceSBarry Smith { 4273a40ed3dSBarry Smith PetscFunctionBegin; 4284482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4294482741eSBarry Smith PetscValidIntPointer(iter,2); 4309b94acceSBarry Smith *iter = snes->iter; 4313a40ed3dSBarry Smith PetscFunctionReturn(0); 4329b94acceSBarry Smith } 43374679c65SBarry Smith 4344a2ae208SSatish Balay #undef __FUNCT__ 4354a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 4369b94acceSBarry Smith /*@ 4379b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4389b94acceSBarry Smith with SNESSSetFunction(). 4399b94acceSBarry Smith 440c7afd0dbSLois Curfman McInnes Collective on SNES 441c7afd0dbSLois Curfman McInnes 4429b94acceSBarry Smith Input Parameter: 4439b94acceSBarry Smith . snes - SNES context 4449b94acceSBarry Smith 4459b94acceSBarry Smith Output Parameter: 4469b94acceSBarry Smith . fnorm - 2-norm of function 4479b94acceSBarry Smith 44836851e7fSLois Curfman McInnes Level: intermediate 44936851e7fSLois Curfman McInnes 4509b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 451a86d99e1SLois Curfman McInnes 452b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 4539b94acceSBarry Smith @*/ 45471f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 4559b94acceSBarry Smith { 4563a40ed3dSBarry Smith PetscFunctionBegin; 4574482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4584482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 4599b94acceSBarry Smith *fnorm = snes->norm; 4603a40ed3dSBarry Smith PetscFunctionReturn(0); 4619b94acceSBarry Smith } 46274679c65SBarry Smith 4634a2ae208SSatish Balay #undef __FUNCT__ 464b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 4659b94acceSBarry Smith /*@ 466b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 4679b94acceSBarry Smith attempted by the nonlinear solver. 4689b94acceSBarry Smith 469c7afd0dbSLois Curfman McInnes Not Collective 470c7afd0dbSLois Curfman McInnes 4719b94acceSBarry Smith Input Parameter: 4729b94acceSBarry Smith . snes - SNES context 4739b94acceSBarry Smith 4749b94acceSBarry Smith Output Parameter: 4759b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 4769b94acceSBarry Smith 477c96a6f78SLois Curfman McInnes Notes: 478c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 479c96a6f78SLois Curfman McInnes 48036851e7fSLois Curfman McInnes Level: intermediate 48136851e7fSLois Curfman McInnes 4829b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 48358ebbce7SBarry Smith 484e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 48558ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 4869b94acceSBarry Smith @*/ 487b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 4889b94acceSBarry Smith { 4893a40ed3dSBarry Smith PetscFunctionBegin; 4904482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4914482741eSBarry Smith PetscValidIntPointer(nfails,2); 49250ffb88aSMatthew Knepley *nfails = snes->numFailures; 49350ffb88aSMatthew Knepley PetscFunctionReturn(0); 49450ffb88aSMatthew Knepley } 49550ffb88aSMatthew Knepley 49650ffb88aSMatthew Knepley #undef __FUNCT__ 497b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 49850ffb88aSMatthew Knepley /*@ 499b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 50050ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 50150ffb88aSMatthew Knepley 50250ffb88aSMatthew Knepley Not Collective 50350ffb88aSMatthew Knepley 50450ffb88aSMatthew Knepley Input Parameters: 50550ffb88aSMatthew Knepley + snes - SNES context 50650ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 50750ffb88aSMatthew Knepley 50850ffb88aSMatthew Knepley Level: intermediate 50950ffb88aSMatthew Knepley 51050ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 51158ebbce7SBarry Smith 512e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 51358ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 51450ffb88aSMatthew Knepley @*/ 515b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 51650ffb88aSMatthew Knepley { 51750ffb88aSMatthew Knepley PetscFunctionBegin; 5184482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 51950ffb88aSMatthew Knepley snes->maxFailures = maxFails; 52050ffb88aSMatthew Knepley PetscFunctionReturn(0); 52150ffb88aSMatthew Knepley } 52250ffb88aSMatthew Knepley 52350ffb88aSMatthew Knepley #undef __FUNCT__ 524b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 52550ffb88aSMatthew Knepley /*@ 526b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 52750ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 52850ffb88aSMatthew Knepley 52950ffb88aSMatthew Knepley Not Collective 53050ffb88aSMatthew Knepley 53150ffb88aSMatthew Knepley Input Parameter: 53250ffb88aSMatthew Knepley . snes - SNES context 53350ffb88aSMatthew Knepley 53450ffb88aSMatthew Knepley Output Parameter: 53550ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 53650ffb88aSMatthew Knepley 53750ffb88aSMatthew Knepley Level: intermediate 53850ffb88aSMatthew Knepley 53950ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 54058ebbce7SBarry Smith 541e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 54258ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 54358ebbce7SBarry Smith 54450ffb88aSMatthew Knepley @*/ 545b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 54650ffb88aSMatthew Knepley { 54750ffb88aSMatthew Knepley PetscFunctionBegin; 5484482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5494482741eSBarry Smith PetscValidIntPointer(maxFails,2); 55050ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 5513a40ed3dSBarry Smith PetscFunctionReturn(0); 5529b94acceSBarry Smith } 553a847f771SSatish Balay 5544a2ae208SSatish Balay #undef __FUNCT__ 5552541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 5562541af92SBarry Smith /*@ 5572541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 5582541af92SBarry Smith done by SNES. 5592541af92SBarry Smith 5602541af92SBarry Smith Not Collective 5612541af92SBarry Smith 5622541af92SBarry Smith Input Parameter: 5632541af92SBarry Smith . snes - SNES context 5642541af92SBarry Smith 5652541af92SBarry Smith Output Parameter: 5662541af92SBarry Smith . nfuncs - number of evaluations 5672541af92SBarry Smith 5682541af92SBarry Smith Level: intermediate 5692541af92SBarry Smith 5702541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 57158ebbce7SBarry Smith 572e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 5732541af92SBarry Smith @*/ 5742541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 5752541af92SBarry Smith { 5762541af92SBarry Smith PetscFunctionBegin; 5772541af92SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5782541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 5792541af92SBarry Smith *nfuncs = snes->nfuncs; 5802541af92SBarry Smith PetscFunctionReturn(0); 5812541af92SBarry Smith } 5822541af92SBarry Smith 5832541af92SBarry Smith #undef __FUNCT__ 5843d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 5853d4c4710SBarry Smith /*@ 5863d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 5873d4c4710SBarry Smith linear solvers. 5883d4c4710SBarry Smith 5893d4c4710SBarry Smith Not Collective 5903d4c4710SBarry Smith 5913d4c4710SBarry Smith Input Parameter: 5923d4c4710SBarry Smith . snes - SNES context 5933d4c4710SBarry Smith 5943d4c4710SBarry Smith Output Parameter: 5953d4c4710SBarry Smith . nfails - number of failed solves 5963d4c4710SBarry Smith 5973d4c4710SBarry Smith Notes: 5983d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 5993d4c4710SBarry Smith 6003d4c4710SBarry Smith Level: intermediate 6013d4c4710SBarry Smith 6023d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 60358ebbce7SBarry Smith 604e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 6053d4c4710SBarry Smith @*/ 6063d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 6073d4c4710SBarry Smith { 6083d4c4710SBarry Smith PetscFunctionBegin; 6093d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6103d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 6113d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 6123d4c4710SBarry Smith PetscFunctionReturn(0); 6133d4c4710SBarry Smith } 6143d4c4710SBarry Smith 6153d4c4710SBarry Smith #undef __FUNCT__ 6163d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 6173d4c4710SBarry Smith /*@ 6183d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 6193d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 6203d4c4710SBarry Smith 6213d4c4710SBarry Smith Collective on SNES 6223d4c4710SBarry Smith 6233d4c4710SBarry Smith Input Parameters: 6243d4c4710SBarry Smith + snes - SNES context 6253d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 6263d4c4710SBarry Smith 6273d4c4710SBarry Smith Level: intermediate 6283d4c4710SBarry Smith 629a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 6303d4c4710SBarry Smith 6313d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 6323d4c4710SBarry Smith 63358ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 6343d4c4710SBarry Smith @*/ 6353d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 6363d4c4710SBarry Smith { 6373d4c4710SBarry Smith PetscFunctionBegin; 6383d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6393d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 6403d4c4710SBarry Smith PetscFunctionReturn(0); 6413d4c4710SBarry Smith } 6423d4c4710SBarry Smith 6433d4c4710SBarry Smith #undef __FUNCT__ 6443d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 6453d4c4710SBarry Smith /*@ 6463d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 6473d4c4710SBarry Smith are allowed before SNES terminates 6483d4c4710SBarry Smith 6493d4c4710SBarry Smith Not Collective 6503d4c4710SBarry Smith 6513d4c4710SBarry Smith Input Parameter: 6523d4c4710SBarry Smith . snes - SNES context 6533d4c4710SBarry Smith 6543d4c4710SBarry Smith Output Parameter: 6553d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 6563d4c4710SBarry Smith 6573d4c4710SBarry Smith Level: intermediate 6583d4c4710SBarry Smith 6593d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 6603d4c4710SBarry Smith 6613d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 6623d4c4710SBarry Smith 663e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 6643d4c4710SBarry Smith @*/ 6653d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 6663d4c4710SBarry Smith { 6673d4c4710SBarry Smith PetscFunctionBegin; 6683d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6693d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 6703d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 6713d4c4710SBarry Smith PetscFunctionReturn(0); 6723d4c4710SBarry Smith } 6733d4c4710SBarry Smith 6743d4c4710SBarry Smith #undef __FUNCT__ 675b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 676c96a6f78SLois Curfman McInnes /*@ 677b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 678c96a6f78SLois Curfman McInnes used by the nonlinear solver. 679c96a6f78SLois Curfman McInnes 680c7afd0dbSLois Curfman McInnes Not Collective 681c7afd0dbSLois Curfman McInnes 682c96a6f78SLois Curfman McInnes Input Parameter: 683c96a6f78SLois Curfman McInnes . snes - SNES context 684c96a6f78SLois Curfman McInnes 685c96a6f78SLois Curfman McInnes Output Parameter: 686c96a6f78SLois Curfman McInnes . lits - number of linear iterations 687c96a6f78SLois Curfman McInnes 688c96a6f78SLois Curfman McInnes Notes: 689c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 690c96a6f78SLois Curfman McInnes 69136851e7fSLois Curfman McInnes Level: intermediate 69236851e7fSLois Curfman McInnes 693c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 6942b668275SBarry Smith 69558ebbce7SBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm()S, NESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 696c96a6f78SLois Curfman McInnes @*/ 697b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 698c96a6f78SLois Curfman McInnes { 6993a40ed3dSBarry Smith PetscFunctionBegin; 7004482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7014482741eSBarry Smith PetscValidIntPointer(lits,2); 702c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 7033a40ed3dSBarry Smith PetscFunctionReturn(0); 704c96a6f78SLois Curfman McInnes } 705c96a6f78SLois Curfman McInnes 7064a2ae208SSatish Balay #undef __FUNCT__ 70794b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 70852baeb72SSatish Balay /*@ 70994b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 7109b94acceSBarry Smith 71194b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 712c7afd0dbSLois Curfman McInnes 7139b94acceSBarry Smith Input Parameter: 7149b94acceSBarry Smith . snes - the SNES context 7159b94acceSBarry Smith 7169b94acceSBarry Smith Output Parameter: 71794b7f48cSBarry Smith . ksp - the KSP context 7189b94acceSBarry Smith 7199b94acceSBarry Smith Notes: 72094b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 7219b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 7222999313aSBarry Smith PC contexts as well. 7239b94acceSBarry Smith 72436851e7fSLois Curfman McInnes Level: beginner 72536851e7fSLois Curfman McInnes 72694b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7279b94acceSBarry Smith 7282999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7299b94acceSBarry Smith @*/ 73063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp) 7319b94acceSBarry Smith { 7321cee3971SBarry Smith PetscErrorCode ierr; 7331cee3971SBarry Smith 7343a40ed3dSBarry Smith PetscFunctionBegin; 7354482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7364482741eSBarry Smith PetscValidPointer(ksp,2); 7371cee3971SBarry Smith 7381cee3971SBarry Smith if (!snes->ksp) { 7391cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 7401cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 7411cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 7421cee3971SBarry Smith } 74394b7f48cSBarry Smith *ksp = snes->ksp; 7443a40ed3dSBarry Smith PetscFunctionReturn(0); 7459b94acceSBarry Smith } 74682bf6240SBarry Smith 7474a2ae208SSatish Balay #undef __FUNCT__ 7482999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 7492999313aSBarry Smith /*@ 7502999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 7512999313aSBarry Smith 7522999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 7532999313aSBarry Smith 7542999313aSBarry Smith Input Parameters: 7552999313aSBarry Smith + snes - the SNES context 7562999313aSBarry Smith - ksp - the KSP context 7572999313aSBarry Smith 7582999313aSBarry Smith Notes: 7592999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 7602999313aSBarry Smith so this routine is rarely needed. 7612999313aSBarry Smith 7622999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 7632999313aSBarry Smith decreased by one. 7642999313aSBarry Smith 7652999313aSBarry Smith Level: developer 7662999313aSBarry Smith 7672999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7682999313aSBarry Smith 7692999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7702999313aSBarry Smith @*/ 7712999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp) 7722999313aSBarry Smith { 7732999313aSBarry Smith PetscErrorCode ierr; 7742999313aSBarry Smith 7752999313aSBarry Smith PetscFunctionBegin; 7762999313aSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7772999313aSBarry Smith PetscValidHeaderSpecific(ksp,KSP_COOKIE,2); 7782999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 7797dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 780906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 7812999313aSBarry Smith snes->ksp = ksp; 7822999313aSBarry Smith PetscFunctionReturn(0); 7832999313aSBarry Smith } 7842999313aSBarry Smith 7857adad957SLisandro Dalcin #if 0 7862999313aSBarry Smith #undef __FUNCT__ 7874a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 7886849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 789e24b481bSBarry Smith { 790e24b481bSBarry Smith PetscFunctionBegin; 791e24b481bSBarry Smith PetscFunctionReturn(0); 792e24b481bSBarry Smith } 7937adad957SLisandro Dalcin #endif 794e24b481bSBarry Smith 7959b94acceSBarry Smith /* -----------------------------------------------------------*/ 7964a2ae208SSatish Balay #undef __FUNCT__ 7974a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 79852baeb72SSatish Balay /*@ 7999b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 8009b94acceSBarry Smith 801c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 802c7afd0dbSLois Curfman McInnes 803c7afd0dbSLois Curfman McInnes Input Parameters: 804906ed7ccSBarry Smith . comm - MPI communicator 8059b94acceSBarry Smith 8069b94acceSBarry Smith Output Parameter: 8079b94acceSBarry Smith . outsnes - the new SNES context 8089b94acceSBarry Smith 809c7afd0dbSLois Curfman McInnes Options Database Keys: 810c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 811c7afd0dbSLois Curfman McInnes and no preconditioning matrix 812c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 813c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 814c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 815c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 816c1f60f51SBarry Smith 81736851e7fSLois Curfman McInnes Level: beginner 81836851e7fSLois Curfman McInnes 8199b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 8209b94acceSBarry Smith 821a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 822a8054027SBarry Smith 8239b94acceSBarry Smith @*/ 82463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes) 8259b94acceSBarry Smith { 826dfbe8321SBarry Smith PetscErrorCode ierr; 8279b94acceSBarry Smith SNES snes; 828fa9f3622SBarry Smith SNESKSPEW *kctx; 82937fcc0dbSBarry Smith 8303a40ed3dSBarry Smith PetscFunctionBegin; 831ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 8328ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 8338ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 8348ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 8358ba1e511SMatthew Knepley #endif 8368ba1e511SMatthew Knepley 837e7788613SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 8387adad957SLisandro Dalcin 83985385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 8409b94acceSBarry Smith snes->max_its = 50; 8419750a799SBarry Smith snes->max_funcs = 10000; 8429b94acceSBarry Smith snes->norm = 0.0; 843b4874afaSBarry Smith snes->rtol = 1.e-8; 844b4874afaSBarry Smith snes->ttol = 0.0; 84570441072SBarry Smith snes->abstol = 1.e-50; 8469b94acceSBarry Smith snes->xtol = 1.e-8; 8474b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 8489b94acceSBarry Smith snes->nfuncs = 0; 84950ffb88aSMatthew Knepley snes->numFailures = 0; 85050ffb88aSMatthew Knepley snes->maxFailures = 1; 8517a00f4a9SLois Curfman McInnes snes->linear_its = 0; 852e35cf81dSBarry Smith snes->lagjacobian = 1; 853a8054027SBarry Smith snes->lagpreconditioner = 1; 854639f9d9dSBarry Smith snes->numbermonitors = 0; 8559b94acceSBarry Smith snes->data = 0; 8564dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 857186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 8586f24a144SLois Curfman McInnes snes->vwork = 0; 8596f24a144SLois Curfman McInnes snes->nwork = 0; 860758f92a0SBarry Smith snes->conv_hist_len = 0; 861758f92a0SBarry Smith snes->conv_hist_max = 0; 862758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 863758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 864758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 865184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 8669b94acceSBarry Smith 8673d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 8683d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 8693d4c4710SBarry Smith 8709b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 87138f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 8729b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 8739b94acceSBarry Smith kctx->version = 2; 8749b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 8759b94acceSBarry Smith this was too large for some test cases */ 8769b94acceSBarry Smith kctx->rtol_last = 0; 8779b94acceSBarry Smith kctx->rtol_max = .9; 8789b94acceSBarry Smith kctx->gamma = 1.0; 87971f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 88071f87433Sdalcinl kctx->alpha2 = kctx->alpha; 8819b94acceSBarry Smith kctx->threshold = .1; 8829b94acceSBarry Smith kctx->lresid_last = 0; 8839b94acceSBarry Smith kctx->norm_last = 0; 8849b94acceSBarry Smith 8859b94acceSBarry Smith *outsnes = snes; 88600036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 8873a40ed3dSBarry Smith PetscFunctionReturn(0); 8889b94acceSBarry Smith } 8899b94acceSBarry Smith 8904a2ae208SSatish Balay #undef __FUNCT__ 8914a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 8929b94acceSBarry Smith /*@C 8939b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 8949b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 8959b94acceSBarry Smith equations. 8969b94acceSBarry Smith 897fee21e36SBarry Smith Collective on SNES 898fee21e36SBarry Smith 899c7afd0dbSLois Curfman McInnes Input Parameters: 900c7afd0dbSLois Curfman McInnes + snes - the SNES context 901c7afd0dbSLois Curfman McInnes . r - vector to store function value 902de044059SHong Zhang . func - function evaluation routine 903c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 904c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 9059b94acceSBarry Smith 906c7afd0dbSLois Curfman McInnes Calling sequence of func: 9078d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 908c7afd0dbSLois Curfman McInnes 909313e4042SLois Curfman McInnes . f - function vector 910c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 9119b94acceSBarry Smith 9129b94acceSBarry Smith Notes: 9139b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 9149b94acceSBarry Smith $ f'(x) x = -f(x), 915c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 9169b94acceSBarry Smith 91736851e7fSLois Curfman McInnes Level: beginner 91836851e7fSLois Curfman McInnes 9199b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9209b94acceSBarry Smith 921a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 9229b94acceSBarry Smith @*/ 92363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 9249b94acceSBarry Smith { 92585385478SLisandro Dalcin PetscErrorCode ierr; 9263a40ed3dSBarry Smith PetscFunctionBegin; 9274482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9284482741eSBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE,2); 929c9780b6fSBarry Smith PetscCheckSameComm(snes,1,r,2); 93085385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 93185385478SLisandro Dalcin if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); } 932e7788613SBarry Smith snes->ops->computefunction = func; 93385385478SLisandro Dalcin snes->vec_func = r; 9349b94acceSBarry Smith snes->funP = ctx; 9353a40ed3dSBarry Smith PetscFunctionReturn(0); 9369b94acceSBarry Smith } 9379b94acceSBarry Smith 9383ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 9393ab0aad5SBarry Smith #undef __FUNCT__ 9401096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 9411096aae1SMatthew Knepley /*@C 9421096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 9431096aae1SMatthew Knepley it assumes a zero right hand side. 9441096aae1SMatthew Knepley 9451096aae1SMatthew Knepley Collective on SNES 9461096aae1SMatthew Knepley 9471096aae1SMatthew Knepley Input Parameter: 9481096aae1SMatthew Knepley . snes - the SNES context 9491096aae1SMatthew Knepley 9501096aae1SMatthew Knepley Output Parameter: 951bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 9521096aae1SMatthew Knepley 9531096aae1SMatthew Knepley Level: intermediate 9541096aae1SMatthew Knepley 9551096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 9561096aae1SMatthew Knepley 95785385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 9581096aae1SMatthew Knepley @*/ 9591096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs) 9601096aae1SMatthew Knepley { 9611096aae1SMatthew Knepley PetscFunctionBegin; 9621096aae1SMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9631096aae1SMatthew Knepley PetscValidPointer(rhs,2); 96485385478SLisandro Dalcin *rhs = snes->vec_rhs; 9651096aae1SMatthew Knepley PetscFunctionReturn(0); 9661096aae1SMatthew Knepley } 9671096aae1SMatthew Knepley 9681096aae1SMatthew Knepley #undef __FUNCT__ 9694a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 9709b94acceSBarry Smith /*@ 97136851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 9729b94acceSBarry Smith SNESSetFunction(). 9739b94acceSBarry Smith 974c7afd0dbSLois Curfman McInnes Collective on SNES 975c7afd0dbSLois Curfman McInnes 9769b94acceSBarry Smith Input Parameters: 977c7afd0dbSLois Curfman McInnes + snes - the SNES context 978c7afd0dbSLois Curfman McInnes - x - input vector 9799b94acceSBarry Smith 9809b94acceSBarry Smith Output Parameter: 9813638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 9829b94acceSBarry Smith 9831bffabb2SLois Curfman McInnes Notes: 98436851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 98536851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 98636851e7fSLois Curfman McInnes themselves. 98736851e7fSLois Curfman McInnes 98836851e7fSLois Curfman McInnes Level: developer 98936851e7fSLois Curfman McInnes 9909b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 9919b94acceSBarry Smith 992a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 9939b94acceSBarry Smith @*/ 99463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y) 9959b94acceSBarry Smith { 996dfbe8321SBarry Smith PetscErrorCode ierr; 9979b94acceSBarry Smith 9983a40ed3dSBarry Smith PetscFunctionBegin; 9994482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10004482741eSBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE,2); 10014482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1002c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1003c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 1004184914b5SBarry Smith 1005d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1006e7788613SBarry Smith if (snes->ops->computefunction) { 1007d64ed03dSBarry Smith PetscStackPush("SNES user function"); 1008e9a2bbcdSBarry Smith CHKMEMQ; 1009e7788613SBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP); 1010e9a2bbcdSBarry Smith CHKMEMQ; 1011d64ed03dSBarry Smith PetscStackPop; 1012d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 101319717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr); 101419717074SBarry Smith } 1015d5e45103SBarry Smith CHKERRQ(ierr); 101685385478SLisandro Dalcin } else if (snes->vec_rhs) { 10171096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 10181096aae1SMatthew Knepley } else { 10191096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 10201096aae1SMatthew Knepley } 102185385478SLisandro Dalcin if (snes->vec_rhs) { 102285385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 10233ab0aad5SBarry Smith } 1024ae3c334cSLois Curfman McInnes snes->nfuncs++; 1025d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 10263a40ed3dSBarry Smith PetscFunctionReturn(0); 10279b94acceSBarry Smith } 10289b94acceSBarry Smith 10294a2ae208SSatish Balay #undef __FUNCT__ 10304a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 103162fef451SLois Curfman McInnes /*@ 103262fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 103362fef451SLois Curfman McInnes set with SNESSetJacobian(). 103462fef451SLois Curfman McInnes 1035c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1036c7afd0dbSLois Curfman McInnes 103762fef451SLois Curfman McInnes Input Parameters: 1038c7afd0dbSLois Curfman McInnes + snes - the SNES context 1039c7afd0dbSLois Curfman McInnes - x - input vector 104062fef451SLois Curfman McInnes 104162fef451SLois Curfman McInnes Output Parameters: 1042c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 104362fef451SLois Curfman McInnes . B - optional preconditioning matrix 10442b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1045fee21e36SBarry Smith 1046e35cf81dSBarry Smith Options Database Keys: 1047e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1048e35cf81dSBarry Smith - -snes_lag_jacobian <lag> 1049e35cf81dSBarry Smith 105062fef451SLois Curfman McInnes Notes: 105162fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 105262fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 105362fef451SLois Curfman McInnes 105494b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1055dc5a77f8SLois Curfman McInnes flag parameter. 105662fef451SLois Curfman McInnes 105736851e7fSLois Curfman McInnes Level: developer 105836851e7fSLois Curfman McInnes 105962fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 106062fef451SLois Curfman McInnes 1061e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 106262fef451SLois Curfman McInnes @*/ 106363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10649b94acceSBarry Smith { 1065dfbe8321SBarry Smith PetscErrorCode ierr; 1066ebd3b9afSBarry Smith PetscTruth flag; 10673a40ed3dSBarry Smith 10683a40ed3dSBarry Smith PetscFunctionBegin; 10694482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10704482741eSBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE,2); 10714482741eSBarry Smith PetscValidPointer(flg,5); 1072c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1073e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1074ebd3b9afSBarry Smith 1075ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1076ebd3b9afSBarry Smith 1077fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1078fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1079fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1080fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1081e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1082e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1083ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1084ebd3b9afSBarry Smith if (flag) { 1085ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1086ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1087ebd3b9afSBarry Smith } 1088e35cf81dSBarry Smith PetscFunctionReturn(0); 1089e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1090e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1091e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1092ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1093ebd3b9afSBarry Smith if (flag) { 1094ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1095ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1096ebd3b9afSBarry Smith } 1097e35cf81dSBarry Smith PetscFunctionReturn(0); 1098e35cf81dSBarry Smith } 1099e35cf81dSBarry Smith 1100c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1101e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1102d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1103dc67937bSBarry Smith CHKMEMQ; 1104e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1105dc67937bSBarry Smith CHKMEMQ; 1106d64ed03dSBarry Smith PetscStackPop; 1107d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1108a8054027SBarry Smith 1109*3b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 1110*3b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 1111*3b4f5425SBarry Smith snes->lagpreconditioner = -1; 1112*3b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1113a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1114a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1115a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1116a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1117a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1118a8054027SBarry Smith } 1119a8054027SBarry Smith 11206d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 11216ce558aeSBarry Smith /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3); 11226ce558aeSBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE,4); */ 11233a40ed3dSBarry Smith PetscFunctionReturn(0); 11249b94acceSBarry Smith } 11259b94acceSBarry Smith 11264a2ae208SSatish Balay #undef __FUNCT__ 11274a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 11289b94acceSBarry Smith /*@C 11299b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1130044dda88SLois Curfman McInnes location to store the matrix. 11319b94acceSBarry Smith 1132c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1133c7afd0dbSLois Curfman McInnes 11349b94acceSBarry Smith Input Parameters: 1135c7afd0dbSLois Curfman McInnes + snes - the SNES context 11369b94acceSBarry Smith . A - Jacobian matrix 11379b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11389b94acceSBarry Smith . func - Jacobian evaluation routine 1139c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11402cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11419b94acceSBarry Smith 11429b94acceSBarry Smith Calling sequence of func: 11438d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11449b94acceSBarry Smith 1145c7afd0dbSLois Curfman McInnes + x - input vector 11469b94acceSBarry Smith . A - Jacobian matrix 11479b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1148ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 11492b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1150c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11519b94acceSBarry Smith 11529b94acceSBarry Smith Notes: 115394b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 11542cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1155ac21db08SLois Curfman McInnes 1156ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11579b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11589b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11599b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11609b94acceSBarry Smith throughout the global iterations. 11619b94acceSBarry Smith 116216913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 116316913363SBarry Smith each matrix. 116416913363SBarry Smith 116536851e7fSLois Curfman McInnes Level: beginner 116636851e7fSLois Curfman McInnes 11679b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11689b94acceSBarry Smith 11693ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 11709b94acceSBarry Smith @*/ 117163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 11729b94acceSBarry Smith { 1173dfbe8321SBarry Smith PetscErrorCode ierr; 11743a7fca6bSBarry Smith 11753a40ed3dSBarry Smith PetscFunctionBegin; 11764482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11774482741eSBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2); 11784482741eSBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3); 1179c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 1180c9780b6fSBarry Smith if (B) PetscCheckSameComm(snes,1,B,2); 1181e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 11823a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 11833a7fca6bSBarry Smith if (A) { 11847dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 11853a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 11869b94acceSBarry Smith snes->jacobian = A; 11873a7fca6bSBarry Smith } 11883a7fca6bSBarry Smith if (B) { 11897dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 11903a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 11919b94acceSBarry Smith snes->jacobian_pre = B; 11923a7fca6bSBarry Smith } 11933a40ed3dSBarry Smith PetscFunctionReturn(0); 11949b94acceSBarry Smith } 119562fef451SLois Curfman McInnes 11964a2ae208SSatish Balay #undef __FUNCT__ 11974a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1198c2aafc4cSSatish Balay /*@C 1199b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1200b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1201b4fd4287SBarry Smith 1202c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1203c7afd0dbSLois Curfman McInnes 1204b4fd4287SBarry Smith Input Parameter: 1205b4fd4287SBarry Smith . snes - the nonlinear solver context 1206b4fd4287SBarry Smith 1207b4fd4287SBarry Smith Output Parameters: 1208c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1209b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 121070e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 121170e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1212fee21e36SBarry Smith 121336851e7fSLois Curfman McInnes Level: advanced 121436851e7fSLois Curfman McInnes 1215b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1216b4fd4287SBarry Smith @*/ 121770e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1218b4fd4287SBarry Smith { 12193a40ed3dSBarry Smith PetscFunctionBegin; 12204482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1221b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1222b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1223e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 122470e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 12253a40ed3dSBarry Smith PetscFunctionReturn(0); 1226b4fd4287SBarry Smith } 1227b4fd4287SBarry Smith 12289b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 122963dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 12309b94acceSBarry Smith 12314a2ae208SSatish Balay #undef __FUNCT__ 12324a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 12339b94acceSBarry Smith /*@ 12349b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1235272ac6f2SLois Curfman McInnes of a nonlinear solver. 12369b94acceSBarry Smith 1237fee21e36SBarry Smith Collective on SNES 1238fee21e36SBarry Smith 1239c7afd0dbSLois Curfman McInnes Input Parameters: 124070e92668SMatthew Knepley . snes - the SNES context 1241c7afd0dbSLois Curfman McInnes 1242272ac6f2SLois Curfman McInnes Notes: 1243272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1244272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1245272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1246272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1247272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1248272ac6f2SLois Curfman McInnes 124936851e7fSLois Curfman McInnes Level: advanced 125036851e7fSLois Curfman McInnes 12519b94acceSBarry Smith .keywords: SNES, nonlinear, setup 12529b94acceSBarry Smith 12539b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 12549b94acceSBarry Smith @*/ 125570e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes) 12569b94acceSBarry Smith { 1257dfbe8321SBarry Smith PetscErrorCode ierr; 125871f87433Sdalcinl PetscTruth flg; 12593a40ed3dSBarry Smith 12603a40ed3dSBarry Smith PetscFunctionBegin; 12614482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 12624dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 12639b94acceSBarry Smith 12647adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 126585385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 126685385478SLisandro Dalcin } 126785385478SLisandro Dalcin 12687adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1269c1f60f51SBarry Smith /* 1270c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1271dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1272c1f60f51SBarry Smith */ 1273c1f60f51SBarry Smith if (flg) { 1274c1f60f51SBarry Smith Mat J; 1275fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12763ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 1277ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12783a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 12793a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1280c1f60f51SBarry Smith } 128145fc7adcSBarry Smith 128203c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT) 12837adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr); 128445fc7adcSBarry Smith if (flg) { 128545fc7adcSBarry Smith Mat J; 128645fc7adcSBarry Smith ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr); 128775396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr); 128845fc7adcSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 128945fc7adcSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 129045fc7adcSBarry Smith } 129132a4b47aSMatthew Knepley #endif 129245fc7adcSBarry Smith 12937adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1294c1f60f51SBarry Smith /* 1295dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1296c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1297c1f60f51SBarry Smith */ 129831615d04SLois Curfman McInnes if (flg) { 1299272ac6f2SLois Curfman McInnes Mat J; 1300b5d62d44SBarry Smith KSP ksp; 130194b7f48cSBarry Smith PC pc; 130275396ef9SLisandro Dalcin /* create and set matrix-free operator */ 1303fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 13043ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 130575396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 13063ec795f1SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 13073a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1308f3ef73ceSBarry Smith /* force no preconditioner */ 130994b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 1310b5d62d44SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 1311a9815358SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 1312a9815358SBarry Smith if (!flg) { 131375396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 1314f3ef73ceSBarry Smith ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 1315272ac6f2SLois Curfman McInnes } 1316a9815358SBarry Smith } 1317f3ef73ceSBarry Smith 131885385478SLisandro Dalcin if (!snes->vec_func && !snes->vec_rhs) { 13191096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 13201096aae1SMatthew Knepley } 132185385478SLisandro Dalcin if (!snes->ops->computefunction && !snes->vec_rhs) { 13221096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 13231096aae1SMatthew Knepley } 132475396ef9SLisandro Dalcin if (!snes->jacobian) { 132575396ef9SLisandro Dalcin SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 132675396ef9SLisandro Dalcin } 1327a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 132829bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1329a8c6a408SBarry Smith } 1330a703fe33SLois Curfman McInnes 1331410397dcSLisandro Dalcin if (snes->ops->setup) { 1332410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1333410397dcSLisandro Dalcin } 13347aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 13353a40ed3dSBarry Smith PetscFunctionReturn(0); 13369b94acceSBarry Smith } 13379b94acceSBarry Smith 13384a2ae208SSatish Balay #undef __FUNCT__ 13394a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 134052baeb72SSatish Balay /*@ 13419b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 13429b94acceSBarry Smith with SNESCreate(). 13439b94acceSBarry Smith 1344c7afd0dbSLois Curfman McInnes Collective on SNES 1345c7afd0dbSLois Curfman McInnes 13469b94acceSBarry Smith Input Parameter: 13479b94acceSBarry Smith . snes - the SNES context 13489b94acceSBarry Smith 134936851e7fSLois Curfman McInnes Level: beginner 135036851e7fSLois Curfman McInnes 13519b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 13529b94acceSBarry Smith 135363a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 13549b94acceSBarry Smith @*/ 135563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes) 13569b94acceSBarry Smith { 13576849ba73SBarry Smith PetscErrorCode ierr; 13583a40ed3dSBarry Smith 13593a40ed3dSBarry Smith PetscFunctionBegin; 13604482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 13617adad957SLisandro Dalcin if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0); 1362d4bb536fSBarry Smith 1363be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 13640f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1365e7788613SBarry Smith if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);} 136685385478SLisandro Dalcin 136785385478SLisandro Dalcin if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);} 136885385478SLisandro Dalcin if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);} 136985385478SLisandro Dalcin if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);} 13703a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 13713a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 13721cee3971SBarry Smith if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);} 137385385478SLisandro Dalcin ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr); 1374522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1375a6570f20SBarry Smith ierr = SNESMonitorCancel(snes);CHKERRQ(ierr); 13767f7931b9SBarry Smith if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);} 1377a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 13783a40ed3dSBarry Smith PetscFunctionReturn(0); 13799b94acceSBarry Smith } 13809b94acceSBarry Smith 13819b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 13829b94acceSBarry Smith 13834a2ae208SSatish Balay #undef __FUNCT__ 1384a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1385a8054027SBarry Smith /*@ 1386a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1387a8054027SBarry Smith 1388a8054027SBarry Smith Collective on SNES 1389a8054027SBarry Smith 1390a8054027SBarry Smith Input Parameters: 1391a8054027SBarry Smith + snes - the SNES context 1392a8054027SBarry 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 1393*3b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1394a8054027SBarry Smith 1395a8054027SBarry Smith Options Database Keys: 1396a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1397a8054027SBarry Smith 1398a8054027SBarry Smith Notes: 1399a8054027SBarry Smith The default is 1 1400a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1401a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1402a8054027SBarry Smith 1403a8054027SBarry Smith Level: intermediate 1404a8054027SBarry Smith 1405a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1406a8054027SBarry Smith 1407e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1408a8054027SBarry Smith 1409a8054027SBarry Smith @*/ 1410a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1411a8054027SBarry Smith { 1412a8054027SBarry Smith PetscFunctionBegin; 1413a8054027SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1414*3b4f5425SBarry Smith if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1415a8054027SBarry Smith if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1416a8054027SBarry Smith snes->lagpreconditioner = lag; 1417a8054027SBarry Smith PetscFunctionReturn(0); 1418a8054027SBarry Smith } 1419a8054027SBarry Smith 1420a8054027SBarry Smith #undef __FUNCT__ 1421a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1422a8054027SBarry Smith /*@ 1423a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1424a8054027SBarry Smith 1425a8054027SBarry Smith Collective on SNES 1426a8054027SBarry Smith 1427a8054027SBarry Smith Input Parameter: 1428a8054027SBarry Smith . snes - the SNES context 1429a8054027SBarry Smith 1430a8054027SBarry Smith Output Parameter: 1431a8054027SBarry 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 1432*3b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1433a8054027SBarry Smith 1434a8054027SBarry Smith Options Database Keys: 1435a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1436a8054027SBarry Smith 1437a8054027SBarry Smith Notes: 1438a8054027SBarry Smith The default is 1 1439a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1440a8054027SBarry Smith 1441a8054027SBarry Smith Level: intermediate 1442a8054027SBarry Smith 1443a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1444a8054027SBarry Smith 1445a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1446a8054027SBarry Smith 1447a8054027SBarry Smith @*/ 1448a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1449a8054027SBarry Smith { 1450a8054027SBarry Smith PetscFunctionBegin; 1451a8054027SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1452a8054027SBarry Smith *lag = snes->lagpreconditioner; 1453a8054027SBarry Smith PetscFunctionReturn(0); 1454a8054027SBarry Smith } 1455a8054027SBarry Smith 1456a8054027SBarry Smith #undef __FUNCT__ 1457e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 1458e35cf81dSBarry Smith /*@ 1459e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 1460e35cf81dSBarry Smith often the preconditioner is rebuilt. 1461e35cf81dSBarry Smith 1462e35cf81dSBarry Smith Collective on SNES 1463e35cf81dSBarry Smith 1464e35cf81dSBarry Smith Input Parameters: 1465e35cf81dSBarry Smith + snes - the SNES context 1466e35cf81dSBarry 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 1467fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 1468e35cf81dSBarry Smith 1469e35cf81dSBarry Smith Options Database Keys: 1470e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1471e35cf81dSBarry Smith 1472e35cf81dSBarry Smith Notes: 1473e35cf81dSBarry Smith The default is 1 1474e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1475fe3ffe1eSBarry 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 1476fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 1477e35cf81dSBarry Smith 1478e35cf81dSBarry Smith Level: intermediate 1479e35cf81dSBarry Smith 1480e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1481e35cf81dSBarry Smith 1482e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 1483e35cf81dSBarry Smith 1484e35cf81dSBarry Smith @*/ 1485e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag) 1486e35cf81dSBarry Smith { 1487e35cf81dSBarry Smith PetscFunctionBegin; 1488e35cf81dSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1489fe3ffe1eSBarry Smith if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1490e35cf81dSBarry Smith if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1491e35cf81dSBarry Smith snes->lagjacobian = lag; 1492e35cf81dSBarry Smith PetscFunctionReturn(0); 1493e35cf81dSBarry Smith } 1494e35cf81dSBarry Smith 1495e35cf81dSBarry Smith #undef __FUNCT__ 1496e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 1497e35cf81dSBarry Smith /*@ 1498e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 1499e35cf81dSBarry Smith 1500e35cf81dSBarry Smith Collective on SNES 1501e35cf81dSBarry Smith 1502e35cf81dSBarry Smith Input Parameter: 1503e35cf81dSBarry Smith . snes - the SNES context 1504e35cf81dSBarry Smith 1505e35cf81dSBarry Smith Output Parameter: 1506e35cf81dSBarry 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 1507e35cf81dSBarry Smith the Jacobian is built etc. 1508e35cf81dSBarry Smith 1509e35cf81dSBarry Smith Options Database Keys: 1510e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1511e35cf81dSBarry Smith 1512e35cf81dSBarry Smith Notes: 1513e35cf81dSBarry Smith The default is 1 1514e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1515e35cf81dSBarry Smith 1516e35cf81dSBarry Smith Level: intermediate 1517e35cf81dSBarry Smith 1518e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1519e35cf81dSBarry Smith 1520e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 1521e35cf81dSBarry Smith 1522e35cf81dSBarry Smith @*/ 1523e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag) 1524e35cf81dSBarry Smith { 1525e35cf81dSBarry Smith PetscFunctionBegin; 1526e35cf81dSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1527e35cf81dSBarry Smith *lag = snes->lagjacobian; 1528e35cf81dSBarry Smith PetscFunctionReturn(0); 1529e35cf81dSBarry Smith } 1530e35cf81dSBarry Smith 1531e35cf81dSBarry Smith #undef __FUNCT__ 15324a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 15339b94acceSBarry Smith /*@ 1534d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 15359b94acceSBarry Smith 1536c7afd0dbSLois Curfman McInnes Collective on SNES 1537c7afd0dbSLois Curfman McInnes 15389b94acceSBarry Smith Input Parameters: 1539c7afd0dbSLois Curfman McInnes + snes - the SNES context 154070441072SBarry Smith . abstol - absolute convergence tolerance 154133174efeSLois Curfman McInnes . rtol - relative convergence tolerance 154233174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 154333174efeSLois Curfman McInnes of the change in the solution between steps 154433174efeSLois Curfman McInnes . maxit - maximum number of iterations 1545c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1546fee21e36SBarry Smith 154733174efeSLois Curfman McInnes Options Database Keys: 154870441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1549c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1550c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1551c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1552c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15539b94acceSBarry Smith 1554d7a720efSLois Curfman McInnes Notes: 15559b94acceSBarry Smith The default maximum number of iterations is 50. 15569b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15579b94acceSBarry Smith 155836851e7fSLois Curfman McInnes Level: intermediate 155936851e7fSLois Curfman McInnes 156033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15619b94acceSBarry Smith 15622492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 15639b94acceSBarry Smith @*/ 156463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 15659b94acceSBarry Smith { 15663a40ed3dSBarry Smith PetscFunctionBegin; 15674482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 156870441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1569d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1570d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1571d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1572d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15733a40ed3dSBarry Smith PetscFunctionReturn(0); 15749b94acceSBarry Smith } 15759b94acceSBarry Smith 15764a2ae208SSatish Balay #undef __FUNCT__ 15774a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 15789b94acceSBarry Smith /*@ 157933174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 158033174efeSLois Curfman McInnes 1581c7afd0dbSLois Curfman McInnes Not Collective 1582c7afd0dbSLois Curfman McInnes 158333174efeSLois Curfman McInnes Input Parameters: 1584c7afd0dbSLois Curfman McInnes + snes - the SNES context 158585385478SLisandro Dalcin . atol - absolute convergence tolerance 158633174efeSLois Curfman McInnes . rtol - relative convergence tolerance 158733174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 158833174efeSLois Curfman McInnes of the change in the solution between steps 158933174efeSLois Curfman McInnes . maxit - maximum number of iterations 1590c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1591fee21e36SBarry Smith 159233174efeSLois Curfman McInnes Notes: 159333174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 159433174efeSLois Curfman McInnes 159536851e7fSLois Curfman McInnes Level: intermediate 159636851e7fSLois Curfman McInnes 159733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 159833174efeSLois Curfman McInnes 159933174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 160033174efeSLois Curfman McInnes @*/ 160185385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 160233174efeSLois Curfman McInnes { 16033a40ed3dSBarry Smith PetscFunctionBegin; 16044482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 160585385478SLisandro Dalcin if (atol) *atol = snes->abstol; 160633174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 160733174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 160833174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 160933174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 16103a40ed3dSBarry Smith PetscFunctionReturn(0); 161133174efeSLois Curfman McInnes } 161233174efeSLois Curfman McInnes 16134a2ae208SSatish Balay #undef __FUNCT__ 16144a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 161533174efeSLois Curfman McInnes /*@ 16169b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 16179b94acceSBarry Smith 1618fee21e36SBarry Smith Collective on SNES 1619fee21e36SBarry Smith 1620c7afd0dbSLois Curfman McInnes Input Parameters: 1621c7afd0dbSLois Curfman McInnes + snes - the SNES context 1622c7afd0dbSLois Curfman McInnes - tol - tolerance 1623c7afd0dbSLois Curfman McInnes 16249b94acceSBarry Smith Options Database Key: 1625c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 16269b94acceSBarry Smith 162736851e7fSLois Curfman McInnes Level: intermediate 162836851e7fSLois Curfman McInnes 16299b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 16309b94acceSBarry Smith 16312492ecdbSBarry Smith .seealso: SNESSetTolerances() 16329b94acceSBarry Smith @*/ 163363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 16349b94acceSBarry Smith { 16353a40ed3dSBarry Smith PetscFunctionBegin; 16364482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16379b94acceSBarry Smith snes->deltatol = tol; 16383a40ed3dSBarry Smith PetscFunctionReturn(0); 16399b94acceSBarry Smith } 16409b94acceSBarry Smith 1641df9fa365SBarry Smith /* 1642df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1643df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1644df9fa365SBarry Smith macros instead of functions 1645df9fa365SBarry Smith */ 16464a2ae208SSatish Balay #undef __FUNCT__ 1647a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 1648a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1649ce1608b8SBarry Smith { 1650dfbe8321SBarry Smith PetscErrorCode ierr; 1651ce1608b8SBarry Smith 1652ce1608b8SBarry Smith PetscFunctionBegin; 16534482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1654a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1655ce1608b8SBarry Smith PetscFunctionReturn(0); 1656ce1608b8SBarry Smith } 1657ce1608b8SBarry Smith 16584a2ae208SSatish Balay #undef __FUNCT__ 1659a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 1660a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1661df9fa365SBarry Smith { 1662dfbe8321SBarry Smith PetscErrorCode ierr; 1663df9fa365SBarry Smith 1664df9fa365SBarry Smith PetscFunctionBegin; 1665a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1666df9fa365SBarry Smith PetscFunctionReturn(0); 1667df9fa365SBarry Smith } 1668df9fa365SBarry Smith 16694a2ae208SSatish Balay #undef __FUNCT__ 1670a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 1671a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw) 1672df9fa365SBarry Smith { 1673dfbe8321SBarry Smith PetscErrorCode ierr; 1674df9fa365SBarry Smith 1675df9fa365SBarry Smith PetscFunctionBegin; 1676a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1677df9fa365SBarry Smith PetscFunctionReturn(0); 1678df9fa365SBarry Smith } 1679df9fa365SBarry Smith 16809b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 16819b94acceSBarry Smith 16824a2ae208SSatish Balay #undef __FUNCT__ 1683a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 16849b94acceSBarry Smith /*@C 1685a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 16869b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 16879b94acceSBarry Smith progress. 16889b94acceSBarry Smith 1689fee21e36SBarry Smith Collective on SNES 1690fee21e36SBarry Smith 1691c7afd0dbSLois Curfman McInnes Input Parameters: 1692c7afd0dbSLois Curfman McInnes + snes - the SNES context 1693c7afd0dbSLois Curfman McInnes . func - monitoring routine 1694b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1695e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 1696b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1697b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 16989b94acceSBarry Smith 1699c7afd0dbSLois Curfman McInnes Calling sequence of func: 1700a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 1701c7afd0dbSLois Curfman McInnes 1702c7afd0dbSLois Curfman McInnes + snes - the SNES context 1703c7afd0dbSLois Curfman McInnes . its - iteration number 1704c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 170540a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 17069b94acceSBarry Smith 17079665c990SLois Curfman McInnes Options Database Keys: 1708a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 1709a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 1710a6570f20SBarry Smith uses SNESMonitorLGCreate() 1711a6570f20SBarry Smith _ -snes_monitor_cancel - cancels all monitors that have 1712c7afd0dbSLois Curfman McInnes been hardwired into a code by 1713a6570f20SBarry Smith calls to SNESMonitorSet(), but 1714c7afd0dbSLois Curfman McInnes does not cancel those set via 1715c7afd0dbSLois Curfman McInnes the options database. 17169665c990SLois Curfman McInnes 1717639f9d9dSBarry Smith Notes: 17186bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 1719a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 17206bc08f3fSLois Curfman McInnes order in which they were set. 1721639f9d9dSBarry Smith 1722025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 1723025f1a04SBarry Smith 172436851e7fSLois Curfman McInnes Level: intermediate 172536851e7fSLois Curfman McInnes 17269b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 17279b94acceSBarry Smith 1728a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 17299b94acceSBarry Smith @*/ 1730b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 17319b94acceSBarry Smith { 1732b90d0a6eSBarry Smith PetscInt i; 1733b90d0a6eSBarry Smith 17343a40ed3dSBarry Smith PetscFunctionBegin; 17354482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1736639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 173729bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1738639f9d9dSBarry Smith } 1739b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 1740b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 1741b90d0a6eSBarry Smith 1742b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 1743b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 1744b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 1745b90d0a6eSBarry Smith PetscErrorCode ierr; 1746b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 1747b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 1748b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 1749b90d0a6eSBarry Smith ierr = (*monitordestroy)(mctx);CHKERRQ(ierr); 1750b90d0a6eSBarry Smith PetscFunctionReturn(0); 1751b90d0a6eSBarry Smith } 1752b90d0a6eSBarry Smith } 1753b90d0a6eSBarry Smith } 1754b90d0a6eSBarry Smith } 1755b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 1756b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1757639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 17583a40ed3dSBarry Smith PetscFunctionReturn(0); 17599b94acceSBarry Smith } 17609b94acceSBarry Smith 17614a2ae208SSatish Balay #undef __FUNCT__ 1762a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 17635cd90555SBarry Smith /*@C 1764a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 17655cd90555SBarry Smith 1766c7afd0dbSLois Curfman McInnes Collective on SNES 1767c7afd0dbSLois Curfman McInnes 17685cd90555SBarry Smith Input Parameters: 17695cd90555SBarry Smith . snes - the SNES context 17705cd90555SBarry Smith 17711a480d89SAdministrator Options Database Key: 1772a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 1773a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 1774c7afd0dbSLois Curfman McInnes set via the options database 17755cd90555SBarry Smith 17765cd90555SBarry Smith Notes: 17775cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 17785cd90555SBarry Smith 177936851e7fSLois Curfman McInnes Level: intermediate 178036851e7fSLois Curfman McInnes 17815cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 17825cd90555SBarry Smith 1783a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 17845cd90555SBarry Smith @*/ 1785a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes) 17865cd90555SBarry Smith { 1787d952e501SBarry Smith PetscErrorCode ierr; 1788d952e501SBarry Smith PetscInt i; 1789d952e501SBarry Smith 17905cd90555SBarry Smith PetscFunctionBegin; 17914482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1792d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1793d952e501SBarry Smith if (snes->monitordestroy[i]) { 1794d952e501SBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1795d952e501SBarry Smith } 1796d952e501SBarry Smith } 17975cd90555SBarry Smith snes->numbermonitors = 0; 17985cd90555SBarry Smith PetscFunctionReturn(0); 17995cd90555SBarry Smith } 18005cd90555SBarry Smith 18014a2ae208SSatish Balay #undef __FUNCT__ 18024a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 18039b94acceSBarry Smith /*@C 18049b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 18059b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 18069b94acceSBarry Smith 1807fee21e36SBarry Smith Collective on SNES 1808fee21e36SBarry Smith 1809c7afd0dbSLois Curfman McInnes Input Parameters: 1810c7afd0dbSLois Curfman McInnes + snes - the SNES context 1811c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 18127f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 18137f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 18149b94acceSBarry Smith 1815c7afd0dbSLois Curfman McInnes Calling sequence of func: 181606ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1817c7afd0dbSLois Curfman McInnes 1818c7afd0dbSLois Curfman McInnes + snes - the SNES context 181906ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 1820c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1821184914b5SBarry Smith . reason - reason for convergence/divergence 1822c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 18234b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 18244b27c08aSLois Curfman McInnes - f - 2-norm of function 18259b94acceSBarry Smith 182636851e7fSLois Curfman McInnes Level: advanced 182736851e7fSLois Curfman McInnes 18289b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 18299b94acceSBarry Smith 183085385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 18319b94acceSBarry Smith @*/ 18327f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 18339b94acceSBarry Smith { 18347f7931b9SBarry Smith PetscErrorCode ierr; 18357f7931b9SBarry Smith 18363a40ed3dSBarry Smith PetscFunctionBegin; 18374482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 183885385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 18397f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 18407f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 18417f7931b9SBarry Smith } 184285385478SLisandro Dalcin snes->ops->converged = func; 18437f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 184485385478SLisandro Dalcin snes->cnvP = cctx; 18453a40ed3dSBarry Smith PetscFunctionReturn(0); 18469b94acceSBarry Smith } 18479b94acceSBarry Smith 18484a2ae208SSatish Balay #undef __FUNCT__ 18494a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 185052baeb72SSatish Balay /*@ 1851184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1852184914b5SBarry Smith 1853184914b5SBarry Smith Not Collective 1854184914b5SBarry Smith 1855184914b5SBarry Smith Input Parameter: 1856184914b5SBarry Smith . snes - the SNES context 1857184914b5SBarry Smith 1858184914b5SBarry Smith Output Parameter: 18594d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 1860184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1861184914b5SBarry Smith 1862184914b5SBarry Smith Level: intermediate 1863184914b5SBarry Smith 1864184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1865184914b5SBarry Smith 1866184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1867184914b5SBarry Smith 186885385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 1869184914b5SBarry Smith @*/ 187063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1871184914b5SBarry Smith { 1872184914b5SBarry Smith PetscFunctionBegin; 18734482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 18744482741eSBarry Smith PetscValidPointer(reason,2); 1875184914b5SBarry Smith *reason = snes->reason; 1876184914b5SBarry Smith PetscFunctionReturn(0); 1877184914b5SBarry Smith } 1878184914b5SBarry Smith 18794a2ae208SSatish Balay #undef __FUNCT__ 18804a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 1881c9005455SLois Curfman McInnes /*@ 1882c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1883c9005455SLois Curfman McInnes 1884fee21e36SBarry Smith Collective on SNES 1885fee21e36SBarry Smith 1886c7afd0dbSLois Curfman McInnes Input Parameters: 1887c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1888c7afd0dbSLois Curfman McInnes . a - array to hold history 1889cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 1890758f92a0SBarry Smith . na - size of a and its 189164731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 1892758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1893c7afd0dbSLois Curfman McInnes 1894c9005455SLois Curfman McInnes Notes: 18954b27c08aSLois Curfman McInnes If set, this array will contain the function norms computed 1896c9005455SLois Curfman McInnes at each step. 1897c9005455SLois Curfman McInnes 1898c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1899c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1900c9005455SLois Curfman McInnes during the section of code that is being timed. 1901c9005455SLois Curfman McInnes 190236851e7fSLois Curfman McInnes Level: intermediate 190336851e7fSLois Curfman McInnes 1904c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1905758f92a0SBarry Smith 190608405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1907758f92a0SBarry Smith 1908c9005455SLois Curfman McInnes @*/ 1909a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset) 1910c9005455SLois Curfman McInnes { 19113a40ed3dSBarry Smith PetscFunctionBegin; 19124482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 19134482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 1914a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 1915c9005455SLois Curfman McInnes snes->conv_hist = a; 1916758f92a0SBarry Smith snes->conv_hist_its = its; 1917758f92a0SBarry Smith snes->conv_hist_max = na; 1918a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 1919758f92a0SBarry Smith snes->conv_hist_reset = reset; 1920758f92a0SBarry Smith PetscFunctionReturn(0); 1921758f92a0SBarry Smith } 1922758f92a0SBarry Smith 19234a2ae208SSatish Balay #undef __FUNCT__ 19244a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 19250c4c9dddSBarry Smith /*@C 1926758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1927758f92a0SBarry Smith 1928758f92a0SBarry Smith Collective on SNES 1929758f92a0SBarry Smith 1930758f92a0SBarry Smith Input Parameter: 1931758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1932758f92a0SBarry Smith 1933758f92a0SBarry Smith Output Parameters: 1934758f92a0SBarry Smith . a - array to hold history 1935758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1936758f92a0SBarry Smith negative if not converged) for each solve. 1937758f92a0SBarry Smith - na - size of a and its 1938758f92a0SBarry Smith 1939758f92a0SBarry Smith Notes: 1940758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1941758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1942758f92a0SBarry Smith 1943758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1944758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1945758f92a0SBarry Smith during the section of code that is being timed. 1946758f92a0SBarry Smith 1947758f92a0SBarry Smith Level: intermediate 1948758f92a0SBarry Smith 1949758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1950758f92a0SBarry Smith 1951758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1952758f92a0SBarry Smith 1953758f92a0SBarry Smith @*/ 195463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 1955758f92a0SBarry Smith { 1956758f92a0SBarry Smith PetscFunctionBegin; 19574482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1958758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1959758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1960758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 19613a40ed3dSBarry Smith PetscFunctionReturn(0); 1962c9005455SLois Curfman McInnes } 1963c9005455SLois Curfman McInnes 1964e74ef692SMatthew Knepley #undef __FUNCT__ 1965e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 1966ac226902SBarry Smith /*@C 196776b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 19687e4bb74cSBarry Smith at the beginning o every iteration of the nonlinear solve. Specifically 19697e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 197076b2cf59SMatthew Knepley 197176b2cf59SMatthew Knepley Collective on SNES 197276b2cf59SMatthew Knepley 197376b2cf59SMatthew Knepley Input Parameters: 197476b2cf59SMatthew Knepley . snes - The nonlinear solver context 197576b2cf59SMatthew Knepley . func - The function 197676b2cf59SMatthew Knepley 197776b2cf59SMatthew Knepley Calling sequence of func: 1978b5d30489SBarry Smith . func (SNES snes, PetscInt step); 197976b2cf59SMatthew Knepley 198076b2cf59SMatthew Knepley . step - The current step of the iteration 198176b2cf59SMatthew Knepley 198276b2cf59SMatthew Knepley Level: intermediate 198376b2cf59SMatthew Knepley 198476b2cf59SMatthew Knepley .keywords: SNES, update 1985b5d30489SBarry Smith 198685385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 198776b2cf59SMatthew Knepley @*/ 198863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 198976b2cf59SMatthew Knepley { 199076b2cf59SMatthew Knepley PetscFunctionBegin; 19914482741eSBarry Smith PetscValidHeaderSpecific(snes, SNES_COOKIE,1); 1992e7788613SBarry Smith snes->ops->update = func; 199376b2cf59SMatthew Knepley PetscFunctionReturn(0); 199476b2cf59SMatthew Knepley } 199576b2cf59SMatthew Knepley 1996e74ef692SMatthew Knepley #undef __FUNCT__ 1997e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 199876b2cf59SMatthew Knepley /*@ 199976b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 200076b2cf59SMatthew Knepley 200176b2cf59SMatthew Knepley Not collective 200276b2cf59SMatthew Knepley 200376b2cf59SMatthew Knepley Input Parameters: 200476b2cf59SMatthew Knepley . snes - The nonlinear solver context 200576b2cf59SMatthew Knepley . step - The current step of the iteration 200676b2cf59SMatthew Knepley 2007205452f4SMatthew Knepley Level: intermediate 2008205452f4SMatthew Knepley 200976b2cf59SMatthew Knepley .keywords: SNES, update 2010a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 201176b2cf59SMatthew Knepley @*/ 201263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step) 201376b2cf59SMatthew Knepley { 201476b2cf59SMatthew Knepley PetscFunctionBegin; 201576b2cf59SMatthew Knepley PetscFunctionReturn(0); 201676b2cf59SMatthew Knepley } 201776b2cf59SMatthew Knepley 20184a2ae208SSatish Balay #undef __FUNCT__ 20194a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 20209b94acceSBarry Smith /* 20219b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 20229b94acceSBarry Smith positive parameter delta. 20239b94acceSBarry Smith 20249b94acceSBarry Smith Input Parameters: 2025c7afd0dbSLois Curfman McInnes + snes - the SNES context 20269b94acceSBarry Smith . y - approximate solution of linear system 20279b94acceSBarry Smith . fnorm - 2-norm of current function 2028c7afd0dbSLois Curfman McInnes - delta - trust region size 20299b94acceSBarry Smith 20309b94acceSBarry Smith Output Parameters: 2031c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 20329b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 20339b94acceSBarry Smith region, and exceeds zero otherwise. 2034c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 20359b94acceSBarry Smith 20369b94acceSBarry Smith Note: 20374b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 20389b94acceSBarry Smith is set to be the maximum allowable step size. 20399b94acceSBarry Smith 20409b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 20419b94acceSBarry Smith */ 2042dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 20439b94acceSBarry Smith { 2044064f8208SBarry Smith PetscReal nrm; 2045ea709b57SSatish Balay PetscScalar cnorm; 2046dfbe8321SBarry Smith PetscErrorCode ierr; 20473a40ed3dSBarry Smith 20483a40ed3dSBarry Smith PetscFunctionBegin; 20494482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20504482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,2); 2051c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 2052184914b5SBarry Smith 2053064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 2054064f8208SBarry Smith if (nrm > *delta) { 2055064f8208SBarry Smith nrm = *delta/nrm; 2056064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 2057064f8208SBarry Smith cnorm = nrm; 20582dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 20599b94acceSBarry Smith *ynorm = *delta; 20609b94acceSBarry Smith } else { 20619b94acceSBarry Smith *gpnorm = 0.0; 2062064f8208SBarry Smith *ynorm = nrm; 20639b94acceSBarry Smith } 20643a40ed3dSBarry Smith PetscFunctionReturn(0); 20659b94acceSBarry Smith } 20669b94acceSBarry Smith 20674a2ae208SSatish Balay #undef __FUNCT__ 20684a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 20696ce558aeSBarry Smith /*@C 2070f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 2071f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 20729b94acceSBarry Smith 2073c7afd0dbSLois Curfman McInnes Collective on SNES 2074c7afd0dbSLois Curfman McInnes 2075b2002411SLois Curfman McInnes Input Parameters: 2076c7afd0dbSLois Curfman McInnes + snes - the SNES context 2077f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 207885385478SLisandro Dalcin - x - the solution vector. 20799b94acceSBarry Smith 2080b2002411SLois Curfman McInnes Notes: 20818ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 20828ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 20838ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 20848ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 20858ddd3da0SLois Curfman McInnes 208636851e7fSLois Curfman McInnes Level: beginner 208736851e7fSLois Curfman McInnes 20889b94acceSBarry Smith .keywords: SNES, nonlinear, solve 20899b94acceSBarry Smith 209085385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 20919b94acceSBarry Smith @*/ 2092f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x) 20939b94acceSBarry Smith { 2094dfbe8321SBarry Smith PetscErrorCode ierr; 2095f1af5d2fSBarry Smith PetscTruth flg; 2096eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 2097eabae89aSBarry Smith PetscViewer viewer; 2098052efed2SBarry Smith 20993a40ed3dSBarry Smith PetscFunctionBegin; 21004482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2101f69a0ea3SMatthew Knepley PetscValidHeaderSpecific(x,VEC_COOKIE,3); 2102f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 210385385478SLisandro Dalcin if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2); 210485385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 210585385478SLisandro Dalcin 210685385478SLisandro Dalcin /* set solution vector */ 210785385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 210885385478SLisandro Dalcin if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); } 210985385478SLisandro Dalcin snes->vec_sol = x; 211085385478SLisandro Dalcin /* set afine vector if provided */ 211185385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 211285385478SLisandro Dalcin if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); } 211385385478SLisandro Dalcin snes->vec_rhs = b; 211485385478SLisandro Dalcin 211585385478SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 211685385478SLisandro Dalcin ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr); 211770e92668SMatthew Knepley } 21183f149594SLisandro Dalcin 211970e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 21203f149594SLisandro Dalcin 2121abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 212250ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2123d5e45103SBarry Smith 21243f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 21254936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 212685385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 21274936397dSBarry Smith if (snes->domainerror){ 21284936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 21294936397dSBarry Smith snes->domainerror = PETSC_FALSE; 21304936397dSBarry Smith } 213185385478SLisandro Dalcin 21323f149594SLisandro Dalcin if (!snes->reason) { 21333f149594SLisandro Dalcin SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 21343f149594SLisandro Dalcin } 21353f149594SLisandro Dalcin 21367adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2137eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 21387adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2139eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 2140eabae89aSBarry Smith ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 2141eabae89aSBarry Smith } 2142eabae89aSBarry Smith 21437adad957SLisandro Dalcin ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr); 2144da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 21455968eb51SBarry Smith if (snes->printreason) { 21465968eb51SBarry Smith if (snes->reason > 0) { 21477adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 21485968eb51SBarry Smith } else { 21497adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 21505968eb51SBarry Smith } 21515968eb51SBarry Smith } 21525968eb51SBarry Smith 21533a40ed3dSBarry Smith PetscFunctionReturn(0); 21549b94acceSBarry Smith } 21559b94acceSBarry Smith 21569b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 21579b94acceSBarry Smith 21584a2ae208SSatish Balay #undef __FUNCT__ 21594a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 216082bf6240SBarry Smith /*@C 21614b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 21629b94acceSBarry Smith 2163fee21e36SBarry Smith Collective on SNES 2164fee21e36SBarry Smith 2165c7afd0dbSLois Curfman McInnes Input Parameters: 2166c7afd0dbSLois Curfman McInnes + snes - the SNES context 2167454a90a3SBarry Smith - type - a known method 2168c7afd0dbSLois Curfman McInnes 2169c7afd0dbSLois Curfman McInnes Options Database Key: 2170454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2171c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2172ae12b187SLois Curfman McInnes 21739b94acceSBarry Smith Notes: 2174e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 21754b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2176c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 21774b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2178c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 21799b94acceSBarry Smith 2180ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2181ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2182ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2183ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2184ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2185ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2186ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2187ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2188ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2189b0a32e0cSBarry Smith appropriate method. 219036851e7fSLois Curfman McInnes 219136851e7fSLois Curfman McInnes Level: intermediate 2192a703fe33SLois Curfman McInnes 2193454a90a3SBarry Smith .keywords: SNES, set, type 2194435da068SBarry Smith 2195435da068SBarry Smith .seealso: SNESType, SNESCreate() 2196435da068SBarry Smith 21979b94acceSBarry Smith @*/ 2198a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type) 21999b94acceSBarry Smith { 2200dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 22016831982aSBarry Smith PetscTruth match; 22023a40ed3dSBarry Smith 22033a40ed3dSBarry Smith PetscFunctionBegin; 22044482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 22054482741eSBarry Smith PetscValidCharPointer(type,2); 220682bf6240SBarry Smith 22076831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 22080f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 220992ff6ae8SBarry Smith 22107adad957SLisandro Dalcin ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 2211958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 221275396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 221375396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 221475396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 221575396ef9SLisandro Dalcin snes->ops->setup = 0; 221675396ef9SLisandro Dalcin snes->ops->solve = 0; 221775396ef9SLisandro Dalcin snes->ops->view = 0; 221875396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 221975396ef9SLisandro Dalcin snes->ops->destroy = 0; 222075396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 222175396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 22223a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 2223454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 22243a40ed3dSBarry Smith PetscFunctionReturn(0); 22259b94acceSBarry Smith } 22269b94acceSBarry Smith 2227a847f771SSatish Balay 22289b94acceSBarry Smith /* --------------------------------------------------------------------- */ 22294a2ae208SSatish Balay #undef __FUNCT__ 22304a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 223152baeb72SSatish Balay /*@ 22329b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2233f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 22349b94acceSBarry Smith 2235fee21e36SBarry Smith Not Collective 2236fee21e36SBarry Smith 223736851e7fSLois Curfman McInnes Level: advanced 223836851e7fSLois Curfman McInnes 22399b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 22409b94acceSBarry Smith 22419b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 22429b94acceSBarry Smith @*/ 224363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void) 22449b94acceSBarry Smith { 2245dfbe8321SBarry Smith PetscErrorCode ierr; 224682bf6240SBarry Smith 22473a40ed3dSBarry Smith PetscFunctionBegin; 22481441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 22494c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 22503a40ed3dSBarry Smith PetscFunctionReturn(0); 22519b94acceSBarry Smith } 22529b94acceSBarry Smith 22534a2ae208SSatish Balay #undef __FUNCT__ 22544a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 22559b94acceSBarry Smith /*@C 22569a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 22579b94acceSBarry Smith 2258c7afd0dbSLois Curfman McInnes Not Collective 2259c7afd0dbSLois Curfman McInnes 22609b94acceSBarry Smith Input Parameter: 22614b0e389bSBarry Smith . snes - nonlinear solver context 22629b94acceSBarry Smith 22639b94acceSBarry Smith Output Parameter: 22643a7fca6bSBarry Smith . type - SNES method (a character string) 22659b94acceSBarry Smith 226636851e7fSLois Curfman McInnes Level: intermediate 226736851e7fSLois Curfman McInnes 2268454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 22699b94acceSBarry Smith @*/ 2270a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type) 22719b94acceSBarry Smith { 22723a40ed3dSBarry Smith PetscFunctionBegin; 22734482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 22744482741eSBarry Smith PetscValidPointer(type,2); 22757adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 22763a40ed3dSBarry Smith PetscFunctionReturn(0); 22779b94acceSBarry Smith } 22789b94acceSBarry Smith 22794a2ae208SSatish Balay #undef __FUNCT__ 22804a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 228152baeb72SSatish Balay /*@ 22829b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 22839b94acceSBarry Smith stored. 22849b94acceSBarry Smith 2285c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2286c7afd0dbSLois Curfman McInnes 22879b94acceSBarry Smith Input Parameter: 22889b94acceSBarry Smith . snes - the SNES context 22899b94acceSBarry Smith 22909b94acceSBarry Smith Output Parameter: 22919b94acceSBarry Smith . x - the solution 22929b94acceSBarry Smith 229370e92668SMatthew Knepley Level: intermediate 229436851e7fSLois Curfman McInnes 22959b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 22969b94acceSBarry Smith 229785385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 22989b94acceSBarry Smith @*/ 229963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x) 23009b94acceSBarry Smith { 23013a40ed3dSBarry Smith PetscFunctionBegin; 23024482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 23034482741eSBarry Smith PetscValidPointer(x,2); 230485385478SLisandro Dalcin *x = snes->vec_sol; 230570e92668SMatthew Knepley PetscFunctionReturn(0); 230670e92668SMatthew Knepley } 230770e92668SMatthew Knepley 230870e92668SMatthew Knepley #undef __FUNCT__ 23094a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 231052baeb72SSatish Balay /*@ 23119b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 23129b94acceSBarry Smith stored. 23139b94acceSBarry Smith 2314c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2315c7afd0dbSLois Curfman McInnes 23169b94acceSBarry Smith Input Parameter: 23179b94acceSBarry Smith . snes - the SNES context 23189b94acceSBarry Smith 23199b94acceSBarry Smith Output Parameter: 23209b94acceSBarry Smith . x - the solution update 23219b94acceSBarry Smith 232236851e7fSLois Curfman McInnes Level: advanced 232336851e7fSLois Curfman McInnes 23249b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 23259b94acceSBarry Smith 232685385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 23279b94acceSBarry Smith @*/ 232863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x) 23299b94acceSBarry Smith { 23303a40ed3dSBarry Smith PetscFunctionBegin; 23314482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 23324482741eSBarry Smith PetscValidPointer(x,2); 233385385478SLisandro Dalcin *x = snes->vec_sol_update; 23343a40ed3dSBarry Smith PetscFunctionReturn(0); 23359b94acceSBarry Smith } 23369b94acceSBarry Smith 23374a2ae208SSatish Balay #undef __FUNCT__ 23384a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 23399b94acceSBarry Smith /*@C 23403638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 23419b94acceSBarry Smith 2342c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2343c7afd0dbSLois Curfman McInnes 23449b94acceSBarry Smith Input Parameter: 23459b94acceSBarry Smith . snes - the SNES context 23469b94acceSBarry Smith 23479b94acceSBarry Smith Output Parameter: 23487bf4e008SBarry Smith + r - the function (or PETSC_NULL) 234970e92668SMatthew Knepley . func - the function (or PETSC_NULL) 235070e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 23519b94acceSBarry Smith 235236851e7fSLois Curfman McInnes Level: advanced 235336851e7fSLois Curfman McInnes 2354a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 23559b94acceSBarry Smith 23564b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 23579b94acceSBarry Smith @*/ 235870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 23599b94acceSBarry Smith { 23603a40ed3dSBarry Smith PetscFunctionBegin; 23614482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 236285385478SLisandro Dalcin if (r) *r = snes->vec_func; 2363e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 236470e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 23653a40ed3dSBarry Smith PetscFunctionReturn(0); 23669b94acceSBarry Smith } 23679b94acceSBarry Smith 23684a2ae208SSatish Balay #undef __FUNCT__ 23694a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 23703c7409f5SSatish Balay /*@C 23713c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2372d850072dSLois Curfman McInnes SNES options in the database. 23733c7409f5SSatish Balay 2374fee21e36SBarry Smith Collective on SNES 2375fee21e36SBarry Smith 2376c7afd0dbSLois Curfman McInnes Input Parameter: 2377c7afd0dbSLois Curfman McInnes + snes - the SNES context 2378c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2379c7afd0dbSLois Curfman McInnes 2380d850072dSLois Curfman McInnes Notes: 2381a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2382c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2383d850072dSLois Curfman McInnes 238436851e7fSLois Curfman McInnes Level: advanced 238536851e7fSLois Curfman McInnes 23863c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2387a86d99e1SLois Curfman McInnes 2388a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 23893c7409f5SSatish Balay @*/ 239063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[]) 23913c7409f5SSatish Balay { 2392dfbe8321SBarry Smith PetscErrorCode ierr; 23933c7409f5SSatish Balay 23943a40ed3dSBarry Smith PetscFunctionBegin; 23954482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2396639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 23971cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 239894b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 23993a40ed3dSBarry Smith PetscFunctionReturn(0); 24003c7409f5SSatish Balay } 24013c7409f5SSatish Balay 24024a2ae208SSatish Balay #undef __FUNCT__ 24034a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 24043c7409f5SSatish Balay /*@C 2405f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2406d850072dSLois Curfman McInnes SNES options in the database. 24073c7409f5SSatish Balay 2408fee21e36SBarry Smith Collective on SNES 2409fee21e36SBarry Smith 2410c7afd0dbSLois Curfman McInnes Input Parameters: 2411c7afd0dbSLois Curfman McInnes + snes - the SNES context 2412c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2413c7afd0dbSLois Curfman McInnes 2414d850072dSLois Curfman McInnes Notes: 2415a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2416c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2417d850072dSLois Curfman McInnes 241836851e7fSLois Curfman McInnes Level: advanced 241936851e7fSLois Curfman McInnes 24203c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2421a86d99e1SLois Curfman McInnes 2422a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 24233c7409f5SSatish Balay @*/ 242463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 24253c7409f5SSatish Balay { 2426dfbe8321SBarry Smith PetscErrorCode ierr; 24273c7409f5SSatish Balay 24283a40ed3dSBarry Smith PetscFunctionBegin; 24294482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2430639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 24311cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 243294b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 24333a40ed3dSBarry Smith PetscFunctionReturn(0); 24343c7409f5SSatish Balay } 24353c7409f5SSatish Balay 24364a2ae208SSatish Balay #undef __FUNCT__ 24374a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 24389ab63eb5SSatish Balay /*@C 24393c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 24403c7409f5SSatish Balay SNES options in the database. 24413c7409f5SSatish Balay 2442c7afd0dbSLois Curfman McInnes Not Collective 2443c7afd0dbSLois Curfman McInnes 24443c7409f5SSatish Balay Input Parameter: 24453c7409f5SSatish Balay . snes - the SNES context 24463c7409f5SSatish Balay 24473c7409f5SSatish Balay Output Parameter: 24483c7409f5SSatish Balay . prefix - pointer to the prefix string used 24493c7409f5SSatish Balay 24504ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 24519ab63eb5SSatish Balay sufficient length to hold the prefix. 24529ab63eb5SSatish Balay 245336851e7fSLois Curfman McInnes Level: advanced 245436851e7fSLois Curfman McInnes 24553c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2456a86d99e1SLois Curfman McInnes 2457a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 24583c7409f5SSatish Balay @*/ 2459e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 24603c7409f5SSatish Balay { 2461dfbe8321SBarry Smith PetscErrorCode ierr; 24623c7409f5SSatish Balay 24633a40ed3dSBarry Smith PetscFunctionBegin; 24644482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2465639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 24663a40ed3dSBarry Smith PetscFunctionReturn(0); 24673c7409f5SSatish Balay } 24683c7409f5SSatish Balay 2469b2002411SLois Curfman McInnes 24704a2ae208SSatish Balay #undef __FUNCT__ 24714a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 24723cea93caSBarry Smith /*@C 24733cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 24743cea93caSBarry Smith 24757f6c08e0SMatthew Knepley Level: advanced 24763cea93caSBarry Smith @*/ 247763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2478b2002411SLois Curfman McInnes { 2479e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2480dfbe8321SBarry Smith PetscErrorCode ierr; 2481b2002411SLois Curfman McInnes 2482b2002411SLois Curfman McInnes PetscFunctionBegin; 2483b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2484c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2485b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2486b2002411SLois Curfman McInnes } 2487da9b6338SBarry Smith 2488da9b6338SBarry Smith #undef __FUNCT__ 2489da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 249063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes) 2491da9b6338SBarry Smith { 2492dfbe8321SBarry Smith PetscErrorCode ierr; 249377431f27SBarry Smith PetscInt N,i,j; 2494da9b6338SBarry Smith Vec u,uh,fh; 2495da9b6338SBarry Smith PetscScalar value; 2496da9b6338SBarry Smith PetscReal norm; 2497da9b6338SBarry Smith 2498da9b6338SBarry Smith PetscFunctionBegin; 2499da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 2500da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 2501da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 2502da9b6338SBarry Smith 2503da9b6338SBarry Smith /* currently only works for sequential */ 2504da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 2505da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 2506da9b6338SBarry Smith for (i=0; i<N; i++) { 2507da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 250877431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 2509da9b6338SBarry Smith for (j=-10; j<11; j++) { 2510ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 2511da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 25123ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 2513da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 251477431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 2515da9b6338SBarry Smith value = -value; 2516da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 2517da9b6338SBarry Smith } 2518da9b6338SBarry Smith } 2519da9b6338SBarry Smith ierr = VecDestroy(uh);CHKERRQ(ierr); 2520da9b6338SBarry Smith ierr = VecDestroy(fh);CHKERRQ(ierr); 2521da9b6338SBarry Smith PetscFunctionReturn(0); 2522da9b6338SBarry Smith } 252371f87433Sdalcinl 252471f87433Sdalcinl #undef __FUNCT__ 2525fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 252671f87433Sdalcinl /*@ 2527fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 252871f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 252971f87433Sdalcinl Newton method. 253071f87433Sdalcinl 253171f87433Sdalcinl Collective on SNES 253271f87433Sdalcinl 253371f87433Sdalcinl Input Parameters: 253471f87433Sdalcinl + snes - SNES context 253571f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 253671f87433Sdalcinl 253771f87433Sdalcinl Notes: 253871f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 253971f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 254071f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 254171f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 254271f87433Sdalcinl solver. 254371f87433Sdalcinl 254471f87433Sdalcinl Level: advanced 254571f87433Sdalcinl 254671f87433Sdalcinl Reference: 254771f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 254871f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 254971f87433Sdalcinl 255071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 255171f87433Sdalcinl 2552fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 255371f87433Sdalcinl @*/ 2554fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag) 255571f87433Sdalcinl { 255671f87433Sdalcinl PetscFunctionBegin; 255771f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 255871f87433Sdalcinl snes->ksp_ewconv = flag; 255971f87433Sdalcinl PetscFunctionReturn(0); 256071f87433Sdalcinl } 256171f87433Sdalcinl 256271f87433Sdalcinl #undef __FUNCT__ 2563fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 256471f87433Sdalcinl /*@ 2565fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 256671f87433Sdalcinl for computing relative tolerance for linear solvers within an 256771f87433Sdalcinl inexact Newton method. 256871f87433Sdalcinl 256971f87433Sdalcinl Not Collective 257071f87433Sdalcinl 257171f87433Sdalcinl Input Parameter: 257271f87433Sdalcinl . snes - SNES context 257371f87433Sdalcinl 257471f87433Sdalcinl Output Parameter: 257571f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 257671f87433Sdalcinl 257771f87433Sdalcinl Notes: 257871f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 257971f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 258071f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 258171f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 258271f87433Sdalcinl solver. 258371f87433Sdalcinl 258471f87433Sdalcinl Level: advanced 258571f87433Sdalcinl 258671f87433Sdalcinl Reference: 258771f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 258871f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 258971f87433Sdalcinl 259071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 259171f87433Sdalcinl 2592fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 259371f87433Sdalcinl @*/ 2594fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag) 259571f87433Sdalcinl { 259671f87433Sdalcinl PetscFunctionBegin; 259771f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 259871f87433Sdalcinl PetscValidPointer(flag,2); 259971f87433Sdalcinl *flag = snes->ksp_ewconv; 260071f87433Sdalcinl PetscFunctionReturn(0); 260171f87433Sdalcinl } 260271f87433Sdalcinl 260371f87433Sdalcinl #undef __FUNCT__ 2604fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 260571f87433Sdalcinl /*@ 2606fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 260771f87433Sdalcinl convergence criteria for the linear solvers within an inexact 260871f87433Sdalcinl Newton method. 260971f87433Sdalcinl 261071f87433Sdalcinl Collective on SNES 261171f87433Sdalcinl 261271f87433Sdalcinl Input Parameters: 261371f87433Sdalcinl + snes - SNES context 261471f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 261571f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 261671f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 261771f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 261871f87433Sdalcinl (0 <= gamma2 <= 1) 261971f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 262071f87433Sdalcinl . alpha2 - power for safeguard 262171f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 262271f87433Sdalcinl 262371f87433Sdalcinl Note: 262471f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 262571f87433Sdalcinl 262671f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 262771f87433Sdalcinl 262871f87433Sdalcinl Level: advanced 262971f87433Sdalcinl 263071f87433Sdalcinl Reference: 263171f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 263271f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 263371f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 263471f87433Sdalcinl 263571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 263671f87433Sdalcinl 2637fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 263871f87433Sdalcinl @*/ 2639fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 264071f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 264171f87433Sdalcinl { 2642fa9f3622SBarry Smith SNESKSPEW *kctx; 264371f87433Sdalcinl PetscFunctionBegin; 264471f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2645fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 264671f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 264771f87433Sdalcinl 264871f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 264971f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 265071f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 265171f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 265271f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 265371f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 265471f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 265571f87433Sdalcinl 265671f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 265771f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 265871f87433Sdalcinl } 265971f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 266071f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 266171f87433Sdalcinl } 266271f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 266371f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 266471f87433Sdalcinl } 266571f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 266671f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 266771f87433Sdalcinl } 266871f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 266971f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 267071f87433Sdalcinl } 267171f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 267271f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 267371f87433Sdalcinl } 267471f87433Sdalcinl PetscFunctionReturn(0); 267571f87433Sdalcinl } 267671f87433Sdalcinl 267771f87433Sdalcinl #undef __FUNCT__ 2678fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 267971f87433Sdalcinl /*@ 2680fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 268171f87433Sdalcinl convergence criteria for the linear solvers within an inexact 268271f87433Sdalcinl Newton method. 268371f87433Sdalcinl 268471f87433Sdalcinl Not Collective 268571f87433Sdalcinl 268671f87433Sdalcinl Input Parameters: 268771f87433Sdalcinl snes - SNES context 268871f87433Sdalcinl 268971f87433Sdalcinl Output Parameters: 269071f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 269171f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 269271f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 269371f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 269471f87433Sdalcinl (0 <= gamma2 <= 1) 269571f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 269671f87433Sdalcinl . alpha2 - power for safeguard 269771f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 269871f87433Sdalcinl 269971f87433Sdalcinl Level: advanced 270071f87433Sdalcinl 270171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 270271f87433Sdalcinl 2703fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 270471f87433Sdalcinl @*/ 2705fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 270671f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 270771f87433Sdalcinl { 2708fa9f3622SBarry Smith SNESKSPEW *kctx; 270971f87433Sdalcinl PetscFunctionBegin; 271071f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2711fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 271271f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 271371f87433Sdalcinl if(version) *version = kctx->version; 271471f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 271571f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 271671f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 271771f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 271871f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 271971f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 272071f87433Sdalcinl PetscFunctionReturn(0); 272171f87433Sdalcinl } 272271f87433Sdalcinl 272371f87433Sdalcinl #undef __FUNCT__ 2724fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 2725fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 272671f87433Sdalcinl { 272771f87433Sdalcinl PetscErrorCode ierr; 2728fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 272971f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 273071f87433Sdalcinl 273171f87433Sdalcinl PetscFunctionBegin; 273271f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 273371f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 273471f87433Sdalcinl rtol = kctx->rtol_0; 273571f87433Sdalcinl } else { 273671f87433Sdalcinl if (kctx->version == 1) { 273771f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 273871f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 273971f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 274071f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 274171f87433Sdalcinl } else if (kctx->version == 2) { 274271f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 274371f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 274471f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 274571f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 274671f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 274771f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 274871f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 274971f87433Sdalcinl stol = PetscMax(rtol,stol); 275071f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 275171f87433Sdalcinl /* safeguard: avoid oversolving */ 275271f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 275371f87433Sdalcinl stol = PetscMax(rtol,stol); 275471f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 275571f87433Sdalcinl } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 275671f87433Sdalcinl } 275771f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 275871f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 275971f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 276071f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 276171f87433Sdalcinl PetscFunctionReturn(0); 276271f87433Sdalcinl } 276371f87433Sdalcinl 276471f87433Sdalcinl #undef __FUNCT__ 2765fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 2766fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 276771f87433Sdalcinl { 276871f87433Sdalcinl PetscErrorCode ierr; 2769fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 277071f87433Sdalcinl PCSide pcside; 277171f87433Sdalcinl Vec lres; 277271f87433Sdalcinl 277371f87433Sdalcinl PetscFunctionBegin; 277471f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 277571f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 277671f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 277771f87433Sdalcinl if (kctx->version == 1) { 277871f87433Sdalcinl ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr); 277971f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 278071f87433Sdalcinl /* KSP residual is true linear residual */ 278171f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 278271f87433Sdalcinl } else { 278371f87433Sdalcinl /* KSP residual is preconditioned residual */ 278471f87433Sdalcinl /* compute true linear residual norm */ 278571f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 278671f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 278771f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 278871f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 278971f87433Sdalcinl ierr = VecDestroy(lres);CHKERRQ(ierr); 279071f87433Sdalcinl } 279171f87433Sdalcinl } 279271f87433Sdalcinl PetscFunctionReturn(0); 279371f87433Sdalcinl } 279471f87433Sdalcinl 279571f87433Sdalcinl #undef __FUNCT__ 279671f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 279771f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 279871f87433Sdalcinl { 279971f87433Sdalcinl PetscErrorCode ierr; 280071f87433Sdalcinl 280171f87433Sdalcinl PetscFunctionBegin; 2802fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 280371f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 2804fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 280571f87433Sdalcinl PetscFunctionReturn(0); 280671f87433Sdalcinl } 2807