19b94acceSBarry Smith 2c6db04a5SJed Brown #include <private/snesimpl.h> /*I "petscsnes.h" I*/ 36cab3a1bSJed Brown #include <petscdmshell.h> /*I "petscdmshell.h" I*/ 49b94acceSBarry Smith 5ace3abfcSBarry Smith PetscBool SNESRegisterAllCalled = PETSC_FALSE; 68ba1e511SMatthew Knepley PetscFList SNESList = PETSC_NULL; 78ba1e511SMatthew Knepley 88ba1e511SMatthew Knepley /* Logging support */ 97087cfbeSBarry Smith PetscClassId SNES_CLASSID; 10*f1c6b773SPeter Brune PetscLogEvent SNES_Solve, SNES_FunctionEval, SNES_JacobianEval, SNES_GSEval; 11a09944afSBarry Smith 12a09944afSBarry Smith #undef __FUNCT__ 13cab2e9ccSBarry Smith #define __FUNCT__ "SNESDMComputeJacobian" 14cab2e9ccSBarry Smith /* 15cab2e9ccSBarry Smith Translates from a SNES call to a DM call in computing a Jacobian 16cab2e9ccSBarry Smith */ 17cab2e9ccSBarry Smith PetscErrorCode SNESDMComputeJacobian(SNES snes,Vec X,Mat *J,Mat *B,MatStructure *flag,void *ptr) 18cab2e9ccSBarry Smith { 19cab2e9ccSBarry Smith PetscErrorCode ierr; 20cab2e9ccSBarry Smith DM dm; 21cab2e9ccSBarry Smith 22cab2e9ccSBarry Smith PetscFunctionBegin; 23cab2e9ccSBarry Smith ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 24cab2e9ccSBarry Smith ierr = DMComputeJacobian(dm,X,*J,*B,flag);CHKERRQ(ierr); 25cab2e9ccSBarry Smith PetscFunctionReturn(0); 26cab2e9ccSBarry Smith } 27cab2e9ccSBarry Smith 28cab2e9ccSBarry Smith #undef __FUNCT__ 29e113a28aSBarry Smith #define __FUNCT__ "SNESSetErrorIfNotConverged" 30e113a28aSBarry Smith /*@ 31e113a28aSBarry Smith SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged. 32e113a28aSBarry Smith 333f9fe445SBarry Smith Logically Collective on SNES 34e113a28aSBarry Smith 35e113a28aSBarry Smith Input Parameters: 36e113a28aSBarry Smith + snes - iterative context obtained from SNESCreate() 37e113a28aSBarry Smith - flg - PETSC_TRUE indicates you want the error generated 38e113a28aSBarry Smith 39e113a28aSBarry Smith Options database keys: 40e113a28aSBarry Smith . -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false) 41e113a28aSBarry Smith 42e113a28aSBarry Smith Level: intermediate 43e113a28aSBarry Smith 44e113a28aSBarry Smith Notes: 45e113a28aSBarry Smith Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve() 46e113a28aSBarry Smith to determine if it has converged. 47e113a28aSBarry Smith 48e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero 49e113a28aSBarry Smith 50e113a28aSBarry Smith .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged() 51e113a28aSBarry Smith @*/ 527087cfbeSBarry Smith PetscErrorCode SNESSetErrorIfNotConverged(SNES snes,PetscBool flg) 53e113a28aSBarry Smith { 54e113a28aSBarry Smith PetscFunctionBegin; 55e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 56acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flg,2); 57e113a28aSBarry Smith snes->errorifnotconverged = flg; 58dd568438SSatish Balay 59e113a28aSBarry Smith PetscFunctionReturn(0); 60e113a28aSBarry Smith } 61e113a28aSBarry Smith 62e113a28aSBarry Smith #undef __FUNCT__ 63e113a28aSBarry Smith #define __FUNCT__ "SNESGetErrorIfNotConverged" 64e113a28aSBarry Smith /*@ 65e113a28aSBarry Smith SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge? 66e113a28aSBarry Smith 67e113a28aSBarry Smith Not Collective 68e113a28aSBarry Smith 69e113a28aSBarry Smith Input Parameter: 70e113a28aSBarry Smith . snes - iterative context obtained from SNESCreate() 71e113a28aSBarry Smith 72e113a28aSBarry Smith Output Parameter: 73e113a28aSBarry Smith . flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE 74e113a28aSBarry Smith 75e113a28aSBarry Smith Level: intermediate 76e113a28aSBarry Smith 77e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero 78e113a28aSBarry Smith 79e113a28aSBarry Smith .seealso: SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged() 80e113a28aSBarry Smith @*/ 817087cfbeSBarry Smith PetscErrorCode SNESGetErrorIfNotConverged(SNES snes,PetscBool *flag) 82e113a28aSBarry Smith { 83e113a28aSBarry Smith PetscFunctionBegin; 84e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 85e113a28aSBarry Smith PetscValidPointer(flag,2); 86e113a28aSBarry Smith *flag = snes->errorifnotconverged; 87e113a28aSBarry Smith PetscFunctionReturn(0); 88e113a28aSBarry Smith } 89e113a28aSBarry Smith 90e113a28aSBarry Smith #undef __FUNCT__ 914936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError" 92e725d27bSBarry Smith /*@ 934936397dSBarry Smith SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not 944936397dSBarry Smith in the functions domain. For example, negative pressure. 954936397dSBarry Smith 963f9fe445SBarry Smith Logically Collective on SNES 974936397dSBarry Smith 984936397dSBarry Smith Input Parameters: 996a388c36SPeter Brune . snes - the SNES context 1004936397dSBarry Smith 10128529972SSatish Balay Level: advanced 1024936397dSBarry Smith 1034936397dSBarry Smith .keywords: SNES, view 1044936397dSBarry Smith 1054936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction() 1064936397dSBarry Smith @*/ 1077087cfbeSBarry Smith PetscErrorCode SNESSetFunctionDomainError(SNES snes) 1084936397dSBarry Smith { 1094936397dSBarry Smith PetscFunctionBegin; 1100700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1114936397dSBarry Smith snes->domainerror = PETSC_TRUE; 1124936397dSBarry Smith PetscFunctionReturn(0); 1134936397dSBarry Smith } 1144936397dSBarry Smith 1156a388c36SPeter Brune 1166a388c36SPeter Brune #undef __FUNCT__ 1176a388c36SPeter Brune #define __FUNCT__ "SNESGetFunctionDomainError" 1186a388c36SPeter Brune /*@ 119c77b2880SPeter Brune SNESGetFunctionDomainError - Gets the status of the domain error after a call to SNESComputeFunction; 1206a388c36SPeter Brune 1216a388c36SPeter Brune Logically Collective on SNES 1226a388c36SPeter Brune 1236a388c36SPeter Brune Input Parameters: 1246a388c36SPeter Brune . snes - the SNES context 1256a388c36SPeter Brune 1266a388c36SPeter Brune Output Parameters: 1276a388c36SPeter Brune . domainerror Set to PETSC_TRUE if there's a domain error; PETSC_FALSE otherwise. 1286a388c36SPeter Brune 1296a388c36SPeter Brune Level: advanced 1306a388c36SPeter Brune 1316a388c36SPeter Brune .keywords: SNES, view 1326a388c36SPeter Brune 1336a388c36SPeter Brune .seealso: SNESSetFunctionDomainError, SNESComputeFunction() 1346a388c36SPeter Brune @*/ 1356a388c36SPeter Brune PetscErrorCode SNESGetFunctionDomainError(SNES snes, PetscBool *domainerror) 1366a388c36SPeter Brune { 1376a388c36SPeter Brune PetscFunctionBegin; 1386a388c36SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1396a388c36SPeter Brune PetscValidPointer(domainerror, 2); 1406a388c36SPeter Brune *domainerror = snes->domainerror; 1416a388c36SPeter Brune PetscFunctionReturn(0); 1426a388c36SPeter Brune } 1436a388c36SPeter Brune 1446a388c36SPeter Brune 1454936397dSBarry Smith #undef __FUNCT__ 1464a2ae208SSatish Balay #define __FUNCT__ "SNESView" 1477e2c5f70SBarry Smith /*@C 1489b94acceSBarry Smith SNESView - Prints the SNES data structure. 1499b94acceSBarry Smith 1504c49b128SBarry Smith Collective on SNES 151fee21e36SBarry Smith 152c7afd0dbSLois Curfman McInnes Input Parameters: 153c7afd0dbSLois Curfman McInnes + SNES - the SNES context 154c7afd0dbSLois Curfman McInnes - viewer - visualization context 155c7afd0dbSLois Curfman McInnes 1569b94acceSBarry Smith Options Database Key: 157c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 1589b94acceSBarry Smith 1599b94acceSBarry Smith Notes: 1609b94acceSBarry Smith The available visualization contexts include 161b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 162b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 163c8a8ba5cSLois Curfman McInnes output where only the first processor opens 164c8a8ba5cSLois Curfman McInnes the file. All other processors send their 165c8a8ba5cSLois Curfman McInnes data to the first processor to print. 1669b94acceSBarry Smith 1673e081fefSLois Curfman McInnes The user can open an alternative visualization context with 168b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 1699b94acceSBarry Smith 17036851e7fSLois Curfman McInnes Level: beginner 17136851e7fSLois Curfman McInnes 1729b94acceSBarry Smith .keywords: SNES, view 1739b94acceSBarry Smith 174b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 1759b94acceSBarry Smith @*/ 1767087cfbeSBarry Smith PetscErrorCode SNESView(SNES snes,PetscViewer viewer) 1779b94acceSBarry Smith { 178fa9f3622SBarry Smith SNESKSPEW *kctx; 179dfbe8321SBarry Smith PetscErrorCode ierr; 18094b7f48cSBarry Smith KSP ksp; 181ace3abfcSBarry Smith PetscBool iascii,isstring; 1829b94acceSBarry Smith 1833a40ed3dSBarry Smith PetscFunctionBegin; 1840700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1853050cee2SBarry Smith if (!viewer) { 1867adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr); 1873050cee2SBarry Smith } 1880700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 189c9780b6fSBarry Smith PetscCheckSameComm(snes,1,viewer,2); 19074679c65SBarry Smith 1912692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 1922692d6eeSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 19332077d6dSBarry Smith if (iascii) { 194317d6ea6SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer,"SNES Object");CHKERRQ(ierr); 195e7788613SBarry Smith if (snes->ops->view) { 196b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 197e7788613SBarry Smith ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 198b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1990ef38995SBarry Smith } 20077431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 201a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, solution=%G\n", 20270441072SBarry Smith snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr); 20377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr); 20477431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr); 2059b94acceSBarry Smith if (snes->ksp_ewconv) { 206fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 2079b94acceSBarry Smith if (kctx) { 20877431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr); 209a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 210a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 2119b94acceSBarry Smith } 2129b94acceSBarry Smith } 213eb1f6c34SBarry Smith if (snes->lagpreconditioner == -1) { 214eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is never rebuilt\n");CHKERRQ(ierr); 215eb1f6c34SBarry Smith } else if (snes->lagpreconditioner > 1) { 216eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr); 217eb1f6c34SBarry Smith } 218eb1f6c34SBarry Smith if (snes->lagjacobian == -1) { 219eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is never rebuilt\n");CHKERRQ(ierr); 220eb1f6c34SBarry Smith } else if (snes->lagjacobian > 1) { 22142f4f86dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is rebuilt every %D SNES iterations\n",snes->lagjacobian);CHKERRQ(ierr); 222eb1f6c34SBarry Smith } 2230f5bd95cSBarry Smith } else if (isstring) { 224317d6ea6SBarry Smith const char *type; 225454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 226b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 22719bcc07fSBarry Smith } 22842f4f86dSBarry Smith if (snes->pc && snes->usespc) { 2294a0c5b0cSMatthew G Knepley ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 2304a0c5b0cSMatthew G Knepley ierr = SNESView(snes->pc, viewer);CHKERRQ(ierr); 2314a0c5b0cSMatthew G Knepley ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2324a0c5b0cSMatthew G Knepley } 2332c155ee1SBarry Smith if (snes->usesksp) { 2342c155ee1SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 235b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 23694b7f48cSBarry Smith ierr = KSPView(ksp,viewer);CHKERRQ(ierr); 237b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 2382c155ee1SBarry Smith } 2399e764e56SPeter Brune ierr = PetscViewerASCIIPrintf(viewer, " line search variant: %s\n",((PetscObject)snes->linesearch)->type_name);CHKERRQ(ierr); 2403a40ed3dSBarry Smith PetscFunctionReturn(0); 2419b94acceSBarry Smith } 2429b94acceSBarry Smith 24376b2cf59SMatthew Knepley /* 24476b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 24576b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 24676b2cf59SMatthew Knepley */ 24776b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 248a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 2496849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 25076b2cf59SMatthew Knepley 251e74ef692SMatthew Knepley #undef __FUNCT__ 252e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker" 253ac226902SBarry Smith /*@C 25476b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 25576b2cf59SMatthew Knepley 25676b2cf59SMatthew Knepley Not Collective 25776b2cf59SMatthew Knepley 25876b2cf59SMatthew Knepley Input Parameter: 25976b2cf59SMatthew Knepley . snescheck - function that checks for options 26076b2cf59SMatthew Knepley 26176b2cf59SMatthew Knepley Level: developer 26276b2cf59SMatthew Knepley 26376b2cf59SMatthew Knepley .seealso: SNESSetFromOptions() 26476b2cf59SMatthew Knepley @*/ 2657087cfbeSBarry Smith PetscErrorCode SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 26676b2cf59SMatthew Knepley { 26776b2cf59SMatthew Knepley PetscFunctionBegin; 26876b2cf59SMatthew Knepley if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 269e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS); 27076b2cf59SMatthew Knepley } 27176b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 27276b2cf59SMatthew Knepley PetscFunctionReturn(0); 27376b2cf59SMatthew Knepley } 27476b2cf59SMatthew Knepley 2757087cfbeSBarry Smith extern PetscErrorCode SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 276aa3661deSLisandro Dalcin 277aa3661deSLisandro Dalcin #undef __FUNCT__ 278aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private" 279ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool hasOperator, PetscInt version) 280aa3661deSLisandro Dalcin { 281aa3661deSLisandro Dalcin Mat J; 282aa3661deSLisandro Dalcin KSP ksp; 283aa3661deSLisandro Dalcin PC pc; 284ace3abfcSBarry Smith PetscBool match; 285aa3661deSLisandro Dalcin PetscErrorCode ierr; 286aa3661deSLisandro Dalcin 287aa3661deSLisandro Dalcin PetscFunctionBegin; 2880700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 289aa3661deSLisandro Dalcin 29098613b67SLisandro Dalcin if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) { 29198613b67SLisandro Dalcin Mat A = snes->jacobian, B = snes->jacobian_pre; 29298613b67SLisandro Dalcin ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr); 29398613b67SLisandro Dalcin } 29498613b67SLisandro Dalcin 295aa3661deSLisandro Dalcin if (version == 1) { 296aa3661deSLisandro Dalcin ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 29798613b67SLisandro Dalcin ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 2989c6ac3b3SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 299aa3661deSLisandro Dalcin } else if (version == 2) { 300e32f2f54SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first"); 30182a7e548SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) 302aa3661deSLisandro Dalcin ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr); 303aa3661deSLisandro Dalcin #else 304e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)"); 305aa3661deSLisandro Dalcin #endif 306a8248277SBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2"); 307aa3661deSLisandro Dalcin 308aa3661deSLisandro Dalcin ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr); 309d3462f78SMatthew Knepley if (hasOperator) { 310aa3661deSLisandro Dalcin /* This version replaces the user provided Jacobian matrix with a 311aa3661deSLisandro Dalcin matrix-free version but still employs the user-provided preconditioner matrix. */ 312aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 313aa3661deSLisandro Dalcin } else { 314aa3661deSLisandro Dalcin /* This version replaces both the user-provided Jacobian and the user- 315aa3661deSLisandro Dalcin provided preconditioner matrix with the default matrix free version. */ 3166cab3a1bSJed Brown void *functx; 3176cab3a1bSJed Brown ierr = SNESGetFunction(snes,PETSC_NULL,PETSC_NULL,&functx);CHKERRQ(ierr); 3186cab3a1bSJed Brown ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,functx);CHKERRQ(ierr); 319aa3661deSLisandro Dalcin /* Force no preconditioner */ 320aa3661deSLisandro Dalcin ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 321aa3661deSLisandro Dalcin ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 322aa3661deSLisandro Dalcin ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr); 323aa3661deSLisandro Dalcin if (!match) { 324aa3661deSLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 325aa3661deSLisandro Dalcin ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 326aa3661deSLisandro Dalcin } 327aa3661deSLisandro Dalcin } 3286bf464f9SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 329aa3661deSLisandro Dalcin PetscFunctionReturn(0); 330aa3661deSLisandro Dalcin } 331aa3661deSLisandro Dalcin 3324a2ae208SSatish Balay #undef __FUNCT__ 3336cab3a1bSJed Brown #define __FUNCT__ "SNESSetUpMatrices" 3346cab3a1bSJed Brown /*@ 3356cab3a1bSJed Brown SNESSetUpMatrices - ensures that matrices are available for SNES, to be called by SNESSetUp_XXX() 3366cab3a1bSJed Brown 3376cab3a1bSJed Brown Collective 3386cab3a1bSJed Brown 3396cab3a1bSJed Brown Input Arguments: 3406cab3a1bSJed Brown . snes - snes to configure 3416cab3a1bSJed Brown 3426cab3a1bSJed Brown Level: developer 3436cab3a1bSJed Brown 3446cab3a1bSJed Brown .seealso: SNESSetUp() 3456cab3a1bSJed Brown @*/ 3466cab3a1bSJed Brown PetscErrorCode SNESSetUpMatrices(SNES snes) 3476cab3a1bSJed Brown { 3486cab3a1bSJed Brown PetscErrorCode ierr; 3496cab3a1bSJed Brown DM dm; 3506cab3a1bSJed Brown SNESDM sdm; 3516cab3a1bSJed Brown 3526cab3a1bSJed Brown PetscFunctionBegin; 3536cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 3546cab3a1bSJed Brown ierr = DMSNESGetContext(dm,&sdm);CHKERRQ(ierr); 3556cab3a1bSJed Brown if (!sdm->computejacobian && snes->dm) { 3566cab3a1bSJed Brown Mat J,B; 3576cab3a1bSJed Brown ierr = DMCreateMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr); 3586cab3a1bSJed Brown if (snes->mf_operator) { 3596cab3a1bSJed Brown ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 3606cab3a1bSJed Brown ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 3616cab3a1bSJed Brown ierr = MatSetFromOptions(J);CHKERRQ(ierr); 3626cab3a1bSJed Brown } else { 3636cab3a1bSJed Brown J = B; 3646cab3a1bSJed Brown ierr = PetscObjectReference((PetscObject)J);CHKERRQ(ierr); 3656cab3a1bSJed Brown } 3666cab3a1bSJed Brown ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,PETSC_NULL);CHKERRQ(ierr); 3676cab3a1bSJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 3686cab3a1bSJed Brown ierr = MatDestroy(&B);CHKERRQ(ierr); 3696cab3a1bSJed Brown } else if (!snes->jacobian && sdm->computejacobian == MatMFFDComputeJacobian) { 3706cab3a1bSJed Brown Mat J; 3716cab3a1bSJed Brown void *functx; 3726cab3a1bSJed Brown ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 3736cab3a1bSJed Brown ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 3746cab3a1bSJed Brown ierr = MatSetFromOptions(J);CHKERRQ(ierr); 3756cab3a1bSJed Brown ierr = SNESGetFunction(snes,PETSC_NULL,PETSC_NULL,&functx);CHKERRQ(ierr); 3766cab3a1bSJed Brown ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,functx);CHKERRQ(ierr); 3776cab3a1bSJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 3786cab3a1bSJed Brown } else if (snes->dm && snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) { 3796cab3a1bSJed Brown Mat J,B; 3806cab3a1bSJed Brown void *functx; 3816cab3a1bSJed Brown ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 3826cab3a1bSJed Brown ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 3836cab3a1bSJed Brown ierr = MatSetFromOptions(J);CHKERRQ(ierr); 3846cab3a1bSJed Brown ierr = DMCreateMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr); 3856cab3a1bSJed Brown ierr = SNESGetFunction(snes,PETSC_NULL,PETSC_NULL,&functx);CHKERRQ(ierr); 3866cab3a1bSJed Brown ierr = SNESSetJacobian(snes,J,B,SNESDMComputeJacobian,functx);CHKERRQ(ierr); 3876cab3a1bSJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 3886cab3a1bSJed Brown ierr = MatDestroy(&B);CHKERRQ(ierr); 3896cab3a1bSJed Brown } else if (snes->dm && !snes->jacobian_pre) { 3906cab3a1bSJed Brown Mat J,B; 3916cab3a1bSJed Brown J = snes->jacobian; 3926cab3a1bSJed Brown ierr = DMCreateMatrix(snes->dm,MATAIJ,&B);CHKERRQ(ierr); 3936cab3a1bSJed Brown ierr = SNESSetJacobian(snes,J?J:B,B,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 3946cab3a1bSJed Brown ierr = MatDestroy(&B);CHKERRQ(ierr); 3956cab3a1bSJed Brown } 3966cab3a1bSJed Brown PetscFunctionReturn(0); 3976cab3a1bSJed Brown } 3986cab3a1bSJed Brown 3996cab3a1bSJed Brown #undef __FUNCT__ 4004a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions" 4019b94acceSBarry Smith /*@ 40294b7f48cSBarry Smith SNESSetFromOptions - Sets various SNES and KSP parameters from user options. 4039b94acceSBarry Smith 404c7afd0dbSLois Curfman McInnes Collective on SNES 405c7afd0dbSLois Curfman McInnes 4069b94acceSBarry Smith Input Parameter: 4079b94acceSBarry Smith . snes - the SNES context 4089b94acceSBarry Smith 40936851e7fSLois Curfman McInnes Options Database Keys: 410ea630c6eSPeter Brune + -snes_type <type> - ls, tr, ngmres, ncg, richardson, qn, vi, fas 41182738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 41282738288SBarry Smith of the change in the solution between steps 41370441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 414b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 415b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 416b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 4174839bfe8SBarry Smith . -snes_max_fail <max_fail> - maximum number of line search failures allowed before stopping, default is none 418ddf469c8SBarry Smith . -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops 419a8054027SBarry Smith . -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild) 420e35cf81dSBarry Smith . -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild) 421b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 4222492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 42382738288SBarry Smith solver; hence iterations will continue until max_it 4241fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 42582738288SBarry Smith of convergence test 426e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 427e8105e01SRichard Katz filename given prints to stdout 428a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 429a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 430a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 431a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 432e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 4335968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 434fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 43582738288SBarry Smith 43682738288SBarry Smith Options Database for Eisenstat-Walker method: 437fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 4384b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 43936851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 44036851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 44136851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 44236851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 44336851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 44436851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 44582738288SBarry Smith 44611ca99fdSLois Curfman McInnes Notes: 44711ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 4480598bfebSBarry Smith the <A href="../../docs/manual.pdf#nameddest=ch_snes">SNES chapter of the users manual</A>. 44983e2fdc7SBarry Smith 45036851e7fSLois Curfman McInnes Level: beginner 45136851e7fSLois Curfman McInnes 4529b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 4539b94acceSBarry Smith 45469ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 4559b94acceSBarry Smith @*/ 4567087cfbeSBarry Smith PetscErrorCode SNESSetFromOptions(SNES snes) 4579b94acceSBarry Smith { 458872b6db9SPeter Brune PetscBool flg,mf,mf_operator,pcset; 459efd51863SBarry Smith PetscInt i,indx,lag,mf_version,grids; 460aa3661deSLisandro Dalcin MatStructure matflag; 46185385478SLisandro Dalcin const char *deft = SNESLS; 46285385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 46385385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 464e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 46551e86f29SPeter Brune const char *optionsprefix; 466649052a6SBarry Smith PetscViewer monviewer; 46785385478SLisandro Dalcin PetscErrorCode ierr; 4689b94acceSBarry Smith 4693a40ed3dSBarry Smith PetscFunctionBegin; 4700700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 471ca161407SBarry Smith 472186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 4733194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)snes);CHKERRQ(ierr); 4747adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 475b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 476d64ed03dSBarry Smith if (flg) { 477186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 4787adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 479186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 480d64ed03dSBarry Smith } 48190d69ab7SBarry Smith /* not used here, but called so will go into help messaage */ 482909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 48393c39befSBarry Smith 48457034d6fSHong Zhang ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 48557034d6fSHong Zhang ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 486186905e3SBarry Smith 48757034d6fSHong Zhang ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 488b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 489b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 49050ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 491ddf469c8SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr); 492acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,PETSC_NULL);CHKERRQ(ierr); 49385385478SLisandro Dalcin 494a8054027SBarry Smith ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr); 495a8054027SBarry Smith if (flg) { 496a8054027SBarry Smith ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr); 497a8054027SBarry Smith } 498e35cf81dSBarry Smith ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr); 499e35cf81dSBarry Smith if (flg) { 500e35cf81dSBarry Smith ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr); 501e35cf81dSBarry Smith } 502efd51863SBarry Smith ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr); 503efd51863SBarry Smith if (flg) { 504efd51863SBarry Smith ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr); 505efd51863SBarry Smith } 506a8054027SBarry Smith 50785385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 50885385478SLisandro Dalcin if (flg) { 50985385478SLisandro Dalcin switch (indx) { 5107f7931b9SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 5117f7931b9SBarry Smith case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 51285385478SLisandro Dalcin } 51385385478SLisandro Dalcin } 51485385478SLisandro Dalcin 515acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr); 516186905e3SBarry Smith 51785385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 51885385478SLisandro Dalcin 519acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 520186905e3SBarry Smith 521fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 522fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 523fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 524fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 525fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 526fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 527fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 528186905e3SBarry Smith 52990d69ab7SBarry Smith flg = PETSC_FALSE; 530acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 531a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 532eabae89aSBarry Smith 533a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 534e8105e01SRichard Katz if (flg) { 535649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr); 536649052a6SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 537e8105e01SRichard Katz } 538eabae89aSBarry Smith 539b271bb04SBarry Smith ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 540b271bb04SBarry Smith if (flg) { 541b271bb04SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr); 542b271bb04SBarry Smith } 543b271bb04SBarry Smith 544a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 545eabae89aSBarry Smith if (flg) { 546649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr); 547f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 548e8105e01SRichard Katz } 549eabae89aSBarry Smith 550a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 551eabae89aSBarry Smith if (flg) { 552649052a6SBarry Smith ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,monfilename,&monviewer);CHKERRQ(ierr); 553649052a6SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 554eabae89aSBarry Smith } 555eabae89aSBarry Smith 5565180491cSLisandro Dalcin ierr = PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 5575180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)snes,monfilename);CHKERRQ(ierr);} 5585180491cSLisandro Dalcin 55990d69ab7SBarry Smith flg = PETSC_FALSE; 560acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 561a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 56290d69ab7SBarry Smith flg = PETSC_FALSE; 563acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 564a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 56590d69ab7SBarry Smith flg = PETSC_FALSE; 566acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 567a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 56890d69ab7SBarry Smith flg = PETSC_FALSE; 569acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 570a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 57190d69ab7SBarry Smith flg = PETSC_FALSE; 572acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 573b271bb04SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 574e24b481bSBarry Smith 57590d69ab7SBarry Smith flg = PETSC_FALSE; 576acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 5774b27c08aSLois Curfman McInnes if (flg) { 5786cab3a1bSJed Brown void *functx; 5796cab3a1bSJed Brown ierr = SNESGetFunction(snes,PETSC_NULL,PETSC_NULL,&functx);CHKERRQ(ierr); 5806cab3a1bSJed Brown ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,functx);CHKERRQ(ierr); 581ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 5829b94acceSBarry Smith } 583639f9d9dSBarry Smith 584aa3661deSLisandro Dalcin mf = mf_operator = PETSC_FALSE; 585aa3661deSLisandro Dalcin flg = PETSC_FALSE; 586acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr); 587a8248277SBarry Smith if (flg && mf_operator) { 588a8248277SBarry Smith snes->mf_operator = PETSC_TRUE; 589a8248277SBarry Smith mf = PETSC_TRUE; 590a8248277SBarry Smith } 591aa3661deSLisandro Dalcin flg = PETSC_FALSE; 592acfcf0e5SJed Brown ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr); 593aa3661deSLisandro Dalcin if (!flg && mf_operator) mf = PETSC_TRUE; 594aa3661deSLisandro Dalcin mf_version = 1; 595aa3661deSLisandro Dalcin ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr); 596aa3661deSLisandro Dalcin 597d28543b3SPeter Brune 59889b92e6fSPeter Brune /* GS Options */ 59989b92e6fSPeter Brune ierr = PetscOptionsInt("-snes_gs_sweeps","Number of sweeps of GS to apply","SNESComputeGS",snes->gssweeps,&snes->gssweeps,PETSC_NULL);CHKERRQ(ierr); 60089b92e6fSPeter Brune 60176b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 60276b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 60376b2cf59SMatthew Knepley } 60476b2cf59SMatthew Knepley 605e7788613SBarry Smith if (snes->ops->setfromoptions) { 606e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 607639f9d9dSBarry Smith } 6085d973c19SBarry Smith 6095d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 6105d973c19SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject)snes);CHKERRQ(ierr); 611b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 6124bbc92c1SBarry Smith 613aa3661deSLisandro Dalcin if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); } 6141cee3971SBarry Smith 6151cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 616aa3661deSLisandro Dalcin ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag); 617aa3661deSLisandro Dalcin ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr); 61885385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 61993993e2dSLois Curfman McInnes 6209e764e56SPeter Brune if (!snes->linesearch) { 621*f1c6b773SPeter Brune ierr = SNESGetSNESLineSearch(snes, &snes->linesearch);CHKERRQ(ierr); 6229e764e56SPeter Brune } 623*f1c6b773SPeter Brune ierr = SNESLineSearchSetFromOptions(snes->linesearch);CHKERRQ(ierr); 6249e764e56SPeter Brune 62551e86f29SPeter Brune /* if someone has set the SNES PC type, create it. */ 62651e86f29SPeter Brune ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr); 62751e86f29SPeter Brune ierr = PetscOptionsHasName(optionsprefix, "-npc_snes_type", &pcset);CHKERRQ(ierr); 62851e86f29SPeter Brune if (pcset && (!snes->pc)) { 62951e86f29SPeter Brune ierr = SNESGetPC(snes, &snes->pc);CHKERRQ(ierr); 63051e86f29SPeter Brune } 6314a0c5b0cSMatthew G Knepley if (snes->pc) { 632fde0ff24SPeter Brune ierr = SNESSetOptionsPrefix(snes->pc, optionsprefix);CHKERRQ(ierr); 633fde0ff24SPeter Brune ierr = SNESAppendOptionsPrefix(snes->pc, "npc_");CHKERRQ(ierr); 6344a0c5b0cSMatthew G Knepley ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 6354a0c5b0cSMatthew G Knepley ierr = SNESSetFromOptions(snes->pc);CHKERRQ(ierr); 6364a0c5b0cSMatthew G Knepley } 6373a40ed3dSBarry Smith PetscFunctionReturn(0); 6389b94acceSBarry Smith } 6399b94acceSBarry Smith 640d25893d9SBarry Smith #undef __FUNCT__ 641d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeApplicationContext" 642d25893d9SBarry Smith /*@ 643d25893d9SBarry Smith SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for 644d25893d9SBarry Smith the nonlinear solvers. 645d25893d9SBarry Smith 646d25893d9SBarry Smith Logically Collective on SNES 647d25893d9SBarry Smith 648d25893d9SBarry Smith Input Parameters: 649d25893d9SBarry Smith + snes - the SNES context 650d25893d9SBarry Smith . compute - function to compute the context 651d25893d9SBarry Smith - destroy - function to destroy the context 652d25893d9SBarry Smith 653d25893d9SBarry Smith Level: intermediate 654d25893d9SBarry Smith 655d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context 656d25893d9SBarry Smith 657d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext() 658d25893d9SBarry Smith @*/ 659d25893d9SBarry Smith PetscErrorCode SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**)) 660d25893d9SBarry Smith { 661d25893d9SBarry Smith PetscFunctionBegin; 662d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 663d25893d9SBarry Smith snes->ops->usercompute = compute; 664d25893d9SBarry Smith snes->ops->userdestroy = destroy; 665d25893d9SBarry Smith PetscFunctionReturn(0); 666d25893d9SBarry Smith } 667a847f771SSatish Balay 6684a2ae208SSatish Balay #undef __FUNCT__ 6694a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 670b07ff414SBarry Smith /*@ 6719b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 6729b94acceSBarry Smith the nonlinear solvers. 6739b94acceSBarry Smith 6743f9fe445SBarry Smith Logically Collective on SNES 675fee21e36SBarry Smith 676c7afd0dbSLois Curfman McInnes Input Parameters: 677c7afd0dbSLois Curfman McInnes + snes - the SNES context 678c7afd0dbSLois Curfman McInnes - usrP - optional user context 679c7afd0dbSLois Curfman McInnes 68036851e7fSLois Curfman McInnes Level: intermediate 68136851e7fSLois Curfman McInnes 6829b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 6839b94acceSBarry Smith 684d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetApplicationContext() 6859b94acceSBarry Smith @*/ 6867087cfbeSBarry Smith PetscErrorCode SNESSetApplicationContext(SNES snes,void *usrP) 6879b94acceSBarry Smith { 6881b2093e4SBarry Smith PetscErrorCode ierr; 689b07ff414SBarry Smith KSP ksp; 6901b2093e4SBarry Smith 6913a40ed3dSBarry Smith PetscFunctionBegin; 6920700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 693b07ff414SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 694b07ff414SBarry Smith ierr = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr); 6959b94acceSBarry Smith snes->user = usrP; 6963a40ed3dSBarry Smith PetscFunctionReturn(0); 6979b94acceSBarry Smith } 69874679c65SBarry Smith 6994a2ae208SSatish Balay #undef __FUNCT__ 7004a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 701b07ff414SBarry Smith /*@ 7029b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 7039b94acceSBarry Smith nonlinear solvers. 7049b94acceSBarry Smith 705c7afd0dbSLois Curfman McInnes Not Collective 706c7afd0dbSLois Curfman McInnes 7079b94acceSBarry Smith Input Parameter: 7089b94acceSBarry Smith . snes - SNES context 7099b94acceSBarry Smith 7109b94acceSBarry Smith Output Parameter: 7119b94acceSBarry Smith . usrP - user context 7129b94acceSBarry Smith 71336851e7fSLois Curfman McInnes Level: intermediate 71436851e7fSLois Curfman McInnes 7159b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 7169b94acceSBarry Smith 7179b94acceSBarry Smith .seealso: SNESSetApplicationContext() 7189b94acceSBarry Smith @*/ 719e71120c6SJed Brown PetscErrorCode SNESGetApplicationContext(SNES snes,void *usrP) 7209b94acceSBarry Smith { 7213a40ed3dSBarry Smith PetscFunctionBegin; 7220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 723e71120c6SJed Brown *(void**)usrP = snes->user; 7243a40ed3dSBarry Smith PetscFunctionReturn(0); 7259b94acceSBarry Smith } 72674679c65SBarry Smith 7274a2ae208SSatish Balay #undef __FUNCT__ 7284a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 7299b94acceSBarry Smith /*@ 730c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 731c8228a4eSBarry Smith at this time. 7329b94acceSBarry Smith 733c7afd0dbSLois Curfman McInnes Not Collective 734c7afd0dbSLois Curfman McInnes 7359b94acceSBarry Smith Input Parameter: 7369b94acceSBarry Smith . snes - SNES context 7379b94acceSBarry Smith 7389b94acceSBarry Smith Output Parameter: 7399b94acceSBarry Smith . iter - iteration number 7409b94acceSBarry Smith 741c8228a4eSBarry Smith Notes: 742c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 743c8228a4eSBarry Smith 744c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 74508405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 74608405cd6SLois Curfman McInnes .vb 74708405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 74808405cd6SLois Curfman McInnes if (!(it % 2)) { 74908405cd6SLois Curfman McInnes [compute Jacobian here] 75008405cd6SLois Curfman McInnes } 75108405cd6SLois Curfman McInnes .ve 752c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 75308405cd6SLois Curfman McInnes recomputed every second SNES iteration. 754c8228a4eSBarry Smith 75536851e7fSLois Curfman McInnes Level: intermediate 75636851e7fSLois Curfman McInnes 7572b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 7582b668275SBarry Smith 759b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 7609b94acceSBarry Smith @*/ 7617087cfbeSBarry Smith PetscErrorCode SNESGetIterationNumber(SNES snes,PetscInt* iter) 7629b94acceSBarry Smith { 7633a40ed3dSBarry Smith PetscFunctionBegin; 7640700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7654482741eSBarry Smith PetscValidIntPointer(iter,2); 7669b94acceSBarry Smith *iter = snes->iter; 7673a40ed3dSBarry Smith PetscFunctionReturn(0); 7689b94acceSBarry Smith } 76974679c65SBarry Smith 7704a2ae208SSatish Balay #undef __FUNCT__ 771360c497dSPeter Brune #define __FUNCT__ "SNESSetIterationNumber" 772360c497dSPeter Brune /*@ 773360c497dSPeter Brune SNESSetIterationNumber - Sets the current iteration number. 774360c497dSPeter Brune 775360c497dSPeter Brune Not Collective 776360c497dSPeter Brune 777360c497dSPeter Brune Input Parameter: 778360c497dSPeter Brune . snes - SNES context 779360c497dSPeter Brune . iter - iteration number 780360c497dSPeter Brune 781360c497dSPeter Brune Level: developer 782360c497dSPeter Brune 783360c497dSPeter Brune .keywords: SNES, nonlinear, set, iteration, number, 784360c497dSPeter Brune 785360c497dSPeter Brune .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 786360c497dSPeter Brune @*/ 787360c497dSPeter Brune PetscErrorCode SNESSetIterationNumber(SNES snes,PetscInt iter) 788360c497dSPeter Brune { 789360c497dSPeter Brune PetscErrorCode ierr; 790360c497dSPeter Brune 791360c497dSPeter Brune PetscFunctionBegin; 792360c497dSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 793360c497dSPeter Brune ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 794360c497dSPeter Brune snes->iter = iter; 795360c497dSPeter Brune ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 796360c497dSPeter Brune PetscFunctionReturn(0); 797360c497dSPeter Brune } 798360c497dSPeter Brune 799360c497dSPeter Brune #undef __FUNCT__ 8004a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 8019b94acceSBarry Smith /*@ 8029b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 8039b94acceSBarry Smith with SNESSSetFunction(). 8049b94acceSBarry Smith 805c7afd0dbSLois Curfman McInnes Collective on SNES 806c7afd0dbSLois Curfman McInnes 8079b94acceSBarry Smith Input Parameter: 8089b94acceSBarry Smith . snes - SNES context 8099b94acceSBarry Smith 8109b94acceSBarry Smith Output Parameter: 8119b94acceSBarry Smith . fnorm - 2-norm of function 8129b94acceSBarry Smith 81336851e7fSLois Curfman McInnes Level: intermediate 81436851e7fSLois Curfman McInnes 8159b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 816a86d99e1SLois Curfman McInnes 817b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 8189b94acceSBarry Smith @*/ 8197087cfbeSBarry Smith PetscErrorCode SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 8209b94acceSBarry Smith { 8213a40ed3dSBarry Smith PetscFunctionBegin; 8220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8234482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 8249b94acceSBarry Smith *fnorm = snes->norm; 8253a40ed3dSBarry Smith PetscFunctionReturn(0); 8269b94acceSBarry Smith } 82774679c65SBarry Smith 828360c497dSPeter Brune 829360c497dSPeter Brune #undef __FUNCT__ 830360c497dSPeter Brune #define __FUNCT__ "SNESSetFunctionNorm" 831360c497dSPeter Brune /*@ 832360c497dSPeter Brune SNESSetFunctionNorm - Sets the 2-norm of the current function computed using VecNorm(). 833360c497dSPeter Brune 834360c497dSPeter Brune Collective on SNES 835360c497dSPeter Brune 836360c497dSPeter Brune Input Parameter: 837360c497dSPeter Brune . snes - SNES context 838360c497dSPeter Brune . fnorm - 2-norm of function 839360c497dSPeter Brune 840360c497dSPeter Brune Level: developer 841360c497dSPeter Brune 842360c497dSPeter Brune .keywords: SNES, nonlinear, set, function, norm 843360c497dSPeter Brune 844360c497dSPeter Brune .seealso: SNESSetFunction(), SNESSetIterationNumber(), VecNorm(). 845360c497dSPeter Brune @*/ 846360c497dSPeter Brune PetscErrorCode SNESSetFunctionNorm(SNES snes,PetscReal fnorm) 847360c497dSPeter Brune { 848360c497dSPeter Brune 849360c497dSPeter Brune PetscErrorCode ierr; 850360c497dSPeter Brune 851360c497dSPeter Brune PetscFunctionBegin; 852360c497dSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 853360c497dSPeter Brune ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 854360c497dSPeter Brune snes->norm = fnorm; 855360c497dSPeter Brune ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 856360c497dSPeter Brune PetscFunctionReturn(0); 857360c497dSPeter Brune } 858360c497dSPeter Brune 8594a2ae208SSatish Balay #undef __FUNCT__ 860b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 8619b94acceSBarry Smith /*@ 862b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 8639b94acceSBarry Smith attempted by the nonlinear solver. 8649b94acceSBarry Smith 865c7afd0dbSLois Curfman McInnes Not Collective 866c7afd0dbSLois Curfman McInnes 8679b94acceSBarry Smith Input Parameter: 8689b94acceSBarry Smith . snes - SNES context 8699b94acceSBarry Smith 8709b94acceSBarry Smith Output Parameter: 8719b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 8729b94acceSBarry Smith 873c96a6f78SLois Curfman McInnes Notes: 874c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 875c96a6f78SLois Curfman McInnes 87636851e7fSLois Curfman McInnes Level: intermediate 87736851e7fSLois Curfman McInnes 8789b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 87958ebbce7SBarry Smith 880e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 88158ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 8829b94acceSBarry Smith @*/ 8837087cfbeSBarry Smith PetscErrorCode SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 8849b94acceSBarry Smith { 8853a40ed3dSBarry Smith PetscFunctionBegin; 8860700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8874482741eSBarry Smith PetscValidIntPointer(nfails,2); 88850ffb88aSMatthew Knepley *nfails = snes->numFailures; 88950ffb88aSMatthew Knepley PetscFunctionReturn(0); 89050ffb88aSMatthew Knepley } 89150ffb88aSMatthew Knepley 89250ffb88aSMatthew Knepley #undef __FUNCT__ 893b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 89450ffb88aSMatthew Knepley /*@ 895b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 89650ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 89750ffb88aSMatthew Knepley 89850ffb88aSMatthew Knepley Not Collective 89950ffb88aSMatthew Knepley 90050ffb88aSMatthew Knepley Input Parameters: 90150ffb88aSMatthew Knepley + snes - SNES context 90250ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 90350ffb88aSMatthew Knepley 90450ffb88aSMatthew Knepley Level: intermediate 90550ffb88aSMatthew Knepley 90650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 90758ebbce7SBarry Smith 908e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 90958ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 91050ffb88aSMatthew Knepley @*/ 9117087cfbeSBarry Smith PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 91250ffb88aSMatthew Knepley { 91350ffb88aSMatthew Knepley PetscFunctionBegin; 9140700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 91550ffb88aSMatthew Knepley snes->maxFailures = maxFails; 91650ffb88aSMatthew Knepley PetscFunctionReturn(0); 91750ffb88aSMatthew Knepley } 91850ffb88aSMatthew Knepley 91950ffb88aSMatthew Knepley #undef __FUNCT__ 920b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 92150ffb88aSMatthew Knepley /*@ 922b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 92350ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 92450ffb88aSMatthew Knepley 92550ffb88aSMatthew Knepley Not Collective 92650ffb88aSMatthew Knepley 92750ffb88aSMatthew Knepley Input Parameter: 92850ffb88aSMatthew Knepley . snes - SNES context 92950ffb88aSMatthew Knepley 93050ffb88aSMatthew Knepley Output Parameter: 93150ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 93250ffb88aSMatthew Knepley 93350ffb88aSMatthew Knepley Level: intermediate 93450ffb88aSMatthew Knepley 93550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 93658ebbce7SBarry Smith 937e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 93858ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 93958ebbce7SBarry Smith 94050ffb88aSMatthew Knepley @*/ 9417087cfbeSBarry Smith PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 94250ffb88aSMatthew Knepley { 94350ffb88aSMatthew Knepley PetscFunctionBegin; 9440700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9454482741eSBarry Smith PetscValidIntPointer(maxFails,2); 94650ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 9473a40ed3dSBarry Smith PetscFunctionReturn(0); 9489b94acceSBarry Smith } 949a847f771SSatish Balay 9504a2ae208SSatish Balay #undef __FUNCT__ 9512541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 9522541af92SBarry Smith /*@ 9532541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 9542541af92SBarry Smith done by SNES. 9552541af92SBarry Smith 9562541af92SBarry Smith Not Collective 9572541af92SBarry Smith 9582541af92SBarry Smith Input Parameter: 9592541af92SBarry Smith . snes - SNES context 9602541af92SBarry Smith 9612541af92SBarry Smith Output Parameter: 9622541af92SBarry Smith . nfuncs - number of evaluations 9632541af92SBarry Smith 9642541af92SBarry Smith Level: intermediate 9652541af92SBarry Smith 9662541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 96758ebbce7SBarry Smith 968e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 9692541af92SBarry Smith @*/ 9707087cfbeSBarry Smith PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 9712541af92SBarry Smith { 9722541af92SBarry Smith PetscFunctionBegin; 9730700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9742541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 9752541af92SBarry Smith *nfuncs = snes->nfuncs; 9762541af92SBarry Smith PetscFunctionReturn(0); 9772541af92SBarry Smith } 9782541af92SBarry Smith 9792541af92SBarry Smith #undef __FUNCT__ 9803d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 9813d4c4710SBarry Smith /*@ 9823d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 9833d4c4710SBarry Smith linear solvers. 9843d4c4710SBarry Smith 9853d4c4710SBarry Smith Not Collective 9863d4c4710SBarry Smith 9873d4c4710SBarry Smith Input Parameter: 9883d4c4710SBarry Smith . snes - SNES context 9893d4c4710SBarry Smith 9903d4c4710SBarry Smith Output Parameter: 9913d4c4710SBarry Smith . nfails - number of failed solves 9923d4c4710SBarry Smith 9933d4c4710SBarry Smith Notes: 9943d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 9953d4c4710SBarry Smith 9963d4c4710SBarry Smith Level: intermediate 9973d4c4710SBarry Smith 9983d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 99958ebbce7SBarry Smith 1000e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 10013d4c4710SBarry Smith @*/ 10027087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 10033d4c4710SBarry Smith { 10043d4c4710SBarry Smith PetscFunctionBegin; 10050700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10063d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 10073d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 10083d4c4710SBarry Smith PetscFunctionReturn(0); 10093d4c4710SBarry Smith } 10103d4c4710SBarry Smith 10113d4c4710SBarry Smith #undef __FUNCT__ 10123d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 10133d4c4710SBarry Smith /*@ 10143d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 10153d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 10163d4c4710SBarry Smith 10173f9fe445SBarry Smith Logically Collective on SNES 10183d4c4710SBarry Smith 10193d4c4710SBarry Smith Input Parameters: 10203d4c4710SBarry Smith + snes - SNES context 10213d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 10223d4c4710SBarry Smith 10233d4c4710SBarry Smith Level: intermediate 10243d4c4710SBarry Smith 1025a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 10263d4c4710SBarry Smith 10273d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 10283d4c4710SBarry Smith 102958ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 10303d4c4710SBarry Smith @*/ 10317087cfbeSBarry Smith PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 10323d4c4710SBarry Smith { 10333d4c4710SBarry Smith PetscFunctionBegin; 10340700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1035c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxFails,2); 10363d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 10373d4c4710SBarry Smith PetscFunctionReturn(0); 10383d4c4710SBarry Smith } 10393d4c4710SBarry Smith 10403d4c4710SBarry Smith #undef __FUNCT__ 10413d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 10423d4c4710SBarry Smith /*@ 10433d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 10443d4c4710SBarry Smith are allowed before SNES terminates 10453d4c4710SBarry Smith 10463d4c4710SBarry Smith Not Collective 10473d4c4710SBarry Smith 10483d4c4710SBarry Smith Input Parameter: 10493d4c4710SBarry Smith . snes - SNES context 10503d4c4710SBarry Smith 10513d4c4710SBarry Smith Output Parameter: 10523d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 10533d4c4710SBarry Smith 10543d4c4710SBarry Smith Level: intermediate 10553d4c4710SBarry Smith 10563d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 10573d4c4710SBarry Smith 10583d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 10593d4c4710SBarry Smith 1060e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 10613d4c4710SBarry Smith @*/ 10627087cfbeSBarry Smith PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 10633d4c4710SBarry Smith { 10643d4c4710SBarry Smith PetscFunctionBegin; 10650700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10663d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 10673d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 10683d4c4710SBarry Smith PetscFunctionReturn(0); 10693d4c4710SBarry Smith } 10703d4c4710SBarry Smith 10713d4c4710SBarry Smith #undef __FUNCT__ 1072b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 1073c96a6f78SLois Curfman McInnes /*@ 1074b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 1075c96a6f78SLois Curfman McInnes used by the nonlinear solver. 1076c96a6f78SLois Curfman McInnes 1077c7afd0dbSLois Curfman McInnes Not Collective 1078c7afd0dbSLois Curfman McInnes 1079c96a6f78SLois Curfman McInnes Input Parameter: 1080c96a6f78SLois Curfman McInnes . snes - SNES context 1081c96a6f78SLois Curfman McInnes 1082c96a6f78SLois Curfman McInnes Output Parameter: 1083c96a6f78SLois Curfman McInnes . lits - number of linear iterations 1084c96a6f78SLois Curfman McInnes 1085c96a6f78SLois Curfman McInnes Notes: 1086c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 1087c96a6f78SLois Curfman McInnes 108836851e7fSLois Curfman McInnes Level: intermediate 108936851e7fSLois Curfman McInnes 1090c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 10912b668275SBarry Smith 10928c7482ecSBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 1093c96a6f78SLois Curfman McInnes @*/ 10947087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 1095c96a6f78SLois Curfman McInnes { 10963a40ed3dSBarry Smith PetscFunctionBegin; 10970700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10984482741eSBarry Smith PetscValidIntPointer(lits,2); 1099c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 11003a40ed3dSBarry Smith PetscFunctionReturn(0); 1101c96a6f78SLois Curfman McInnes } 1102c96a6f78SLois Curfman McInnes 11034a2ae208SSatish Balay #undef __FUNCT__ 110494b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 110552baeb72SSatish Balay /*@ 110694b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 11079b94acceSBarry Smith 110894b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 1109c7afd0dbSLois Curfman McInnes 11109b94acceSBarry Smith Input Parameter: 11119b94acceSBarry Smith . snes - the SNES context 11129b94acceSBarry Smith 11139b94acceSBarry Smith Output Parameter: 111494b7f48cSBarry Smith . ksp - the KSP context 11159b94acceSBarry Smith 11169b94acceSBarry Smith Notes: 111794b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 11189b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 11192999313aSBarry Smith PC contexts as well. 11209b94acceSBarry Smith 112136851e7fSLois Curfman McInnes Level: beginner 112236851e7fSLois Curfman McInnes 112394b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 11249b94acceSBarry Smith 11252999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 11269b94acceSBarry Smith @*/ 11277087cfbeSBarry Smith PetscErrorCode SNESGetKSP(SNES snes,KSP *ksp) 11289b94acceSBarry Smith { 11291cee3971SBarry Smith PetscErrorCode ierr; 11301cee3971SBarry Smith 11313a40ed3dSBarry Smith PetscFunctionBegin; 11320700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 11334482741eSBarry Smith PetscValidPointer(ksp,2); 11341cee3971SBarry Smith 11351cee3971SBarry Smith if (!snes->ksp) { 11361cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 11371cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 11381cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 11391cee3971SBarry Smith } 114094b7f48cSBarry Smith *ksp = snes->ksp; 11413a40ed3dSBarry Smith PetscFunctionReturn(0); 11429b94acceSBarry Smith } 114382bf6240SBarry Smith 11444a2ae208SSatish Balay #undef __FUNCT__ 11452999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 11462999313aSBarry Smith /*@ 11472999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 11482999313aSBarry Smith 11492999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 11502999313aSBarry Smith 11512999313aSBarry Smith Input Parameters: 11522999313aSBarry Smith + snes - the SNES context 11532999313aSBarry Smith - ksp - the KSP context 11542999313aSBarry Smith 11552999313aSBarry Smith Notes: 11562999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 11572999313aSBarry Smith so this routine is rarely needed. 11582999313aSBarry Smith 11592999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 11602999313aSBarry Smith decreased by one. 11612999313aSBarry Smith 11622999313aSBarry Smith Level: developer 11632999313aSBarry Smith 11642999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 11652999313aSBarry Smith 11662999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 11672999313aSBarry Smith @*/ 11687087cfbeSBarry Smith PetscErrorCode SNESSetKSP(SNES snes,KSP ksp) 11692999313aSBarry Smith { 11702999313aSBarry Smith PetscErrorCode ierr; 11712999313aSBarry Smith 11722999313aSBarry Smith PetscFunctionBegin; 11730700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 11740700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 11752999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 11767dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 1177906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 11782999313aSBarry Smith snes->ksp = ksp; 11792999313aSBarry Smith PetscFunctionReturn(0); 11802999313aSBarry Smith } 11812999313aSBarry Smith 11827adad957SLisandro Dalcin #if 0 11832999313aSBarry Smith #undef __FUNCT__ 11844a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 11856849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 1186e24b481bSBarry Smith { 1187e24b481bSBarry Smith PetscFunctionBegin; 1188e24b481bSBarry Smith PetscFunctionReturn(0); 1189e24b481bSBarry Smith } 11907adad957SLisandro Dalcin #endif 1191e24b481bSBarry Smith 11929b94acceSBarry Smith /* -----------------------------------------------------------*/ 11934a2ae208SSatish Balay #undef __FUNCT__ 11944a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 119552baeb72SSatish Balay /*@ 11969b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 11979b94acceSBarry Smith 1198c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 1199c7afd0dbSLois Curfman McInnes 1200c7afd0dbSLois Curfman McInnes Input Parameters: 1201906ed7ccSBarry Smith . comm - MPI communicator 12029b94acceSBarry Smith 12039b94acceSBarry Smith Output Parameter: 12049b94acceSBarry Smith . outsnes - the new SNES context 12059b94acceSBarry Smith 1206c7afd0dbSLois Curfman McInnes Options Database Keys: 1207c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 1208c7afd0dbSLois Curfman McInnes and no preconditioning matrix 1209c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 1210c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 1211c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 1212c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 1213c1f60f51SBarry Smith 121436851e7fSLois Curfman McInnes Level: beginner 121536851e7fSLois Curfman McInnes 12169b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 12179b94acceSBarry Smith 1218a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 1219a8054027SBarry Smith 12209b94acceSBarry Smith @*/ 12217087cfbeSBarry Smith PetscErrorCode SNESCreate(MPI_Comm comm,SNES *outsnes) 12229b94acceSBarry Smith { 1223dfbe8321SBarry Smith PetscErrorCode ierr; 12249b94acceSBarry Smith SNES snes; 1225fa9f3622SBarry Smith SNESKSPEW *kctx; 122637fcc0dbSBarry Smith 12273a40ed3dSBarry Smith PetscFunctionBegin; 1228ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 12298ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 12308ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 12318ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 12328ba1e511SMatthew Knepley #endif 12338ba1e511SMatthew Knepley 12343194b578SJed Brown ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 12357adad957SLisandro Dalcin 123685385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 12372c155ee1SBarry Smith snes->usesksp = PETSC_TRUE; 12389b94acceSBarry Smith snes->max_its = 50; 12399750a799SBarry Smith snes->max_funcs = 10000; 12409b94acceSBarry Smith snes->norm = 0.0; 1241b4874afaSBarry Smith snes->rtol = 1.e-8; 1242b4874afaSBarry Smith snes->ttol = 0.0; 124370441072SBarry Smith snes->abstol = 1.e-50; 12449b94acceSBarry Smith snes->xtol = 1.e-8; 12454b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 12469b94acceSBarry Smith snes->nfuncs = 0; 124750ffb88aSMatthew Knepley snes->numFailures = 0; 124850ffb88aSMatthew Knepley snes->maxFailures = 1; 12497a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1250e35cf81dSBarry Smith snes->lagjacobian = 1; 1251a8054027SBarry Smith snes->lagpreconditioner = 1; 1252639f9d9dSBarry Smith snes->numbermonitors = 0; 12539b94acceSBarry Smith snes->data = 0; 12544dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1255186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 12566f24a144SLois Curfman McInnes snes->nwork = 0; 125758c9b817SLisandro Dalcin snes->work = 0; 125858c9b817SLisandro Dalcin snes->nvwork = 0; 125958c9b817SLisandro Dalcin snes->vwork = 0; 1260758f92a0SBarry Smith snes->conv_hist_len = 0; 1261758f92a0SBarry Smith snes->conv_hist_max = 0; 1262758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 1263758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 1264758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1265184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 126689b92e6fSPeter Brune snes->gssweeps = 1; 12679b94acceSBarry Smith 12683d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 12693d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 12703d4c4710SBarry Smith 12719b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 127238f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 12739b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 12749b94acceSBarry Smith kctx->version = 2; 12759b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 12769b94acceSBarry Smith this was too large for some test cases */ 127775567043SBarry Smith kctx->rtol_last = 0.0; 12789b94acceSBarry Smith kctx->rtol_max = .9; 12799b94acceSBarry Smith kctx->gamma = 1.0; 128062d1f40fSBarry Smith kctx->alpha = .5*(1.0 + PetscSqrtReal(5.0)); 128171f87433Sdalcinl kctx->alpha2 = kctx->alpha; 12829b94acceSBarry Smith kctx->threshold = .1; 128375567043SBarry Smith kctx->lresid_last = 0.0; 128475567043SBarry Smith kctx->norm_last = 0.0; 12859b94acceSBarry Smith 12869b94acceSBarry Smith *outsnes = snes; 12873a40ed3dSBarry Smith PetscFunctionReturn(0); 12889b94acceSBarry Smith } 12899b94acceSBarry Smith 12904a2ae208SSatish Balay #undef __FUNCT__ 12914a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 12929b94acceSBarry Smith /*@C 12939b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 12949b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 12959b94acceSBarry Smith equations. 12969b94acceSBarry Smith 12973f9fe445SBarry Smith Logically Collective on SNES 1298fee21e36SBarry Smith 1299c7afd0dbSLois Curfman McInnes Input Parameters: 1300c7afd0dbSLois Curfman McInnes + snes - the SNES context 1301c7afd0dbSLois Curfman McInnes . r - vector to store function value 1302de044059SHong Zhang . func - function evaluation routine 1303c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1304c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 13059b94acceSBarry Smith 1306c7afd0dbSLois Curfman McInnes Calling sequence of func: 13078d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 1308c7afd0dbSLois Curfman McInnes 1309c586c404SJed Brown + snes - the SNES context 1310c586c404SJed Brown . x - state at which to evaluate residual 1311c586c404SJed Brown . f - vector to put residual 1312c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 13139b94acceSBarry Smith 13149b94acceSBarry Smith Notes: 13159b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 13169b94acceSBarry Smith $ f'(x) x = -f(x), 1317c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 13189b94acceSBarry Smith 131936851e7fSLois Curfman McInnes Level: beginner 132036851e7fSLois Curfman McInnes 13219b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 13229b94acceSBarry Smith 13238b0a5094SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetPicard() 13249b94acceSBarry Smith @*/ 13257087cfbeSBarry Smith PetscErrorCode SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 13269b94acceSBarry Smith { 132785385478SLisandro Dalcin PetscErrorCode ierr; 13286cab3a1bSJed Brown DM dm; 13296cab3a1bSJed Brown 13303a40ed3dSBarry Smith PetscFunctionBegin; 13310700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1332d2a683ecSLisandro Dalcin if (r) { 1333d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1334d2a683ecSLisandro Dalcin PetscCheckSameComm(snes,1,r,2); 133585385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 13366bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 133785385478SLisandro Dalcin snes->vec_func = r; 1338d2a683ecSLisandro Dalcin } 13396cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 13406cab3a1bSJed Brown ierr = DMSNESSetFunction(dm,func,ctx);CHKERRQ(ierr); 13413a40ed3dSBarry Smith PetscFunctionReturn(0); 13429b94acceSBarry Smith } 13439b94acceSBarry Smith 1344646217ecSPeter Brune 1345646217ecSPeter Brune #undef __FUNCT__ 1346646217ecSPeter Brune #define __FUNCT__ "SNESSetGS" 1347c79ef259SPeter Brune /*@C 1348c79ef259SPeter Brune SNESSetGS - Sets the user nonlinear Gauss-Seidel routine for 1349c79ef259SPeter Brune use with composed nonlinear solvers. 1350c79ef259SPeter Brune 1351c79ef259SPeter Brune Input Parameters: 1352c79ef259SPeter Brune + snes - the SNES context 1353c79ef259SPeter Brune . gsfunc - function evaluation routine 1354c79ef259SPeter Brune - ctx - [optional] user-defined context for private data for the 1355c79ef259SPeter Brune smoother evaluation routine (may be PETSC_NULL) 1356c79ef259SPeter Brune 1357c79ef259SPeter Brune Calling sequence of func: 1358c79ef259SPeter Brune $ func (SNES snes,Vec x,Vec b,void *ctx); 1359c79ef259SPeter Brune 1360c79ef259SPeter Brune + X - solution vector 1361c79ef259SPeter Brune . B - RHS vector 1362d28543b3SPeter Brune - ctx - optional user-defined Gauss-Seidel context 1363c79ef259SPeter Brune 1364c79ef259SPeter Brune Notes: 1365c79ef259SPeter Brune The GS routines are used by the composed nonlinear solver to generate 1366c79ef259SPeter Brune a problem appropriate update to the solution, particularly FAS. 1367c79ef259SPeter Brune 1368d28543b3SPeter Brune Level: intermediate 1369c79ef259SPeter Brune 1370d28543b3SPeter Brune .keywords: SNES, nonlinear, set, Gauss-Seidel 1371c79ef259SPeter Brune 1372c79ef259SPeter Brune .seealso: SNESGetFunction(), SNESComputeGS() 1373c79ef259SPeter Brune @*/ 13746cab3a1bSJed Brown PetscErrorCode SNESSetGS(SNES snes,PetscErrorCode (*gsfunc)(SNES,Vec,Vec,void*),void *ctx) 13756cab3a1bSJed Brown { 13766cab3a1bSJed Brown PetscErrorCode ierr; 13776cab3a1bSJed Brown DM dm; 13786cab3a1bSJed Brown 1379646217ecSPeter Brune PetscFunctionBegin; 13806cab3a1bSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13816cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 13826cab3a1bSJed Brown ierr = DMSNESSetGS(dm,gsfunc,ctx);CHKERRQ(ierr); 1383646217ecSPeter Brune PetscFunctionReturn(0); 1384646217ecSPeter Brune } 1385646217ecSPeter Brune 1386d25893d9SBarry Smith #undef __FUNCT__ 138789b92e6fSPeter Brune #define __FUNCT__ "SNESSetGSSweeps" 138889b92e6fSPeter Brune /*@ 138989b92e6fSPeter Brune SNESSetGSSweeps - Sets the number of sweeps of GS to use. 139089b92e6fSPeter Brune 139189b92e6fSPeter Brune Input Parameters: 139289b92e6fSPeter Brune + snes - the SNES context 139389b92e6fSPeter Brune - sweeps - the number of sweeps of GS to perform. 139489b92e6fSPeter Brune 139589b92e6fSPeter Brune Level: intermediate 139689b92e6fSPeter Brune 139789b92e6fSPeter Brune .keywords: SNES, nonlinear, set, Gauss-Siedel 139889b92e6fSPeter Brune 139989b92e6fSPeter Brune .seealso: SNESSetGS(), SNESGetGS(), SNESSetPC(), SNESGetGSSweeps() 140089b92e6fSPeter Brune @*/ 140189b92e6fSPeter Brune 140289b92e6fSPeter Brune PetscErrorCode SNESSetGSSweeps(SNES snes, PetscInt sweeps) { 140389b92e6fSPeter Brune PetscFunctionBegin; 140489b92e6fSPeter Brune snes->gssweeps = sweeps; 140589b92e6fSPeter Brune PetscFunctionReturn(0); 140689b92e6fSPeter Brune } 140789b92e6fSPeter Brune 140889b92e6fSPeter Brune 140989b92e6fSPeter Brune #undef __FUNCT__ 141089b92e6fSPeter Brune #define __FUNCT__ "SNESGetGSSweeps" 141189b92e6fSPeter Brune /*@ 141289b92e6fSPeter Brune SNESGetGSSweeps - Gets the number of sweeps GS will use. 141389b92e6fSPeter Brune 141489b92e6fSPeter Brune Input Parameters: 141589b92e6fSPeter Brune . snes - the SNES context 141689b92e6fSPeter Brune 141789b92e6fSPeter Brune Output Parameters: 141889b92e6fSPeter Brune . sweeps - the number of sweeps of GS to perform. 141989b92e6fSPeter Brune 142089b92e6fSPeter Brune Level: intermediate 142189b92e6fSPeter Brune 142289b92e6fSPeter Brune .keywords: SNES, nonlinear, set, Gauss-Siedel 142389b92e6fSPeter Brune 142489b92e6fSPeter Brune .seealso: SNESSetGS(), SNESGetGS(), SNESSetPC(), SNESSetGSSweeps() 142589b92e6fSPeter Brune @*/ 142689b92e6fSPeter Brune PetscErrorCode SNESGetGSSweeps(SNES snes, PetscInt * sweeps) { 142789b92e6fSPeter Brune PetscFunctionBegin; 142889b92e6fSPeter Brune *sweeps = snes->gssweeps; 142989b92e6fSPeter Brune PetscFunctionReturn(0); 143089b92e6fSPeter Brune } 143189b92e6fSPeter Brune 143289b92e6fSPeter Brune #undef __FUNCT__ 14338b0a5094SBarry Smith #define __FUNCT__ "SNESPicardComputeFunction" 14348b0a5094SBarry Smith PetscErrorCode SNESPicardComputeFunction(SNES snes,Vec x,Vec f,void *ctx) 14358b0a5094SBarry Smith { 14368b0a5094SBarry Smith PetscErrorCode ierr; 14376cab3a1bSJed Brown void *functx,*jacctx; 14386cab3a1bSJed Brown 14398b0a5094SBarry Smith PetscFunctionBegin; 14406cab3a1bSJed Brown ierr = SNESGetFunction(snes,PETSC_NULL,PETSC_NULL,&functx);CHKERRQ(ierr); 14416cab3a1bSJed Brown ierr = SNESGetJacobian(snes,PETSC_NULL,PETSC_NULL,PETSC_NULL,&jacctx);CHKERRQ(ierr); 14428b0a5094SBarry Smith /* A(x)*x - b(x) */ 14436cab3a1bSJed Brown ierr = (*snes->ops->computepjacobian)(snes,x,&snes->jacobian,&snes->jacobian_pre,&snes->matstruct,jacctx);CHKERRQ(ierr); 14446cab3a1bSJed Brown ierr = (*snes->ops->computepfunction)(snes,x,f,functx);CHKERRQ(ierr); 14458b0a5094SBarry Smith ierr = VecView(x,PETSC_VIEWER_BINARY_WORLD);CHKERRQ(ierr); 14468b0a5094SBarry Smith ierr = VecView(f,PETSC_VIEWER_BINARY_WORLD);CHKERRQ(ierr); 14478b0a5094SBarry Smith ierr = VecScale(f,-1.0);CHKERRQ(ierr); 14488b0a5094SBarry Smith ierr = MatMultAdd(snes->jacobian_pre,x,f,f);CHKERRQ(ierr); 14498b0a5094SBarry Smith PetscFunctionReturn(0); 14508b0a5094SBarry Smith } 14518b0a5094SBarry Smith 14528b0a5094SBarry Smith #undef __FUNCT__ 14538b0a5094SBarry Smith #define __FUNCT__ "SNESPicardComputeJacobian" 14548b0a5094SBarry Smith PetscErrorCode SNESPicardComputeJacobian(SNES snes,Vec x1,Mat *J,Mat *B,MatStructure *flag,void *ctx) 14558b0a5094SBarry Smith { 14568b0a5094SBarry Smith PetscFunctionBegin; 14578b0a5094SBarry Smith *flag = snes->matstruct; 14588b0a5094SBarry Smith PetscFunctionReturn(0); 14598b0a5094SBarry Smith } 14608b0a5094SBarry Smith 14618b0a5094SBarry Smith #undef __FUNCT__ 14628b0a5094SBarry Smith #define __FUNCT__ "SNESSetPicard" 14638b0a5094SBarry Smith /*@C 14640d04baf8SBarry Smith SNESSetPicard - Use SNES to solve the semilinear-system A(x) x = b(x) via a Picard type iteration (Picard linearization) 14658b0a5094SBarry Smith 14668b0a5094SBarry Smith Logically Collective on SNES 14678b0a5094SBarry Smith 14688b0a5094SBarry Smith Input Parameters: 14698b0a5094SBarry Smith + snes - the SNES context 14708b0a5094SBarry Smith . r - vector to store function value 14718b0a5094SBarry Smith . func - function evaluation routine 14728b0a5094SBarry Smith . jmat - normally the same as mat but you can pass another matrix for which you compute the Jacobian of A(x) x - b(x) (see jmat below) 14738b0a5094SBarry Smith . mat - matrix to store A 14748b0a5094SBarry Smith . mfunc - function to compute matrix value 14758b0a5094SBarry Smith - ctx - [optional] user-defined context for private data for the 14768b0a5094SBarry Smith function evaluation routine (may be PETSC_NULL) 14778b0a5094SBarry Smith 14788b0a5094SBarry Smith Calling sequence of func: 14798b0a5094SBarry Smith $ func (SNES snes,Vec x,Vec f,void *ctx); 14808b0a5094SBarry Smith 14818b0a5094SBarry Smith + f - function vector 14828b0a5094SBarry Smith - ctx - optional user-defined function context 14838b0a5094SBarry Smith 14848b0a5094SBarry Smith Calling sequence of mfunc: 14858b0a5094SBarry Smith $ mfunc (SNES snes,Vec x,Mat *jmat,Mat *mat,int *flag,void *ctx); 14868b0a5094SBarry Smith 14878b0a5094SBarry Smith + x - input vector 14888b0a5094SBarry Smith . jmat - Form Jacobian matrix of A(x) x - b(x) if available, not there is really no reason to use it in this way since then you can just use SNESSetJacobian(), 14898b0a5094SBarry Smith normally just pass mat in this location 14908b0a5094SBarry Smith . mat - form A(x) matrix 14918b0a5094SBarry Smith . flag - flag indicating information about the preconditioner matrix 14928b0a5094SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 14938b0a5094SBarry Smith - ctx - [optional] user-defined Jacobian context 14948b0a5094SBarry Smith 14958b0a5094SBarry Smith Notes: 14968b0a5094SBarry Smith One can call SNESSetPicard() or SNESSetFunction() (and possibly SNESSetJacobian()) but cannot call both 14978b0a5094SBarry Smith 14988b0a5094SBarry Smith $ Solves the equation A(x) x = b(x) via the defect correction algorithm A(x^{n}) (x^{n+1} - x^{n}) = b(x^{n}) - A(x^{n})x^{n} 14998b0a5094SBarry Smith $ Note that when an exact solver is used this corresponds to the "classic" Picard A(x^{n}) x^{n+1} = b(x^{n}) iteration. 15008b0a5094SBarry Smith 15018b0a5094SBarry Smith Run with -snes_mf_operator to solve the system with Newton's method using A(x^{n}) to construct the preconditioner. 15028b0a5094SBarry Smith 15030d04baf8SBarry Smith We implement the defect correction form of the Picard iteration because it converges much more generally when inexact linear solvers are used then 15040d04baf8SBarry Smith the direct Picard iteration A(x^n) x^{n+1} = b(x^n) 15058b0a5094SBarry Smith 15068b0a5094SBarry Smith There is some controversity over the definition of a Picard iteration for nonlinear systems but almost everyone agrees that it involves a linear solve and some 15078b0a5094SBarry Smith believe it is the iteration A(x^{n}) x^{n+1} = b(x^{n}) hence we use the name Picard. If anyone has an authoritative reference that defines the Picard iteration 15088b0a5094SBarry Smith different please contact us at petsc-dev@mcs.anl.gov and we'll have an entirely new argument :-). 15098b0a5094SBarry Smith 15108b0a5094SBarry Smith Level: beginner 15118b0a5094SBarry Smith 15128b0a5094SBarry Smith .keywords: SNES, nonlinear, set, function 15138b0a5094SBarry Smith 15140d04baf8SBarry Smith .seealso: SNESGetFunction(), SNESSetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESGetPicard(), SNESLineSearchPreCheckPicard() 15158b0a5094SBarry Smith @*/ 15168b0a5094SBarry Smith PetscErrorCode SNESSetPicard(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),Mat jmat, Mat mat, PetscErrorCode (*mfunc)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 15178b0a5094SBarry Smith { 15188b0a5094SBarry Smith PetscErrorCode ierr; 15198b0a5094SBarry Smith PetscFunctionBegin; 15208b0a5094SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15218b0a5094SBarry Smith snes->ops->computepfunction = func; 15228b0a5094SBarry Smith snes->ops->computepjacobian = mfunc; 15238b0a5094SBarry Smith ierr = SNESSetFunction(snes,r,SNESPicardComputeFunction,ctx);CHKERRQ(ierr); 15248b0a5094SBarry Smith ierr = SNESSetJacobian(snes,jmat,mat,SNESPicardComputeJacobian,ctx);CHKERRQ(ierr); 15258b0a5094SBarry Smith PetscFunctionReturn(0); 15268b0a5094SBarry Smith } 15278b0a5094SBarry Smith 15288b0a5094SBarry Smith #undef __FUNCT__ 1529d25893d9SBarry Smith #define __FUNCT__ "SNESSetComputeInitialGuess" 1530d25893d9SBarry Smith /*@C 1531d25893d9SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem 1532d25893d9SBarry Smith 1533d25893d9SBarry Smith Logically Collective on SNES 1534d25893d9SBarry Smith 1535d25893d9SBarry Smith Input Parameters: 1536d25893d9SBarry Smith + snes - the SNES context 1537d25893d9SBarry Smith . func - function evaluation routine 1538d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 1539d25893d9SBarry Smith function evaluation routine (may be PETSC_NULL) 1540d25893d9SBarry Smith 1541d25893d9SBarry Smith Calling sequence of func: 1542d25893d9SBarry Smith $ func (SNES snes,Vec x,void *ctx); 1543d25893d9SBarry Smith 1544d25893d9SBarry Smith . f - function vector 1545d25893d9SBarry Smith - ctx - optional user-defined function context 1546d25893d9SBarry Smith 1547d25893d9SBarry Smith Level: intermediate 1548d25893d9SBarry Smith 1549d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function 1550d25893d9SBarry Smith 1551d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 1552d25893d9SBarry Smith @*/ 1553d25893d9SBarry Smith PetscErrorCode SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx) 1554d25893d9SBarry Smith { 1555d25893d9SBarry Smith PetscFunctionBegin; 1556d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1557d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 1558d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 1559d25893d9SBarry Smith PetscFunctionReturn(0); 1560d25893d9SBarry Smith } 1561d25893d9SBarry Smith 15623ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 15633ab0aad5SBarry Smith #undef __FUNCT__ 15641096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 15651096aae1SMatthew Knepley /*@C 15661096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 15671096aae1SMatthew Knepley it assumes a zero right hand side. 15681096aae1SMatthew Knepley 15693f9fe445SBarry Smith Logically Collective on SNES 15701096aae1SMatthew Knepley 15711096aae1SMatthew Knepley Input Parameter: 15721096aae1SMatthew Knepley . snes - the SNES context 15731096aae1SMatthew Knepley 15741096aae1SMatthew Knepley Output Parameter: 1575bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 15761096aae1SMatthew Knepley 15771096aae1SMatthew Knepley Level: intermediate 15781096aae1SMatthew Knepley 15791096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 15801096aae1SMatthew Knepley 158185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 15821096aae1SMatthew Knepley @*/ 15837087cfbeSBarry Smith PetscErrorCode SNESGetRhs(SNES snes,Vec *rhs) 15841096aae1SMatthew Knepley { 15851096aae1SMatthew Knepley PetscFunctionBegin; 15860700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15871096aae1SMatthew Knepley PetscValidPointer(rhs,2); 158885385478SLisandro Dalcin *rhs = snes->vec_rhs; 15891096aae1SMatthew Knepley PetscFunctionReturn(0); 15901096aae1SMatthew Knepley } 15911096aae1SMatthew Knepley 15921096aae1SMatthew Knepley #undef __FUNCT__ 15934a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 15949b94acceSBarry Smith /*@ 159536851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 15969b94acceSBarry Smith SNESSetFunction(). 15979b94acceSBarry Smith 1598c7afd0dbSLois Curfman McInnes Collective on SNES 1599c7afd0dbSLois Curfman McInnes 16009b94acceSBarry Smith Input Parameters: 1601c7afd0dbSLois Curfman McInnes + snes - the SNES context 1602c7afd0dbSLois Curfman McInnes - x - input vector 16039b94acceSBarry Smith 16049b94acceSBarry Smith Output Parameter: 16053638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 16069b94acceSBarry Smith 16071bffabb2SLois Curfman McInnes Notes: 160836851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 160936851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 161036851e7fSLois Curfman McInnes themselves. 161136851e7fSLois Curfman McInnes 161236851e7fSLois Curfman McInnes Level: developer 161336851e7fSLois Curfman McInnes 16149b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 16159b94acceSBarry Smith 1616a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 16179b94acceSBarry Smith @*/ 16187087cfbeSBarry Smith PetscErrorCode SNESComputeFunction(SNES snes,Vec x,Vec y) 16199b94acceSBarry Smith { 1620dfbe8321SBarry Smith PetscErrorCode ierr; 16216cab3a1bSJed Brown DM dm; 16226cab3a1bSJed Brown SNESDM sdm; 16239b94acceSBarry Smith 16243a40ed3dSBarry Smith PetscFunctionBegin; 16250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 16260700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 16270700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 1628c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1629c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 16304ec14c9cSBarry Smith VecValidValues(x,2,PETSC_TRUE); 1631184914b5SBarry Smith 16326cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 16336cab3a1bSJed Brown ierr = DMSNESGetContext(dm,&sdm);CHKERRQ(ierr); 1634d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 16356cab3a1bSJed Brown if (sdm->computefunction) { 1636d64ed03dSBarry Smith PetscStackPush("SNES user function"); 16376cab3a1bSJed Brown ierr = (*sdm->computefunction)(snes,x,y,sdm->functionctx);CHKERRQ(ierr); 1638d64ed03dSBarry Smith PetscStackPop; 163973250ac0SBarry Smith } else if (snes->dm) { 1640644e2e5bSBarry Smith ierr = DMComputeFunction(snes->dm,x,y);CHKERRQ(ierr); 1641c90fad12SPeter Brune } else if (snes->vec_rhs) { 1642c90fad12SPeter Brune ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 1643644e2e5bSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve()."); 164485385478SLisandro Dalcin if (snes->vec_rhs) { 164585385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 16463ab0aad5SBarry Smith } 1647ae3c334cSLois Curfman McInnes snes->nfuncs++; 1648d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 16494ec14c9cSBarry Smith VecValidValues(y,3,PETSC_FALSE); 16503a40ed3dSBarry Smith PetscFunctionReturn(0); 16519b94acceSBarry Smith } 16529b94acceSBarry Smith 16534a2ae208SSatish Balay #undef __FUNCT__ 1654646217ecSPeter Brune #define __FUNCT__ "SNESComputeGS" 1655c79ef259SPeter Brune /*@ 1656c79ef259SPeter Brune SNESComputeGS - Calls the Gauss-Seidel function that has been set with 1657c79ef259SPeter Brune SNESSetGS(). 1658c79ef259SPeter Brune 1659c79ef259SPeter Brune Collective on SNES 1660c79ef259SPeter Brune 1661c79ef259SPeter Brune Input Parameters: 1662c79ef259SPeter Brune + snes - the SNES context 1663c79ef259SPeter Brune . x - input vector 1664c79ef259SPeter Brune - b - rhs vector 1665c79ef259SPeter Brune 1666c79ef259SPeter Brune Output Parameter: 1667c79ef259SPeter Brune . x - new solution vector 1668c79ef259SPeter Brune 1669c79ef259SPeter Brune Notes: 1670c79ef259SPeter Brune SNESComputeGS() is typically used within composed nonlinear solver 1671c79ef259SPeter Brune implementations, so most users would not generally call this routine 1672c79ef259SPeter Brune themselves. 1673c79ef259SPeter Brune 1674c79ef259SPeter Brune Level: developer 1675c79ef259SPeter Brune 1676c79ef259SPeter Brune .keywords: SNES, nonlinear, compute, function 1677c79ef259SPeter Brune 1678c79ef259SPeter Brune .seealso: SNESSetGS(), SNESComputeFunction() 1679c79ef259SPeter Brune @*/ 1680646217ecSPeter Brune PetscErrorCode SNESComputeGS(SNES snes,Vec b,Vec x) 1681646217ecSPeter Brune { 1682646217ecSPeter Brune PetscErrorCode ierr; 168389b92e6fSPeter Brune PetscInt i; 16846cab3a1bSJed Brown DM dm; 16856cab3a1bSJed Brown SNESDM sdm; 1686646217ecSPeter Brune 1687646217ecSPeter Brune PetscFunctionBegin; 1688646217ecSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1689646217ecSPeter Brune PetscValidHeaderSpecific(x,VEC_CLASSID,2); 1690646217ecSPeter Brune if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,3); 1691646217ecSPeter Brune PetscCheckSameComm(snes,1,x,2); 1692646217ecSPeter Brune if(b) PetscCheckSameComm(snes,1,b,3); 16934ec14c9cSBarry Smith if (b) VecValidValues(b,2,PETSC_TRUE); 1694701cf23dSPeter Brune ierr = PetscLogEventBegin(SNES_GSEval,snes,x,b,0);CHKERRQ(ierr); 16956cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 16966cab3a1bSJed Brown ierr = DMSNESGetContext(dm,&sdm);CHKERRQ(ierr); 16976cab3a1bSJed Brown if (sdm->computegs) { 169889b92e6fSPeter Brune for (i = 0; i < snes->gssweeps; i++) { 1699646217ecSPeter Brune PetscStackPush("SNES user GS"); 17006cab3a1bSJed Brown ierr = (*sdm->computegs)(snes,x,b,sdm->gsctx);CHKERRQ(ierr); 1701646217ecSPeter Brune PetscStackPop; 170289b92e6fSPeter Brune } 1703646217ecSPeter Brune } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetGS() before SNESComputeGS(), likely called from SNESSolve()."); 1704701cf23dSPeter Brune ierr = PetscLogEventEnd(SNES_GSEval,snes,x,b,0);CHKERRQ(ierr); 17054ec14c9cSBarry Smith VecValidValues(x,3,PETSC_FALSE); 1706646217ecSPeter Brune PetscFunctionReturn(0); 1707646217ecSPeter Brune } 1708646217ecSPeter Brune 1709646217ecSPeter Brune 1710646217ecSPeter Brune #undef __FUNCT__ 17114a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 171262fef451SLois Curfman McInnes /*@ 171362fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 171462fef451SLois Curfman McInnes set with SNESSetJacobian(). 171562fef451SLois Curfman McInnes 1716c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1717c7afd0dbSLois Curfman McInnes 171862fef451SLois Curfman McInnes Input Parameters: 1719c7afd0dbSLois Curfman McInnes + snes - the SNES context 1720c7afd0dbSLois Curfman McInnes - x - input vector 172162fef451SLois Curfman McInnes 172262fef451SLois Curfman McInnes Output Parameters: 1723c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 172462fef451SLois Curfman McInnes . B - optional preconditioning matrix 17252b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1726fee21e36SBarry Smith 1727e35cf81dSBarry Smith Options Database Keys: 1728e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1729693365a8SJed Brown . -snes_lag_jacobian <lag> 1730693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 1731693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 1732693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 17334c30e9fbSJed Brown . -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 1734c01495d3SJed Brown . -snes_compare_coloring - Compute the finite differece Jacobian using coloring and display norms of difference 1735c01495d3SJed Brown . -snes_compare_coloring_display - Compute the finite differece Jacobian using coloring and display verbose differences 1736c01495d3SJed Brown . -snes_compare_coloring_threshold - Display only those matrix entries that differ by more than a given threshold 1737c01495d3SJed Brown . -snes_compare_coloring_threshold_atol - Absolute tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 1738c01495d3SJed Brown . -snes_compare_coloring_threshold_rtol - Relative tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 17394c30e9fbSJed Brown . -snes_compare_coloring_draw - Compute the finite differece Jacobian using coloring and draw differences 1740c01495d3SJed Brown - -snes_compare_coloring_draw_contour - Compute the finite differece Jacobian using coloring and show contours of matrices and differences 1741c01495d3SJed Brown 1742e35cf81dSBarry Smith 174362fef451SLois Curfman McInnes Notes: 174462fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 174562fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 174662fef451SLois Curfman McInnes 174794b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1748dc5a77f8SLois Curfman McInnes flag parameter. 174962fef451SLois Curfman McInnes 175036851e7fSLois Curfman McInnes Level: developer 175136851e7fSLois Curfman McInnes 175262fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 175362fef451SLois Curfman McInnes 1754e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 175562fef451SLois Curfman McInnes @*/ 17567087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 17579b94acceSBarry Smith { 1758dfbe8321SBarry Smith PetscErrorCode ierr; 1759ace3abfcSBarry Smith PetscBool flag; 17606cab3a1bSJed Brown DM dm; 17616cab3a1bSJed Brown SNESDM sdm; 17623a40ed3dSBarry Smith 17633a40ed3dSBarry Smith PetscFunctionBegin; 17640700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 17650700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 17664482741eSBarry Smith PetscValidPointer(flg,5); 1767c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 17684ec14c9cSBarry Smith VecValidValues(X,2,PETSC_TRUE); 17696cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 17706cab3a1bSJed Brown ierr = DMSNESGetContext(dm,&sdm);CHKERRQ(ierr); 17716cab3a1bSJed Brown if (!sdm->computejacobian) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_USER,"Must call SNESSetJacobian(), DMSNESSetJacobian(), DMDASNESSetJacobianLocal(), etc"); 1772ebd3b9afSBarry Smith 1773ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1774ebd3b9afSBarry Smith 1775fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1776fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1777fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1778fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1779e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1780e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1781ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1782ebd3b9afSBarry Smith if (flag) { 1783ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1784ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1785ebd3b9afSBarry Smith } 1786e35cf81dSBarry Smith PetscFunctionReturn(0); 1787e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1788e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1789e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1790ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1791ebd3b9afSBarry Smith if (flag) { 1792ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1793ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1794ebd3b9afSBarry Smith } 1795e35cf81dSBarry Smith PetscFunctionReturn(0); 1796e35cf81dSBarry Smith } 1797e35cf81dSBarry Smith 1798c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1799e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1800d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 18016cab3a1bSJed Brown ierr = (*sdm->computejacobian)(snes,X,A,B,flg,sdm->jacobianctx);CHKERRQ(ierr); 1802d64ed03dSBarry Smith PetscStackPop; 1803d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1804a8054027SBarry Smith 18053b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 18063b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 18073b4f5425SBarry Smith snes->lagpreconditioner = -1; 18083b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1809a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1810a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1811a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1812a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1813a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1814a8054027SBarry Smith } 1815a8054027SBarry Smith 18166d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 18170700a824SBarry Smith /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 18180700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,4); */ 1819693365a8SJed Brown { 1820693365a8SJed Brown PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE; 1821693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit",&flag,PETSC_NULL);CHKERRQ(ierr); 1822693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 1823693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 1824693365a8SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_operator",&flag_operator,PETSC_NULL);CHKERRQ(ierr); 1825693365a8SJed Brown if (flag || flag_draw || flag_contour) { 1826693365a8SJed Brown Mat Bexp_mine = PETSC_NULL,Bexp,FDexp; 1827693365a8SJed Brown MatStructure mstruct; 1828693365a8SJed Brown PetscViewer vdraw,vstdout; 18296b3a5b13SJed Brown PetscBool flg; 1830693365a8SJed Brown if (flag_operator) { 1831693365a8SJed Brown ierr = MatComputeExplicitOperator(*A,&Bexp_mine);CHKERRQ(ierr); 1832693365a8SJed Brown Bexp = Bexp_mine; 1833693365a8SJed Brown } else { 1834693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 1835693365a8SJed Brown ierr = PetscTypeCompareAny((PetscObject)*B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr); 1836693365a8SJed Brown if (flg) Bexp = *B; 1837693365a8SJed Brown else { 1838693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 1839693365a8SJed Brown ierr = MatComputeExplicitOperator(*B,&Bexp_mine);CHKERRQ(ierr); 1840693365a8SJed Brown Bexp = Bexp_mine; 1841693365a8SJed Brown } 1842693365a8SJed Brown } 1843693365a8SJed Brown ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr); 1844693365a8SJed Brown ierr = SNESDefaultComputeJacobian(snes,X,&FDexp,&FDexp,&mstruct,NULL);CHKERRQ(ierr); 1845693365a8SJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 1846693365a8SJed Brown if (flag_draw || flag_contour) { 1847693365a8SJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 1848693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 1849693365a8SJed Brown } else vdraw = PETSC_NULL; 1850693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator?"Jacobian":"preconditioning Jacobian");CHKERRQ(ierr); 1851693365a8SJed Brown if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);} 1852693365a8SJed Brown if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);} 1853693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr); 1854693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1855693365a8SJed Brown if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);} 1856693365a8SJed Brown ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 1857693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr); 1858693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 1859693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 1860693365a8SJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 1861693365a8SJed Brown ierr = MatView(FDexp,vdraw);CHKERRQ(ierr); 1862693365a8SJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 1863693365a8SJed Brown } 1864693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 1865693365a8SJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 1866693365a8SJed Brown ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr); 1867693365a8SJed Brown ierr = MatDestroy(&FDexp);CHKERRQ(ierr); 1868693365a8SJed Brown } 1869693365a8SJed Brown } 18704c30e9fbSJed Brown { 18716719d8e4SJed Brown PetscBool flag = PETSC_FALSE,flag_display = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_threshold = PETSC_FALSE; 18726719d8e4SJed Brown PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON,threshold_rtol = 10*PETSC_SQRT_MACHINE_EPSILON; 18734c30e9fbSJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring",&flag,PETSC_NULL);CHKERRQ(ierr); 18746719d8e4SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_display",&flag_display,PETSC_NULL);CHKERRQ(ierr); 18754c30e9fbSJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw",&flag_draw,PETSC_NULL);CHKERRQ(ierr); 18764c30e9fbSJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_draw_contour",&flag_contour,PETSC_NULL);CHKERRQ(ierr); 18776719d8e4SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold",&flag_threshold,PETSC_NULL);CHKERRQ(ierr); 18786719d8e4SJed Brown ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_rtol",&threshold_rtol,PETSC_NULL);CHKERRQ(ierr); 18796719d8e4SJed Brown ierr = PetscOptionsGetReal(((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_atol",&threshold_atol,PETSC_NULL);CHKERRQ(ierr); 18806719d8e4SJed Brown if (flag || flag_display || flag_draw || flag_contour || flag_threshold) { 18814c30e9fbSJed Brown Mat Bfd; 18824c30e9fbSJed Brown MatStructure mstruct; 18834c30e9fbSJed Brown PetscViewer vdraw,vstdout; 18844c30e9fbSJed Brown ISColoring iscoloring; 18854c30e9fbSJed Brown MatFDColoring matfdcoloring; 18864c30e9fbSJed Brown PetscErrorCode (*func)(SNES,Vec,Vec,void*); 18874c30e9fbSJed Brown void *funcctx; 18886719d8e4SJed Brown PetscReal norm1,norm2,normmax; 18894c30e9fbSJed Brown 18904c30e9fbSJed Brown ierr = MatDuplicate(*B,MAT_DO_NOT_COPY_VALUES,&Bfd);CHKERRQ(ierr); 18914c30e9fbSJed Brown ierr = MatGetColoring(Bfd,MATCOLORINGSL,&iscoloring);CHKERRQ(ierr); 18924c30e9fbSJed Brown ierr = MatFDColoringCreate(Bfd,iscoloring,&matfdcoloring);CHKERRQ(ierr); 18934c30e9fbSJed Brown ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 18944c30e9fbSJed Brown 18954c30e9fbSJed Brown /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */ 18964c30e9fbSJed Brown ierr = SNESGetFunction(snes,PETSC_NULL,&func,&funcctx);CHKERRQ(ierr); 18974c30e9fbSJed Brown ierr = MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode(*)(void))func,funcctx);CHKERRQ(ierr); 18984c30e9fbSJed Brown ierr = PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring,((PetscObject)snes)->prefix);CHKERRQ(ierr); 18994c30e9fbSJed Brown ierr = PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring,"coloring_");CHKERRQ(ierr); 19004c30e9fbSJed Brown ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr); 19014c30e9fbSJed Brown ierr = MatFDColoringApply(Bfd,matfdcoloring,X,&mstruct,snes);CHKERRQ(ierr); 19024c30e9fbSJed Brown ierr = MatFDColoringDestroy(&matfdcoloring);CHKERRQ(ierr); 19034c30e9fbSJed Brown 19044c30e9fbSJed Brown ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&vstdout);CHKERRQ(ierr); 19054c30e9fbSJed Brown if (flag_draw || flag_contour) { 19064c30e9fbSJed Brown ierr = PetscViewerDrawOpen(((PetscObject)snes)->comm,0,"Colored Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 19074c30e9fbSJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 19084c30e9fbSJed Brown } else vdraw = PETSC_NULL; 19094c30e9fbSJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit preconditioning Jacobian\n");CHKERRQ(ierr); 19106719d8e4SJed Brown if (flag_display) {ierr = MatView(*B,vstdout);CHKERRQ(ierr);} 19114c30e9fbSJed Brown if (vdraw) {ierr = MatView(*B,vdraw);CHKERRQ(ierr);} 19124c30e9fbSJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Colored Finite difference Jacobian\n");CHKERRQ(ierr); 19136719d8e4SJed Brown if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);} 19144c30e9fbSJed Brown if (vdraw) {ierr = MatView(Bfd,vdraw);CHKERRQ(ierr);} 19154c30e9fbSJed Brown ierr = MatAYPX(Bfd,-1.0,*B,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 19164c30e9fbSJed Brown ierr = MatNorm(Bfd,NORM_1,&norm1);CHKERRQ(ierr); 19176719d8e4SJed Brown ierr = MatNorm(Bfd,NORM_FROBENIUS,&norm2);CHKERRQ(ierr); 19184c30e9fbSJed Brown ierr = MatNorm(Bfd,NORM_MAX,&normmax);CHKERRQ(ierr); 19196719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian, norm1=%G normFrob=%G normmax=%G\n",norm1,norm2,normmax);CHKERRQ(ierr); 19206719d8e4SJed Brown if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);} 19214c30e9fbSJed Brown if (vdraw) { /* Always use contour for the difference */ 19224c30e9fbSJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 19234c30e9fbSJed Brown ierr = MatView(Bfd,vdraw);CHKERRQ(ierr); 19244c30e9fbSJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 19254c30e9fbSJed Brown } 19264c30e9fbSJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 19276719d8e4SJed Brown 19286719d8e4SJed Brown if (flag_threshold) { 19296719d8e4SJed Brown PetscInt bs,rstart,rend,i; 19306719d8e4SJed Brown ierr = MatGetBlockSize(*B,&bs);CHKERRQ(ierr); 19316719d8e4SJed Brown ierr = MatGetOwnershipRange(*B,&rstart,&rend);CHKERRQ(ierr); 19326719d8e4SJed Brown for (i=rstart; i<rend; i++) { 19336719d8e4SJed Brown const PetscScalar *ba,*ca; 19346719d8e4SJed Brown const PetscInt *bj,*cj; 19356719d8e4SJed Brown PetscInt bn,cn,j,maxentrycol = -1,maxdiffcol = -1,maxrdiffcol = -1; 19366719d8e4SJed Brown PetscReal maxentry = 0,maxdiff = 0,maxrdiff = 0; 19376719d8e4SJed Brown ierr = MatGetRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr); 19386719d8e4SJed Brown ierr = MatGetRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr); 19396719d8e4SJed Brown if (bn != cn) SETERRQ(((PetscObject)*A)->comm,PETSC_ERR_PLIB,"Unexpected different nonzero pattern in -snes_compare_coloring_threshold"); 19406719d8e4SJed Brown for (j=0; j<bn; j++) { 19416719d8e4SJed Brown PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 19426719d8e4SJed Brown if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) { 19436719d8e4SJed Brown maxentrycol = bj[j]; 19446719d8e4SJed Brown maxentry = PetscRealPart(ba[j]); 19456719d8e4SJed Brown } 19466719d8e4SJed Brown if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) { 19476719d8e4SJed Brown maxdiffcol = bj[j]; 19486719d8e4SJed Brown maxdiff = PetscRealPart(ca[j]); 19496719d8e4SJed Brown } 19506719d8e4SJed Brown if (rdiff > maxrdiff) { 19516719d8e4SJed Brown maxrdiffcol = bj[j]; 19526719d8e4SJed Brown maxrdiff = rdiff; 19536719d8e4SJed Brown } 19546719d8e4SJed Brown } 19556719d8e4SJed Brown if (maxrdiff > 1) { 19566719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"row %D (maxentry=%G at %D, maxdiff=%G at %D, maxrdiff=%G at %D):",i,maxentry,maxentrycol,maxdiff,maxdiffcol,maxrdiff,maxrdiffcol);CHKERRQ(ierr); 19576719d8e4SJed Brown for (j=0; j<bn; j++) { 19586719d8e4SJed Brown PetscReal rdiff; 19596719d8e4SJed Brown rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 19606719d8e4SJed Brown if (rdiff > 1) { 19616719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout," (%D,%G:%G)",bj[j],PetscRealPart(ba[j]),PetscRealPart(ca[j]));CHKERRQ(ierr); 19626719d8e4SJed Brown } 19636719d8e4SJed Brown } 19646719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"\n",i,maxentry,maxdiff,maxrdiff);CHKERRQ(ierr); 19656719d8e4SJed Brown } 19666719d8e4SJed Brown ierr = MatRestoreRow(*B,i,&bn,&bj,&ba);CHKERRQ(ierr); 19676719d8e4SJed Brown ierr = MatRestoreRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr); 19686719d8e4SJed Brown } 19696719d8e4SJed Brown } 19704c30e9fbSJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 19714c30e9fbSJed Brown ierr = MatDestroy(&Bfd);CHKERRQ(ierr); 19724c30e9fbSJed Brown } 19734c30e9fbSJed Brown } 19743a40ed3dSBarry Smith PetscFunctionReturn(0); 19759b94acceSBarry Smith } 19769b94acceSBarry Smith 19774a2ae208SSatish Balay #undef __FUNCT__ 19784a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 19799b94acceSBarry Smith /*@C 19809b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1981044dda88SLois Curfman McInnes location to store the matrix. 19829b94acceSBarry Smith 19833f9fe445SBarry Smith Logically Collective on SNES and Mat 1984c7afd0dbSLois Curfman McInnes 19859b94acceSBarry Smith Input Parameters: 1986c7afd0dbSLois Curfman McInnes + snes - the SNES context 19879b94acceSBarry Smith . A - Jacobian matrix 19889b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 1989efd51863SBarry Smith . func - Jacobian evaluation routine (if PETSC_NULL then SNES retains any previously set value) 1990c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1991efd51863SBarry Smith Jacobian evaluation routine (may be PETSC_NULL) (if PETSC_NULL then SNES retains any previously set value) 19929b94acceSBarry Smith 19939b94acceSBarry Smith Calling sequence of func: 19948d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 19959b94acceSBarry Smith 1996c7afd0dbSLois Curfman McInnes + x - input vector 19979b94acceSBarry Smith . A - Jacobian matrix 19989b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1999ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 20002b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 2001c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 20029b94acceSBarry Smith 20039b94acceSBarry Smith Notes: 200494b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 20052cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 2006ac21db08SLois Curfman McInnes 2007ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 20089b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 20099b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 20109b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 20119b94acceSBarry Smith throughout the global iterations. 20129b94acceSBarry Smith 201316913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 201416913363SBarry Smith each matrix. 201516913363SBarry Smith 2016a8a26c1eSJed Brown If using SNESDefaultComputeJacobianColor() to assemble a Jacobian, the ctx argument 2017a8a26c1eSJed Brown must be a MatFDColoring. 2018a8a26c1eSJed Brown 2019c3cc8fd1SJed Brown Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian. One common 2020c3cc8fd1SJed Brown example is to use the "Picard linearization" which only differentiates through the highest order parts of each term. 2021c3cc8fd1SJed Brown 202236851e7fSLois Curfman McInnes Level: beginner 202336851e7fSLois Curfman McInnes 20249b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 20259b94acceSBarry Smith 20263ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 20279b94acceSBarry Smith @*/ 20287087cfbeSBarry Smith PetscErrorCode SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 20299b94acceSBarry Smith { 2030dfbe8321SBarry Smith PetscErrorCode ierr; 20316cab3a1bSJed Brown DM dm; 20323a7fca6bSBarry Smith 20333a40ed3dSBarry Smith PetscFunctionBegin; 20340700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 20350700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 20360700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 2037c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 203806975374SJed Brown if (B) PetscCheckSameComm(snes,1,B,3); 20396cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 20406cab3a1bSJed Brown ierr = DMSNESSetJacobian(dm,func,ctx);CHKERRQ(ierr); 20413a7fca6bSBarry Smith if (A) { 20427dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 20436bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 20449b94acceSBarry Smith snes->jacobian = A; 20453a7fca6bSBarry Smith } 20463a7fca6bSBarry Smith if (B) { 20477dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 20486bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 20499b94acceSBarry Smith snes->jacobian_pre = B; 20503a7fca6bSBarry Smith } 20513a40ed3dSBarry Smith PetscFunctionReturn(0); 20529b94acceSBarry Smith } 205362fef451SLois Curfman McInnes 20544a2ae208SSatish Balay #undef __FUNCT__ 20554a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 2056c2aafc4cSSatish Balay /*@C 2057b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 2058b4fd4287SBarry Smith provided context for evaluating the Jacobian. 2059b4fd4287SBarry Smith 2060c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 2061c7afd0dbSLois Curfman McInnes 2062b4fd4287SBarry Smith Input Parameter: 2063b4fd4287SBarry Smith . snes - the nonlinear solver context 2064b4fd4287SBarry Smith 2065b4fd4287SBarry Smith Output Parameters: 2066c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 2067b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 206870e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 206970e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 2070fee21e36SBarry Smith 207136851e7fSLois Curfman McInnes Level: advanced 207236851e7fSLois Curfman McInnes 2073b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 2074b4fd4287SBarry Smith @*/ 20757087cfbeSBarry Smith PetscErrorCode SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 2076b4fd4287SBarry Smith { 20776cab3a1bSJed Brown PetscErrorCode ierr; 20786cab3a1bSJed Brown DM dm; 20796cab3a1bSJed Brown SNESDM sdm; 20806cab3a1bSJed Brown 20813a40ed3dSBarry Smith PetscFunctionBegin; 20820700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2083b4fd4287SBarry Smith if (A) *A = snes->jacobian; 2084b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 20856cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 20866cab3a1bSJed Brown ierr = DMSNESGetContext(dm,&sdm);CHKERRQ(ierr); 20876cab3a1bSJed Brown if (func) *func = sdm->computejacobian; 20886cab3a1bSJed Brown if (ctx) *ctx = sdm->jacobianctx; 20893a40ed3dSBarry Smith PetscFunctionReturn(0); 2090b4fd4287SBarry Smith } 2091b4fd4287SBarry Smith 20929b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 20939b94acceSBarry Smith 20944a2ae208SSatish Balay #undef __FUNCT__ 20954a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 20969b94acceSBarry Smith /*@ 20979b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 2098272ac6f2SLois Curfman McInnes of a nonlinear solver. 20999b94acceSBarry Smith 2100fee21e36SBarry Smith Collective on SNES 2101fee21e36SBarry Smith 2102c7afd0dbSLois Curfman McInnes Input Parameters: 210370e92668SMatthew Knepley . snes - the SNES context 2104c7afd0dbSLois Curfman McInnes 2105272ac6f2SLois Curfman McInnes Notes: 2106272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 2107272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 2108272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 2109272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 2110272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 2111272ac6f2SLois Curfman McInnes 211236851e7fSLois Curfman McInnes Level: advanced 211336851e7fSLois Curfman McInnes 21149b94acceSBarry Smith .keywords: SNES, nonlinear, setup 21159b94acceSBarry Smith 21169b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 21179b94acceSBarry Smith @*/ 21187087cfbeSBarry Smith PetscErrorCode SNESSetUp(SNES snes) 21199b94acceSBarry Smith { 2120dfbe8321SBarry Smith PetscErrorCode ierr; 21216cab3a1bSJed Brown DM dm; 21226cab3a1bSJed Brown SNESDM sdm; 21233a40ed3dSBarry Smith 21243a40ed3dSBarry Smith PetscFunctionBegin; 21250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 21264dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 21279b94acceSBarry Smith 21287adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 212985385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 213085385478SLisandro Dalcin } 213185385478SLisandro Dalcin 2132a63bb30eSJed Brown ierr = SNESGetFunction(snes,&snes->vec_func,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 213317186662SBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 213458c9b817SLisandro Dalcin if (snes->vec_rhs == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector"); 213558c9b817SLisandro Dalcin 213658c9b817SLisandro Dalcin if (!snes->vec_sol_update /* && snes->vec_sol */) { 213758c9b817SLisandro Dalcin ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr); 213858c9b817SLisandro Dalcin ierr = PetscLogObjectParent(snes,snes->vec_sol_update);CHKERRQ(ierr); 213958c9b817SLisandro Dalcin } 214058c9b817SLisandro Dalcin 21416cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 21426cab3a1bSJed Brown ierr = DMSNESSetUpLegacy(dm);CHKERRQ(ierr); /* To be removed when function routines are taken out of the DM package */ 21436cab3a1bSJed Brown ierr = DMSNESGetContext(dm,&sdm);CHKERRQ(ierr); 21446cab3a1bSJed Brown if (!sdm->computefunction) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_ARG_WRONGSTATE,"Must provide a residual function with SNESSetFunction(), DMSNESSetFunction(), DMDASNESSetFunctionLocal(), etc"); 21456cab3a1bSJed Brown if (!snes->vec_func) { 21466cab3a1bSJed Brown ierr = DMCreateGlobalVector(dm,&snes->vec_func);CHKERRQ(ierr); 2147214df951SJed Brown } 2148efd51863SBarry Smith 2149b710008aSBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);} 2150b710008aSBarry Smith 2151*f1c6b773SPeter Brune if (!snes->linesearch) {ierr = SNESGetSNESLineSearch(snes, &snes->linesearch);} 21529e764e56SPeter Brune 2153d25893d9SBarry Smith if (snes->ops->usercompute && !snes->user) { 2154d25893d9SBarry Smith ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr); 2155d25893d9SBarry Smith } 2156d25893d9SBarry Smith 2157410397dcSLisandro Dalcin if (snes->ops->setup) { 2158410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 2159410397dcSLisandro Dalcin } 216058c9b817SLisandro Dalcin 21617aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 21623a40ed3dSBarry Smith PetscFunctionReturn(0); 21639b94acceSBarry Smith } 21649b94acceSBarry Smith 21654a2ae208SSatish Balay #undef __FUNCT__ 216637596af1SLisandro Dalcin #define __FUNCT__ "SNESReset" 216737596af1SLisandro Dalcin /*@ 216837596af1SLisandro Dalcin SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats 216937596af1SLisandro Dalcin 217037596af1SLisandro Dalcin Collective on SNES 217137596af1SLisandro Dalcin 217237596af1SLisandro Dalcin Input Parameter: 217337596af1SLisandro Dalcin . snes - iterative context obtained from SNESCreate() 217437596af1SLisandro Dalcin 2175d25893d9SBarry Smith Level: intermediate 2176d25893d9SBarry Smith 2177d25893d9SBarry Smith Notes: Also calls the application context destroy routine set with SNESSetComputeApplicationContext() 217837596af1SLisandro Dalcin 217937596af1SLisandro Dalcin .keywords: SNES, destroy 218037596af1SLisandro Dalcin 218137596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 218237596af1SLisandro Dalcin @*/ 218337596af1SLisandro Dalcin PetscErrorCode SNESReset(SNES snes) 218437596af1SLisandro Dalcin { 218537596af1SLisandro Dalcin PetscErrorCode ierr; 218637596af1SLisandro Dalcin 218737596af1SLisandro Dalcin PetscFunctionBegin; 218837596af1SLisandro Dalcin PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2189d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 2190d25893d9SBarry Smith ierr = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr); 2191d25893d9SBarry Smith snes->user = PETSC_NULL; 2192d25893d9SBarry Smith } 21938a23116dSBarry Smith if (snes->pc) { 21948a23116dSBarry Smith ierr = SNESReset(snes->pc);CHKERRQ(ierr); 21958a23116dSBarry Smith } 21968a23116dSBarry Smith 219737596af1SLisandro Dalcin if (snes->ops->reset) { 219837596af1SLisandro Dalcin ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr); 219937596af1SLisandro Dalcin } 22009e764e56SPeter Brune if (snes->ksp) { 22019e764e56SPeter Brune ierr = KSPReset(snes->ksp);CHKERRQ(ierr); 22029e764e56SPeter Brune } 22039e764e56SPeter Brune 22049e764e56SPeter Brune if (snes->linesearch) { 2205*f1c6b773SPeter Brune ierr = SNESLineSearchReset(snes->linesearch);CHKERRQ(ierr); 22069e764e56SPeter Brune } 22079e764e56SPeter Brune 22086bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 22096bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 22106bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr); 22116bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 22126bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 22136bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 2214c41d97abSJed Brown ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr); 2215c41d97abSJed Brown ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr); 221637596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 221737596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 221837596af1SLisandro Dalcin PetscFunctionReturn(0); 221937596af1SLisandro Dalcin } 222037596af1SLisandro Dalcin 222137596af1SLisandro Dalcin #undef __FUNCT__ 22224a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 222352baeb72SSatish Balay /*@ 22249b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 22259b94acceSBarry Smith with SNESCreate(). 22269b94acceSBarry Smith 2227c7afd0dbSLois Curfman McInnes Collective on SNES 2228c7afd0dbSLois Curfman McInnes 22299b94acceSBarry Smith Input Parameter: 22309b94acceSBarry Smith . snes - the SNES context 22319b94acceSBarry Smith 223236851e7fSLois Curfman McInnes Level: beginner 223336851e7fSLois Curfman McInnes 22349b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 22359b94acceSBarry Smith 223663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 22379b94acceSBarry Smith @*/ 22386bf464f9SBarry Smith PetscErrorCode SNESDestroy(SNES *snes) 22399b94acceSBarry Smith { 22406849ba73SBarry Smith PetscErrorCode ierr; 22413a40ed3dSBarry Smith 22423a40ed3dSBarry Smith PetscFunctionBegin; 22436bf464f9SBarry Smith if (!*snes) PetscFunctionReturn(0); 22446bf464f9SBarry Smith PetscValidHeaderSpecific((*snes),SNES_CLASSID,1); 22456bf464f9SBarry Smith if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);} 2246d4bb536fSBarry Smith 22476bf464f9SBarry Smith ierr = SNESReset((*snes));CHKERRQ(ierr); 22488a23116dSBarry Smith ierr = SNESDestroy(&(*snes)->pc);CHKERRQ(ierr); 22496b8b9a38SLisandro Dalcin 2250be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 22516bf464f9SBarry Smith ierr = PetscObjectDepublish((*snes));CHKERRQ(ierr); 22526bf464f9SBarry Smith if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);} 22536d4c513bSLisandro Dalcin 22546bf464f9SBarry Smith ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr); 22556bf464f9SBarry Smith ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr); 2256*f1c6b773SPeter Brune ierr = SNESLineSearchDestroy(&(*snes)->linesearch);CHKERRQ(ierr); 22576b8b9a38SLisandro Dalcin 22586bf464f9SBarry Smith ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr); 22596bf464f9SBarry Smith if ((*snes)->ops->convergeddestroy) { 22606bf464f9SBarry Smith ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr); 22616b8b9a38SLisandro Dalcin } 22626bf464f9SBarry Smith if ((*snes)->conv_malloc) { 22636bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr); 22646bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr); 226558c9b817SLisandro Dalcin } 22666bf464f9SBarry Smith ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr); 2267a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 22683a40ed3dSBarry Smith PetscFunctionReturn(0); 22699b94acceSBarry Smith } 22709b94acceSBarry Smith 22719b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 22729b94acceSBarry Smith 22734a2ae208SSatish Balay #undef __FUNCT__ 2274a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 2275a8054027SBarry Smith /*@ 2276a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 2277a8054027SBarry Smith 22783f9fe445SBarry Smith Logically Collective on SNES 2279a8054027SBarry Smith 2280a8054027SBarry Smith Input Parameters: 2281a8054027SBarry Smith + snes - the SNES context 2282a8054027SBarry 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 22833b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 2284a8054027SBarry Smith 2285a8054027SBarry Smith Options Database Keys: 2286a8054027SBarry Smith . -snes_lag_preconditioner <lag> 2287a8054027SBarry Smith 2288a8054027SBarry Smith Notes: 2289a8054027SBarry Smith The default is 1 2290a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2291a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 2292a8054027SBarry Smith 2293a8054027SBarry Smith Level: intermediate 2294a8054027SBarry Smith 2295a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2296a8054027SBarry Smith 2297e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 2298a8054027SBarry Smith 2299a8054027SBarry Smith @*/ 23007087cfbeSBarry Smith PetscErrorCode SNESSetLagPreconditioner(SNES snes,PetscInt lag) 2301a8054027SBarry Smith { 2302a8054027SBarry Smith PetscFunctionBegin; 23030700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2304e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 2305e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 2306c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 2307a8054027SBarry Smith snes->lagpreconditioner = lag; 2308a8054027SBarry Smith PetscFunctionReturn(0); 2309a8054027SBarry Smith } 2310a8054027SBarry Smith 2311a8054027SBarry Smith #undef __FUNCT__ 2312efd51863SBarry Smith #define __FUNCT__ "SNESSetGridSequence" 2313efd51863SBarry Smith /*@ 2314efd51863SBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does 2315efd51863SBarry Smith 2316efd51863SBarry Smith Logically Collective on SNES 2317efd51863SBarry Smith 2318efd51863SBarry Smith Input Parameters: 2319efd51863SBarry Smith + snes - the SNES context 2320efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 2321efd51863SBarry Smith 2322efd51863SBarry Smith Options Database Keys: 2323efd51863SBarry Smith . -snes_grid_sequence <steps> 2324efd51863SBarry Smith 2325efd51863SBarry Smith Level: intermediate 2326efd51863SBarry Smith 2327c0df2a02SJed Brown Notes: 2328c0df2a02SJed Brown Use SNESGetSolution() to extract the fine grid solution after grid sequencing. 2329c0df2a02SJed Brown 2330efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2331efd51863SBarry Smith 2332efd51863SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 2333efd51863SBarry Smith 2334efd51863SBarry Smith @*/ 2335efd51863SBarry Smith PetscErrorCode SNESSetGridSequence(SNES snes,PetscInt steps) 2336efd51863SBarry Smith { 2337efd51863SBarry Smith PetscFunctionBegin; 2338efd51863SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2339efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes,steps,2); 2340efd51863SBarry Smith snes->gridsequence = steps; 2341efd51863SBarry Smith PetscFunctionReturn(0); 2342efd51863SBarry Smith } 2343efd51863SBarry Smith 2344efd51863SBarry Smith #undef __FUNCT__ 2345a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 2346a8054027SBarry Smith /*@ 2347a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 2348a8054027SBarry Smith 23493f9fe445SBarry Smith Not Collective 2350a8054027SBarry Smith 2351a8054027SBarry Smith Input Parameter: 2352a8054027SBarry Smith . snes - the SNES context 2353a8054027SBarry Smith 2354a8054027SBarry Smith Output Parameter: 2355a8054027SBarry 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 23563b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 2357a8054027SBarry Smith 2358a8054027SBarry Smith Options Database Keys: 2359a8054027SBarry Smith . -snes_lag_preconditioner <lag> 2360a8054027SBarry Smith 2361a8054027SBarry Smith Notes: 2362a8054027SBarry Smith The default is 1 2363a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2364a8054027SBarry Smith 2365a8054027SBarry Smith Level: intermediate 2366a8054027SBarry Smith 2367a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2368a8054027SBarry Smith 2369a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 2370a8054027SBarry Smith 2371a8054027SBarry Smith @*/ 23727087cfbeSBarry Smith PetscErrorCode SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 2373a8054027SBarry Smith { 2374a8054027SBarry Smith PetscFunctionBegin; 23750700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2376a8054027SBarry Smith *lag = snes->lagpreconditioner; 2377a8054027SBarry Smith PetscFunctionReturn(0); 2378a8054027SBarry Smith } 2379a8054027SBarry Smith 2380a8054027SBarry Smith #undef __FUNCT__ 2381e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 2382e35cf81dSBarry Smith /*@ 2383e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 2384e35cf81dSBarry Smith often the preconditioner is rebuilt. 2385e35cf81dSBarry Smith 23863f9fe445SBarry Smith Logically Collective on SNES 2387e35cf81dSBarry Smith 2388e35cf81dSBarry Smith Input Parameters: 2389e35cf81dSBarry Smith + snes - the SNES context 2390e35cf81dSBarry 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 2391fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 2392e35cf81dSBarry Smith 2393e35cf81dSBarry Smith Options Database Keys: 2394e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 2395e35cf81dSBarry Smith 2396e35cf81dSBarry Smith Notes: 2397e35cf81dSBarry Smith The default is 1 2398e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2399fe3ffe1eSBarry 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 2400fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 2401e35cf81dSBarry Smith 2402e35cf81dSBarry Smith Level: intermediate 2403e35cf81dSBarry Smith 2404e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2405e35cf81dSBarry Smith 2406e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 2407e35cf81dSBarry Smith 2408e35cf81dSBarry Smith @*/ 24097087cfbeSBarry Smith PetscErrorCode SNESSetLagJacobian(SNES snes,PetscInt lag) 2410e35cf81dSBarry Smith { 2411e35cf81dSBarry Smith PetscFunctionBegin; 24120700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2413e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 2414e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 2415c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 2416e35cf81dSBarry Smith snes->lagjacobian = lag; 2417e35cf81dSBarry Smith PetscFunctionReturn(0); 2418e35cf81dSBarry Smith } 2419e35cf81dSBarry Smith 2420e35cf81dSBarry Smith #undef __FUNCT__ 2421e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 2422e35cf81dSBarry Smith /*@ 2423e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 2424e35cf81dSBarry Smith 24253f9fe445SBarry Smith Not Collective 2426e35cf81dSBarry Smith 2427e35cf81dSBarry Smith Input Parameter: 2428e35cf81dSBarry Smith . snes - the SNES context 2429e35cf81dSBarry Smith 2430e35cf81dSBarry Smith Output Parameter: 2431e35cf81dSBarry 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 2432e35cf81dSBarry Smith the Jacobian is built etc. 2433e35cf81dSBarry Smith 2434e35cf81dSBarry Smith Options Database Keys: 2435e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 2436e35cf81dSBarry Smith 2437e35cf81dSBarry Smith Notes: 2438e35cf81dSBarry Smith The default is 1 2439e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 2440e35cf81dSBarry Smith 2441e35cf81dSBarry Smith Level: intermediate 2442e35cf81dSBarry Smith 2443e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 2444e35cf81dSBarry Smith 2445e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 2446e35cf81dSBarry Smith 2447e35cf81dSBarry Smith @*/ 24487087cfbeSBarry Smith PetscErrorCode SNESGetLagJacobian(SNES snes,PetscInt *lag) 2449e35cf81dSBarry Smith { 2450e35cf81dSBarry Smith PetscFunctionBegin; 24510700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2452e35cf81dSBarry Smith *lag = snes->lagjacobian; 2453e35cf81dSBarry Smith PetscFunctionReturn(0); 2454e35cf81dSBarry Smith } 2455e35cf81dSBarry Smith 2456e35cf81dSBarry Smith #undef __FUNCT__ 24574a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 24589b94acceSBarry Smith /*@ 2459d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 24609b94acceSBarry Smith 24613f9fe445SBarry Smith Logically Collective on SNES 2462c7afd0dbSLois Curfman McInnes 24639b94acceSBarry Smith Input Parameters: 2464c7afd0dbSLois Curfman McInnes + snes - the SNES context 246570441072SBarry Smith . abstol - absolute convergence tolerance 246633174efeSLois Curfman McInnes . rtol - relative convergence tolerance 246733174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 246833174efeSLois Curfman McInnes of the change in the solution between steps 246933174efeSLois Curfman McInnes . maxit - maximum number of iterations 2470c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 2471fee21e36SBarry Smith 247233174efeSLois Curfman McInnes Options Database Keys: 247370441072SBarry Smith + -snes_atol <abstol> - Sets abstol 2474c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 2475c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 2476c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 2477c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 24789b94acceSBarry Smith 2479d7a720efSLois Curfman McInnes Notes: 24809b94acceSBarry Smith The default maximum number of iterations is 50. 24819b94acceSBarry Smith The default maximum number of function evaluations is 1000. 24829b94acceSBarry Smith 248336851e7fSLois Curfman McInnes Level: intermediate 248436851e7fSLois Curfman McInnes 248533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 24869b94acceSBarry Smith 24872492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 24889b94acceSBarry Smith @*/ 24897087cfbeSBarry Smith PetscErrorCode SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 24909b94acceSBarry Smith { 24913a40ed3dSBarry Smith PetscFunctionBegin; 24920700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2493c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,abstol,2); 2494c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol,3); 2495c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,stol,4); 2496c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxit,5); 2497c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxf,6); 2498c5eb9154SBarry Smith 2499ab54825eSJed Brown if (abstol != PETSC_DEFAULT) { 2500ab54825eSJed Brown if (abstol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %G must be non-negative",abstol); 2501ab54825eSJed Brown snes->abstol = abstol; 2502ab54825eSJed Brown } 2503ab54825eSJed Brown if (rtol != PETSC_DEFAULT) { 2504ab54825eSJed Brown if (rtol < 0.0 || 1.0 <= rtol) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Relative tolerance %G must be non-negative and less than 1.0",rtol); 2505ab54825eSJed Brown snes->rtol = rtol; 2506ab54825eSJed Brown } 2507ab54825eSJed Brown if (stol != PETSC_DEFAULT) { 2508ab54825eSJed Brown if (stol < 0.0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %G must be non-negative",stol); 2509ab54825eSJed Brown snes->xtol = stol; 2510ab54825eSJed Brown } 2511ab54825eSJed Brown if (maxit != PETSC_DEFAULT) { 2512ab54825eSJed Brown if (maxit < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit); 2513ab54825eSJed Brown snes->max_its = maxit; 2514ab54825eSJed Brown } 2515ab54825eSJed Brown if (maxf != PETSC_DEFAULT) { 2516ab54825eSJed Brown if (maxf < 0) SETERRQ1(((PetscObject)snes)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be non-negative",maxf); 2517ab54825eSJed Brown snes->max_funcs = maxf; 2518ab54825eSJed Brown } 25193a40ed3dSBarry Smith PetscFunctionReturn(0); 25209b94acceSBarry Smith } 25219b94acceSBarry Smith 25224a2ae208SSatish Balay #undef __FUNCT__ 25234a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 25249b94acceSBarry Smith /*@ 252533174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 252633174efeSLois Curfman McInnes 2527c7afd0dbSLois Curfman McInnes Not Collective 2528c7afd0dbSLois Curfman McInnes 252933174efeSLois Curfman McInnes Input Parameters: 2530c7afd0dbSLois Curfman McInnes + snes - the SNES context 253185385478SLisandro Dalcin . atol - absolute convergence tolerance 253233174efeSLois Curfman McInnes . rtol - relative convergence tolerance 253333174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 253433174efeSLois Curfman McInnes of the change in the solution between steps 253533174efeSLois Curfman McInnes . maxit - maximum number of iterations 2536c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 2537fee21e36SBarry Smith 253833174efeSLois Curfman McInnes Notes: 253933174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 254033174efeSLois Curfman McInnes 254136851e7fSLois Curfman McInnes Level: intermediate 254236851e7fSLois Curfman McInnes 254333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 254433174efeSLois Curfman McInnes 254533174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 254633174efeSLois Curfman McInnes @*/ 25477087cfbeSBarry Smith PetscErrorCode SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 254833174efeSLois Curfman McInnes { 25493a40ed3dSBarry Smith PetscFunctionBegin; 25500700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 255185385478SLisandro Dalcin if (atol) *atol = snes->abstol; 255233174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 255333174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 255433174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 255533174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 25563a40ed3dSBarry Smith PetscFunctionReturn(0); 255733174efeSLois Curfman McInnes } 255833174efeSLois Curfman McInnes 25594a2ae208SSatish Balay #undef __FUNCT__ 25604a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 256133174efeSLois Curfman McInnes /*@ 25629b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 25639b94acceSBarry Smith 25643f9fe445SBarry Smith Logically Collective on SNES 2565fee21e36SBarry Smith 2566c7afd0dbSLois Curfman McInnes Input Parameters: 2567c7afd0dbSLois Curfman McInnes + snes - the SNES context 2568c7afd0dbSLois Curfman McInnes - tol - tolerance 2569c7afd0dbSLois Curfman McInnes 25709b94acceSBarry Smith Options Database Key: 2571c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 25729b94acceSBarry Smith 257336851e7fSLois Curfman McInnes Level: intermediate 257436851e7fSLois Curfman McInnes 25759b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 25769b94acceSBarry Smith 25772492ecdbSBarry Smith .seealso: SNESSetTolerances() 25789b94acceSBarry Smith @*/ 25797087cfbeSBarry Smith PetscErrorCode SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 25809b94acceSBarry Smith { 25813a40ed3dSBarry Smith PetscFunctionBegin; 25820700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2583c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,tol,2); 25849b94acceSBarry Smith snes->deltatol = tol; 25853a40ed3dSBarry Smith PetscFunctionReturn(0); 25869b94acceSBarry Smith } 25879b94acceSBarry Smith 2588df9fa365SBarry Smith /* 2589df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 2590df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 2591df9fa365SBarry Smith macros instead of functions 2592df9fa365SBarry Smith */ 25934a2ae208SSatish Balay #undef __FUNCT__ 2594a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 25957087cfbeSBarry Smith PetscErrorCode SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 2596ce1608b8SBarry Smith { 2597dfbe8321SBarry Smith PetscErrorCode ierr; 2598ce1608b8SBarry Smith 2599ce1608b8SBarry Smith PetscFunctionBegin; 26000700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2601a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 2602ce1608b8SBarry Smith PetscFunctionReturn(0); 2603ce1608b8SBarry Smith } 2604ce1608b8SBarry Smith 26054a2ae208SSatish Balay #undef __FUNCT__ 2606a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 26077087cfbeSBarry Smith PetscErrorCode SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2608df9fa365SBarry Smith { 2609dfbe8321SBarry Smith PetscErrorCode ierr; 2610df9fa365SBarry Smith 2611df9fa365SBarry Smith PetscFunctionBegin; 2612a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2613df9fa365SBarry Smith PetscFunctionReturn(0); 2614df9fa365SBarry Smith } 2615df9fa365SBarry Smith 26164a2ae208SSatish Balay #undef __FUNCT__ 2617a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 26186bf464f9SBarry Smith PetscErrorCode SNESMonitorLGDestroy(PetscDrawLG *draw) 2619df9fa365SBarry Smith { 2620dfbe8321SBarry Smith PetscErrorCode ierr; 2621df9fa365SBarry Smith 2622df9fa365SBarry Smith PetscFunctionBegin; 2623a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2624df9fa365SBarry Smith PetscFunctionReturn(0); 2625df9fa365SBarry Smith } 2626df9fa365SBarry Smith 26277087cfbeSBarry Smith extern PetscErrorCode SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 2628b271bb04SBarry Smith #undef __FUNCT__ 2629b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 26307087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 2631b271bb04SBarry Smith { 2632b271bb04SBarry Smith PetscDrawLG lg; 2633b271bb04SBarry Smith PetscErrorCode ierr; 2634b271bb04SBarry Smith PetscReal x,y,per; 2635b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 2636b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 2637b271bb04SBarry Smith PetscDraw draw; 2638b271bb04SBarry Smith PetscFunctionBegin; 2639b271bb04SBarry Smith if (!monctx) { 2640b271bb04SBarry Smith MPI_Comm comm; 2641b271bb04SBarry Smith 2642b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 2643b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 2644b271bb04SBarry Smith } 2645b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 2646b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2647b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2648b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 2649b271bb04SBarry Smith x = (PetscReal) n; 2650b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 2651b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2652b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2653b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2654b271bb04SBarry Smith } 2655b271bb04SBarry Smith 2656b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 2657b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2658b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2659b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 2660b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 2661b271bb04SBarry Smith x = (PetscReal) n; 2662b271bb04SBarry Smith y = 100.0*per; 2663b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2664b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2665b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2666b271bb04SBarry Smith } 2667b271bb04SBarry Smith 2668b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 2669b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2670b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2671b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 2672b271bb04SBarry Smith x = (PetscReal) n; 2673b271bb04SBarry Smith y = (prev - rnorm)/prev; 2674b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2675b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2676b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2677b271bb04SBarry Smith } 2678b271bb04SBarry Smith 2679b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 2680b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 2681b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 2682b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 2683b271bb04SBarry Smith x = (PetscReal) n; 2684b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 2685b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 2686b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 2687b271bb04SBarry Smith } 2688b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 2689b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 2690b271bb04SBarry Smith } 2691b271bb04SBarry Smith prev = rnorm; 2692b271bb04SBarry Smith PetscFunctionReturn(0); 2693b271bb04SBarry Smith } 2694b271bb04SBarry Smith 2695b271bb04SBarry Smith #undef __FUNCT__ 2696b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 26977087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 2698b271bb04SBarry Smith { 2699b271bb04SBarry Smith PetscErrorCode ierr; 2700b271bb04SBarry Smith 2701b271bb04SBarry Smith PetscFunctionBegin; 2702b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 2703b271bb04SBarry Smith PetscFunctionReturn(0); 2704b271bb04SBarry Smith } 2705b271bb04SBarry Smith 2706b271bb04SBarry Smith #undef __FUNCT__ 2707b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 27086bf464f9SBarry Smith PetscErrorCode SNESMonitorLGRangeDestroy(PetscDrawLG *draw) 2709b271bb04SBarry Smith { 2710b271bb04SBarry Smith PetscErrorCode ierr; 2711b271bb04SBarry Smith 2712b271bb04SBarry Smith PetscFunctionBegin; 2713b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 2714b271bb04SBarry Smith PetscFunctionReturn(0); 2715b271bb04SBarry Smith } 2716b271bb04SBarry Smith 27177a03ce2fSLisandro Dalcin #undef __FUNCT__ 27187a03ce2fSLisandro Dalcin #define __FUNCT__ "SNESMonitor" 2719228d79bcSJed Brown /*@ 2720228d79bcSJed Brown SNESMonitor - runs the user provided monitor routines, if they exist 2721228d79bcSJed Brown 2722228d79bcSJed Brown Collective on SNES 2723228d79bcSJed Brown 2724228d79bcSJed Brown Input Parameters: 2725228d79bcSJed Brown + snes - nonlinear solver context obtained from SNESCreate() 2726228d79bcSJed Brown . iter - iteration number 2727228d79bcSJed Brown - rnorm - relative norm of the residual 2728228d79bcSJed Brown 2729228d79bcSJed Brown Notes: 2730228d79bcSJed Brown This routine is called by the SNES implementations. 2731228d79bcSJed Brown It does not typically need to be called by the user. 2732228d79bcSJed Brown 2733228d79bcSJed Brown Level: developer 2734228d79bcSJed Brown 2735228d79bcSJed Brown .seealso: SNESMonitorSet() 2736228d79bcSJed Brown @*/ 27377a03ce2fSLisandro Dalcin PetscErrorCode SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm) 27387a03ce2fSLisandro Dalcin { 27397a03ce2fSLisandro Dalcin PetscErrorCode ierr; 27407a03ce2fSLisandro Dalcin PetscInt i,n = snes->numbermonitors; 27417a03ce2fSLisandro Dalcin 27427a03ce2fSLisandro Dalcin PetscFunctionBegin; 27437a03ce2fSLisandro Dalcin for (i=0; i<n; i++) { 27447a03ce2fSLisandro Dalcin ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr); 27457a03ce2fSLisandro Dalcin } 27467a03ce2fSLisandro Dalcin PetscFunctionReturn(0); 27477a03ce2fSLisandro Dalcin } 27487a03ce2fSLisandro Dalcin 27499b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 27509b94acceSBarry Smith 27514a2ae208SSatish Balay #undef __FUNCT__ 2752a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 27539b94acceSBarry Smith /*@C 2754a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 27559b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 27569b94acceSBarry Smith progress. 27579b94acceSBarry Smith 27583f9fe445SBarry Smith Logically Collective on SNES 2759fee21e36SBarry Smith 2760c7afd0dbSLois Curfman McInnes Input Parameters: 2761c7afd0dbSLois Curfman McInnes + snes - the SNES context 2762c7afd0dbSLois Curfman McInnes . func - monitoring routine 2763b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 2764e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 2765b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 2766b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 27679b94acceSBarry Smith 2768c7afd0dbSLois Curfman McInnes Calling sequence of func: 2769a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 2770c7afd0dbSLois Curfman McInnes 2771c7afd0dbSLois Curfman McInnes + snes - the SNES context 2772c7afd0dbSLois Curfman McInnes . its - iteration number 2773c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 277440a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 27759b94acceSBarry Smith 27769665c990SLois Curfman McInnes Options Database Keys: 2777a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 2778a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 2779a6570f20SBarry Smith uses SNESMonitorLGCreate() 2780cca6129bSJed Brown - -snes_monitor_cancel - cancels all monitors that have 2781c7afd0dbSLois Curfman McInnes been hardwired into a code by 2782a6570f20SBarry Smith calls to SNESMonitorSet(), but 2783c7afd0dbSLois Curfman McInnes does not cancel those set via 2784c7afd0dbSLois Curfman McInnes the options database. 27859665c990SLois Curfman McInnes 2786639f9d9dSBarry Smith Notes: 27876bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 2788a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 27896bc08f3fSLois Curfman McInnes order in which they were set. 2790639f9d9dSBarry Smith 2791025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 2792025f1a04SBarry Smith 279336851e7fSLois Curfman McInnes Level: intermediate 279436851e7fSLois Curfman McInnes 27959b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 27969b94acceSBarry Smith 2797a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 27989b94acceSBarry Smith @*/ 2799c2efdce3SBarry Smith PetscErrorCode SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 28009b94acceSBarry Smith { 2801b90d0a6eSBarry Smith PetscInt i; 2802649052a6SBarry Smith PetscErrorCode ierr; 2803b90d0a6eSBarry Smith 28043a40ed3dSBarry Smith PetscFunctionBegin; 28050700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 280617186662SBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 2807b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 2808649052a6SBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) { 2809649052a6SBarry Smith if (monitordestroy) { 2810c2efdce3SBarry Smith ierr = (*monitordestroy)(&mctx);CHKERRQ(ierr); 2811649052a6SBarry Smith } 2812b90d0a6eSBarry Smith PetscFunctionReturn(0); 2813b90d0a6eSBarry Smith } 2814b90d0a6eSBarry Smith } 2815b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 2816b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 2817639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 28183a40ed3dSBarry Smith PetscFunctionReturn(0); 28199b94acceSBarry Smith } 28209b94acceSBarry Smith 28214a2ae208SSatish Balay #undef __FUNCT__ 2822a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 28235cd90555SBarry Smith /*@C 2824a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 28255cd90555SBarry Smith 28263f9fe445SBarry Smith Logically Collective on SNES 2827c7afd0dbSLois Curfman McInnes 28285cd90555SBarry Smith Input Parameters: 28295cd90555SBarry Smith . snes - the SNES context 28305cd90555SBarry Smith 28311a480d89SAdministrator Options Database Key: 2832a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 2833a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 2834c7afd0dbSLois Curfman McInnes set via the options database 28355cd90555SBarry Smith 28365cd90555SBarry Smith Notes: 28375cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 28385cd90555SBarry Smith 283936851e7fSLois Curfman McInnes Level: intermediate 284036851e7fSLois Curfman McInnes 28415cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 28425cd90555SBarry Smith 2843a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 28445cd90555SBarry Smith @*/ 28457087cfbeSBarry Smith PetscErrorCode SNESMonitorCancel(SNES snes) 28465cd90555SBarry Smith { 2847d952e501SBarry Smith PetscErrorCode ierr; 2848d952e501SBarry Smith PetscInt i; 2849d952e501SBarry Smith 28505cd90555SBarry Smith PetscFunctionBegin; 28510700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2852d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 2853d952e501SBarry Smith if (snes->monitordestroy[i]) { 28543c4aec1bSBarry Smith ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr); 2855d952e501SBarry Smith } 2856d952e501SBarry Smith } 28575cd90555SBarry Smith snes->numbermonitors = 0; 28585cd90555SBarry Smith PetscFunctionReturn(0); 28595cd90555SBarry Smith } 28605cd90555SBarry Smith 28614a2ae208SSatish Balay #undef __FUNCT__ 28624a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 28639b94acceSBarry Smith /*@C 28649b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 28659b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 28669b94acceSBarry Smith 28673f9fe445SBarry Smith Logically Collective on SNES 2868fee21e36SBarry Smith 2869c7afd0dbSLois Curfman McInnes Input Parameters: 2870c7afd0dbSLois Curfman McInnes + snes - the SNES context 2871c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 28727f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 28737f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 28749b94acceSBarry Smith 2875c7afd0dbSLois Curfman McInnes Calling sequence of func: 287606ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 2877c7afd0dbSLois Curfman McInnes 2878c7afd0dbSLois Curfman McInnes + snes - the SNES context 287906ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 2880c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 2881184914b5SBarry Smith . reason - reason for convergence/divergence 2882c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 28834b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 28844b27c08aSLois Curfman McInnes - f - 2-norm of function 28859b94acceSBarry Smith 288636851e7fSLois Curfman McInnes Level: advanced 288736851e7fSLois Curfman McInnes 28889b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 28899b94acceSBarry Smith 289085385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 28919b94acceSBarry Smith @*/ 28927087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 28939b94acceSBarry Smith { 28947f7931b9SBarry Smith PetscErrorCode ierr; 28957f7931b9SBarry Smith 28963a40ed3dSBarry Smith PetscFunctionBegin; 28970700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 289885385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 28997f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 29007f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 29017f7931b9SBarry Smith } 290285385478SLisandro Dalcin snes->ops->converged = func; 29037f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 290485385478SLisandro Dalcin snes->cnvP = cctx; 29053a40ed3dSBarry Smith PetscFunctionReturn(0); 29069b94acceSBarry Smith } 29079b94acceSBarry Smith 29084a2ae208SSatish Balay #undef __FUNCT__ 29094a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 291052baeb72SSatish Balay /*@ 2911184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 2912184914b5SBarry Smith 2913184914b5SBarry Smith Not Collective 2914184914b5SBarry Smith 2915184914b5SBarry Smith Input Parameter: 2916184914b5SBarry Smith . snes - the SNES context 2917184914b5SBarry Smith 2918184914b5SBarry Smith Output Parameter: 29194d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 2920184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 2921184914b5SBarry Smith 2922184914b5SBarry Smith Level: intermediate 2923184914b5SBarry Smith 2924184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 2925184914b5SBarry Smith 2926184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 2927184914b5SBarry Smith 292885385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 2929184914b5SBarry Smith @*/ 29307087cfbeSBarry Smith PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 2931184914b5SBarry Smith { 2932184914b5SBarry Smith PetscFunctionBegin; 29330700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29344482741eSBarry Smith PetscValidPointer(reason,2); 2935184914b5SBarry Smith *reason = snes->reason; 2936184914b5SBarry Smith PetscFunctionReturn(0); 2937184914b5SBarry Smith } 2938184914b5SBarry Smith 29394a2ae208SSatish Balay #undef __FUNCT__ 29404a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 2941c9005455SLois Curfman McInnes /*@ 2942c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 2943c9005455SLois Curfman McInnes 29443f9fe445SBarry Smith Logically Collective on SNES 2945fee21e36SBarry Smith 2946c7afd0dbSLois Curfman McInnes Input Parameters: 2947c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 29488c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 2949cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 2950758f92a0SBarry Smith . na - size of a and its 295164731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 2952758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 2953c7afd0dbSLois Curfman McInnes 2954308dcc3eSBarry Smith Notes: 2955308dcc3eSBarry Smith If 'a' and 'its' are PETSC_NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a 2956308dcc3eSBarry Smith default array of length 10000 is allocated. 2957308dcc3eSBarry Smith 2958c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 2959c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 2960c9005455SLois Curfman McInnes during the section of code that is being timed. 2961c9005455SLois Curfman McInnes 296236851e7fSLois Curfman McInnes Level: intermediate 296336851e7fSLois Curfman McInnes 2964c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2965758f92a0SBarry Smith 296608405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2967758f92a0SBarry Smith 2968c9005455SLois Curfman McInnes @*/ 29697087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset) 2970c9005455SLois Curfman McInnes { 2971308dcc3eSBarry Smith PetscErrorCode ierr; 2972308dcc3eSBarry Smith 29733a40ed3dSBarry Smith PetscFunctionBegin; 29740700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29754482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2976a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2977308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT || !a) { 2978308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 2979308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscReal),&a);CHKERRQ(ierr); 2980308dcc3eSBarry Smith ierr = PetscMalloc(na*sizeof(PetscInt),&its);CHKERRQ(ierr); 2981308dcc3eSBarry Smith snes->conv_malloc = PETSC_TRUE; 2982308dcc3eSBarry Smith } 2983c9005455SLois Curfman McInnes snes->conv_hist = a; 2984758f92a0SBarry Smith snes->conv_hist_its = its; 2985758f92a0SBarry Smith snes->conv_hist_max = na; 2986a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2987758f92a0SBarry Smith snes->conv_hist_reset = reset; 2988758f92a0SBarry Smith PetscFunctionReturn(0); 2989758f92a0SBarry Smith } 2990758f92a0SBarry Smith 2991308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 2992c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 2993c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 2994308dcc3eSBarry Smith EXTERN_C_BEGIN 2995308dcc3eSBarry Smith #undef __FUNCT__ 2996308dcc3eSBarry Smith #define __FUNCT__ "SNESGetConvergenceHistoryMatlab" 2997308dcc3eSBarry Smith mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 2998308dcc3eSBarry Smith { 2999308dcc3eSBarry Smith mxArray *mat; 3000308dcc3eSBarry Smith PetscInt i; 3001308dcc3eSBarry Smith PetscReal *ar; 3002308dcc3eSBarry Smith 3003308dcc3eSBarry Smith PetscFunctionBegin; 3004308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL); 3005308dcc3eSBarry Smith ar = (PetscReal*) mxGetData(mat); 3006308dcc3eSBarry Smith for (i=0; i<snes->conv_hist_len; i++) { 3007308dcc3eSBarry Smith ar[i] = snes->conv_hist[i]; 3008308dcc3eSBarry Smith } 3009308dcc3eSBarry Smith PetscFunctionReturn(mat); 3010308dcc3eSBarry Smith } 3011308dcc3eSBarry Smith EXTERN_C_END 3012308dcc3eSBarry Smith #endif 3013308dcc3eSBarry Smith 3014308dcc3eSBarry Smith 30154a2ae208SSatish Balay #undef __FUNCT__ 30164a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 30170c4c9dddSBarry Smith /*@C 3018758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 3019758f92a0SBarry Smith 30203f9fe445SBarry Smith Not Collective 3021758f92a0SBarry Smith 3022758f92a0SBarry Smith Input Parameter: 3023758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 3024758f92a0SBarry Smith 3025758f92a0SBarry Smith Output Parameters: 3026758f92a0SBarry Smith . a - array to hold history 3027758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 3028758f92a0SBarry Smith negative if not converged) for each solve. 3029758f92a0SBarry Smith - na - size of a and its 3030758f92a0SBarry Smith 3031758f92a0SBarry Smith Notes: 3032758f92a0SBarry Smith The calling sequence for this routine in Fortran is 3033758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 3034758f92a0SBarry Smith 3035758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 3036758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 3037758f92a0SBarry Smith during the section of code that is being timed. 3038758f92a0SBarry Smith 3039758f92a0SBarry Smith Level: intermediate 3040758f92a0SBarry Smith 3041758f92a0SBarry Smith .keywords: SNES, get, convergence, history 3042758f92a0SBarry Smith 3043758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 3044758f92a0SBarry Smith 3045758f92a0SBarry Smith @*/ 30467087cfbeSBarry Smith PetscErrorCode SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 3047758f92a0SBarry Smith { 3048758f92a0SBarry Smith PetscFunctionBegin; 30490700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3050758f92a0SBarry Smith if (a) *a = snes->conv_hist; 3051758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 3052758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 30533a40ed3dSBarry Smith PetscFunctionReturn(0); 3054c9005455SLois Curfman McInnes } 3055c9005455SLois Curfman McInnes 3056e74ef692SMatthew Knepley #undef __FUNCT__ 3057e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 3058ac226902SBarry Smith /*@C 305976b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 3060eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 30617e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 306276b2cf59SMatthew Knepley 30633f9fe445SBarry Smith Logically Collective on SNES 306476b2cf59SMatthew Knepley 306576b2cf59SMatthew Knepley Input Parameters: 306676b2cf59SMatthew Knepley . snes - The nonlinear solver context 306776b2cf59SMatthew Knepley . func - The function 306876b2cf59SMatthew Knepley 306976b2cf59SMatthew Knepley Calling sequence of func: 3070b5d30489SBarry Smith . func (SNES snes, PetscInt step); 307176b2cf59SMatthew Knepley 307276b2cf59SMatthew Knepley . step - The current step of the iteration 307376b2cf59SMatthew Knepley 3074fe97e370SBarry Smith Level: advanced 3075fe97e370SBarry Smith 3076fe97e370SBarry 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() 3077fe97e370SBarry Smith This is not used by most users. 307876b2cf59SMatthew Knepley 307976b2cf59SMatthew Knepley .keywords: SNES, update 3080b5d30489SBarry Smith 308185385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 308276b2cf59SMatthew Knepley @*/ 30837087cfbeSBarry Smith PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 308476b2cf59SMatthew Knepley { 308576b2cf59SMatthew Knepley PetscFunctionBegin; 30860700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 3087e7788613SBarry Smith snes->ops->update = func; 308876b2cf59SMatthew Knepley PetscFunctionReturn(0); 308976b2cf59SMatthew Knepley } 309076b2cf59SMatthew Knepley 3091e74ef692SMatthew Knepley #undef __FUNCT__ 3092e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 309376b2cf59SMatthew Knepley /*@ 309476b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 309576b2cf59SMatthew Knepley 309676b2cf59SMatthew Knepley Not collective 309776b2cf59SMatthew Knepley 309876b2cf59SMatthew Knepley Input Parameters: 309976b2cf59SMatthew Knepley . snes - The nonlinear solver context 310076b2cf59SMatthew Knepley . step - The current step of the iteration 310176b2cf59SMatthew Knepley 3102205452f4SMatthew Knepley Level: intermediate 3103205452f4SMatthew Knepley 310476b2cf59SMatthew Knepley .keywords: SNES, update 3105a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 310676b2cf59SMatthew Knepley @*/ 31077087cfbeSBarry Smith PetscErrorCode SNESDefaultUpdate(SNES snes, PetscInt step) 310876b2cf59SMatthew Knepley { 310976b2cf59SMatthew Knepley PetscFunctionBegin; 311076b2cf59SMatthew Knepley PetscFunctionReturn(0); 311176b2cf59SMatthew Knepley } 311276b2cf59SMatthew Knepley 31134a2ae208SSatish Balay #undef __FUNCT__ 31144a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 31159b94acceSBarry Smith /* 31169b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 31179b94acceSBarry Smith positive parameter delta. 31189b94acceSBarry Smith 31199b94acceSBarry Smith Input Parameters: 3120c7afd0dbSLois Curfman McInnes + snes - the SNES context 31219b94acceSBarry Smith . y - approximate solution of linear system 31229b94acceSBarry Smith . fnorm - 2-norm of current function 3123c7afd0dbSLois Curfman McInnes - delta - trust region size 31249b94acceSBarry Smith 31259b94acceSBarry Smith Output Parameters: 3126c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 31279b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 31289b94acceSBarry Smith region, and exceeds zero otherwise. 3129c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 31309b94acceSBarry Smith 31319b94acceSBarry Smith Note: 31324b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 31339b94acceSBarry Smith is set to be the maximum allowable step size. 31349b94acceSBarry Smith 31359b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 31369b94acceSBarry Smith */ 3137dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 31389b94acceSBarry Smith { 3139064f8208SBarry Smith PetscReal nrm; 3140ea709b57SSatish Balay PetscScalar cnorm; 3141dfbe8321SBarry Smith PetscErrorCode ierr; 31423a40ed3dSBarry Smith 31433a40ed3dSBarry Smith PetscFunctionBegin; 31440700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 31450700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 3146c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 3147184914b5SBarry Smith 3148064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 3149064f8208SBarry Smith if (nrm > *delta) { 3150064f8208SBarry Smith nrm = *delta/nrm; 3151064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 3152064f8208SBarry Smith cnorm = nrm; 31532dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 31549b94acceSBarry Smith *ynorm = *delta; 31559b94acceSBarry Smith } else { 31569b94acceSBarry Smith *gpnorm = 0.0; 3157064f8208SBarry Smith *ynorm = nrm; 31589b94acceSBarry Smith } 31593a40ed3dSBarry Smith PetscFunctionReturn(0); 31609b94acceSBarry Smith } 31619b94acceSBarry Smith 31624a2ae208SSatish Balay #undef __FUNCT__ 31634a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 31646ce558aeSBarry Smith /*@C 3165f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 3166f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 31679b94acceSBarry Smith 3168c7afd0dbSLois Curfman McInnes Collective on SNES 3169c7afd0dbSLois Curfman McInnes 3170b2002411SLois Curfman McInnes Input Parameters: 3171c7afd0dbSLois Curfman McInnes + snes - the SNES context 31723cebf274SJed Brown . b - the constant part of the equation F(x) = b, or PETSC_NULL to use zero. 317385385478SLisandro Dalcin - x - the solution vector. 31749b94acceSBarry Smith 3175b2002411SLois Curfman McInnes Notes: 31768ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 31778ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 31788ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 31798ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 31808ddd3da0SLois Curfman McInnes 318136851e7fSLois Curfman McInnes Level: beginner 318236851e7fSLois Curfman McInnes 31839b94acceSBarry Smith .keywords: SNES, nonlinear, solve 31849b94acceSBarry Smith 3185c0df2a02SJed Brown .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetGridSequence(), SNESGetSolution() 31869b94acceSBarry Smith @*/ 31877087cfbeSBarry Smith PetscErrorCode SNESSolve(SNES snes,Vec b,Vec x) 31889b94acceSBarry Smith { 3189dfbe8321SBarry Smith PetscErrorCode ierr; 3190ace3abfcSBarry Smith PetscBool flg; 3191eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 3192eabae89aSBarry Smith PetscViewer viewer; 3193efd51863SBarry Smith PetscInt grid; 3194a69afd8bSBarry Smith Vec xcreated = PETSC_NULL; 3195052efed2SBarry Smith 31963a40ed3dSBarry Smith PetscFunctionBegin; 31970700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3198a69afd8bSBarry Smith if (x) PetscValidHeaderSpecific(x,VEC_CLASSID,3); 3199a69afd8bSBarry Smith if (x) PetscCheckSameComm(snes,1,x,3); 32000700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 320185385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 320285385478SLisandro Dalcin 3203a69afd8bSBarry Smith if (!x && snes->dm) { 3204a69afd8bSBarry Smith ierr = DMCreateGlobalVector(snes->dm,&xcreated);CHKERRQ(ierr); 3205a69afd8bSBarry Smith x = xcreated; 3206a69afd8bSBarry Smith } 3207a69afd8bSBarry Smith 3208a8248277SBarry Smith for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr);} 3209efd51863SBarry Smith for (grid=0; grid<snes->gridsequence+1; grid++) { 3210efd51863SBarry Smith 321185385478SLisandro Dalcin /* set solution vector */ 3212efd51863SBarry Smith if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);} 32136bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 321485385478SLisandro Dalcin snes->vec_sol = x; 321585385478SLisandro Dalcin /* set afine vector if provided */ 321685385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 32176bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 321885385478SLisandro Dalcin snes->vec_rhs = b; 321985385478SLisandro Dalcin 322070e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 32213f149594SLisandro Dalcin 32227eee914bSBarry Smith if (!grid) { 32237eee914bSBarry Smith if (snes->ops->computeinitialguess) { 3224d25893d9SBarry Smith ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr); 3225dd568438SSatish Balay } else if (snes->dm) { 3226dd568438SSatish Balay PetscBool ig; 3227dd568438SSatish Balay ierr = DMHasInitialGuess(snes->dm,&ig);CHKERRQ(ierr); 3228dd568438SSatish Balay if (ig) { 32297eee914bSBarry Smith ierr = DMComputeInitialGuess(snes->dm,snes->vec_sol);CHKERRQ(ierr); 32307eee914bSBarry Smith } 3231d25893d9SBarry Smith } 3232dd568438SSatish Balay } 3233d25893d9SBarry Smith 3234abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 323550ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 3236d5e45103SBarry Smith 32373f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 32384936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 323985385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 32404936397dSBarry Smith if (snes->domainerror){ 32414936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 32424936397dSBarry Smith snes->domainerror = PETSC_FALSE; 32434936397dSBarry Smith } 324417186662SBarry Smith if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 32453f149594SLisandro Dalcin 32467adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 3247eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 32487adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 3249eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 32506bf464f9SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 3251eabae89aSBarry Smith } 3252eabae89aSBarry Smith 325390d69ab7SBarry Smith flg = PETSC_FALSE; 3254acfcf0e5SJed Brown ierr = PetscOptionsGetBool(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr); 3255da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 32565968eb51SBarry Smith if (snes->printreason) { 3257a8248277SBarry Smith ierr = PetscViewerASCIIAddTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 32585968eb51SBarry Smith if (snes->reason > 0) { 3259c7e7b494SJed Brown ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve converged due to %s iterations %D\n",SNESConvergedReasons[snes->reason],snes->iter);CHKERRQ(ierr); 32605968eb51SBarry Smith } else { 3261c7e7b494SJed Brown ierr = PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),"Nonlinear solve did not converge due to %s iterations %D\n",SNESConvergedReasons[snes->reason],snes->iter);CHKERRQ(ierr); 32625968eb51SBarry Smith } 3263a8248277SBarry Smith ierr = PetscViewerASCIISubtractTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm),((PetscObject)snes)->tablevel);CHKERRQ(ierr); 32645968eb51SBarry Smith } 32655968eb51SBarry Smith 32668501fc72SJed Brown flg = PETSC_FALSE; 32678501fc72SJed Brown ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view_solution_vtk",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 32688501fc72SJed Brown if (flg) { 32698501fc72SJed Brown PetscViewer viewer; 32708501fc72SJed Brown ierr = PetscViewerCreate(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr); 32718501fc72SJed Brown ierr = PetscViewerSetType(viewer,PETSCVIEWERVTK);CHKERRQ(ierr); 32728501fc72SJed Brown ierr = PetscViewerFileSetName(viewer,filename);CHKERRQ(ierr); 32738501fc72SJed Brown ierr = VecView(snes->vec_sol,viewer);CHKERRQ(ierr); 32748501fc72SJed Brown ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 32758501fc72SJed Brown } 32768501fc72SJed Brown 3277e113a28aSBarry Smith if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(((PetscObject)snes)->comm,PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged"); 3278efd51863SBarry Smith if (grid < snes->gridsequence) { 3279efd51863SBarry Smith DM fine; 3280efd51863SBarry Smith Vec xnew; 3281efd51863SBarry Smith Mat interp; 3282efd51863SBarry Smith 3283efd51863SBarry Smith ierr = DMRefine(snes->dm,((PetscObject)snes)->comm,&fine);CHKERRQ(ierr); 3284e727c939SJed Brown ierr = DMCreateInterpolation(snes->dm,fine,&interp,PETSC_NULL);CHKERRQ(ierr); 3285efd51863SBarry Smith ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr); 3286efd51863SBarry Smith ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr); 3287efd51863SBarry Smith ierr = MatDestroy(&interp);CHKERRQ(ierr); 3288efd51863SBarry Smith x = xnew; 3289efd51863SBarry Smith 3290efd51863SBarry Smith ierr = SNESReset(snes);CHKERRQ(ierr); 3291efd51863SBarry Smith ierr = SNESSetDM(snes,fine);CHKERRQ(ierr); 3292efd51863SBarry Smith ierr = DMDestroy(&fine);CHKERRQ(ierr); 3293a8248277SBarry Smith ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(((PetscObject)snes)->comm));CHKERRQ(ierr); 3294efd51863SBarry Smith } 3295efd51863SBarry Smith } 3296a69afd8bSBarry Smith ierr = VecDestroy(&xcreated);CHKERRQ(ierr); 32973a40ed3dSBarry Smith PetscFunctionReturn(0); 32989b94acceSBarry Smith } 32999b94acceSBarry Smith 33009b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 33019b94acceSBarry Smith 33024a2ae208SSatish Balay #undef __FUNCT__ 33034a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 330482bf6240SBarry Smith /*@C 33054b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 33069b94acceSBarry Smith 3307fee21e36SBarry Smith Collective on SNES 3308fee21e36SBarry Smith 3309c7afd0dbSLois Curfman McInnes Input Parameters: 3310c7afd0dbSLois Curfman McInnes + snes - the SNES context 3311454a90a3SBarry Smith - type - a known method 3312c7afd0dbSLois Curfman McInnes 3313c7afd0dbSLois Curfman McInnes Options Database Key: 3314454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 3315c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 3316ae12b187SLois Curfman McInnes 33179b94acceSBarry Smith Notes: 3318e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 33194b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 3320c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 33214b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 3322c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 33239b94acceSBarry Smith 3324ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 3325ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 3326ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 3327ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 3328ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 3329ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 3330ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 3331ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 3332ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 3333b0a32e0cSBarry Smith appropriate method. 333436851e7fSLois Curfman McInnes 333536851e7fSLois Curfman McInnes Level: intermediate 3336a703fe33SLois Curfman McInnes 3337454a90a3SBarry Smith .keywords: SNES, set, type 3338435da068SBarry Smith 3339435da068SBarry Smith .seealso: SNESType, SNESCreate() 3340435da068SBarry Smith 33419b94acceSBarry Smith @*/ 33427087cfbeSBarry Smith PetscErrorCode SNESSetType(SNES snes,const SNESType type) 33439b94acceSBarry Smith { 3344dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 3345ace3abfcSBarry Smith PetscBool match; 33463a40ed3dSBarry Smith 33473a40ed3dSBarry Smith PetscFunctionBegin; 33480700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 33494482741eSBarry Smith PetscValidCharPointer(type,2); 335082bf6240SBarry Smith 33516831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 33520f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 335392ff6ae8SBarry Smith 33544b91b6eaSBarry Smith ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 3355e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 335675396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 3357b5c23020SJed Brown if (snes->ops->destroy) { 3358b5c23020SJed Brown ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); 3359b5c23020SJed Brown snes->ops->destroy = PETSC_NULL; 3360b5c23020SJed Brown } 336175396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 336275396ef9SLisandro Dalcin snes->ops->setup = 0; 336375396ef9SLisandro Dalcin snes->ops->solve = 0; 336475396ef9SLisandro Dalcin snes->ops->view = 0; 336575396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 336675396ef9SLisandro Dalcin snes->ops->destroy = 0; 336775396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 336875396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 3369454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 337003bfa161SLisandro Dalcin ierr = (*r)(snes);CHKERRQ(ierr); 33719fb22e1aSBarry Smith #if defined(PETSC_HAVE_AMS) 33729fb22e1aSBarry Smith if (PetscAMSPublishAll) { 33739fb22e1aSBarry Smith ierr = PetscObjectAMSPublish((PetscObject)snes);CHKERRQ(ierr); 33749fb22e1aSBarry Smith } 33759fb22e1aSBarry Smith #endif 33763a40ed3dSBarry Smith PetscFunctionReturn(0); 33779b94acceSBarry Smith } 33789b94acceSBarry Smith 3379a847f771SSatish Balay 33809b94acceSBarry Smith /* --------------------------------------------------------------------- */ 33814a2ae208SSatish Balay #undef __FUNCT__ 33824a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 338352baeb72SSatish Balay /*@ 33849b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 3385f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 33869b94acceSBarry Smith 3387fee21e36SBarry Smith Not Collective 3388fee21e36SBarry Smith 338936851e7fSLois Curfman McInnes Level: advanced 339036851e7fSLois Curfman McInnes 33919b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 33929b94acceSBarry Smith 33939b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 33949b94acceSBarry Smith @*/ 33957087cfbeSBarry Smith PetscErrorCode SNESRegisterDestroy(void) 33969b94acceSBarry Smith { 3397dfbe8321SBarry Smith PetscErrorCode ierr; 339882bf6240SBarry Smith 33993a40ed3dSBarry Smith PetscFunctionBegin; 34001441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 34014c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 34023a40ed3dSBarry Smith PetscFunctionReturn(0); 34039b94acceSBarry Smith } 34049b94acceSBarry Smith 34054a2ae208SSatish Balay #undef __FUNCT__ 34064a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 34079b94acceSBarry Smith /*@C 34089a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 34099b94acceSBarry Smith 3410c7afd0dbSLois Curfman McInnes Not Collective 3411c7afd0dbSLois Curfman McInnes 34129b94acceSBarry Smith Input Parameter: 34134b0e389bSBarry Smith . snes - nonlinear solver context 34149b94acceSBarry Smith 34159b94acceSBarry Smith Output Parameter: 34163a7fca6bSBarry Smith . type - SNES method (a character string) 34179b94acceSBarry Smith 341836851e7fSLois Curfman McInnes Level: intermediate 341936851e7fSLois Curfman McInnes 3420454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 34219b94acceSBarry Smith @*/ 34227087cfbeSBarry Smith PetscErrorCode SNESGetType(SNES snes,const SNESType *type) 34239b94acceSBarry Smith { 34243a40ed3dSBarry Smith PetscFunctionBegin; 34250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 34264482741eSBarry Smith PetscValidPointer(type,2); 34277adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 34283a40ed3dSBarry Smith PetscFunctionReturn(0); 34299b94acceSBarry Smith } 34309b94acceSBarry Smith 34314a2ae208SSatish Balay #undef __FUNCT__ 34324a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 343352baeb72SSatish Balay /*@ 34349b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 3435c0df2a02SJed Brown stored. This is the fine grid solution when using SNESSetGridSequence(). 34369b94acceSBarry Smith 3437c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 3438c7afd0dbSLois Curfman McInnes 34399b94acceSBarry Smith Input Parameter: 34409b94acceSBarry Smith . snes - the SNES context 34419b94acceSBarry Smith 34429b94acceSBarry Smith Output Parameter: 34439b94acceSBarry Smith . x - the solution 34449b94acceSBarry Smith 344570e92668SMatthew Knepley Level: intermediate 344636851e7fSLois Curfman McInnes 34479b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 34489b94acceSBarry Smith 344985385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 34509b94acceSBarry Smith @*/ 34517087cfbeSBarry Smith PetscErrorCode SNESGetSolution(SNES snes,Vec *x) 34529b94acceSBarry Smith { 34533a40ed3dSBarry Smith PetscFunctionBegin; 34540700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 34554482741eSBarry Smith PetscValidPointer(x,2); 345685385478SLisandro Dalcin *x = snes->vec_sol; 345770e92668SMatthew Knepley PetscFunctionReturn(0); 345870e92668SMatthew Knepley } 345970e92668SMatthew Knepley 346070e92668SMatthew Knepley #undef __FUNCT__ 34614a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 346252baeb72SSatish Balay /*@ 34639b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 34649b94acceSBarry Smith stored. 34659b94acceSBarry Smith 3466c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 3467c7afd0dbSLois Curfman McInnes 34689b94acceSBarry Smith Input Parameter: 34699b94acceSBarry Smith . snes - the SNES context 34709b94acceSBarry Smith 34719b94acceSBarry Smith Output Parameter: 34729b94acceSBarry Smith . x - the solution update 34739b94acceSBarry Smith 347436851e7fSLois Curfman McInnes Level: advanced 347536851e7fSLois Curfman McInnes 34769b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 34779b94acceSBarry Smith 347885385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 34799b94acceSBarry Smith @*/ 34807087cfbeSBarry Smith PetscErrorCode SNESGetSolutionUpdate(SNES snes,Vec *x) 34819b94acceSBarry Smith { 34823a40ed3dSBarry Smith PetscFunctionBegin; 34830700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 34844482741eSBarry Smith PetscValidPointer(x,2); 348585385478SLisandro Dalcin *x = snes->vec_sol_update; 34863a40ed3dSBarry Smith PetscFunctionReturn(0); 34879b94acceSBarry Smith } 34889b94acceSBarry Smith 34894a2ae208SSatish Balay #undef __FUNCT__ 34904a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 34919b94acceSBarry Smith /*@C 34923638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 34939b94acceSBarry Smith 3494a63bb30eSJed Brown Not Collective, but Vec is parallel if SNES is parallel. Collective if Vec is requested, but has not been created yet. 3495c7afd0dbSLois Curfman McInnes 34969b94acceSBarry Smith Input Parameter: 34979b94acceSBarry Smith . snes - the SNES context 34989b94acceSBarry Smith 34999b94acceSBarry Smith Output Parameter: 35007bf4e008SBarry Smith + r - the function (or PETSC_NULL) 350170e92668SMatthew Knepley . func - the function (or PETSC_NULL) 350270e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 35039b94acceSBarry Smith 350436851e7fSLois Curfman McInnes Level: advanced 350536851e7fSLois Curfman McInnes 3506a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 35079b94acceSBarry Smith 35084b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 35099b94acceSBarry Smith @*/ 35107087cfbeSBarry Smith PetscErrorCode SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 35119b94acceSBarry Smith { 3512a63bb30eSJed Brown PetscErrorCode ierr; 35136cab3a1bSJed Brown DM dm; 3514a63bb30eSJed Brown 35153a40ed3dSBarry Smith PetscFunctionBegin; 35160700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3517a63bb30eSJed Brown if (r) { 3518a63bb30eSJed Brown if (!snes->vec_func) { 3519a63bb30eSJed Brown if (snes->vec_rhs) { 3520a63bb30eSJed Brown ierr = VecDuplicate(snes->vec_rhs,&snes->vec_func);CHKERRQ(ierr); 3521a63bb30eSJed Brown } else if (snes->vec_sol) { 3522a63bb30eSJed Brown ierr = VecDuplicate(snes->vec_sol,&snes->vec_func);CHKERRQ(ierr); 3523a63bb30eSJed Brown } else if (snes->dm) { 3524a63bb30eSJed Brown ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 3525a63bb30eSJed Brown } 3526a63bb30eSJed Brown } 3527a63bb30eSJed Brown *r = snes->vec_func; 3528a63bb30eSJed Brown } 35296cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 35306cab3a1bSJed Brown ierr = DMSNESGetFunction(dm,func,ctx);CHKERRQ(ierr); 35313a40ed3dSBarry Smith PetscFunctionReturn(0); 35329b94acceSBarry Smith } 35339b94acceSBarry Smith 3534c79ef259SPeter Brune /*@C 3535c79ef259SPeter Brune SNESGetGS - Returns the GS function and context. 3536c79ef259SPeter Brune 3537c79ef259SPeter Brune Input Parameter: 3538c79ef259SPeter Brune . snes - the SNES context 3539c79ef259SPeter Brune 3540c79ef259SPeter Brune Output Parameter: 3541c79ef259SPeter Brune + gsfunc - the function (or PETSC_NULL) 3542c79ef259SPeter Brune - ctx - the function context (or PETSC_NULL) 3543c79ef259SPeter Brune 3544c79ef259SPeter Brune Level: advanced 3545c79ef259SPeter Brune 3546c79ef259SPeter Brune .keywords: SNES, nonlinear, get, function 3547c79ef259SPeter Brune 3548c79ef259SPeter Brune .seealso: SNESSetGS(), SNESGetFunction() 3549c79ef259SPeter Brune @*/ 3550c79ef259SPeter Brune 35514a2ae208SSatish Balay #undef __FUNCT__ 3552646217ecSPeter Brune #define __FUNCT__ "SNESGetGS" 3553646217ecSPeter Brune PetscErrorCode SNESGetGS (SNES snes, PetscErrorCode(**func)(SNES, Vec, Vec, void*), void ** ctx) 3554646217ecSPeter Brune { 35556cab3a1bSJed Brown PetscErrorCode ierr; 35566cab3a1bSJed Brown DM dm; 35576cab3a1bSJed Brown 3558646217ecSPeter Brune PetscFunctionBegin; 3559646217ecSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 35606cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 35616cab3a1bSJed Brown ierr = DMSNESGetGS(dm,func,ctx);CHKERRQ(ierr); 3562646217ecSPeter Brune PetscFunctionReturn(0); 3563646217ecSPeter Brune } 3564646217ecSPeter Brune 35654a2ae208SSatish Balay #undef __FUNCT__ 35664a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 35673c7409f5SSatish Balay /*@C 35683c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 3569d850072dSLois Curfman McInnes SNES options in the database. 35703c7409f5SSatish Balay 35713f9fe445SBarry Smith Logically Collective on SNES 3572fee21e36SBarry Smith 3573c7afd0dbSLois Curfman McInnes Input Parameter: 3574c7afd0dbSLois Curfman McInnes + snes - the SNES context 3575c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3576c7afd0dbSLois Curfman McInnes 3577d850072dSLois Curfman McInnes Notes: 3578a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3579c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3580d850072dSLois Curfman McInnes 358136851e7fSLois Curfman McInnes Level: advanced 358236851e7fSLois Curfman McInnes 35833c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 3584a86d99e1SLois Curfman McInnes 3585a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 35863c7409f5SSatish Balay @*/ 35877087cfbeSBarry Smith PetscErrorCode SNESSetOptionsPrefix(SNES snes,const char prefix[]) 35883c7409f5SSatish Balay { 3589dfbe8321SBarry Smith PetscErrorCode ierr; 35903c7409f5SSatish Balay 35913a40ed3dSBarry Smith PetscFunctionBegin; 35920700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3593639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 35941cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 359594b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 35963a40ed3dSBarry Smith PetscFunctionReturn(0); 35973c7409f5SSatish Balay } 35983c7409f5SSatish Balay 35994a2ae208SSatish Balay #undef __FUNCT__ 36004a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 36013c7409f5SSatish Balay /*@C 3602f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 3603d850072dSLois Curfman McInnes SNES options in the database. 36043c7409f5SSatish Balay 36053f9fe445SBarry Smith Logically Collective on SNES 3606fee21e36SBarry Smith 3607c7afd0dbSLois Curfman McInnes Input Parameters: 3608c7afd0dbSLois Curfman McInnes + snes - the SNES context 3609c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 3610c7afd0dbSLois Curfman McInnes 3611d850072dSLois Curfman McInnes Notes: 3612a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 3613c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 3614d850072dSLois Curfman McInnes 361536851e7fSLois Curfman McInnes Level: advanced 361636851e7fSLois Curfman McInnes 36173c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 3618a86d99e1SLois Curfman McInnes 3619a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 36203c7409f5SSatish Balay @*/ 36217087cfbeSBarry Smith PetscErrorCode SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 36223c7409f5SSatish Balay { 3623dfbe8321SBarry Smith PetscErrorCode ierr; 36243c7409f5SSatish Balay 36253a40ed3dSBarry Smith PetscFunctionBegin; 36260700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3627639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 36281cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 362994b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 36303a40ed3dSBarry Smith PetscFunctionReturn(0); 36313c7409f5SSatish Balay } 36323c7409f5SSatish Balay 36334a2ae208SSatish Balay #undef __FUNCT__ 36344a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 36359ab63eb5SSatish Balay /*@C 36363c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 36373c7409f5SSatish Balay SNES options in the database. 36383c7409f5SSatish Balay 3639c7afd0dbSLois Curfman McInnes Not Collective 3640c7afd0dbSLois Curfman McInnes 36413c7409f5SSatish Balay Input Parameter: 36423c7409f5SSatish Balay . snes - the SNES context 36433c7409f5SSatish Balay 36443c7409f5SSatish Balay Output Parameter: 36453c7409f5SSatish Balay . prefix - pointer to the prefix string used 36463c7409f5SSatish Balay 36474ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 36489ab63eb5SSatish Balay sufficient length to hold the prefix. 36499ab63eb5SSatish Balay 365036851e7fSLois Curfman McInnes Level: advanced 365136851e7fSLois Curfman McInnes 36523c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 3653a86d99e1SLois Curfman McInnes 3654a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 36553c7409f5SSatish Balay @*/ 36567087cfbeSBarry Smith PetscErrorCode SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 36573c7409f5SSatish Balay { 3658dfbe8321SBarry Smith PetscErrorCode ierr; 36593c7409f5SSatish Balay 36603a40ed3dSBarry Smith PetscFunctionBegin; 36610700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3662639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 36633a40ed3dSBarry Smith PetscFunctionReturn(0); 36643c7409f5SSatish Balay } 36653c7409f5SSatish Balay 3666b2002411SLois Curfman McInnes 36674a2ae208SSatish Balay #undef __FUNCT__ 36684a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 36693cea93caSBarry Smith /*@C 36703cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 36713cea93caSBarry Smith 36727f6c08e0SMatthew Knepley Level: advanced 36733cea93caSBarry Smith @*/ 36747087cfbeSBarry Smith PetscErrorCode SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 3675b2002411SLois Curfman McInnes { 3676e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 3677dfbe8321SBarry Smith PetscErrorCode ierr; 3678b2002411SLois Curfman McInnes 3679b2002411SLois Curfman McInnes PetscFunctionBegin; 3680b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 3681c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 3682b2002411SLois Curfman McInnes PetscFunctionReturn(0); 3683b2002411SLois Curfman McInnes } 3684da9b6338SBarry Smith 3685da9b6338SBarry Smith #undef __FUNCT__ 3686da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 36877087cfbeSBarry Smith PetscErrorCode SNESTestLocalMin(SNES snes) 3688da9b6338SBarry Smith { 3689dfbe8321SBarry Smith PetscErrorCode ierr; 369077431f27SBarry Smith PetscInt N,i,j; 3691da9b6338SBarry Smith Vec u,uh,fh; 3692da9b6338SBarry Smith PetscScalar value; 3693da9b6338SBarry Smith PetscReal norm; 3694da9b6338SBarry Smith 3695da9b6338SBarry Smith PetscFunctionBegin; 3696da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 3697da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 3698da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 3699da9b6338SBarry Smith 3700da9b6338SBarry Smith /* currently only works for sequential */ 3701da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 3702da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 3703da9b6338SBarry Smith for (i=0; i<N; i++) { 3704da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 370577431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 3706da9b6338SBarry Smith for (j=-10; j<11; j++) { 3707ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 3708da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 37093ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 3710da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 371177431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 3712da9b6338SBarry Smith value = -value; 3713da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 3714da9b6338SBarry Smith } 3715da9b6338SBarry Smith } 37166bf464f9SBarry Smith ierr = VecDestroy(&uh);CHKERRQ(ierr); 37176bf464f9SBarry Smith ierr = VecDestroy(&fh);CHKERRQ(ierr); 3718da9b6338SBarry Smith PetscFunctionReturn(0); 3719da9b6338SBarry Smith } 372071f87433Sdalcinl 372171f87433Sdalcinl #undef __FUNCT__ 3722fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 372371f87433Sdalcinl /*@ 3724fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 372571f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 372671f87433Sdalcinl Newton method. 372771f87433Sdalcinl 37283f9fe445SBarry Smith Logically Collective on SNES 372971f87433Sdalcinl 373071f87433Sdalcinl Input Parameters: 373171f87433Sdalcinl + snes - SNES context 373271f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 373371f87433Sdalcinl 373464ba62caSBarry Smith Options Database: 373564ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 373664ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 373764ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 373864ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 373964ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 374064ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 374164ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 374264ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 374364ba62caSBarry Smith 374471f87433Sdalcinl Notes: 374571f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 374671f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 374771f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 374871f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 374971f87433Sdalcinl solver. 375071f87433Sdalcinl 375171f87433Sdalcinl Level: advanced 375271f87433Sdalcinl 375371f87433Sdalcinl Reference: 375471f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 375571f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 375671f87433Sdalcinl 375771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 375871f87433Sdalcinl 3759fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 376071f87433Sdalcinl @*/ 37617087cfbeSBarry Smith PetscErrorCode SNESKSPSetUseEW(SNES snes,PetscBool flag) 376271f87433Sdalcinl { 376371f87433Sdalcinl PetscFunctionBegin; 37640700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3765acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flag,2); 376671f87433Sdalcinl snes->ksp_ewconv = flag; 376771f87433Sdalcinl PetscFunctionReturn(0); 376871f87433Sdalcinl } 376971f87433Sdalcinl 377071f87433Sdalcinl #undef __FUNCT__ 3771fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 377271f87433Sdalcinl /*@ 3773fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 377471f87433Sdalcinl for computing relative tolerance for linear solvers within an 377571f87433Sdalcinl inexact Newton method. 377671f87433Sdalcinl 377771f87433Sdalcinl Not Collective 377871f87433Sdalcinl 377971f87433Sdalcinl Input Parameter: 378071f87433Sdalcinl . snes - SNES context 378171f87433Sdalcinl 378271f87433Sdalcinl Output Parameter: 378371f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 378471f87433Sdalcinl 378571f87433Sdalcinl Notes: 378671f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 378771f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 378871f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 378971f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 379071f87433Sdalcinl solver. 379171f87433Sdalcinl 379271f87433Sdalcinl Level: advanced 379371f87433Sdalcinl 379471f87433Sdalcinl Reference: 379571f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 379671f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 379771f87433Sdalcinl 379871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 379971f87433Sdalcinl 3800fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 380171f87433Sdalcinl @*/ 38027087cfbeSBarry Smith PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 380371f87433Sdalcinl { 380471f87433Sdalcinl PetscFunctionBegin; 38050700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 380671f87433Sdalcinl PetscValidPointer(flag,2); 380771f87433Sdalcinl *flag = snes->ksp_ewconv; 380871f87433Sdalcinl PetscFunctionReturn(0); 380971f87433Sdalcinl } 381071f87433Sdalcinl 381171f87433Sdalcinl #undef __FUNCT__ 3812fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 381371f87433Sdalcinl /*@ 3814fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 381571f87433Sdalcinl convergence criteria for the linear solvers within an inexact 381671f87433Sdalcinl Newton method. 381771f87433Sdalcinl 38183f9fe445SBarry Smith Logically Collective on SNES 381971f87433Sdalcinl 382071f87433Sdalcinl Input Parameters: 382171f87433Sdalcinl + snes - SNES context 382271f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 382371f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 382471f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 382571f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 382671f87433Sdalcinl (0 <= gamma2 <= 1) 382771f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 382871f87433Sdalcinl . alpha2 - power for safeguard 382971f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 383071f87433Sdalcinl 383171f87433Sdalcinl Note: 383271f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 383371f87433Sdalcinl 383471f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 383571f87433Sdalcinl 383671f87433Sdalcinl Level: advanced 383771f87433Sdalcinl 383871f87433Sdalcinl Reference: 383971f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 384071f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 384171f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 384271f87433Sdalcinl 384371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 384471f87433Sdalcinl 3845fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 384671f87433Sdalcinl @*/ 38477087cfbeSBarry Smith PetscErrorCode SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 384871f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 384971f87433Sdalcinl { 3850fa9f3622SBarry Smith SNESKSPEW *kctx; 385171f87433Sdalcinl PetscFunctionBegin; 38520700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3853fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3854e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 3855c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,version,2); 3856c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_0,3); 3857c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_max,4); 3858c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,gamma,5); 3859c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha,6); 3860c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha2,7); 3861c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,threshold,8); 386271f87433Sdalcinl 386371f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 386471f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 386571f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 386671f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 386771f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 386871f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 386971f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 387071f87433Sdalcinl 387171f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 3872e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 387371f87433Sdalcinl } 387471f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 3875e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 387671f87433Sdalcinl } 387771f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 3878e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 387971f87433Sdalcinl } 388071f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 3881e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 388271f87433Sdalcinl } 388371f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 3884e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 388571f87433Sdalcinl } 388671f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 3887e32f2f54SBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 388871f87433Sdalcinl } 388971f87433Sdalcinl PetscFunctionReturn(0); 389071f87433Sdalcinl } 389171f87433Sdalcinl 389271f87433Sdalcinl #undef __FUNCT__ 3893fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 389471f87433Sdalcinl /*@ 3895fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 389671f87433Sdalcinl convergence criteria for the linear solvers within an inexact 389771f87433Sdalcinl Newton method. 389871f87433Sdalcinl 389971f87433Sdalcinl Not Collective 390071f87433Sdalcinl 390171f87433Sdalcinl Input Parameters: 390271f87433Sdalcinl snes - SNES context 390371f87433Sdalcinl 390471f87433Sdalcinl Output Parameters: 390571f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 390671f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 390771f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 390871f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 390971f87433Sdalcinl (0 <= gamma2 <= 1) 391071f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 391171f87433Sdalcinl . alpha2 - power for safeguard 391271f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 391371f87433Sdalcinl 391471f87433Sdalcinl Level: advanced 391571f87433Sdalcinl 391671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 391771f87433Sdalcinl 3918fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 391971f87433Sdalcinl @*/ 39207087cfbeSBarry Smith PetscErrorCode SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 392171f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 392271f87433Sdalcinl { 3923fa9f3622SBarry Smith SNESKSPEW *kctx; 392471f87433Sdalcinl PetscFunctionBegin; 39250700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3926fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3927e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 392871f87433Sdalcinl if(version) *version = kctx->version; 392971f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 393071f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 393171f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 393271f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 393371f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 393471f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 393571f87433Sdalcinl PetscFunctionReturn(0); 393671f87433Sdalcinl } 393771f87433Sdalcinl 393871f87433Sdalcinl #undef __FUNCT__ 3939fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 3940fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 394171f87433Sdalcinl { 394271f87433Sdalcinl PetscErrorCode ierr; 3943fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 394471f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 394571f87433Sdalcinl 394671f87433Sdalcinl PetscFunctionBegin; 3947e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 394871f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 394971f87433Sdalcinl rtol = kctx->rtol_0; 395071f87433Sdalcinl } else { 395171f87433Sdalcinl if (kctx->version == 1) { 395271f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 395371f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 395471f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 395571f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 395671f87433Sdalcinl } else if (kctx->version == 2) { 395771f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 395871f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 395971f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 396071f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 396171f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 396271f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 396371f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 396471f87433Sdalcinl stol = PetscMax(rtol,stol); 396571f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 396671f87433Sdalcinl /* safeguard: avoid oversolving */ 396771f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 396871f87433Sdalcinl stol = PetscMax(rtol,stol); 396971f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 3970e32f2f54SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 397171f87433Sdalcinl } 397271f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 397371f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 397471f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 397571f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 397671f87433Sdalcinl PetscFunctionReturn(0); 397771f87433Sdalcinl } 397871f87433Sdalcinl 397971f87433Sdalcinl #undef __FUNCT__ 3980fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 3981fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 398271f87433Sdalcinl { 398371f87433Sdalcinl PetscErrorCode ierr; 3984fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 398571f87433Sdalcinl PCSide pcside; 398671f87433Sdalcinl Vec lres; 398771f87433Sdalcinl 398871f87433Sdalcinl PetscFunctionBegin; 3989e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 399071f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 399171f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 399271f87433Sdalcinl if (kctx->version == 1) { 3993b037da10SBarry Smith ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr); 399471f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 399571f87433Sdalcinl /* KSP residual is true linear residual */ 399671f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 399771f87433Sdalcinl } else { 399871f87433Sdalcinl /* KSP residual is preconditioned residual */ 399971f87433Sdalcinl /* compute true linear residual norm */ 400071f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 400171f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 400271f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 400371f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 40046bf464f9SBarry Smith ierr = VecDestroy(&lres);CHKERRQ(ierr); 400571f87433Sdalcinl } 400671f87433Sdalcinl } 400771f87433Sdalcinl PetscFunctionReturn(0); 400871f87433Sdalcinl } 400971f87433Sdalcinl 401071f87433Sdalcinl #undef __FUNCT__ 401171f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 401271f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 401371f87433Sdalcinl { 401471f87433Sdalcinl PetscErrorCode ierr; 401571f87433Sdalcinl 401671f87433Sdalcinl PetscFunctionBegin; 4017fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 401871f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 4019fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 402071f87433Sdalcinl PetscFunctionReturn(0); 402171f87433Sdalcinl } 40226c699258SBarry Smith 40236c699258SBarry Smith #undef __FUNCT__ 40246c699258SBarry Smith #define __FUNCT__ "SNESSetDM" 40256c699258SBarry Smith /*@ 40266c699258SBarry Smith SNESSetDM - Sets the DM that may be used by some preconditioners 40276c699258SBarry Smith 40283f9fe445SBarry Smith Logically Collective on SNES 40296c699258SBarry Smith 40306c699258SBarry Smith Input Parameters: 40316c699258SBarry Smith + snes - the preconditioner context 40326c699258SBarry Smith - dm - the dm 40336c699258SBarry Smith 40346c699258SBarry Smith Level: intermediate 40356c699258SBarry Smith 40366c699258SBarry Smith 40376c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM() 40386c699258SBarry Smith @*/ 40397087cfbeSBarry Smith PetscErrorCode SNESSetDM(SNES snes,DM dm) 40406c699258SBarry Smith { 40416c699258SBarry Smith PetscErrorCode ierr; 4042345fed2cSBarry Smith KSP ksp; 40436cab3a1bSJed Brown SNESDM sdm; 40446c699258SBarry Smith 40456c699258SBarry Smith PetscFunctionBegin; 40460700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4047d0660788SBarry Smith if (dm) {ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);} 40486cab3a1bSJed Brown if (snes->dm) { /* Move the SNESDM context over to the new DM unless the new DM already has one */ 40496cab3a1bSJed Brown PetscContainer oldcontainer,container; 40506cab3a1bSJed Brown ierr = PetscObjectQuery((PetscObject)snes->dm,"SNESDM",(PetscObject*)&oldcontainer);CHKERRQ(ierr); 40516cab3a1bSJed Brown ierr = PetscObjectQuery((PetscObject)dm,"SNESDM",(PetscObject*)&container);CHKERRQ(ierr); 40526cab3a1bSJed Brown if (oldcontainer && !container) { 40536cab3a1bSJed Brown ierr = DMSNESCopyContext(snes->dm,dm);CHKERRQ(ierr); 40546cab3a1bSJed Brown ierr = DMSNESGetContext(snes->dm,&sdm);CHKERRQ(ierr); 40556cab3a1bSJed Brown if (sdm->originaldm == snes->dm) { /* Grant write privileges to the replacement DM */ 40566cab3a1bSJed Brown sdm->originaldm = dm; 40576cab3a1bSJed Brown } 40586cab3a1bSJed Brown } 40596bf464f9SBarry Smith ierr = DMDestroy(&snes->dm);CHKERRQ(ierr); 40606cab3a1bSJed Brown } 40616c699258SBarry Smith snes->dm = dm; 4062345fed2cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 4063345fed2cSBarry Smith ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr); 4064f22f69f0SBarry Smith ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr); 40652c155ee1SBarry Smith if (snes->pc) { 40662c155ee1SBarry Smith ierr = SNESSetDM(snes->pc, snes->dm);CHKERRQ(ierr); 40672c155ee1SBarry Smith } 40686c699258SBarry Smith PetscFunctionReturn(0); 40696c699258SBarry Smith } 40706c699258SBarry Smith 40716c699258SBarry Smith #undef __FUNCT__ 40726c699258SBarry Smith #define __FUNCT__ "SNESGetDM" 40736c699258SBarry Smith /*@ 40746c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 40756c699258SBarry Smith 40763f9fe445SBarry Smith Not Collective but DM obtained is parallel on SNES 40776c699258SBarry Smith 40786c699258SBarry Smith Input Parameter: 40796c699258SBarry Smith . snes - the preconditioner context 40806c699258SBarry Smith 40816c699258SBarry Smith Output Parameter: 40826c699258SBarry Smith . dm - the dm 40836c699258SBarry Smith 40846c699258SBarry Smith Level: intermediate 40856c699258SBarry Smith 40866c699258SBarry Smith 40876c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM() 40886c699258SBarry Smith @*/ 40897087cfbeSBarry Smith PetscErrorCode SNESGetDM(SNES snes,DM *dm) 40906c699258SBarry Smith { 40916cab3a1bSJed Brown PetscErrorCode ierr; 40926cab3a1bSJed Brown 40936c699258SBarry Smith PetscFunctionBegin; 40940700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 40956cab3a1bSJed Brown if (!snes->dm) { 40966cab3a1bSJed Brown ierr = DMShellCreate(((PetscObject)snes)->comm,&snes->dm);CHKERRQ(ierr); 40976cab3a1bSJed Brown } 40986c699258SBarry Smith *dm = snes->dm; 40996c699258SBarry Smith PetscFunctionReturn(0); 41006c699258SBarry Smith } 41010807856dSBarry Smith 410231823bd8SMatthew G Knepley #undef __FUNCT__ 410331823bd8SMatthew G Knepley #define __FUNCT__ "SNESSetPC" 410431823bd8SMatthew G Knepley /*@ 4105fd4b34e9SJed Brown SNESSetPC - Sets the nonlinear preconditioner to be used. 410631823bd8SMatthew G Knepley 410731823bd8SMatthew G Knepley Collective on SNES 410831823bd8SMatthew G Knepley 410931823bd8SMatthew G Knepley Input Parameters: 411031823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate() 411131823bd8SMatthew G Knepley - pc - the preconditioner object 411231823bd8SMatthew G Knepley 411331823bd8SMatthew G Knepley Notes: 411431823bd8SMatthew G Knepley Use SNESGetPC() to retrieve the preconditioner context (for example, 411531823bd8SMatthew G Knepley to configure it using the API). 411631823bd8SMatthew G Knepley 411731823bd8SMatthew G Knepley Level: developer 411831823bd8SMatthew G Knepley 411931823bd8SMatthew G Knepley .keywords: SNES, set, precondition 412031823bd8SMatthew G Knepley .seealso: SNESGetPC() 412131823bd8SMatthew G Knepley @*/ 412231823bd8SMatthew G Knepley PetscErrorCode SNESSetPC(SNES snes, SNES pc) 412331823bd8SMatthew G Knepley { 412431823bd8SMatthew G Knepley PetscErrorCode ierr; 412531823bd8SMatthew G Knepley 412631823bd8SMatthew G Knepley PetscFunctionBegin; 412731823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 412831823bd8SMatthew G Knepley PetscValidHeaderSpecific(pc, SNES_CLASSID, 2); 412931823bd8SMatthew G Knepley PetscCheckSameComm(snes, 1, pc, 2); 413031823bd8SMatthew G Knepley ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr); 4131bf11d3b6SBarry Smith ierr = SNESDestroy(&snes->pc);CHKERRQ(ierr); 413231823bd8SMatthew G Knepley snes->pc = pc; 413331823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 413431823bd8SMatthew G Knepley PetscFunctionReturn(0); 413531823bd8SMatthew G Knepley } 413631823bd8SMatthew G Knepley 413731823bd8SMatthew G Knepley #undef __FUNCT__ 413831823bd8SMatthew G Knepley #define __FUNCT__ "SNESGetPC" 413931823bd8SMatthew G Knepley /*@ 4140fd4b34e9SJed Brown SNESGetPC - Returns a pointer to the nonlinear preconditioning context set with SNESSetPC(). 414131823bd8SMatthew G Knepley 414231823bd8SMatthew G Knepley Not Collective 414331823bd8SMatthew G Knepley 414431823bd8SMatthew G Knepley Input Parameter: 414531823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate() 414631823bd8SMatthew G Knepley 414731823bd8SMatthew G Knepley Output Parameter: 414831823bd8SMatthew G Knepley . pc - preconditioner context 414931823bd8SMatthew G Knepley 415031823bd8SMatthew G Knepley Level: developer 415131823bd8SMatthew G Knepley 415231823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner 415331823bd8SMatthew G Knepley .seealso: SNESSetPC() 415431823bd8SMatthew G Knepley @*/ 415531823bd8SMatthew G Knepley PetscErrorCode SNESGetPC(SNES snes, SNES *pc) 415631823bd8SMatthew G Knepley { 415731823bd8SMatthew G Knepley PetscErrorCode ierr; 415831823bd8SMatthew G Knepley 415931823bd8SMatthew G Knepley PetscFunctionBegin; 416031823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 416131823bd8SMatthew G Knepley PetscValidPointer(pc, 2); 416231823bd8SMatthew G Knepley if (!snes->pc) { 416331823bd8SMatthew G Knepley ierr = SNESCreate(((PetscObject) snes)->comm, &snes->pc);CHKERRQ(ierr); 41644a0c5b0cSMatthew G Knepley ierr = PetscObjectIncrementTabLevel((PetscObject) snes->pc, (PetscObject) snes, 1);CHKERRQ(ierr); 416531823bd8SMatthew G Knepley ierr = PetscLogObjectParent(snes, snes->pc);CHKERRQ(ierr); 416631823bd8SMatthew G Knepley } 416731823bd8SMatthew G Knepley *pc = snes->pc; 416831823bd8SMatthew G Knepley PetscFunctionReturn(0); 416931823bd8SMatthew G Knepley } 417031823bd8SMatthew G Knepley 41719e764e56SPeter Brune #undef __FUNCT__ 4172*f1c6b773SPeter Brune #define __FUNCT__ "SNESSetSNESLineSearch" 41739e764e56SPeter Brune /*@ 4174*f1c6b773SPeter Brune SNESSetSNESLineSearch - Sets the linesearch. 41759e764e56SPeter Brune 41769e764e56SPeter Brune Collective on SNES 41779e764e56SPeter Brune 41789e764e56SPeter Brune Input Parameters: 41799e764e56SPeter Brune + snes - iterative context obtained from SNESCreate() 41809e764e56SPeter Brune - linesearch - the linesearch object 41819e764e56SPeter Brune 41829e764e56SPeter Brune Notes: 4183*f1c6b773SPeter Brune Use SNESGetSNESLineSearch() to retrieve the preconditioner context (for example, 41849e764e56SPeter Brune to configure it using the API). 41859e764e56SPeter Brune 41869e764e56SPeter Brune Level: developer 41879e764e56SPeter Brune 41889e764e56SPeter Brune .keywords: SNES, set, linesearch 4189*f1c6b773SPeter Brune .seealso: SNESGetSNESLineSearch() 41909e764e56SPeter Brune @*/ 4191*f1c6b773SPeter Brune PetscErrorCode SNESSetSNESLineSearch(SNES snes, SNESLineSearch linesearch) 41929e764e56SPeter Brune { 41939e764e56SPeter Brune PetscErrorCode ierr; 41949e764e56SPeter Brune 41959e764e56SPeter Brune PetscFunctionBegin; 41969e764e56SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4197*f1c6b773SPeter Brune PetscValidHeaderSpecific(linesearch, SNESLINESEARCH_CLASSID, 2); 41989e764e56SPeter Brune PetscCheckSameComm(snes, 1, linesearch, 2); 41999e764e56SPeter Brune ierr = PetscObjectReference((PetscObject) linesearch);CHKERRQ(ierr); 4200*f1c6b773SPeter Brune ierr = SNESLineSearchDestroy(&snes->linesearch);CHKERRQ(ierr); 42019e764e56SPeter Brune snes->linesearch = linesearch; 42029e764e56SPeter Brune ierr = PetscLogObjectParent(snes, snes->linesearch);CHKERRQ(ierr); 42039e764e56SPeter Brune PetscFunctionReturn(0); 42049e764e56SPeter Brune } 42059e764e56SPeter Brune 42069e764e56SPeter Brune #undef __FUNCT__ 4207*f1c6b773SPeter Brune #define __FUNCT__ "SNESGetSNESLineSearch" 4208ea5d4fccSPeter Brune /*@C 4209*f1c6b773SPeter Brune SNESGetSNESLineSearch - Returns a pointer to the line search context set with SNESSetLineSearch(). 42109e764e56SPeter Brune 42119e764e56SPeter Brune Not Collective 42129e764e56SPeter Brune 42139e764e56SPeter Brune Input Parameter: 42149e764e56SPeter Brune . snes - iterative context obtained from SNESCreate() 42159e764e56SPeter Brune 42169e764e56SPeter Brune Output Parameter: 42179e764e56SPeter Brune . linesearch - linesearch context 42189e764e56SPeter Brune 42199e764e56SPeter Brune Level: developer 42209e764e56SPeter Brune 42219e764e56SPeter Brune .keywords: SNES, get, linesearch 4222*f1c6b773SPeter Brune .seealso: SNESSetSNESLineSearch() 42239e764e56SPeter Brune @*/ 4224*f1c6b773SPeter Brune PetscErrorCode SNESGetSNESLineSearch(SNES snes, SNESLineSearch *linesearch) 42259e764e56SPeter Brune { 42269e764e56SPeter Brune PetscErrorCode ierr; 42279e764e56SPeter Brune const char *optionsprefix; 42289e764e56SPeter Brune 42299e764e56SPeter Brune PetscFunctionBegin; 42309e764e56SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 42319e764e56SPeter Brune PetscValidPointer(linesearch, 2); 42329e764e56SPeter Brune if (!snes->linesearch) { 42339e764e56SPeter Brune ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr); 4234*f1c6b773SPeter Brune ierr = SNESLineSearchCreate(((PetscObject) snes)->comm, &snes->linesearch);CHKERRQ(ierr); 4235*f1c6b773SPeter Brune ierr = SNESLineSearchSetSNES(snes->linesearch, snes);CHKERRQ(ierr); 42369e764e56SPeter Brune ierr = PetscObjectIncrementTabLevel((PetscObject) snes->linesearch, (PetscObject) snes, 1);CHKERRQ(ierr); 42379e764e56SPeter Brune ierr = PetscLogObjectParent(snes, snes->linesearch);CHKERRQ(ierr); 42389e764e56SPeter Brune } 42399e764e56SPeter Brune *linesearch = snes->linesearch; 42409e764e56SPeter Brune PetscFunctionReturn(0); 42419e764e56SPeter Brune } 42429e764e56SPeter Brune 424369b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4244c6db04a5SJed Brown #include <mex.h> 424569b4f73cSBarry Smith 42468f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 42478f6e6473SBarry Smith 42480807856dSBarry Smith #undef __FUNCT__ 42490807856dSBarry Smith #define __FUNCT__ "SNESComputeFunction_Matlab" 42500807856dSBarry Smith /* 42510807856dSBarry Smith SNESComputeFunction_Matlab - Calls the function that has been set with 42520807856dSBarry Smith SNESSetFunctionMatlab(). 42530807856dSBarry Smith 42540807856dSBarry Smith Collective on SNES 42550807856dSBarry Smith 42560807856dSBarry Smith Input Parameters: 42570807856dSBarry Smith + snes - the SNES context 42580807856dSBarry Smith - x - input vector 42590807856dSBarry Smith 42600807856dSBarry Smith Output Parameter: 42610807856dSBarry Smith . y - function vector, as set by SNESSetFunction() 42620807856dSBarry Smith 42630807856dSBarry Smith Notes: 42640807856dSBarry Smith SNESComputeFunction() is typically used within nonlinear solvers 42650807856dSBarry Smith implementations, so most users would not generally call this routine 42660807856dSBarry Smith themselves. 42670807856dSBarry Smith 42680807856dSBarry Smith Level: developer 42690807856dSBarry Smith 42700807856dSBarry Smith .keywords: SNES, nonlinear, compute, function 42710807856dSBarry Smith 42720807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 427361b2408cSBarry Smith */ 42747087cfbeSBarry Smith PetscErrorCode SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx) 42750807856dSBarry Smith { 4276e650e774SBarry Smith PetscErrorCode ierr; 42778f6e6473SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 42788f6e6473SBarry Smith int nlhs = 1,nrhs = 5; 42798f6e6473SBarry Smith mxArray *plhs[1],*prhs[5]; 428091621f2eSBarry Smith long long int lx = 0,ly = 0,ls = 0; 4281e650e774SBarry Smith 42820807856dSBarry Smith PetscFunctionBegin; 42830807856dSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 42840807856dSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 42850807856dSBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 42860807856dSBarry Smith PetscCheckSameComm(snes,1,x,2); 42870807856dSBarry Smith PetscCheckSameComm(snes,1,y,3); 42880807856dSBarry Smith 42890807856dSBarry Smith /* call Matlab function in ctx with arguments x and y */ 4290e650e774SBarry Smith 429191621f2eSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 4292e650e774SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 4293e650e774SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 429491621f2eSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 429591621f2eSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 429691621f2eSBarry Smith prhs[2] = mxCreateDoubleScalar((double)ly); 42978f6e6473SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 42988f6e6473SBarry Smith prhs[4] = sctx->ctx; 4299b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr); 4300e650e774SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4301e650e774SBarry Smith mxDestroyArray(prhs[0]); 4302e650e774SBarry Smith mxDestroyArray(prhs[1]); 4303e650e774SBarry Smith mxDestroyArray(prhs[2]); 43048f6e6473SBarry Smith mxDestroyArray(prhs[3]); 4305e650e774SBarry Smith mxDestroyArray(plhs[0]); 43060807856dSBarry Smith PetscFunctionReturn(0); 43070807856dSBarry Smith } 43080807856dSBarry Smith 43090807856dSBarry Smith 43100807856dSBarry Smith #undef __FUNCT__ 43110807856dSBarry Smith #define __FUNCT__ "SNESSetFunctionMatlab" 431261b2408cSBarry Smith /* 43130807856dSBarry Smith SNESSetFunctionMatlab - Sets the function evaluation routine and function 43140807856dSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 4315e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 43160807856dSBarry Smith 43170807856dSBarry Smith Logically Collective on SNES 43180807856dSBarry Smith 43190807856dSBarry Smith Input Parameters: 43200807856dSBarry Smith + snes - the SNES context 43210807856dSBarry Smith . r - vector to store function value 43220807856dSBarry Smith - func - function evaluation routine 43230807856dSBarry Smith 43240807856dSBarry Smith Calling sequence of func: 432561b2408cSBarry Smith $ func (SNES snes,Vec x,Vec f,void *ctx); 43260807856dSBarry Smith 43270807856dSBarry Smith 43280807856dSBarry Smith Notes: 43290807856dSBarry Smith The Newton-like methods typically solve linear systems of the form 43300807856dSBarry Smith $ f'(x) x = -f(x), 43310807856dSBarry Smith where f'(x) denotes the Jacobian matrix and f(x) is the function. 43320807856dSBarry Smith 43330807856dSBarry Smith Level: beginner 43340807856dSBarry Smith 43350807856dSBarry Smith .keywords: SNES, nonlinear, set, function 43360807856dSBarry Smith 43370807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 433861b2408cSBarry Smith */ 43397087cfbeSBarry Smith PetscErrorCode SNESSetFunctionMatlab(SNES snes,Vec r,const char *func,mxArray *ctx) 43400807856dSBarry Smith { 43410807856dSBarry Smith PetscErrorCode ierr; 43428f6e6473SBarry Smith SNESMatlabContext *sctx; 43430807856dSBarry Smith 43440807856dSBarry Smith PetscFunctionBegin; 43458f6e6473SBarry Smith /* currently sctx is memory bleed */ 43468f6e6473SBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 43478f6e6473SBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 43488f6e6473SBarry Smith /* 43498f6e6473SBarry Smith This should work, but it doesn't 43508f6e6473SBarry Smith sctx->ctx = ctx; 43518f6e6473SBarry Smith mexMakeArrayPersistent(sctx->ctx); 43528f6e6473SBarry Smith */ 43538f6e6473SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 43548f6e6473SBarry Smith ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr); 43550807856dSBarry Smith PetscFunctionReturn(0); 43560807856dSBarry Smith } 435769b4f73cSBarry Smith 435861b2408cSBarry Smith #undef __FUNCT__ 435961b2408cSBarry Smith #define __FUNCT__ "SNESComputeJacobian_Matlab" 436061b2408cSBarry Smith /* 436161b2408cSBarry Smith SNESComputeJacobian_Matlab - Calls the function that has been set with 436261b2408cSBarry Smith SNESSetJacobianMatlab(). 436361b2408cSBarry Smith 436461b2408cSBarry Smith Collective on SNES 436561b2408cSBarry Smith 436661b2408cSBarry Smith Input Parameters: 436761b2408cSBarry Smith + snes - the SNES context 436861b2408cSBarry Smith . x - input vector 436961b2408cSBarry Smith . A, B - the matrices 437061b2408cSBarry Smith - ctx - user context 437161b2408cSBarry Smith 437261b2408cSBarry Smith Output Parameter: 437361b2408cSBarry Smith . flag - structure of the matrix 437461b2408cSBarry Smith 437561b2408cSBarry Smith Level: developer 437661b2408cSBarry Smith 437761b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function 437861b2408cSBarry Smith 437961b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 438061b2408cSBarry Smith @*/ 43817087cfbeSBarry Smith PetscErrorCode SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag, void *ctx) 438261b2408cSBarry Smith { 438361b2408cSBarry Smith PetscErrorCode ierr; 438461b2408cSBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 438561b2408cSBarry Smith int nlhs = 2,nrhs = 6; 438661b2408cSBarry Smith mxArray *plhs[2],*prhs[6]; 438761b2408cSBarry Smith long long int lx = 0,lA = 0,ls = 0, lB = 0; 438861b2408cSBarry Smith 438961b2408cSBarry Smith PetscFunctionBegin; 439061b2408cSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 439161b2408cSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 439261b2408cSBarry Smith 439361b2408cSBarry Smith /* call Matlab function in ctx with arguments x and y */ 439461b2408cSBarry Smith 439561b2408cSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 439661b2408cSBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 439761b2408cSBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 439861b2408cSBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 439961b2408cSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 440061b2408cSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 440161b2408cSBarry Smith prhs[2] = mxCreateDoubleScalar((double)lA); 440261b2408cSBarry Smith prhs[3] = mxCreateDoubleScalar((double)lB); 440361b2408cSBarry Smith prhs[4] = mxCreateString(sctx->funcname); 440461b2408cSBarry Smith prhs[5] = sctx->ctx; 4405b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr); 440661b2408cSBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 440761b2408cSBarry Smith *flag = (MatStructure) mxGetScalar(plhs[1]);CHKERRQ(ierr); 440861b2408cSBarry Smith mxDestroyArray(prhs[0]); 440961b2408cSBarry Smith mxDestroyArray(prhs[1]); 441061b2408cSBarry Smith mxDestroyArray(prhs[2]); 441161b2408cSBarry Smith mxDestroyArray(prhs[3]); 441261b2408cSBarry Smith mxDestroyArray(prhs[4]); 441361b2408cSBarry Smith mxDestroyArray(plhs[0]); 441461b2408cSBarry Smith mxDestroyArray(plhs[1]); 441561b2408cSBarry Smith PetscFunctionReturn(0); 441661b2408cSBarry Smith } 441761b2408cSBarry Smith 441861b2408cSBarry Smith 441961b2408cSBarry Smith #undef __FUNCT__ 442061b2408cSBarry Smith #define __FUNCT__ "SNESSetJacobianMatlab" 442161b2408cSBarry Smith /* 442261b2408cSBarry Smith SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 442361b2408cSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 4424e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 442561b2408cSBarry Smith 442661b2408cSBarry Smith Logically Collective on SNES 442761b2408cSBarry Smith 442861b2408cSBarry Smith Input Parameters: 442961b2408cSBarry Smith + snes - the SNES context 443061b2408cSBarry Smith . A,B - Jacobian matrices 443161b2408cSBarry Smith . func - function evaluation routine 443261b2408cSBarry Smith - ctx - user context 443361b2408cSBarry Smith 443461b2408cSBarry Smith Calling sequence of func: 443561b2408cSBarry Smith $ flag = func (SNES snes,Vec x,Mat A,Mat B,void *ctx); 443661b2408cSBarry Smith 443761b2408cSBarry Smith 443861b2408cSBarry Smith Level: developer 443961b2408cSBarry Smith 444061b2408cSBarry Smith .keywords: SNES, nonlinear, set, function 444161b2408cSBarry Smith 444261b2408cSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 444361b2408cSBarry Smith */ 44447087cfbeSBarry Smith PetscErrorCode SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *func,mxArray *ctx) 444561b2408cSBarry Smith { 444661b2408cSBarry Smith PetscErrorCode ierr; 444761b2408cSBarry Smith SNESMatlabContext *sctx; 444861b2408cSBarry Smith 444961b2408cSBarry Smith PetscFunctionBegin; 445061b2408cSBarry Smith /* currently sctx is memory bleed */ 445161b2408cSBarry Smith ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 445261b2408cSBarry Smith ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 445361b2408cSBarry Smith /* 445461b2408cSBarry Smith This should work, but it doesn't 445561b2408cSBarry Smith sctx->ctx = ctx; 445661b2408cSBarry Smith mexMakeArrayPersistent(sctx->ctx); 445761b2408cSBarry Smith */ 445861b2408cSBarry Smith sctx->ctx = mxDuplicateArray(ctx); 445961b2408cSBarry Smith ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 446061b2408cSBarry Smith PetscFunctionReturn(0); 446161b2408cSBarry Smith } 446269b4f73cSBarry Smith 4463f9eb7ae2SShri Abhyankar #undef __FUNCT__ 4464f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitor_Matlab" 4465f9eb7ae2SShri Abhyankar /* 4466f9eb7ae2SShri Abhyankar SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab(). 4467f9eb7ae2SShri Abhyankar 4468f9eb7ae2SShri Abhyankar Collective on SNES 4469f9eb7ae2SShri Abhyankar 4470f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction() 4471f9eb7ae2SShri Abhyankar @*/ 44727087cfbeSBarry Smith PetscErrorCode SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx) 4473f9eb7ae2SShri Abhyankar { 4474f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 447548f37e70SShri Abhyankar SNESMatlabContext *sctx = (SNESMatlabContext *)ctx; 4476f9eb7ae2SShri Abhyankar int nlhs = 1,nrhs = 6; 4477f9eb7ae2SShri Abhyankar mxArray *plhs[1],*prhs[6]; 4478f9eb7ae2SShri Abhyankar long long int lx = 0,ls = 0; 4479f9eb7ae2SShri Abhyankar Vec x=snes->vec_sol; 4480f9eb7ae2SShri Abhyankar 4481f9eb7ae2SShri Abhyankar PetscFunctionBegin; 4482f9eb7ae2SShri Abhyankar PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4483f9eb7ae2SShri Abhyankar 4484f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 4485f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 4486f9eb7ae2SShri Abhyankar prhs[0] = mxCreateDoubleScalar((double)ls); 4487f9eb7ae2SShri Abhyankar prhs[1] = mxCreateDoubleScalar((double)it); 4488f9eb7ae2SShri Abhyankar prhs[2] = mxCreateDoubleScalar((double)fnorm); 4489f9eb7ae2SShri Abhyankar prhs[3] = mxCreateDoubleScalar((double)lx); 4490f9eb7ae2SShri Abhyankar prhs[4] = mxCreateString(sctx->funcname); 4491f9eb7ae2SShri Abhyankar prhs[5] = sctx->ctx; 4492f9eb7ae2SShri Abhyankar ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr); 4493f9eb7ae2SShri Abhyankar ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 4494f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[0]); 4495f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[1]); 4496f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[2]); 4497f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[3]); 4498f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[4]); 4499f9eb7ae2SShri Abhyankar mxDestroyArray(plhs[0]); 4500f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 4501f9eb7ae2SShri Abhyankar } 4502f9eb7ae2SShri Abhyankar 4503f9eb7ae2SShri Abhyankar 4504f9eb7ae2SShri Abhyankar #undef __FUNCT__ 4505f9eb7ae2SShri Abhyankar #define __FUNCT__ "SNESMonitorSetMatlab" 4506f9eb7ae2SShri Abhyankar /* 4507e3c5b3baSBarry Smith SNESMonitorSetMatlab - Sets the monitor function from MATLAB 4508f9eb7ae2SShri Abhyankar 4509f9eb7ae2SShri Abhyankar Level: developer 4510f9eb7ae2SShri Abhyankar 4511f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function 4512f9eb7ae2SShri Abhyankar 4513f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 4514f9eb7ae2SShri Abhyankar */ 45157087cfbeSBarry Smith PetscErrorCode SNESMonitorSetMatlab(SNES snes,const char *func,mxArray *ctx) 4516f9eb7ae2SShri Abhyankar { 4517f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 4518f9eb7ae2SShri Abhyankar SNESMatlabContext *sctx; 4519f9eb7ae2SShri Abhyankar 4520f9eb7ae2SShri Abhyankar PetscFunctionBegin; 4521f9eb7ae2SShri Abhyankar /* currently sctx is memory bleed */ 4522f9eb7ae2SShri Abhyankar ierr = PetscMalloc(sizeof(SNESMatlabContext),&sctx);CHKERRQ(ierr); 4523f9eb7ae2SShri Abhyankar ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr); 4524f9eb7ae2SShri Abhyankar /* 4525f9eb7ae2SShri Abhyankar This should work, but it doesn't 4526f9eb7ae2SShri Abhyankar sctx->ctx = ctx; 4527f9eb7ae2SShri Abhyankar mexMakeArrayPersistent(sctx->ctx); 4528f9eb7ae2SShri Abhyankar */ 4529f9eb7ae2SShri Abhyankar sctx->ctx = mxDuplicateArray(ctx); 4530f9eb7ae2SShri Abhyankar ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,PETSC_NULL);CHKERRQ(ierr); 4531f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 4532f9eb7ae2SShri Abhyankar } 4533f9eb7ae2SShri Abhyankar 453469b4f73cSBarry Smith #endif 4535