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); 498af60355fSPeter Brune ierr = PetscOptionsInt("-snes_ls_it" ,"Line search iterations","SNES",snes->ls_its,&snes->ls_its,&flg);CHKERRQ(ierr); 499ea630c6eSPeter Brune ierr = PetscOptionsBool("-snes_ls_monitor","Print progress of line searches","SNESLineSearchSetMonitor",snes->ls_monitor ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 500ea630c6eSPeter Brune if (set) {ierr = SNESLineSearchSetMonitor(snes,flg);CHKERRQ(ierr);} 50115f5eeeaSPeter Brune ierr = PetscOptionsEnum("-snes_ls","Line search used","SNESLineSearchSet",SNESLineSearchTypes,(PetscEnum)snes->ls_type,(PetscEnum*)&indx,&flg);CHKERRQ(ierr); 50215f5eeeaSPeter Brune if (flg) { 503ea630c6eSPeter Brune ierr = SNESLineSearchSetType(snes,(SNESLineSearchType)indx);CHKERRQ(ierr); 50415f5eeeaSPeter Brune } 5058e3fc8c0SJed Brown flg = snes->ops->precheckstep == SNESLineSearchPreCheckPicard ? PETSC_TRUE : PETSC_FALSE; 5068e3fc8c0SJed Brown ierr = PetscOptionsBool("-snes_ls_precheck_picard","Use a correction that sometimes improves convergence of Picard iteration","SNESLineSearchPreCheckPicard",flg,&flg,&set);CHKERRQ(ierr); 5078e3fc8c0SJed Brown if (set) { 5088e3fc8c0SJed Brown if (flg) { 5098e3fc8c0SJed Brown snes->precheck_picard_angle = 10.; /* correction only active if angle is less than 10 degrees */ 5108e3fc8c0SJed 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); 5118e3fc8c0SJed Brown ierr = SNESLineSearchSetPreCheck(snes,SNESLineSearchPreCheckPicard,&snes->precheck_picard_angle);CHKERRQ(ierr); 5128e3fc8c0SJed Brown } else { 5138e3fc8c0SJed Brown ierr = SNESLineSearchSetPreCheck(snes,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 5148e3fc8c0SJed Brown } 5158e3fc8c0SJed Brown } 5168e3fc8c0SJed Brown 51776b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 51876b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 51976b2cf59SMatthew Knepley } 52076b2cf59SMatthew Knepley 521e7788613SBarry Smith if (snes->ops->setfromoptions) { 522e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 523639f9d9dSBarry Smith } 5245d973c19SBarry Smith 5255d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 5265d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr); 527b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 5284bbc92c1SBarry Smith 529aa3661deSLisandro Dalcin if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); } 5301cee3971SBarry Smith 5311cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 532aa3661deSLisandro Dalcin ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag); 533aa3661deSLisandro Dalcin ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr); 53485385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 53593993e2dSLois Curfman McInnes 53651e86f29SPeter Brune /* if someone has set the SNES PC type, create it. */ 53751e86f29SPeter Brune ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr); 53851e86f29SPeter Brune ierr = PetscOptionsHasName(optionsprefix, "-npc_snes_type", &pcset);CHKERRQ(ierr); 53951e86f29SPeter Brune if (pcset && (!snes->pc)) { 54051e86f29SPeter Brune ierr = SNESGetPC(snes, &snes->pc);CHKERRQ(ierr); 54151e86f29SPeter Brune } 54251e86f29SPeter Brune 5434a0c5b0cSMatthew G Knepley if (snes->pc) { 5444a0c5b0cSMatthew G Knepley ierr = SNESSetOptionsPrefix(snes->pc, "npc_");CHKERRQ(ierr); 5454a0c5b0cSMatthew G Knepley ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 5464a0c5b0cSMatthew G Knepley /* Should we make a duplicate vector and matrix? Leave the DM to make it? */ 5474a0c5b0cSMatthew G Knepley ierr = SNESSetFunction(snes->pc, snes->vec_func, snes->ops->computefunction, snes->funP);CHKERRQ(ierr); 5484a0c5b0cSMatthew G Knepley ierr = SNESSetJacobian(snes->pc, snes->jacobian, snes->jacobian_pre, snes->ops->computejacobian, snes->jacP);CHKERRQ(ierr); 5494a0c5b0cSMatthew G Knepley ierr = SNESSetFromOptions(snes->pc);CHKERRQ(ierr); 5504a0c5b0cSMatthew G Knepley } 5513a40ed3dSBarry Smith PetscFunctionReturn(0); 5529b94acceSBarry Smith } 5539b94acceSBarry Smith 554d25893d9SBarry Smith #undef __FUNCT__ 555d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext" 556d25893d9SBarry Smith /*@ 557d25893d9SBarry Smith SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for 558d25893d9SBarry Smith the nonlinear solvers. 559d25893d9SBarry Smith 560d25893d9SBarry Smith Logically Collective on SNES 561d25893d9SBarry Smith 562d25893d9SBarry Smith Input Parameters: 563d25893d9SBarry Smith + snes - the SNES context 564d25893d9SBarry Smith . compute - function to compute the context 565d25893d9SBarry Smith - destroy - function to destroy the context 566d25893d9SBarry Smith 567d25893d9SBarry Smith Level: intermediate 568d25893d9SBarry Smith 569d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context 570d25893d9SBarry Smith 571d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext() 572d25893d9SBarry Smith @*/ 573d25893d9SBarry Smith PetscErrorCode SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**)) 574d25893d9SBarry Smith { 575d25893d9SBarry Smith PetscFunctionBegin; 576d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 577d25893d9SBarry Smith snes->ops->usercompute = compute; 578d25893d9SBarry Smith snes->ops->userdestroy = destroy; 579d25893d9SBarry Smith PetscFunctionReturn(0); 580d25893d9SBarry Smith } 581a847f771SSatish Balay 5824a2ae208SSatish Balay #undef __FUNCT__ 5834a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 584b07ff414SBarry Smith /*@ 5859b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 5869b94acceSBarry Smith the nonlinear solvers. 5879b94acceSBarry Smith 5883f9fe445SBarry Smith Logically Collective on SNES 589fee21e36SBarry Smith 590c7afd0dbSLois Curfman McInnes Input Parameters: 591c7afd0dbSLois Curfman McInnes + snes - the SNES context 592c7afd0dbSLois Curfman McInnes - usrP - optional user context 593c7afd0dbSLois Curfman McInnes 59436851e7fSLois Curfman McInnes Level: intermediate 59536851e7fSLois Curfman McInnes 5969b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 5979b94acceSBarry Smith 598d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext() 5999b94acceSBarry Smith @*/ 6007087cfbeSBarry Smith PetscErrorCode SNESSetApplicationContext(SNES snes,void *usrP) 6019b94acceSBarry Smith { 6021b2093e4SBarry Smith PetscErrorCode ierr; 603b07ff414SBarry Smith KSP ksp; 6041b2093e4SBarry Smith 6053a40ed3dSBarry Smith PetscFunctionBegin; 6060700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 607b07ff414SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 608b07ff414SBarry Smith ierr = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr); 6099b94acceSBarry Smith snes->user = usrP; 6103a40ed3dSBarry Smith PetscFunctionReturn(0); 6119b94acceSBarry Smith } 61274679c65SBarry Smith 6134a2ae208SSatish Balay #undef __FUNCT__ 6144a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 615b07ff414SBarry Smith /*@ 6169b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 6179b94acceSBarry Smith nonlinear solvers. 6189b94acceSBarry Smith 619c7afd0dbSLois Curfman McInnes Not Collective 620c7afd0dbSLois Curfman McInnes 6219b94acceSBarry Smith Input Parameter: 6229b94acceSBarry Smith . snes - SNES context 6239b94acceSBarry Smith 6249b94acceSBarry Smith Output Parameter: 6259b94acceSBarry Smith . usrP - user context 6269b94acceSBarry Smith 62736851e7fSLois Curfman McInnes Level: intermediate 62836851e7fSLois Curfman McInnes 6299b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 6309b94acceSBarry Smith 6319b94acceSBarry Smith .seealso: SNESSetApplicationContext() 6329b94acceSBarry Smith @*/ 633e71120c6SJed Brown PetscErrorCode SNESGetApplicationContext(SNES snes,void *usrP) 6349b94acceSBarry Smith { 6353a40ed3dSBarry Smith PetscFunctionBegin; 6360700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 637e71120c6SJed Brown *(void**)usrP = snes->user; 6383a40ed3dSBarry Smith PetscFunctionReturn(0); 6399b94acceSBarry Smith } 64074679c65SBarry Smith 6414a2ae208SSatish Balay #undef __FUNCT__ 6424a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 6439b94acceSBarry Smith /*@ 644c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 645c8228a4eSBarry Smith at this time. 6469b94acceSBarry Smith 647c7afd0dbSLois Curfman McInnes Not Collective 648c7afd0dbSLois Curfman McInnes 6499b94acceSBarry Smith Input Parameter: 6509b94acceSBarry Smith . snes - SNES context 6519b94acceSBarry Smith 6529b94acceSBarry Smith Output Parameter: 6539b94acceSBarry Smith . iter - iteration number 6549b94acceSBarry Smith 655c8228a4eSBarry Smith Notes: 656c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 657c8228a4eSBarry Smith 658c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 65908405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 66008405cd6SLois Curfman McInnes .vb 66108405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 66208405cd6SLois Curfman McInnes if (!(it % 2)) { 66308405cd6SLois Curfman McInnes [compute Jacobian here] 66408405cd6SLois Curfman McInnes } 66508405cd6SLois Curfman McInnes .ve 666c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 66708405cd6SLois Curfman McInnes recomputed every second SNES iteration. 668c8228a4eSBarry Smith 66936851e7fSLois Curfman McInnes Level: intermediate 67036851e7fSLois Curfman McInnes 6712b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 6722b668275SBarry Smith 673b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 6749b94acceSBarry Smith @*/ 6757087cfbeSBarry Smith PetscErrorCode SNESGetIterationNumber(SNES snes,PetscInt* iter) 6769b94acceSBarry Smith { 6773a40ed3dSBarry Smith PetscFunctionBegin; 6780700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6794482741eSBarry Smith PetscValidIntPointer(iter,2); 6809b94acceSBarry Smith *iter = snes->iter; 6813a40ed3dSBarry Smith PetscFunctionReturn(0); 6829b94acceSBarry Smith } 68374679c65SBarry Smith 6844a2ae208SSatish Balay #undef __FUNCT__ 6854a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 6869b94acceSBarry Smith /*@ 6879b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 6889b94acceSBarry Smith with SNESSSetFunction(). 6899b94acceSBarry Smith 690c7afd0dbSLois Curfman McInnes Collective on SNES 691c7afd0dbSLois Curfman McInnes 6929b94acceSBarry Smith Input Parameter: 6939b94acceSBarry Smith . snes - SNES context 6949b94acceSBarry Smith 6959b94acceSBarry Smith Output Parameter: 6969b94acceSBarry Smith . fnorm - 2-norm of function 6979b94acceSBarry Smith 69836851e7fSLois Curfman McInnes Level: intermediate 69936851e7fSLois Curfman McInnes 7009b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 701a86d99e1SLois Curfman McInnes 702b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 7039b94acceSBarry Smith @*/ 7047087cfbeSBarry Smith PetscErrorCode SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 7059b94acceSBarry Smith { 7063a40ed3dSBarry Smith PetscFunctionBegin; 7070700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7084482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 7099b94acceSBarry Smith *fnorm = snes->norm; 7103a40ed3dSBarry Smith PetscFunctionReturn(0); 7119b94acceSBarry Smith } 71274679c65SBarry Smith 7134a2ae208SSatish Balay #undef __FUNCT__ 714b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 7159b94acceSBarry Smith /*@ 716b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 7179b94acceSBarry Smith attempted by the nonlinear solver. 7189b94acceSBarry Smith 719c7afd0dbSLois Curfman McInnes Not Collective 720c7afd0dbSLois Curfman McInnes 7219b94acceSBarry Smith Input Parameter: 7229b94acceSBarry Smith . snes - SNES context 7239b94acceSBarry Smith 7249b94acceSBarry Smith Output Parameter: 7259b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 7269b94acceSBarry Smith 727c96a6f78SLois Curfman McInnes Notes: 728c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 729c96a6f78SLois Curfman McInnes 73036851e7fSLois Curfman McInnes Level: intermediate 73136851e7fSLois Curfman McInnes 7329b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 73358ebbce7SBarry Smith 734e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 73558ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 7369b94acceSBarry Smith @*/ 7377087cfbeSBarry Smith PetscErrorCode SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 7389b94acceSBarry Smith { 7393a40ed3dSBarry Smith PetscFunctionBegin; 7400700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7414482741eSBarry Smith PetscValidIntPointer(nfails,2); 74250ffb88aSMatthew Knepley *nfails = snes->numFailures; 74350ffb88aSMatthew Knepley PetscFunctionReturn(0); 74450ffb88aSMatthew Knepley } 74550ffb88aSMatthew Knepley 74650ffb88aSMatthew Knepley #undef __FUNCT__ 747b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 74850ffb88aSMatthew Knepley /*@ 749b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 75050ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 75150ffb88aSMatthew Knepley 75250ffb88aSMatthew Knepley Not Collective 75350ffb88aSMatthew Knepley 75450ffb88aSMatthew Knepley Input Parameters: 75550ffb88aSMatthew Knepley + snes - SNES context 75650ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 75750ffb88aSMatthew Knepley 75850ffb88aSMatthew Knepley Level: intermediate 75950ffb88aSMatthew Knepley 76050ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 76158ebbce7SBarry Smith 762e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 76358ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 76450ffb88aSMatthew Knepley @*/ 7657087cfbeSBarry Smith PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 76650ffb88aSMatthew Knepley { 76750ffb88aSMatthew Knepley PetscFunctionBegin; 7680700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 76950ffb88aSMatthew Knepley snes->maxFailures = maxFails; 77050ffb88aSMatthew Knepley PetscFunctionReturn(0); 77150ffb88aSMatthew Knepley } 77250ffb88aSMatthew Knepley 77350ffb88aSMatthew Knepley #undef __FUNCT__ 774b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 77550ffb88aSMatthew Knepley /*@ 776b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 77750ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 77850ffb88aSMatthew Knepley 77950ffb88aSMatthew Knepley Not Collective 78050ffb88aSMatthew Knepley 78150ffb88aSMatthew Knepley Input Parameter: 78250ffb88aSMatthew Knepley . snes - SNES context 78350ffb88aSMatthew Knepley 78450ffb88aSMatthew Knepley Output Parameter: 78550ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 78650ffb88aSMatthew Knepley 78750ffb88aSMatthew Knepley Level: intermediate 78850ffb88aSMatthew Knepley 78950ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 79058ebbce7SBarry Smith 791e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 79258ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 79358ebbce7SBarry Smith 79450ffb88aSMatthew Knepley @*/ 7957087cfbeSBarry Smith PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 79650ffb88aSMatthew Knepley { 79750ffb88aSMatthew Knepley PetscFunctionBegin; 7980700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7994482741eSBarry Smith PetscValidIntPointer(maxFails,2); 80050ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 8013a40ed3dSBarry Smith PetscFunctionReturn(0); 8029b94acceSBarry Smith } 803a847f771SSatish Balay 8044a2ae208SSatish Balay #undef __FUNCT__ 8052541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 8062541af92SBarry Smith /*@ 8072541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 8082541af92SBarry Smith done by SNES. 8092541af92SBarry Smith 8102541af92SBarry Smith Not Collective 8112541af92SBarry Smith 8122541af92SBarry Smith Input Parameter: 8132541af92SBarry Smith . snes - SNES context 8142541af92SBarry Smith 8152541af92SBarry Smith Output Parameter: 8162541af92SBarry Smith . nfuncs - number of evaluations 8172541af92SBarry Smith 8182541af92SBarry Smith Level: intermediate 8192541af92SBarry Smith 8202541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 82158ebbce7SBarry Smith 822e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 8232541af92SBarry Smith @*/ 8247087cfbeSBarry Smith PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 8252541af92SBarry Smith { 8262541af92SBarry Smith PetscFunctionBegin; 8270700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8282541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 8292541af92SBarry Smith *nfuncs = snes->nfuncs; 8302541af92SBarry Smith PetscFunctionReturn(0); 8312541af92SBarry Smith } 8322541af92SBarry Smith 8332541af92SBarry Smith #undef __FUNCT__ 8343d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 8353d4c4710SBarry Smith /*@ 8363d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 8373d4c4710SBarry Smith linear solvers. 8383d4c4710SBarry Smith 8393d4c4710SBarry Smith Not Collective 8403d4c4710SBarry Smith 8413d4c4710SBarry Smith Input Parameter: 8423d4c4710SBarry Smith . snes - SNES context 8433d4c4710SBarry Smith 8443d4c4710SBarry Smith Output Parameter: 8453d4c4710SBarry Smith . nfails - number of failed solves 8463d4c4710SBarry Smith 8473d4c4710SBarry Smith Notes: 8483d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 8493d4c4710SBarry Smith 8503d4c4710SBarry Smith Level: intermediate 8513d4c4710SBarry Smith 8523d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 85358ebbce7SBarry Smith 854e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 8553d4c4710SBarry Smith @*/ 8567087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 8573d4c4710SBarry Smith { 8583d4c4710SBarry Smith PetscFunctionBegin; 8590700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8603d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 8613d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 8623d4c4710SBarry Smith PetscFunctionReturn(0); 8633d4c4710SBarry Smith } 8643d4c4710SBarry Smith 8653d4c4710SBarry Smith #undef __FUNCT__ 8663d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 8673d4c4710SBarry Smith /*@ 8683d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 8693d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 8703d4c4710SBarry Smith 8713f9fe445SBarry Smith Logically Collective on SNES 8723d4c4710SBarry Smith 8733d4c4710SBarry Smith Input Parameters: 8743d4c4710SBarry Smith + snes - SNES context 8753d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 8763d4c4710SBarry Smith 8773d4c4710SBarry Smith Level: intermediate 8783d4c4710SBarry Smith 879a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 8803d4c4710SBarry Smith 8813d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 8823d4c4710SBarry Smith 88358ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 8843d4c4710SBarry Smith @*/ 8857087cfbeSBarry Smith PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 8863d4c4710SBarry Smith { 8873d4c4710SBarry Smith PetscFunctionBegin; 8880700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 889c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxFails,2); 8903d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 8913d4c4710SBarry Smith PetscFunctionReturn(0); 8923d4c4710SBarry Smith } 8933d4c4710SBarry Smith 8943d4c4710SBarry Smith #undef __FUNCT__ 8953d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 8963d4c4710SBarry Smith /*@ 8973d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 8983d4c4710SBarry Smith are allowed before SNES terminates 8993d4c4710SBarry Smith 9003d4c4710SBarry Smith Not Collective 9013d4c4710SBarry Smith 9023d4c4710SBarry Smith Input Parameter: 9033d4c4710SBarry Smith . snes - SNES context 9043d4c4710SBarry Smith 9053d4c4710SBarry Smith Output Parameter: 9063d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 9073d4c4710SBarry Smith 9083d4c4710SBarry Smith Level: intermediate 9093d4c4710SBarry Smith 9103d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 9113d4c4710SBarry Smith 9123d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 9133d4c4710SBarry Smith 914e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 9153d4c4710SBarry Smith @*/ 9167087cfbeSBarry Smith PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 9173d4c4710SBarry Smith { 9183d4c4710SBarry Smith PetscFunctionBegin; 9190700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9203d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 9213d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 9223d4c4710SBarry Smith PetscFunctionReturn(0); 9233d4c4710SBarry Smith } 9243d4c4710SBarry Smith 9253d4c4710SBarry Smith #undef __FUNCT__ 926b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 927c96a6f78SLois Curfman McInnes /*@ 928b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 929c96a6f78SLois Curfman McInnes used by the nonlinear solver. 930c96a6f78SLois Curfman McInnes 931c7afd0dbSLois Curfman McInnes Not Collective 932c7afd0dbSLois Curfman McInnes 933c96a6f78SLois Curfman McInnes Input Parameter: 934c96a6f78SLois Curfman McInnes . snes - SNES context 935c96a6f78SLois Curfman McInnes 936c96a6f78SLois Curfman McInnes Output Parameter: 937c96a6f78SLois Curfman McInnes . lits - number of linear iterations 938c96a6f78SLois Curfman McInnes 939c96a6f78SLois Curfman McInnes Notes: 940c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 941c96a6f78SLois Curfman McInnes 94236851e7fSLois Curfman McInnes Level: intermediate 94336851e7fSLois Curfman McInnes 944c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 9452b668275SBarry Smith 9468c7482ecSBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 947c96a6f78SLois Curfman McInnes @*/ 9487087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 949c96a6f78SLois Curfman McInnes { 9503a40ed3dSBarry Smith PetscFunctionBegin; 9510700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9524482741eSBarry Smith PetscValidIntPointer(lits,2); 953c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 9543a40ed3dSBarry Smith PetscFunctionReturn(0); 955c96a6f78SLois Curfman McInnes } 956c96a6f78SLois Curfman McInnes 9574a2ae208SSatish Balay #undef __FUNCT__ 95894b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 95952baeb72SSatish Balay /*@ 96094b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 9619b94acceSBarry Smith 96294b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 963c7afd0dbSLois Curfman McInnes 9649b94acceSBarry Smith Input Parameter: 9659b94acceSBarry Smith . snes - the SNES context 9669b94acceSBarry Smith 9679b94acceSBarry Smith Output Parameter: 96894b7f48cSBarry Smith . ksp - the KSP context 9699b94acceSBarry Smith 9709b94acceSBarry Smith Notes: 97194b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 9729b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 9732999313aSBarry Smith PC contexts as well. 9749b94acceSBarry Smith 97536851e7fSLois Curfman McInnes Level: beginner 97636851e7fSLois Curfman McInnes 97794b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 9789b94acceSBarry Smith 9792999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 9809b94acceSBarry Smith @*/ 9817087cfbeSBarry Smith PetscErrorCode SNESGetKSP(SNES snes,KSP *ksp) 9829b94acceSBarry Smith { 9831cee3971SBarry Smith PetscErrorCode ierr; 9841cee3971SBarry Smith 9853a40ed3dSBarry Smith PetscFunctionBegin; 9860700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9874482741eSBarry Smith PetscValidPointer(ksp,2); 9881cee3971SBarry Smith 9891cee3971SBarry Smith if (!snes->ksp) { 9901cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 9911cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 9921cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 9931cee3971SBarry Smith } 99494b7f48cSBarry Smith *ksp = snes->ksp; 9953a40ed3dSBarry Smith PetscFunctionReturn(0); 9969b94acceSBarry Smith } 99782bf6240SBarry Smith 9984a2ae208SSatish Balay #undef __FUNCT__ 9992999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 10002999313aSBarry Smith /*@ 10012999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 10022999313aSBarry Smith 10032999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 10042999313aSBarry Smith 10052999313aSBarry Smith Input Parameters: 10062999313aSBarry Smith + snes - the SNES context 10072999313aSBarry Smith - ksp - the KSP context 10082999313aSBarry Smith 10092999313aSBarry Smith Notes: 10102999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 10112999313aSBarry Smith so this routine is rarely needed. 10122999313aSBarry Smith 10132999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 10142999313aSBarry Smith decreased by one. 10152999313aSBarry Smith 10162999313aSBarry Smith Level: developer 10172999313aSBarry Smith 10182999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 10192999313aSBarry Smith 10202999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 10212999313aSBarry Smith @*/ 10227087cfbeSBarry Smith PetscErrorCode SNESSetKSP(SNES snes,KSP ksp) 10232999313aSBarry Smith { 10242999313aSBarry Smith PetscErrorCode ierr; 10252999313aSBarry Smith 10262999313aSBarry Smith PetscFunctionBegin; 10270700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10280700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 10292999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 10307dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 1031906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 10322999313aSBarry Smith snes->ksp = ksp; 10332999313aSBarry Smith PetscFunctionReturn(0); 10342999313aSBarry Smith } 10352999313aSBarry Smith 10367adad957SLisandro Dalcin #if 0 10372999313aSBarry Smith #undef __FUNCT__ 10384a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 10396849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 1040e24b481bSBarry Smith { 1041e24b481bSBarry Smith PetscFunctionBegin; 1042e24b481bSBarry Smith PetscFunctionReturn(0); 1043e24b481bSBarry Smith } 10447adad957SLisandro Dalcin #endif 1045e24b481bSBarry Smith 10469b94acceSBarry Smith /* -----------------------------------------------------------*/ 10474a2ae208SSatish Balay #undef __FUNCT__ 10484a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 104952baeb72SSatish Balay /*@ 10509b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 10519b94acceSBarry Smith 1052c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 1053c7afd0dbSLois Curfman McInnes 1054c7afd0dbSLois Curfman McInnes Input Parameters: 1055906ed7ccSBarry Smith . comm - MPI communicator 10569b94acceSBarry Smith 10579b94acceSBarry Smith Output Parameter: 10589b94acceSBarry Smith . outsnes - the new SNES context 10599b94acceSBarry Smith 1060c7afd0dbSLois Curfman McInnes Options Database Keys: 1061c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 1062c7afd0dbSLois Curfman McInnes and no preconditioning matrix 1063c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 1064c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 1065c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 1066c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 1067c1f60f51SBarry Smith 106836851e7fSLois Curfman McInnes Level: beginner 106936851e7fSLois Curfman McInnes 10709b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 10719b94acceSBarry Smith 1072a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 1073a8054027SBarry Smith 10749b94acceSBarry Smith @*/ 10757087cfbeSBarry Smith PetscErrorCode SNESCreate(MPI_Comm comm,SNES *outsnes) 10769b94acceSBarry Smith { 1077dfbe8321SBarry Smith PetscErrorCode ierr; 10789b94acceSBarry Smith SNES snes; 1079fa9f3622SBarry Smith SNESKSPEW *kctx; 108037fcc0dbSBarry Smith 10813a40ed3dSBarry Smith PetscFunctionBegin; 1082ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 10838ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 10848ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 10858ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 10868ba1e511SMatthew Knepley #endif 10878ba1e511SMatthew Knepley 10883194b578SJed Brown ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 10897adad957SLisandro Dalcin 109085385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 10912c155ee1SBarry Smith snes->usesksp = PETSC_TRUE; 10929b94acceSBarry Smith snes->max_its = 50; 10939750a799SBarry Smith snes->max_funcs = 10000; 10949b94acceSBarry Smith snes->norm = 0.0; 1095b4874afaSBarry Smith snes->rtol = 1.e-8; 1096b4874afaSBarry Smith snes->ttol = 0.0; 109770441072SBarry Smith snes->abstol = 1.e-50; 10989b94acceSBarry Smith snes->xtol = 1.e-8; 10994b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 11009b94acceSBarry Smith snes->nfuncs = 0; 110150ffb88aSMatthew Knepley snes->numFailures = 0; 110250ffb88aSMatthew Knepley snes->maxFailures = 1; 11037a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1104e35cf81dSBarry Smith snes->lagjacobian = 1; 1105a8054027SBarry Smith snes->lagpreconditioner = 1; 1106639f9d9dSBarry Smith snes->numbermonitors = 0; 11079b94acceSBarry Smith snes->data = 0; 11084dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1109186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 11106f24a144SLois Curfman McInnes snes->nwork = 0; 111158c9b817SLisandro Dalcin snes->work = 0; 111258c9b817SLisandro Dalcin snes->nvwork = 0; 111358c9b817SLisandro Dalcin snes->vwork = 0; 1114758f92a0SBarry Smith snes->conv_hist_len = 0; 1115758f92a0SBarry Smith snes->conv_hist_max = 0; 1116758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 1117758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 1118758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1119184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 11209b94acceSBarry Smith 1121ea630c6eSPeter Brune /* initialize the line search options */ 1122ea630c6eSPeter Brune snes->ls_type = SNES_LS_BASIC; 1123af60355fSPeter Brune snes->ls_its = 1; 1124ea630c6eSPeter Brune snes->damping = 1.0; 1125ea630c6eSPeter Brune snes->maxstep = 1e8; 1126ea630c6eSPeter Brune snes->steptol = 1e-12; 1127ea630c6eSPeter Brune snes->ls_alpha = 1e-4; 1128ea630c6eSPeter Brune snes->ls_monitor = PETSC_NULL; 1129ea630c6eSPeter Brune 1130ea630c6eSPeter Brune snes->ops->linesearch = PETSC_NULL; 1131ea630c6eSPeter Brune snes->precheck = PETSC_NULL; 1132ea630c6eSPeter Brune snes->ops->precheckstep = PETSC_NULL; 1133ea630c6eSPeter Brune snes->postcheck = PETSC_NULL; 1134ea630c6eSPeter Brune snes->ops->postcheckstep= PETSC_NULL; 1135ea630c6eSPeter Brune 11363d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 11373d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 11383d4c4710SBarry Smith 11399b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 114038f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 11419b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 11429b94acceSBarry Smith kctx->version = 2; 11439b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 11449b94acceSBarry Smith this was too large for some test cases */ 114575567043SBarry Smith kctx->rtol_last = 0.0; 11469b94acceSBarry Smith kctx->rtol_max = .9; 11479b94acceSBarry Smith kctx->gamma = 1.0; 114871f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 114971f87433Sdalcinl kctx->alpha2 = kctx->alpha; 11509b94acceSBarry Smith kctx->threshold = .1; 115175567043SBarry Smith kctx->lresid_last = 0.0; 115275567043SBarry Smith kctx->norm_last = 0.0; 11539b94acceSBarry Smith 11549b94acceSBarry Smith *outsnes = snes; 11553a40ed3dSBarry Smith PetscFunctionReturn(0); 11569b94acceSBarry Smith } 11579b94acceSBarry Smith 11584a2ae208SSatish Balay #undef __FUNCT__ 11594a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 11609b94acceSBarry Smith /*@C 11619b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 11629b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 11639b94acceSBarry Smith equations. 11649b94acceSBarry Smith 11653f9fe445SBarry Smith Logically Collective on SNES 1166fee21e36SBarry Smith 1167c7afd0dbSLois Curfman McInnes Input Parameters: 1168c7afd0dbSLois Curfman McInnes + snes - the SNES context 1169c7afd0dbSLois Curfman McInnes . r - vector to store function value 1170de044059SHong Zhang . func - function evaluation routine 1171c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1172c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 11739b94acceSBarry Smith 1174c7afd0dbSLois Curfman McInnes Calling sequence of func: 11758d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 1176c7afd0dbSLois Curfman McInnes 1177313e4042SLois Curfman McInnes . f - function vector 1178c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 11799b94acceSBarry Smith 11809b94acceSBarry Smith Notes: 11819b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 11829b94acceSBarry Smith $ f'(x) x = -f(x), 1183c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 11849b94acceSBarry Smith 118536851e7fSLois Curfman McInnes Level: beginner 118636851e7fSLois Curfman McInnes 11879b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 11889b94acceSBarry Smith 1189a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 11909b94acceSBarry Smith @*/ 11917087cfbeSBarry Smith PetscErrorCode SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 11929b94acceSBarry Smith { 119385385478SLisandro Dalcin PetscErrorCode ierr; 11943a40ed3dSBarry Smith PetscFunctionBegin; 11950700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1196d2a683ecSLisandro Dalcin if (r) { 1197d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1198d2a683ecSLisandro Dalcin PetscCheckSameComm(snes,1,r,2); 119985385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 12006bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 120185385478SLisandro Dalcin snes->vec_func = r; 1202d2a683ecSLisandro Dalcin } else if (!snes->vec_func && snes->dm) { 1203d2a683ecSLisandro Dalcin ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1204d2a683ecSLisandro Dalcin } 1205d2a683ecSLisandro Dalcin if (func) snes->ops->computefunction = func; 1206d2a683ecSLisandro Dalcin if (ctx) snes->funP = ctx; 12073a40ed3dSBarry Smith PetscFunctionReturn(0); 12089b94acceSBarry Smith } 12099b94acceSBarry Smith 1210d25893d9SBarry Smith #undef __FUNCT__ 1211d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess" 1212d25893d9SBarry Smith /*@C 1213d25893d9SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem 1214d25893d9SBarry Smith 1215d25893d9SBarry Smith Logically Collective on SNES 1216d25893d9SBarry Smith 1217d25893d9SBarry Smith Input Parameters: 1218d25893d9SBarry Smith + snes - the SNES context 1219d25893d9SBarry Smith . func - function evaluation routine 1220d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 1221d25893d9SBarry Smith function evaluation routine (may be PETSC_NULL) 1222d25893d9SBarry Smith 1223d25893d9SBarry Smith Calling sequence of func: 1224d25893d9SBarry Smith $ func (SNES snes,Vec x,void *ctx); 1225d25893d9SBarry Smith 1226d25893d9SBarry Smith . f - function vector 1227d25893d9SBarry Smith - ctx - optional user-defined function context 1228d25893d9SBarry Smith 1229d25893d9SBarry Smith Level: intermediate 1230d25893d9SBarry Smith 1231d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function 1232d25893d9SBarry Smith 1233d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 1234d25893d9SBarry Smith @*/ 1235d25893d9SBarry Smith PetscErrorCode SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx) 1236d25893d9SBarry Smith { 1237d25893d9SBarry Smith PetscFunctionBegin; 1238d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1239d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 1240d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 1241d25893d9SBarry Smith PetscFunctionReturn(0); 1242d25893d9SBarry Smith } 1243d25893d9SBarry Smith 12443ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 12453ab0aad5SBarry Smith #undef __FUNCT__ 12461096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 12471096aae1SMatthew Knepley /*@C 12481096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 12491096aae1SMatthew Knepley it assumes a zero right hand side. 12501096aae1SMatthew Knepley 12513f9fe445SBarry Smith Logically Collective on SNES 12521096aae1SMatthew Knepley 12531096aae1SMatthew Knepley Input Parameter: 12541096aae1SMatthew Knepley . snes - the SNES context 12551096aae1SMatthew Knepley 12561096aae1SMatthew Knepley Output Parameter: 1257bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 12581096aae1SMatthew Knepley 12591096aae1SMatthew Knepley Level: intermediate 12601096aae1SMatthew Knepley 12611096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 12621096aae1SMatthew Knepley 126385385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 12641096aae1SMatthew Knepley @*/ 12657087cfbeSBarry Smith PetscErrorCode SNESGetRhs(SNES snes,Vec *rhs) 12661096aae1SMatthew Knepley { 12671096aae1SMatthew Knepley PetscFunctionBegin; 12680700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12691096aae1SMatthew Knepley PetscValidPointer(rhs,2); 127085385478SLisandro Dalcin *rhs = snes->vec_rhs; 12711096aae1SMatthew Knepley PetscFunctionReturn(0); 12721096aae1SMatthew Knepley } 12731096aae1SMatthew Knepley 12741096aae1SMatthew Knepley #undef __FUNCT__ 12754a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 12769b94acceSBarry Smith /*@ 127736851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 12789b94acceSBarry Smith SNESSetFunction(). 12799b94acceSBarry Smith 1280c7afd0dbSLois Curfman McInnes Collective on SNES 1281c7afd0dbSLois Curfman McInnes 12829b94acceSBarry Smith Input Parameters: 1283c7afd0dbSLois Curfman McInnes + snes - the SNES context 1284c7afd0dbSLois Curfman McInnes - x - input vector 12859b94acceSBarry Smith 12869b94acceSBarry Smith Output Parameter: 12873638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 12889b94acceSBarry Smith 12891bffabb2SLois Curfman McInnes Notes: 129036851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 129136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 129236851e7fSLois Curfman McInnes themselves. 129336851e7fSLois Curfman McInnes 129436851e7fSLois Curfman McInnes Level: developer 129536851e7fSLois Curfman McInnes 12969b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 12979b94acceSBarry Smith 1298a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 12999b94acceSBarry Smith @*/ 13007087cfbeSBarry Smith PetscErrorCode SNESComputeFunction(SNES snes,Vec x,Vec y) 13019b94acceSBarry Smith { 1302dfbe8321SBarry Smith PetscErrorCode ierr; 13039b94acceSBarry Smith 13043a40ed3dSBarry Smith PetscFunctionBegin; 13050700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13060700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 13070700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 1308c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1309c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 1310184914b5SBarry Smith 1311d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1312e7788613SBarry Smith if (snes->ops->computefunction) { 1313d64ed03dSBarry Smith PetscStackPush("SNES user function"); 131439d508bbSBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1315d64ed03dSBarry Smith PetscStackPop; 131673250ac0SBarry Smith } else if (snes->dm) { 1317644e2e5bSBarry Smith ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr); 1318c90fad12SPeter Brune } else if (snes->vec_rhs) { 1319c90fad12SPeter Brune ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 1320644e2e5bSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve()."); 132185385478SLisandro Dalcin if (snes->vec_rhs) { 132285385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 13233ab0aad5SBarry Smith } 1324ae3c334cSLois Curfman McInnes snes->nfuncs++; 1325d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 13263a40ed3dSBarry Smith PetscFunctionReturn(0); 13279b94acceSBarry Smith } 13289b94acceSBarry Smith 13294a2ae208SSatish Balay #undef __FUNCT__ 13304a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 133162fef451SLois Curfman McInnes /*@ 133262fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 133362fef451SLois Curfman McInnes set with SNESSetJacobian(). 133462fef451SLois Curfman McInnes 1335c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1336c7afd0dbSLois Curfman McInnes 133762fef451SLois Curfman McInnes Input Parameters: 1338c7afd0dbSLois Curfman McInnes + snes - the SNES context 1339c7afd0dbSLois Curfman McInnes - x - input vector 134062fef451SLois Curfman McInnes 134162fef451SLois Curfman McInnes Output Parameters: 1342c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 134362fef451SLois Curfman McInnes . B - optional preconditioning matrix 13442b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1345fee21e36SBarry Smith 1346e35cf81dSBarry Smith Options Database Keys: 1347e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1348693365a8SJed Brown . -snes_lag_jacobian <lag> 1349693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 1350693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 1351693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 13524c30e9fbSJed Brown . -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 1353c01495d3SJed Brown . -snes_compare_coloring - Compute the finite differece Jacobian using coloring and display norms of difference 1354c01495d3SJed Brown . -snes_compare_coloring_display - Compute the finite differece Jacobian using coloring and display verbose differences 1355c01495d3SJed Brown . -snes_compare_coloring_threshold - Display only those matrix entries that differ by more than a given threshold 1356c01495d3SJed Brown . -snes_compare_coloring_threshold_atol - Absolute tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 1357c01495d3SJed Brown . -snes_compare_coloring_threshold_rtol - Relative tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 13584c30e9fbSJed Brown . -snes_compare_coloring_draw - Compute the finite differece Jacobian using coloring and draw differences 1359c01495d3SJed Brown - -snes_compare_coloring_draw_contour - Compute the finite differece Jacobian using coloring and show contours of matrices and differences 1360c01495d3SJed Brown 1361e35cf81dSBarry Smith 136262fef451SLois Curfman McInnes Notes: 136362fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 136462fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 136562fef451SLois Curfman McInnes 136694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1367dc5a77f8SLois Curfman McInnes flag parameter. 136862fef451SLois Curfman McInnes 136936851e7fSLois Curfman McInnes Level: developer 137036851e7fSLois Curfman McInnes 137162fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 137262fef451SLois Curfman McInnes 1373e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 137462fef451SLois Curfman McInnes @*/ 13757087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 13769b94acceSBarry Smith { 1377dfbe8321SBarry Smith PetscErrorCode ierr; 1378ace3abfcSBarry Smith PetscBool flag; 13793a40ed3dSBarry Smith 13803a40ed3dSBarry Smith PetscFunctionBegin; 13810700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13820700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 13834482741eSBarry Smith PetscValidPointer(flg,5); 1384c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1385e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1386ebd3b9afSBarry Smith 1387ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1388ebd3b9afSBarry Smith 1389fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1390fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1391fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1392fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1393e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1394e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1395ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1396ebd3b9afSBarry Smith if (flag) { 1397ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1398ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1399ebd3b9afSBarry Smith } 1400e35cf81dSBarry Smith PetscFunctionReturn(0); 1401e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1402e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1403e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1404ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1405ebd3b9afSBarry Smith if (flag) { 1406ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1407ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1408ebd3b9afSBarry Smith } 1409e35cf81dSBarry Smith PetscFunctionReturn(0); 1410e35cf81dSBarry Smith } 1411e35cf81dSBarry Smith 1412c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1413e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1414d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1415e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1416d64ed03dSBarry Smith PetscStackPop; 1417d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1418a8054027SBarry Smith 14193b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 14203b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 14213b4f5425SBarry Smith snes->lagpreconditioner = -1; 14223b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1423a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1424a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1425a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1426a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1427a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1428a8054027SBarry Smith } 1429a8054027SBarry Smith 14306d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 14310700a824SBarry Smith /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 14320700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,4); */ 1433693365a8SJed Brown { 1434693365a8SJed Brown PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE; 1435693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr); 1436693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 1437693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 1438693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr); 1439693365a8SJed Brown if (flag || flag_draw || flag_contour) { 1440693365a8SJed Brown Mat Bexp_mine = PETSC_NULL,Bexp,FDexp; 1441693365a8SJed Brown MatStructure mstruct; 1442693365a8SJed Brown PetscViewer vdraw,vstdout; 14436b3a5b13SJed Brown PetscBool flg; 1444693365a8SJed Brown if (flag_operator) { 1445693365a8SJed Brown ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr); 1446693365a8SJed Brown Bexp = Bexp_mine; 1447693365a8SJed Brown } else { 1448693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 1449693365a8SJed Brown ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr); 1450693365a8SJed Brown if (flg) Bexp = *B; 1451693365a8SJed Brown else { 1452693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 1453693365a8SJed Brown ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr); 1454693365a8SJed Brown Bexp = Bexp_mine; 1455693365a8SJed Brown } 1456693365a8SJed Brown } 1457693365a8SJed Brown ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr); 1458693365a8SJed Brown ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr); 1459693365a8SJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 1460693365a8SJed Brown if (flag_draw || flag_contour) { 1461693365a8SJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 1462693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 1463693365a8SJed Brown } else vdraw = PETSC_NULL; 1464693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr); 1465693365a8SJed Brown if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);} 1466693365a8SJed Brown if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);} 1467693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr); 1468693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1469693365a8SJed Brown if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);} 1470693365a8SJed Brown ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 1471693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr); 1472693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1473693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 1474693365a8SJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 1475693365a8SJed Brown ierr = MatView(FDexp,vdraw);CHKERRQ(ierr); 1476693365a8SJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 1477693365a8SJed Brown } 1478693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 1479693365a8SJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 1480693365a8SJed Brown ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr); 1481693365a8SJed Brown ierr = MatDestroy(&FDexp);CHKERRQ(ierr); 1482693365a8SJed Brown } 1483693365a8SJed Brown } 14844c30e9fbSJed Brown { 14856719d8e4SJed Brown PetscBool flag = PETSC_FALSE,flag_display = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_threshold = PETSC_FALSE; 14866719d8e4SJed Brown PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON,threshold_rtol = 10*PETSC_SQRT_MACHINE_EPSILON; 14874c30e9fbSJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring",&flag,PETSC_NULL);CHKERRQ(ierr); 14886719d8e4SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_display",&flag_display,PETSC_NULL);CHKERRQ(ierr); 14894c30e9fbSJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 14904c30e9fbSJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 14916719d8e4SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold",&flag_threshold,PETSC_NULL);CHKERRQ(ierr); 14926719d8e4SJed Brown ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_rtol",&threshold_rtol,PETSC_NULL);CHKERRQ(ierr); 14936719d8e4SJed Brown ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_atol",&threshold_atol,PETSC_NULL);CHKERRQ(ierr); 14946719d8e4SJed Brown if (flag || flag_display || flag_draw || flag_contour || flag_threshold) { 14954c30e9fbSJed Brown Mat Bfd; 14964c30e9fbSJed Brown MatStructure mstruct; 14974c30e9fbSJed Brown PetscViewer vdraw,vstdout; 14984c30e9fbSJed Brown ISColoring iscoloring; 14994c30e9fbSJed Brown MatFDColoring matfdcoloring; 15004c30e9fbSJed Brown PetscErrorCode (*func)(SNES,Vec,Vec,void*); 15014c30e9fbSJed Brown void *funcctx; 15026719d8e4SJed Brown PetscReal norm1,norm2,normmax; 15034c30e9fbSJed Brown 15044c30e9fbSJed Brown ierr = MatDuplicate(*B,MAT_DO_NOT_COPY_VALUES,&Bfd);CHKERRQ(ierr); 15054c30e9fbSJed Brown ierr = MatGetColoring(Bfd,MATCOLORINGSL,&iscoloring);CHKERRQ(ierr); 15064c30e9fbSJed Brown ierr = MatFDColoringCreate(Bfd,iscoloring,&matfdcoloring);CHKERRQ(ierr); 15074c30e9fbSJed Brown ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 15084c30e9fbSJed Brown 15094c30e9fbSJed Brown /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */ 15104c30e9fbSJed Brown ierr = SNESGetFunction(snes,PETSC_NULL,&func,&funcctx);CHKERRQ(ierr); 15114c30e9fbSJed Brown ierr = MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode(*)(void))func,funcctx);CHKERRQ(ierr); 15124c30e9fbSJed Brown ierr = PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring,((PetscObject)snes)->prefix);CHKERRQ(ierr); 15134c30e9fbSJed Brown ierr = PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring,"coloring_");CHKERRQ(ierr); 15144c30e9fbSJed Brown ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr); 15154c30e9fbSJed Brown ierr = MatFDColoringApply(Bfd,matfdcoloring,X,&mstruct,snes);CHKERRQ(ierr); 15164c30e9fbSJed Brown ierr = MatFDColoringDestroy(&matfdcoloring);CHKERRQ(ierr); 15174c30e9fbSJed Brown 15184c30e9fbSJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 15194c30e9fbSJed Brown if (flag_draw || flag_contour) { 15204c30e9fbSJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Colored Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 15214c30e9fbSJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 15224c30e9fbSJed Brown } else vdraw = PETSC_NULL; 15234c30e9fbSJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit preconditioning Jacobian\n");CHKERRQ(ierr); 15246719d8e4SJed Brown if (flag_display) {ierr = MatView(*B,vstdout);CHKERRQ(ierr);} 15254c30e9fbSJed Brown if (vdraw) {ierr = MatView(*B,vdraw);CHKERRQ(ierr);} 15264c30e9fbSJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Colored Finite difference Jacobian\n");CHKERRQ(ierr); 15276719d8e4SJed Brown if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);} 15284c30e9fbSJed Brown if (vdraw) {ierr = MatView(Bfd,vdraw);CHKERRQ(ierr);} 15294c30e9fbSJed Brown ierr = MatAYPX(Bfd,-1.0,*B,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 15304c30e9fbSJed Brown ierr = MatNorm(Bfd,NORM_1,&norm1);CHKERRQ(ierr); 15316719d8e4SJed Brown ierr = MatNorm(Bfd,NORM_FROBENIUS,&norm2);CHKERRQ(ierr); 15324c30e9fbSJed Brown ierr = MatNorm(Bfd,NORM_MAX,&normmax);CHKERRQ(ierr); 15336719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian, norm1=%G normFrob=%G normmax=%G\n",norm1,norm2,normmax);CHKERRQ(ierr); 15346719d8e4SJed Brown if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);} 15354c30e9fbSJed Brown if (vdraw) { /* Always use contour for the difference */ 15364c30e9fbSJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 15374c30e9fbSJed Brown ierr = MatView(Bfd,vdraw);CHKERRQ(ierr); 15384c30e9fbSJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 15394c30e9fbSJed Brown } 15404c30e9fbSJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 15416719d8e4SJed Brown 15426719d8e4SJed Brown if (flag_threshold) { 15436719d8e4SJed Brown PetscInt bs,rstart,rend,i; 15446719d8e4SJed Brown ierr = MatGetBlockSize(*B,&bs);CHKERRQ(ierr); 15456719d8e4SJed Brown ierr = MatGetOwnershipRange(*B,&rstart,&rend);CHKERRQ(ierr); 15466719d8e4SJed Brown for (i=rstart; i<rend; i++) { 15476719d8e4SJed Brown const PetscScalar *ba,*ca; 15486719d8e4SJed Brown const PetscInt *bj,*cj; 15496719d8e4SJed Brown PetscInt bn,cn,j,maxentrycol = -1,maxdiffcol = -1,maxrdiffcol = -1; 15506719d8e4SJed Brown PetscReal maxentry = 0,maxdiff = 0,maxrdiff = 0; 15516719d8e4SJed Brown ierr = MatGetRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr); 15526719d8e4SJed Brown ierr = MatGetRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr); 15536719d8e4SJed Brown if (bn != cn) SETERRQ(((PetscObject)*A)->comm,PETSC_ERR_PLIB,"Unexpected different nonzero pattern in -snes_compare_coloring_threshold"); 15546719d8e4SJed Brown for (j=0; j<bn; j++) { 15556719d8e4SJed Brown PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 15566719d8e4SJed Brown if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) { 15576719d8e4SJed Brown maxentrycol = bj[j]; 15586719d8e4SJed Brown maxentry = PetscRealPart(ba[j]); 15596719d8e4SJed Brown } 15606719d8e4SJed Brown if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) { 15616719d8e4SJed Brown maxdiffcol = bj[j]; 15626719d8e4SJed Brown maxdiff = PetscRealPart(ca[j]); 15636719d8e4SJed Brown } 15646719d8e4SJed Brown if (rdiff > maxrdiff) { 15656719d8e4SJed Brown maxrdiffcol = bj[j]; 15666719d8e4SJed Brown maxrdiff = rdiff; 15676719d8e4SJed Brown } 15686719d8e4SJed Brown } 15696719d8e4SJed Brown if (maxrdiff > 1) { 15706719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"row %D (maxentry=%G at %D, maxdiff=%G at %D, maxrdiff=%G at %D):",i,maxentry,maxentrycol,maxdiff,maxdiffcol,maxrdiff,maxrdiffcol);CHKERRQ(ierr); 15716719d8e4SJed Brown for (j=0; j<bn; j++) { 15726719d8e4SJed Brown PetscReal rdiff; 15736719d8e4SJed Brown rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 15746719d8e4SJed Brown if (rdiff > 1) { 15756719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout," (%D,%G:%G)",bj[j],PetscRealPart(ba[j]),PetscRealPart(ca[j]));CHKERRQ(ierr); 15766719d8e4SJed Brown } 15776719d8e4SJed Brown } 15786719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"\n",i,maxentry,maxdiff,maxrdiff);CHKERRQ(ierr); 15796719d8e4SJed Brown } 15806719d8e4SJed Brown ierr = MatRestoreRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr); 15816719d8e4SJed Brown ierr = MatRestoreRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr); 15826719d8e4SJed Brown } 15836719d8e4SJed Brown } 15844c30e9fbSJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 15854c30e9fbSJed Brown ierr = MatDestroy(&Bfd);CHKERRQ(ierr); 15864c30e9fbSJed Brown } 15874c30e9fbSJed Brown } 15883a40ed3dSBarry Smith PetscFunctionReturn(0); 15899b94acceSBarry Smith } 15909b94acceSBarry Smith 15914a2ae208SSatish Balay #undef __FUNCT__ 15924a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 15939b94acceSBarry Smith /*@C 15949b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1595044dda88SLois Curfman McInnes location to store the matrix. 15969b94acceSBarry Smith 15973f9fe445SBarry Smith Logically Collective on SNES and Mat 1598c7afd0dbSLois Curfman McInnes 15999b94acceSBarry Smith Input Parameters: 1600c7afd0dbSLois Curfman McInnes + snes - the SNES context 16019b94acceSBarry Smith . A - Jacobian matrix 16029b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 1603efd51863SBarry Smith . func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value) 1604c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1605efd51863SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value) 16069b94acceSBarry Smith 16079b94acceSBarry Smith Calling sequence of func: 16088d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 16099b94acceSBarry Smith 1610c7afd0dbSLois Curfman McInnes + x - input vector 16119b94acceSBarry Smith . A - Jacobian matrix 16129b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1613ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 16142b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1615c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 16169b94acceSBarry Smith 16179b94acceSBarry Smith Notes: 161894b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 16192cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1620ac21db08SLois Curfman McInnes 1621ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 16229b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 16239b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 16249b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 16259b94acceSBarry Smith throughout the global iterations. 16269b94acceSBarry Smith 162716913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 162816913363SBarry Smith each matrix. 162916913363SBarry Smith 1630a8a26c1eSJed Brown If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument 1631a8a26c1eSJed Brown must be a MatFDColoring. 1632a8a26c1eSJed Brown 1633c3cc8fd1SJed Brown Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian. One common 1634c3cc8fd1SJed Brown example is to use the "Picard linearization" which only differentiates through the highest order parts of each term. 1635c3cc8fd1SJed Brown 163636851e7fSLois Curfman McInnes Level: beginner 163736851e7fSLois Curfman McInnes 16389b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 16399b94acceSBarry Smith 16403ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 16419b94acceSBarry Smith @*/ 16427087cfbeSBarry Smith PetscErrorCode SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 16439b94acceSBarry Smith { 1644dfbe8321SBarry Smith PetscErrorCode ierr; 16453a7fca6bSBarry Smith 16463a40ed3dSBarry Smith PetscFunctionBegin; 16470700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 16480700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 16490700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1650c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 165106975374SJed Brown if (B) PetscCheckSameComm(snes,1,B,3); 1652e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 16533a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 16543a7fca6bSBarry Smith if (A) { 16557dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 16566bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 16579b94acceSBarry Smith snes->jacobian = A; 16583a7fca6bSBarry Smith } 16593a7fca6bSBarry Smith if (B) { 16607dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 16616bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 16629b94acceSBarry Smith snes->jacobian_pre = B; 16633a7fca6bSBarry Smith } 16643a40ed3dSBarry Smith PetscFunctionReturn(0); 16659b94acceSBarry Smith } 166662fef451SLois Curfman McInnes 16674a2ae208SSatish Balay #undef __FUNCT__ 16684a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1669c2aafc4cSSatish Balay /*@C 1670b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1671b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1672b4fd4287SBarry Smith 1673c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1674c7afd0dbSLois Curfman McInnes 1675b4fd4287SBarry Smith Input Parameter: 1676b4fd4287SBarry Smith . snes - the nonlinear solver context 1677b4fd4287SBarry Smith 1678b4fd4287SBarry Smith Output Parameters: 1679c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1680b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 168170e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 168270e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1683fee21e36SBarry Smith 168436851e7fSLois Curfman McInnes Level: advanced 168536851e7fSLois Curfman McInnes 1686b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1687b4fd4287SBarry Smith @*/ 16887087cfbeSBarry Smith PetscErrorCode SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1689b4fd4287SBarry Smith { 16903a40ed3dSBarry Smith PetscFunctionBegin; 16910700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1692b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1693b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1694e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 169570e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 16963a40ed3dSBarry Smith PetscFunctionReturn(0); 1697b4fd4287SBarry Smith } 1698b4fd4287SBarry Smith 16999b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 17009b94acceSBarry Smith 17014a2ae208SSatish Balay #undef __FUNCT__ 17024a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 17039b94acceSBarry Smith /*@ 17049b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1705272ac6f2SLois Curfman McInnes of a nonlinear solver. 17069b94acceSBarry Smith 1707fee21e36SBarry Smith Collective on SNES 1708fee21e36SBarry Smith 1709c7afd0dbSLois Curfman McInnes Input Parameters: 171070e92668SMatthew Knepley . snes - the SNES context 1711c7afd0dbSLois Curfman McInnes 1712272ac6f2SLois Curfman McInnes Notes: 1713272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1714272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1715272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1716272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1717272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1718272ac6f2SLois Curfman McInnes 171936851e7fSLois Curfman McInnes Level: advanced 172036851e7fSLois Curfman McInnes 17219b94acceSBarry Smith .keywords: SNES, nonlinear, setup 17229b94acceSBarry Smith 17239b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 17249b94acceSBarry Smith @*/ 17257087cfbeSBarry Smith PetscErrorCode SNESSetUp(SNES snes) 17269b94acceSBarry Smith { 1727dfbe8321SBarry Smith PetscErrorCode ierr; 17283a40ed3dSBarry Smith 17293a40ed3dSBarry Smith PetscFunctionBegin; 17300700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 17314dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 17329b94acceSBarry Smith 17337adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 173485385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 173585385478SLisandro Dalcin } 173685385478SLisandro Dalcin 1737c9b9fda1SJed Brown if (!snes->vec_func) { 1738c9b9fda1SJed Brown if (snes->vec_rhs) { 1739c9b9fda1SJed Brown ierr = VecDuplicate(snes->vec_rhs,&snes->vec_func);CHKERRQ(ierr); 1740c9b9fda1SJed Brown } else if (snes->vec_sol) { 1741c9b9fda1SJed Brown ierr = VecDuplicate(snes->vec_sol,&snes->vec_func);CHKERRQ(ierr); 1742c9b9fda1SJed Brown } else if (snes->dm) { 1743efd51863SBarry Smith ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1744efd51863SBarry Smith } 1745c9b9fda1SJed Brown } 174617186662SBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 174758c9b817SLisandro Dalcin if (snes->vec_rhs == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector"); 174858c9b817SLisandro Dalcin 174958c9b817SLisandro Dalcin if (!snes->vec_sol_update /* && snes->vec_sol */) { 175058c9b817SLisandro Dalcin ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr); 175158c9b817SLisandro Dalcin ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr); 175258c9b817SLisandro Dalcin } 175358c9b817SLisandro Dalcin 1754ef8dffc7SBarry Smith if (!snes->ops->computejacobian && snes->dm) { 1755ef8dffc7SBarry Smith Mat J; 1756ef8dffc7SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 1757cab2e9ccSBarry Smith ierr = SNESSetJacobian(snes,J,J,SNESDMComputeJacobian,PETSC_NULL);CHKERRQ(ierr); 1758cab2e9ccSBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 17592ef29e96SBarry Smith } else if (!snes->jacobian && snes->ops->computejacobian == MatMFFDComputeJacobian) { 1760a8248277SBarry Smith Mat J; 1761a8248277SBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 1762a8248277SBarry Smith ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 1763a8248277SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 1764a8248277SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 1765a8248277SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1766a8248277SBarry Smith } else if (snes->dm && snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) { 1767a8248277SBarry Smith Mat J,B; 1768a8248277SBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 1769a8248277SBarry Smith ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 1770a8248277SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 1771a8248277SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr); 1772a8248277SBarry Smith ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,snes->funP);CHKERRQ(ierr); 1773a8248277SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1774a8248277SBarry Smith ierr = MatDestroy(&B);CHKERRQ(ierr); 1775efd51863SBarry Smith } else if (snes->dm && !snes->jacobian_pre){ 1776efd51863SBarry Smith Mat J; 1777efd51863SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 17783cbb28f5SBarry Smith ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 1779efd51863SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1780ef8dffc7SBarry Smith } 1781cfaf3a74SBarry 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()"); 1782c9b9fda1SJed Brown if (!snes->vec_func) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a vector when calling SNESSetFunction() or call SNESSetDM()"); 1783efd51863SBarry Smith 1784b710008aSBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);} 1785b710008aSBarry Smith 1786d25893d9SBarry Smith if (snes->ops->usercompute && !snes->user) { 1787d25893d9SBarry Smith ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr); 1788d25893d9SBarry Smith } 1789d25893d9SBarry Smith 1790410397dcSLisandro Dalcin if (snes->ops->setup) { 1791410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1792410397dcSLisandro Dalcin } 179358c9b817SLisandro Dalcin 17947aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 17953a40ed3dSBarry Smith PetscFunctionReturn(0); 17969b94acceSBarry Smith } 17979b94acceSBarry Smith 17984a2ae208SSatish Balay #undef __FUNCT__ 179937596af1SLisandro Dalcin #define __FUNCT__ "SNESReset" 180037596af1SLisandro Dalcin /*@ 180137596af1SLisandro Dalcin SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats 180237596af1SLisandro Dalcin 180337596af1SLisandro Dalcin Collective on SNES 180437596af1SLisandro Dalcin 180537596af1SLisandro Dalcin Input Parameter: 180637596af1SLisandro Dalcin . snes - iterative context obtained from SNESCreate() 180737596af1SLisandro Dalcin 1808d25893d9SBarry Smith Level: intermediate 1809d25893d9SBarry Smith 1810d25893d9SBarry Smith Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext() 181137596af1SLisandro Dalcin 181237596af1SLisandro Dalcin .keywords: SNES, destroy 181337596af1SLisandro Dalcin 181437596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 181537596af1SLisandro Dalcin @*/ 181637596af1SLisandro Dalcin PetscErrorCode SNESReset(SNES snes) 181737596af1SLisandro Dalcin { 181837596af1SLisandro Dalcin PetscErrorCode ierr; 181937596af1SLisandro Dalcin 182037596af1SLisandro Dalcin PetscFunctionBegin; 182137596af1SLisandro Dalcin PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1822d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 1823d25893d9SBarry Smith ierr = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr); 1824d25893d9SBarry Smith snes->user = PETSC_NULL; 1825d25893d9SBarry Smith } 18268a23116dSBarry Smith if (snes->pc) { 18278a23116dSBarry Smith ierr = SNESReset(snes->pc);CHKERRQ(ierr); 18288a23116dSBarry Smith } 18298a23116dSBarry Smith 183037596af1SLisandro Dalcin if (snes->ops->reset) { 183137596af1SLisandro Dalcin ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr); 183237596af1SLisandro Dalcin } 183337596af1SLisandro Dalcin if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);} 18346bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 18356bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 18366bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr); 18376bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 18386bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 18396bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 184037596af1SLisandro Dalcin if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);} 184137596af1SLisandro Dalcin if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);} 184237596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 184337596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 184437596af1SLisandro Dalcin PetscFunctionReturn(0); 184537596af1SLisandro Dalcin } 184637596af1SLisandro Dalcin 184737596af1SLisandro Dalcin #undef __FUNCT__ 18484a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 184952baeb72SSatish Balay /*@ 18509b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 18519b94acceSBarry Smith with SNESCreate(). 18529b94acceSBarry Smith 1853c7afd0dbSLois Curfman McInnes Collective on SNES 1854c7afd0dbSLois Curfman McInnes 18559b94acceSBarry Smith Input Parameter: 18569b94acceSBarry Smith . snes - the SNES context 18579b94acceSBarry Smith 185836851e7fSLois Curfman McInnes Level: beginner 185936851e7fSLois Curfman McInnes 18609b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 18619b94acceSBarry Smith 186263a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 18639b94acceSBarry Smith @*/ 18646bf464f9SBarry Smith PetscErrorCode SNESDestroy(SNES *snes) 18659b94acceSBarry Smith { 18666849ba73SBarry Smith PetscErrorCode ierr; 18673a40ed3dSBarry Smith 18683a40ed3dSBarry Smith PetscFunctionBegin; 18696bf464f9SBarry Smith if (!*snes) PetscFunctionReturn(0); 18706bf464f9SBarry Smith PetscValidHeaderSpecific((*snes),SNES_CLASSID,1); 18716bf464f9SBarry Smith if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);} 1872d4bb536fSBarry Smith 18736bf464f9SBarry Smith ierr = SNESReset((*snes));CHKERRQ(ierr); 18748a23116dSBarry Smith ierr = SNESDestroy(&(*snes)->pc);CHKERRQ(ierr); 18756b8b9a38SLisandro Dalcin 1876be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 18776bf464f9SBarry Smith ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr); 18786bf464f9SBarry Smith if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);} 18796d4c513bSLisandro Dalcin 18806bf464f9SBarry Smith ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr); 18816bf464f9SBarry Smith ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr); 18826b8b9a38SLisandro Dalcin 18836bf464f9SBarry Smith ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr); 18846bf464f9SBarry Smith if ((*snes)->ops->convergeddestroy) { 18856bf464f9SBarry Smith ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr); 18866b8b9a38SLisandro Dalcin } 18876bf464f9SBarry Smith if ((*snes)->conv_malloc) { 18886bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr); 18896bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr); 189058c9b817SLisandro Dalcin } 1891ea630c6eSPeter Brune ierr = PetscViewerDestroy(&(*snes)->ls_monitor);CHKERRQ(ierr); 18926bf464f9SBarry Smith ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr); 1893a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 18943a40ed3dSBarry Smith PetscFunctionReturn(0); 18959b94acceSBarry Smith } 18969b94acceSBarry Smith 18979b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 18989b94acceSBarry Smith 18994a2ae208SSatish Balay #undef __FUNCT__ 1900a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1901a8054027SBarry Smith /*@ 1902a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1903a8054027SBarry Smith 19043f9fe445SBarry Smith Logically Collective on SNES 1905a8054027SBarry Smith 1906a8054027SBarry Smith Input Parameters: 1907a8054027SBarry Smith + snes - the SNES context 1908a8054027SBarry 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 19093b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1910a8054027SBarry Smith 1911a8054027SBarry Smith Options Database Keys: 1912a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1913a8054027SBarry Smith 1914a8054027SBarry Smith Notes: 1915a8054027SBarry Smith The default is 1 1916a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1917a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1918a8054027SBarry Smith 1919a8054027SBarry Smith Level: intermediate 1920a8054027SBarry Smith 1921a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1922a8054027SBarry Smith 1923e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1924a8054027SBarry Smith 1925a8054027SBarry Smith @*/ 19267087cfbeSBarry Smith PetscErrorCode SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1927a8054027SBarry Smith { 1928a8054027SBarry Smith PetscFunctionBegin; 19290700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1930e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1931e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1932c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 1933a8054027SBarry Smith snes->lagpreconditioner = lag; 1934a8054027SBarry Smith PetscFunctionReturn(0); 1935a8054027SBarry Smith } 1936a8054027SBarry Smith 1937a8054027SBarry Smith #undef __FUNCT__ 1938efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence" 1939efd51863SBarry Smith /*@ 1940efd51863SBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does 1941efd51863SBarry Smith 1942efd51863SBarry Smith Logically Collective on SNES 1943efd51863SBarry Smith 1944efd51863SBarry Smith Input Parameters: 1945efd51863SBarry Smith + snes - the SNES context 1946efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 1947efd51863SBarry Smith 1948efd51863SBarry Smith Options Database Keys: 1949efd51863SBarry Smith . -snes_grid_sequence <steps> 1950efd51863SBarry Smith 1951efd51863SBarry Smith Level: intermediate 1952efd51863SBarry Smith 1953*c0df2a02SJed Brown Notes: 1954*c0df2a02SJed Brown Use SNESGetSolution() to extract the fine grid solution after grid sequencing. 1955*c0df2a02SJed Brown 1956efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1957efd51863SBarry Smith 1958efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1959efd51863SBarry Smith 1960efd51863SBarry Smith @*/ 1961efd51863SBarry Smith PetscErrorCode SNESSetGridSequence(SNES snes,PetscInt steps) 1962efd51863SBarry Smith { 1963efd51863SBarry Smith PetscFunctionBegin; 1964efd51863SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1965efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes,steps,2); 1966efd51863SBarry Smith snes->gridsequence = steps; 1967efd51863SBarry Smith PetscFunctionReturn(0); 1968efd51863SBarry Smith } 1969efd51863SBarry Smith 1970efd51863SBarry Smith #undef __FUNCT__ 1971a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1972a8054027SBarry Smith /*@ 1973a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1974a8054027SBarry Smith 19753f9fe445SBarry Smith Not Collective 1976a8054027SBarry Smith 1977a8054027SBarry Smith Input Parameter: 1978a8054027SBarry Smith . snes - the SNES context 1979a8054027SBarry Smith 1980a8054027SBarry Smith Output Parameter: 1981a8054027SBarry 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 19823b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1983a8054027SBarry Smith 1984a8054027SBarry Smith Options Database Keys: 1985a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1986a8054027SBarry Smith 1987a8054027SBarry Smith Notes: 1988a8054027SBarry Smith The default is 1 1989a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1990a8054027SBarry Smith 1991a8054027SBarry Smith Level: intermediate 1992a8054027SBarry Smith 1993a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1994a8054027SBarry Smith 1995a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1996a8054027SBarry Smith 1997a8054027SBarry Smith @*/ 19987087cfbeSBarry Smith PetscErrorCode SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1999a8054027SBarry Smith { 2000a8054027SBarry Smith PetscFunctionBegin; 20010700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2002a8054027SBarry Smith *lag = snes->lagpreconditioner; 2003a8054027SBarry Smith PetscFunctionReturn(0); 2004a8054027SBarry Smith } 2005a8054027SBarry Smith 2006a8054027SBarry Smith #undef __FUNCT__ 2007e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 2008e35cf81dSBarry Smith /*@ 2009e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 2010e35cf81dSBarry Smith often the preconditioner is rebuilt. 2011e35cf81dSBarry Smith 20123f9fe445SBarry Smith Logically Collective on SNES 2013e35cf81dSBarry Smith 2014e35cf81dSBarry Smith Input Parameters: 2015e35cf81dSBarry Smith + snes - the SNES context 2016e35cf81dSBarry 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 2017fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 2018e35cf81dSBarry Smith 2019e35cf81dSBarry Smith Options Database Keys: 2020e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 2021e35cf81dSBarry Smith 2022e35cf81dSBarry Smith Notes: 2023e35cf81dSBarry Smith The default is 1 2024e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2025fe3ffe1eSBarry 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 2026fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 2027e35cf81dSBarry Smith 2028e35cf81dSBarry Smith Level: intermediate 2029e35cf81dSBarry Smith 2030e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2031e35cf81dSBarry Smith 2032e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 2033e35cf81dSBarry Smith 2034e35cf81dSBarry Smith @*/ 20357087cfbeSBarry Smith PetscErrorCode SNESSetLagJacobian(SNES snes,PetscInt lag) 2036e35cf81dSBarry Smith { 2037e35cf81dSBarry Smith PetscFunctionBegin; 20380700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2039e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 2040e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 2041c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 2042e35cf81dSBarry Smith snes->lagjacobian = lag; 2043e35cf81dSBarry Smith PetscFunctionReturn(0); 2044e35cf81dSBarry Smith } 2045e35cf81dSBarry Smith 2046e35cf81dSBarry Smith #undef __FUNCT__ 2047e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 2048e35cf81dSBarry Smith /*@ 2049e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 2050e35cf81dSBarry Smith 20513f9fe445SBarry Smith Not Collective 2052e35cf81dSBarry Smith 2053e35cf81dSBarry Smith Input Parameter: 2054e35cf81dSBarry Smith . snes - the SNES context 2055e35cf81dSBarry Smith 2056e35cf81dSBarry Smith Output Parameter: 2057e35cf81dSBarry 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 2058e35cf81dSBarry Smith the Jacobian is built etc. 2059e35cf81dSBarry Smith 2060e35cf81dSBarry Smith Options Database Keys: 2061e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 2062e35cf81dSBarry Smith 2063e35cf81dSBarry Smith Notes: 2064e35cf81dSBarry Smith The default is 1 2065e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2066e35cf81dSBarry Smith 2067e35cf81dSBarry Smith Level: intermediate 2068e35cf81dSBarry Smith 2069e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2070e35cf81dSBarry Smith 2071e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 2072e35cf81dSBarry Smith 2073e35cf81dSBarry Smith @*/ 20747087cfbeSBarry Smith PetscErrorCode SNESGetLagJacobian(SNES snes,PetscInt *lag) 2075e35cf81dSBarry Smith { 2076e35cf81dSBarry Smith PetscFunctionBegin; 20770700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2078e35cf81dSBarry Smith *lag = snes->lagjacobian; 2079e35cf81dSBarry Smith PetscFunctionReturn(0); 2080e35cf81dSBarry Smith } 2081e35cf81dSBarry Smith 2082e35cf81dSBarry Smith #undef __FUNCT__ 20834a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 20849b94acceSBarry Smith /*@ 2085d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 20869b94acceSBarry Smith 20873f9fe445SBarry Smith Logically Collective on SNES 2088c7afd0dbSLois Curfman McInnes 20899b94acceSBarry Smith Input Parameters: 2090c7afd0dbSLois Curfman McInnes + snes - the SNES context 209170441072SBarry Smith . abstol - absolute convergence tolerance 209233174efeSLois Curfman McInnes . rtol - relative convergence tolerance 209333174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 209433174efeSLois Curfman McInnes of the change in the solution between steps 209533174efeSLois Curfman McInnes . maxit - maximum number of iterations 2096c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 2097fee21e36SBarry Smith 209833174efeSLois Curfman McInnes Options Database Keys: 209970441072SBarry Smith + -snes_atol <abstol> - Sets abstol 2100c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 2101c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 2102c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 2103c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 21049b94acceSBarry Smith 2105d7a720efSLois Curfman McInnes Notes: 21069b94acceSBarry Smith The default maximum number of iterations is 50. 21079b94acceSBarry Smith The default maximum number of function evaluations is 1000. 21089b94acceSBarry Smith 210936851e7fSLois Curfman McInnes Level: intermediate 211036851e7fSLois Curfman McInnes 211133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 21129b94acceSBarry Smith 21132492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 21149b94acceSBarry Smith @*/ 21157087cfbeSBarry Smith PetscErrorCode SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 21169b94acceSBarry Smith { 21173a40ed3dSBarry Smith PetscFunctionBegin; 21180700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2119c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,abstol,2); 2120c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol,3); 2121c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,stol,4); 2122c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxit,5); 2123c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxf,6); 2124c5eb9154SBarry Smith 2125ab54825eSJed Brown if (abstol != PETSC_DEFAULT) { 2126ab54825eSJed Brown if (abstol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %G must be non-negative",abstol); 2127ab54825eSJed Brown snes->abstol = abstol; 2128ab54825eSJed Brown } 2129ab54825eSJed Brown if (rtol != PETSC_DEFAULT) { 2130ab54825eSJed 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); 2131ab54825eSJed Brown snes->rtol = rtol; 2132ab54825eSJed Brown } 2133ab54825eSJed Brown if (stol != PETSC_DEFAULT) { 2134ab54825eSJed Brown if (stol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %G must be non-negative",stol); 2135ab54825eSJed Brown snes->xtol = stol; 2136ab54825eSJed Brown } 2137ab54825eSJed Brown if (maxit != PETSC_DEFAULT) { 2138ab54825eSJed Brown if (maxit < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit); 2139ab54825eSJed Brown snes->max_its = maxit; 2140ab54825eSJed Brown } 2141ab54825eSJed Brown if (maxf != PETSC_DEFAULT) { 2142ab54825eSJed Brown if (maxf < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be non-negative",maxf); 2143ab54825eSJed Brown snes->max_funcs = maxf; 2144ab54825eSJed Brown } 21453a40ed3dSBarry Smith PetscFunctionReturn(0); 21469b94acceSBarry Smith } 21479b94acceSBarry Smith 21484a2ae208SSatish Balay #undef __FUNCT__ 21494a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 21509b94acceSBarry Smith /*@ 215133174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 215233174efeSLois Curfman McInnes 2153c7afd0dbSLois Curfman McInnes Not Collective 2154c7afd0dbSLois Curfman McInnes 215533174efeSLois Curfman McInnes Input Parameters: 2156c7afd0dbSLois Curfman McInnes + snes - the SNES context 215785385478SLisandro Dalcin . atol - absolute convergence tolerance 215833174efeSLois Curfman McInnes . rtol - relative convergence tolerance 215933174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 216033174efeSLois Curfman McInnes of the change in the solution between steps 216133174efeSLois Curfman McInnes . maxit - maximum number of iterations 2162c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 2163fee21e36SBarry Smith 216433174efeSLois Curfman McInnes Notes: 216533174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 216633174efeSLois Curfman McInnes 216736851e7fSLois Curfman McInnes Level: intermediate 216836851e7fSLois Curfman McInnes 216933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 217033174efeSLois Curfman McInnes 217133174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 217233174efeSLois Curfman McInnes @*/ 21737087cfbeSBarry Smith PetscErrorCode SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 217433174efeSLois Curfman McInnes { 21753a40ed3dSBarry Smith PetscFunctionBegin; 21760700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 217785385478SLisandro Dalcin if (atol) *atol = snes->abstol; 217833174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 217933174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 218033174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 218133174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 21823a40ed3dSBarry Smith PetscFunctionReturn(0); 218333174efeSLois Curfman McInnes } 218433174efeSLois Curfman McInnes 21854a2ae208SSatish Balay #undef __FUNCT__ 21864a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 218733174efeSLois Curfman McInnes /*@ 21889b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 21899b94acceSBarry Smith 21903f9fe445SBarry Smith Logically Collective on SNES 2191fee21e36SBarry Smith 2192c7afd0dbSLois Curfman McInnes Input Parameters: 2193c7afd0dbSLois Curfman McInnes + snes - the SNES context 2194c7afd0dbSLois Curfman McInnes - tol - tolerance 2195c7afd0dbSLois Curfman McInnes 21969b94acceSBarry Smith Options Database Key: 2197c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 21989b94acceSBarry Smith 219936851e7fSLois Curfman McInnes Level: intermediate 220036851e7fSLois Curfman McInnes 22019b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 22029b94acceSBarry Smith 22032492ecdbSBarry Smith .seealso: SNESSetTolerances() 22049b94acceSBarry Smith @*/ 22057087cfbeSBarry Smith PetscErrorCode SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 22069b94acceSBarry Smith { 22073a40ed3dSBarry Smith PetscFunctionBegin; 22080700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2209c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,tol,2); 22109b94acceSBarry Smith snes->deltatol = tol; 22113a40ed3dSBarry Smith PetscFunctionReturn(0); 22129b94acceSBarry Smith } 22139b94acceSBarry Smith 2214df9fa365SBarry Smith /* 2215df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 2216df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 2217df9fa365SBarry Smith macros instead of functions 2218df9fa365SBarry Smith */ 22194a2ae208SSatish Balay #undef __FUNCT__ 2220a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 22217087cfbeSBarry Smith PetscErrorCode SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 2222ce1608b8SBarry Smith { 2223dfbe8321SBarry Smith PetscErrorCode ierr; 2224ce1608b8SBarry Smith 2225ce1608b8SBarry Smith PetscFunctionBegin; 22260700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2227a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 2228ce1608b8SBarry Smith PetscFunctionReturn(0); 2229ce1608b8SBarry Smith } 2230ce1608b8SBarry Smith 22314a2ae208SSatish Balay #undef __FUNCT__ 2232a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 22337087cfbeSBarry Smith PetscErrorCode SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2234df9fa365SBarry Smith { 2235dfbe8321SBarry Smith PetscErrorCode ierr; 2236df9fa365SBarry Smith 2237df9fa365SBarry Smith PetscFunctionBegin; 2238a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2239df9fa365SBarry Smith PetscFunctionReturn(0); 2240df9fa365SBarry Smith } 2241df9fa365SBarry Smith 22424a2ae208SSatish Balay #undef __FUNCT__ 2243a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 22446bf464f9SBarry Smith PetscErrorCode SNESMonitorLGDestroy(PetscDrawLG *draw) 2245df9fa365SBarry Smith { 2246dfbe8321SBarry Smith PetscErrorCode ierr; 2247df9fa365SBarry Smith 2248df9fa365SBarry Smith PetscFunctionBegin; 2249a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2250df9fa365SBarry Smith PetscFunctionReturn(0); 2251df9fa365SBarry Smith } 2252df9fa365SBarry Smith 22537087cfbeSBarry Smith extern PetscErrorCode SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 2254b271bb04SBarry Smith #undef __FUNCT__ 2255b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 22567087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 2257b271bb04SBarry Smith { 2258b271bb04SBarry Smith PetscDrawLG lg; 2259b271bb04SBarry Smith PetscErrorCode ierr; 2260b271bb04SBarry Smith PetscReal x,y,per; 2261b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 2262b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 2263b271bb04SBarry Smith PetscDraw draw; 2264b271bb04SBarry Smith PetscFunctionBegin; 2265b271bb04SBarry Smith if (!monctx) { 2266b271bb04SBarry Smith MPI_Comm comm; 2267b271bb04SBarry Smith 2268b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 2269b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 2270b271bb04SBarry Smith } 2271b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 2272b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2273b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2274b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 2275b271bb04SBarry Smith x = (PetscReal) n; 2276b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 2277b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2278b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2279b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2280b271bb04SBarry Smith } 2281b271bb04SBarry Smith 2282b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 2283b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2284b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2285b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 2286b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 2287b271bb04SBarry Smith x = (PetscReal) n; 2288b271bb04SBarry Smith y = 100.0*per; 2289b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2290b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2291b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2292b271bb04SBarry Smith } 2293b271bb04SBarry Smith 2294b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 2295b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2296b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2297b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 2298b271bb04SBarry Smith x = (PetscReal) n; 2299b271bb04SBarry Smith y = (prev - rnorm)/prev; 2300b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2301b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2302b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2303b271bb04SBarry Smith } 2304b271bb04SBarry Smith 2305b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 2306b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2307b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2308b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 2309b271bb04SBarry Smith x = (PetscReal) n; 2310b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 2311b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 2312b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2313b271bb04SBarry Smith } 2314b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2315b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2316b271bb04SBarry Smith } 2317b271bb04SBarry Smith prev = rnorm; 2318b271bb04SBarry Smith PetscFunctionReturn(0); 2319b271bb04SBarry Smith } 2320b271bb04SBarry Smith 2321b271bb04SBarry Smith #undef __FUNCT__ 2322b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 23237087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2324b271bb04SBarry Smith { 2325b271bb04SBarry Smith PetscErrorCode ierr; 2326b271bb04SBarry Smith 2327b271bb04SBarry Smith PetscFunctionBegin; 2328b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2329b271bb04SBarry Smith PetscFunctionReturn(0); 2330b271bb04SBarry Smith } 2331b271bb04SBarry Smith 2332b271bb04SBarry Smith #undef __FUNCT__ 2333b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 23346bf464f9SBarry Smith PetscErrorCode SNESMonitorLGRangeDestroy(PetscDrawLG *draw) 2335b271bb04SBarry Smith { 2336b271bb04SBarry Smith PetscErrorCode ierr; 2337b271bb04SBarry Smith 2338b271bb04SBarry Smith PetscFunctionBegin; 2339b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2340b271bb04SBarry Smith PetscFunctionReturn(0); 2341b271bb04SBarry Smith } 2342b271bb04SBarry Smith 23437a03ce2fSLisandro Dalcin #undef __FUNCT__ 23447a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor" 2345228d79bcSJed Brown /*@ 2346228d79bcSJed Brown SNESMonitor - runs the user provided monitor routines, if they exist 2347228d79bcSJed Brown 2348228d79bcSJed Brown Collective on SNES 2349228d79bcSJed Brown 2350228d79bcSJed Brown Input Parameters: 2351228d79bcSJed Brown + snes - nonlinear solver context obtained from SNESCreate() 2352228d79bcSJed Brown . iter - iteration number 2353228d79bcSJed Brown - rnorm - relative norm of the residual 2354228d79bcSJed Brown 2355228d79bcSJed Brown Notes: 2356228d79bcSJed Brown This routine is called by the SNES implementations. 2357228d79bcSJed Brown It does not typically need to be called by the user. 2358228d79bcSJed Brown 2359228d79bcSJed Brown Level: developer 2360228d79bcSJed Brown 2361228d79bcSJed Brown .seealso: SNESMonitorSet() 2362228d79bcSJed Brown @*/ 23637a03ce2fSLisandro Dalcin PetscErrorCode SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm) 23647a03ce2fSLisandro Dalcin { 23657a03ce2fSLisandro Dalcin PetscErrorCode ierr; 23667a03ce2fSLisandro Dalcin PetscInt i,n = snes->numbermonitors; 23677a03ce2fSLisandro Dalcin 23687a03ce2fSLisandro Dalcin PetscFunctionBegin; 23697a03ce2fSLisandro Dalcin for (i=0; i<n; i++) { 23707a03ce2fSLisandro Dalcin ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr); 23717a03ce2fSLisandro Dalcin } 23727a03ce2fSLisandro Dalcin PetscFunctionReturn(0); 23737a03ce2fSLisandro Dalcin } 23747a03ce2fSLisandro Dalcin 23759b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 23769b94acceSBarry Smith 23774a2ae208SSatish Balay #undef __FUNCT__ 2378a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 23799b94acceSBarry Smith /*@C 2380a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 23819b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 23829b94acceSBarry Smith progress. 23839b94acceSBarry Smith 23843f9fe445SBarry Smith Logically Collective on SNES 2385fee21e36SBarry Smith 2386c7afd0dbSLois Curfman McInnes Input Parameters: 2387c7afd0dbSLois Curfman McInnes + snes - the SNES context 2388c7afd0dbSLois Curfman McInnes . func - monitoring routine 2389b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 2390e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 2391b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 2392b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 23939b94acceSBarry Smith 2394c7afd0dbSLois Curfman McInnes Calling sequence of func: 2395a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 2396c7afd0dbSLois Curfman McInnes 2397c7afd0dbSLois Curfman McInnes + snes - the SNES context 2398c7afd0dbSLois Curfman McInnes . its - iteration number 2399c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 240040a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 24019b94acceSBarry Smith 24029665c990SLois Curfman McInnes Options Database Keys: 2403a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 2404a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 2405a6570f20SBarry Smith uses SNESMonitorLGCreate() 2406cca6129bSJed Brown - -snes_monitor_cancel - cancels all monitors that have 2407c7afd0dbSLois Curfman McInnes been hardwired into a code by 2408a6570f20SBarry Smith calls to SNESMonitorSet(), but 2409c7afd0dbSLois Curfman McInnes does not cancel those set via 2410c7afd0dbSLois Curfman McInnes the options database. 24119665c990SLois Curfman McInnes 2412639f9d9dSBarry Smith Notes: 24136bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 2414a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 24156bc08f3fSLois Curfman McInnes order in which they were set. 2416639f9d9dSBarry Smith 2417025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 2418025f1a04SBarry Smith 241936851e7fSLois Curfman McInnes Level: intermediate 242036851e7fSLois Curfman McInnes 24219b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 24229b94acceSBarry Smith 2423a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 24249b94acceSBarry Smith @*/ 2425c2efdce3SBarry Smith PetscErrorCode SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 24269b94acceSBarry Smith { 2427b90d0a6eSBarry Smith PetscInt i; 2428649052a6SBarry Smith PetscErrorCode ierr; 2429b90d0a6eSBarry Smith 24303a40ed3dSBarry Smith PetscFunctionBegin; 24310700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 243217186662SBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2433b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 2434649052a6SBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) { 2435649052a6SBarry Smith if (monitordestroy) { 2436c2efdce3SBarry Smith ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr); 2437649052a6SBarry Smith } 2438b90d0a6eSBarry Smith PetscFunctionReturn(0); 2439b90d0a6eSBarry Smith } 2440b90d0a6eSBarry Smith } 2441b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 2442b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 2443639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 24443a40ed3dSBarry Smith PetscFunctionReturn(0); 24459b94acceSBarry Smith } 24469b94acceSBarry Smith 24474a2ae208SSatish Balay #undef __FUNCT__ 2448a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 24495cd90555SBarry Smith /*@C 2450a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 24515cd90555SBarry Smith 24523f9fe445SBarry Smith Logically Collective on SNES 2453c7afd0dbSLois Curfman McInnes 24545cd90555SBarry Smith Input Parameters: 24555cd90555SBarry Smith . snes - the SNES context 24565cd90555SBarry Smith 24571a480d89SAdministrator Options Database Key: 2458a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 2459a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 2460c7afd0dbSLois Curfman McInnes set via the options database 24615cd90555SBarry Smith 24625cd90555SBarry Smith Notes: 24635cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 24645cd90555SBarry Smith 246536851e7fSLois Curfman McInnes Level: intermediate 246636851e7fSLois Curfman McInnes 24675cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 24685cd90555SBarry Smith 2469a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 24705cd90555SBarry Smith @*/ 24717087cfbeSBarry Smith PetscErrorCode SNESMonitorCancel(SNES snes) 24725cd90555SBarry Smith { 2473d952e501SBarry Smith PetscErrorCode ierr; 2474d952e501SBarry Smith PetscInt i; 2475d952e501SBarry Smith 24765cd90555SBarry Smith PetscFunctionBegin; 24770700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2478d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 2479d952e501SBarry Smith if (snes->monitordestroy[i]) { 24803c4aec1bSBarry Smith ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr); 2481d952e501SBarry Smith } 2482d952e501SBarry Smith } 24835cd90555SBarry Smith snes->numbermonitors = 0; 24845cd90555SBarry Smith PetscFunctionReturn(0); 24855cd90555SBarry Smith } 24865cd90555SBarry Smith 24874a2ae208SSatish Balay #undef __FUNCT__ 24884a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 24899b94acceSBarry Smith /*@C 24909b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 24919b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 24929b94acceSBarry Smith 24933f9fe445SBarry Smith Logically Collective on SNES 2494fee21e36SBarry Smith 2495c7afd0dbSLois Curfman McInnes Input Parameters: 2496c7afd0dbSLois Curfman McInnes + snes - the SNES context 2497c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 24987f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 24997f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 25009b94acceSBarry Smith 2501c7afd0dbSLois Curfman McInnes Calling sequence of func: 250206ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 2503c7afd0dbSLois Curfman McInnes 2504c7afd0dbSLois Curfman McInnes + snes - the SNES context 250506ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 2506c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 2507184914b5SBarry Smith . reason - reason for convergence/divergence 2508c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 25094b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 25104b27c08aSLois Curfman McInnes - f - 2-norm of function 25119b94acceSBarry Smith 251236851e7fSLois Curfman McInnes Level: advanced 251336851e7fSLois Curfman McInnes 25149b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 25159b94acceSBarry Smith 251685385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 25179b94acceSBarry Smith @*/ 25187087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 25199b94acceSBarry Smith { 25207f7931b9SBarry Smith PetscErrorCode ierr; 25217f7931b9SBarry Smith 25223a40ed3dSBarry Smith PetscFunctionBegin; 25230700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 252485385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 25257f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 25267f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 25277f7931b9SBarry Smith } 252885385478SLisandro Dalcin snes->ops->converged = func; 25297f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 253085385478SLisandro Dalcin snes->cnvP = cctx; 25313a40ed3dSBarry Smith PetscFunctionReturn(0); 25329b94acceSBarry Smith } 25339b94acceSBarry Smith 25344a2ae208SSatish Balay #undef __FUNCT__ 25354a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 253652baeb72SSatish Balay /*@ 2537184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 2538184914b5SBarry Smith 2539184914b5SBarry Smith Not Collective 2540184914b5SBarry Smith 2541184914b5SBarry Smith Input Parameter: 2542184914b5SBarry Smith . snes - the SNES context 2543184914b5SBarry Smith 2544184914b5SBarry Smith Output Parameter: 25454d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 2546184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 2547184914b5SBarry Smith 2548184914b5SBarry Smith Level: intermediate 2549184914b5SBarry Smith 2550184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 2551184914b5SBarry Smith 2552184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 2553184914b5SBarry Smith 255485385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 2555184914b5SBarry Smith @*/ 25567087cfbeSBarry Smith PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 2557184914b5SBarry Smith { 2558184914b5SBarry Smith PetscFunctionBegin; 25590700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 25604482741eSBarry Smith PetscValidPointer(reason,2); 2561184914b5SBarry Smith *reason = snes->reason; 2562184914b5SBarry Smith PetscFunctionReturn(0); 2563184914b5SBarry Smith } 2564184914b5SBarry Smith 25654a2ae208SSatish Balay #undef __FUNCT__ 25664a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 2567c9005455SLois Curfman McInnes /*@ 2568c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 2569c9005455SLois Curfman McInnes 25703f9fe445SBarry Smith Logically Collective on SNES 2571fee21e36SBarry Smith 2572c7afd0dbSLois Curfman McInnes Input Parameters: 2573c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 25748c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 2575cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 2576758f92a0SBarry Smith . na - size of a and its 257764731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 2578758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 2579c7afd0dbSLois Curfman McInnes 2580308dcc3eSBarry Smith Notes: 2581308dcc3eSBarry Smith If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a 2582308dcc3eSBarry Smith default array of length 10000 is allocated. 2583308dcc3eSBarry Smith 2584c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 2585c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 2586c9005455SLois Curfman McInnes during the section of code that is being timed. 2587c9005455SLois Curfman McInnes 258836851e7fSLois Curfman McInnes Level: intermediate 258936851e7fSLois Curfman McInnes 2590c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2591758f92a0SBarry Smith 259208405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2593758f92a0SBarry Smith 2594c9005455SLois Curfman McInnes @*/ 25957087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset) 2596c9005455SLois Curfman McInnes { 2597308dcc3eSBarry Smith PetscErrorCode ierr; 2598308dcc3eSBarry Smith 25993a40ed3dSBarry Smith PetscFunctionBegin; 26000700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 26014482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2602a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2603308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) { 2604308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2605308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr); 2606308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr); 2607308dcc3eSBarry Smith snes->conv_malloc = PETSC_TRUE; 2608308dcc3eSBarry Smith } 2609c9005455SLois Curfman McInnes snes->conv_hist = a; 2610758f92a0SBarry Smith snes->conv_hist_its = its; 2611758f92a0SBarry Smith snes->conv_hist_max = na; 2612a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2613758f92a0SBarry Smith snes->conv_hist_reset = reset; 2614758f92a0SBarry Smith PetscFunctionReturn(0); 2615758f92a0SBarry Smith } 2616758f92a0SBarry Smith 2617308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2618c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 2619c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 2620308dcc3eSBarry Smith EXTERN_C_BEGIN 2621308dcc3eSBarry Smith #undef __FUNCT__ 2622308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab" 2623308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 2624308dcc3eSBarry Smith { 2625308dcc3eSBarry Smith mxArray *mat; 2626308dcc3eSBarry Smith PetscInt i; 2627308dcc3eSBarry Smith PetscReal *ar; 2628308dcc3eSBarry Smith 2629308dcc3eSBarry Smith PetscFunctionBegin; 2630308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL); 2631308dcc3eSBarry Smith ar = (PetscReal*) mxGetData(mat); 2632308dcc3eSBarry Smith for (i=0; i<snes->conv_hist_len; i++) { 2633308dcc3eSBarry Smith ar[i] = snes->conv_hist[i]; 2634308dcc3eSBarry Smith } 2635308dcc3eSBarry Smith PetscFunctionReturn(mat); 2636308dcc3eSBarry Smith } 2637308dcc3eSBarry Smith EXTERN_C_END 2638308dcc3eSBarry Smith #endif 2639308dcc3eSBarry Smith 2640308dcc3eSBarry Smith 26414a2ae208SSatish Balay #undef __FUNCT__ 26424a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 26430c4c9dddSBarry Smith /*@C 2644758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 2645758f92a0SBarry Smith 26463f9fe445SBarry Smith Not Collective 2647758f92a0SBarry Smith 2648758f92a0SBarry Smith Input Parameter: 2649758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 2650758f92a0SBarry Smith 2651758f92a0SBarry Smith Output Parameters: 2652758f92a0SBarry Smith . a - array to hold history 2653758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 2654758f92a0SBarry Smith negative if not converged) for each solve. 2655758f92a0SBarry Smith - na - size of a and its 2656758f92a0SBarry Smith 2657758f92a0SBarry Smith Notes: 2658758f92a0SBarry Smith The calling sequence for this routine in Fortran is 2659758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 2660758f92a0SBarry Smith 2661758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 2662758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 2663758f92a0SBarry Smith during the section of code that is being timed. 2664758f92a0SBarry Smith 2665758f92a0SBarry Smith Level: intermediate 2666758f92a0SBarry Smith 2667758f92a0SBarry Smith .keywords: SNES, get, convergence, history 2668758f92a0SBarry Smith 2669758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 2670758f92a0SBarry Smith 2671758f92a0SBarry Smith @*/ 26727087cfbeSBarry Smith PetscErrorCode SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 2673758f92a0SBarry Smith { 2674758f92a0SBarry Smith PetscFunctionBegin; 26750700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2676758f92a0SBarry Smith if (a) *a = snes->conv_hist; 2677758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 2678758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 26793a40ed3dSBarry Smith PetscFunctionReturn(0); 2680c9005455SLois Curfman McInnes } 2681c9005455SLois Curfman McInnes 2682e74ef692SMatthew Knepley #undef __FUNCT__ 2683e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 2684ac226902SBarry Smith /*@C 268576b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 2686eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 26877e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 268876b2cf59SMatthew Knepley 26893f9fe445SBarry Smith Logically Collective on SNES 269076b2cf59SMatthew Knepley 269176b2cf59SMatthew Knepley Input Parameters: 269276b2cf59SMatthew Knepley . snes - The nonlinear solver context 269376b2cf59SMatthew Knepley . func - The function 269476b2cf59SMatthew Knepley 269576b2cf59SMatthew Knepley Calling sequence of func: 2696b5d30489SBarry Smith . func (SNES snes, PetscInt step); 269776b2cf59SMatthew Knepley 269876b2cf59SMatthew Knepley . step - The current step of the iteration 269976b2cf59SMatthew Knepley 2700fe97e370SBarry Smith Level: advanced 2701fe97e370SBarry Smith 2702fe97e370SBarry 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() 2703fe97e370SBarry Smith This is not used by most users. 270476b2cf59SMatthew Knepley 270576b2cf59SMatthew Knepley .keywords: SNES, update 2706b5d30489SBarry Smith 270785385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 270876b2cf59SMatthew Knepley @*/ 27097087cfbeSBarry Smith PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 271076b2cf59SMatthew Knepley { 271176b2cf59SMatthew Knepley PetscFunctionBegin; 27120700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 2713e7788613SBarry Smith snes->ops->update = func; 271476b2cf59SMatthew Knepley PetscFunctionReturn(0); 271576b2cf59SMatthew Knepley } 271676b2cf59SMatthew Knepley 2717e74ef692SMatthew Knepley #undef __FUNCT__ 2718e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 271976b2cf59SMatthew Knepley /*@ 272076b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 272176b2cf59SMatthew Knepley 272276b2cf59SMatthew Knepley Not collective 272376b2cf59SMatthew Knepley 272476b2cf59SMatthew Knepley Input Parameters: 272576b2cf59SMatthew Knepley . snes - The nonlinear solver context 272676b2cf59SMatthew Knepley . step - The current step of the iteration 272776b2cf59SMatthew Knepley 2728205452f4SMatthew Knepley Level: intermediate 2729205452f4SMatthew Knepley 273076b2cf59SMatthew Knepley .keywords: SNES, update 2731a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 273276b2cf59SMatthew Knepley @*/ 27337087cfbeSBarry Smith PetscErrorCode SNESDefaultUpdate(SNES snes, PetscInt step) 273476b2cf59SMatthew Knepley { 273576b2cf59SMatthew Knepley PetscFunctionBegin; 273676b2cf59SMatthew Knepley PetscFunctionReturn(0); 273776b2cf59SMatthew Knepley } 273876b2cf59SMatthew Knepley 27394a2ae208SSatish Balay #undef __FUNCT__ 27404a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 27419b94acceSBarry Smith /* 27429b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 27439b94acceSBarry Smith positive parameter delta. 27449b94acceSBarry Smith 27459b94acceSBarry Smith Input Parameters: 2746c7afd0dbSLois Curfman McInnes + snes - the SNES context 27479b94acceSBarry Smith . y - approximate solution of linear system 27489b94acceSBarry Smith . fnorm - 2-norm of current function 2749c7afd0dbSLois Curfman McInnes - delta - trust region size 27509b94acceSBarry Smith 27519b94acceSBarry Smith Output Parameters: 2752c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 27539b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 27549b94acceSBarry Smith region, and exceeds zero otherwise. 2755c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 27569b94acceSBarry Smith 27579b94acceSBarry Smith Note: 27584b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 27599b94acceSBarry Smith is set to be the maximum allowable step size. 27609b94acceSBarry Smith 27619b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 27629b94acceSBarry Smith */ 2763dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 27649b94acceSBarry Smith { 2765064f8208SBarry Smith PetscReal nrm; 2766ea709b57SSatish Balay PetscScalar cnorm; 2767dfbe8321SBarry Smith PetscErrorCode ierr; 27683a40ed3dSBarry Smith 27693a40ed3dSBarry Smith PetscFunctionBegin; 27700700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 27710700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 2772c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 2773184914b5SBarry Smith 2774064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 2775064f8208SBarry Smith if (nrm > *delta) { 2776064f8208SBarry Smith nrm = *delta/nrm; 2777064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 2778064f8208SBarry Smith cnorm = nrm; 27792dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 27809b94acceSBarry Smith *ynorm = *delta; 27819b94acceSBarry Smith } else { 27829b94acceSBarry Smith *gpnorm = 0.0; 2783064f8208SBarry Smith *ynorm = nrm; 27849b94acceSBarry Smith } 27853a40ed3dSBarry Smith PetscFunctionReturn(0); 27869b94acceSBarry Smith } 27879b94acceSBarry Smith 27884a2ae208SSatish Balay #undef __FUNCT__ 27894a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 27906ce558aeSBarry Smith /*@C 2791f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 2792f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 27939b94acceSBarry Smith 2794c7afd0dbSLois Curfman McInnes Collective on SNES 2795c7afd0dbSLois Curfman McInnes 2796b2002411SLois Curfman McInnes Input Parameters: 2797c7afd0dbSLois Curfman McInnes + snes - the SNES context 27983cebf274SJed Brown . b - the constant part of the equation F(x) = b, or PETSC_NULL to use zero. 279985385478SLisandro Dalcin - x - the solution vector. 28009b94acceSBarry Smith 2801b2002411SLois Curfman McInnes Notes: 28028ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 28038ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 28048ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 28058ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 28068ddd3da0SLois Curfman McInnes 280736851e7fSLois Curfman McInnes Level: beginner 280836851e7fSLois Curfman McInnes 28099b94acceSBarry Smith .keywords: SNES, nonlinear, solve 28109b94acceSBarry Smith 2811*c0df2a02SJed Brown .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetGridSequence(), SNESGetSolution() 28129b94acceSBarry Smith @*/ 28137087cfbeSBarry Smith PetscErrorCode SNESSolve(SNES snes,Vec b,Vec x) 28149b94acceSBarry Smith { 2815dfbe8321SBarry Smith PetscErrorCode ierr; 2816ace3abfcSBarry Smith PetscBool flg; 2817eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 2818eabae89aSBarry Smith PetscViewer viewer; 2819efd51863SBarry Smith PetscInt grid; 2820052efed2SBarry Smith 28213a40ed3dSBarry Smith PetscFunctionBegin; 28220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28230700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2824f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 28250700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 282685385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 282785385478SLisandro Dalcin 2828a8248277SBarry Smith for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);} 2829efd51863SBarry Smith for (grid=0; grid<snes->gridsequence+1; grid++) { 2830efd51863SBarry Smith 283185385478SLisandro Dalcin /* set solution vector */ 2832efd51863SBarry Smith if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);} 28336bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 283485385478SLisandro Dalcin snes->vec_sol = x; 283585385478SLisandro Dalcin /* set afine vector if provided */ 283685385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 28376bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 283885385478SLisandro Dalcin snes->vec_rhs = b; 283985385478SLisandro Dalcin 284070e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 28413f149594SLisandro Dalcin 2842d25893d9SBarry Smith if (!grid && snes->ops->computeinitialguess) { 2843d25893d9SBarry Smith ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr); 2844d25893d9SBarry Smith } 2845d25893d9SBarry Smith 2846abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 284750ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2848d5e45103SBarry Smith 28493f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 28504936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 285185385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 28524936397dSBarry Smith if (snes->domainerror){ 28534936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 28544936397dSBarry Smith snes->domainerror = PETSC_FALSE; 28554936397dSBarry Smith } 285617186662SBarry Smith if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 28573f149594SLisandro Dalcin 28587adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2859eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 28607adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2861eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 28626bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2863eabae89aSBarry Smith } 2864eabae89aSBarry Smith 286590d69ab7SBarry Smith flg = PETSC_FALSE; 2866acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr); 2867da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 28685968eb51SBarry Smith if (snes->printreason) { 2869a8248277SBarry Smith ierr = PetscViewerASCIIAddTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 28705968eb51SBarry Smith if (snes->reason > 0) { 2871a8248277SBarry Smith ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 28725968eb51SBarry Smith } else { 2873a8248277SBarry Smith ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 28745968eb51SBarry Smith } 2875a8248277SBarry Smith ierr = PetscViewerASCIISubtractTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 28765968eb51SBarry Smith } 28775968eb51SBarry Smith 28788501fc72SJed Brown flg = PETSC_FALSE; 28798501fc72SJed Brown ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view_solution_vtk",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 28808501fc72SJed Brown if (flg) { 28818501fc72SJed Brown PetscViewer viewer; 28828501fc72SJed Brown ierr = PetscViewerCreate(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr); 28838501fc72SJed Brown ierr = PetscViewerSetType(viewer,PETSCVIEWERVTK);CHKERRQ(ierr); 28848501fc72SJed Brown ierr = PetscViewerFileSetName(viewer,filename);CHKERRQ(ierr); 28858501fc72SJed Brown ierr = VecView(snes->vec_sol,viewer);CHKERRQ(ierr); 28868501fc72SJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 28878501fc72SJed Brown } 28888501fc72SJed Brown 2889e113a28aSBarry Smith if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged"); 2890efd51863SBarry Smith if (grid < snes->gridsequence) { 2891efd51863SBarry Smith DM fine; 2892efd51863SBarry Smith Vec xnew; 2893efd51863SBarry Smith Mat interp; 2894efd51863SBarry Smith 2895efd51863SBarry Smith ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr); 2896efd51863SBarry Smith ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr); 2897efd51863SBarry Smith ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr); 2898efd51863SBarry Smith ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr); 2899efd51863SBarry Smith ierr = MatDestroy(&interp);CHKERRQ(ierr); 2900efd51863SBarry Smith x = xnew; 2901efd51863SBarry Smith 2902efd51863SBarry Smith ierr = SNESReset(snes);CHKERRQ(ierr); 2903efd51863SBarry Smith ierr = SNESSetDM(snes,fine);CHKERRQ(ierr); 2904efd51863SBarry Smith ierr = DMDestroy(&fine);CHKERRQ(ierr); 2905a8248277SBarry Smith ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr); 2906efd51863SBarry Smith } 2907efd51863SBarry Smith } 29083a40ed3dSBarry Smith PetscFunctionReturn(0); 29099b94acceSBarry Smith } 29109b94acceSBarry Smith 29119b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 29129b94acceSBarry Smith 29134a2ae208SSatish Balay #undef __FUNCT__ 29144a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 291582bf6240SBarry Smith /*@C 29164b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 29179b94acceSBarry Smith 2918fee21e36SBarry Smith Collective on SNES 2919fee21e36SBarry Smith 2920c7afd0dbSLois Curfman McInnes Input Parameters: 2921c7afd0dbSLois Curfman McInnes + snes - the SNES context 2922454a90a3SBarry Smith - type - a known method 2923c7afd0dbSLois Curfman McInnes 2924c7afd0dbSLois Curfman McInnes Options Database Key: 2925454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2926c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2927ae12b187SLois Curfman McInnes 29289b94acceSBarry Smith Notes: 2929e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 29304b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2931c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 29324b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2933c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 29349b94acceSBarry Smith 2935ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2936ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2937ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2938ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2939ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2940ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2941ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2942ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2943ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2944b0a32e0cSBarry Smith appropriate method. 294536851e7fSLois Curfman McInnes 294636851e7fSLois Curfman McInnes Level: intermediate 2947a703fe33SLois Curfman McInnes 2948454a90a3SBarry Smith .keywords: SNES, set, type 2949435da068SBarry Smith 2950435da068SBarry Smith .seealso: SNESType, SNESCreate() 2951435da068SBarry Smith 29529b94acceSBarry Smith @*/ 29537087cfbeSBarry Smith PetscErrorCode SNESSetType(SNES snes,const SNESType type) 29549b94acceSBarry Smith { 2955dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 2956ace3abfcSBarry Smith PetscBool match; 29573a40ed3dSBarry Smith 29583a40ed3dSBarry Smith PetscFunctionBegin; 29590700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29604482741eSBarry Smith PetscValidCharPointer(type,2); 296182bf6240SBarry Smith 29626831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 29630f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 296492ff6ae8SBarry Smith 29654b91b6eaSBarry Smith ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 2966e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 296775396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 296875396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 296975396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 297075396ef9SLisandro Dalcin snes->ops->setup = 0; 297175396ef9SLisandro Dalcin snes->ops->solve = 0; 297275396ef9SLisandro Dalcin snes->ops->view = 0; 297375396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 297475396ef9SLisandro Dalcin snes->ops->destroy = 0; 297575396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 297675396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 2977454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 297803bfa161SLisandro Dalcin ierr = (*r)(snes);CHKERRQ(ierr); 29799fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS) 29809fb22e1aSBarry Smith if (PetscAMSPublishAll) { 29819fb22e1aSBarry Smith ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr); 29829fb22e1aSBarry Smith } 29839fb22e1aSBarry Smith #endif 29843a40ed3dSBarry Smith PetscFunctionReturn(0); 29859b94acceSBarry Smith } 29869b94acceSBarry Smith 2987a847f771SSatish Balay 29889b94acceSBarry Smith /* --------------------------------------------------------------------- */ 29894a2ae208SSatish Balay #undef __FUNCT__ 29904a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 299152baeb72SSatish Balay /*@ 29929b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2993f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 29949b94acceSBarry Smith 2995fee21e36SBarry Smith Not Collective 2996fee21e36SBarry Smith 299736851e7fSLois Curfman McInnes Level: advanced 299836851e7fSLois Curfman McInnes 29999b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 30009b94acceSBarry Smith 30019b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 30029b94acceSBarry Smith @*/ 30037087cfbeSBarry Smith PetscErrorCode SNESRegisterDestroy(void) 30049b94acceSBarry Smith { 3005dfbe8321SBarry Smith PetscErrorCode ierr; 300682bf6240SBarry Smith 30073a40ed3dSBarry Smith PetscFunctionBegin; 30081441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 30094c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 30103a40ed3dSBarry Smith PetscFunctionReturn(0); 30119b94acceSBarry Smith } 30129b94acceSBarry Smith 30134a2ae208SSatish Balay #undef __FUNCT__ 30144a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 30159b94acceSBarry Smith /*@C 30169a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 30179b94acceSBarry Smith 3018c7afd0dbSLois Curfman McInnes Not Collective 3019c7afd0dbSLois Curfman McInnes 30209b94acceSBarry Smith Input Parameter: 30214b0e389bSBarry Smith . snes - nonlinear solver context 30229b94acceSBarry Smith 30239b94acceSBarry Smith Output Parameter: 30243a7fca6bSBarry Smith . type - SNES method (a character string) 30259b94acceSBarry Smith 302636851e7fSLois Curfman McInnes Level: intermediate 302736851e7fSLois Curfman McInnes 3028454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 30299b94acceSBarry Smith @*/ 30307087cfbeSBarry Smith PetscErrorCode SNESGetType(SNES snes,const SNESType *type) 30319b94acceSBarry Smith { 30323a40ed3dSBarry Smith PetscFunctionBegin; 30330700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 30344482741eSBarry Smith PetscValidPointer(type,2); 30357adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 30363a40ed3dSBarry Smith PetscFunctionReturn(0); 30379b94acceSBarry Smith } 30389b94acceSBarry Smith 30394a2ae208SSatish Balay #undef __FUNCT__ 30404a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 304152baeb72SSatish Balay /*@ 30429b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 3043*c0df2a02SJed Brown stored. This is the fine grid solution when using SNESSetGridSequence(). 30449b94acceSBarry Smith 3045c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 3046c7afd0dbSLois Curfman McInnes 30479b94acceSBarry Smith Input Parameter: 30489b94acceSBarry Smith . snes - the SNES context 30499b94acceSBarry Smith 30509b94acceSBarry Smith Output Parameter: 30519b94acceSBarry Smith . x - the solution 30529b94acceSBarry Smith 305370e92668SMatthew Knepley Level: intermediate 305436851e7fSLois Curfman McInnes 30559b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 30569b94acceSBarry Smith 305785385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 30589b94acceSBarry Smith @*/ 30597087cfbeSBarry Smith PetscErrorCode SNESGetSolution(SNES snes,Vec *x) 30609b94acceSBarry Smith { 30613a40ed3dSBarry Smith PetscFunctionBegin; 30620700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 30634482741eSBarry Smith PetscValidPointer(x,2); 306485385478SLisandro Dalcin *x = snes->vec_sol; 306570e92668SMatthew Knepley PetscFunctionReturn(0); 306670e92668SMatthew Knepley } 306770e92668SMatthew Knepley 306870e92668SMatthew Knepley #undef __FUNCT__ 30694a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 307052baeb72SSatish Balay /*@ 30719b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 30729b94acceSBarry Smith stored. 30739b94acceSBarry Smith 3074c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 3075c7afd0dbSLois Curfman McInnes 30769b94acceSBarry Smith Input Parameter: 30779b94acceSBarry Smith . snes - the SNES context 30789b94acceSBarry Smith 30799b94acceSBarry Smith Output Parameter: 30809b94acceSBarry Smith . x - the solution update 30819b94acceSBarry Smith 308236851e7fSLois Curfman McInnes Level: advanced 308336851e7fSLois Curfman McInnes 30849b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 30859b94acceSBarry Smith 308685385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 30879b94acceSBarry Smith @*/ 30887087cfbeSBarry Smith PetscErrorCode SNESGetSolutionUpdate(SNES snes,Vec *x) 30899b94acceSBarry Smith { 30903a40ed3dSBarry Smith PetscFunctionBegin; 30910700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 30924482741eSBarry Smith PetscValidPointer(x,2); 309385385478SLisandro Dalcin *x = snes->vec_sol_update; 30943a40ed3dSBarry Smith PetscFunctionReturn(0); 30959b94acceSBarry Smith } 30969b94acceSBarry Smith 30974a2ae208SSatish Balay #undef __FUNCT__ 30984a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 30999b94acceSBarry Smith /*@C 31003638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 31019b94acceSBarry Smith 3102c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 3103c7afd0dbSLois Curfman McInnes 31049b94acceSBarry Smith Input Parameter: 31059b94acceSBarry Smith . snes - the SNES context 31069b94acceSBarry Smith 31079b94acceSBarry Smith Output Parameter: 31087bf4e008SBarry Smith + r - the function (or PETSC_NULL) 310970e92668SMatthew Knepley . func - the function (or PETSC_NULL) 311070e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 31119b94acceSBarry Smith 311236851e7fSLois Curfman McInnes Level: advanced 311336851e7fSLois Curfman McInnes 3114a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 31159b94acceSBarry Smith 31164b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 31179b94acceSBarry Smith @*/ 31187087cfbeSBarry Smith PetscErrorCode SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 31199b94acceSBarry Smith { 31203a40ed3dSBarry Smith PetscFunctionBegin; 31210700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 312285385478SLisandro Dalcin if (r) *r = snes->vec_func; 3123e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 312470e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 31253a40ed3dSBarry Smith PetscFunctionReturn(0); 31269b94acceSBarry Smith } 31279b94acceSBarry Smith 31284a2ae208SSatish Balay #undef __FUNCT__ 31294a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 31303c7409f5SSatish Balay /*@C 31313c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 3132d850072dSLois Curfman McInnes SNES options in the database. 31333c7409f5SSatish Balay 31343f9fe445SBarry Smith Logically Collective on SNES 3135fee21e36SBarry Smith 3136c7afd0dbSLois Curfman McInnes Input Parameter: 3137c7afd0dbSLois Curfman McInnes + snes - the SNES context 3138c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3139c7afd0dbSLois Curfman McInnes 3140d850072dSLois Curfman McInnes Notes: 3141a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3142c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3143d850072dSLois Curfman McInnes 314436851e7fSLois Curfman McInnes Level: advanced 314536851e7fSLois Curfman McInnes 31463c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 3147a86d99e1SLois Curfman McInnes 3148a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 31493c7409f5SSatish Balay @*/ 31507087cfbeSBarry Smith PetscErrorCode SNESSetOptionsPrefix(SNES snes,const char prefix[]) 31513c7409f5SSatish Balay { 3152dfbe8321SBarry Smith PetscErrorCode ierr; 31533c7409f5SSatish Balay 31543a40ed3dSBarry Smith PetscFunctionBegin; 31550700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3156639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 31571cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 315894b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 31593a40ed3dSBarry Smith PetscFunctionReturn(0); 31603c7409f5SSatish Balay } 31613c7409f5SSatish Balay 31624a2ae208SSatish Balay #undef __FUNCT__ 31634a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 31643c7409f5SSatish Balay /*@C 3165f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 3166d850072dSLois Curfman McInnes SNES options in the database. 31673c7409f5SSatish Balay 31683f9fe445SBarry Smith Logically Collective on SNES 3169fee21e36SBarry Smith 3170c7afd0dbSLois Curfman McInnes Input Parameters: 3171c7afd0dbSLois Curfman McInnes + snes - the SNES context 3172c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3173c7afd0dbSLois Curfman McInnes 3174d850072dSLois Curfman McInnes Notes: 3175a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3176c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3177d850072dSLois Curfman McInnes 317836851e7fSLois Curfman McInnes Level: advanced 317936851e7fSLois Curfman McInnes 31803c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 3181a86d99e1SLois Curfman McInnes 3182a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 31833c7409f5SSatish Balay @*/ 31847087cfbeSBarry Smith PetscErrorCode SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 31853c7409f5SSatish Balay { 3186dfbe8321SBarry Smith PetscErrorCode ierr; 31873c7409f5SSatish Balay 31883a40ed3dSBarry Smith PetscFunctionBegin; 31890700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3190639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 31911cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 319294b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 31933a40ed3dSBarry Smith PetscFunctionReturn(0); 31943c7409f5SSatish Balay } 31953c7409f5SSatish Balay 31964a2ae208SSatish Balay #undef __FUNCT__ 31974a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 31989ab63eb5SSatish Balay /*@C 31993c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 32003c7409f5SSatish Balay SNES options in the database. 32013c7409f5SSatish Balay 3202c7afd0dbSLois Curfman McInnes Not Collective 3203c7afd0dbSLois Curfman McInnes 32043c7409f5SSatish Balay Input Parameter: 32053c7409f5SSatish Balay . snes - the SNES context 32063c7409f5SSatish Balay 32073c7409f5SSatish Balay Output Parameter: 32083c7409f5SSatish Balay . prefix - pointer to the prefix string used 32093c7409f5SSatish Balay 32104ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 32119ab63eb5SSatish Balay sufficient length to hold the prefix. 32129ab63eb5SSatish Balay 321336851e7fSLois Curfman McInnes Level: advanced 321436851e7fSLois Curfman McInnes 32153c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 3216a86d99e1SLois Curfman McInnes 3217a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 32183c7409f5SSatish Balay @*/ 32197087cfbeSBarry Smith PetscErrorCode SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 32203c7409f5SSatish Balay { 3221dfbe8321SBarry Smith PetscErrorCode ierr; 32223c7409f5SSatish Balay 32233a40ed3dSBarry Smith PetscFunctionBegin; 32240700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3225639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 32263a40ed3dSBarry Smith PetscFunctionReturn(0); 32273c7409f5SSatish Balay } 32283c7409f5SSatish Balay 3229b2002411SLois Curfman McInnes 32304a2ae208SSatish Balay #undef __FUNCT__ 32314a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 32323cea93caSBarry Smith /*@C 32333cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 32343cea93caSBarry Smith 32357f6c08e0SMatthew Knepley Level: advanced 32363cea93caSBarry Smith @*/ 32377087cfbeSBarry Smith PetscErrorCode SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 3238b2002411SLois Curfman McInnes { 3239e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 3240dfbe8321SBarry Smith PetscErrorCode ierr; 3241b2002411SLois Curfman McInnes 3242b2002411SLois Curfman McInnes PetscFunctionBegin; 3243b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 3244c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 3245b2002411SLois Curfman McInnes PetscFunctionReturn(0); 3246b2002411SLois Curfman McInnes } 3247da9b6338SBarry Smith 3248da9b6338SBarry Smith #undef __FUNCT__ 3249da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 32507087cfbeSBarry Smith PetscErrorCode SNESTestLocalMin(SNES snes) 3251da9b6338SBarry Smith { 3252dfbe8321SBarry Smith PetscErrorCode ierr; 325377431f27SBarry Smith PetscInt N,i,j; 3254da9b6338SBarry Smith Vec u,uh,fh; 3255da9b6338SBarry Smith PetscScalar value; 3256da9b6338SBarry Smith PetscReal norm; 3257da9b6338SBarry Smith 3258da9b6338SBarry Smith PetscFunctionBegin; 3259da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 3260da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 3261da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 3262da9b6338SBarry Smith 3263da9b6338SBarry Smith /* currently only works for sequential */ 3264da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 3265da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 3266da9b6338SBarry Smith for (i=0; i<N; i++) { 3267da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 326877431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 3269da9b6338SBarry Smith for (j=-10; j<11; j++) { 3270ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 3271da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 32723ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 3273da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 327477431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 3275da9b6338SBarry Smith value = -value; 3276da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 3277da9b6338SBarry Smith } 3278da9b6338SBarry Smith } 32796bf464f9SBarry Smith ierr = VecDestroy(&uh);CHKERRQ(ierr); 32806bf464f9SBarry Smith ierr = VecDestroy(&fh);CHKERRQ(ierr); 3281da9b6338SBarry Smith PetscFunctionReturn(0); 3282da9b6338SBarry Smith } 328371f87433Sdalcinl 328471f87433Sdalcinl #undef __FUNCT__ 3285fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 328671f87433Sdalcinl /*@ 3287fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 328871f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 328971f87433Sdalcinl Newton method. 329071f87433Sdalcinl 32913f9fe445SBarry Smith Logically Collective on SNES 329271f87433Sdalcinl 329371f87433Sdalcinl Input Parameters: 329471f87433Sdalcinl + snes - SNES context 329571f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 329671f87433Sdalcinl 329764ba62caSBarry Smith Options Database: 329864ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 329964ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 330064ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 330164ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 330264ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 330364ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 330464ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 330564ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 330664ba62caSBarry Smith 330771f87433Sdalcinl Notes: 330871f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 330971f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 331071f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 331171f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 331271f87433Sdalcinl solver. 331371f87433Sdalcinl 331471f87433Sdalcinl Level: advanced 331571f87433Sdalcinl 331671f87433Sdalcinl Reference: 331771f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 331871f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 331971f87433Sdalcinl 332071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 332171f87433Sdalcinl 3322fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 332371f87433Sdalcinl @*/ 33247087cfbeSBarry Smith PetscErrorCode SNESKSPSetUseEW(SNES snes,PetscBool flag) 332571f87433Sdalcinl { 332671f87433Sdalcinl PetscFunctionBegin; 33270700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3328acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flag,2); 332971f87433Sdalcinl snes->ksp_ewconv = flag; 333071f87433Sdalcinl PetscFunctionReturn(0); 333171f87433Sdalcinl } 333271f87433Sdalcinl 333371f87433Sdalcinl #undef __FUNCT__ 3334fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 333571f87433Sdalcinl /*@ 3336fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 333771f87433Sdalcinl for computing relative tolerance for linear solvers within an 333871f87433Sdalcinl inexact Newton method. 333971f87433Sdalcinl 334071f87433Sdalcinl Not Collective 334171f87433Sdalcinl 334271f87433Sdalcinl Input Parameter: 334371f87433Sdalcinl . snes - SNES context 334471f87433Sdalcinl 334571f87433Sdalcinl Output Parameter: 334671f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 334771f87433Sdalcinl 334871f87433Sdalcinl Notes: 334971f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 335071f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 335171f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 335271f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 335371f87433Sdalcinl solver. 335471f87433Sdalcinl 335571f87433Sdalcinl Level: advanced 335671f87433Sdalcinl 335771f87433Sdalcinl Reference: 335871f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 335971f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 336071f87433Sdalcinl 336171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 336271f87433Sdalcinl 3363fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 336471f87433Sdalcinl @*/ 33657087cfbeSBarry Smith PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 336671f87433Sdalcinl { 336771f87433Sdalcinl PetscFunctionBegin; 33680700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 336971f87433Sdalcinl PetscValidPointer(flag,2); 337071f87433Sdalcinl *flag = snes->ksp_ewconv; 337171f87433Sdalcinl PetscFunctionReturn(0); 337271f87433Sdalcinl } 337371f87433Sdalcinl 337471f87433Sdalcinl #undef __FUNCT__ 3375fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 337671f87433Sdalcinl /*@ 3377fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 337871f87433Sdalcinl convergence criteria for the linear solvers within an inexact 337971f87433Sdalcinl Newton method. 338071f87433Sdalcinl 33813f9fe445SBarry Smith Logically Collective on SNES 338271f87433Sdalcinl 338371f87433Sdalcinl Input Parameters: 338471f87433Sdalcinl + snes - SNES context 338571f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 338671f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 338771f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 338871f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 338971f87433Sdalcinl (0 <= gamma2 <= 1) 339071f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 339171f87433Sdalcinl . alpha2 - power for safeguard 339271f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 339371f87433Sdalcinl 339471f87433Sdalcinl Note: 339571f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 339671f87433Sdalcinl 339771f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 339871f87433Sdalcinl 339971f87433Sdalcinl Level: advanced 340071f87433Sdalcinl 340171f87433Sdalcinl Reference: 340271f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 340371f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 340471f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 340571f87433Sdalcinl 340671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 340771f87433Sdalcinl 3408fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 340971f87433Sdalcinl @*/ 34107087cfbeSBarry Smith PetscErrorCode SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 341171f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 341271f87433Sdalcinl { 3413fa9f3622SBarry Smith SNESKSPEW *kctx; 341471f87433Sdalcinl PetscFunctionBegin; 34150700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3416fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3417e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 3418c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,version,2); 3419c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_0,3); 3420c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_max,4); 3421c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,gamma,5); 3422c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha,6); 3423c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha2,7); 3424c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,threshold,8); 342571f87433Sdalcinl 342671f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 342771f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 342871f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 342971f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 343071f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 343171f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 343271f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 343371f87433Sdalcinl 343471f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 3435e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 343671f87433Sdalcinl } 343771f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 3438e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 343971f87433Sdalcinl } 344071f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 3441e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 344271f87433Sdalcinl } 344371f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 3444e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 344571f87433Sdalcinl } 344671f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 3447e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 344871f87433Sdalcinl } 344971f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 3450e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 345171f87433Sdalcinl } 345271f87433Sdalcinl PetscFunctionReturn(0); 345371f87433Sdalcinl } 345471f87433Sdalcinl 345571f87433Sdalcinl #undef __FUNCT__ 3456fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 345771f87433Sdalcinl /*@ 3458fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 345971f87433Sdalcinl convergence criteria for the linear solvers within an inexact 346071f87433Sdalcinl Newton method. 346171f87433Sdalcinl 346271f87433Sdalcinl Not Collective 346371f87433Sdalcinl 346471f87433Sdalcinl Input Parameters: 346571f87433Sdalcinl snes - SNES context 346671f87433Sdalcinl 346771f87433Sdalcinl Output Parameters: 346871f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 346971f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 347071f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 347171f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 347271f87433Sdalcinl (0 <= gamma2 <= 1) 347371f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 347471f87433Sdalcinl . alpha2 - power for safeguard 347571f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 347671f87433Sdalcinl 347771f87433Sdalcinl Level: advanced 347871f87433Sdalcinl 347971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 348071f87433Sdalcinl 3481fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 348271f87433Sdalcinl @*/ 34837087cfbeSBarry Smith PetscErrorCode SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 348471f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 348571f87433Sdalcinl { 3486fa9f3622SBarry Smith SNESKSPEW *kctx; 348771f87433Sdalcinl PetscFunctionBegin; 34880700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3489fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3490e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 349171f87433Sdalcinl if(version) *version = kctx->version; 349271f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 349371f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 349471f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 349571f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 349671f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 349771f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 349871f87433Sdalcinl PetscFunctionReturn(0); 349971f87433Sdalcinl } 350071f87433Sdalcinl 350171f87433Sdalcinl #undef __FUNCT__ 3502fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 3503fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 350471f87433Sdalcinl { 350571f87433Sdalcinl PetscErrorCode ierr; 3506fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 350771f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 350871f87433Sdalcinl 350971f87433Sdalcinl PetscFunctionBegin; 3510e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 351171f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 351271f87433Sdalcinl rtol = kctx->rtol_0; 351371f87433Sdalcinl } else { 351471f87433Sdalcinl if (kctx->version == 1) { 351571f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 351671f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 351771f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 351871f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 351971f87433Sdalcinl } else if (kctx->version == 2) { 352071f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 352171f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 352271f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 352371f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 352471f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 352571f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 352671f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 352771f87433Sdalcinl stol = PetscMax(rtol,stol); 352871f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 352971f87433Sdalcinl /* safeguard: avoid oversolving */ 353071f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 353171f87433Sdalcinl stol = PetscMax(rtol,stol); 353271f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 3533e32f2f54SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 353471f87433Sdalcinl } 353571f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 353671f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 353771f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 353871f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 353971f87433Sdalcinl PetscFunctionReturn(0); 354071f87433Sdalcinl } 354171f87433Sdalcinl 354271f87433Sdalcinl #undef __FUNCT__ 3543fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 3544fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 354571f87433Sdalcinl { 354671f87433Sdalcinl PetscErrorCode ierr; 3547fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 354871f87433Sdalcinl PCSide pcside; 354971f87433Sdalcinl Vec lres; 355071f87433Sdalcinl 355171f87433Sdalcinl PetscFunctionBegin; 3552e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 355371f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 355471f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 355571f87433Sdalcinl if (kctx->version == 1) { 3556b037da10SBarry Smith ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr); 355771f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 355871f87433Sdalcinl /* KSP residual is true linear residual */ 355971f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 356071f87433Sdalcinl } else { 356171f87433Sdalcinl /* KSP residual is preconditioned residual */ 356271f87433Sdalcinl /* compute true linear residual norm */ 356371f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 356471f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 356571f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 356671f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 35676bf464f9SBarry Smith ierr = VecDestroy(&lres);CHKERRQ(ierr); 356871f87433Sdalcinl } 356971f87433Sdalcinl } 357071f87433Sdalcinl PetscFunctionReturn(0); 357171f87433Sdalcinl } 357271f87433Sdalcinl 357371f87433Sdalcinl #undef __FUNCT__ 357471f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 357571f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 357671f87433Sdalcinl { 357771f87433Sdalcinl PetscErrorCode ierr; 357871f87433Sdalcinl 357971f87433Sdalcinl PetscFunctionBegin; 3580fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 358171f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 3582fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 358371f87433Sdalcinl PetscFunctionReturn(0); 358471f87433Sdalcinl } 35856c699258SBarry Smith 35866c699258SBarry Smith #undef __FUNCT__ 35876c699258SBarry Smith #define __FUNCT__ "SNESSetDM" 35886c699258SBarry Smith /*@ 35896c699258SBarry Smith SNESSetDM - Sets the DM that may be used by some preconditioners 35906c699258SBarry Smith 35913f9fe445SBarry Smith Logically Collective on SNES 35926c699258SBarry Smith 35936c699258SBarry Smith Input Parameters: 35946c699258SBarry Smith + snes - the preconditioner context 35956c699258SBarry Smith - dm - the dm 35966c699258SBarry Smith 35976c699258SBarry Smith Level: intermediate 35986c699258SBarry Smith 35996c699258SBarry Smith 36006c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM() 36016c699258SBarry Smith @*/ 36027087cfbeSBarry Smith PetscErrorCode SNESSetDM(SNES snes,DM dm) 36036c699258SBarry Smith { 36046c699258SBarry Smith PetscErrorCode ierr; 3605345fed2cSBarry Smith KSP ksp; 36066c699258SBarry Smith 36076c699258SBarry Smith PetscFunctionBegin; 36080700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3609d0660788SBarry Smith if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);} 36106bf464f9SBarry Smith ierr = DMDestroy(&snes->dm);CHKERRQ(ierr); 36116c699258SBarry Smith snes->dm = dm; 3612345fed2cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 3613345fed2cSBarry Smith ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr); 3614f22f69f0SBarry Smith ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr); 36152c155ee1SBarry Smith if (snes->pc) { 36162c155ee1SBarry Smith ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 36172c155ee1SBarry Smith } 36186c699258SBarry Smith PetscFunctionReturn(0); 36196c699258SBarry Smith } 36206c699258SBarry Smith 36216c699258SBarry Smith #undef __FUNCT__ 36226c699258SBarry Smith #define __FUNCT__ "SNESGetDM" 36236c699258SBarry Smith /*@ 36246c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 36256c699258SBarry Smith 36263f9fe445SBarry Smith Not Collective but DM obtained is parallel on SNES 36276c699258SBarry Smith 36286c699258SBarry Smith Input Parameter: 36296c699258SBarry Smith . snes - the preconditioner context 36306c699258SBarry Smith 36316c699258SBarry Smith Output Parameter: 36326c699258SBarry Smith . dm - the dm 36336c699258SBarry Smith 36346c699258SBarry Smith Level: intermediate 36356c699258SBarry Smith 36366c699258SBarry Smith 36376c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM() 36386c699258SBarry Smith @*/ 36397087cfbeSBarry Smith PetscErrorCode SNESGetDM(SNES snes,DM *dm) 36406c699258SBarry Smith { 36416c699258SBarry Smith PetscFunctionBegin; 36420700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 36436c699258SBarry Smith *dm = snes->dm; 36446c699258SBarry Smith PetscFunctionReturn(0); 36456c699258SBarry Smith } 36460807856dSBarry Smith 364731823bd8SMatthew G Knepley #undef __FUNCT__ 364831823bd8SMatthew G Knepley #define __FUNCT__ "SNESSetPC" 364931823bd8SMatthew G Knepley /*@ 3650fd4b34e9SJed Brown SNESSetPC - Sets the nonlinear preconditioner to be used. 365131823bd8SMatthew G Knepley 365231823bd8SMatthew G Knepley Collective on SNES 365331823bd8SMatthew G Knepley 365431823bd8SMatthew G Knepley Input Parameters: 365531823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate() 365631823bd8SMatthew G Knepley - pc - the preconditioner object 365731823bd8SMatthew G Knepley 365831823bd8SMatthew G Knepley Notes: 365931823bd8SMatthew G Knepley Use SNESGetPC() to retrieve the preconditioner context (for example, 366031823bd8SMatthew G Knepley to configure it using the API). 366131823bd8SMatthew G Knepley 366231823bd8SMatthew G Knepley Level: developer 366331823bd8SMatthew G Knepley 366431823bd8SMatthew G Knepley .keywords: SNES, set, precondition 366531823bd8SMatthew G Knepley .seealso: SNESGetPC() 366631823bd8SMatthew G Knepley @*/ 366731823bd8SMatthew G Knepley PetscErrorCode SNESSetPC(SNES snes, SNES pc) 366831823bd8SMatthew G Knepley { 366931823bd8SMatthew G Knepley PetscErrorCode ierr; 367031823bd8SMatthew G Knepley 367131823bd8SMatthew G Knepley PetscFunctionBegin; 367231823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 367331823bd8SMatthew G Knepley PetscValidHeaderSpecific(pc, SNES_CLASSID, 2); 367431823bd8SMatthew G Knepley PetscCheckSameComm(snes, 1, pc, 2); 367531823bd8SMatthew G Knepley ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr); 3676bf11d3b6SBarry Smith ierr = SNESDestroy(&snes->pc);CHKERRQ(ierr); 367731823bd8SMatthew G Knepley snes->pc = pc; 367831823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 367931823bd8SMatthew G Knepley PetscFunctionReturn(0); 368031823bd8SMatthew G Knepley } 368131823bd8SMatthew G Knepley 368231823bd8SMatthew G Knepley #undef __FUNCT__ 368331823bd8SMatthew G Knepley #define __FUNCT__ "SNESGetPC" 368431823bd8SMatthew G Knepley /*@ 3685fd4b34e9SJed Brown SNESGetPC - Returns a pointer to the nonlinear preconditioning context set with SNESSetPC(). 368631823bd8SMatthew G Knepley 368731823bd8SMatthew G Knepley Not Collective 368831823bd8SMatthew G Knepley 368931823bd8SMatthew G Knepley Input Parameter: 369031823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate() 369131823bd8SMatthew G Knepley 369231823bd8SMatthew G Knepley Output Parameter: 369331823bd8SMatthew G Knepley . pc - preconditioner context 369431823bd8SMatthew G Knepley 369531823bd8SMatthew G Knepley Level: developer 369631823bd8SMatthew G Knepley 369731823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner 369831823bd8SMatthew G Knepley .seealso: SNESSetPC() 369931823bd8SMatthew G Knepley @*/ 370031823bd8SMatthew G Knepley PetscErrorCode SNESGetPC(SNES snes, SNES *pc) 370131823bd8SMatthew G Knepley { 370231823bd8SMatthew G Knepley PetscErrorCode ierr; 370331823bd8SMatthew G Knepley 370431823bd8SMatthew G Knepley PetscFunctionBegin; 370531823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 370631823bd8SMatthew G Knepley PetscValidPointer(pc, 2); 370731823bd8SMatthew G Knepley if (!snes->pc) { 370831823bd8SMatthew G Knepley ierr = SNESCreate(((PetscObject) snes)->comm, &snes->pc);CHKERRQ(ierr); 37094a0c5b0cSMatthew G Knepley ierr = PetscObjectIncrementTabLevel((PetscObject) snes->pc, (PetscObject) snes, 1);CHKERRQ(ierr); 371031823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 371131823bd8SMatthew G Knepley } 371231823bd8SMatthew G Knepley *pc = snes->pc; 371331823bd8SMatthew G Knepley PetscFunctionReturn(0); 371431823bd8SMatthew G Knepley } 371531823bd8SMatthew G Knepley 371669b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3717c6db04a5SJed Brown #include <mex.h> 371869b4f73cSBarry Smith 37198f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 37208f6e6473SBarry Smith 37210807856dSBarry Smith #undef __FUNCT__ 37220807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab" 37230807856dSBarry Smith /* 37240807856dSBarry Smith SNESComputeFunction_Matlab - Calls the function that has been set with 37250807856dSBarry Smith SNESSetFunctionMatlab(). 37260807856dSBarry Smith 37270807856dSBarry Smith Collective on SNES 37280807856dSBarry Smith 37290807856dSBarry Smith Input Parameters: 37300807856dSBarry Smith + snes - the SNES context 37310807856dSBarry Smith - x - input vector 37320807856dSBarry Smith 37330807856dSBarry Smith Output Parameter: 37340807856dSBarry Smith . y - function vector, as set by SNESSetFunction() 37350807856dSBarry Smith 37360807856dSBarry Smith Notes: 37370807856dSBarry Smith SNESComputeFunction() is typically used within nonlinear solvers 37380807856dSBarry Smith implementations, so most users would not generally call this routine 37390807856dSBarry Smith themselves. 37400807856dSBarry Smith 37410807856dSBarry Smith Level: developer 37420807856dSBarry Smith 37430807856dSBarry Smith .keywords: SNES, nonlinear, compute, function 37440807856dSBarry Smith 37450807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 374661b2408cSBarry Smith */ 37477087cfbeSBarry Smith PetscErrorCode SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx) 37480807856dSBarry Smith { 3749e650e774SBarry Smith PetscErrorCode ierr; 37508f6e6473SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 37518f6e6473SBarry Smith int nlhs = 1,nrhs = 5; 37528f6e6473SBarry Smith mxArray *plhs[1],*prhs[5]; 375391621f2eSBarry Smith long long int lx = 0,ly = 0,ls = 0; 3754e650e774SBarry Smith 37550807856dSBarry Smith PetscFunctionBegin; 37560807856dSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 37570807856dSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 37580807856dSBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 37590807856dSBarry Smith PetscCheckSameComm(snes,1,x,2); 37600807856dSBarry Smith PetscCheckSameComm(snes,1,y,3); 37610807856dSBarry Smith 37620807856dSBarry Smith /* call Matlab function in ctx with arguments x and y */ 3763e650e774SBarry Smith 376491621f2eSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3765e650e774SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3766e650e774SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 376791621f2eSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 376891621f2eSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 376991621f2eSBarry Smith prhs[2] = mxCreateDoubleScalar((double)ly); 37708f6e6473SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 37718f6e6473SBarry Smith prhs[4] = sctx->ctx; 3772b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr); 3773e650e774SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3774e650e774SBarry Smith mxDestroyArray(prhs[0]); 3775e650e774SBarry Smith mxDestroyArray(prhs[1]); 3776e650e774SBarry Smith mxDestroyArray(prhs[2]); 37778f6e6473SBarry Smith mxDestroyArray(prhs[3]); 3778e650e774SBarry Smith mxDestroyArray(plhs[0]); 37790807856dSBarry Smith PetscFunctionReturn(0); 37800807856dSBarry Smith } 37810807856dSBarry Smith 37820807856dSBarry Smith 37830807856dSBarry Smith #undef __FUNCT__ 37840807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab" 378561b2408cSBarry Smith /* 37860807856dSBarry Smith SNESSetFunctionMatlab - Sets the function evaluation routine and function 37870807856dSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3788e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 37890807856dSBarry Smith 37900807856dSBarry Smith Logically Collective on SNES 37910807856dSBarry Smith 37920807856dSBarry Smith Input Parameters: 37930807856dSBarry Smith + snes - the SNES context 37940807856dSBarry Smith . r - vector to store function value 37950807856dSBarry Smith - func - function evaluation routine 37960807856dSBarry Smith 37970807856dSBarry Smith Calling sequence of func: 379861b2408cSBarry Smith $ func (SNES snes,Vec x,Vec f,void *ctx); 37990807856dSBarry Smith 38000807856dSBarry Smith 38010807856dSBarry Smith Notes: 38020807856dSBarry Smith The Newton-like methods typically solve linear systems of the form 38030807856dSBarry Smith $ f'(x) x = -f(x), 38040807856dSBarry Smith where f'(x) denotes the Jacobian matrix and f(x) is the function. 38050807856dSBarry Smith 38060807856dSBarry Smith Level: beginner 38070807856dSBarry Smith 38080807856dSBarry Smith .keywords: SNES, nonlinear, set, function 38090807856dSBarry Smith 38100807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 381161b2408cSBarry Smith */ 38127087cfbeSBarry Smith PetscErrorCode SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx) 38130807856dSBarry Smith { 38140807856dSBarry Smith PetscErrorCode ierr; 38158f6e6473SBarry Smith SNESMatlabContext *sctx; 38160807856dSBarry Smith 38170807856dSBarry Smith PetscFunctionBegin; 38188f6e6473SBarry Smith /* currently sctx is memory bleed */ 38198f6e6473SBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 38208f6e6473SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 38218f6e6473SBarry Smith /* 38228f6e6473SBarry Smith This should work, but it doesn't 38238f6e6473SBarry Smith sctx->ctx = ctx; 38248f6e6473SBarry Smith mexMakeArrayPersistent(sctx->ctx); 38258f6e6473SBarry Smith */ 38268f6e6473SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 38278f6e6473SBarry Smith ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr); 38280807856dSBarry Smith PetscFunctionReturn(0); 38290807856dSBarry Smith } 383069b4f73cSBarry Smith 383161b2408cSBarry Smith #undef __FUNCT__ 383261b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab" 383361b2408cSBarry Smith /* 383461b2408cSBarry Smith SNESComputeJacobian_Matlab - Calls the function that has been set with 383561b2408cSBarry Smith SNESSetJacobianMatlab(). 383661b2408cSBarry Smith 383761b2408cSBarry Smith Collective on SNES 383861b2408cSBarry Smith 383961b2408cSBarry Smith Input Parameters: 384061b2408cSBarry Smith + snes - the SNES context 384161b2408cSBarry Smith . x - input vector 384261b2408cSBarry Smith . A, B - the matrices 384361b2408cSBarry Smith - ctx - user context 384461b2408cSBarry Smith 384561b2408cSBarry Smith Output Parameter: 384661b2408cSBarry Smith . flag - structure of the matrix 384761b2408cSBarry Smith 384861b2408cSBarry Smith Level: developer 384961b2408cSBarry Smith 385061b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function 385161b2408cSBarry Smith 385261b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 385361b2408cSBarry Smith @*/ 38547087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx) 385561b2408cSBarry Smith { 385661b2408cSBarry Smith PetscErrorCode ierr; 385761b2408cSBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 385861b2408cSBarry Smith int nlhs = 2,nrhs = 6; 385961b2408cSBarry Smith mxArray *plhs[2],*prhs[6]; 386061b2408cSBarry Smith long long int lx = 0,lA = 0,ls = 0, lB = 0; 386161b2408cSBarry Smith 386261b2408cSBarry Smith PetscFunctionBegin; 386361b2408cSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 386461b2408cSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 386561b2408cSBarry Smith 386661b2408cSBarry Smith /* call Matlab function in ctx with arguments x and y */ 386761b2408cSBarry Smith 386861b2408cSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 386961b2408cSBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 387061b2408cSBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 387161b2408cSBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 387261b2408cSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 387361b2408cSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 387461b2408cSBarry Smith prhs[2] = mxCreateDoubleScalar((double)lA); 387561b2408cSBarry Smith prhs[3] = mxCreateDoubleScalar((double)lB); 387661b2408cSBarry Smith prhs[4] = mxCreateString(sctx->funcname); 387761b2408cSBarry Smith prhs[5] = sctx->ctx; 3878b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr); 387961b2408cSBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 388061b2408cSBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 388161b2408cSBarry Smith mxDestroyArray(prhs[0]); 388261b2408cSBarry Smith mxDestroyArray(prhs[1]); 388361b2408cSBarry Smith mxDestroyArray(prhs[2]); 388461b2408cSBarry Smith mxDestroyArray(prhs[3]); 388561b2408cSBarry Smith mxDestroyArray(prhs[4]); 388661b2408cSBarry Smith mxDestroyArray(plhs[0]); 388761b2408cSBarry Smith mxDestroyArray(plhs[1]); 388861b2408cSBarry Smith PetscFunctionReturn(0); 388961b2408cSBarry Smith } 389061b2408cSBarry Smith 389161b2408cSBarry Smith 389261b2408cSBarry Smith #undef __FUNCT__ 389361b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab" 389461b2408cSBarry Smith /* 389561b2408cSBarry Smith SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 389661b2408cSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3897e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 389861b2408cSBarry Smith 389961b2408cSBarry Smith Logically Collective on SNES 390061b2408cSBarry Smith 390161b2408cSBarry Smith Input Parameters: 390261b2408cSBarry Smith + snes - the SNES context 390361b2408cSBarry Smith . A,B - Jacobian matrices 390461b2408cSBarry Smith . func - function evaluation routine 390561b2408cSBarry Smith - ctx - user context 390661b2408cSBarry Smith 390761b2408cSBarry Smith Calling sequence of func: 390861b2408cSBarry Smith $ flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx); 390961b2408cSBarry Smith 391061b2408cSBarry Smith 391161b2408cSBarry Smith Level: developer 391261b2408cSBarry Smith 391361b2408cSBarry Smith .keywords: SNES, nonlinear, set, function 391461b2408cSBarry Smith 391561b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 391661b2408cSBarry Smith */ 39177087cfbeSBarry Smith PetscErrorCode SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx) 391861b2408cSBarry Smith { 391961b2408cSBarry Smith PetscErrorCode ierr; 392061b2408cSBarry Smith SNESMatlabContext *sctx; 392161b2408cSBarry Smith 392261b2408cSBarry Smith PetscFunctionBegin; 392361b2408cSBarry Smith /* currently sctx is memory bleed */ 392461b2408cSBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 392561b2408cSBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 392661b2408cSBarry Smith /* 392761b2408cSBarry Smith This should work, but it doesn't 392861b2408cSBarry Smith sctx->ctx = ctx; 392961b2408cSBarry Smith mexMakeArrayPersistent(sctx->ctx); 393061b2408cSBarry Smith */ 393161b2408cSBarry Smith sctx->ctx = mxDuplicateArray(ctx); 393261b2408cSBarry Smith ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 393361b2408cSBarry Smith PetscFunctionReturn(0); 393461b2408cSBarry Smith } 393569b4f73cSBarry Smith 3936f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3937f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab" 3938f9eb7ae2SShri Abhyankar /* 3939f9eb7ae2SShri Abhyankar SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab(). 3940f9eb7ae2SShri Abhyankar 3941f9eb7ae2SShri Abhyankar Collective on SNES 3942f9eb7ae2SShri Abhyankar 3943f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction() 3944f9eb7ae2SShri Abhyankar @*/ 39457087cfbeSBarry Smith PetscErrorCode SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx) 3946f9eb7ae2SShri Abhyankar { 3947f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 394848f37e70SShri Abhyankar SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 3949f9eb7ae2SShri Abhyankar int nlhs = 1,nrhs = 6; 3950f9eb7ae2SShri Abhyankar mxArray *plhs[1],*prhs[6]; 3951f9eb7ae2SShri Abhyankar long long int lx = 0,ls = 0; 3952f9eb7ae2SShri Abhyankar Vec x=snes->vec_sol; 3953f9eb7ae2SShri Abhyankar 3954f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3955f9eb7ae2SShri Abhyankar PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3956f9eb7ae2SShri Abhyankar 3957f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3958f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3959f9eb7ae2SShri Abhyankar prhs[0] = mxCreateDoubleScalar((double)ls); 3960f9eb7ae2SShri Abhyankar prhs[1] = mxCreateDoubleScalar((double)it); 3961f9eb7ae2SShri Abhyankar prhs[2] = mxCreateDoubleScalar((double)fnorm); 3962f9eb7ae2SShri Abhyankar prhs[3] = mxCreateDoubleScalar((double)lx); 3963f9eb7ae2SShri Abhyankar prhs[4] = mxCreateString(sctx->funcname); 3964f9eb7ae2SShri Abhyankar prhs[5] = sctx->ctx; 3965f9eb7ae2SShri Abhyankar ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr); 3966f9eb7ae2SShri Abhyankar ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3967f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[0]); 3968f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[1]); 3969f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[2]); 3970f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[3]); 3971f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[4]); 3972f9eb7ae2SShri Abhyankar mxDestroyArray(plhs[0]); 3973f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 3974f9eb7ae2SShri Abhyankar } 3975f9eb7ae2SShri Abhyankar 3976f9eb7ae2SShri Abhyankar 3977f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3978f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab" 3979f9eb7ae2SShri Abhyankar /* 3980e3c5b3baSBarry Smith SNESMonitorSetMatlab - Sets the monitor function from MATLAB 3981f9eb7ae2SShri Abhyankar 3982f9eb7ae2SShri Abhyankar Level: developer 3983f9eb7ae2SShri Abhyankar 3984f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function 3985f9eb7ae2SShri Abhyankar 3986f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 3987f9eb7ae2SShri Abhyankar */ 39887087cfbeSBarry Smith PetscErrorCode SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx) 3989f9eb7ae2SShri Abhyankar { 3990f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 3991f9eb7ae2SShri Abhyankar SNESMatlabContext *sctx; 3992f9eb7ae2SShri Abhyankar 3993f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3994f9eb7ae2SShri Abhyankar /* currently sctx is memory bleed */ 3995f9eb7ae2SShri Abhyankar ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 3996f9eb7ae2SShri Abhyankar ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3997f9eb7ae2SShri Abhyankar /* 3998f9eb7ae2SShri Abhyankar This should work, but it doesn't 3999f9eb7ae2SShri Abhyankar sctx->ctx = ctx; 4000f9eb7ae2SShri Abhyankar mexMakeArrayPersistent(sctx->ctx); 4001f9eb7ae2SShri Abhyankar */ 4002f9eb7ae2SShri Abhyankar sctx->ctx = mxDuplicateArray(ctx); 4003f9eb7ae2SShri Abhyankar ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 4004f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 4005f9eb7ae2SShri Abhyankar } 4006f9eb7ae2SShri Abhyankar 400769b4f73cSBarry Smith #endif 4008