19b94acceSBarry Smith 2c6db04a5SJed Brown #include <private/snesimpl.h> /*I "petscsnes.h" I*/ 39b94acceSBarry Smith 4ace3abfcSBarry Smith PetscBool SNESRegisterAllCalled = PETSC_FALSE; 58ba1e511SMatthew Knepley PetscFList SNESList = PETSC_NULL; 68ba1e511SMatthew Knepley 78ba1e511SMatthew Knepley /* Logging support */ 87087cfbeSBarry Smith PetscClassId SNES_CLASSID; 9166c7f25SBarry Smith PetscLogEvent SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval; 10a09944afSBarry Smith 11a09944afSBarry Smith #undef __FUNCT__ 12e113a28aSBarry Smith #define __FUNCT__ "SNESSetErrorIfNotConverged" 13e113a28aSBarry Smith /*@ 14e113a28aSBarry Smith SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged. 15e113a28aSBarry Smith 163f9fe445SBarry Smith Logically Collective on SNES 17e113a28aSBarry Smith 18e113a28aSBarry Smith Input Parameters: 19e113a28aSBarry Smith + snes - iterative context obtained from SNESCreate() 20e113a28aSBarry Smith - flg - PETSC_TRUE indicates you want the error generated 21e113a28aSBarry Smith 22e113a28aSBarry Smith Options database keys: 23e113a28aSBarry Smith . -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false) 24e113a28aSBarry Smith 25e113a28aSBarry Smith Level: intermediate 26e113a28aSBarry Smith 27e113a28aSBarry Smith Notes: 28e113a28aSBarry Smith Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve() 29e113a28aSBarry Smith to determine if it has converged. 30e113a28aSBarry Smith 31e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero 32e113a28aSBarry Smith 33e113a28aSBarry Smith .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged() 34e113a28aSBarry Smith @*/ 357087cfbeSBarry Smith PetscErrorCode SNESSetErrorIfNotConverged(SNES snes,PetscBool flg) 36e113a28aSBarry Smith { 37e113a28aSBarry Smith PetscFunctionBegin; 38e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 39acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flg,2); 40e113a28aSBarry Smith snes->errorifnotconverged = flg; 41e113a28aSBarry Smith PetscFunctionReturn(0); 42e113a28aSBarry Smith } 43e113a28aSBarry Smith 44e113a28aSBarry Smith #undef __FUNCT__ 45e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged" 46e113a28aSBarry Smith /*@ 47e113a28aSBarry Smith SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge? 48e113a28aSBarry Smith 49e113a28aSBarry Smith Not Collective 50e113a28aSBarry Smith 51e113a28aSBarry Smith Input Parameter: 52e113a28aSBarry Smith . snes - iterative context obtained from SNESCreate() 53e113a28aSBarry Smith 54e113a28aSBarry Smith Output Parameter: 55e113a28aSBarry Smith . flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE 56e113a28aSBarry Smith 57e113a28aSBarry Smith Level: intermediate 58e113a28aSBarry Smith 59e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero 60e113a28aSBarry Smith 61e113a28aSBarry Smith .seealso: SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged() 62e113a28aSBarry Smith @*/ 637087cfbeSBarry Smith PetscErrorCode SNESGetErrorIfNotConverged(SNES snes,PetscBool *flag) 64e113a28aSBarry Smith { 65e113a28aSBarry Smith PetscFunctionBegin; 66e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 67e113a28aSBarry Smith PetscValidPointer(flag,2); 68e113a28aSBarry Smith *flag = snes->errorifnotconverged; 69e113a28aSBarry Smith PetscFunctionReturn(0); 70e113a28aSBarry Smith } 71e113a28aSBarry Smith 72e113a28aSBarry Smith #undef __FUNCT__ 734936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError" 74e725d27bSBarry Smith /*@ 754936397dSBarry Smith SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not 764936397dSBarry Smith in the functions domain. For example, negative pressure. 774936397dSBarry Smith 783f9fe445SBarry Smith Logically Collective on SNES 794936397dSBarry Smith 804936397dSBarry Smith Input Parameters: 814936397dSBarry Smith . SNES - the SNES context 824936397dSBarry Smith 8328529972SSatish Balay Level: advanced 844936397dSBarry Smith 854936397dSBarry Smith .keywords: SNES, view 864936397dSBarry Smith 874936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction() 884936397dSBarry Smith @*/ 897087cfbeSBarry Smith PetscErrorCode SNESSetFunctionDomainError(SNES snes) 904936397dSBarry Smith { 914936397dSBarry Smith PetscFunctionBegin; 920700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 934936397dSBarry Smith snes->domainerror = PETSC_TRUE; 944936397dSBarry Smith PetscFunctionReturn(0); 954936397dSBarry Smith } 964936397dSBarry Smith 974936397dSBarry Smith #undef __FUNCT__ 984a2ae208SSatish Balay #define __FUNCT__ "SNESView" 997e2c5f70SBarry Smith /*@C 1009b94acceSBarry Smith SNESView - Prints the SNES data structure. 1019b94acceSBarry Smith 1024c49b128SBarry Smith Collective on SNES 103fee21e36SBarry Smith 104c7afd0dbSLois Curfman McInnes Input Parameters: 105c7afd0dbSLois Curfman McInnes + SNES - the SNES context 106c7afd0dbSLois Curfman McInnes - viewer - visualization context 107c7afd0dbSLois Curfman McInnes 1089b94acceSBarry Smith Options Database Key: 109c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 1109b94acceSBarry Smith 1119b94acceSBarry Smith Notes: 1129b94acceSBarry Smith The available visualization contexts include 113b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 114b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 115c8a8ba5cSLois Curfman McInnes output where only the first processor opens 116c8a8ba5cSLois Curfman McInnes the file. All other processors send their 117c8a8ba5cSLois Curfman McInnes data to the first processor to print. 1189b94acceSBarry Smith 1193e081fefSLois Curfman McInnes The user can open an alternative visualization context with 120b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1219b94acceSBarry Smith 12236851e7fSLois Curfman McInnes Level: beginner 12336851e7fSLois Curfman McInnes 1249b94acceSBarry Smith .keywords: SNES, view 1259b94acceSBarry Smith 126b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1279b94acceSBarry Smith @*/ 1287087cfbeSBarry Smith PetscErrorCode SNESView(SNES snes,PetscViewer viewer) 1299b94acceSBarry Smith { 130fa9f3622SBarry Smith SNESKSPEW *kctx; 131dfbe8321SBarry Smith PetscErrorCode ierr; 13294b7f48cSBarry Smith KSP ksp; 133ace3abfcSBarry Smith PetscBool iascii,isstring; 1349b94acceSBarry Smith 1353a40ed3dSBarry Smith PetscFunctionBegin; 1360700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1373050cee2SBarry Smith if (!viewer) { 1387adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr); 1393050cee2SBarry Smith } 1400700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 141c9780b6fSBarry Smith PetscCheckSameComm(snes,1,viewer,2); 14274679c65SBarry Smith 1432692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1442692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 14532077d6dSBarry Smith if (iascii) { 146317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer,"SNES Object");CHKERRQ(ierr); 147e7788613SBarry Smith if (snes->ops->view) { 148b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 149e7788613SBarry Smith ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 150b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1510ef38995SBarry Smith } 15277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 153a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, solution=%G\n", 15470441072SBarry Smith snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr); 15577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr); 15677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr); 1579b94acceSBarry Smith if (snes->ksp_ewconv) { 158fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 1599b94acceSBarry Smith if (kctx) { 16077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr); 161a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 162a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 1639b94acceSBarry Smith } 1649b94acceSBarry Smith } 165eb1f6c34SBarry Smith if (snes->lagpreconditioner == -1) { 166eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is never rebuilt\n");CHKERRQ(ierr); 167eb1f6c34SBarry Smith } else if (snes->lagpreconditioner > 1) { 168eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr); 169eb1f6c34SBarry Smith } 170eb1f6c34SBarry Smith if (snes->lagjacobian == -1) { 171eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is never rebuilt\n");CHKERRQ(ierr); 172eb1f6c34SBarry Smith } else if (snes->lagjacobian > 1) { 173eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is rebuilt every %D Newton iterations\n",snes->lagjacobian);CHKERRQ(ierr); 174eb1f6c34SBarry Smith } 1750f5bd95cSBarry Smith } else if (isstring) { 176317d6ea6SBarry Smith const char *type; 177454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 178b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 17919bcc07fSBarry Smith } 18094b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 181b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 18294b7f48cSBarry Smith ierr = KSPView(ksp,viewer);CHKERRQ(ierr); 183b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1843a40ed3dSBarry Smith PetscFunctionReturn(0); 1859b94acceSBarry Smith } 1869b94acceSBarry Smith 18776b2cf59SMatthew Knepley /* 18876b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 18976b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 19076b2cf59SMatthew Knepley */ 19176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 192a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 1936849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 19476b2cf59SMatthew Knepley 195e74ef692SMatthew Knepley #undef __FUNCT__ 196e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker" 197ac226902SBarry Smith /*@C 19876b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 19976b2cf59SMatthew Knepley 20076b2cf59SMatthew Knepley Not Collective 20176b2cf59SMatthew Knepley 20276b2cf59SMatthew Knepley Input Parameter: 20376b2cf59SMatthew Knepley . snescheck - function that checks for options 20476b2cf59SMatthew Knepley 20576b2cf59SMatthew Knepley Level: developer 20676b2cf59SMatthew Knepley 20776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions() 20876b2cf59SMatthew Knepley @*/ 2097087cfbeSBarry Smith PetscErrorCode SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 21076b2cf59SMatthew Knepley { 21176b2cf59SMatthew Knepley PetscFunctionBegin; 21276b2cf59SMatthew Knepley if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 213e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS); 21476b2cf59SMatthew Knepley } 21576b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 21676b2cf59SMatthew Knepley PetscFunctionReturn(0); 21776b2cf59SMatthew Knepley } 21876b2cf59SMatthew Knepley 2197087cfbeSBarry Smith extern PetscErrorCode SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 220aa3661deSLisandro Dalcin 221aa3661deSLisandro Dalcin #undef __FUNCT__ 222aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private" 223ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool hasOperator, PetscInt version) 224aa3661deSLisandro Dalcin { 225aa3661deSLisandro Dalcin Mat J; 226aa3661deSLisandro Dalcin KSP ksp; 227aa3661deSLisandro Dalcin PC pc; 228ace3abfcSBarry Smith PetscBool match; 229aa3661deSLisandro Dalcin PetscErrorCode ierr; 230aa3661deSLisandro Dalcin 231aa3661deSLisandro Dalcin PetscFunctionBegin; 2320700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 233aa3661deSLisandro Dalcin 23498613b67SLisandro Dalcin if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) { 23598613b67SLisandro Dalcin Mat A = snes->jacobian, B = snes->jacobian_pre; 23698613b67SLisandro Dalcin ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr); 23798613b67SLisandro Dalcin } 23898613b67SLisandro Dalcin 239aa3661deSLisandro Dalcin if (version == 1) { 240aa3661deSLisandro Dalcin ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 24198613b67SLisandro Dalcin ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 2429c6ac3b3SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 243aa3661deSLisandro Dalcin } else if (version == 2) { 244e32f2f54SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first"); 245ce63c4c1SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) && !defined(PETSC_USE_REAL_LONG_DOUBLE) 246aa3661deSLisandro Dalcin ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr); 247aa3661deSLisandro Dalcin #else 248e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)"); 249aa3661deSLisandro Dalcin #endif 250aa3661deSLisandro Dalcin } else { 251e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2"); 252aa3661deSLisandro Dalcin } 253aa3661deSLisandro Dalcin 254aa3661deSLisandro Dalcin ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr); 255d3462f78SMatthew Knepley if (hasOperator) { 256aa3661deSLisandro Dalcin /* This version replaces the user provided Jacobian matrix with a 257aa3661deSLisandro Dalcin matrix-free version but still employs the user-provided preconditioner matrix. */ 258aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 259aa3661deSLisandro Dalcin } else { 260aa3661deSLisandro Dalcin /* This version replaces both the user-provided Jacobian and the user- 261aa3661deSLisandro Dalcin provided preconditioner matrix with the default matrix free version. */ 262aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 263aa3661deSLisandro Dalcin /* Force no preconditioner */ 264aa3661deSLisandro Dalcin ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 265aa3661deSLisandro Dalcin ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 266aa3661deSLisandro Dalcin ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr); 267aa3661deSLisandro Dalcin if (!match) { 268aa3661deSLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 269aa3661deSLisandro Dalcin ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 270aa3661deSLisandro Dalcin } 271aa3661deSLisandro Dalcin } 2726bf464f9SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 273aa3661deSLisandro Dalcin 274aa3661deSLisandro Dalcin PetscFunctionReturn(0); 275aa3661deSLisandro Dalcin } 276aa3661deSLisandro Dalcin 2774a2ae208SSatish Balay #undef __FUNCT__ 2784a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions" 2799b94acceSBarry Smith /*@ 28094b7f48cSBarry Smith SNESSetFromOptions - Sets various SNES and KSP parameters from user options. 2819b94acceSBarry Smith 282c7afd0dbSLois Curfman McInnes Collective on SNES 283c7afd0dbSLois Curfman McInnes 2849b94acceSBarry Smith Input Parameter: 2859b94acceSBarry Smith . snes - the SNES context 2869b94acceSBarry Smith 28736851e7fSLois Curfman McInnes Options Database Keys: 2886831982aSBarry Smith + -snes_type <type> - ls, tr, umls, umtr, test 28982738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 29082738288SBarry Smith of the change in the solution between steps 29170441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 292b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 293b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 294b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 29550ffb88aSMatthew Knepley . -snes_max_fail <max_fail> - maximum number of failures 296ddf469c8SBarry Smith . -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops 297a8054027SBarry Smith . -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild) 298e35cf81dSBarry Smith . -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild) 299b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 3002492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 30182738288SBarry Smith solver; hence iterations will continue until max_it 3021fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 30382738288SBarry Smith of convergence test 304e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 305e8105e01SRichard Katz filename given prints to stdout 306a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 307a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 308a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 309a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 310e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 3115968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 312fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 31382738288SBarry Smith 31482738288SBarry Smith Options Database for Eisenstat-Walker method: 315fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 3164b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 31736851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 31836851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 31936851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 32036851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 32136851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 32236851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 32382738288SBarry Smith 32411ca99fdSLois Curfman McInnes Notes: 32511ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 3260598bfebSBarry Smith the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A>. 32783e2fdc7SBarry Smith 32836851e7fSLois Curfman McInnes Level: beginner 32936851e7fSLois Curfman McInnes 3309b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 3319b94acceSBarry Smith 33269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 3339b94acceSBarry Smith @*/ 3347087cfbeSBarry Smith PetscErrorCode SNESSetFromOptions(SNES snes) 3359b94acceSBarry Smith { 336ace3abfcSBarry Smith PetscBool flg,mf,mf_operator; 337efd51863SBarry Smith PetscInt i,indx,lag,mf_version,grids; 338aa3661deSLisandro Dalcin MatStructure matflag; 33985385478SLisandro Dalcin const char *deft = SNESLS; 34085385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 34185385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 342e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 34323d894e5SBarry Smith PetscViewerASCIIMonitor monviewer; 34485385478SLisandro Dalcin PetscErrorCode ierr; 3459b94acceSBarry Smith 3463a40ed3dSBarry Smith PetscFunctionBegin; 3470700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 348ca161407SBarry Smith 349186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 350cce0b1b2SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr); 3517adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 352b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 353d64ed03dSBarry Smith if (flg) { 354186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 3557adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 356186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 357d64ed03dSBarry Smith } 35890d69ab7SBarry Smith /* not used here, but called so will go into help messaage */ 359909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 36093c39befSBarry Smith 36157034d6fSHong Zhang ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 36257034d6fSHong Zhang ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 363186905e3SBarry Smith 36457034d6fSHong Zhang ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 365b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 366b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 36750ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 368ddf469c8SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr); 369acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr); 37085385478SLisandro Dalcin 371a8054027SBarry Smith ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr); 372a8054027SBarry Smith if (flg) { 373a8054027SBarry Smith ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr); 374a8054027SBarry Smith } 375e35cf81dSBarry Smith ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr); 376e35cf81dSBarry Smith if (flg) { 377e35cf81dSBarry Smith ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr); 378e35cf81dSBarry Smith } 379efd51863SBarry Smith ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr); 380efd51863SBarry Smith if (flg) { 381efd51863SBarry Smith ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr); 382efd51863SBarry Smith } 383a8054027SBarry Smith 38485385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 38585385478SLisandro Dalcin if (flg) { 38685385478SLisandro Dalcin switch (indx) { 3877f7931b9SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 3887f7931b9SBarry Smith case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 38985385478SLisandro Dalcin } 39085385478SLisandro Dalcin } 39185385478SLisandro Dalcin 392acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr); 393186905e3SBarry Smith 39485385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 39585385478SLisandro Dalcin 396acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 397186905e3SBarry Smith 398fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 399fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 400fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 401fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 402fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 403fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 404fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 405186905e3SBarry Smith 40690d69ab7SBarry Smith flg = PETSC_FALSE; 407acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 408a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 409eabae89aSBarry Smith 410a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 411e8105e01SRichard Katz if (flg) { 412050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 413c2efdce3SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 414e8105e01SRichard Katz } 415eabae89aSBarry Smith 416b271bb04SBarry Smith ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 417b271bb04SBarry Smith if (flg) { 418b271bb04SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr); 419b271bb04SBarry Smith } 420b271bb04SBarry Smith 421a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 422eabae89aSBarry Smith if (flg) { 423050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 424f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 425e8105e01SRichard Katz } 426eabae89aSBarry Smith 427a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 428eabae89aSBarry Smith if (flg) { 429050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 430c2efdce3SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void**))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 431eabae89aSBarry Smith } 432eabae89aSBarry Smith 43390d69ab7SBarry Smith flg = PETSC_FALSE; 434acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 435a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 43690d69ab7SBarry Smith flg = PETSC_FALSE; 437acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 438a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 43990d69ab7SBarry Smith flg = PETSC_FALSE; 440acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 441a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 44290d69ab7SBarry Smith flg = PETSC_FALSE; 443acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 444a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 44590d69ab7SBarry Smith flg = PETSC_FALSE; 446acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 447b271bb04SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 448e24b481bSBarry Smith 44990d69ab7SBarry Smith flg = PETSC_FALSE; 450acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 4514b27c08aSLois Curfman McInnes if (flg) { 452186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 453ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 4549b94acceSBarry Smith } 455639f9d9dSBarry Smith 456aa3661deSLisandro Dalcin mf = mf_operator = PETSC_FALSE; 457aa3661deSLisandro Dalcin flg = PETSC_FALSE; 458acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr); 459aa3661deSLisandro Dalcin if (flg && mf_operator) mf = PETSC_TRUE; 460aa3661deSLisandro Dalcin flg = PETSC_FALSE; 461acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr); 462aa3661deSLisandro Dalcin if (!flg && mf_operator) mf = PETSC_TRUE; 463aa3661deSLisandro Dalcin mf_version = 1; 464aa3661deSLisandro Dalcin ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr); 465aa3661deSLisandro Dalcin 46676b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 46776b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 46876b2cf59SMatthew Knepley } 46976b2cf59SMatthew Knepley 470e7788613SBarry Smith if (snes->ops->setfromoptions) { 471e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 472639f9d9dSBarry Smith } 4735d973c19SBarry Smith 4745d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 4755d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr); 476b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 4774bbc92c1SBarry Smith 478aa3661deSLisandro Dalcin if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); } 4791cee3971SBarry Smith 4801cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 481aa3661deSLisandro Dalcin ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag); 482aa3661deSLisandro Dalcin ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr); 48385385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 48493993e2dSLois Curfman McInnes 4853a40ed3dSBarry Smith PetscFunctionReturn(0); 4869b94acceSBarry Smith } 4879b94acceSBarry Smith 488d25893d9SBarry Smith #undef __FUNCT__ 489d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext" 490d25893d9SBarry Smith /*@ 491d25893d9SBarry Smith SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for 492d25893d9SBarry Smith the nonlinear solvers. 493d25893d9SBarry Smith 494d25893d9SBarry Smith Logically Collective on SNES 495d25893d9SBarry Smith 496d25893d9SBarry Smith Input Parameters: 497d25893d9SBarry Smith + snes - the SNES context 498d25893d9SBarry Smith . compute - function to compute the context 499d25893d9SBarry Smith - destroy - function to destroy the context 500d25893d9SBarry Smith 501d25893d9SBarry Smith Level: intermediate 502d25893d9SBarry Smith 503d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context 504d25893d9SBarry Smith 505d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext() 506d25893d9SBarry Smith @*/ 507d25893d9SBarry Smith PetscErrorCode SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**)) 508d25893d9SBarry Smith { 509d25893d9SBarry Smith PetscFunctionBegin; 510d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 511d25893d9SBarry Smith snes->ops->usercompute = compute; 512d25893d9SBarry Smith snes->ops->userdestroy = destroy; 513d25893d9SBarry Smith PetscFunctionReturn(0); 514d25893d9SBarry Smith } 515a847f771SSatish Balay 5164a2ae208SSatish Balay #undef __FUNCT__ 5174a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 518b07ff414SBarry Smith /*@ 5199b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 5209b94acceSBarry Smith the nonlinear solvers. 5219b94acceSBarry Smith 5223f9fe445SBarry Smith Logically Collective on SNES 523fee21e36SBarry Smith 524c7afd0dbSLois Curfman McInnes Input Parameters: 525c7afd0dbSLois Curfman McInnes + snes - the SNES context 526c7afd0dbSLois Curfman McInnes - usrP - optional user context 527c7afd0dbSLois Curfman McInnes 52836851e7fSLois Curfman McInnes Level: intermediate 52936851e7fSLois Curfman McInnes 5309b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 5319b94acceSBarry Smith 532d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext() 5339b94acceSBarry Smith @*/ 5347087cfbeSBarry Smith PetscErrorCode SNESSetApplicationContext(SNES snes,void *usrP) 5359b94acceSBarry Smith { 5361b2093e4SBarry Smith PetscErrorCode ierr; 537b07ff414SBarry Smith KSP ksp; 5381b2093e4SBarry Smith 5393a40ed3dSBarry Smith PetscFunctionBegin; 5400700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 541b07ff414SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 542b07ff414SBarry Smith ierr = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr); 5439b94acceSBarry Smith snes->user = usrP; 5443a40ed3dSBarry Smith PetscFunctionReturn(0); 5459b94acceSBarry Smith } 54674679c65SBarry Smith 5474a2ae208SSatish Balay #undef __FUNCT__ 5484a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 549b07ff414SBarry Smith /*@ 5509b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 5519b94acceSBarry Smith nonlinear solvers. 5529b94acceSBarry Smith 553c7afd0dbSLois Curfman McInnes Not Collective 554c7afd0dbSLois Curfman McInnes 5559b94acceSBarry Smith Input Parameter: 5569b94acceSBarry Smith . snes - SNES context 5579b94acceSBarry Smith 5589b94acceSBarry Smith Output Parameter: 5599b94acceSBarry Smith . usrP - user context 5609b94acceSBarry Smith 56136851e7fSLois Curfman McInnes Level: intermediate 56236851e7fSLois Curfman McInnes 5639b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 5649b94acceSBarry Smith 5659b94acceSBarry Smith .seealso: SNESSetApplicationContext() 5669b94acceSBarry Smith @*/ 5677087cfbeSBarry Smith PetscErrorCode SNESGetApplicationContext(SNES snes,void **usrP) 5689b94acceSBarry Smith { 5693a40ed3dSBarry Smith PetscFunctionBegin; 5700700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5719b94acceSBarry Smith *usrP = snes->user; 5723a40ed3dSBarry Smith PetscFunctionReturn(0); 5739b94acceSBarry Smith } 57474679c65SBarry Smith 5754a2ae208SSatish Balay #undef __FUNCT__ 5764a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 5779b94acceSBarry Smith /*@ 578c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 579c8228a4eSBarry Smith at this time. 5809b94acceSBarry Smith 581c7afd0dbSLois Curfman McInnes Not Collective 582c7afd0dbSLois Curfman McInnes 5839b94acceSBarry Smith Input Parameter: 5849b94acceSBarry Smith . snes - SNES context 5859b94acceSBarry Smith 5869b94acceSBarry Smith Output Parameter: 5879b94acceSBarry Smith . iter - iteration number 5889b94acceSBarry Smith 589c8228a4eSBarry Smith Notes: 590c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 591c8228a4eSBarry Smith 592c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 59308405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 59408405cd6SLois Curfman McInnes .vb 59508405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 59608405cd6SLois Curfman McInnes if (!(it % 2)) { 59708405cd6SLois Curfman McInnes [compute Jacobian here] 59808405cd6SLois Curfman McInnes } 59908405cd6SLois Curfman McInnes .ve 600c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 60108405cd6SLois Curfman McInnes recomputed every second SNES iteration. 602c8228a4eSBarry Smith 60336851e7fSLois Curfman McInnes Level: intermediate 60436851e7fSLois Curfman McInnes 6052b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 6062b668275SBarry Smith 607b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 6089b94acceSBarry Smith @*/ 6097087cfbeSBarry Smith PetscErrorCode SNESGetIterationNumber(SNES snes,PetscInt* iter) 6109b94acceSBarry Smith { 6113a40ed3dSBarry Smith PetscFunctionBegin; 6120700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6134482741eSBarry Smith PetscValidIntPointer(iter,2); 6149b94acceSBarry Smith *iter = snes->iter; 6153a40ed3dSBarry Smith PetscFunctionReturn(0); 6169b94acceSBarry Smith } 61774679c65SBarry Smith 6184a2ae208SSatish Balay #undef __FUNCT__ 6194a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 6209b94acceSBarry Smith /*@ 6219b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 6229b94acceSBarry Smith with SNESSSetFunction(). 6239b94acceSBarry Smith 624c7afd0dbSLois Curfman McInnes Collective on SNES 625c7afd0dbSLois Curfman McInnes 6269b94acceSBarry Smith Input Parameter: 6279b94acceSBarry Smith . snes - SNES context 6289b94acceSBarry Smith 6299b94acceSBarry Smith Output Parameter: 6309b94acceSBarry Smith . fnorm - 2-norm of function 6319b94acceSBarry Smith 63236851e7fSLois Curfman McInnes Level: intermediate 63336851e7fSLois Curfman McInnes 6349b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 635a86d99e1SLois Curfman McInnes 636b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 6379b94acceSBarry Smith @*/ 6387087cfbeSBarry Smith PetscErrorCode SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 6399b94acceSBarry Smith { 6403a40ed3dSBarry Smith PetscFunctionBegin; 6410700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6424482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 6439b94acceSBarry Smith *fnorm = snes->norm; 6443a40ed3dSBarry Smith PetscFunctionReturn(0); 6459b94acceSBarry Smith } 64674679c65SBarry Smith 6474a2ae208SSatish Balay #undef __FUNCT__ 648b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 6499b94acceSBarry Smith /*@ 650b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 6519b94acceSBarry Smith attempted by the nonlinear solver. 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 . nfails - number of unsuccessful steps attempted 6609b94acceSBarry Smith 661c96a6f78SLois Curfman McInnes Notes: 662c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 663c96a6f78SLois Curfman McInnes 66436851e7fSLois Curfman McInnes Level: intermediate 66536851e7fSLois Curfman McInnes 6669b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 66758ebbce7SBarry Smith 668e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 66958ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 6709b94acceSBarry Smith @*/ 6717087cfbeSBarry Smith PetscErrorCode SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 6729b94acceSBarry Smith { 6733a40ed3dSBarry Smith PetscFunctionBegin; 6740700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6754482741eSBarry Smith PetscValidIntPointer(nfails,2); 67650ffb88aSMatthew Knepley *nfails = snes->numFailures; 67750ffb88aSMatthew Knepley PetscFunctionReturn(0); 67850ffb88aSMatthew Knepley } 67950ffb88aSMatthew Knepley 68050ffb88aSMatthew Knepley #undef __FUNCT__ 681b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 68250ffb88aSMatthew Knepley /*@ 683b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 68450ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 68550ffb88aSMatthew Knepley 68650ffb88aSMatthew Knepley Not Collective 68750ffb88aSMatthew Knepley 68850ffb88aSMatthew Knepley Input Parameters: 68950ffb88aSMatthew Knepley + snes - SNES context 69050ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 69150ffb88aSMatthew Knepley 69250ffb88aSMatthew Knepley Level: intermediate 69350ffb88aSMatthew Knepley 69450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 69558ebbce7SBarry Smith 696e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 69758ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 69850ffb88aSMatthew Knepley @*/ 6997087cfbeSBarry Smith PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 70050ffb88aSMatthew Knepley { 70150ffb88aSMatthew Knepley PetscFunctionBegin; 7020700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 70350ffb88aSMatthew Knepley snes->maxFailures = maxFails; 70450ffb88aSMatthew Knepley PetscFunctionReturn(0); 70550ffb88aSMatthew Knepley } 70650ffb88aSMatthew Knepley 70750ffb88aSMatthew Knepley #undef __FUNCT__ 708b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 70950ffb88aSMatthew Knepley /*@ 710b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 71150ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 71250ffb88aSMatthew Knepley 71350ffb88aSMatthew Knepley Not Collective 71450ffb88aSMatthew Knepley 71550ffb88aSMatthew Knepley Input Parameter: 71650ffb88aSMatthew Knepley . snes - SNES context 71750ffb88aSMatthew Knepley 71850ffb88aSMatthew Knepley Output Parameter: 71950ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 72050ffb88aSMatthew Knepley 72150ffb88aSMatthew Knepley Level: intermediate 72250ffb88aSMatthew Knepley 72350ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 72458ebbce7SBarry Smith 725e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 72658ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 72758ebbce7SBarry Smith 72850ffb88aSMatthew Knepley @*/ 7297087cfbeSBarry Smith PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 73050ffb88aSMatthew Knepley { 73150ffb88aSMatthew Knepley PetscFunctionBegin; 7320700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7334482741eSBarry Smith PetscValidIntPointer(maxFails,2); 73450ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 7353a40ed3dSBarry Smith PetscFunctionReturn(0); 7369b94acceSBarry Smith } 737a847f771SSatish Balay 7384a2ae208SSatish Balay #undef __FUNCT__ 7392541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 7402541af92SBarry Smith /*@ 7412541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 7422541af92SBarry Smith done by SNES. 7432541af92SBarry Smith 7442541af92SBarry Smith Not Collective 7452541af92SBarry Smith 7462541af92SBarry Smith Input Parameter: 7472541af92SBarry Smith . snes - SNES context 7482541af92SBarry Smith 7492541af92SBarry Smith Output Parameter: 7502541af92SBarry Smith . nfuncs - number of evaluations 7512541af92SBarry Smith 7522541af92SBarry Smith Level: intermediate 7532541af92SBarry Smith 7542541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 75558ebbce7SBarry Smith 756e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 7572541af92SBarry Smith @*/ 7587087cfbeSBarry Smith PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 7592541af92SBarry Smith { 7602541af92SBarry Smith PetscFunctionBegin; 7610700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7622541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 7632541af92SBarry Smith *nfuncs = snes->nfuncs; 7642541af92SBarry Smith PetscFunctionReturn(0); 7652541af92SBarry Smith } 7662541af92SBarry Smith 7672541af92SBarry Smith #undef __FUNCT__ 7683d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 7693d4c4710SBarry Smith /*@ 7703d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 7713d4c4710SBarry Smith linear solvers. 7723d4c4710SBarry Smith 7733d4c4710SBarry Smith Not Collective 7743d4c4710SBarry Smith 7753d4c4710SBarry Smith Input Parameter: 7763d4c4710SBarry Smith . snes - SNES context 7773d4c4710SBarry Smith 7783d4c4710SBarry Smith Output Parameter: 7793d4c4710SBarry Smith . nfails - number of failed solves 7803d4c4710SBarry Smith 7813d4c4710SBarry Smith Notes: 7823d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 7833d4c4710SBarry Smith 7843d4c4710SBarry Smith Level: intermediate 7853d4c4710SBarry Smith 7863d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 78758ebbce7SBarry Smith 788e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 7893d4c4710SBarry Smith @*/ 7907087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 7913d4c4710SBarry Smith { 7923d4c4710SBarry Smith PetscFunctionBegin; 7930700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7943d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 7953d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 7963d4c4710SBarry Smith PetscFunctionReturn(0); 7973d4c4710SBarry Smith } 7983d4c4710SBarry Smith 7993d4c4710SBarry Smith #undef __FUNCT__ 8003d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 8013d4c4710SBarry Smith /*@ 8023d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 8033d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 8043d4c4710SBarry Smith 8053f9fe445SBarry Smith Logically Collective on SNES 8063d4c4710SBarry Smith 8073d4c4710SBarry Smith Input Parameters: 8083d4c4710SBarry Smith + snes - SNES context 8093d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 8103d4c4710SBarry Smith 8113d4c4710SBarry Smith Level: intermediate 8123d4c4710SBarry Smith 813a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 8143d4c4710SBarry Smith 8153d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 8163d4c4710SBarry Smith 81758ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 8183d4c4710SBarry Smith @*/ 8197087cfbeSBarry Smith PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 8203d4c4710SBarry Smith { 8213d4c4710SBarry Smith PetscFunctionBegin; 8220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 823c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxFails,2); 8243d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 8253d4c4710SBarry Smith PetscFunctionReturn(0); 8263d4c4710SBarry Smith } 8273d4c4710SBarry Smith 8283d4c4710SBarry Smith #undef __FUNCT__ 8293d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 8303d4c4710SBarry Smith /*@ 8313d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 8323d4c4710SBarry Smith are allowed before SNES terminates 8333d4c4710SBarry Smith 8343d4c4710SBarry Smith Not Collective 8353d4c4710SBarry Smith 8363d4c4710SBarry Smith Input Parameter: 8373d4c4710SBarry Smith . snes - SNES context 8383d4c4710SBarry Smith 8393d4c4710SBarry Smith Output Parameter: 8403d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 8413d4c4710SBarry Smith 8423d4c4710SBarry Smith Level: intermediate 8433d4c4710SBarry Smith 8443d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 8453d4c4710SBarry Smith 8463d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 8473d4c4710SBarry Smith 848e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 8493d4c4710SBarry Smith @*/ 8507087cfbeSBarry Smith PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 8513d4c4710SBarry Smith { 8523d4c4710SBarry Smith PetscFunctionBegin; 8530700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8543d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 8553d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 8563d4c4710SBarry Smith PetscFunctionReturn(0); 8573d4c4710SBarry Smith } 8583d4c4710SBarry Smith 8593d4c4710SBarry Smith #undef __FUNCT__ 860b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 861c96a6f78SLois Curfman McInnes /*@ 862b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 863c96a6f78SLois Curfman McInnes used by the nonlinear solver. 864c96a6f78SLois Curfman McInnes 865c7afd0dbSLois Curfman McInnes Not Collective 866c7afd0dbSLois Curfman McInnes 867c96a6f78SLois Curfman McInnes Input Parameter: 868c96a6f78SLois Curfman McInnes . snes - SNES context 869c96a6f78SLois Curfman McInnes 870c96a6f78SLois Curfman McInnes Output Parameter: 871c96a6f78SLois Curfman McInnes . lits - number of linear iterations 872c96a6f78SLois Curfman McInnes 873c96a6f78SLois Curfman McInnes Notes: 874c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 875c96a6f78SLois Curfman McInnes 87636851e7fSLois Curfman McInnes Level: intermediate 87736851e7fSLois Curfman McInnes 878c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 8792b668275SBarry Smith 8808c7482ecSBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 881c96a6f78SLois Curfman McInnes @*/ 8827087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 883c96a6f78SLois Curfman McInnes { 8843a40ed3dSBarry Smith PetscFunctionBegin; 8850700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8864482741eSBarry Smith PetscValidIntPointer(lits,2); 887c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 8883a40ed3dSBarry Smith PetscFunctionReturn(0); 889c96a6f78SLois Curfman McInnes } 890c96a6f78SLois Curfman McInnes 8914a2ae208SSatish Balay #undef __FUNCT__ 89294b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 89352baeb72SSatish Balay /*@ 89494b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 8959b94acceSBarry Smith 89694b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 897c7afd0dbSLois Curfman McInnes 8989b94acceSBarry Smith Input Parameter: 8999b94acceSBarry Smith . snes - the SNES context 9009b94acceSBarry Smith 9019b94acceSBarry Smith Output Parameter: 90294b7f48cSBarry Smith . ksp - the KSP context 9039b94acceSBarry Smith 9049b94acceSBarry Smith Notes: 90594b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 9069b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 9072999313aSBarry Smith PC contexts as well. 9089b94acceSBarry Smith 90936851e7fSLois Curfman McInnes Level: beginner 91036851e7fSLois Curfman McInnes 91194b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 9129b94acceSBarry Smith 9132999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 9149b94acceSBarry Smith @*/ 9157087cfbeSBarry Smith PetscErrorCode SNESGetKSP(SNES snes,KSP *ksp) 9169b94acceSBarry Smith { 9171cee3971SBarry Smith PetscErrorCode ierr; 9181cee3971SBarry Smith 9193a40ed3dSBarry Smith PetscFunctionBegin; 9200700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9214482741eSBarry Smith PetscValidPointer(ksp,2); 9221cee3971SBarry Smith 9231cee3971SBarry Smith if (!snes->ksp) { 9241cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 9251cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 9261cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 9271cee3971SBarry Smith } 92894b7f48cSBarry Smith *ksp = snes->ksp; 9293a40ed3dSBarry Smith PetscFunctionReturn(0); 9309b94acceSBarry Smith } 93182bf6240SBarry Smith 9324a2ae208SSatish Balay #undef __FUNCT__ 9332999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 9342999313aSBarry Smith /*@ 9352999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 9362999313aSBarry Smith 9372999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 9382999313aSBarry Smith 9392999313aSBarry Smith Input Parameters: 9402999313aSBarry Smith + snes - the SNES context 9412999313aSBarry Smith - ksp - the KSP context 9422999313aSBarry Smith 9432999313aSBarry Smith Notes: 9442999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 9452999313aSBarry Smith so this routine is rarely needed. 9462999313aSBarry Smith 9472999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 9482999313aSBarry Smith decreased by one. 9492999313aSBarry Smith 9502999313aSBarry Smith Level: developer 9512999313aSBarry Smith 9522999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 9532999313aSBarry Smith 9542999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 9552999313aSBarry Smith @*/ 9567087cfbeSBarry Smith PetscErrorCode SNESSetKSP(SNES snes,KSP ksp) 9572999313aSBarry Smith { 9582999313aSBarry Smith PetscErrorCode ierr; 9592999313aSBarry Smith 9602999313aSBarry Smith PetscFunctionBegin; 9610700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9620700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 9632999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 9647dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 965906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 9662999313aSBarry Smith snes->ksp = ksp; 9672999313aSBarry Smith PetscFunctionReturn(0); 9682999313aSBarry Smith } 9692999313aSBarry Smith 9707adad957SLisandro Dalcin #if 0 9712999313aSBarry Smith #undef __FUNCT__ 9724a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 9736849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 974e24b481bSBarry Smith { 975e24b481bSBarry Smith PetscFunctionBegin; 976e24b481bSBarry Smith PetscFunctionReturn(0); 977e24b481bSBarry Smith } 9787adad957SLisandro Dalcin #endif 979e24b481bSBarry Smith 9809b94acceSBarry Smith /* -----------------------------------------------------------*/ 9814a2ae208SSatish Balay #undef __FUNCT__ 9824a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 98352baeb72SSatish Balay /*@ 9849b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 9859b94acceSBarry Smith 986c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 987c7afd0dbSLois Curfman McInnes 988c7afd0dbSLois Curfman McInnes Input Parameters: 989906ed7ccSBarry Smith . comm - MPI communicator 9909b94acceSBarry Smith 9919b94acceSBarry Smith Output Parameter: 9929b94acceSBarry Smith . outsnes - the new SNES context 9939b94acceSBarry Smith 994c7afd0dbSLois Curfman McInnes Options Database Keys: 995c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 996c7afd0dbSLois Curfman McInnes and no preconditioning matrix 997c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 998c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 999c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 1000c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 1001c1f60f51SBarry Smith 100236851e7fSLois Curfman McInnes Level: beginner 100336851e7fSLois Curfman McInnes 10049b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 10059b94acceSBarry Smith 1006a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 1007a8054027SBarry Smith 10089b94acceSBarry Smith @*/ 10097087cfbeSBarry Smith PetscErrorCode SNESCreate(MPI_Comm comm,SNES *outsnes) 10109b94acceSBarry Smith { 1011dfbe8321SBarry Smith PetscErrorCode ierr; 10129b94acceSBarry Smith SNES snes; 1013fa9f3622SBarry Smith SNESKSPEW *kctx; 101437fcc0dbSBarry Smith 10153a40ed3dSBarry Smith PetscFunctionBegin; 1016ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 10178ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 10188ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 10198ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 10208ba1e511SMatthew Knepley #endif 10218ba1e511SMatthew Knepley 10220700a824SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 10237adad957SLisandro Dalcin 102485385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 10259b94acceSBarry Smith snes->max_its = 50; 10269750a799SBarry Smith snes->max_funcs = 10000; 10279b94acceSBarry Smith snes->norm = 0.0; 1028b4874afaSBarry Smith snes->rtol = 1.e-8; 1029b4874afaSBarry Smith snes->ttol = 0.0; 103070441072SBarry Smith snes->abstol = 1.e-50; 10319b94acceSBarry Smith snes->xtol = 1.e-8; 10324b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 10339b94acceSBarry Smith snes->nfuncs = 0; 103450ffb88aSMatthew Knepley snes->numFailures = 0; 103550ffb88aSMatthew Knepley snes->maxFailures = 1; 10367a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1037e35cf81dSBarry Smith snes->lagjacobian = 1; 1038a8054027SBarry Smith snes->lagpreconditioner = 1; 1039639f9d9dSBarry Smith snes->numbermonitors = 0; 10409b94acceSBarry Smith snes->data = 0; 10414dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1042186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 10436f24a144SLois Curfman McInnes snes->nwork = 0; 104458c9b817SLisandro Dalcin snes->work = 0; 104558c9b817SLisandro Dalcin snes->nvwork = 0; 104658c9b817SLisandro Dalcin snes->vwork = 0; 1047758f92a0SBarry Smith snes->conv_hist_len = 0; 1048758f92a0SBarry Smith snes->conv_hist_max = 0; 1049758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 1050758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 1051758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1052184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 10539b94acceSBarry Smith 10543d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 10553d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 10563d4c4710SBarry Smith 10579b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 105838f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 10599b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 10609b94acceSBarry Smith kctx->version = 2; 10619b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 10629b94acceSBarry Smith this was too large for some test cases */ 106375567043SBarry Smith kctx->rtol_last = 0.0; 10649b94acceSBarry Smith kctx->rtol_max = .9; 10659b94acceSBarry Smith kctx->gamma = 1.0; 106671f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 106771f87433Sdalcinl kctx->alpha2 = kctx->alpha; 10689b94acceSBarry Smith kctx->threshold = .1; 106975567043SBarry Smith kctx->lresid_last = 0.0; 107075567043SBarry Smith kctx->norm_last = 0.0; 10719b94acceSBarry Smith 10729b94acceSBarry Smith *outsnes = snes; 10733a40ed3dSBarry Smith PetscFunctionReturn(0); 10749b94acceSBarry Smith } 10759b94acceSBarry Smith 10764a2ae208SSatish Balay #undef __FUNCT__ 10774a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 10789b94acceSBarry Smith /*@C 10799b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 10809b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 10819b94acceSBarry Smith equations. 10829b94acceSBarry Smith 10833f9fe445SBarry Smith Logically Collective on SNES 1084fee21e36SBarry Smith 1085c7afd0dbSLois Curfman McInnes Input Parameters: 1086c7afd0dbSLois Curfman McInnes + snes - the SNES context 1087c7afd0dbSLois Curfman McInnes . r - vector to store function value 1088de044059SHong Zhang . func - function evaluation routine 1089c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1090c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 10919b94acceSBarry Smith 1092c7afd0dbSLois Curfman McInnes Calling sequence of func: 10938d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 1094c7afd0dbSLois Curfman McInnes 1095313e4042SLois Curfman McInnes . f - function vector 1096c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 10979b94acceSBarry Smith 10989b94acceSBarry Smith Notes: 10999b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 11009b94acceSBarry Smith $ f'(x) x = -f(x), 1101c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 11029b94acceSBarry Smith 110336851e7fSLois Curfman McInnes Level: beginner 110436851e7fSLois Curfman McInnes 11059b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 11069b94acceSBarry Smith 1107a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 11089b94acceSBarry Smith @*/ 11097087cfbeSBarry Smith PetscErrorCode SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 11109b94acceSBarry Smith { 111185385478SLisandro Dalcin PetscErrorCode ierr; 11123a40ed3dSBarry Smith PetscFunctionBegin; 11130700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1114d2a683ecSLisandro Dalcin if (r) { 1115d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1116d2a683ecSLisandro Dalcin PetscCheckSameComm(snes,1,r,2); 111785385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 11186bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 111985385478SLisandro Dalcin snes->vec_func = r; 1120d2a683ecSLisandro Dalcin } else if (!snes->vec_func && snes->dm) { 1121d2a683ecSLisandro Dalcin ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1122d2a683ecSLisandro Dalcin } 1123d2a683ecSLisandro Dalcin if (func) snes->ops->computefunction = func; 1124d2a683ecSLisandro Dalcin if (ctx) snes->funP = ctx; 11253a40ed3dSBarry Smith PetscFunctionReturn(0); 11269b94acceSBarry Smith } 11279b94acceSBarry Smith 1128d25893d9SBarry Smith #undef __FUNCT__ 1129d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess" 1130d25893d9SBarry Smith /*@C 1131d25893d9SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem 1132d25893d9SBarry Smith 1133d25893d9SBarry Smith Logically Collective on SNES 1134d25893d9SBarry Smith 1135d25893d9SBarry Smith Input Parameters: 1136d25893d9SBarry Smith + snes - the SNES context 1137d25893d9SBarry Smith . func - function evaluation routine 1138d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 1139d25893d9SBarry Smith function evaluation routine (may be PETSC_NULL) 1140d25893d9SBarry Smith 1141d25893d9SBarry Smith Calling sequence of func: 1142d25893d9SBarry Smith $ func (SNES snes,Vec x,void *ctx); 1143d25893d9SBarry Smith 1144d25893d9SBarry Smith . f - function vector 1145d25893d9SBarry Smith - ctx - optional user-defined function context 1146d25893d9SBarry Smith 1147d25893d9SBarry Smith Level: intermediate 1148d25893d9SBarry Smith 1149d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function 1150d25893d9SBarry Smith 1151d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 1152d25893d9SBarry Smith @*/ 1153d25893d9SBarry Smith PetscErrorCode SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx) 1154d25893d9SBarry Smith { 1155d25893d9SBarry Smith PetscFunctionBegin; 1156d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1157d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 1158d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 1159d25893d9SBarry Smith PetscFunctionReturn(0); 1160d25893d9SBarry Smith } 1161d25893d9SBarry Smith 11623ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 11633ab0aad5SBarry Smith #undef __FUNCT__ 11641096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 11651096aae1SMatthew Knepley /*@C 11661096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 11671096aae1SMatthew Knepley it assumes a zero right hand side. 11681096aae1SMatthew Knepley 11693f9fe445SBarry Smith Logically Collective on SNES 11701096aae1SMatthew Knepley 11711096aae1SMatthew Knepley Input Parameter: 11721096aae1SMatthew Knepley . snes - the SNES context 11731096aae1SMatthew Knepley 11741096aae1SMatthew Knepley Output Parameter: 1175bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 11761096aae1SMatthew Knepley 11771096aae1SMatthew Knepley Level: intermediate 11781096aae1SMatthew Knepley 11791096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 11801096aae1SMatthew Knepley 118185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 11821096aae1SMatthew Knepley @*/ 11837087cfbeSBarry Smith PetscErrorCode SNESGetRhs(SNES snes,Vec *rhs) 11841096aae1SMatthew Knepley { 11851096aae1SMatthew Knepley PetscFunctionBegin; 11860700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 11871096aae1SMatthew Knepley PetscValidPointer(rhs,2); 118885385478SLisandro Dalcin *rhs = snes->vec_rhs; 11891096aae1SMatthew Knepley PetscFunctionReturn(0); 11901096aae1SMatthew Knepley } 11911096aae1SMatthew Knepley 11921096aae1SMatthew Knepley #undef __FUNCT__ 11934a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 11949b94acceSBarry Smith /*@ 119536851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 11969b94acceSBarry Smith SNESSetFunction(). 11979b94acceSBarry Smith 1198c7afd0dbSLois Curfman McInnes Collective on SNES 1199c7afd0dbSLois Curfman McInnes 12009b94acceSBarry Smith Input Parameters: 1201c7afd0dbSLois Curfman McInnes + snes - the SNES context 1202c7afd0dbSLois Curfman McInnes - x - input vector 12039b94acceSBarry Smith 12049b94acceSBarry Smith Output Parameter: 12053638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 12069b94acceSBarry Smith 12071bffabb2SLois Curfman McInnes Notes: 120836851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 120936851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 121036851e7fSLois Curfman McInnes themselves. 121136851e7fSLois Curfman McInnes 121236851e7fSLois Curfman McInnes Level: developer 121336851e7fSLois Curfman McInnes 12149b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 12159b94acceSBarry Smith 1216a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 12179b94acceSBarry Smith @*/ 12187087cfbeSBarry Smith PetscErrorCode SNESComputeFunction(SNES snes,Vec x,Vec y) 12199b94acceSBarry Smith { 1220dfbe8321SBarry Smith PetscErrorCode ierr; 12219b94acceSBarry Smith 12223a40ed3dSBarry Smith PetscFunctionBegin; 12230700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12240700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 12250700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 1226c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1227c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 1228184914b5SBarry Smith 1229d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1230e7788613SBarry Smith if (snes->ops->computefunction) { 1231d64ed03dSBarry Smith PetscStackPush("SNES user function"); 123239d508bbSBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1233d64ed03dSBarry Smith PetscStackPop; 123485385478SLisandro Dalcin } else if (snes->vec_rhs) { 12351096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 1236*644e2e5bSBarry Smith } if (snes->dm) { 1237*644e2e5bSBarry Smith ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr); 1238*644e2e5bSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve()."); 123985385478SLisandro Dalcin if (snes->vec_rhs) { 124085385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 12413ab0aad5SBarry Smith } 1242ae3c334cSLois Curfman McInnes snes->nfuncs++; 1243d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 12443a40ed3dSBarry Smith PetscFunctionReturn(0); 12459b94acceSBarry Smith } 12469b94acceSBarry Smith 12474a2ae208SSatish Balay #undef __FUNCT__ 12484a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 124962fef451SLois Curfman McInnes /*@ 125062fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 125162fef451SLois Curfman McInnes set with SNESSetJacobian(). 125262fef451SLois Curfman McInnes 1253c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1254c7afd0dbSLois Curfman McInnes 125562fef451SLois Curfman McInnes Input Parameters: 1256c7afd0dbSLois Curfman McInnes + snes - the SNES context 1257c7afd0dbSLois Curfman McInnes - x - input vector 125862fef451SLois Curfman McInnes 125962fef451SLois Curfman McInnes Output Parameters: 1260c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 126162fef451SLois Curfman McInnes . B - optional preconditioning matrix 12622b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1263fee21e36SBarry Smith 1264e35cf81dSBarry Smith Options Database Keys: 1265e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1266693365a8SJed Brown . -snes_lag_jacobian <lag> 1267693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 1268693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 1269693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 1270693365a8SJed Brown - -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 1271e35cf81dSBarry Smith 127262fef451SLois Curfman McInnes Notes: 127362fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 127462fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 127562fef451SLois Curfman McInnes 127694b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1277dc5a77f8SLois Curfman McInnes flag parameter. 127862fef451SLois Curfman McInnes 127936851e7fSLois Curfman McInnes Level: developer 128036851e7fSLois Curfman McInnes 128162fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 128262fef451SLois Curfman McInnes 1283e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 128462fef451SLois Curfman McInnes @*/ 12857087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 12869b94acceSBarry Smith { 1287dfbe8321SBarry Smith PetscErrorCode ierr; 1288ace3abfcSBarry Smith PetscBool flag; 12893a40ed3dSBarry Smith 12903a40ed3dSBarry Smith PetscFunctionBegin; 12910700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12920700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 12934482741eSBarry Smith PetscValidPointer(flg,5); 1294c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1295e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1296ebd3b9afSBarry Smith 1297ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1298ebd3b9afSBarry Smith 1299fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1300fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1301fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1302fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1303e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1304e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1305ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1306ebd3b9afSBarry Smith if (flag) { 1307ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1308ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1309ebd3b9afSBarry Smith } 1310e35cf81dSBarry Smith PetscFunctionReturn(0); 1311e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1312e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1313e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1314ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1315ebd3b9afSBarry Smith if (flag) { 1316ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1317ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1318ebd3b9afSBarry Smith } 1319e35cf81dSBarry Smith PetscFunctionReturn(0); 1320e35cf81dSBarry Smith } 1321e35cf81dSBarry Smith 1322c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1323e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1324d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1325e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1326d64ed03dSBarry Smith PetscStackPop; 1327d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1328a8054027SBarry Smith 13293b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 13303b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 13313b4f5425SBarry Smith snes->lagpreconditioner = -1; 13323b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1333a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1334a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1335a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1336a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1337a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1338a8054027SBarry Smith } 1339a8054027SBarry Smith 13406d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 13410700a824SBarry Smith /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 13420700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,4); */ 1343693365a8SJed Brown { 1344693365a8SJed Brown PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE; 1345693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr); 1346693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 1347693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 1348693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr); 1349693365a8SJed Brown if (flag || flag_draw || flag_contour) { 1350693365a8SJed Brown Mat Bexp_mine = PETSC_NULL,Bexp,FDexp; 1351693365a8SJed Brown MatStructure mstruct; 1352693365a8SJed Brown PetscViewer vdraw,vstdout; 13536b3a5b13SJed Brown PetscBool flg; 1354693365a8SJed Brown if (flag_operator) { 1355693365a8SJed Brown ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr); 1356693365a8SJed Brown Bexp = Bexp_mine; 1357693365a8SJed Brown } else { 1358693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 1359693365a8SJed Brown ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr); 1360693365a8SJed Brown if (flg) Bexp = *B; 1361693365a8SJed Brown else { 1362693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 1363693365a8SJed Brown ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr); 1364693365a8SJed Brown Bexp = Bexp_mine; 1365693365a8SJed Brown } 1366693365a8SJed Brown } 1367693365a8SJed Brown ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr); 1368693365a8SJed Brown ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr); 1369693365a8SJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 1370693365a8SJed Brown if (flag_draw || flag_contour) { 1371693365a8SJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 1372693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 1373693365a8SJed Brown } else vdraw = PETSC_NULL; 1374693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr); 1375693365a8SJed Brown if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);} 1376693365a8SJed Brown if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);} 1377693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr); 1378693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1379693365a8SJed Brown if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);} 1380693365a8SJed Brown ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 1381693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr); 1382693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1383693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 1384693365a8SJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 1385693365a8SJed Brown ierr = MatView(FDexp,vdraw);CHKERRQ(ierr); 1386693365a8SJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 1387693365a8SJed Brown } 1388693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 1389693365a8SJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 1390693365a8SJed Brown ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr); 1391693365a8SJed Brown ierr = MatDestroy(&FDexp);CHKERRQ(ierr); 1392693365a8SJed Brown } 1393693365a8SJed Brown } 13943a40ed3dSBarry Smith PetscFunctionReturn(0); 13959b94acceSBarry Smith } 13969b94acceSBarry Smith 13974a2ae208SSatish Balay #undef __FUNCT__ 13984a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 13999b94acceSBarry Smith /*@C 14009b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1401044dda88SLois Curfman McInnes location to store the matrix. 14029b94acceSBarry Smith 14033f9fe445SBarry Smith Logically Collective on SNES and Mat 1404c7afd0dbSLois Curfman McInnes 14059b94acceSBarry Smith Input Parameters: 1406c7afd0dbSLois Curfman McInnes + snes - the SNES context 14079b94acceSBarry Smith . A - Jacobian matrix 14089b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 1409efd51863SBarry Smith . func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value) 1410c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1411efd51863SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value) 14129b94acceSBarry Smith 14139b94acceSBarry Smith Calling sequence of func: 14148d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 14159b94acceSBarry Smith 1416c7afd0dbSLois Curfman McInnes + x - input vector 14179b94acceSBarry Smith . A - Jacobian matrix 14189b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1419ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 14202b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1421c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 14229b94acceSBarry Smith 14239b94acceSBarry Smith Notes: 142494b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 14252cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1426ac21db08SLois Curfman McInnes 1427ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 14289b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 14299b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 14309b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 14319b94acceSBarry Smith throughout the global iterations. 14329b94acceSBarry Smith 143316913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 143416913363SBarry Smith each matrix. 143516913363SBarry Smith 1436a8a26c1eSJed Brown If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument 1437a8a26c1eSJed Brown must be a MatFDColoring. 1438a8a26c1eSJed Brown 143936851e7fSLois Curfman McInnes Level: beginner 144036851e7fSLois Curfman McInnes 14419b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 14429b94acceSBarry Smith 14433ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 14449b94acceSBarry Smith @*/ 14457087cfbeSBarry Smith PetscErrorCode SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 14469b94acceSBarry Smith { 1447dfbe8321SBarry Smith PetscErrorCode ierr; 14483a7fca6bSBarry Smith 14493a40ed3dSBarry Smith PetscFunctionBegin; 14500700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14510700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 14520700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1453c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 145406975374SJed Brown if (B) PetscCheckSameComm(snes,1,B,3); 1455e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 14563a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 14573a7fca6bSBarry Smith if (A) { 14587dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 14596bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 14609b94acceSBarry Smith snes->jacobian = A; 14613a7fca6bSBarry Smith } 14623a7fca6bSBarry Smith if (B) { 14637dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 14646bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 14659b94acceSBarry Smith snes->jacobian_pre = B; 14663a7fca6bSBarry Smith } 14673a40ed3dSBarry Smith PetscFunctionReturn(0); 14689b94acceSBarry Smith } 146962fef451SLois Curfman McInnes 14704a2ae208SSatish Balay #undef __FUNCT__ 14714a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1472c2aafc4cSSatish Balay /*@C 1473b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1474b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1475b4fd4287SBarry Smith 1476c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1477c7afd0dbSLois Curfman McInnes 1478b4fd4287SBarry Smith Input Parameter: 1479b4fd4287SBarry Smith . snes - the nonlinear solver context 1480b4fd4287SBarry Smith 1481b4fd4287SBarry Smith Output Parameters: 1482c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1483b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 148470e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 148570e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1486fee21e36SBarry Smith 148736851e7fSLois Curfman McInnes Level: advanced 148836851e7fSLois Curfman McInnes 1489b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1490b4fd4287SBarry Smith @*/ 14917087cfbeSBarry Smith PetscErrorCode SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1492b4fd4287SBarry Smith { 14933a40ed3dSBarry Smith PetscFunctionBegin; 14940700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1495b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1496b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1497e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 149870e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 14993a40ed3dSBarry Smith PetscFunctionReturn(0); 1500b4fd4287SBarry Smith } 1501b4fd4287SBarry Smith 15029b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 15039b94acceSBarry Smith 15044a2ae208SSatish Balay #undef __FUNCT__ 15054a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 15069b94acceSBarry Smith /*@ 15079b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1508272ac6f2SLois Curfman McInnes of a nonlinear solver. 15099b94acceSBarry Smith 1510fee21e36SBarry Smith Collective on SNES 1511fee21e36SBarry Smith 1512c7afd0dbSLois Curfman McInnes Input Parameters: 151370e92668SMatthew Knepley . snes - the SNES context 1514c7afd0dbSLois Curfman McInnes 1515272ac6f2SLois Curfman McInnes Notes: 1516272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1517272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1518272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1519272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1520272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1521272ac6f2SLois Curfman McInnes 152236851e7fSLois Curfman McInnes Level: advanced 152336851e7fSLois Curfman McInnes 15249b94acceSBarry Smith .keywords: SNES, nonlinear, setup 15259b94acceSBarry Smith 15269b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 15279b94acceSBarry Smith @*/ 15287087cfbeSBarry Smith PetscErrorCode SNESSetUp(SNES snes) 15299b94acceSBarry Smith { 1530dfbe8321SBarry Smith PetscErrorCode ierr; 15313a40ed3dSBarry Smith 15323a40ed3dSBarry Smith PetscFunctionBegin; 15330700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15344dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 15359b94acceSBarry Smith 15367adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 153785385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 153885385478SLisandro Dalcin } 153985385478SLisandro Dalcin 1540efd51863SBarry Smith if (!snes->vec_func && snes->dm && !snes->vec_rhs) { 1541efd51863SBarry Smith ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1542efd51863SBarry Smith } 154317186662SBarry Smith if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 1544*644e2e5bSBarry Smith if (!snes->ops->computefunction && !snes->vec_rhs && !snes->dm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() or SNESSetDM() first"); 154517186662SBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 154658c9b817SLisandro Dalcin if (snes->vec_rhs == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector"); 154758c9b817SLisandro Dalcin 154858c9b817SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 154958c9b817SLisandro Dalcin ierr = VecDuplicate(snes->vec_rhs, &snes->vec_func);CHKERRQ(ierr); 155058c9b817SLisandro Dalcin } 155158c9b817SLisandro Dalcin if (!snes->vec_sol_update /* && snes->vec_sol */) { 155258c9b817SLisandro Dalcin ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr); 155358c9b817SLisandro Dalcin ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr); 155458c9b817SLisandro Dalcin } 155558c9b817SLisandro Dalcin 1556ef8dffc7SBarry Smith if (!snes->ops->computejacobian && snes->dm) { 1557ef8dffc7SBarry Smith Mat J; 1558ef8dffc7SBarry Smith ISColoring coloring; 1559ef8dffc7SBarry Smith MatFDColoring fd; 1560a703fe33SLois Curfman McInnes 1561ef8dffc7SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 1562ef8dffc7SBarry Smith ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr); 1563ef8dffc7SBarry Smith ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr); 1564*644e2e5bSBarry Smith if (snes->ops->computefunction) { 1565ef8dffc7SBarry Smith ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr); 1566*644e2e5bSBarry Smith } else { 1567*644e2e5bSBarry Smith void *ctx; 1568*644e2e5bSBarry Smith ierr = DMGetApplicationContext(snes->dm,&ctx);CHKERRQ(ierr); 1569*644e2e5bSBarry Smith ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))SNESDAFormFunction,ctx);CHKERRQ(ierr); 1570*644e2e5bSBarry Smith } 1571ef8dffc7SBarry Smith ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr); 15726bf464f9SBarry Smith ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr); 1573efd51863SBarry Smith } else if (snes->dm && !snes->jacobian_pre){ 1574efd51863SBarry Smith Mat J; 1575efd51863SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 1576efd51863SBarry Smith ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 1577efd51863SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1578ef8dffc7SBarry Smith } 1579efd51863SBarry Smith 1580b710008aSBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);} 1581b710008aSBarry Smith 1582d25893d9SBarry Smith if (snes->ops->usercompute && !snes->user) { 1583d25893d9SBarry Smith ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr); 1584d25893d9SBarry Smith } 1585d25893d9SBarry Smith 1586410397dcSLisandro Dalcin if (snes->ops->setup) { 1587410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1588410397dcSLisandro Dalcin } 158958c9b817SLisandro Dalcin 15907aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 15913a40ed3dSBarry Smith PetscFunctionReturn(0); 15929b94acceSBarry Smith } 15939b94acceSBarry Smith 15944a2ae208SSatish Balay #undef __FUNCT__ 159537596af1SLisandro Dalcin #define __FUNCT__ "SNESReset" 159637596af1SLisandro Dalcin /*@ 159737596af1SLisandro Dalcin SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats 159837596af1SLisandro Dalcin 159937596af1SLisandro Dalcin Collective on SNES 160037596af1SLisandro Dalcin 160137596af1SLisandro Dalcin Input Parameter: 160237596af1SLisandro Dalcin . snes - iterative context obtained from SNESCreate() 160337596af1SLisandro Dalcin 1604d25893d9SBarry Smith Level: intermediate 1605d25893d9SBarry Smith 1606d25893d9SBarry Smith Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext() 160737596af1SLisandro Dalcin 160837596af1SLisandro Dalcin .keywords: SNES, destroy 160937596af1SLisandro Dalcin 161037596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 161137596af1SLisandro Dalcin @*/ 161237596af1SLisandro Dalcin PetscErrorCode SNESReset(SNES snes) 161337596af1SLisandro Dalcin { 161437596af1SLisandro Dalcin PetscErrorCode ierr; 161537596af1SLisandro Dalcin 161637596af1SLisandro Dalcin PetscFunctionBegin; 161737596af1SLisandro Dalcin PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1618d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 1619d25893d9SBarry Smith ierr = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr); 1620d25893d9SBarry Smith snes->user = PETSC_NULL; 1621d25893d9SBarry Smith } 1622*644e2e5bSBarry Smith if (snes->ops->computejacobian == SNESDefaultComputeJacobianColor && snes->dm) { 1623*644e2e5bSBarry Smith ierr = MatFDColoringDestroy((MatFDColoring*)&snes->jacP);CHKERRQ(ierr); 1624*644e2e5bSBarry Smith snes->ops->computejacobian = PETSC_NULL; 1625*644e2e5bSBarry Smith } 1626d25893d9SBarry Smith 162737596af1SLisandro Dalcin if (snes->ops->reset) { 162837596af1SLisandro Dalcin ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr); 162937596af1SLisandro Dalcin } 163037596af1SLisandro Dalcin if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);} 16316bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 16326bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 16336bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr); 16346bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 16356bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 16366bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 163737596af1SLisandro Dalcin if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);} 163837596af1SLisandro Dalcin if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);} 163937596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 164037596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 164137596af1SLisandro Dalcin PetscFunctionReturn(0); 164237596af1SLisandro Dalcin } 164337596af1SLisandro Dalcin 164437596af1SLisandro Dalcin #undef __FUNCT__ 16454a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 164652baeb72SSatish Balay /*@ 16479b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 16489b94acceSBarry Smith with SNESCreate(). 16499b94acceSBarry Smith 1650c7afd0dbSLois Curfman McInnes Collective on SNES 1651c7afd0dbSLois Curfman McInnes 16529b94acceSBarry Smith Input Parameter: 16539b94acceSBarry Smith . snes - the SNES context 16549b94acceSBarry Smith 165536851e7fSLois Curfman McInnes Level: beginner 165636851e7fSLois Curfman McInnes 16579b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 16589b94acceSBarry Smith 165963a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 16609b94acceSBarry Smith @*/ 16616bf464f9SBarry Smith PetscErrorCode SNESDestroy(SNES *snes) 16629b94acceSBarry Smith { 16636849ba73SBarry Smith PetscErrorCode ierr; 16643a40ed3dSBarry Smith 16653a40ed3dSBarry Smith PetscFunctionBegin; 16666bf464f9SBarry Smith if (!*snes) PetscFunctionReturn(0); 16676bf464f9SBarry Smith PetscValidHeaderSpecific((*snes),SNES_CLASSID,1); 16686bf464f9SBarry Smith if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);} 1669d4bb536fSBarry Smith 16706bf464f9SBarry Smith ierr = SNESReset((*snes));CHKERRQ(ierr); 16716b8b9a38SLisandro Dalcin 1672be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 16736bf464f9SBarry Smith ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr); 16746bf464f9SBarry Smith if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);} 16756d4c513bSLisandro Dalcin 16766bf464f9SBarry Smith ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr); 16776bf464f9SBarry Smith ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr); 16786b8b9a38SLisandro Dalcin 16796bf464f9SBarry Smith ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr); 16806bf464f9SBarry Smith if ((*snes)->ops->convergeddestroy) { 16816bf464f9SBarry Smith ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr); 16826b8b9a38SLisandro Dalcin } 16836bf464f9SBarry Smith if ((*snes)->conv_malloc) { 16846bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr); 16856bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr); 168658c9b817SLisandro Dalcin } 16876bf464f9SBarry Smith ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr); 1688a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 16893a40ed3dSBarry Smith PetscFunctionReturn(0); 16909b94acceSBarry Smith } 16919b94acceSBarry Smith 16929b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 16939b94acceSBarry Smith 16944a2ae208SSatish Balay #undef __FUNCT__ 1695a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1696a8054027SBarry Smith /*@ 1697a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1698a8054027SBarry Smith 16993f9fe445SBarry Smith Logically Collective on SNES 1700a8054027SBarry Smith 1701a8054027SBarry Smith Input Parameters: 1702a8054027SBarry Smith + snes - the SNES context 1703a8054027SBarry 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 17043b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1705a8054027SBarry Smith 1706a8054027SBarry Smith Options Database Keys: 1707a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1708a8054027SBarry Smith 1709a8054027SBarry Smith Notes: 1710a8054027SBarry Smith The default is 1 1711a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1712a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1713a8054027SBarry Smith 1714a8054027SBarry Smith Level: intermediate 1715a8054027SBarry Smith 1716a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1717a8054027SBarry Smith 1718e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1719a8054027SBarry Smith 1720a8054027SBarry Smith @*/ 17217087cfbeSBarry Smith PetscErrorCode SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1722a8054027SBarry Smith { 1723a8054027SBarry Smith PetscFunctionBegin; 17240700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1725e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1726e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1727c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 1728a8054027SBarry Smith snes->lagpreconditioner = lag; 1729a8054027SBarry Smith PetscFunctionReturn(0); 1730a8054027SBarry Smith } 1731a8054027SBarry Smith 1732a8054027SBarry Smith #undef __FUNCT__ 1733efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence" 1734efd51863SBarry Smith /*@ 1735efd51863SBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does 1736efd51863SBarry Smith 1737efd51863SBarry Smith Logically Collective on SNES 1738efd51863SBarry Smith 1739efd51863SBarry Smith Input Parameters: 1740efd51863SBarry Smith + snes - the SNES context 1741efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 1742efd51863SBarry Smith 1743efd51863SBarry Smith Options Database Keys: 1744efd51863SBarry Smith . -snes_grid_sequence <steps> 1745efd51863SBarry Smith 1746efd51863SBarry Smith Level: intermediate 1747efd51863SBarry Smith 1748efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1749efd51863SBarry Smith 1750efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1751efd51863SBarry Smith 1752efd51863SBarry Smith @*/ 1753efd51863SBarry Smith PetscErrorCode SNESSetGridSequence(SNES snes,PetscInt steps) 1754efd51863SBarry Smith { 1755efd51863SBarry Smith PetscFunctionBegin; 1756efd51863SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1757efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes,steps,2); 1758efd51863SBarry Smith snes->gridsequence = steps; 1759efd51863SBarry Smith PetscFunctionReturn(0); 1760efd51863SBarry Smith } 1761efd51863SBarry Smith 1762efd51863SBarry Smith #undef __FUNCT__ 1763a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1764a8054027SBarry Smith /*@ 1765a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1766a8054027SBarry Smith 17673f9fe445SBarry Smith Not Collective 1768a8054027SBarry Smith 1769a8054027SBarry Smith Input Parameter: 1770a8054027SBarry Smith . snes - the SNES context 1771a8054027SBarry Smith 1772a8054027SBarry Smith Output Parameter: 1773a8054027SBarry 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 17743b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1775a8054027SBarry Smith 1776a8054027SBarry Smith Options Database Keys: 1777a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1778a8054027SBarry Smith 1779a8054027SBarry Smith Notes: 1780a8054027SBarry Smith The default is 1 1781a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1782a8054027SBarry Smith 1783a8054027SBarry Smith Level: intermediate 1784a8054027SBarry Smith 1785a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1786a8054027SBarry Smith 1787a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1788a8054027SBarry Smith 1789a8054027SBarry Smith @*/ 17907087cfbeSBarry Smith PetscErrorCode SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1791a8054027SBarry Smith { 1792a8054027SBarry Smith PetscFunctionBegin; 17930700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1794a8054027SBarry Smith *lag = snes->lagpreconditioner; 1795a8054027SBarry Smith PetscFunctionReturn(0); 1796a8054027SBarry Smith } 1797a8054027SBarry Smith 1798a8054027SBarry Smith #undef __FUNCT__ 1799e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 1800e35cf81dSBarry Smith /*@ 1801e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 1802e35cf81dSBarry Smith often the preconditioner is rebuilt. 1803e35cf81dSBarry Smith 18043f9fe445SBarry Smith Logically Collective on SNES 1805e35cf81dSBarry Smith 1806e35cf81dSBarry Smith Input Parameters: 1807e35cf81dSBarry Smith + snes - the SNES context 1808e35cf81dSBarry 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 1809fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 1810e35cf81dSBarry Smith 1811e35cf81dSBarry Smith Options Database Keys: 1812e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1813e35cf81dSBarry Smith 1814e35cf81dSBarry Smith Notes: 1815e35cf81dSBarry Smith The default is 1 1816e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1817fe3ffe1eSBarry 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 1818fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 1819e35cf81dSBarry Smith 1820e35cf81dSBarry Smith Level: intermediate 1821e35cf81dSBarry Smith 1822e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1823e35cf81dSBarry Smith 1824e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 1825e35cf81dSBarry Smith 1826e35cf81dSBarry Smith @*/ 18277087cfbeSBarry Smith PetscErrorCode SNESSetLagJacobian(SNES snes,PetscInt lag) 1828e35cf81dSBarry Smith { 1829e35cf81dSBarry Smith PetscFunctionBegin; 18300700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1831e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1832e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1833c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 1834e35cf81dSBarry Smith snes->lagjacobian = lag; 1835e35cf81dSBarry Smith PetscFunctionReturn(0); 1836e35cf81dSBarry Smith } 1837e35cf81dSBarry Smith 1838e35cf81dSBarry Smith #undef __FUNCT__ 1839e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 1840e35cf81dSBarry Smith /*@ 1841e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 1842e35cf81dSBarry Smith 18433f9fe445SBarry Smith Not Collective 1844e35cf81dSBarry Smith 1845e35cf81dSBarry Smith Input Parameter: 1846e35cf81dSBarry Smith . snes - the SNES context 1847e35cf81dSBarry Smith 1848e35cf81dSBarry Smith Output Parameter: 1849e35cf81dSBarry 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 1850e35cf81dSBarry Smith the Jacobian is built etc. 1851e35cf81dSBarry Smith 1852e35cf81dSBarry Smith Options Database Keys: 1853e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1854e35cf81dSBarry Smith 1855e35cf81dSBarry Smith Notes: 1856e35cf81dSBarry Smith The default is 1 1857e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1858e35cf81dSBarry Smith 1859e35cf81dSBarry Smith Level: intermediate 1860e35cf81dSBarry Smith 1861e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1862e35cf81dSBarry Smith 1863e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 1864e35cf81dSBarry Smith 1865e35cf81dSBarry Smith @*/ 18667087cfbeSBarry Smith PetscErrorCode SNESGetLagJacobian(SNES snes,PetscInt *lag) 1867e35cf81dSBarry Smith { 1868e35cf81dSBarry Smith PetscFunctionBegin; 18690700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1870e35cf81dSBarry Smith *lag = snes->lagjacobian; 1871e35cf81dSBarry Smith PetscFunctionReturn(0); 1872e35cf81dSBarry Smith } 1873e35cf81dSBarry Smith 1874e35cf81dSBarry Smith #undef __FUNCT__ 18754a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 18769b94acceSBarry Smith /*@ 1877d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 18789b94acceSBarry Smith 18793f9fe445SBarry Smith Logically Collective on SNES 1880c7afd0dbSLois Curfman McInnes 18819b94acceSBarry Smith Input Parameters: 1882c7afd0dbSLois Curfman McInnes + snes - the SNES context 188370441072SBarry Smith . abstol - absolute convergence tolerance 188433174efeSLois Curfman McInnes . rtol - relative convergence tolerance 188533174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 188633174efeSLois Curfman McInnes of the change in the solution between steps 188733174efeSLois Curfman McInnes . maxit - maximum number of iterations 1888c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1889fee21e36SBarry Smith 189033174efeSLois Curfman McInnes Options Database Keys: 189170441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1892c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1893c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1894c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1895c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 18969b94acceSBarry Smith 1897d7a720efSLois Curfman McInnes Notes: 18989b94acceSBarry Smith The default maximum number of iterations is 50. 18999b94acceSBarry Smith The default maximum number of function evaluations is 1000. 19009b94acceSBarry Smith 190136851e7fSLois Curfman McInnes Level: intermediate 190236851e7fSLois Curfman McInnes 190333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 19049b94acceSBarry Smith 19052492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 19069b94acceSBarry Smith @*/ 19077087cfbeSBarry Smith PetscErrorCode SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 19089b94acceSBarry Smith { 19093a40ed3dSBarry Smith PetscFunctionBegin; 19100700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1911c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,abstol,2); 1912c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol,3); 1913c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,stol,4); 1914c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxit,5); 1915c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxf,6); 1916c5eb9154SBarry Smith 191770441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1918d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1919d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1920d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1921d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 19223a40ed3dSBarry Smith PetscFunctionReturn(0); 19239b94acceSBarry Smith } 19249b94acceSBarry Smith 19254a2ae208SSatish Balay #undef __FUNCT__ 19264a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 19279b94acceSBarry Smith /*@ 192833174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 192933174efeSLois Curfman McInnes 1930c7afd0dbSLois Curfman McInnes Not Collective 1931c7afd0dbSLois Curfman McInnes 193233174efeSLois Curfman McInnes Input Parameters: 1933c7afd0dbSLois Curfman McInnes + snes - the SNES context 193485385478SLisandro Dalcin . atol - absolute convergence tolerance 193533174efeSLois Curfman McInnes . rtol - relative convergence tolerance 193633174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 193733174efeSLois Curfman McInnes of the change in the solution between steps 193833174efeSLois Curfman McInnes . maxit - maximum number of iterations 1939c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1940fee21e36SBarry Smith 194133174efeSLois Curfman McInnes Notes: 194233174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 194333174efeSLois Curfman McInnes 194436851e7fSLois Curfman McInnes Level: intermediate 194536851e7fSLois Curfman McInnes 194633174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 194733174efeSLois Curfman McInnes 194833174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 194933174efeSLois Curfman McInnes @*/ 19507087cfbeSBarry Smith PetscErrorCode SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 195133174efeSLois Curfman McInnes { 19523a40ed3dSBarry Smith PetscFunctionBegin; 19530700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 195485385478SLisandro Dalcin if (atol) *atol = snes->abstol; 195533174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 195633174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 195733174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 195833174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 19593a40ed3dSBarry Smith PetscFunctionReturn(0); 196033174efeSLois Curfman McInnes } 196133174efeSLois Curfman McInnes 19624a2ae208SSatish Balay #undef __FUNCT__ 19634a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 196433174efeSLois Curfman McInnes /*@ 19659b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 19669b94acceSBarry Smith 19673f9fe445SBarry Smith Logically Collective on SNES 1968fee21e36SBarry Smith 1969c7afd0dbSLois Curfman McInnes Input Parameters: 1970c7afd0dbSLois Curfman McInnes + snes - the SNES context 1971c7afd0dbSLois Curfman McInnes - tol - tolerance 1972c7afd0dbSLois Curfman McInnes 19739b94acceSBarry Smith Options Database Key: 1974c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 19759b94acceSBarry Smith 197636851e7fSLois Curfman McInnes Level: intermediate 197736851e7fSLois Curfman McInnes 19789b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 19799b94acceSBarry Smith 19802492ecdbSBarry Smith .seealso: SNESSetTolerances() 19819b94acceSBarry Smith @*/ 19827087cfbeSBarry Smith PetscErrorCode SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 19839b94acceSBarry Smith { 19843a40ed3dSBarry Smith PetscFunctionBegin; 19850700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1986c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,tol,2); 19879b94acceSBarry Smith snes->deltatol = tol; 19883a40ed3dSBarry Smith PetscFunctionReturn(0); 19899b94acceSBarry Smith } 19909b94acceSBarry Smith 1991df9fa365SBarry Smith /* 1992df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1993df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1994df9fa365SBarry Smith macros instead of functions 1995df9fa365SBarry Smith */ 19964a2ae208SSatish Balay #undef __FUNCT__ 1997a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 19987087cfbeSBarry Smith PetscErrorCode SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1999ce1608b8SBarry Smith { 2000dfbe8321SBarry Smith PetscErrorCode ierr; 2001ce1608b8SBarry Smith 2002ce1608b8SBarry Smith PetscFunctionBegin; 20030700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2004a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 2005ce1608b8SBarry Smith PetscFunctionReturn(0); 2006ce1608b8SBarry Smith } 2007ce1608b8SBarry Smith 20084a2ae208SSatish Balay #undef __FUNCT__ 2009a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 20107087cfbeSBarry Smith PetscErrorCode SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2011df9fa365SBarry Smith { 2012dfbe8321SBarry Smith PetscErrorCode ierr; 2013df9fa365SBarry Smith 2014df9fa365SBarry Smith PetscFunctionBegin; 2015a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2016df9fa365SBarry Smith PetscFunctionReturn(0); 2017df9fa365SBarry Smith } 2018df9fa365SBarry Smith 20194a2ae208SSatish Balay #undef __FUNCT__ 2020a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 20216bf464f9SBarry Smith PetscErrorCode SNESMonitorLGDestroy(PetscDrawLG *draw) 2022df9fa365SBarry Smith { 2023dfbe8321SBarry Smith PetscErrorCode ierr; 2024df9fa365SBarry Smith 2025df9fa365SBarry Smith PetscFunctionBegin; 2026a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2027df9fa365SBarry Smith PetscFunctionReturn(0); 2028df9fa365SBarry Smith } 2029df9fa365SBarry Smith 20307087cfbeSBarry Smith extern PetscErrorCode SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 2031b271bb04SBarry Smith #undef __FUNCT__ 2032b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 20337087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 2034b271bb04SBarry Smith { 2035b271bb04SBarry Smith PetscDrawLG lg; 2036b271bb04SBarry Smith PetscErrorCode ierr; 2037b271bb04SBarry Smith PetscReal x,y,per; 2038b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 2039b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 2040b271bb04SBarry Smith PetscDraw draw; 2041b271bb04SBarry Smith PetscFunctionBegin; 2042b271bb04SBarry Smith if (!monctx) { 2043b271bb04SBarry Smith MPI_Comm comm; 2044b271bb04SBarry Smith 2045b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 2046b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 2047b271bb04SBarry Smith } 2048b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 2049b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2050b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2051b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 2052b271bb04SBarry Smith x = (PetscReal) n; 2053b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 2054b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2055b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2056b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2057b271bb04SBarry Smith } 2058b271bb04SBarry Smith 2059b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 2060b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2061b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2062b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 2063b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 2064b271bb04SBarry Smith x = (PetscReal) n; 2065b271bb04SBarry Smith y = 100.0*per; 2066b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2067b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2068b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2069b271bb04SBarry Smith } 2070b271bb04SBarry Smith 2071b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 2072b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2073b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2074b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 2075b271bb04SBarry Smith x = (PetscReal) n; 2076b271bb04SBarry Smith y = (prev - rnorm)/prev; 2077b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2078b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2079b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2080b271bb04SBarry Smith } 2081b271bb04SBarry Smith 2082b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 2083b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2084b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2085b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 2086b271bb04SBarry Smith x = (PetscReal) n; 2087b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 2088b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 2089b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2090b271bb04SBarry Smith } 2091b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2092b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2093b271bb04SBarry Smith } 2094b271bb04SBarry Smith prev = rnorm; 2095b271bb04SBarry Smith PetscFunctionReturn(0); 2096b271bb04SBarry Smith } 2097b271bb04SBarry Smith 2098b271bb04SBarry Smith #undef __FUNCT__ 2099b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 21007087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2101b271bb04SBarry Smith { 2102b271bb04SBarry Smith PetscErrorCode ierr; 2103b271bb04SBarry Smith 2104b271bb04SBarry Smith PetscFunctionBegin; 2105b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2106b271bb04SBarry Smith PetscFunctionReturn(0); 2107b271bb04SBarry Smith } 2108b271bb04SBarry Smith 2109b271bb04SBarry Smith #undef __FUNCT__ 2110b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 21116bf464f9SBarry Smith PetscErrorCode SNESMonitorLGRangeDestroy(PetscDrawLG *draw) 2112b271bb04SBarry Smith { 2113b271bb04SBarry Smith PetscErrorCode ierr; 2114b271bb04SBarry Smith 2115b271bb04SBarry Smith PetscFunctionBegin; 2116b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2117b271bb04SBarry Smith PetscFunctionReturn(0); 2118b271bb04SBarry Smith } 2119b271bb04SBarry Smith 21207a03ce2fSLisandro Dalcin #undef __FUNCT__ 21217a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor" 21227a03ce2fSLisandro Dalcin /* 21237a03ce2fSLisandro Dalcin Runs the user provided monitor routines, if they exists. 21247a03ce2fSLisandro Dalcin */ 21257a03ce2fSLisandro Dalcin PetscErrorCode SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm) 21267a03ce2fSLisandro Dalcin { 21277a03ce2fSLisandro Dalcin PetscErrorCode ierr; 21287a03ce2fSLisandro Dalcin PetscInt i,n = snes->numbermonitors; 21297a03ce2fSLisandro Dalcin 21307a03ce2fSLisandro Dalcin PetscFunctionBegin; 21317a03ce2fSLisandro Dalcin for (i=0; i<n; i++) { 21327a03ce2fSLisandro Dalcin ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr); 21337a03ce2fSLisandro Dalcin } 21347a03ce2fSLisandro Dalcin PetscFunctionReturn(0); 21357a03ce2fSLisandro Dalcin } 21367a03ce2fSLisandro Dalcin 21379b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 21389b94acceSBarry Smith 21394a2ae208SSatish Balay #undef __FUNCT__ 2140a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 21419b94acceSBarry Smith /*@C 2142a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 21439b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 21449b94acceSBarry Smith progress. 21459b94acceSBarry Smith 21463f9fe445SBarry Smith Logically Collective on SNES 2147fee21e36SBarry Smith 2148c7afd0dbSLois Curfman McInnes Input Parameters: 2149c7afd0dbSLois Curfman McInnes + snes - the SNES context 2150c7afd0dbSLois Curfman McInnes . func - monitoring routine 2151b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 2152e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 2153b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 2154b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 21559b94acceSBarry Smith 2156c7afd0dbSLois Curfman McInnes Calling sequence of func: 2157a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 2158c7afd0dbSLois Curfman McInnes 2159c7afd0dbSLois Curfman McInnes + snes - the SNES context 2160c7afd0dbSLois Curfman McInnes . its - iteration number 2161c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 216240a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 21639b94acceSBarry Smith 21649665c990SLois Curfman McInnes Options Database Keys: 2165a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 2166a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 2167a6570f20SBarry Smith uses SNESMonitorLGCreate() 2168cca6129bSJed Brown - -snes_monitor_cancel - cancels all monitors that have 2169c7afd0dbSLois Curfman McInnes been hardwired into a code by 2170a6570f20SBarry Smith calls to SNESMonitorSet(), but 2171c7afd0dbSLois Curfman McInnes does not cancel those set via 2172c7afd0dbSLois Curfman McInnes the options database. 21739665c990SLois Curfman McInnes 2174639f9d9dSBarry Smith Notes: 21756bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 2176a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 21776bc08f3fSLois Curfman McInnes order in which they were set. 2178639f9d9dSBarry Smith 2179025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 2180025f1a04SBarry Smith 218136851e7fSLois Curfman McInnes Level: intermediate 218236851e7fSLois Curfman McInnes 21839b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 21849b94acceSBarry Smith 2185a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 21869b94acceSBarry Smith @*/ 2187c2efdce3SBarry Smith PetscErrorCode SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 21889b94acceSBarry Smith { 2189b90d0a6eSBarry Smith PetscInt i; 2190b90d0a6eSBarry Smith 21913a40ed3dSBarry Smith PetscFunctionBegin; 21920700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 219317186662SBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2194b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 2195b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 2196b90d0a6eSBarry Smith 2197b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 2198b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 2199b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 2200b90d0a6eSBarry Smith PetscErrorCode ierr; 2201b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 2202b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 2203b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 2204c2efdce3SBarry Smith ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr); 2205b90d0a6eSBarry Smith PetscFunctionReturn(0); 2206b90d0a6eSBarry Smith } 2207b90d0a6eSBarry Smith } 2208b90d0a6eSBarry Smith } 2209b90d0a6eSBarry Smith } 2210b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 2211b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 2212639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 22133a40ed3dSBarry Smith PetscFunctionReturn(0); 22149b94acceSBarry Smith } 22159b94acceSBarry Smith 22164a2ae208SSatish Balay #undef __FUNCT__ 2217a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 22185cd90555SBarry Smith /*@C 2219a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 22205cd90555SBarry Smith 22213f9fe445SBarry Smith Logically Collective on SNES 2222c7afd0dbSLois Curfman McInnes 22235cd90555SBarry Smith Input Parameters: 22245cd90555SBarry Smith . snes - the SNES context 22255cd90555SBarry Smith 22261a480d89SAdministrator Options Database Key: 2227a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 2228a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 2229c7afd0dbSLois Curfman McInnes set via the options database 22305cd90555SBarry Smith 22315cd90555SBarry Smith Notes: 22325cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 22335cd90555SBarry Smith 223436851e7fSLois Curfman McInnes Level: intermediate 223536851e7fSLois Curfman McInnes 22365cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 22375cd90555SBarry Smith 2238a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 22395cd90555SBarry Smith @*/ 22407087cfbeSBarry Smith PetscErrorCode SNESMonitorCancel(SNES snes) 22415cd90555SBarry Smith { 2242d952e501SBarry Smith PetscErrorCode ierr; 2243d952e501SBarry Smith PetscInt i; 2244d952e501SBarry Smith 22455cd90555SBarry Smith PetscFunctionBegin; 22460700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2247d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 2248d952e501SBarry Smith if (snes->monitordestroy[i]) { 22493c4aec1bSBarry Smith ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr); 2250d952e501SBarry Smith } 2251d952e501SBarry Smith } 22525cd90555SBarry Smith snes->numbermonitors = 0; 22535cd90555SBarry Smith PetscFunctionReturn(0); 22545cd90555SBarry Smith } 22555cd90555SBarry Smith 22564a2ae208SSatish Balay #undef __FUNCT__ 22574a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 22589b94acceSBarry Smith /*@C 22599b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 22609b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 22619b94acceSBarry Smith 22623f9fe445SBarry Smith Logically Collective on SNES 2263fee21e36SBarry Smith 2264c7afd0dbSLois Curfman McInnes Input Parameters: 2265c7afd0dbSLois Curfman McInnes + snes - the SNES context 2266c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 22677f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 22687f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 22699b94acceSBarry Smith 2270c7afd0dbSLois Curfman McInnes Calling sequence of func: 227106ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 2272c7afd0dbSLois Curfman McInnes 2273c7afd0dbSLois Curfman McInnes + snes - the SNES context 227406ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 2275c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 2276184914b5SBarry Smith . reason - reason for convergence/divergence 2277c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 22784b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 22794b27c08aSLois Curfman McInnes - f - 2-norm of function 22809b94acceSBarry Smith 228136851e7fSLois Curfman McInnes Level: advanced 228236851e7fSLois Curfman McInnes 22839b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 22849b94acceSBarry Smith 228585385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 22869b94acceSBarry Smith @*/ 22877087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 22889b94acceSBarry Smith { 22897f7931b9SBarry Smith PetscErrorCode ierr; 22907f7931b9SBarry Smith 22913a40ed3dSBarry Smith PetscFunctionBegin; 22920700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 229385385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 22947f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 22957f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 22967f7931b9SBarry Smith } 229785385478SLisandro Dalcin snes->ops->converged = func; 22987f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 229985385478SLisandro Dalcin snes->cnvP = cctx; 23003a40ed3dSBarry Smith PetscFunctionReturn(0); 23019b94acceSBarry Smith } 23029b94acceSBarry Smith 23034a2ae208SSatish Balay #undef __FUNCT__ 23044a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 230552baeb72SSatish Balay /*@ 2306184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 2307184914b5SBarry Smith 2308184914b5SBarry Smith Not Collective 2309184914b5SBarry Smith 2310184914b5SBarry Smith Input Parameter: 2311184914b5SBarry Smith . snes - the SNES context 2312184914b5SBarry Smith 2313184914b5SBarry Smith Output Parameter: 23144d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 2315184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 2316184914b5SBarry Smith 2317184914b5SBarry Smith Level: intermediate 2318184914b5SBarry Smith 2319184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 2320184914b5SBarry Smith 2321184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 2322184914b5SBarry Smith 232385385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 2324184914b5SBarry Smith @*/ 23257087cfbeSBarry Smith PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 2326184914b5SBarry Smith { 2327184914b5SBarry Smith PetscFunctionBegin; 23280700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23294482741eSBarry Smith PetscValidPointer(reason,2); 2330184914b5SBarry Smith *reason = snes->reason; 2331184914b5SBarry Smith PetscFunctionReturn(0); 2332184914b5SBarry Smith } 2333184914b5SBarry Smith 23344a2ae208SSatish Balay #undef __FUNCT__ 23354a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 2336c9005455SLois Curfman McInnes /*@ 2337c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 2338c9005455SLois Curfman McInnes 23393f9fe445SBarry Smith Logically Collective on SNES 2340fee21e36SBarry Smith 2341c7afd0dbSLois Curfman McInnes Input Parameters: 2342c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 23438c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 2344cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 2345758f92a0SBarry Smith . na - size of a and its 234664731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 2347758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 2348c7afd0dbSLois Curfman McInnes 2349308dcc3eSBarry Smith Notes: 2350308dcc3eSBarry Smith If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a 2351308dcc3eSBarry Smith default array of length 10000 is allocated. 2352308dcc3eSBarry Smith 2353c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 2354c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 2355c9005455SLois Curfman McInnes during the section of code that is being timed. 2356c9005455SLois Curfman McInnes 235736851e7fSLois Curfman McInnes Level: intermediate 235836851e7fSLois Curfman McInnes 2359c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2360758f92a0SBarry Smith 236108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2362758f92a0SBarry Smith 2363c9005455SLois Curfman McInnes @*/ 23647087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset) 2365c9005455SLois Curfman McInnes { 2366308dcc3eSBarry Smith PetscErrorCode ierr; 2367308dcc3eSBarry Smith 23683a40ed3dSBarry Smith PetscFunctionBegin; 23690700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23704482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2371a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2372308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) { 2373308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2374308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr); 2375308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr); 2376308dcc3eSBarry Smith snes->conv_malloc = PETSC_TRUE; 2377308dcc3eSBarry Smith } 2378c9005455SLois Curfman McInnes snes->conv_hist = a; 2379758f92a0SBarry Smith snes->conv_hist_its = its; 2380758f92a0SBarry Smith snes->conv_hist_max = na; 2381a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2382758f92a0SBarry Smith snes->conv_hist_reset = reset; 2383758f92a0SBarry Smith PetscFunctionReturn(0); 2384758f92a0SBarry Smith } 2385758f92a0SBarry Smith 2386308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2387c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 2388c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 2389308dcc3eSBarry Smith EXTERN_C_BEGIN 2390308dcc3eSBarry Smith #undef __FUNCT__ 2391308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab" 2392308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 2393308dcc3eSBarry Smith { 2394308dcc3eSBarry Smith mxArray *mat; 2395308dcc3eSBarry Smith PetscInt i; 2396308dcc3eSBarry Smith PetscReal *ar; 2397308dcc3eSBarry Smith 2398308dcc3eSBarry Smith PetscFunctionBegin; 2399308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL); 2400308dcc3eSBarry Smith ar = (PetscReal*) mxGetData(mat); 2401308dcc3eSBarry Smith for (i=0; i<snes->conv_hist_len; i++) { 2402308dcc3eSBarry Smith ar[i] = snes->conv_hist[i]; 2403308dcc3eSBarry Smith } 2404308dcc3eSBarry Smith PetscFunctionReturn(mat); 2405308dcc3eSBarry Smith } 2406308dcc3eSBarry Smith EXTERN_C_END 2407308dcc3eSBarry Smith #endif 2408308dcc3eSBarry Smith 2409308dcc3eSBarry Smith 24104a2ae208SSatish Balay #undef __FUNCT__ 24114a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 24120c4c9dddSBarry Smith /*@C 2413758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 2414758f92a0SBarry Smith 24153f9fe445SBarry Smith Not Collective 2416758f92a0SBarry Smith 2417758f92a0SBarry Smith Input Parameter: 2418758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 2419758f92a0SBarry Smith 2420758f92a0SBarry Smith Output Parameters: 2421758f92a0SBarry Smith . a - array to hold history 2422758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 2423758f92a0SBarry Smith negative if not converged) for each solve. 2424758f92a0SBarry Smith - na - size of a and its 2425758f92a0SBarry Smith 2426758f92a0SBarry Smith Notes: 2427758f92a0SBarry Smith The calling sequence for this routine in Fortran is 2428758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 2429758f92a0SBarry Smith 2430758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 2431758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 2432758f92a0SBarry Smith during the section of code that is being timed. 2433758f92a0SBarry Smith 2434758f92a0SBarry Smith Level: intermediate 2435758f92a0SBarry Smith 2436758f92a0SBarry Smith .keywords: SNES, get, convergence, history 2437758f92a0SBarry Smith 2438758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 2439758f92a0SBarry Smith 2440758f92a0SBarry Smith @*/ 24417087cfbeSBarry Smith PetscErrorCode SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 2442758f92a0SBarry Smith { 2443758f92a0SBarry Smith PetscFunctionBegin; 24440700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2445758f92a0SBarry Smith if (a) *a = snes->conv_hist; 2446758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 2447758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 24483a40ed3dSBarry Smith PetscFunctionReturn(0); 2449c9005455SLois Curfman McInnes } 2450c9005455SLois Curfman McInnes 2451e74ef692SMatthew Knepley #undef __FUNCT__ 2452e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 2453ac226902SBarry Smith /*@C 245476b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 2455eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 24567e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 245776b2cf59SMatthew Knepley 24583f9fe445SBarry Smith Logically Collective on SNES 245976b2cf59SMatthew Knepley 246076b2cf59SMatthew Knepley Input Parameters: 246176b2cf59SMatthew Knepley . snes - The nonlinear solver context 246276b2cf59SMatthew Knepley . func - The function 246376b2cf59SMatthew Knepley 246476b2cf59SMatthew Knepley Calling sequence of func: 2465b5d30489SBarry Smith . func (SNES snes, PetscInt step); 246676b2cf59SMatthew Knepley 246776b2cf59SMatthew Knepley . step - The current step of the iteration 246876b2cf59SMatthew Knepley 2469fe97e370SBarry Smith Level: advanced 2470fe97e370SBarry Smith 2471fe97e370SBarry 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() 2472fe97e370SBarry Smith This is not used by most users. 247376b2cf59SMatthew Knepley 247476b2cf59SMatthew Knepley .keywords: SNES, update 2475b5d30489SBarry Smith 247685385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 247776b2cf59SMatthew Knepley @*/ 24787087cfbeSBarry Smith PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 247976b2cf59SMatthew Knepley { 248076b2cf59SMatthew Knepley PetscFunctionBegin; 24810700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 2482e7788613SBarry Smith snes->ops->update = func; 248376b2cf59SMatthew Knepley PetscFunctionReturn(0); 248476b2cf59SMatthew Knepley } 248576b2cf59SMatthew Knepley 2486e74ef692SMatthew Knepley #undef __FUNCT__ 2487e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 248876b2cf59SMatthew Knepley /*@ 248976b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 249076b2cf59SMatthew Knepley 249176b2cf59SMatthew Knepley Not collective 249276b2cf59SMatthew Knepley 249376b2cf59SMatthew Knepley Input Parameters: 249476b2cf59SMatthew Knepley . snes - The nonlinear solver context 249576b2cf59SMatthew Knepley . step - The current step of the iteration 249676b2cf59SMatthew Knepley 2497205452f4SMatthew Knepley Level: intermediate 2498205452f4SMatthew Knepley 249976b2cf59SMatthew Knepley .keywords: SNES, update 2500a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 250176b2cf59SMatthew Knepley @*/ 25027087cfbeSBarry Smith PetscErrorCode SNESDefaultUpdate(SNES snes, PetscInt step) 250376b2cf59SMatthew Knepley { 250476b2cf59SMatthew Knepley PetscFunctionBegin; 250576b2cf59SMatthew Knepley PetscFunctionReturn(0); 250676b2cf59SMatthew Knepley } 250776b2cf59SMatthew Knepley 25084a2ae208SSatish Balay #undef __FUNCT__ 25094a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 25109b94acceSBarry Smith /* 25119b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 25129b94acceSBarry Smith positive parameter delta. 25139b94acceSBarry Smith 25149b94acceSBarry Smith Input Parameters: 2515c7afd0dbSLois Curfman McInnes + snes - the SNES context 25169b94acceSBarry Smith . y - approximate solution of linear system 25179b94acceSBarry Smith . fnorm - 2-norm of current function 2518c7afd0dbSLois Curfman McInnes - delta - trust region size 25199b94acceSBarry Smith 25209b94acceSBarry Smith Output Parameters: 2521c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 25229b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 25239b94acceSBarry Smith region, and exceeds zero otherwise. 2524c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 25259b94acceSBarry Smith 25269b94acceSBarry Smith Note: 25274b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 25289b94acceSBarry Smith is set to be the maximum allowable step size. 25299b94acceSBarry Smith 25309b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 25319b94acceSBarry Smith */ 2532dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 25339b94acceSBarry Smith { 2534064f8208SBarry Smith PetscReal nrm; 2535ea709b57SSatish Balay PetscScalar cnorm; 2536dfbe8321SBarry Smith PetscErrorCode ierr; 25373a40ed3dSBarry Smith 25383a40ed3dSBarry Smith PetscFunctionBegin; 25390700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 25400700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 2541c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 2542184914b5SBarry Smith 2543064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 2544064f8208SBarry Smith if (nrm > *delta) { 2545064f8208SBarry Smith nrm = *delta/nrm; 2546064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 2547064f8208SBarry Smith cnorm = nrm; 25482dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 25499b94acceSBarry Smith *ynorm = *delta; 25509b94acceSBarry Smith } else { 25519b94acceSBarry Smith *gpnorm = 0.0; 2552064f8208SBarry Smith *ynorm = nrm; 25539b94acceSBarry Smith } 25543a40ed3dSBarry Smith PetscFunctionReturn(0); 25559b94acceSBarry Smith } 25569b94acceSBarry Smith 25574a2ae208SSatish Balay #undef __FUNCT__ 25584a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 25596ce558aeSBarry Smith /*@C 2560f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 2561f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 25629b94acceSBarry Smith 2563c7afd0dbSLois Curfman McInnes Collective on SNES 2564c7afd0dbSLois Curfman McInnes 2565b2002411SLois Curfman McInnes Input Parameters: 2566c7afd0dbSLois Curfman McInnes + snes - the SNES context 2567f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 256885385478SLisandro Dalcin - x - the solution vector. 25699b94acceSBarry Smith 2570b2002411SLois Curfman McInnes Notes: 25718ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 25728ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 25738ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 25748ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 25758ddd3da0SLois Curfman McInnes 257636851e7fSLois Curfman McInnes Level: beginner 257736851e7fSLois Curfman McInnes 25789b94acceSBarry Smith .keywords: SNES, nonlinear, solve 25799b94acceSBarry Smith 258085385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 25819b94acceSBarry Smith @*/ 25827087cfbeSBarry Smith PetscErrorCode SNESSolve(SNES snes,Vec b,Vec x) 25839b94acceSBarry Smith { 2584dfbe8321SBarry Smith PetscErrorCode ierr; 2585ace3abfcSBarry Smith PetscBool flg; 2586eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 2587eabae89aSBarry Smith PetscViewer viewer; 2588efd51863SBarry Smith PetscInt grid; 2589052efed2SBarry Smith 25903a40ed3dSBarry Smith PetscFunctionBegin; 25910700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 25920700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2593f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 25940700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 259585385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 259685385478SLisandro Dalcin 2597efd51863SBarry Smith for (grid=0; grid<snes->gridsequence+1; grid++) { 2598efd51863SBarry Smith 259985385478SLisandro Dalcin /* set solution vector */ 2600efd51863SBarry Smith if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);} 26016bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 260285385478SLisandro Dalcin snes->vec_sol = x; 260385385478SLisandro Dalcin /* set afine vector if provided */ 260485385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 26056bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 260685385478SLisandro Dalcin snes->vec_rhs = b; 260785385478SLisandro Dalcin 260870e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 26093f149594SLisandro Dalcin 2610d25893d9SBarry Smith if (!grid && snes->ops->computeinitialguess) { 2611d25893d9SBarry Smith ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr); 2612d25893d9SBarry Smith } 2613d25893d9SBarry Smith 2614abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 261550ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2616d5e45103SBarry Smith 26173f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 26184936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 261985385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 26204936397dSBarry Smith if (snes->domainerror){ 26214936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 26224936397dSBarry Smith snes->domainerror = PETSC_FALSE; 26234936397dSBarry Smith } 262417186662SBarry Smith if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 26253f149594SLisandro Dalcin 26267adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2627eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 26287adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2629eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 26306bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2631eabae89aSBarry Smith } 2632eabae89aSBarry Smith 263390d69ab7SBarry Smith flg = PETSC_FALSE; 2634acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr); 2635da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 26365968eb51SBarry Smith if (snes->printreason) { 26375968eb51SBarry Smith if (snes->reason > 0) { 26387adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 26395968eb51SBarry Smith } else { 26407adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 26415968eb51SBarry Smith } 26425968eb51SBarry Smith } 26435968eb51SBarry Smith 2644e113a28aSBarry Smith if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged"); 2645efd51863SBarry Smith if (grid < snes->gridsequence) { 2646efd51863SBarry Smith DM fine; 2647efd51863SBarry Smith Vec xnew; 2648efd51863SBarry Smith Mat interp; 2649efd51863SBarry Smith 2650efd51863SBarry Smith ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr); 2651efd51863SBarry Smith ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr); 2652efd51863SBarry Smith ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr); 2653efd51863SBarry Smith ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr); 2654efd51863SBarry Smith ierr = MatDestroy(&interp);CHKERRQ(ierr); 2655efd51863SBarry Smith x = xnew; 2656efd51863SBarry Smith 2657efd51863SBarry Smith ierr = SNESReset(snes);CHKERRQ(ierr); 2658efd51863SBarry Smith ierr = SNESSetDM(snes,fine);CHKERRQ(ierr); 2659efd51863SBarry Smith ierr = DMDestroy(&fine);CHKERRQ(ierr); 2660efd51863SBarry Smith } 2661efd51863SBarry Smith } 26623a40ed3dSBarry Smith PetscFunctionReturn(0); 26639b94acceSBarry Smith } 26649b94acceSBarry Smith 26659b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 26669b94acceSBarry Smith 26674a2ae208SSatish Balay #undef __FUNCT__ 26684a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 266982bf6240SBarry Smith /*@C 26704b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 26719b94acceSBarry Smith 2672fee21e36SBarry Smith Collective on SNES 2673fee21e36SBarry Smith 2674c7afd0dbSLois Curfman McInnes Input Parameters: 2675c7afd0dbSLois Curfman McInnes + snes - the SNES context 2676454a90a3SBarry Smith - type - a known method 2677c7afd0dbSLois Curfman McInnes 2678c7afd0dbSLois Curfman McInnes Options Database Key: 2679454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2680c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2681ae12b187SLois Curfman McInnes 26829b94acceSBarry Smith Notes: 2683e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 26844b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2685c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 26864b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2687c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 26889b94acceSBarry Smith 2689ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2690ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2691ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2692ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2693ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2694ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2695ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2696ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2697ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2698b0a32e0cSBarry Smith appropriate method. 269936851e7fSLois Curfman McInnes 270036851e7fSLois Curfman McInnes Level: intermediate 2701a703fe33SLois Curfman McInnes 2702454a90a3SBarry Smith .keywords: SNES, set, type 2703435da068SBarry Smith 2704435da068SBarry Smith .seealso: SNESType, SNESCreate() 2705435da068SBarry Smith 27069b94acceSBarry Smith @*/ 27077087cfbeSBarry Smith PetscErrorCode SNESSetType(SNES snes,const SNESType type) 27089b94acceSBarry Smith { 2709dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 2710ace3abfcSBarry Smith PetscBool match; 27113a40ed3dSBarry Smith 27123a40ed3dSBarry Smith PetscFunctionBegin; 27130700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 27144482741eSBarry Smith PetscValidCharPointer(type,2); 271582bf6240SBarry Smith 27166831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 27170f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 271892ff6ae8SBarry Smith 27194b91b6eaSBarry Smith ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 2720e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 272175396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 272275396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 272375396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 272475396ef9SLisandro Dalcin snes->ops->setup = 0; 272575396ef9SLisandro Dalcin snes->ops->solve = 0; 272675396ef9SLisandro Dalcin snes->ops->view = 0; 272775396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 272875396ef9SLisandro Dalcin snes->ops->destroy = 0; 272975396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 273075396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 2731454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 273203bfa161SLisandro Dalcin ierr = (*r)(snes);CHKERRQ(ierr); 27339fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS) 27349fb22e1aSBarry Smith if (PetscAMSPublishAll) { 27359fb22e1aSBarry Smith ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr); 27369fb22e1aSBarry Smith } 27379fb22e1aSBarry Smith #endif 27383a40ed3dSBarry Smith PetscFunctionReturn(0); 27399b94acceSBarry Smith } 27409b94acceSBarry Smith 2741a847f771SSatish Balay 27429b94acceSBarry Smith /* --------------------------------------------------------------------- */ 27434a2ae208SSatish Balay #undef __FUNCT__ 27444a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 274552baeb72SSatish Balay /*@ 27469b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2747f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 27489b94acceSBarry Smith 2749fee21e36SBarry Smith Not Collective 2750fee21e36SBarry Smith 275136851e7fSLois Curfman McInnes Level: advanced 275236851e7fSLois Curfman McInnes 27539b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 27549b94acceSBarry Smith 27559b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 27569b94acceSBarry Smith @*/ 27577087cfbeSBarry Smith PetscErrorCode SNESRegisterDestroy(void) 27589b94acceSBarry Smith { 2759dfbe8321SBarry Smith PetscErrorCode ierr; 276082bf6240SBarry Smith 27613a40ed3dSBarry Smith PetscFunctionBegin; 27621441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 27634c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 27643a40ed3dSBarry Smith PetscFunctionReturn(0); 27659b94acceSBarry Smith } 27669b94acceSBarry Smith 27674a2ae208SSatish Balay #undef __FUNCT__ 27684a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 27699b94acceSBarry Smith /*@C 27709a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 27719b94acceSBarry Smith 2772c7afd0dbSLois Curfman McInnes Not Collective 2773c7afd0dbSLois Curfman McInnes 27749b94acceSBarry Smith Input Parameter: 27754b0e389bSBarry Smith . snes - nonlinear solver context 27769b94acceSBarry Smith 27779b94acceSBarry Smith Output Parameter: 27783a7fca6bSBarry Smith . type - SNES method (a character string) 27799b94acceSBarry Smith 278036851e7fSLois Curfman McInnes Level: intermediate 278136851e7fSLois Curfman McInnes 2782454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 27839b94acceSBarry Smith @*/ 27847087cfbeSBarry Smith PetscErrorCode SNESGetType(SNES snes,const SNESType *type) 27859b94acceSBarry Smith { 27863a40ed3dSBarry Smith PetscFunctionBegin; 27870700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 27884482741eSBarry Smith PetscValidPointer(type,2); 27897adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 27903a40ed3dSBarry Smith PetscFunctionReturn(0); 27919b94acceSBarry Smith } 27929b94acceSBarry Smith 27934a2ae208SSatish Balay #undef __FUNCT__ 27944a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 279552baeb72SSatish Balay /*@ 27969b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 27979b94acceSBarry Smith stored. 27989b94acceSBarry Smith 2799c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2800c7afd0dbSLois Curfman McInnes 28019b94acceSBarry Smith Input Parameter: 28029b94acceSBarry Smith . snes - the SNES context 28039b94acceSBarry Smith 28049b94acceSBarry Smith Output Parameter: 28059b94acceSBarry Smith . x - the solution 28069b94acceSBarry Smith 280770e92668SMatthew Knepley Level: intermediate 280836851e7fSLois Curfman McInnes 28099b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 28109b94acceSBarry Smith 281185385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 28129b94acceSBarry Smith @*/ 28137087cfbeSBarry Smith PetscErrorCode SNESGetSolution(SNES snes,Vec *x) 28149b94acceSBarry Smith { 28153a40ed3dSBarry Smith PetscFunctionBegin; 28160700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28174482741eSBarry Smith PetscValidPointer(x,2); 281885385478SLisandro Dalcin *x = snes->vec_sol; 281970e92668SMatthew Knepley PetscFunctionReturn(0); 282070e92668SMatthew Knepley } 282170e92668SMatthew Knepley 282270e92668SMatthew Knepley #undef __FUNCT__ 28234a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 282452baeb72SSatish Balay /*@ 28259b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 28269b94acceSBarry Smith stored. 28279b94acceSBarry Smith 2828c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2829c7afd0dbSLois Curfman McInnes 28309b94acceSBarry Smith Input Parameter: 28319b94acceSBarry Smith . snes - the SNES context 28329b94acceSBarry Smith 28339b94acceSBarry Smith Output Parameter: 28349b94acceSBarry Smith . x - the solution update 28359b94acceSBarry Smith 283636851e7fSLois Curfman McInnes Level: advanced 283736851e7fSLois Curfman McInnes 28389b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 28399b94acceSBarry Smith 284085385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 28419b94acceSBarry Smith @*/ 28427087cfbeSBarry Smith PetscErrorCode SNESGetSolutionUpdate(SNES snes,Vec *x) 28439b94acceSBarry Smith { 28443a40ed3dSBarry Smith PetscFunctionBegin; 28450700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28464482741eSBarry Smith PetscValidPointer(x,2); 284785385478SLisandro Dalcin *x = snes->vec_sol_update; 28483a40ed3dSBarry Smith PetscFunctionReturn(0); 28499b94acceSBarry Smith } 28509b94acceSBarry Smith 28514a2ae208SSatish Balay #undef __FUNCT__ 28524a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 28539b94acceSBarry Smith /*@C 28543638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 28559b94acceSBarry Smith 2856c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2857c7afd0dbSLois Curfman McInnes 28589b94acceSBarry Smith Input Parameter: 28599b94acceSBarry Smith . snes - the SNES context 28609b94acceSBarry Smith 28619b94acceSBarry Smith Output Parameter: 28627bf4e008SBarry Smith + r - the function (or PETSC_NULL) 286370e92668SMatthew Knepley . func - the function (or PETSC_NULL) 286470e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 28659b94acceSBarry Smith 286636851e7fSLois Curfman McInnes Level: advanced 286736851e7fSLois Curfman McInnes 2868a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 28699b94acceSBarry Smith 28704b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 28719b94acceSBarry Smith @*/ 28727087cfbeSBarry Smith PetscErrorCode SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 28739b94acceSBarry Smith { 28743a40ed3dSBarry Smith PetscFunctionBegin; 28750700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 287685385478SLisandro Dalcin if (r) *r = snes->vec_func; 2877e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 287870e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 28793a40ed3dSBarry Smith PetscFunctionReturn(0); 28809b94acceSBarry Smith } 28819b94acceSBarry Smith 28824a2ae208SSatish Balay #undef __FUNCT__ 28834a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 28843c7409f5SSatish Balay /*@C 28853c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2886d850072dSLois Curfman McInnes SNES options in the database. 28873c7409f5SSatish Balay 28883f9fe445SBarry Smith Logically Collective on SNES 2889fee21e36SBarry Smith 2890c7afd0dbSLois Curfman McInnes Input Parameter: 2891c7afd0dbSLois Curfman McInnes + snes - the SNES context 2892c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2893c7afd0dbSLois Curfman McInnes 2894d850072dSLois Curfman McInnes Notes: 2895a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2896c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2897d850072dSLois Curfman McInnes 289836851e7fSLois Curfman McInnes Level: advanced 289936851e7fSLois Curfman McInnes 29003c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2901a86d99e1SLois Curfman McInnes 2902a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 29033c7409f5SSatish Balay @*/ 29047087cfbeSBarry Smith PetscErrorCode SNESSetOptionsPrefix(SNES snes,const char prefix[]) 29053c7409f5SSatish Balay { 2906dfbe8321SBarry Smith PetscErrorCode ierr; 29073c7409f5SSatish Balay 29083a40ed3dSBarry Smith PetscFunctionBegin; 29090700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2910639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 29111cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 291294b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 29133a40ed3dSBarry Smith PetscFunctionReturn(0); 29143c7409f5SSatish Balay } 29153c7409f5SSatish Balay 29164a2ae208SSatish Balay #undef __FUNCT__ 29174a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 29183c7409f5SSatish Balay /*@C 2919f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2920d850072dSLois Curfman McInnes SNES options in the database. 29213c7409f5SSatish Balay 29223f9fe445SBarry Smith Logically Collective on SNES 2923fee21e36SBarry Smith 2924c7afd0dbSLois Curfman McInnes Input Parameters: 2925c7afd0dbSLois Curfman McInnes + snes - the SNES context 2926c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2927c7afd0dbSLois Curfman McInnes 2928d850072dSLois Curfman McInnes Notes: 2929a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2930c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2931d850072dSLois Curfman McInnes 293236851e7fSLois Curfman McInnes Level: advanced 293336851e7fSLois Curfman McInnes 29343c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2935a86d99e1SLois Curfman McInnes 2936a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 29373c7409f5SSatish Balay @*/ 29387087cfbeSBarry Smith PetscErrorCode SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 29393c7409f5SSatish Balay { 2940dfbe8321SBarry Smith PetscErrorCode ierr; 29413c7409f5SSatish Balay 29423a40ed3dSBarry Smith PetscFunctionBegin; 29430700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2944639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 29451cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 294694b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 29473a40ed3dSBarry Smith PetscFunctionReturn(0); 29483c7409f5SSatish Balay } 29493c7409f5SSatish Balay 29504a2ae208SSatish Balay #undef __FUNCT__ 29514a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 29529ab63eb5SSatish Balay /*@C 29533c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 29543c7409f5SSatish Balay SNES options in the database. 29553c7409f5SSatish Balay 2956c7afd0dbSLois Curfman McInnes Not Collective 2957c7afd0dbSLois Curfman McInnes 29583c7409f5SSatish Balay Input Parameter: 29593c7409f5SSatish Balay . snes - the SNES context 29603c7409f5SSatish Balay 29613c7409f5SSatish Balay Output Parameter: 29623c7409f5SSatish Balay . prefix - pointer to the prefix string used 29633c7409f5SSatish Balay 29644ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 29659ab63eb5SSatish Balay sufficient length to hold the prefix. 29669ab63eb5SSatish Balay 296736851e7fSLois Curfman McInnes Level: advanced 296836851e7fSLois Curfman McInnes 29693c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2970a86d99e1SLois Curfman McInnes 2971a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 29723c7409f5SSatish Balay @*/ 29737087cfbeSBarry Smith PetscErrorCode SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 29743c7409f5SSatish Balay { 2975dfbe8321SBarry Smith PetscErrorCode ierr; 29763c7409f5SSatish Balay 29773a40ed3dSBarry Smith PetscFunctionBegin; 29780700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2979639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 29803a40ed3dSBarry Smith PetscFunctionReturn(0); 29813c7409f5SSatish Balay } 29823c7409f5SSatish Balay 2983b2002411SLois Curfman McInnes 29844a2ae208SSatish Balay #undef __FUNCT__ 29854a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 29863cea93caSBarry Smith /*@C 29873cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 29883cea93caSBarry Smith 29897f6c08e0SMatthew Knepley Level: advanced 29903cea93caSBarry Smith @*/ 29917087cfbeSBarry Smith PetscErrorCode SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2992b2002411SLois Curfman McInnes { 2993e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2994dfbe8321SBarry Smith PetscErrorCode ierr; 2995b2002411SLois Curfman McInnes 2996b2002411SLois Curfman McInnes PetscFunctionBegin; 2997b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2998c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2999b2002411SLois Curfman McInnes PetscFunctionReturn(0); 3000b2002411SLois Curfman McInnes } 3001da9b6338SBarry Smith 3002da9b6338SBarry Smith #undef __FUNCT__ 3003da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 30047087cfbeSBarry Smith PetscErrorCode SNESTestLocalMin(SNES snes) 3005da9b6338SBarry Smith { 3006dfbe8321SBarry Smith PetscErrorCode ierr; 300777431f27SBarry Smith PetscInt N,i,j; 3008da9b6338SBarry Smith Vec u,uh,fh; 3009da9b6338SBarry Smith PetscScalar value; 3010da9b6338SBarry Smith PetscReal norm; 3011da9b6338SBarry Smith 3012da9b6338SBarry Smith PetscFunctionBegin; 3013da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 3014da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 3015da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 3016da9b6338SBarry Smith 3017da9b6338SBarry Smith /* currently only works for sequential */ 3018da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 3019da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 3020da9b6338SBarry Smith for (i=0; i<N; i++) { 3021da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 302277431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 3023da9b6338SBarry Smith for (j=-10; j<11; j++) { 3024ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 3025da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 30263ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 3027da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 302877431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 3029da9b6338SBarry Smith value = -value; 3030da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 3031da9b6338SBarry Smith } 3032da9b6338SBarry Smith } 30336bf464f9SBarry Smith ierr = VecDestroy(&uh);CHKERRQ(ierr); 30346bf464f9SBarry Smith ierr = VecDestroy(&fh);CHKERRQ(ierr); 3035da9b6338SBarry Smith PetscFunctionReturn(0); 3036da9b6338SBarry Smith } 303771f87433Sdalcinl 303871f87433Sdalcinl #undef __FUNCT__ 3039fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 304071f87433Sdalcinl /*@ 3041fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 304271f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 304371f87433Sdalcinl Newton method. 304471f87433Sdalcinl 30453f9fe445SBarry Smith Logically Collective on SNES 304671f87433Sdalcinl 304771f87433Sdalcinl Input Parameters: 304871f87433Sdalcinl + snes - SNES context 304971f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 305071f87433Sdalcinl 305164ba62caSBarry Smith Options Database: 305264ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 305364ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 305464ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 305564ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 305664ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 305764ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 305864ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 305964ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 306064ba62caSBarry Smith 306171f87433Sdalcinl Notes: 306271f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 306371f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 306471f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 306571f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 306671f87433Sdalcinl solver. 306771f87433Sdalcinl 306871f87433Sdalcinl Level: advanced 306971f87433Sdalcinl 307071f87433Sdalcinl Reference: 307171f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 307271f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 307371f87433Sdalcinl 307471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 307571f87433Sdalcinl 3076fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 307771f87433Sdalcinl @*/ 30787087cfbeSBarry Smith PetscErrorCode SNESKSPSetUseEW(SNES snes,PetscBool flag) 307971f87433Sdalcinl { 308071f87433Sdalcinl PetscFunctionBegin; 30810700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3082acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flag,2); 308371f87433Sdalcinl snes->ksp_ewconv = flag; 308471f87433Sdalcinl PetscFunctionReturn(0); 308571f87433Sdalcinl } 308671f87433Sdalcinl 308771f87433Sdalcinl #undef __FUNCT__ 3088fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 308971f87433Sdalcinl /*@ 3090fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 309171f87433Sdalcinl for computing relative tolerance for linear solvers within an 309271f87433Sdalcinl inexact Newton method. 309371f87433Sdalcinl 309471f87433Sdalcinl Not Collective 309571f87433Sdalcinl 309671f87433Sdalcinl Input Parameter: 309771f87433Sdalcinl . snes - SNES context 309871f87433Sdalcinl 309971f87433Sdalcinl Output Parameter: 310071f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 310171f87433Sdalcinl 310271f87433Sdalcinl Notes: 310371f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 310471f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 310571f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 310671f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 310771f87433Sdalcinl solver. 310871f87433Sdalcinl 310971f87433Sdalcinl Level: advanced 311071f87433Sdalcinl 311171f87433Sdalcinl Reference: 311271f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 311371f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 311471f87433Sdalcinl 311571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 311671f87433Sdalcinl 3117fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 311871f87433Sdalcinl @*/ 31197087cfbeSBarry Smith PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 312071f87433Sdalcinl { 312171f87433Sdalcinl PetscFunctionBegin; 31220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 312371f87433Sdalcinl PetscValidPointer(flag,2); 312471f87433Sdalcinl *flag = snes->ksp_ewconv; 312571f87433Sdalcinl PetscFunctionReturn(0); 312671f87433Sdalcinl } 312771f87433Sdalcinl 312871f87433Sdalcinl #undef __FUNCT__ 3129fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 313071f87433Sdalcinl /*@ 3131fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 313271f87433Sdalcinl convergence criteria for the linear solvers within an inexact 313371f87433Sdalcinl Newton method. 313471f87433Sdalcinl 31353f9fe445SBarry Smith Logically Collective on SNES 313671f87433Sdalcinl 313771f87433Sdalcinl Input Parameters: 313871f87433Sdalcinl + snes - SNES context 313971f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 314071f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 314171f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 314271f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 314371f87433Sdalcinl (0 <= gamma2 <= 1) 314471f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 314571f87433Sdalcinl . alpha2 - power for safeguard 314671f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 314771f87433Sdalcinl 314871f87433Sdalcinl Note: 314971f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 315071f87433Sdalcinl 315171f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 315271f87433Sdalcinl 315371f87433Sdalcinl Level: advanced 315471f87433Sdalcinl 315571f87433Sdalcinl Reference: 315671f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 315771f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 315871f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 315971f87433Sdalcinl 316071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 316171f87433Sdalcinl 3162fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 316371f87433Sdalcinl @*/ 31647087cfbeSBarry Smith PetscErrorCode SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 316571f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 316671f87433Sdalcinl { 3167fa9f3622SBarry Smith SNESKSPEW *kctx; 316871f87433Sdalcinl PetscFunctionBegin; 31690700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3170fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3171e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 3172c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,version,2); 3173c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_0,3); 3174c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_max,4); 3175c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,gamma,5); 3176c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha,6); 3177c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha2,7); 3178c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,threshold,8); 317971f87433Sdalcinl 318071f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 318171f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 318271f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 318371f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 318471f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 318571f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 318671f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 318771f87433Sdalcinl 318871f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 3189e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 319071f87433Sdalcinl } 319171f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 3192e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 319371f87433Sdalcinl } 319471f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 3195e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 319671f87433Sdalcinl } 319771f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 3198e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 319971f87433Sdalcinl } 320071f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 3201e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 320271f87433Sdalcinl } 320371f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 3204e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 320571f87433Sdalcinl } 320671f87433Sdalcinl PetscFunctionReturn(0); 320771f87433Sdalcinl } 320871f87433Sdalcinl 320971f87433Sdalcinl #undef __FUNCT__ 3210fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 321171f87433Sdalcinl /*@ 3212fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 321371f87433Sdalcinl convergence criteria for the linear solvers within an inexact 321471f87433Sdalcinl Newton method. 321571f87433Sdalcinl 321671f87433Sdalcinl Not Collective 321771f87433Sdalcinl 321871f87433Sdalcinl Input Parameters: 321971f87433Sdalcinl snes - SNES context 322071f87433Sdalcinl 322171f87433Sdalcinl Output Parameters: 322271f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 322371f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 322471f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 322571f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 322671f87433Sdalcinl (0 <= gamma2 <= 1) 322771f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 322871f87433Sdalcinl . alpha2 - power for safeguard 322971f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 323071f87433Sdalcinl 323171f87433Sdalcinl Level: advanced 323271f87433Sdalcinl 323371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 323471f87433Sdalcinl 3235fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 323671f87433Sdalcinl @*/ 32377087cfbeSBarry Smith PetscErrorCode SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 323871f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 323971f87433Sdalcinl { 3240fa9f3622SBarry Smith SNESKSPEW *kctx; 324171f87433Sdalcinl PetscFunctionBegin; 32420700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3243fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3244e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 324571f87433Sdalcinl if(version) *version = kctx->version; 324671f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 324771f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 324871f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 324971f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 325071f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 325171f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 325271f87433Sdalcinl PetscFunctionReturn(0); 325371f87433Sdalcinl } 325471f87433Sdalcinl 325571f87433Sdalcinl #undef __FUNCT__ 3256fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 3257fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 325871f87433Sdalcinl { 325971f87433Sdalcinl PetscErrorCode ierr; 3260fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 326171f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 326271f87433Sdalcinl 326371f87433Sdalcinl PetscFunctionBegin; 3264e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 326571f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 326671f87433Sdalcinl rtol = kctx->rtol_0; 326771f87433Sdalcinl } else { 326871f87433Sdalcinl if (kctx->version == 1) { 326971f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 327071f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 327171f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 327271f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 327371f87433Sdalcinl } else if (kctx->version == 2) { 327471f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 327571f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 327671f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 327771f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 327871f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 327971f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 328071f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 328171f87433Sdalcinl stol = PetscMax(rtol,stol); 328271f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 328371f87433Sdalcinl /* safeguard: avoid oversolving */ 328471f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 328571f87433Sdalcinl stol = PetscMax(rtol,stol); 328671f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 3287e32f2f54SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 328871f87433Sdalcinl } 328971f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 329071f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 329171f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 329271f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 329371f87433Sdalcinl PetscFunctionReturn(0); 329471f87433Sdalcinl } 329571f87433Sdalcinl 329671f87433Sdalcinl #undef __FUNCT__ 3297fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 3298fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 329971f87433Sdalcinl { 330071f87433Sdalcinl PetscErrorCode ierr; 3301fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 330271f87433Sdalcinl PCSide pcside; 330371f87433Sdalcinl Vec lres; 330471f87433Sdalcinl 330571f87433Sdalcinl PetscFunctionBegin; 3306e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 330771f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 330871f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 330971f87433Sdalcinl if (kctx->version == 1) { 3310b037da10SBarry Smith ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr); 331171f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 331271f87433Sdalcinl /* KSP residual is true linear residual */ 331371f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 331471f87433Sdalcinl } else { 331571f87433Sdalcinl /* KSP residual is preconditioned residual */ 331671f87433Sdalcinl /* compute true linear residual norm */ 331771f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 331871f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 331971f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 332071f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 33216bf464f9SBarry Smith ierr = VecDestroy(&lres);CHKERRQ(ierr); 332271f87433Sdalcinl } 332371f87433Sdalcinl } 332471f87433Sdalcinl PetscFunctionReturn(0); 332571f87433Sdalcinl } 332671f87433Sdalcinl 332771f87433Sdalcinl #undef __FUNCT__ 332871f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 332971f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 333071f87433Sdalcinl { 333171f87433Sdalcinl PetscErrorCode ierr; 333271f87433Sdalcinl 333371f87433Sdalcinl PetscFunctionBegin; 3334fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 333571f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 3336fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 333771f87433Sdalcinl PetscFunctionReturn(0); 333871f87433Sdalcinl } 33396c699258SBarry Smith 33406c699258SBarry Smith #undef __FUNCT__ 33416c699258SBarry Smith #define __FUNCT__ "SNESSetDM" 33426c699258SBarry Smith /*@ 33436c699258SBarry Smith SNESSetDM - Sets the DM that may be used by some preconditioners 33446c699258SBarry Smith 33453f9fe445SBarry Smith Logically Collective on SNES 33466c699258SBarry Smith 33476c699258SBarry Smith Input Parameters: 33486c699258SBarry Smith + snes - the preconditioner context 33496c699258SBarry Smith - dm - the dm 33506c699258SBarry Smith 33516c699258SBarry Smith Level: intermediate 33526c699258SBarry Smith 33536c699258SBarry Smith 33546c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM() 33556c699258SBarry Smith @*/ 33567087cfbeSBarry Smith PetscErrorCode SNESSetDM(SNES snes,DM dm) 33576c699258SBarry Smith { 33586c699258SBarry Smith PetscErrorCode ierr; 3359345fed2cSBarry Smith KSP ksp; 33606c699258SBarry Smith 33616c699258SBarry Smith PetscFunctionBegin; 33620700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3363d0660788SBarry Smith if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);} 33646bf464f9SBarry Smith ierr = DMDestroy(&snes->dm);CHKERRQ(ierr); 33656c699258SBarry Smith snes->dm = dm; 3366345fed2cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 3367345fed2cSBarry Smith ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr); 3368f22f69f0SBarry Smith ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr); 33696c699258SBarry Smith PetscFunctionReturn(0); 33706c699258SBarry Smith } 33716c699258SBarry Smith 33726c699258SBarry Smith #undef __FUNCT__ 33736c699258SBarry Smith #define __FUNCT__ "SNESGetDM" 33746c699258SBarry Smith /*@ 33756c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 33766c699258SBarry Smith 33773f9fe445SBarry Smith Not Collective but DM obtained is parallel on SNES 33786c699258SBarry Smith 33796c699258SBarry Smith Input Parameter: 33806c699258SBarry Smith . snes - the preconditioner context 33816c699258SBarry Smith 33826c699258SBarry Smith Output Parameter: 33836c699258SBarry Smith . dm - the dm 33846c699258SBarry Smith 33856c699258SBarry Smith Level: intermediate 33866c699258SBarry Smith 33876c699258SBarry Smith 33886c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM() 33896c699258SBarry Smith @*/ 33907087cfbeSBarry Smith PetscErrorCode SNESGetDM(SNES snes,DM *dm) 33916c699258SBarry Smith { 33926c699258SBarry Smith PetscFunctionBegin; 33930700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 33946c699258SBarry Smith *dm = snes->dm; 33956c699258SBarry Smith PetscFunctionReturn(0); 33966c699258SBarry Smith } 33970807856dSBarry Smith 339869b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3399c6db04a5SJed Brown #include <mex.h> 340069b4f73cSBarry Smith 34018f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 34028f6e6473SBarry Smith 34030807856dSBarry Smith #undef __FUNCT__ 34040807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab" 34050807856dSBarry Smith /* 34060807856dSBarry Smith SNESComputeFunction_Matlab - Calls the function that has been set with 34070807856dSBarry Smith SNESSetFunctionMatlab(). 34080807856dSBarry Smith 34090807856dSBarry Smith Collective on SNES 34100807856dSBarry Smith 34110807856dSBarry Smith Input Parameters: 34120807856dSBarry Smith + snes - the SNES context 34130807856dSBarry Smith - x - input vector 34140807856dSBarry Smith 34150807856dSBarry Smith Output Parameter: 34160807856dSBarry Smith . y - function vector, as set by SNESSetFunction() 34170807856dSBarry Smith 34180807856dSBarry Smith Notes: 34190807856dSBarry Smith SNESComputeFunction() is typically used within nonlinear solvers 34200807856dSBarry Smith implementations, so most users would not generally call this routine 34210807856dSBarry Smith themselves. 34220807856dSBarry Smith 34230807856dSBarry Smith Level: developer 34240807856dSBarry Smith 34250807856dSBarry Smith .keywords: SNES, nonlinear, compute, function 34260807856dSBarry Smith 34270807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 342861b2408cSBarry Smith */ 34297087cfbeSBarry Smith PetscErrorCode SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx) 34300807856dSBarry Smith { 3431e650e774SBarry Smith PetscErrorCode ierr; 34328f6e6473SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 34338f6e6473SBarry Smith int nlhs = 1,nrhs = 5; 34348f6e6473SBarry Smith mxArray *plhs[1],*prhs[5]; 343591621f2eSBarry Smith long long int lx = 0,ly = 0,ls = 0; 3436e650e774SBarry Smith 34370807856dSBarry Smith PetscFunctionBegin; 34380807856dSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 34390807856dSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 34400807856dSBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 34410807856dSBarry Smith PetscCheckSameComm(snes,1,x,2); 34420807856dSBarry Smith PetscCheckSameComm(snes,1,y,3); 34430807856dSBarry Smith 34440807856dSBarry Smith /* call Matlab function in ctx with arguments x and y */ 3445e650e774SBarry Smith 344691621f2eSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3447e650e774SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3448e650e774SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 344991621f2eSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 345091621f2eSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 345191621f2eSBarry Smith prhs[2] = mxCreateDoubleScalar((double)ly); 34528f6e6473SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 34538f6e6473SBarry Smith prhs[4] = sctx->ctx; 3454b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr); 3455e650e774SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3456e650e774SBarry Smith mxDestroyArray(prhs[0]); 3457e650e774SBarry Smith mxDestroyArray(prhs[1]); 3458e650e774SBarry Smith mxDestroyArray(prhs[2]); 34598f6e6473SBarry Smith mxDestroyArray(prhs[3]); 3460e650e774SBarry Smith mxDestroyArray(plhs[0]); 34610807856dSBarry Smith PetscFunctionReturn(0); 34620807856dSBarry Smith } 34630807856dSBarry Smith 34640807856dSBarry Smith 34650807856dSBarry Smith #undef __FUNCT__ 34660807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab" 346761b2408cSBarry Smith /* 34680807856dSBarry Smith SNESSetFunctionMatlab - Sets the function evaluation routine and function 34690807856dSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3470e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 34710807856dSBarry Smith 34720807856dSBarry Smith Logically Collective on SNES 34730807856dSBarry Smith 34740807856dSBarry Smith Input Parameters: 34750807856dSBarry Smith + snes - the SNES context 34760807856dSBarry Smith . r - vector to store function value 34770807856dSBarry Smith - func - function evaluation routine 34780807856dSBarry Smith 34790807856dSBarry Smith Calling sequence of func: 348061b2408cSBarry Smith $ func (SNES snes,Vec x,Vec f,void *ctx); 34810807856dSBarry Smith 34820807856dSBarry Smith 34830807856dSBarry Smith Notes: 34840807856dSBarry Smith The Newton-like methods typically solve linear systems of the form 34850807856dSBarry Smith $ f'(x) x = -f(x), 34860807856dSBarry Smith where f'(x) denotes the Jacobian matrix and f(x) is the function. 34870807856dSBarry Smith 34880807856dSBarry Smith Level: beginner 34890807856dSBarry Smith 34900807856dSBarry Smith .keywords: SNES, nonlinear, set, function 34910807856dSBarry Smith 34920807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 349361b2408cSBarry Smith */ 34947087cfbeSBarry Smith PetscErrorCode SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx) 34950807856dSBarry Smith { 34960807856dSBarry Smith PetscErrorCode ierr; 34978f6e6473SBarry Smith SNESMatlabContext *sctx; 34980807856dSBarry Smith 34990807856dSBarry Smith PetscFunctionBegin; 35008f6e6473SBarry Smith /* currently sctx is memory bleed */ 35018f6e6473SBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 35028f6e6473SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 35038f6e6473SBarry Smith /* 35048f6e6473SBarry Smith This should work, but it doesn't 35058f6e6473SBarry Smith sctx->ctx = ctx; 35068f6e6473SBarry Smith mexMakeArrayPersistent(sctx->ctx); 35078f6e6473SBarry Smith */ 35088f6e6473SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 35098f6e6473SBarry Smith ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr); 35100807856dSBarry Smith PetscFunctionReturn(0); 35110807856dSBarry Smith } 351269b4f73cSBarry Smith 351361b2408cSBarry Smith #undef __FUNCT__ 351461b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab" 351561b2408cSBarry Smith /* 351661b2408cSBarry Smith SNESComputeJacobian_Matlab - Calls the function that has been set with 351761b2408cSBarry Smith SNESSetJacobianMatlab(). 351861b2408cSBarry Smith 351961b2408cSBarry Smith Collective on SNES 352061b2408cSBarry Smith 352161b2408cSBarry Smith Input Parameters: 352261b2408cSBarry Smith + snes - the SNES context 352361b2408cSBarry Smith . x - input vector 352461b2408cSBarry Smith . A, B - the matrices 352561b2408cSBarry Smith - ctx - user context 352661b2408cSBarry Smith 352761b2408cSBarry Smith Output Parameter: 352861b2408cSBarry Smith . flag - structure of the matrix 352961b2408cSBarry Smith 353061b2408cSBarry Smith Level: developer 353161b2408cSBarry Smith 353261b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function 353361b2408cSBarry Smith 353461b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 353561b2408cSBarry Smith @*/ 35367087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx) 353761b2408cSBarry Smith { 353861b2408cSBarry Smith PetscErrorCode ierr; 353961b2408cSBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 354061b2408cSBarry Smith int nlhs = 2,nrhs = 6; 354161b2408cSBarry Smith mxArray *plhs[2],*prhs[6]; 354261b2408cSBarry Smith long long int lx = 0,lA = 0,ls = 0, lB = 0; 354361b2408cSBarry Smith 354461b2408cSBarry Smith PetscFunctionBegin; 354561b2408cSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 354661b2408cSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 354761b2408cSBarry Smith 354861b2408cSBarry Smith /* call Matlab function in ctx with arguments x and y */ 354961b2408cSBarry Smith 355061b2408cSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 355161b2408cSBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 355261b2408cSBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 355361b2408cSBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 355461b2408cSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 355561b2408cSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 355661b2408cSBarry Smith prhs[2] = mxCreateDoubleScalar((double)lA); 355761b2408cSBarry Smith prhs[3] = mxCreateDoubleScalar((double)lB); 355861b2408cSBarry Smith prhs[4] = mxCreateString(sctx->funcname); 355961b2408cSBarry Smith prhs[5] = sctx->ctx; 3560b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr); 356161b2408cSBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 356261b2408cSBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 356361b2408cSBarry Smith mxDestroyArray(prhs[0]); 356461b2408cSBarry Smith mxDestroyArray(prhs[1]); 356561b2408cSBarry Smith mxDestroyArray(prhs[2]); 356661b2408cSBarry Smith mxDestroyArray(prhs[3]); 356761b2408cSBarry Smith mxDestroyArray(prhs[4]); 356861b2408cSBarry Smith mxDestroyArray(plhs[0]); 356961b2408cSBarry Smith mxDestroyArray(plhs[1]); 357061b2408cSBarry Smith PetscFunctionReturn(0); 357161b2408cSBarry Smith } 357261b2408cSBarry Smith 357361b2408cSBarry Smith 357461b2408cSBarry Smith #undef __FUNCT__ 357561b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab" 357661b2408cSBarry Smith /* 357761b2408cSBarry Smith SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 357861b2408cSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3579e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 358061b2408cSBarry Smith 358161b2408cSBarry Smith Logically Collective on SNES 358261b2408cSBarry Smith 358361b2408cSBarry Smith Input Parameters: 358461b2408cSBarry Smith + snes - the SNES context 358561b2408cSBarry Smith . A,B - Jacobian matrices 358661b2408cSBarry Smith . func - function evaluation routine 358761b2408cSBarry Smith - ctx - user context 358861b2408cSBarry Smith 358961b2408cSBarry Smith Calling sequence of func: 359061b2408cSBarry Smith $ flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx); 359161b2408cSBarry Smith 359261b2408cSBarry Smith 359361b2408cSBarry Smith Level: developer 359461b2408cSBarry Smith 359561b2408cSBarry Smith .keywords: SNES, nonlinear, set, function 359661b2408cSBarry Smith 359761b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 359861b2408cSBarry Smith */ 35997087cfbeSBarry Smith PetscErrorCode SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx) 360061b2408cSBarry Smith { 360161b2408cSBarry Smith PetscErrorCode ierr; 360261b2408cSBarry Smith SNESMatlabContext *sctx; 360361b2408cSBarry Smith 360461b2408cSBarry Smith PetscFunctionBegin; 360561b2408cSBarry Smith /* currently sctx is memory bleed */ 360661b2408cSBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 360761b2408cSBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 360861b2408cSBarry Smith /* 360961b2408cSBarry Smith This should work, but it doesn't 361061b2408cSBarry Smith sctx->ctx = ctx; 361161b2408cSBarry Smith mexMakeArrayPersistent(sctx->ctx); 361261b2408cSBarry Smith */ 361361b2408cSBarry Smith sctx->ctx = mxDuplicateArray(ctx); 361461b2408cSBarry Smith ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 361561b2408cSBarry Smith PetscFunctionReturn(0); 361661b2408cSBarry Smith } 361769b4f73cSBarry Smith 3618f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3619f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab" 3620f9eb7ae2SShri Abhyankar /* 3621f9eb7ae2SShri Abhyankar SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab(). 3622f9eb7ae2SShri Abhyankar 3623f9eb7ae2SShri Abhyankar Collective on SNES 3624f9eb7ae2SShri Abhyankar 3625f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction() 3626f9eb7ae2SShri Abhyankar @*/ 36277087cfbeSBarry Smith PetscErrorCode SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx) 3628f9eb7ae2SShri Abhyankar { 3629f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 363048f37e70SShri Abhyankar SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 3631f9eb7ae2SShri Abhyankar int nlhs = 1,nrhs = 6; 3632f9eb7ae2SShri Abhyankar mxArray *plhs[1],*prhs[6]; 3633f9eb7ae2SShri Abhyankar long long int lx = 0,ls = 0; 3634f9eb7ae2SShri Abhyankar Vec x=snes->vec_sol; 3635f9eb7ae2SShri Abhyankar 3636f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3637f9eb7ae2SShri Abhyankar PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3638f9eb7ae2SShri Abhyankar 3639f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3640f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3641f9eb7ae2SShri Abhyankar prhs[0] = mxCreateDoubleScalar((double)ls); 3642f9eb7ae2SShri Abhyankar prhs[1] = mxCreateDoubleScalar((double)it); 3643f9eb7ae2SShri Abhyankar prhs[2] = mxCreateDoubleScalar((double)fnorm); 3644f9eb7ae2SShri Abhyankar prhs[3] = mxCreateDoubleScalar((double)lx); 3645f9eb7ae2SShri Abhyankar prhs[4] = mxCreateString(sctx->funcname); 3646f9eb7ae2SShri Abhyankar prhs[5] = sctx->ctx; 3647f9eb7ae2SShri Abhyankar ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr); 3648f9eb7ae2SShri Abhyankar ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3649f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[0]); 3650f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[1]); 3651f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[2]); 3652f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[3]); 3653f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[4]); 3654f9eb7ae2SShri Abhyankar mxDestroyArray(plhs[0]); 3655f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 3656f9eb7ae2SShri Abhyankar } 3657f9eb7ae2SShri Abhyankar 3658f9eb7ae2SShri Abhyankar 3659f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3660f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab" 3661f9eb7ae2SShri Abhyankar /* 3662e3c5b3baSBarry Smith SNESMonitorSetMatlab - Sets the monitor function from MATLAB 3663f9eb7ae2SShri Abhyankar 3664f9eb7ae2SShri Abhyankar Level: developer 3665f9eb7ae2SShri Abhyankar 3666f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function 3667f9eb7ae2SShri Abhyankar 3668f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 3669f9eb7ae2SShri Abhyankar */ 36707087cfbeSBarry Smith PetscErrorCode SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx) 3671f9eb7ae2SShri Abhyankar { 3672f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 3673f9eb7ae2SShri Abhyankar SNESMatlabContext *sctx; 3674f9eb7ae2SShri Abhyankar 3675f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3676f9eb7ae2SShri Abhyankar /* currently sctx is memory bleed */ 3677f9eb7ae2SShri Abhyankar ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 3678f9eb7ae2SShri Abhyankar ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3679f9eb7ae2SShri Abhyankar /* 3680f9eb7ae2SShri Abhyankar This should work, but it doesn't 3681f9eb7ae2SShri Abhyankar sctx->ctx = ctx; 3682f9eb7ae2SShri Abhyankar mexMakeArrayPersistent(sctx->ctx); 3683f9eb7ae2SShri Abhyankar */ 3684f9eb7ae2SShri Abhyankar sctx->ctx = mxDuplicateArray(ctx); 3685f9eb7ae2SShri Abhyankar ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 3686f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 3687f9eb7ae2SShri Abhyankar } 3688f9eb7ae2SShri Abhyankar 368969b4f73cSBarry Smith #endif 3690