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" 5188af28204SBarry Smith /*@C 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; 5371b2093e4SBarry Smith 5383a40ed3dSBarry Smith PetscFunctionBegin; 5390700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5401b2093e4SBarry Smith ierr = KSPSetApplicationContext(snes->ksp,usrP);CHKERRQ(ierr); 5419b94acceSBarry Smith snes->user = usrP; 5423a40ed3dSBarry Smith PetscFunctionReturn(0); 5439b94acceSBarry Smith } 54474679c65SBarry Smith 5454a2ae208SSatish Balay #undef __FUNCT__ 5464a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 5479b94acceSBarry Smith /*@C 5489b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 5499b94acceSBarry Smith nonlinear solvers. 5509b94acceSBarry Smith 551c7afd0dbSLois Curfman McInnes Not Collective 552c7afd0dbSLois Curfman McInnes 5539b94acceSBarry Smith Input Parameter: 5549b94acceSBarry Smith . snes - SNES context 5559b94acceSBarry Smith 5569b94acceSBarry Smith Output Parameter: 5579b94acceSBarry Smith . usrP - user context 5589b94acceSBarry Smith 55936851e7fSLois Curfman McInnes Level: intermediate 56036851e7fSLois Curfman McInnes 5619b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 5629b94acceSBarry Smith 5639b94acceSBarry Smith .seealso: SNESSetApplicationContext() 5649b94acceSBarry Smith @*/ 5657087cfbeSBarry Smith PetscErrorCode SNESGetApplicationContext(SNES snes,void **usrP) 5669b94acceSBarry Smith { 5673a40ed3dSBarry Smith PetscFunctionBegin; 5680700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5699b94acceSBarry Smith *usrP = snes->user; 5703a40ed3dSBarry Smith PetscFunctionReturn(0); 5719b94acceSBarry Smith } 57274679c65SBarry Smith 5734a2ae208SSatish Balay #undef __FUNCT__ 5744a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 5759b94acceSBarry Smith /*@ 576c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 577c8228a4eSBarry Smith at this time. 5789b94acceSBarry Smith 579c7afd0dbSLois Curfman McInnes Not Collective 580c7afd0dbSLois Curfman McInnes 5819b94acceSBarry Smith Input Parameter: 5829b94acceSBarry Smith . snes - SNES context 5839b94acceSBarry Smith 5849b94acceSBarry Smith Output Parameter: 5859b94acceSBarry Smith . iter - iteration number 5869b94acceSBarry Smith 587c8228a4eSBarry Smith Notes: 588c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 589c8228a4eSBarry Smith 590c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 59108405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 59208405cd6SLois Curfman McInnes .vb 59308405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 59408405cd6SLois Curfman McInnes if (!(it % 2)) { 59508405cd6SLois Curfman McInnes [compute Jacobian here] 59608405cd6SLois Curfman McInnes } 59708405cd6SLois Curfman McInnes .ve 598c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 59908405cd6SLois Curfman McInnes recomputed every second SNES iteration. 600c8228a4eSBarry Smith 60136851e7fSLois Curfman McInnes Level: intermediate 60236851e7fSLois Curfman McInnes 6032b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 6042b668275SBarry Smith 605b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 6069b94acceSBarry Smith @*/ 6077087cfbeSBarry Smith PetscErrorCode SNESGetIterationNumber(SNES snes,PetscInt* iter) 6089b94acceSBarry Smith { 6093a40ed3dSBarry Smith PetscFunctionBegin; 6100700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6114482741eSBarry Smith PetscValidIntPointer(iter,2); 6129b94acceSBarry Smith *iter = snes->iter; 6133a40ed3dSBarry Smith PetscFunctionReturn(0); 6149b94acceSBarry Smith } 61574679c65SBarry Smith 6164a2ae208SSatish Balay #undef __FUNCT__ 6174a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 6189b94acceSBarry Smith /*@ 6199b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 6209b94acceSBarry Smith with SNESSSetFunction(). 6219b94acceSBarry Smith 622c7afd0dbSLois Curfman McInnes Collective on SNES 623c7afd0dbSLois Curfman McInnes 6249b94acceSBarry Smith Input Parameter: 6259b94acceSBarry Smith . snes - SNES context 6269b94acceSBarry Smith 6279b94acceSBarry Smith Output Parameter: 6289b94acceSBarry Smith . fnorm - 2-norm of function 6299b94acceSBarry Smith 63036851e7fSLois Curfman McInnes Level: intermediate 63136851e7fSLois Curfman McInnes 6329b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 633a86d99e1SLois Curfman McInnes 634b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 6359b94acceSBarry Smith @*/ 6367087cfbeSBarry Smith PetscErrorCode SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 6379b94acceSBarry Smith { 6383a40ed3dSBarry Smith PetscFunctionBegin; 6390700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6404482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 6419b94acceSBarry Smith *fnorm = snes->norm; 6423a40ed3dSBarry Smith PetscFunctionReturn(0); 6439b94acceSBarry Smith } 64474679c65SBarry Smith 6454a2ae208SSatish Balay #undef __FUNCT__ 646b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 6479b94acceSBarry Smith /*@ 648b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 6499b94acceSBarry Smith attempted by the nonlinear solver. 6509b94acceSBarry Smith 651c7afd0dbSLois Curfman McInnes Not Collective 652c7afd0dbSLois Curfman McInnes 6539b94acceSBarry Smith Input Parameter: 6549b94acceSBarry Smith . snes - SNES context 6559b94acceSBarry Smith 6569b94acceSBarry Smith Output Parameter: 6579b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 6589b94acceSBarry Smith 659c96a6f78SLois Curfman McInnes Notes: 660c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 661c96a6f78SLois Curfman McInnes 66236851e7fSLois Curfman McInnes Level: intermediate 66336851e7fSLois Curfman McInnes 6649b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 66558ebbce7SBarry Smith 666e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 66758ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 6689b94acceSBarry Smith @*/ 6697087cfbeSBarry Smith PetscErrorCode SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 6709b94acceSBarry Smith { 6713a40ed3dSBarry Smith PetscFunctionBegin; 6720700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6734482741eSBarry Smith PetscValidIntPointer(nfails,2); 67450ffb88aSMatthew Knepley *nfails = snes->numFailures; 67550ffb88aSMatthew Knepley PetscFunctionReturn(0); 67650ffb88aSMatthew Knepley } 67750ffb88aSMatthew Knepley 67850ffb88aSMatthew Knepley #undef __FUNCT__ 679b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 68050ffb88aSMatthew Knepley /*@ 681b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 68250ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 68350ffb88aSMatthew Knepley 68450ffb88aSMatthew Knepley Not Collective 68550ffb88aSMatthew Knepley 68650ffb88aSMatthew Knepley Input Parameters: 68750ffb88aSMatthew Knepley + snes - SNES context 68850ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 68950ffb88aSMatthew Knepley 69050ffb88aSMatthew Knepley Level: intermediate 69150ffb88aSMatthew Knepley 69250ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 69358ebbce7SBarry Smith 694e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 69558ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 69650ffb88aSMatthew Knepley @*/ 6977087cfbeSBarry Smith PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 69850ffb88aSMatthew Knepley { 69950ffb88aSMatthew Knepley PetscFunctionBegin; 7000700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 70150ffb88aSMatthew Knepley snes->maxFailures = maxFails; 70250ffb88aSMatthew Knepley PetscFunctionReturn(0); 70350ffb88aSMatthew Knepley } 70450ffb88aSMatthew Knepley 70550ffb88aSMatthew Knepley #undef __FUNCT__ 706b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 70750ffb88aSMatthew Knepley /*@ 708b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 70950ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 71050ffb88aSMatthew Knepley 71150ffb88aSMatthew Knepley Not Collective 71250ffb88aSMatthew Knepley 71350ffb88aSMatthew Knepley Input Parameter: 71450ffb88aSMatthew Knepley . snes - SNES context 71550ffb88aSMatthew Knepley 71650ffb88aSMatthew Knepley Output Parameter: 71750ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 71850ffb88aSMatthew Knepley 71950ffb88aSMatthew Knepley Level: intermediate 72050ffb88aSMatthew Knepley 72150ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 72258ebbce7SBarry Smith 723e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 72458ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 72558ebbce7SBarry Smith 72650ffb88aSMatthew Knepley @*/ 7277087cfbeSBarry Smith PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 72850ffb88aSMatthew Knepley { 72950ffb88aSMatthew Knepley PetscFunctionBegin; 7300700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7314482741eSBarry Smith PetscValidIntPointer(maxFails,2); 73250ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 7333a40ed3dSBarry Smith PetscFunctionReturn(0); 7349b94acceSBarry Smith } 735a847f771SSatish Balay 7364a2ae208SSatish Balay #undef __FUNCT__ 7372541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 7382541af92SBarry Smith /*@ 7392541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 7402541af92SBarry Smith done by SNES. 7412541af92SBarry Smith 7422541af92SBarry Smith Not Collective 7432541af92SBarry Smith 7442541af92SBarry Smith Input Parameter: 7452541af92SBarry Smith . snes - SNES context 7462541af92SBarry Smith 7472541af92SBarry Smith Output Parameter: 7482541af92SBarry Smith . nfuncs - number of evaluations 7492541af92SBarry Smith 7502541af92SBarry Smith Level: intermediate 7512541af92SBarry Smith 7522541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 75358ebbce7SBarry Smith 754e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 7552541af92SBarry Smith @*/ 7567087cfbeSBarry Smith PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 7572541af92SBarry Smith { 7582541af92SBarry Smith PetscFunctionBegin; 7590700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7602541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 7612541af92SBarry Smith *nfuncs = snes->nfuncs; 7622541af92SBarry Smith PetscFunctionReturn(0); 7632541af92SBarry Smith } 7642541af92SBarry Smith 7652541af92SBarry Smith #undef __FUNCT__ 7663d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 7673d4c4710SBarry Smith /*@ 7683d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 7693d4c4710SBarry Smith linear solvers. 7703d4c4710SBarry Smith 7713d4c4710SBarry Smith Not Collective 7723d4c4710SBarry Smith 7733d4c4710SBarry Smith Input Parameter: 7743d4c4710SBarry Smith . snes - SNES context 7753d4c4710SBarry Smith 7763d4c4710SBarry Smith Output Parameter: 7773d4c4710SBarry Smith . nfails - number of failed solves 7783d4c4710SBarry Smith 7793d4c4710SBarry Smith Notes: 7803d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 7813d4c4710SBarry Smith 7823d4c4710SBarry Smith Level: intermediate 7833d4c4710SBarry Smith 7843d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 78558ebbce7SBarry Smith 786e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 7873d4c4710SBarry Smith @*/ 7887087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 7893d4c4710SBarry Smith { 7903d4c4710SBarry Smith PetscFunctionBegin; 7910700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7923d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 7933d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 7943d4c4710SBarry Smith PetscFunctionReturn(0); 7953d4c4710SBarry Smith } 7963d4c4710SBarry Smith 7973d4c4710SBarry Smith #undef __FUNCT__ 7983d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 7993d4c4710SBarry Smith /*@ 8003d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 8013d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 8023d4c4710SBarry Smith 8033f9fe445SBarry Smith Logically Collective on SNES 8043d4c4710SBarry Smith 8053d4c4710SBarry Smith Input Parameters: 8063d4c4710SBarry Smith + snes - SNES context 8073d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 8083d4c4710SBarry Smith 8093d4c4710SBarry Smith Level: intermediate 8103d4c4710SBarry Smith 811a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 8123d4c4710SBarry Smith 8133d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 8143d4c4710SBarry Smith 81558ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 8163d4c4710SBarry Smith @*/ 8177087cfbeSBarry Smith PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 8183d4c4710SBarry Smith { 8193d4c4710SBarry Smith PetscFunctionBegin; 8200700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 821c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxFails,2); 8223d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 8233d4c4710SBarry Smith PetscFunctionReturn(0); 8243d4c4710SBarry Smith } 8253d4c4710SBarry Smith 8263d4c4710SBarry Smith #undef __FUNCT__ 8273d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 8283d4c4710SBarry Smith /*@ 8293d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 8303d4c4710SBarry Smith are allowed before SNES terminates 8313d4c4710SBarry Smith 8323d4c4710SBarry Smith Not Collective 8333d4c4710SBarry Smith 8343d4c4710SBarry Smith Input Parameter: 8353d4c4710SBarry Smith . snes - SNES context 8363d4c4710SBarry Smith 8373d4c4710SBarry Smith Output Parameter: 8383d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 8393d4c4710SBarry Smith 8403d4c4710SBarry Smith Level: intermediate 8413d4c4710SBarry Smith 8423d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 8433d4c4710SBarry Smith 8443d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 8453d4c4710SBarry Smith 846e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 8473d4c4710SBarry Smith @*/ 8487087cfbeSBarry Smith PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 8493d4c4710SBarry Smith { 8503d4c4710SBarry Smith PetscFunctionBegin; 8510700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8523d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 8533d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 8543d4c4710SBarry Smith PetscFunctionReturn(0); 8553d4c4710SBarry Smith } 8563d4c4710SBarry Smith 8573d4c4710SBarry Smith #undef __FUNCT__ 858b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 859c96a6f78SLois Curfman McInnes /*@ 860b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 861c96a6f78SLois Curfman McInnes used by the nonlinear solver. 862c96a6f78SLois Curfman McInnes 863c7afd0dbSLois Curfman McInnes Not Collective 864c7afd0dbSLois Curfman McInnes 865c96a6f78SLois Curfman McInnes Input Parameter: 866c96a6f78SLois Curfman McInnes . snes - SNES context 867c96a6f78SLois Curfman McInnes 868c96a6f78SLois Curfman McInnes Output Parameter: 869c96a6f78SLois Curfman McInnes . lits - number of linear iterations 870c96a6f78SLois Curfman McInnes 871c96a6f78SLois Curfman McInnes Notes: 872c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 873c96a6f78SLois Curfman McInnes 87436851e7fSLois Curfman McInnes Level: intermediate 87536851e7fSLois Curfman McInnes 876c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 8772b668275SBarry Smith 8788c7482ecSBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 879c96a6f78SLois Curfman McInnes @*/ 8807087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 881c96a6f78SLois Curfman McInnes { 8823a40ed3dSBarry Smith PetscFunctionBegin; 8830700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8844482741eSBarry Smith PetscValidIntPointer(lits,2); 885c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 8863a40ed3dSBarry Smith PetscFunctionReturn(0); 887c96a6f78SLois Curfman McInnes } 888c96a6f78SLois Curfman McInnes 8894a2ae208SSatish Balay #undef __FUNCT__ 89094b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 89152baeb72SSatish Balay /*@ 89294b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 8939b94acceSBarry Smith 89494b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 895c7afd0dbSLois Curfman McInnes 8969b94acceSBarry Smith Input Parameter: 8979b94acceSBarry Smith . snes - the SNES context 8989b94acceSBarry Smith 8999b94acceSBarry Smith Output Parameter: 90094b7f48cSBarry Smith . ksp - the KSP context 9019b94acceSBarry Smith 9029b94acceSBarry Smith Notes: 90394b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 9049b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 9052999313aSBarry Smith PC contexts as well. 9069b94acceSBarry Smith 90736851e7fSLois Curfman McInnes Level: beginner 90836851e7fSLois Curfman McInnes 90994b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 9109b94acceSBarry Smith 9112999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 9129b94acceSBarry Smith @*/ 9137087cfbeSBarry Smith PetscErrorCode SNESGetKSP(SNES snes,KSP *ksp) 9149b94acceSBarry Smith { 9151cee3971SBarry Smith PetscErrorCode ierr; 9161cee3971SBarry Smith 9173a40ed3dSBarry Smith PetscFunctionBegin; 9180700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9194482741eSBarry Smith PetscValidPointer(ksp,2); 9201cee3971SBarry Smith 9211cee3971SBarry Smith if (!snes->ksp) { 9221cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 9231cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 9241cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 9251cee3971SBarry Smith } 92694b7f48cSBarry Smith *ksp = snes->ksp; 9273a40ed3dSBarry Smith PetscFunctionReturn(0); 9289b94acceSBarry Smith } 92982bf6240SBarry Smith 9304a2ae208SSatish Balay #undef __FUNCT__ 9312999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 9322999313aSBarry Smith /*@ 9332999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 9342999313aSBarry Smith 9352999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 9362999313aSBarry Smith 9372999313aSBarry Smith Input Parameters: 9382999313aSBarry Smith + snes - the SNES context 9392999313aSBarry Smith - ksp - the KSP context 9402999313aSBarry Smith 9412999313aSBarry Smith Notes: 9422999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 9432999313aSBarry Smith so this routine is rarely needed. 9442999313aSBarry Smith 9452999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 9462999313aSBarry Smith decreased by one. 9472999313aSBarry Smith 9482999313aSBarry Smith Level: developer 9492999313aSBarry Smith 9502999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 9512999313aSBarry Smith 9522999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 9532999313aSBarry Smith @*/ 9547087cfbeSBarry Smith PetscErrorCode SNESSetKSP(SNES snes,KSP ksp) 9552999313aSBarry Smith { 9562999313aSBarry Smith PetscErrorCode ierr; 9572999313aSBarry Smith 9582999313aSBarry Smith PetscFunctionBegin; 9590700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9600700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 9612999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 9627dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 963906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 9642999313aSBarry Smith snes->ksp = ksp; 9652999313aSBarry Smith PetscFunctionReturn(0); 9662999313aSBarry Smith } 9672999313aSBarry Smith 9687adad957SLisandro Dalcin #if 0 9692999313aSBarry Smith #undef __FUNCT__ 9704a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 9716849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 972e24b481bSBarry Smith { 973e24b481bSBarry Smith PetscFunctionBegin; 974e24b481bSBarry Smith PetscFunctionReturn(0); 975e24b481bSBarry Smith } 9767adad957SLisandro Dalcin #endif 977e24b481bSBarry Smith 9789b94acceSBarry Smith /* -----------------------------------------------------------*/ 9794a2ae208SSatish Balay #undef __FUNCT__ 9804a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 98152baeb72SSatish Balay /*@ 9829b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 9839b94acceSBarry Smith 984c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 985c7afd0dbSLois Curfman McInnes 986c7afd0dbSLois Curfman McInnes Input Parameters: 987906ed7ccSBarry Smith . comm - MPI communicator 9889b94acceSBarry Smith 9899b94acceSBarry Smith Output Parameter: 9909b94acceSBarry Smith . outsnes - the new SNES context 9919b94acceSBarry Smith 992c7afd0dbSLois Curfman McInnes Options Database Keys: 993c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 994c7afd0dbSLois Curfman McInnes and no preconditioning matrix 995c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 996c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 997c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 998c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 999c1f60f51SBarry Smith 100036851e7fSLois Curfman McInnes Level: beginner 100136851e7fSLois Curfman McInnes 10029b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 10039b94acceSBarry Smith 1004a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 1005a8054027SBarry Smith 10069b94acceSBarry Smith @*/ 10077087cfbeSBarry Smith PetscErrorCode SNESCreate(MPI_Comm comm,SNES *outsnes) 10089b94acceSBarry Smith { 1009dfbe8321SBarry Smith PetscErrorCode ierr; 10109b94acceSBarry Smith SNES snes; 1011fa9f3622SBarry Smith SNESKSPEW *kctx; 101237fcc0dbSBarry Smith 10133a40ed3dSBarry Smith PetscFunctionBegin; 1014ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 10158ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 10168ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 10178ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 10188ba1e511SMatthew Knepley #endif 10198ba1e511SMatthew Knepley 10200700a824SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 10217adad957SLisandro Dalcin 102285385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 10239b94acceSBarry Smith snes->max_its = 50; 10249750a799SBarry Smith snes->max_funcs = 10000; 10259b94acceSBarry Smith snes->norm = 0.0; 1026b4874afaSBarry Smith snes->rtol = 1.e-8; 1027b4874afaSBarry Smith snes->ttol = 0.0; 102870441072SBarry Smith snes->abstol = 1.e-50; 10299b94acceSBarry Smith snes->xtol = 1.e-8; 10304b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 10319b94acceSBarry Smith snes->nfuncs = 0; 103250ffb88aSMatthew Knepley snes->numFailures = 0; 103350ffb88aSMatthew Knepley snes->maxFailures = 1; 10347a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1035e35cf81dSBarry Smith snes->lagjacobian = 1; 1036a8054027SBarry Smith snes->lagpreconditioner = 1; 1037639f9d9dSBarry Smith snes->numbermonitors = 0; 10389b94acceSBarry Smith snes->data = 0; 10394dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1040186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 10416f24a144SLois Curfman McInnes snes->nwork = 0; 104258c9b817SLisandro Dalcin snes->work = 0; 104358c9b817SLisandro Dalcin snes->nvwork = 0; 104458c9b817SLisandro Dalcin snes->vwork = 0; 1045758f92a0SBarry Smith snes->conv_hist_len = 0; 1046758f92a0SBarry Smith snes->conv_hist_max = 0; 1047758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 1048758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 1049758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1050184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 10519b94acceSBarry Smith 10523d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 10533d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 10543d4c4710SBarry Smith 10559b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 105638f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 10579b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 10589b94acceSBarry Smith kctx->version = 2; 10599b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 10609b94acceSBarry Smith this was too large for some test cases */ 106175567043SBarry Smith kctx->rtol_last = 0.0; 10629b94acceSBarry Smith kctx->rtol_max = .9; 10639b94acceSBarry Smith kctx->gamma = 1.0; 106471f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 106571f87433Sdalcinl kctx->alpha2 = kctx->alpha; 10669b94acceSBarry Smith kctx->threshold = .1; 106775567043SBarry Smith kctx->lresid_last = 0.0; 106875567043SBarry Smith kctx->norm_last = 0.0; 10699b94acceSBarry Smith 10709b94acceSBarry Smith *outsnes = snes; 10713a40ed3dSBarry Smith PetscFunctionReturn(0); 10729b94acceSBarry Smith } 10739b94acceSBarry Smith 10744a2ae208SSatish Balay #undef __FUNCT__ 10754a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 10769b94acceSBarry Smith /*@C 10779b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 10789b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 10799b94acceSBarry Smith equations. 10809b94acceSBarry Smith 10813f9fe445SBarry Smith Logically Collective on SNES 1082fee21e36SBarry Smith 1083c7afd0dbSLois Curfman McInnes Input Parameters: 1084c7afd0dbSLois Curfman McInnes + snes - the SNES context 1085c7afd0dbSLois Curfman McInnes . r - vector to store function value 1086de044059SHong Zhang . func - function evaluation routine 1087c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1088c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 10899b94acceSBarry Smith 1090c7afd0dbSLois Curfman McInnes Calling sequence of func: 10918d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 1092c7afd0dbSLois Curfman McInnes 1093313e4042SLois Curfman McInnes . f - function vector 1094c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 10959b94acceSBarry Smith 10969b94acceSBarry Smith Notes: 10979b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 10989b94acceSBarry Smith $ f'(x) x = -f(x), 1099c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 11009b94acceSBarry Smith 110136851e7fSLois Curfman McInnes Level: beginner 110236851e7fSLois Curfman McInnes 11039b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 11049b94acceSBarry Smith 1105a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 11069b94acceSBarry Smith @*/ 11077087cfbeSBarry Smith PetscErrorCode SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 11089b94acceSBarry Smith { 110985385478SLisandro Dalcin PetscErrorCode ierr; 11103a40ed3dSBarry Smith PetscFunctionBegin; 11110700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1112d2a683ecSLisandro Dalcin if (r) { 1113d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1114d2a683ecSLisandro Dalcin PetscCheckSameComm(snes,1,r,2); 111585385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 11166bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 111785385478SLisandro Dalcin snes->vec_func = r; 1118d2a683ecSLisandro Dalcin } else if (!snes->vec_func && snes->dm) { 1119d2a683ecSLisandro Dalcin ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1120d2a683ecSLisandro Dalcin } 1121d2a683ecSLisandro Dalcin if (func) snes->ops->computefunction = func; 1122d2a683ecSLisandro Dalcin if (ctx) snes->funP = ctx; 11233a40ed3dSBarry Smith PetscFunctionReturn(0); 11249b94acceSBarry Smith } 11259b94acceSBarry Smith 1126d25893d9SBarry Smith #undef __FUNCT__ 1127d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess" 1128d25893d9SBarry Smith /*@C 1129d25893d9SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem 1130d25893d9SBarry Smith 1131d25893d9SBarry Smith Logically Collective on SNES 1132d25893d9SBarry Smith 1133d25893d9SBarry Smith Input Parameters: 1134d25893d9SBarry Smith + snes - the SNES context 1135d25893d9SBarry Smith . func - function evaluation routine 1136d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 1137d25893d9SBarry Smith function evaluation routine (may be PETSC_NULL) 1138d25893d9SBarry Smith 1139d25893d9SBarry Smith Calling sequence of func: 1140d25893d9SBarry Smith $ func (SNES snes,Vec x,void *ctx); 1141d25893d9SBarry Smith 1142d25893d9SBarry Smith . f - function vector 1143d25893d9SBarry Smith - ctx - optional user-defined function context 1144d25893d9SBarry Smith 1145d25893d9SBarry Smith Level: intermediate 1146d25893d9SBarry Smith 1147d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function 1148d25893d9SBarry Smith 1149d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 1150d25893d9SBarry Smith @*/ 1151d25893d9SBarry Smith PetscErrorCode SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx) 1152d25893d9SBarry Smith { 1153d25893d9SBarry Smith PetscFunctionBegin; 1154d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1155d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 1156d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 1157d25893d9SBarry Smith PetscFunctionReturn(0); 1158d25893d9SBarry Smith } 1159d25893d9SBarry Smith 11603ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 11613ab0aad5SBarry Smith #undef __FUNCT__ 11621096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 11631096aae1SMatthew Knepley /*@C 11641096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 11651096aae1SMatthew Knepley it assumes a zero right hand side. 11661096aae1SMatthew Knepley 11673f9fe445SBarry Smith Logically Collective on SNES 11681096aae1SMatthew Knepley 11691096aae1SMatthew Knepley Input Parameter: 11701096aae1SMatthew Knepley . snes - the SNES context 11711096aae1SMatthew Knepley 11721096aae1SMatthew Knepley Output Parameter: 1173bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 11741096aae1SMatthew Knepley 11751096aae1SMatthew Knepley Level: intermediate 11761096aae1SMatthew Knepley 11771096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 11781096aae1SMatthew Knepley 117985385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 11801096aae1SMatthew Knepley @*/ 11817087cfbeSBarry Smith PetscErrorCode SNESGetRhs(SNES snes,Vec *rhs) 11821096aae1SMatthew Knepley { 11831096aae1SMatthew Knepley PetscFunctionBegin; 11840700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 11851096aae1SMatthew Knepley PetscValidPointer(rhs,2); 118685385478SLisandro Dalcin *rhs = snes->vec_rhs; 11871096aae1SMatthew Knepley PetscFunctionReturn(0); 11881096aae1SMatthew Knepley } 11891096aae1SMatthew Knepley 11901096aae1SMatthew Knepley #undef __FUNCT__ 11914a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 11929b94acceSBarry Smith /*@ 119336851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 11949b94acceSBarry Smith SNESSetFunction(). 11959b94acceSBarry Smith 1196c7afd0dbSLois Curfman McInnes Collective on SNES 1197c7afd0dbSLois Curfman McInnes 11989b94acceSBarry Smith Input Parameters: 1199c7afd0dbSLois Curfman McInnes + snes - the SNES context 1200c7afd0dbSLois Curfman McInnes - x - input vector 12019b94acceSBarry Smith 12029b94acceSBarry Smith Output Parameter: 12033638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 12049b94acceSBarry Smith 12051bffabb2SLois Curfman McInnes Notes: 120636851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 120736851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 120836851e7fSLois Curfman McInnes themselves. 120936851e7fSLois Curfman McInnes 121036851e7fSLois Curfman McInnes Level: developer 121136851e7fSLois Curfman McInnes 12129b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 12139b94acceSBarry Smith 1214a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 12159b94acceSBarry Smith @*/ 12167087cfbeSBarry Smith PetscErrorCode SNESComputeFunction(SNES snes,Vec x,Vec y) 12179b94acceSBarry Smith { 1218dfbe8321SBarry Smith PetscErrorCode ierr; 12199b94acceSBarry Smith 12203a40ed3dSBarry Smith PetscFunctionBegin; 12210700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12220700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 12230700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 1224c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1225c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 1226184914b5SBarry Smith 1227d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1228e7788613SBarry Smith if (snes->ops->computefunction) { 1229d64ed03dSBarry Smith PetscStackPush("SNES user function"); 123039d508bbSBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1231d64ed03dSBarry Smith PetscStackPop; 123285385478SLisandro Dalcin } else if (snes->vec_rhs) { 12331096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 123417186662SBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 123585385478SLisandro Dalcin if (snes->vec_rhs) { 123685385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 12373ab0aad5SBarry Smith } 1238ae3c334cSLois Curfman McInnes snes->nfuncs++; 1239d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 12403a40ed3dSBarry Smith PetscFunctionReturn(0); 12419b94acceSBarry Smith } 12429b94acceSBarry Smith 12434a2ae208SSatish Balay #undef __FUNCT__ 12444a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 124562fef451SLois Curfman McInnes /*@ 124662fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 124762fef451SLois Curfman McInnes set with SNESSetJacobian(). 124862fef451SLois Curfman McInnes 1249c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1250c7afd0dbSLois Curfman McInnes 125162fef451SLois Curfman McInnes Input Parameters: 1252c7afd0dbSLois Curfman McInnes + snes - the SNES context 1253c7afd0dbSLois Curfman McInnes - x - input vector 125462fef451SLois Curfman McInnes 125562fef451SLois Curfman McInnes Output Parameters: 1256c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 125762fef451SLois Curfman McInnes . B - optional preconditioning matrix 12582b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1259fee21e36SBarry Smith 1260e35cf81dSBarry Smith Options Database Keys: 1261e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1262693365a8SJed Brown . -snes_lag_jacobian <lag> 1263693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 1264693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 1265693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 1266693365a8SJed Brown - -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 1267e35cf81dSBarry Smith 126862fef451SLois Curfman McInnes Notes: 126962fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 127062fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 127162fef451SLois Curfman McInnes 127294b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1273dc5a77f8SLois Curfman McInnes flag parameter. 127462fef451SLois Curfman McInnes 127536851e7fSLois Curfman McInnes Level: developer 127636851e7fSLois Curfman McInnes 127762fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 127862fef451SLois Curfman McInnes 1279e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 128062fef451SLois Curfman McInnes @*/ 12817087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 12829b94acceSBarry Smith { 1283dfbe8321SBarry Smith PetscErrorCode ierr; 1284ace3abfcSBarry Smith PetscBool flag; 12853a40ed3dSBarry Smith 12863a40ed3dSBarry Smith PetscFunctionBegin; 12870700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12880700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 12894482741eSBarry Smith PetscValidPointer(flg,5); 1290c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1291e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1292ebd3b9afSBarry Smith 1293ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1294ebd3b9afSBarry Smith 1295fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1296fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1297fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1298fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1299e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1300e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1301ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1302ebd3b9afSBarry Smith if (flag) { 1303ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1304ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1305ebd3b9afSBarry Smith } 1306e35cf81dSBarry Smith PetscFunctionReturn(0); 1307e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1308e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1309e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1310ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1311ebd3b9afSBarry Smith if (flag) { 1312ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1313ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1314ebd3b9afSBarry Smith } 1315e35cf81dSBarry Smith PetscFunctionReturn(0); 1316e35cf81dSBarry Smith } 1317e35cf81dSBarry Smith 1318c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1319e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1320d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1321e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1322d64ed03dSBarry Smith PetscStackPop; 1323d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1324a8054027SBarry Smith 13253b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 13263b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 13273b4f5425SBarry Smith snes->lagpreconditioner = -1; 13283b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1329a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1330a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1331a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1332a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1333a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1334a8054027SBarry Smith } 1335a8054027SBarry Smith 13366d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 13370700a824SBarry Smith /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 13380700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,4); */ 1339693365a8SJed Brown { 1340693365a8SJed Brown PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE; 1341693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr); 1342693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 1343693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 1344693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr); 1345693365a8SJed Brown if (flag || flag_draw || flag_contour) { 1346693365a8SJed Brown Mat Bexp_mine = PETSC_NULL,Bexp,FDexp; 1347693365a8SJed Brown MatStructure mstruct; 1348693365a8SJed Brown PetscViewer vdraw,vstdout; 1349*6b3a5b13SJed Brown PetscBool flg; 1350693365a8SJed Brown if (flag_operator) { 1351693365a8SJed Brown ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr); 1352693365a8SJed Brown Bexp = Bexp_mine; 1353693365a8SJed Brown } else { 1354693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 1355693365a8SJed Brown ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr); 1356693365a8SJed Brown if (flg) Bexp = *B; 1357693365a8SJed Brown else { 1358693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 1359693365a8SJed Brown ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr); 1360693365a8SJed Brown Bexp = Bexp_mine; 1361693365a8SJed Brown } 1362693365a8SJed Brown } 1363693365a8SJed Brown ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr); 1364693365a8SJed Brown ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr); 1365693365a8SJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 1366693365a8SJed Brown if (flag_draw || flag_contour) { 1367693365a8SJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 1368693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 1369693365a8SJed Brown } else vdraw = PETSC_NULL; 1370693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr); 1371693365a8SJed Brown if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);} 1372693365a8SJed Brown if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);} 1373693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr); 1374693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1375693365a8SJed Brown if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);} 1376693365a8SJed Brown ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 1377693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr); 1378693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1379693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 1380693365a8SJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 1381693365a8SJed Brown ierr = MatView(FDexp,vdraw);CHKERRQ(ierr); 1382693365a8SJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 1383693365a8SJed Brown } 1384693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 1385693365a8SJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 1386693365a8SJed Brown ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr); 1387693365a8SJed Brown ierr = MatDestroy(&FDexp);CHKERRQ(ierr); 1388693365a8SJed Brown } 1389693365a8SJed Brown } 13903a40ed3dSBarry Smith PetscFunctionReturn(0); 13919b94acceSBarry Smith } 13929b94acceSBarry Smith 13934a2ae208SSatish Balay #undef __FUNCT__ 13944a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 13959b94acceSBarry Smith /*@C 13969b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1397044dda88SLois Curfman McInnes location to store the matrix. 13989b94acceSBarry Smith 13993f9fe445SBarry Smith Logically Collective on SNES and Mat 1400c7afd0dbSLois Curfman McInnes 14019b94acceSBarry Smith Input Parameters: 1402c7afd0dbSLois Curfman McInnes + snes - the SNES context 14039b94acceSBarry Smith . A - Jacobian matrix 14049b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 1405efd51863SBarry Smith . func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value) 1406c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1407efd51863SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value) 14089b94acceSBarry Smith 14099b94acceSBarry Smith Calling sequence of func: 14108d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 14119b94acceSBarry Smith 1412c7afd0dbSLois Curfman McInnes + x - input vector 14139b94acceSBarry Smith . A - Jacobian matrix 14149b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1415ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 14162b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1417c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 14189b94acceSBarry Smith 14199b94acceSBarry Smith Notes: 142094b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 14212cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1422ac21db08SLois Curfman McInnes 1423ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 14249b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 14259b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 14269b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 14279b94acceSBarry Smith throughout the global iterations. 14289b94acceSBarry Smith 142916913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 143016913363SBarry Smith each matrix. 143116913363SBarry Smith 1432a8a26c1eSJed Brown If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument 1433a8a26c1eSJed Brown must be a MatFDColoring. 1434a8a26c1eSJed Brown 143536851e7fSLois Curfman McInnes Level: beginner 143636851e7fSLois Curfman McInnes 14379b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 14389b94acceSBarry Smith 14393ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 14409b94acceSBarry Smith @*/ 14417087cfbeSBarry Smith PetscErrorCode SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 14429b94acceSBarry Smith { 1443dfbe8321SBarry Smith PetscErrorCode ierr; 14443a7fca6bSBarry Smith 14453a40ed3dSBarry Smith PetscFunctionBegin; 14460700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14470700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 14480700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1449c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 145006975374SJed Brown if (B) PetscCheckSameComm(snes,1,B,3); 1451e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 14523a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 14533a7fca6bSBarry Smith if (A) { 14547dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 14556bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 14569b94acceSBarry Smith snes->jacobian = A; 14573a7fca6bSBarry Smith } 14583a7fca6bSBarry Smith if (B) { 14597dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 14606bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 14619b94acceSBarry Smith snes->jacobian_pre = B; 14623a7fca6bSBarry Smith } 14633a40ed3dSBarry Smith PetscFunctionReturn(0); 14649b94acceSBarry Smith } 146562fef451SLois Curfman McInnes 14664a2ae208SSatish Balay #undef __FUNCT__ 14674a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1468c2aafc4cSSatish Balay /*@C 1469b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1470b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1471b4fd4287SBarry Smith 1472c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1473c7afd0dbSLois Curfman McInnes 1474b4fd4287SBarry Smith Input Parameter: 1475b4fd4287SBarry Smith . snes - the nonlinear solver context 1476b4fd4287SBarry Smith 1477b4fd4287SBarry Smith Output Parameters: 1478c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1479b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 148070e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 148170e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1482fee21e36SBarry Smith 148336851e7fSLois Curfman McInnes Level: advanced 148436851e7fSLois Curfman McInnes 1485b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1486b4fd4287SBarry Smith @*/ 14877087cfbeSBarry Smith PetscErrorCode SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1488b4fd4287SBarry Smith { 14893a40ed3dSBarry Smith PetscFunctionBegin; 14900700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1491b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1492b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1493e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 149470e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 14953a40ed3dSBarry Smith PetscFunctionReturn(0); 1496b4fd4287SBarry Smith } 1497b4fd4287SBarry Smith 14989b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 14999b94acceSBarry Smith 15004a2ae208SSatish Balay #undef __FUNCT__ 15014a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 15029b94acceSBarry Smith /*@ 15039b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1504272ac6f2SLois Curfman McInnes of a nonlinear solver. 15059b94acceSBarry Smith 1506fee21e36SBarry Smith Collective on SNES 1507fee21e36SBarry Smith 1508c7afd0dbSLois Curfman McInnes Input Parameters: 150970e92668SMatthew Knepley . snes - the SNES context 1510c7afd0dbSLois Curfman McInnes 1511272ac6f2SLois Curfman McInnes Notes: 1512272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1513272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1514272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1515272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1516272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1517272ac6f2SLois Curfman McInnes 151836851e7fSLois Curfman McInnes Level: advanced 151936851e7fSLois Curfman McInnes 15209b94acceSBarry Smith .keywords: SNES, nonlinear, setup 15219b94acceSBarry Smith 15229b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 15239b94acceSBarry Smith @*/ 15247087cfbeSBarry Smith PetscErrorCode SNESSetUp(SNES snes) 15259b94acceSBarry Smith { 1526dfbe8321SBarry Smith PetscErrorCode ierr; 15273a40ed3dSBarry Smith 15283a40ed3dSBarry Smith PetscFunctionBegin; 15290700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15304dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 15319b94acceSBarry Smith 15327adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 153385385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 153485385478SLisandro Dalcin } 153585385478SLisandro Dalcin 1536efd51863SBarry Smith if (!snes->vec_func && snes->dm && !snes->vec_rhs) { 1537efd51863SBarry Smith ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 1538efd51863SBarry Smith } 153917186662SBarry Smith if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 154017186662SBarry Smith if (!snes->ops->computefunction && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 154117186662SBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 154258c9b817SLisandro Dalcin if (snes->vec_rhs == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector"); 154358c9b817SLisandro Dalcin 154458c9b817SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 154558c9b817SLisandro Dalcin ierr = VecDuplicate(snes->vec_rhs, &snes->vec_func);CHKERRQ(ierr); 154658c9b817SLisandro Dalcin } 154758c9b817SLisandro Dalcin if (!snes->vec_sol_update /* && snes->vec_sol */) { 154858c9b817SLisandro Dalcin ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr); 154958c9b817SLisandro Dalcin ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr); 155058c9b817SLisandro Dalcin } 155158c9b817SLisandro Dalcin 1552ef8dffc7SBarry Smith if (!snes->ops->computejacobian && snes->dm) { 1553ef8dffc7SBarry Smith Mat J; 1554ef8dffc7SBarry Smith ISColoring coloring; 1555ef8dffc7SBarry Smith MatFDColoring fd; 1556a703fe33SLois Curfman McInnes 1557ef8dffc7SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 1558ef8dffc7SBarry Smith ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr); 1559ef8dffc7SBarry Smith ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr); 1560ef8dffc7SBarry Smith ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr); 1561ef8dffc7SBarry Smith ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr); 15626bf464f9SBarry Smith ierr = ISColoringDestroy(&coloring);CHKERRQ(ierr); 1563efd51863SBarry Smith } else if (snes->dm && !snes->jacobian_pre){ 1564efd51863SBarry Smith Mat J; 1565efd51863SBarry Smith ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr); 1566efd51863SBarry Smith ierr = SNESSetJacobian(snes,J,J,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 1567efd51863SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 1568ef8dffc7SBarry Smith } 1569efd51863SBarry Smith 1570b710008aSBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);} 1571b710008aSBarry Smith 1572d25893d9SBarry Smith if (snes->ops->usercompute && !snes->user) { 1573d25893d9SBarry Smith ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr); 1574d25893d9SBarry Smith } 1575d25893d9SBarry Smith 1576410397dcSLisandro Dalcin if (snes->ops->setup) { 1577410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1578410397dcSLisandro Dalcin } 157958c9b817SLisandro Dalcin 15807aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 15813a40ed3dSBarry Smith PetscFunctionReturn(0); 15829b94acceSBarry Smith } 15839b94acceSBarry Smith 15844a2ae208SSatish Balay #undef __FUNCT__ 158537596af1SLisandro Dalcin #define __FUNCT__ "SNESReset" 158637596af1SLisandro Dalcin /*@ 158737596af1SLisandro Dalcin SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats 158837596af1SLisandro Dalcin 158937596af1SLisandro Dalcin Collective on SNES 159037596af1SLisandro Dalcin 159137596af1SLisandro Dalcin Input Parameter: 159237596af1SLisandro Dalcin . snes - iterative context obtained from SNESCreate() 159337596af1SLisandro Dalcin 1594d25893d9SBarry Smith Level: intermediate 1595d25893d9SBarry Smith 1596d25893d9SBarry Smith Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext() 159737596af1SLisandro Dalcin 159837596af1SLisandro Dalcin .keywords: SNES, destroy 159937596af1SLisandro Dalcin 160037596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 160137596af1SLisandro Dalcin @*/ 160237596af1SLisandro Dalcin PetscErrorCode SNESReset(SNES snes) 160337596af1SLisandro Dalcin { 160437596af1SLisandro Dalcin PetscErrorCode ierr; 160537596af1SLisandro Dalcin 160637596af1SLisandro Dalcin PetscFunctionBegin; 160737596af1SLisandro Dalcin PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1608d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 1609d25893d9SBarry Smith ierr = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr); 1610d25893d9SBarry Smith snes->user = PETSC_NULL; 1611d25893d9SBarry Smith } 1612d25893d9SBarry Smith 161337596af1SLisandro Dalcin if (snes->ops->reset) { 161437596af1SLisandro Dalcin ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr); 161537596af1SLisandro Dalcin } 161637596af1SLisandro Dalcin if (snes->ksp) {ierr = KSPReset(snes->ksp);CHKERRQ(ierr);} 16176bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 16186bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 16196bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr); 16206bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 16216bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 16226bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 162337596af1SLisandro Dalcin if (snes->work) {ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr);} 162437596af1SLisandro Dalcin if (snes->vwork) {ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr);} 162537596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 162637596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 162737596af1SLisandro Dalcin PetscFunctionReturn(0); 162837596af1SLisandro Dalcin } 162937596af1SLisandro Dalcin 163037596af1SLisandro Dalcin #undef __FUNCT__ 16314a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 163252baeb72SSatish Balay /*@ 16339b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 16349b94acceSBarry Smith with SNESCreate(). 16359b94acceSBarry Smith 1636c7afd0dbSLois Curfman McInnes Collective on SNES 1637c7afd0dbSLois Curfman McInnes 16389b94acceSBarry Smith Input Parameter: 16399b94acceSBarry Smith . snes - the SNES context 16409b94acceSBarry Smith 164136851e7fSLois Curfman McInnes Level: beginner 164236851e7fSLois Curfman McInnes 16439b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 16449b94acceSBarry Smith 164563a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 16469b94acceSBarry Smith @*/ 16476bf464f9SBarry Smith PetscErrorCode SNESDestroy(SNES *snes) 16489b94acceSBarry Smith { 16496849ba73SBarry Smith PetscErrorCode ierr; 16503a40ed3dSBarry Smith 16513a40ed3dSBarry Smith PetscFunctionBegin; 16526bf464f9SBarry Smith if (!*snes) PetscFunctionReturn(0); 16536bf464f9SBarry Smith PetscValidHeaderSpecific((*snes),SNES_CLASSID,1); 16546bf464f9SBarry Smith if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);} 1655d4bb536fSBarry Smith 16566bf464f9SBarry Smith ierr = SNESReset((*snes));CHKERRQ(ierr); 16576b8b9a38SLisandro Dalcin 1658be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 16596bf464f9SBarry Smith ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr); 16606bf464f9SBarry Smith if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);} 16616d4c513bSLisandro Dalcin 16626bf464f9SBarry Smith ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr); 16636bf464f9SBarry Smith ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr); 16646b8b9a38SLisandro Dalcin 16656bf464f9SBarry Smith ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr); 16666bf464f9SBarry Smith if ((*snes)->ops->convergeddestroy) { 16676bf464f9SBarry Smith ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr); 16686b8b9a38SLisandro Dalcin } 16696bf464f9SBarry Smith if ((*snes)->conv_malloc) { 16706bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr); 16716bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr); 167258c9b817SLisandro Dalcin } 16736bf464f9SBarry Smith ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr); 1674a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 16753a40ed3dSBarry Smith PetscFunctionReturn(0); 16769b94acceSBarry Smith } 16779b94acceSBarry Smith 16789b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 16799b94acceSBarry Smith 16804a2ae208SSatish Balay #undef __FUNCT__ 1681a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1682a8054027SBarry Smith /*@ 1683a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1684a8054027SBarry Smith 16853f9fe445SBarry Smith Logically Collective on SNES 1686a8054027SBarry Smith 1687a8054027SBarry Smith Input Parameters: 1688a8054027SBarry Smith + snes - the SNES context 1689a8054027SBarry 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 16903b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1691a8054027SBarry Smith 1692a8054027SBarry Smith Options Database Keys: 1693a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1694a8054027SBarry Smith 1695a8054027SBarry Smith Notes: 1696a8054027SBarry Smith The default is 1 1697a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1698a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1699a8054027SBarry Smith 1700a8054027SBarry Smith Level: intermediate 1701a8054027SBarry Smith 1702a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1703a8054027SBarry Smith 1704e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1705a8054027SBarry Smith 1706a8054027SBarry Smith @*/ 17077087cfbeSBarry Smith PetscErrorCode SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1708a8054027SBarry Smith { 1709a8054027SBarry Smith PetscFunctionBegin; 17100700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1711e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1712e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1713c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 1714a8054027SBarry Smith snes->lagpreconditioner = lag; 1715a8054027SBarry Smith PetscFunctionReturn(0); 1716a8054027SBarry Smith } 1717a8054027SBarry Smith 1718a8054027SBarry Smith #undef __FUNCT__ 1719efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence" 1720efd51863SBarry Smith /*@ 1721efd51863SBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does 1722efd51863SBarry Smith 1723efd51863SBarry Smith Logically Collective on SNES 1724efd51863SBarry Smith 1725efd51863SBarry Smith Input Parameters: 1726efd51863SBarry Smith + snes - the SNES context 1727efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 1728efd51863SBarry Smith 1729efd51863SBarry Smith Options Database Keys: 1730efd51863SBarry Smith . -snes_grid_sequence <steps> 1731efd51863SBarry Smith 1732efd51863SBarry Smith Level: intermediate 1733efd51863SBarry Smith 1734efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1735efd51863SBarry Smith 1736efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1737efd51863SBarry Smith 1738efd51863SBarry Smith @*/ 1739efd51863SBarry Smith PetscErrorCode SNESSetGridSequence(SNES snes,PetscInt steps) 1740efd51863SBarry Smith { 1741efd51863SBarry Smith PetscFunctionBegin; 1742efd51863SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1743efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes,steps,2); 1744efd51863SBarry Smith snes->gridsequence = steps; 1745efd51863SBarry Smith PetscFunctionReturn(0); 1746efd51863SBarry Smith } 1747efd51863SBarry Smith 1748efd51863SBarry Smith #undef __FUNCT__ 1749a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1750a8054027SBarry Smith /*@ 1751a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1752a8054027SBarry Smith 17533f9fe445SBarry Smith Not Collective 1754a8054027SBarry Smith 1755a8054027SBarry Smith Input Parameter: 1756a8054027SBarry Smith . snes - the SNES context 1757a8054027SBarry Smith 1758a8054027SBarry Smith Output Parameter: 1759a8054027SBarry 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 17603b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1761a8054027SBarry Smith 1762a8054027SBarry Smith Options Database Keys: 1763a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1764a8054027SBarry Smith 1765a8054027SBarry Smith Notes: 1766a8054027SBarry Smith The default is 1 1767a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1768a8054027SBarry Smith 1769a8054027SBarry Smith Level: intermediate 1770a8054027SBarry Smith 1771a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1772a8054027SBarry Smith 1773a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1774a8054027SBarry Smith 1775a8054027SBarry Smith @*/ 17767087cfbeSBarry Smith PetscErrorCode SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1777a8054027SBarry Smith { 1778a8054027SBarry Smith PetscFunctionBegin; 17790700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1780a8054027SBarry Smith *lag = snes->lagpreconditioner; 1781a8054027SBarry Smith PetscFunctionReturn(0); 1782a8054027SBarry Smith } 1783a8054027SBarry Smith 1784a8054027SBarry Smith #undef __FUNCT__ 1785e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 1786e35cf81dSBarry Smith /*@ 1787e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 1788e35cf81dSBarry Smith often the preconditioner is rebuilt. 1789e35cf81dSBarry Smith 17903f9fe445SBarry Smith Logically Collective on SNES 1791e35cf81dSBarry Smith 1792e35cf81dSBarry Smith Input Parameters: 1793e35cf81dSBarry Smith + snes - the SNES context 1794e35cf81dSBarry 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 1795fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 1796e35cf81dSBarry Smith 1797e35cf81dSBarry Smith Options Database Keys: 1798e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1799e35cf81dSBarry Smith 1800e35cf81dSBarry Smith Notes: 1801e35cf81dSBarry Smith The default is 1 1802e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1803fe3ffe1eSBarry 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 1804fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 1805e35cf81dSBarry Smith 1806e35cf81dSBarry Smith Level: intermediate 1807e35cf81dSBarry Smith 1808e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1809e35cf81dSBarry Smith 1810e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 1811e35cf81dSBarry Smith 1812e35cf81dSBarry Smith @*/ 18137087cfbeSBarry Smith PetscErrorCode SNESSetLagJacobian(SNES snes,PetscInt lag) 1814e35cf81dSBarry Smith { 1815e35cf81dSBarry Smith PetscFunctionBegin; 18160700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1817e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1818e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1819c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 1820e35cf81dSBarry Smith snes->lagjacobian = lag; 1821e35cf81dSBarry Smith PetscFunctionReturn(0); 1822e35cf81dSBarry Smith } 1823e35cf81dSBarry Smith 1824e35cf81dSBarry Smith #undef __FUNCT__ 1825e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 1826e35cf81dSBarry Smith /*@ 1827e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 1828e35cf81dSBarry Smith 18293f9fe445SBarry Smith Not Collective 1830e35cf81dSBarry Smith 1831e35cf81dSBarry Smith Input Parameter: 1832e35cf81dSBarry Smith . snes - the SNES context 1833e35cf81dSBarry Smith 1834e35cf81dSBarry Smith Output Parameter: 1835e35cf81dSBarry 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 1836e35cf81dSBarry Smith the Jacobian is built etc. 1837e35cf81dSBarry Smith 1838e35cf81dSBarry Smith Options Database Keys: 1839e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1840e35cf81dSBarry Smith 1841e35cf81dSBarry Smith Notes: 1842e35cf81dSBarry Smith The default is 1 1843e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1844e35cf81dSBarry Smith 1845e35cf81dSBarry Smith Level: intermediate 1846e35cf81dSBarry Smith 1847e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1848e35cf81dSBarry Smith 1849e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 1850e35cf81dSBarry Smith 1851e35cf81dSBarry Smith @*/ 18527087cfbeSBarry Smith PetscErrorCode SNESGetLagJacobian(SNES snes,PetscInt *lag) 1853e35cf81dSBarry Smith { 1854e35cf81dSBarry Smith PetscFunctionBegin; 18550700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1856e35cf81dSBarry Smith *lag = snes->lagjacobian; 1857e35cf81dSBarry Smith PetscFunctionReturn(0); 1858e35cf81dSBarry Smith } 1859e35cf81dSBarry Smith 1860e35cf81dSBarry Smith #undef __FUNCT__ 18614a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 18629b94acceSBarry Smith /*@ 1863d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 18649b94acceSBarry Smith 18653f9fe445SBarry Smith Logically Collective on SNES 1866c7afd0dbSLois Curfman McInnes 18679b94acceSBarry Smith Input Parameters: 1868c7afd0dbSLois Curfman McInnes + snes - the SNES context 186970441072SBarry Smith . abstol - absolute convergence tolerance 187033174efeSLois Curfman McInnes . rtol - relative convergence tolerance 187133174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 187233174efeSLois Curfman McInnes of the change in the solution between steps 187333174efeSLois Curfman McInnes . maxit - maximum number of iterations 1874c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1875fee21e36SBarry Smith 187633174efeSLois Curfman McInnes Options Database Keys: 187770441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1878c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1879c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1880c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1881c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 18829b94acceSBarry Smith 1883d7a720efSLois Curfman McInnes Notes: 18849b94acceSBarry Smith The default maximum number of iterations is 50. 18859b94acceSBarry Smith The default maximum number of function evaluations is 1000. 18869b94acceSBarry Smith 188736851e7fSLois Curfman McInnes Level: intermediate 188836851e7fSLois Curfman McInnes 188933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 18909b94acceSBarry Smith 18912492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 18929b94acceSBarry Smith @*/ 18937087cfbeSBarry Smith PetscErrorCode SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 18949b94acceSBarry Smith { 18953a40ed3dSBarry Smith PetscFunctionBegin; 18960700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1897c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,abstol,2); 1898c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol,3); 1899c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,stol,4); 1900c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxit,5); 1901c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxf,6); 1902c5eb9154SBarry Smith 190370441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1904d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1905d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1906d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1907d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 19083a40ed3dSBarry Smith PetscFunctionReturn(0); 19099b94acceSBarry Smith } 19109b94acceSBarry Smith 19114a2ae208SSatish Balay #undef __FUNCT__ 19124a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 19139b94acceSBarry Smith /*@ 191433174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 191533174efeSLois Curfman McInnes 1916c7afd0dbSLois Curfman McInnes Not Collective 1917c7afd0dbSLois Curfman McInnes 191833174efeSLois Curfman McInnes Input Parameters: 1919c7afd0dbSLois Curfman McInnes + snes - the SNES context 192085385478SLisandro Dalcin . atol - absolute convergence tolerance 192133174efeSLois Curfman McInnes . rtol - relative convergence tolerance 192233174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 192333174efeSLois Curfman McInnes of the change in the solution between steps 192433174efeSLois Curfman McInnes . maxit - maximum number of iterations 1925c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1926fee21e36SBarry Smith 192733174efeSLois Curfman McInnes Notes: 192833174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 192933174efeSLois Curfman McInnes 193036851e7fSLois Curfman McInnes Level: intermediate 193136851e7fSLois Curfman McInnes 193233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 193333174efeSLois Curfman McInnes 193433174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 193533174efeSLois Curfman McInnes @*/ 19367087cfbeSBarry Smith PetscErrorCode SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 193733174efeSLois Curfman McInnes { 19383a40ed3dSBarry Smith PetscFunctionBegin; 19390700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 194085385478SLisandro Dalcin if (atol) *atol = snes->abstol; 194133174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 194233174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 194333174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 194433174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 19453a40ed3dSBarry Smith PetscFunctionReturn(0); 194633174efeSLois Curfman McInnes } 194733174efeSLois Curfman McInnes 19484a2ae208SSatish Balay #undef __FUNCT__ 19494a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 195033174efeSLois Curfman McInnes /*@ 19519b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 19529b94acceSBarry Smith 19533f9fe445SBarry Smith Logically Collective on SNES 1954fee21e36SBarry Smith 1955c7afd0dbSLois Curfman McInnes Input Parameters: 1956c7afd0dbSLois Curfman McInnes + snes - the SNES context 1957c7afd0dbSLois Curfman McInnes - tol - tolerance 1958c7afd0dbSLois Curfman McInnes 19599b94acceSBarry Smith Options Database Key: 1960c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 19619b94acceSBarry Smith 196236851e7fSLois Curfman McInnes Level: intermediate 196336851e7fSLois Curfman McInnes 19649b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 19659b94acceSBarry Smith 19662492ecdbSBarry Smith .seealso: SNESSetTolerances() 19679b94acceSBarry Smith @*/ 19687087cfbeSBarry Smith PetscErrorCode SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 19699b94acceSBarry Smith { 19703a40ed3dSBarry Smith PetscFunctionBegin; 19710700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1972c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,tol,2); 19739b94acceSBarry Smith snes->deltatol = tol; 19743a40ed3dSBarry Smith PetscFunctionReturn(0); 19759b94acceSBarry Smith } 19769b94acceSBarry Smith 1977df9fa365SBarry Smith /* 1978df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1979df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1980df9fa365SBarry Smith macros instead of functions 1981df9fa365SBarry Smith */ 19824a2ae208SSatish Balay #undef __FUNCT__ 1983a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 19847087cfbeSBarry Smith PetscErrorCode SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1985ce1608b8SBarry Smith { 1986dfbe8321SBarry Smith PetscErrorCode ierr; 1987ce1608b8SBarry Smith 1988ce1608b8SBarry Smith PetscFunctionBegin; 19890700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1990a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1991ce1608b8SBarry Smith PetscFunctionReturn(0); 1992ce1608b8SBarry Smith } 1993ce1608b8SBarry Smith 19944a2ae208SSatish Balay #undef __FUNCT__ 1995a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 19967087cfbeSBarry Smith PetscErrorCode SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1997df9fa365SBarry Smith { 1998dfbe8321SBarry Smith PetscErrorCode ierr; 1999df9fa365SBarry Smith 2000df9fa365SBarry Smith PetscFunctionBegin; 2001a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2002df9fa365SBarry Smith PetscFunctionReturn(0); 2003df9fa365SBarry Smith } 2004df9fa365SBarry Smith 20054a2ae208SSatish Balay #undef __FUNCT__ 2006a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 20076bf464f9SBarry Smith PetscErrorCode SNESMonitorLGDestroy(PetscDrawLG *draw) 2008df9fa365SBarry Smith { 2009dfbe8321SBarry Smith PetscErrorCode ierr; 2010df9fa365SBarry Smith 2011df9fa365SBarry Smith PetscFunctionBegin; 2012a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2013df9fa365SBarry Smith PetscFunctionReturn(0); 2014df9fa365SBarry Smith } 2015df9fa365SBarry Smith 20167087cfbeSBarry Smith extern PetscErrorCode SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 2017b271bb04SBarry Smith #undef __FUNCT__ 2018b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 20197087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 2020b271bb04SBarry Smith { 2021b271bb04SBarry Smith PetscDrawLG lg; 2022b271bb04SBarry Smith PetscErrorCode ierr; 2023b271bb04SBarry Smith PetscReal x,y,per; 2024b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 2025b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 2026b271bb04SBarry Smith PetscDraw draw; 2027b271bb04SBarry Smith PetscFunctionBegin; 2028b271bb04SBarry Smith if (!monctx) { 2029b271bb04SBarry Smith MPI_Comm comm; 2030b271bb04SBarry Smith 2031b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 2032b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 2033b271bb04SBarry Smith } 2034b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 2035b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2036b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2037b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 2038b271bb04SBarry Smith x = (PetscReal) n; 2039b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 2040b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2041b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2042b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2043b271bb04SBarry Smith } 2044b271bb04SBarry Smith 2045b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 2046b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2047b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2048b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 2049b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 2050b271bb04SBarry Smith x = (PetscReal) n; 2051b271bb04SBarry Smith y = 100.0*per; 2052b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2053b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2054b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2055b271bb04SBarry Smith } 2056b271bb04SBarry Smith 2057b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 2058b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2059b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2060b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 2061b271bb04SBarry Smith x = (PetscReal) n; 2062b271bb04SBarry Smith y = (prev - rnorm)/prev; 2063b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2064b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2065b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2066b271bb04SBarry Smith } 2067b271bb04SBarry Smith 2068b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 2069b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2070b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2071b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 2072b271bb04SBarry Smith x = (PetscReal) n; 2073b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 2074b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 2075b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2076b271bb04SBarry Smith } 2077b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2078b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2079b271bb04SBarry Smith } 2080b271bb04SBarry Smith prev = rnorm; 2081b271bb04SBarry Smith PetscFunctionReturn(0); 2082b271bb04SBarry Smith } 2083b271bb04SBarry Smith 2084b271bb04SBarry Smith #undef __FUNCT__ 2085b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 20867087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2087b271bb04SBarry Smith { 2088b271bb04SBarry Smith PetscErrorCode ierr; 2089b271bb04SBarry Smith 2090b271bb04SBarry Smith PetscFunctionBegin; 2091b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2092b271bb04SBarry Smith PetscFunctionReturn(0); 2093b271bb04SBarry Smith } 2094b271bb04SBarry Smith 2095b271bb04SBarry Smith #undef __FUNCT__ 2096b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 20976bf464f9SBarry Smith PetscErrorCode SNESMonitorLGRangeDestroy(PetscDrawLG *draw) 2098b271bb04SBarry Smith { 2099b271bb04SBarry Smith PetscErrorCode ierr; 2100b271bb04SBarry Smith 2101b271bb04SBarry Smith PetscFunctionBegin; 2102b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2103b271bb04SBarry Smith PetscFunctionReturn(0); 2104b271bb04SBarry Smith } 2105b271bb04SBarry Smith 21067a03ce2fSLisandro Dalcin #undef __FUNCT__ 21077a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor" 21087a03ce2fSLisandro Dalcin /* 21097a03ce2fSLisandro Dalcin Runs the user provided monitor routines, if they exists. 21107a03ce2fSLisandro Dalcin */ 21117a03ce2fSLisandro Dalcin PetscErrorCode SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm) 21127a03ce2fSLisandro Dalcin { 21137a03ce2fSLisandro Dalcin PetscErrorCode ierr; 21147a03ce2fSLisandro Dalcin PetscInt i,n = snes->numbermonitors; 21157a03ce2fSLisandro Dalcin 21167a03ce2fSLisandro Dalcin PetscFunctionBegin; 21177a03ce2fSLisandro Dalcin for (i=0; i<n; i++) { 21187a03ce2fSLisandro Dalcin ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr); 21197a03ce2fSLisandro Dalcin } 21207a03ce2fSLisandro Dalcin PetscFunctionReturn(0); 21217a03ce2fSLisandro Dalcin } 21227a03ce2fSLisandro Dalcin 21239b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 21249b94acceSBarry Smith 21254a2ae208SSatish Balay #undef __FUNCT__ 2126a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 21279b94acceSBarry Smith /*@C 2128a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 21299b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 21309b94acceSBarry Smith progress. 21319b94acceSBarry Smith 21323f9fe445SBarry Smith Logically Collective on SNES 2133fee21e36SBarry Smith 2134c7afd0dbSLois Curfman McInnes Input Parameters: 2135c7afd0dbSLois Curfman McInnes + snes - the SNES context 2136c7afd0dbSLois Curfman McInnes . func - monitoring routine 2137b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 2138e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 2139b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 2140b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 21419b94acceSBarry Smith 2142c7afd0dbSLois Curfman McInnes Calling sequence of func: 2143a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 2144c7afd0dbSLois Curfman McInnes 2145c7afd0dbSLois Curfman McInnes + snes - the SNES context 2146c7afd0dbSLois Curfman McInnes . its - iteration number 2147c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 214840a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 21499b94acceSBarry Smith 21509665c990SLois Curfman McInnes Options Database Keys: 2151a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 2152a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 2153a6570f20SBarry Smith uses SNESMonitorLGCreate() 2154cca6129bSJed Brown - -snes_monitor_cancel - cancels all monitors that have 2155c7afd0dbSLois Curfman McInnes been hardwired into a code by 2156a6570f20SBarry Smith calls to SNESMonitorSet(), but 2157c7afd0dbSLois Curfman McInnes does not cancel those set via 2158c7afd0dbSLois Curfman McInnes the options database. 21599665c990SLois Curfman McInnes 2160639f9d9dSBarry Smith Notes: 21616bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 2162a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 21636bc08f3fSLois Curfman McInnes order in which they were set. 2164639f9d9dSBarry Smith 2165025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 2166025f1a04SBarry Smith 216736851e7fSLois Curfman McInnes Level: intermediate 216836851e7fSLois Curfman McInnes 21699b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 21709b94acceSBarry Smith 2171a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 21729b94acceSBarry Smith @*/ 2173c2efdce3SBarry Smith PetscErrorCode SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 21749b94acceSBarry Smith { 2175b90d0a6eSBarry Smith PetscInt i; 2176b90d0a6eSBarry Smith 21773a40ed3dSBarry Smith PetscFunctionBegin; 21780700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 217917186662SBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2180b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 2181b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 2182b90d0a6eSBarry Smith 2183b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 2184b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 2185b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 2186b90d0a6eSBarry Smith PetscErrorCode ierr; 2187b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 2188b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 2189b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 2190c2efdce3SBarry Smith ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr); 2191b90d0a6eSBarry Smith PetscFunctionReturn(0); 2192b90d0a6eSBarry Smith } 2193b90d0a6eSBarry Smith } 2194b90d0a6eSBarry Smith } 2195b90d0a6eSBarry Smith } 2196b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 2197b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 2198639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 21993a40ed3dSBarry Smith PetscFunctionReturn(0); 22009b94acceSBarry Smith } 22019b94acceSBarry Smith 22024a2ae208SSatish Balay #undef __FUNCT__ 2203a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 22045cd90555SBarry Smith /*@C 2205a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 22065cd90555SBarry Smith 22073f9fe445SBarry Smith Logically Collective on SNES 2208c7afd0dbSLois Curfman McInnes 22095cd90555SBarry Smith Input Parameters: 22105cd90555SBarry Smith . snes - the SNES context 22115cd90555SBarry Smith 22121a480d89SAdministrator Options Database Key: 2213a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 2214a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 2215c7afd0dbSLois Curfman McInnes set via the options database 22165cd90555SBarry Smith 22175cd90555SBarry Smith Notes: 22185cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 22195cd90555SBarry Smith 222036851e7fSLois Curfman McInnes Level: intermediate 222136851e7fSLois Curfman McInnes 22225cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 22235cd90555SBarry Smith 2224a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 22255cd90555SBarry Smith @*/ 22267087cfbeSBarry Smith PetscErrorCode SNESMonitorCancel(SNES snes) 22275cd90555SBarry Smith { 2228d952e501SBarry Smith PetscErrorCode ierr; 2229d952e501SBarry Smith PetscInt i; 2230d952e501SBarry Smith 22315cd90555SBarry Smith PetscFunctionBegin; 22320700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2233d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 2234d952e501SBarry Smith if (snes->monitordestroy[i]) { 22353c4aec1bSBarry Smith ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr); 2236d952e501SBarry Smith } 2237d952e501SBarry Smith } 22385cd90555SBarry Smith snes->numbermonitors = 0; 22395cd90555SBarry Smith PetscFunctionReturn(0); 22405cd90555SBarry Smith } 22415cd90555SBarry Smith 22424a2ae208SSatish Balay #undef __FUNCT__ 22434a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 22449b94acceSBarry Smith /*@C 22459b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 22469b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 22479b94acceSBarry Smith 22483f9fe445SBarry Smith Logically Collective on SNES 2249fee21e36SBarry Smith 2250c7afd0dbSLois Curfman McInnes Input Parameters: 2251c7afd0dbSLois Curfman McInnes + snes - the SNES context 2252c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 22537f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 22547f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 22559b94acceSBarry Smith 2256c7afd0dbSLois Curfman McInnes Calling sequence of func: 225706ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 2258c7afd0dbSLois Curfman McInnes 2259c7afd0dbSLois Curfman McInnes + snes - the SNES context 226006ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 2261c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 2262184914b5SBarry Smith . reason - reason for convergence/divergence 2263c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 22644b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 22654b27c08aSLois Curfman McInnes - f - 2-norm of function 22669b94acceSBarry Smith 226736851e7fSLois Curfman McInnes Level: advanced 226836851e7fSLois Curfman McInnes 22699b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 22709b94acceSBarry Smith 227185385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 22729b94acceSBarry Smith @*/ 22737087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 22749b94acceSBarry Smith { 22757f7931b9SBarry Smith PetscErrorCode ierr; 22767f7931b9SBarry Smith 22773a40ed3dSBarry Smith PetscFunctionBegin; 22780700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 227985385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 22807f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 22817f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 22827f7931b9SBarry Smith } 228385385478SLisandro Dalcin snes->ops->converged = func; 22847f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 228585385478SLisandro Dalcin snes->cnvP = cctx; 22863a40ed3dSBarry Smith PetscFunctionReturn(0); 22879b94acceSBarry Smith } 22889b94acceSBarry Smith 22894a2ae208SSatish Balay #undef __FUNCT__ 22904a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 229152baeb72SSatish Balay /*@ 2292184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 2293184914b5SBarry Smith 2294184914b5SBarry Smith Not Collective 2295184914b5SBarry Smith 2296184914b5SBarry Smith Input Parameter: 2297184914b5SBarry Smith . snes - the SNES context 2298184914b5SBarry Smith 2299184914b5SBarry Smith Output Parameter: 23004d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 2301184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 2302184914b5SBarry Smith 2303184914b5SBarry Smith Level: intermediate 2304184914b5SBarry Smith 2305184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 2306184914b5SBarry Smith 2307184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 2308184914b5SBarry Smith 230985385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 2310184914b5SBarry Smith @*/ 23117087cfbeSBarry Smith PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 2312184914b5SBarry Smith { 2313184914b5SBarry Smith PetscFunctionBegin; 23140700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23154482741eSBarry Smith PetscValidPointer(reason,2); 2316184914b5SBarry Smith *reason = snes->reason; 2317184914b5SBarry Smith PetscFunctionReturn(0); 2318184914b5SBarry Smith } 2319184914b5SBarry Smith 23204a2ae208SSatish Balay #undef __FUNCT__ 23214a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 2322c9005455SLois Curfman McInnes /*@ 2323c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 2324c9005455SLois Curfman McInnes 23253f9fe445SBarry Smith Logically Collective on SNES 2326fee21e36SBarry Smith 2327c7afd0dbSLois Curfman McInnes Input Parameters: 2328c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 23298c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 2330cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 2331758f92a0SBarry Smith . na - size of a and its 233264731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 2333758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 2334c7afd0dbSLois Curfman McInnes 2335308dcc3eSBarry Smith Notes: 2336308dcc3eSBarry Smith If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a 2337308dcc3eSBarry Smith default array of length 10000 is allocated. 2338308dcc3eSBarry Smith 2339c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 2340c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 2341c9005455SLois Curfman McInnes during the section of code that is being timed. 2342c9005455SLois Curfman McInnes 234336851e7fSLois Curfman McInnes Level: intermediate 234436851e7fSLois Curfman McInnes 2345c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2346758f92a0SBarry Smith 234708405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2348758f92a0SBarry Smith 2349c9005455SLois Curfman McInnes @*/ 23507087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset) 2351c9005455SLois Curfman McInnes { 2352308dcc3eSBarry Smith PetscErrorCode ierr; 2353308dcc3eSBarry Smith 23543a40ed3dSBarry Smith PetscFunctionBegin; 23550700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23564482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2357a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2358308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) { 2359308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2360308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr); 2361308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr); 2362308dcc3eSBarry Smith snes->conv_malloc = PETSC_TRUE; 2363308dcc3eSBarry Smith } 2364c9005455SLois Curfman McInnes snes->conv_hist = a; 2365758f92a0SBarry Smith snes->conv_hist_its = its; 2366758f92a0SBarry Smith snes->conv_hist_max = na; 2367a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2368758f92a0SBarry Smith snes->conv_hist_reset = reset; 2369758f92a0SBarry Smith PetscFunctionReturn(0); 2370758f92a0SBarry Smith } 2371758f92a0SBarry Smith 2372308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2373c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 2374c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 2375308dcc3eSBarry Smith EXTERN_C_BEGIN 2376308dcc3eSBarry Smith #undef __FUNCT__ 2377308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab" 2378308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 2379308dcc3eSBarry Smith { 2380308dcc3eSBarry Smith mxArray *mat; 2381308dcc3eSBarry Smith PetscInt i; 2382308dcc3eSBarry Smith PetscReal *ar; 2383308dcc3eSBarry Smith 2384308dcc3eSBarry Smith PetscFunctionBegin; 2385308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL); 2386308dcc3eSBarry Smith ar = (PetscReal*) mxGetData(mat); 2387308dcc3eSBarry Smith for (i=0; i<snes->conv_hist_len; i++) { 2388308dcc3eSBarry Smith ar[i] = snes->conv_hist[i]; 2389308dcc3eSBarry Smith } 2390308dcc3eSBarry Smith PetscFunctionReturn(mat); 2391308dcc3eSBarry Smith } 2392308dcc3eSBarry Smith EXTERN_C_END 2393308dcc3eSBarry Smith #endif 2394308dcc3eSBarry Smith 2395308dcc3eSBarry Smith 23964a2ae208SSatish Balay #undef __FUNCT__ 23974a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 23980c4c9dddSBarry Smith /*@C 2399758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 2400758f92a0SBarry Smith 24013f9fe445SBarry Smith Not Collective 2402758f92a0SBarry Smith 2403758f92a0SBarry Smith Input Parameter: 2404758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 2405758f92a0SBarry Smith 2406758f92a0SBarry Smith Output Parameters: 2407758f92a0SBarry Smith . a - array to hold history 2408758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 2409758f92a0SBarry Smith negative if not converged) for each solve. 2410758f92a0SBarry Smith - na - size of a and its 2411758f92a0SBarry Smith 2412758f92a0SBarry Smith Notes: 2413758f92a0SBarry Smith The calling sequence for this routine in Fortran is 2414758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 2415758f92a0SBarry Smith 2416758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 2417758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 2418758f92a0SBarry Smith during the section of code that is being timed. 2419758f92a0SBarry Smith 2420758f92a0SBarry Smith Level: intermediate 2421758f92a0SBarry Smith 2422758f92a0SBarry Smith .keywords: SNES, get, convergence, history 2423758f92a0SBarry Smith 2424758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 2425758f92a0SBarry Smith 2426758f92a0SBarry Smith @*/ 24277087cfbeSBarry Smith PetscErrorCode SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 2428758f92a0SBarry Smith { 2429758f92a0SBarry Smith PetscFunctionBegin; 24300700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2431758f92a0SBarry Smith if (a) *a = snes->conv_hist; 2432758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 2433758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 24343a40ed3dSBarry Smith PetscFunctionReturn(0); 2435c9005455SLois Curfman McInnes } 2436c9005455SLois Curfman McInnes 2437e74ef692SMatthew Knepley #undef __FUNCT__ 2438e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 2439ac226902SBarry Smith /*@C 244076b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 2441eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 24427e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 244376b2cf59SMatthew Knepley 24443f9fe445SBarry Smith Logically Collective on SNES 244576b2cf59SMatthew Knepley 244676b2cf59SMatthew Knepley Input Parameters: 244776b2cf59SMatthew Knepley . snes - The nonlinear solver context 244876b2cf59SMatthew Knepley . func - The function 244976b2cf59SMatthew Knepley 245076b2cf59SMatthew Knepley Calling sequence of func: 2451b5d30489SBarry Smith . func (SNES snes, PetscInt step); 245276b2cf59SMatthew Knepley 245376b2cf59SMatthew Knepley . step - The current step of the iteration 245476b2cf59SMatthew Knepley 2455fe97e370SBarry Smith Level: advanced 2456fe97e370SBarry Smith 2457fe97e370SBarry 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() 2458fe97e370SBarry Smith This is not used by most users. 245976b2cf59SMatthew Knepley 246076b2cf59SMatthew Knepley .keywords: SNES, update 2461b5d30489SBarry Smith 246285385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 246376b2cf59SMatthew Knepley @*/ 24647087cfbeSBarry Smith PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 246576b2cf59SMatthew Knepley { 246676b2cf59SMatthew Knepley PetscFunctionBegin; 24670700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 2468e7788613SBarry Smith snes->ops->update = func; 246976b2cf59SMatthew Knepley PetscFunctionReturn(0); 247076b2cf59SMatthew Knepley } 247176b2cf59SMatthew Knepley 2472e74ef692SMatthew Knepley #undef __FUNCT__ 2473e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 247476b2cf59SMatthew Knepley /*@ 247576b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 247676b2cf59SMatthew Knepley 247776b2cf59SMatthew Knepley Not collective 247876b2cf59SMatthew Knepley 247976b2cf59SMatthew Knepley Input Parameters: 248076b2cf59SMatthew Knepley . snes - The nonlinear solver context 248176b2cf59SMatthew Knepley . step - The current step of the iteration 248276b2cf59SMatthew Knepley 2483205452f4SMatthew Knepley Level: intermediate 2484205452f4SMatthew Knepley 248576b2cf59SMatthew Knepley .keywords: SNES, update 2486a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 248776b2cf59SMatthew Knepley @*/ 24887087cfbeSBarry Smith PetscErrorCode SNESDefaultUpdate(SNES snes, PetscInt step) 248976b2cf59SMatthew Knepley { 249076b2cf59SMatthew Knepley PetscFunctionBegin; 249176b2cf59SMatthew Knepley PetscFunctionReturn(0); 249276b2cf59SMatthew Knepley } 249376b2cf59SMatthew Knepley 24944a2ae208SSatish Balay #undef __FUNCT__ 24954a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 24969b94acceSBarry Smith /* 24979b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 24989b94acceSBarry Smith positive parameter delta. 24999b94acceSBarry Smith 25009b94acceSBarry Smith Input Parameters: 2501c7afd0dbSLois Curfman McInnes + snes - the SNES context 25029b94acceSBarry Smith . y - approximate solution of linear system 25039b94acceSBarry Smith . fnorm - 2-norm of current function 2504c7afd0dbSLois Curfman McInnes - delta - trust region size 25059b94acceSBarry Smith 25069b94acceSBarry Smith Output Parameters: 2507c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 25089b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 25099b94acceSBarry Smith region, and exceeds zero otherwise. 2510c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 25119b94acceSBarry Smith 25129b94acceSBarry Smith Note: 25134b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 25149b94acceSBarry Smith is set to be the maximum allowable step size. 25159b94acceSBarry Smith 25169b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 25179b94acceSBarry Smith */ 2518dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 25199b94acceSBarry Smith { 2520064f8208SBarry Smith PetscReal nrm; 2521ea709b57SSatish Balay PetscScalar cnorm; 2522dfbe8321SBarry Smith PetscErrorCode ierr; 25233a40ed3dSBarry Smith 25243a40ed3dSBarry Smith PetscFunctionBegin; 25250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 25260700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 2527c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 2528184914b5SBarry Smith 2529064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 2530064f8208SBarry Smith if (nrm > *delta) { 2531064f8208SBarry Smith nrm = *delta/nrm; 2532064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 2533064f8208SBarry Smith cnorm = nrm; 25342dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 25359b94acceSBarry Smith *ynorm = *delta; 25369b94acceSBarry Smith } else { 25379b94acceSBarry Smith *gpnorm = 0.0; 2538064f8208SBarry Smith *ynorm = nrm; 25399b94acceSBarry Smith } 25403a40ed3dSBarry Smith PetscFunctionReturn(0); 25419b94acceSBarry Smith } 25429b94acceSBarry Smith 25434a2ae208SSatish Balay #undef __FUNCT__ 25444a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 25456ce558aeSBarry Smith /*@C 2546f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 2547f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 25489b94acceSBarry Smith 2549c7afd0dbSLois Curfman McInnes Collective on SNES 2550c7afd0dbSLois Curfman McInnes 2551b2002411SLois Curfman McInnes Input Parameters: 2552c7afd0dbSLois Curfman McInnes + snes - the SNES context 2553f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 255485385478SLisandro Dalcin - x - the solution vector. 25559b94acceSBarry Smith 2556b2002411SLois Curfman McInnes Notes: 25578ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 25588ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 25598ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 25608ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 25618ddd3da0SLois Curfman McInnes 256236851e7fSLois Curfman McInnes Level: beginner 256336851e7fSLois Curfman McInnes 25649b94acceSBarry Smith .keywords: SNES, nonlinear, solve 25659b94acceSBarry Smith 256685385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 25679b94acceSBarry Smith @*/ 25687087cfbeSBarry Smith PetscErrorCode SNESSolve(SNES snes,Vec b,Vec x) 25699b94acceSBarry Smith { 2570dfbe8321SBarry Smith PetscErrorCode ierr; 2571ace3abfcSBarry Smith PetscBool flg; 2572eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 2573eabae89aSBarry Smith PetscViewer viewer; 2574efd51863SBarry Smith PetscInt grid; 2575052efed2SBarry Smith 25763a40ed3dSBarry Smith PetscFunctionBegin; 25770700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 25780700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2579f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 25800700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 258185385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 258285385478SLisandro Dalcin 2583efd51863SBarry Smith for (grid=0; grid<snes->gridsequence+1; grid++) { 2584efd51863SBarry Smith 258585385478SLisandro Dalcin /* set solution vector */ 2586efd51863SBarry Smith if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);} 25876bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 258885385478SLisandro Dalcin snes->vec_sol = x; 258985385478SLisandro Dalcin /* set afine vector if provided */ 259085385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 25916bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 259285385478SLisandro Dalcin snes->vec_rhs = b; 259385385478SLisandro Dalcin 259470e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 25953f149594SLisandro Dalcin 2596d25893d9SBarry Smith if (!grid && snes->ops->computeinitialguess) { 2597d25893d9SBarry Smith ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr); 2598d25893d9SBarry Smith } 2599d25893d9SBarry Smith 2600abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 260150ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2602d5e45103SBarry Smith 26033f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 26044936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 260585385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 26064936397dSBarry Smith if (snes->domainerror){ 26074936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 26084936397dSBarry Smith snes->domainerror = PETSC_FALSE; 26094936397dSBarry Smith } 261017186662SBarry Smith if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 26113f149594SLisandro Dalcin 26127adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2613eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 26147adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2615eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 26166bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 2617eabae89aSBarry Smith } 2618eabae89aSBarry Smith 261990d69ab7SBarry Smith flg = PETSC_FALSE; 2620acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr); 2621da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 26225968eb51SBarry Smith if (snes->printreason) { 26235968eb51SBarry Smith if (snes->reason > 0) { 26247adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 26255968eb51SBarry Smith } else { 26267adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 26275968eb51SBarry Smith } 26285968eb51SBarry Smith } 26295968eb51SBarry Smith 2630e113a28aSBarry Smith if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged"); 2631efd51863SBarry Smith if (grid < snes->gridsequence) { 2632efd51863SBarry Smith DM fine; 2633efd51863SBarry Smith Vec xnew; 2634efd51863SBarry Smith Mat interp; 2635efd51863SBarry Smith 2636efd51863SBarry Smith ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr); 2637efd51863SBarry Smith ierr = DMGetInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr); 2638efd51863SBarry Smith ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr); 2639efd51863SBarry Smith ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr); 2640efd51863SBarry Smith ierr = MatDestroy(&interp);CHKERRQ(ierr); 2641efd51863SBarry Smith x = xnew; 2642efd51863SBarry Smith 2643efd51863SBarry Smith ierr = SNESReset(snes);CHKERRQ(ierr); 2644efd51863SBarry Smith ierr = SNESSetDM(snes,fine);CHKERRQ(ierr); 2645efd51863SBarry Smith ierr = DMDestroy(&fine);CHKERRQ(ierr); 2646efd51863SBarry Smith } 2647efd51863SBarry Smith } 26483a40ed3dSBarry Smith PetscFunctionReturn(0); 26499b94acceSBarry Smith } 26509b94acceSBarry Smith 26519b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 26529b94acceSBarry Smith 26534a2ae208SSatish Balay #undef __FUNCT__ 26544a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 265582bf6240SBarry Smith /*@C 26564b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 26579b94acceSBarry Smith 2658fee21e36SBarry Smith Collective on SNES 2659fee21e36SBarry Smith 2660c7afd0dbSLois Curfman McInnes Input Parameters: 2661c7afd0dbSLois Curfman McInnes + snes - the SNES context 2662454a90a3SBarry Smith - type - a known method 2663c7afd0dbSLois Curfman McInnes 2664c7afd0dbSLois Curfman McInnes Options Database Key: 2665454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2666c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2667ae12b187SLois Curfman McInnes 26689b94acceSBarry Smith Notes: 2669e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 26704b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2671c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 26724b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2673c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 26749b94acceSBarry Smith 2675ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2676ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2677ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2678ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2679ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2680ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2681ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2682ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2683ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2684b0a32e0cSBarry Smith appropriate method. 268536851e7fSLois Curfman McInnes 268636851e7fSLois Curfman McInnes Level: intermediate 2687a703fe33SLois Curfman McInnes 2688454a90a3SBarry Smith .keywords: SNES, set, type 2689435da068SBarry Smith 2690435da068SBarry Smith .seealso: SNESType, SNESCreate() 2691435da068SBarry Smith 26929b94acceSBarry Smith @*/ 26937087cfbeSBarry Smith PetscErrorCode SNESSetType(SNES snes,const SNESType type) 26949b94acceSBarry Smith { 2695dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 2696ace3abfcSBarry Smith PetscBool match; 26973a40ed3dSBarry Smith 26983a40ed3dSBarry Smith PetscFunctionBegin; 26990700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 27004482741eSBarry Smith PetscValidCharPointer(type,2); 270182bf6240SBarry Smith 27026831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 27030f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 270492ff6ae8SBarry Smith 27054b91b6eaSBarry Smith ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 2706e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 270775396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 270875396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 270975396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 271075396ef9SLisandro Dalcin snes->ops->setup = 0; 271175396ef9SLisandro Dalcin snes->ops->solve = 0; 271275396ef9SLisandro Dalcin snes->ops->view = 0; 271375396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 271475396ef9SLisandro Dalcin snes->ops->destroy = 0; 271575396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 271675396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 2717454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 271803bfa161SLisandro Dalcin ierr = (*r)(snes);CHKERRQ(ierr); 27199fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS) 27209fb22e1aSBarry Smith if (PetscAMSPublishAll) { 27219fb22e1aSBarry Smith ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr); 27229fb22e1aSBarry Smith } 27239fb22e1aSBarry Smith #endif 27243a40ed3dSBarry Smith PetscFunctionReturn(0); 27259b94acceSBarry Smith } 27269b94acceSBarry Smith 2727a847f771SSatish Balay 27289b94acceSBarry Smith /* --------------------------------------------------------------------- */ 27294a2ae208SSatish Balay #undef __FUNCT__ 27304a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 273152baeb72SSatish Balay /*@ 27329b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2733f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 27349b94acceSBarry Smith 2735fee21e36SBarry Smith Not Collective 2736fee21e36SBarry Smith 273736851e7fSLois Curfman McInnes Level: advanced 273836851e7fSLois Curfman McInnes 27399b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 27409b94acceSBarry Smith 27419b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 27429b94acceSBarry Smith @*/ 27437087cfbeSBarry Smith PetscErrorCode SNESRegisterDestroy(void) 27449b94acceSBarry Smith { 2745dfbe8321SBarry Smith PetscErrorCode ierr; 274682bf6240SBarry Smith 27473a40ed3dSBarry Smith PetscFunctionBegin; 27481441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 27494c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 27503a40ed3dSBarry Smith PetscFunctionReturn(0); 27519b94acceSBarry Smith } 27529b94acceSBarry Smith 27534a2ae208SSatish Balay #undef __FUNCT__ 27544a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 27559b94acceSBarry Smith /*@C 27569a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 27579b94acceSBarry Smith 2758c7afd0dbSLois Curfman McInnes Not Collective 2759c7afd0dbSLois Curfman McInnes 27609b94acceSBarry Smith Input Parameter: 27614b0e389bSBarry Smith . snes - nonlinear solver context 27629b94acceSBarry Smith 27639b94acceSBarry Smith Output Parameter: 27643a7fca6bSBarry Smith . type - SNES method (a character string) 27659b94acceSBarry Smith 276636851e7fSLois Curfman McInnes Level: intermediate 276736851e7fSLois Curfman McInnes 2768454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 27699b94acceSBarry Smith @*/ 27707087cfbeSBarry Smith PetscErrorCode SNESGetType(SNES snes,const SNESType *type) 27719b94acceSBarry Smith { 27723a40ed3dSBarry Smith PetscFunctionBegin; 27730700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 27744482741eSBarry Smith PetscValidPointer(type,2); 27757adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 27763a40ed3dSBarry Smith PetscFunctionReturn(0); 27779b94acceSBarry Smith } 27789b94acceSBarry Smith 27794a2ae208SSatish Balay #undef __FUNCT__ 27804a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 278152baeb72SSatish Balay /*@ 27829b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 27839b94acceSBarry Smith stored. 27849b94acceSBarry Smith 2785c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2786c7afd0dbSLois Curfman McInnes 27879b94acceSBarry Smith Input Parameter: 27889b94acceSBarry Smith . snes - the SNES context 27899b94acceSBarry Smith 27909b94acceSBarry Smith Output Parameter: 27919b94acceSBarry Smith . x - the solution 27929b94acceSBarry Smith 279370e92668SMatthew Knepley Level: intermediate 279436851e7fSLois Curfman McInnes 27959b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 27969b94acceSBarry Smith 279785385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 27989b94acceSBarry Smith @*/ 27997087cfbeSBarry Smith PetscErrorCode SNESGetSolution(SNES snes,Vec *x) 28009b94acceSBarry Smith { 28013a40ed3dSBarry Smith PetscFunctionBegin; 28020700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28034482741eSBarry Smith PetscValidPointer(x,2); 280485385478SLisandro Dalcin *x = snes->vec_sol; 280570e92668SMatthew Knepley PetscFunctionReturn(0); 280670e92668SMatthew Knepley } 280770e92668SMatthew Knepley 280870e92668SMatthew Knepley #undef __FUNCT__ 28094a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 281052baeb72SSatish Balay /*@ 28119b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 28129b94acceSBarry Smith stored. 28139b94acceSBarry Smith 2814c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2815c7afd0dbSLois Curfman McInnes 28169b94acceSBarry Smith Input Parameter: 28179b94acceSBarry Smith . snes - the SNES context 28189b94acceSBarry Smith 28199b94acceSBarry Smith Output Parameter: 28209b94acceSBarry Smith . x - the solution update 28219b94acceSBarry Smith 282236851e7fSLois Curfman McInnes Level: advanced 282336851e7fSLois Curfman McInnes 28249b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 28259b94acceSBarry Smith 282685385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 28279b94acceSBarry Smith @*/ 28287087cfbeSBarry Smith PetscErrorCode SNESGetSolutionUpdate(SNES snes,Vec *x) 28299b94acceSBarry Smith { 28303a40ed3dSBarry Smith PetscFunctionBegin; 28310700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28324482741eSBarry Smith PetscValidPointer(x,2); 283385385478SLisandro Dalcin *x = snes->vec_sol_update; 28343a40ed3dSBarry Smith PetscFunctionReturn(0); 28359b94acceSBarry Smith } 28369b94acceSBarry Smith 28374a2ae208SSatish Balay #undef __FUNCT__ 28384a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 28399b94acceSBarry Smith /*@C 28403638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 28419b94acceSBarry Smith 2842c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2843c7afd0dbSLois Curfman McInnes 28449b94acceSBarry Smith Input Parameter: 28459b94acceSBarry Smith . snes - the SNES context 28469b94acceSBarry Smith 28479b94acceSBarry Smith Output Parameter: 28487bf4e008SBarry Smith + r - the function (or PETSC_NULL) 284970e92668SMatthew Knepley . func - the function (or PETSC_NULL) 285070e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 28519b94acceSBarry Smith 285236851e7fSLois Curfman McInnes Level: advanced 285336851e7fSLois Curfman McInnes 2854a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 28559b94acceSBarry Smith 28564b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 28579b94acceSBarry Smith @*/ 28587087cfbeSBarry Smith PetscErrorCode SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 28599b94acceSBarry Smith { 28603a40ed3dSBarry Smith PetscFunctionBegin; 28610700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 286285385478SLisandro Dalcin if (r) *r = snes->vec_func; 2863e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 286470e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 28653a40ed3dSBarry Smith PetscFunctionReturn(0); 28669b94acceSBarry Smith } 28679b94acceSBarry Smith 28684a2ae208SSatish Balay #undef __FUNCT__ 28694a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 28703c7409f5SSatish Balay /*@C 28713c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2872d850072dSLois Curfman McInnes SNES options in the database. 28733c7409f5SSatish Balay 28743f9fe445SBarry Smith Logically Collective on SNES 2875fee21e36SBarry Smith 2876c7afd0dbSLois Curfman McInnes Input Parameter: 2877c7afd0dbSLois Curfman McInnes + snes - the SNES context 2878c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2879c7afd0dbSLois Curfman McInnes 2880d850072dSLois Curfman McInnes Notes: 2881a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2882c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2883d850072dSLois Curfman McInnes 288436851e7fSLois Curfman McInnes Level: advanced 288536851e7fSLois Curfman McInnes 28863c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2887a86d99e1SLois Curfman McInnes 2888a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 28893c7409f5SSatish Balay @*/ 28907087cfbeSBarry Smith PetscErrorCode SNESSetOptionsPrefix(SNES snes,const char prefix[]) 28913c7409f5SSatish Balay { 2892dfbe8321SBarry Smith PetscErrorCode ierr; 28933c7409f5SSatish Balay 28943a40ed3dSBarry Smith PetscFunctionBegin; 28950700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2896639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 28971cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 289894b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 28993a40ed3dSBarry Smith PetscFunctionReturn(0); 29003c7409f5SSatish Balay } 29013c7409f5SSatish Balay 29024a2ae208SSatish Balay #undef __FUNCT__ 29034a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 29043c7409f5SSatish Balay /*@C 2905f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2906d850072dSLois Curfman McInnes SNES options in the database. 29073c7409f5SSatish Balay 29083f9fe445SBarry Smith Logically Collective on SNES 2909fee21e36SBarry Smith 2910c7afd0dbSLois Curfman McInnes Input Parameters: 2911c7afd0dbSLois Curfman McInnes + snes - the SNES context 2912c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2913c7afd0dbSLois Curfman McInnes 2914d850072dSLois Curfman McInnes Notes: 2915a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2916c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2917d850072dSLois Curfman McInnes 291836851e7fSLois Curfman McInnes Level: advanced 291936851e7fSLois Curfman McInnes 29203c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2921a86d99e1SLois Curfman McInnes 2922a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 29233c7409f5SSatish Balay @*/ 29247087cfbeSBarry Smith PetscErrorCode SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 29253c7409f5SSatish Balay { 2926dfbe8321SBarry Smith PetscErrorCode ierr; 29273c7409f5SSatish Balay 29283a40ed3dSBarry Smith PetscFunctionBegin; 29290700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2930639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 29311cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 293294b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 29333a40ed3dSBarry Smith PetscFunctionReturn(0); 29343c7409f5SSatish Balay } 29353c7409f5SSatish Balay 29364a2ae208SSatish Balay #undef __FUNCT__ 29374a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 29389ab63eb5SSatish Balay /*@C 29393c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 29403c7409f5SSatish Balay SNES options in the database. 29413c7409f5SSatish Balay 2942c7afd0dbSLois Curfman McInnes Not Collective 2943c7afd0dbSLois Curfman McInnes 29443c7409f5SSatish Balay Input Parameter: 29453c7409f5SSatish Balay . snes - the SNES context 29463c7409f5SSatish Balay 29473c7409f5SSatish Balay Output Parameter: 29483c7409f5SSatish Balay . prefix - pointer to the prefix string used 29493c7409f5SSatish Balay 29504ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 29519ab63eb5SSatish Balay sufficient length to hold the prefix. 29529ab63eb5SSatish Balay 295336851e7fSLois Curfman McInnes Level: advanced 295436851e7fSLois Curfman McInnes 29553c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2956a86d99e1SLois Curfman McInnes 2957a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 29583c7409f5SSatish Balay @*/ 29597087cfbeSBarry Smith PetscErrorCode SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 29603c7409f5SSatish Balay { 2961dfbe8321SBarry Smith PetscErrorCode ierr; 29623c7409f5SSatish Balay 29633a40ed3dSBarry Smith PetscFunctionBegin; 29640700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2965639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 29663a40ed3dSBarry Smith PetscFunctionReturn(0); 29673c7409f5SSatish Balay } 29683c7409f5SSatish Balay 2969b2002411SLois Curfman McInnes 29704a2ae208SSatish Balay #undef __FUNCT__ 29714a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 29723cea93caSBarry Smith /*@C 29733cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 29743cea93caSBarry Smith 29757f6c08e0SMatthew Knepley Level: advanced 29763cea93caSBarry Smith @*/ 29777087cfbeSBarry Smith PetscErrorCode SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2978b2002411SLois Curfman McInnes { 2979e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2980dfbe8321SBarry Smith PetscErrorCode ierr; 2981b2002411SLois Curfman McInnes 2982b2002411SLois Curfman McInnes PetscFunctionBegin; 2983b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2984c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2985b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2986b2002411SLois Curfman McInnes } 2987da9b6338SBarry Smith 2988da9b6338SBarry Smith #undef __FUNCT__ 2989da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 29907087cfbeSBarry Smith PetscErrorCode SNESTestLocalMin(SNES snes) 2991da9b6338SBarry Smith { 2992dfbe8321SBarry Smith PetscErrorCode ierr; 299377431f27SBarry Smith PetscInt N,i,j; 2994da9b6338SBarry Smith Vec u,uh,fh; 2995da9b6338SBarry Smith PetscScalar value; 2996da9b6338SBarry Smith PetscReal norm; 2997da9b6338SBarry Smith 2998da9b6338SBarry Smith PetscFunctionBegin; 2999da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 3000da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 3001da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 3002da9b6338SBarry Smith 3003da9b6338SBarry Smith /* currently only works for sequential */ 3004da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 3005da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 3006da9b6338SBarry Smith for (i=0; i<N; i++) { 3007da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 300877431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 3009da9b6338SBarry Smith for (j=-10; j<11; j++) { 3010ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 3011da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 30123ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 3013da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 301477431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 3015da9b6338SBarry Smith value = -value; 3016da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 3017da9b6338SBarry Smith } 3018da9b6338SBarry Smith } 30196bf464f9SBarry Smith ierr = VecDestroy(&uh);CHKERRQ(ierr); 30206bf464f9SBarry Smith ierr = VecDestroy(&fh);CHKERRQ(ierr); 3021da9b6338SBarry Smith PetscFunctionReturn(0); 3022da9b6338SBarry Smith } 302371f87433Sdalcinl 302471f87433Sdalcinl #undef __FUNCT__ 3025fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 302671f87433Sdalcinl /*@ 3027fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 302871f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 302971f87433Sdalcinl Newton method. 303071f87433Sdalcinl 30313f9fe445SBarry Smith Logically Collective on SNES 303271f87433Sdalcinl 303371f87433Sdalcinl Input Parameters: 303471f87433Sdalcinl + snes - SNES context 303571f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 303671f87433Sdalcinl 303764ba62caSBarry Smith Options Database: 303864ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 303964ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 304064ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 304164ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 304264ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 304364ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 304464ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 304564ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 304664ba62caSBarry Smith 304771f87433Sdalcinl Notes: 304871f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 304971f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 305071f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 305171f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 305271f87433Sdalcinl solver. 305371f87433Sdalcinl 305471f87433Sdalcinl Level: advanced 305571f87433Sdalcinl 305671f87433Sdalcinl Reference: 305771f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 305871f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 305971f87433Sdalcinl 306071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 306171f87433Sdalcinl 3062fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 306371f87433Sdalcinl @*/ 30647087cfbeSBarry Smith PetscErrorCode SNESKSPSetUseEW(SNES snes,PetscBool flag) 306571f87433Sdalcinl { 306671f87433Sdalcinl PetscFunctionBegin; 30670700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3068acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flag,2); 306971f87433Sdalcinl snes->ksp_ewconv = flag; 307071f87433Sdalcinl PetscFunctionReturn(0); 307171f87433Sdalcinl } 307271f87433Sdalcinl 307371f87433Sdalcinl #undef __FUNCT__ 3074fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 307571f87433Sdalcinl /*@ 3076fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 307771f87433Sdalcinl for computing relative tolerance for linear solvers within an 307871f87433Sdalcinl inexact Newton method. 307971f87433Sdalcinl 308071f87433Sdalcinl Not Collective 308171f87433Sdalcinl 308271f87433Sdalcinl Input Parameter: 308371f87433Sdalcinl . snes - SNES context 308471f87433Sdalcinl 308571f87433Sdalcinl Output Parameter: 308671f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 308771f87433Sdalcinl 308871f87433Sdalcinl Notes: 308971f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 309071f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 309171f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 309271f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 309371f87433Sdalcinl solver. 309471f87433Sdalcinl 309571f87433Sdalcinl Level: advanced 309671f87433Sdalcinl 309771f87433Sdalcinl Reference: 309871f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 309971f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 310071f87433Sdalcinl 310171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 310271f87433Sdalcinl 3103fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 310471f87433Sdalcinl @*/ 31057087cfbeSBarry Smith PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 310671f87433Sdalcinl { 310771f87433Sdalcinl PetscFunctionBegin; 31080700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 310971f87433Sdalcinl PetscValidPointer(flag,2); 311071f87433Sdalcinl *flag = snes->ksp_ewconv; 311171f87433Sdalcinl PetscFunctionReturn(0); 311271f87433Sdalcinl } 311371f87433Sdalcinl 311471f87433Sdalcinl #undef __FUNCT__ 3115fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 311671f87433Sdalcinl /*@ 3117fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 311871f87433Sdalcinl convergence criteria for the linear solvers within an inexact 311971f87433Sdalcinl Newton method. 312071f87433Sdalcinl 31213f9fe445SBarry Smith Logically Collective on SNES 312271f87433Sdalcinl 312371f87433Sdalcinl Input Parameters: 312471f87433Sdalcinl + snes - SNES context 312571f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 312671f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 312771f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 312871f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 312971f87433Sdalcinl (0 <= gamma2 <= 1) 313071f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 313171f87433Sdalcinl . alpha2 - power for safeguard 313271f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 313371f87433Sdalcinl 313471f87433Sdalcinl Note: 313571f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 313671f87433Sdalcinl 313771f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 313871f87433Sdalcinl 313971f87433Sdalcinl Level: advanced 314071f87433Sdalcinl 314171f87433Sdalcinl Reference: 314271f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 314371f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 314471f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 314571f87433Sdalcinl 314671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 314771f87433Sdalcinl 3148fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 314971f87433Sdalcinl @*/ 31507087cfbeSBarry Smith PetscErrorCode SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 315171f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 315271f87433Sdalcinl { 3153fa9f3622SBarry Smith SNESKSPEW *kctx; 315471f87433Sdalcinl PetscFunctionBegin; 31550700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3156fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3157e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 3158c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,version,2); 3159c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_0,3); 3160c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_max,4); 3161c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,gamma,5); 3162c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha,6); 3163c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha2,7); 3164c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,threshold,8); 316571f87433Sdalcinl 316671f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 316771f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 316871f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 316971f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 317071f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 317171f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 317271f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 317371f87433Sdalcinl 317471f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 3175e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 317671f87433Sdalcinl } 317771f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 3178e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 317971f87433Sdalcinl } 318071f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 3181e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 318271f87433Sdalcinl } 318371f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 3184e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 318571f87433Sdalcinl } 318671f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 3187e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 318871f87433Sdalcinl } 318971f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 3190e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 319171f87433Sdalcinl } 319271f87433Sdalcinl PetscFunctionReturn(0); 319371f87433Sdalcinl } 319471f87433Sdalcinl 319571f87433Sdalcinl #undef __FUNCT__ 3196fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 319771f87433Sdalcinl /*@ 3198fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 319971f87433Sdalcinl convergence criteria for the linear solvers within an inexact 320071f87433Sdalcinl Newton method. 320171f87433Sdalcinl 320271f87433Sdalcinl Not Collective 320371f87433Sdalcinl 320471f87433Sdalcinl Input Parameters: 320571f87433Sdalcinl snes - SNES context 320671f87433Sdalcinl 320771f87433Sdalcinl Output Parameters: 320871f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 320971f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 321071f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 321171f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 321271f87433Sdalcinl (0 <= gamma2 <= 1) 321371f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 321471f87433Sdalcinl . alpha2 - power for safeguard 321571f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 321671f87433Sdalcinl 321771f87433Sdalcinl Level: advanced 321871f87433Sdalcinl 321971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 322071f87433Sdalcinl 3221fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 322271f87433Sdalcinl @*/ 32237087cfbeSBarry Smith PetscErrorCode SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 322471f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 322571f87433Sdalcinl { 3226fa9f3622SBarry Smith SNESKSPEW *kctx; 322771f87433Sdalcinl PetscFunctionBegin; 32280700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3229fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3230e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 323171f87433Sdalcinl if(version) *version = kctx->version; 323271f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 323371f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 323471f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 323571f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 323671f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 323771f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 323871f87433Sdalcinl PetscFunctionReturn(0); 323971f87433Sdalcinl } 324071f87433Sdalcinl 324171f87433Sdalcinl #undef __FUNCT__ 3242fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 3243fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 324471f87433Sdalcinl { 324571f87433Sdalcinl PetscErrorCode ierr; 3246fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 324771f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 324871f87433Sdalcinl 324971f87433Sdalcinl PetscFunctionBegin; 3250e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 325171f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 325271f87433Sdalcinl rtol = kctx->rtol_0; 325371f87433Sdalcinl } else { 325471f87433Sdalcinl if (kctx->version == 1) { 325571f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 325671f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 325771f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 325871f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 325971f87433Sdalcinl } else if (kctx->version == 2) { 326071f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 326171f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 326271f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 326371f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 326471f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 326571f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 326671f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 326771f87433Sdalcinl stol = PetscMax(rtol,stol); 326871f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 326971f87433Sdalcinl /* safeguard: avoid oversolving */ 327071f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 327171f87433Sdalcinl stol = PetscMax(rtol,stol); 327271f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 3273e32f2f54SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 327471f87433Sdalcinl } 327571f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 327671f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 327771f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 327871f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 327971f87433Sdalcinl PetscFunctionReturn(0); 328071f87433Sdalcinl } 328171f87433Sdalcinl 328271f87433Sdalcinl #undef __FUNCT__ 3283fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 3284fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 328571f87433Sdalcinl { 328671f87433Sdalcinl PetscErrorCode ierr; 3287fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 328871f87433Sdalcinl PCSide pcside; 328971f87433Sdalcinl Vec lres; 329071f87433Sdalcinl 329171f87433Sdalcinl PetscFunctionBegin; 3292e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 329371f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 329471f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 329571f87433Sdalcinl if (kctx->version == 1) { 3296b037da10SBarry Smith ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr); 329771f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 329871f87433Sdalcinl /* KSP residual is true linear residual */ 329971f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 330071f87433Sdalcinl } else { 330171f87433Sdalcinl /* KSP residual is preconditioned residual */ 330271f87433Sdalcinl /* compute true linear residual norm */ 330371f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 330471f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 330571f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 330671f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 33076bf464f9SBarry Smith ierr = VecDestroy(&lres);CHKERRQ(ierr); 330871f87433Sdalcinl } 330971f87433Sdalcinl } 331071f87433Sdalcinl PetscFunctionReturn(0); 331171f87433Sdalcinl } 331271f87433Sdalcinl 331371f87433Sdalcinl #undef __FUNCT__ 331471f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 331571f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 331671f87433Sdalcinl { 331771f87433Sdalcinl PetscErrorCode ierr; 331871f87433Sdalcinl 331971f87433Sdalcinl PetscFunctionBegin; 3320fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 332171f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 3322fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 332371f87433Sdalcinl PetscFunctionReturn(0); 332471f87433Sdalcinl } 33256c699258SBarry Smith 33266c699258SBarry Smith #undef __FUNCT__ 33276c699258SBarry Smith #define __FUNCT__ "SNESSetDM" 33286c699258SBarry Smith /*@ 33296c699258SBarry Smith SNESSetDM - Sets the DM that may be used by some preconditioners 33306c699258SBarry Smith 33313f9fe445SBarry Smith Logically Collective on SNES 33326c699258SBarry Smith 33336c699258SBarry Smith Input Parameters: 33346c699258SBarry Smith + snes - the preconditioner context 33356c699258SBarry Smith - dm - the dm 33366c699258SBarry Smith 33376c699258SBarry Smith Level: intermediate 33386c699258SBarry Smith 33396c699258SBarry Smith 33406c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM() 33416c699258SBarry Smith @*/ 33427087cfbeSBarry Smith PetscErrorCode SNESSetDM(SNES snes,DM dm) 33436c699258SBarry Smith { 33446c699258SBarry Smith PetscErrorCode ierr; 3345345fed2cSBarry Smith KSP ksp; 33466c699258SBarry Smith 33476c699258SBarry Smith PetscFunctionBegin; 33480700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3349d0660788SBarry Smith if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);} 33506bf464f9SBarry Smith ierr = DMDestroy(&snes->dm);CHKERRQ(ierr); 33516c699258SBarry Smith snes->dm = dm; 3352345fed2cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 3353345fed2cSBarry Smith ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr); 3354f22f69f0SBarry Smith ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr); 33556c699258SBarry Smith PetscFunctionReturn(0); 33566c699258SBarry Smith } 33576c699258SBarry Smith 33586c699258SBarry Smith #undef __FUNCT__ 33596c699258SBarry Smith #define __FUNCT__ "SNESGetDM" 33606c699258SBarry Smith /*@ 33616c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 33626c699258SBarry Smith 33633f9fe445SBarry Smith Not Collective but DM obtained is parallel on SNES 33646c699258SBarry Smith 33656c699258SBarry Smith Input Parameter: 33666c699258SBarry Smith . snes - the preconditioner context 33676c699258SBarry Smith 33686c699258SBarry Smith Output Parameter: 33696c699258SBarry Smith . dm - the dm 33706c699258SBarry Smith 33716c699258SBarry Smith Level: intermediate 33726c699258SBarry Smith 33736c699258SBarry Smith 33746c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM() 33756c699258SBarry Smith @*/ 33767087cfbeSBarry Smith PetscErrorCode SNESGetDM(SNES snes,DM *dm) 33776c699258SBarry Smith { 33786c699258SBarry Smith PetscFunctionBegin; 33790700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 33806c699258SBarry Smith *dm = snes->dm; 33816c699258SBarry Smith PetscFunctionReturn(0); 33826c699258SBarry Smith } 33830807856dSBarry Smith 338469b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 3385c6db04a5SJed Brown #include <mex.h> 338669b4f73cSBarry Smith 33878f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 33888f6e6473SBarry Smith 33890807856dSBarry Smith #undef __FUNCT__ 33900807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab" 33910807856dSBarry Smith /* 33920807856dSBarry Smith SNESComputeFunction_Matlab - Calls the function that has been set with 33930807856dSBarry Smith SNESSetFunctionMatlab(). 33940807856dSBarry Smith 33950807856dSBarry Smith Collective on SNES 33960807856dSBarry Smith 33970807856dSBarry Smith Input Parameters: 33980807856dSBarry Smith + snes - the SNES context 33990807856dSBarry Smith - x - input vector 34000807856dSBarry Smith 34010807856dSBarry Smith Output Parameter: 34020807856dSBarry Smith . y - function vector, as set by SNESSetFunction() 34030807856dSBarry Smith 34040807856dSBarry Smith Notes: 34050807856dSBarry Smith SNESComputeFunction() is typically used within nonlinear solvers 34060807856dSBarry Smith implementations, so most users would not generally call this routine 34070807856dSBarry Smith themselves. 34080807856dSBarry Smith 34090807856dSBarry Smith Level: developer 34100807856dSBarry Smith 34110807856dSBarry Smith .keywords: SNES, nonlinear, compute, function 34120807856dSBarry Smith 34130807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 341461b2408cSBarry Smith */ 34157087cfbeSBarry Smith PetscErrorCode SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx) 34160807856dSBarry Smith { 3417e650e774SBarry Smith PetscErrorCode ierr; 34188f6e6473SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 34198f6e6473SBarry Smith int nlhs = 1,nrhs = 5; 34208f6e6473SBarry Smith mxArray *plhs[1],*prhs[5]; 342191621f2eSBarry Smith long long int lx = 0,ly = 0,ls = 0; 3422e650e774SBarry Smith 34230807856dSBarry Smith PetscFunctionBegin; 34240807856dSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 34250807856dSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 34260807856dSBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 34270807856dSBarry Smith PetscCheckSameComm(snes,1,x,2); 34280807856dSBarry Smith PetscCheckSameComm(snes,1,y,3); 34290807856dSBarry Smith 34300807856dSBarry Smith /* call Matlab function in ctx with arguments x and y */ 3431e650e774SBarry Smith 343291621f2eSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3433e650e774SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3434e650e774SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 343591621f2eSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 343691621f2eSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 343791621f2eSBarry Smith prhs[2] = mxCreateDoubleScalar((double)ly); 34388f6e6473SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 34398f6e6473SBarry Smith prhs[4] = sctx->ctx; 3440b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr); 3441e650e774SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3442e650e774SBarry Smith mxDestroyArray(prhs[0]); 3443e650e774SBarry Smith mxDestroyArray(prhs[1]); 3444e650e774SBarry Smith mxDestroyArray(prhs[2]); 34458f6e6473SBarry Smith mxDestroyArray(prhs[3]); 3446e650e774SBarry Smith mxDestroyArray(plhs[0]); 34470807856dSBarry Smith PetscFunctionReturn(0); 34480807856dSBarry Smith } 34490807856dSBarry Smith 34500807856dSBarry Smith 34510807856dSBarry Smith #undef __FUNCT__ 34520807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab" 345361b2408cSBarry Smith /* 34540807856dSBarry Smith SNESSetFunctionMatlab - Sets the function evaluation routine and function 34550807856dSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3456e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 34570807856dSBarry Smith 34580807856dSBarry Smith Logically Collective on SNES 34590807856dSBarry Smith 34600807856dSBarry Smith Input Parameters: 34610807856dSBarry Smith + snes - the SNES context 34620807856dSBarry Smith . r - vector to store function value 34630807856dSBarry Smith - func - function evaluation routine 34640807856dSBarry Smith 34650807856dSBarry Smith Calling sequence of func: 346661b2408cSBarry Smith $ func (SNES snes,Vec x,Vec f,void *ctx); 34670807856dSBarry Smith 34680807856dSBarry Smith 34690807856dSBarry Smith Notes: 34700807856dSBarry Smith The Newton-like methods typically solve linear systems of the form 34710807856dSBarry Smith $ f'(x) x = -f(x), 34720807856dSBarry Smith where f'(x) denotes the Jacobian matrix and f(x) is the function. 34730807856dSBarry Smith 34740807856dSBarry Smith Level: beginner 34750807856dSBarry Smith 34760807856dSBarry Smith .keywords: SNES, nonlinear, set, function 34770807856dSBarry Smith 34780807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 347961b2408cSBarry Smith */ 34807087cfbeSBarry Smith PetscErrorCode SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx) 34810807856dSBarry Smith { 34820807856dSBarry Smith PetscErrorCode ierr; 34838f6e6473SBarry Smith SNESMatlabContext *sctx; 34840807856dSBarry Smith 34850807856dSBarry Smith PetscFunctionBegin; 34868f6e6473SBarry Smith /* currently sctx is memory bleed */ 34878f6e6473SBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 34888f6e6473SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 34898f6e6473SBarry Smith /* 34908f6e6473SBarry Smith This should work, but it doesn't 34918f6e6473SBarry Smith sctx->ctx = ctx; 34928f6e6473SBarry Smith mexMakeArrayPersistent(sctx->ctx); 34938f6e6473SBarry Smith */ 34948f6e6473SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 34958f6e6473SBarry Smith ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr); 34960807856dSBarry Smith PetscFunctionReturn(0); 34970807856dSBarry Smith } 349869b4f73cSBarry Smith 349961b2408cSBarry Smith #undef __FUNCT__ 350061b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab" 350161b2408cSBarry Smith /* 350261b2408cSBarry Smith SNESComputeJacobian_Matlab - Calls the function that has been set with 350361b2408cSBarry Smith SNESSetJacobianMatlab(). 350461b2408cSBarry Smith 350561b2408cSBarry Smith Collective on SNES 350661b2408cSBarry Smith 350761b2408cSBarry Smith Input Parameters: 350861b2408cSBarry Smith + snes - the SNES context 350961b2408cSBarry Smith . x - input vector 351061b2408cSBarry Smith . A, B - the matrices 351161b2408cSBarry Smith - ctx - user context 351261b2408cSBarry Smith 351361b2408cSBarry Smith Output Parameter: 351461b2408cSBarry Smith . flag - structure of the matrix 351561b2408cSBarry Smith 351661b2408cSBarry Smith Level: developer 351761b2408cSBarry Smith 351861b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function 351961b2408cSBarry Smith 352061b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 352161b2408cSBarry Smith @*/ 35227087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx) 352361b2408cSBarry Smith { 352461b2408cSBarry Smith PetscErrorCode ierr; 352561b2408cSBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 352661b2408cSBarry Smith int nlhs = 2,nrhs = 6; 352761b2408cSBarry Smith mxArray *plhs[2],*prhs[6]; 352861b2408cSBarry Smith long long int lx = 0,lA = 0,ls = 0, lB = 0; 352961b2408cSBarry Smith 353061b2408cSBarry Smith PetscFunctionBegin; 353161b2408cSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 353261b2408cSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 353361b2408cSBarry Smith 353461b2408cSBarry Smith /* call Matlab function in ctx with arguments x and y */ 353561b2408cSBarry Smith 353661b2408cSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 353761b2408cSBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 353861b2408cSBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 353961b2408cSBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 354061b2408cSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 354161b2408cSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 354261b2408cSBarry Smith prhs[2] = mxCreateDoubleScalar((double)lA); 354361b2408cSBarry Smith prhs[3] = mxCreateDoubleScalar((double)lB); 354461b2408cSBarry Smith prhs[4] = mxCreateString(sctx->funcname); 354561b2408cSBarry Smith prhs[5] = sctx->ctx; 3546b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr); 354761b2408cSBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 354861b2408cSBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 354961b2408cSBarry Smith mxDestroyArray(prhs[0]); 355061b2408cSBarry Smith mxDestroyArray(prhs[1]); 355161b2408cSBarry Smith mxDestroyArray(prhs[2]); 355261b2408cSBarry Smith mxDestroyArray(prhs[3]); 355361b2408cSBarry Smith mxDestroyArray(prhs[4]); 355461b2408cSBarry Smith mxDestroyArray(plhs[0]); 355561b2408cSBarry Smith mxDestroyArray(plhs[1]); 355661b2408cSBarry Smith PetscFunctionReturn(0); 355761b2408cSBarry Smith } 355861b2408cSBarry Smith 355961b2408cSBarry Smith 356061b2408cSBarry Smith #undef __FUNCT__ 356161b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab" 356261b2408cSBarry Smith /* 356361b2408cSBarry Smith SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 356461b2408cSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 3565e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 356661b2408cSBarry Smith 356761b2408cSBarry Smith Logically Collective on SNES 356861b2408cSBarry Smith 356961b2408cSBarry Smith Input Parameters: 357061b2408cSBarry Smith + snes - the SNES context 357161b2408cSBarry Smith . A,B - Jacobian matrices 357261b2408cSBarry Smith . func - function evaluation routine 357361b2408cSBarry Smith - ctx - user context 357461b2408cSBarry Smith 357561b2408cSBarry Smith Calling sequence of func: 357661b2408cSBarry Smith $ flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx); 357761b2408cSBarry Smith 357861b2408cSBarry Smith 357961b2408cSBarry Smith Level: developer 358061b2408cSBarry Smith 358161b2408cSBarry Smith .keywords: SNES, nonlinear, set, function 358261b2408cSBarry Smith 358361b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 358461b2408cSBarry Smith */ 35857087cfbeSBarry Smith PetscErrorCode SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx) 358661b2408cSBarry Smith { 358761b2408cSBarry Smith PetscErrorCode ierr; 358861b2408cSBarry Smith SNESMatlabContext *sctx; 358961b2408cSBarry Smith 359061b2408cSBarry Smith PetscFunctionBegin; 359161b2408cSBarry Smith /* currently sctx is memory bleed */ 359261b2408cSBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 359361b2408cSBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 359461b2408cSBarry Smith /* 359561b2408cSBarry Smith This should work, but it doesn't 359661b2408cSBarry Smith sctx->ctx = ctx; 359761b2408cSBarry Smith mexMakeArrayPersistent(sctx->ctx); 359861b2408cSBarry Smith */ 359961b2408cSBarry Smith sctx->ctx = mxDuplicateArray(ctx); 360061b2408cSBarry Smith ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 360161b2408cSBarry Smith PetscFunctionReturn(0); 360261b2408cSBarry Smith } 360369b4f73cSBarry Smith 3604f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3605f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab" 3606f9eb7ae2SShri Abhyankar /* 3607f9eb7ae2SShri Abhyankar SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab(). 3608f9eb7ae2SShri Abhyankar 3609f9eb7ae2SShri Abhyankar Collective on SNES 3610f9eb7ae2SShri Abhyankar 3611f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction() 3612f9eb7ae2SShri Abhyankar @*/ 36137087cfbeSBarry Smith PetscErrorCode SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx) 3614f9eb7ae2SShri Abhyankar { 3615f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 361648f37e70SShri Abhyankar SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 3617f9eb7ae2SShri Abhyankar int nlhs = 1,nrhs = 6; 3618f9eb7ae2SShri Abhyankar mxArray *plhs[1],*prhs[6]; 3619f9eb7ae2SShri Abhyankar long long int lx = 0,ls = 0; 3620f9eb7ae2SShri Abhyankar Vec x=snes->vec_sol; 3621f9eb7ae2SShri Abhyankar 3622f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3623f9eb7ae2SShri Abhyankar PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3624f9eb7ae2SShri Abhyankar 3625f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 3626f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 3627f9eb7ae2SShri Abhyankar prhs[0] = mxCreateDoubleScalar((double)ls); 3628f9eb7ae2SShri Abhyankar prhs[1] = mxCreateDoubleScalar((double)it); 3629f9eb7ae2SShri Abhyankar prhs[2] = mxCreateDoubleScalar((double)fnorm); 3630f9eb7ae2SShri Abhyankar prhs[3] = mxCreateDoubleScalar((double)lx); 3631f9eb7ae2SShri Abhyankar prhs[4] = mxCreateString(sctx->funcname); 3632f9eb7ae2SShri Abhyankar prhs[5] = sctx->ctx; 3633f9eb7ae2SShri Abhyankar ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr); 3634f9eb7ae2SShri Abhyankar ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 3635f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[0]); 3636f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[1]); 3637f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[2]); 3638f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[3]); 3639f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[4]); 3640f9eb7ae2SShri Abhyankar mxDestroyArray(plhs[0]); 3641f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 3642f9eb7ae2SShri Abhyankar } 3643f9eb7ae2SShri Abhyankar 3644f9eb7ae2SShri Abhyankar 3645f9eb7ae2SShri Abhyankar #undef __FUNCT__ 3646f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab" 3647f9eb7ae2SShri Abhyankar /* 3648e3c5b3baSBarry Smith SNESMonitorSetMatlab - Sets the monitor function from MATLAB 3649f9eb7ae2SShri Abhyankar 3650f9eb7ae2SShri Abhyankar Level: developer 3651f9eb7ae2SShri Abhyankar 3652f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function 3653f9eb7ae2SShri Abhyankar 3654f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 3655f9eb7ae2SShri Abhyankar */ 36567087cfbeSBarry Smith PetscErrorCode SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx) 3657f9eb7ae2SShri Abhyankar { 3658f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 3659f9eb7ae2SShri Abhyankar SNESMatlabContext *sctx; 3660f9eb7ae2SShri Abhyankar 3661f9eb7ae2SShri Abhyankar PetscFunctionBegin; 3662f9eb7ae2SShri Abhyankar /* currently sctx is memory bleed */ 3663f9eb7ae2SShri Abhyankar ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 3664f9eb7ae2SShri Abhyankar ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 3665f9eb7ae2SShri Abhyankar /* 3666f9eb7ae2SShri Abhyankar This should work, but it doesn't 3667f9eb7ae2SShri Abhyankar sctx->ctx = ctx; 3668f9eb7ae2SShri Abhyankar mexMakeArrayPersistent(sctx->ctx); 3669f9eb7ae2SShri Abhyankar */ 3670f9eb7ae2SShri Abhyankar sctx->ctx = mxDuplicateArray(ctx); 3671f9eb7ae2SShri Abhyankar ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 3672f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 3673f9eb7ae2SShri Abhyankar } 3674f9eb7ae2SShri Abhyankar 367569b4f73cSBarry Smith #endif 3676