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; 9701cf23dSPeter Brune PetscLogEvent SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval, SNES_GSEval; 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; 57dd568438SSatish Balay 58e113a28aSBarry Smith PetscFunctionReturn(0); 59e113a28aSBarry Smith } 60e113a28aSBarry Smith 61e113a28aSBarry Smith #undef __FUNCT__ 62e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged" 63e113a28aSBarry Smith /*@ 64e113a28aSBarry Smith SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge? 65e113a28aSBarry Smith 66e113a28aSBarry Smith Not Collective 67e113a28aSBarry Smith 68e113a28aSBarry Smith Input Parameter: 69e113a28aSBarry Smith . snes - iterative context obtained from SNESCreate() 70e113a28aSBarry Smith 71e113a28aSBarry Smith Output Parameter: 72e113a28aSBarry Smith . flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE 73e113a28aSBarry Smith 74e113a28aSBarry Smith Level: intermediate 75e113a28aSBarry Smith 76e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero 77e113a28aSBarry Smith 78e113a28aSBarry Smith .seealso: SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged() 79e113a28aSBarry Smith @*/ 807087cfbeSBarry Smith PetscErrorCode SNESGetErrorIfNotConverged(SNES snes,PetscBool *flag) 81e113a28aSBarry Smith { 82e113a28aSBarry Smith PetscFunctionBegin; 83e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 84e113a28aSBarry Smith PetscValidPointer(flag,2); 85e113a28aSBarry Smith *flag = snes->errorifnotconverged; 86e113a28aSBarry Smith PetscFunctionReturn(0); 87e113a28aSBarry Smith } 88e113a28aSBarry Smith 89e113a28aSBarry Smith #undef __FUNCT__ 904936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError" 91e725d27bSBarry Smith /*@ 924936397dSBarry Smith SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not 934936397dSBarry Smith in the functions domain. For example, negative pressure. 944936397dSBarry Smith 953f9fe445SBarry Smith Logically Collective on SNES 964936397dSBarry Smith 974936397dSBarry Smith Input Parameters: 984936397dSBarry Smith . SNES - the SNES context 994936397dSBarry Smith 10028529972SSatish Balay Level: advanced 1014936397dSBarry Smith 1024936397dSBarry Smith .keywords: SNES, view 1034936397dSBarry Smith 1044936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction() 1054936397dSBarry Smith @*/ 1067087cfbeSBarry Smith PetscErrorCode SNESSetFunctionDomainError(SNES snes) 1074936397dSBarry Smith { 1084936397dSBarry Smith PetscFunctionBegin; 1090700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1104936397dSBarry Smith snes->domainerror = PETSC_TRUE; 1114936397dSBarry Smith PetscFunctionReturn(0); 1124936397dSBarry Smith } 1134936397dSBarry Smith 1144936397dSBarry Smith #undef __FUNCT__ 1154a2ae208SSatish Balay #define __FUNCT__ "SNESView" 1167e2c5f70SBarry Smith /*@C 1179b94acceSBarry Smith SNESView - Prints the SNES data structure. 1189b94acceSBarry Smith 1194c49b128SBarry Smith Collective on SNES 120fee21e36SBarry Smith 121c7afd0dbSLois Curfman McInnes Input Parameters: 122c7afd0dbSLois Curfman McInnes + SNES - the SNES context 123c7afd0dbSLois Curfman McInnes - viewer - visualization context 124c7afd0dbSLois Curfman McInnes 1259b94acceSBarry Smith Options Database Key: 126c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 1279b94acceSBarry Smith 1289b94acceSBarry Smith Notes: 1299b94acceSBarry Smith The available visualization contexts include 130b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 131b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 132c8a8ba5cSLois Curfman McInnes output where only the first processor opens 133c8a8ba5cSLois Curfman McInnes the file. All other processors send their 134c8a8ba5cSLois Curfman McInnes data to the first processor to print. 1359b94acceSBarry Smith 1363e081fefSLois Curfman McInnes The user can open an alternative visualization context with 137b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1389b94acceSBarry Smith 13936851e7fSLois Curfman McInnes Level: beginner 14036851e7fSLois Curfman McInnes 1419b94acceSBarry Smith .keywords: SNES, view 1429b94acceSBarry Smith 143b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1449b94acceSBarry Smith @*/ 1457087cfbeSBarry Smith PetscErrorCode SNESView(SNES snes,PetscViewer viewer) 1469b94acceSBarry Smith { 147fa9f3622SBarry Smith SNESKSPEW *kctx; 148dfbe8321SBarry Smith PetscErrorCode ierr; 14994b7f48cSBarry Smith KSP ksp; 150ace3abfcSBarry Smith PetscBool iascii,isstring; 1519b94acceSBarry Smith 1523a40ed3dSBarry Smith PetscFunctionBegin; 1530700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1543050cee2SBarry Smith if (!viewer) { 1557adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr); 1563050cee2SBarry Smith } 1570700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 158c9780b6fSBarry Smith PetscCheckSameComm(snes,1,viewer,2); 15974679c65SBarry Smith 1602692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1612692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 16232077d6dSBarry Smith if (iascii) { 163317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer,"SNES Object");CHKERRQ(ierr); 164e7788613SBarry Smith if (snes->ops->view) { 165b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 166e7788613SBarry Smith ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 167b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1680ef38995SBarry Smith } 16977431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 170a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, solution=%G\n", 17170441072SBarry Smith snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr); 17277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr); 17377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr); 1749b94acceSBarry Smith if (snes->ksp_ewconv) { 175fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 1769b94acceSBarry Smith if (kctx) { 17777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr); 178a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 179a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 1809b94acceSBarry Smith } 1819b94acceSBarry Smith } 182eb1f6c34SBarry Smith if (snes->lagpreconditioner == -1) { 183eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is never rebuilt\n");CHKERRQ(ierr); 184eb1f6c34SBarry Smith } else if (snes->lagpreconditioner > 1) { 185eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr); 186eb1f6c34SBarry Smith } 187eb1f6c34SBarry Smith if (snes->lagjacobian == -1) { 188eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is never rebuilt\n");CHKERRQ(ierr); 189eb1f6c34SBarry Smith } else if (snes->lagjacobian > 1) { 19042f4f86dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is rebuilt every %D SNES iterations\n",snes->lagjacobian);CHKERRQ(ierr); 191eb1f6c34SBarry Smith } 1920f5bd95cSBarry Smith } else if (isstring) { 193317d6ea6SBarry Smith const char *type; 194454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 195b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 19619bcc07fSBarry Smith } 19742f4f86dSBarry Smith if (snes->pc && snes->usespc) { 1984a0c5b0cSMatthew G Knepley ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1994a0c5b0cSMatthew G Knepley ierr = SNESView(snes->pc, viewer);CHKERRQ(ierr); 2004a0c5b0cSMatthew G Knepley ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2014a0c5b0cSMatthew G Knepley } 2022c155ee1SBarry Smith if (snes->usesksp) { 2032c155ee1SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 204b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 20594b7f48cSBarry Smith ierr = KSPView(ksp,viewer);CHKERRQ(ierr); 206b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2072c155ee1SBarry Smith } 2083a40ed3dSBarry Smith PetscFunctionReturn(0); 2099b94acceSBarry Smith } 2109b94acceSBarry Smith 21176b2cf59SMatthew Knepley /* 21276b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 21376b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 21476b2cf59SMatthew Knepley */ 21576b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 216a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 2176849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 21876b2cf59SMatthew Knepley 219e74ef692SMatthew Knepley #undef __FUNCT__ 220e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker" 221ac226902SBarry Smith /*@C 22276b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 22376b2cf59SMatthew Knepley 22476b2cf59SMatthew Knepley Not Collective 22576b2cf59SMatthew Knepley 22676b2cf59SMatthew Knepley Input Parameter: 22776b2cf59SMatthew Knepley . snescheck - function that checks for options 22876b2cf59SMatthew Knepley 22976b2cf59SMatthew Knepley Level: developer 23076b2cf59SMatthew Knepley 23176b2cf59SMatthew Knepley .seealso: SNESSetFromOptions() 23276b2cf59SMatthew Knepley @*/ 2337087cfbeSBarry Smith PetscErrorCode SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 23476b2cf59SMatthew Knepley { 23576b2cf59SMatthew Knepley PetscFunctionBegin; 23676b2cf59SMatthew Knepley if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 237e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS); 23876b2cf59SMatthew Knepley } 23976b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 24076b2cf59SMatthew Knepley PetscFunctionReturn(0); 24176b2cf59SMatthew Knepley } 24276b2cf59SMatthew Knepley 2437087cfbeSBarry Smith extern PetscErrorCode SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 244aa3661deSLisandro Dalcin 245aa3661deSLisandro Dalcin #undef __FUNCT__ 246aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private" 247ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool hasOperator, PetscInt version) 248aa3661deSLisandro Dalcin { 249aa3661deSLisandro Dalcin Mat J; 250aa3661deSLisandro Dalcin KSP ksp; 251aa3661deSLisandro Dalcin PC pc; 252ace3abfcSBarry Smith PetscBool match; 253aa3661deSLisandro Dalcin PetscErrorCode ierr; 254aa3661deSLisandro Dalcin 255aa3661deSLisandro Dalcin PetscFunctionBegin; 2560700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 257aa3661deSLisandro Dalcin 25898613b67SLisandro Dalcin if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) { 25998613b67SLisandro Dalcin Mat A = snes->jacobian, B = snes->jacobian_pre; 26098613b67SLisandro Dalcin ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr); 26198613b67SLisandro Dalcin } 26298613b67SLisandro Dalcin 263aa3661deSLisandro Dalcin if (version == 1) { 264aa3661deSLisandro Dalcin ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 26598613b67SLisandro Dalcin ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 2669c6ac3b3SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 267aa3661deSLisandro Dalcin } else if (version == 2) { 268e32f2f54SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first"); 26982a7e548SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) 270aa3661deSLisandro Dalcin ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr); 271aa3661deSLisandro Dalcin #else 272e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)"); 273aa3661deSLisandro Dalcin #endif 274a8248277SBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2"); 275aa3661deSLisandro Dalcin 276aa3661deSLisandro Dalcin ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr); 277d3462f78SMatthew Knepley if (hasOperator) { 278aa3661deSLisandro Dalcin /* This version replaces the user provided Jacobian matrix with a 279aa3661deSLisandro Dalcin matrix-free version but still employs the user-provided preconditioner matrix. */ 280aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 281aa3661deSLisandro Dalcin } else { 282aa3661deSLisandro Dalcin /* This version replaces both the user-provided Jacobian and the user- 283aa3661deSLisandro Dalcin provided preconditioner matrix with the default matrix free version. */ 284aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 285aa3661deSLisandro Dalcin /* Force no preconditioner */ 286aa3661deSLisandro Dalcin ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 287aa3661deSLisandro Dalcin ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 288aa3661deSLisandro Dalcin ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr); 289aa3661deSLisandro Dalcin if (!match) { 290aa3661deSLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 291aa3661deSLisandro Dalcin ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 292aa3661deSLisandro Dalcin } 293aa3661deSLisandro Dalcin } 2946bf464f9SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 295aa3661deSLisandro Dalcin PetscFunctionReturn(0); 296aa3661deSLisandro Dalcin } 297aa3661deSLisandro Dalcin 2984a2ae208SSatish Balay #undef __FUNCT__ 2994a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions" 3009b94acceSBarry Smith /*@ 30194b7f48cSBarry Smith SNESSetFromOptions - Sets various SNES and KSP parameters from user options. 3029b94acceSBarry Smith 303c7afd0dbSLois Curfman McInnes Collective on SNES 304c7afd0dbSLois Curfman McInnes 3059b94acceSBarry Smith Input Parameter: 3069b94acceSBarry Smith . snes - the SNES context 3079b94acceSBarry Smith 30836851e7fSLois Curfman McInnes Options Database Keys: 309ea630c6eSPeter Brune + -snes_type <type> - ls, tr, ngmres, ncg, richardson, qn, vi, fas 31082738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 31182738288SBarry Smith of the change in the solution between steps 31270441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 313b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 314b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 315b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 3164839bfe8SBarry Smith . -snes_max_fail <max_fail> - maximum number of line search failures allowed before stopping, default is none 317ddf469c8SBarry Smith . -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops 318a8054027SBarry Smith . -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild) 319e35cf81dSBarry Smith . -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild) 320b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 3212492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 32282738288SBarry Smith solver; hence iterations will continue until max_it 3231fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 32482738288SBarry Smith of convergence test 325e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 326e8105e01SRichard Katz filename given prints to stdout 327a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 328a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 329a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 330a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 331e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 3325968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 333fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 33482738288SBarry Smith 33582738288SBarry Smith Options Database for Eisenstat-Walker method: 336fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 3374b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 33836851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 33936851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 34036851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 34136851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 34236851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 34336851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 34482738288SBarry Smith 34511ca99fdSLois Curfman McInnes Notes: 34611ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 3470598bfebSBarry Smith the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A>. 34883e2fdc7SBarry Smith 34936851e7fSLois Curfman McInnes Level: beginner 35036851e7fSLois Curfman McInnes 3519b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 3529b94acceSBarry Smith 35369ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 3549b94acceSBarry Smith @*/ 3557087cfbeSBarry Smith PetscErrorCode SNESSetFromOptions(SNES snes) 3569b94acceSBarry Smith { 357ea630c6eSPeter Brune PetscBool flg,set,mf,mf_operator,pcset; 358efd51863SBarry Smith PetscInt i,indx,lag,mf_version,grids; 359aa3661deSLisandro Dalcin MatStructure matflag; 36085385478SLisandro Dalcin const char *deft = SNESLS; 36185385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 36285385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 363e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 36451e86f29SPeter Brune const char *optionsprefix; 365649052a6SBarry Smith PetscViewer monviewer; 36685385478SLisandro Dalcin PetscErrorCode ierr; 3679b94acceSBarry Smith 3683a40ed3dSBarry Smith PetscFunctionBegin; 3690700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 370ca161407SBarry Smith 371186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 3723194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)snes);CHKERRQ(ierr); 3737adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 374b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 375d64ed03dSBarry Smith if (flg) { 376186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 3777adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 378186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 379d64ed03dSBarry Smith } 38090d69ab7SBarry Smith /* not used here, but called so will go into help messaage */ 381909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 38293c39befSBarry Smith 38357034d6fSHong Zhang ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 38457034d6fSHong Zhang ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 385186905e3SBarry Smith 38657034d6fSHong Zhang ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 387b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 388b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 38950ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 390ddf469c8SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr); 391acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr); 39285385478SLisandro Dalcin 393a8054027SBarry Smith ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr); 394a8054027SBarry Smith if (flg) { 395a8054027SBarry Smith ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr); 396a8054027SBarry Smith } 397e35cf81dSBarry Smith ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr); 398e35cf81dSBarry Smith if (flg) { 399e35cf81dSBarry Smith ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr); 400e35cf81dSBarry Smith } 401efd51863SBarry Smith ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr); 402efd51863SBarry Smith if (flg) { 403efd51863SBarry Smith ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr); 404efd51863SBarry Smith } 405a8054027SBarry Smith 40685385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 40785385478SLisandro Dalcin if (flg) { 40885385478SLisandro Dalcin switch (indx) { 4097f7931b9SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 4107f7931b9SBarry Smith case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 41185385478SLisandro Dalcin } 41285385478SLisandro Dalcin } 41385385478SLisandro Dalcin 414acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr); 415186905e3SBarry Smith 41685385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 41785385478SLisandro Dalcin 418acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 419186905e3SBarry Smith 420fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 421fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 422fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 423fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 424fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 425fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 426fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 427186905e3SBarry Smith 42890d69ab7SBarry Smith flg = PETSC_FALSE; 429acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 430a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 431eabae89aSBarry Smith 432a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 433e8105e01SRichard Katz if (flg) { 434649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr); 435649052a6SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 436e8105e01SRichard Katz } 437eabae89aSBarry Smith 438b271bb04SBarry Smith ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 439b271bb04SBarry Smith if (flg) { 440b271bb04SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr); 441b271bb04SBarry Smith } 442b271bb04SBarry Smith 443a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 444eabae89aSBarry Smith if (flg) { 445649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr); 446f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 447e8105e01SRichard Katz } 448eabae89aSBarry Smith 449a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 450eabae89aSBarry Smith if (flg) { 451649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr); 452649052a6SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 453eabae89aSBarry Smith } 454eabae89aSBarry Smith 4555180491cSLisandro Dalcin ierr = PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 4565180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)snes,monfilename);CHKERRQ(ierr);} 4575180491cSLisandro Dalcin 45890d69ab7SBarry Smith flg = PETSC_FALSE; 459acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 460a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 46190d69ab7SBarry Smith flg = PETSC_FALSE; 462acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 463a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 46490d69ab7SBarry Smith flg = PETSC_FALSE; 465acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 466a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 46790d69ab7SBarry Smith flg = PETSC_FALSE; 468acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 469a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 47090d69ab7SBarry Smith flg = PETSC_FALSE; 471acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 472b271bb04SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 473e24b481bSBarry Smith 47490d69ab7SBarry Smith flg = PETSC_FALSE; 475acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 4764b27c08aSLois Curfman McInnes if (flg) { 477186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 478ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 4799b94acceSBarry Smith } 480639f9d9dSBarry Smith 481aa3661deSLisandro Dalcin mf = mf_operator = PETSC_FALSE; 482aa3661deSLisandro Dalcin flg = PETSC_FALSE; 483acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr); 484a8248277SBarry Smith if (flg && mf_operator) { 485a8248277SBarry Smith snes->mf_operator = PETSC_TRUE; 486a8248277SBarry Smith mf = PETSC_TRUE; 487a8248277SBarry Smith } 488aa3661deSLisandro Dalcin flg = PETSC_FALSE; 489acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr); 490aa3661deSLisandro Dalcin if (!flg && mf_operator) mf = PETSC_TRUE; 491aa3661deSLisandro Dalcin mf_version = 1; 492aa3661deSLisandro Dalcin ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr); 493aa3661deSLisandro Dalcin 494d28543b3SPeter Brune 49589b92e6fSPeter Brune /* GS Options */ 49689b92e6fSPeter Brune ierr = PetscOptionsInt("-snes_gs_sweeps","Number of sweeps of GS to apply","SNESComputeGS",snes->gssweeps,&snes->gssweeps,PETSC_NULL);CHKERRQ(ierr); 49789b92e6fSPeter Brune 498ea630c6eSPeter Brune /* line search options */ 499ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_alpha","Constant function norm must decrease by","None",snes->ls_alpha,&snes->ls_alpha,0);CHKERRQ(ierr); 500ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_maxstep","Step must be less than","None",snes->maxstep,&snes->maxstep,0);CHKERRQ(ierr); 501ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_steptol","Minimum lambda allowed","None",snes->steptol,&snes->steptol,0);CHKERRQ(ierr); 502ea630c6eSPeter Brune ierr = PetscOptionsReal("-snes_ls_damping","Damping parameter","SNES",snes->damping,&snes->damping,&flg);CHKERRQ(ierr); 503af60355fSPeter Brune ierr = PetscOptionsInt("-snes_ls_it" ,"Line search iterations","SNES",snes->ls_its,&snes->ls_its,&flg);CHKERRQ(ierr); 504ea630c6eSPeter Brune ierr = PetscOptionsBool("-snes_ls_monitor","Print progress of line searches","SNESLineSearchSetMonitor",snes->ls_monitor ? PETSC_TRUE : PETSC_FALSE,&flg,&set);CHKERRQ(ierr); 505ea630c6eSPeter Brune if (set) {ierr = SNESLineSearchSetMonitor(snes,flg);CHKERRQ(ierr);} 50615f5eeeaSPeter Brune ierr = PetscOptionsEnum("-snes_ls","Line search used","SNESLineSearchSet",SNESLineSearchTypes,(PetscEnum)snes->ls_type,(PetscEnum*)&indx,&flg);CHKERRQ(ierr); 50715f5eeeaSPeter Brune if (flg) { 508ea630c6eSPeter Brune ierr = SNESLineSearchSetType(snes,(SNESLineSearchType)indx);CHKERRQ(ierr); 50915f5eeeaSPeter Brune } 5108e3fc8c0SJed Brown flg = snes->ops->precheckstep == SNESLineSearchPreCheckPicard ? PETSC_TRUE : PETSC_FALSE; 5118e3fc8c0SJed Brown ierr = PetscOptionsBool("-snes_ls_precheck_picard","Use a correction that sometimes improves convergence of Picard iteration","SNESLineSearchPreCheckPicard",flg,&flg,&set);CHKERRQ(ierr); 5128e3fc8c0SJed Brown if (set) { 5138e3fc8c0SJed Brown if (flg) { 5148e3fc8c0SJed Brown snes->precheck_picard_angle = 10.; /* correction only active if angle is less than 10 degrees */ 5158e3fc8c0SJed 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); 5168e3fc8c0SJed Brown ierr = SNESLineSearchSetPreCheck(snes,SNESLineSearchPreCheckPicard,&snes->precheck_picard_angle);CHKERRQ(ierr); 5178e3fc8c0SJed Brown } else { 5188e3fc8c0SJed Brown ierr = SNESLineSearchSetPreCheck(snes,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 5198e3fc8c0SJed Brown } 5208e3fc8c0SJed Brown } 5218e3fc8c0SJed Brown 52276b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 52376b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 52476b2cf59SMatthew Knepley } 52576b2cf59SMatthew Knepley 526e7788613SBarry Smith if (snes->ops->setfromoptions) { 527e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 528639f9d9dSBarry Smith } 5295d973c19SBarry Smith 5305d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 5315d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr); 532b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 5334bbc92c1SBarry Smith 534aa3661deSLisandro Dalcin if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); } 5351cee3971SBarry Smith 5361cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 537aa3661deSLisandro Dalcin ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag); 538aa3661deSLisandro Dalcin ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr); 53985385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 54093993e2dSLois Curfman McInnes 54151e86f29SPeter Brune /* if someone has set the SNES PC type, create it. */ 54251e86f29SPeter Brune ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr); 54351e86f29SPeter Brune ierr = PetscOptionsHasName(optionsprefix, "-npc_snes_type", &pcset);CHKERRQ(ierr); 54451e86f29SPeter Brune if (pcset && (!snes->pc)) { 54551e86f29SPeter Brune ierr = SNESGetPC(snes, &snes->pc);CHKERRQ(ierr); 54651e86f29SPeter Brune } 5474a0c5b0cSMatthew G Knepley if (snes->pc) { 548fde0ff24SPeter Brune ierr = SNESSetOptionsPrefix(snes->pc, optionsprefix);CHKERRQ(ierr); 549fde0ff24SPeter Brune ierr = SNESAppendOptionsPrefix(snes->pc, "npc_");CHKERRQ(ierr); 5504a0c5b0cSMatthew G Knepley ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 551646217ecSPeter Brune ierr = SNESSetGS(snes->pc, snes->ops->computegs, snes->gsP);CHKERRQ(ierr); 5524a0c5b0cSMatthew G Knepley /* Should we make a duplicate vector and matrix? Leave the DM to make it? */ 5534a0c5b0cSMatthew G Knepley ierr = SNESSetFunction(snes->pc, snes->vec_func, snes->ops->computefunction, snes->funP);CHKERRQ(ierr); 5544a0c5b0cSMatthew G Knepley ierr = SNESSetJacobian(snes->pc, snes->jacobian, snes->jacobian_pre, snes->ops->computejacobian, snes->jacP);CHKERRQ(ierr); 5554a0c5b0cSMatthew G Knepley ierr = SNESSetFromOptions(snes->pc);CHKERRQ(ierr); 5564a0c5b0cSMatthew G Knepley } 5573a40ed3dSBarry Smith PetscFunctionReturn(0); 5589b94acceSBarry Smith } 5599b94acceSBarry Smith 560d25893d9SBarry Smith #undef __FUNCT__ 561d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext" 562d25893d9SBarry Smith /*@ 563d25893d9SBarry Smith SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for 564d25893d9SBarry Smith the nonlinear solvers. 565d25893d9SBarry Smith 566d25893d9SBarry Smith Logically Collective on SNES 567d25893d9SBarry Smith 568d25893d9SBarry Smith Input Parameters: 569d25893d9SBarry Smith + snes - the SNES context 570d25893d9SBarry Smith . compute - function to compute the context 571d25893d9SBarry Smith - destroy - function to destroy the context 572d25893d9SBarry Smith 573d25893d9SBarry Smith Level: intermediate 574d25893d9SBarry Smith 575d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context 576d25893d9SBarry Smith 577d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext() 578d25893d9SBarry Smith @*/ 579d25893d9SBarry Smith PetscErrorCode SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**)) 580d25893d9SBarry Smith { 581d25893d9SBarry Smith PetscFunctionBegin; 582d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 583d25893d9SBarry Smith snes->ops->usercompute = compute; 584d25893d9SBarry Smith snes->ops->userdestroy = destroy; 585d25893d9SBarry Smith PetscFunctionReturn(0); 586d25893d9SBarry Smith } 587a847f771SSatish Balay 5884a2ae208SSatish Balay #undef __FUNCT__ 5894a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 590b07ff414SBarry Smith /*@ 5919b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 5929b94acceSBarry Smith the nonlinear solvers. 5939b94acceSBarry Smith 5943f9fe445SBarry Smith Logically Collective on SNES 595fee21e36SBarry Smith 596c7afd0dbSLois Curfman McInnes Input Parameters: 597c7afd0dbSLois Curfman McInnes + snes - the SNES context 598c7afd0dbSLois Curfman McInnes - usrP - optional user context 599c7afd0dbSLois Curfman McInnes 60036851e7fSLois Curfman McInnes Level: intermediate 60136851e7fSLois Curfman McInnes 6029b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 6039b94acceSBarry Smith 604d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext() 6059b94acceSBarry Smith @*/ 6067087cfbeSBarry Smith PetscErrorCode SNESSetApplicationContext(SNES snes,void *usrP) 6079b94acceSBarry Smith { 6081b2093e4SBarry Smith PetscErrorCode ierr; 609b07ff414SBarry Smith KSP ksp; 6101b2093e4SBarry Smith 6113a40ed3dSBarry Smith PetscFunctionBegin; 6120700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 613b07ff414SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 614b07ff414SBarry Smith ierr = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr); 6159b94acceSBarry Smith snes->user = usrP; 6163a40ed3dSBarry Smith PetscFunctionReturn(0); 6179b94acceSBarry Smith } 61874679c65SBarry Smith 6194a2ae208SSatish Balay #undef __FUNCT__ 6204a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 621b07ff414SBarry Smith /*@ 6229b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 6239b94acceSBarry Smith nonlinear solvers. 6249b94acceSBarry Smith 625c7afd0dbSLois Curfman McInnes Not Collective 626c7afd0dbSLois Curfman McInnes 6279b94acceSBarry Smith Input Parameter: 6289b94acceSBarry Smith . snes - SNES context 6299b94acceSBarry Smith 6309b94acceSBarry Smith Output Parameter: 6319b94acceSBarry Smith . usrP - user context 6329b94acceSBarry Smith 63336851e7fSLois Curfman McInnes Level: intermediate 63436851e7fSLois Curfman McInnes 6359b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 6369b94acceSBarry Smith 6379b94acceSBarry Smith .seealso: SNESSetApplicationContext() 6389b94acceSBarry Smith @*/ 639e71120c6SJed Brown PetscErrorCode SNESGetApplicationContext(SNES snes,void *usrP) 6409b94acceSBarry Smith { 6413a40ed3dSBarry Smith PetscFunctionBegin; 6420700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 643e71120c6SJed Brown *(void**)usrP = snes->user; 6443a40ed3dSBarry Smith PetscFunctionReturn(0); 6459b94acceSBarry Smith } 64674679c65SBarry Smith 6474a2ae208SSatish Balay #undef __FUNCT__ 6484a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 6499b94acceSBarry Smith /*@ 650c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 651c8228a4eSBarry Smith at this time. 6529b94acceSBarry Smith 653c7afd0dbSLois Curfman McInnes Not Collective 654c7afd0dbSLois Curfman McInnes 6559b94acceSBarry Smith Input Parameter: 6569b94acceSBarry Smith . snes - SNES context 6579b94acceSBarry Smith 6589b94acceSBarry Smith Output Parameter: 6599b94acceSBarry Smith . iter - iteration number 6609b94acceSBarry Smith 661c8228a4eSBarry Smith Notes: 662c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 663c8228a4eSBarry Smith 664c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 66508405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 66608405cd6SLois Curfman McInnes .vb 66708405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 66808405cd6SLois Curfman McInnes if (!(it % 2)) { 66908405cd6SLois Curfman McInnes [compute Jacobian here] 67008405cd6SLois Curfman McInnes } 67108405cd6SLois Curfman McInnes .ve 672c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 67308405cd6SLois Curfman McInnes recomputed every second SNES iteration. 674c8228a4eSBarry Smith 67536851e7fSLois Curfman McInnes Level: intermediate 67636851e7fSLois Curfman McInnes 6772b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 6782b668275SBarry Smith 679b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 6809b94acceSBarry Smith @*/ 6817087cfbeSBarry Smith PetscErrorCode SNESGetIterationNumber(SNES snes,PetscInt* iter) 6829b94acceSBarry Smith { 6833a40ed3dSBarry Smith PetscFunctionBegin; 6840700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6854482741eSBarry Smith PetscValidIntPointer(iter,2); 6869b94acceSBarry Smith *iter = snes->iter; 6873a40ed3dSBarry Smith PetscFunctionReturn(0); 6889b94acceSBarry Smith } 68974679c65SBarry Smith 6904a2ae208SSatish Balay #undef __FUNCT__ 691360c497dSPeter Brune #define __FUNCT__ "SNESSetIterationNumber" 692360c497dSPeter Brune /*@ 693360c497dSPeter Brune SNESSetIterationNumber - Sets the current iteration number. 694360c497dSPeter Brune 695360c497dSPeter Brune Not Collective 696360c497dSPeter Brune 697360c497dSPeter Brune Input Parameter: 698360c497dSPeter Brune . snes - SNES context 699360c497dSPeter Brune . iter - iteration number 700360c497dSPeter Brune 701360c497dSPeter Brune Level: developer 702360c497dSPeter Brune 703360c497dSPeter Brune .keywords: SNES, nonlinear, set, iteration, number, 704360c497dSPeter Brune 705360c497dSPeter Brune .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 706360c497dSPeter Brune @*/ 707360c497dSPeter Brune PetscErrorCode SNESSetIterationNumber(SNES snes,PetscInt iter) 708360c497dSPeter Brune { 709360c497dSPeter Brune PetscErrorCode ierr; 710360c497dSPeter Brune 711360c497dSPeter Brune PetscFunctionBegin; 712360c497dSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 713360c497dSPeter Brune ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 714360c497dSPeter Brune snes->iter = iter; 715360c497dSPeter Brune ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 716360c497dSPeter Brune PetscFunctionReturn(0); 717360c497dSPeter Brune } 718360c497dSPeter Brune 719360c497dSPeter Brune #undef __FUNCT__ 7204a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 7219b94acceSBarry Smith /*@ 7229b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 7239b94acceSBarry Smith with SNESSSetFunction(). 7249b94acceSBarry Smith 725c7afd0dbSLois Curfman McInnes Collective on SNES 726c7afd0dbSLois Curfman McInnes 7279b94acceSBarry Smith Input Parameter: 7289b94acceSBarry Smith . snes - SNES context 7299b94acceSBarry Smith 7309b94acceSBarry Smith Output Parameter: 7319b94acceSBarry Smith . fnorm - 2-norm of function 7329b94acceSBarry Smith 73336851e7fSLois Curfman McInnes Level: intermediate 73436851e7fSLois Curfman McInnes 7359b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 736a86d99e1SLois Curfman McInnes 737b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 7389b94acceSBarry Smith @*/ 7397087cfbeSBarry Smith PetscErrorCode SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 7409b94acceSBarry Smith { 7413a40ed3dSBarry Smith PetscFunctionBegin; 7420700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7434482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 7449b94acceSBarry Smith *fnorm = snes->norm; 7453a40ed3dSBarry Smith PetscFunctionReturn(0); 7469b94acceSBarry Smith } 74774679c65SBarry Smith 748360c497dSPeter Brune 749360c497dSPeter Brune #undef __FUNCT__ 750360c497dSPeter Brune #define __FUNCT__ "SNESSetFunctionNorm" 751360c497dSPeter Brune /*@ 752360c497dSPeter Brune SNESSetFunctionNorm - Sets the 2-norm of the current function computed using VecNorm(). 753360c497dSPeter Brune 754360c497dSPeter Brune Collective on SNES 755360c497dSPeter Brune 756360c497dSPeter Brune Input Parameter: 757360c497dSPeter Brune . snes - SNES context 758360c497dSPeter Brune . fnorm - 2-norm of function 759360c497dSPeter Brune 760360c497dSPeter Brune Level: developer 761360c497dSPeter Brune 762360c497dSPeter Brune .keywords: SNES, nonlinear, set, function, norm 763360c497dSPeter Brune 764360c497dSPeter Brune .seealso: SNESSetFunction(), SNESSetIterationNumber(), VecNorm(). 765360c497dSPeter Brune @*/ 766360c497dSPeter Brune PetscErrorCode SNESSetFunctionNorm(SNES snes,PetscReal fnorm) 767360c497dSPeter Brune { 768360c497dSPeter Brune 769360c497dSPeter Brune PetscErrorCode ierr; 770360c497dSPeter Brune 771360c497dSPeter Brune PetscFunctionBegin; 772360c497dSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 773360c497dSPeter Brune ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 774360c497dSPeter Brune snes->norm = fnorm; 775360c497dSPeter Brune ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 776360c497dSPeter Brune PetscFunctionReturn(0); 777360c497dSPeter Brune } 778360c497dSPeter Brune 7794a2ae208SSatish Balay #undef __FUNCT__ 780b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 7819b94acceSBarry Smith /*@ 782b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 7839b94acceSBarry Smith attempted by the nonlinear solver. 7849b94acceSBarry Smith 785c7afd0dbSLois Curfman McInnes Not Collective 786c7afd0dbSLois Curfman McInnes 7879b94acceSBarry Smith Input Parameter: 7889b94acceSBarry Smith . snes - SNES context 7899b94acceSBarry Smith 7909b94acceSBarry Smith Output Parameter: 7919b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 7929b94acceSBarry Smith 793c96a6f78SLois Curfman McInnes Notes: 794c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 795c96a6f78SLois Curfman McInnes 79636851e7fSLois Curfman McInnes Level: intermediate 79736851e7fSLois Curfman McInnes 7989b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 79958ebbce7SBarry Smith 800e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 80158ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 8029b94acceSBarry Smith @*/ 8037087cfbeSBarry Smith PetscErrorCode SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 8049b94acceSBarry Smith { 8053a40ed3dSBarry Smith PetscFunctionBegin; 8060700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8074482741eSBarry Smith PetscValidIntPointer(nfails,2); 80850ffb88aSMatthew Knepley *nfails = snes->numFailures; 80950ffb88aSMatthew Knepley PetscFunctionReturn(0); 81050ffb88aSMatthew Knepley } 81150ffb88aSMatthew Knepley 81250ffb88aSMatthew Knepley #undef __FUNCT__ 813b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 81450ffb88aSMatthew Knepley /*@ 815b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 81650ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 81750ffb88aSMatthew Knepley 81850ffb88aSMatthew Knepley Not Collective 81950ffb88aSMatthew Knepley 82050ffb88aSMatthew Knepley Input Parameters: 82150ffb88aSMatthew Knepley + snes - SNES context 82250ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 82350ffb88aSMatthew Knepley 82450ffb88aSMatthew Knepley Level: intermediate 82550ffb88aSMatthew Knepley 82650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 82758ebbce7SBarry Smith 828e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 82958ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 83050ffb88aSMatthew Knepley @*/ 8317087cfbeSBarry Smith PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 83250ffb88aSMatthew Knepley { 83350ffb88aSMatthew Knepley PetscFunctionBegin; 8340700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 83550ffb88aSMatthew Knepley snes->maxFailures = maxFails; 83650ffb88aSMatthew Knepley PetscFunctionReturn(0); 83750ffb88aSMatthew Knepley } 83850ffb88aSMatthew Knepley 83950ffb88aSMatthew Knepley #undef __FUNCT__ 840b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 84150ffb88aSMatthew Knepley /*@ 842b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 84350ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 84450ffb88aSMatthew Knepley 84550ffb88aSMatthew Knepley Not Collective 84650ffb88aSMatthew Knepley 84750ffb88aSMatthew Knepley Input Parameter: 84850ffb88aSMatthew Knepley . snes - SNES context 84950ffb88aSMatthew Knepley 85050ffb88aSMatthew Knepley Output Parameter: 85150ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 85250ffb88aSMatthew Knepley 85350ffb88aSMatthew Knepley Level: intermediate 85450ffb88aSMatthew Knepley 85550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 85658ebbce7SBarry Smith 857e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 85858ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 85958ebbce7SBarry Smith 86050ffb88aSMatthew Knepley @*/ 8617087cfbeSBarry Smith PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 86250ffb88aSMatthew Knepley { 86350ffb88aSMatthew Knepley PetscFunctionBegin; 8640700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8654482741eSBarry Smith PetscValidIntPointer(maxFails,2); 86650ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 8673a40ed3dSBarry Smith PetscFunctionReturn(0); 8689b94acceSBarry Smith } 869a847f771SSatish Balay 8704a2ae208SSatish Balay #undef __FUNCT__ 8712541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 8722541af92SBarry Smith /*@ 8732541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 8742541af92SBarry Smith done by SNES. 8752541af92SBarry Smith 8762541af92SBarry Smith Not Collective 8772541af92SBarry Smith 8782541af92SBarry Smith Input Parameter: 8792541af92SBarry Smith . snes - SNES context 8802541af92SBarry Smith 8812541af92SBarry Smith Output Parameter: 8822541af92SBarry Smith . nfuncs - number of evaluations 8832541af92SBarry Smith 8842541af92SBarry Smith Level: intermediate 8852541af92SBarry Smith 8862541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 88758ebbce7SBarry Smith 888e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 8892541af92SBarry Smith @*/ 8907087cfbeSBarry Smith PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 8912541af92SBarry Smith { 8922541af92SBarry Smith PetscFunctionBegin; 8930700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8942541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 8952541af92SBarry Smith *nfuncs = snes->nfuncs; 8962541af92SBarry Smith PetscFunctionReturn(0); 8972541af92SBarry Smith } 8982541af92SBarry Smith 8992541af92SBarry Smith #undef __FUNCT__ 9003d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 9013d4c4710SBarry Smith /*@ 9023d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 9033d4c4710SBarry Smith linear solvers. 9043d4c4710SBarry Smith 9053d4c4710SBarry Smith Not Collective 9063d4c4710SBarry Smith 9073d4c4710SBarry Smith Input Parameter: 9083d4c4710SBarry Smith . snes - SNES context 9093d4c4710SBarry Smith 9103d4c4710SBarry Smith Output Parameter: 9113d4c4710SBarry Smith . nfails - number of failed solves 9123d4c4710SBarry Smith 9133d4c4710SBarry Smith Notes: 9143d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 9153d4c4710SBarry Smith 9163d4c4710SBarry Smith Level: intermediate 9173d4c4710SBarry Smith 9183d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 91958ebbce7SBarry Smith 920e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 9213d4c4710SBarry Smith @*/ 9227087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 9233d4c4710SBarry Smith { 9243d4c4710SBarry Smith PetscFunctionBegin; 9250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9263d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 9273d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 9283d4c4710SBarry Smith PetscFunctionReturn(0); 9293d4c4710SBarry Smith } 9303d4c4710SBarry Smith 9313d4c4710SBarry Smith #undef __FUNCT__ 9323d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 9333d4c4710SBarry Smith /*@ 9343d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 9353d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 9363d4c4710SBarry Smith 9373f9fe445SBarry Smith Logically Collective on SNES 9383d4c4710SBarry Smith 9393d4c4710SBarry Smith Input Parameters: 9403d4c4710SBarry Smith + snes - SNES context 9413d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 9423d4c4710SBarry Smith 9433d4c4710SBarry Smith Level: intermediate 9443d4c4710SBarry Smith 945a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 9463d4c4710SBarry Smith 9473d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 9483d4c4710SBarry Smith 94958ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 9503d4c4710SBarry Smith @*/ 9517087cfbeSBarry Smith PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 9523d4c4710SBarry Smith { 9533d4c4710SBarry Smith PetscFunctionBegin; 9540700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 955c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxFails,2); 9563d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 9573d4c4710SBarry Smith PetscFunctionReturn(0); 9583d4c4710SBarry Smith } 9593d4c4710SBarry Smith 9603d4c4710SBarry Smith #undef __FUNCT__ 9613d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 9623d4c4710SBarry Smith /*@ 9633d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 9643d4c4710SBarry Smith are allowed before SNES terminates 9653d4c4710SBarry Smith 9663d4c4710SBarry Smith Not Collective 9673d4c4710SBarry Smith 9683d4c4710SBarry Smith Input Parameter: 9693d4c4710SBarry Smith . snes - SNES context 9703d4c4710SBarry Smith 9713d4c4710SBarry Smith Output Parameter: 9723d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 9733d4c4710SBarry Smith 9743d4c4710SBarry Smith Level: intermediate 9753d4c4710SBarry Smith 9763d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 9773d4c4710SBarry Smith 9783d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 9793d4c4710SBarry Smith 980e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 9813d4c4710SBarry Smith @*/ 9827087cfbeSBarry Smith PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 9833d4c4710SBarry Smith { 9843d4c4710SBarry Smith PetscFunctionBegin; 9850700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9863d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 9873d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 9883d4c4710SBarry Smith PetscFunctionReturn(0); 9893d4c4710SBarry Smith } 9903d4c4710SBarry Smith 9913d4c4710SBarry Smith #undef __FUNCT__ 992b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 993c96a6f78SLois Curfman McInnes /*@ 994b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 995c96a6f78SLois Curfman McInnes used by the nonlinear solver. 996c96a6f78SLois Curfman McInnes 997c7afd0dbSLois Curfman McInnes Not Collective 998c7afd0dbSLois Curfman McInnes 999c96a6f78SLois Curfman McInnes Input Parameter: 1000c96a6f78SLois Curfman McInnes . snes - SNES context 1001c96a6f78SLois Curfman McInnes 1002c96a6f78SLois Curfman McInnes Output Parameter: 1003c96a6f78SLois Curfman McInnes . lits - number of linear iterations 1004c96a6f78SLois Curfman McInnes 1005c96a6f78SLois Curfman McInnes Notes: 1006c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 1007c96a6f78SLois Curfman McInnes 100836851e7fSLois Curfman McInnes Level: intermediate 100936851e7fSLois Curfman McInnes 1010c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 10112b668275SBarry Smith 10128c7482ecSBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 1013c96a6f78SLois Curfman McInnes @*/ 10147087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 1015c96a6f78SLois Curfman McInnes { 10163a40ed3dSBarry Smith PetscFunctionBegin; 10170700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10184482741eSBarry Smith PetscValidIntPointer(lits,2); 1019c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 10203a40ed3dSBarry Smith PetscFunctionReturn(0); 1021c96a6f78SLois Curfman McInnes } 1022c96a6f78SLois Curfman McInnes 10234a2ae208SSatish Balay #undef __FUNCT__ 102494b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 102552baeb72SSatish Balay /*@ 102694b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 10279b94acceSBarry Smith 102894b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 1029c7afd0dbSLois Curfman McInnes 10309b94acceSBarry Smith Input Parameter: 10319b94acceSBarry Smith . snes - the SNES context 10329b94acceSBarry Smith 10339b94acceSBarry Smith Output Parameter: 103494b7f48cSBarry Smith . ksp - the KSP context 10359b94acceSBarry Smith 10369b94acceSBarry Smith Notes: 103794b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 10389b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 10392999313aSBarry Smith PC contexts as well. 10409b94acceSBarry Smith 104136851e7fSLois Curfman McInnes Level: beginner 104236851e7fSLois Curfman McInnes 104394b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 10449b94acceSBarry Smith 10452999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 10469b94acceSBarry Smith @*/ 10477087cfbeSBarry Smith PetscErrorCode SNESGetKSP(SNES snes,KSP *ksp) 10489b94acceSBarry Smith { 10491cee3971SBarry Smith PetscErrorCode ierr; 10501cee3971SBarry Smith 10513a40ed3dSBarry Smith PetscFunctionBegin; 10520700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10534482741eSBarry Smith PetscValidPointer(ksp,2); 10541cee3971SBarry Smith 10551cee3971SBarry Smith if (!snes->ksp) { 10561cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 10571cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 10581cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 10591cee3971SBarry Smith } 106094b7f48cSBarry Smith *ksp = snes->ksp; 10613a40ed3dSBarry Smith PetscFunctionReturn(0); 10629b94acceSBarry Smith } 106382bf6240SBarry Smith 10644a2ae208SSatish Balay #undef __FUNCT__ 10652999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 10662999313aSBarry Smith /*@ 10672999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 10682999313aSBarry Smith 10692999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 10702999313aSBarry Smith 10712999313aSBarry Smith Input Parameters: 10722999313aSBarry Smith + snes - the SNES context 10732999313aSBarry Smith - ksp - the KSP context 10742999313aSBarry Smith 10752999313aSBarry Smith Notes: 10762999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 10772999313aSBarry Smith so this routine is rarely needed. 10782999313aSBarry Smith 10792999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 10802999313aSBarry Smith decreased by one. 10812999313aSBarry Smith 10822999313aSBarry Smith Level: developer 10832999313aSBarry Smith 10842999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 10852999313aSBarry Smith 10862999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 10872999313aSBarry Smith @*/ 10887087cfbeSBarry Smith PetscErrorCode SNESSetKSP(SNES snes,KSP ksp) 10892999313aSBarry Smith { 10902999313aSBarry Smith PetscErrorCode ierr; 10912999313aSBarry Smith 10922999313aSBarry Smith PetscFunctionBegin; 10930700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10940700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 10952999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 10967dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 1097906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 10982999313aSBarry Smith snes->ksp = ksp; 10992999313aSBarry Smith PetscFunctionReturn(0); 11002999313aSBarry Smith } 11012999313aSBarry Smith 11027adad957SLisandro Dalcin #if 0 11032999313aSBarry Smith #undef __FUNCT__ 11044a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 11056849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 1106e24b481bSBarry Smith { 1107e24b481bSBarry Smith PetscFunctionBegin; 1108e24b481bSBarry Smith PetscFunctionReturn(0); 1109e24b481bSBarry Smith } 11107adad957SLisandro Dalcin #endif 1111e24b481bSBarry Smith 11129b94acceSBarry Smith /* -----------------------------------------------------------*/ 11134a2ae208SSatish Balay #undef __FUNCT__ 11144a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 111552baeb72SSatish Balay /*@ 11169b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 11179b94acceSBarry Smith 1118c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 1119c7afd0dbSLois Curfman McInnes 1120c7afd0dbSLois Curfman McInnes Input Parameters: 1121906ed7ccSBarry Smith . comm - MPI communicator 11229b94acceSBarry Smith 11239b94acceSBarry Smith Output Parameter: 11249b94acceSBarry Smith . outsnes - the new SNES context 11259b94acceSBarry Smith 1126c7afd0dbSLois Curfman McInnes Options Database Keys: 1127c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 1128c7afd0dbSLois Curfman McInnes and no preconditioning matrix 1129c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 1130c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 1131c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 1132c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 1133c1f60f51SBarry Smith 113436851e7fSLois Curfman McInnes Level: beginner 113536851e7fSLois Curfman McInnes 11369b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 11379b94acceSBarry Smith 1138a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 1139a8054027SBarry Smith 11409b94acceSBarry Smith @*/ 11417087cfbeSBarry Smith PetscErrorCode SNESCreate(MPI_Comm comm,SNES *outsnes) 11429b94acceSBarry Smith { 1143dfbe8321SBarry Smith PetscErrorCode ierr; 11449b94acceSBarry Smith SNES snes; 1145fa9f3622SBarry Smith SNESKSPEW *kctx; 114637fcc0dbSBarry Smith 11473a40ed3dSBarry Smith PetscFunctionBegin; 1148ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 11498ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 11508ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 11518ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 11528ba1e511SMatthew Knepley #endif 11538ba1e511SMatthew Knepley 11543194b578SJed Brown ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 11557adad957SLisandro Dalcin 115685385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 11572c155ee1SBarry Smith snes->usesksp = PETSC_TRUE; 11589b94acceSBarry Smith snes->max_its = 50; 11599750a799SBarry Smith snes->max_funcs = 10000; 11609b94acceSBarry Smith snes->norm = 0.0; 1161b4874afaSBarry Smith snes->rtol = 1.e-8; 1162b4874afaSBarry Smith snes->ttol = 0.0; 116370441072SBarry Smith snes->abstol = 1.e-50; 11649b94acceSBarry Smith snes->xtol = 1.e-8; 11654b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 11669b94acceSBarry Smith snes->nfuncs = 0; 116750ffb88aSMatthew Knepley snes->numFailures = 0; 116850ffb88aSMatthew Knepley snes->maxFailures = 1; 11697a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1170e35cf81dSBarry Smith snes->lagjacobian = 1; 1171a8054027SBarry Smith snes->lagpreconditioner = 1; 1172639f9d9dSBarry Smith snes->numbermonitors = 0; 11739b94acceSBarry Smith snes->data = 0; 11744dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1175186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 11766f24a144SLois Curfman McInnes snes->nwork = 0; 117758c9b817SLisandro Dalcin snes->work = 0; 117858c9b817SLisandro Dalcin snes->nvwork = 0; 117958c9b817SLisandro Dalcin snes->vwork = 0; 1180758f92a0SBarry Smith snes->conv_hist_len = 0; 1181758f92a0SBarry Smith snes->conv_hist_max = 0; 1182758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 1183758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 1184758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1185184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 118689b92e6fSPeter Brune snes->gssweeps = 1; 11879b94acceSBarry Smith 1188ea630c6eSPeter Brune /* initialize the line search options */ 1189ea630c6eSPeter Brune snes->ls_type = SNES_LS_BASIC; 1190af60355fSPeter Brune snes->ls_its = 1; 1191ea630c6eSPeter Brune snes->damping = 1.0; 1192ea630c6eSPeter Brune snes->maxstep = 1e8; 1193ea630c6eSPeter Brune snes->steptol = 1e-12; 1194ea630c6eSPeter Brune snes->ls_alpha = 1e-4; 1195ea630c6eSPeter Brune snes->ls_monitor = PETSC_NULL; 1196ea630c6eSPeter Brune 1197ea630c6eSPeter Brune snes->ops->linesearch = PETSC_NULL; 1198ea630c6eSPeter Brune snes->precheck = PETSC_NULL; 1199ea630c6eSPeter Brune snes->ops->precheckstep = PETSC_NULL; 1200ea630c6eSPeter Brune snes->postcheck = PETSC_NULL; 1201ea630c6eSPeter Brune snes->ops->postcheckstep= PETSC_NULL; 1202ea630c6eSPeter Brune 12033d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 12043d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 12053d4c4710SBarry Smith 12069b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 120738f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 12089b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 12099b94acceSBarry Smith kctx->version = 2; 12109b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 12119b94acceSBarry Smith this was too large for some test cases */ 121275567043SBarry Smith kctx->rtol_last = 0.0; 12139b94acceSBarry Smith kctx->rtol_max = .9; 12149b94acceSBarry Smith kctx->gamma = 1.0; 121562d1f40fSBarry Smith kctx->alpha = .5*(1.0 + PetscSqrtReal(5.0)); 121671f87433Sdalcinl kctx->alpha2 = kctx->alpha; 12179b94acceSBarry Smith kctx->threshold = .1; 121875567043SBarry Smith kctx->lresid_last = 0.0; 121975567043SBarry Smith kctx->norm_last = 0.0; 12209b94acceSBarry Smith 12219b94acceSBarry Smith *outsnes = snes; 12223a40ed3dSBarry Smith PetscFunctionReturn(0); 12239b94acceSBarry Smith } 12249b94acceSBarry Smith 12254a2ae208SSatish Balay #undef __FUNCT__ 12264a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 12279b94acceSBarry Smith /*@C 12289b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 12299b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 12309b94acceSBarry Smith equations. 12319b94acceSBarry Smith 12323f9fe445SBarry Smith Logically Collective on SNES 1233fee21e36SBarry Smith 1234c7afd0dbSLois Curfman McInnes Input Parameters: 1235c7afd0dbSLois Curfman McInnes + snes - the SNES context 1236c7afd0dbSLois Curfman McInnes . r - vector to store function value 1237de044059SHong Zhang . func - function evaluation routine 1238c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1239c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 12409b94acceSBarry Smith 1241c7afd0dbSLois Curfman McInnes Calling sequence of func: 12428d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 1243c7afd0dbSLois Curfman McInnes 1244313e4042SLois Curfman McInnes . f - function vector 1245c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 12469b94acceSBarry Smith 12479b94acceSBarry Smith Notes: 12489b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 12499b94acceSBarry Smith $ f'(x) x = -f(x), 1250c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 12519b94acceSBarry Smith 125236851e7fSLois Curfman McInnes Level: beginner 125336851e7fSLois Curfman McInnes 12549b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 12559b94acceSBarry Smith 12568b0a5094SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetPicard() 12579b94acceSBarry Smith @*/ 12587087cfbeSBarry Smith PetscErrorCode SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 12599b94acceSBarry Smith { 126085385478SLisandro Dalcin PetscErrorCode ierr; 12613a40ed3dSBarry Smith PetscFunctionBegin; 12620700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1263d2a683ecSLisandro Dalcin if (r) { 1264d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1265d2a683ecSLisandro Dalcin PetscCheckSameComm(snes,1,r,2); 126685385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 12676bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 126885385478SLisandro Dalcin snes->vec_func = r; 1269d2a683ecSLisandro Dalcin } else if (!snes->vec_func && snes->dm) { 1270d2a683ecSLisandro Dalcin ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1271d2a683ecSLisandro Dalcin } 1272d2a683ecSLisandro Dalcin if (func) snes->ops->computefunction = func; 1273d2a683ecSLisandro Dalcin if (ctx) snes->funP = ctx; 12743a40ed3dSBarry Smith PetscFunctionReturn(0); 12759b94acceSBarry Smith } 12769b94acceSBarry Smith 1277646217ecSPeter Brune 1278646217ecSPeter Brune #undef __FUNCT__ 1279646217ecSPeter Brune #define __FUNCT__ "SNESSetGS" 1280c79ef259SPeter Brune /*@C 1281c79ef259SPeter Brune SNESSetGS - Sets the user nonlinear Gauss-Seidel routine for 1282c79ef259SPeter Brune use with composed nonlinear solvers. 1283c79ef259SPeter Brune 1284c79ef259SPeter Brune Input Parameters: 1285c79ef259SPeter Brune + snes - the SNES context 1286c79ef259SPeter Brune . gsfunc - function evaluation routine 1287c79ef259SPeter Brune - ctx - [optional] user-defined context for private data for the 1288c79ef259SPeter Brune smoother evaluation routine (may be PETSC_NULL) 1289c79ef259SPeter Brune 1290c79ef259SPeter Brune Calling sequence of func: 1291c79ef259SPeter Brune $ func (SNES snes,Vec x,Vec b,void *ctx); 1292c79ef259SPeter Brune 1293c79ef259SPeter Brune + X - solution vector 1294c79ef259SPeter Brune . B - RHS vector 1295d28543b3SPeter Brune - ctx - optional user-defined Gauss-Seidel context 1296c79ef259SPeter Brune 1297c79ef259SPeter Brune Notes: 1298c79ef259SPeter Brune The GS routines are used by the composed nonlinear solver to generate 1299c79ef259SPeter Brune a problem appropriate update to the solution, particularly FAS. 1300c79ef259SPeter Brune 1301d28543b3SPeter Brune Level: intermediate 1302c79ef259SPeter Brune 1303d28543b3SPeter Brune .keywords: SNES, nonlinear, set, Gauss-Seidel 1304c79ef259SPeter Brune 1305c79ef259SPeter Brune .seealso: SNESGetFunction(), SNESComputeGS() 1306c79ef259SPeter Brune @*/ 1307646217ecSPeter Brune PetscErrorCode SNESSetGS(SNES snes, PetscErrorCode (*gsfunc)(SNES,Vec,Vec,void *), void * ctx) { 1308646217ecSPeter Brune PetscFunctionBegin; 1309646217ecSPeter Brune if (gsfunc) snes->ops->computegs = gsfunc; 1310646217ecSPeter Brune if (ctx) snes->gsP = ctx; 1311646217ecSPeter Brune PetscFunctionReturn(0); 1312646217ecSPeter Brune } 1313646217ecSPeter Brune 1314d25893d9SBarry Smith #undef __FUNCT__ 131589b92e6fSPeter Brune #define __FUNCT__ "SNESSetGSSweeps" 131689b92e6fSPeter Brune /*@ 131789b92e6fSPeter Brune SNESSetGSSweeps - Sets the number of sweeps of GS to use. 131889b92e6fSPeter Brune 131989b92e6fSPeter Brune Input Parameters: 132089b92e6fSPeter Brune + snes - the SNES context 132189b92e6fSPeter Brune - sweeps - the number of sweeps of GS to perform. 132289b92e6fSPeter Brune 132389b92e6fSPeter Brune Level: intermediate 132489b92e6fSPeter Brune 132589b92e6fSPeter Brune .keywords: SNES, nonlinear, set, Gauss-Siedel 132689b92e6fSPeter Brune 132789b92e6fSPeter Brune .seealso: SNESSetGS(), SNESGetGS(), SNESSetPC(), SNESGetGSSweeps() 132889b92e6fSPeter Brune @*/ 132989b92e6fSPeter Brune 133089b92e6fSPeter Brune PetscErrorCode SNESSetGSSweeps(SNES snes, PetscInt sweeps) { 133189b92e6fSPeter Brune PetscFunctionBegin; 133289b92e6fSPeter Brune snes->gssweeps = sweeps; 133389b92e6fSPeter Brune PetscFunctionReturn(0); 133489b92e6fSPeter Brune } 133589b92e6fSPeter Brune 133689b92e6fSPeter Brune 133789b92e6fSPeter Brune #undef __FUNCT__ 133889b92e6fSPeter Brune #define __FUNCT__ "SNESGetGSSweeps" 133989b92e6fSPeter Brune /*@ 134089b92e6fSPeter Brune SNESGetGSSweeps - Gets the number of sweeps GS will use. 134189b92e6fSPeter Brune 134289b92e6fSPeter Brune Input Parameters: 134389b92e6fSPeter Brune . snes - the SNES context 134489b92e6fSPeter Brune 134589b92e6fSPeter Brune Output Parameters: 134689b92e6fSPeter Brune . sweeps - the number of sweeps of GS to perform. 134789b92e6fSPeter Brune 134889b92e6fSPeter Brune Level: intermediate 134989b92e6fSPeter Brune 135089b92e6fSPeter Brune .keywords: SNES, nonlinear, set, Gauss-Siedel 135189b92e6fSPeter Brune 135289b92e6fSPeter Brune .seealso: SNESSetGS(), SNESGetGS(), SNESSetPC(), SNESSetGSSweeps() 135389b92e6fSPeter Brune @*/ 135489b92e6fSPeter Brune PetscErrorCode SNESGetGSSweeps(SNES snes, PetscInt * sweeps) { 135589b92e6fSPeter Brune PetscFunctionBegin; 135689b92e6fSPeter Brune *sweeps = snes->gssweeps; 135789b92e6fSPeter Brune PetscFunctionReturn(0); 135889b92e6fSPeter Brune } 135989b92e6fSPeter Brune 136089b92e6fSPeter Brune #undef __FUNCT__ 13618b0a5094SBarry Smith #define __FUNCT__ "SNESPicardComputeFunction" 13628b0a5094SBarry Smith PetscErrorCode SNESPicardComputeFunction(SNES snes,Vec x,Vec f,void *ctx) 13638b0a5094SBarry Smith { 13648b0a5094SBarry Smith PetscErrorCode ierr; 13658b0a5094SBarry Smith PetscFunctionBegin; 13668b0a5094SBarry Smith /* A(x)*x - b(x) */ 13678b0a5094SBarry Smith ierr = (*snes->ops->computepjacobian)(snes,x,&snes->jacobian,&snes->jacobian_pre,&snes->matstruct,snes->jacP);CHKERRQ(ierr); 13688b0a5094SBarry Smith ierr = (*snes->ops->computepfunction)(snes,x,f,snes->funP);CHKERRQ(ierr); 13698b0a5094SBarry Smith ierr = VecView(x,PETSC_VIEWER_BINARY_WORLD);CHKERRQ(ierr); 13708b0a5094SBarry Smith ierr = VecView(f,PETSC_VIEWER_BINARY_WORLD);CHKERRQ(ierr); 13718b0a5094SBarry Smith ierr = VecScale(f,-1.0);CHKERRQ(ierr); 13728b0a5094SBarry Smith ierr = MatMultAdd(snes->jacobian_pre,x,f,f);CHKERRQ(ierr); 13738b0a5094SBarry Smith PetscFunctionReturn(0); 13748b0a5094SBarry Smith } 13758b0a5094SBarry Smith 13768b0a5094SBarry Smith #undef __FUNCT__ 13778b0a5094SBarry Smith #define __FUNCT__ "SNESPicardComputeJacobian" 13788b0a5094SBarry Smith PetscErrorCode SNESPicardComputeJacobian(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx) 13798b0a5094SBarry Smith { 13808b0a5094SBarry Smith PetscFunctionBegin; 13818b0a5094SBarry Smith *flag = snes->matstruct; 13828b0a5094SBarry Smith PetscFunctionReturn(0); 13838b0a5094SBarry Smith } 13848b0a5094SBarry Smith 13858b0a5094SBarry Smith #undef __FUNCT__ 13868b0a5094SBarry Smith #define __FUNCT__ "SNESSetPicard" 13878b0a5094SBarry Smith /*@C 13880d04baf8SBarry Smith SNESSetPicard - Use SNES to solve the semilinear-system A(x) x = b(x) via a Picard type iteration (Picard linearization) 13898b0a5094SBarry Smith 13908b0a5094SBarry Smith Logically Collective on SNES 13918b0a5094SBarry Smith 13928b0a5094SBarry Smith Input Parameters: 13938b0a5094SBarry Smith + snes - the SNES context 13948b0a5094SBarry Smith . r - vector to store function value 13958b0a5094SBarry Smith . func - function evaluation routine 13968b0a5094SBarry Smith . jmat - normally the same as mat but you can pass another matrix for which you compute the Jacobian of A(x) x - b(x) (see jmat below) 13978b0a5094SBarry Smith . mat - matrix to store A 13988b0a5094SBarry Smith . mfunc - function to compute matrix value 13998b0a5094SBarry Smith - ctx - [optional] user-defined context for private data for the 14008b0a5094SBarry Smith function evaluation routine (may be PETSC_NULL) 14018b0a5094SBarry Smith 14028b0a5094SBarry Smith Calling sequence of func: 14038b0a5094SBarry Smith $ func (SNES snes,Vec x,Vec f,void *ctx); 14048b0a5094SBarry Smith 14058b0a5094SBarry Smith + f - function vector 14068b0a5094SBarry Smith - ctx - optional user-defined function context 14078b0a5094SBarry Smith 14088b0a5094SBarry Smith Calling sequence of mfunc: 14098b0a5094SBarry Smith $ mfunc (SNES snes,Vec x,Mat *jmat,Mat *mat,int *flag,void *ctx); 14108b0a5094SBarry Smith 14118b0a5094SBarry Smith + x - input vector 14128b0a5094SBarry Smith . jmat - Form Jacobian matrix of A(x) x - b(x) if available, not there is really no reason to use it in this way since then you can just use SNESSetJacobian(), 14138b0a5094SBarry Smith normally just pass mat in this location 14148b0a5094SBarry Smith . mat - form A(x) matrix 14158b0a5094SBarry Smith . flag - flag indicating information about the preconditioner matrix 14168b0a5094SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 14178b0a5094SBarry Smith - ctx - [optional] user-defined Jacobian context 14188b0a5094SBarry Smith 14198b0a5094SBarry Smith Notes: 14208b0a5094SBarry Smith One can call SNESSetPicard() or SNESSetFunction() (and possibly SNESSetJacobian()) but cannot call both 14218b0a5094SBarry Smith 14228b0a5094SBarry Smith $ Solves the equation A(x) x = b(x) via the defect correction algorithm A(x^{n}) (x^{n+1} - x^{n}) = b(x^{n}) - A(x^{n})x^{n} 14238b0a5094SBarry Smith $ Note that when an exact solver is used this corresponds to the "classic" Picard A(x^{n}) x^{n+1} = b(x^{n}) iteration. 14248b0a5094SBarry Smith 14258b0a5094SBarry Smith Run with -snes_mf_operator to solve the system with Newton's method using A(x^{n}) to construct the preconditioner. 14268b0a5094SBarry Smith 14270d04baf8SBarry Smith We implement the defect correction form of the Picard iteration because it converges much more generally when inexact linear solvers are used then 14280d04baf8SBarry Smith the direct Picard iteration A(x^n) x^{n+1} = b(x^n) 14298b0a5094SBarry Smith 14308b0a5094SBarry Smith There is some controversity over the definition of a Picard iteration for nonlinear systems but almost everyone agrees that it involves a linear solve and some 14318b0a5094SBarry Smith believe it is the iteration A(x^{n}) x^{n+1} = b(x^{n}) hence we use the name Picard. If anyone has an authoritative reference that defines the Picard iteration 14328b0a5094SBarry Smith different please contact us at petsc-dev@mcs.anl.gov and we'll have an entirely new argument :-). 14338b0a5094SBarry Smith 14348b0a5094SBarry Smith Level: beginner 14358b0a5094SBarry Smith 14368b0a5094SBarry Smith .keywords: SNES, nonlinear, set, function 14378b0a5094SBarry Smith 14380d04baf8SBarry Smith .seealso: SNESGetFunction(), SNESSetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESGetPicard(), SNESLineSearchPreCheckPicard() 14398b0a5094SBarry Smith @*/ 14408b0a5094SBarry Smith PetscErrorCode SNESSetPicard(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),Mat jmat, Mat mat, PetscErrorCode (*mfunc)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 14418b0a5094SBarry Smith { 14428b0a5094SBarry Smith PetscErrorCode ierr; 14438b0a5094SBarry Smith PetscFunctionBegin; 14448b0a5094SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14458b0a5094SBarry Smith snes->ops->computepfunction = func; 14468b0a5094SBarry Smith snes->ops->computepjacobian = mfunc; 14478b0a5094SBarry Smith ierr = SNESSetFunction(snes,r,SNESPicardComputeFunction,ctx);CHKERRQ(ierr); 14488b0a5094SBarry Smith ierr = SNESSetJacobian(snes,jmat,mat,SNESPicardComputeJacobian,ctx);CHKERRQ(ierr); 14498b0a5094SBarry Smith PetscFunctionReturn(0); 14508b0a5094SBarry Smith } 14518b0a5094SBarry Smith 14528b0a5094SBarry Smith #undef __FUNCT__ 1453d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess" 1454d25893d9SBarry Smith /*@C 1455d25893d9SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem 1456d25893d9SBarry Smith 1457d25893d9SBarry Smith Logically Collective on SNES 1458d25893d9SBarry Smith 1459d25893d9SBarry Smith Input Parameters: 1460d25893d9SBarry Smith + snes - the SNES context 1461d25893d9SBarry Smith . func - function evaluation routine 1462d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 1463d25893d9SBarry Smith function evaluation routine (may be PETSC_NULL) 1464d25893d9SBarry Smith 1465d25893d9SBarry Smith Calling sequence of func: 1466d25893d9SBarry Smith $ func (SNES snes,Vec x,void *ctx); 1467d25893d9SBarry Smith 1468d25893d9SBarry Smith . f - function vector 1469d25893d9SBarry Smith - ctx - optional user-defined function context 1470d25893d9SBarry Smith 1471d25893d9SBarry Smith Level: intermediate 1472d25893d9SBarry Smith 1473d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function 1474d25893d9SBarry Smith 1475d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 1476d25893d9SBarry Smith @*/ 1477d25893d9SBarry Smith PetscErrorCode SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx) 1478d25893d9SBarry Smith { 1479d25893d9SBarry Smith PetscFunctionBegin; 1480d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1481d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 1482d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 1483d25893d9SBarry Smith PetscFunctionReturn(0); 1484d25893d9SBarry Smith } 1485d25893d9SBarry Smith 14863ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 14873ab0aad5SBarry Smith #undef __FUNCT__ 14881096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 14891096aae1SMatthew Knepley /*@C 14901096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 14911096aae1SMatthew Knepley it assumes a zero right hand side. 14921096aae1SMatthew Knepley 14933f9fe445SBarry Smith Logically Collective on SNES 14941096aae1SMatthew Knepley 14951096aae1SMatthew Knepley Input Parameter: 14961096aae1SMatthew Knepley . snes - the SNES context 14971096aae1SMatthew Knepley 14981096aae1SMatthew Knepley Output Parameter: 1499bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 15001096aae1SMatthew Knepley 15011096aae1SMatthew Knepley Level: intermediate 15021096aae1SMatthew Knepley 15031096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 15041096aae1SMatthew Knepley 150585385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 15061096aae1SMatthew Knepley @*/ 15077087cfbeSBarry Smith PetscErrorCode SNESGetRhs(SNES snes,Vec *rhs) 15081096aae1SMatthew Knepley { 15091096aae1SMatthew Knepley PetscFunctionBegin; 15100700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15111096aae1SMatthew Knepley PetscValidPointer(rhs,2); 151285385478SLisandro Dalcin *rhs = snes->vec_rhs; 15131096aae1SMatthew Knepley PetscFunctionReturn(0); 15141096aae1SMatthew Knepley } 15151096aae1SMatthew Knepley 15161096aae1SMatthew Knepley #undef __FUNCT__ 15174a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 15189b94acceSBarry Smith /*@ 151936851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 15209b94acceSBarry Smith SNESSetFunction(). 15219b94acceSBarry Smith 1522c7afd0dbSLois Curfman McInnes Collective on SNES 1523c7afd0dbSLois Curfman McInnes 15249b94acceSBarry Smith Input Parameters: 1525c7afd0dbSLois Curfman McInnes + snes - the SNES context 1526c7afd0dbSLois Curfman McInnes - x - input vector 15279b94acceSBarry Smith 15289b94acceSBarry Smith Output Parameter: 15293638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 15309b94acceSBarry Smith 15311bffabb2SLois Curfman McInnes Notes: 153236851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 153336851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 153436851e7fSLois Curfman McInnes themselves. 153536851e7fSLois Curfman McInnes 153636851e7fSLois Curfman McInnes Level: developer 153736851e7fSLois Curfman McInnes 15389b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 15399b94acceSBarry Smith 1540a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 15419b94acceSBarry Smith @*/ 15427087cfbeSBarry Smith PetscErrorCode SNESComputeFunction(SNES snes,Vec x,Vec y) 15439b94acceSBarry Smith { 1544dfbe8321SBarry Smith PetscErrorCode ierr; 15459b94acceSBarry Smith 15463a40ed3dSBarry Smith PetscFunctionBegin; 15470700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15480700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 15490700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 1550c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1551c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 15524ec14c9cSBarry Smith VecValidValues(x,2,PETSC_TRUE); 1553184914b5SBarry Smith 1554d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1555e7788613SBarry Smith if (snes->ops->computefunction) { 1556d64ed03dSBarry Smith PetscStackPush("SNES user function"); 155739d508bbSBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1558d64ed03dSBarry Smith PetscStackPop; 155973250ac0SBarry Smith } else if (snes->dm) { 1560644e2e5bSBarry Smith ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr); 1561c90fad12SPeter Brune } else if (snes->vec_rhs) { 1562c90fad12SPeter Brune ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 1563644e2e5bSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve()."); 156485385478SLisandro Dalcin if (snes->vec_rhs) { 156585385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 15663ab0aad5SBarry Smith } 1567ae3c334cSLois Curfman McInnes snes->nfuncs++; 1568d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 15694ec14c9cSBarry Smith VecValidValues(y,3,PETSC_FALSE); 15703a40ed3dSBarry Smith PetscFunctionReturn(0); 15719b94acceSBarry Smith } 15729b94acceSBarry Smith 15734a2ae208SSatish Balay #undef __FUNCT__ 1574646217ecSPeter Brune #define __FUNCT__ "SNESComputeGS" 1575c79ef259SPeter Brune /*@ 1576c79ef259SPeter Brune SNESComputeGS - Calls the Gauss-Seidel function that has been set with 1577c79ef259SPeter Brune SNESSetGS(). 1578c79ef259SPeter Brune 1579c79ef259SPeter Brune Collective on SNES 1580c79ef259SPeter Brune 1581c79ef259SPeter Brune Input Parameters: 1582c79ef259SPeter Brune + snes - the SNES context 1583c79ef259SPeter Brune . x - input vector 1584c79ef259SPeter Brune - b - rhs vector 1585c79ef259SPeter Brune 1586c79ef259SPeter Brune Output Parameter: 1587c79ef259SPeter Brune . x - new solution vector 1588c79ef259SPeter Brune 1589c79ef259SPeter Brune Notes: 1590c79ef259SPeter Brune SNESComputeGS() is typically used within composed nonlinear solver 1591c79ef259SPeter Brune implementations, so most users would not generally call this routine 1592c79ef259SPeter Brune themselves. 1593c79ef259SPeter Brune 1594c79ef259SPeter Brune Level: developer 1595c79ef259SPeter Brune 1596c79ef259SPeter Brune .keywords: SNES, nonlinear, compute, function 1597c79ef259SPeter Brune 1598c79ef259SPeter Brune .seealso: SNESSetGS(), SNESComputeFunction() 1599c79ef259SPeter Brune @*/ 1600646217ecSPeter Brune PetscErrorCode SNESComputeGS(SNES snes,Vec b,Vec x) 1601646217ecSPeter Brune { 1602646217ecSPeter Brune PetscErrorCode ierr; 160389b92e6fSPeter Brune PetscInt i; 1604646217ecSPeter Brune 1605646217ecSPeter Brune PetscFunctionBegin; 1606646217ecSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1607646217ecSPeter Brune PetscValidHeaderSpecific(x,VEC_CLASSID,2); 1608646217ecSPeter Brune if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,3); 1609646217ecSPeter Brune PetscCheckSameComm(snes,1,x,2); 1610646217ecSPeter Brune if(b) PetscCheckSameComm(snes,1,b,3); 16114ec14c9cSBarry Smith if (b) VecValidValues(b,2,PETSC_TRUE); 1612701cf23dSPeter Brune ierr = PetscLogEventBegin(SNES_GSEval,snes,x,b,0);CHKERRQ(ierr); 1613646217ecSPeter Brune if (snes->ops->computegs) { 161489b92e6fSPeter Brune for (i = 0; i < snes->gssweeps; i++) { 1615646217ecSPeter Brune PetscStackPush("SNES user GS"); 1616646217ecSPeter Brune ierr = (*snes->ops->computegs)(snes,x,b,snes->gsP);CHKERRQ(ierr); 1617646217ecSPeter Brune PetscStackPop; 161889b92e6fSPeter Brune } 1619646217ecSPeter Brune } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetGS() before SNESComputeGS(), likely called from SNESSolve()."); 1620701cf23dSPeter Brune ierr = PetscLogEventEnd(SNES_GSEval,snes,x,b,0);CHKERRQ(ierr); 16214ec14c9cSBarry Smith VecValidValues(x,3,PETSC_FALSE); 1622646217ecSPeter Brune PetscFunctionReturn(0); 1623646217ecSPeter Brune } 1624646217ecSPeter Brune 1625646217ecSPeter Brune 1626646217ecSPeter Brune #undef __FUNCT__ 16274a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 162862fef451SLois Curfman McInnes /*@ 162962fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 163062fef451SLois Curfman McInnes set with SNESSetJacobian(). 163162fef451SLois Curfman McInnes 1632c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1633c7afd0dbSLois Curfman McInnes 163462fef451SLois Curfman McInnes Input Parameters: 1635c7afd0dbSLois Curfman McInnes + snes - the SNES context 1636c7afd0dbSLois Curfman McInnes - x - input vector 163762fef451SLois Curfman McInnes 163862fef451SLois Curfman McInnes Output Parameters: 1639c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 164062fef451SLois Curfman McInnes . B - optional preconditioning matrix 16412b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1642fee21e36SBarry Smith 1643e35cf81dSBarry Smith Options Database Keys: 1644e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1645693365a8SJed Brown . -snes_lag_jacobian <lag> 1646693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 1647693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 1648693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 16494c30e9fbSJed Brown . -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 1650c01495d3SJed Brown . -snes_compare_coloring - Compute the finite differece Jacobian using coloring and display norms of difference 1651c01495d3SJed Brown . -snes_compare_coloring_display - Compute the finite differece Jacobian using coloring and display verbose differences 1652c01495d3SJed Brown . -snes_compare_coloring_threshold - Display only those matrix entries that differ by more than a given threshold 1653c01495d3SJed Brown . -snes_compare_coloring_threshold_atol - Absolute tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 1654c01495d3SJed Brown . -snes_compare_coloring_threshold_rtol - Relative tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 16554c30e9fbSJed Brown . -snes_compare_coloring_draw - Compute the finite differece Jacobian using coloring and draw differences 1656c01495d3SJed Brown - -snes_compare_coloring_draw_contour - Compute the finite differece Jacobian using coloring and show contours of matrices and differences 1657c01495d3SJed Brown 1658e35cf81dSBarry Smith 165962fef451SLois Curfman McInnes Notes: 166062fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 166162fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 166262fef451SLois Curfman McInnes 166394b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1664dc5a77f8SLois Curfman McInnes flag parameter. 166562fef451SLois Curfman McInnes 166636851e7fSLois Curfman McInnes Level: developer 166736851e7fSLois Curfman McInnes 166862fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 166962fef451SLois Curfman McInnes 1670e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 167162fef451SLois Curfman McInnes @*/ 16727087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 16739b94acceSBarry Smith { 1674dfbe8321SBarry Smith PetscErrorCode ierr; 1675ace3abfcSBarry Smith PetscBool flag; 16763a40ed3dSBarry Smith 16773a40ed3dSBarry Smith PetscFunctionBegin; 16780700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 16790700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 16804482741eSBarry Smith PetscValidPointer(flg,5); 1681c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 16824ec14c9cSBarry Smith VecValidValues(X,2,PETSC_TRUE); 1683e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1684ebd3b9afSBarry Smith 1685ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1686ebd3b9afSBarry Smith 1687fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1688fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1689fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1690fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1691e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1692e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1693ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1694ebd3b9afSBarry Smith if (flag) { 1695ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1696ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1697ebd3b9afSBarry Smith } 1698e35cf81dSBarry Smith PetscFunctionReturn(0); 1699e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1700e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1701e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1702ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1703ebd3b9afSBarry Smith if (flag) { 1704ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1705ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1706ebd3b9afSBarry Smith } 1707e35cf81dSBarry Smith PetscFunctionReturn(0); 1708e35cf81dSBarry Smith } 1709e35cf81dSBarry Smith 1710c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1711e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1712d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1713e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1714d64ed03dSBarry Smith PetscStackPop; 1715d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1716a8054027SBarry Smith 17173b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 17183b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 17193b4f5425SBarry Smith snes->lagpreconditioner = -1; 17203b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1721a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1722a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1723a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1724a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1725a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1726a8054027SBarry Smith } 1727a8054027SBarry Smith 17286d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 17290700a824SBarry Smith /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 17300700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,4); */ 1731693365a8SJed Brown { 1732693365a8SJed Brown PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE; 1733693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr); 1734693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 1735693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 1736693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr); 1737693365a8SJed Brown if (flag || flag_draw || flag_contour) { 1738693365a8SJed Brown Mat Bexp_mine = PETSC_NULL,Bexp,FDexp; 1739693365a8SJed Brown MatStructure mstruct; 1740693365a8SJed Brown PetscViewer vdraw,vstdout; 17416b3a5b13SJed Brown PetscBool flg; 1742693365a8SJed Brown if (flag_operator) { 1743693365a8SJed Brown ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr); 1744693365a8SJed Brown Bexp = Bexp_mine; 1745693365a8SJed Brown } else { 1746693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 1747693365a8SJed Brown ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr); 1748693365a8SJed Brown if (flg) Bexp = *B; 1749693365a8SJed Brown else { 1750693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 1751693365a8SJed Brown ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr); 1752693365a8SJed Brown Bexp = Bexp_mine; 1753693365a8SJed Brown } 1754693365a8SJed Brown } 1755693365a8SJed Brown ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr); 1756693365a8SJed Brown ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr); 1757693365a8SJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 1758693365a8SJed Brown if (flag_draw || flag_contour) { 1759693365a8SJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 1760693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 1761693365a8SJed Brown } else vdraw = PETSC_NULL; 1762693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr); 1763693365a8SJed Brown if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);} 1764693365a8SJed Brown if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);} 1765693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr); 1766693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1767693365a8SJed Brown if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);} 1768693365a8SJed Brown ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 1769693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr); 1770693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1771693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 1772693365a8SJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 1773693365a8SJed Brown ierr = MatView(FDexp,vdraw);CHKERRQ(ierr); 1774693365a8SJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 1775693365a8SJed Brown } 1776693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 1777693365a8SJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 1778693365a8SJed Brown ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr); 1779693365a8SJed Brown ierr = MatDestroy(&FDexp);CHKERRQ(ierr); 1780693365a8SJed Brown } 1781693365a8SJed Brown } 17824c30e9fbSJed Brown { 17836719d8e4SJed Brown PetscBool flag = PETSC_FALSE,flag_display = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_threshold = PETSC_FALSE; 17846719d8e4SJed Brown PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON,threshold_rtol = 10*PETSC_SQRT_MACHINE_EPSILON; 17854c30e9fbSJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring",&flag,PETSC_NULL);CHKERRQ(ierr); 17866719d8e4SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_display",&flag_display,PETSC_NULL);CHKERRQ(ierr); 17874c30e9fbSJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 17884c30e9fbSJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 17896719d8e4SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold",&flag_threshold,PETSC_NULL);CHKERRQ(ierr); 17906719d8e4SJed Brown ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_rtol",&threshold_rtol,PETSC_NULL);CHKERRQ(ierr); 17916719d8e4SJed Brown ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_atol",&threshold_atol,PETSC_NULL);CHKERRQ(ierr); 17926719d8e4SJed Brown if (flag || flag_display || flag_draw || flag_contour || flag_threshold) { 17934c30e9fbSJed Brown Mat Bfd; 17944c30e9fbSJed Brown MatStructure mstruct; 17954c30e9fbSJed Brown PetscViewer vdraw,vstdout; 17964c30e9fbSJed Brown ISColoring iscoloring; 17974c30e9fbSJed Brown MatFDColoring matfdcoloring; 17984c30e9fbSJed Brown PetscErrorCode (*func)(SNES,Vec,Vec,void*); 17994c30e9fbSJed Brown void *funcctx; 18006719d8e4SJed Brown PetscReal norm1,norm2,normmax; 18014c30e9fbSJed Brown 18024c30e9fbSJed Brown ierr = MatDuplicate(*B,MAT_DO_NOT_COPY_VALUES,&Bfd);CHKERRQ(ierr); 18034c30e9fbSJed Brown ierr = MatGetColoring(Bfd,MATCOLORINGSL,&iscoloring);CHKERRQ(ierr); 18044c30e9fbSJed Brown ierr = MatFDColoringCreate(Bfd,iscoloring,&matfdcoloring);CHKERRQ(ierr); 18054c30e9fbSJed Brown ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 18064c30e9fbSJed Brown 18074c30e9fbSJed Brown /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */ 18084c30e9fbSJed Brown ierr = SNESGetFunction(snes,PETSC_NULL,&func,&funcctx);CHKERRQ(ierr); 18094c30e9fbSJed Brown ierr = MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode(*)(void))func,funcctx);CHKERRQ(ierr); 18104c30e9fbSJed Brown ierr = PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring,((PetscObject)snes)->prefix);CHKERRQ(ierr); 18114c30e9fbSJed Brown ierr = PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring,"coloring_");CHKERRQ(ierr); 18124c30e9fbSJed Brown ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr); 18134c30e9fbSJed Brown ierr = MatFDColoringApply(Bfd,matfdcoloring,X,&mstruct,snes);CHKERRQ(ierr); 18144c30e9fbSJed Brown ierr = MatFDColoringDestroy(&matfdcoloring);CHKERRQ(ierr); 18154c30e9fbSJed Brown 18164c30e9fbSJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 18174c30e9fbSJed Brown if (flag_draw || flag_contour) { 18184c30e9fbSJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Colored Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 18194c30e9fbSJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 18204c30e9fbSJed Brown } else vdraw = PETSC_NULL; 18214c30e9fbSJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit preconditioning Jacobian\n");CHKERRQ(ierr); 18226719d8e4SJed Brown if (flag_display) {ierr = MatView(*B,vstdout);CHKERRQ(ierr);} 18234c30e9fbSJed Brown if (vdraw) {ierr = MatView(*B,vdraw);CHKERRQ(ierr);} 18244c30e9fbSJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Colored Finite difference Jacobian\n");CHKERRQ(ierr); 18256719d8e4SJed Brown if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);} 18264c30e9fbSJed Brown if (vdraw) {ierr = MatView(Bfd,vdraw);CHKERRQ(ierr);} 18274c30e9fbSJed Brown ierr = MatAYPX(Bfd,-1.0,*B,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 18284c30e9fbSJed Brown ierr = MatNorm(Bfd,NORM_1,&norm1);CHKERRQ(ierr); 18296719d8e4SJed Brown ierr = MatNorm(Bfd,NORM_FROBENIUS,&norm2);CHKERRQ(ierr); 18304c30e9fbSJed Brown ierr = MatNorm(Bfd,NORM_MAX,&normmax);CHKERRQ(ierr); 18316719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian, norm1=%G normFrob=%G normmax=%G\n",norm1,norm2,normmax);CHKERRQ(ierr); 18326719d8e4SJed Brown if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);} 18334c30e9fbSJed Brown if (vdraw) { /* Always use contour for the difference */ 18344c30e9fbSJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 18354c30e9fbSJed Brown ierr = MatView(Bfd,vdraw);CHKERRQ(ierr); 18364c30e9fbSJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 18374c30e9fbSJed Brown } 18384c30e9fbSJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 18396719d8e4SJed Brown 18406719d8e4SJed Brown if (flag_threshold) { 18416719d8e4SJed Brown PetscInt bs,rstart,rend,i; 18426719d8e4SJed Brown ierr = MatGetBlockSize(*B,&bs);CHKERRQ(ierr); 18436719d8e4SJed Brown ierr = MatGetOwnershipRange(*B,&rstart,&rend);CHKERRQ(ierr); 18446719d8e4SJed Brown for (i=rstart; i<rend; i++) { 18456719d8e4SJed Brown const PetscScalar *ba,*ca; 18466719d8e4SJed Brown const PetscInt *bj,*cj; 18476719d8e4SJed Brown PetscInt bn,cn,j,maxentrycol = -1,maxdiffcol = -1,maxrdiffcol = -1; 18486719d8e4SJed Brown PetscReal maxentry = 0,maxdiff = 0,maxrdiff = 0; 18496719d8e4SJed Brown ierr = MatGetRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr); 18506719d8e4SJed Brown ierr = MatGetRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr); 18516719d8e4SJed Brown if (bn != cn) SETERRQ(((PetscObject)*A)->comm,PETSC_ERR_PLIB,"Unexpected different nonzero pattern in -snes_compare_coloring_threshold"); 18526719d8e4SJed Brown for (j=0; j<bn; j++) { 18536719d8e4SJed Brown PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 18546719d8e4SJed Brown if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) { 18556719d8e4SJed Brown maxentrycol = bj[j]; 18566719d8e4SJed Brown maxentry = PetscRealPart(ba[j]); 18576719d8e4SJed Brown } 18586719d8e4SJed Brown if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) { 18596719d8e4SJed Brown maxdiffcol = bj[j]; 18606719d8e4SJed Brown maxdiff = PetscRealPart(ca[j]); 18616719d8e4SJed Brown } 18626719d8e4SJed Brown if (rdiff > maxrdiff) { 18636719d8e4SJed Brown maxrdiffcol = bj[j]; 18646719d8e4SJed Brown maxrdiff = rdiff; 18656719d8e4SJed Brown } 18666719d8e4SJed Brown } 18676719d8e4SJed Brown if (maxrdiff > 1) { 18686719d8e4SJed 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); 18696719d8e4SJed Brown for (j=0; j<bn; j++) { 18706719d8e4SJed Brown PetscReal rdiff; 18716719d8e4SJed Brown rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 18726719d8e4SJed Brown if (rdiff > 1) { 18736719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout," (%D,%G:%G)",bj[j],PetscRealPart(ba[j]),PetscRealPart(ca[j]));CHKERRQ(ierr); 18746719d8e4SJed Brown } 18756719d8e4SJed Brown } 18766719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"\n",i,maxentry,maxdiff,maxrdiff);CHKERRQ(ierr); 18776719d8e4SJed Brown } 18786719d8e4SJed Brown ierr = MatRestoreRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr); 18796719d8e4SJed Brown ierr = MatRestoreRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr); 18806719d8e4SJed Brown } 18816719d8e4SJed Brown } 18824c30e9fbSJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 18834c30e9fbSJed Brown ierr = MatDestroy(&Bfd);CHKERRQ(ierr); 18844c30e9fbSJed Brown } 18854c30e9fbSJed Brown } 18863a40ed3dSBarry Smith PetscFunctionReturn(0); 18879b94acceSBarry Smith } 18889b94acceSBarry Smith 18894a2ae208SSatish Balay #undef __FUNCT__ 18904a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 18919b94acceSBarry Smith /*@C 18929b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1893044dda88SLois Curfman McInnes location to store the matrix. 18949b94acceSBarry Smith 18953f9fe445SBarry Smith Logically Collective on SNES and Mat 1896c7afd0dbSLois Curfman McInnes 18979b94acceSBarry Smith Input Parameters: 1898c7afd0dbSLois Curfman McInnes + snes - the SNES context 18999b94acceSBarry Smith . A - Jacobian matrix 19009b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 1901efd51863SBarry Smith . func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value) 1902c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1903efd51863SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value) 19049b94acceSBarry Smith 19059b94acceSBarry Smith Calling sequence of func: 19068d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 19079b94acceSBarry Smith 1908c7afd0dbSLois Curfman McInnes + x - input vector 19099b94acceSBarry Smith . A - Jacobian matrix 19109b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1911ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 19122b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1913c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 19149b94acceSBarry Smith 19159b94acceSBarry Smith Notes: 191694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 19172cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1918ac21db08SLois Curfman McInnes 1919ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 19209b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 19219b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 19229b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 19239b94acceSBarry Smith throughout the global iterations. 19249b94acceSBarry Smith 192516913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 192616913363SBarry Smith each matrix. 192716913363SBarry Smith 1928a8a26c1eSJed Brown If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument 1929a8a26c1eSJed Brown must be a MatFDColoring. 1930a8a26c1eSJed Brown 1931c3cc8fd1SJed Brown Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian. One common 1932c3cc8fd1SJed Brown example is to use the "Picard linearization" which only differentiates through the highest order parts of each term. 1933c3cc8fd1SJed Brown 193436851e7fSLois Curfman McInnes Level: beginner 193536851e7fSLois Curfman McInnes 19369b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 19379b94acceSBarry Smith 19383ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 19399b94acceSBarry Smith @*/ 19407087cfbeSBarry Smith PetscErrorCode SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 19419b94acceSBarry Smith { 1942dfbe8321SBarry Smith PetscErrorCode ierr; 19433a7fca6bSBarry Smith 19443a40ed3dSBarry Smith PetscFunctionBegin; 19450700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 19460700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 19470700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1948c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 194906975374SJed Brown if (B) PetscCheckSameComm(snes,1,B,3); 1950e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 19513a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 19523a7fca6bSBarry Smith if (A) { 19537dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 19546bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 19559b94acceSBarry Smith snes->jacobian = A; 19563a7fca6bSBarry Smith } 19573a7fca6bSBarry Smith if (B) { 19587dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 19596bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 19609b94acceSBarry Smith snes->jacobian_pre = B; 19613a7fca6bSBarry Smith } 19623a40ed3dSBarry Smith PetscFunctionReturn(0); 19639b94acceSBarry Smith } 196462fef451SLois Curfman McInnes 19654a2ae208SSatish Balay #undef __FUNCT__ 19664a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1967c2aafc4cSSatish Balay /*@C 1968b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1969b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1970b4fd4287SBarry Smith 1971c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1972c7afd0dbSLois Curfman McInnes 1973b4fd4287SBarry Smith Input Parameter: 1974b4fd4287SBarry Smith . snes - the nonlinear solver context 1975b4fd4287SBarry Smith 1976b4fd4287SBarry Smith Output Parameters: 1977c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1978b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 197970e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 198070e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1981fee21e36SBarry Smith 198236851e7fSLois Curfman McInnes Level: advanced 198336851e7fSLois Curfman McInnes 1984b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1985b4fd4287SBarry Smith @*/ 19867087cfbeSBarry Smith PetscErrorCode SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1987b4fd4287SBarry Smith { 19883a40ed3dSBarry Smith PetscFunctionBegin; 19890700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1990b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1991b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1992e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 199370e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 19943a40ed3dSBarry Smith PetscFunctionReturn(0); 1995b4fd4287SBarry Smith } 1996b4fd4287SBarry Smith 19979b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 19989b94acceSBarry Smith 19994a2ae208SSatish Balay #undef __FUNCT__ 20004a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 20019b94acceSBarry Smith /*@ 20029b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 2003272ac6f2SLois Curfman McInnes of a nonlinear solver. 20049b94acceSBarry Smith 2005fee21e36SBarry Smith Collective on SNES 2006fee21e36SBarry Smith 2007c7afd0dbSLois Curfman McInnes Input Parameters: 200870e92668SMatthew Knepley . snes - the SNES context 2009c7afd0dbSLois Curfman McInnes 2010272ac6f2SLois Curfman McInnes Notes: 2011272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 2012272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 2013272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 2014272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 2015272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 2016272ac6f2SLois Curfman McInnes 201736851e7fSLois Curfman McInnes Level: advanced 201836851e7fSLois Curfman McInnes 20199b94acceSBarry Smith .keywords: SNES, nonlinear, setup 20209b94acceSBarry Smith 20219b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 20229b94acceSBarry Smith @*/ 20237087cfbeSBarry Smith PetscErrorCode SNESSetUp(SNES snes) 20249b94acceSBarry Smith { 2025dfbe8321SBarry Smith PetscErrorCode ierr; 20263a40ed3dSBarry Smith 20273a40ed3dSBarry Smith PetscFunctionBegin; 20280700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 20294dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 20309b94acceSBarry Smith 20317adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 203285385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 203385385478SLisandro Dalcin } 203485385478SLisandro Dalcin 2035a63bb30eSJed Brown ierr = SNESGetFunction(snes,&snes->vec_func,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 203617186662SBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 203758c9b817SLisandro Dalcin if (snes->vec_rhs == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector"); 203858c9b817SLisandro Dalcin 203958c9b817SLisandro Dalcin if (!snes->vec_sol_update /* && snes->vec_sol */) { 204058c9b817SLisandro Dalcin ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr); 204158c9b817SLisandro Dalcin ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr); 204258c9b817SLisandro Dalcin } 204358c9b817SLisandro Dalcin 2044ef8dffc7SBarry Smith if (!snes->ops->computejacobian && snes->dm) { 2045214df951SJed Brown Mat J,B; 2046214df951SJed Brown ierr = DMCreateMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr); 2047214df951SJed Brown if (snes->mf_operator) { 2048214df951SJed Brown ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 2049214df951SJed Brown ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 2050214df951SJed Brown ierr = MatSetFromOptions(J);CHKERRQ(ierr); 2051214df951SJed Brown } else { 2052214df951SJed Brown J = B; 2053214df951SJed Brown ierr = PetscObjectReference((PetscObject)J);CHKERRQ(ierr); 2054214df951SJed Brown } 2055214df951SJed Brown ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,PETSC_NULL);CHKERRQ(ierr); 2056cab2e9ccSBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 2057214df951SJed Brown ierr = MatDestroy(&B);CHKERRQ(ierr); 20582ef29e96SBarry Smith } else if (!snes->jacobian && snes->ops->computejacobian == MatMFFDComputeJacobian) { 2059a8248277SBarry Smith Mat J; 2060a8248277SBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 2061a8248277SBarry Smith ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 2062a8248277SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 2063a8248277SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 2064a8248277SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 2065a8248277SBarry Smith } else if (snes->dm && snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) { 2066a8248277SBarry Smith Mat J,B; 2067a8248277SBarry Smith ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 2068a8248277SBarry Smith ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 2069a8248277SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 2070950540a4SJed Brown ierr = DMCreateMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr); 2071a8248277SBarry Smith ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,snes->funP);CHKERRQ(ierr); 2072a8248277SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 2073a8248277SBarry Smith ierr = MatDestroy(&B);CHKERRQ(ierr); 2074efd51863SBarry Smith } else if (snes->dm && !snes->jacobian_pre){ 2075efd51863SBarry Smith Mat J; 2076950540a4SJed Brown ierr = DMCreateMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 20773cbb28f5SBarry Smith ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2078efd51863SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 2079ef8dffc7SBarry Smith } 2080cfaf3a74SBarry 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()"); 2081c9b9fda1SJed Brown if (!snes->vec_func) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a vector when calling SNESSetFunction() or call SNESSetDM()"); 2082efd51863SBarry Smith 2083b710008aSBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);} 2084b710008aSBarry Smith 2085d25893d9SBarry Smith if (snes->ops->usercompute && !snes->user) { 2086d25893d9SBarry Smith ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr); 2087d25893d9SBarry Smith } 2088d25893d9SBarry Smith 2089410397dcSLisandro Dalcin if (snes->ops->setup) { 2090410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 2091410397dcSLisandro Dalcin } 209258c9b817SLisandro Dalcin 20937aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 20943a40ed3dSBarry Smith PetscFunctionReturn(0); 20959b94acceSBarry Smith } 20969b94acceSBarry Smith 20974a2ae208SSatish Balay #undef __FUNCT__ 209837596af1SLisandro Dalcin #define __FUNCT__ "SNESReset" 209937596af1SLisandro Dalcin /*@ 210037596af1SLisandro Dalcin SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats 210137596af1SLisandro Dalcin 210237596af1SLisandro Dalcin Collective on SNES 210337596af1SLisandro Dalcin 210437596af1SLisandro Dalcin Input Parameter: 210537596af1SLisandro Dalcin . snes - iterative context obtained from SNESCreate() 210637596af1SLisandro Dalcin 2107d25893d9SBarry Smith Level: intermediate 2108d25893d9SBarry Smith 2109d25893d9SBarry Smith Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext() 211037596af1SLisandro Dalcin 211137596af1SLisandro Dalcin .keywords: SNES, destroy 211237596af1SLisandro Dalcin 211337596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 211437596af1SLisandro Dalcin @*/ 211537596af1SLisandro Dalcin PetscErrorCode SNESReset(SNES snes) 211637596af1SLisandro Dalcin { 211737596af1SLisandro Dalcin PetscErrorCode ierr; 211837596af1SLisandro Dalcin 211937596af1SLisandro Dalcin PetscFunctionBegin; 212037596af1SLisandro Dalcin PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2121d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 2122d25893d9SBarry Smith ierr = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr); 2123d25893d9SBarry Smith snes->user = PETSC_NULL; 2124d25893d9SBarry Smith } 21258a23116dSBarry Smith if (snes->pc) { 21268a23116dSBarry Smith ierr = SNESReset(snes->pc);CHKERRQ(ierr); 21278a23116dSBarry Smith } 21288a23116dSBarry Smith 212937596af1SLisandro Dalcin if (snes->ops->reset) { 213037596af1SLisandro Dalcin ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr); 213137596af1SLisandro Dalcin } 213237596af1SLisandro Dalcin if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);} 21336bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 21346bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 21356bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr); 21366bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 21376bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 21386bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 2139c41d97abSJed Brown ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr); 2140c41d97abSJed Brown ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr); 214137596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 214237596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 214337596af1SLisandro Dalcin PetscFunctionReturn(0); 214437596af1SLisandro Dalcin } 214537596af1SLisandro Dalcin 214637596af1SLisandro Dalcin #undef __FUNCT__ 21474a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 214852baeb72SSatish Balay /*@ 21499b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 21509b94acceSBarry Smith with SNESCreate(). 21519b94acceSBarry Smith 2152c7afd0dbSLois Curfman McInnes Collective on SNES 2153c7afd0dbSLois Curfman McInnes 21549b94acceSBarry Smith Input Parameter: 21559b94acceSBarry Smith . snes - the SNES context 21569b94acceSBarry Smith 215736851e7fSLois Curfman McInnes Level: beginner 215836851e7fSLois Curfman McInnes 21599b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 21609b94acceSBarry Smith 216163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 21629b94acceSBarry Smith @*/ 21636bf464f9SBarry Smith PetscErrorCode SNESDestroy(SNES *snes) 21649b94acceSBarry Smith { 21656849ba73SBarry Smith PetscErrorCode ierr; 21663a40ed3dSBarry Smith 21673a40ed3dSBarry Smith PetscFunctionBegin; 21686bf464f9SBarry Smith if (!*snes) PetscFunctionReturn(0); 21696bf464f9SBarry Smith PetscValidHeaderSpecific((*snes),SNES_CLASSID,1); 21706bf464f9SBarry Smith if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);} 2171d4bb536fSBarry Smith 21726bf464f9SBarry Smith ierr = SNESReset((*snes));CHKERRQ(ierr); 21738a23116dSBarry Smith ierr = SNESDestroy(&(*snes)->pc);CHKERRQ(ierr); 21746b8b9a38SLisandro Dalcin 2175be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 21766bf464f9SBarry Smith ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr); 21776bf464f9SBarry Smith if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);} 21786d4c513bSLisandro Dalcin 21796bf464f9SBarry Smith ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr); 21806bf464f9SBarry Smith ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr); 21816b8b9a38SLisandro Dalcin 21826bf464f9SBarry Smith ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr); 21836bf464f9SBarry Smith if ((*snes)->ops->convergeddestroy) { 21846bf464f9SBarry Smith ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr); 21856b8b9a38SLisandro Dalcin } 21866bf464f9SBarry Smith if ((*snes)->conv_malloc) { 21876bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr); 21886bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr); 218958c9b817SLisandro Dalcin } 2190ea630c6eSPeter Brune ierr = PetscViewerDestroy(&(*snes)->ls_monitor);CHKERRQ(ierr); 21916bf464f9SBarry Smith ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr); 2192a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 21933a40ed3dSBarry Smith PetscFunctionReturn(0); 21949b94acceSBarry Smith } 21959b94acceSBarry Smith 21969b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 21979b94acceSBarry Smith 21984a2ae208SSatish Balay #undef __FUNCT__ 2199a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 2200a8054027SBarry Smith /*@ 2201a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 2202a8054027SBarry Smith 22033f9fe445SBarry Smith Logically Collective on SNES 2204a8054027SBarry Smith 2205a8054027SBarry Smith Input Parameters: 2206a8054027SBarry Smith + snes - the SNES context 2207a8054027SBarry 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 22083b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 2209a8054027SBarry Smith 2210a8054027SBarry Smith Options Database Keys: 2211a8054027SBarry Smith . -snes_lag_preconditioner <lag> 2212a8054027SBarry Smith 2213a8054027SBarry Smith Notes: 2214a8054027SBarry Smith The default is 1 2215a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2216a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 2217a8054027SBarry Smith 2218a8054027SBarry Smith Level: intermediate 2219a8054027SBarry Smith 2220a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2221a8054027SBarry Smith 2222e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 2223a8054027SBarry Smith 2224a8054027SBarry Smith @*/ 22257087cfbeSBarry Smith PetscErrorCode SNESSetLagPreconditioner(SNES snes,PetscInt lag) 2226a8054027SBarry Smith { 2227a8054027SBarry Smith PetscFunctionBegin; 22280700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2229e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 2230e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 2231c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 2232a8054027SBarry Smith snes->lagpreconditioner = lag; 2233a8054027SBarry Smith PetscFunctionReturn(0); 2234a8054027SBarry Smith } 2235a8054027SBarry Smith 2236a8054027SBarry Smith #undef __FUNCT__ 2237efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence" 2238efd51863SBarry Smith /*@ 2239efd51863SBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does 2240efd51863SBarry Smith 2241efd51863SBarry Smith Logically Collective on SNES 2242efd51863SBarry Smith 2243efd51863SBarry Smith Input Parameters: 2244efd51863SBarry Smith + snes - the SNES context 2245efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 2246efd51863SBarry Smith 2247efd51863SBarry Smith Options Database Keys: 2248efd51863SBarry Smith . -snes_grid_sequence <steps> 2249efd51863SBarry Smith 2250efd51863SBarry Smith Level: intermediate 2251efd51863SBarry Smith 2252c0df2a02SJed Brown Notes: 2253c0df2a02SJed Brown Use SNESGetSolution() to extract the fine grid solution after grid sequencing. 2254c0df2a02SJed Brown 2255efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2256efd51863SBarry Smith 2257efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 2258efd51863SBarry Smith 2259efd51863SBarry Smith @*/ 2260efd51863SBarry Smith PetscErrorCode SNESSetGridSequence(SNES snes,PetscInt steps) 2261efd51863SBarry Smith { 2262efd51863SBarry Smith PetscFunctionBegin; 2263efd51863SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2264efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes,steps,2); 2265efd51863SBarry Smith snes->gridsequence = steps; 2266efd51863SBarry Smith PetscFunctionReturn(0); 2267efd51863SBarry Smith } 2268efd51863SBarry Smith 2269efd51863SBarry Smith #undef __FUNCT__ 2270a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 2271a8054027SBarry Smith /*@ 2272a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 2273a8054027SBarry Smith 22743f9fe445SBarry Smith Not Collective 2275a8054027SBarry Smith 2276a8054027SBarry Smith Input Parameter: 2277a8054027SBarry Smith . snes - the SNES context 2278a8054027SBarry Smith 2279a8054027SBarry Smith Output Parameter: 2280a8054027SBarry 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 22813b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 2282a8054027SBarry Smith 2283a8054027SBarry Smith Options Database Keys: 2284a8054027SBarry Smith . -snes_lag_preconditioner <lag> 2285a8054027SBarry Smith 2286a8054027SBarry Smith Notes: 2287a8054027SBarry Smith The default is 1 2288a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2289a8054027SBarry Smith 2290a8054027SBarry Smith Level: intermediate 2291a8054027SBarry Smith 2292a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2293a8054027SBarry Smith 2294a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 2295a8054027SBarry Smith 2296a8054027SBarry Smith @*/ 22977087cfbeSBarry Smith PetscErrorCode SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 2298a8054027SBarry Smith { 2299a8054027SBarry Smith PetscFunctionBegin; 23000700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2301a8054027SBarry Smith *lag = snes->lagpreconditioner; 2302a8054027SBarry Smith PetscFunctionReturn(0); 2303a8054027SBarry Smith } 2304a8054027SBarry Smith 2305a8054027SBarry Smith #undef __FUNCT__ 2306e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 2307e35cf81dSBarry Smith /*@ 2308e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 2309e35cf81dSBarry Smith often the preconditioner is rebuilt. 2310e35cf81dSBarry Smith 23113f9fe445SBarry Smith Logically Collective on SNES 2312e35cf81dSBarry Smith 2313e35cf81dSBarry Smith Input Parameters: 2314e35cf81dSBarry Smith + snes - the SNES context 2315e35cf81dSBarry 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 2316fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 2317e35cf81dSBarry Smith 2318e35cf81dSBarry Smith Options Database Keys: 2319e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 2320e35cf81dSBarry Smith 2321e35cf81dSBarry Smith Notes: 2322e35cf81dSBarry Smith The default is 1 2323e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2324fe3ffe1eSBarry 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 2325fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 2326e35cf81dSBarry Smith 2327e35cf81dSBarry Smith Level: intermediate 2328e35cf81dSBarry Smith 2329e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2330e35cf81dSBarry Smith 2331e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 2332e35cf81dSBarry Smith 2333e35cf81dSBarry Smith @*/ 23347087cfbeSBarry Smith PetscErrorCode SNESSetLagJacobian(SNES snes,PetscInt lag) 2335e35cf81dSBarry Smith { 2336e35cf81dSBarry Smith PetscFunctionBegin; 23370700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2338e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 2339e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 2340c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 2341e35cf81dSBarry Smith snes->lagjacobian = lag; 2342e35cf81dSBarry Smith PetscFunctionReturn(0); 2343e35cf81dSBarry Smith } 2344e35cf81dSBarry Smith 2345e35cf81dSBarry Smith #undef __FUNCT__ 2346e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 2347e35cf81dSBarry Smith /*@ 2348e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 2349e35cf81dSBarry Smith 23503f9fe445SBarry Smith Not Collective 2351e35cf81dSBarry Smith 2352e35cf81dSBarry Smith Input Parameter: 2353e35cf81dSBarry Smith . snes - the SNES context 2354e35cf81dSBarry Smith 2355e35cf81dSBarry Smith Output Parameter: 2356e35cf81dSBarry 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 2357e35cf81dSBarry Smith the Jacobian is built etc. 2358e35cf81dSBarry Smith 2359e35cf81dSBarry Smith Options Database Keys: 2360e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 2361e35cf81dSBarry Smith 2362e35cf81dSBarry Smith Notes: 2363e35cf81dSBarry Smith The default is 1 2364e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2365e35cf81dSBarry Smith 2366e35cf81dSBarry Smith Level: intermediate 2367e35cf81dSBarry Smith 2368e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2369e35cf81dSBarry Smith 2370e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 2371e35cf81dSBarry Smith 2372e35cf81dSBarry Smith @*/ 23737087cfbeSBarry Smith PetscErrorCode SNESGetLagJacobian(SNES snes,PetscInt *lag) 2374e35cf81dSBarry Smith { 2375e35cf81dSBarry Smith PetscFunctionBegin; 23760700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2377e35cf81dSBarry Smith *lag = snes->lagjacobian; 2378e35cf81dSBarry Smith PetscFunctionReturn(0); 2379e35cf81dSBarry Smith } 2380e35cf81dSBarry Smith 2381e35cf81dSBarry Smith #undef __FUNCT__ 23824a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 23839b94acceSBarry Smith /*@ 2384d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 23859b94acceSBarry Smith 23863f9fe445SBarry Smith Logically Collective on SNES 2387c7afd0dbSLois Curfman McInnes 23889b94acceSBarry Smith Input Parameters: 2389c7afd0dbSLois Curfman McInnes + snes - the SNES context 239070441072SBarry Smith . abstol - absolute convergence tolerance 239133174efeSLois Curfman McInnes . rtol - relative convergence tolerance 239233174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 239333174efeSLois Curfman McInnes of the change in the solution between steps 239433174efeSLois Curfman McInnes . maxit - maximum number of iterations 2395c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 2396fee21e36SBarry Smith 239733174efeSLois Curfman McInnes Options Database Keys: 239870441072SBarry Smith + -snes_atol <abstol> - Sets abstol 2399c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 2400c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 2401c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 2402c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 24039b94acceSBarry Smith 2404d7a720efSLois Curfman McInnes Notes: 24059b94acceSBarry Smith The default maximum number of iterations is 50. 24069b94acceSBarry Smith The default maximum number of function evaluations is 1000. 24079b94acceSBarry Smith 240836851e7fSLois Curfman McInnes Level: intermediate 240936851e7fSLois Curfman McInnes 241033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 24119b94acceSBarry Smith 24122492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 24139b94acceSBarry Smith @*/ 24147087cfbeSBarry Smith PetscErrorCode SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 24159b94acceSBarry Smith { 24163a40ed3dSBarry Smith PetscFunctionBegin; 24170700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2418c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,abstol,2); 2419c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol,3); 2420c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,stol,4); 2421c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxit,5); 2422c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxf,6); 2423c5eb9154SBarry Smith 2424ab54825eSJed Brown if (abstol != PETSC_DEFAULT) { 2425ab54825eSJed Brown if (abstol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %G must be non-negative",abstol); 2426ab54825eSJed Brown snes->abstol = abstol; 2427ab54825eSJed Brown } 2428ab54825eSJed Brown if (rtol != PETSC_DEFAULT) { 2429ab54825eSJed 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); 2430ab54825eSJed Brown snes->rtol = rtol; 2431ab54825eSJed Brown } 2432ab54825eSJed Brown if (stol != PETSC_DEFAULT) { 2433ab54825eSJed Brown if (stol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %G must be non-negative",stol); 2434ab54825eSJed Brown snes->xtol = stol; 2435ab54825eSJed Brown } 2436ab54825eSJed Brown if (maxit != PETSC_DEFAULT) { 2437ab54825eSJed Brown if (maxit < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit); 2438ab54825eSJed Brown snes->max_its = maxit; 2439ab54825eSJed Brown } 2440ab54825eSJed Brown if (maxf != PETSC_DEFAULT) { 2441ab54825eSJed Brown if (maxf < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be non-negative",maxf); 2442ab54825eSJed Brown snes->max_funcs = maxf; 2443ab54825eSJed Brown } 24443a40ed3dSBarry Smith PetscFunctionReturn(0); 24459b94acceSBarry Smith } 24469b94acceSBarry Smith 24474a2ae208SSatish Balay #undef __FUNCT__ 24484a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 24499b94acceSBarry Smith /*@ 245033174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 245133174efeSLois Curfman McInnes 2452c7afd0dbSLois Curfman McInnes Not Collective 2453c7afd0dbSLois Curfman McInnes 245433174efeSLois Curfman McInnes Input Parameters: 2455c7afd0dbSLois Curfman McInnes + snes - the SNES context 245685385478SLisandro Dalcin . atol - absolute convergence tolerance 245733174efeSLois Curfman McInnes . rtol - relative convergence tolerance 245833174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 245933174efeSLois Curfman McInnes of the change in the solution between steps 246033174efeSLois Curfman McInnes . maxit - maximum number of iterations 2461c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 2462fee21e36SBarry Smith 246333174efeSLois Curfman McInnes Notes: 246433174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 246533174efeSLois Curfman McInnes 246636851e7fSLois Curfman McInnes Level: intermediate 246736851e7fSLois Curfman McInnes 246833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 246933174efeSLois Curfman McInnes 247033174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 247133174efeSLois Curfman McInnes @*/ 24727087cfbeSBarry Smith PetscErrorCode SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 247333174efeSLois Curfman McInnes { 24743a40ed3dSBarry Smith PetscFunctionBegin; 24750700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 247685385478SLisandro Dalcin if (atol) *atol = snes->abstol; 247733174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 247833174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 247933174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 248033174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 24813a40ed3dSBarry Smith PetscFunctionReturn(0); 248233174efeSLois Curfman McInnes } 248333174efeSLois Curfman McInnes 24844a2ae208SSatish Balay #undef __FUNCT__ 24854a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 248633174efeSLois Curfman McInnes /*@ 24879b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 24889b94acceSBarry Smith 24893f9fe445SBarry Smith Logically Collective on SNES 2490fee21e36SBarry Smith 2491c7afd0dbSLois Curfman McInnes Input Parameters: 2492c7afd0dbSLois Curfman McInnes + snes - the SNES context 2493c7afd0dbSLois Curfman McInnes - tol - tolerance 2494c7afd0dbSLois Curfman McInnes 24959b94acceSBarry Smith Options Database Key: 2496c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 24979b94acceSBarry Smith 249836851e7fSLois Curfman McInnes Level: intermediate 249936851e7fSLois Curfman McInnes 25009b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 25019b94acceSBarry Smith 25022492ecdbSBarry Smith .seealso: SNESSetTolerances() 25039b94acceSBarry Smith @*/ 25047087cfbeSBarry Smith PetscErrorCode SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 25059b94acceSBarry Smith { 25063a40ed3dSBarry Smith PetscFunctionBegin; 25070700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2508c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,tol,2); 25099b94acceSBarry Smith snes->deltatol = tol; 25103a40ed3dSBarry Smith PetscFunctionReturn(0); 25119b94acceSBarry Smith } 25129b94acceSBarry Smith 2513df9fa365SBarry Smith /* 2514df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 2515df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 2516df9fa365SBarry Smith macros instead of functions 2517df9fa365SBarry Smith */ 25184a2ae208SSatish Balay #undef __FUNCT__ 2519a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 25207087cfbeSBarry Smith PetscErrorCode SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 2521ce1608b8SBarry Smith { 2522dfbe8321SBarry Smith PetscErrorCode ierr; 2523ce1608b8SBarry Smith 2524ce1608b8SBarry Smith PetscFunctionBegin; 25250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2526a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 2527ce1608b8SBarry Smith PetscFunctionReturn(0); 2528ce1608b8SBarry Smith } 2529ce1608b8SBarry Smith 25304a2ae208SSatish Balay #undef __FUNCT__ 2531a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 25327087cfbeSBarry Smith PetscErrorCode SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2533df9fa365SBarry Smith { 2534dfbe8321SBarry Smith PetscErrorCode ierr; 2535df9fa365SBarry Smith 2536df9fa365SBarry Smith PetscFunctionBegin; 2537a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2538df9fa365SBarry Smith PetscFunctionReturn(0); 2539df9fa365SBarry Smith } 2540df9fa365SBarry Smith 25414a2ae208SSatish Balay #undef __FUNCT__ 2542a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 25436bf464f9SBarry Smith PetscErrorCode SNESMonitorLGDestroy(PetscDrawLG *draw) 2544df9fa365SBarry Smith { 2545dfbe8321SBarry Smith PetscErrorCode ierr; 2546df9fa365SBarry Smith 2547df9fa365SBarry Smith PetscFunctionBegin; 2548a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2549df9fa365SBarry Smith PetscFunctionReturn(0); 2550df9fa365SBarry Smith } 2551df9fa365SBarry Smith 25527087cfbeSBarry Smith extern PetscErrorCode SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 2553b271bb04SBarry Smith #undef __FUNCT__ 2554b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 25557087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 2556b271bb04SBarry Smith { 2557b271bb04SBarry Smith PetscDrawLG lg; 2558b271bb04SBarry Smith PetscErrorCode ierr; 2559b271bb04SBarry Smith PetscReal x,y,per; 2560b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 2561b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 2562b271bb04SBarry Smith PetscDraw draw; 2563b271bb04SBarry Smith PetscFunctionBegin; 2564b271bb04SBarry Smith if (!monctx) { 2565b271bb04SBarry Smith MPI_Comm comm; 2566b271bb04SBarry Smith 2567b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 2568b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 2569b271bb04SBarry Smith } 2570b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 2571b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2572b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2573b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 2574b271bb04SBarry Smith x = (PetscReal) n; 2575b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 2576b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2577b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2578b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2579b271bb04SBarry Smith } 2580b271bb04SBarry Smith 2581b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 2582b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2583b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2584b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 2585b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 2586b271bb04SBarry Smith x = (PetscReal) n; 2587b271bb04SBarry Smith y = 100.0*per; 2588b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2589b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2590b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2591b271bb04SBarry Smith } 2592b271bb04SBarry Smith 2593b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 2594b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2595b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2596b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 2597b271bb04SBarry Smith x = (PetscReal) n; 2598b271bb04SBarry Smith y = (prev - rnorm)/prev; 2599b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2600b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2601b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2602b271bb04SBarry Smith } 2603b271bb04SBarry Smith 2604b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 2605b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2606b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2607b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 2608b271bb04SBarry Smith x = (PetscReal) n; 2609b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 2610b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 2611b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2612b271bb04SBarry Smith } 2613b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2614b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2615b271bb04SBarry Smith } 2616b271bb04SBarry Smith prev = rnorm; 2617b271bb04SBarry Smith PetscFunctionReturn(0); 2618b271bb04SBarry Smith } 2619b271bb04SBarry Smith 2620b271bb04SBarry Smith #undef __FUNCT__ 2621b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 26227087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2623b271bb04SBarry Smith { 2624b271bb04SBarry Smith PetscErrorCode ierr; 2625b271bb04SBarry Smith 2626b271bb04SBarry Smith PetscFunctionBegin; 2627b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2628b271bb04SBarry Smith PetscFunctionReturn(0); 2629b271bb04SBarry Smith } 2630b271bb04SBarry Smith 2631b271bb04SBarry Smith #undef __FUNCT__ 2632b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 26336bf464f9SBarry Smith PetscErrorCode SNESMonitorLGRangeDestroy(PetscDrawLG *draw) 2634b271bb04SBarry Smith { 2635b271bb04SBarry Smith PetscErrorCode ierr; 2636b271bb04SBarry Smith 2637b271bb04SBarry Smith PetscFunctionBegin; 2638b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2639b271bb04SBarry Smith PetscFunctionReturn(0); 2640b271bb04SBarry Smith } 2641b271bb04SBarry Smith 26427a03ce2fSLisandro Dalcin #undef __FUNCT__ 26437a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor" 2644228d79bcSJed Brown /*@ 2645228d79bcSJed Brown SNESMonitor - runs the user provided monitor routines, if they exist 2646228d79bcSJed Brown 2647228d79bcSJed Brown Collective on SNES 2648228d79bcSJed Brown 2649228d79bcSJed Brown Input Parameters: 2650228d79bcSJed Brown + snes - nonlinear solver context obtained from SNESCreate() 2651228d79bcSJed Brown . iter - iteration number 2652228d79bcSJed Brown - rnorm - relative norm of the residual 2653228d79bcSJed Brown 2654228d79bcSJed Brown Notes: 2655228d79bcSJed Brown This routine is called by the SNES implementations. 2656228d79bcSJed Brown It does not typically need to be called by the user. 2657228d79bcSJed Brown 2658228d79bcSJed Brown Level: developer 2659228d79bcSJed Brown 2660228d79bcSJed Brown .seealso: SNESMonitorSet() 2661228d79bcSJed Brown @*/ 26627a03ce2fSLisandro Dalcin PetscErrorCode SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm) 26637a03ce2fSLisandro Dalcin { 26647a03ce2fSLisandro Dalcin PetscErrorCode ierr; 26657a03ce2fSLisandro Dalcin PetscInt i,n = snes->numbermonitors; 26667a03ce2fSLisandro Dalcin 26677a03ce2fSLisandro Dalcin PetscFunctionBegin; 26687a03ce2fSLisandro Dalcin for (i=0; i<n; i++) { 26697a03ce2fSLisandro Dalcin ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr); 26707a03ce2fSLisandro Dalcin } 26717a03ce2fSLisandro Dalcin PetscFunctionReturn(0); 26727a03ce2fSLisandro Dalcin } 26737a03ce2fSLisandro Dalcin 26749b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 26759b94acceSBarry Smith 26764a2ae208SSatish Balay #undef __FUNCT__ 2677a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 26789b94acceSBarry Smith /*@C 2679a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 26809b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 26819b94acceSBarry Smith progress. 26829b94acceSBarry Smith 26833f9fe445SBarry Smith Logically Collective on SNES 2684fee21e36SBarry Smith 2685c7afd0dbSLois Curfman McInnes Input Parameters: 2686c7afd0dbSLois Curfman McInnes + snes - the SNES context 2687c7afd0dbSLois Curfman McInnes . func - monitoring routine 2688b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 2689e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 2690b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 2691b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 26929b94acceSBarry Smith 2693c7afd0dbSLois Curfman McInnes Calling sequence of func: 2694a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 2695c7afd0dbSLois Curfman McInnes 2696c7afd0dbSLois Curfman McInnes + snes - the SNES context 2697c7afd0dbSLois Curfman McInnes . its - iteration number 2698c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 269940a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 27009b94acceSBarry Smith 27019665c990SLois Curfman McInnes Options Database Keys: 2702a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 2703a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 2704a6570f20SBarry Smith uses SNESMonitorLGCreate() 2705cca6129bSJed Brown - -snes_monitor_cancel - cancels all monitors that have 2706c7afd0dbSLois Curfman McInnes been hardwired into a code by 2707a6570f20SBarry Smith calls to SNESMonitorSet(), but 2708c7afd0dbSLois Curfman McInnes does not cancel those set via 2709c7afd0dbSLois Curfman McInnes the options database. 27109665c990SLois Curfman McInnes 2711639f9d9dSBarry Smith Notes: 27126bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 2713a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 27146bc08f3fSLois Curfman McInnes order in which they were set. 2715639f9d9dSBarry Smith 2716025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 2717025f1a04SBarry Smith 271836851e7fSLois Curfman McInnes Level: intermediate 271936851e7fSLois Curfman McInnes 27209b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 27219b94acceSBarry Smith 2722a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 27239b94acceSBarry Smith @*/ 2724c2efdce3SBarry Smith PetscErrorCode SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 27259b94acceSBarry Smith { 2726b90d0a6eSBarry Smith PetscInt i; 2727649052a6SBarry Smith PetscErrorCode ierr; 2728b90d0a6eSBarry Smith 27293a40ed3dSBarry Smith PetscFunctionBegin; 27300700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 273117186662SBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2732b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 2733649052a6SBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) { 2734649052a6SBarry Smith if (monitordestroy) { 2735c2efdce3SBarry Smith ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr); 2736649052a6SBarry Smith } 2737b90d0a6eSBarry Smith PetscFunctionReturn(0); 2738b90d0a6eSBarry Smith } 2739b90d0a6eSBarry Smith } 2740b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 2741b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 2742639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 27433a40ed3dSBarry Smith PetscFunctionReturn(0); 27449b94acceSBarry Smith } 27459b94acceSBarry Smith 27464a2ae208SSatish Balay #undef __FUNCT__ 2747a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 27485cd90555SBarry Smith /*@C 2749a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 27505cd90555SBarry Smith 27513f9fe445SBarry Smith Logically Collective on SNES 2752c7afd0dbSLois Curfman McInnes 27535cd90555SBarry Smith Input Parameters: 27545cd90555SBarry Smith . snes - the SNES context 27555cd90555SBarry Smith 27561a480d89SAdministrator Options Database Key: 2757a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 2758a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 2759c7afd0dbSLois Curfman McInnes set via the options database 27605cd90555SBarry Smith 27615cd90555SBarry Smith Notes: 27625cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 27635cd90555SBarry Smith 276436851e7fSLois Curfman McInnes Level: intermediate 276536851e7fSLois Curfman McInnes 27665cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 27675cd90555SBarry Smith 2768a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 27695cd90555SBarry Smith @*/ 27707087cfbeSBarry Smith PetscErrorCode SNESMonitorCancel(SNES snes) 27715cd90555SBarry Smith { 2772d952e501SBarry Smith PetscErrorCode ierr; 2773d952e501SBarry Smith PetscInt i; 2774d952e501SBarry Smith 27755cd90555SBarry Smith PetscFunctionBegin; 27760700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2777d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 2778d952e501SBarry Smith if (snes->monitordestroy[i]) { 27793c4aec1bSBarry Smith ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr); 2780d952e501SBarry Smith } 2781d952e501SBarry Smith } 27825cd90555SBarry Smith snes->numbermonitors = 0; 27835cd90555SBarry Smith PetscFunctionReturn(0); 27845cd90555SBarry Smith } 27855cd90555SBarry Smith 27864a2ae208SSatish Balay #undef __FUNCT__ 27874a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 27889b94acceSBarry Smith /*@C 27899b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 27909b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 27919b94acceSBarry Smith 27923f9fe445SBarry Smith Logically Collective on SNES 2793fee21e36SBarry Smith 2794c7afd0dbSLois Curfman McInnes Input Parameters: 2795c7afd0dbSLois Curfman McInnes + snes - the SNES context 2796c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 27977f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 27987f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 27999b94acceSBarry Smith 2800c7afd0dbSLois Curfman McInnes Calling sequence of func: 280106ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 2802c7afd0dbSLois Curfman McInnes 2803c7afd0dbSLois Curfman McInnes + snes - the SNES context 280406ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 2805c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 2806184914b5SBarry Smith . reason - reason for convergence/divergence 2807c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 28084b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 28094b27c08aSLois Curfman McInnes - f - 2-norm of function 28109b94acceSBarry Smith 281136851e7fSLois Curfman McInnes Level: advanced 281236851e7fSLois Curfman McInnes 28139b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 28149b94acceSBarry Smith 281585385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 28169b94acceSBarry Smith @*/ 28177087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 28189b94acceSBarry Smith { 28197f7931b9SBarry Smith PetscErrorCode ierr; 28207f7931b9SBarry Smith 28213a40ed3dSBarry Smith PetscFunctionBegin; 28220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 282385385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 28247f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 28257f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 28267f7931b9SBarry Smith } 282785385478SLisandro Dalcin snes->ops->converged = func; 28287f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 282985385478SLisandro Dalcin snes->cnvP = cctx; 28303a40ed3dSBarry Smith PetscFunctionReturn(0); 28319b94acceSBarry Smith } 28329b94acceSBarry Smith 28334a2ae208SSatish Balay #undef __FUNCT__ 28344a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 283552baeb72SSatish Balay /*@ 2836184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 2837184914b5SBarry Smith 2838184914b5SBarry Smith Not Collective 2839184914b5SBarry Smith 2840184914b5SBarry Smith Input Parameter: 2841184914b5SBarry Smith . snes - the SNES context 2842184914b5SBarry Smith 2843184914b5SBarry Smith Output Parameter: 28444d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 2845184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 2846184914b5SBarry Smith 2847184914b5SBarry Smith Level: intermediate 2848184914b5SBarry Smith 2849184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 2850184914b5SBarry Smith 2851184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 2852184914b5SBarry Smith 285385385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 2854184914b5SBarry Smith @*/ 28557087cfbeSBarry Smith PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 2856184914b5SBarry Smith { 2857184914b5SBarry Smith PetscFunctionBegin; 28580700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28594482741eSBarry Smith PetscValidPointer(reason,2); 2860184914b5SBarry Smith *reason = snes->reason; 2861184914b5SBarry Smith PetscFunctionReturn(0); 2862184914b5SBarry Smith } 2863184914b5SBarry Smith 28644a2ae208SSatish Balay #undef __FUNCT__ 28654a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 2866c9005455SLois Curfman McInnes /*@ 2867c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 2868c9005455SLois Curfman McInnes 28693f9fe445SBarry Smith Logically Collective on SNES 2870fee21e36SBarry Smith 2871c7afd0dbSLois Curfman McInnes Input Parameters: 2872c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 28738c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 2874cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 2875758f92a0SBarry Smith . na - size of a and its 287664731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 2877758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 2878c7afd0dbSLois Curfman McInnes 2879308dcc3eSBarry Smith Notes: 2880308dcc3eSBarry Smith If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a 2881308dcc3eSBarry Smith default array of length 10000 is allocated. 2882308dcc3eSBarry Smith 2883c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 2884c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 2885c9005455SLois Curfman McInnes during the section of code that is being timed. 2886c9005455SLois Curfman McInnes 288736851e7fSLois Curfman McInnes Level: intermediate 288836851e7fSLois Curfman McInnes 2889c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2890758f92a0SBarry Smith 289108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2892758f92a0SBarry Smith 2893c9005455SLois Curfman McInnes @*/ 28947087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset) 2895c9005455SLois Curfman McInnes { 2896308dcc3eSBarry Smith PetscErrorCode ierr; 2897308dcc3eSBarry Smith 28983a40ed3dSBarry Smith PetscFunctionBegin; 28990700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29004482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2901a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2902308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) { 2903308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2904308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr); 2905308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr); 2906308dcc3eSBarry Smith snes->conv_malloc = PETSC_TRUE; 2907308dcc3eSBarry Smith } 2908c9005455SLois Curfman McInnes snes->conv_hist = a; 2909758f92a0SBarry Smith snes->conv_hist_its = its; 2910758f92a0SBarry Smith snes->conv_hist_max = na; 2911a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2912758f92a0SBarry Smith snes->conv_hist_reset = reset; 2913758f92a0SBarry Smith PetscFunctionReturn(0); 2914758f92a0SBarry Smith } 2915758f92a0SBarry Smith 2916308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2917c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 2918c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 2919308dcc3eSBarry Smith EXTERN_C_BEGIN 2920308dcc3eSBarry Smith #undef __FUNCT__ 2921308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab" 2922308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 2923308dcc3eSBarry Smith { 2924308dcc3eSBarry Smith mxArray *mat; 2925308dcc3eSBarry Smith PetscInt i; 2926308dcc3eSBarry Smith PetscReal *ar; 2927308dcc3eSBarry Smith 2928308dcc3eSBarry Smith PetscFunctionBegin; 2929308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL); 2930308dcc3eSBarry Smith ar = (PetscReal*) mxGetData(mat); 2931308dcc3eSBarry Smith for (i=0; i<snes->conv_hist_len; i++) { 2932308dcc3eSBarry Smith ar[i] = snes->conv_hist[i]; 2933308dcc3eSBarry Smith } 2934308dcc3eSBarry Smith PetscFunctionReturn(mat); 2935308dcc3eSBarry Smith } 2936308dcc3eSBarry Smith EXTERN_C_END 2937308dcc3eSBarry Smith #endif 2938308dcc3eSBarry Smith 2939308dcc3eSBarry Smith 29404a2ae208SSatish Balay #undef __FUNCT__ 29414a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 29420c4c9dddSBarry Smith /*@C 2943758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 2944758f92a0SBarry Smith 29453f9fe445SBarry Smith Not Collective 2946758f92a0SBarry Smith 2947758f92a0SBarry Smith Input Parameter: 2948758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 2949758f92a0SBarry Smith 2950758f92a0SBarry Smith Output Parameters: 2951758f92a0SBarry Smith . a - array to hold history 2952758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 2953758f92a0SBarry Smith negative if not converged) for each solve. 2954758f92a0SBarry Smith - na - size of a and its 2955758f92a0SBarry Smith 2956758f92a0SBarry Smith Notes: 2957758f92a0SBarry Smith The calling sequence for this routine in Fortran is 2958758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 2959758f92a0SBarry Smith 2960758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 2961758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 2962758f92a0SBarry Smith during the section of code that is being timed. 2963758f92a0SBarry Smith 2964758f92a0SBarry Smith Level: intermediate 2965758f92a0SBarry Smith 2966758f92a0SBarry Smith .keywords: SNES, get, convergence, history 2967758f92a0SBarry Smith 2968758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 2969758f92a0SBarry Smith 2970758f92a0SBarry Smith @*/ 29717087cfbeSBarry Smith PetscErrorCode SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 2972758f92a0SBarry Smith { 2973758f92a0SBarry Smith PetscFunctionBegin; 29740700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2975758f92a0SBarry Smith if (a) *a = snes->conv_hist; 2976758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 2977758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 29783a40ed3dSBarry Smith PetscFunctionReturn(0); 2979c9005455SLois Curfman McInnes } 2980c9005455SLois Curfman McInnes 2981e74ef692SMatthew Knepley #undef __FUNCT__ 2982e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 2983ac226902SBarry Smith /*@C 298476b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 2985eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 29867e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 298776b2cf59SMatthew Knepley 29883f9fe445SBarry Smith Logically Collective on SNES 298976b2cf59SMatthew Knepley 299076b2cf59SMatthew Knepley Input Parameters: 299176b2cf59SMatthew Knepley . snes - The nonlinear solver context 299276b2cf59SMatthew Knepley . func - The function 299376b2cf59SMatthew Knepley 299476b2cf59SMatthew Knepley Calling sequence of func: 2995b5d30489SBarry Smith . func (SNES snes, PetscInt step); 299676b2cf59SMatthew Knepley 299776b2cf59SMatthew Knepley . step - The current step of the iteration 299876b2cf59SMatthew Knepley 2999fe97e370SBarry Smith Level: advanced 3000fe97e370SBarry Smith 3001fe97e370SBarry 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() 3002fe97e370SBarry Smith This is not used by most users. 300376b2cf59SMatthew Knepley 300476b2cf59SMatthew Knepley .keywords: SNES, update 3005b5d30489SBarry Smith 300685385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 300776b2cf59SMatthew Knepley @*/ 30087087cfbeSBarry Smith PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 300976b2cf59SMatthew Knepley { 301076b2cf59SMatthew Knepley PetscFunctionBegin; 30110700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 3012e7788613SBarry Smith snes->ops->update = func; 301376b2cf59SMatthew Knepley PetscFunctionReturn(0); 301476b2cf59SMatthew Knepley } 301576b2cf59SMatthew Knepley 3016e74ef692SMatthew Knepley #undef __FUNCT__ 3017e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 301876b2cf59SMatthew Knepley /*@ 301976b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 302076b2cf59SMatthew Knepley 302176b2cf59SMatthew Knepley Not collective 302276b2cf59SMatthew Knepley 302376b2cf59SMatthew Knepley Input Parameters: 302476b2cf59SMatthew Knepley . snes - The nonlinear solver context 302576b2cf59SMatthew Knepley . step - The current step of the iteration 302676b2cf59SMatthew Knepley 3027205452f4SMatthew Knepley Level: intermediate 3028205452f4SMatthew Knepley 302976b2cf59SMatthew Knepley .keywords: SNES, update 3030a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 303176b2cf59SMatthew Knepley @*/ 30327087cfbeSBarry Smith PetscErrorCode SNESDefaultUpdate(SNES snes, PetscInt step) 303376b2cf59SMatthew Knepley { 303476b2cf59SMatthew Knepley PetscFunctionBegin; 303576b2cf59SMatthew Knepley PetscFunctionReturn(0); 303676b2cf59SMatthew Knepley } 303776b2cf59SMatthew Knepley 30384a2ae208SSatish Balay #undef __FUNCT__ 30394a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 30409b94acceSBarry Smith /* 30419b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 30429b94acceSBarry Smith positive parameter delta. 30439b94acceSBarry Smith 30449b94acceSBarry Smith Input Parameters: 3045c7afd0dbSLois Curfman McInnes + snes - the SNES context 30469b94acceSBarry Smith . y - approximate solution of linear system 30479b94acceSBarry Smith . fnorm - 2-norm of current function 3048c7afd0dbSLois Curfman McInnes - delta - trust region size 30499b94acceSBarry Smith 30509b94acceSBarry Smith Output Parameters: 3051c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 30529b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 30539b94acceSBarry Smith region, and exceeds zero otherwise. 3054c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 30559b94acceSBarry Smith 30569b94acceSBarry Smith Note: 30574b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 30589b94acceSBarry Smith is set to be the maximum allowable step size. 30599b94acceSBarry Smith 30609b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 30619b94acceSBarry Smith */ 3062dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 30639b94acceSBarry Smith { 3064064f8208SBarry Smith PetscReal nrm; 3065ea709b57SSatish Balay PetscScalar cnorm; 3066dfbe8321SBarry Smith PetscErrorCode ierr; 30673a40ed3dSBarry Smith 30683a40ed3dSBarry Smith PetscFunctionBegin; 30690700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 30700700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 3071c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 3072184914b5SBarry Smith 3073064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 3074064f8208SBarry Smith if (nrm > *delta) { 3075064f8208SBarry Smith nrm = *delta/nrm; 3076064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 3077064f8208SBarry Smith cnorm = nrm; 30782dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 30799b94acceSBarry Smith *ynorm = *delta; 30809b94acceSBarry Smith } else { 30819b94acceSBarry Smith *gpnorm = 0.0; 3082064f8208SBarry Smith *ynorm = nrm; 30839b94acceSBarry Smith } 30843a40ed3dSBarry Smith PetscFunctionReturn(0); 30859b94acceSBarry Smith } 30869b94acceSBarry Smith 30874a2ae208SSatish Balay #undef __FUNCT__ 30884a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 30896ce558aeSBarry Smith /*@C 3090f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 3091f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 30929b94acceSBarry Smith 3093c7afd0dbSLois Curfman McInnes Collective on SNES 3094c7afd0dbSLois Curfman McInnes 3095b2002411SLois Curfman McInnes Input Parameters: 3096c7afd0dbSLois Curfman McInnes + snes - the SNES context 30973cebf274SJed Brown . b - the constant part of the equation F(x) = b, or PETSC_NULL to use zero. 309885385478SLisandro Dalcin - x - the solution vector. 30999b94acceSBarry Smith 3100b2002411SLois Curfman McInnes Notes: 31018ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 31028ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 31038ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 31048ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 31058ddd3da0SLois Curfman McInnes 310636851e7fSLois Curfman McInnes Level: beginner 310736851e7fSLois Curfman McInnes 31089b94acceSBarry Smith .keywords: SNES, nonlinear, solve 31099b94acceSBarry Smith 3110c0df2a02SJed Brown .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetGridSequence(), SNESGetSolution() 31119b94acceSBarry Smith @*/ 31127087cfbeSBarry Smith PetscErrorCode SNESSolve(SNES snes,Vec b,Vec x) 31139b94acceSBarry Smith { 3114dfbe8321SBarry Smith PetscErrorCode ierr; 3115ace3abfcSBarry Smith PetscBool flg; 3116eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 3117eabae89aSBarry Smith PetscViewer viewer; 3118efd51863SBarry Smith PetscInt grid; 3119a69afd8bSBarry Smith Vec xcreated = PETSC_NULL; 3120052efed2SBarry Smith 31213a40ed3dSBarry Smith PetscFunctionBegin; 31220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3123a69afd8bSBarry Smith if (x) PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3124a69afd8bSBarry Smith if (x) PetscCheckSameComm(snes,1,x,3); 31250700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 312685385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 312785385478SLisandro Dalcin 3128a69afd8bSBarry Smith if (!x && snes->dm) { 3129a69afd8bSBarry Smith ierr = DMCreateGlobalVector(snes->dm,&xcreated);CHKERRQ(ierr); 3130a69afd8bSBarry Smith x = xcreated; 3131a69afd8bSBarry Smith } 3132a69afd8bSBarry Smith 3133a8248277SBarry Smith for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);} 3134efd51863SBarry Smith for (grid=0; grid<snes->gridsequence+1; grid++) { 3135efd51863SBarry Smith 313685385478SLisandro Dalcin /* set solution vector */ 3137efd51863SBarry Smith if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);} 31386bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 313985385478SLisandro Dalcin snes->vec_sol = x; 314085385478SLisandro Dalcin /* set afine vector if provided */ 314185385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 31426bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 314385385478SLisandro Dalcin snes->vec_rhs = b; 314485385478SLisandro Dalcin 314570e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 31463f149594SLisandro Dalcin 31477eee914bSBarry Smith if (!grid) { 31487eee914bSBarry Smith if (snes->ops->computeinitialguess) { 3149d25893d9SBarry Smith ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr); 3150dd568438SSatish Balay } else if (snes->dm) { 3151dd568438SSatish Balay PetscBool ig; 3152dd568438SSatish Balay ierr = DMHasInitialGuess(snes->dm,&ig);CHKERRQ(ierr); 3153dd568438SSatish Balay if (ig) { 31547eee914bSBarry Smith ierr = DMComputeInitialGuess(snes->dm,snes->vec_sol);CHKERRQ(ierr); 31557eee914bSBarry Smith } 3156d25893d9SBarry Smith } 3157dd568438SSatish Balay } 3158d25893d9SBarry Smith 3159abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 316050ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 3161d5e45103SBarry Smith 31623f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 31634936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 316485385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 31654936397dSBarry Smith if (snes->domainerror){ 31664936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 31674936397dSBarry Smith snes->domainerror = PETSC_FALSE; 31684936397dSBarry Smith } 316917186662SBarry Smith if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 31703f149594SLisandro Dalcin 31717adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 3172eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 31737adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 3174eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 31756bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3176eabae89aSBarry Smith } 3177eabae89aSBarry Smith 317890d69ab7SBarry Smith flg = PETSC_FALSE; 3179acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr); 3180da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 31815968eb51SBarry Smith if (snes->printreason) { 3182a8248277SBarry Smith ierr = PetscViewerASCIIAddTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 31835968eb51SBarry Smith if (snes->reason > 0) { 3184a8248277SBarry Smith ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 31855968eb51SBarry Smith } else { 3186a8248277SBarry Smith ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 31875968eb51SBarry Smith } 3188a8248277SBarry Smith ierr = PetscViewerASCIISubtractTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 31895968eb51SBarry Smith } 31905968eb51SBarry Smith 31918501fc72SJed Brown flg = PETSC_FALSE; 31928501fc72SJed Brown ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view_solution_vtk",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 31938501fc72SJed Brown if (flg) { 31948501fc72SJed Brown PetscViewer viewer; 31958501fc72SJed Brown ierr = PetscViewerCreate(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr); 31968501fc72SJed Brown ierr = PetscViewerSetType(viewer,PETSCVIEWERVTK);CHKERRQ(ierr); 31978501fc72SJed Brown ierr = PetscViewerFileSetName(viewer,filename);CHKERRQ(ierr); 31988501fc72SJed Brown ierr = VecView(snes->vec_sol,viewer);CHKERRQ(ierr); 31998501fc72SJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 32008501fc72SJed Brown } 32018501fc72SJed Brown 3202e113a28aSBarry Smith if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged"); 3203efd51863SBarry Smith if (grid < snes->gridsequence) { 3204efd51863SBarry Smith DM fine; 3205efd51863SBarry Smith Vec xnew; 3206efd51863SBarry Smith Mat interp; 3207efd51863SBarry Smith 3208efd51863SBarry Smith ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr); 3209e727c939SJed Brown ierr = DMCreateInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr); 3210efd51863SBarry Smith ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr); 3211efd51863SBarry Smith ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr); 3212efd51863SBarry Smith ierr = MatDestroy(&interp);CHKERRQ(ierr); 3213efd51863SBarry Smith x = xnew; 3214efd51863SBarry Smith 3215efd51863SBarry Smith ierr = SNESReset(snes);CHKERRQ(ierr); 3216efd51863SBarry Smith ierr = SNESSetDM(snes,fine);CHKERRQ(ierr); 3217efd51863SBarry Smith ierr = DMDestroy(&fine);CHKERRQ(ierr); 3218a8248277SBarry Smith ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr); 3219efd51863SBarry Smith } 3220efd51863SBarry Smith } 3221a69afd8bSBarry Smith ierr = VecDestroy(&xcreated);CHKERRQ(ierr); 32223a40ed3dSBarry Smith PetscFunctionReturn(0); 32239b94acceSBarry Smith } 32249b94acceSBarry Smith 32259b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 32269b94acceSBarry Smith 32274a2ae208SSatish Balay #undef __FUNCT__ 32284a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 322982bf6240SBarry Smith /*@C 32304b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 32319b94acceSBarry Smith 3232fee21e36SBarry Smith Collective on SNES 3233fee21e36SBarry Smith 3234c7afd0dbSLois Curfman McInnes Input Parameters: 3235c7afd0dbSLois Curfman McInnes + snes - the SNES context 3236454a90a3SBarry Smith - type - a known method 3237c7afd0dbSLois Curfman McInnes 3238c7afd0dbSLois Curfman McInnes Options Database Key: 3239454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 3240c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 3241ae12b187SLois Curfman McInnes 32429b94acceSBarry Smith Notes: 3243e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 32444b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 3245c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 32464b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 3247c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 32489b94acceSBarry Smith 3249ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 3250ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 3251ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 3252ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 3253ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 3254ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 3255ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 3256ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 3257ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 3258b0a32e0cSBarry Smith appropriate method. 325936851e7fSLois Curfman McInnes 326036851e7fSLois Curfman McInnes Level: intermediate 3261a703fe33SLois Curfman McInnes 3262454a90a3SBarry Smith .keywords: SNES, set, type 3263435da068SBarry Smith 3264435da068SBarry Smith .seealso: SNESType, SNESCreate() 3265435da068SBarry Smith 32669b94acceSBarry Smith @*/ 32677087cfbeSBarry Smith PetscErrorCode SNESSetType(SNES snes,const SNESType type) 32689b94acceSBarry Smith { 3269dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 3270ace3abfcSBarry Smith PetscBool match; 32713a40ed3dSBarry Smith 32723a40ed3dSBarry Smith PetscFunctionBegin; 32730700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 32744482741eSBarry Smith PetscValidCharPointer(type,2); 327582bf6240SBarry Smith 32766831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 32770f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 327892ff6ae8SBarry Smith 32794b91b6eaSBarry Smith ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 3280e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 328175396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 3282*b5c23020SJed Brown if (snes->ops->destroy) { 3283*b5c23020SJed Brown ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); 3284*b5c23020SJed Brown snes->ops->destroy = PETSC_NULL; 3285*b5c23020SJed Brown } 328675396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 328775396ef9SLisandro Dalcin snes->ops->setup = 0; 328875396ef9SLisandro Dalcin snes->ops->solve = 0; 328975396ef9SLisandro Dalcin snes->ops->view = 0; 329075396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 329175396ef9SLisandro Dalcin snes->ops->destroy = 0; 329275396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 329375396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 3294454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 329503bfa161SLisandro Dalcin ierr = (*r)(snes);CHKERRQ(ierr); 32969fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS) 32979fb22e1aSBarry Smith if (PetscAMSPublishAll) { 32989fb22e1aSBarry Smith ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr); 32999fb22e1aSBarry Smith } 33009fb22e1aSBarry Smith #endif 33013a40ed3dSBarry Smith PetscFunctionReturn(0); 33029b94acceSBarry Smith } 33039b94acceSBarry Smith 3304a847f771SSatish Balay 33059b94acceSBarry Smith /* --------------------------------------------------------------------- */ 33064a2ae208SSatish Balay #undef __FUNCT__ 33074a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 330852baeb72SSatish Balay /*@ 33099b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 3310f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 33119b94acceSBarry Smith 3312fee21e36SBarry Smith Not Collective 3313fee21e36SBarry Smith 331436851e7fSLois Curfman McInnes Level: advanced 331536851e7fSLois Curfman McInnes 33169b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 33179b94acceSBarry Smith 33189b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 33199b94acceSBarry Smith @*/ 33207087cfbeSBarry Smith PetscErrorCode SNESRegisterDestroy(void) 33219b94acceSBarry Smith { 3322dfbe8321SBarry Smith PetscErrorCode ierr; 332382bf6240SBarry Smith 33243a40ed3dSBarry Smith PetscFunctionBegin; 33251441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 33264c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 33273a40ed3dSBarry Smith PetscFunctionReturn(0); 33289b94acceSBarry Smith } 33299b94acceSBarry Smith 33304a2ae208SSatish Balay #undef __FUNCT__ 33314a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 33329b94acceSBarry Smith /*@C 33339a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 33349b94acceSBarry Smith 3335c7afd0dbSLois Curfman McInnes Not Collective 3336c7afd0dbSLois Curfman McInnes 33379b94acceSBarry Smith Input Parameter: 33384b0e389bSBarry Smith . snes - nonlinear solver context 33399b94acceSBarry Smith 33409b94acceSBarry Smith Output Parameter: 33413a7fca6bSBarry Smith . type - SNES method (a character string) 33429b94acceSBarry Smith 334336851e7fSLois Curfman McInnes Level: intermediate 334436851e7fSLois Curfman McInnes 3345454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 33469b94acceSBarry Smith @*/ 33477087cfbeSBarry Smith PetscErrorCode SNESGetType(SNES snes,const SNESType *type) 33489b94acceSBarry Smith { 33493a40ed3dSBarry Smith PetscFunctionBegin; 33500700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 33514482741eSBarry Smith PetscValidPointer(type,2); 33527adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 33533a40ed3dSBarry Smith PetscFunctionReturn(0); 33549b94acceSBarry Smith } 33559b94acceSBarry Smith 33564a2ae208SSatish Balay #undef __FUNCT__ 33574a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 335852baeb72SSatish Balay /*@ 33599b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 3360c0df2a02SJed Brown stored. This is the fine grid solution when using SNESSetGridSequence(). 33619b94acceSBarry Smith 3362c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 3363c7afd0dbSLois Curfman McInnes 33649b94acceSBarry Smith Input Parameter: 33659b94acceSBarry Smith . snes - the SNES context 33669b94acceSBarry Smith 33679b94acceSBarry Smith Output Parameter: 33689b94acceSBarry Smith . x - the solution 33699b94acceSBarry Smith 337070e92668SMatthew Knepley Level: intermediate 337136851e7fSLois Curfman McInnes 33729b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 33739b94acceSBarry Smith 337485385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 33759b94acceSBarry Smith @*/ 33767087cfbeSBarry Smith PetscErrorCode SNESGetSolution(SNES snes,Vec *x) 33779b94acceSBarry Smith { 33783a40ed3dSBarry Smith PetscFunctionBegin; 33790700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 33804482741eSBarry Smith PetscValidPointer(x,2); 338185385478SLisandro Dalcin *x = snes->vec_sol; 338270e92668SMatthew Knepley PetscFunctionReturn(0); 338370e92668SMatthew Knepley } 338470e92668SMatthew Knepley 338570e92668SMatthew Knepley #undef __FUNCT__ 33864a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 338752baeb72SSatish Balay /*@ 33889b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 33899b94acceSBarry Smith stored. 33909b94acceSBarry Smith 3391c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 3392c7afd0dbSLois Curfman McInnes 33939b94acceSBarry Smith Input Parameter: 33949b94acceSBarry Smith . snes - the SNES context 33959b94acceSBarry Smith 33969b94acceSBarry Smith Output Parameter: 33979b94acceSBarry Smith . x - the solution update 33989b94acceSBarry Smith 339936851e7fSLois Curfman McInnes Level: advanced 340036851e7fSLois Curfman McInnes 34019b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 34029b94acceSBarry Smith 340385385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 34049b94acceSBarry Smith @*/ 34057087cfbeSBarry Smith PetscErrorCode SNESGetSolutionUpdate(SNES snes,Vec *x) 34069b94acceSBarry Smith { 34073a40ed3dSBarry Smith PetscFunctionBegin; 34080700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 34094482741eSBarry Smith PetscValidPointer(x,2); 341085385478SLisandro Dalcin *x = snes->vec_sol_update; 34113a40ed3dSBarry Smith PetscFunctionReturn(0); 34129b94acceSBarry Smith } 34139b94acceSBarry Smith 34144a2ae208SSatish Balay #undef __FUNCT__ 34154a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 34169b94acceSBarry Smith /*@C 34173638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 34189b94acceSBarry Smith 3419a63bb30eSJed Brown Not Collective, but Vec is parallel if SNES is parallel. Collective if Vec is requested, but has not been created yet. 3420c7afd0dbSLois Curfman McInnes 34219b94acceSBarry Smith Input Parameter: 34229b94acceSBarry Smith . snes - the SNES context 34239b94acceSBarry Smith 34249b94acceSBarry Smith Output Parameter: 34257bf4e008SBarry Smith + r - the function (or PETSC_NULL) 342670e92668SMatthew Knepley . func - the function (or PETSC_NULL) 342770e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 34289b94acceSBarry Smith 342936851e7fSLois Curfman McInnes Level: advanced 343036851e7fSLois Curfman McInnes 3431a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 34329b94acceSBarry Smith 34334b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 34349b94acceSBarry Smith @*/ 34357087cfbeSBarry Smith PetscErrorCode SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 34369b94acceSBarry Smith { 3437a63bb30eSJed Brown PetscErrorCode ierr; 3438a63bb30eSJed Brown 34393a40ed3dSBarry Smith PetscFunctionBegin; 34400700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3441a63bb30eSJed Brown if (r) { 3442a63bb30eSJed Brown if (!snes->vec_func) { 3443a63bb30eSJed Brown if (snes->vec_rhs) { 3444a63bb30eSJed Brown ierr = VecDuplicate(snes->vec_rhs,&snes->vec_func);CHKERRQ(ierr); 3445a63bb30eSJed Brown } else if (snes->vec_sol) { 3446a63bb30eSJed Brown ierr = VecDuplicate(snes->vec_sol,&snes->vec_func);CHKERRQ(ierr); 3447a63bb30eSJed Brown } else if (snes->dm) { 3448a63bb30eSJed Brown ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 3449a63bb30eSJed Brown } 3450a63bb30eSJed Brown } 3451a63bb30eSJed Brown *r = snes->vec_func; 3452a63bb30eSJed Brown } 3453e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 345470e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 34553a40ed3dSBarry Smith PetscFunctionReturn(0); 34569b94acceSBarry Smith } 34579b94acceSBarry Smith 3458c79ef259SPeter Brune /*@C 3459c79ef259SPeter Brune SNESGetGS - Returns the GS function and context. 3460c79ef259SPeter Brune 3461c79ef259SPeter Brune Input Parameter: 3462c79ef259SPeter Brune . snes - the SNES context 3463c79ef259SPeter Brune 3464c79ef259SPeter Brune Output Parameter: 3465c79ef259SPeter Brune + gsfunc - the function (or PETSC_NULL) 3466c79ef259SPeter Brune - ctx - the function context (or PETSC_NULL) 3467c79ef259SPeter Brune 3468c79ef259SPeter Brune Level: advanced 3469c79ef259SPeter Brune 3470c79ef259SPeter Brune .keywords: SNES, nonlinear, get, function 3471c79ef259SPeter Brune 3472c79ef259SPeter Brune .seealso: SNESSetGS(), SNESGetFunction() 3473c79ef259SPeter Brune @*/ 3474c79ef259SPeter Brune 34754a2ae208SSatish Balay #undef __FUNCT__ 3476646217ecSPeter Brune #define __FUNCT__ "SNESGetGS" 3477646217ecSPeter Brune PetscErrorCode SNESGetGS (SNES snes, PetscErrorCode(**func)(SNES, Vec, Vec, void*), void ** ctx) 3478646217ecSPeter Brune { 3479646217ecSPeter Brune PetscFunctionBegin; 3480646217ecSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3481646217ecSPeter Brune if (func) *func = snes->ops->computegs; 3482646217ecSPeter Brune if (ctx) *ctx = snes->funP; 3483646217ecSPeter Brune PetscFunctionReturn(0); 3484646217ecSPeter Brune } 3485646217ecSPeter Brune 34864a2ae208SSatish Balay #undef __FUNCT__ 34874a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 34883c7409f5SSatish Balay /*@C 34893c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 3490d850072dSLois Curfman McInnes SNES options in the database. 34913c7409f5SSatish Balay 34923f9fe445SBarry Smith Logically Collective on SNES 3493fee21e36SBarry Smith 3494c7afd0dbSLois Curfman McInnes Input Parameter: 3495c7afd0dbSLois Curfman McInnes + snes - the SNES context 3496c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3497c7afd0dbSLois Curfman McInnes 3498d850072dSLois Curfman McInnes Notes: 3499a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3500c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3501d850072dSLois Curfman McInnes 350236851e7fSLois Curfman McInnes Level: advanced 350336851e7fSLois Curfman McInnes 35043c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 3505a86d99e1SLois Curfman McInnes 3506a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 35073c7409f5SSatish Balay @*/ 35087087cfbeSBarry Smith PetscErrorCode SNESSetOptionsPrefix(SNES snes,const char prefix[]) 35093c7409f5SSatish Balay { 3510dfbe8321SBarry Smith PetscErrorCode ierr; 35113c7409f5SSatish Balay 35123a40ed3dSBarry Smith PetscFunctionBegin; 35130700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3514639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 35151cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 351694b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 35173a40ed3dSBarry Smith PetscFunctionReturn(0); 35183c7409f5SSatish Balay } 35193c7409f5SSatish Balay 35204a2ae208SSatish Balay #undef __FUNCT__ 35214a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 35223c7409f5SSatish Balay /*@C 3523f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 3524d850072dSLois Curfman McInnes SNES options in the database. 35253c7409f5SSatish Balay 35263f9fe445SBarry Smith Logically Collective on SNES 3527fee21e36SBarry Smith 3528c7afd0dbSLois Curfman McInnes Input Parameters: 3529c7afd0dbSLois Curfman McInnes + snes - the SNES context 3530c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3531c7afd0dbSLois Curfman McInnes 3532d850072dSLois Curfman McInnes Notes: 3533a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3534c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3535d850072dSLois Curfman McInnes 353636851e7fSLois Curfman McInnes Level: advanced 353736851e7fSLois Curfman McInnes 35383c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 3539a86d99e1SLois Curfman McInnes 3540a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 35413c7409f5SSatish Balay @*/ 35427087cfbeSBarry Smith PetscErrorCode SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 35433c7409f5SSatish Balay { 3544dfbe8321SBarry Smith PetscErrorCode ierr; 35453c7409f5SSatish Balay 35463a40ed3dSBarry Smith PetscFunctionBegin; 35470700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3548639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 35491cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 355094b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 35513a40ed3dSBarry Smith PetscFunctionReturn(0); 35523c7409f5SSatish Balay } 35533c7409f5SSatish Balay 35544a2ae208SSatish Balay #undef __FUNCT__ 35554a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 35569ab63eb5SSatish Balay /*@C 35573c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 35583c7409f5SSatish Balay SNES options in the database. 35593c7409f5SSatish Balay 3560c7afd0dbSLois Curfman McInnes Not Collective 3561c7afd0dbSLois Curfman McInnes 35623c7409f5SSatish Balay Input Parameter: 35633c7409f5SSatish Balay . snes - the SNES context 35643c7409f5SSatish Balay 35653c7409f5SSatish Balay Output Parameter: 35663c7409f5SSatish Balay . prefix - pointer to the prefix string used 35673c7409f5SSatish Balay 35684ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 35699ab63eb5SSatish Balay sufficient length to hold the prefix. 35709ab63eb5SSatish Balay 357136851e7fSLois Curfman McInnes Level: advanced 357236851e7fSLois Curfman McInnes 35733c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 3574a86d99e1SLois Curfman McInnes 3575a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 35763c7409f5SSatish Balay @*/ 35777087cfbeSBarry Smith PetscErrorCode SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 35783c7409f5SSatish Balay { 3579dfbe8321SBarry Smith PetscErrorCode ierr; 35803c7409f5SSatish Balay 35813a40ed3dSBarry Smith PetscFunctionBegin; 35820700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3583639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 35843a40ed3dSBarry Smith PetscFunctionReturn(0); 35853c7409f5SSatish Balay } 35863c7409f5SSatish Balay 3587b2002411SLois Curfman McInnes 35884a2ae208SSatish Balay #undef __FUNCT__ 35894a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 35903cea93caSBarry Smith /*@C 35913cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 35923cea93caSBarry Smith 35937f6c08e0SMatthew Knepley Level: advanced 35943cea93caSBarry Smith @*/ 35957087cfbeSBarry Smith PetscErrorCode SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 3596b2002411SLois Curfman McInnes { 3597e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 3598dfbe8321SBarry Smith PetscErrorCode ierr; 3599b2002411SLois Curfman McInnes 3600b2002411SLois Curfman McInnes PetscFunctionBegin; 3601b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 3602c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 3603b2002411SLois Curfman McInnes PetscFunctionReturn(0); 3604b2002411SLois Curfman McInnes } 3605da9b6338SBarry Smith 3606da9b6338SBarry Smith #undef __FUNCT__ 3607da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 36087087cfbeSBarry Smith PetscErrorCode SNESTestLocalMin(SNES snes) 3609da9b6338SBarry Smith { 3610dfbe8321SBarry Smith PetscErrorCode ierr; 361177431f27SBarry Smith PetscInt N,i,j; 3612da9b6338SBarry Smith Vec u,uh,fh; 3613da9b6338SBarry Smith PetscScalar value; 3614da9b6338SBarry Smith PetscReal norm; 3615da9b6338SBarry Smith 3616da9b6338SBarry Smith PetscFunctionBegin; 3617da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 3618da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 3619da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 3620da9b6338SBarry Smith 3621da9b6338SBarry Smith /* currently only works for sequential */ 3622da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 3623da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 3624da9b6338SBarry Smith for (i=0; i<N; i++) { 3625da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 362677431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 3627da9b6338SBarry Smith for (j=-10; j<11; j++) { 3628ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 3629da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 36303ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 3631da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 363277431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 3633da9b6338SBarry Smith value = -value; 3634da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 3635da9b6338SBarry Smith } 3636da9b6338SBarry Smith } 36376bf464f9SBarry Smith ierr = VecDestroy(&uh);CHKERRQ(ierr); 36386bf464f9SBarry Smith ierr = VecDestroy(&fh);CHKERRQ(ierr); 3639da9b6338SBarry Smith PetscFunctionReturn(0); 3640da9b6338SBarry Smith } 364171f87433Sdalcinl 364271f87433Sdalcinl #undef __FUNCT__ 3643fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 364471f87433Sdalcinl /*@ 3645fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 364671f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 364771f87433Sdalcinl Newton method. 364871f87433Sdalcinl 36493f9fe445SBarry Smith Logically Collective on SNES 365071f87433Sdalcinl 365171f87433Sdalcinl Input Parameters: 365271f87433Sdalcinl + snes - SNES context 365371f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 365471f87433Sdalcinl 365564ba62caSBarry Smith Options Database: 365664ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 365764ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 365864ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 365964ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 366064ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 366164ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 366264ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 366364ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 366464ba62caSBarry Smith 366571f87433Sdalcinl Notes: 366671f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 366771f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 366871f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 366971f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 367071f87433Sdalcinl solver. 367171f87433Sdalcinl 367271f87433Sdalcinl Level: advanced 367371f87433Sdalcinl 367471f87433Sdalcinl Reference: 367571f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 367671f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 367771f87433Sdalcinl 367871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 367971f87433Sdalcinl 3680fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 368171f87433Sdalcinl @*/ 36827087cfbeSBarry Smith PetscErrorCode SNESKSPSetUseEW(SNES snes,PetscBool flag) 368371f87433Sdalcinl { 368471f87433Sdalcinl PetscFunctionBegin; 36850700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3686acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flag,2); 368771f87433Sdalcinl snes->ksp_ewconv = flag; 368871f87433Sdalcinl PetscFunctionReturn(0); 368971f87433Sdalcinl } 369071f87433Sdalcinl 369171f87433Sdalcinl #undef __FUNCT__ 3692fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 369371f87433Sdalcinl /*@ 3694fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 369571f87433Sdalcinl for computing relative tolerance for linear solvers within an 369671f87433Sdalcinl inexact Newton method. 369771f87433Sdalcinl 369871f87433Sdalcinl Not Collective 369971f87433Sdalcinl 370071f87433Sdalcinl Input Parameter: 370171f87433Sdalcinl . snes - SNES context 370271f87433Sdalcinl 370371f87433Sdalcinl Output Parameter: 370471f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 370571f87433Sdalcinl 370671f87433Sdalcinl Notes: 370771f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 370871f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 370971f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 371071f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 371171f87433Sdalcinl solver. 371271f87433Sdalcinl 371371f87433Sdalcinl Level: advanced 371471f87433Sdalcinl 371571f87433Sdalcinl Reference: 371671f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 371771f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 371871f87433Sdalcinl 371971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 372071f87433Sdalcinl 3721fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 372271f87433Sdalcinl @*/ 37237087cfbeSBarry Smith PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 372471f87433Sdalcinl { 372571f87433Sdalcinl PetscFunctionBegin; 37260700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 372771f87433Sdalcinl PetscValidPointer(flag,2); 372871f87433Sdalcinl *flag = snes->ksp_ewconv; 372971f87433Sdalcinl PetscFunctionReturn(0); 373071f87433Sdalcinl } 373171f87433Sdalcinl 373271f87433Sdalcinl #undef __FUNCT__ 3733fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 373471f87433Sdalcinl /*@ 3735fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 373671f87433Sdalcinl convergence criteria for the linear solvers within an inexact 373771f87433Sdalcinl Newton method. 373871f87433Sdalcinl 37393f9fe445SBarry Smith Logically Collective on SNES 374071f87433Sdalcinl 374171f87433Sdalcinl Input Parameters: 374271f87433Sdalcinl + snes - SNES context 374371f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 374471f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 374571f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 374671f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 374771f87433Sdalcinl (0 <= gamma2 <= 1) 374871f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 374971f87433Sdalcinl . alpha2 - power for safeguard 375071f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 375171f87433Sdalcinl 375271f87433Sdalcinl Note: 375371f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 375471f87433Sdalcinl 375571f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 375671f87433Sdalcinl 375771f87433Sdalcinl Level: advanced 375871f87433Sdalcinl 375971f87433Sdalcinl Reference: 376071f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 376171f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 376271f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 376371f87433Sdalcinl 376471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 376571f87433Sdalcinl 3766fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 376771f87433Sdalcinl @*/ 37687087cfbeSBarry Smith PetscErrorCode SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 376971f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 377071f87433Sdalcinl { 3771fa9f3622SBarry Smith SNESKSPEW *kctx; 377271f87433Sdalcinl PetscFunctionBegin; 37730700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3774fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3775e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 3776c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,version,2); 3777c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_0,3); 3778c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_max,4); 3779c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,gamma,5); 3780c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha,6); 3781c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha2,7); 3782c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,threshold,8); 378371f87433Sdalcinl 378471f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 378571f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 378671f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 378771f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 378871f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 378971f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 379071f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 379171f87433Sdalcinl 379271f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 3793e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 379471f87433Sdalcinl } 379571f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 3796e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 379771f87433Sdalcinl } 379871f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 3799e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 380071f87433Sdalcinl } 380171f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 3802e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 380371f87433Sdalcinl } 380471f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 3805e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 380671f87433Sdalcinl } 380771f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 3808e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 380971f87433Sdalcinl } 381071f87433Sdalcinl PetscFunctionReturn(0); 381171f87433Sdalcinl } 381271f87433Sdalcinl 381371f87433Sdalcinl #undef __FUNCT__ 3814fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 381571f87433Sdalcinl /*@ 3816fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 381771f87433Sdalcinl convergence criteria for the linear solvers within an inexact 381871f87433Sdalcinl Newton method. 381971f87433Sdalcinl 382071f87433Sdalcinl Not Collective 382171f87433Sdalcinl 382271f87433Sdalcinl Input Parameters: 382371f87433Sdalcinl snes - SNES context 382471f87433Sdalcinl 382571f87433Sdalcinl Output Parameters: 382671f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 382771f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 382871f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 382971f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 383071f87433Sdalcinl (0 <= gamma2 <= 1) 383171f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 383271f87433Sdalcinl . alpha2 - power for safeguard 383371f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 383471f87433Sdalcinl 383571f87433Sdalcinl Level: advanced 383671f87433Sdalcinl 383771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 383871f87433Sdalcinl 3839fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 384071f87433Sdalcinl @*/ 38417087cfbeSBarry Smith PetscErrorCode SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 384271f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 384371f87433Sdalcinl { 3844fa9f3622SBarry Smith SNESKSPEW *kctx; 384571f87433Sdalcinl PetscFunctionBegin; 38460700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3847fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3848e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 384971f87433Sdalcinl if(version) *version = kctx->version; 385071f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 385171f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 385271f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 385371f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 385471f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 385571f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 385671f87433Sdalcinl PetscFunctionReturn(0); 385771f87433Sdalcinl } 385871f87433Sdalcinl 385971f87433Sdalcinl #undef __FUNCT__ 3860fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 3861fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 386271f87433Sdalcinl { 386371f87433Sdalcinl PetscErrorCode ierr; 3864fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 386571f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 386671f87433Sdalcinl 386771f87433Sdalcinl PetscFunctionBegin; 3868e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 386971f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 387071f87433Sdalcinl rtol = kctx->rtol_0; 387171f87433Sdalcinl } else { 387271f87433Sdalcinl if (kctx->version == 1) { 387371f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 387471f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 387571f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 387671f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 387771f87433Sdalcinl } else if (kctx->version == 2) { 387871f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 387971f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 388071f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 388171f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 388271f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 388371f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 388471f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 388571f87433Sdalcinl stol = PetscMax(rtol,stol); 388671f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 388771f87433Sdalcinl /* safeguard: avoid oversolving */ 388871f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 388971f87433Sdalcinl stol = PetscMax(rtol,stol); 389071f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 3891e32f2f54SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 389271f87433Sdalcinl } 389371f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 389471f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 389571f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 389671f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 389771f87433Sdalcinl PetscFunctionReturn(0); 389871f87433Sdalcinl } 389971f87433Sdalcinl 390071f87433Sdalcinl #undef __FUNCT__ 3901fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 3902fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 390371f87433Sdalcinl { 390471f87433Sdalcinl PetscErrorCode ierr; 3905fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 390671f87433Sdalcinl PCSide pcside; 390771f87433Sdalcinl Vec lres; 390871f87433Sdalcinl 390971f87433Sdalcinl PetscFunctionBegin; 3910e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 391171f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 391271f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 391371f87433Sdalcinl if (kctx->version == 1) { 3914b037da10SBarry Smith ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr); 391571f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 391671f87433Sdalcinl /* KSP residual is true linear residual */ 391771f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 391871f87433Sdalcinl } else { 391971f87433Sdalcinl /* KSP residual is preconditioned residual */ 392071f87433Sdalcinl /* compute true linear residual norm */ 392171f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 392271f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 392371f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 392471f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 39256bf464f9SBarry Smith ierr = VecDestroy(&lres);CHKERRQ(ierr); 392671f87433Sdalcinl } 392771f87433Sdalcinl } 392871f87433Sdalcinl PetscFunctionReturn(0); 392971f87433Sdalcinl } 393071f87433Sdalcinl 393171f87433Sdalcinl #undef __FUNCT__ 393271f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 393371f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 393471f87433Sdalcinl { 393571f87433Sdalcinl PetscErrorCode ierr; 393671f87433Sdalcinl 393771f87433Sdalcinl PetscFunctionBegin; 3938fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 393971f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 3940fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 394171f87433Sdalcinl PetscFunctionReturn(0); 394271f87433Sdalcinl } 39436c699258SBarry Smith 39446c699258SBarry Smith #undef __FUNCT__ 39456c699258SBarry Smith #define __FUNCT__ "SNESSetDM" 39466c699258SBarry Smith /*@ 39476c699258SBarry Smith SNESSetDM - Sets the DM that may be used by some preconditioners 39486c699258SBarry Smith 39493f9fe445SBarry Smith Logically Collective on SNES 39506c699258SBarry Smith 39516c699258SBarry Smith Input Parameters: 39526c699258SBarry Smith + snes - the preconditioner context 39536c699258SBarry Smith - dm - the dm 39546c699258SBarry Smith 39556c699258SBarry Smith Level: intermediate 39566c699258SBarry Smith 39576c699258SBarry Smith 39586c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM() 39596c699258SBarry Smith @*/ 39607087cfbeSBarry Smith PetscErrorCode SNESSetDM(SNES snes,DM dm) 39616c699258SBarry Smith { 39626c699258SBarry Smith PetscErrorCode ierr; 3963345fed2cSBarry Smith KSP ksp; 39646c699258SBarry Smith 39656c699258SBarry Smith PetscFunctionBegin; 39660700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3967d0660788SBarry Smith if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);} 39686bf464f9SBarry Smith ierr = DMDestroy(&snes->dm);CHKERRQ(ierr); 39696c699258SBarry Smith snes->dm = dm; 3970345fed2cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 3971345fed2cSBarry Smith ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr); 3972f22f69f0SBarry Smith ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr); 39732c155ee1SBarry Smith if (snes->pc) { 39742c155ee1SBarry Smith ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 39752c155ee1SBarry Smith } 39766c699258SBarry Smith PetscFunctionReturn(0); 39776c699258SBarry Smith } 39786c699258SBarry Smith 39796c699258SBarry Smith #undef __FUNCT__ 39806c699258SBarry Smith #define __FUNCT__ "SNESGetDM" 39816c699258SBarry Smith /*@ 39826c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 39836c699258SBarry Smith 39843f9fe445SBarry Smith Not Collective but DM obtained is parallel on SNES 39856c699258SBarry Smith 39866c699258SBarry Smith Input Parameter: 39876c699258SBarry Smith . snes - the preconditioner context 39886c699258SBarry Smith 39896c699258SBarry Smith Output Parameter: 39906c699258SBarry Smith . dm - the dm 39916c699258SBarry Smith 39926c699258SBarry Smith Level: intermediate 39936c699258SBarry Smith 39946c699258SBarry Smith 39956c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM() 39966c699258SBarry Smith @*/ 39977087cfbeSBarry Smith PetscErrorCode SNESGetDM(SNES snes,DM *dm) 39986c699258SBarry Smith { 39996c699258SBarry Smith PetscFunctionBegin; 40000700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 40016c699258SBarry Smith *dm = snes->dm; 40026c699258SBarry Smith PetscFunctionReturn(0); 40036c699258SBarry Smith } 40040807856dSBarry Smith 400531823bd8SMatthew G Knepley #undef __FUNCT__ 400631823bd8SMatthew G Knepley #define __FUNCT__ "SNESSetPC" 400731823bd8SMatthew G Knepley /*@ 4008fd4b34e9SJed Brown SNESSetPC - Sets the nonlinear preconditioner to be used. 400931823bd8SMatthew G Knepley 401031823bd8SMatthew G Knepley Collective on SNES 401131823bd8SMatthew G Knepley 401231823bd8SMatthew G Knepley Input Parameters: 401331823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate() 401431823bd8SMatthew G Knepley - pc - the preconditioner object 401531823bd8SMatthew G Knepley 401631823bd8SMatthew G Knepley Notes: 401731823bd8SMatthew G Knepley Use SNESGetPC() to retrieve the preconditioner context (for example, 401831823bd8SMatthew G Knepley to configure it using the API). 401931823bd8SMatthew G Knepley 402031823bd8SMatthew G Knepley Level: developer 402131823bd8SMatthew G Knepley 402231823bd8SMatthew G Knepley .keywords: SNES, set, precondition 402331823bd8SMatthew G Knepley .seealso: SNESGetPC() 402431823bd8SMatthew G Knepley @*/ 402531823bd8SMatthew G Knepley PetscErrorCode SNESSetPC(SNES snes, SNES pc) 402631823bd8SMatthew G Knepley { 402731823bd8SMatthew G Knepley PetscErrorCode ierr; 402831823bd8SMatthew G Knepley 402931823bd8SMatthew G Knepley PetscFunctionBegin; 403031823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 403131823bd8SMatthew G Knepley PetscValidHeaderSpecific(pc, SNES_CLASSID, 2); 403231823bd8SMatthew G Knepley PetscCheckSameComm(snes, 1, pc, 2); 403331823bd8SMatthew G Knepley ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr); 4034bf11d3b6SBarry Smith ierr = SNESDestroy(&snes->pc);CHKERRQ(ierr); 403531823bd8SMatthew G Knepley snes->pc = pc; 403631823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 403731823bd8SMatthew G Knepley PetscFunctionReturn(0); 403831823bd8SMatthew G Knepley } 403931823bd8SMatthew G Knepley 404031823bd8SMatthew G Knepley #undef __FUNCT__ 404131823bd8SMatthew G Knepley #define __FUNCT__ "SNESGetPC" 404231823bd8SMatthew G Knepley /*@ 4043fd4b34e9SJed Brown SNESGetPC - Returns a pointer to the nonlinear preconditioning context set with SNESSetPC(). 404431823bd8SMatthew G Knepley 404531823bd8SMatthew G Knepley Not Collective 404631823bd8SMatthew G Knepley 404731823bd8SMatthew G Knepley Input Parameter: 404831823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate() 404931823bd8SMatthew G Knepley 405031823bd8SMatthew G Knepley Output Parameter: 405131823bd8SMatthew G Knepley . pc - preconditioner context 405231823bd8SMatthew G Knepley 405331823bd8SMatthew G Knepley Level: developer 405431823bd8SMatthew G Knepley 405531823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner 405631823bd8SMatthew G Knepley .seealso: SNESSetPC() 405731823bd8SMatthew G Knepley @*/ 405831823bd8SMatthew G Knepley PetscErrorCode SNESGetPC(SNES snes, SNES *pc) 405931823bd8SMatthew G Knepley { 406031823bd8SMatthew G Knepley PetscErrorCode ierr; 406131823bd8SMatthew G Knepley 406231823bd8SMatthew G Knepley PetscFunctionBegin; 406331823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 406431823bd8SMatthew G Knepley PetscValidPointer(pc, 2); 406531823bd8SMatthew G Knepley if (!snes->pc) { 406631823bd8SMatthew G Knepley ierr = SNESCreate(((PetscObject) snes)->comm, &snes->pc);CHKERRQ(ierr); 40674a0c5b0cSMatthew G Knepley ierr = PetscObjectIncrementTabLevel((PetscObject) snes->pc, (PetscObject) snes, 1);CHKERRQ(ierr); 406831823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 406931823bd8SMatthew G Knepley } 407031823bd8SMatthew G Knepley *pc = snes->pc; 407131823bd8SMatthew G Knepley PetscFunctionReturn(0); 407231823bd8SMatthew G Knepley } 407331823bd8SMatthew G Knepley 407469b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4075c6db04a5SJed Brown #include <mex.h> 407669b4f73cSBarry Smith 40778f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 40788f6e6473SBarry Smith 40790807856dSBarry Smith #undef __FUNCT__ 40800807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab" 40810807856dSBarry Smith /* 40820807856dSBarry Smith SNESComputeFunction_Matlab - Calls the function that has been set with 40830807856dSBarry Smith SNESSetFunctionMatlab(). 40840807856dSBarry Smith 40850807856dSBarry Smith Collective on SNES 40860807856dSBarry Smith 40870807856dSBarry Smith Input Parameters: 40880807856dSBarry Smith + snes - the SNES context 40890807856dSBarry Smith - x - input vector 40900807856dSBarry Smith 40910807856dSBarry Smith Output Parameter: 40920807856dSBarry Smith . y - function vector, as set by SNESSetFunction() 40930807856dSBarry Smith 40940807856dSBarry Smith Notes: 40950807856dSBarry Smith SNESComputeFunction() is typically used within nonlinear solvers 40960807856dSBarry Smith implementations, so most users would not generally call this routine 40970807856dSBarry Smith themselves. 40980807856dSBarry Smith 40990807856dSBarry Smith Level: developer 41000807856dSBarry Smith 41010807856dSBarry Smith .keywords: SNES, nonlinear, compute, function 41020807856dSBarry Smith 41030807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 410461b2408cSBarry Smith */ 41057087cfbeSBarry Smith PetscErrorCode SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx) 41060807856dSBarry Smith { 4107e650e774SBarry Smith PetscErrorCode ierr; 41088f6e6473SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 41098f6e6473SBarry Smith int nlhs = 1,nrhs = 5; 41108f6e6473SBarry Smith mxArray *plhs[1],*prhs[5]; 411191621f2eSBarry Smith long long int lx = 0,ly = 0,ls = 0; 4112e650e774SBarry Smith 41130807856dSBarry Smith PetscFunctionBegin; 41140807856dSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 41150807856dSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 41160807856dSBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 41170807856dSBarry Smith PetscCheckSameComm(snes,1,x,2); 41180807856dSBarry Smith PetscCheckSameComm(snes,1,y,3); 41190807856dSBarry Smith 41200807856dSBarry Smith /* call Matlab function in ctx with arguments x and y */ 4121e650e774SBarry Smith 412291621f2eSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 4123e650e774SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 4124e650e774SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 412591621f2eSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 412691621f2eSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 412791621f2eSBarry Smith prhs[2] = mxCreateDoubleScalar((double)ly); 41288f6e6473SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 41298f6e6473SBarry Smith prhs[4] = sctx->ctx; 4130b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr); 4131e650e774SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4132e650e774SBarry Smith mxDestroyArray(prhs[0]); 4133e650e774SBarry Smith mxDestroyArray(prhs[1]); 4134e650e774SBarry Smith mxDestroyArray(prhs[2]); 41358f6e6473SBarry Smith mxDestroyArray(prhs[3]); 4136e650e774SBarry Smith mxDestroyArray(plhs[0]); 41370807856dSBarry Smith PetscFunctionReturn(0); 41380807856dSBarry Smith } 41390807856dSBarry Smith 41400807856dSBarry Smith 41410807856dSBarry Smith #undef __FUNCT__ 41420807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab" 414361b2408cSBarry Smith /* 41440807856dSBarry Smith SNESSetFunctionMatlab - Sets the function evaluation routine and function 41450807856dSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 4146e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 41470807856dSBarry Smith 41480807856dSBarry Smith Logically Collective on SNES 41490807856dSBarry Smith 41500807856dSBarry Smith Input Parameters: 41510807856dSBarry Smith + snes - the SNES context 41520807856dSBarry Smith . r - vector to store function value 41530807856dSBarry Smith - func - function evaluation routine 41540807856dSBarry Smith 41550807856dSBarry Smith Calling sequence of func: 415661b2408cSBarry Smith $ func (SNES snes,Vec x,Vec f,void *ctx); 41570807856dSBarry Smith 41580807856dSBarry Smith 41590807856dSBarry Smith Notes: 41600807856dSBarry Smith The Newton-like methods typically solve linear systems of the form 41610807856dSBarry Smith $ f'(x) x = -f(x), 41620807856dSBarry Smith where f'(x) denotes the Jacobian matrix and f(x) is the function. 41630807856dSBarry Smith 41640807856dSBarry Smith Level: beginner 41650807856dSBarry Smith 41660807856dSBarry Smith .keywords: SNES, nonlinear, set, function 41670807856dSBarry Smith 41680807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 416961b2408cSBarry Smith */ 41707087cfbeSBarry Smith PetscErrorCode SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx) 41710807856dSBarry Smith { 41720807856dSBarry Smith PetscErrorCode ierr; 41738f6e6473SBarry Smith SNESMatlabContext *sctx; 41740807856dSBarry Smith 41750807856dSBarry Smith PetscFunctionBegin; 41768f6e6473SBarry Smith /* currently sctx is memory bleed */ 41778f6e6473SBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 41788f6e6473SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 41798f6e6473SBarry Smith /* 41808f6e6473SBarry Smith This should work, but it doesn't 41818f6e6473SBarry Smith sctx->ctx = ctx; 41828f6e6473SBarry Smith mexMakeArrayPersistent(sctx->ctx); 41838f6e6473SBarry Smith */ 41848f6e6473SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 41858f6e6473SBarry Smith ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr); 41860807856dSBarry Smith PetscFunctionReturn(0); 41870807856dSBarry Smith } 418869b4f73cSBarry Smith 418961b2408cSBarry Smith #undef __FUNCT__ 419061b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab" 419161b2408cSBarry Smith /* 419261b2408cSBarry Smith SNESComputeJacobian_Matlab - Calls the function that has been set with 419361b2408cSBarry Smith SNESSetJacobianMatlab(). 419461b2408cSBarry Smith 419561b2408cSBarry Smith Collective on SNES 419661b2408cSBarry Smith 419761b2408cSBarry Smith Input Parameters: 419861b2408cSBarry Smith + snes - the SNES context 419961b2408cSBarry Smith . x - input vector 420061b2408cSBarry Smith . A, B - the matrices 420161b2408cSBarry Smith - ctx - user context 420261b2408cSBarry Smith 420361b2408cSBarry Smith Output Parameter: 420461b2408cSBarry Smith . flag - structure of the matrix 420561b2408cSBarry Smith 420661b2408cSBarry Smith Level: developer 420761b2408cSBarry Smith 420861b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function 420961b2408cSBarry Smith 421061b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 421161b2408cSBarry Smith @*/ 42127087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx) 421361b2408cSBarry Smith { 421461b2408cSBarry Smith PetscErrorCode ierr; 421561b2408cSBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 421661b2408cSBarry Smith int nlhs = 2,nrhs = 6; 421761b2408cSBarry Smith mxArray *plhs[2],*prhs[6]; 421861b2408cSBarry Smith long long int lx = 0,lA = 0,ls = 0, lB = 0; 421961b2408cSBarry Smith 422061b2408cSBarry Smith PetscFunctionBegin; 422161b2408cSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 422261b2408cSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 422361b2408cSBarry Smith 422461b2408cSBarry Smith /* call Matlab function in ctx with arguments x and y */ 422561b2408cSBarry Smith 422661b2408cSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 422761b2408cSBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 422861b2408cSBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 422961b2408cSBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 423061b2408cSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 423161b2408cSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 423261b2408cSBarry Smith prhs[2] = mxCreateDoubleScalar((double)lA); 423361b2408cSBarry Smith prhs[3] = mxCreateDoubleScalar((double)lB); 423461b2408cSBarry Smith prhs[4] = mxCreateString(sctx->funcname); 423561b2408cSBarry Smith prhs[5] = sctx->ctx; 4236b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr); 423761b2408cSBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 423861b2408cSBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 423961b2408cSBarry Smith mxDestroyArray(prhs[0]); 424061b2408cSBarry Smith mxDestroyArray(prhs[1]); 424161b2408cSBarry Smith mxDestroyArray(prhs[2]); 424261b2408cSBarry Smith mxDestroyArray(prhs[3]); 424361b2408cSBarry Smith mxDestroyArray(prhs[4]); 424461b2408cSBarry Smith mxDestroyArray(plhs[0]); 424561b2408cSBarry Smith mxDestroyArray(plhs[1]); 424661b2408cSBarry Smith PetscFunctionReturn(0); 424761b2408cSBarry Smith } 424861b2408cSBarry Smith 424961b2408cSBarry Smith 425061b2408cSBarry Smith #undef __FUNCT__ 425161b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab" 425261b2408cSBarry Smith /* 425361b2408cSBarry Smith SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 425461b2408cSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 4255e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 425661b2408cSBarry Smith 425761b2408cSBarry Smith Logically Collective on SNES 425861b2408cSBarry Smith 425961b2408cSBarry Smith Input Parameters: 426061b2408cSBarry Smith + snes - the SNES context 426161b2408cSBarry Smith . A,B - Jacobian matrices 426261b2408cSBarry Smith . func - function evaluation routine 426361b2408cSBarry Smith - ctx - user context 426461b2408cSBarry Smith 426561b2408cSBarry Smith Calling sequence of func: 426661b2408cSBarry Smith $ flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx); 426761b2408cSBarry Smith 426861b2408cSBarry Smith 426961b2408cSBarry Smith Level: developer 427061b2408cSBarry Smith 427161b2408cSBarry Smith .keywords: SNES, nonlinear, set, function 427261b2408cSBarry Smith 427361b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 427461b2408cSBarry Smith */ 42757087cfbeSBarry Smith PetscErrorCode SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx) 427661b2408cSBarry Smith { 427761b2408cSBarry Smith PetscErrorCode ierr; 427861b2408cSBarry Smith SNESMatlabContext *sctx; 427961b2408cSBarry Smith 428061b2408cSBarry Smith PetscFunctionBegin; 428161b2408cSBarry Smith /* currently sctx is memory bleed */ 428261b2408cSBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 428361b2408cSBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 428461b2408cSBarry Smith /* 428561b2408cSBarry Smith This should work, but it doesn't 428661b2408cSBarry Smith sctx->ctx = ctx; 428761b2408cSBarry Smith mexMakeArrayPersistent(sctx->ctx); 428861b2408cSBarry Smith */ 428961b2408cSBarry Smith sctx->ctx = mxDuplicateArray(ctx); 429061b2408cSBarry Smith ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 429161b2408cSBarry Smith PetscFunctionReturn(0); 429261b2408cSBarry Smith } 429369b4f73cSBarry Smith 4294f9eb7ae2SShri Abhyankar #undef __FUNCT__ 4295f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab" 4296f9eb7ae2SShri Abhyankar /* 4297f9eb7ae2SShri Abhyankar SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab(). 4298f9eb7ae2SShri Abhyankar 4299f9eb7ae2SShri Abhyankar Collective on SNES 4300f9eb7ae2SShri Abhyankar 4301f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction() 4302f9eb7ae2SShri Abhyankar @*/ 43037087cfbeSBarry Smith PetscErrorCode SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx) 4304f9eb7ae2SShri Abhyankar { 4305f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 430648f37e70SShri Abhyankar SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 4307f9eb7ae2SShri Abhyankar int nlhs = 1,nrhs = 6; 4308f9eb7ae2SShri Abhyankar mxArray *plhs[1],*prhs[6]; 4309f9eb7ae2SShri Abhyankar long long int lx = 0,ls = 0; 4310f9eb7ae2SShri Abhyankar Vec x=snes->vec_sol; 4311f9eb7ae2SShri Abhyankar 4312f9eb7ae2SShri Abhyankar PetscFunctionBegin; 4313f9eb7ae2SShri Abhyankar PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4314f9eb7ae2SShri Abhyankar 4315f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 4316f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 4317f9eb7ae2SShri Abhyankar prhs[0] = mxCreateDoubleScalar((double)ls); 4318f9eb7ae2SShri Abhyankar prhs[1] = mxCreateDoubleScalar((double)it); 4319f9eb7ae2SShri Abhyankar prhs[2] = mxCreateDoubleScalar((double)fnorm); 4320f9eb7ae2SShri Abhyankar prhs[3] = mxCreateDoubleScalar((double)lx); 4321f9eb7ae2SShri Abhyankar prhs[4] = mxCreateString(sctx->funcname); 4322f9eb7ae2SShri Abhyankar prhs[5] = sctx->ctx; 4323f9eb7ae2SShri Abhyankar ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr); 4324f9eb7ae2SShri Abhyankar ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4325f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[0]); 4326f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[1]); 4327f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[2]); 4328f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[3]); 4329f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[4]); 4330f9eb7ae2SShri Abhyankar mxDestroyArray(plhs[0]); 4331f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 4332f9eb7ae2SShri Abhyankar } 4333f9eb7ae2SShri Abhyankar 4334f9eb7ae2SShri Abhyankar 4335f9eb7ae2SShri Abhyankar #undef __FUNCT__ 4336f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab" 4337f9eb7ae2SShri Abhyankar /* 4338e3c5b3baSBarry Smith SNESMonitorSetMatlab - Sets the monitor function from MATLAB 4339f9eb7ae2SShri Abhyankar 4340f9eb7ae2SShri Abhyankar Level: developer 4341f9eb7ae2SShri Abhyankar 4342f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function 4343f9eb7ae2SShri Abhyankar 4344f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 4345f9eb7ae2SShri Abhyankar */ 43467087cfbeSBarry Smith PetscErrorCode SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx) 4347f9eb7ae2SShri Abhyankar { 4348f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 4349f9eb7ae2SShri Abhyankar SNESMatlabContext *sctx; 4350f9eb7ae2SShri Abhyankar 4351f9eb7ae2SShri Abhyankar PetscFunctionBegin; 4352f9eb7ae2SShri Abhyankar /* currently sctx is memory bleed */ 4353f9eb7ae2SShri Abhyankar ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 4354f9eb7ae2SShri Abhyankar ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4355f9eb7ae2SShri Abhyankar /* 4356f9eb7ae2SShri Abhyankar This should work, but it doesn't 4357f9eb7ae2SShri Abhyankar sctx->ctx = ctx; 4358f9eb7ae2SShri Abhyankar mexMakeArrayPersistent(sctx->ctx); 4359f9eb7ae2SShri Abhyankar */ 4360f9eb7ae2SShri Abhyankar sctx->ctx = mxDuplicateArray(ctx); 4361f9eb7ae2SShri Abhyankar ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 4362f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 4363f9eb7ae2SShri Abhyankar } 4364f9eb7ae2SShri Abhyankar 436569b4f73cSBarry Smith #endif 4366