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: 308ea630c6eSPeter 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 { 356ea630c6eSPeter 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 493ea630c6eSPeter Brune /* line search options */ 494ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_alpha","Constant function norm must decrease by","None",snes->ls_alpha,&snes->ls_alpha,0);CHKERRQ(ierr); 495ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_maxstep","Step must be less than","None",snes->maxstep,&snes->maxstep,0);CHKERRQ(ierr); 496ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_steptol","Minimum lambda allowed","None",snes->steptol,&snes->steptol,0);CHKERRQ(ierr); 497ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_damping","Damping parameter","SNES",snes->damping,&snes->damping,&flg);CHKERRQ(ierr); 498ea630c6eSPeter Brune ierr = PetscOptionsBool("-snes_ls_monitor","Print progress of line searches","SNESLineSearchSetMonitor",snes->ls_monitor ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 499ea630c6eSPeter Brune if (set) {ierr = SNESLineSearchSetMonitor(snes,flg);CHKERRQ(ierr);} 500ea630c6eSPeter Brune ierr = PetscOptionsEnum("-snes_ls","Line search used","SNESLineSearchSet",SNESLineSearchTypes,(PetscEnum)SNES_LS_CUBIC,(PetscEnum*)&indx,&flg);CHKERRQ(ierr); 501ea630c6eSPeter Brune ierr = SNESLineSearchSetType(snes,(SNESLineSearchType)indx);CHKERRQ(ierr); 502ea630c6eSPeter Brune 503*8e3fc8c0SJed Brown flg = snes->ops->precheckstep == SNESLineSearchPreCheckPicard ? PETSC_TRUE : PETSC_FALSE; 504*8e3fc8c0SJed Brown ierr = PetscOptionsBool("-snes_ls_precheck_picard","Use a correction that sometimes improves convergence of Picard iteration","SNESLineSearchPreCheckPicard",flg,&flg,&set);CHKERRQ(ierr); 505*8e3fc8c0SJed Brown if (set) { 506*8e3fc8c0SJed Brown if (flg) { 507*8e3fc8c0SJed Brown snes->precheck_picard_angle = 10.; /* correction only active if angle is less than 10 degrees */ 508*8e3fc8c0SJed Brown ierr = PetscOptionsReal("-snes_ls_precheck_picard_angle","Maximum angle at which to activate the correction","none",snes->precheck_picard_angle,&snes->precheck_picard_angle,PETSC_NULL);CHKERRQ(ierr); 509*8e3fc8c0SJed Brown ierr = SNESLineSearchSetPreCheck(snes,SNESLineSearchPreCheckPicard,&snes->precheck_picard_angle);CHKERRQ(ierr); 510*8e3fc8c0SJed Brown } else { 511*8e3fc8c0SJed Brown ierr = SNESLineSearchSetPreCheck(snes,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 512*8e3fc8c0SJed Brown } 513*8e3fc8c0SJed Brown } 514*8e3fc8c0SJed Brown 51576b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 51676b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 51776b2cf59SMatthew Knepley } 51876b2cf59SMatthew Knepley 519e7788613SBarry Smith if (snes->ops->setfromoptions) { 520e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 521639f9d9dSBarry Smith } 5225d973c19SBarry Smith 5235d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 5245d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr); 525b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 5264bbc92c1SBarry Smith 527aa3661deSLisandro Dalcin if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); } 5281cee3971SBarry Smith 5291cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 530aa3661deSLisandro Dalcin ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag); 531aa3661deSLisandro Dalcin ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr); 53285385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 53393993e2dSLois Curfman McInnes 53451e86f29SPeter Brune /* if someone has set the SNES PC type, create it. */ 53551e86f29SPeter Brune ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr); 53651e86f29SPeter Brune ierr = PetscOptionsHasName(optionsprefix, "-npc_snes_type", &pcset);CHKERRQ(ierr); 53751e86f29SPeter Brune if (pcset && (!snes->pc)) { 53851e86f29SPeter Brune ierr = SNESGetPC(snes, &snes->pc);CHKERRQ(ierr); 53951e86f29SPeter Brune } 54051e86f29SPeter Brune 5414a0c5b0cSMatthew G Knepley if (snes->pc) { 5424a0c5b0cSMatthew G Knepley ierr = SNESSetOptionsPrefix(snes->pc, "npc_");CHKERRQ(ierr); 5434a0c5b0cSMatthew G Knepley ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 5444a0c5b0cSMatthew G Knepley /* Should we make a duplicate vector and matrix? Leave the DM to make it? */ 5454a0c5b0cSMatthew G Knepley ierr = SNESSetFunction(snes->pc, snes->vec_func, snes->ops->computefunction, snes->funP);CHKERRQ(ierr); 5464a0c5b0cSMatthew G Knepley ierr = SNESSetJacobian(snes->pc, snes->jacobian, snes->jacobian_pre, snes->ops->computejacobian, snes->jacP);CHKERRQ(ierr); 5474a0c5b0cSMatthew G Knepley ierr = SNESSetFromOptions(snes->pc);CHKERRQ(ierr); 5484a0c5b0cSMatthew G Knepley } 5493a40ed3dSBarry Smith PetscFunctionReturn(0); 5509b94acceSBarry Smith } 5519b94acceSBarry Smith 552d25893d9SBarry Smith #undef __FUNCT__ 553d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext" 554d25893d9SBarry Smith /*@ 555d25893d9SBarry Smith SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for 556d25893d9SBarry Smith the nonlinear solvers. 557d25893d9SBarry Smith 558d25893d9SBarry Smith Logically Collective on SNES 559d25893d9SBarry Smith 560d25893d9SBarry Smith Input Parameters: 561d25893d9SBarry Smith + snes - the SNES context 562d25893d9SBarry Smith . compute - function to compute the context 563d25893d9SBarry Smith - destroy - function to destroy the context 564d25893d9SBarry Smith 565d25893d9SBarry Smith Level: intermediate 566d25893d9SBarry Smith 567d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context 568d25893d9SBarry Smith 569d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext() 570d25893d9SBarry Smith @*/ 571d25893d9SBarry Smith PetscErrorCode SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**)) 572d25893d9SBarry Smith { 573d25893d9SBarry Smith PetscFunctionBegin; 574d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 575d25893d9SBarry Smith snes->ops->usercompute = compute; 576d25893d9SBarry Smith snes->ops->userdestroy = destroy; 577d25893d9SBarry Smith PetscFunctionReturn(0); 578d25893d9SBarry Smith } 579a847f771SSatish Balay 5804a2ae208SSatish Balay #undef __FUNCT__ 5814a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 582b07ff414SBarry Smith /*@ 5839b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 5849b94acceSBarry Smith the nonlinear solvers. 5859b94acceSBarry Smith 5863f9fe445SBarry Smith Logically Collective on SNES 587fee21e36SBarry Smith 588c7afd0dbSLois Curfman McInnes Input Parameters: 589c7afd0dbSLois Curfman McInnes + snes - the SNES context 590c7afd0dbSLois Curfman McInnes - usrP - optional user context 591c7afd0dbSLois Curfman McInnes 59236851e7fSLois Curfman McInnes Level: intermediate 59336851e7fSLois Curfman McInnes 5949b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 5959b94acceSBarry Smith 596d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext() 5979b94acceSBarry Smith @*/ 5987087cfbeSBarry Smith PetscErrorCode SNESSetApplicationContext(SNES snes,void *usrP) 5999b94acceSBarry Smith { 6001b2093e4SBarry Smith PetscErrorCode ierr; 601b07ff414SBarry Smith KSP ksp; 6021b2093e4SBarry Smith 6033a40ed3dSBarry Smith PetscFunctionBegin; 6040700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 605b07ff414SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 606b07ff414SBarry Smith ierr = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr); 6079b94acceSBarry Smith snes->user = usrP; 6083a40ed3dSBarry Smith PetscFunctionReturn(0); 6099b94acceSBarry Smith } 61074679c65SBarry Smith 6114a2ae208SSatish Balay #undef __FUNCT__ 6124a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 613b07ff414SBarry Smith /*@ 6149b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 6159b94acceSBarry Smith nonlinear solvers. 6169b94acceSBarry Smith 617c7afd0dbSLois Curfman McInnes Not Collective 618c7afd0dbSLois Curfman McInnes 6199b94acceSBarry Smith Input Parameter: 6209b94acceSBarry Smith . snes - SNES context 6219b94acceSBarry Smith 6229b94acceSBarry Smith Output Parameter: 6239b94acceSBarry Smith . usrP - user context 6249b94acceSBarry Smith 62536851e7fSLois Curfman McInnes Level: intermediate 62636851e7fSLois Curfman McInnes 6279b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 6289b94acceSBarry Smith 6299b94acceSBarry Smith .seealso: SNESSetApplicationContext() 6309b94acceSBarry Smith @*/ 631e71120c6SJed Brown PetscErrorCode SNESGetApplicationContext(SNES snes,void *usrP) 6329b94acceSBarry Smith { 6333a40ed3dSBarry Smith PetscFunctionBegin; 6340700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 635e71120c6SJed Brown *(void**)usrP = snes->user; 6363a40ed3dSBarry Smith PetscFunctionReturn(0); 6379b94acceSBarry Smith } 63874679c65SBarry Smith 6394a2ae208SSatish Balay #undef __FUNCT__ 6404a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 6419b94acceSBarry Smith /*@ 642c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 643c8228a4eSBarry Smith at this time. 6449b94acceSBarry Smith 645c7afd0dbSLois Curfman McInnes Not Collective 646c7afd0dbSLois Curfman McInnes 6479b94acceSBarry Smith Input Parameter: 6489b94acceSBarry Smith . snes - SNES context 6499b94acceSBarry Smith 6509b94acceSBarry Smith Output Parameter: 6519b94acceSBarry Smith . iter - iteration number 6529b94acceSBarry Smith 653c8228a4eSBarry Smith Notes: 654c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 655c8228a4eSBarry Smith 656c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 65708405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 65808405cd6SLois Curfman McInnes .vb 65908405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 66008405cd6SLois Curfman McInnes if (!(it % 2)) { 66108405cd6SLois Curfman McInnes [compute Jacobian here] 66208405cd6SLois Curfman McInnes } 66308405cd6SLois Curfman McInnes .ve 664c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 66508405cd6SLois Curfman McInnes recomputed every second SNES iteration. 666c8228a4eSBarry Smith 66736851e7fSLois Curfman McInnes Level: intermediate 66836851e7fSLois Curfman McInnes 6692b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 6702b668275SBarry Smith 671b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 6729b94acceSBarry Smith @*/ 6737087cfbeSBarry Smith PetscErrorCode SNESGetIterationNumber(SNES snes,PetscInt* iter) 6749b94acceSBarry Smith { 6753a40ed3dSBarry Smith PetscFunctionBegin; 6760700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6774482741eSBarry Smith PetscValidIntPointer(iter,2); 6789b94acceSBarry Smith *iter = snes->iter; 6793a40ed3dSBarry Smith PetscFunctionReturn(0); 6809b94acceSBarry Smith } 68174679c65SBarry Smith 6824a2ae208SSatish Balay #undef __FUNCT__ 6834a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 6849b94acceSBarry Smith /*@ 6859b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 6869b94acceSBarry Smith with SNESSSetFunction(). 6879b94acceSBarry Smith 688c7afd0dbSLois Curfman McInnes Collective on SNES 689c7afd0dbSLois Curfman McInnes 6909b94acceSBarry Smith Input Parameter: 6919b94acceSBarry Smith . snes - SNES context 6929b94acceSBarry Smith 6939b94acceSBarry Smith Output Parameter: 6949b94acceSBarry Smith . fnorm - 2-norm of function 6959b94acceSBarry Smith 69636851e7fSLois Curfman McInnes Level: intermediate 69736851e7fSLois Curfman McInnes 6989b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 699a86d99e1SLois Curfman McInnes 700b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 7019b94acceSBarry Smith @*/ 7027087cfbeSBarry Smith PetscErrorCode SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 7039b94acceSBarry Smith { 7043a40ed3dSBarry Smith PetscFunctionBegin; 7050700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7064482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 7079b94acceSBarry Smith *fnorm = snes->norm; 7083a40ed3dSBarry Smith PetscFunctionReturn(0); 7099b94acceSBarry Smith } 71074679c65SBarry Smith 7114a2ae208SSatish Balay #undef __FUNCT__ 712b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 7139b94acceSBarry Smith /*@ 714b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 7159b94acceSBarry Smith attempted by the nonlinear solver. 7169b94acceSBarry Smith 717c7afd0dbSLois Curfman McInnes Not Collective 718c7afd0dbSLois Curfman McInnes 7199b94acceSBarry Smith Input Parameter: 7209b94acceSBarry Smith . snes - SNES context 7219b94acceSBarry Smith 7229b94acceSBarry Smith Output Parameter: 7239b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 7249b94acceSBarry Smith 725c96a6f78SLois Curfman McInnes Notes: 726c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 727c96a6f78SLois Curfman McInnes 72836851e7fSLois Curfman McInnes Level: intermediate 72936851e7fSLois Curfman McInnes 7309b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 73158ebbce7SBarry Smith 732e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 73358ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 7349b94acceSBarry Smith @*/ 7357087cfbeSBarry Smith PetscErrorCode SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 7369b94acceSBarry Smith { 7373a40ed3dSBarry Smith PetscFunctionBegin; 7380700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7394482741eSBarry Smith PetscValidIntPointer(nfails,2); 74050ffb88aSMatthew Knepley *nfails = snes->numFailures; 74150ffb88aSMatthew Knepley PetscFunctionReturn(0); 74250ffb88aSMatthew Knepley } 74350ffb88aSMatthew Knepley 74450ffb88aSMatthew Knepley #undef __FUNCT__ 745b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 74650ffb88aSMatthew Knepley /*@ 747b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 74850ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 74950ffb88aSMatthew Knepley 75050ffb88aSMatthew Knepley Not Collective 75150ffb88aSMatthew Knepley 75250ffb88aSMatthew Knepley Input Parameters: 75350ffb88aSMatthew Knepley + snes - SNES context 75450ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 75550ffb88aSMatthew Knepley 75650ffb88aSMatthew Knepley Level: intermediate 75750ffb88aSMatthew Knepley 75850ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 75958ebbce7SBarry Smith 760e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 76158ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 76250ffb88aSMatthew Knepley @*/ 7637087cfbeSBarry Smith PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 76450ffb88aSMatthew Knepley { 76550ffb88aSMatthew Knepley PetscFunctionBegin; 7660700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 76750ffb88aSMatthew Knepley snes->maxFailures = maxFails; 76850ffb88aSMatthew Knepley PetscFunctionReturn(0); 76950ffb88aSMatthew Knepley } 77050ffb88aSMatthew Knepley 77150ffb88aSMatthew Knepley #undef __FUNCT__ 772b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 77350ffb88aSMatthew Knepley /*@ 774b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 77550ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 77650ffb88aSMatthew Knepley 77750ffb88aSMatthew Knepley Not Collective 77850ffb88aSMatthew Knepley 77950ffb88aSMatthew Knepley Input Parameter: 78050ffb88aSMatthew Knepley . snes - SNES context 78150ffb88aSMatthew Knepley 78250ffb88aSMatthew Knepley Output Parameter: 78350ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 78450ffb88aSMatthew Knepley 78550ffb88aSMatthew Knepley Level: intermediate 78650ffb88aSMatthew Knepley 78750ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 78858ebbce7SBarry Smith 789e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 79058ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 79158ebbce7SBarry Smith 79250ffb88aSMatthew Knepley @*/ 7937087cfbeSBarry Smith PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 79450ffb88aSMatthew Knepley { 79550ffb88aSMatthew Knepley PetscFunctionBegin; 7960700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7974482741eSBarry Smith PetscValidIntPointer(maxFails,2); 79850ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 7993a40ed3dSBarry Smith PetscFunctionReturn(0); 8009b94acceSBarry Smith } 801a847f771SSatish Balay 8024a2ae208SSatish Balay #undef __FUNCT__ 8032541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 8042541af92SBarry Smith /*@ 8052541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 8062541af92SBarry Smith done by SNES. 8072541af92SBarry Smith 8082541af92SBarry Smith Not Collective 8092541af92SBarry Smith 8102541af92SBarry Smith Input Parameter: 8112541af92SBarry Smith . snes - SNES context 8122541af92SBarry Smith 8132541af92SBarry Smith Output Parameter: 8142541af92SBarry Smith . nfuncs - number of evaluations 8152541af92SBarry Smith 8162541af92SBarry Smith Level: intermediate 8172541af92SBarry Smith 8182541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 81958ebbce7SBarry Smith 820e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 8212541af92SBarry Smith @*/ 8227087cfbeSBarry Smith PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 8232541af92SBarry Smith { 8242541af92SBarry Smith PetscFunctionBegin; 8250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8262541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 8272541af92SBarry Smith *nfuncs = snes->nfuncs; 8282541af92SBarry Smith PetscFunctionReturn(0); 8292541af92SBarry Smith } 8302541af92SBarry Smith 8312541af92SBarry Smith #undef __FUNCT__ 8323d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 8333d4c4710SBarry Smith /*@ 8343d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 8353d4c4710SBarry Smith linear solvers. 8363d4c4710SBarry Smith 8373d4c4710SBarry Smith Not Collective 8383d4c4710SBarry Smith 8393d4c4710SBarry Smith Input Parameter: 8403d4c4710SBarry Smith . snes - SNES context 8413d4c4710SBarry Smith 8423d4c4710SBarry Smith Output Parameter: 8433d4c4710SBarry Smith . nfails - number of failed solves 8443d4c4710SBarry Smith 8453d4c4710SBarry Smith Notes: 8463d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 8473d4c4710SBarry Smith 8483d4c4710SBarry Smith Level: intermediate 8493d4c4710SBarry Smith 8503d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 85158ebbce7SBarry Smith 852e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 8533d4c4710SBarry Smith @*/ 8547087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 8553d4c4710SBarry Smith { 8563d4c4710SBarry Smith PetscFunctionBegin; 8570700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8583d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 8593d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 8603d4c4710SBarry Smith PetscFunctionReturn(0); 8613d4c4710SBarry Smith } 8623d4c4710SBarry Smith 8633d4c4710SBarry Smith #undef __FUNCT__ 8643d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 8653d4c4710SBarry Smith /*@ 8663d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 8673d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 8683d4c4710SBarry Smith 8693f9fe445SBarry Smith Logically Collective on SNES 8703d4c4710SBarry Smith 8713d4c4710SBarry Smith Input Parameters: 8723d4c4710SBarry Smith + snes - SNES context 8733d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 8743d4c4710SBarry Smith 8753d4c4710SBarry Smith Level: intermediate 8763d4c4710SBarry Smith 877a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 8783d4c4710SBarry Smith 8793d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 8803d4c4710SBarry Smith 88158ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 8823d4c4710SBarry Smith @*/ 8837087cfbeSBarry Smith PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 8843d4c4710SBarry Smith { 8853d4c4710SBarry Smith PetscFunctionBegin; 8860700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 887c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxFails,2); 8883d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 8893d4c4710SBarry Smith PetscFunctionReturn(0); 8903d4c4710SBarry Smith } 8913d4c4710SBarry Smith 8923d4c4710SBarry Smith #undef __FUNCT__ 8933d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 8943d4c4710SBarry Smith /*@ 8953d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 8963d4c4710SBarry Smith are allowed before SNES terminates 8973d4c4710SBarry Smith 8983d4c4710SBarry Smith Not Collective 8993d4c4710SBarry Smith 9003d4c4710SBarry Smith Input Parameter: 9013d4c4710SBarry Smith . snes - SNES context 9023d4c4710SBarry Smith 9033d4c4710SBarry Smith Output Parameter: 9043d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 9053d4c4710SBarry Smith 9063d4c4710SBarry Smith Level: intermediate 9073d4c4710SBarry Smith 9083d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 9093d4c4710SBarry Smith 9103d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 9113d4c4710SBarry Smith 912e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 9133d4c4710SBarry Smith @*/ 9147087cfbeSBarry Smith PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 9153d4c4710SBarry Smith { 9163d4c4710SBarry Smith PetscFunctionBegin; 9170700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9183d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 9193d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 9203d4c4710SBarry Smith PetscFunctionReturn(0); 9213d4c4710SBarry Smith } 9223d4c4710SBarry Smith 9233d4c4710SBarry Smith #undef __FUNCT__ 924b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 925c96a6f78SLois Curfman McInnes /*@ 926b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 927c96a6f78SLois Curfman McInnes used by the nonlinear solver. 928c96a6f78SLois Curfman McInnes 929c7afd0dbSLois Curfman McInnes Not Collective 930c7afd0dbSLois Curfman McInnes 931c96a6f78SLois Curfman McInnes Input Parameter: 932c96a6f78SLois Curfman McInnes . snes - SNES context 933c96a6f78SLois Curfman McInnes 934c96a6f78SLois Curfman McInnes Output Parameter: 935c96a6f78SLois Curfman McInnes . lits - number of linear iterations 936c96a6f78SLois Curfman McInnes 937c96a6f78SLois Curfman McInnes Notes: 938c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 939c96a6f78SLois Curfman McInnes 94036851e7fSLois Curfman McInnes Level: intermediate 94136851e7fSLois Curfman McInnes 942c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 9432b668275SBarry Smith 9448c7482ecSBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 945c96a6f78SLois Curfman McInnes @*/ 9467087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 947c96a6f78SLois Curfman McInnes { 9483a40ed3dSBarry Smith PetscFunctionBegin; 9490700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9504482741eSBarry Smith PetscValidIntPointer(lits,2); 951c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 9523a40ed3dSBarry Smith PetscFunctionReturn(0); 953c96a6f78SLois Curfman McInnes } 954c96a6f78SLois Curfman McInnes 9554a2ae208SSatish Balay #undef __FUNCT__ 95694b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 95752baeb72SSatish Balay /*@ 95894b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 9599b94acceSBarry Smith 96094b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 961c7afd0dbSLois Curfman McInnes 9629b94acceSBarry Smith Input Parameter: 9639b94acceSBarry Smith . snes - the SNES context 9649b94acceSBarry Smith 9659b94acceSBarry Smith Output Parameter: 96694b7f48cSBarry Smith . ksp - the KSP context 9679b94acceSBarry Smith 9689b94acceSBarry Smith Notes: 96994b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 9709b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 9712999313aSBarry Smith PC contexts as well. 9729b94acceSBarry Smith 97336851e7fSLois Curfman McInnes Level: beginner 97436851e7fSLois Curfman McInnes 97594b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 9769b94acceSBarry Smith 9772999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 9789b94acceSBarry Smith @*/ 9797087cfbeSBarry Smith PetscErrorCode SNESGetKSP(SNES snes,KSP *ksp) 9809b94acceSBarry Smith { 9811cee3971SBarry Smith PetscErrorCode ierr; 9821cee3971SBarry Smith 9833a40ed3dSBarry Smith PetscFunctionBegin; 9840700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9854482741eSBarry Smith PetscValidPointer(ksp,2); 9861cee3971SBarry Smith 9871cee3971SBarry Smith if (!snes->ksp) { 9881cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 9891cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 9901cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 9911cee3971SBarry Smith } 99294b7f48cSBarry Smith *ksp = snes->ksp; 9933a40ed3dSBarry Smith PetscFunctionReturn(0); 9949b94acceSBarry Smith } 99582bf6240SBarry Smith 9964a2ae208SSatish Balay #undef __FUNCT__ 9972999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 9982999313aSBarry Smith /*@ 9992999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 10002999313aSBarry Smith 10012999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 10022999313aSBarry Smith 10032999313aSBarry Smith Input Parameters: 10042999313aSBarry Smith + snes - the SNES context 10052999313aSBarry Smith - ksp - the KSP context 10062999313aSBarry Smith 10072999313aSBarry Smith Notes: 10082999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 10092999313aSBarry Smith so this routine is rarely needed. 10102999313aSBarry Smith 10112999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 10122999313aSBarry Smith decreased by one. 10132999313aSBarry Smith 10142999313aSBarry Smith Level: developer 10152999313aSBarry Smith 10162999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 10172999313aSBarry Smith 10182999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 10192999313aSBarry Smith @*/ 10207087cfbeSBarry Smith PetscErrorCode SNESSetKSP(SNES snes,KSP ksp) 10212999313aSBarry Smith { 10222999313aSBarry Smith PetscErrorCode ierr; 10232999313aSBarry Smith 10242999313aSBarry Smith PetscFunctionBegin; 10250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10260700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 10272999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 10287dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 1029906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 10302999313aSBarry Smith snes->ksp = ksp; 10312999313aSBarry Smith PetscFunctionReturn(0); 10322999313aSBarry Smith } 10332999313aSBarry Smith 10347adad957SLisandro Dalcin #if 0 10352999313aSBarry Smith #undef __FUNCT__ 10364a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 10376849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 1038e24b481bSBarry Smith { 1039e24b481bSBarry Smith PetscFunctionBegin; 1040e24b481bSBarry Smith PetscFunctionReturn(0); 1041e24b481bSBarry Smith } 10427adad957SLisandro Dalcin #endif 1043e24b481bSBarry Smith 10449b94acceSBarry Smith /* -----------------------------------------------------------*/ 10454a2ae208SSatish Balay #undef __FUNCT__ 10464a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 104752baeb72SSatish Balay /*@ 10489b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 10499b94acceSBarry Smith 1050c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 1051c7afd0dbSLois Curfman McInnes 1052c7afd0dbSLois Curfman McInnes Input Parameters: 1053906ed7ccSBarry Smith . comm - MPI communicator 10549b94acceSBarry Smith 10559b94acceSBarry Smith Output Parameter: 10569b94acceSBarry Smith . outsnes - the new SNES context 10579b94acceSBarry Smith 1058c7afd0dbSLois Curfman McInnes Options Database Keys: 1059c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 1060c7afd0dbSLois Curfman McInnes and no preconditioning matrix 1061c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 1062c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 1063c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 1064c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 1065c1f60f51SBarry Smith 106636851e7fSLois Curfman McInnes Level: beginner 106736851e7fSLois Curfman McInnes 10689b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 10699b94acceSBarry Smith 1070a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 1071a8054027SBarry Smith 10729b94acceSBarry Smith @*/ 10737087cfbeSBarry Smith PetscErrorCode SNESCreate(MPI_Comm comm,SNES *outsnes) 10749b94acceSBarry Smith { 1075dfbe8321SBarry Smith PetscErrorCode ierr; 10769b94acceSBarry Smith SNES snes; 1077fa9f3622SBarry Smith SNESKSPEW *kctx; 107837fcc0dbSBarry Smith 10793a40ed3dSBarry Smith PetscFunctionBegin; 1080ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 10818ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 10828ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 10838ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 10848ba1e511SMatthew Knepley #endif 10858ba1e511SMatthew Knepley 10863194b578SJed Brown ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 10877adad957SLisandro Dalcin 108885385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 10892c155ee1SBarry Smith snes->usesksp = PETSC_TRUE; 10909b94acceSBarry Smith snes->max_its = 50; 10919750a799SBarry Smith snes->max_funcs = 10000; 10929b94acceSBarry Smith snes->norm = 0.0; 1093b4874afaSBarry Smith snes->rtol = 1.e-8; 1094b4874afaSBarry Smith snes->ttol = 0.0; 109570441072SBarry Smith snes->abstol = 1.e-50; 10969b94acceSBarry Smith snes->xtol = 1.e-8; 10974b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 10989b94acceSBarry Smith snes->nfuncs = 0; 109950ffb88aSMatthew Knepley snes->numFailures = 0; 110050ffb88aSMatthew Knepley snes->maxFailures = 1; 11017a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1102e35cf81dSBarry Smith snes->lagjacobian = 1; 1103a8054027SBarry Smith snes->lagpreconditioner = 1; 1104639f9d9dSBarry Smith snes->numbermonitors = 0; 11059b94acceSBarry Smith snes->data = 0; 11064dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1107186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 11086f24a144SLois Curfman McInnes snes->nwork = 0; 110958c9b817SLisandro Dalcin snes->work = 0; 111058c9b817SLisandro Dalcin snes->nvwork = 0; 111158c9b817SLisandro Dalcin snes->vwork = 0; 1112758f92a0SBarry Smith snes->conv_hist_len = 0; 1113758f92a0SBarry Smith snes->conv_hist_max = 0; 1114758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 1115758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 1116758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1117184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 11189b94acceSBarry Smith 1119ea630c6eSPeter Brune /* initialize the line search options */ 1120ea630c6eSPeter Brune snes->ls_type = SNES_LS_BASIC; 1121ea630c6eSPeter Brune snes->damping = 1.0; 1122ea630c6eSPeter Brune snes->maxstep = 1e8; 1123ea630c6eSPeter Brune snes->steptol = 1e-12; 1124ea630c6eSPeter Brune snes->ls_alpha = 1e-4; 1125ea630c6eSPeter Brune snes->ls_monitor = PETSC_NULL; 1126ea630c6eSPeter Brune 1127ea630c6eSPeter Brune snes->ops->linesearch = PETSC_NULL; 1128ea630c6eSPeter Brune snes->precheck = PETSC_NULL; 1129ea630c6eSPeter Brune snes->ops->precheckstep = PETSC_NULL; 1130ea630c6eSPeter Brune snes->postcheck = PETSC_NULL; 1131ea630c6eSPeter Brune snes->ops->postcheckstep= PETSC_NULL; 1132ea630c6eSPeter Brune 1133ea630c6eSPeter Brune snes->ops->linesearchquadratic = PETSC_NULL; 1134ea630c6eSPeter Brune snes->ops->linesearchcubic = PETSC_NULL; 1135ea630c6eSPeter Brune snes->ops->linesearchno = PETSC_NULL; 1136ea630c6eSPeter Brune snes->ops->linesearchnonorms = PETSC_NULL; 1137ea630c6eSPeter Brune snes->ops->linesearchexact = PETSC_NULL; 1138ea630c6eSPeter Brune snes->ops->linesearchtest = PETSC_NULL; 1139ea630c6eSPeter Brune 11403d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 11413d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 11423d4c4710SBarry Smith 11439b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 114438f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 11459b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 11469b94acceSBarry Smith kctx->version = 2; 11479b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 11489b94acceSBarry Smith this was too large for some test cases */ 114975567043SBarry Smith kctx->rtol_last = 0.0; 11509b94acceSBarry Smith kctx->rtol_max = .9; 11519b94acceSBarry Smith kctx->gamma = 1.0; 115271f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 115371f87433Sdalcinl kctx->alpha2 = kctx->alpha; 11549b94acceSBarry Smith kctx->threshold = .1; 115575567043SBarry Smith kctx->lresid_last = 0.0; 115675567043SBarry Smith kctx->norm_last = 0.0; 11579b94acceSBarry Smith 11589b94acceSBarry Smith *outsnes = snes; 11593a40ed3dSBarry Smith PetscFunctionReturn(0); 11609b94acceSBarry Smith } 11619b94acceSBarry Smith 11624a2ae208SSatish Balay #undef __FUNCT__ 11634a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 11649b94acceSBarry Smith /*@C 11659b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 11669b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 11679b94acceSBarry Smith equations. 11689b94acceSBarry Smith 11693f9fe445SBarry Smith Logically Collective on SNES 1170fee21e36SBarry Smith 1171c7afd0dbSLois Curfman McInnes Input Parameters: 1172c7afd0dbSLois Curfman McInnes + snes - the SNES context 1173c7afd0dbSLois Curfman McInnes . r - vector to store function value 1174de044059SHong Zhang . func - function evaluation routine 1175c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1176c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 11779b94acceSBarry Smith 1178c7afd0dbSLois Curfman McInnes Calling sequence of func: 11798d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 1180c7afd0dbSLois Curfman McInnes 1181313e4042SLois Curfman McInnes . f - function vector 1182c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 11839b94acceSBarry Smith 11849b94acceSBarry Smith Notes: 11859b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 11869b94acceSBarry Smith $ f'(x) x = -f(x), 1187c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 11889b94acceSBarry Smith 118936851e7fSLois Curfman McInnes Level: beginner 119036851e7fSLois Curfman McInnes 11919b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 11929b94acceSBarry Smith 1193a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 11949b94acceSBarry Smith @*/ 11957087cfbeSBarry Smith PetscErrorCode SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 11969b94acceSBarry Smith { 119785385478SLisandro Dalcin PetscErrorCode ierr; 11983a40ed3dSBarry Smith PetscFunctionBegin; 11990700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1200d2a683ecSLisandro Dalcin if (r) { 1201d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1202d2a683ecSLisandro Dalcin PetscCheckSameComm(snes,1,r,2); 120385385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 12046bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 120585385478SLisandro Dalcin snes->vec_func = r; 1206d2a683ecSLisandro Dalcin } else if (!snes->vec_func && snes->dm) { 1207d2a683ecSLisandro Dalcin ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1208d2a683ecSLisandro Dalcin } 1209d2a683ecSLisandro Dalcin if (func) snes->ops->computefunction = func; 1210d2a683ecSLisandro Dalcin if (ctx) snes->funP = ctx; 12113a40ed3dSBarry Smith PetscFunctionReturn(0); 12129b94acceSBarry Smith } 12139b94acceSBarry Smith 1214d25893d9SBarry Smith #undef __FUNCT__ 1215d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess" 1216d25893d9SBarry Smith /*@C 1217d25893d9SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem 1218d25893d9SBarry Smith 1219d25893d9SBarry Smith Logically Collective on SNES 1220d25893d9SBarry Smith 1221d25893d9SBarry Smith Input Parameters: 1222d25893d9SBarry Smith + snes - the SNES context 1223d25893d9SBarry Smith . func - function evaluation routine 1224d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 1225d25893d9SBarry Smith function evaluation routine (may be PETSC_NULL) 1226d25893d9SBarry Smith 1227d25893d9SBarry Smith Calling sequence of func: 1228d25893d9SBarry Smith $ func (SNES snes,Vec x,void *ctx); 1229d25893d9SBarry Smith 1230d25893d9SBarry Smith . f - function vector 1231d25893d9SBarry Smith - ctx - optional user-defined function context 1232d25893d9SBarry Smith 1233d25893d9SBarry Smith Level: intermediate 1234d25893d9SBarry Smith 1235d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function 1236d25893d9SBarry Smith 1237d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 1238d25893d9SBarry Smith @*/ 1239d25893d9SBarry Smith PetscErrorCode SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx) 1240d25893d9SBarry Smith { 1241d25893d9SBarry Smith PetscFunctionBegin; 1242d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1243d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 1244d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 1245d25893d9SBarry Smith PetscFunctionReturn(0); 1246d25893d9SBarry Smith } 1247d25893d9SBarry Smith 12483ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 12493ab0aad5SBarry Smith #undef __FUNCT__ 12501096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 12511096aae1SMatthew Knepley /*@C 12521096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 12531096aae1SMatthew Knepley it assumes a zero right hand side. 12541096aae1SMatthew Knepley 12553f9fe445SBarry Smith Logically Collective on SNES 12561096aae1SMatthew Knepley 12571096aae1SMatthew Knepley Input Parameter: 12581096aae1SMatthew Knepley . snes - the SNES context 12591096aae1SMatthew Knepley 12601096aae1SMatthew Knepley Output Parameter: 1261bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 12621096aae1SMatthew Knepley 12631096aae1SMatthew Knepley Level: intermediate 12641096aae1SMatthew Knepley 12651096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 12661096aae1SMatthew Knepley 126785385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 12681096aae1SMatthew Knepley @*/ 12697087cfbeSBarry Smith PetscErrorCode SNESGetRhs(SNES snes,Vec *rhs) 12701096aae1SMatthew Knepley { 12711096aae1SMatthew Knepley PetscFunctionBegin; 12720700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12731096aae1SMatthew Knepley PetscValidPointer(rhs,2); 127485385478SLisandro Dalcin *rhs = snes->vec_rhs; 12751096aae1SMatthew Knepley PetscFunctionReturn(0); 12761096aae1SMatthew Knepley } 12771096aae1SMatthew Knepley 12781096aae1SMatthew Knepley #undef __FUNCT__ 12794a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 12809b94acceSBarry Smith /*@ 128136851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 12829b94acceSBarry Smith SNESSetFunction(). 12839b94acceSBarry Smith 1284c7afd0dbSLois Curfman McInnes Collective on SNES 1285c7afd0dbSLois Curfman McInnes 12869b94acceSBarry Smith Input Parameters: 1287c7afd0dbSLois Curfman McInnes + snes - the SNES context 1288c7afd0dbSLois Curfman McInnes - x - input vector 12899b94acceSBarry Smith 12909b94acceSBarry Smith Output Parameter: 12913638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 12929b94acceSBarry Smith 12931bffabb2SLois Curfman McInnes Notes: 129436851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 129536851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 129636851e7fSLois Curfman McInnes themselves. 129736851e7fSLois Curfman McInnes 129836851e7fSLois Curfman McInnes Level: developer 129936851e7fSLois Curfman McInnes 13009b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 13019b94acceSBarry Smith 1302a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 13039b94acceSBarry Smith @*/ 13047087cfbeSBarry Smith PetscErrorCode SNESComputeFunction(SNES snes,Vec x,Vec y) 13059b94acceSBarry Smith { 1306dfbe8321SBarry Smith PetscErrorCode ierr; 13079b94acceSBarry Smith 13083a40ed3dSBarry Smith PetscFunctionBegin; 13090700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13100700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 13110700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 1312c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1313c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 1314184914b5SBarry Smith 1315d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1316e7788613SBarry Smith if (snes->ops->computefunction) { 1317d64ed03dSBarry Smith PetscStackPush("SNES user function"); 131839d508bbSBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1319d64ed03dSBarry Smith PetscStackPop; 132073250ac0SBarry Smith } else if (snes->dm) { 1321644e2e5bSBarry Smith ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr); 1322c90fad12SPeter Brune } else if (snes->vec_rhs) { 1323c90fad12SPeter Brune ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 1324644e2e5bSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve()."); 132585385478SLisandro Dalcin if (snes->vec_rhs) { 132685385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 13273ab0aad5SBarry Smith } 1328ae3c334cSLois Curfman McInnes snes->nfuncs++; 1329d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 13303a40ed3dSBarry Smith PetscFunctionReturn(0); 13319b94acceSBarry Smith } 13329b94acceSBarry Smith 13334a2ae208SSatish Balay #undef __FUNCT__ 13344a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 133562fef451SLois Curfman McInnes /*@ 133662fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 133762fef451SLois Curfman McInnes set with SNESSetJacobian(). 133862fef451SLois Curfman McInnes 1339c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1340c7afd0dbSLois Curfman McInnes 134162fef451SLois Curfman McInnes Input Parameters: 1342c7afd0dbSLois Curfman McInnes + snes - the SNES context 1343c7afd0dbSLois Curfman McInnes - x - input vector 134462fef451SLois Curfman McInnes 134562fef451SLois Curfman McInnes Output Parameters: 1346c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 134762fef451SLois Curfman McInnes . B - optional preconditioning matrix 13482b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1349fee21e36SBarry Smith 1350e35cf81dSBarry Smith Options Database Keys: 1351e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1352693365a8SJed Brown . -snes_lag_jacobian <lag> 1353693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 1354693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 1355693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 1356693365a8SJed Brown - -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 1357e35cf81dSBarry Smith 135862fef451SLois Curfman McInnes Notes: 135962fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 136062fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 136162fef451SLois Curfman McInnes 136294b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1363dc5a77f8SLois Curfman McInnes flag parameter. 136462fef451SLois Curfman McInnes 136536851e7fSLois Curfman McInnes Level: developer 136636851e7fSLois Curfman McInnes 136762fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 136862fef451SLois Curfman McInnes 1369e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 137062fef451SLois Curfman McInnes @*/ 13717087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 13729b94acceSBarry Smith { 1373dfbe8321SBarry Smith PetscErrorCode ierr; 1374ace3abfcSBarry Smith PetscBool flag; 13753a40ed3dSBarry Smith 13763a40ed3dSBarry Smith PetscFunctionBegin; 13770700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13780700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 13794482741eSBarry Smith PetscValidPointer(flg,5); 1380c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1381e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1382ebd3b9afSBarry Smith 1383ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1384ebd3b9afSBarry Smith 1385fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1386fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1387fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1388fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1389e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1390e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1391ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1392ebd3b9afSBarry Smith if (flag) { 1393ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1394ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1395ebd3b9afSBarry Smith } 1396e35cf81dSBarry Smith PetscFunctionReturn(0); 1397e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1398e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1399e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1400ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1401ebd3b9afSBarry Smith if (flag) { 1402ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1403ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1404ebd3b9afSBarry Smith } 1405e35cf81dSBarry Smith PetscFunctionReturn(0); 1406e35cf81dSBarry Smith } 1407e35cf81dSBarry Smith 1408c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1409e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1410d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1411e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1412d64ed03dSBarry Smith PetscStackPop; 1413d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1414a8054027SBarry Smith 14153b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 14163b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 14173b4f5425SBarry Smith snes->lagpreconditioner = -1; 14183b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1419a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1420a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1421a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1422a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1423a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1424a8054027SBarry Smith } 1425a8054027SBarry Smith 14266d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 14270700a824SBarry Smith /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 14280700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,4); */ 1429693365a8SJed Brown { 1430693365a8SJed Brown PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE; 1431693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr); 1432693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 1433693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 1434693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr); 1435693365a8SJed Brown if (flag || flag_draw || flag_contour) { 1436693365a8SJed Brown Mat Bexp_mine = PETSC_NULL,Bexp,FDexp; 1437693365a8SJed Brown MatStructure mstruct; 1438693365a8SJed Brown PetscViewer vdraw,vstdout; 14396b3a5b13SJed Brown PetscBool flg; 1440693365a8SJed Brown if (flag_operator) { 1441693365a8SJed Brown ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr); 1442693365a8SJed Brown Bexp = Bexp_mine; 1443693365a8SJed Brown } else { 1444693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 1445693365a8SJed Brown ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr); 1446693365a8SJed Brown if (flg) Bexp = *B; 1447693365a8SJed Brown else { 1448693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 1449693365a8SJed Brown ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr); 1450693365a8SJed Brown Bexp = Bexp_mine; 1451693365a8SJed Brown } 1452693365a8SJed Brown } 1453693365a8SJed Brown ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr); 1454693365a8SJed Brown ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr); 1455693365a8SJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 1456693365a8SJed Brown if (flag_draw || flag_contour) { 1457693365a8SJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 1458693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 1459693365a8SJed Brown } else vdraw = PETSC_NULL; 1460693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr); 1461693365a8SJed Brown if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);} 1462693365a8SJed Brown if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);} 1463693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr); 1464693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1465693365a8SJed Brown if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);} 1466693365a8SJed Brown ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 1467693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr); 1468693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1469693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 1470693365a8SJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 1471693365a8SJed Brown ierr = MatView(FDexp,vdraw);CHKERRQ(ierr); 1472693365a8SJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 1473693365a8SJed Brown } 1474693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 1475693365a8SJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 1476693365a8SJed Brown ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr); 1477693365a8SJed Brown ierr = MatDestroy(&FDexp);CHKERRQ(ierr); 1478693365a8SJed Brown } 1479693365a8SJed Brown } 14803a40ed3dSBarry Smith PetscFunctionReturn(0); 14819b94acceSBarry Smith } 14829b94acceSBarry Smith 14834a2ae208SSatish Balay #undef __FUNCT__ 14844a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 14859b94acceSBarry Smith /*@C 14869b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1487044dda88SLois Curfman McInnes location to store the matrix. 14889b94acceSBarry Smith 14893f9fe445SBarry Smith Logically Collective on SNES and Mat 1490c7afd0dbSLois Curfman McInnes 14919b94acceSBarry Smith Input Parameters: 1492c7afd0dbSLois Curfman McInnes + snes - the SNES context 14939b94acceSBarry Smith . A - Jacobian matrix 14949b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 1495efd51863SBarry Smith . func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value) 1496c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1497efd51863SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value) 14989b94acceSBarry Smith 14999b94acceSBarry Smith Calling sequence of func: 15008d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 15019b94acceSBarry Smith 1502c7afd0dbSLois Curfman McInnes + x - input vector 15039b94acceSBarry Smith . A - Jacobian matrix 15049b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1505ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 15062b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1507c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 15089b94acceSBarry Smith 15099b94acceSBarry Smith Notes: 151094b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 15112cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1512ac21db08SLois Curfman McInnes 1513ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 15149b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 15159b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 15169b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 15179b94acceSBarry Smith throughout the global iterations. 15189b94acceSBarry Smith 151916913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 152016913363SBarry Smith each matrix. 152116913363SBarry Smith 1522a8a26c1eSJed Brown If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument 1523a8a26c1eSJed Brown must be a MatFDColoring. 1524a8a26c1eSJed Brown 1525c3cc8fd1SJed Brown Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian. One common 1526c3cc8fd1SJed Brown example is to use the "Picard linearization" which only differentiates through the highest order parts of each term. 1527c3cc8fd1SJed Brown 152836851e7fSLois Curfman McInnes Level: beginner 152936851e7fSLois Curfman McInnes 15309b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 15319b94acceSBarry Smith 15323ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 15339b94acceSBarry Smith @*/ 15347087cfbeSBarry Smith PetscErrorCode SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 15359b94acceSBarry Smith { 1536dfbe8321SBarry Smith PetscErrorCode ierr; 15373a7fca6bSBarry Smith 15383a40ed3dSBarry Smith PetscFunctionBegin; 15390700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15400700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 15410700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1542c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 154306975374SJed Brown if (B) PetscCheckSameComm(snes,1,B,3); 1544e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 15453a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 15463a7fca6bSBarry Smith if (A) { 15477dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 15486bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 15499b94acceSBarry Smith snes->jacobian = A; 15503a7fca6bSBarry Smith } 15513a7fca6bSBarry Smith if (B) { 15527dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 15536bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 15549b94acceSBarry Smith snes->jacobian_pre = B; 15553a7fca6bSBarry Smith } 15563a40ed3dSBarry Smith PetscFunctionReturn(0); 15579b94acceSBarry Smith } 155862fef451SLois Curfman McInnes 15594a2ae208SSatish Balay #undef __FUNCT__ 15604a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1561c2aafc4cSSatish Balay /*@C 1562b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1563b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1564b4fd4287SBarry Smith 1565c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1566c7afd0dbSLois Curfman McInnes 1567b4fd4287SBarry Smith Input Parameter: 1568b4fd4287SBarry Smith . snes - the nonlinear solver context 1569b4fd4287SBarry Smith 1570b4fd4287SBarry Smith Output Parameters: 1571c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1572b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 157370e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 157470e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1575fee21e36SBarry Smith 157636851e7fSLois Curfman McInnes Level: advanced 157736851e7fSLois Curfman McInnes 1578b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1579b4fd4287SBarry Smith @*/ 15807087cfbeSBarry Smith PetscErrorCode SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1581b4fd4287SBarry Smith { 15823a40ed3dSBarry Smith PetscFunctionBegin; 15830700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1584b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1585b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1586e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 158770e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 15883a40ed3dSBarry Smith PetscFunctionReturn(0); 1589b4fd4287SBarry Smith } 1590b4fd4287SBarry Smith 15919b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 15929b94acceSBarry Smith 15934a2ae208SSatish Balay #undef __FUNCT__ 15944a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 15959b94acceSBarry Smith /*@ 15969b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1597272ac6f2SLois Curfman McInnes of a nonlinear solver. 15989b94acceSBarry Smith 1599fee21e36SBarry Smith Collective on SNES 1600fee21e36SBarry Smith 1601c7afd0dbSLois Curfman McInnes Input Parameters: 160270e92668SMatthew Knepley . snes - the SNES context 1603c7afd0dbSLois Curfman McInnes 1604272ac6f2SLois Curfman McInnes Notes: 1605272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1606272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1607272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1608272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1609272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1610272ac6f2SLois Curfman McInnes 161136851e7fSLois Curfman McInnes Level: advanced 161236851e7fSLois Curfman McInnes 16139b94acceSBarry Smith .keywords: SNES, nonlinear, setup 16149b94acceSBarry Smith 16159b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 16169b94acceSBarry Smith @*/ 16177087cfbeSBarry Smith PetscErrorCode SNESSetUp(SNES snes) 16189b94acceSBarry Smith { 1619dfbe8321SBarry Smith PetscErrorCode ierr; 16203a40ed3dSBarry Smith 16213a40ed3dSBarry Smith PetscFunctionBegin; 16220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 16234dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 16249b94acceSBarry Smith 16257adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 162685385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 162785385478SLisandro Dalcin } 162885385478SLisandro Dalcin 1629c9b9fda1SJed Brown if (!snes->vec_func) { 1630c9b9fda1SJed Brown if (snes->vec_rhs) { 1631c9b9fda1SJed Brown ierr = VecDuplicate(snes->vec_rhs,&snes->vec_func);CHKERRQ(ierr); 1632c9b9fda1SJed Brown } else if (snes->vec_sol) { 1633c9b9fda1SJed Brown ierr = VecDuplicate(snes->vec_sol,&snes->vec_func);CHKERRQ(ierr); 1634c9b9fda1SJed Brown } else if (snes->dm) { 1635efd51863SBarry Smith ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1636efd51863SBarry Smith } 1637c9b9fda1SJed Brown } 163817186662SBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 163958c9b817SLisandro Dalcin if (snes->vec_rhs == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector"); 164058c9b817SLisandro Dalcin 164158c9b817SLisandro Dalcin if (!snes->vec_sol_update /* && snes->vec_sol */) { 164258c9b817SLisandro Dalcin ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr); 164358c9b817SLisandro Dalcin ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr); 164458c9b817SLisandro Dalcin } 164558c9b817SLisandro Dalcin 1646ef8dffc7SBarry Smith if (!snes->ops->computejacobian && snes->dm) { 1647ef8dffc7SBarry Smith Mat J; 1648ef8dffc7SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 1649cab2e9ccSBarry Smith ierr = SNESSetJacobian(snes,J,J,SNESDMComputeJacobian,PETSC_NULL);CHKERRQ(ierr); 1650cab2e9ccSBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 16512ef29e96SBarry Smith } else if (!snes->jacobian && snes->ops->computejacobian == MatMFFDComputeJacobian) { 1652a8248277SBarry Smith Mat J; 1653a8248277SBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 1654a8248277SBarry Smith ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 1655a8248277SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 1656a8248277SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 1657a8248277SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1658a8248277SBarry Smith } else if (snes->dm && snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) { 1659a8248277SBarry Smith Mat J,B; 1660a8248277SBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 1661a8248277SBarry Smith ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 1662a8248277SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 1663a8248277SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr); 1664a8248277SBarry Smith ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,snes->funP);CHKERRQ(ierr); 1665a8248277SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1666a8248277SBarry Smith ierr = MatDestroy(&B);CHKERRQ(ierr); 1667efd51863SBarry Smith } else if (snes->dm && !snes->jacobian_pre){ 1668efd51863SBarry Smith Mat J; 1669efd51863SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 16703cbb28f5SBarry Smith ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 1671efd51863SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1672ef8dffc7SBarry Smith } 1673cfaf3a74SBarry 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()"); 1674c9b9fda1SJed Brown if (!snes->vec_func) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a vector when calling SNESSetFunction() or call SNESSetDM()"); 1675efd51863SBarry Smith 1676b710008aSBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);} 1677b710008aSBarry Smith 1678d25893d9SBarry Smith if (snes->ops->usercompute && !snes->user) { 1679d25893d9SBarry Smith ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr); 1680d25893d9SBarry Smith } 1681d25893d9SBarry Smith 1682410397dcSLisandro Dalcin if (snes->ops->setup) { 1683410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1684410397dcSLisandro Dalcin } 168558c9b817SLisandro Dalcin 16867aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 16873a40ed3dSBarry Smith PetscFunctionReturn(0); 16889b94acceSBarry Smith } 16899b94acceSBarry Smith 16904a2ae208SSatish Balay #undef __FUNCT__ 169137596af1SLisandro Dalcin #define __FUNCT__ "SNESReset" 169237596af1SLisandro Dalcin /*@ 169337596af1SLisandro Dalcin SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats 169437596af1SLisandro Dalcin 169537596af1SLisandro Dalcin Collective on SNES 169637596af1SLisandro Dalcin 169737596af1SLisandro Dalcin Input Parameter: 169837596af1SLisandro Dalcin . snes - iterative context obtained from SNESCreate() 169937596af1SLisandro Dalcin 1700d25893d9SBarry Smith Level: intermediate 1701d25893d9SBarry Smith 1702d25893d9SBarry Smith Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext() 170337596af1SLisandro Dalcin 170437596af1SLisandro Dalcin .keywords: SNES, destroy 170537596af1SLisandro Dalcin 170637596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 170737596af1SLisandro Dalcin @*/ 170837596af1SLisandro Dalcin PetscErrorCode SNESReset(SNES snes) 170937596af1SLisandro Dalcin { 171037596af1SLisandro Dalcin PetscErrorCode ierr; 171137596af1SLisandro Dalcin 171237596af1SLisandro Dalcin PetscFunctionBegin; 171337596af1SLisandro Dalcin PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1714d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 1715d25893d9SBarry Smith ierr = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr); 1716d25893d9SBarry Smith snes->user = PETSC_NULL; 1717d25893d9SBarry Smith } 17188a23116dSBarry Smith if (snes->pc) { 17198a23116dSBarry Smith ierr = SNESReset(snes->pc);CHKERRQ(ierr); 17208a23116dSBarry Smith } 17218a23116dSBarry Smith 172237596af1SLisandro Dalcin if (snes->ops->reset) { 172337596af1SLisandro Dalcin ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr); 172437596af1SLisandro Dalcin } 172537596af1SLisandro Dalcin if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);} 17266bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 17276bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 17286bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr); 17296bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 17306bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 17316bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 173237596af1SLisandro Dalcin if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);} 173337596af1SLisandro Dalcin if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);} 173437596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 173537596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 173637596af1SLisandro Dalcin PetscFunctionReturn(0); 173737596af1SLisandro Dalcin } 173837596af1SLisandro Dalcin 173937596af1SLisandro Dalcin #undef __FUNCT__ 17404a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 174152baeb72SSatish Balay /*@ 17429b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 17439b94acceSBarry Smith with SNESCreate(). 17449b94acceSBarry Smith 1745c7afd0dbSLois Curfman McInnes Collective on SNES 1746c7afd0dbSLois Curfman McInnes 17479b94acceSBarry Smith Input Parameter: 17489b94acceSBarry Smith . snes - the SNES context 17499b94acceSBarry Smith 175036851e7fSLois Curfman McInnes Level: beginner 175136851e7fSLois Curfman McInnes 17529b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 17539b94acceSBarry Smith 175463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 17559b94acceSBarry Smith @*/ 17566bf464f9SBarry Smith PetscErrorCode SNESDestroy(SNES *snes) 17579b94acceSBarry Smith { 17586849ba73SBarry Smith PetscErrorCode ierr; 17593a40ed3dSBarry Smith 17603a40ed3dSBarry Smith PetscFunctionBegin; 17616bf464f9SBarry Smith if (!*snes) PetscFunctionReturn(0); 17626bf464f9SBarry Smith PetscValidHeaderSpecific((*snes),SNES_CLASSID,1); 17636bf464f9SBarry Smith if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);} 1764d4bb536fSBarry Smith 17656bf464f9SBarry Smith ierr = SNESReset((*snes));CHKERRQ(ierr); 17668a23116dSBarry Smith ierr = SNESDestroy(&(*snes)->pc);CHKERRQ(ierr); 17676b8b9a38SLisandro Dalcin 1768be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 17696bf464f9SBarry Smith ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr); 17706bf464f9SBarry Smith if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);} 17716d4c513bSLisandro Dalcin 17726bf464f9SBarry Smith ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr); 17736bf464f9SBarry Smith ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr); 17746b8b9a38SLisandro Dalcin 17756bf464f9SBarry Smith ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr); 17766bf464f9SBarry Smith if ((*snes)->ops->convergeddestroy) { 17776bf464f9SBarry Smith ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr); 17786b8b9a38SLisandro Dalcin } 17796bf464f9SBarry Smith if ((*snes)->conv_malloc) { 17806bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr); 17816bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr); 178258c9b817SLisandro Dalcin } 1783ea630c6eSPeter Brune ierr = PetscViewerDestroy(&(*snes)->ls_monitor);CHKERRQ(ierr); 17846bf464f9SBarry Smith ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr); 1785a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 17863a40ed3dSBarry Smith PetscFunctionReturn(0); 17879b94acceSBarry Smith } 17889b94acceSBarry Smith 17899b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 17909b94acceSBarry Smith 17914a2ae208SSatish Balay #undef __FUNCT__ 1792a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1793a8054027SBarry Smith /*@ 1794a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1795a8054027SBarry Smith 17963f9fe445SBarry Smith Logically Collective on SNES 1797a8054027SBarry Smith 1798a8054027SBarry Smith Input Parameters: 1799a8054027SBarry Smith + snes - the SNES context 1800a8054027SBarry 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 18013b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1802a8054027SBarry Smith 1803a8054027SBarry Smith Options Database Keys: 1804a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1805a8054027SBarry Smith 1806a8054027SBarry Smith Notes: 1807a8054027SBarry Smith The default is 1 1808a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1809a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1810a8054027SBarry Smith 1811a8054027SBarry Smith Level: intermediate 1812a8054027SBarry Smith 1813a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1814a8054027SBarry Smith 1815e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1816a8054027SBarry Smith 1817a8054027SBarry Smith @*/ 18187087cfbeSBarry Smith PetscErrorCode SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1819a8054027SBarry Smith { 1820a8054027SBarry Smith PetscFunctionBegin; 18210700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1822e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1823e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1824c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 1825a8054027SBarry Smith snes->lagpreconditioner = lag; 1826a8054027SBarry Smith PetscFunctionReturn(0); 1827a8054027SBarry Smith } 1828a8054027SBarry Smith 1829a8054027SBarry Smith #undef __FUNCT__ 1830efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence" 1831efd51863SBarry Smith /*@ 1832efd51863SBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does 1833efd51863SBarry Smith 1834efd51863SBarry Smith Logically Collective on SNES 1835efd51863SBarry Smith 1836efd51863SBarry Smith Input Parameters: 1837efd51863SBarry Smith + snes - the SNES context 1838efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 1839efd51863SBarry Smith 1840efd51863SBarry Smith Options Database Keys: 1841efd51863SBarry Smith . -snes_grid_sequence <steps> 1842efd51863SBarry Smith 1843efd51863SBarry Smith Level: intermediate 1844efd51863SBarry Smith 1845efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1846efd51863SBarry Smith 1847efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1848efd51863SBarry Smith 1849efd51863SBarry Smith @*/ 1850efd51863SBarry Smith PetscErrorCode SNESSetGridSequence(SNES snes,PetscInt steps) 1851efd51863SBarry Smith { 1852efd51863SBarry Smith PetscFunctionBegin; 1853efd51863SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1854efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes,steps,2); 1855efd51863SBarry Smith snes->gridsequence = steps; 1856efd51863SBarry Smith PetscFunctionReturn(0); 1857efd51863SBarry Smith } 1858efd51863SBarry Smith 1859efd51863SBarry Smith #undef __FUNCT__ 1860a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1861a8054027SBarry Smith /*@ 1862a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1863a8054027SBarry Smith 18643f9fe445SBarry Smith Not Collective 1865a8054027SBarry Smith 1866a8054027SBarry Smith Input Parameter: 1867a8054027SBarry Smith . snes - the SNES context 1868a8054027SBarry Smith 1869a8054027SBarry Smith Output Parameter: 1870a8054027SBarry 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 18713b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1872a8054027SBarry Smith 1873a8054027SBarry Smith Options Database Keys: 1874a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1875a8054027SBarry Smith 1876a8054027SBarry Smith Notes: 1877a8054027SBarry Smith The default is 1 1878a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1879a8054027SBarry Smith 1880a8054027SBarry Smith Level: intermediate 1881a8054027SBarry Smith 1882a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1883a8054027SBarry Smith 1884a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1885a8054027SBarry Smith 1886a8054027SBarry Smith @*/ 18877087cfbeSBarry Smith PetscErrorCode SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1888a8054027SBarry Smith { 1889a8054027SBarry Smith PetscFunctionBegin; 18900700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1891a8054027SBarry Smith *lag = snes->lagpreconditioner; 1892a8054027SBarry Smith PetscFunctionReturn(0); 1893a8054027SBarry Smith } 1894a8054027SBarry Smith 1895a8054027SBarry Smith #undef __FUNCT__ 1896e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 1897e35cf81dSBarry Smith /*@ 1898e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 1899e35cf81dSBarry Smith often the preconditioner is rebuilt. 1900e35cf81dSBarry Smith 19013f9fe445SBarry Smith Logically Collective on SNES 1902e35cf81dSBarry Smith 1903e35cf81dSBarry Smith Input Parameters: 1904e35cf81dSBarry Smith + snes - the SNES context 1905e35cf81dSBarry 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 1906fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 1907e35cf81dSBarry Smith 1908e35cf81dSBarry Smith Options Database Keys: 1909e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1910e35cf81dSBarry Smith 1911e35cf81dSBarry Smith Notes: 1912e35cf81dSBarry Smith The default is 1 1913e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1914fe3ffe1eSBarry 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 1915fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 1916e35cf81dSBarry Smith 1917e35cf81dSBarry Smith Level: intermediate 1918e35cf81dSBarry Smith 1919e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1920e35cf81dSBarry Smith 1921e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 1922e35cf81dSBarry Smith 1923e35cf81dSBarry Smith @*/ 19247087cfbeSBarry Smith PetscErrorCode SNESSetLagJacobian(SNES snes,PetscInt lag) 1925e35cf81dSBarry Smith { 1926e35cf81dSBarry Smith PetscFunctionBegin; 19270700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1928e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1929e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1930c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 1931e35cf81dSBarry Smith snes->lagjacobian = lag; 1932e35cf81dSBarry Smith PetscFunctionReturn(0); 1933e35cf81dSBarry Smith } 1934e35cf81dSBarry Smith 1935e35cf81dSBarry Smith #undef __FUNCT__ 1936e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 1937e35cf81dSBarry Smith /*@ 1938e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 1939e35cf81dSBarry Smith 19403f9fe445SBarry Smith Not Collective 1941e35cf81dSBarry Smith 1942e35cf81dSBarry Smith Input Parameter: 1943e35cf81dSBarry Smith . snes - the SNES context 1944e35cf81dSBarry Smith 1945e35cf81dSBarry Smith Output Parameter: 1946e35cf81dSBarry 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 1947e35cf81dSBarry Smith the Jacobian is built etc. 1948e35cf81dSBarry Smith 1949e35cf81dSBarry Smith Options Database Keys: 1950e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1951e35cf81dSBarry Smith 1952e35cf81dSBarry Smith Notes: 1953e35cf81dSBarry Smith The default is 1 1954e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1955e35cf81dSBarry Smith 1956e35cf81dSBarry Smith Level: intermediate 1957e35cf81dSBarry Smith 1958e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1959e35cf81dSBarry Smith 1960e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 1961e35cf81dSBarry Smith 1962e35cf81dSBarry Smith @*/ 19637087cfbeSBarry Smith PetscErrorCode SNESGetLagJacobian(SNES snes,PetscInt *lag) 1964e35cf81dSBarry Smith { 1965e35cf81dSBarry Smith PetscFunctionBegin; 19660700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1967e35cf81dSBarry Smith *lag = snes->lagjacobian; 1968e35cf81dSBarry Smith PetscFunctionReturn(0); 1969e35cf81dSBarry Smith } 1970e35cf81dSBarry Smith 1971e35cf81dSBarry Smith #undef __FUNCT__ 19724a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 19739b94acceSBarry Smith /*@ 1974d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 19759b94acceSBarry Smith 19763f9fe445SBarry Smith Logically Collective on SNES 1977c7afd0dbSLois Curfman McInnes 19789b94acceSBarry Smith Input Parameters: 1979c7afd0dbSLois Curfman McInnes + snes - the SNES context 198070441072SBarry Smith . abstol - absolute convergence tolerance 198133174efeSLois Curfman McInnes . rtol - relative convergence tolerance 198233174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 198333174efeSLois Curfman McInnes of the change in the solution between steps 198433174efeSLois Curfman McInnes . maxit - maximum number of iterations 1985c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1986fee21e36SBarry Smith 198733174efeSLois Curfman McInnes Options Database Keys: 198870441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1989c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1990c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1991c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1992c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 19939b94acceSBarry Smith 1994d7a720efSLois Curfman McInnes Notes: 19959b94acceSBarry Smith The default maximum number of iterations is 50. 19969b94acceSBarry Smith The default maximum number of function evaluations is 1000. 19979b94acceSBarry Smith 199836851e7fSLois Curfman McInnes Level: intermediate 199936851e7fSLois Curfman McInnes 200033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 20019b94acceSBarry Smith 20022492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 20039b94acceSBarry Smith @*/ 20047087cfbeSBarry Smith PetscErrorCode SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 20059b94acceSBarry Smith { 20063a40ed3dSBarry Smith PetscFunctionBegin; 20070700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2008c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,abstol,2); 2009c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol,3); 2010c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,stol,4); 2011c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxit,5); 2012c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxf,6); 2013c5eb9154SBarry Smith 2014ab54825eSJed Brown if (abstol != PETSC_DEFAULT) { 2015ab54825eSJed Brown if (abstol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %G must be non-negative",abstol); 2016ab54825eSJed Brown snes->abstol = abstol; 2017ab54825eSJed Brown } 2018ab54825eSJed Brown if (rtol != PETSC_DEFAULT) { 2019ab54825eSJed 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); 2020ab54825eSJed Brown snes->rtol = rtol; 2021ab54825eSJed Brown } 2022ab54825eSJed Brown if (stol != PETSC_DEFAULT) { 2023ab54825eSJed Brown if (stol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %G must be non-negative",stol); 2024ab54825eSJed Brown snes->xtol = stol; 2025ab54825eSJed Brown } 2026ab54825eSJed Brown if (maxit != PETSC_DEFAULT) { 2027ab54825eSJed Brown if (maxit < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit); 2028ab54825eSJed Brown snes->max_its = maxit; 2029ab54825eSJed Brown } 2030ab54825eSJed Brown if (maxf != PETSC_DEFAULT) { 2031ab54825eSJed Brown if (maxf < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be non-negative",maxf); 2032ab54825eSJed Brown snes->max_funcs = maxf; 2033ab54825eSJed Brown } 20343a40ed3dSBarry Smith PetscFunctionReturn(0); 20359b94acceSBarry Smith } 20369b94acceSBarry Smith 20374a2ae208SSatish Balay #undef __FUNCT__ 20384a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 20399b94acceSBarry Smith /*@ 204033174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 204133174efeSLois Curfman McInnes 2042c7afd0dbSLois Curfman McInnes Not Collective 2043c7afd0dbSLois Curfman McInnes 204433174efeSLois Curfman McInnes Input Parameters: 2045c7afd0dbSLois Curfman McInnes + snes - the SNES context 204685385478SLisandro Dalcin . atol - absolute convergence tolerance 204733174efeSLois Curfman McInnes . rtol - relative convergence tolerance 204833174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 204933174efeSLois Curfman McInnes of the change in the solution between steps 205033174efeSLois Curfman McInnes . maxit - maximum number of iterations 2051c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 2052fee21e36SBarry Smith 205333174efeSLois Curfman McInnes Notes: 205433174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 205533174efeSLois Curfman McInnes 205636851e7fSLois Curfman McInnes Level: intermediate 205736851e7fSLois Curfman McInnes 205833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 205933174efeSLois Curfman McInnes 206033174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 206133174efeSLois Curfman McInnes @*/ 20627087cfbeSBarry Smith PetscErrorCode SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 206333174efeSLois Curfman McInnes { 20643a40ed3dSBarry Smith PetscFunctionBegin; 20650700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 206685385478SLisandro Dalcin if (atol) *atol = snes->abstol; 206733174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 206833174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 206933174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 207033174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 20713a40ed3dSBarry Smith PetscFunctionReturn(0); 207233174efeSLois Curfman McInnes } 207333174efeSLois Curfman McInnes 20744a2ae208SSatish Balay #undef __FUNCT__ 20754a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 207633174efeSLois Curfman McInnes /*@ 20779b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 20789b94acceSBarry Smith 20793f9fe445SBarry Smith Logically Collective on SNES 2080fee21e36SBarry Smith 2081c7afd0dbSLois Curfman McInnes Input Parameters: 2082c7afd0dbSLois Curfman McInnes + snes - the SNES context 2083c7afd0dbSLois Curfman McInnes - tol - tolerance 2084c7afd0dbSLois Curfman McInnes 20859b94acceSBarry Smith Options Database Key: 2086c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 20879b94acceSBarry Smith 208836851e7fSLois Curfman McInnes Level: intermediate 208936851e7fSLois Curfman McInnes 20909b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 20919b94acceSBarry Smith 20922492ecdbSBarry Smith .seealso: SNESSetTolerances() 20939b94acceSBarry Smith @*/ 20947087cfbeSBarry Smith PetscErrorCode SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 20959b94acceSBarry Smith { 20963a40ed3dSBarry Smith PetscFunctionBegin; 20970700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2098c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,tol,2); 20999b94acceSBarry Smith snes->deltatol = tol; 21003a40ed3dSBarry Smith PetscFunctionReturn(0); 21019b94acceSBarry Smith } 21029b94acceSBarry Smith 2103df9fa365SBarry Smith /* 2104df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 2105df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 2106df9fa365SBarry Smith macros instead of functions 2107df9fa365SBarry Smith */ 21084a2ae208SSatish Balay #undef __FUNCT__ 2109a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 21107087cfbeSBarry Smith PetscErrorCode SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 2111ce1608b8SBarry Smith { 2112dfbe8321SBarry Smith PetscErrorCode ierr; 2113ce1608b8SBarry Smith 2114ce1608b8SBarry Smith PetscFunctionBegin; 21150700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2116a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 2117ce1608b8SBarry Smith PetscFunctionReturn(0); 2118ce1608b8SBarry Smith } 2119ce1608b8SBarry Smith 21204a2ae208SSatish Balay #undef __FUNCT__ 2121a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 21227087cfbeSBarry Smith PetscErrorCode SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2123df9fa365SBarry Smith { 2124dfbe8321SBarry Smith PetscErrorCode ierr; 2125df9fa365SBarry Smith 2126df9fa365SBarry Smith PetscFunctionBegin; 2127a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2128df9fa365SBarry Smith PetscFunctionReturn(0); 2129df9fa365SBarry Smith } 2130df9fa365SBarry Smith 21314a2ae208SSatish Balay #undef __FUNCT__ 2132a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 21336bf464f9SBarry Smith PetscErrorCode SNESMonitorLGDestroy(PetscDrawLG *draw) 2134df9fa365SBarry Smith { 2135dfbe8321SBarry Smith PetscErrorCode ierr; 2136df9fa365SBarry Smith 2137df9fa365SBarry Smith PetscFunctionBegin; 2138a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2139df9fa365SBarry Smith PetscFunctionReturn(0); 2140df9fa365SBarry Smith } 2141df9fa365SBarry Smith 21427087cfbeSBarry Smith extern PetscErrorCode SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 2143b271bb04SBarry Smith #undef __FUNCT__ 2144b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 21457087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 2146b271bb04SBarry Smith { 2147b271bb04SBarry Smith PetscDrawLG lg; 2148b271bb04SBarry Smith PetscErrorCode ierr; 2149b271bb04SBarry Smith PetscReal x,y,per; 2150b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 2151b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 2152b271bb04SBarry Smith PetscDraw draw; 2153b271bb04SBarry Smith PetscFunctionBegin; 2154b271bb04SBarry Smith if (!monctx) { 2155b271bb04SBarry Smith MPI_Comm comm; 2156b271bb04SBarry Smith 2157b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 2158b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 2159b271bb04SBarry Smith } 2160b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 2161b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2162b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2163b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 2164b271bb04SBarry Smith x = (PetscReal) n; 2165b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 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,1,&lg);CHKERRQ(ierr); 2172b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2173b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2174b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 2175b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 2176b271bb04SBarry Smith x = (PetscReal) n; 2177b271bb04SBarry Smith y = 100.0*per; 2178b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2179b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2180b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2181b271bb04SBarry Smith } 2182b271bb04SBarry Smith 2183b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 2184b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2185b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2186b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 2187b271bb04SBarry Smith x = (PetscReal) n; 2188b271bb04SBarry Smith y = (prev - rnorm)/prev; 2189b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2190b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2191b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2192b271bb04SBarry Smith } 2193b271bb04SBarry Smith 2194b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 2195b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2196b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2197b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 2198b271bb04SBarry Smith x = (PetscReal) n; 2199b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 2200b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 2201b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2202b271bb04SBarry Smith } 2203b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2204b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2205b271bb04SBarry Smith } 2206b271bb04SBarry Smith prev = rnorm; 2207b271bb04SBarry Smith PetscFunctionReturn(0); 2208b271bb04SBarry Smith } 2209b271bb04SBarry Smith 2210b271bb04SBarry Smith #undef __FUNCT__ 2211b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 22127087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2213b271bb04SBarry Smith { 2214b271bb04SBarry Smith PetscErrorCode ierr; 2215b271bb04SBarry Smith 2216b271bb04SBarry Smith PetscFunctionBegin; 2217b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2218b271bb04SBarry Smith PetscFunctionReturn(0); 2219b271bb04SBarry Smith } 2220b271bb04SBarry Smith 2221b271bb04SBarry Smith #undef __FUNCT__ 2222b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 22236bf464f9SBarry Smith PetscErrorCode SNESMonitorLGRangeDestroy(PetscDrawLG *draw) 2224b271bb04SBarry Smith { 2225b271bb04SBarry Smith PetscErrorCode ierr; 2226b271bb04SBarry Smith 2227b271bb04SBarry Smith PetscFunctionBegin; 2228b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2229b271bb04SBarry Smith PetscFunctionReturn(0); 2230b271bb04SBarry Smith } 2231b271bb04SBarry Smith 22327a03ce2fSLisandro Dalcin #undef __FUNCT__ 22337a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor" 2234228d79bcSJed Brown /*@ 2235228d79bcSJed Brown SNESMonitor - runs the user provided monitor routines, if they exist 2236228d79bcSJed Brown 2237228d79bcSJed Brown Collective on SNES 2238228d79bcSJed Brown 2239228d79bcSJed Brown Input Parameters: 2240228d79bcSJed Brown + snes - nonlinear solver context obtained from SNESCreate() 2241228d79bcSJed Brown . iter - iteration number 2242228d79bcSJed Brown - rnorm - relative norm of the residual 2243228d79bcSJed Brown 2244228d79bcSJed Brown Notes: 2245228d79bcSJed Brown This routine is called by the SNES implementations. 2246228d79bcSJed Brown It does not typically need to be called by the user. 2247228d79bcSJed Brown 2248228d79bcSJed Brown Level: developer 2249228d79bcSJed Brown 2250228d79bcSJed Brown .seealso: SNESMonitorSet() 2251228d79bcSJed Brown @*/ 22527a03ce2fSLisandro Dalcin PetscErrorCode SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm) 22537a03ce2fSLisandro Dalcin { 22547a03ce2fSLisandro Dalcin PetscErrorCode ierr; 22557a03ce2fSLisandro Dalcin PetscInt i,n = snes->numbermonitors; 22567a03ce2fSLisandro Dalcin 22577a03ce2fSLisandro Dalcin PetscFunctionBegin; 22587a03ce2fSLisandro Dalcin for (i=0; i<n; i++) { 22597a03ce2fSLisandro Dalcin ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr); 22607a03ce2fSLisandro Dalcin } 22617a03ce2fSLisandro Dalcin PetscFunctionReturn(0); 22627a03ce2fSLisandro Dalcin } 22637a03ce2fSLisandro Dalcin 22649b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 22659b94acceSBarry Smith 22664a2ae208SSatish Balay #undef __FUNCT__ 2267a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 22689b94acceSBarry Smith /*@C 2269a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 22709b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 22719b94acceSBarry Smith progress. 22729b94acceSBarry Smith 22733f9fe445SBarry Smith Logically Collective on SNES 2274fee21e36SBarry Smith 2275c7afd0dbSLois Curfman McInnes Input Parameters: 2276c7afd0dbSLois Curfman McInnes + snes - the SNES context 2277c7afd0dbSLois Curfman McInnes . func - monitoring routine 2278b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 2279e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 2280b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 2281b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 22829b94acceSBarry Smith 2283c7afd0dbSLois Curfman McInnes Calling sequence of func: 2284a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 2285c7afd0dbSLois Curfman McInnes 2286c7afd0dbSLois Curfman McInnes + snes - the SNES context 2287c7afd0dbSLois Curfman McInnes . its - iteration number 2288c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 228940a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 22909b94acceSBarry Smith 22919665c990SLois Curfman McInnes Options Database Keys: 2292a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 2293a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 2294a6570f20SBarry Smith uses SNESMonitorLGCreate() 2295cca6129bSJed Brown - -snes_monitor_cancel - cancels all monitors that have 2296c7afd0dbSLois Curfman McInnes been hardwired into a code by 2297a6570f20SBarry Smith calls to SNESMonitorSet(), but 2298c7afd0dbSLois Curfman McInnes does not cancel those set via 2299c7afd0dbSLois Curfman McInnes the options database. 23009665c990SLois Curfman McInnes 2301639f9d9dSBarry Smith Notes: 23026bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 2303a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 23046bc08f3fSLois Curfman McInnes order in which they were set. 2305639f9d9dSBarry Smith 2306025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 2307025f1a04SBarry Smith 230836851e7fSLois Curfman McInnes Level: intermediate 230936851e7fSLois Curfman McInnes 23109b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 23119b94acceSBarry Smith 2312a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 23139b94acceSBarry Smith @*/ 2314c2efdce3SBarry Smith PetscErrorCode SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 23159b94acceSBarry Smith { 2316b90d0a6eSBarry Smith PetscInt i; 2317649052a6SBarry Smith PetscErrorCode ierr; 2318b90d0a6eSBarry Smith 23193a40ed3dSBarry Smith PetscFunctionBegin; 23200700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 232117186662SBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2322b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 2323649052a6SBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) { 2324649052a6SBarry Smith if (monitordestroy) { 2325c2efdce3SBarry Smith ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr); 2326649052a6SBarry Smith } 2327b90d0a6eSBarry Smith PetscFunctionReturn(0); 2328b90d0a6eSBarry Smith } 2329b90d0a6eSBarry Smith } 2330b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 2331b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 2332639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 23333a40ed3dSBarry Smith PetscFunctionReturn(0); 23349b94acceSBarry Smith } 23359b94acceSBarry Smith 23364a2ae208SSatish Balay #undef __FUNCT__ 2337a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 23385cd90555SBarry Smith /*@C 2339a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 23405cd90555SBarry Smith 23413f9fe445SBarry Smith Logically Collective on SNES 2342c7afd0dbSLois Curfman McInnes 23435cd90555SBarry Smith Input Parameters: 23445cd90555SBarry Smith . snes - the SNES context 23455cd90555SBarry Smith 23461a480d89SAdministrator Options Database Key: 2347a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 2348a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 2349c7afd0dbSLois Curfman McInnes set via the options database 23505cd90555SBarry Smith 23515cd90555SBarry Smith Notes: 23525cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 23535cd90555SBarry Smith 235436851e7fSLois Curfman McInnes Level: intermediate 235536851e7fSLois Curfman McInnes 23565cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 23575cd90555SBarry Smith 2358a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 23595cd90555SBarry Smith @*/ 23607087cfbeSBarry Smith PetscErrorCode SNESMonitorCancel(SNES snes) 23615cd90555SBarry Smith { 2362d952e501SBarry Smith PetscErrorCode ierr; 2363d952e501SBarry Smith PetscInt i; 2364d952e501SBarry Smith 23655cd90555SBarry Smith PetscFunctionBegin; 23660700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2367d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 2368d952e501SBarry Smith if (snes->monitordestroy[i]) { 23693c4aec1bSBarry Smith ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr); 2370d952e501SBarry Smith } 2371d952e501SBarry Smith } 23725cd90555SBarry Smith snes->numbermonitors = 0; 23735cd90555SBarry Smith PetscFunctionReturn(0); 23745cd90555SBarry Smith } 23755cd90555SBarry Smith 23764a2ae208SSatish Balay #undef __FUNCT__ 23774a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 23789b94acceSBarry Smith /*@C 23799b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 23809b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 23819b94acceSBarry Smith 23823f9fe445SBarry Smith Logically Collective on SNES 2383fee21e36SBarry Smith 2384c7afd0dbSLois Curfman McInnes Input Parameters: 2385c7afd0dbSLois Curfman McInnes + snes - the SNES context 2386c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 23877f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 23887f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 23899b94acceSBarry Smith 2390c7afd0dbSLois Curfman McInnes Calling sequence of func: 239106ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 2392c7afd0dbSLois Curfman McInnes 2393c7afd0dbSLois Curfman McInnes + snes - the SNES context 239406ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 2395c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 2396184914b5SBarry Smith . reason - reason for convergence/divergence 2397c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 23984b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 23994b27c08aSLois Curfman McInnes - f - 2-norm of function 24009b94acceSBarry Smith 240136851e7fSLois Curfman McInnes Level: advanced 240236851e7fSLois Curfman McInnes 24039b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 24049b94acceSBarry Smith 240585385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 24069b94acceSBarry Smith @*/ 24077087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 24089b94acceSBarry Smith { 24097f7931b9SBarry Smith PetscErrorCode ierr; 24107f7931b9SBarry Smith 24113a40ed3dSBarry Smith PetscFunctionBegin; 24120700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 241385385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 24147f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 24157f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 24167f7931b9SBarry Smith } 241785385478SLisandro Dalcin snes->ops->converged = func; 24187f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 241985385478SLisandro Dalcin snes->cnvP = cctx; 24203a40ed3dSBarry Smith PetscFunctionReturn(0); 24219b94acceSBarry Smith } 24229b94acceSBarry Smith 24234a2ae208SSatish Balay #undef __FUNCT__ 24244a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 242552baeb72SSatish Balay /*@ 2426184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 2427184914b5SBarry Smith 2428184914b5SBarry Smith Not Collective 2429184914b5SBarry Smith 2430184914b5SBarry Smith Input Parameter: 2431184914b5SBarry Smith . snes - the SNES context 2432184914b5SBarry Smith 2433184914b5SBarry Smith Output Parameter: 24344d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 2435184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 2436184914b5SBarry Smith 2437184914b5SBarry Smith Level: intermediate 2438184914b5SBarry Smith 2439184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 2440184914b5SBarry Smith 2441184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 2442184914b5SBarry Smith 244385385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 2444184914b5SBarry Smith @*/ 24457087cfbeSBarry Smith PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 2446184914b5SBarry Smith { 2447184914b5SBarry Smith PetscFunctionBegin; 24480700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 24494482741eSBarry Smith PetscValidPointer(reason,2); 2450184914b5SBarry Smith *reason = snes->reason; 2451184914b5SBarry Smith PetscFunctionReturn(0); 2452184914b5SBarry Smith } 2453184914b5SBarry Smith 24544a2ae208SSatish Balay #undef __FUNCT__ 24554a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 2456c9005455SLois Curfman McInnes /*@ 2457c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 2458c9005455SLois Curfman McInnes 24593f9fe445SBarry Smith Logically Collective on SNES 2460fee21e36SBarry Smith 2461c7afd0dbSLois Curfman McInnes Input Parameters: 2462c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 24638c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 2464cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 2465758f92a0SBarry Smith . na - size of a and its 246664731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 2467758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 2468c7afd0dbSLois Curfman McInnes 2469308dcc3eSBarry Smith Notes: 2470308dcc3eSBarry Smith If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a 2471308dcc3eSBarry Smith default array of length 10000 is allocated. 2472308dcc3eSBarry Smith 2473c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 2474c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 2475c9005455SLois Curfman McInnes during the section of code that is being timed. 2476c9005455SLois Curfman McInnes 247736851e7fSLois Curfman McInnes Level: intermediate 247836851e7fSLois Curfman McInnes 2479c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2480758f92a0SBarry Smith 248108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2482758f92a0SBarry Smith 2483c9005455SLois Curfman McInnes @*/ 24847087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset) 2485c9005455SLois Curfman McInnes { 2486308dcc3eSBarry Smith PetscErrorCode ierr; 2487308dcc3eSBarry Smith 24883a40ed3dSBarry Smith PetscFunctionBegin; 24890700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 24904482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2491a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2492308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) { 2493308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2494308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr); 2495308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr); 2496308dcc3eSBarry Smith snes->conv_malloc = PETSC_TRUE; 2497308dcc3eSBarry Smith } 2498c9005455SLois Curfman McInnes snes->conv_hist = a; 2499758f92a0SBarry Smith snes->conv_hist_its = its; 2500758f92a0SBarry Smith snes->conv_hist_max = na; 2501a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2502758f92a0SBarry Smith snes->conv_hist_reset = reset; 2503758f92a0SBarry Smith PetscFunctionReturn(0); 2504758f92a0SBarry Smith } 2505758f92a0SBarry Smith 2506308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2507c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 2508c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 2509308dcc3eSBarry Smith EXTERN_C_BEGIN 2510308dcc3eSBarry Smith #undef __FUNCT__ 2511308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab" 2512308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 2513308dcc3eSBarry Smith { 2514308dcc3eSBarry Smith mxArray *mat; 2515308dcc3eSBarry Smith PetscInt i; 2516308dcc3eSBarry Smith PetscReal *ar; 2517308dcc3eSBarry Smith 2518308dcc3eSBarry Smith PetscFunctionBegin; 2519308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL); 2520308dcc3eSBarry Smith ar = (PetscReal*) mxGetData(mat); 2521308dcc3eSBarry Smith for (i=0; i<snes->conv_hist_len; i++) { 2522308dcc3eSBarry Smith ar[i] = snes->conv_hist[i]; 2523308dcc3eSBarry Smith } 2524308dcc3eSBarry Smith PetscFunctionReturn(mat); 2525308dcc3eSBarry Smith } 2526308dcc3eSBarry Smith EXTERN_C_END 2527308dcc3eSBarry Smith #endif 2528308dcc3eSBarry Smith 2529308dcc3eSBarry Smith 25304a2ae208SSatish Balay #undef __FUNCT__ 25314a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 25320c4c9dddSBarry Smith /*@C 2533758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 2534758f92a0SBarry Smith 25353f9fe445SBarry Smith Not Collective 2536758f92a0SBarry Smith 2537758f92a0SBarry Smith Input Parameter: 2538758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 2539758f92a0SBarry Smith 2540758f92a0SBarry Smith Output Parameters: 2541758f92a0SBarry Smith . a - array to hold history 2542758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 2543758f92a0SBarry Smith negative if not converged) for each solve. 2544758f92a0SBarry Smith - na - size of a and its 2545758f92a0SBarry Smith 2546758f92a0SBarry Smith Notes: 2547758f92a0SBarry Smith The calling sequence for this routine in Fortran is 2548758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 2549758f92a0SBarry Smith 2550758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 2551758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 2552758f92a0SBarry Smith during the section of code that is being timed. 2553758f92a0SBarry Smith 2554758f92a0SBarry Smith Level: intermediate 2555758f92a0SBarry Smith 2556758f92a0SBarry Smith .keywords: SNES, get, convergence, history 2557758f92a0SBarry Smith 2558758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 2559758f92a0SBarry Smith 2560758f92a0SBarry Smith @*/ 25617087cfbeSBarry Smith PetscErrorCode SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 2562758f92a0SBarry Smith { 2563758f92a0SBarry Smith PetscFunctionBegin; 25640700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2565758f92a0SBarry Smith if (a) *a = snes->conv_hist; 2566758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 2567758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 25683a40ed3dSBarry Smith PetscFunctionReturn(0); 2569c9005455SLois Curfman McInnes } 2570c9005455SLois Curfman McInnes 2571e74ef692SMatthew Knepley #undef __FUNCT__ 2572e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 2573ac226902SBarry Smith /*@C 257476b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 2575eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 25767e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 257776b2cf59SMatthew Knepley 25783f9fe445SBarry Smith Logically Collective on SNES 257976b2cf59SMatthew Knepley 258076b2cf59SMatthew Knepley Input Parameters: 258176b2cf59SMatthew Knepley . snes - The nonlinear solver context 258276b2cf59SMatthew Knepley . func - The function 258376b2cf59SMatthew Knepley 258476b2cf59SMatthew Knepley Calling sequence of func: 2585b5d30489SBarry Smith . func (SNES snes, PetscInt step); 258676b2cf59SMatthew Knepley 258776b2cf59SMatthew Knepley . step - The current step of the iteration 258876b2cf59SMatthew Knepley 2589fe97e370SBarry Smith Level: advanced 2590fe97e370SBarry Smith 2591fe97e370SBarry 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() 2592fe97e370SBarry Smith This is not used by most users. 259376b2cf59SMatthew Knepley 259476b2cf59SMatthew Knepley .keywords: SNES, update 2595b5d30489SBarry Smith 259685385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 259776b2cf59SMatthew Knepley @*/ 25987087cfbeSBarry Smith PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 259976b2cf59SMatthew Knepley { 260076b2cf59SMatthew Knepley PetscFunctionBegin; 26010700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 2602e7788613SBarry Smith snes->ops->update = func; 260376b2cf59SMatthew Knepley PetscFunctionReturn(0); 260476b2cf59SMatthew Knepley } 260576b2cf59SMatthew Knepley 2606e74ef692SMatthew Knepley #undef __FUNCT__ 2607e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 260876b2cf59SMatthew Knepley /*@ 260976b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 261076b2cf59SMatthew Knepley 261176b2cf59SMatthew Knepley Not collective 261276b2cf59SMatthew Knepley 261376b2cf59SMatthew Knepley Input Parameters: 261476b2cf59SMatthew Knepley . snes - The nonlinear solver context 261576b2cf59SMatthew Knepley . step - The current step of the iteration 261676b2cf59SMatthew Knepley 2617205452f4SMatthew Knepley Level: intermediate 2618205452f4SMatthew Knepley 261976b2cf59SMatthew Knepley .keywords: SNES, update 2620a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 262176b2cf59SMatthew Knepley @*/ 26227087cfbeSBarry Smith PetscErrorCode SNESDefaultUpdate(SNES snes, PetscInt step) 262376b2cf59SMatthew Knepley { 262476b2cf59SMatthew Knepley PetscFunctionBegin; 262576b2cf59SMatthew Knepley PetscFunctionReturn(0); 262676b2cf59SMatthew Knepley } 262776b2cf59SMatthew Knepley 26284a2ae208SSatish Balay #undef __FUNCT__ 26294a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 26309b94acceSBarry Smith /* 26319b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 26329b94acceSBarry Smith positive parameter delta. 26339b94acceSBarry Smith 26349b94acceSBarry Smith Input Parameters: 2635c7afd0dbSLois Curfman McInnes + snes - the SNES context 26369b94acceSBarry Smith . y - approximate solution of linear system 26379b94acceSBarry Smith . fnorm - 2-norm of current function 2638c7afd0dbSLois Curfman McInnes - delta - trust region size 26399b94acceSBarry Smith 26409b94acceSBarry Smith Output Parameters: 2641c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 26429b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 26439b94acceSBarry Smith region, and exceeds zero otherwise. 2644c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 26459b94acceSBarry Smith 26469b94acceSBarry Smith Note: 26474b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 26489b94acceSBarry Smith is set to be the maximum allowable step size. 26499b94acceSBarry Smith 26509b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 26519b94acceSBarry Smith */ 2652dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 26539b94acceSBarry Smith { 2654064f8208SBarry Smith PetscReal nrm; 2655ea709b57SSatish Balay PetscScalar cnorm; 2656dfbe8321SBarry Smith PetscErrorCode ierr; 26573a40ed3dSBarry Smith 26583a40ed3dSBarry Smith PetscFunctionBegin; 26590700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 26600700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 2661c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 2662184914b5SBarry Smith 2663064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 2664064f8208SBarry Smith if (nrm > *delta) { 2665064f8208SBarry Smith nrm = *delta/nrm; 2666064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 2667064f8208SBarry Smith cnorm = nrm; 26682dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 26699b94acceSBarry Smith *ynorm = *delta; 26709b94acceSBarry Smith } else { 26719b94acceSBarry Smith *gpnorm = 0.0; 2672064f8208SBarry Smith *ynorm = nrm; 26739b94acceSBarry Smith } 26743a40ed3dSBarry Smith PetscFunctionReturn(0); 26759b94acceSBarry Smith } 26769b94acceSBarry Smith 26774a2ae208SSatish Balay #undef __FUNCT__ 26784a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 26796ce558aeSBarry Smith /*@C 2680f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 2681f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 26829b94acceSBarry Smith 2683c7afd0dbSLois Curfman McInnes Collective on SNES 2684c7afd0dbSLois Curfman McInnes 2685b2002411SLois Curfman McInnes Input Parameters: 2686c7afd0dbSLois Curfman McInnes + snes - the SNES context 26873cebf274SJed Brown . b - the constant part of the equation F(x) = b, or PETSC_NULL to use zero. 268885385478SLisandro Dalcin - x - the solution vector. 26899b94acceSBarry Smith 2690b2002411SLois Curfman McInnes Notes: 26918ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 26928ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 26938ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 26948ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 26958ddd3da0SLois Curfman McInnes 269636851e7fSLois Curfman McInnes Level: beginner 269736851e7fSLois Curfman McInnes 26989b94acceSBarry Smith .keywords: SNES, nonlinear, solve 26999b94acceSBarry Smith 270085385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 27019b94acceSBarry Smith @*/ 27027087cfbeSBarry Smith PetscErrorCode SNESSolve(SNES snes,Vec b,Vec x) 27039b94acceSBarry Smith { 2704dfbe8321SBarry Smith PetscErrorCode ierr; 2705ace3abfcSBarry Smith PetscBool flg; 2706eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 2707eabae89aSBarry Smith PetscViewer viewer; 2708efd51863SBarry Smith PetscInt grid; 2709052efed2SBarry Smith 27103a40ed3dSBarry Smith PetscFunctionBegin; 27110700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 27120700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2713f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 27140700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 271585385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 271685385478SLisandro Dalcin 2717a8248277SBarry Smith for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);} 2718efd51863SBarry Smith for (grid=0; grid<snes->gridsequence+1; grid++) { 2719efd51863SBarry Smith 272085385478SLisandro Dalcin /* set solution vector */ 2721efd51863SBarry Smith if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);} 27226bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 272385385478SLisandro Dalcin snes->vec_sol = x; 272485385478SLisandro Dalcin /* set afine vector if provided */ 272585385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 27266bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 272785385478SLisandro Dalcin snes->vec_rhs = b; 272885385478SLisandro Dalcin 272970e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 27303f149594SLisandro Dalcin 2731d25893d9SBarry Smith if (!grid && snes->ops->computeinitialguess) { 2732d25893d9SBarry Smith ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr); 2733d25893d9SBarry Smith } 2734d25893d9SBarry Smith 2735abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 273650ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2737d5e45103SBarry Smith 27383f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 27394936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 274085385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 27414936397dSBarry Smith if (snes->domainerror){ 27424936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 27434936397dSBarry Smith snes->domainerror = PETSC_FALSE; 27444936397dSBarry Smith } 274517186662SBarry Smith if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 27463f149594SLisandro Dalcin 27477adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2748eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 27497adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2750eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 27516bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2752eabae89aSBarry Smith } 2753eabae89aSBarry Smith 275490d69ab7SBarry Smith flg = PETSC_FALSE; 2755acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr); 2756da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 27575968eb51SBarry Smith if (snes->printreason) { 2758a8248277SBarry Smith ierr = PetscViewerASCIIAddTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 27595968eb51SBarry Smith if (snes->reason > 0) { 2760a8248277SBarry Smith ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 27615968eb51SBarry Smith } else { 2762a8248277SBarry Smith ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 27635968eb51SBarry Smith } 2764a8248277SBarry Smith ierr = PetscViewerASCIISubtractTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 27655968eb51SBarry Smith } 27665968eb51SBarry Smith 2767e113a28aSBarry Smith if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged"); 2768efd51863SBarry Smith if (grid < snes->gridsequence) { 2769efd51863SBarry Smith DM fine; 2770efd51863SBarry Smith Vec xnew; 2771efd51863SBarry Smith Mat interp; 2772efd51863SBarry Smith 2773efd51863SBarry Smith ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr); 2774efd51863SBarry Smith ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr); 2775efd51863SBarry Smith ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr); 2776efd51863SBarry Smith ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr); 2777efd51863SBarry Smith ierr = MatDestroy(&interp);CHKERRQ(ierr); 2778efd51863SBarry Smith x = xnew; 2779efd51863SBarry Smith 2780efd51863SBarry Smith ierr = SNESReset(snes);CHKERRQ(ierr); 2781efd51863SBarry Smith ierr = SNESSetDM(snes,fine);CHKERRQ(ierr); 2782efd51863SBarry Smith ierr = DMDestroy(&fine);CHKERRQ(ierr); 2783a8248277SBarry Smith ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr); 2784efd51863SBarry Smith } 2785efd51863SBarry Smith } 27863a40ed3dSBarry Smith PetscFunctionReturn(0); 27879b94acceSBarry Smith } 27889b94acceSBarry Smith 27899b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 27909b94acceSBarry Smith 27914a2ae208SSatish Balay #undef __FUNCT__ 27924a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 279382bf6240SBarry Smith /*@C 27944b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 27959b94acceSBarry Smith 2796fee21e36SBarry Smith Collective on SNES 2797fee21e36SBarry Smith 2798c7afd0dbSLois Curfman McInnes Input Parameters: 2799c7afd0dbSLois Curfman McInnes + snes - the SNES context 2800454a90a3SBarry Smith - type - a known method 2801c7afd0dbSLois Curfman McInnes 2802c7afd0dbSLois Curfman McInnes Options Database Key: 2803454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2804c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2805ae12b187SLois Curfman McInnes 28069b94acceSBarry Smith Notes: 2807e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 28084b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2809c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 28104b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2811c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 28129b94acceSBarry Smith 2813ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2814ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2815ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2816ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2817ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2818ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2819ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2820ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2821ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2822b0a32e0cSBarry Smith appropriate method. 282336851e7fSLois Curfman McInnes 282436851e7fSLois Curfman McInnes Level: intermediate 2825a703fe33SLois Curfman McInnes 2826454a90a3SBarry Smith .keywords: SNES, set, type 2827435da068SBarry Smith 2828435da068SBarry Smith .seealso: SNESType, SNESCreate() 2829435da068SBarry Smith 28309b94acceSBarry Smith @*/ 28317087cfbeSBarry Smith PetscErrorCode SNESSetType(SNES snes,const SNESType type) 28329b94acceSBarry Smith { 2833dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 2834ace3abfcSBarry Smith PetscBool match; 28353a40ed3dSBarry Smith 28363a40ed3dSBarry Smith PetscFunctionBegin; 28370700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28384482741eSBarry Smith PetscValidCharPointer(type,2); 283982bf6240SBarry Smith 28406831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 28410f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 284292ff6ae8SBarry Smith 28434b91b6eaSBarry Smith ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 2844e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 284575396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 284675396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 284775396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 284875396ef9SLisandro Dalcin snes->ops->setup = 0; 284975396ef9SLisandro Dalcin snes->ops->solve = 0; 285075396ef9SLisandro Dalcin snes->ops->view = 0; 285175396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 285275396ef9SLisandro Dalcin snes->ops->destroy = 0; 285375396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 285475396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 2855454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 285603bfa161SLisandro Dalcin ierr = (*r)(snes);CHKERRQ(ierr); 28579fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS) 28589fb22e1aSBarry Smith if (PetscAMSPublishAll) { 28599fb22e1aSBarry Smith ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr); 28609fb22e1aSBarry Smith } 28619fb22e1aSBarry Smith #endif 28623a40ed3dSBarry Smith PetscFunctionReturn(0); 28639b94acceSBarry Smith } 28649b94acceSBarry Smith 2865a847f771SSatish Balay 28669b94acceSBarry Smith /* --------------------------------------------------------------------- */ 28674a2ae208SSatish Balay #undef __FUNCT__ 28684a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 286952baeb72SSatish Balay /*@ 28709b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2871f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 28729b94acceSBarry Smith 2873fee21e36SBarry Smith Not Collective 2874fee21e36SBarry Smith 287536851e7fSLois Curfman McInnes Level: advanced 287636851e7fSLois Curfman McInnes 28779b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 28789b94acceSBarry Smith 28799b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 28809b94acceSBarry Smith @*/ 28817087cfbeSBarry Smith PetscErrorCode SNESRegisterDestroy(void) 28829b94acceSBarry Smith { 2883dfbe8321SBarry Smith PetscErrorCode ierr; 288482bf6240SBarry Smith 28853a40ed3dSBarry Smith PetscFunctionBegin; 28861441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 28874c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 28883a40ed3dSBarry Smith PetscFunctionReturn(0); 28899b94acceSBarry Smith } 28909b94acceSBarry Smith 28914a2ae208SSatish Balay #undef __FUNCT__ 28924a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 28939b94acceSBarry Smith /*@C 28949a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 28959b94acceSBarry Smith 2896c7afd0dbSLois Curfman McInnes Not Collective 2897c7afd0dbSLois Curfman McInnes 28989b94acceSBarry Smith Input Parameter: 28994b0e389bSBarry Smith . snes - nonlinear solver context 29009b94acceSBarry Smith 29019b94acceSBarry Smith Output Parameter: 29023a7fca6bSBarry Smith . type - SNES method (a character string) 29039b94acceSBarry Smith 290436851e7fSLois Curfman McInnes Level: intermediate 290536851e7fSLois Curfman McInnes 2906454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 29079b94acceSBarry Smith @*/ 29087087cfbeSBarry Smith PetscErrorCode SNESGetType(SNES snes,const SNESType *type) 29099b94acceSBarry Smith { 29103a40ed3dSBarry Smith PetscFunctionBegin; 29110700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29124482741eSBarry Smith PetscValidPointer(type,2); 29137adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 29143a40ed3dSBarry Smith PetscFunctionReturn(0); 29159b94acceSBarry Smith } 29169b94acceSBarry Smith 29174a2ae208SSatish Balay #undef __FUNCT__ 29184a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 291952baeb72SSatish Balay /*@ 29209b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 29219b94acceSBarry Smith stored. 29229b94acceSBarry Smith 2923c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2924c7afd0dbSLois Curfman McInnes 29259b94acceSBarry Smith Input Parameter: 29269b94acceSBarry Smith . snes - the SNES context 29279b94acceSBarry Smith 29289b94acceSBarry Smith Output Parameter: 29299b94acceSBarry Smith . x - the solution 29309b94acceSBarry Smith 293170e92668SMatthew Knepley Level: intermediate 293236851e7fSLois Curfman McInnes 29339b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 29349b94acceSBarry Smith 293585385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 29369b94acceSBarry Smith @*/ 29377087cfbeSBarry Smith PetscErrorCode SNESGetSolution(SNES snes,Vec *x) 29389b94acceSBarry Smith { 29393a40ed3dSBarry Smith PetscFunctionBegin; 29400700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29414482741eSBarry Smith PetscValidPointer(x,2); 294285385478SLisandro Dalcin *x = snes->vec_sol; 294370e92668SMatthew Knepley PetscFunctionReturn(0); 294470e92668SMatthew Knepley } 294570e92668SMatthew Knepley 294670e92668SMatthew Knepley #undef __FUNCT__ 29474a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 294852baeb72SSatish Balay /*@ 29499b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 29509b94acceSBarry Smith stored. 29519b94acceSBarry Smith 2952c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2953c7afd0dbSLois Curfman McInnes 29549b94acceSBarry Smith Input Parameter: 29559b94acceSBarry Smith . snes - the SNES context 29569b94acceSBarry Smith 29579b94acceSBarry Smith Output Parameter: 29589b94acceSBarry Smith . x - the solution update 29599b94acceSBarry Smith 296036851e7fSLois Curfman McInnes Level: advanced 296136851e7fSLois Curfman McInnes 29629b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 29639b94acceSBarry Smith 296485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 29659b94acceSBarry Smith @*/ 29667087cfbeSBarry Smith PetscErrorCode SNESGetSolutionUpdate(SNES snes,Vec *x) 29679b94acceSBarry Smith { 29683a40ed3dSBarry Smith PetscFunctionBegin; 29690700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29704482741eSBarry Smith PetscValidPointer(x,2); 297185385478SLisandro Dalcin *x = snes->vec_sol_update; 29723a40ed3dSBarry Smith PetscFunctionReturn(0); 29739b94acceSBarry Smith } 29749b94acceSBarry Smith 29754a2ae208SSatish Balay #undef __FUNCT__ 29764a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 29779b94acceSBarry Smith /*@C 29783638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 29799b94acceSBarry Smith 2980c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2981c7afd0dbSLois Curfman McInnes 29829b94acceSBarry Smith Input Parameter: 29839b94acceSBarry Smith . snes - the SNES context 29849b94acceSBarry Smith 29859b94acceSBarry Smith Output Parameter: 29867bf4e008SBarry Smith + r - the function (or PETSC_NULL) 298770e92668SMatthew Knepley . func - the function (or PETSC_NULL) 298870e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 29899b94acceSBarry Smith 299036851e7fSLois Curfman McInnes Level: advanced 299136851e7fSLois Curfman McInnes 2992a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 29939b94acceSBarry Smith 29944b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 29959b94acceSBarry Smith @*/ 29967087cfbeSBarry Smith PetscErrorCode SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 29979b94acceSBarry Smith { 29983a40ed3dSBarry Smith PetscFunctionBegin; 29990700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 300085385478SLisandro Dalcin if (r) *r = snes->vec_func; 3001e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 300270e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 30033a40ed3dSBarry Smith PetscFunctionReturn(0); 30049b94acceSBarry Smith } 30059b94acceSBarry Smith 30064a2ae208SSatish Balay #undef __FUNCT__ 30074a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 30083c7409f5SSatish Balay /*@C 30093c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 3010d850072dSLois Curfman McInnes SNES options in the database. 30113c7409f5SSatish Balay 30123f9fe445SBarry Smith Logically Collective on SNES 3013fee21e36SBarry Smith 3014c7afd0dbSLois Curfman McInnes Input Parameter: 3015c7afd0dbSLois Curfman McInnes + snes - the SNES context 3016c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3017c7afd0dbSLois Curfman McInnes 3018d850072dSLois Curfman McInnes Notes: 3019a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3020c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3021d850072dSLois Curfman McInnes 302236851e7fSLois Curfman McInnes Level: advanced 302336851e7fSLois Curfman McInnes 30243c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 3025a86d99e1SLois Curfman McInnes 3026a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 30273c7409f5SSatish Balay @*/ 30287087cfbeSBarry Smith PetscErrorCode SNESSetOptionsPrefix(SNES snes,const char prefix[]) 30293c7409f5SSatish Balay { 3030dfbe8321SBarry Smith PetscErrorCode ierr; 30313c7409f5SSatish Balay 30323a40ed3dSBarry Smith PetscFunctionBegin; 30330700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3034639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 30351cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 303694b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 30373a40ed3dSBarry Smith PetscFunctionReturn(0); 30383c7409f5SSatish Balay } 30393c7409f5SSatish Balay 30404a2ae208SSatish Balay #undef __FUNCT__ 30414a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 30423c7409f5SSatish Balay /*@C 3043f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 3044d850072dSLois Curfman McInnes SNES options in the database. 30453c7409f5SSatish Balay 30463f9fe445SBarry Smith Logically Collective on SNES 3047fee21e36SBarry Smith 3048c7afd0dbSLois Curfman McInnes Input Parameters: 3049c7afd0dbSLois Curfman McInnes + snes - the SNES context 3050c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3051c7afd0dbSLois Curfman McInnes 3052d850072dSLois Curfman McInnes Notes: 3053a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3054c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3055d850072dSLois Curfman McInnes 305636851e7fSLois Curfman McInnes Level: advanced 305736851e7fSLois Curfman McInnes 30583c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 3059a86d99e1SLois Curfman McInnes 3060a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 30613c7409f5SSatish Balay @*/ 30627087cfbeSBarry Smith PetscErrorCode SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 30633c7409f5SSatish Balay { 3064dfbe8321SBarry Smith PetscErrorCode ierr; 30653c7409f5SSatish Balay 30663a40ed3dSBarry Smith PetscFunctionBegin; 30670700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3068639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 30691cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 307094b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 30713a40ed3dSBarry Smith PetscFunctionReturn(0); 30723c7409f5SSatish Balay } 30733c7409f5SSatish Balay 30744a2ae208SSatish Balay #undef __FUNCT__ 30754a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 30769ab63eb5SSatish Balay /*@C 30773c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 30783c7409f5SSatish Balay SNES options in the database. 30793c7409f5SSatish Balay 3080c7afd0dbSLois Curfman McInnes Not Collective 3081c7afd0dbSLois Curfman McInnes 30823c7409f5SSatish Balay Input Parameter: 30833c7409f5SSatish Balay . snes - the SNES context 30843c7409f5SSatish Balay 30853c7409f5SSatish Balay Output Parameter: 30863c7409f5SSatish Balay . prefix - pointer to the prefix string used 30873c7409f5SSatish Balay 30884ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 30899ab63eb5SSatish Balay sufficient length to hold the prefix. 30909ab63eb5SSatish Balay 309136851e7fSLois Curfman McInnes Level: advanced 309236851e7fSLois Curfman McInnes 30933c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 3094a86d99e1SLois Curfman McInnes 3095a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 30963c7409f5SSatish Balay @*/ 30977087cfbeSBarry Smith PetscErrorCode SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 30983c7409f5SSatish Balay { 3099dfbe8321SBarry Smith PetscErrorCode ierr; 31003c7409f5SSatish Balay 31013a40ed3dSBarry Smith PetscFunctionBegin; 31020700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3103639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 31043a40ed3dSBarry Smith PetscFunctionReturn(0); 31053c7409f5SSatish Balay } 31063c7409f5SSatish Balay 3107b2002411SLois Curfman McInnes 31084a2ae208SSatish Balay #undef __FUNCT__ 31094a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 31103cea93caSBarry Smith /*@C 31113cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 31123cea93caSBarry Smith 31137f6c08e0SMatthew Knepley Level: advanced 31143cea93caSBarry Smith @*/ 31157087cfbeSBarry Smith PetscErrorCode SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 3116b2002411SLois Curfman McInnes { 3117e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 3118dfbe8321SBarry Smith PetscErrorCode ierr; 3119b2002411SLois Curfman McInnes 3120b2002411SLois Curfman McInnes PetscFunctionBegin; 3121b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 3122c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 3123b2002411SLois Curfman McInnes PetscFunctionReturn(0); 3124b2002411SLois Curfman McInnes } 3125da9b6338SBarry Smith 3126da9b6338SBarry Smith #undef __FUNCT__ 3127da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 31287087cfbeSBarry Smith PetscErrorCode SNESTestLocalMin(SNES snes) 3129da9b6338SBarry Smith { 3130dfbe8321SBarry Smith PetscErrorCode ierr; 313177431f27SBarry Smith PetscInt N,i,j; 3132da9b6338SBarry Smith Vec u,uh,fh; 3133da9b6338SBarry Smith PetscScalar value; 3134da9b6338SBarry Smith PetscReal norm; 3135da9b6338SBarry Smith 3136da9b6338SBarry Smith PetscFunctionBegin; 3137da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 3138da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 3139da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 3140da9b6338SBarry Smith 3141da9b6338SBarry Smith /* currently only works for sequential */ 3142da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 3143da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 3144da9b6338SBarry Smith for (i=0; i<N; i++) { 3145da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 314677431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 3147da9b6338SBarry Smith for (j=-10; j<11; j++) { 3148ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 3149da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 31503ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 3151da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 315277431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 3153da9b6338SBarry Smith value = -value; 3154da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 3155da9b6338SBarry Smith } 3156da9b6338SBarry Smith } 31576bf464f9SBarry Smith ierr = VecDestroy(&uh);CHKERRQ(ierr); 31586bf464f9SBarry Smith ierr = VecDestroy(&fh);CHKERRQ(ierr); 3159da9b6338SBarry Smith PetscFunctionReturn(0); 3160da9b6338SBarry Smith } 316171f87433Sdalcinl 316271f87433Sdalcinl #undef __FUNCT__ 3163fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 316471f87433Sdalcinl /*@ 3165fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 316671f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 316771f87433Sdalcinl Newton method. 316871f87433Sdalcinl 31693f9fe445SBarry Smith Logically Collective on SNES 317071f87433Sdalcinl 317171f87433Sdalcinl Input Parameters: 317271f87433Sdalcinl + snes - SNES context 317371f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 317471f87433Sdalcinl 317564ba62caSBarry Smith Options Database: 317664ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 317764ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 317864ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 317964ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 318064ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 318164ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 318264ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 318364ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 318464ba62caSBarry Smith 318571f87433Sdalcinl Notes: 318671f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 318771f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 318871f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 318971f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 319071f87433Sdalcinl solver. 319171f87433Sdalcinl 319271f87433Sdalcinl Level: advanced 319371f87433Sdalcinl 319471f87433Sdalcinl Reference: 319571f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 319671f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 319771f87433Sdalcinl 319871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 319971f87433Sdalcinl 3200fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 320171f87433Sdalcinl @*/ 32027087cfbeSBarry Smith PetscErrorCode SNESKSPSetUseEW(SNES snes,PetscBool flag) 320371f87433Sdalcinl { 320471f87433Sdalcinl PetscFunctionBegin; 32050700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3206acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flag,2); 320771f87433Sdalcinl snes->ksp_ewconv = flag; 320871f87433Sdalcinl PetscFunctionReturn(0); 320971f87433Sdalcinl } 321071f87433Sdalcinl 321171f87433Sdalcinl #undef __FUNCT__ 3212fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 321371f87433Sdalcinl /*@ 3214fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 321571f87433Sdalcinl for computing relative tolerance for linear solvers within an 321671f87433Sdalcinl inexact Newton method. 321771f87433Sdalcinl 321871f87433Sdalcinl Not Collective 321971f87433Sdalcinl 322071f87433Sdalcinl Input Parameter: 322171f87433Sdalcinl . snes - SNES context 322271f87433Sdalcinl 322371f87433Sdalcinl Output Parameter: 322471f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 322571f87433Sdalcinl 322671f87433Sdalcinl Notes: 322771f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 322871f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 322971f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 323071f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 323171f87433Sdalcinl solver. 323271f87433Sdalcinl 323371f87433Sdalcinl Level: advanced 323471f87433Sdalcinl 323571f87433Sdalcinl Reference: 323671f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 323771f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 323871f87433Sdalcinl 323971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 324071f87433Sdalcinl 3241fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 324271f87433Sdalcinl @*/ 32437087cfbeSBarry Smith PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 324471f87433Sdalcinl { 324571f87433Sdalcinl PetscFunctionBegin; 32460700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 324771f87433Sdalcinl PetscValidPointer(flag,2); 324871f87433Sdalcinl *flag = snes->ksp_ewconv; 324971f87433Sdalcinl PetscFunctionReturn(0); 325071f87433Sdalcinl } 325171f87433Sdalcinl 325271f87433Sdalcinl #undef __FUNCT__ 3253fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 325471f87433Sdalcinl /*@ 3255fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 325671f87433Sdalcinl convergence criteria for the linear solvers within an inexact 325771f87433Sdalcinl Newton method. 325871f87433Sdalcinl 32593f9fe445SBarry Smith Logically Collective on SNES 326071f87433Sdalcinl 326171f87433Sdalcinl Input Parameters: 326271f87433Sdalcinl + snes - SNES context 326371f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 326471f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 326571f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 326671f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 326771f87433Sdalcinl (0 <= gamma2 <= 1) 326871f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 326971f87433Sdalcinl . alpha2 - power for safeguard 327071f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 327171f87433Sdalcinl 327271f87433Sdalcinl Note: 327371f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 327471f87433Sdalcinl 327571f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 327671f87433Sdalcinl 327771f87433Sdalcinl Level: advanced 327871f87433Sdalcinl 327971f87433Sdalcinl Reference: 328071f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 328171f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 328271f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 328371f87433Sdalcinl 328471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 328571f87433Sdalcinl 3286fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 328771f87433Sdalcinl @*/ 32887087cfbeSBarry Smith PetscErrorCode SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 328971f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 329071f87433Sdalcinl { 3291fa9f3622SBarry Smith SNESKSPEW *kctx; 329271f87433Sdalcinl PetscFunctionBegin; 32930700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3294fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3295e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 3296c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,version,2); 3297c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_0,3); 3298c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_max,4); 3299c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,gamma,5); 3300c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha,6); 3301c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha2,7); 3302c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,threshold,8); 330371f87433Sdalcinl 330471f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 330571f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 330671f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 330771f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 330871f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 330971f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 331071f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 331171f87433Sdalcinl 331271f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 3313e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 331471f87433Sdalcinl } 331571f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 3316e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 331771f87433Sdalcinl } 331871f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 3319e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 332071f87433Sdalcinl } 332171f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 3322e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 332371f87433Sdalcinl } 332471f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 3325e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 332671f87433Sdalcinl } 332771f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 3328e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 332971f87433Sdalcinl } 333071f87433Sdalcinl PetscFunctionReturn(0); 333171f87433Sdalcinl } 333271f87433Sdalcinl 333371f87433Sdalcinl #undef __FUNCT__ 3334fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 333571f87433Sdalcinl /*@ 3336fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 333771f87433Sdalcinl convergence criteria for the linear solvers within an inexact 333871f87433Sdalcinl Newton method. 333971f87433Sdalcinl 334071f87433Sdalcinl Not Collective 334171f87433Sdalcinl 334271f87433Sdalcinl Input Parameters: 334371f87433Sdalcinl snes - SNES context 334471f87433Sdalcinl 334571f87433Sdalcinl Output Parameters: 334671f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 334771f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 334871f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 334971f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 335071f87433Sdalcinl (0 <= gamma2 <= 1) 335171f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 335271f87433Sdalcinl . alpha2 - power for safeguard 335371f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 335471f87433Sdalcinl 335571f87433Sdalcinl Level: advanced 335671f87433Sdalcinl 335771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 335871f87433Sdalcinl 3359fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 336071f87433Sdalcinl @*/ 33617087cfbeSBarry Smith PetscErrorCode SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 336271f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 336371f87433Sdalcinl { 3364fa9f3622SBarry Smith SNESKSPEW *kctx; 336571f87433Sdalcinl PetscFunctionBegin; 33660700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3367fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3368e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 336971f87433Sdalcinl if(version) *version = kctx->version; 337071f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 337171f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 337271f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 337371f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 337471f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 337571f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 337671f87433Sdalcinl PetscFunctionReturn(0); 337771f87433Sdalcinl } 337871f87433Sdalcinl 337971f87433Sdalcinl #undef __FUNCT__ 3380fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 3381fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 338271f87433Sdalcinl { 338371f87433Sdalcinl PetscErrorCode ierr; 3384fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 338571f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 338671f87433Sdalcinl 338771f87433Sdalcinl PetscFunctionBegin; 3388e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 338971f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 339071f87433Sdalcinl rtol = kctx->rtol_0; 339171f87433Sdalcinl } else { 339271f87433Sdalcinl if (kctx->version == 1) { 339371f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 339471f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 339571f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 339671f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 339771f87433Sdalcinl } else if (kctx->version == 2) { 339871f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 339971f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 340071f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 340171f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 340271f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 340371f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 340471f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 340571f87433Sdalcinl stol = PetscMax(rtol,stol); 340671f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 340771f87433Sdalcinl /* safeguard: avoid oversolving */ 340871f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 340971f87433Sdalcinl stol = PetscMax(rtol,stol); 341071f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 3411e32f2f54SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 341271f87433Sdalcinl } 341371f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 341471f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 341571f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 341671f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 341771f87433Sdalcinl PetscFunctionReturn(0); 341871f87433Sdalcinl } 341971f87433Sdalcinl 342071f87433Sdalcinl #undef __FUNCT__ 3421fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 3422fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 342371f87433Sdalcinl { 342471f87433Sdalcinl PetscErrorCode ierr; 3425fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 342671f87433Sdalcinl PCSide pcside; 342771f87433Sdalcinl Vec lres; 342871f87433Sdalcinl 342971f87433Sdalcinl PetscFunctionBegin; 3430e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 343171f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 343271f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 343371f87433Sdalcinl if (kctx->version == 1) { 3434b037da10SBarry Smith ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr); 343571f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 343671f87433Sdalcinl /* KSP residual is true linear residual */ 343771f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 343871f87433Sdalcinl } else { 343971f87433Sdalcinl /* KSP residual is preconditioned residual */ 344071f87433Sdalcinl /* compute true linear residual norm */ 344171f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 344271f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 344371f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 344471f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 34456bf464f9SBarry Smith ierr = VecDestroy(&lres);CHKERRQ(ierr); 344671f87433Sdalcinl } 344771f87433Sdalcinl } 344871f87433Sdalcinl PetscFunctionReturn(0); 344971f87433Sdalcinl } 345071f87433Sdalcinl 345171f87433Sdalcinl #undef __FUNCT__ 345271f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 345371f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 345471f87433Sdalcinl { 345571f87433Sdalcinl PetscErrorCode ierr; 345671f87433Sdalcinl 345771f87433Sdalcinl PetscFunctionBegin; 3458fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 345971f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 3460fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 346171f87433Sdalcinl PetscFunctionReturn(0); 346271f87433Sdalcinl } 34636c699258SBarry Smith 34646c699258SBarry Smith #undef __FUNCT__ 34656c699258SBarry Smith #define __FUNCT__ "SNESSetDM" 34666c699258SBarry Smith /*@ 34676c699258SBarry Smith SNESSetDM - Sets the DM that may be used by some preconditioners 34686c699258SBarry Smith 34693f9fe445SBarry Smith Logically Collective on SNES 34706c699258SBarry Smith 34716c699258SBarry Smith Input Parameters: 34726c699258SBarry Smith + snes - the preconditioner context 34736c699258SBarry Smith - dm - the dm 34746c699258SBarry Smith 34756c699258SBarry Smith Level: intermediate 34766c699258SBarry Smith 34776c699258SBarry Smith 34786c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM() 34796c699258SBarry Smith @*/ 34807087cfbeSBarry Smith PetscErrorCode SNESSetDM(SNES snes,DM dm) 34816c699258SBarry Smith { 34826c699258SBarry Smith PetscErrorCode ierr; 3483345fed2cSBarry Smith KSP ksp; 34846c699258SBarry Smith 34856c699258SBarry Smith PetscFunctionBegin; 34860700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3487d0660788SBarry Smith if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);} 34886bf464f9SBarry Smith ierr = DMDestroy(&snes->dm);CHKERRQ(ierr); 34896c699258SBarry Smith snes->dm = dm; 3490345fed2cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 3491345fed2cSBarry Smith ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr); 3492f22f69f0SBarry Smith ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr); 34932c155ee1SBarry Smith if (snes->pc) { 34942c155ee1SBarry Smith ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 34952c155ee1SBarry Smith } 34966c699258SBarry Smith PetscFunctionReturn(0); 34976c699258SBarry Smith } 34986c699258SBarry Smith 34996c699258SBarry Smith #undef __FUNCT__ 35006c699258SBarry Smith #define __FUNCT__ "SNESGetDM" 35016c699258SBarry Smith /*@ 35026c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 35036c699258SBarry Smith 35043f9fe445SBarry Smith Not Collective but DM obtained is parallel on SNES 35056c699258SBarry Smith 35066c699258SBarry Smith Input Parameter: 35076c699258SBarry Smith . snes - the preconditioner context 35086c699258SBarry Smith 35096c699258SBarry Smith Output Parameter: 35106c699258SBarry Smith . dm - the dm 35116c699258SBarry Smith 35126c699258SBarry Smith Level: intermediate 35136c699258SBarry Smith 35146c699258SBarry Smith 35156c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM() 35166c699258SBarry Smith @*/ 35177087cfbeSBarry Smith PetscErrorCode SNESGetDM(SNES snes,DM *dm) 35186c699258SBarry Smith { 35196c699258SBarry Smith PetscFunctionBegin; 35200700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 35216c699258SBarry Smith *dm = snes->dm; 35226c699258SBarry Smith PetscFunctionReturn(0); 35236c699258SBarry Smith } 35240807856dSBarry Smith 352531823bd8SMatthew G Knepley #undef __FUNCT__ 352631823bd8SMatthew G Knepley #define __FUNCT__ "SNESSetPC" 352731823bd8SMatthew G Knepley /*@ 3528fd4b34e9SJed Brown SNESSetPC - Sets the nonlinear preconditioner to be used. 352931823bd8SMatthew G Knepley 353031823bd8SMatthew G Knepley Collective on SNES 353131823bd8SMatthew G Knepley 353231823bd8SMatthew G Knepley Input Parameters: 353331823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate() 353431823bd8SMatthew G Knepley - pc - the preconditioner object 353531823bd8SMatthew G Knepley 353631823bd8SMatthew G Knepley Notes: 353731823bd8SMatthew G Knepley Use SNESGetPC() to retrieve the preconditioner context (for example, 353831823bd8SMatthew G Knepley to configure it using the API). 353931823bd8SMatthew G Knepley 354031823bd8SMatthew G Knepley Level: developer 354131823bd8SMatthew G Knepley 354231823bd8SMatthew G Knepley .keywords: SNES, set, precondition 354331823bd8SMatthew G Knepley .seealso: SNESGetPC() 354431823bd8SMatthew G Knepley @*/ 354531823bd8SMatthew G Knepley PetscErrorCode SNESSetPC(SNES snes, SNES pc) 354631823bd8SMatthew G Knepley { 354731823bd8SMatthew G Knepley PetscErrorCode ierr; 354831823bd8SMatthew G Knepley 354931823bd8SMatthew G Knepley PetscFunctionBegin; 355031823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 355131823bd8SMatthew G Knepley PetscValidHeaderSpecific(pc, SNES_CLASSID, 2); 355231823bd8SMatthew G Knepley PetscCheckSameComm(snes, 1, pc, 2); 355331823bd8SMatthew G Knepley ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr); 3554bf11d3b6SBarry Smith ierr = SNESDestroy(&snes->pc);CHKERRQ(ierr); 355531823bd8SMatthew G Knepley snes->pc = pc; 355631823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 355731823bd8SMatthew G Knepley PetscFunctionReturn(0); 355831823bd8SMatthew G Knepley } 355931823bd8SMatthew G Knepley 356031823bd8SMatthew G Knepley #undef __FUNCT__ 356131823bd8SMatthew G Knepley #define __FUNCT__ "SNESGetPC" 356231823bd8SMatthew G Knepley /*@ 3563fd4b34e9SJed Brown SNESGetPC - Returns a pointer to the nonlinear preconditioning context set with SNESSetPC(). 356431823bd8SMatthew G Knepley 356531823bd8SMatthew G Knepley Not Collective 356631823bd8SMatthew G Knepley 356731823bd8SMatthew G Knepley Input Parameter: 356831823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate() 356931823bd8SMatthew G Knepley 357031823bd8SMatthew G Knepley Output Parameter: 357131823bd8SMatthew G Knepley . pc - preconditioner context 357231823bd8SMatthew G Knepley 357331823bd8SMatthew G Knepley Level: developer 357431823bd8SMatthew G Knepley 357531823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner 357631823bd8SMatthew G Knepley .seealso: SNESSetPC() 357731823bd8SMatthew G Knepley @*/ 357831823bd8SMatthew G Knepley PetscErrorCode SNESGetPC(SNES snes, SNES *pc) 357931823bd8SMatthew G Knepley { 358031823bd8SMatthew G Knepley PetscErrorCode ierr; 358131823bd8SMatthew G Knepley 358231823bd8SMatthew G Knepley PetscFunctionBegin; 358331823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 358431823bd8SMatthew G Knepley PetscValidPointer(pc, 2); 358531823bd8SMatthew G Knepley if (!snes->pc) { 358631823bd8SMatthew G Knepley ierr = SNESCreate(((PetscObject) snes)->comm, &snes->pc);CHKERRQ(ierr); 35874a0c5b0cSMatthew G Knepley ierr = PetscObjectIncrementTabLevel((PetscObject) snes->pc, (PetscObject) snes, 1);CHKERRQ(ierr); 358831823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 358931823bd8SMatthew G Knepley } 359031823bd8SMatthew G Knepley *pc = snes->pc; 359131823bd8SMatthew G Knepley PetscFunctionReturn(0); 359231823bd8SMatthew G Knepley } 359331823bd8SMatthew G Knepley 359469b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3595c6db04a5SJed Brown #include <mex.h> 359669b4f73cSBarry Smith 35978f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 35988f6e6473SBarry Smith 35990807856dSBarry Smith #undef __FUNCT__ 36000807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab" 36010807856dSBarry Smith /* 36020807856dSBarry Smith SNESComputeFunction_Matlab - Calls the function that has been set with 36030807856dSBarry Smith SNESSetFunctionMatlab(). 36040807856dSBarry Smith 36050807856dSBarry Smith Collective on SNES 36060807856dSBarry Smith 36070807856dSBarry Smith Input Parameters: 36080807856dSBarry Smith + snes - the SNES context 36090807856dSBarry Smith - x - input vector 36100807856dSBarry Smith 36110807856dSBarry Smith Output Parameter: 36120807856dSBarry Smith . y - function vector, as set by SNESSetFunction() 36130807856dSBarry Smith 36140807856dSBarry Smith Notes: 36150807856dSBarry Smith SNESComputeFunction() is typically used within nonlinear solvers 36160807856dSBarry Smith implementations, so most users would not generally call this routine 36170807856dSBarry Smith themselves. 36180807856dSBarry Smith 36190807856dSBarry Smith Level: developer 36200807856dSBarry Smith 36210807856dSBarry Smith .keywords: SNES, nonlinear, compute, function 36220807856dSBarry Smith 36230807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 362461b2408cSBarry Smith */ 36257087cfbeSBarry Smith PetscErrorCode SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx) 36260807856dSBarry Smith { 3627e650e774SBarry Smith PetscErrorCode ierr; 36288f6e6473SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 36298f6e6473SBarry Smith int nlhs = 1,nrhs = 5; 36308f6e6473SBarry Smith mxArray *plhs[1],*prhs[5]; 363191621f2eSBarry Smith long long int lx = 0,ly = 0,ls = 0; 3632e650e774SBarry Smith 36330807856dSBarry Smith PetscFunctionBegin; 36340807856dSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 36350807856dSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 36360807856dSBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 36370807856dSBarry Smith PetscCheckSameComm(snes,1,x,2); 36380807856dSBarry Smith PetscCheckSameComm(snes,1,y,3); 36390807856dSBarry Smith 36400807856dSBarry Smith /* call Matlab function in ctx with arguments x and y */ 3641e650e774SBarry Smith 364291621f2eSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3643e650e774SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3644e650e774SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 364591621f2eSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 364691621f2eSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 364791621f2eSBarry Smith prhs[2] = mxCreateDoubleScalar((double)ly); 36488f6e6473SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 36498f6e6473SBarry Smith prhs[4] = sctx->ctx; 3650b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr); 3651e650e774SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3652e650e774SBarry Smith mxDestroyArray(prhs[0]); 3653e650e774SBarry Smith mxDestroyArray(prhs[1]); 3654e650e774SBarry Smith mxDestroyArray(prhs[2]); 36558f6e6473SBarry Smith mxDestroyArray(prhs[3]); 3656e650e774SBarry Smith mxDestroyArray(plhs[0]); 36570807856dSBarry Smith PetscFunctionReturn(0); 36580807856dSBarry Smith } 36590807856dSBarry Smith 36600807856dSBarry Smith 36610807856dSBarry Smith #undef __FUNCT__ 36620807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab" 366361b2408cSBarry Smith /* 36640807856dSBarry Smith SNESSetFunctionMatlab - Sets the function evaluation routine and function 36650807856dSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3666e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 36670807856dSBarry Smith 36680807856dSBarry Smith Logically Collective on SNES 36690807856dSBarry Smith 36700807856dSBarry Smith Input Parameters: 36710807856dSBarry Smith + snes - the SNES context 36720807856dSBarry Smith . r - vector to store function value 36730807856dSBarry Smith - func - function evaluation routine 36740807856dSBarry Smith 36750807856dSBarry Smith Calling sequence of func: 367661b2408cSBarry Smith $ func (SNES snes,Vec x,Vec f,void *ctx); 36770807856dSBarry Smith 36780807856dSBarry Smith 36790807856dSBarry Smith Notes: 36800807856dSBarry Smith The Newton-like methods typically solve linear systems of the form 36810807856dSBarry Smith $ f'(x) x = -f(x), 36820807856dSBarry Smith where f'(x) denotes the Jacobian matrix and f(x) is the function. 36830807856dSBarry Smith 36840807856dSBarry Smith Level: beginner 36850807856dSBarry Smith 36860807856dSBarry Smith .keywords: SNES, nonlinear, set, function 36870807856dSBarry Smith 36880807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 368961b2408cSBarry Smith */ 36907087cfbeSBarry Smith PetscErrorCode SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx) 36910807856dSBarry Smith { 36920807856dSBarry Smith PetscErrorCode ierr; 36938f6e6473SBarry Smith SNESMatlabContext *sctx; 36940807856dSBarry Smith 36950807856dSBarry Smith PetscFunctionBegin; 36968f6e6473SBarry Smith /* currently sctx is memory bleed */ 36978f6e6473SBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 36988f6e6473SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 36998f6e6473SBarry Smith /* 37008f6e6473SBarry Smith This should work, but it doesn't 37018f6e6473SBarry Smith sctx->ctx = ctx; 37028f6e6473SBarry Smith mexMakeArrayPersistent(sctx->ctx); 37038f6e6473SBarry Smith */ 37048f6e6473SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 37058f6e6473SBarry Smith ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr); 37060807856dSBarry Smith PetscFunctionReturn(0); 37070807856dSBarry Smith } 370869b4f73cSBarry Smith 370961b2408cSBarry Smith #undef __FUNCT__ 371061b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab" 371161b2408cSBarry Smith /* 371261b2408cSBarry Smith SNESComputeJacobian_Matlab - Calls the function that has been set with 371361b2408cSBarry Smith SNESSetJacobianMatlab(). 371461b2408cSBarry Smith 371561b2408cSBarry Smith Collective on SNES 371661b2408cSBarry Smith 371761b2408cSBarry Smith Input Parameters: 371861b2408cSBarry Smith + snes - the SNES context 371961b2408cSBarry Smith . x - input vector 372061b2408cSBarry Smith . A, B - the matrices 372161b2408cSBarry Smith - ctx - user context 372261b2408cSBarry Smith 372361b2408cSBarry Smith Output Parameter: 372461b2408cSBarry Smith . flag - structure of the matrix 372561b2408cSBarry Smith 372661b2408cSBarry Smith Level: developer 372761b2408cSBarry Smith 372861b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function 372961b2408cSBarry Smith 373061b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 373161b2408cSBarry Smith @*/ 37327087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx) 373361b2408cSBarry Smith { 373461b2408cSBarry Smith PetscErrorCode ierr; 373561b2408cSBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 373661b2408cSBarry Smith int nlhs = 2,nrhs = 6; 373761b2408cSBarry Smith mxArray *plhs[2],*prhs[6]; 373861b2408cSBarry Smith long long int lx = 0,lA = 0,ls = 0, lB = 0; 373961b2408cSBarry Smith 374061b2408cSBarry Smith PetscFunctionBegin; 374161b2408cSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 374261b2408cSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 374361b2408cSBarry Smith 374461b2408cSBarry Smith /* call Matlab function in ctx with arguments x and y */ 374561b2408cSBarry Smith 374661b2408cSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 374761b2408cSBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 374861b2408cSBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 374961b2408cSBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 375061b2408cSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 375161b2408cSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 375261b2408cSBarry Smith prhs[2] = mxCreateDoubleScalar((double)lA); 375361b2408cSBarry Smith prhs[3] = mxCreateDoubleScalar((double)lB); 375461b2408cSBarry Smith prhs[4] = mxCreateString(sctx->funcname); 375561b2408cSBarry Smith prhs[5] = sctx->ctx; 3756b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr); 375761b2408cSBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 375861b2408cSBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 375961b2408cSBarry Smith mxDestroyArray(prhs[0]); 376061b2408cSBarry Smith mxDestroyArray(prhs[1]); 376161b2408cSBarry Smith mxDestroyArray(prhs[2]); 376261b2408cSBarry Smith mxDestroyArray(prhs[3]); 376361b2408cSBarry Smith mxDestroyArray(prhs[4]); 376461b2408cSBarry Smith mxDestroyArray(plhs[0]); 376561b2408cSBarry Smith mxDestroyArray(plhs[1]); 376661b2408cSBarry Smith PetscFunctionReturn(0); 376761b2408cSBarry Smith } 376861b2408cSBarry Smith 376961b2408cSBarry Smith 377061b2408cSBarry Smith #undef __FUNCT__ 377161b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab" 377261b2408cSBarry Smith /* 377361b2408cSBarry Smith SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 377461b2408cSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3775e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 377661b2408cSBarry Smith 377761b2408cSBarry Smith Logically Collective on SNES 377861b2408cSBarry Smith 377961b2408cSBarry Smith Input Parameters: 378061b2408cSBarry Smith + snes - the SNES context 378161b2408cSBarry Smith . A,B - Jacobian matrices 378261b2408cSBarry Smith . func - function evaluation routine 378361b2408cSBarry Smith - ctx - user context 378461b2408cSBarry Smith 378561b2408cSBarry Smith Calling sequence of func: 378661b2408cSBarry Smith $ flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx); 378761b2408cSBarry Smith 378861b2408cSBarry Smith 378961b2408cSBarry Smith Level: developer 379061b2408cSBarry Smith 379161b2408cSBarry Smith .keywords: SNES, nonlinear, set, function 379261b2408cSBarry Smith 379361b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 379461b2408cSBarry Smith */ 37957087cfbeSBarry Smith PetscErrorCode SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx) 379661b2408cSBarry Smith { 379761b2408cSBarry Smith PetscErrorCode ierr; 379861b2408cSBarry Smith SNESMatlabContext *sctx; 379961b2408cSBarry Smith 380061b2408cSBarry Smith PetscFunctionBegin; 380161b2408cSBarry Smith /* currently sctx is memory bleed */ 380261b2408cSBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 380361b2408cSBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 380461b2408cSBarry Smith /* 380561b2408cSBarry Smith This should work, but it doesn't 380661b2408cSBarry Smith sctx->ctx = ctx; 380761b2408cSBarry Smith mexMakeArrayPersistent(sctx->ctx); 380861b2408cSBarry Smith */ 380961b2408cSBarry Smith sctx->ctx = mxDuplicateArray(ctx); 381061b2408cSBarry Smith ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 381161b2408cSBarry Smith PetscFunctionReturn(0); 381261b2408cSBarry Smith } 381369b4f73cSBarry Smith 3814f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3815f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab" 3816f9eb7ae2SShri Abhyankar /* 3817f9eb7ae2SShri Abhyankar SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab(). 3818f9eb7ae2SShri Abhyankar 3819f9eb7ae2SShri Abhyankar Collective on SNES 3820f9eb7ae2SShri Abhyankar 3821f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction() 3822f9eb7ae2SShri Abhyankar @*/ 38237087cfbeSBarry Smith PetscErrorCode SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx) 3824f9eb7ae2SShri Abhyankar { 3825f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 382648f37e70SShri Abhyankar SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 3827f9eb7ae2SShri Abhyankar int nlhs = 1,nrhs = 6; 3828f9eb7ae2SShri Abhyankar mxArray *plhs[1],*prhs[6]; 3829f9eb7ae2SShri Abhyankar long long int lx = 0,ls = 0; 3830f9eb7ae2SShri Abhyankar Vec x=snes->vec_sol; 3831f9eb7ae2SShri Abhyankar 3832f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3833f9eb7ae2SShri Abhyankar PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3834f9eb7ae2SShri Abhyankar 3835f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3836f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3837f9eb7ae2SShri Abhyankar prhs[0] = mxCreateDoubleScalar((double)ls); 3838f9eb7ae2SShri Abhyankar prhs[1] = mxCreateDoubleScalar((double)it); 3839f9eb7ae2SShri Abhyankar prhs[2] = mxCreateDoubleScalar((double)fnorm); 3840f9eb7ae2SShri Abhyankar prhs[3] = mxCreateDoubleScalar((double)lx); 3841f9eb7ae2SShri Abhyankar prhs[4] = mxCreateString(sctx->funcname); 3842f9eb7ae2SShri Abhyankar prhs[5] = sctx->ctx; 3843f9eb7ae2SShri Abhyankar ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr); 3844f9eb7ae2SShri Abhyankar ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3845f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[0]); 3846f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[1]); 3847f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[2]); 3848f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[3]); 3849f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[4]); 3850f9eb7ae2SShri Abhyankar mxDestroyArray(plhs[0]); 3851f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 3852f9eb7ae2SShri Abhyankar } 3853f9eb7ae2SShri Abhyankar 3854f9eb7ae2SShri Abhyankar 3855f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3856f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab" 3857f9eb7ae2SShri Abhyankar /* 3858e3c5b3baSBarry Smith SNESMonitorSetMatlab - Sets the monitor function from MATLAB 3859f9eb7ae2SShri Abhyankar 3860f9eb7ae2SShri Abhyankar Level: developer 3861f9eb7ae2SShri Abhyankar 3862f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function 3863f9eb7ae2SShri Abhyankar 3864f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 3865f9eb7ae2SShri Abhyankar */ 38667087cfbeSBarry Smith PetscErrorCode SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx) 3867f9eb7ae2SShri Abhyankar { 3868f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 3869f9eb7ae2SShri Abhyankar SNESMatlabContext *sctx; 3870f9eb7ae2SShri Abhyankar 3871f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3872f9eb7ae2SShri Abhyankar /* currently sctx is memory bleed */ 3873f9eb7ae2SShri Abhyankar ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 3874f9eb7ae2SShri Abhyankar ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3875f9eb7ae2SShri Abhyankar /* 3876f9eb7ae2SShri Abhyankar This should work, but it doesn't 3877f9eb7ae2SShri Abhyankar sctx->ctx = ctx; 3878f9eb7ae2SShri Abhyankar mexMakeArrayPersistent(sctx->ctx); 3879f9eb7ae2SShri Abhyankar */ 3880f9eb7ae2SShri Abhyankar sctx->ctx = mxDuplicateArray(ctx); 3881f9eb7ae2SShri Abhyankar ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 3882f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 3883f9eb7ae2SShri Abhyankar } 3884f9eb7ae2SShri Abhyankar 388569b4f73cSBarry Smith #endif 3886