19b94acceSBarry Smith 2c6db04a5SJed Brown #include <private/snesimpl.h> /*I "petscsnes.h" I*/ 39b94acceSBarry Smith 4ace3abfcSBarry Smith PetscBool SNESRegisterAllCalled = PETSC_FALSE; 58ba1e511SMatthew Knepley PetscFList SNESList = PETSC_NULL; 68ba1e511SMatthew Knepley 78ba1e511SMatthew Knepley /* Logging support */ 87087cfbeSBarry Smith PetscClassId SNES_CLASSID; 9166c7f25SBarry Smith PetscLogEvent SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval; 10a09944afSBarry Smith 11a09944afSBarry Smith #undef __FUNCT__ 12cab2e9ccSBarry Smith #define __FUNCT__ "SNESDMComputeJacobian" 13cab2e9ccSBarry Smith /* 14cab2e9ccSBarry Smith Translates from a SNES call to a DM call in computing a Jacobian 15cab2e9ccSBarry Smith */ 16cab2e9ccSBarry Smith PetscErrorCode SNESDMComputeJacobian(SNES snes,Vec X,Mat *J,Mat *B,MatStructure *flag,void *ptr) 17cab2e9ccSBarry Smith { 18cab2e9ccSBarry Smith PetscErrorCode ierr; 19cab2e9ccSBarry Smith DM dm; 20cab2e9ccSBarry Smith 21cab2e9ccSBarry Smith PetscFunctionBegin; 22cab2e9ccSBarry Smith ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 23cab2e9ccSBarry Smith ierr = DMComputeJacobian(dm,X,*J,*B,flag);CHKERRQ(ierr); 24cab2e9ccSBarry Smith PetscFunctionReturn(0); 25cab2e9ccSBarry Smith } 26cab2e9ccSBarry Smith 27cab2e9ccSBarry Smith #undef __FUNCT__ 28e113a28aSBarry Smith #define __FUNCT__ "SNESSetErrorIfNotConverged" 29e113a28aSBarry Smith /*@ 30e113a28aSBarry Smith SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged. 31e113a28aSBarry Smith 323f9fe445SBarry Smith Logically Collective on SNES 33e113a28aSBarry Smith 34e113a28aSBarry Smith Input Parameters: 35e113a28aSBarry Smith + snes - iterative context obtained from SNESCreate() 36e113a28aSBarry Smith - flg - PETSC_TRUE indicates you want the error generated 37e113a28aSBarry Smith 38e113a28aSBarry Smith Options database keys: 39e113a28aSBarry Smith . -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false) 40e113a28aSBarry Smith 41e113a28aSBarry Smith Level: intermediate 42e113a28aSBarry Smith 43e113a28aSBarry Smith Notes: 44e113a28aSBarry Smith Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve() 45e113a28aSBarry Smith to determine if it has converged. 46e113a28aSBarry Smith 47e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero 48e113a28aSBarry Smith 49e113a28aSBarry Smith .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged() 50e113a28aSBarry Smith @*/ 517087cfbeSBarry Smith PetscErrorCode SNESSetErrorIfNotConverged(SNES snes,PetscBool flg) 52e113a28aSBarry Smith { 53e113a28aSBarry Smith PetscFunctionBegin; 54e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 55acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flg,2); 56e113a28aSBarry Smith snes->errorifnotconverged = flg; 57e113a28aSBarry Smith PetscFunctionReturn(0); 58e113a28aSBarry Smith } 59e113a28aSBarry Smith 60e113a28aSBarry Smith #undef __FUNCT__ 61e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged" 62e113a28aSBarry Smith /*@ 63e113a28aSBarry Smith SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge? 64e113a28aSBarry Smith 65e113a28aSBarry Smith Not Collective 66e113a28aSBarry Smith 67e113a28aSBarry Smith Input Parameter: 68e113a28aSBarry Smith . snes - iterative context obtained from SNESCreate() 69e113a28aSBarry Smith 70e113a28aSBarry Smith Output Parameter: 71e113a28aSBarry Smith . flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE 72e113a28aSBarry Smith 73e113a28aSBarry Smith Level: intermediate 74e113a28aSBarry Smith 75e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero 76e113a28aSBarry Smith 77e113a28aSBarry Smith .seealso: SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged() 78e113a28aSBarry Smith @*/ 797087cfbeSBarry Smith PetscErrorCode SNESGetErrorIfNotConverged(SNES snes,PetscBool *flag) 80e113a28aSBarry Smith { 81e113a28aSBarry Smith PetscFunctionBegin; 82e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 83e113a28aSBarry Smith PetscValidPointer(flag,2); 84e113a28aSBarry Smith *flag = snes->errorifnotconverged; 85e113a28aSBarry Smith PetscFunctionReturn(0); 86e113a28aSBarry Smith } 87e113a28aSBarry Smith 88e113a28aSBarry Smith #undef __FUNCT__ 894936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError" 90e725d27bSBarry Smith /*@ 914936397dSBarry Smith SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not 924936397dSBarry Smith in the functions domain. For example, negative pressure. 934936397dSBarry Smith 943f9fe445SBarry Smith Logically Collective on SNES 954936397dSBarry Smith 964936397dSBarry Smith Input Parameters: 974936397dSBarry Smith . SNES - the SNES context 984936397dSBarry Smith 9928529972SSatish Balay Level: advanced 1004936397dSBarry Smith 1014936397dSBarry Smith .keywords: SNES, view 1024936397dSBarry Smith 1034936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction() 1044936397dSBarry Smith @*/ 1057087cfbeSBarry Smith PetscErrorCode SNESSetFunctionDomainError(SNES snes) 1064936397dSBarry Smith { 1074936397dSBarry Smith PetscFunctionBegin; 1080700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1094936397dSBarry Smith snes->domainerror = PETSC_TRUE; 1104936397dSBarry Smith PetscFunctionReturn(0); 1114936397dSBarry Smith } 1124936397dSBarry Smith 1134936397dSBarry Smith #undef __FUNCT__ 1144a2ae208SSatish Balay #define __FUNCT__ "SNESView" 1157e2c5f70SBarry Smith /*@C 1169b94acceSBarry Smith SNESView - Prints the SNES data structure. 1179b94acceSBarry Smith 1184c49b128SBarry Smith Collective on SNES 119fee21e36SBarry Smith 120c7afd0dbSLois Curfman McInnes Input Parameters: 121c7afd0dbSLois Curfman McInnes + SNES - the SNES context 122c7afd0dbSLois Curfman McInnes - viewer - visualization context 123c7afd0dbSLois Curfman McInnes 1249b94acceSBarry Smith Options Database Key: 125c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 1269b94acceSBarry Smith 1279b94acceSBarry Smith Notes: 1289b94acceSBarry Smith The available visualization contexts include 129b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 130b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 131c8a8ba5cSLois Curfman McInnes output where only the first processor opens 132c8a8ba5cSLois Curfman McInnes the file. All other processors send their 133c8a8ba5cSLois Curfman McInnes data to the first processor to print. 1349b94acceSBarry Smith 1353e081fefSLois Curfman McInnes The user can open an alternative visualization context with 136b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1379b94acceSBarry Smith 13836851e7fSLois Curfman McInnes Level: beginner 13936851e7fSLois Curfman McInnes 1409b94acceSBarry Smith .keywords: SNES, view 1419b94acceSBarry Smith 142b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1439b94acceSBarry Smith @*/ 1447087cfbeSBarry Smith PetscErrorCode SNESView(SNES snes,PetscViewer viewer) 1459b94acceSBarry Smith { 146fa9f3622SBarry Smith SNESKSPEW *kctx; 147dfbe8321SBarry Smith PetscErrorCode ierr; 14894b7f48cSBarry Smith KSP ksp; 149ace3abfcSBarry Smith PetscBool iascii,isstring; 1509b94acceSBarry Smith 1513a40ed3dSBarry Smith PetscFunctionBegin; 1520700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1533050cee2SBarry Smith if (!viewer) { 1547adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr); 1553050cee2SBarry Smith } 1560700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 157c9780b6fSBarry Smith PetscCheckSameComm(snes,1,viewer,2); 15874679c65SBarry Smith 1592692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1602692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 16132077d6dSBarry Smith if (iascii) { 162317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer,"SNES Object");CHKERRQ(ierr); 163e7788613SBarry Smith if (snes->ops->view) { 164b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 165e7788613SBarry Smith ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 166b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1670ef38995SBarry Smith } 16877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 169a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, solution=%G\n", 17070441072SBarry Smith snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr); 17177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr); 17277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr); 1739b94acceSBarry Smith if (snes->ksp_ewconv) { 174fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 1759b94acceSBarry Smith if (kctx) { 17677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr); 177a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 178a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 1799b94acceSBarry Smith } 1809b94acceSBarry Smith } 181eb1f6c34SBarry Smith if (snes->lagpreconditioner == -1) { 182eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is never rebuilt\n");CHKERRQ(ierr); 183eb1f6c34SBarry Smith } else if (snes->lagpreconditioner > 1) { 184eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr); 185eb1f6c34SBarry Smith } 186eb1f6c34SBarry Smith if (snes->lagjacobian == -1) { 187eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is never rebuilt\n");CHKERRQ(ierr); 188eb1f6c34SBarry Smith } else if (snes->lagjacobian > 1) { 18942f4f86dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is rebuilt every %D SNES iterations\n",snes->lagjacobian);CHKERRQ(ierr); 190eb1f6c34SBarry Smith } 1910f5bd95cSBarry Smith } else if (isstring) { 192317d6ea6SBarry Smith const char *type; 193454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 194b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 19519bcc07fSBarry Smith } 19642f4f86dSBarry Smith if (snes->pc && snes->usespc) { 1974a0c5b0cSMatthew G Knepley ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1984a0c5b0cSMatthew G Knepley ierr = SNESView(snes->pc, viewer);CHKERRQ(ierr); 1994a0c5b0cSMatthew G Knepley ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2004a0c5b0cSMatthew G Knepley } 2012c155ee1SBarry Smith if (snes->usesksp) { 2022c155ee1SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 203b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 20494b7f48cSBarry Smith ierr = KSPView(ksp,viewer);CHKERRQ(ierr); 205b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2062c155ee1SBarry Smith } 2073a40ed3dSBarry Smith PetscFunctionReturn(0); 2089b94acceSBarry Smith } 2099b94acceSBarry Smith 21076b2cf59SMatthew Knepley /* 21176b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 21276b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 21376b2cf59SMatthew Knepley */ 21476b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 215a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 2166849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 21776b2cf59SMatthew Knepley 218e74ef692SMatthew Knepley #undef __FUNCT__ 219e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker" 220ac226902SBarry Smith /*@C 22176b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 22276b2cf59SMatthew Knepley 22376b2cf59SMatthew Knepley Not Collective 22476b2cf59SMatthew Knepley 22576b2cf59SMatthew Knepley Input Parameter: 22676b2cf59SMatthew Knepley . snescheck - function that checks for options 22776b2cf59SMatthew Knepley 22876b2cf59SMatthew Knepley Level: developer 22976b2cf59SMatthew Knepley 23076b2cf59SMatthew Knepley .seealso: SNESSetFromOptions() 23176b2cf59SMatthew Knepley @*/ 2327087cfbeSBarry Smith PetscErrorCode SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 23376b2cf59SMatthew Knepley { 23476b2cf59SMatthew Knepley PetscFunctionBegin; 23576b2cf59SMatthew Knepley if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 236e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS); 23776b2cf59SMatthew Knepley } 23876b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 23976b2cf59SMatthew Knepley PetscFunctionReturn(0); 24076b2cf59SMatthew Knepley } 24176b2cf59SMatthew Knepley 2427087cfbeSBarry Smith extern PetscErrorCode SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 243aa3661deSLisandro Dalcin 244aa3661deSLisandro Dalcin #undef __FUNCT__ 245aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private" 246ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool hasOperator, PetscInt version) 247aa3661deSLisandro Dalcin { 248aa3661deSLisandro Dalcin Mat J; 249aa3661deSLisandro Dalcin KSP ksp; 250aa3661deSLisandro Dalcin PC pc; 251ace3abfcSBarry Smith PetscBool match; 252aa3661deSLisandro Dalcin PetscErrorCode ierr; 253aa3661deSLisandro Dalcin 254aa3661deSLisandro Dalcin PetscFunctionBegin; 2550700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 256aa3661deSLisandro Dalcin 25798613b67SLisandro Dalcin if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) { 25898613b67SLisandro Dalcin Mat A = snes->jacobian, B = snes->jacobian_pre; 25998613b67SLisandro Dalcin ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr); 26098613b67SLisandro Dalcin } 26198613b67SLisandro Dalcin 262aa3661deSLisandro Dalcin if (version == 1) { 263aa3661deSLisandro Dalcin ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 26498613b67SLisandro Dalcin ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 2659c6ac3b3SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 266aa3661deSLisandro Dalcin } else if (version == 2) { 267e32f2f54SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first"); 26882a7e548SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) 269aa3661deSLisandro Dalcin ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr); 270aa3661deSLisandro Dalcin #else 271e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)"); 272aa3661deSLisandro Dalcin #endif 273a8248277SBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2"); 274aa3661deSLisandro Dalcin 275aa3661deSLisandro Dalcin ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr); 276d3462f78SMatthew Knepley if (hasOperator) { 277aa3661deSLisandro Dalcin /* This version replaces the user provided Jacobian matrix with a 278aa3661deSLisandro Dalcin matrix-free version but still employs the user-provided preconditioner matrix. */ 279aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 280aa3661deSLisandro Dalcin } else { 281aa3661deSLisandro Dalcin /* This version replaces both the user-provided Jacobian and the user- 282aa3661deSLisandro Dalcin provided preconditioner matrix with the default matrix free version. */ 283aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 284aa3661deSLisandro Dalcin /* Force no preconditioner */ 285aa3661deSLisandro Dalcin ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 286aa3661deSLisandro Dalcin ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 287aa3661deSLisandro Dalcin ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr); 288aa3661deSLisandro Dalcin if (!match) { 289aa3661deSLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 290aa3661deSLisandro Dalcin ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 291aa3661deSLisandro Dalcin } 292aa3661deSLisandro Dalcin } 2936bf464f9SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 294aa3661deSLisandro Dalcin PetscFunctionReturn(0); 295aa3661deSLisandro Dalcin } 296aa3661deSLisandro Dalcin 2974a2ae208SSatish Balay #undef __FUNCT__ 2984a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions" 2999b94acceSBarry Smith /*@ 30094b7f48cSBarry Smith SNESSetFromOptions - Sets various SNES and KSP parameters from user options. 3019b94acceSBarry Smith 302c7afd0dbSLois Curfman McInnes Collective on SNES 303c7afd0dbSLois Curfman McInnes 3049b94acceSBarry Smith Input Parameter: 3059b94acceSBarry Smith . snes - the SNES context 3069b94acceSBarry Smith 30736851e7fSLois Curfman McInnes Options Database Keys: 308*ea630c6eSPeter Brune + -snes_type <type> - ls, tr, ngmres, ncg, richardson, qn, vi, fas 30982738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 31082738288SBarry Smith of the change in the solution between steps 31170441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 312b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 313b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 314b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 31550ffb88aSMatthew Knepley . -snes_max_fail <max_fail> - maximum number of failures 316ddf469c8SBarry Smith . -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops 317a8054027SBarry Smith . -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild) 318e35cf81dSBarry Smith . -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild) 319b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 3202492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 32182738288SBarry Smith solver; hence iterations will continue until max_it 3221fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 32382738288SBarry Smith of convergence test 324e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 325e8105e01SRichard Katz filename given prints to stdout 326a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 327a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 328a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 329a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 330e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 3315968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 332fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 33382738288SBarry Smith 33482738288SBarry Smith Options Database for Eisenstat-Walker method: 335fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 3364b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 33736851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 33836851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 33936851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 34036851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 34136851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 34236851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 34382738288SBarry Smith 34411ca99fdSLois Curfman McInnes Notes: 34511ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 3460598bfebSBarry Smith the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A>. 34783e2fdc7SBarry Smith 34836851e7fSLois Curfman McInnes Level: beginner 34936851e7fSLois Curfman McInnes 3509b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 3519b94acceSBarry Smith 35269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 3539b94acceSBarry Smith @*/ 3547087cfbeSBarry Smith PetscErrorCode SNESSetFromOptions(SNES snes) 3559b94acceSBarry Smith { 356*ea630c6eSPeter Brune PetscBool flg,set,mf,mf_operator,pcset; 357efd51863SBarry Smith PetscInt i,indx,lag,mf_version,grids; 358aa3661deSLisandro Dalcin MatStructure matflag; 35985385478SLisandro Dalcin const char *deft = SNESLS; 36085385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 36185385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 362e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 36351e86f29SPeter Brune const char *optionsprefix; 364649052a6SBarry Smith PetscViewer monviewer; 36585385478SLisandro Dalcin PetscErrorCode ierr; 3669b94acceSBarry Smith 3673a40ed3dSBarry Smith PetscFunctionBegin; 3680700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 369ca161407SBarry Smith 370186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 3713194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)snes);CHKERRQ(ierr); 3727adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 373b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 374d64ed03dSBarry Smith if (flg) { 375186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 3767adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 377186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 378d64ed03dSBarry Smith } 37990d69ab7SBarry Smith /* not used here, but called so will go into help messaage */ 380909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 38193c39befSBarry Smith 38257034d6fSHong Zhang ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 38357034d6fSHong Zhang ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 384186905e3SBarry Smith 38557034d6fSHong Zhang ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 386b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 387b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 38850ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 389ddf469c8SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr); 390acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr); 39185385478SLisandro Dalcin 392a8054027SBarry Smith ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr); 393a8054027SBarry Smith if (flg) { 394a8054027SBarry Smith ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr); 395a8054027SBarry Smith } 396e35cf81dSBarry Smith ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr); 397e35cf81dSBarry Smith if (flg) { 398e35cf81dSBarry Smith ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr); 399e35cf81dSBarry Smith } 400efd51863SBarry Smith ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr); 401efd51863SBarry Smith if (flg) { 402efd51863SBarry Smith ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr); 403efd51863SBarry Smith } 404a8054027SBarry Smith 40585385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 40685385478SLisandro Dalcin if (flg) { 40785385478SLisandro Dalcin switch (indx) { 4087f7931b9SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 4097f7931b9SBarry Smith case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 41085385478SLisandro Dalcin } 41185385478SLisandro Dalcin } 41285385478SLisandro Dalcin 413acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr); 414186905e3SBarry Smith 41585385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 41685385478SLisandro Dalcin 417acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 418186905e3SBarry Smith 419fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 420fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 421fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 422fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 423fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 424fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 425fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 426186905e3SBarry Smith 42790d69ab7SBarry Smith flg = PETSC_FALSE; 428acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 429a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 430eabae89aSBarry Smith 431a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 432e8105e01SRichard Katz if (flg) { 433649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr); 434649052a6SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 435e8105e01SRichard Katz } 436eabae89aSBarry Smith 437b271bb04SBarry Smith ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 438b271bb04SBarry Smith if (flg) { 439b271bb04SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr); 440b271bb04SBarry Smith } 441b271bb04SBarry Smith 442a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 443eabae89aSBarry Smith if (flg) { 444649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr); 445f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 446e8105e01SRichard Katz } 447eabae89aSBarry Smith 448a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 449eabae89aSBarry Smith if (flg) { 450649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr); 451649052a6SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 452eabae89aSBarry Smith } 453eabae89aSBarry Smith 4545180491cSLisandro Dalcin ierr = PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 4555180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)snes,monfilename);CHKERRQ(ierr);} 4565180491cSLisandro Dalcin 45790d69ab7SBarry Smith flg = PETSC_FALSE; 458acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 459a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 46090d69ab7SBarry Smith flg = PETSC_FALSE; 461acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 462a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 46390d69ab7SBarry Smith flg = PETSC_FALSE; 464acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 465a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 46690d69ab7SBarry Smith flg = PETSC_FALSE; 467acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 468a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 46990d69ab7SBarry Smith flg = PETSC_FALSE; 470acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 471b271bb04SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 472e24b481bSBarry Smith 47390d69ab7SBarry Smith flg = PETSC_FALSE; 474acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 4754b27c08aSLois Curfman McInnes if (flg) { 476186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 477ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 4789b94acceSBarry Smith } 479639f9d9dSBarry Smith 480aa3661deSLisandro Dalcin mf = mf_operator = PETSC_FALSE; 481aa3661deSLisandro Dalcin flg = PETSC_FALSE; 482acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr); 483a8248277SBarry Smith if (flg && mf_operator) { 484a8248277SBarry Smith snes->mf_operator = PETSC_TRUE; 485a8248277SBarry Smith mf = PETSC_TRUE; 486a8248277SBarry Smith } 487aa3661deSLisandro Dalcin flg = PETSC_FALSE; 488acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr); 489aa3661deSLisandro Dalcin if (!flg && mf_operator) mf = PETSC_TRUE; 490aa3661deSLisandro Dalcin mf_version = 1; 491aa3661deSLisandro Dalcin ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr); 492aa3661deSLisandro Dalcin 493*ea630c6eSPeter Brune /* line search options */ 494*ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_alpha","Constant function norm must decrease by","None",snes->ls_alpha,&snes->ls_alpha,0);CHKERRQ(ierr); 495*ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_maxstep","Step must be less than","None",snes->maxstep,&snes->maxstep,0);CHKERRQ(ierr); 496*ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_steptol","Minimum lambda allowed","None",snes->steptol,&snes->steptol,0);CHKERRQ(ierr); 497*ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_damping","Damping parameter","SNES",snes->damping,&snes->damping,&flg);CHKERRQ(ierr); 498*ea630c6eSPeter Brune ierr = PetscOptionsBool("-snes_ls_monitor","Print progress of line searches","SNESLineSearchSetMonitor",snes->ls_monitor ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 499*ea630c6eSPeter Brune if (set) {ierr = SNESLineSearchSetMonitor(snes,flg);CHKERRQ(ierr);} 500*ea630c6eSPeter Brune ierr = PetscOptionsEnum("-snes_ls","Line search used","SNESLineSearchSet",SNESLineSearchTypes,(PetscEnum)SNES_LS_CUBIC,(PetscEnum*)&indx,&flg);CHKERRQ(ierr); 501*ea630c6eSPeter Brune ierr = SNESLineSearchSetType(snes,(SNESLineSearchType)indx);CHKERRQ(ierr); 502*ea630c6eSPeter Brune 50376b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 50476b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 50576b2cf59SMatthew Knepley } 50676b2cf59SMatthew Knepley 507e7788613SBarry Smith if (snes->ops->setfromoptions) { 508e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 509639f9d9dSBarry Smith } 5105d973c19SBarry Smith 5115d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 5125d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr); 513b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 5144bbc92c1SBarry Smith 515aa3661deSLisandro Dalcin if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); } 5161cee3971SBarry Smith 5171cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 518aa3661deSLisandro Dalcin ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag); 519aa3661deSLisandro Dalcin ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr); 52085385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 52193993e2dSLois Curfman McInnes 52251e86f29SPeter Brune /* if someone has set the SNES PC type, create it. */ 52351e86f29SPeter Brune ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr); 52451e86f29SPeter Brune ierr = PetscOptionsHasName(optionsprefix, "-npc_snes_type", &pcset);CHKERRQ(ierr); 52551e86f29SPeter Brune if (pcset && (!snes->pc)) { 52651e86f29SPeter Brune ierr = SNESGetPC(snes, &snes->pc);CHKERRQ(ierr); 52751e86f29SPeter Brune } 52851e86f29SPeter Brune 5294a0c5b0cSMatthew G Knepley if (snes->pc) { 5304a0c5b0cSMatthew G Knepley ierr = SNESSetOptionsPrefix(snes->pc, "npc_");CHKERRQ(ierr); 5314a0c5b0cSMatthew G Knepley ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 5324a0c5b0cSMatthew G Knepley /* Should we make a duplicate vector and matrix? Leave the DM to make it? */ 5334a0c5b0cSMatthew G Knepley ierr = SNESSetFunction(snes->pc, snes->vec_func, snes->ops->computefunction, snes->funP);CHKERRQ(ierr); 5344a0c5b0cSMatthew G Knepley ierr = SNESSetJacobian(snes->pc, snes->jacobian, snes->jacobian_pre, snes->ops->computejacobian, snes->jacP);CHKERRQ(ierr); 5354a0c5b0cSMatthew G Knepley ierr = SNESSetFromOptions(snes->pc);CHKERRQ(ierr); 5364a0c5b0cSMatthew G Knepley } 5373a40ed3dSBarry Smith PetscFunctionReturn(0); 5389b94acceSBarry Smith } 5399b94acceSBarry Smith 540d25893d9SBarry Smith #undef __FUNCT__ 541d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext" 542d25893d9SBarry Smith /*@ 543d25893d9SBarry Smith SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for 544d25893d9SBarry Smith the nonlinear solvers. 545d25893d9SBarry Smith 546d25893d9SBarry Smith Logically Collective on SNES 547d25893d9SBarry Smith 548d25893d9SBarry Smith Input Parameters: 549d25893d9SBarry Smith + snes - the SNES context 550d25893d9SBarry Smith . compute - function to compute the context 551d25893d9SBarry Smith - destroy - function to destroy the context 552d25893d9SBarry Smith 553d25893d9SBarry Smith Level: intermediate 554d25893d9SBarry Smith 555d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context 556d25893d9SBarry Smith 557d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext() 558d25893d9SBarry Smith @*/ 559d25893d9SBarry Smith PetscErrorCode SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**)) 560d25893d9SBarry Smith { 561d25893d9SBarry Smith PetscFunctionBegin; 562d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 563d25893d9SBarry Smith snes->ops->usercompute = compute; 564d25893d9SBarry Smith snes->ops->userdestroy = destroy; 565d25893d9SBarry Smith PetscFunctionReturn(0); 566d25893d9SBarry Smith } 567a847f771SSatish Balay 5684a2ae208SSatish Balay #undef __FUNCT__ 5694a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 570b07ff414SBarry Smith /*@ 5719b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 5729b94acceSBarry Smith the nonlinear solvers. 5739b94acceSBarry Smith 5743f9fe445SBarry Smith Logically Collective on SNES 575fee21e36SBarry Smith 576c7afd0dbSLois Curfman McInnes Input Parameters: 577c7afd0dbSLois Curfman McInnes + snes - the SNES context 578c7afd0dbSLois Curfman McInnes - usrP - optional user context 579c7afd0dbSLois Curfman McInnes 58036851e7fSLois Curfman McInnes Level: intermediate 58136851e7fSLois Curfman McInnes 5829b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 5839b94acceSBarry Smith 584d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext() 5859b94acceSBarry Smith @*/ 5867087cfbeSBarry Smith PetscErrorCode SNESSetApplicationContext(SNES snes,void *usrP) 5879b94acceSBarry Smith { 5881b2093e4SBarry Smith PetscErrorCode ierr; 589b07ff414SBarry Smith KSP ksp; 5901b2093e4SBarry Smith 5913a40ed3dSBarry Smith PetscFunctionBegin; 5920700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 593b07ff414SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 594b07ff414SBarry Smith ierr = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr); 5959b94acceSBarry Smith snes->user = usrP; 5963a40ed3dSBarry Smith PetscFunctionReturn(0); 5979b94acceSBarry Smith } 59874679c65SBarry Smith 5994a2ae208SSatish Balay #undef __FUNCT__ 6004a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 601b07ff414SBarry Smith /*@ 6029b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 6039b94acceSBarry Smith nonlinear solvers. 6049b94acceSBarry Smith 605c7afd0dbSLois Curfman McInnes Not Collective 606c7afd0dbSLois Curfman McInnes 6079b94acceSBarry Smith Input Parameter: 6089b94acceSBarry Smith . snes - SNES context 6099b94acceSBarry Smith 6109b94acceSBarry Smith Output Parameter: 6119b94acceSBarry Smith . usrP - user context 6129b94acceSBarry Smith 61336851e7fSLois Curfman McInnes Level: intermediate 61436851e7fSLois Curfman McInnes 6159b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 6169b94acceSBarry Smith 6179b94acceSBarry Smith .seealso: SNESSetApplicationContext() 6189b94acceSBarry Smith @*/ 619e71120c6SJed Brown PetscErrorCode SNESGetApplicationContext(SNES snes,void *usrP) 6209b94acceSBarry Smith { 6213a40ed3dSBarry Smith PetscFunctionBegin; 6220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 623e71120c6SJed Brown *(void**)usrP = snes->user; 6243a40ed3dSBarry Smith PetscFunctionReturn(0); 6259b94acceSBarry Smith } 62674679c65SBarry Smith 6274a2ae208SSatish Balay #undef __FUNCT__ 6284a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 6299b94acceSBarry Smith /*@ 630c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 631c8228a4eSBarry Smith at this time. 6329b94acceSBarry Smith 633c7afd0dbSLois Curfman McInnes Not Collective 634c7afd0dbSLois Curfman McInnes 6359b94acceSBarry Smith Input Parameter: 6369b94acceSBarry Smith . snes - SNES context 6379b94acceSBarry Smith 6389b94acceSBarry Smith Output Parameter: 6399b94acceSBarry Smith . iter - iteration number 6409b94acceSBarry Smith 641c8228a4eSBarry Smith Notes: 642c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 643c8228a4eSBarry Smith 644c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 64508405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 64608405cd6SLois Curfman McInnes .vb 64708405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 64808405cd6SLois Curfman McInnes if (!(it % 2)) { 64908405cd6SLois Curfman McInnes [compute Jacobian here] 65008405cd6SLois Curfman McInnes } 65108405cd6SLois Curfman McInnes .ve 652c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 65308405cd6SLois Curfman McInnes recomputed every second SNES iteration. 654c8228a4eSBarry Smith 65536851e7fSLois Curfman McInnes Level: intermediate 65636851e7fSLois Curfman McInnes 6572b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 6582b668275SBarry Smith 659b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 6609b94acceSBarry Smith @*/ 6617087cfbeSBarry Smith PetscErrorCode SNESGetIterationNumber(SNES snes,PetscInt* iter) 6629b94acceSBarry Smith { 6633a40ed3dSBarry Smith PetscFunctionBegin; 6640700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6654482741eSBarry Smith PetscValidIntPointer(iter,2); 6669b94acceSBarry Smith *iter = snes->iter; 6673a40ed3dSBarry Smith PetscFunctionReturn(0); 6689b94acceSBarry Smith } 66974679c65SBarry Smith 6704a2ae208SSatish Balay #undef __FUNCT__ 6714a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 6729b94acceSBarry Smith /*@ 6739b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 6749b94acceSBarry Smith with SNESSSetFunction(). 6759b94acceSBarry Smith 676c7afd0dbSLois Curfman McInnes Collective on SNES 677c7afd0dbSLois Curfman McInnes 6789b94acceSBarry Smith Input Parameter: 6799b94acceSBarry Smith . snes - SNES context 6809b94acceSBarry Smith 6819b94acceSBarry Smith Output Parameter: 6829b94acceSBarry Smith . fnorm - 2-norm of function 6839b94acceSBarry Smith 68436851e7fSLois Curfman McInnes Level: intermediate 68536851e7fSLois Curfman McInnes 6869b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 687a86d99e1SLois Curfman McInnes 688b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 6899b94acceSBarry Smith @*/ 6907087cfbeSBarry Smith PetscErrorCode SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 6919b94acceSBarry Smith { 6923a40ed3dSBarry Smith PetscFunctionBegin; 6930700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6944482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 6959b94acceSBarry Smith *fnorm = snes->norm; 6963a40ed3dSBarry Smith PetscFunctionReturn(0); 6979b94acceSBarry Smith } 69874679c65SBarry Smith 6994a2ae208SSatish Balay #undef __FUNCT__ 700b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 7019b94acceSBarry Smith /*@ 702b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 7039b94acceSBarry Smith attempted by the nonlinear solver. 7049b94acceSBarry Smith 705c7afd0dbSLois Curfman McInnes Not Collective 706c7afd0dbSLois Curfman McInnes 7079b94acceSBarry Smith Input Parameter: 7089b94acceSBarry Smith . snes - SNES context 7099b94acceSBarry Smith 7109b94acceSBarry Smith Output Parameter: 7119b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 7129b94acceSBarry Smith 713c96a6f78SLois Curfman McInnes Notes: 714c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 715c96a6f78SLois Curfman McInnes 71636851e7fSLois Curfman McInnes Level: intermediate 71736851e7fSLois Curfman McInnes 7189b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 71958ebbce7SBarry Smith 720e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 72158ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 7229b94acceSBarry Smith @*/ 7237087cfbeSBarry Smith PetscErrorCode SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 7249b94acceSBarry Smith { 7253a40ed3dSBarry Smith PetscFunctionBegin; 7260700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7274482741eSBarry Smith PetscValidIntPointer(nfails,2); 72850ffb88aSMatthew Knepley *nfails = snes->numFailures; 72950ffb88aSMatthew Knepley PetscFunctionReturn(0); 73050ffb88aSMatthew Knepley } 73150ffb88aSMatthew Knepley 73250ffb88aSMatthew Knepley #undef __FUNCT__ 733b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 73450ffb88aSMatthew Knepley /*@ 735b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 73650ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 73750ffb88aSMatthew Knepley 73850ffb88aSMatthew Knepley Not Collective 73950ffb88aSMatthew Knepley 74050ffb88aSMatthew Knepley Input Parameters: 74150ffb88aSMatthew Knepley + snes - SNES context 74250ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 74350ffb88aSMatthew Knepley 74450ffb88aSMatthew Knepley Level: intermediate 74550ffb88aSMatthew Knepley 74650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 74758ebbce7SBarry Smith 748e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 74958ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 75050ffb88aSMatthew Knepley @*/ 7517087cfbeSBarry Smith PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 75250ffb88aSMatthew Knepley { 75350ffb88aSMatthew Knepley PetscFunctionBegin; 7540700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 75550ffb88aSMatthew Knepley snes->maxFailures = maxFails; 75650ffb88aSMatthew Knepley PetscFunctionReturn(0); 75750ffb88aSMatthew Knepley } 75850ffb88aSMatthew Knepley 75950ffb88aSMatthew Knepley #undef __FUNCT__ 760b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 76150ffb88aSMatthew Knepley /*@ 762b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 76350ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 76450ffb88aSMatthew Knepley 76550ffb88aSMatthew Knepley Not Collective 76650ffb88aSMatthew Knepley 76750ffb88aSMatthew Knepley Input Parameter: 76850ffb88aSMatthew Knepley . snes - SNES context 76950ffb88aSMatthew Knepley 77050ffb88aSMatthew Knepley Output Parameter: 77150ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 77250ffb88aSMatthew Knepley 77350ffb88aSMatthew Knepley Level: intermediate 77450ffb88aSMatthew Knepley 77550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 77658ebbce7SBarry Smith 777e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 77858ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 77958ebbce7SBarry Smith 78050ffb88aSMatthew Knepley @*/ 7817087cfbeSBarry Smith PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 78250ffb88aSMatthew Knepley { 78350ffb88aSMatthew Knepley PetscFunctionBegin; 7840700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7854482741eSBarry Smith PetscValidIntPointer(maxFails,2); 78650ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 7873a40ed3dSBarry Smith PetscFunctionReturn(0); 7889b94acceSBarry Smith } 789a847f771SSatish Balay 7904a2ae208SSatish Balay #undef __FUNCT__ 7912541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 7922541af92SBarry Smith /*@ 7932541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 7942541af92SBarry Smith done by SNES. 7952541af92SBarry Smith 7962541af92SBarry Smith Not Collective 7972541af92SBarry Smith 7982541af92SBarry Smith Input Parameter: 7992541af92SBarry Smith . snes - SNES context 8002541af92SBarry Smith 8012541af92SBarry Smith Output Parameter: 8022541af92SBarry Smith . nfuncs - number of evaluations 8032541af92SBarry Smith 8042541af92SBarry Smith Level: intermediate 8052541af92SBarry Smith 8062541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 80758ebbce7SBarry Smith 808e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 8092541af92SBarry Smith @*/ 8107087cfbeSBarry Smith PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 8112541af92SBarry Smith { 8122541af92SBarry Smith PetscFunctionBegin; 8130700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8142541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 8152541af92SBarry Smith *nfuncs = snes->nfuncs; 8162541af92SBarry Smith PetscFunctionReturn(0); 8172541af92SBarry Smith } 8182541af92SBarry Smith 8192541af92SBarry Smith #undef __FUNCT__ 8203d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 8213d4c4710SBarry Smith /*@ 8223d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 8233d4c4710SBarry Smith linear solvers. 8243d4c4710SBarry Smith 8253d4c4710SBarry Smith Not Collective 8263d4c4710SBarry Smith 8273d4c4710SBarry Smith Input Parameter: 8283d4c4710SBarry Smith . snes - SNES context 8293d4c4710SBarry Smith 8303d4c4710SBarry Smith Output Parameter: 8313d4c4710SBarry Smith . nfails - number of failed solves 8323d4c4710SBarry Smith 8333d4c4710SBarry Smith Notes: 8343d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 8353d4c4710SBarry Smith 8363d4c4710SBarry Smith Level: intermediate 8373d4c4710SBarry Smith 8383d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 83958ebbce7SBarry Smith 840e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 8413d4c4710SBarry Smith @*/ 8427087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 8433d4c4710SBarry Smith { 8443d4c4710SBarry Smith PetscFunctionBegin; 8450700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8463d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 8473d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 8483d4c4710SBarry Smith PetscFunctionReturn(0); 8493d4c4710SBarry Smith } 8503d4c4710SBarry Smith 8513d4c4710SBarry Smith #undef __FUNCT__ 8523d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 8533d4c4710SBarry Smith /*@ 8543d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 8553d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 8563d4c4710SBarry Smith 8573f9fe445SBarry Smith Logically Collective on SNES 8583d4c4710SBarry Smith 8593d4c4710SBarry Smith Input Parameters: 8603d4c4710SBarry Smith + snes - SNES context 8613d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 8623d4c4710SBarry Smith 8633d4c4710SBarry Smith Level: intermediate 8643d4c4710SBarry Smith 865a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 8663d4c4710SBarry Smith 8673d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 8683d4c4710SBarry Smith 86958ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 8703d4c4710SBarry Smith @*/ 8717087cfbeSBarry Smith PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 8723d4c4710SBarry Smith { 8733d4c4710SBarry Smith PetscFunctionBegin; 8740700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 875c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxFails,2); 8763d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 8773d4c4710SBarry Smith PetscFunctionReturn(0); 8783d4c4710SBarry Smith } 8793d4c4710SBarry Smith 8803d4c4710SBarry Smith #undef __FUNCT__ 8813d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 8823d4c4710SBarry Smith /*@ 8833d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 8843d4c4710SBarry Smith are allowed before SNES terminates 8853d4c4710SBarry Smith 8863d4c4710SBarry Smith Not Collective 8873d4c4710SBarry Smith 8883d4c4710SBarry Smith Input Parameter: 8893d4c4710SBarry Smith . snes - SNES context 8903d4c4710SBarry Smith 8913d4c4710SBarry Smith Output Parameter: 8923d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 8933d4c4710SBarry Smith 8943d4c4710SBarry Smith Level: intermediate 8953d4c4710SBarry Smith 8963d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 8973d4c4710SBarry Smith 8983d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 8993d4c4710SBarry Smith 900e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 9013d4c4710SBarry Smith @*/ 9027087cfbeSBarry Smith PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 9033d4c4710SBarry Smith { 9043d4c4710SBarry Smith PetscFunctionBegin; 9050700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9063d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 9073d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 9083d4c4710SBarry Smith PetscFunctionReturn(0); 9093d4c4710SBarry Smith } 9103d4c4710SBarry Smith 9113d4c4710SBarry Smith #undef __FUNCT__ 912b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 913c96a6f78SLois Curfman McInnes /*@ 914b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 915c96a6f78SLois Curfman McInnes used by the nonlinear solver. 916c96a6f78SLois Curfman McInnes 917c7afd0dbSLois Curfman McInnes Not Collective 918c7afd0dbSLois Curfman McInnes 919c96a6f78SLois Curfman McInnes Input Parameter: 920c96a6f78SLois Curfman McInnes . snes - SNES context 921c96a6f78SLois Curfman McInnes 922c96a6f78SLois Curfman McInnes Output Parameter: 923c96a6f78SLois Curfman McInnes . lits - number of linear iterations 924c96a6f78SLois Curfman McInnes 925c96a6f78SLois Curfman McInnes Notes: 926c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 927c96a6f78SLois Curfman McInnes 92836851e7fSLois Curfman McInnes Level: intermediate 92936851e7fSLois Curfman McInnes 930c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 9312b668275SBarry Smith 9328c7482ecSBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 933c96a6f78SLois Curfman McInnes @*/ 9347087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 935c96a6f78SLois Curfman McInnes { 9363a40ed3dSBarry Smith PetscFunctionBegin; 9370700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9384482741eSBarry Smith PetscValidIntPointer(lits,2); 939c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 9403a40ed3dSBarry Smith PetscFunctionReturn(0); 941c96a6f78SLois Curfman McInnes } 942c96a6f78SLois Curfman McInnes 9434a2ae208SSatish Balay #undef __FUNCT__ 94494b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 94552baeb72SSatish Balay /*@ 94694b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 9479b94acceSBarry Smith 94894b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 949c7afd0dbSLois Curfman McInnes 9509b94acceSBarry Smith Input Parameter: 9519b94acceSBarry Smith . snes - the SNES context 9529b94acceSBarry Smith 9539b94acceSBarry Smith Output Parameter: 95494b7f48cSBarry Smith . ksp - the KSP context 9559b94acceSBarry Smith 9569b94acceSBarry Smith Notes: 95794b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 9589b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 9592999313aSBarry Smith PC contexts as well. 9609b94acceSBarry Smith 96136851e7fSLois Curfman McInnes Level: beginner 96236851e7fSLois Curfman McInnes 96394b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 9649b94acceSBarry Smith 9652999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 9669b94acceSBarry Smith @*/ 9677087cfbeSBarry Smith PetscErrorCode SNESGetKSP(SNES snes,KSP *ksp) 9689b94acceSBarry Smith { 9691cee3971SBarry Smith PetscErrorCode ierr; 9701cee3971SBarry Smith 9713a40ed3dSBarry Smith PetscFunctionBegin; 9720700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9734482741eSBarry Smith PetscValidPointer(ksp,2); 9741cee3971SBarry Smith 9751cee3971SBarry Smith if (!snes->ksp) { 9761cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 9771cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 9781cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 9791cee3971SBarry Smith } 98094b7f48cSBarry Smith *ksp = snes->ksp; 9813a40ed3dSBarry Smith PetscFunctionReturn(0); 9829b94acceSBarry Smith } 98382bf6240SBarry Smith 9844a2ae208SSatish Balay #undef __FUNCT__ 9852999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 9862999313aSBarry Smith /*@ 9872999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 9882999313aSBarry Smith 9892999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 9902999313aSBarry Smith 9912999313aSBarry Smith Input Parameters: 9922999313aSBarry Smith + snes - the SNES context 9932999313aSBarry Smith - ksp - the KSP context 9942999313aSBarry Smith 9952999313aSBarry Smith Notes: 9962999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 9972999313aSBarry Smith so this routine is rarely needed. 9982999313aSBarry Smith 9992999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 10002999313aSBarry Smith decreased by one. 10012999313aSBarry Smith 10022999313aSBarry Smith Level: developer 10032999313aSBarry Smith 10042999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 10052999313aSBarry Smith 10062999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 10072999313aSBarry Smith @*/ 10087087cfbeSBarry Smith PetscErrorCode SNESSetKSP(SNES snes,KSP ksp) 10092999313aSBarry Smith { 10102999313aSBarry Smith PetscErrorCode ierr; 10112999313aSBarry Smith 10122999313aSBarry Smith PetscFunctionBegin; 10130700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10140700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 10152999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 10167dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 1017906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 10182999313aSBarry Smith snes->ksp = ksp; 10192999313aSBarry Smith PetscFunctionReturn(0); 10202999313aSBarry Smith } 10212999313aSBarry Smith 10227adad957SLisandro Dalcin #if 0 10232999313aSBarry Smith #undef __FUNCT__ 10244a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 10256849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 1026e24b481bSBarry Smith { 1027e24b481bSBarry Smith PetscFunctionBegin; 1028e24b481bSBarry Smith PetscFunctionReturn(0); 1029e24b481bSBarry Smith } 10307adad957SLisandro Dalcin #endif 1031e24b481bSBarry Smith 10329b94acceSBarry Smith /* -----------------------------------------------------------*/ 10334a2ae208SSatish Balay #undef __FUNCT__ 10344a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 103552baeb72SSatish Balay /*@ 10369b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 10379b94acceSBarry Smith 1038c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 1039c7afd0dbSLois Curfman McInnes 1040c7afd0dbSLois Curfman McInnes Input Parameters: 1041906ed7ccSBarry Smith . comm - MPI communicator 10429b94acceSBarry Smith 10439b94acceSBarry Smith Output Parameter: 10449b94acceSBarry Smith . outsnes - the new SNES context 10459b94acceSBarry Smith 1046c7afd0dbSLois Curfman McInnes Options Database Keys: 1047c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 1048c7afd0dbSLois Curfman McInnes and no preconditioning matrix 1049c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 1050c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 1051c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 1052c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 1053c1f60f51SBarry Smith 105436851e7fSLois Curfman McInnes Level: beginner 105536851e7fSLois Curfman McInnes 10569b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 10579b94acceSBarry Smith 1058a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 1059a8054027SBarry Smith 10609b94acceSBarry Smith @*/ 10617087cfbeSBarry Smith PetscErrorCode SNESCreate(MPI_Comm comm,SNES *outsnes) 10629b94acceSBarry Smith { 1063dfbe8321SBarry Smith PetscErrorCode ierr; 10649b94acceSBarry Smith SNES snes; 1065fa9f3622SBarry Smith SNESKSPEW *kctx; 106637fcc0dbSBarry Smith 10673a40ed3dSBarry Smith PetscFunctionBegin; 1068ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 10698ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 10708ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 10718ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 10728ba1e511SMatthew Knepley #endif 10738ba1e511SMatthew Knepley 10743194b578SJed Brown ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 10757adad957SLisandro Dalcin 107685385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 10772c155ee1SBarry Smith snes->usesksp = PETSC_TRUE; 10789b94acceSBarry Smith snes->max_its = 50; 10799750a799SBarry Smith snes->max_funcs = 10000; 10809b94acceSBarry Smith snes->norm = 0.0; 1081b4874afaSBarry Smith snes->rtol = 1.e-8; 1082b4874afaSBarry Smith snes->ttol = 0.0; 108370441072SBarry Smith snes->abstol = 1.e-50; 10849b94acceSBarry Smith snes->xtol = 1.e-8; 10854b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 10869b94acceSBarry Smith snes->nfuncs = 0; 108750ffb88aSMatthew Knepley snes->numFailures = 0; 108850ffb88aSMatthew Knepley snes->maxFailures = 1; 10897a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1090e35cf81dSBarry Smith snes->lagjacobian = 1; 1091a8054027SBarry Smith snes->lagpreconditioner = 1; 1092639f9d9dSBarry Smith snes->numbermonitors = 0; 10939b94acceSBarry Smith snes->data = 0; 10944dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1095186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 10966f24a144SLois Curfman McInnes snes->nwork = 0; 109758c9b817SLisandro Dalcin snes->work = 0; 109858c9b817SLisandro Dalcin snes->nvwork = 0; 109958c9b817SLisandro Dalcin snes->vwork = 0; 1100758f92a0SBarry Smith snes->conv_hist_len = 0; 1101758f92a0SBarry Smith snes->conv_hist_max = 0; 1102758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 1103758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 1104758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1105184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 11069b94acceSBarry Smith 1107*ea630c6eSPeter Brune /* initialize the line search options */ 1108*ea630c6eSPeter Brune snes->ls_type = SNES_LS_BASIC; 1109*ea630c6eSPeter Brune snes->damping = 1.0; 1110*ea630c6eSPeter Brune snes->maxstep = 1e8; 1111*ea630c6eSPeter Brune snes->steptol = 1e-12; 1112*ea630c6eSPeter Brune snes->ls_alpha = 1e-4; 1113*ea630c6eSPeter Brune snes->ls_monitor = PETSC_NULL; 1114*ea630c6eSPeter Brune 1115*ea630c6eSPeter Brune snes->ops->linesearch = PETSC_NULL; 1116*ea630c6eSPeter Brune snes->precheck = PETSC_NULL; 1117*ea630c6eSPeter Brune snes->ops->precheckstep = PETSC_NULL; 1118*ea630c6eSPeter Brune snes->postcheck = PETSC_NULL; 1119*ea630c6eSPeter Brune snes->ops->postcheckstep= PETSC_NULL; 1120*ea630c6eSPeter Brune 1121*ea630c6eSPeter Brune snes->ops->linesearchquadratic = PETSC_NULL; 1122*ea630c6eSPeter Brune snes->ops->linesearchcubic = PETSC_NULL; 1123*ea630c6eSPeter Brune snes->ops->linesearchno = PETSC_NULL; 1124*ea630c6eSPeter Brune snes->ops->linesearchnonorms = PETSC_NULL; 1125*ea630c6eSPeter Brune snes->ops->linesearchexact = PETSC_NULL; 1126*ea630c6eSPeter Brune snes->ops->linesearchtest = PETSC_NULL; 1127*ea630c6eSPeter Brune 11283d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 11293d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 11303d4c4710SBarry Smith 11319b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 113238f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 11339b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 11349b94acceSBarry Smith kctx->version = 2; 11359b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 11369b94acceSBarry Smith this was too large for some test cases */ 113775567043SBarry Smith kctx->rtol_last = 0.0; 11389b94acceSBarry Smith kctx->rtol_max = .9; 11399b94acceSBarry Smith kctx->gamma = 1.0; 114071f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 114171f87433Sdalcinl kctx->alpha2 = kctx->alpha; 11429b94acceSBarry Smith kctx->threshold = .1; 114375567043SBarry Smith kctx->lresid_last = 0.0; 114475567043SBarry Smith kctx->norm_last = 0.0; 11459b94acceSBarry Smith 11469b94acceSBarry Smith *outsnes = snes; 11473a40ed3dSBarry Smith PetscFunctionReturn(0); 11489b94acceSBarry Smith } 11499b94acceSBarry Smith 11504a2ae208SSatish Balay #undef __FUNCT__ 11514a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 11529b94acceSBarry Smith /*@C 11539b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 11549b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 11559b94acceSBarry Smith equations. 11569b94acceSBarry Smith 11573f9fe445SBarry Smith Logically Collective on SNES 1158fee21e36SBarry Smith 1159c7afd0dbSLois Curfman McInnes Input Parameters: 1160c7afd0dbSLois Curfman McInnes + snes - the SNES context 1161c7afd0dbSLois Curfman McInnes . r - vector to store function value 1162de044059SHong Zhang . func - function evaluation routine 1163c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1164c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 11659b94acceSBarry Smith 1166c7afd0dbSLois Curfman McInnes Calling sequence of func: 11678d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 1168c7afd0dbSLois Curfman McInnes 1169313e4042SLois Curfman McInnes . f - function vector 1170c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 11719b94acceSBarry Smith 11729b94acceSBarry Smith Notes: 11739b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 11749b94acceSBarry Smith $ f'(x) x = -f(x), 1175c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 11769b94acceSBarry Smith 117736851e7fSLois Curfman McInnes Level: beginner 117836851e7fSLois Curfman McInnes 11799b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 11809b94acceSBarry Smith 1181a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 11829b94acceSBarry Smith @*/ 11837087cfbeSBarry Smith PetscErrorCode SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 11849b94acceSBarry Smith { 118585385478SLisandro Dalcin PetscErrorCode ierr; 11863a40ed3dSBarry Smith PetscFunctionBegin; 11870700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1188d2a683ecSLisandro Dalcin if (r) { 1189d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1190d2a683ecSLisandro Dalcin PetscCheckSameComm(snes,1,r,2); 119185385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 11926bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 119385385478SLisandro Dalcin snes->vec_func = r; 1194d2a683ecSLisandro Dalcin } else if (!snes->vec_func && snes->dm) { 1195d2a683ecSLisandro Dalcin ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1196d2a683ecSLisandro Dalcin } 1197d2a683ecSLisandro Dalcin if (func) snes->ops->computefunction = func; 1198d2a683ecSLisandro Dalcin if (ctx) snes->funP = ctx; 11993a40ed3dSBarry Smith PetscFunctionReturn(0); 12009b94acceSBarry Smith } 12019b94acceSBarry Smith 1202d25893d9SBarry Smith #undef __FUNCT__ 1203d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess" 1204d25893d9SBarry Smith /*@C 1205d25893d9SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem 1206d25893d9SBarry Smith 1207d25893d9SBarry Smith Logically Collective on SNES 1208d25893d9SBarry Smith 1209d25893d9SBarry Smith Input Parameters: 1210d25893d9SBarry Smith + snes - the SNES context 1211d25893d9SBarry Smith . func - function evaluation routine 1212d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 1213d25893d9SBarry Smith function evaluation routine (may be PETSC_NULL) 1214d25893d9SBarry Smith 1215d25893d9SBarry Smith Calling sequence of func: 1216d25893d9SBarry Smith $ func (SNES snes,Vec x,void *ctx); 1217d25893d9SBarry Smith 1218d25893d9SBarry Smith . f - function vector 1219d25893d9SBarry Smith - ctx - optional user-defined function context 1220d25893d9SBarry Smith 1221d25893d9SBarry Smith Level: intermediate 1222d25893d9SBarry Smith 1223d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function 1224d25893d9SBarry Smith 1225d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 1226d25893d9SBarry Smith @*/ 1227d25893d9SBarry Smith PetscErrorCode SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx) 1228d25893d9SBarry Smith { 1229d25893d9SBarry Smith PetscFunctionBegin; 1230d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1231d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 1232d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 1233d25893d9SBarry Smith PetscFunctionReturn(0); 1234d25893d9SBarry Smith } 1235d25893d9SBarry Smith 12363ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 12373ab0aad5SBarry Smith #undef __FUNCT__ 12381096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 12391096aae1SMatthew Knepley /*@C 12401096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 12411096aae1SMatthew Knepley it assumes a zero right hand side. 12421096aae1SMatthew Knepley 12433f9fe445SBarry Smith Logically Collective on SNES 12441096aae1SMatthew Knepley 12451096aae1SMatthew Knepley Input Parameter: 12461096aae1SMatthew Knepley . snes - the SNES context 12471096aae1SMatthew Knepley 12481096aae1SMatthew Knepley Output Parameter: 1249bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 12501096aae1SMatthew Knepley 12511096aae1SMatthew Knepley Level: intermediate 12521096aae1SMatthew Knepley 12531096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 12541096aae1SMatthew Knepley 125585385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 12561096aae1SMatthew Knepley @*/ 12577087cfbeSBarry Smith PetscErrorCode SNESGetRhs(SNES snes,Vec *rhs) 12581096aae1SMatthew Knepley { 12591096aae1SMatthew Knepley PetscFunctionBegin; 12600700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12611096aae1SMatthew Knepley PetscValidPointer(rhs,2); 126285385478SLisandro Dalcin *rhs = snes->vec_rhs; 12631096aae1SMatthew Knepley PetscFunctionReturn(0); 12641096aae1SMatthew Knepley } 12651096aae1SMatthew Knepley 12661096aae1SMatthew Knepley #undef __FUNCT__ 12674a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 12689b94acceSBarry Smith /*@ 126936851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 12709b94acceSBarry Smith SNESSetFunction(). 12719b94acceSBarry Smith 1272c7afd0dbSLois Curfman McInnes Collective on SNES 1273c7afd0dbSLois Curfman McInnes 12749b94acceSBarry Smith Input Parameters: 1275c7afd0dbSLois Curfman McInnes + snes - the SNES context 1276c7afd0dbSLois Curfman McInnes - x - input vector 12779b94acceSBarry Smith 12789b94acceSBarry Smith Output Parameter: 12793638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 12809b94acceSBarry Smith 12811bffabb2SLois Curfman McInnes Notes: 128236851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 128336851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 128436851e7fSLois Curfman McInnes themselves. 128536851e7fSLois Curfman McInnes 128636851e7fSLois Curfman McInnes Level: developer 128736851e7fSLois Curfman McInnes 12889b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 12899b94acceSBarry Smith 1290a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 12919b94acceSBarry Smith @*/ 12927087cfbeSBarry Smith PetscErrorCode SNESComputeFunction(SNES snes,Vec x,Vec y) 12939b94acceSBarry Smith { 1294dfbe8321SBarry Smith PetscErrorCode ierr; 12959b94acceSBarry Smith 12963a40ed3dSBarry Smith PetscFunctionBegin; 12970700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12980700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 12990700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 1300c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1301c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 1302184914b5SBarry Smith 1303d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1304e7788613SBarry Smith if (snes->ops->computefunction) { 1305d64ed03dSBarry Smith PetscStackPush("SNES user function"); 130639d508bbSBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1307d64ed03dSBarry Smith PetscStackPop; 130873250ac0SBarry Smith } else if (snes->dm) { 1309644e2e5bSBarry Smith ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr); 1310c90fad12SPeter Brune } else if (snes->vec_rhs) { 1311c90fad12SPeter Brune ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 1312644e2e5bSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve()."); 131385385478SLisandro Dalcin if (snes->vec_rhs) { 131485385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 13153ab0aad5SBarry Smith } 1316ae3c334cSLois Curfman McInnes snes->nfuncs++; 1317d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 13183a40ed3dSBarry Smith PetscFunctionReturn(0); 13199b94acceSBarry Smith } 13209b94acceSBarry Smith 13214a2ae208SSatish Balay #undef __FUNCT__ 13224a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 132362fef451SLois Curfman McInnes /*@ 132462fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 132562fef451SLois Curfman McInnes set with SNESSetJacobian(). 132662fef451SLois Curfman McInnes 1327c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1328c7afd0dbSLois Curfman McInnes 132962fef451SLois Curfman McInnes Input Parameters: 1330c7afd0dbSLois Curfman McInnes + snes - the SNES context 1331c7afd0dbSLois Curfman McInnes - x - input vector 133262fef451SLois Curfman McInnes 133362fef451SLois Curfman McInnes Output Parameters: 1334c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 133562fef451SLois Curfman McInnes . B - optional preconditioning matrix 13362b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1337fee21e36SBarry Smith 1338e35cf81dSBarry Smith Options Database Keys: 1339e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1340693365a8SJed Brown . -snes_lag_jacobian <lag> 1341693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 1342693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 1343693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 1344693365a8SJed Brown - -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 1345e35cf81dSBarry Smith 134662fef451SLois Curfman McInnes Notes: 134762fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 134862fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 134962fef451SLois Curfman McInnes 135094b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1351dc5a77f8SLois Curfman McInnes flag parameter. 135262fef451SLois Curfman McInnes 135336851e7fSLois Curfman McInnes Level: developer 135436851e7fSLois Curfman McInnes 135562fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 135662fef451SLois Curfman McInnes 1357e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 135862fef451SLois Curfman McInnes @*/ 13597087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 13609b94acceSBarry Smith { 1361dfbe8321SBarry Smith PetscErrorCode ierr; 1362ace3abfcSBarry Smith PetscBool flag; 13633a40ed3dSBarry Smith 13643a40ed3dSBarry Smith PetscFunctionBegin; 13650700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13660700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 13674482741eSBarry Smith PetscValidPointer(flg,5); 1368c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1369e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1370ebd3b9afSBarry Smith 1371ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1372ebd3b9afSBarry Smith 1373fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1374fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1375fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1376fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1377e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1378e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1379ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1380ebd3b9afSBarry Smith if (flag) { 1381ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1382ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1383ebd3b9afSBarry Smith } 1384e35cf81dSBarry Smith PetscFunctionReturn(0); 1385e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1386e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1387e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1388ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1389ebd3b9afSBarry Smith if (flag) { 1390ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1391ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1392ebd3b9afSBarry Smith } 1393e35cf81dSBarry Smith PetscFunctionReturn(0); 1394e35cf81dSBarry Smith } 1395e35cf81dSBarry Smith 1396c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1397e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1398d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1399e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1400d64ed03dSBarry Smith PetscStackPop; 1401d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1402a8054027SBarry Smith 14033b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 14043b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 14053b4f5425SBarry Smith snes->lagpreconditioner = -1; 14063b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1407a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1408a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1409a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1410a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1411a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1412a8054027SBarry Smith } 1413a8054027SBarry Smith 14146d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 14150700a824SBarry Smith /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 14160700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,4); */ 1417693365a8SJed Brown { 1418693365a8SJed Brown PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE; 1419693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr); 1420693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 1421693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 1422693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr); 1423693365a8SJed Brown if (flag || flag_draw || flag_contour) { 1424693365a8SJed Brown Mat Bexp_mine = PETSC_NULL,Bexp,FDexp; 1425693365a8SJed Brown MatStructure mstruct; 1426693365a8SJed Brown PetscViewer vdraw,vstdout; 14276b3a5b13SJed Brown PetscBool flg; 1428693365a8SJed Brown if (flag_operator) { 1429693365a8SJed Brown ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr); 1430693365a8SJed Brown Bexp = Bexp_mine; 1431693365a8SJed Brown } else { 1432693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 1433693365a8SJed Brown ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr); 1434693365a8SJed Brown if (flg) Bexp = *B; 1435693365a8SJed Brown else { 1436693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 1437693365a8SJed Brown ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr); 1438693365a8SJed Brown Bexp = Bexp_mine; 1439693365a8SJed Brown } 1440693365a8SJed Brown } 1441693365a8SJed Brown ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr); 1442693365a8SJed Brown ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr); 1443693365a8SJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 1444693365a8SJed Brown if (flag_draw || flag_contour) { 1445693365a8SJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 1446693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 1447693365a8SJed Brown } else vdraw = PETSC_NULL; 1448693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr); 1449693365a8SJed Brown if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);} 1450693365a8SJed Brown if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);} 1451693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr); 1452693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1453693365a8SJed Brown if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);} 1454693365a8SJed Brown ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 1455693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr); 1456693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1457693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 1458693365a8SJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 1459693365a8SJed Brown ierr = MatView(FDexp,vdraw);CHKERRQ(ierr); 1460693365a8SJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 1461693365a8SJed Brown } 1462693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 1463693365a8SJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 1464693365a8SJed Brown ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr); 1465693365a8SJed Brown ierr = MatDestroy(&FDexp);CHKERRQ(ierr); 1466693365a8SJed Brown } 1467693365a8SJed Brown } 14683a40ed3dSBarry Smith PetscFunctionReturn(0); 14699b94acceSBarry Smith } 14709b94acceSBarry Smith 14714a2ae208SSatish Balay #undef __FUNCT__ 14724a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 14739b94acceSBarry Smith /*@C 14749b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1475044dda88SLois Curfman McInnes location to store the matrix. 14769b94acceSBarry Smith 14773f9fe445SBarry Smith Logically Collective on SNES and Mat 1478c7afd0dbSLois Curfman McInnes 14799b94acceSBarry Smith Input Parameters: 1480c7afd0dbSLois Curfman McInnes + snes - the SNES context 14819b94acceSBarry Smith . A - Jacobian matrix 14829b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 1483efd51863SBarry Smith . func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value) 1484c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1485efd51863SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value) 14869b94acceSBarry Smith 14879b94acceSBarry Smith Calling sequence of func: 14888d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 14899b94acceSBarry Smith 1490c7afd0dbSLois Curfman McInnes + x - input vector 14919b94acceSBarry Smith . A - Jacobian matrix 14929b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1493ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 14942b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1495c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 14969b94acceSBarry Smith 14979b94acceSBarry Smith Notes: 149894b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 14992cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1500ac21db08SLois Curfman McInnes 1501ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 15029b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 15039b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 15049b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 15059b94acceSBarry Smith throughout the global iterations. 15069b94acceSBarry Smith 150716913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 150816913363SBarry Smith each matrix. 150916913363SBarry Smith 1510a8a26c1eSJed Brown If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument 1511a8a26c1eSJed Brown must be a MatFDColoring. 1512a8a26c1eSJed Brown 1513c3cc8fd1SJed Brown Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian. One common 1514c3cc8fd1SJed Brown example is to use the "Picard linearization" which only differentiates through the highest order parts of each term. 1515c3cc8fd1SJed Brown 151636851e7fSLois Curfman McInnes Level: beginner 151736851e7fSLois Curfman McInnes 15189b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 15199b94acceSBarry Smith 15203ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 15219b94acceSBarry Smith @*/ 15227087cfbeSBarry Smith PetscErrorCode SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 15239b94acceSBarry Smith { 1524dfbe8321SBarry Smith PetscErrorCode ierr; 15253a7fca6bSBarry Smith 15263a40ed3dSBarry Smith PetscFunctionBegin; 15270700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15280700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 15290700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1530c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 153106975374SJed Brown if (B) PetscCheckSameComm(snes,1,B,3); 1532e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 15333a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 15343a7fca6bSBarry Smith if (A) { 15357dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 15366bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 15379b94acceSBarry Smith snes->jacobian = A; 15383a7fca6bSBarry Smith } 15393a7fca6bSBarry Smith if (B) { 15407dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 15416bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 15429b94acceSBarry Smith snes->jacobian_pre = B; 15433a7fca6bSBarry Smith } 15443a40ed3dSBarry Smith PetscFunctionReturn(0); 15459b94acceSBarry Smith } 154662fef451SLois Curfman McInnes 15474a2ae208SSatish Balay #undef __FUNCT__ 15484a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1549c2aafc4cSSatish Balay /*@C 1550b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1551b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1552b4fd4287SBarry Smith 1553c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1554c7afd0dbSLois Curfman McInnes 1555b4fd4287SBarry Smith Input Parameter: 1556b4fd4287SBarry Smith . snes - the nonlinear solver context 1557b4fd4287SBarry Smith 1558b4fd4287SBarry Smith Output Parameters: 1559c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1560b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 156170e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 156270e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1563fee21e36SBarry Smith 156436851e7fSLois Curfman McInnes Level: advanced 156536851e7fSLois Curfman McInnes 1566b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1567b4fd4287SBarry Smith @*/ 15687087cfbeSBarry Smith PetscErrorCode SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1569b4fd4287SBarry Smith { 15703a40ed3dSBarry Smith PetscFunctionBegin; 15710700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1572b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1573b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1574e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 157570e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 15763a40ed3dSBarry Smith PetscFunctionReturn(0); 1577b4fd4287SBarry Smith } 1578b4fd4287SBarry Smith 15799b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 15809b94acceSBarry Smith 15814a2ae208SSatish Balay #undef __FUNCT__ 15824a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 15839b94acceSBarry Smith /*@ 15849b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1585272ac6f2SLois Curfman McInnes of a nonlinear solver. 15869b94acceSBarry Smith 1587fee21e36SBarry Smith Collective on SNES 1588fee21e36SBarry Smith 1589c7afd0dbSLois Curfman McInnes Input Parameters: 159070e92668SMatthew Knepley . snes - the SNES context 1591c7afd0dbSLois Curfman McInnes 1592272ac6f2SLois Curfman McInnes Notes: 1593272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1594272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1595272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1596272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1597272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1598272ac6f2SLois Curfman McInnes 159936851e7fSLois Curfman McInnes Level: advanced 160036851e7fSLois Curfman McInnes 16019b94acceSBarry Smith .keywords: SNES, nonlinear, setup 16029b94acceSBarry Smith 16039b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 16049b94acceSBarry Smith @*/ 16057087cfbeSBarry Smith PetscErrorCode SNESSetUp(SNES snes) 16069b94acceSBarry Smith { 1607dfbe8321SBarry Smith PetscErrorCode ierr; 16083a40ed3dSBarry Smith 16093a40ed3dSBarry Smith PetscFunctionBegin; 16100700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 16114dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 16129b94acceSBarry Smith 16137adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 161485385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 161585385478SLisandro Dalcin } 161685385478SLisandro Dalcin 1617c9b9fda1SJed Brown if (!snes->vec_func) { 1618c9b9fda1SJed Brown if (snes->vec_rhs) { 1619c9b9fda1SJed Brown ierr = VecDuplicate(snes->vec_rhs,&snes->vec_func);CHKERRQ(ierr); 1620c9b9fda1SJed Brown } else if (snes->vec_sol) { 1621c9b9fda1SJed Brown ierr = VecDuplicate(snes->vec_sol,&snes->vec_func);CHKERRQ(ierr); 1622c9b9fda1SJed Brown } else if (snes->dm) { 1623efd51863SBarry Smith ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1624efd51863SBarry Smith } 1625c9b9fda1SJed Brown } 162617186662SBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 162758c9b817SLisandro Dalcin if (snes->vec_rhs == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector"); 162858c9b817SLisandro Dalcin 162958c9b817SLisandro Dalcin if (!snes->vec_sol_update /* && snes->vec_sol */) { 163058c9b817SLisandro Dalcin ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr); 163158c9b817SLisandro Dalcin ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr); 163258c9b817SLisandro Dalcin } 163358c9b817SLisandro Dalcin 1634ef8dffc7SBarry Smith if (!snes->ops->computejacobian && snes->dm) { 1635ef8dffc7SBarry Smith Mat J; 1636ef8dffc7SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 1637cab2e9ccSBarry Smith ierr = SNESSetJacobian(snes,J,J,SNESDMComputeJacobian,PETSC_NULL);CHKERRQ(ierr); 1638cab2e9ccSBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 16392ef29e96SBarry Smith } else if (!snes->jacobian && snes->ops->computejacobian == MatMFFDComputeJacobian) { 1640a8248277SBarry Smith Mat J; 1641a8248277SBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 1642a8248277SBarry Smith ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 1643a8248277SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 1644a8248277SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 1645a8248277SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1646a8248277SBarry Smith } else if (snes->dm && snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) { 1647a8248277SBarry Smith Mat J,B; 1648a8248277SBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 1649a8248277SBarry Smith ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 1650a8248277SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 1651a8248277SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr); 1652a8248277SBarry Smith ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,snes->funP);CHKERRQ(ierr); 1653a8248277SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1654a8248277SBarry Smith ierr = MatDestroy(&B);CHKERRQ(ierr); 1655efd51863SBarry Smith } else if (snes->dm && !snes->jacobian_pre){ 1656efd51863SBarry Smith Mat J; 1657efd51863SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 16583cbb28f5SBarry Smith ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 1659efd51863SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1660ef8dffc7SBarry Smith } 1661cfaf3a74SBarry Smith if (!snes->ops->computefunction && !snes->dm) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a residual function with SNESSetFunction() or call SNESSetDM()"); 1662c9b9fda1SJed Brown if (!snes->vec_func) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a vector when calling SNESSetFunction() or call SNESSetDM()"); 1663efd51863SBarry Smith 1664b710008aSBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);} 1665b710008aSBarry Smith 1666d25893d9SBarry Smith if (snes->ops->usercompute && !snes->user) { 1667d25893d9SBarry Smith ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr); 1668d25893d9SBarry Smith } 1669d25893d9SBarry Smith 1670410397dcSLisandro Dalcin if (snes->ops->setup) { 1671410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1672410397dcSLisandro Dalcin } 167358c9b817SLisandro Dalcin 16747aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 16753a40ed3dSBarry Smith PetscFunctionReturn(0); 16769b94acceSBarry Smith } 16779b94acceSBarry Smith 16784a2ae208SSatish Balay #undef __FUNCT__ 167937596af1SLisandro Dalcin #define __FUNCT__ "SNESReset" 168037596af1SLisandro Dalcin /*@ 168137596af1SLisandro Dalcin SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats 168237596af1SLisandro Dalcin 168337596af1SLisandro Dalcin Collective on SNES 168437596af1SLisandro Dalcin 168537596af1SLisandro Dalcin Input Parameter: 168637596af1SLisandro Dalcin . snes - iterative context obtained from SNESCreate() 168737596af1SLisandro Dalcin 1688d25893d9SBarry Smith Level: intermediate 1689d25893d9SBarry Smith 1690d25893d9SBarry Smith Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext() 169137596af1SLisandro Dalcin 169237596af1SLisandro Dalcin .keywords: SNES, destroy 169337596af1SLisandro Dalcin 169437596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 169537596af1SLisandro Dalcin @*/ 169637596af1SLisandro Dalcin PetscErrorCode SNESReset(SNES snes) 169737596af1SLisandro Dalcin { 169837596af1SLisandro Dalcin PetscErrorCode ierr; 169937596af1SLisandro Dalcin 170037596af1SLisandro Dalcin PetscFunctionBegin; 170137596af1SLisandro Dalcin PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1702d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 1703d25893d9SBarry Smith ierr = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr); 1704d25893d9SBarry Smith snes->user = PETSC_NULL; 1705d25893d9SBarry Smith } 17068a23116dSBarry Smith if (snes->pc) { 17078a23116dSBarry Smith ierr = SNESReset(snes->pc);CHKERRQ(ierr); 17088a23116dSBarry Smith } 17098a23116dSBarry Smith 171037596af1SLisandro Dalcin if (snes->ops->reset) { 171137596af1SLisandro Dalcin ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr); 171237596af1SLisandro Dalcin } 171337596af1SLisandro Dalcin if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);} 17146bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 17156bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 17166bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr); 17176bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 17186bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 17196bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 172037596af1SLisandro Dalcin if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);} 172137596af1SLisandro Dalcin if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);} 172237596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 172337596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 172437596af1SLisandro Dalcin PetscFunctionReturn(0); 172537596af1SLisandro Dalcin } 172637596af1SLisandro Dalcin 172737596af1SLisandro Dalcin #undef __FUNCT__ 17284a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 172952baeb72SSatish Balay /*@ 17309b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 17319b94acceSBarry Smith with SNESCreate(). 17329b94acceSBarry Smith 1733c7afd0dbSLois Curfman McInnes Collective on SNES 1734c7afd0dbSLois Curfman McInnes 17359b94acceSBarry Smith Input Parameter: 17369b94acceSBarry Smith . snes - the SNES context 17379b94acceSBarry Smith 173836851e7fSLois Curfman McInnes Level: beginner 173936851e7fSLois Curfman McInnes 17409b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 17419b94acceSBarry Smith 174263a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 17439b94acceSBarry Smith @*/ 17446bf464f9SBarry Smith PetscErrorCode SNESDestroy(SNES *snes) 17459b94acceSBarry Smith { 17466849ba73SBarry Smith PetscErrorCode ierr; 17473a40ed3dSBarry Smith 17483a40ed3dSBarry Smith PetscFunctionBegin; 17496bf464f9SBarry Smith if (!*snes) PetscFunctionReturn(0); 17506bf464f9SBarry Smith PetscValidHeaderSpecific((*snes),SNES_CLASSID,1); 17516bf464f9SBarry Smith if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);} 1752d4bb536fSBarry Smith 17536bf464f9SBarry Smith ierr = SNESReset((*snes));CHKERRQ(ierr); 17548a23116dSBarry Smith ierr = SNESDestroy(&(*snes)->pc);CHKERRQ(ierr); 17556b8b9a38SLisandro Dalcin 1756be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 17576bf464f9SBarry Smith ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr); 17586bf464f9SBarry Smith if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);} 17596d4c513bSLisandro Dalcin 17606bf464f9SBarry Smith ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr); 17616bf464f9SBarry Smith ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr); 17626b8b9a38SLisandro Dalcin 17636bf464f9SBarry Smith ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr); 17646bf464f9SBarry Smith if ((*snes)->ops->convergeddestroy) { 17656bf464f9SBarry Smith ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr); 17666b8b9a38SLisandro Dalcin } 17676bf464f9SBarry Smith if ((*snes)->conv_malloc) { 17686bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr); 17696bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr); 177058c9b817SLisandro Dalcin } 1771*ea630c6eSPeter Brune ierr = PetscViewerDestroy(&(*snes)->ls_monitor);CHKERRQ(ierr); 17726bf464f9SBarry Smith ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr); 1773a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 17743a40ed3dSBarry Smith PetscFunctionReturn(0); 17759b94acceSBarry Smith } 17769b94acceSBarry Smith 17779b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 17789b94acceSBarry Smith 17794a2ae208SSatish Balay #undef __FUNCT__ 1780a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1781a8054027SBarry Smith /*@ 1782a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1783a8054027SBarry Smith 17843f9fe445SBarry Smith Logically Collective on SNES 1785a8054027SBarry Smith 1786a8054027SBarry Smith Input Parameters: 1787a8054027SBarry Smith + snes - the SNES context 1788a8054027SBarry 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 17893b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1790a8054027SBarry Smith 1791a8054027SBarry Smith Options Database Keys: 1792a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1793a8054027SBarry Smith 1794a8054027SBarry Smith Notes: 1795a8054027SBarry Smith The default is 1 1796a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1797a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1798a8054027SBarry Smith 1799a8054027SBarry Smith Level: intermediate 1800a8054027SBarry Smith 1801a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1802a8054027SBarry Smith 1803e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1804a8054027SBarry Smith 1805a8054027SBarry Smith @*/ 18067087cfbeSBarry Smith PetscErrorCode SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1807a8054027SBarry Smith { 1808a8054027SBarry Smith PetscFunctionBegin; 18090700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1810e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1811e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1812c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 1813a8054027SBarry Smith snes->lagpreconditioner = lag; 1814a8054027SBarry Smith PetscFunctionReturn(0); 1815a8054027SBarry Smith } 1816a8054027SBarry Smith 1817a8054027SBarry Smith #undef __FUNCT__ 1818efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence" 1819efd51863SBarry Smith /*@ 1820efd51863SBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does 1821efd51863SBarry Smith 1822efd51863SBarry Smith Logically Collective on SNES 1823efd51863SBarry Smith 1824efd51863SBarry Smith Input Parameters: 1825efd51863SBarry Smith + snes - the SNES context 1826efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 1827efd51863SBarry Smith 1828efd51863SBarry Smith Options Database Keys: 1829efd51863SBarry Smith . -snes_grid_sequence <steps> 1830efd51863SBarry Smith 1831efd51863SBarry Smith Level: intermediate 1832efd51863SBarry Smith 1833efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1834efd51863SBarry Smith 1835efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1836efd51863SBarry Smith 1837efd51863SBarry Smith @*/ 1838efd51863SBarry Smith PetscErrorCode SNESSetGridSequence(SNES snes,PetscInt steps) 1839efd51863SBarry Smith { 1840efd51863SBarry Smith PetscFunctionBegin; 1841efd51863SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1842efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes,steps,2); 1843efd51863SBarry Smith snes->gridsequence = steps; 1844efd51863SBarry Smith PetscFunctionReturn(0); 1845efd51863SBarry Smith } 1846efd51863SBarry Smith 1847efd51863SBarry Smith #undef __FUNCT__ 1848a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1849a8054027SBarry Smith /*@ 1850a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1851a8054027SBarry Smith 18523f9fe445SBarry Smith Not Collective 1853a8054027SBarry Smith 1854a8054027SBarry Smith Input Parameter: 1855a8054027SBarry Smith . snes - the SNES context 1856a8054027SBarry Smith 1857a8054027SBarry Smith Output Parameter: 1858a8054027SBarry 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 18593b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1860a8054027SBarry Smith 1861a8054027SBarry Smith Options Database Keys: 1862a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1863a8054027SBarry Smith 1864a8054027SBarry Smith Notes: 1865a8054027SBarry Smith The default is 1 1866a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1867a8054027SBarry Smith 1868a8054027SBarry Smith Level: intermediate 1869a8054027SBarry Smith 1870a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1871a8054027SBarry Smith 1872a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1873a8054027SBarry Smith 1874a8054027SBarry Smith @*/ 18757087cfbeSBarry Smith PetscErrorCode SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1876a8054027SBarry Smith { 1877a8054027SBarry Smith PetscFunctionBegin; 18780700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1879a8054027SBarry Smith *lag = snes->lagpreconditioner; 1880a8054027SBarry Smith PetscFunctionReturn(0); 1881a8054027SBarry Smith } 1882a8054027SBarry Smith 1883a8054027SBarry Smith #undef __FUNCT__ 1884e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 1885e35cf81dSBarry Smith /*@ 1886e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 1887e35cf81dSBarry Smith often the preconditioner is rebuilt. 1888e35cf81dSBarry Smith 18893f9fe445SBarry Smith Logically Collective on SNES 1890e35cf81dSBarry Smith 1891e35cf81dSBarry Smith Input Parameters: 1892e35cf81dSBarry Smith + snes - the SNES context 1893e35cf81dSBarry 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 1894fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 1895e35cf81dSBarry Smith 1896e35cf81dSBarry Smith Options Database Keys: 1897e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1898e35cf81dSBarry Smith 1899e35cf81dSBarry Smith Notes: 1900e35cf81dSBarry Smith The default is 1 1901e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1902fe3ffe1eSBarry 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 1903fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 1904e35cf81dSBarry Smith 1905e35cf81dSBarry Smith Level: intermediate 1906e35cf81dSBarry Smith 1907e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1908e35cf81dSBarry Smith 1909e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 1910e35cf81dSBarry Smith 1911e35cf81dSBarry Smith @*/ 19127087cfbeSBarry Smith PetscErrorCode SNESSetLagJacobian(SNES snes,PetscInt lag) 1913e35cf81dSBarry Smith { 1914e35cf81dSBarry Smith PetscFunctionBegin; 19150700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1916e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1917e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1918c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 1919e35cf81dSBarry Smith snes->lagjacobian = lag; 1920e35cf81dSBarry Smith PetscFunctionReturn(0); 1921e35cf81dSBarry Smith } 1922e35cf81dSBarry Smith 1923e35cf81dSBarry Smith #undef __FUNCT__ 1924e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 1925e35cf81dSBarry Smith /*@ 1926e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 1927e35cf81dSBarry Smith 19283f9fe445SBarry Smith Not Collective 1929e35cf81dSBarry Smith 1930e35cf81dSBarry Smith Input Parameter: 1931e35cf81dSBarry Smith . snes - the SNES context 1932e35cf81dSBarry Smith 1933e35cf81dSBarry Smith Output Parameter: 1934e35cf81dSBarry 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 1935e35cf81dSBarry Smith the Jacobian is built etc. 1936e35cf81dSBarry Smith 1937e35cf81dSBarry Smith Options Database Keys: 1938e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1939e35cf81dSBarry Smith 1940e35cf81dSBarry Smith Notes: 1941e35cf81dSBarry Smith The default is 1 1942e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1943e35cf81dSBarry Smith 1944e35cf81dSBarry Smith Level: intermediate 1945e35cf81dSBarry Smith 1946e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1947e35cf81dSBarry Smith 1948e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 1949e35cf81dSBarry Smith 1950e35cf81dSBarry Smith @*/ 19517087cfbeSBarry Smith PetscErrorCode SNESGetLagJacobian(SNES snes,PetscInt *lag) 1952e35cf81dSBarry Smith { 1953e35cf81dSBarry Smith PetscFunctionBegin; 19540700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1955e35cf81dSBarry Smith *lag = snes->lagjacobian; 1956e35cf81dSBarry Smith PetscFunctionReturn(0); 1957e35cf81dSBarry Smith } 1958e35cf81dSBarry Smith 1959e35cf81dSBarry Smith #undef __FUNCT__ 19604a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 19619b94acceSBarry Smith /*@ 1962d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 19639b94acceSBarry Smith 19643f9fe445SBarry Smith Logically Collective on SNES 1965c7afd0dbSLois Curfman McInnes 19669b94acceSBarry Smith Input Parameters: 1967c7afd0dbSLois Curfman McInnes + snes - the SNES context 196870441072SBarry Smith . abstol - absolute convergence tolerance 196933174efeSLois Curfman McInnes . rtol - relative convergence tolerance 197033174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 197133174efeSLois Curfman McInnes of the change in the solution between steps 197233174efeSLois Curfman McInnes . maxit - maximum number of iterations 1973c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1974fee21e36SBarry Smith 197533174efeSLois Curfman McInnes Options Database Keys: 197670441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1977c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1978c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1979c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1980c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 19819b94acceSBarry Smith 1982d7a720efSLois Curfman McInnes Notes: 19839b94acceSBarry Smith The default maximum number of iterations is 50. 19849b94acceSBarry Smith The default maximum number of function evaluations is 1000. 19859b94acceSBarry Smith 198636851e7fSLois Curfman McInnes Level: intermediate 198736851e7fSLois Curfman McInnes 198833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 19899b94acceSBarry Smith 19902492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 19919b94acceSBarry Smith @*/ 19927087cfbeSBarry Smith PetscErrorCode SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 19939b94acceSBarry Smith { 19943a40ed3dSBarry Smith PetscFunctionBegin; 19950700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1996c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,abstol,2); 1997c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol,3); 1998c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,stol,4); 1999c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxit,5); 2000c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxf,6); 2001c5eb9154SBarry Smith 2002ab54825eSJed Brown if (abstol != PETSC_DEFAULT) { 2003ab54825eSJed Brown if (abstol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %G must be non-negative",abstol); 2004ab54825eSJed Brown snes->abstol = abstol; 2005ab54825eSJed Brown } 2006ab54825eSJed Brown if (rtol != PETSC_DEFAULT) { 2007ab54825eSJed Brown if (rtol < 0.0 || 1.0 <= rtol) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Relative tolerance %G must be non-negative and less than 1.0",rtol); 2008ab54825eSJed Brown snes->rtol = rtol; 2009ab54825eSJed Brown } 2010ab54825eSJed Brown if (stol != PETSC_DEFAULT) { 2011ab54825eSJed Brown if (stol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %G must be non-negative",stol); 2012ab54825eSJed Brown snes->xtol = stol; 2013ab54825eSJed Brown } 2014ab54825eSJed Brown if (maxit != PETSC_DEFAULT) { 2015ab54825eSJed Brown if (maxit < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit); 2016ab54825eSJed Brown snes->max_its = maxit; 2017ab54825eSJed Brown } 2018ab54825eSJed Brown if (maxf != PETSC_DEFAULT) { 2019ab54825eSJed Brown if (maxf < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be non-negative",maxf); 2020ab54825eSJed Brown snes->max_funcs = maxf; 2021ab54825eSJed Brown } 20223a40ed3dSBarry Smith PetscFunctionReturn(0); 20239b94acceSBarry Smith } 20249b94acceSBarry Smith 20254a2ae208SSatish Balay #undef __FUNCT__ 20264a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 20279b94acceSBarry Smith /*@ 202833174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 202933174efeSLois Curfman McInnes 2030c7afd0dbSLois Curfman McInnes Not Collective 2031c7afd0dbSLois Curfman McInnes 203233174efeSLois Curfman McInnes Input Parameters: 2033c7afd0dbSLois Curfman McInnes + snes - the SNES context 203485385478SLisandro Dalcin . atol - absolute convergence tolerance 203533174efeSLois Curfman McInnes . rtol - relative convergence tolerance 203633174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 203733174efeSLois Curfman McInnes of the change in the solution between steps 203833174efeSLois Curfman McInnes . maxit - maximum number of iterations 2039c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 2040fee21e36SBarry Smith 204133174efeSLois Curfman McInnes Notes: 204233174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 204333174efeSLois Curfman McInnes 204436851e7fSLois Curfman McInnes Level: intermediate 204536851e7fSLois Curfman McInnes 204633174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 204733174efeSLois Curfman McInnes 204833174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 204933174efeSLois Curfman McInnes @*/ 20507087cfbeSBarry Smith PetscErrorCode SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 205133174efeSLois Curfman McInnes { 20523a40ed3dSBarry Smith PetscFunctionBegin; 20530700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 205485385478SLisandro Dalcin if (atol) *atol = snes->abstol; 205533174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 205633174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 205733174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 205833174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 20593a40ed3dSBarry Smith PetscFunctionReturn(0); 206033174efeSLois Curfman McInnes } 206133174efeSLois Curfman McInnes 20624a2ae208SSatish Balay #undef __FUNCT__ 20634a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 206433174efeSLois Curfman McInnes /*@ 20659b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 20669b94acceSBarry Smith 20673f9fe445SBarry Smith Logically Collective on SNES 2068fee21e36SBarry Smith 2069c7afd0dbSLois Curfman McInnes Input Parameters: 2070c7afd0dbSLois Curfman McInnes + snes - the SNES context 2071c7afd0dbSLois Curfman McInnes - tol - tolerance 2072c7afd0dbSLois Curfman McInnes 20739b94acceSBarry Smith Options Database Key: 2074c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 20759b94acceSBarry Smith 207636851e7fSLois Curfman McInnes Level: intermediate 207736851e7fSLois Curfman McInnes 20789b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 20799b94acceSBarry Smith 20802492ecdbSBarry Smith .seealso: SNESSetTolerances() 20819b94acceSBarry Smith @*/ 20827087cfbeSBarry Smith PetscErrorCode SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 20839b94acceSBarry Smith { 20843a40ed3dSBarry Smith PetscFunctionBegin; 20850700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2086c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,tol,2); 20879b94acceSBarry Smith snes->deltatol = tol; 20883a40ed3dSBarry Smith PetscFunctionReturn(0); 20899b94acceSBarry Smith } 20909b94acceSBarry Smith 2091df9fa365SBarry Smith /* 2092df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 2093df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 2094df9fa365SBarry Smith macros instead of functions 2095df9fa365SBarry Smith */ 20964a2ae208SSatish Balay #undef __FUNCT__ 2097a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 20987087cfbeSBarry Smith PetscErrorCode SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 2099ce1608b8SBarry Smith { 2100dfbe8321SBarry Smith PetscErrorCode ierr; 2101ce1608b8SBarry Smith 2102ce1608b8SBarry Smith PetscFunctionBegin; 21030700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2104a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 2105ce1608b8SBarry Smith PetscFunctionReturn(0); 2106ce1608b8SBarry Smith } 2107ce1608b8SBarry Smith 21084a2ae208SSatish Balay #undef __FUNCT__ 2109a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 21107087cfbeSBarry Smith PetscErrorCode SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2111df9fa365SBarry Smith { 2112dfbe8321SBarry Smith PetscErrorCode ierr; 2113df9fa365SBarry Smith 2114df9fa365SBarry Smith PetscFunctionBegin; 2115a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2116df9fa365SBarry Smith PetscFunctionReturn(0); 2117df9fa365SBarry Smith } 2118df9fa365SBarry Smith 21194a2ae208SSatish Balay #undef __FUNCT__ 2120a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 21216bf464f9SBarry Smith PetscErrorCode SNESMonitorLGDestroy(PetscDrawLG *draw) 2122df9fa365SBarry Smith { 2123dfbe8321SBarry Smith PetscErrorCode ierr; 2124df9fa365SBarry Smith 2125df9fa365SBarry Smith PetscFunctionBegin; 2126a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2127df9fa365SBarry Smith PetscFunctionReturn(0); 2128df9fa365SBarry Smith } 2129df9fa365SBarry Smith 21307087cfbeSBarry Smith extern PetscErrorCode SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 2131b271bb04SBarry Smith #undef __FUNCT__ 2132b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 21337087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 2134b271bb04SBarry Smith { 2135b271bb04SBarry Smith PetscDrawLG lg; 2136b271bb04SBarry Smith PetscErrorCode ierr; 2137b271bb04SBarry Smith PetscReal x,y,per; 2138b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 2139b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 2140b271bb04SBarry Smith PetscDraw draw; 2141b271bb04SBarry Smith PetscFunctionBegin; 2142b271bb04SBarry Smith if (!monctx) { 2143b271bb04SBarry Smith MPI_Comm comm; 2144b271bb04SBarry Smith 2145b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 2146b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 2147b271bb04SBarry Smith } 2148b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 2149b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2150b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2151b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 2152b271bb04SBarry Smith x = (PetscReal) n; 2153b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 2154b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2155b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2156b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2157b271bb04SBarry Smith } 2158b271bb04SBarry Smith 2159b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 2160b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2161b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2162b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 2163b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 2164b271bb04SBarry Smith x = (PetscReal) n; 2165b271bb04SBarry Smith y = 100.0*per; 2166b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2167b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2168b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2169b271bb04SBarry Smith } 2170b271bb04SBarry Smith 2171b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 2172b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2173b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2174b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 2175b271bb04SBarry Smith x = (PetscReal) n; 2176b271bb04SBarry Smith y = (prev - rnorm)/prev; 2177b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2178b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2179b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2180b271bb04SBarry Smith } 2181b271bb04SBarry Smith 2182b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 2183b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2184b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2185b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 2186b271bb04SBarry Smith x = (PetscReal) n; 2187b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 2188b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 2189b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2190b271bb04SBarry Smith } 2191b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2192b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2193b271bb04SBarry Smith } 2194b271bb04SBarry Smith prev = rnorm; 2195b271bb04SBarry Smith PetscFunctionReturn(0); 2196b271bb04SBarry Smith } 2197b271bb04SBarry Smith 2198b271bb04SBarry Smith #undef __FUNCT__ 2199b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 22007087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2201b271bb04SBarry Smith { 2202b271bb04SBarry Smith PetscErrorCode ierr; 2203b271bb04SBarry Smith 2204b271bb04SBarry Smith PetscFunctionBegin; 2205b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2206b271bb04SBarry Smith PetscFunctionReturn(0); 2207b271bb04SBarry Smith } 2208b271bb04SBarry Smith 2209b271bb04SBarry Smith #undef __FUNCT__ 2210b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 22116bf464f9SBarry Smith PetscErrorCode SNESMonitorLGRangeDestroy(PetscDrawLG *draw) 2212b271bb04SBarry Smith { 2213b271bb04SBarry Smith PetscErrorCode ierr; 2214b271bb04SBarry Smith 2215b271bb04SBarry Smith PetscFunctionBegin; 2216b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2217b271bb04SBarry Smith PetscFunctionReturn(0); 2218b271bb04SBarry Smith } 2219b271bb04SBarry Smith 22207a03ce2fSLisandro Dalcin #undef __FUNCT__ 22217a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor" 2222228d79bcSJed Brown /*@ 2223228d79bcSJed Brown SNESMonitor - runs the user provided monitor routines, if they exist 2224228d79bcSJed Brown 2225228d79bcSJed Brown Collective on SNES 2226228d79bcSJed Brown 2227228d79bcSJed Brown Input Parameters: 2228228d79bcSJed Brown + snes - nonlinear solver context obtained from SNESCreate() 2229228d79bcSJed Brown . iter - iteration number 2230228d79bcSJed Brown - rnorm - relative norm of the residual 2231228d79bcSJed Brown 2232228d79bcSJed Brown Notes: 2233228d79bcSJed Brown This routine is called by the SNES implementations. 2234228d79bcSJed Brown It does not typically need to be called by the user. 2235228d79bcSJed Brown 2236228d79bcSJed Brown Level: developer 2237228d79bcSJed Brown 2238228d79bcSJed Brown .seealso: SNESMonitorSet() 2239228d79bcSJed Brown @*/ 22407a03ce2fSLisandro Dalcin PetscErrorCode SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm) 22417a03ce2fSLisandro Dalcin { 22427a03ce2fSLisandro Dalcin PetscErrorCode ierr; 22437a03ce2fSLisandro Dalcin PetscInt i,n = snes->numbermonitors; 22447a03ce2fSLisandro Dalcin 22457a03ce2fSLisandro Dalcin PetscFunctionBegin; 22467a03ce2fSLisandro Dalcin for (i=0; i<n; i++) { 22477a03ce2fSLisandro Dalcin ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr); 22487a03ce2fSLisandro Dalcin } 22497a03ce2fSLisandro Dalcin PetscFunctionReturn(0); 22507a03ce2fSLisandro Dalcin } 22517a03ce2fSLisandro Dalcin 22529b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 22539b94acceSBarry Smith 22544a2ae208SSatish Balay #undef __FUNCT__ 2255a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 22569b94acceSBarry Smith /*@C 2257a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 22589b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 22599b94acceSBarry Smith progress. 22609b94acceSBarry Smith 22613f9fe445SBarry Smith Logically Collective on SNES 2262fee21e36SBarry Smith 2263c7afd0dbSLois Curfman McInnes Input Parameters: 2264c7afd0dbSLois Curfman McInnes + snes - the SNES context 2265c7afd0dbSLois Curfman McInnes . func - monitoring routine 2266b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 2267e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 2268b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 2269b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 22709b94acceSBarry Smith 2271c7afd0dbSLois Curfman McInnes Calling sequence of func: 2272a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 2273c7afd0dbSLois Curfman McInnes 2274c7afd0dbSLois Curfman McInnes + snes - the SNES context 2275c7afd0dbSLois Curfman McInnes . its - iteration number 2276c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 227740a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 22789b94acceSBarry Smith 22799665c990SLois Curfman McInnes Options Database Keys: 2280a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 2281a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 2282a6570f20SBarry Smith uses SNESMonitorLGCreate() 2283cca6129bSJed Brown - -snes_monitor_cancel - cancels all monitors that have 2284c7afd0dbSLois Curfman McInnes been hardwired into a code by 2285a6570f20SBarry Smith calls to SNESMonitorSet(), but 2286c7afd0dbSLois Curfman McInnes does not cancel those set via 2287c7afd0dbSLois Curfman McInnes the options database. 22889665c990SLois Curfman McInnes 2289639f9d9dSBarry Smith Notes: 22906bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 2291a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 22926bc08f3fSLois Curfman McInnes order in which they were set. 2293639f9d9dSBarry Smith 2294025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 2295025f1a04SBarry Smith 229636851e7fSLois Curfman McInnes Level: intermediate 229736851e7fSLois Curfman McInnes 22989b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 22999b94acceSBarry Smith 2300a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 23019b94acceSBarry Smith @*/ 2302c2efdce3SBarry Smith PetscErrorCode SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 23039b94acceSBarry Smith { 2304b90d0a6eSBarry Smith PetscInt i; 2305649052a6SBarry Smith PetscErrorCode ierr; 2306b90d0a6eSBarry Smith 23073a40ed3dSBarry Smith PetscFunctionBegin; 23080700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 230917186662SBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2310b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 2311649052a6SBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) { 2312649052a6SBarry Smith if (monitordestroy) { 2313c2efdce3SBarry Smith ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr); 2314649052a6SBarry Smith } 2315b90d0a6eSBarry Smith PetscFunctionReturn(0); 2316b90d0a6eSBarry Smith } 2317b90d0a6eSBarry Smith } 2318b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 2319b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 2320639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 23213a40ed3dSBarry Smith PetscFunctionReturn(0); 23229b94acceSBarry Smith } 23239b94acceSBarry Smith 23244a2ae208SSatish Balay #undef __FUNCT__ 2325a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 23265cd90555SBarry Smith /*@C 2327a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 23285cd90555SBarry Smith 23293f9fe445SBarry Smith Logically Collective on SNES 2330c7afd0dbSLois Curfman McInnes 23315cd90555SBarry Smith Input Parameters: 23325cd90555SBarry Smith . snes - the SNES context 23335cd90555SBarry Smith 23341a480d89SAdministrator Options Database Key: 2335a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 2336a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 2337c7afd0dbSLois Curfman McInnes set via the options database 23385cd90555SBarry Smith 23395cd90555SBarry Smith Notes: 23405cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 23415cd90555SBarry Smith 234236851e7fSLois Curfman McInnes Level: intermediate 234336851e7fSLois Curfman McInnes 23445cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 23455cd90555SBarry Smith 2346a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 23475cd90555SBarry Smith @*/ 23487087cfbeSBarry Smith PetscErrorCode SNESMonitorCancel(SNES snes) 23495cd90555SBarry Smith { 2350d952e501SBarry Smith PetscErrorCode ierr; 2351d952e501SBarry Smith PetscInt i; 2352d952e501SBarry Smith 23535cd90555SBarry Smith PetscFunctionBegin; 23540700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2355d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 2356d952e501SBarry Smith if (snes->monitordestroy[i]) { 23573c4aec1bSBarry Smith ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr); 2358d952e501SBarry Smith } 2359d952e501SBarry Smith } 23605cd90555SBarry Smith snes->numbermonitors = 0; 23615cd90555SBarry Smith PetscFunctionReturn(0); 23625cd90555SBarry Smith } 23635cd90555SBarry Smith 23644a2ae208SSatish Balay #undef __FUNCT__ 23654a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 23669b94acceSBarry Smith /*@C 23679b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 23689b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 23699b94acceSBarry Smith 23703f9fe445SBarry Smith Logically Collective on SNES 2371fee21e36SBarry Smith 2372c7afd0dbSLois Curfman McInnes Input Parameters: 2373c7afd0dbSLois Curfman McInnes + snes - the SNES context 2374c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 23757f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 23767f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 23779b94acceSBarry Smith 2378c7afd0dbSLois Curfman McInnes Calling sequence of func: 237906ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 2380c7afd0dbSLois Curfman McInnes 2381c7afd0dbSLois Curfman McInnes + snes - the SNES context 238206ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 2383c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 2384184914b5SBarry Smith . reason - reason for convergence/divergence 2385c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 23864b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 23874b27c08aSLois Curfman McInnes - f - 2-norm of function 23889b94acceSBarry Smith 238936851e7fSLois Curfman McInnes Level: advanced 239036851e7fSLois Curfman McInnes 23919b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 23929b94acceSBarry Smith 239385385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 23949b94acceSBarry Smith @*/ 23957087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 23969b94acceSBarry Smith { 23977f7931b9SBarry Smith PetscErrorCode ierr; 23987f7931b9SBarry Smith 23993a40ed3dSBarry Smith PetscFunctionBegin; 24000700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 240185385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 24027f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 24037f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 24047f7931b9SBarry Smith } 240585385478SLisandro Dalcin snes->ops->converged = func; 24067f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 240785385478SLisandro Dalcin snes->cnvP = cctx; 24083a40ed3dSBarry Smith PetscFunctionReturn(0); 24099b94acceSBarry Smith } 24109b94acceSBarry Smith 24114a2ae208SSatish Balay #undef __FUNCT__ 24124a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 241352baeb72SSatish Balay /*@ 2414184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 2415184914b5SBarry Smith 2416184914b5SBarry Smith Not Collective 2417184914b5SBarry Smith 2418184914b5SBarry Smith Input Parameter: 2419184914b5SBarry Smith . snes - the SNES context 2420184914b5SBarry Smith 2421184914b5SBarry Smith Output Parameter: 24224d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 2423184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 2424184914b5SBarry Smith 2425184914b5SBarry Smith Level: intermediate 2426184914b5SBarry Smith 2427184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 2428184914b5SBarry Smith 2429184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 2430184914b5SBarry Smith 243185385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 2432184914b5SBarry Smith @*/ 24337087cfbeSBarry Smith PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 2434184914b5SBarry Smith { 2435184914b5SBarry Smith PetscFunctionBegin; 24360700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 24374482741eSBarry Smith PetscValidPointer(reason,2); 2438184914b5SBarry Smith *reason = snes->reason; 2439184914b5SBarry Smith PetscFunctionReturn(0); 2440184914b5SBarry Smith } 2441184914b5SBarry Smith 24424a2ae208SSatish Balay #undef __FUNCT__ 24434a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 2444c9005455SLois Curfman McInnes /*@ 2445c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 2446c9005455SLois Curfman McInnes 24473f9fe445SBarry Smith Logically Collective on SNES 2448fee21e36SBarry Smith 2449c7afd0dbSLois Curfman McInnes Input Parameters: 2450c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 24518c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 2452cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 2453758f92a0SBarry Smith . na - size of a and its 245464731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 2455758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 2456c7afd0dbSLois Curfman McInnes 2457308dcc3eSBarry Smith Notes: 2458308dcc3eSBarry Smith If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a 2459308dcc3eSBarry Smith default array of length 10000 is allocated. 2460308dcc3eSBarry Smith 2461c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 2462c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 2463c9005455SLois Curfman McInnes during the section of code that is being timed. 2464c9005455SLois Curfman McInnes 246536851e7fSLois Curfman McInnes Level: intermediate 246636851e7fSLois Curfman McInnes 2467c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2468758f92a0SBarry Smith 246908405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2470758f92a0SBarry Smith 2471c9005455SLois Curfman McInnes @*/ 24727087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset) 2473c9005455SLois Curfman McInnes { 2474308dcc3eSBarry Smith PetscErrorCode ierr; 2475308dcc3eSBarry Smith 24763a40ed3dSBarry Smith PetscFunctionBegin; 24770700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 24784482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2479a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2480308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) { 2481308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2482308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr); 2483308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr); 2484308dcc3eSBarry Smith snes->conv_malloc = PETSC_TRUE; 2485308dcc3eSBarry Smith } 2486c9005455SLois Curfman McInnes snes->conv_hist = a; 2487758f92a0SBarry Smith snes->conv_hist_its = its; 2488758f92a0SBarry Smith snes->conv_hist_max = na; 2489a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2490758f92a0SBarry Smith snes->conv_hist_reset = reset; 2491758f92a0SBarry Smith PetscFunctionReturn(0); 2492758f92a0SBarry Smith } 2493758f92a0SBarry Smith 2494308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2495c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 2496c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 2497308dcc3eSBarry Smith EXTERN_C_BEGIN 2498308dcc3eSBarry Smith #undef __FUNCT__ 2499308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab" 2500308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 2501308dcc3eSBarry Smith { 2502308dcc3eSBarry Smith mxArray *mat; 2503308dcc3eSBarry Smith PetscInt i; 2504308dcc3eSBarry Smith PetscReal *ar; 2505308dcc3eSBarry Smith 2506308dcc3eSBarry Smith PetscFunctionBegin; 2507308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL); 2508308dcc3eSBarry Smith ar = (PetscReal*) mxGetData(mat); 2509308dcc3eSBarry Smith for (i=0; i<snes->conv_hist_len; i++) { 2510308dcc3eSBarry Smith ar[i] = snes->conv_hist[i]; 2511308dcc3eSBarry Smith } 2512308dcc3eSBarry Smith PetscFunctionReturn(mat); 2513308dcc3eSBarry Smith } 2514308dcc3eSBarry Smith EXTERN_C_END 2515308dcc3eSBarry Smith #endif 2516308dcc3eSBarry Smith 2517308dcc3eSBarry Smith 25184a2ae208SSatish Balay #undef __FUNCT__ 25194a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 25200c4c9dddSBarry Smith /*@C 2521758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 2522758f92a0SBarry Smith 25233f9fe445SBarry Smith Not Collective 2524758f92a0SBarry Smith 2525758f92a0SBarry Smith Input Parameter: 2526758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 2527758f92a0SBarry Smith 2528758f92a0SBarry Smith Output Parameters: 2529758f92a0SBarry Smith . a - array to hold history 2530758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 2531758f92a0SBarry Smith negative if not converged) for each solve. 2532758f92a0SBarry Smith - na - size of a and its 2533758f92a0SBarry Smith 2534758f92a0SBarry Smith Notes: 2535758f92a0SBarry Smith The calling sequence for this routine in Fortran is 2536758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 2537758f92a0SBarry Smith 2538758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 2539758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 2540758f92a0SBarry Smith during the section of code that is being timed. 2541758f92a0SBarry Smith 2542758f92a0SBarry Smith Level: intermediate 2543758f92a0SBarry Smith 2544758f92a0SBarry Smith .keywords: SNES, get, convergence, history 2545758f92a0SBarry Smith 2546758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 2547758f92a0SBarry Smith 2548758f92a0SBarry Smith @*/ 25497087cfbeSBarry Smith PetscErrorCode SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 2550758f92a0SBarry Smith { 2551758f92a0SBarry Smith PetscFunctionBegin; 25520700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2553758f92a0SBarry Smith if (a) *a = snes->conv_hist; 2554758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 2555758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 25563a40ed3dSBarry Smith PetscFunctionReturn(0); 2557c9005455SLois Curfman McInnes } 2558c9005455SLois Curfman McInnes 2559e74ef692SMatthew Knepley #undef __FUNCT__ 2560e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 2561ac226902SBarry Smith /*@C 256276b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 2563eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 25647e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 256576b2cf59SMatthew Knepley 25663f9fe445SBarry Smith Logically Collective on SNES 256776b2cf59SMatthew Knepley 256876b2cf59SMatthew Knepley Input Parameters: 256976b2cf59SMatthew Knepley . snes - The nonlinear solver context 257076b2cf59SMatthew Knepley . func - The function 257176b2cf59SMatthew Knepley 257276b2cf59SMatthew Knepley Calling sequence of func: 2573b5d30489SBarry Smith . func (SNES snes, PetscInt step); 257476b2cf59SMatthew Knepley 257576b2cf59SMatthew Knepley . step - The current step of the iteration 257676b2cf59SMatthew Knepley 2577fe97e370SBarry Smith Level: advanced 2578fe97e370SBarry Smith 2579fe97e370SBarry Smith Note: This is NOT what one uses to update the ghost points before a function evaluation, that should be done at the beginning of your FormFunction() 2580fe97e370SBarry Smith This is not used by most users. 258176b2cf59SMatthew Knepley 258276b2cf59SMatthew Knepley .keywords: SNES, update 2583b5d30489SBarry Smith 258485385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 258576b2cf59SMatthew Knepley @*/ 25867087cfbeSBarry Smith PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 258776b2cf59SMatthew Knepley { 258876b2cf59SMatthew Knepley PetscFunctionBegin; 25890700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 2590e7788613SBarry Smith snes->ops->update = func; 259176b2cf59SMatthew Knepley PetscFunctionReturn(0); 259276b2cf59SMatthew Knepley } 259376b2cf59SMatthew Knepley 2594e74ef692SMatthew Knepley #undef __FUNCT__ 2595e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 259676b2cf59SMatthew Knepley /*@ 259776b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 259876b2cf59SMatthew Knepley 259976b2cf59SMatthew Knepley Not collective 260076b2cf59SMatthew Knepley 260176b2cf59SMatthew Knepley Input Parameters: 260276b2cf59SMatthew Knepley . snes - The nonlinear solver context 260376b2cf59SMatthew Knepley . step - The current step of the iteration 260476b2cf59SMatthew Knepley 2605205452f4SMatthew Knepley Level: intermediate 2606205452f4SMatthew Knepley 260776b2cf59SMatthew Knepley .keywords: SNES, update 2608a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 260976b2cf59SMatthew Knepley @*/ 26107087cfbeSBarry Smith PetscErrorCode SNESDefaultUpdate(SNES snes, PetscInt step) 261176b2cf59SMatthew Knepley { 261276b2cf59SMatthew Knepley PetscFunctionBegin; 261376b2cf59SMatthew Knepley PetscFunctionReturn(0); 261476b2cf59SMatthew Knepley } 261576b2cf59SMatthew Knepley 26164a2ae208SSatish Balay #undef __FUNCT__ 26174a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 26189b94acceSBarry Smith /* 26199b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 26209b94acceSBarry Smith positive parameter delta. 26219b94acceSBarry Smith 26229b94acceSBarry Smith Input Parameters: 2623c7afd0dbSLois Curfman McInnes + snes - the SNES context 26249b94acceSBarry Smith . y - approximate solution of linear system 26259b94acceSBarry Smith . fnorm - 2-norm of current function 2626c7afd0dbSLois Curfman McInnes - delta - trust region size 26279b94acceSBarry Smith 26289b94acceSBarry Smith Output Parameters: 2629c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 26309b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 26319b94acceSBarry Smith region, and exceeds zero otherwise. 2632c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 26339b94acceSBarry Smith 26349b94acceSBarry Smith Note: 26354b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 26369b94acceSBarry Smith is set to be the maximum allowable step size. 26379b94acceSBarry Smith 26389b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 26399b94acceSBarry Smith */ 2640dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 26419b94acceSBarry Smith { 2642064f8208SBarry Smith PetscReal nrm; 2643ea709b57SSatish Balay PetscScalar cnorm; 2644dfbe8321SBarry Smith PetscErrorCode ierr; 26453a40ed3dSBarry Smith 26463a40ed3dSBarry Smith PetscFunctionBegin; 26470700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 26480700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 2649c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 2650184914b5SBarry Smith 2651064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 2652064f8208SBarry Smith if (nrm > *delta) { 2653064f8208SBarry Smith nrm = *delta/nrm; 2654064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 2655064f8208SBarry Smith cnorm = nrm; 26562dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 26579b94acceSBarry Smith *ynorm = *delta; 26589b94acceSBarry Smith } else { 26599b94acceSBarry Smith *gpnorm = 0.0; 2660064f8208SBarry Smith *ynorm = nrm; 26619b94acceSBarry Smith } 26623a40ed3dSBarry Smith PetscFunctionReturn(0); 26639b94acceSBarry Smith } 26649b94acceSBarry Smith 26654a2ae208SSatish Balay #undef __FUNCT__ 26664a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 26676ce558aeSBarry Smith /*@C 2668f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 2669f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 26709b94acceSBarry Smith 2671c7afd0dbSLois Curfman McInnes Collective on SNES 2672c7afd0dbSLois Curfman McInnes 2673b2002411SLois Curfman McInnes Input Parameters: 2674c7afd0dbSLois Curfman McInnes + snes - the SNES context 26753cebf274SJed Brown . b - the constant part of the equation F(x) = b, or PETSC_NULL to use zero. 267685385478SLisandro Dalcin - x - the solution vector. 26779b94acceSBarry Smith 2678b2002411SLois Curfman McInnes Notes: 26798ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 26808ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 26818ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 26828ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 26838ddd3da0SLois Curfman McInnes 268436851e7fSLois Curfman McInnes Level: beginner 268536851e7fSLois Curfman McInnes 26869b94acceSBarry Smith .keywords: SNES, nonlinear, solve 26879b94acceSBarry Smith 268885385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 26899b94acceSBarry Smith @*/ 26907087cfbeSBarry Smith PetscErrorCode SNESSolve(SNES snes,Vec b,Vec x) 26919b94acceSBarry Smith { 2692dfbe8321SBarry Smith PetscErrorCode ierr; 2693ace3abfcSBarry Smith PetscBool flg; 2694eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 2695eabae89aSBarry Smith PetscViewer viewer; 2696efd51863SBarry Smith PetscInt grid; 2697052efed2SBarry Smith 26983a40ed3dSBarry Smith PetscFunctionBegin; 26990700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 27000700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2701f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 27020700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 270385385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 270485385478SLisandro Dalcin 2705a8248277SBarry Smith for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);} 2706efd51863SBarry Smith for (grid=0; grid<snes->gridsequence+1; grid++) { 2707efd51863SBarry Smith 270885385478SLisandro Dalcin /* set solution vector */ 2709efd51863SBarry Smith if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);} 27106bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 271185385478SLisandro Dalcin snes->vec_sol = x; 271285385478SLisandro Dalcin /* set afine vector if provided */ 271385385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 27146bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 271585385478SLisandro Dalcin snes->vec_rhs = b; 271685385478SLisandro Dalcin 271770e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 27183f149594SLisandro Dalcin 2719d25893d9SBarry Smith if (!grid && snes->ops->computeinitialguess) { 2720d25893d9SBarry Smith ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr); 2721d25893d9SBarry Smith } 2722d25893d9SBarry Smith 2723abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 272450ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2725d5e45103SBarry Smith 27263f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 27274936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 272885385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 27294936397dSBarry Smith if (snes->domainerror){ 27304936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 27314936397dSBarry Smith snes->domainerror = PETSC_FALSE; 27324936397dSBarry Smith } 273317186662SBarry Smith if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 27343f149594SLisandro Dalcin 27357adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2736eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 27377adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2738eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 27396bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2740eabae89aSBarry Smith } 2741eabae89aSBarry Smith 274290d69ab7SBarry Smith flg = PETSC_FALSE; 2743acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr); 2744da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 27455968eb51SBarry Smith if (snes->printreason) { 2746a8248277SBarry Smith ierr = PetscViewerASCIIAddTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 27475968eb51SBarry Smith if (snes->reason > 0) { 2748a8248277SBarry Smith ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 27495968eb51SBarry Smith } else { 2750a8248277SBarry Smith ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 27515968eb51SBarry Smith } 2752a8248277SBarry Smith ierr = PetscViewerASCIISubtractTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 27535968eb51SBarry Smith } 27545968eb51SBarry Smith 2755e113a28aSBarry Smith if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged"); 2756efd51863SBarry Smith if (grid < snes->gridsequence) { 2757efd51863SBarry Smith DM fine; 2758efd51863SBarry Smith Vec xnew; 2759efd51863SBarry Smith Mat interp; 2760efd51863SBarry Smith 2761efd51863SBarry Smith ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr); 2762efd51863SBarry Smith ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr); 2763efd51863SBarry Smith ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr); 2764efd51863SBarry Smith ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr); 2765efd51863SBarry Smith ierr = MatDestroy(&interp);CHKERRQ(ierr); 2766efd51863SBarry Smith x = xnew; 2767efd51863SBarry Smith 2768efd51863SBarry Smith ierr = SNESReset(snes);CHKERRQ(ierr); 2769efd51863SBarry Smith ierr = SNESSetDM(snes,fine);CHKERRQ(ierr); 2770efd51863SBarry Smith ierr = DMDestroy(&fine);CHKERRQ(ierr); 2771a8248277SBarry Smith ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr); 2772efd51863SBarry Smith } 2773efd51863SBarry Smith } 27743a40ed3dSBarry Smith PetscFunctionReturn(0); 27759b94acceSBarry Smith } 27769b94acceSBarry Smith 27779b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 27789b94acceSBarry Smith 27794a2ae208SSatish Balay #undef __FUNCT__ 27804a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 278182bf6240SBarry Smith /*@C 27824b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 27839b94acceSBarry Smith 2784fee21e36SBarry Smith Collective on SNES 2785fee21e36SBarry Smith 2786c7afd0dbSLois Curfman McInnes Input Parameters: 2787c7afd0dbSLois Curfman McInnes + snes - the SNES context 2788454a90a3SBarry Smith - type - a known method 2789c7afd0dbSLois Curfman McInnes 2790c7afd0dbSLois Curfman McInnes Options Database Key: 2791454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2792c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2793ae12b187SLois Curfman McInnes 27949b94acceSBarry Smith Notes: 2795e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 27964b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2797c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 27984b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2799c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 28009b94acceSBarry Smith 2801ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2802ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2803ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2804ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2805ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2806ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2807ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2808ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2809ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2810b0a32e0cSBarry Smith appropriate method. 281136851e7fSLois Curfman McInnes 281236851e7fSLois Curfman McInnes Level: intermediate 2813a703fe33SLois Curfman McInnes 2814454a90a3SBarry Smith .keywords: SNES, set, type 2815435da068SBarry Smith 2816435da068SBarry Smith .seealso: SNESType, SNESCreate() 2817435da068SBarry Smith 28189b94acceSBarry Smith @*/ 28197087cfbeSBarry Smith PetscErrorCode SNESSetType(SNES snes,const SNESType type) 28209b94acceSBarry Smith { 2821dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 2822ace3abfcSBarry Smith PetscBool match; 28233a40ed3dSBarry Smith 28243a40ed3dSBarry Smith PetscFunctionBegin; 28250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28264482741eSBarry Smith PetscValidCharPointer(type,2); 282782bf6240SBarry Smith 28286831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 28290f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 283092ff6ae8SBarry Smith 28314b91b6eaSBarry Smith ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 2832e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 283375396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 283475396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 283575396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 283675396ef9SLisandro Dalcin snes->ops->setup = 0; 283775396ef9SLisandro Dalcin snes->ops->solve = 0; 283875396ef9SLisandro Dalcin snes->ops->view = 0; 283975396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 284075396ef9SLisandro Dalcin snes->ops->destroy = 0; 284175396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 284275396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 2843454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 284403bfa161SLisandro Dalcin ierr = (*r)(snes);CHKERRQ(ierr); 28459fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS) 28469fb22e1aSBarry Smith if (PetscAMSPublishAll) { 28479fb22e1aSBarry Smith ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr); 28489fb22e1aSBarry Smith } 28499fb22e1aSBarry Smith #endif 28503a40ed3dSBarry Smith PetscFunctionReturn(0); 28519b94acceSBarry Smith } 28529b94acceSBarry Smith 2853a847f771SSatish Balay 28549b94acceSBarry Smith /* --------------------------------------------------------------------- */ 28554a2ae208SSatish Balay #undef __FUNCT__ 28564a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 285752baeb72SSatish Balay /*@ 28589b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2859f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 28609b94acceSBarry Smith 2861fee21e36SBarry Smith Not Collective 2862fee21e36SBarry Smith 286336851e7fSLois Curfman McInnes Level: advanced 286436851e7fSLois Curfman McInnes 28659b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 28669b94acceSBarry Smith 28679b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 28689b94acceSBarry Smith @*/ 28697087cfbeSBarry Smith PetscErrorCode SNESRegisterDestroy(void) 28709b94acceSBarry Smith { 2871dfbe8321SBarry Smith PetscErrorCode ierr; 287282bf6240SBarry Smith 28733a40ed3dSBarry Smith PetscFunctionBegin; 28741441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 28754c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 28763a40ed3dSBarry Smith PetscFunctionReturn(0); 28779b94acceSBarry Smith } 28789b94acceSBarry Smith 28794a2ae208SSatish Balay #undef __FUNCT__ 28804a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 28819b94acceSBarry Smith /*@C 28829a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 28839b94acceSBarry Smith 2884c7afd0dbSLois Curfman McInnes Not Collective 2885c7afd0dbSLois Curfman McInnes 28869b94acceSBarry Smith Input Parameter: 28874b0e389bSBarry Smith . snes - nonlinear solver context 28889b94acceSBarry Smith 28899b94acceSBarry Smith Output Parameter: 28903a7fca6bSBarry Smith . type - SNES method (a character string) 28919b94acceSBarry Smith 289236851e7fSLois Curfman McInnes Level: intermediate 289336851e7fSLois Curfman McInnes 2894454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 28959b94acceSBarry Smith @*/ 28967087cfbeSBarry Smith PetscErrorCode SNESGetType(SNES snes,const SNESType *type) 28979b94acceSBarry Smith { 28983a40ed3dSBarry Smith PetscFunctionBegin; 28990700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29004482741eSBarry Smith PetscValidPointer(type,2); 29017adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 29023a40ed3dSBarry Smith PetscFunctionReturn(0); 29039b94acceSBarry Smith } 29049b94acceSBarry Smith 29054a2ae208SSatish Balay #undef __FUNCT__ 29064a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 290752baeb72SSatish Balay /*@ 29089b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 29099b94acceSBarry Smith stored. 29109b94acceSBarry Smith 2911c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2912c7afd0dbSLois Curfman McInnes 29139b94acceSBarry Smith Input Parameter: 29149b94acceSBarry Smith . snes - the SNES context 29159b94acceSBarry Smith 29169b94acceSBarry Smith Output Parameter: 29179b94acceSBarry Smith . x - the solution 29189b94acceSBarry Smith 291970e92668SMatthew Knepley Level: intermediate 292036851e7fSLois Curfman McInnes 29219b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 29229b94acceSBarry Smith 292385385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 29249b94acceSBarry Smith @*/ 29257087cfbeSBarry Smith PetscErrorCode SNESGetSolution(SNES snes,Vec *x) 29269b94acceSBarry Smith { 29273a40ed3dSBarry Smith PetscFunctionBegin; 29280700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29294482741eSBarry Smith PetscValidPointer(x,2); 293085385478SLisandro Dalcin *x = snes->vec_sol; 293170e92668SMatthew Knepley PetscFunctionReturn(0); 293270e92668SMatthew Knepley } 293370e92668SMatthew Knepley 293470e92668SMatthew Knepley #undef __FUNCT__ 29354a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 293652baeb72SSatish Balay /*@ 29379b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 29389b94acceSBarry Smith stored. 29399b94acceSBarry Smith 2940c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2941c7afd0dbSLois Curfman McInnes 29429b94acceSBarry Smith Input Parameter: 29439b94acceSBarry Smith . snes - the SNES context 29449b94acceSBarry Smith 29459b94acceSBarry Smith Output Parameter: 29469b94acceSBarry Smith . x - the solution update 29479b94acceSBarry Smith 294836851e7fSLois Curfman McInnes Level: advanced 294936851e7fSLois Curfman McInnes 29509b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 29519b94acceSBarry Smith 295285385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 29539b94acceSBarry Smith @*/ 29547087cfbeSBarry Smith PetscErrorCode SNESGetSolutionUpdate(SNES snes,Vec *x) 29559b94acceSBarry Smith { 29563a40ed3dSBarry Smith PetscFunctionBegin; 29570700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29584482741eSBarry Smith PetscValidPointer(x,2); 295985385478SLisandro Dalcin *x = snes->vec_sol_update; 29603a40ed3dSBarry Smith PetscFunctionReturn(0); 29619b94acceSBarry Smith } 29629b94acceSBarry Smith 29634a2ae208SSatish Balay #undef __FUNCT__ 29644a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 29659b94acceSBarry Smith /*@C 29663638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 29679b94acceSBarry Smith 2968c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2969c7afd0dbSLois Curfman McInnes 29709b94acceSBarry Smith Input Parameter: 29719b94acceSBarry Smith . snes - the SNES context 29729b94acceSBarry Smith 29739b94acceSBarry Smith Output Parameter: 29747bf4e008SBarry Smith + r - the function (or PETSC_NULL) 297570e92668SMatthew Knepley . func - the function (or PETSC_NULL) 297670e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 29779b94acceSBarry Smith 297836851e7fSLois Curfman McInnes Level: advanced 297936851e7fSLois Curfman McInnes 2980a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 29819b94acceSBarry Smith 29824b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 29839b94acceSBarry Smith @*/ 29847087cfbeSBarry Smith PetscErrorCode SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 29859b94acceSBarry Smith { 29863a40ed3dSBarry Smith PetscFunctionBegin; 29870700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 298885385478SLisandro Dalcin if (r) *r = snes->vec_func; 2989e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 299070e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 29913a40ed3dSBarry Smith PetscFunctionReturn(0); 29929b94acceSBarry Smith } 29939b94acceSBarry Smith 29944a2ae208SSatish Balay #undef __FUNCT__ 29954a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 29963c7409f5SSatish Balay /*@C 29973c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2998d850072dSLois Curfman McInnes SNES options in the database. 29993c7409f5SSatish Balay 30003f9fe445SBarry Smith Logically Collective on SNES 3001fee21e36SBarry Smith 3002c7afd0dbSLois Curfman McInnes Input Parameter: 3003c7afd0dbSLois Curfman McInnes + snes - the SNES context 3004c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3005c7afd0dbSLois Curfman McInnes 3006d850072dSLois Curfman McInnes Notes: 3007a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3008c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3009d850072dSLois Curfman McInnes 301036851e7fSLois Curfman McInnes Level: advanced 301136851e7fSLois Curfman McInnes 30123c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 3013a86d99e1SLois Curfman McInnes 3014a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 30153c7409f5SSatish Balay @*/ 30167087cfbeSBarry Smith PetscErrorCode SNESSetOptionsPrefix(SNES snes,const char prefix[]) 30173c7409f5SSatish Balay { 3018dfbe8321SBarry Smith PetscErrorCode ierr; 30193c7409f5SSatish Balay 30203a40ed3dSBarry Smith PetscFunctionBegin; 30210700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3022639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 30231cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 302494b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 30253a40ed3dSBarry Smith PetscFunctionReturn(0); 30263c7409f5SSatish Balay } 30273c7409f5SSatish Balay 30284a2ae208SSatish Balay #undef __FUNCT__ 30294a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 30303c7409f5SSatish Balay /*@C 3031f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 3032d850072dSLois Curfman McInnes SNES options in the database. 30333c7409f5SSatish Balay 30343f9fe445SBarry Smith Logically Collective on SNES 3035fee21e36SBarry Smith 3036c7afd0dbSLois Curfman McInnes Input Parameters: 3037c7afd0dbSLois Curfman McInnes + snes - the SNES context 3038c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3039c7afd0dbSLois Curfman McInnes 3040d850072dSLois Curfman McInnes Notes: 3041a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3042c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3043d850072dSLois Curfman McInnes 304436851e7fSLois Curfman McInnes Level: advanced 304536851e7fSLois Curfman McInnes 30463c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 3047a86d99e1SLois Curfman McInnes 3048a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 30493c7409f5SSatish Balay @*/ 30507087cfbeSBarry Smith PetscErrorCode SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 30513c7409f5SSatish Balay { 3052dfbe8321SBarry Smith PetscErrorCode ierr; 30533c7409f5SSatish Balay 30543a40ed3dSBarry Smith PetscFunctionBegin; 30550700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3056639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 30571cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 305894b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 30593a40ed3dSBarry Smith PetscFunctionReturn(0); 30603c7409f5SSatish Balay } 30613c7409f5SSatish Balay 30624a2ae208SSatish Balay #undef __FUNCT__ 30634a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 30649ab63eb5SSatish Balay /*@C 30653c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 30663c7409f5SSatish Balay SNES options in the database. 30673c7409f5SSatish Balay 3068c7afd0dbSLois Curfman McInnes Not Collective 3069c7afd0dbSLois Curfman McInnes 30703c7409f5SSatish Balay Input Parameter: 30713c7409f5SSatish Balay . snes - the SNES context 30723c7409f5SSatish Balay 30733c7409f5SSatish Balay Output Parameter: 30743c7409f5SSatish Balay . prefix - pointer to the prefix string used 30753c7409f5SSatish Balay 30764ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 30779ab63eb5SSatish Balay sufficient length to hold the prefix. 30789ab63eb5SSatish Balay 307936851e7fSLois Curfman McInnes Level: advanced 308036851e7fSLois Curfman McInnes 30813c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 3082a86d99e1SLois Curfman McInnes 3083a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 30843c7409f5SSatish Balay @*/ 30857087cfbeSBarry Smith PetscErrorCode SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 30863c7409f5SSatish Balay { 3087dfbe8321SBarry Smith PetscErrorCode ierr; 30883c7409f5SSatish Balay 30893a40ed3dSBarry Smith PetscFunctionBegin; 30900700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3091639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 30923a40ed3dSBarry Smith PetscFunctionReturn(0); 30933c7409f5SSatish Balay } 30943c7409f5SSatish Balay 3095b2002411SLois Curfman McInnes 30964a2ae208SSatish Balay #undef __FUNCT__ 30974a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 30983cea93caSBarry Smith /*@C 30993cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 31003cea93caSBarry Smith 31017f6c08e0SMatthew Knepley Level: advanced 31023cea93caSBarry Smith @*/ 31037087cfbeSBarry Smith PetscErrorCode SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 3104b2002411SLois Curfman McInnes { 3105e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 3106dfbe8321SBarry Smith PetscErrorCode ierr; 3107b2002411SLois Curfman McInnes 3108b2002411SLois Curfman McInnes PetscFunctionBegin; 3109b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 3110c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 3111b2002411SLois Curfman McInnes PetscFunctionReturn(0); 3112b2002411SLois Curfman McInnes } 3113da9b6338SBarry Smith 3114da9b6338SBarry Smith #undef __FUNCT__ 3115da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 31167087cfbeSBarry Smith PetscErrorCode SNESTestLocalMin(SNES snes) 3117da9b6338SBarry Smith { 3118dfbe8321SBarry Smith PetscErrorCode ierr; 311977431f27SBarry Smith PetscInt N,i,j; 3120da9b6338SBarry Smith Vec u,uh,fh; 3121da9b6338SBarry Smith PetscScalar value; 3122da9b6338SBarry Smith PetscReal norm; 3123da9b6338SBarry Smith 3124da9b6338SBarry Smith PetscFunctionBegin; 3125da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 3126da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 3127da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 3128da9b6338SBarry Smith 3129da9b6338SBarry Smith /* currently only works for sequential */ 3130da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 3131da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 3132da9b6338SBarry Smith for (i=0; i<N; i++) { 3133da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 313477431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 3135da9b6338SBarry Smith for (j=-10; j<11; j++) { 3136ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 3137da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 31383ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 3139da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 314077431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 3141da9b6338SBarry Smith value = -value; 3142da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 3143da9b6338SBarry Smith } 3144da9b6338SBarry Smith } 31456bf464f9SBarry Smith ierr = VecDestroy(&uh);CHKERRQ(ierr); 31466bf464f9SBarry Smith ierr = VecDestroy(&fh);CHKERRQ(ierr); 3147da9b6338SBarry Smith PetscFunctionReturn(0); 3148da9b6338SBarry Smith } 314971f87433Sdalcinl 315071f87433Sdalcinl #undef __FUNCT__ 3151fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 315271f87433Sdalcinl /*@ 3153fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 315471f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 315571f87433Sdalcinl Newton method. 315671f87433Sdalcinl 31573f9fe445SBarry Smith Logically Collective on SNES 315871f87433Sdalcinl 315971f87433Sdalcinl Input Parameters: 316071f87433Sdalcinl + snes - SNES context 316171f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 316271f87433Sdalcinl 316364ba62caSBarry Smith Options Database: 316464ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 316564ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 316664ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 316764ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 316864ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 316964ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 317064ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 317164ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 317264ba62caSBarry Smith 317371f87433Sdalcinl Notes: 317471f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 317571f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 317671f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 317771f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 317871f87433Sdalcinl solver. 317971f87433Sdalcinl 318071f87433Sdalcinl Level: advanced 318171f87433Sdalcinl 318271f87433Sdalcinl Reference: 318371f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 318471f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 318571f87433Sdalcinl 318671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 318771f87433Sdalcinl 3188fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 318971f87433Sdalcinl @*/ 31907087cfbeSBarry Smith PetscErrorCode SNESKSPSetUseEW(SNES snes,PetscBool flag) 319171f87433Sdalcinl { 319271f87433Sdalcinl PetscFunctionBegin; 31930700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3194acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flag,2); 319571f87433Sdalcinl snes->ksp_ewconv = flag; 319671f87433Sdalcinl PetscFunctionReturn(0); 319771f87433Sdalcinl } 319871f87433Sdalcinl 319971f87433Sdalcinl #undef __FUNCT__ 3200fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 320171f87433Sdalcinl /*@ 3202fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 320371f87433Sdalcinl for computing relative tolerance for linear solvers within an 320471f87433Sdalcinl inexact Newton method. 320571f87433Sdalcinl 320671f87433Sdalcinl Not Collective 320771f87433Sdalcinl 320871f87433Sdalcinl Input Parameter: 320971f87433Sdalcinl . snes - SNES context 321071f87433Sdalcinl 321171f87433Sdalcinl Output Parameter: 321271f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 321371f87433Sdalcinl 321471f87433Sdalcinl Notes: 321571f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 321671f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 321771f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 321871f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 321971f87433Sdalcinl solver. 322071f87433Sdalcinl 322171f87433Sdalcinl Level: advanced 322271f87433Sdalcinl 322371f87433Sdalcinl Reference: 322471f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 322571f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 322671f87433Sdalcinl 322771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 322871f87433Sdalcinl 3229fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 323071f87433Sdalcinl @*/ 32317087cfbeSBarry Smith PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 323271f87433Sdalcinl { 323371f87433Sdalcinl PetscFunctionBegin; 32340700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 323571f87433Sdalcinl PetscValidPointer(flag,2); 323671f87433Sdalcinl *flag = snes->ksp_ewconv; 323771f87433Sdalcinl PetscFunctionReturn(0); 323871f87433Sdalcinl } 323971f87433Sdalcinl 324071f87433Sdalcinl #undef __FUNCT__ 3241fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 324271f87433Sdalcinl /*@ 3243fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 324471f87433Sdalcinl convergence criteria for the linear solvers within an inexact 324571f87433Sdalcinl Newton method. 324671f87433Sdalcinl 32473f9fe445SBarry Smith Logically Collective on SNES 324871f87433Sdalcinl 324971f87433Sdalcinl Input Parameters: 325071f87433Sdalcinl + snes - SNES context 325171f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 325271f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 325371f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 325471f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 325571f87433Sdalcinl (0 <= gamma2 <= 1) 325671f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 325771f87433Sdalcinl . alpha2 - power for safeguard 325871f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 325971f87433Sdalcinl 326071f87433Sdalcinl Note: 326171f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 326271f87433Sdalcinl 326371f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 326471f87433Sdalcinl 326571f87433Sdalcinl Level: advanced 326671f87433Sdalcinl 326771f87433Sdalcinl Reference: 326871f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 326971f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 327071f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 327171f87433Sdalcinl 327271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 327371f87433Sdalcinl 3274fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 327571f87433Sdalcinl @*/ 32767087cfbeSBarry Smith PetscErrorCode SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 327771f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 327871f87433Sdalcinl { 3279fa9f3622SBarry Smith SNESKSPEW *kctx; 328071f87433Sdalcinl PetscFunctionBegin; 32810700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3282fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3283e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 3284c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,version,2); 3285c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_0,3); 3286c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_max,4); 3287c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,gamma,5); 3288c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha,6); 3289c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha2,7); 3290c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,threshold,8); 329171f87433Sdalcinl 329271f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 329371f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 329471f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 329571f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 329671f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 329771f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 329871f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 329971f87433Sdalcinl 330071f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 3301e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 330271f87433Sdalcinl } 330371f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 3304e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 330571f87433Sdalcinl } 330671f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 3307e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 330871f87433Sdalcinl } 330971f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 3310e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 331171f87433Sdalcinl } 331271f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 3313e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 331471f87433Sdalcinl } 331571f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 3316e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 331771f87433Sdalcinl } 331871f87433Sdalcinl PetscFunctionReturn(0); 331971f87433Sdalcinl } 332071f87433Sdalcinl 332171f87433Sdalcinl #undef __FUNCT__ 3322fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 332371f87433Sdalcinl /*@ 3324fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 332571f87433Sdalcinl convergence criteria for the linear solvers within an inexact 332671f87433Sdalcinl Newton method. 332771f87433Sdalcinl 332871f87433Sdalcinl Not Collective 332971f87433Sdalcinl 333071f87433Sdalcinl Input Parameters: 333171f87433Sdalcinl snes - SNES context 333271f87433Sdalcinl 333371f87433Sdalcinl Output Parameters: 333471f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 333571f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 333671f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 333771f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 333871f87433Sdalcinl (0 <= gamma2 <= 1) 333971f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 334071f87433Sdalcinl . alpha2 - power for safeguard 334171f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 334271f87433Sdalcinl 334371f87433Sdalcinl Level: advanced 334471f87433Sdalcinl 334571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 334671f87433Sdalcinl 3347fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 334871f87433Sdalcinl @*/ 33497087cfbeSBarry Smith PetscErrorCode SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 335071f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 335171f87433Sdalcinl { 3352fa9f3622SBarry Smith SNESKSPEW *kctx; 335371f87433Sdalcinl PetscFunctionBegin; 33540700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3355fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3356e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 335771f87433Sdalcinl if(version) *version = kctx->version; 335871f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 335971f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 336071f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 336171f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 336271f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 336371f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 336471f87433Sdalcinl PetscFunctionReturn(0); 336571f87433Sdalcinl } 336671f87433Sdalcinl 336771f87433Sdalcinl #undef __FUNCT__ 3368fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 3369fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 337071f87433Sdalcinl { 337171f87433Sdalcinl PetscErrorCode ierr; 3372fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 337371f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 337471f87433Sdalcinl 337571f87433Sdalcinl PetscFunctionBegin; 3376e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 337771f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 337871f87433Sdalcinl rtol = kctx->rtol_0; 337971f87433Sdalcinl } else { 338071f87433Sdalcinl if (kctx->version == 1) { 338171f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 338271f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 338371f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 338471f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 338571f87433Sdalcinl } else if (kctx->version == 2) { 338671f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 338771f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 338871f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 338971f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 339071f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 339171f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 339271f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 339371f87433Sdalcinl stol = PetscMax(rtol,stol); 339471f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 339571f87433Sdalcinl /* safeguard: avoid oversolving */ 339671f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 339771f87433Sdalcinl stol = PetscMax(rtol,stol); 339871f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 3399e32f2f54SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 340071f87433Sdalcinl } 340171f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 340271f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 340371f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 340471f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 340571f87433Sdalcinl PetscFunctionReturn(0); 340671f87433Sdalcinl } 340771f87433Sdalcinl 340871f87433Sdalcinl #undef __FUNCT__ 3409fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 3410fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 341171f87433Sdalcinl { 341271f87433Sdalcinl PetscErrorCode ierr; 3413fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 341471f87433Sdalcinl PCSide pcside; 341571f87433Sdalcinl Vec lres; 341671f87433Sdalcinl 341771f87433Sdalcinl PetscFunctionBegin; 3418e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 341971f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 342071f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 342171f87433Sdalcinl if (kctx->version == 1) { 3422b037da10SBarry Smith ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr); 342371f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 342471f87433Sdalcinl /* KSP residual is true linear residual */ 342571f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 342671f87433Sdalcinl } else { 342771f87433Sdalcinl /* KSP residual is preconditioned residual */ 342871f87433Sdalcinl /* compute true linear residual norm */ 342971f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 343071f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 343171f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 343271f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 34336bf464f9SBarry Smith ierr = VecDestroy(&lres);CHKERRQ(ierr); 343471f87433Sdalcinl } 343571f87433Sdalcinl } 343671f87433Sdalcinl PetscFunctionReturn(0); 343771f87433Sdalcinl } 343871f87433Sdalcinl 343971f87433Sdalcinl #undef __FUNCT__ 344071f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 344171f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 344271f87433Sdalcinl { 344371f87433Sdalcinl PetscErrorCode ierr; 344471f87433Sdalcinl 344571f87433Sdalcinl PetscFunctionBegin; 3446fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 344771f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 3448fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 344971f87433Sdalcinl PetscFunctionReturn(0); 345071f87433Sdalcinl } 34516c699258SBarry Smith 34526c699258SBarry Smith #undef __FUNCT__ 34536c699258SBarry Smith #define __FUNCT__ "SNESSetDM" 34546c699258SBarry Smith /*@ 34556c699258SBarry Smith SNESSetDM - Sets the DM that may be used by some preconditioners 34566c699258SBarry Smith 34573f9fe445SBarry Smith Logically Collective on SNES 34586c699258SBarry Smith 34596c699258SBarry Smith Input Parameters: 34606c699258SBarry Smith + snes - the preconditioner context 34616c699258SBarry Smith - dm - the dm 34626c699258SBarry Smith 34636c699258SBarry Smith Level: intermediate 34646c699258SBarry Smith 34656c699258SBarry Smith 34666c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM() 34676c699258SBarry Smith @*/ 34687087cfbeSBarry Smith PetscErrorCode SNESSetDM(SNES snes,DM dm) 34696c699258SBarry Smith { 34706c699258SBarry Smith PetscErrorCode ierr; 3471345fed2cSBarry Smith KSP ksp; 34726c699258SBarry Smith 34736c699258SBarry Smith PetscFunctionBegin; 34740700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3475d0660788SBarry Smith if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);} 34766bf464f9SBarry Smith ierr = DMDestroy(&snes->dm);CHKERRQ(ierr); 34776c699258SBarry Smith snes->dm = dm; 3478345fed2cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 3479345fed2cSBarry Smith ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr); 3480f22f69f0SBarry Smith ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr); 34812c155ee1SBarry Smith if (snes->pc) { 34822c155ee1SBarry Smith ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 34832c155ee1SBarry Smith } 34846c699258SBarry Smith PetscFunctionReturn(0); 34856c699258SBarry Smith } 34866c699258SBarry Smith 34876c699258SBarry Smith #undef __FUNCT__ 34886c699258SBarry Smith #define __FUNCT__ "SNESGetDM" 34896c699258SBarry Smith /*@ 34906c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 34916c699258SBarry Smith 34923f9fe445SBarry Smith Not Collective but DM obtained is parallel on SNES 34936c699258SBarry Smith 34946c699258SBarry Smith Input Parameter: 34956c699258SBarry Smith . snes - the preconditioner context 34966c699258SBarry Smith 34976c699258SBarry Smith Output Parameter: 34986c699258SBarry Smith . dm - the dm 34996c699258SBarry Smith 35006c699258SBarry Smith Level: intermediate 35016c699258SBarry Smith 35026c699258SBarry Smith 35036c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM() 35046c699258SBarry Smith @*/ 35057087cfbeSBarry Smith PetscErrorCode SNESGetDM(SNES snes,DM *dm) 35066c699258SBarry Smith { 35076c699258SBarry Smith PetscFunctionBegin; 35080700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 35096c699258SBarry Smith *dm = snes->dm; 35106c699258SBarry Smith PetscFunctionReturn(0); 35116c699258SBarry Smith } 35120807856dSBarry Smith 351331823bd8SMatthew G Knepley #undef __FUNCT__ 351431823bd8SMatthew G Knepley #define __FUNCT__ "SNESSetPC" 351531823bd8SMatthew G Knepley /*@ 3516fd4b34e9SJed Brown SNESSetPC - Sets the nonlinear preconditioner to be used. 351731823bd8SMatthew G Knepley 351831823bd8SMatthew G Knepley Collective on SNES 351931823bd8SMatthew G Knepley 352031823bd8SMatthew G Knepley Input Parameters: 352131823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate() 352231823bd8SMatthew G Knepley - pc - the preconditioner object 352331823bd8SMatthew G Knepley 352431823bd8SMatthew G Knepley Notes: 352531823bd8SMatthew G Knepley Use SNESGetPC() to retrieve the preconditioner context (for example, 352631823bd8SMatthew G Knepley to configure it using the API). 352731823bd8SMatthew G Knepley 352831823bd8SMatthew G Knepley Level: developer 352931823bd8SMatthew G Knepley 353031823bd8SMatthew G Knepley .keywords: SNES, set, precondition 353131823bd8SMatthew G Knepley .seealso: SNESGetPC() 353231823bd8SMatthew G Knepley @*/ 353331823bd8SMatthew G Knepley PetscErrorCode SNESSetPC(SNES snes, SNES pc) 353431823bd8SMatthew G Knepley { 353531823bd8SMatthew G Knepley PetscErrorCode ierr; 353631823bd8SMatthew G Knepley 353731823bd8SMatthew G Knepley PetscFunctionBegin; 353831823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 353931823bd8SMatthew G Knepley PetscValidHeaderSpecific(pc, SNES_CLASSID, 2); 354031823bd8SMatthew G Knepley PetscCheckSameComm(snes, 1, pc, 2); 354131823bd8SMatthew G Knepley ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr); 3542bf11d3b6SBarry Smith ierr = SNESDestroy(&snes->pc);CHKERRQ(ierr); 354331823bd8SMatthew G Knepley snes->pc = pc; 354431823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 354531823bd8SMatthew G Knepley PetscFunctionReturn(0); 354631823bd8SMatthew G Knepley } 354731823bd8SMatthew G Knepley 354831823bd8SMatthew G Knepley #undef __FUNCT__ 354931823bd8SMatthew G Knepley #define __FUNCT__ "SNESGetPC" 355031823bd8SMatthew G Knepley /*@ 3551fd4b34e9SJed Brown SNESGetPC - Returns a pointer to the nonlinear preconditioning context set with SNESSetPC(). 355231823bd8SMatthew G Knepley 355331823bd8SMatthew G Knepley Not Collective 355431823bd8SMatthew G Knepley 355531823bd8SMatthew G Knepley Input Parameter: 355631823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate() 355731823bd8SMatthew G Knepley 355831823bd8SMatthew G Knepley Output Parameter: 355931823bd8SMatthew G Knepley . pc - preconditioner context 356031823bd8SMatthew G Knepley 356131823bd8SMatthew G Knepley Level: developer 356231823bd8SMatthew G Knepley 356331823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner 356431823bd8SMatthew G Knepley .seealso: SNESSetPC() 356531823bd8SMatthew G Knepley @*/ 356631823bd8SMatthew G Knepley PetscErrorCode SNESGetPC(SNES snes, SNES *pc) 356731823bd8SMatthew G Knepley { 356831823bd8SMatthew G Knepley PetscErrorCode ierr; 356931823bd8SMatthew G Knepley 357031823bd8SMatthew G Knepley PetscFunctionBegin; 357131823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 357231823bd8SMatthew G Knepley PetscValidPointer(pc, 2); 357331823bd8SMatthew G Knepley if (!snes->pc) { 357431823bd8SMatthew G Knepley ierr = SNESCreate(((PetscObject) snes)->comm, &snes->pc);CHKERRQ(ierr); 35754a0c5b0cSMatthew G Knepley ierr = PetscObjectIncrementTabLevel((PetscObject) snes->pc, (PetscObject) snes, 1);CHKERRQ(ierr); 357631823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 357731823bd8SMatthew G Knepley } 357831823bd8SMatthew G Knepley *pc = snes->pc; 357931823bd8SMatthew G Knepley PetscFunctionReturn(0); 358031823bd8SMatthew G Knepley } 358131823bd8SMatthew G Knepley 358269b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3583c6db04a5SJed Brown #include <mex.h> 358469b4f73cSBarry Smith 35858f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 35868f6e6473SBarry Smith 35870807856dSBarry Smith #undef __FUNCT__ 35880807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab" 35890807856dSBarry Smith /* 35900807856dSBarry Smith SNESComputeFunction_Matlab - Calls the function that has been set with 35910807856dSBarry Smith SNESSetFunctionMatlab(). 35920807856dSBarry Smith 35930807856dSBarry Smith Collective on SNES 35940807856dSBarry Smith 35950807856dSBarry Smith Input Parameters: 35960807856dSBarry Smith + snes - the SNES context 35970807856dSBarry Smith - x - input vector 35980807856dSBarry Smith 35990807856dSBarry Smith Output Parameter: 36000807856dSBarry Smith . y - function vector, as set by SNESSetFunction() 36010807856dSBarry Smith 36020807856dSBarry Smith Notes: 36030807856dSBarry Smith SNESComputeFunction() is typically used within nonlinear solvers 36040807856dSBarry Smith implementations, so most users would not generally call this routine 36050807856dSBarry Smith themselves. 36060807856dSBarry Smith 36070807856dSBarry Smith Level: developer 36080807856dSBarry Smith 36090807856dSBarry Smith .keywords: SNES, nonlinear, compute, function 36100807856dSBarry Smith 36110807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 361261b2408cSBarry Smith */ 36137087cfbeSBarry Smith PetscErrorCode SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx) 36140807856dSBarry Smith { 3615e650e774SBarry Smith PetscErrorCode ierr; 36168f6e6473SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 36178f6e6473SBarry Smith int nlhs = 1,nrhs = 5; 36188f6e6473SBarry Smith mxArray *plhs[1],*prhs[5]; 361991621f2eSBarry Smith long long int lx = 0,ly = 0,ls = 0; 3620e650e774SBarry Smith 36210807856dSBarry Smith PetscFunctionBegin; 36220807856dSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 36230807856dSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 36240807856dSBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 36250807856dSBarry Smith PetscCheckSameComm(snes,1,x,2); 36260807856dSBarry Smith PetscCheckSameComm(snes,1,y,3); 36270807856dSBarry Smith 36280807856dSBarry Smith /* call Matlab function in ctx with arguments x and y */ 3629e650e774SBarry Smith 363091621f2eSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3631e650e774SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3632e650e774SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 363391621f2eSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 363491621f2eSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 363591621f2eSBarry Smith prhs[2] = mxCreateDoubleScalar((double)ly); 36368f6e6473SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 36378f6e6473SBarry Smith prhs[4] = sctx->ctx; 3638b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr); 3639e650e774SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3640e650e774SBarry Smith mxDestroyArray(prhs[0]); 3641e650e774SBarry Smith mxDestroyArray(prhs[1]); 3642e650e774SBarry Smith mxDestroyArray(prhs[2]); 36438f6e6473SBarry Smith mxDestroyArray(prhs[3]); 3644e650e774SBarry Smith mxDestroyArray(plhs[0]); 36450807856dSBarry Smith PetscFunctionReturn(0); 36460807856dSBarry Smith } 36470807856dSBarry Smith 36480807856dSBarry Smith 36490807856dSBarry Smith #undef __FUNCT__ 36500807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab" 365161b2408cSBarry Smith /* 36520807856dSBarry Smith SNESSetFunctionMatlab - Sets the function evaluation routine and function 36530807856dSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3654e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 36550807856dSBarry Smith 36560807856dSBarry Smith Logically Collective on SNES 36570807856dSBarry Smith 36580807856dSBarry Smith Input Parameters: 36590807856dSBarry Smith + snes - the SNES context 36600807856dSBarry Smith . r - vector to store function value 36610807856dSBarry Smith - func - function evaluation routine 36620807856dSBarry Smith 36630807856dSBarry Smith Calling sequence of func: 366461b2408cSBarry Smith $ func (SNES snes,Vec x,Vec f,void *ctx); 36650807856dSBarry Smith 36660807856dSBarry Smith 36670807856dSBarry Smith Notes: 36680807856dSBarry Smith The Newton-like methods typically solve linear systems of the form 36690807856dSBarry Smith $ f'(x) x = -f(x), 36700807856dSBarry Smith where f'(x) denotes the Jacobian matrix and f(x) is the function. 36710807856dSBarry Smith 36720807856dSBarry Smith Level: beginner 36730807856dSBarry Smith 36740807856dSBarry Smith .keywords: SNES, nonlinear, set, function 36750807856dSBarry Smith 36760807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 367761b2408cSBarry Smith */ 36787087cfbeSBarry Smith PetscErrorCode SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx) 36790807856dSBarry Smith { 36800807856dSBarry Smith PetscErrorCode ierr; 36818f6e6473SBarry Smith SNESMatlabContext *sctx; 36820807856dSBarry Smith 36830807856dSBarry Smith PetscFunctionBegin; 36848f6e6473SBarry Smith /* currently sctx is memory bleed */ 36858f6e6473SBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 36868f6e6473SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 36878f6e6473SBarry Smith /* 36888f6e6473SBarry Smith This should work, but it doesn't 36898f6e6473SBarry Smith sctx->ctx = ctx; 36908f6e6473SBarry Smith mexMakeArrayPersistent(sctx->ctx); 36918f6e6473SBarry Smith */ 36928f6e6473SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 36938f6e6473SBarry Smith ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr); 36940807856dSBarry Smith PetscFunctionReturn(0); 36950807856dSBarry Smith } 369669b4f73cSBarry Smith 369761b2408cSBarry Smith #undef __FUNCT__ 369861b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab" 369961b2408cSBarry Smith /* 370061b2408cSBarry Smith SNESComputeJacobian_Matlab - Calls the function that has been set with 370161b2408cSBarry Smith SNESSetJacobianMatlab(). 370261b2408cSBarry Smith 370361b2408cSBarry Smith Collective on SNES 370461b2408cSBarry Smith 370561b2408cSBarry Smith Input Parameters: 370661b2408cSBarry Smith + snes - the SNES context 370761b2408cSBarry Smith . x - input vector 370861b2408cSBarry Smith . A, B - the matrices 370961b2408cSBarry Smith - ctx - user context 371061b2408cSBarry Smith 371161b2408cSBarry Smith Output Parameter: 371261b2408cSBarry Smith . flag - structure of the matrix 371361b2408cSBarry Smith 371461b2408cSBarry Smith Level: developer 371561b2408cSBarry Smith 371661b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function 371761b2408cSBarry Smith 371861b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 371961b2408cSBarry Smith @*/ 37207087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx) 372161b2408cSBarry Smith { 372261b2408cSBarry Smith PetscErrorCode ierr; 372361b2408cSBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 372461b2408cSBarry Smith int nlhs = 2,nrhs = 6; 372561b2408cSBarry Smith mxArray *plhs[2],*prhs[6]; 372661b2408cSBarry Smith long long int lx = 0,lA = 0,ls = 0, lB = 0; 372761b2408cSBarry Smith 372861b2408cSBarry Smith PetscFunctionBegin; 372961b2408cSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 373061b2408cSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 373161b2408cSBarry Smith 373261b2408cSBarry Smith /* call Matlab function in ctx with arguments x and y */ 373361b2408cSBarry Smith 373461b2408cSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 373561b2408cSBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 373661b2408cSBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 373761b2408cSBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 373861b2408cSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 373961b2408cSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 374061b2408cSBarry Smith prhs[2] = mxCreateDoubleScalar((double)lA); 374161b2408cSBarry Smith prhs[3] = mxCreateDoubleScalar((double)lB); 374261b2408cSBarry Smith prhs[4] = mxCreateString(sctx->funcname); 374361b2408cSBarry Smith prhs[5] = sctx->ctx; 3744b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr); 374561b2408cSBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 374661b2408cSBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 374761b2408cSBarry Smith mxDestroyArray(prhs[0]); 374861b2408cSBarry Smith mxDestroyArray(prhs[1]); 374961b2408cSBarry Smith mxDestroyArray(prhs[2]); 375061b2408cSBarry Smith mxDestroyArray(prhs[3]); 375161b2408cSBarry Smith mxDestroyArray(prhs[4]); 375261b2408cSBarry Smith mxDestroyArray(plhs[0]); 375361b2408cSBarry Smith mxDestroyArray(plhs[1]); 375461b2408cSBarry Smith PetscFunctionReturn(0); 375561b2408cSBarry Smith } 375661b2408cSBarry Smith 375761b2408cSBarry Smith 375861b2408cSBarry Smith #undef __FUNCT__ 375961b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab" 376061b2408cSBarry Smith /* 376161b2408cSBarry Smith SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 376261b2408cSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3763e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 376461b2408cSBarry Smith 376561b2408cSBarry Smith Logically Collective on SNES 376661b2408cSBarry Smith 376761b2408cSBarry Smith Input Parameters: 376861b2408cSBarry Smith + snes - the SNES context 376961b2408cSBarry Smith . A,B - Jacobian matrices 377061b2408cSBarry Smith . func - function evaluation routine 377161b2408cSBarry Smith - ctx - user context 377261b2408cSBarry Smith 377361b2408cSBarry Smith Calling sequence of func: 377461b2408cSBarry Smith $ flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx); 377561b2408cSBarry Smith 377661b2408cSBarry Smith 377761b2408cSBarry Smith Level: developer 377861b2408cSBarry Smith 377961b2408cSBarry Smith .keywords: SNES, nonlinear, set, function 378061b2408cSBarry Smith 378161b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 378261b2408cSBarry Smith */ 37837087cfbeSBarry Smith PetscErrorCode SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx) 378461b2408cSBarry Smith { 378561b2408cSBarry Smith PetscErrorCode ierr; 378661b2408cSBarry Smith SNESMatlabContext *sctx; 378761b2408cSBarry Smith 378861b2408cSBarry Smith PetscFunctionBegin; 378961b2408cSBarry Smith /* currently sctx is memory bleed */ 379061b2408cSBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 379161b2408cSBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 379261b2408cSBarry Smith /* 379361b2408cSBarry Smith This should work, but it doesn't 379461b2408cSBarry Smith sctx->ctx = ctx; 379561b2408cSBarry Smith mexMakeArrayPersistent(sctx->ctx); 379661b2408cSBarry Smith */ 379761b2408cSBarry Smith sctx->ctx = mxDuplicateArray(ctx); 379861b2408cSBarry Smith ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 379961b2408cSBarry Smith PetscFunctionReturn(0); 380061b2408cSBarry Smith } 380169b4f73cSBarry Smith 3802f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3803f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab" 3804f9eb7ae2SShri Abhyankar /* 3805f9eb7ae2SShri Abhyankar SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab(). 3806f9eb7ae2SShri Abhyankar 3807f9eb7ae2SShri Abhyankar Collective on SNES 3808f9eb7ae2SShri Abhyankar 3809f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction() 3810f9eb7ae2SShri Abhyankar @*/ 38117087cfbeSBarry Smith PetscErrorCode SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx) 3812f9eb7ae2SShri Abhyankar { 3813f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 381448f37e70SShri Abhyankar SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 3815f9eb7ae2SShri Abhyankar int nlhs = 1,nrhs = 6; 3816f9eb7ae2SShri Abhyankar mxArray *plhs[1],*prhs[6]; 3817f9eb7ae2SShri Abhyankar long long int lx = 0,ls = 0; 3818f9eb7ae2SShri Abhyankar Vec x=snes->vec_sol; 3819f9eb7ae2SShri Abhyankar 3820f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3821f9eb7ae2SShri Abhyankar PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3822f9eb7ae2SShri Abhyankar 3823f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3824f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3825f9eb7ae2SShri Abhyankar prhs[0] = mxCreateDoubleScalar((double)ls); 3826f9eb7ae2SShri Abhyankar prhs[1] = mxCreateDoubleScalar((double)it); 3827f9eb7ae2SShri Abhyankar prhs[2] = mxCreateDoubleScalar((double)fnorm); 3828f9eb7ae2SShri Abhyankar prhs[3] = mxCreateDoubleScalar((double)lx); 3829f9eb7ae2SShri Abhyankar prhs[4] = mxCreateString(sctx->funcname); 3830f9eb7ae2SShri Abhyankar prhs[5] = sctx->ctx; 3831f9eb7ae2SShri Abhyankar ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr); 3832f9eb7ae2SShri Abhyankar ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3833f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[0]); 3834f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[1]); 3835f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[2]); 3836f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[3]); 3837f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[4]); 3838f9eb7ae2SShri Abhyankar mxDestroyArray(plhs[0]); 3839f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 3840f9eb7ae2SShri Abhyankar } 3841f9eb7ae2SShri Abhyankar 3842f9eb7ae2SShri Abhyankar 3843f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3844f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab" 3845f9eb7ae2SShri Abhyankar /* 3846e3c5b3baSBarry Smith SNESMonitorSetMatlab - Sets the monitor function from MATLAB 3847f9eb7ae2SShri Abhyankar 3848f9eb7ae2SShri Abhyankar Level: developer 3849f9eb7ae2SShri Abhyankar 3850f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function 3851f9eb7ae2SShri Abhyankar 3852f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 3853f9eb7ae2SShri Abhyankar */ 38547087cfbeSBarry Smith PetscErrorCode SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx) 3855f9eb7ae2SShri Abhyankar { 3856f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 3857f9eb7ae2SShri Abhyankar SNESMatlabContext *sctx; 3858f9eb7ae2SShri Abhyankar 3859f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3860f9eb7ae2SShri Abhyankar /* currently sctx is memory bleed */ 3861f9eb7ae2SShri Abhyankar ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 3862f9eb7ae2SShri Abhyankar ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3863f9eb7ae2SShri Abhyankar /* 3864f9eb7ae2SShri Abhyankar This should work, but it doesn't 3865f9eb7ae2SShri Abhyankar sctx->ctx = ctx; 3866f9eb7ae2SShri Abhyankar mexMakeArrayPersistent(sctx->ctx); 3867f9eb7ae2SShri Abhyankar */ 3868f9eb7ae2SShri Abhyankar sctx->ctx = mxDuplicateArray(ctx); 3869f9eb7ae2SShri Abhyankar ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 3870f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 3871f9eb7ae2SShri Abhyankar } 3872f9eb7ae2SShri Abhyankar 387369b4f73cSBarry Smith #endif 3874