163dd3a1aSKris Buschelman #define PETSCSNES_DLL 29b94acceSBarry Smith 37c4f633dSBarry Smith #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 230186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 231cce0b1b2SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");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 } 239*90d69ab7SBarry Smith /* not used here, but called so will go into help messaage */ 240909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 24193c39befSBarry Smith 24287828ca2SBarry Smith ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 24370441072SBarry Smith ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 244186905e3SBarry Smith 24587828ca2SBarry Smith ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 246b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 247b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 24850ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 249ddf469c8SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr); 25085385478SLisandro Dalcin 251a8054027SBarry Smith ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr); 252a8054027SBarry Smith if (flg) { 253a8054027SBarry Smith ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr); 254a8054027SBarry Smith } 255e35cf81dSBarry Smith ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr); 256e35cf81dSBarry Smith if (flg) { 257e35cf81dSBarry Smith ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr); 258e35cf81dSBarry Smith } 259a8054027SBarry Smith 26085385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 26185385478SLisandro Dalcin if (flg) { 26285385478SLisandro Dalcin switch (indx) { 2637f7931b9SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 2647f7931b9SBarry Smith case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 26585385478SLisandro Dalcin } 26685385478SLisandro Dalcin } 26785385478SLisandro Dalcin 268*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr); 269186905e3SBarry Smith 27085385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 27185385478SLisandro Dalcin 272fa9f3622SBarry Smith ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 273186905e3SBarry Smith 274fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 275fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 276fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 277fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 278fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 279fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 280fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 281186905e3SBarry Smith 282*90d69ab7SBarry Smith flg = PETSC_FALSE; 283*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 284a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 285eabae89aSBarry Smith 286a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 287e8105e01SRichard Katz if (flg) { 288050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 28923d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 290e8105e01SRichard Katz } 291eabae89aSBarry Smith 292b271bb04SBarry Smith ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 293b271bb04SBarry Smith if (flg) { 294b271bb04SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr); 295b271bb04SBarry Smith } 296b271bb04SBarry Smith 297a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 298eabae89aSBarry Smith if (flg) { 299050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 300f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 301e8105e01SRichard Katz } 302eabae89aSBarry Smith 303a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 304eabae89aSBarry Smith if (flg) { 305050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 30623d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 307eabae89aSBarry Smith } 308eabae89aSBarry Smith 309*90d69ab7SBarry Smith flg = PETSC_FALSE; 310*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 311a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 312*90d69ab7SBarry Smith flg = PETSC_FALSE; 313*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 314a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 315*90d69ab7SBarry Smith flg = PETSC_FALSE; 316*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 317a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 318*90d69ab7SBarry Smith flg = PETSC_FALSE; 319*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 320a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 321*90d69ab7SBarry Smith flg = PETSC_FALSE; 322*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 323b271bb04SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 324e24b481bSBarry Smith 325*90d69ab7SBarry Smith flg = PETSC_FALSE; 326*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 3274b27c08aSLois Curfman McInnes if (flg) { 328186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 329ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 3309b94acceSBarry Smith } 331639f9d9dSBarry Smith 33276b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 33376b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 33476b2cf59SMatthew Knepley } 33576b2cf59SMatthew Knepley 336e7788613SBarry Smith if (snes->ops->setfromoptions) { 337e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 338639f9d9dSBarry Smith } 339b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3404bbc92c1SBarry Smith 3411cee3971SBarry Smith 3421cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 34385385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 34493993e2dSLois Curfman McInnes 3453a40ed3dSBarry Smith PetscFunctionReturn(0); 3469b94acceSBarry Smith } 3479b94acceSBarry Smith 348a847f771SSatish Balay 3494a2ae208SSatish Balay #undef __FUNCT__ 3504a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 3519b94acceSBarry Smith /*@ 3529b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3539b94acceSBarry Smith the nonlinear solvers. 3549b94acceSBarry Smith 355fee21e36SBarry Smith Collective on SNES 356fee21e36SBarry Smith 357c7afd0dbSLois Curfman McInnes Input Parameters: 358c7afd0dbSLois Curfman McInnes + snes - the SNES context 359c7afd0dbSLois Curfman McInnes - usrP - optional user context 360c7afd0dbSLois Curfman McInnes 36136851e7fSLois Curfman McInnes Level: intermediate 36236851e7fSLois Curfman McInnes 3639b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3649b94acceSBarry Smith 3659b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3669b94acceSBarry Smith @*/ 36763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP) 3689b94acceSBarry Smith { 3693a40ed3dSBarry Smith PetscFunctionBegin; 3704482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3719b94acceSBarry Smith snes->user = usrP; 3723a40ed3dSBarry Smith PetscFunctionReturn(0); 3739b94acceSBarry Smith } 37474679c65SBarry Smith 3754a2ae208SSatish Balay #undef __FUNCT__ 3764a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 3779b94acceSBarry Smith /*@C 3789b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3799b94acceSBarry Smith nonlinear solvers. 3809b94acceSBarry Smith 381c7afd0dbSLois Curfman McInnes Not Collective 382c7afd0dbSLois Curfman McInnes 3839b94acceSBarry Smith Input Parameter: 3849b94acceSBarry Smith . snes - SNES context 3859b94acceSBarry Smith 3869b94acceSBarry Smith Output Parameter: 3879b94acceSBarry Smith . usrP - user context 3889b94acceSBarry Smith 38936851e7fSLois Curfman McInnes Level: intermediate 39036851e7fSLois Curfman McInnes 3919b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3929b94acceSBarry Smith 3939b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3949b94acceSBarry Smith @*/ 39563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP) 3969b94acceSBarry Smith { 3973a40ed3dSBarry Smith PetscFunctionBegin; 3984482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 3999b94acceSBarry Smith *usrP = snes->user; 4003a40ed3dSBarry Smith PetscFunctionReturn(0); 4019b94acceSBarry Smith } 40274679c65SBarry Smith 4034a2ae208SSatish Balay #undef __FUNCT__ 4044a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 4059b94acceSBarry Smith /*@ 406c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 407c8228a4eSBarry Smith at this time. 4089b94acceSBarry Smith 409c7afd0dbSLois Curfman McInnes Not Collective 410c7afd0dbSLois Curfman McInnes 4119b94acceSBarry Smith Input Parameter: 4129b94acceSBarry Smith . snes - SNES context 4139b94acceSBarry Smith 4149b94acceSBarry Smith Output Parameter: 4159b94acceSBarry Smith . iter - iteration number 4169b94acceSBarry Smith 417c8228a4eSBarry Smith Notes: 418c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 419c8228a4eSBarry Smith 420c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 42108405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 42208405cd6SLois Curfman McInnes .vb 42308405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 42408405cd6SLois Curfman McInnes if (!(it % 2)) { 42508405cd6SLois Curfman McInnes [compute Jacobian here] 42608405cd6SLois Curfman McInnes } 42708405cd6SLois Curfman McInnes .ve 428c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 42908405cd6SLois Curfman McInnes recomputed every second SNES iteration. 430c8228a4eSBarry Smith 43136851e7fSLois Curfman McInnes Level: intermediate 43236851e7fSLois Curfman McInnes 4332b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 4342b668275SBarry Smith 435b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 4369b94acceSBarry Smith @*/ 43763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter) 4389b94acceSBarry Smith { 4393a40ed3dSBarry Smith PetscFunctionBegin; 4404482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4414482741eSBarry Smith PetscValidIntPointer(iter,2); 4429b94acceSBarry Smith *iter = snes->iter; 4433a40ed3dSBarry Smith PetscFunctionReturn(0); 4449b94acceSBarry Smith } 44574679c65SBarry Smith 4464a2ae208SSatish Balay #undef __FUNCT__ 4474a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 4489b94acceSBarry Smith /*@ 4499b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4509b94acceSBarry Smith with SNESSSetFunction(). 4519b94acceSBarry Smith 452c7afd0dbSLois Curfman McInnes Collective on SNES 453c7afd0dbSLois Curfman McInnes 4549b94acceSBarry Smith Input Parameter: 4559b94acceSBarry Smith . snes - SNES context 4569b94acceSBarry Smith 4579b94acceSBarry Smith Output Parameter: 4589b94acceSBarry Smith . fnorm - 2-norm of function 4599b94acceSBarry Smith 46036851e7fSLois Curfman McInnes Level: intermediate 46136851e7fSLois Curfman McInnes 4629b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 463a86d99e1SLois Curfman McInnes 464b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 4659b94acceSBarry Smith @*/ 46671f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 4679b94acceSBarry Smith { 4683a40ed3dSBarry Smith PetscFunctionBegin; 4694482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 4704482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 4719b94acceSBarry Smith *fnorm = snes->norm; 4723a40ed3dSBarry Smith PetscFunctionReturn(0); 4739b94acceSBarry Smith } 47474679c65SBarry Smith 4754a2ae208SSatish Balay #undef __FUNCT__ 476b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 4779b94acceSBarry Smith /*@ 478b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 4799b94acceSBarry Smith attempted by the nonlinear solver. 4809b94acceSBarry Smith 481c7afd0dbSLois Curfman McInnes Not Collective 482c7afd0dbSLois Curfman McInnes 4839b94acceSBarry Smith Input Parameter: 4849b94acceSBarry Smith . snes - SNES context 4859b94acceSBarry Smith 4869b94acceSBarry Smith Output Parameter: 4879b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 4889b94acceSBarry Smith 489c96a6f78SLois Curfman McInnes Notes: 490c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 491c96a6f78SLois Curfman McInnes 49236851e7fSLois Curfman McInnes Level: intermediate 49336851e7fSLois Curfman McInnes 4949b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 49558ebbce7SBarry Smith 496e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 49758ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 4989b94acceSBarry Smith @*/ 499b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 5009b94acceSBarry Smith { 5013a40ed3dSBarry Smith PetscFunctionBegin; 5024482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5034482741eSBarry Smith PetscValidIntPointer(nfails,2); 50450ffb88aSMatthew Knepley *nfails = snes->numFailures; 50550ffb88aSMatthew Knepley PetscFunctionReturn(0); 50650ffb88aSMatthew Knepley } 50750ffb88aSMatthew Knepley 50850ffb88aSMatthew Knepley #undef __FUNCT__ 509b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 51050ffb88aSMatthew Knepley /*@ 511b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 51250ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 51350ffb88aSMatthew Knepley 51450ffb88aSMatthew Knepley Not Collective 51550ffb88aSMatthew Knepley 51650ffb88aSMatthew Knepley Input Parameters: 51750ffb88aSMatthew Knepley + snes - SNES context 51850ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 51950ffb88aSMatthew Knepley 52050ffb88aSMatthew Knepley Level: intermediate 52150ffb88aSMatthew Knepley 52250ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 52358ebbce7SBarry Smith 524e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 52558ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 52650ffb88aSMatthew Knepley @*/ 527b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 52850ffb88aSMatthew Knepley { 52950ffb88aSMatthew Knepley PetscFunctionBegin; 5304482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 53150ffb88aSMatthew Knepley snes->maxFailures = maxFails; 53250ffb88aSMatthew Knepley PetscFunctionReturn(0); 53350ffb88aSMatthew Knepley } 53450ffb88aSMatthew Knepley 53550ffb88aSMatthew Knepley #undef __FUNCT__ 536b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 53750ffb88aSMatthew Knepley /*@ 538b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 53950ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 54050ffb88aSMatthew Knepley 54150ffb88aSMatthew Knepley Not Collective 54250ffb88aSMatthew Knepley 54350ffb88aSMatthew Knepley Input Parameter: 54450ffb88aSMatthew Knepley . snes - SNES context 54550ffb88aSMatthew Knepley 54650ffb88aSMatthew Knepley Output Parameter: 54750ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 54850ffb88aSMatthew Knepley 54950ffb88aSMatthew Knepley Level: intermediate 55050ffb88aSMatthew Knepley 55150ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 55258ebbce7SBarry Smith 553e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 55458ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 55558ebbce7SBarry Smith 55650ffb88aSMatthew Knepley @*/ 557b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 55850ffb88aSMatthew Knepley { 55950ffb88aSMatthew Knepley PetscFunctionBegin; 5604482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5614482741eSBarry Smith PetscValidIntPointer(maxFails,2); 56250ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 5633a40ed3dSBarry Smith PetscFunctionReturn(0); 5649b94acceSBarry Smith } 565a847f771SSatish Balay 5664a2ae208SSatish Balay #undef __FUNCT__ 5672541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 5682541af92SBarry Smith /*@ 5692541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 5702541af92SBarry Smith done by SNES. 5712541af92SBarry Smith 5722541af92SBarry Smith Not Collective 5732541af92SBarry Smith 5742541af92SBarry Smith Input Parameter: 5752541af92SBarry Smith . snes - SNES context 5762541af92SBarry Smith 5772541af92SBarry Smith Output Parameter: 5782541af92SBarry Smith . nfuncs - number of evaluations 5792541af92SBarry Smith 5802541af92SBarry Smith Level: intermediate 5812541af92SBarry Smith 5822541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 58358ebbce7SBarry Smith 584e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 5852541af92SBarry Smith @*/ 5862541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 5872541af92SBarry Smith { 5882541af92SBarry Smith PetscFunctionBegin; 5892541af92SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 5902541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 5912541af92SBarry Smith *nfuncs = snes->nfuncs; 5922541af92SBarry Smith PetscFunctionReturn(0); 5932541af92SBarry Smith } 5942541af92SBarry Smith 5952541af92SBarry Smith #undef __FUNCT__ 5963d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 5973d4c4710SBarry Smith /*@ 5983d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 5993d4c4710SBarry Smith linear solvers. 6003d4c4710SBarry Smith 6013d4c4710SBarry Smith Not Collective 6023d4c4710SBarry Smith 6033d4c4710SBarry Smith Input Parameter: 6043d4c4710SBarry Smith . snes - SNES context 6053d4c4710SBarry Smith 6063d4c4710SBarry Smith Output Parameter: 6073d4c4710SBarry Smith . nfails - number of failed solves 6083d4c4710SBarry Smith 6093d4c4710SBarry Smith Notes: 6103d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 6113d4c4710SBarry Smith 6123d4c4710SBarry Smith Level: intermediate 6133d4c4710SBarry Smith 6143d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 61558ebbce7SBarry Smith 616e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 6173d4c4710SBarry Smith @*/ 6183d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 6193d4c4710SBarry Smith { 6203d4c4710SBarry Smith PetscFunctionBegin; 6213d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6223d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 6233d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 6243d4c4710SBarry Smith PetscFunctionReturn(0); 6253d4c4710SBarry Smith } 6263d4c4710SBarry Smith 6273d4c4710SBarry Smith #undef __FUNCT__ 6283d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 6293d4c4710SBarry Smith /*@ 6303d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 6313d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 6323d4c4710SBarry Smith 6333d4c4710SBarry Smith Collective on SNES 6343d4c4710SBarry Smith 6353d4c4710SBarry Smith Input Parameters: 6363d4c4710SBarry Smith + snes - SNES context 6373d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 6383d4c4710SBarry Smith 6393d4c4710SBarry Smith Level: intermediate 6403d4c4710SBarry Smith 641a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 6423d4c4710SBarry Smith 6433d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 6443d4c4710SBarry Smith 64558ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 6463d4c4710SBarry Smith @*/ 6473d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 6483d4c4710SBarry Smith { 6493d4c4710SBarry Smith PetscFunctionBegin; 6503d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6513d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 6523d4c4710SBarry Smith PetscFunctionReturn(0); 6533d4c4710SBarry Smith } 6543d4c4710SBarry Smith 6553d4c4710SBarry Smith #undef __FUNCT__ 6563d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 6573d4c4710SBarry Smith /*@ 6583d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 6593d4c4710SBarry Smith are allowed before SNES terminates 6603d4c4710SBarry Smith 6613d4c4710SBarry Smith Not Collective 6623d4c4710SBarry Smith 6633d4c4710SBarry Smith Input Parameter: 6643d4c4710SBarry Smith . snes - SNES context 6653d4c4710SBarry Smith 6663d4c4710SBarry Smith Output Parameter: 6673d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 6683d4c4710SBarry Smith 6693d4c4710SBarry Smith Level: intermediate 6703d4c4710SBarry Smith 6713d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 6723d4c4710SBarry Smith 6733d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 6743d4c4710SBarry Smith 675e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 6763d4c4710SBarry Smith @*/ 6773d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 6783d4c4710SBarry Smith { 6793d4c4710SBarry Smith PetscFunctionBegin; 6803d4c4710SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 6813d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 6823d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 6833d4c4710SBarry Smith PetscFunctionReturn(0); 6843d4c4710SBarry Smith } 6853d4c4710SBarry Smith 6863d4c4710SBarry Smith #undef __FUNCT__ 687b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 688c96a6f78SLois Curfman McInnes /*@ 689b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 690c96a6f78SLois Curfman McInnes used by the nonlinear solver. 691c96a6f78SLois Curfman McInnes 692c7afd0dbSLois Curfman McInnes Not Collective 693c7afd0dbSLois Curfman McInnes 694c96a6f78SLois Curfman McInnes Input Parameter: 695c96a6f78SLois Curfman McInnes . snes - SNES context 696c96a6f78SLois Curfman McInnes 697c96a6f78SLois Curfman McInnes Output Parameter: 698c96a6f78SLois Curfman McInnes . lits - number of linear iterations 699c96a6f78SLois Curfman McInnes 700c96a6f78SLois Curfman McInnes Notes: 701c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 702c96a6f78SLois Curfman McInnes 70336851e7fSLois Curfman McInnes Level: intermediate 70436851e7fSLois Curfman McInnes 705c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 7062b668275SBarry Smith 7078c7482ecSBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 708c96a6f78SLois Curfman McInnes @*/ 709b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 710c96a6f78SLois Curfman McInnes { 7113a40ed3dSBarry Smith PetscFunctionBegin; 7124482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7134482741eSBarry Smith PetscValidIntPointer(lits,2); 714c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 7153a40ed3dSBarry Smith PetscFunctionReturn(0); 716c96a6f78SLois Curfman McInnes } 717c96a6f78SLois Curfman McInnes 7184a2ae208SSatish Balay #undef __FUNCT__ 71994b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 72052baeb72SSatish Balay /*@ 72194b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 7229b94acceSBarry Smith 72394b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 724c7afd0dbSLois Curfman McInnes 7259b94acceSBarry Smith Input Parameter: 7269b94acceSBarry Smith . snes - the SNES context 7279b94acceSBarry Smith 7289b94acceSBarry Smith Output Parameter: 72994b7f48cSBarry Smith . ksp - the KSP context 7309b94acceSBarry Smith 7319b94acceSBarry Smith Notes: 73294b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 7339b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 7342999313aSBarry Smith PC contexts as well. 7359b94acceSBarry Smith 73636851e7fSLois Curfman McInnes Level: beginner 73736851e7fSLois Curfman McInnes 73894b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7399b94acceSBarry Smith 7402999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7419b94acceSBarry Smith @*/ 74263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp) 7439b94acceSBarry Smith { 7441cee3971SBarry Smith PetscErrorCode ierr; 7451cee3971SBarry Smith 7463a40ed3dSBarry Smith PetscFunctionBegin; 7474482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7484482741eSBarry Smith PetscValidPointer(ksp,2); 7491cee3971SBarry Smith 7501cee3971SBarry Smith if (!snes->ksp) { 7511cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 7521cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 7531cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 7541cee3971SBarry Smith } 75594b7f48cSBarry Smith *ksp = snes->ksp; 7563a40ed3dSBarry Smith PetscFunctionReturn(0); 7579b94acceSBarry Smith } 75882bf6240SBarry Smith 7594a2ae208SSatish Balay #undef __FUNCT__ 7602999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 7612999313aSBarry Smith /*@ 7622999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 7632999313aSBarry Smith 7642999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 7652999313aSBarry Smith 7662999313aSBarry Smith Input Parameters: 7672999313aSBarry Smith + snes - the SNES context 7682999313aSBarry Smith - ksp - the KSP context 7692999313aSBarry Smith 7702999313aSBarry Smith Notes: 7712999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 7722999313aSBarry Smith so this routine is rarely needed. 7732999313aSBarry Smith 7742999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 7752999313aSBarry Smith decreased by one. 7762999313aSBarry Smith 7772999313aSBarry Smith Level: developer 7782999313aSBarry Smith 7792999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 7802999313aSBarry Smith 7812999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 7822999313aSBarry Smith @*/ 7832999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp) 7842999313aSBarry Smith { 7852999313aSBarry Smith PetscErrorCode ierr; 7862999313aSBarry Smith 7872999313aSBarry Smith PetscFunctionBegin; 7882999313aSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 7892999313aSBarry Smith PetscValidHeaderSpecific(ksp,KSP_COOKIE,2); 7902999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 7917dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 792906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 7932999313aSBarry Smith snes->ksp = ksp; 7942999313aSBarry Smith PetscFunctionReturn(0); 7952999313aSBarry Smith } 7962999313aSBarry Smith 7977adad957SLisandro Dalcin #if 0 7982999313aSBarry Smith #undef __FUNCT__ 7994a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 8006849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 801e24b481bSBarry Smith { 802e24b481bSBarry Smith PetscFunctionBegin; 803e24b481bSBarry Smith PetscFunctionReturn(0); 804e24b481bSBarry Smith } 8057adad957SLisandro Dalcin #endif 806e24b481bSBarry Smith 8079b94acceSBarry Smith /* -----------------------------------------------------------*/ 8084a2ae208SSatish Balay #undef __FUNCT__ 8094a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 81052baeb72SSatish Balay /*@ 8119b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 8129b94acceSBarry Smith 813c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 814c7afd0dbSLois Curfman McInnes 815c7afd0dbSLois Curfman McInnes Input Parameters: 816906ed7ccSBarry Smith . comm - MPI communicator 8179b94acceSBarry Smith 8189b94acceSBarry Smith Output Parameter: 8199b94acceSBarry Smith . outsnes - the new SNES context 8209b94acceSBarry Smith 821c7afd0dbSLois Curfman McInnes Options Database Keys: 822c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 823c7afd0dbSLois Curfman McInnes and no preconditioning matrix 824c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 825c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 826c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 827c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 828c1f60f51SBarry Smith 82936851e7fSLois Curfman McInnes Level: beginner 83036851e7fSLois Curfman McInnes 8319b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 8329b94acceSBarry Smith 833a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 834a8054027SBarry Smith 8359b94acceSBarry Smith @*/ 83663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes) 8379b94acceSBarry Smith { 838dfbe8321SBarry Smith PetscErrorCode ierr; 8399b94acceSBarry Smith SNES snes; 840fa9f3622SBarry Smith SNESKSPEW *kctx; 84137fcc0dbSBarry Smith 8423a40ed3dSBarry Smith PetscFunctionBegin; 843ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 8448ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 8458ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 8468ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 8478ba1e511SMatthew Knepley #endif 8488ba1e511SMatthew Knepley 849e7788613SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 8507adad957SLisandro Dalcin 85185385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 8529b94acceSBarry Smith snes->max_its = 50; 8539750a799SBarry Smith snes->max_funcs = 10000; 8549b94acceSBarry Smith snes->norm = 0.0; 855b4874afaSBarry Smith snes->rtol = 1.e-8; 856b4874afaSBarry Smith snes->ttol = 0.0; 85770441072SBarry Smith snes->abstol = 1.e-50; 8589b94acceSBarry Smith snes->xtol = 1.e-8; 8594b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 8609b94acceSBarry Smith snes->nfuncs = 0; 86150ffb88aSMatthew Knepley snes->numFailures = 0; 86250ffb88aSMatthew Knepley snes->maxFailures = 1; 8637a00f4a9SLois Curfman McInnes snes->linear_its = 0; 864e35cf81dSBarry Smith snes->lagjacobian = 1; 865a8054027SBarry Smith snes->lagpreconditioner = 1; 866639f9d9dSBarry Smith snes->numbermonitors = 0; 8679b94acceSBarry Smith snes->data = 0; 8684dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 869186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 8706f24a144SLois Curfman McInnes snes->vwork = 0; 8716f24a144SLois Curfman McInnes snes->nwork = 0; 872758f92a0SBarry Smith snes->conv_hist_len = 0; 873758f92a0SBarry Smith snes->conv_hist_max = 0; 874758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 875758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 876758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 877184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 8789b94acceSBarry Smith 8793d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 8803d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 8813d4c4710SBarry Smith 8829b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 88338f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 8849b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 8859b94acceSBarry Smith kctx->version = 2; 8869b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 8879b94acceSBarry Smith this was too large for some test cases */ 8889b94acceSBarry Smith kctx->rtol_last = 0; 8899b94acceSBarry Smith kctx->rtol_max = .9; 8909b94acceSBarry Smith kctx->gamma = 1.0; 89171f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 89271f87433Sdalcinl kctx->alpha2 = kctx->alpha; 8939b94acceSBarry Smith kctx->threshold = .1; 8949b94acceSBarry Smith kctx->lresid_last = 0; 8959b94acceSBarry Smith kctx->norm_last = 0; 8969b94acceSBarry Smith 8979b94acceSBarry Smith *outsnes = snes; 89800036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 8993a40ed3dSBarry Smith PetscFunctionReturn(0); 9009b94acceSBarry Smith } 9019b94acceSBarry Smith 9024a2ae208SSatish Balay #undef __FUNCT__ 9034a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 9049b94acceSBarry Smith /*@C 9059b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 9069b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 9079b94acceSBarry Smith equations. 9089b94acceSBarry Smith 909fee21e36SBarry Smith Collective on SNES 910fee21e36SBarry Smith 911c7afd0dbSLois Curfman McInnes Input Parameters: 912c7afd0dbSLois Curfman McInnes + snes - the SNES context 913c7afd0dbSLois Curfman McInnes . r - vector to store function value 914de044059SHong Zhang . func - function evaluation routine 915c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 916c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 9179b94acceSBarry Smith 918c7afd0dbSLois Curfman McInnes Calling sequence of func: 9198d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 920c7afd0dbSLois Curfman McInnes 921313e4042SLois Curfman McInnes . f - function vector 922c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 9239b94acceSBarry Smith 9249b94acceSBarry Smith Notes: 9259b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 9269b94acceSBarry Smith $ f'(x) x = -f(x), 927c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 9289b94acceSBarry Smith 92936851e7fSLois Curfman McInnes Level: beginner 93036851e7fSLois Curfman McInnes 9319b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9329b94acceSBarry Smith 933a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 9349b94acceSBarry Smith @*/ 93563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 9369b94acceSBarry Smith { 93785385478SLisandro Dalcin PetscErrorCode ierr; 9383a40ed3dSBarry Smith PetscFunctionBegin; 9394482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9404482741eSBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE,2); 941c9780b6fSBarry Smith PetscCheckSameComm(snes,1,r,2); 94285385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 94385385478SLisandro Dalcin if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); } 944e7788613SBarry Smith snes->ops->computefunction = func; 94585385478SLisandro Dalcin snes->vec_func = r; 9469b94acceSBarry Smith snes->funP = ctx; 9473a40ed3dSBarry Smith PetscFunctionReturn(0); 9489b94acceSBarry Smith } 9499b94acceSBarry Smith 9503ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 9513ab0aad5SBarry Smith #undef __FUNCT__ 9521096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 9531096aae1SMatthew Knepley /*@C 9541096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 9551096aae1SMatthew Knepley it assumes a zero right hand side. 9561096aae1SMatthew Knepley 9571096aae1SMatthew Knepley Collective on SNES 9581096aae1SMatthew Knepley 9591096aae1SMatthew Knepley Input Parameter: 9601096aae1SMatthew Knepley . snes - the SNES context 9611096aae1SMatthew Knepley 9621096aae1SMatthew Knepley Output Parameter: 963bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 9641096aae1SMatthew Knepley 9651096aae1SMatthew Knepley Level: intermediate 9661096aae1SMatthew Knepley 9671096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 9681096aae1SMatthew Knepley 96985385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 9701096aae1SMatthew Knepley @*/ 9711096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs) 9721096aae1SMatthew Knepley { 9731096aae1SMatthew Knepley PetscFunctionBegin; 9741096aae1SMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 9751096aae1SMatthew Knepley PetscValidPointer(rhs,2); 97685385478SLisandro Dalcin *rhs = snes->vec_rhs; 9771096aae1SMatthew Knepley PetscFunctionReturn(0); 9781096aae1SMatthew Knepley } 9791096aae1SMatthew Knepley 9801096aae1SMatthew Knepley #undef __FUNCT__ 9814a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 9829b94acceSBarry Smith /*@ 98336851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 9849b94acceSBarry Smith SNESSetFunction(). 9859b94acceSBarry Smith 986c7afd0dbSLois Curfman McInnes Collective on SNES 987c7afd0dbSLois Curfman McInnes 9889b94acceSBarry Smith Input Parameters: 989c7afd0dbSLois Curfman McInnes + snes - the SNES context 990c7afd0dbSLois Curfman McInnes - x - input vector 9919b94acceSBarry Smith 9929b94acceSBarry Smith Output Parameter: 9933638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 9949b94acceSBarry Smith 9951bffabb2SLois Curfman McInnes Notes: 99636851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 99736851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 99836851e7fSLois Curfman McInnes themselves. 99936851e7fSLois Curfman McInnes 100036851e7fSLois Curfman McInnes Level: developer 100136851e7fSLois Curfman McInnes 10029b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 10039b94acceSBarry Smith 1004a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 10059b94acceSBarry Smith @*/ 100663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y) 10079b94acceSBarry Smith { 1008dfbe8321SBarry Smith PetscErrorCode ierr; 10099b94acceSBarry Smith 10103a40ed3dSBarry Smith PetscFunctionBegin; 10114482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10124482741eSBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE,2); 10134482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,3); 1014c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1015c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 1016184914b5SBarry Smith 1017d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1018e7788613SBarry Smith if (snes->ops->computefunction) { 1019d64ed03dSBarry Smith PetscStackPush("SNES user function"); 1020e9a2bbcdSBarry Smith CHKMEMQ; 1021e7788613SBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP); 1022e9a2bbcdSBarry Smith CHKMEMQ; 1023d64ed03dSBarry Smith PetscStackPop; 1024d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 102519717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr); 102619717074SBarry Smith } 1027d5e45103SBarry Smith CHKERRQ(ierr); 102885385478SLisandro Dalcin } else if (snes->vec_rhs) { 10291096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 10301096aae1SMatthew Knepley } else { 10311096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 10321096aae1SMatthew Knepley } 103385385478SLisandro Dalcin if (snes->vec_rhs) { 103485385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 10353ab0aad5SBarry Smith } 1036ae3c334cSLois Curfman McInnes snes->nfuncs++; 1037d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 10383a40ed3dSBarry Smith PetscFunctionReturn(0); 10399b94acceSBarry Smith } 10409b94acceSBarry Smith 10414a2ae208SSatish Balay #undef __FUNCT__ 10424a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 104362fef451SLois Curfman McInnes /*@ 104462fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 104562fef451SLois Curfman McInnes set with SNESSetJacobian(). 104662fef451SLois Curfman McInnes 1047c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1048c7afd0dbSLois Curfman McInnes 104962fef451SLois Curfman McInnes Input Parameters: 1050c7afd0dbSLois Curfman McInnes + snes - the SNES context 1051c7afd0dbSLois Curfman McInnes - x - input vector 105262fef451SLois Curfman McInnes 105362fef451SLois Curfman McInnes Output Parameters: 1054c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 105562fef451SLois Curfman McInnes . B - optional preconditioning matrix 10562b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1057fee21e36SBarry Smith 1058e35cf81dSBarry Smith Options Database Keys: 1059e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1060e35cf81dSBarry Smith - -snes_lag_jacobian <lag> 1061e35cf81dSBarry Smith 106262fef451SLois Curfman McInnes Notes: 106362fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 106462fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 106562fef451SLois Curfman McInnes 106694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1067dc5a77f8SLois Curfman McInnes flag parameter. 106862fef451SLois Curfman McInnes 106936851e7fSLois Curfman McInnes Level: developer 107036851e7fSLois Curfman McInnes 107162fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 107262fef451SLois Curfman McInnes 1073e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 107462fef451SLois Curfman McInnes @*/ 107563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10769b94acceSBarry Smith { 1077dfbe8321SBarry Smith PetscErrorCode ierr; 1078ebd3b9afSBarry Smith PetscTruth flag; 10793a40ed3dSBarry Smith 10803a40ed3dSBarry Smith PetscFunctionBegin; 10814482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 10824482741eSBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE,2); 10834482741eSBarry Smith PetscValidPointer(flg,5); 1084c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1085e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1086ebd3b9afSBarry Smith 1087ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1088ebd3b9afSBarry Smith 1089fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1090fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1091fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1092fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1093e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1094e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1095ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1096ebd3b9afSBarry Smith if (flag) { 1097ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1098ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1099ebd3b9afSBarry Smith } 1100e35cf81dSBarry Smith PetscFunctionReturn(0); 1101e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1102e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1103e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1104ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1105ebd3b9afSBarry Smith if (flag) { 1106ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1107ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1108ebd3b9afSBarry Smith } 1109e35cf81dSBarry Smith PetscFunctionReturn(0); 1110e35cf81dSBarry Smith } 1111e35cf81dSBarry Smith 1112c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1113e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1114d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1115dc67937bSBarry Smith CHKMEMQ; 1116e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1117dc67937bSBarry Smith CHKMEMQ; 1118d64ed03dSBarry Smith PetscStackPop; 1119d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1120a8054027SBarry Smith 11213b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 11223b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 11233b4f5425SBarry Smith snes->lagpreconditioner = -1; 11243b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1125a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1126a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1127a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1128a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1129a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1130a8054027SBarry Smith } 1131a8054027SBarry Smith 11326d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 11336ce558aeSBarry Smith /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3); 11346ce558aeSBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE,4); */ 11353a40ed3dSBarry Smith PetscFunctionReturn(0); 11369b94acceSBarry Smith } 11379b94acceSBarry Smith 11384a2ae208SSatish Balay #undef __FUNCT__ 11394a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 11409b94acceSBarry Smith /*@C 11419b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1142044dda88SLois Curfman McInnes location to store the matrix. 11439b94acceSBarry Smith 1144c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1145c7afd0dbSLois Curfman McInnes 11469b94acceSBarry Smith Input Parameters: 1147c7afd0dbSLois Curfman McInnes + snes - the SNES context 11489b94acceSBarry Smith . A - Jacobian matrix 11499b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11509b94acceSBarry Smith . func - Jacobian evaluation routine 1151c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11522cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11539b94acceSBarry Smith 11549b94acceSBarry Smith Calling sequence of func: 11558d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11569b94acceSBarry Smith 1157c7afd0dbSLois Curfman McInnes + x - input vector 11589b94acceSBarry Smith . A - Jacobian matrix 11599b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1160ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 11612b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1162c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11639b94acceSBarry Smith 11649b94acceSBarry Smith Notes: 116594b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 11662cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1167ac21db08SLois Curfman McInnes 1168ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11699b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11709b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11719b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11729b94acceSBarry Smith throughout the global iterations. 11739b94acceSBarry Smith 117416913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 117516913363SBarry Smith each matrix. 117616913363SBarry Smith 117736851e7fSLois Curfman McInnes Level: beginner 117836851e7fSLois Curfman McInnes 11799b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11809b94acceSBarry Smith 11813ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 11829b94acceSBarry Smith @*/ 118363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 11849b94acceSBarry Smith { 1185dfbe8321SBarry Smith PetscErrorCode ierr; 11863a7fca6bSBarry Smith 11873a40ed3dSBarry Smith PetscFunctionBegin; 11884482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 11894482741eSBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2); 11904482741eSBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3); 1191c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 1192c9780b6fSBarry Smith if (B) PetscCheckSameComm(snes,1,B,2); 1193e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 11943a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 11953a7fca6bSBarry Smith if (A) { 11967dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 11973a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 11989b94acceSBarry Smith snes->jacobian = A; 11993a7fca6bSBarry Smith } 12003a7fca6bSBarry Smith if (B) { 12017dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 12023a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 12039b94acceSBarry Smith snes->jacobian_pre = B; 12043a7fca6bSBarry Smith } 12053a40ed3dSBarry Smith PetscFunctionReturn(0); 12069b94acceSBarry Smith } 120762fef451SLois Curfman McInnes 12084a2ae208SSatish Balay #undef __FUNCT__ 12094a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1210c2aafc4cSSatish Balay /*@C 1211b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1212b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1213b4fd4287SBarry Smith 1214c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1215c7afd0dbSLois Curfman McInnes 1216b4fd4287SBarry Smith Input Parameter: 1217b4fd4287SBarry Smith . snes - the nonlinear solver context 1218b4fd4287SBarry Smith 1219b4fd4287SBarry Smith Output Parameters: 1220c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1221b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 122270e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 122370e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1224fee21e36SBarry Smith 122536851e7fSLois Curfman McInnes Level: advanced 122636851e7fSLois Curfman McInnes 1227b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1228b4fd4287SBarry Smith @*/ 122970e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1230b4fd4287SBarry Smith { 12313a40ed3dSBarry Smith PetscFunctionBegin; 12324482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1233b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1234b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1235e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 123670e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 12373a40ed3dSBarry Smith PetscFunctionReturn(0); 1238b4fd4287SBarry Smith } 1239b4fd4287SBarry Smith 12409b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 124163dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 12429b94acceSBarry Smith 12434a2ae208SSatish Balay #undef __FUNCT__ 12444a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 12459b94acceSBarry Smith /*@ 12469b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1247272ac6f2SLois Curfman McInnes of a nonlinear solver. 12489b94acceSBarry Smith 1249fee21e36SBarry Smith Collective on SNES 1250fee21e36SBarry Smith 1251c7afd0dbSLois Curfman McInnes Input Parameters: 125270e92668SMatthew Knepley . snes - the SNES context 1253c7afd0dbSLois Curfman McInnes 1254272ac6f2SLois Curfman McInnes Notes: 1255272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1256272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1257272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1258272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1259272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1260272ac6f2SLois Curfman McInnes 126136851e7fSLois Curfman McInnes Level: advanced 126236851e7fSLois Curfman McInnes 12639b94acceSBarry Smith .keywords: SNES, nonlinear, setup 12649b94acceSBarry Smith 12659b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 12669b94acceSBarry Smith @*/ 126770e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes) 12689b94acceSBarry Smith { 1269dfbe8321SBarry Smith PetscErrorCode ierr; 1270*90d69ab7SBarry Smith PetscTruth mf = PETSC_FALSE, mfOp = PETSC_FALSE, mfOp2 = PETSC_FALSE; 12713a40ed3dSBarry Smith 12723a40ed3dSBarry Smith PetscFunctionBegin; 12734482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 12744dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 12759b94acceSBarry Smith 12767adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 127785385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 127885385478SLisandro Dalcin } 127985385478SLisandro Dalcin 128098382af6SMatthew Knepley ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) auxiliary options","SNES_EX");CHKERRQ(ierr); 1281*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",mf,&mf,PETSC_NULL);CHKERRQ(ierr); 1282*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",mfOp,&mfOp,PETSC_NULL);CHKERRQ(ierr); 1283*90d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_mf_operator2","Use a Matrix-Free (version 2) Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",mfOp2,&mfOp2,PETSC_NULL);CHKERRQ(ierr); 128498382af6SMatthew Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 128598382af6SMatthew Knepley 1286c1f60f51SBarry Smith /* 1287c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1288dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1289c1f60f51SBarry Smith */ 129098382af6SMatthew Knepley if (mfOp) { 1291c1f60f51SBarry Smith Mat J; 1292fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 12933ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 1294ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 12953a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 12963a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1297c1f60f51SBarry Smith } 129845fc7adcSBarry Smith 129903c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT) 130098382af6SMatthew Knepley if (mfOp2) { 130145fc7adcSBarry Smith Mat J; 130245fc7adcSBarry Smith ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr); 130375396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr); 130445fc7adcSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 130545fc7adcSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 130645fc7adcSBarry Smith } 130732a4b47aSMatthew Knepley #endif 130845fc7adcSBarry Smith 1309c1f60f51SBarry Smith /* 1310dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1311c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1312c1f60f51SBarry Smith */ 131398382af6SMatthew Knepley if (mf) { 1314272ac6f2SLois Curfman McInnes Mat J; 1315b5d62d44SBarry Smith KSP ksp; 131694b7f48cSBarry Smith PC pc; 131798382af6SMatthew Knepley PetscTruth flg; 131875396ef9SLisandro Dalcin /* create and set matrix-free operator */ 1319fef1beadSBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 13203ec795f1SBarry Smith ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 132175396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr); 13223ec795f1SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 13233a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1324f3ef73ceSBarry Smith /* force no preconditioner */ 132594b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 1326b5d62d44SBarry Smith ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 1327a9815358SBarry Smith ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr); 1328a9815358SBarry Smith if (!flg) { 132975396ef9SLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 1330f3ef73ceSBarry Smith ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 1331272ac6f2SLois Curfman McInnes } 1332a9815358SBarry Smith } 1333f3ef73ceSBarry Smith 133485385478SLisandro Dalcin if (!snes->vec_func && !snes->vec_rhs) { 13351096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 13361096aae1SMatthew Knepley } 133785385478SLisandro Dalcin if (!snes->ops->computefunction && !snes->vec_rhs) { 13381096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 13391096aae1SMatthew Knepley } 1340a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 134129bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1342a8c6a408SBarry Smith } 1343a703fe33SLois Curfman McInnes 1344b710008aSBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);} 1345b710008aSBarry Smith 1346410397dcSLisandro Dalcin if (snes->ops->setup) { 1347410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1348410397dcSLisandro Dalcin } 13497aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 13503a40ed3dSBarry Smith PetscFunctionReturn(0); 13519b94acceSBarry Smith } 13529b94acceSBarry Smith 13534a2ae208SSatish Balay #undef __FUNCT__ 13544a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 135552baeb72SSatish Balay /*@ 13569b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 13579b94acceSBarry Smith with SNESCreate(). 13589b94acceSBarry Smith 1359c7afd0dbSLois Curfman McInnes Collective on SNES 1360c7afd0dbSLois Curfman McInnes 13619b94acceSBarry Smith Input Parameter: 13629b94acceSBarry Smith . snes - the SNES context 13639b94acceSBarry Smith 136436851e7fSLois Curfman McInnes Level: beginner 136536851e7fSLois Curfman McInnes 13669b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 13679b94acceSBarry Smith 136863a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 13699b94acceSBarry Smith @*/ 137063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes) 13719b94acceSBarry Smith { 13726849ba73SBarry Smith PetscErrorCode ierr; 13733a40ed3dSBarry Smith 13743a40ed3dSBarry Smith PetscFunctionBegin; 13754482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 13767adad957SLisandro Dalcin if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0); 1377d4bb536fSBarry Smith 1378be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 13790f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1380e7788613SBarry Smith if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);} 138185385478SLisandro Dalcin 138285385478SLisandro Dalcin if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);} 138385385478SLisandro Dalcin if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);} 138485385478SLisandro Dalcin if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);} 13853a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 13863a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 13871cee3971SBarry Smith if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);} 138885385478SLisandro Dalcin ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr); 1389522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1390a6570f20SBarry Smith ierr = SNESMonitorCancel(snes);CHKERRQ(ierr); 13917f7931b9SBarry Smith if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);} 1392a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 13933a40ed3dSBarry Smith PetscFunctionReturn(0); 13949b94acceSBarry Smith } 13959b94acceSBarry Smith 13969b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 13979b94acceSBarry Smith 13984a2ae208SSatish Balay #undef __FUNCT__ 1399a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1400a8054027SBarry Smith /*@ 1401a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1402a8054027SBarry Smith 1403a8054027SBarry Smith Collective on SNES 1404a8054027SBarry Smith 1405a8054027SBarry Smith Input Parameters: 1406a8054027SBarry Smith + snes - the SNES context 1407a8054027SBarry 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 14083b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1409a8054027SBarry Smith 1410a8054027SBarry Smith Options Database Keys: 1411a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1412a8054027SBarry Smith 1413a8054027SBarry Smith Notes: 1414a8054027SBarry Smith The default is 1 1415a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1416a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1417a8054027SBarry Smith 1418a8054027SBarry Smith Level: intermediate 1419a8054027SBarry Smith 1420a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1421a8054027SBarry Smith 1422e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1423a8054027SBarry Smith 1424a8054027SBarry Smith @*/ 1425a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1426a8054027SBarry Smith { 1427a8054027SBarry Smith PetscFunctionBegin; 1428a8054027SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 14293b4f5425SBarry Smith if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1430a8054027SBarry Smith if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1431a8054027SBarry Smith snes->lagpreconditioner = lag; 1432a8054027SBarry Smith PetscFunctionReturn(0); 1433a8054027SBarry Smith } 1434a8054027SBarry Smith 1435a8054027SBarry Smith #undef __FUNCT__ 1436a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1437a8054027SBarry Smith /*@ 1438a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1439a8054027SBarry Smith 1440a8054027SBarry Smith Collective on SNES 1441a8054027SBarry Smith 1442a8054027SBarry Smith Input Parameter: 1443a8054027SBarry Smith . snes - the SNES context 1444a8054027SBarry Smith 1445a8054027SBarry Smith Output Parameter: 1446a8054027SBarry 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 14473b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1448a8054027SBarry Smith 1449a8054027SBarry Smith Options Database Keys: 1450a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1451a8054027SBarry Smith 1452a8054027SBarry Smith Notes: 1453a8054027SBarry Smith The default is 1 1454a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1455a8054027SBarry Smith 1456a8054027SBarry Smith Level: intermediate 1457a8054027SBarry Smith 1458a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1459a8054027SBarry Smith 1460a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1461a8054027SBarry Smith 1462a8054027SBarry Smith @*/ 1463a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1464a8054027SBarry Smith { 1465a8054027SBarry Smith PetscFunctionBegin; 1466a8054027SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1467a8054027SBarry Smith *lag = snes->lagpreconditioner; 1468a8054027SBarry Smith PetscFunctionReturn(0); 1469a8054027SBarry Smith } 1470a8054027SBarry Smith 1471a8054027SBarry Smith #undef __FUNCT__ 1472e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 1473e35cf81dSBarry Smith /*@ 1474e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 1475e35cf81dSBarry Smith often the preconditioner is rebuilt. 1476e35cf81dSBarry Smith 1477e35cf81dSBarry Smith Collective on SNES 1478e35cf81dSBarry Smith 1479e35cf81dSBarry Smith Input Parameters: 1480e35cf81dSBarry Smith + snes - the SNES context 1481e35cf81dSBarry 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 1482fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 1483e35cf81dSBarry Smith 1484e35cf81dSBarry Smith Options Database Keys: 1485e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1486e35cf81dSBarry Smith 1487e35cf81dSBarry Smith Notes: 1488e35cf81dSBarry Smith The default is 1 1489e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1490fe3ffe1eSBarry 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 1491fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 1492e35cf81dSBarry Smith 1493e35cf81dSBarry Smith Level: intermediate 1494e35cf81dSBarry Smith 1495e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1496e35cf81dSBarry Smith 1497e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 1498e35cf81dSBarry Smith 1499e35cf81dSBarry Smith @*/ 1500e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag) 1501e35cf81dSBarry Smith { 1502e35cf81dSBarry Smith PetscFunctionBegin; 1503e35cf81dSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1504fe3ffe1eSBarry Smith if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1505e35cf81dSBarry Smith if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1506e35cf81dSBarry Smith snes->lagjacobian = lag; 1507e35cf81dSBarry Smith PetscFunctionReturn(0); 1508e35cf81dSBarry Smith } 1509e35cf81dSBarry Smith 1510e35cf81dSBarry Smith #undef __FUNCT__ 1511e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 1512e35cf81dSBarry Smith /*@ 1513e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 1514e35cf81dSBarry Smith 1515e35cf81dSBarry Smith Collective on SNES 1516e35cf81dSBarry Smith 1517e35cf81dSBarry Smith Input Parameter: 1518e35cf81dSBarry Smith . snes - the SNES context 1519e35cf81dSBarry Smith 1520e35cf81dSBarry Smith Output Parameter: 1521e35cf81dSBarry 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 1522e35cf81dSBarry Smith the Jacobian is built etc. 1523e35cf81dSBarry Smith 1524e35cf81dSBarry Smith Options Database Keys: 1525e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1526e35cf81dSBarry Smith 1527e35cf81dSBarry Smith Notes: 1528e35cf81dSBarry Smith The default is 1 1529e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1530e35cf81dSBarry Smith 1531e35cf81dSBarry Smith Level: intermediate 1532e35cf81dSBarry Smith 1533e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1534e35cf81dSBarry Smith 1535e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 1536e35cf81dSBarry Smith 1537e35cf81dSBarry Smith @*/ 1538e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag) 1539e35cf81dSBarry Smith { 1540e35cf81dSBarry Smith PetscFunctionBegin; 1541e35cf81dSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1542e35cf81dSBarry Smith *lag = snes->lagjacobian; 1543e35cf81dSBarry Smith PetscFunctionReturn(0); 1544e35cf81dSBarry Smith } 1545e35cf81dSBarry Smith 1546e35cf81dSBarry Smith #undef __FUNCT__ 15474a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 15489b94acceSBarry Smith /*@ 1549d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 15509b94acceSBarry Smith 1551c7afd0dbSLois Curfman McInnes Collective on SNES 1552c7afd0dbSLois Curfman McInnes 15539b94acceSBarry Smith Input Parameters: 1554c7afd0dbSLois Curfman McInnes + snes - the SNES context 155570441072SBarry Smith . abstol - absolute convergence tolerance 155633174efeSLois Curfman McInnes . rtol - relative convergence tolerance 155733174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 155833174efeSLois Curfman McInnes of the change in the solution between steps 155933174efeSLois Curfman McInnes . maxit - maximum number of iterations 1560c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1561fee21e36SBarry Smith 156233174efeSLois Curfman McInnes Options Database Keys: 156370441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1564c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1565c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1566c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1567c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15689b94acceSBarry Smith 1569d7a720efSLois Curfman McInnes Notes: 15709b94acceSBarry Smith The default maximum number of iterations is 50. 15719b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15729b94acceSBarry Smith 157336851e7fSLois Curfman McInnes Level: intermediate 157436851e7fSLois Curfman McInnes 157533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15769b94acceSBarry Smith 15772492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 15789b94acceSBarry Smith @*/ 157963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 15809b94acceSBarry Smith { 15813a40ed3dSBarry Smith PetscFunctionBegin; 15824482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 158370441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1584d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1585d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1586d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1587d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15883a40ed3dSBarry Smith PetscFunctionReturn(0); 15899b94acceSBarry Smith } 15909b94acceSBarry Smith 15914a2ae208SSatish Balay #undef __FUNCT__ 15924a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 15939b94acceSBarry Smith /*@ 159433174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 159533174efeSLois Curfman McInnes 1596c7afd0dbSLois Curfman McInnes Not Collective 1597c7afd0dbSLois Curfman McInnes 159833174efeSLois Curfman McInnes Input Parameters: 1599c7afd0dbSLois Curfman McInnes + snes - the SNES context 160085385478SLisandro Dalcin . atol - absolute convergence tolerance 160133174efeSLois Curfman McInnes . rtol - relative convergence tolerance 160233174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 160333174efeSLois Curfman McInnes of the change in the solution between steps 160433174efeSLois Curfman McInnes . maxit - maximum number of iterations 1605c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1606fee21e36SBarry Smith 160733174efeSLois Curfman McInnes Notes: 160833174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 160933174efeSLois Curfman McInnes 161036851e7fSLois Curfman McInnes Level: intermediate 161136851e7fSLois Curfman McInnes 161233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 161333174efeSLois Curfman McInnes 161433174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 161533174efeSLois Curfman McInnes @*/ 161685385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 161733174efeSLois Curfman McInnes { 16183a40ed3dSBarry Smith PetscFunctionBegin; 16194482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 162085385478SLisandro Dalcin if (atol) *atol = snes->abstol; 162133174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 162233174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 162333174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 162433174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 16253a40ed3dSBarry Smith PetscFunctionReturn(0); 162633174efeSLois Curfman McInnes } 162733174efeSLois Curfman McInnes 16284a2ae208SSatish Balay #undef __FUNCT__ 16294a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 163033174efeSLois Curfman McInnes /*@ 16319b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 16329b94acceSBarry Smith 1633fee21e36SBarry Smith Collective on SNES 1634fee21e36SBarry Smith 1635c7afd0dbSLois Curfman McInnes Input Parameters: 1636c7afd0dbSLois Curfman McInnes + snes - the SNES context 1637c7afd0dbSLois Curfman McInnes - tol - tolerance 1638c7afd0dbSLois Curfman McInnes 16399b94acceSBarry Smith Options Database Key: 1640c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 16419b94acceSBarry Smith 164236851e7fSLois Curfman McInnes Level: intermediate 164336851e7fSLois Curfman McInnes 16449b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 16459b94acceSBarry Smith 16462492ecdbSBarry Smith .seealso: SNESSetTolerances() 16479b94acceSBarry Smith @*/ 164863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 16499b94acceSBarry Smith { 16503a40ed3dSBarry Smith PetscFunctionBegin; 16514482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 16529b94acceSBarry Smith snes->deltatol = tol; 16533a40ed3dSBarry Smith PetscFunctionReturn(0); 16549b94acceSBarry Smith } 16559b94acceSBarry Smith 1656df9fa365SBarry Smith /* 1657df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1658df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1659df9fa365SBarry Smith macros instead of functions 1660df9fa365SBarry Smith */ 16614a2ae208SSatish Balay #undef __FUNCT__ 1662a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 1663a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1664ce1608b8SBarry Smith { 1665dfbe8321SBarry Smith PetscErrorCode ierr; 1666ce1608b8SBarry Smith 1667ce1608b8SBarry Smith PetscFunctionBegin; 16684482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1669a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1670ce1608b8SBarry Smith PetscFunctionReturn(0); 1671ce1608b8SBarry Smith } 1672ce1608b8SBarry Smith 16734a2ae208SSatish Balay #undef __FUNCT__ 1674a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 1675a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1676df9fa365SBarry Smith { 1677dfbe8321SBarry Smith PetscErrorCode ierr; 1678df9fa365SBarry Smith 1679df9fa365SBarry Smith PetscFunctionBegin; 1680a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1681df9fa365SBarry Smith PetscFunctionReturn(0); 1682df9fa365SBarry Smith } 1683df9fa365SBarry Smith 16844a2ae208SSatish Balay #undef __FUNCT__ 1685a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 1686a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw) 1687df9fa365SBarry Smith { 1688dfbe8321SBarry Smith PetscErrorCode ierr; 1689df9fa365SBarry Smith 1690df9fa365SBarry Smith PetscFunctionBegin; 1691a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1692df9fa365SBarry Smith PetscFunctionReturn(0); 1693df9fa365SBarry Smith } 1694df9fa365SBarry Smith 1695b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 1696b271bb04SBarry Smith #undef __FUNCT__ 1697b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 1698b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 1699b271bb04SBarry Smith { 1700b271bb04SBarry Smith PetscDrawLG lg; 1701b271bb04SBarry Smith PetscErrorCode ierr; 1702b271bb04SBarry Smith PetscReal x,y,per; 1703b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 1704b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 1705b271bb04SBarry Smith PetscDraw draw; 1706b271bb04SBarry Smith PetscFunctionBegin; 1707b271bb04SBarry Smith if (!monctx) { 1708b271bb04SBarry Smith MPI_Comm comm; 1709b271bb04SBarry Smith 1710b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 1711b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 1712b271bb04SBarry Smith } 1713b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 1714b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1715b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1716b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 1717b271bb04SBarry Smith x = (PetscReal) n; 1718b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 1719b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1720b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1721b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1722b271bb04SBarry Smith } 1723b271bb04SBarry Smith 1724b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 1725b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1726b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1727b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 1728b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 1729b271bb04SBarry Smith x = (PetscReal) n; 1730b271bb04SBarry Smith y = 100.0*per; 1731b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1732b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1733b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1734b271bb04SBarry Smith } 1735b271bb04SBarry Smith 1736b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 1737b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1738b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1739b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 1740b271bb04SBarry Smith x = (PetscReal) n; 1741b271bb04SBarry Smith y = (prev - rnorm)/prev; 1742b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1743b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1744b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1745b271bb04SBarry Smith } 1746b271bb04SBarry Smith 1747b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 1748b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1749b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1750b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 1751b271bb04SBarry Smith x = (PetscReal) n; 1752b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 1753b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 1754b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1755b271bb04SBarry Smith } 1756b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1757b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1758b271bb04SBarry Smith } 1759b271bb04SBarry Smith prev = rnorm; 1760b271bb04SBarry Smith PetscFunctionReturn(0); 1761b271bb04SBarry Smith } 1762b271bb04SBarry Smith 1763b271bb04SBarry Smith #undef __FUNCT__ 1764b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 1765b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1766b271bb04SBarry Smith { 1767b271bb04SBarry Smith PetscErrorCode ierr; 1768b271bb04SBarry Smith 1769b271bb04SBarry Smith PetscFunctionBegin; 1770b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1771b271bb04SBarry Smith PetscFunctionReturn(0); 1772b271bb04SBarry Smith } 1773b271bb04SBarry Smith 1774b271bb04SBarry Smith #undef __FUNCT__ 1775b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 1776b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw) 1777b271bb04SBarry Smith { 1778b271bb04SBarry Smith PetscErrorCode ierr; 1779b271bb04SBarry Smith 1780b271bb04SBarry Smith PetscFunctionBegin; 1781b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1782b271bb04SBarry Smith PetscFunctionReturn(0); 1783b271bb04SBarry Smith } 1784b271bb04SBarry Smith 17859b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 17869b94acceSBarry Smith 17874a2ae208SSatish Balay #undef __FUNCT__ 1788a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 17899b94acceSBarry Smith /*@C 1790a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 17919b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 17929b94acceSBarry Smith progress. 17939b94acceSBarry Smith 1794fee21e36SBarry Smith Collective on SNES 1795fee21e36SBarry Smith 1796c7afd0dbSLois Curfman McInnes Input Parameters: 1797c7afd0dbSLois Curfman McInnes + snes - the SNES context 1798c7afd0dbSLois Curfman McInnes . func - monitoring routine 1799b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1800e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 1801b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1802b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 18039b94acceSBarry Smith 1804c7afd0dbSLois Curfman McInnes Calling sequence of func: 1805a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 1806c7afd0dbSLois Curfman McInnes 1807c7afd0dbSLois Curfman McInnes + snes - the SNES context 1808c7afd0dbSLois Curfman McInnes . its - iteration number 1809c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 181040a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 18119b94acceSBarry Smith 18129665c990SLois Curfman McInnes Options Database Keys: 1813a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 1814a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 1815a6570f20SBarry Smith uses SNESMonitorLGCreate() 1816a6570f20SBarry Smith _ -snes_monitor_cancel - cancels all monitors that have 1817c7afd0dbSLois Curfman McInnes been hardwired into a code by 1818a6570f20SBarry Smith calls to SNESMonitorSet(), but 1819c7afd0dbSLois Curfman McInnes does not cancel those set via 1820c7afd0dbSLois Curfman McInnes the options database. 18219665c990SLois Curfman McInnes 1822639f9d9dSBarry Smith Notes: 18236bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 1824a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 18256bc08f3fSLois Curfman McInnes order in which they were set. 1826639f9d9dSBarry Smith 1827025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 1828025f1a04SBarry Smith 182936851e7fSLois Curfman McInnes Level: intermediate 183036851e7fSLois Curfman McInnes 18319b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 18329b94acceSBarry Smith 1833a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 18349b94acceSBarry Smith @*/ 1835b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 18369b94acceSBarry Smith { 1837b90d0a6eSBarry Smith PetscInt i; 1838b90d0a6eSBarry Smith 18393a40ed3dSBarry Smith PetscFunctionBegin; 18404482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1841639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 184229bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1843639f9d9dSBarry Smith } 1844b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 1845b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 1846b90d0a6eSBarry Smith 1847b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 1848b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 1849b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 1850b90d0a6eSBarry Smith PetscErrorCode ierr; 1851b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 1852b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 1853b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 1854b90d0a6eSBarry Smith ierr = (*monitordestroy)(mctx);CHKERRQ(ierr); 1855b90d0a6eSBarry Smith PetscFunctionReturn(0); 1856b90d0a6eSBarry Smith } 1857b90d0a6eSBarry Smith } 1858b90d0a6eSBarry Smith } 1859b90d0a6eSBarry Smith } 1860b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 1861b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1862639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 18633a40ed3dSBarry Smith PetscFunctionReturn(0); 18649b94acceSBarry Smith } 18659b94acceSBarry Smith 18664a2ae208SSatish Balay #undef __FUNCT__ 1867a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 18685cd90555SBarry Smith /*@C 1869a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 18705cd90555SBarry Smith 1871c7afd0dbSLois Curfman McInnes Collective on SNES 1872c7afd0dbSLois Curfman McInnes 18735cd90555SBarry Smith Input Parameters: 18745cd90555SBarry Smith . snes - the SNES context 18755cd90555SBarry Smith 18761a480d89SAdministrator Options Database Key: 1877a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 1878a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 1879c7afd0dbSLois Curfman McInnes set via the options database 18805cd90555SBarry Smith 18815cd90555SBarry Smith Notes: 18825cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 18835cd90555SBarry Smith 188436851e7fSLois Curfman McInnes Level: intermediate 188536851e7fSLois Curfman McInnes 18865cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 18875cd90555SBarry Smith 1888a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 18895cd90555SBarry Smith @*/ 1890a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes) 18915cd90555SBarry Smith { 1892d952e501SBarry Smith PetscErrorCode ierr; 1893d952e501SBarry Smith PetscInt i; 1894d952e501SBarry Smith 18955cd90555SBarry Smith PetscFunctionBegin; 18964482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 1897d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1898d952e501SBarry Smith if (snes->monitordestroy[i]) { 1899d952e501SBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1900d952e501SBarry Smith } 1901d952e501SBarry Smith } 19025cd90555SBarry Smith snes->numbermonitors = 0; 19035cd90555SBarry Smith PetscFunctionReturn(0); 19045cd90555SBarry Smith } 19055cd90555SBarry Smith 19064a2ae208SSatish Balay #undef __FUNCT__ 19074a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 19089b94acceSBarry Smith /*@C 19099b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 19109b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 19119b94acceSBarry Smith 1912fee21e36SBarry Smith Collective on SNES 1913fee21e36SBarry Smith 1914c7afd0dbSLois Curfman McInnes Input Parameters: 1915c7afd0dbSLois Curfman McInnes + snes - the SNES context 1916c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 19177f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 19187f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 19199b94acceSBarry Smith 1920c7afd0dbSLois Curfman McInnes Calling sequence of func: 192106ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1922c7afd0dbSLois Curfman McInnes 1923c7afd0dbSLois Curfman McInnes + snes - the SNES context 192406ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 1925c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1926184914b5SBarry Smith . reason - reason for convergence/divergence 1927c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 19284b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 19294b27c08aSLois Curfman McInnes - f - 2-norm of function 19309b94acceSBarry Smith 193136851e7fSLois Curfman McInnes Level: advanced 193236851e7fSLois Curfman McInnes 19339b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 19349b94acceSBarry Smith 193585385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 19369b94acceSBarry Smith @*/ 19377f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 19389b94acceSBarry Smith { 19397f7931b9SBarry Smith PetscErrorCode ierr; 19407f7931b9SBarry Smith 19413a40ed3dSBarry Smith PetscFunctionBegin; 19424482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 194385385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 19447f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 19457f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 19467f7931b9SBarry Smith } 194785385478SLisandro Dalcin snes->ops->converged = func; 19487f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 194985385478SLisandro Dalcin snes->cnvP = cctx; 19503a40ed3dSBarry Smith PetscFunctionReturn(0); 19519b94acceSBarry Smith } 19529b94acceSBarry Smith 19534a2ae208SSatish Balay #undef __FUNCT__ 19544a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 195552baeb72SSatish Balay /*@ 1956184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1957184914b5SBarry Smith 1958184914b5SBarry Smith Not Collective 1959184914b5SBarry Smith 1960184914b5SBarry Smith Input Parameter: 1961184914b5SBarry Smith . snes - the SNES context 1962184914b5SBarry Smith 1963184914b5SBarry Smith Output Parameter: 19644d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 1965184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1966184914b5SBarry Smith 1967184914b5SBarry Smith Level: intermediate 1968184914b5SBarry Smith 1969184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1970184914b5SBarry Smith 1971184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1972184914b5SBarry Smith 197385385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 1974184914b5SBarry Smith @*/ 197563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1976184914b5SBarry Smith { 1977184914b5SBarry Smith PetscFunctionBegin; 19784482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 19794482741eSBarry Smith PetscValidPointer(reason,2); 1980184914b5SBarry Smith *reason = snes->reason; 1981184914b5SBarry Smith PetscFunctionReturn(0); 1982184914b5SBarry Smith } 1983184914b5SBarry Smith 19844a2ae208SSatish Balay #undef __FUNCT__ 19854a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 1986c9005455SLois Curfman McInnes /*@ 1987c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1988c9005455SLois Curfman McInnes 1989fee21e36SBarry Smith Collective on SNES 1990fee21e36SBarry Smith 1991c7afd0dbSLois Curfman McInnes Input Parameters: 1992c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 19938c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 1994cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 1995758f92a0SBarry Smith . na - size of a and its 199664731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 1997758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1998c7afd0dbSLois Curfman McInnes 1999c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 2000c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 2001c9005455SLois Curfman McInnes during the section of code that is being timed. 2002c9005455SLois Curfman McInnes 200336851e7fSLois Curfman McInnes Level: intermediate 200436851e7fSLois Curfman McInnes 2005c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2006758f92a0SBarry Smith 200708405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2008758f92a0SBarry Smith 2009c9005455SLois Curfman McInnes @*/ 2010a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset) 2011c9005455SLois Curfman McInnes { 20123a40ed3dSBarry Smith PetscFunctionBegin; 20134482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 20144482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2015a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2016c9005455SLois Curfman McInnes snes->conv_hist = a; 2017758f92a0SBarry Smith snes->conv_hist_its = its; 2018758f92a0SBarry Smith snes->conv_hist_max = na; 2019a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2020758f92a0SBarry Smith snes->conv_hist_reset = reset; 2021758f92a0SBarry Smith PetscFunctionReturn(0); 2022758f92a0SBarry Smith } 2023758f92a0SBarry Smith 20244a2ae208SSatish Balay #undef __FUNCT__ 20254a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 20260c4c9dddSBarry Smith /*@C 2027758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 2028758f92a0SBarry Smith 2029758f92a0SBarry Smith Collective on SNES 2030758f92a0SBarry Smith 2031758f92a0SBarry Smith Input Parameter: 2032758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 2033758f92a0SBarry Smith 2034758f92a0SBarry Smith Output Parameters: 2035758f92a0SBarry Smith . a - array to hold history 2036758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 2037758f92a0SBarry Smith negative if not converged) for each solve. 2038758f92a0SBarry Smith - na - size of a and its 2039758f92a0SBarry Smith 2040758f92a0SBarry Smith Notes: 2041758f92a0SBarry Smith The calling sequence for this routine in Fortran is 2042758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 2043758f92a0SBarry Smith 2044758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 2045758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 2046758f92a0SBarry Smith during the section of code that is being timed. 2047758f92a0SBarry Smith 2048758f92a0SBarry Smith Level: intermediate 2049758f92a0SBarry Smith 2050758f92a0SBarry Smith .keywords: SNES, get, convergence, history 2051758f92a0SBarry Smith 2052758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 2053758f92a0SBarry Smith 2054758f92a0SBarry Smith @*/ 205563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 2056758f92a0SBarry Smith { 2057758f92a0SBarry Smith PetscFunctionBegin; 20584482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2059758f92a0SBarry Smith if (a) *a = snes->conv_hist; 2060758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 2061758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 20623a40ed3dSBarry Smith PetscFunctionReturn(0); 2063c9005455SLois Curfman McInnes } 2064c9005455SLois Curfman McInnes 2065e74ef692SMatthew Knepley #undef __FUNCT__ 2066e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 2067ac226902SBarry Smith /*@C 206876b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 20697e4bb74cSBarry Smith at the beginning o every iteration of the nonlinear solve. Specifically 20707e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 207176b2cf59SMatthew Knepley 207276b2cf59SMatthew Knepley Collective on SNES 207376b2cf59SMatthew Knepley 207476b2cf59SMatthew Knepley Input Parameters: 207576b2cf59SMatthew Knepley . snes - The nonlinear solver context 207676b2cf59SMatthew Knepley . func - The function 207776b2cf59SMatthew Knepley 207876b2cf59SMatthew Knepley Calling sequence of func: 2079b5d30489SBarry Smith . func (SNES snes, PetscInt step); 208076b2cf59SMatthew Knepley 208176b2cf59SMatthew Knepley . step - The current step of the iteration 208276b2cf59SMatthew Knepley 208376b2cf59SMatthew Knepley Level: intermediate 208476b2cf59SMatthew Knepley 208576b2cf59SMatthew Knepley .keywords: SNES, update 2086b5d30489SBarry Smith 208785385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 208876b2cf59SMatthew Knepley @*/ 208963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 209076b2cf59SMatthew Knepley { 209176b2cf59SMatthew Knepley PetscFunctionBegin; 20924482741eSBarry Smith PetscValidHeaderSpecific(snes, SNES_COOKIE,1); 2093e7788613SBarry Smith snes->ops->update = func; 209476b2cf59SMatthew Knepley PetscFunctionReturn(0); 209576b2cf59SMatthew Knepley } 209676b2cf59SMatthew Knepley 2097e74ef692SMatthew Knepley #undef __FUNCT__ 2098e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 209976b2cf59SMatthew Knepley /*@ 210076b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 210176b2cf59SMatthew Knepley 210276b2cf59SMatthew Knepley Not collective 210376b2cf59SMatthew Knepley 210476b2cf59SMatthew Knepley Input Parameters: 210576b2cf59SMatthew Knepley . snes - The nonlinear solver context 210676b2cf59SMatthew Knepley . step - The current step of the iteration 210776b2cf59SMatthew Knepley 2108205452f4SMatthew Knepley Level: intermediate 2109205452f4SMatthew Knepley 211076b2cf59SMatthew Knepley .keywords: SNES, update 2111a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 211276b2cf59SMatthew Knepley @*/ 211363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step) 211476b2cf59SMatthew Knepley { 211576b2cf59SMatthew Knepley PetscFunctionBegin; 211676b2cf59SMatthew Knepley PetscFunctionReturn(0); 211776b2cf59SMatthew Knepley } 211876b2cf59SMatthew Knepley 21194a2ae208SSatish Balay #undef __FUNCT__ 21204a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 21219b94acceSBarry Smith /* 21229b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 21239b94acceSBarry Smith positive parameter delta. 21249b94acceSBarry Smith 21259b94acceSBarry Smith Input Parameters: 2126c7afd0dbSLois Curfman McInnes + snes - the SNES context 21279b94acceSBarry Smith . y - approximate solution of linear system 21289b94acceSBarry Smith . fnorm - 2-norm of current function 2129c7afd0dbSLois Curfman McInnes - delta - trust region size 21309b94acceSBarry Smith 21319b94acceSBarry Smith Output Parameters: 2132c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 21339b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 21349b94acceSBarry Smith region, and exceeds zero otherwise. 2135c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 21369b94acceSBarry Smith 21379b94acceSBarry Smith Note: 21384b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 21399b94acceSBarry Smith is set to be the maximum allowable step size. 21409b94acceSBarry Smith 21419b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 21429b94acceSBarry Smith */ 2143dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 21449b94acceSBarry Smith { 2145064f8208SBarry Smith PetscReal nrm; 2146ea709b57SSatish Balay PetscScalar cnorm; 2147dfbe8321SBarry Smith PetscErrorCode ierr; 21483a40ed3dSBarry Smith 21493a40ed3dSBarry Smith PetscFunctionBegin; 21504482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 21514482741eSBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE,2); 2152c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 2153184914b5SBarry Smith 2154064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 2155064f8208SBarry Smith if (nrm > *delta) { 2156064f8208SBarry Smith nrm = *delta/nrm; 2157064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 2158064f8208SBarry Smith cnorm = nrm; 21592dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 21609b94acceSBarry Smith *ynorm = *delta; 21619b94acceSBarry Smith } else { 21629b94acceSBarry Smith *gpnorm = 0.0; 2163064f8208SBarry Smith *ynorm = nrm; 21649b94acceSBarry Smith } 21653a40ed3dSBarry Smith PetscFunctionReturn(0); 21669b94acceSBarry Smith } 21679b94acceSBarry Smith 21684a2ae208SSatish Balay #undef __FUNCT__ 21694a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 21706ce558aeSBarry Smith /*@C 2171f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 2172f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 21739b94acceSBarry Smith 2174c7afd0dbSLois Curfman McInnes Collective on SNES 2175c7afd0dbSLois Curfman McInnes 2176b2002411SLois Curfman McInnes Input Parameters: 2177c7afd0dbSLois Curfman McInnes + snes - the SNES context 2178f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 217985385478SLisandro Dalcin - x - the solution vector. 21809b94acceSBarry Smith 2181b2002411SLois Curfman McInnes Notes: 21828ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 21838ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 21848ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 21858ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 21868ddd3da0SLois Curfman McInnes 218736851e7fSLois Curfman McInnes Level: beginner 218836851e7fSLois Curfman McInnes 21899b94acceSBarry Smith .keywords: SNES, nonlinear, solve 21909b94acceSBarry Smith 219185385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 21929b94acceSBarry Smith @*/ 2193f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x) 21949b94acceSBarry Smith { 2195dfbe8321SBarry Smith PetscErrorCode ierr; 2196f1af5d2fSBarry Smith PetscTruth flg; 2197eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 2198eabae89aSBarry Smith PetscViewer viewer; 2199052efed2SBarry Smith 22003a40ed3dSBarry Smith PetscFunctionBegin; 22014482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2202f69a0ea3SMatthew Knepley PetscValidHeaderSpecific(x,VEC_COOKIE,3); 2203f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 220485385478SLisandro Dalcin if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2); 220585385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 220685385478SLisandro Dalcin 220785385478SLisandro Dalcin /* set solution vector */ 220885385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 220985385478SLisandro Dalcin if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); } 221085385478SLisandro Dalcin snes->vec_sol = x; 221185385478SLisandro Dalcin /* set afine vector if provided */ 221285385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 221385385478SLisandro Dalcin if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); } 221485385478SLisandro Dalcin snes->vec_rhs = b; 221585385478SLisandro Dalcin 221685385478SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 221785385478SLisandro Dalcin ierr = VecDuplicate(b, &snes->vec_func);CHKERRQ(ierr); 221870e92668SMatthew Knepley } 22193f149594SLisandro Dalcin 222070e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 22213f149594SLisandro Dalcin 2222abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 222350ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2224d5e45103SBarry Smith 22253f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 22264936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 222785385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 22284936397dSBarry Smith if (snes->domainerror){ 22294936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 22304936397dSBarry Smith snes->domainerror = PETSC_FALSE; 22314936397dSBarry Smith } 223285385478SLisandro Dalcin 22333f149594SLisandro Dalcin if (!snes->reason) { 22343f149594SLisandro Dalcin SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 22353f149594SLisandro Dalcin } 22363f149594SLisandro Dalcin 22377adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2238eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 22397adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2240eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 2241eabae89aSBarry Smith ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 2242eabae89aSBarry Smith } 2243eabae89aSBarry Smith 2244*90d69ab7SBarry Smith flg = PETSC_FALSE; 2245*90d69ab7SBarry Smith ierr = PetscOptionsGetTruth(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr); 2246da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 22475968eb51SBarry Smith if (snes->printreason) { 22485968eb51SBarry Smith if (snes->reason > 0) { 22497adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 22505968eb51SBarry Smith } else { 22517adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 22525968eb51SBarry Smith } 22535968eb51SBarry Smith } 22545968eb51SBarry Smith 22553a40ed3dSBarry Smith PetscFunctionReturn(0); 22569b94acceSBarry Smith } 22579b94acceSBarry Smith 22589b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 22599b94acceSBarry Smith 22604a2ae208SSatish Balay #undef __FUNCT__ 22614a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 226282bf6240SBarry Smith /*@C 22634b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 22649b94acceSBarry Smith 2265fee21e36SBarry Smith Collective on SNES 2266fee21e36SBarry Smith 2267c7afd0dbSLois Curfman McInnes Input Parameters: 2268c7afd0dbSLois Curfman McInnes + snes - the SNES context 2269454a90a3SBarry Smith - type - a known method 2270c7afd0dbSLois Curfman McInnes 2271c7afd0dbSLois Curfman McInnes Options Database Key: 2272454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2273c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2274ae12b187SLois Curfman McInnes 22759b94acceSBarry Smith Notes: 2276e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 22774b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2278c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 22794b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2280c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 22819b94acceSBarry Smith 2282ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2283ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2284ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2285ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2286ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2287ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2288ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2289ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2290ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2291b0a32e0cSBarry Smith appropriate method. 229236851e7fSLois Curfman McInnes 229336851e7fSLois Curfman McInnes Level: intermediate 2294a703fe33SLois Curfman McInnes 2295454a90a3SBarry Smith .keywords: SNES, set, type 2296435da068SBarry Smith 2297435da068SBarry Smith .seealso: SNESType, SNESCreate() 2298435da068SBarry Smith 22999b94acceSBarry Smith @*/ 2300a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type) 23019b94acceSBarry Smith { 2302dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 23036831982aSBarry Smith PetscTruth match; 23043a40ed3dSBarry Smith 23053a40ed3dSBarry Smith PetscFunctionBegin; 23064482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 23074482741eSBarry Smith PetscValidCharPointer(type,2); 230882bf6240SBarry Smith 23096831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 23100f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 231192ff6ae8SBarry Smith 23127adad957SLisandro Dalcin ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 2313958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 231475396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 231575396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 231675396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 231775396ef9SLisandro Dalcin snes->ops->setup = 0; 231875396ef9SLisandro Dalcin snes->ops->solve = 0; 231975396ef9SLisandro Dalcin snes->ops->view = 0; 232075396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 232175396ef9SLisandro Dalcin snes->ops->destroy = 0; 232275396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 232375396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 23243a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 2325454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 23263a40ed3dSBarry Smith PetscFunctionReturn(0); 23279b94acceSBarry Smith } 23289b94acceSBarry Smith 2329a847f771SSatish Balay 23309b94acceSBarry Smith /* --------------------------------------------------------------------- */ 23314a2ae208SSatish Balay #undef __FUNCT__ 23324a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 233352baeb72SSatish Balay /*@ 23349b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2335f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 23369b94acceSBarry Smith 2337fee21e36SBarry Smith Not Collective 2338fee21e36SBarry Smith 233936851e7fSLois Curfman McInnes Level: advanced 234036851e7fSLois Curfman McInnes 23419b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 23429b94acceSBarry Smith 23439b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 23449b94acceSBarry Smith @*/ 234563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void) 23469b94acceSBarry Smith { 2347dfbe8321SBarry Smith PetscErrorCode ierr; 234882bf6240SBarry Smith 23493a40ed3dSBarry Smith PetscFunctionBegin; 23501441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 23514c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 23523a40ed3dSBarry Smith PetscFunctionReturn(0); 23539b94acceSBarry Smith } 23549b94acceSBarry Smith 23554a2ae208SSatish Balay #undef __FUNCT__ 23564a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 23579b94acceSBarry Smith /*@C 23589a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 23599b94acceSBarry Smith 2360c7afd0dbSLois Curfman McInnes Not Collective 2361c7afd0dbSLois Curfman McInnes 23629b94acceSBarry Smith Input Parameter: 23634b0e389bSBarry Smith . snes - nonlinear solver context 23649b94acceSBarry Smith 23659b94acceSBarry Smith Output Parameter: 23663a7fca6bSBarry Smith . type - SNES method (a character string) 23679b94acceSBarry Smith 236836851e7fSLois Curfman McInnes Level: intermediate 236936851e7fSLois Curfman McInnes 2370454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 23719b94acceSBarry Smith @*/ 2372a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type) 23739b94acceSBarry Smith { 23743a40ed3dSBarry Smith PetscFunctionBegin; 23754482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 23764482741eSBarry Smith PetscValidPointer(type,2); 23777adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 23783a40ed3dSBarry Smith PetscFunctionReturn(0); 23799b94acceSBarry Smith } 23809b94acceSBarry Smith 23814a2ae208SSatish Balay #undef __FUNCT__ 23824a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 238352baeb72SSatish Balay /*@ 23849b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 23859b94acceSBarry Smith stored. 23869b94acceSBarry Smith 2387c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2388c7afd0dbSLois Curfman McInnes 23899b94acceSBarry Smith Input Parameter: 23909b94acceSBarry Smith . snes - the SNES context 23919b94acceSBarry Smith 23929b94acceSBarry Smith Output Parameter: 23939b94acceSBarry Smith . x - the solution 23949b94acceSBarry Smith 239570e92668SMatthew Knepley Level: intermediate 239636851e7fSLois Curfman McInnes 23979b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 23989b94acceSBarry Smith 239985385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 24009b94acceSBarry Smith @*/ 240163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x) 24029b94acceSBarry Smith { 24033a40ed3dSBarry Smith PetscFunctionBegin; 24044482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 24054482741eSBarry Smith PetscValidPointer(x,2); 240685385478SLisandro Dalcin *x = snes->vec_sol; 240770e92668SMatthew Knepley PetscFunctionReturn(0); 240870e92668SMatthew Knepley } 240970e92668SMatthew Knepley 241070e92668SMatthew Knepley #undef __FUNCT__ 24114a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 241252baeb72SSatish Balay /*@ 24139b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 24149b94acceSBarry Smith stored. 24159b94acceSBarry Smith 2416c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2417c7afd0dbSLois Curfman McInnes 24189b94acceSBarry Smith Input Parameter: 24199b94acceSBarry Smith . snes - the SNES context 24209b94acceSBarry Smith 24219b94acceSBarry Smith Output Parameter: 24229b94acceSBarry Smith . x - the solution update 24239b94acceSBarry Smith 242436851e7fSLois Curfman McInnes Level: advanced 242536851e7fSLois Curfman McInnes 24269b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 24279b94acceSBarry Smith 242885385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 24299b94acceSBarry Smith @*/ 243063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x) 24319b94acceSBarry Smith { 24323a40ed3dSBarry Smith PetscFunctionBegin; 24334482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 24344482741eSBarry Smith PetscValidPointer(x,2); 243585385478SLisandro Dalcin *x = snes->vec_sol_update; 24363a40ed3dSBarry Smith PetscFunctionReturn(0); 24379b94acceSBarry Smith } 24389b94acceSBarry Smith 24394a2ae208SSatish Balay #undef __FUNCT__ 24404a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 24419b94acceSBarry Smith /*@C 24423638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 24439b94acceSBarry Smith 2444c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2445c7afd0dbSLois Curfman McInnes 24469b94acceSBarry Smith Input Parameter: 24479b94acceSBarry Smith . snes - the SNES context 24489b94acceSBarry Smith 24499b94acceSBarry Smith Output Parameter: 24507bf4e008SBarry Smith + r - the function (or PETSC_NULL) 245170e92668SMatthew Knepley . func - the function (or PETSC_NULL) 245270e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 24539b94acceSBarry Smith 245436851e7fSLois Curfman McInnes Level: advanced 245536851e7fSLois Curfman McInnes 2456a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 24579b94acceSBarry Smith 24584b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 24599b94acceSBarry Smith @*/ 246070e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 24619b94acceSBarry Smith { 24623a40ed3dSBarry Smith PetscFunctionBegin; 24634482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 246485385478SLisandro Dalcin if (r) *r = snes->vec_func; 2465e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 246670e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 24673a40ed3dSBarry Smith PetscFunctionReturn(0); 24689b94acceSBarry Smith } 24699b94acceSBarry Smith 24704a2ae208SSatish Balay #undef __FUNCT__ 24714a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 24723c7409f5SSatish Balay /*@C 24733c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2474d850072dSLois Curfman McInnes SNES options in the database. 24753c7409f5SSatish Balay 2476fee21e36SBarry Smith Collective on SNES 2477fee21e36SBarry Smith 2478c7afd0dbSLois Curfman McInnes Input Parameter: 2479c7afd0dbSLois Curfman McInnes + snes - the SNES context 2480c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2481c7afd0dbSLois Curfman McInnes 2482d850072dSLois Curfman McInnes Notes: 2483a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2484c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2485d850072dSLois Curfman McInnes 248636851e7fSLois Curfman McInnes Level: advanced 248736851e7fSLois Curfman McInnes 24883c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2489a86d99e1SLois Curfman McInnes 2490a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 24913c7409f5SSatish Balay @*/ 249263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[]) 24933c7409f5SSatish Balay { 2494dfbe8321SBarry Smith PetscErrorCode ierr; 24953c7409f5SSatish Balay 24963a40ed3dSBarry Smith PetscFunctionBegin; 24974482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2498639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 24991cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 250094b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 25013a40ed3dSBarry Smith PetscFunctionReturn(0); 25023c7409f5SSatish Balay } 25033c7409f5SSatish Balay 25044a2ae208SSatish Balay #undef __FUNCT__ 25054a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 25063c7409f5SSatish Balay /*@C 2507f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2508d850072dSLois Curfman McInnes SNES options in the database. 25093c7409f5SSatish Balay 2510fee21e36SBarry Smith Collective on SNES 2511fee21e36SBarry Smith 2512c7afd0dbSLois Curfman McInnes Input Parameters: 2513c7afd0dbSLois Curfman McInnes + snes - the SNES context 2514c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2515c7afd0dbSLois Curfman McInnes 2516d850072dSLois Curfman McInnes Notes: 2517a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2518c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2519d850072dSLois Curfman McInnes 252036851e7fSLois Curfman McInnes Level: advanced 252136851e7fSLois Curfman McInnes 25223c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2523a86d99e1SLois Curfman McInnes 2524a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 25253c7409f5SSatish Balay @*/ 252663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 25273c7409f5SSatish Balay { 2528dfbe8321SBarry Smith PetscErrorCode ierr; 25293c7409f5SSatish Balay 25303a40ed3dSBarry Smith PetscFunctionBegin; 25314482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2532639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 25331cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 253494b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 25353a40ed3dSBarry Smith PetscFunctionReturn(0); 25363c7409f5SSatish Balay } 25373c7409f5SSatish Balay 25384a2ae208SSatish Balay #undef __FUNCT__ 25394a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 25409ab63eb5SSatish Balay /*@C 25413c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 25423c7409f5SSatish Balay SNES options in the database. 25433c7409f5SSatish Balay 2544c7afd0dbSLois Curfman McInnes Not Collective 2545c7afd0dbSLois Curfman McInnes 25463c7409f5SSatish Balay Input Parameter: 25473c7409f5SSatish Balay . snes - the SNES context 25483c7409f5SSatish Balay 25493c7409f5SSatish Balay Output Parameter: 25503c7409f5SSatish Balay . prefix - pointer to the prefix string used 25513c7409f5SSatish Balay 25524ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 25539ab63eb5SSatish Balay sufficient length to hold the prefix. 25549ab63eb5SSatish Balay 255536851e7fSLois Curfman McInnes Level: advanced 255636851e7fSLois Curfman McInnes 25573c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2558a86d99e1SLois Curfman McInnes 2559a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 25603c7409f5SSatish Balay @*/ 2561e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 25623c7409f5SSatish Balay { 2563dfbe8321SBarry Smith PetscErrorCode ierr; 25643c7409f5SSatish Balay 25653a40ed3dSBarry Smith PetscFunctionBegin; 25664482741eSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2567639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 25683a40ed3dSBarry Smith PetscFunctionReturn(0); 25693c7409f5SSatish Balay } 25703c7409f5SSatish Balay 2571b2002411SLois Curfman McInnes 25724a2ae208SSatish Balay #undef __FUNCT__ 25734a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 25743cea93caSBarry Smith /*@C 25753cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 25763cea93caSBarry Smith 25777f6c08e0SMatthew Knepley Level: advanced 25783cea93caSBarry Smith @*/ 257963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2580b2002411SLois Curfman McInnes { 2581e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2582dfbe8321SBarry Smith PetscErrorCode ierr; 2583b2002411SLois Curfman McInnes 2584b2002411SLois Curfman McInnes PetscFunctionBegin; 2585b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2586c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2587b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2588b2002411SLois Curfman McInnes } 2589da9b6338SBarry Smith 2590da9b6338SBarry Smith #undef __FUNCT__ 2591da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 259263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes) 2593da9b6338SBarry Smith { 2594dfbe8321SBarry Smith PetscErrorCode ierr; 259577431f27SBarry Smith PetscInt N,i,j; 2596da9b6338SBarry Smith Vec u,uh,fh; 2597da9b6338SBarry Smith PetscScalar value; 2598da9b6338SBarry Smith PetscReal norm; 2599da9b6338SBarry Smith 2600da9b6338SBarry Smith PetscFunctionBegin; 2601da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 2602da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 2603da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 2604da9b6338SBarry Smith 2605da9b6338SBarry Smith /* currently only works for sequential */ 2606da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 2607da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 2608da9b6338SBarry Smith for (i=0; i<N; i++) { 2609da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 261077431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 2611da9b6338SBarry Smith for (j=-10; j<11; j++) { 2612ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 2613da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 26143ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 2615da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 261677431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 2617da9b6338SBarry Smith value = -value; 2618da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 2619da9b6338SBarry Smith } 2620da9b6338SBarry Smith } 2621da9b6338SBarry Smith ierr = VecDestroy(uh);CHKERRQ(ierr); 2622da9b6338SBarry Smith ierr = VecDestroy(fh);CHKERRQ(ierr); 2623da9b6338SBarry Smith PetscFunctionReturn(0); 2624da9b6338SBarry Smith } 262571f87433Sdalcinl 262671f87433Sdalcinl #undef __FUNCT__ 2627fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 262871f87433Sdalcinl /*@ 2629fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 263071f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 263171f87433Sdalcinl Newton method. 263271f87433Sdalcinl 263371f87433Sdalcinl Collective on SNES 263471f87433Sdalcinl 263571f87433Sdalcinl Input Parameters: 263671f87433Sdalcinl + snes - SNES context 263771f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 263871f87433Sdalcinl 263964ba62caSBarry Smith Options Database: 264064ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 264164ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 264264ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 264364ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 264464ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 264564ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 264664ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 264764ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 264864ba62caSBarry Smith 264971f87433Sdalcinl Notes: 265071f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 265171f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 265271f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 265371f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 265471f87433Sdalcinl solver. 265571f87433Sdalcinl 265671f87433Sdalcinl Level: advanced 265771f87433Sdalcinl 265871f87433Sdalcinl Reference: 265971f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 266071f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 266171f87433Sdalcinl 266271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 266371f87433Sdalcinl 2664fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 266571f87433Sdalcinl @*/ 2666fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag) 266771f87433Sdalcinl { 266871f87433Sdalcinl PetscFunctionBegin; 266971f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 267071f87433Sdalcinl snes->ksp_ewconv = flag; 267171f87433Sdalcinl PetscFunctionReturn(0); 267271f87433Sdalcinl } 267371f87433Sdalcinl 267471f87433Sdalcinl #undef __FUNCT__ 2675fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 267671f87433Sdalcinl /*@ 2677fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 267871f87433Sdalcinl for computing relative tolerance for linear solvers within an 267971f87433Sdalcinl inexact Newton method. 268071f87433Sdalcinl 268171f87433Sdalcinl Not Collective 268271f87433Sdalcinl 268371f87433Sdalcinl Input Parameter: 268471f87433Sdalcinl . snes - SNES context 268571f87433Sdalcinl 268671f87433Sdalcinl Output Parameter: 268771f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 268871f87433Sdalcinl 268971f87433Sdalcinl Notes: 269071f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 269171f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 269271f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 269371f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 269471f87433Sdalcinl solver. 269571f87433Sdalcinl 269671f87433Sdalcinl Level: advanced 269771f87433Sdalcinl 269871f87433Sdalcinl Reference: 269971f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 270071f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 270171f87433Sdalcinl 270271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 270371f87433Sdalcinl 2704fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 270571f87433Sdalcinl @*/ 2706fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag) 270771f87433Sdalcinl { 270871f87433Sdalcinl PetscFunctionBegin; 270971f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 271071f87433Sdalcinl PetscValidPointer(flag,2); 271171f87433Sdalcinl *flag = snes->ksp_ewconv; 271271f87433Sdalcinl PetscFunctionReturn(0); 271371f87433Sdalcinl } 271471f87433Sdalcinl 271571f87433Sdalcinl #undef __FUNCT__ 2716fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 271771f87433Sdalcinl /*@ 2718fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 271971f87433Sdalcinl convergence criteria for the linear solvers within an inexact 272071f87433Sdalcinl Newton method. 272171f87433Sdalcinl 272271f87433Sdalcinl Collective on SNES 272371f87433Sdalcinl 272471f87433Sdalcinl Input Parameters: 272571f87433Sdalcinl + snes - SNES context 272671f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 272771f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 272871f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 272971f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 273071f87433Sdalcinl (0 <= gamma2 <= 1) 273171f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 273271f87433Sdalcinl . alpha2 - power for safeguard 273371f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 273471f87433Sdalcinl 273571f87433Sdalcinl Note: 273671f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 273771f87433Sdalcinl 273871f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 273971f87433Sdalcinl 274071f87433Sdalcinl Level: advanced 274171f87433Sdalcinl 274271f87433Sdalcinl Reference: 274371f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 274471f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 274571f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 274671f87433Sdalcinl 274771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 274871f87433Sdalcinl 2749fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 275071f87433Sdalcinl @*/ 2751fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 275271f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 275371f87433Sdalcinl { 2754fa9f3622SBarry Smith SNESKSPEW *kctx; 275571f87433Sdalcinl PetscFunctionBegin; 275671f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2757fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 275871f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 275971f87433Sdalcinl 276071f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 276171f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 276271f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 276371f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 276471f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 276571f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 276671f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 276771f87433Sdalcinl 276871f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 276971f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 277071f87433Sdalcinl } 277171f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 277271f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 277371f87433Sdalcinl } 277471f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 277571f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 277671f87433Sdalcinl } 277771f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 277871f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 277971f87433Sdalcinl } 278071f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 278171f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 278271f87433Sdalcinl } 278371f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 278471f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 278571f87433Sdalcinl } 278671f87433Sdalcinl PetscFunctionReturn(0); 278771f87433Sdalcinl } 278871f87433Sdalcinl 278971f87433Sdalcinl #undef __FUNCT__ 2790fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 279171f87433Sdalcinl /*@ 2792fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 279371f87433Sdalcinl convergence criteria for the linear solvers within an inexact 279471f87433Sdalcinl Newton method. 279571f87433Sdalcinl 279671f87433Sdalcinl Not Collective 279771f87433Sdalcinl 279871f87433Sdalcinl Input Parameters: 279971f87433Sdalcinl snes - SNES context 280071f87433Sdalcinl 280171f87433Sdalcinl Output Parameters: 280271f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 280371f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 280471f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 280571f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 280671f87433Sdalcinl (0 <= gamma2 <= 1) 280771f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 280871f87433Sdalcinl . alpha2 - power for safeguard 280971f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 281071f87433Sdalcinl 281171f87433Sdalcinl Level: advanced 281271f87433Sdalcinl 281371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 281471f87433Sdalcinl 2815fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 281671f87433Sdalcinl @*/ 2817fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 281871f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 281971f87433Sdalcinl { 2820fa9f3622SBarry Smith SNESKSPEW *kctx; 282171f87433Sdalcinl PetscFunctionBegin; 282271f87433Sdalcinl PetscValidHeaderSpecific(snes,SNES_COOKIE,1); 2823fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 282471f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 282571f87433Sdalcinl if(version) *version = kctx->version; 282671f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 282771f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 282871f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 282971f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 283071f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 283171f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 283271f87433Sdalcinl PetscFunctionReturn(0); 283371f87433Sdalcinl } 283471f87433Sdalcinl 283571f87433Sdalcinl #undef __FUNCT__ 2836fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 2837fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 283871f87433Sdalcinl { 283971f87433Sdalcinl PetscErrorCode ierr; 2840fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 284171f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 284271f87433Sdalcinl 284371f87433Sdalcinl PetscFunctionBegin; 284471f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 284571f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 284671f87433Sdalcinl rtol = kctx->rtol_0; 284771f87433Sdalcinl } else { 284871f87433Sdalcinl if (kctx->version == 1) { 284971f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 285071f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 285171f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 285271f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 285371f87433Sdalcinl } else if (kctx->version == 2) { 285471f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 285571f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 285671f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 285771f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 285871f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 285971f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 286071f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 286171f87433Sdalcinl stol = PetscMax(rtol,stol); 286271f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 286371f87433Sdalcinl /* safeguard: avoid oversolving */ 286471f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 286571f87433Sdalcinl stol = PetscMax(rtol,stol); 286671f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 286771f87433Sdalcinl } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 286871f87433Sdalcinl } 286971f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 287071f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 287171f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 287271f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 287371f87433Sdalcinl PetscFunctionReturn(0); 287471f87433Sdalcinl } 287571f87433Sdalcinl 287671f87433Sdalcinl #undef __FUNCT__ 2877fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 2878fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 287971f87433Sdalcinl { 288071f87433Sdalcinl PetscErrorCode ierr; 2881fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 288271f87433Sdalcinl PCSide pcside; 288371f87433Sdalcinl Vec lres; 288471f87433Sdalcinl 288571f87433Sdalcinl PetscFunctionBegin; 288671f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 288771f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 288871f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 288971f87433Sdalcinl if (kctx->version == 1) { 289071f87433Sdalcinl ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr); 289171f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 289271f87433Sdalcinl /* KSP residual is true linear residual */ 289371f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 289471f87433Sdalcinl } else { 289571f87433Sdalcinl /* KSP residual is preconditioned residual */ 289671f87433Sdalcinl /* compute true linear residual norm */ 289771f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 289871f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 289971f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 290071f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 290171f87433Sdalcinl ierr = VecDestroy(lres);CHKERRQ(ierr); 290271f87433Sdalcinl } 290371f87433Sdalcinl } 290471f87433Sdalcinl PetscFunctionReturn(0); 290571f87433Sdalcinl } 290671f87433Sdalcinl 290771f87433Sdalcinl #undef __FUNCT__ 290871f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 290971f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 291071f87433Sdalcinl { 291171f87433Sdalcinl PetscErrorCode ierr; 291271f87433Sdalcinl 291371f87433Sdalcinl PetscFunctionBegin; 2914fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 291571f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 2916fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 291771f87433Sdalcinl PetscFunctionReturn(0); 291871f87433Sdalcinl } 2919