1af0996ceSBarry Smith #include <petsc/private/snesimpl.h> /*I "petscsnes.h" I*/ 207475bc1SBarry Smith #include <petscdmshell.h> 3d96771aaSLisandro Dalcin #include <petscdraw.h> 4a01aa210SMatthew G. Knepley #include <petscds.h> 534b4d3a8SMatthew G. Knepley #include <petscdmadaptor.h> 606fc46c8SMatthew G. Knepley #include <petscconvest.h> 79b94acceSBarry Smith 8ace3abfcSBarry Smith PetscBool SNESRegisterAllCalled = PETSC_FALSE; 90298fd71SBarry Smith PetscFunctionList SNESList = NULL; 108ba1e511SMatthew Knepley 118ba1e511SMatthew Knepley /* Logging support */ 1222c6f798SBarry Smith PetscClassId SNES_CLASSID, DMSNES_CLASSID; 13e3ed9ee7SBarry Smith PetscLogEvent SNES_Solve, SNES_Setup, SNES_FunctionEval, SNES_JacobianEval, SNES_NGSEval, SNES_NGSFuncEval, SNES_NPCSolve, SNES_ObjectiveEval; 14a09944afSBarry Smith 15e113a28aSBarry Smith /*@ 16e113a28aSBarry Smith SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged. 17e113a28aSBarry Smith 183f9fe445SBarry Smith Logically Collective on SNES 19e113a28aSBarry Smith 20e113a28aSBarry Smith Input Parameters: 21e113a28aSBarry Smith + snes - iterative context obtained from SNESCreate() 22e113a28aSBarry Smith - flg - PETSC_TRUE indicates you want the error generated 23e113a28aSBarry Smith 24e113a28aSBarry Smith Options database keys: 2567b8a455SSatish Balay . -snes_error_if_not_converged <true,false> - cause an immediate error condition and stop the program if the solver does not converge 26e113a28aSBarry Smith 27e113a28aSBarry Smith Level: intermediate 28e113a28aSBarry Smith 29e113a28aSBarry Smith Notes: 30e113a28aSBarry Smith Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve() 31e113a28aSBarry Smith to determine if it has converged. 32e113a28aSBarry Smith 33db781477SPatrick Sanan .seealso: `SNESGetErrorIfNotConverged()`, `KSPGetErrorIfNotConverged()`, `KSPSetErrorIfNotConverged()` 34e113a28aSBarry Smith @*/ 357087cfbeSBarry Smith PetscErrorCode SNESSetErrorIfNotConverged(SNES snes,PetscBool flg) 36e113a28aSBarry Smith { 37e113a28aSBarry Smith PetscFunctionBegin; 38e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 39acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flg,2); 40e113a28aSBarry Smith snes->errorifnotconverged = flg; 41e113a28aSBarry Smith PetscFunctionReturn(0); 42e113a28aSBarry Smith } 43e113a28aSBarry Smith 44e113a28aSBarry Smith /*@ 45e113a28aSBarry Smith SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge? 46e113a28aSBarry Smith 47e113a28aSBarry Smith Not Collective 48e113a28aSBarry Smith 49e113a28aSBarry Smith Input Parameter: 50e113a28aSBarry Smith . snes - iterative context obtained from SNESCreate() 51e113a28aSBarry Smith 52e113a28aSBarry Smith Output Parameter: 53e113a28aSBarry Smith . flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE 54e113a28aSBarry Smith 55e113a28aSBarry Smith Level: intermediate 56e113a28aSBarry Smith 57db781477SPatrick Sanan .seealso: `SNESSetErrorIfNotConverged()`, `KSPGetErrorIfNotConverged()`, `KSPSetErrorIfNotConverged()` 58e113a28aSBarry Smith @*/ 597087cfbeSBarry Smith PetscErrorCode SNESGetErrorIfNotConverged(SNES snes,PetscBool *flag) 60e113a28aSBarry Smith { 61e113a28aSBarry Smith PetscFunctionBegin; 62e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 63534a8f05SLisandro Dalcin PetscValidBoolPointer(flag,2); 64e113a28aSBarry Smith *flag = snes->errorifnotconverged; 65e113a28aSBarry Smith PetscFunctionReturn(0); 66e113a28aSBarry Smith } 67e113a28aSBarry Smith 684fc747eaSLawrence Mitchell /*@ 694fc747eaSLawrence Mitchell SNESSetAlwaysComputesFinalResidual - does the SNES always compute the residual at the final solution? 704fc747eaSLawrence Mitchell 714fc747eaSLawrence Mitchell Logically Collective on SNES 724fc747eaSLawrence Mitchell 734fc747eaSLawrence Mitchell Input Parameters: 744fc747eaSLawrence Mitchell + snes - the shell SNES 754fc747eaSLawrence Mitchell - flg - is the residual computed? 764fc747eaSLawrence Mitchell 774fc747eaSLawrence Mitchell Level: advanced 784fc747eaSLawrence Mitchell 79db781477SPatrick Sanan .seealso: `SNESGetAlwaysComputesFinalResidual()` 804fc747eaSLawrence Mitchell @*/ 814fc747eaSLawrence Mitchell PetscErrorCode SNESSetAlwaysComputesFinalResidual(SNES snes, PetscBool flg) 824fc747eaSLawrence Mitchell { 834fc747eaSLawrence Mitchell PetscFunctionBegin; 844fc747eaSLawrence Mitchell PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 854fc747eaSLawrence Mitchell snes->alwayscomputesfinalresidual = flg; 864fc747eaSLawrence Mitchell PetscFunctionReturn(0); 874fc747eaSLawrence Mitchell } 884fc747eaSLawrence Mitchell 894fc747eaSLawrence Mitchell /*@ 904fc747eaSLawrence Mitchell SNESGetAlwaysComputesFinalResidual - does the SNES always compute the residual at the final solution? 914fc747eaSLawrence Mitchell 924fc747eaSLawrence Mitchell Logically Collective on SNES 934fc747eaSLawrence Mitchell 944fc747eaSLawrence Mitchell Input Parameter: 954fc747eaSLawrence Mitchell . snes - the shell SNES 964fc747eaSLawrence Mitchell 974fc747eaSLawrence Mitchell Output Parameter: 984fc747eaSLawrence Mitchell . flg - is the residual computed? 994fc747eaSLawrence Mitchell 1004fc747eaSLawrence Mitchell Level: advanced 1014fc747eaSLawrence Mitchell 102db781477SPatrick Sanan .seealso: `SNESSetAlwaysComputesFinalResidual()` 1034fc747eaSLawrence Mitchell @*/ 1044fc747eaSLawrence Mitchell PetscErrorCode SNESGetAlwaysComputesFinalResidual(SNES snes, PetscBool *flg) 1054fc747eaSLawrence Mitchell { 1064fc747eaSLawrence Mitchell PetscFunctionBegin; 1074fc747eaSLawrence Mitchell PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1084fc747eaSLawrence Mitchell *flg = snes->alwayscomputesfinalresidual; 1094fc747eaSLawrence Mitchell PetscFunctionReturn(0); 1104fc747eaSLawrence Mitchell } 1114fc747eaSLawrence Mitchell 112e725d27bSBarry Smith /*@ 113bf388a1fSBarry Smith SNESSetFunctionDomainError - tells SNES that the input vector to your SNESFunction is not 1144936397dSBarry Smith in the functions domain. For example, negative pressure. 1154936397dSBarry Smith 1163f9fe445SBarry Smith Logically Collective on SNES 1174936397dSBarry Smith 1184936397dSBarry Smith Input Parameters: 1196a388c36SPeter Brune . snes - the SNES context 1204936397dSBarry Smith 12128529972SSatish Balay Level: advanced 1224936397dSBarry Smith 123db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESSetFunction()`, `SNESFunction` 1244936397dSBarry Smith @*/ 1257087cfbeSBarry Smith PetscErrorCode SNESSetFunctionDomainError(SNES snes) 1264936397dSBarry Smith { 1274936397dSBarry Smith PetscFunctionBegin; 1280700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1295f80ce2aSJacob Faibussowitsch PetscCheck(!snes->errorifnotconverged,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"User code indicates input vector is not in the function domain"); 1304936397dSBarry Smith snes->domainerror = PETSC_TRUE; 1314936397dSBarry Smith PetscFunctionReturn(0); 1324936397dSBarry Smith } 1334936397dSBarry Smith 1346a388c36SPeter Brune /*@ 13507b62357SFande Kong SNESSetJacobianDomainError - tells SNES that computeJacobian does not make sense any more. For example there is a negative element transformation. 13607b62357SFande Kong 13707b62357SFande Kong Logically Collective on SNES 13807b62357SFande Kong 13907b62357SFande Kong Input Parameters: 14007b62357SFande Kong . snes - the SNES context 14107b62357SFande Kong 14207b62357SFande Kong Level: advanced 14307b62357SFande Kong 144db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESSetFunction()`, `SNESFunction()`, `SNESSetFunctionDomainError()` 14507b62357SFande Kong @*/ 14607b62357SFande Kong PetscErrorCode SNESSetJacobianDomainError(SNES snes) 14707b62357SFande Kong { 14807b62357SFande Kong PetscFunctionBegin; 14907b62357SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1505f80ce2aSJacob Faibussowitsch PetscCheck(!snes->errorifnotconverged,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"User code indicates computeJacobian does not make sense"); 15107b62357SFande Kong snes->jacobiandomainerror = PETSC_TRUE; 15207b62357SFande Kong PetscFunctionReturn(0); 15307b62357SFande Kong } 15407b62357SFande Kong 15507b62357SFande Kong /*@ 156b351a90bSFande Kong SNESSetCheckJacobianDomainError - if or not to check jacobian domain error after each Jacobian evaluation. By default, we check Jacobian domain error 157b351a90bSFande Kong in the debug mode, and do not check it in the optimized mode. 158b351a90bSFande Kong 159b351a90bSFande Kong Logically Collective on SNES 160b351a90bSFande Kong 161b351a90bSFande Kong Input Parameters: 162a2b725a8SWilliam Gropp + snes - the SNES context 163a2b725a8SWilliam Gropp - flg - indicates if or not to check jacobian domain error after each Jacobian evaluation 164b351a90bSFande Kong 165b351a90bSFande Kong Level: advanced 166b351a90bSFande Kong 167db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESSetFunction()`, `SNESFunction()`, `SNESSetFunctionDomainError()`, `SNESGetCheckJacobianDomainError()` 168b351a90bSFande Kong @*/ 169b351a90bSFande Kong PetscErrorCode SNESSetCheckJacobianDomainError(SNES snes, PetscBool flg) 170b351a90bSFande Kong { 171b351a90bSFande Kong PetscFunctionBegin; 172b351a90bSFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 173b351a90bSFande Kong snes->checkjacdomainerror = flg; 174b351a90bSFande Kong PetscFunctionReturn(0); 175b351a90bSFande Kong } 176b351a90bSFande Kong 177b351a90bSFande Kong /*@ 1788383d7d7SFande Kong SNESGetCheckJacobianDomainError - Get an indicator whether or not we are checking Jacobian domain errors after each Jacobian evaluation. 1798383d7d7SFande Kong 1808383d7d7SFande Kong Logically Collective on SNES 1818383d7d7SFande Kong 1828383d7d7SFande Kong Input Parameters: 1838383d7d7SFande Kong . snes - the SNES context 1848383d7d7SFande Kong 1858383d7d7SFande Kong Output Parameters: 1868383d7d7SFande Kong . flg - PETSC_FALSE indicates that we don't check jacobian domain errors after each Jacobian evaluation 1878383d7d7SFande Kong 1888383d7d7SFande Kong Level: advanced 1898383d7d7SFande Kong 190db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESSetFunction()`, `SNESFunction()`, `SNESSetFunctionDomainError()`, `SNESSetCheckJacobianDomainError()` 1918383d7d7SFande Kong @*/ 1928383d7d7SFande Kong PetscErrorCode SNESGetCheckJacobianDomainError(SNES snes, PetscBool *flg) 1938383d7d7SFande Kong { 1948383d7d7SFande Kong PetscFunctionBegin; 1958383d7d7SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 196534a8f05SLisandro Dalcin PetscValidBoolPointer(flg,2); 1978383d7d7SFande Kong *flg = snes->checkjacdomainerror; 1988383d7d7SFande Kong PetscFunctionReturn(0); 1998383d7d7SFande Kong } 2008383d7d7SFande Kong 2018383d7d7SFande Kong /*@ 202c77b2880SPeter Brune SNESGetFunctionDomainError - Gets the status of the domain error after a call to SNESComputeFunction; 2036a388c36SPeter Brune 2046a388c36SPeter Brune Logically Collective on SNES 2056a388c36SPeter Brune 2066a388c36SPeter Brune Input Parameters: 2076a388c36SPeter Brune . snes - the SNES context 2086a388c36SPeter Brune 2096a388c36SPeter Brune Output Parameters: 210bf388a1fSBarry Smith . domainerror - Set to PETSC_TRUE if there's a domain error; PETSC_FALSE otherwise. 2116a388c36SPeter Brune 2126a388c36SPeter Brune Level: advanced 2136a388c36SPeter Brune 214db781477SPatrick Sanan .seealso: `SNESSetFunctionDomainError()`, `SNESComputeFunction()` 2156a388c36SPeter Brune @*/ 2166a388c36SPeter Brune PetscErrorCode SNESGetFunctionDomainError(SNES snes, PetscBool *domainerror) 2176a388c36SPeter Brune { 2186a388c36SPeter Brune PetscFunctionBegin; 2196a388c36SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 220534a8f05SLisandro Dalcin PetscValidBoolPointer(domainerror,2); 2216a388c36SPeter Brune *domainerror = snes->domainerror; 2226a388c36SPeter Brune PetscFunctionReturn(0); 2236a388c36SPeter Brune } 2246a388c36SPeter Brune 22507b62357SFande Kong /*@ 22607b62357SFande Kong SNESGetJacobianDomainError - Gets the status of the Jacobian domain error after a call to SNESComputeJacobian; 22707b62357SFande Kong 22807b62357SFande Kong Logically Collective on SNES 22907b62357SFande Kong 23007b62357SFande Kong Input Parameters: 23107b62357SFande Kong . snes - the SNES context 23207b62357SFande Kong 23307b62357SFande Kong Output Parameters: 23407b62357SFande Kong . domainerror - Set to PETSC_TRUE if there's a jacobian domain error; PETSC_FALSE otherwise. 23507b62357SFande Kong 23607b62357SFande Kong Level: advanced 23707b62357SFande Kong 238c2e3fba1SPatrick Sanan .seealso: `SNESSetFunctionDomainError()`, `SNESComputeFunction()`, `SNESGetFunctionDomainError()` 23907b62357SFande Kong @*/ 24007b62357SFande Kong PetscErrorCode SNESGetJacobianDomainError(SNES snes, PetscBool *domainerror) 24107b62357SFande Kong { 24207b62357SFande Kong PetscFunctionBegin; 24307b62357SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 244534a8f05SLisandro Dalcin PetscValidBoolPointer(domainerror,2); 24507b62357SFande Kong *domainerror = snes->jacobiandomainerror; 24607b62357SFande Kong PetscFunctionReturn(0); 24707b62357SFande Kong } 24807b62357SFande Kong 24955849f57SBarry Smith /*@C 25055849f57SBarry Smith SNESLoad - Loads a SNES that has been stored in binary with SNESView(). 25155849f57SBarry Smith 25255849f57SBarry Smith Collective on PetscViewer 25355849f57SBarry Smith 25455849f57SBarry Smith Input Parameters: 25555849f57SBarry Smith + newdm - the newly loaded SNES, this needs to have been created with SNESCreate() or 25655849f57SBarry Smith some related function before a call to SNESLoad(). 25755849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 25855849f57SBarry Smith 25955849f57SBarry Smith Level: intermediate 26055849f57SBarry Smith 26155849f57SBarry Smith Notes: 26255849f57SBarry Smith The type is determined by the data in the file, any type set into the SNES before this call is ignored. 26355849f57SBarry Smith 26455849f57SBarry Smith Notes for advanced users: 26555849f57SBarry Smith Most users should not need to know the details of the binary storage 26655849f57SBarry Smith format, since SNESLoad() and TSView() completely hide these details. 26755849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 26855849f57SBarry Smith format is 26955849f57SBarry Smith .vb 27055849f57SBarry Smith has not yet been determined 27155849f57SBarry Smith .ve 27255849f57SBarry Smith 273db781477SPatrick Sanan .seealso: `PetscViewerBinaryOpen()`, `SNESView()`, `MatLoad()`, `VecLoad()` 27455849f57SBarry Smith @*/ 2752d53ad75SBarry Smith PetscErrorCode SNESLoad(SNES snes, PetscViewer viewer) 27655849f57SBarry Smith { 27755849f57SBarry Smith PetscBool isbinary; 278060da220SMatthew G. Knepley PetscInt classid; 27955849f57SBarry Smith char type[256]; 28055849f57SBarry Smith KSP ksp; 2812d53ad75SBarry Smith DM dm; 2822d53ad75SBarry Smith DMSNES dmsnes; 28355849f57SBarry Smith 28455849f57SBarry Smith PetscFunctionBegin; 2852d53ad75SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 28655849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 2879566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 2885f80ce2aSJacob Faibussowitsch PetscCheck(isbinary,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 28955849f57SBarry Smith 2909566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT)); 2915f80ce2aSJacob Faibussowitsch PetscCheck(classid == SNES_FILE_CLASSID,PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONG,"Not SNES next in file"); 2929566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR)); 2939566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes, type)); 294*1baa6e33SBarry Smith if (snes->ops->load) PetscCall((*snes->ops->load)(snes,viewer)); 2959566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 2969566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&dmsnes)); 2979566063dSJacob Faibussowitsch PetscCall(DMSNESLoad(dmsnes,viewer)); 2989566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&ksp)); 2999566063dSJacob Faibussowitsch PetscCall(KSPLoad(ksp,viewer)); 30055849f57SBarry Smith PetscFunctionReturn(0); 30155849f57SBarry Smith } 3026a388c36SPeter Brune 3039804daf3SBarry Smith #include <petscdraw.h> 304e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 305e04113cfSBarry Smith #include <petscviewersaws.h> 306bfb97211SBarry Smith #endif 3078404b7f3SBarry Smith 308fe2efc57SMark /*@C 309fe2efc57SMark SNESViewFromOptions - View from Options 310fe2efc57SMark 311fe2efc57SMark Collective on SNES 312fe2efc57SMark 313fe2efc57SMark Input Parameters: 314fe2efc57SMark + A - the application ordering context 315736c3998SJose E. Roman . obj - Optional object 316736c3998SJose E. Roman - name - command line option 317fe2efc57SMark 318fe2efc57SMark Level: intermediate 319db781477SPatrick Sanan .seealso: `SNES`, `SNESView`, `PetscObjectViewFromOptions()`, `SNESCreate()` 320fe2efc57SMark @*/ 321fe2efc57SMark PetscErrorCode SNESViewFromOptions(SNES A,PetscObject obj,const char name[]) 322fe2efc57SMark { 323fe2efc57SMark PetscFunctionBegin; 324fe2efc57SMark PetscValidHeaderSpecific(A,SNES_CLASSID,1); 3259566063dSJacob Faibussowitsch PetscCall(PetscObjectViewFromOptions((PetscObject)A,obj,name)); 326fe2efc57SMark PetscFunctionReturn(0); 327fe2efc57SMark } 328fe2efc57SMark 329789d8953SBarry Smith PETSC_EXTERN PetscErrorCode SNESComputeJacobian_DMDA(SNES,Vec,Mat,Mat,void*); 330789d8953SBarry Smith 3317e2c5f70SBarry Smith /*@C 3329b94acceSBarry Smith SNESView - Prints the SNES data structure. 3339b94acceSBarry Smith 3344c49b128SBarry Smith Collective on SNES 335fee21e36SBarry Smith 336c7afd0dbSLois Curfman McInnes Input Parameters: 337c7afd0dbSLois Curfman McInnes + SNES - the SNES context 338c7afd0dbSLois Curfman McInnes - viewer - visualization context 339c7afd0dbSLois Curfman McInnes 3409b94acceSBarry Smith Options Database Key: 341c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 3429b94acceSBarry Smith 3439b94acceSBarry Smith Notes: 3449b94acceSBarry Smith The available visualization contexts include 345b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 346b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 347c8a8ba5cSLois Curfman McInnes output where only the first processor opens 348c8a8ba5cSLois Curfman McInnes the file. All other processors send their 349c8a8ba5cSLois Curfman McInnes data to the first processor to print. 3509b94acceSBarry Smith 351052bf0daSPierre Jolivet The available formats include 352052bf0daSPierre Jolivet + PETSC_VIEWER_DEFAULT - standard output (default) 353052bf0daSPierre Jolivet - PETSC_VIEWER_ASCII_INFO_DETAIL - more verbose output for SNESNASM 354052bf0daSPierre Jolivet 3553e081fefSLois Curfman McInnes The user can open an alternative visualization context with 356b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 3579b94acceSBarry Smith 358595c91d4SBarry Smith In the debugger you can do "call SNESView(snes,0)" to display the SNES solver. (The same holds for any PETSc object viewer). 359595c91d4SBarry Smith 36036851e7fSLois Curfman McInnes Level: beginner 36136851e7fSLois Curfman McInnes 362db781477SPatrick Sanan .seealso: `PetscViewerASCIIOpen()` 3639b94acceSBarry Smith @*/ 3647087cfbeSBarry Smith PetscErrorCode SNESView(SNES snes,PetscViewer viewer) 3659b94acceSBarry Smith { 366fa9f3622SBarry Smith SNESKSPEW *kctx; 36794b7f48cSBarry Smith KSP ksp; 3687f1410a3SPeter Brune SNESLineSearch linesearch; 36972a02f06SBarry Smith PetscBool iascii,isstring,isbinary,isdraw; 3702d53ad75SBarry Smith DMSNES dmsnes; 371e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 372536b137fSBarry Smith PetscBool issaws; 373bfb97211SBarry Smith #endif 3749b94acceSBarry Smith 3753a40ed3dSBarry Smith PetscFunctionBegin; 3760700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3773050cee2SBarry Smith if (!viewer) { 3789566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&viewer)); 3793050cee2SBarry Smith } 3800700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 381c9780b6fSBarry Smith PetscCheckSameComm(snes,1,viewer,2); 38274679c65SBarry Smith 3839566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii)); 3849566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring)); 3859566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary)); 3869566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw)); 387e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 3889566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws)); 389bfb97211SBarry Smith #endif 39032077d6dSBarry Smith if (iascii) { 391dc0571f2SMatthew G. Knepley SNESNormSchedule normschedule; 3928404b7f3SBarry Smith DM dm; 3938404b7f3SBarry Smith PetscErrorCode (*cJ)(SNES,Vec,Mat,Mat,void*); 3948404b7f3SBarry Smith void *ctx; 395789d8953SBarry Smith const char *pre = ""; 396dc0571f2SMatthew G. Knepley 3979566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer)); 398fce1e034SJed Brown if (!snes->setupcalled) { 3999566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," SNES has not been set up so information may be incomplete\n")); 400fce1e034SJed Brown } 401e7788613SBarry Smith if (snes->ops->view) { 4029566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 4039566063dSJacob Faibussowitsch PetscCall((*snes->ops->view)(snes,viewer)); 4049566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 4050ef38995SBarry Smith } 40663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," maximum iterations=%" PetscInt_FMT ", maximum function evaluations=%" PetscInt_FMT "\n",snes->max_its,snes->max_funcs)); 4079566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," tolerances: relative=%g, absolute=%g, solution=%g\n",(double)snes->rtol,(double)snes->abstol,(double)snes->stol)); 408efd4aadfSBarry Smith if (snes->usesksp) { 40963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%" PetscInt_FMT "\n",snes->linear_its)); 410efd4aadfSBarry Smith } 41163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of function evaluations=%" PetscInt_FMT "\n",snes->nfuncs)); 4129566063dSJacob Faibussowitsch PetscCall(SNESGetNormSchedule(snes, &normschedule)); 4139566063dSJacob Faibussowitsch if (normschedule > 0) PetscCall(PetscViewerASCIIPrintf(viewer," norm schedule %s\n",SNESNormSchedules[normschedule])); 41417fe4bdfSPeter Brune if (snes->gridsequence) { 41563a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," total number of grid sequence refinements=%" PetscInt_FMT "\n",snes->gridsequence)); 41617fe4bdfSPeter Brune } 4179b94acceSBarry Smith if (snes->ksp_ewconv) { 418fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 4199b94acceSBarry Smith if (kctx) { 42063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %" PetscInt_FMT ")\n",kctx->version)); 4219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," rtol_0=%g, rtol_max=%g, threshold=%g\n",(double)kctx->rtol_0,(double)kctx->rtol_max,(double)kctx->threshold)); 4229566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," gamma=%g, alpha=%g, alpha2=%g\n",(double)kctx->gamma,(double)kctx->alpha,(double)kctx->alpha2)); 4239b94acceSBarry Smith } 4249b94acceSBarry Smith } 425eb1f6c34SBarry Smith if (snes->lagpreconditioner == -1) { 4269566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Preconditioned is never rebuilt\n")); 427eb1f6c34SBarry Smith } else if (snes->lagpreconditioner > 1) { 42863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Preconditioned is rebuilt every %" PetscInt_FMT " new Jacobians\n",snes->lagpreconditioner)); 429eb1f6c34SBarry Smith } 430eb1f6c34SBarry Smith if (snes->lagjacobian == -1) { 4319566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Jacobian is never rebuilt\n")); 432eb1f6c34SBarry Smith } else if (snes->lagjacobian > 1) { 43363a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Jacobian is rebuilt every %" PetscInt_FMT " SNES iterations\n",snes->lagjacobian)); 434eb1f6c34SBarry Smith } 4359566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 4369566063dSJacob Faibussowitsch PetscCall(DMSNESGetJacobian(dm,&cJ,&ctx)); 437789d8953SBarry Smith if (snes->mf_operator) { 4389566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Jacobian is applied matrix-free with differencing\n")); 439789d8953SBarry Smith pre = "Preconditioning "; 440789d8953SBarry Smith } 4418404b7f3SBarry Smith if (cJ == SNESComputeJacobianDefault) { 4429566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," %sJacobian is built using finite differences one column at a time\n",pre)); 4438404b7f3SBarry Smith } else if (cJ == SNESComputeJacobianDefaultColor) { 4449566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," %sJacobian is built using finite differences with coloring\n",pre)); 445789d8953SBarry Smith /* it slightly breaks data encapsulation for access the DMDA information directly */ 446789d8953SBarry Smith } else if (cJ == SNESComputeJacobian_DMDA) { 447789d8953SBarry Smith MatFDColoring fdcoloring; 4489566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm,"DMDASNES_FDCOLORING",(PetscObject*)&fdcoloring)); 449789d8953SBarry Smith if (fdcoloring) { 4509566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," %sJacobian is built using colored finite differences on a DMDA\n",pre)); 451789d8953SBarry Smith } else { 4529566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," %sJacobian is built using a DMDA local Jacobian\n",pre)); 453789d8953SBarry Smith } 454789d8953SBarry Smith } else if (snes->mf) { 4559566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Jacobian is applied matrix-free with differencing, no explicit Jacobian\n")); 4568404b7f3SBarry Smith } 4570f5bd95cSBarry Smith } else if (isstring) { 458317d6ea6SBarry Smith const char *type; 4599566063dSJacob Faibussowitsch PetscCall(SNESGetType(snes,&type)); 4609566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer," SNESType: %-7.7s",type)); 4619566063dSJacob Faibussowitsch if (snes->ops->view) PetscCall((*snes->ops->view)(snes,viewer)); 46255849f57SBarry Smith } else if (isbinary) { 46355849f57SBarry Smith PetscInt classid = SNES_FILE_CLASSID; 46455849f57SBarry Smith MPI_Comm comm; 46555849f57SBarry Smith PetscMPIInt rank; 46655849f57SBarry Smith char type[256]; 46755849f57SBarry Smith 4689566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)snes,&comm)); 4699566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm,&rank)); 470dd400576SPatrick Sanan if (rank == 0) { 4719566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT)); 4729566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(type,((PetscObject)snes)->type_name,sizeof(type))); 4739566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer,type,sizeof(type),PETSC_CHAR)); 47455849f57SBarry Smith } 475*1baa6e33SBarry Smith if (snes->ops->view) PetscCall((*snes->ops->view)(snes,viewer)); 47672a02f06SBarry Smith } else if (isdraw) { 47772a02f06SBarry Smith PetscDraw draw; 47872a02f06SBarry Smith char str[36]; 47989fd9fafSBarry Smith PetscReal x,y,bottom,h; 48072a02f06SBarry Smith 4819566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw)); 4829566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw,&x,&y)); 4839566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(str,"SNES: ",sizeof(str))); 4849566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(str,((PetscObject)snes)->type_name,sizeof(str))); 4859566063dSJacob Faibussowitsch PetscCall(PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLUE,PETSC_DRAW_BLACK,str,NULL,&h)); 48689fd9fafSBarry Smith bottom = y - h; 4879566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw,x,bottom)); 488*1baa6e33SBarry Smith if (snes->ops->view) PetscCall((*snes->ops->view)(snes,viewer)); 489e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 490536b137fSBarry Smith } else if (issaws) { 491d45a07a7SBarry Smith PetscMPIInt rank; 4922657e9d9SBarry Smith const char *name; 493d45a07a7SBarry Smith 4949566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)snes,&name)); 4959566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 496dd400576SPatrick Sanan if (!((PetscObject)snes)->amsmem && rank == 0) { 497d45a07a7SBarry Smith char dir[1024]; 498d45a07a7SBarry Smith 4999566063dSJacob Faibussowitsch PetscCall(PetscObjectViewSAWs((PetscObject)snes,viewer)); 5009566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/its",name)); 5012657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&snes->iter,1,SAWs_READ,SAWs_INT)); 502bfb97211SBarry Smith if (!snes->conv_hist) { 5039566063dSJacob Faibussowitsch PetscCall(SNESSetConvergenceHistory(snes,NULL,NULL,PETSC_DECIDE,PETSC_TRUE)); 504bfb97211SBarry Smith } 5059566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/conv_hist",name)); 5062657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,snes->conv_hist,10,SAWs_READ,SAWs_DOUBLE)); 507f05ece33SBarry Smith } 508bfb97211SBarry Smith #endif 50972a02f06SBarry Smith } 51072a02f06SBarry Smith if (snes->linesearch) { 5119566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &linesearch)); 5129566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 5139566063dSJacob Faibussowitsch PetscCall(SNESLineSearchView(linesearch, viewer)); 5149566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 51519bcc07fSBarry Smith } 516efd4aadfSBarry Smith if (snes->npc && snes->usesnpc) { 5179566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 5189566063dSJacob Faibussowitsch PetscCall(SNESView(snes->npc, viewer)); 5199566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 5204a0c5b0cSMatthew G Knepley } 5219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 5229566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(snes->dm,&dmsnes)); 5239566063dSJacob Faibussowitsch PetscCall(DMSNESView(dmsnes, viewer)); 5249566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 5252c155ee1SBarry Smith if (snes->usesksp) { 5269566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&ksp)); 5279566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 5289566063dSJacob Faibussowitsch PetscCall(KSPView(ksp,viewer)); 5299566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 5302c155ee1SBarry Smith } 53172a02f06SBarry Smith if (isdraw) { 53272a02f06SBarry Smith PetscDraw draw; 5339566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer,0,&draw)); 5349566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 5357f1410a3SPeter Brune } 5363a40ed3dSBarry Smith PetscFunctionReturn(0); 5379b94acceSBarry Smith } 5389b94acceSBarry Smith 53976b2cf59SMatthew Knepley /* 54076b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 54176b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 54276b2cf59SMatthew Knepley */ 54376b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 544a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 5456849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 54676b2cf59SMatthew Knepley 547ac226902SBarry Smith /*@C 54876b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 54976b2cf59SMatthew Knepley 55076b2cf59SMatthew Knepley Not Collective 55176b2cf59SMatthew Knepley 55276b2cf59SMatthew Knepley Input Parameter: 55376b2cf59SMatthew Knepley . snescheck - function that checks for options 55476b2cf59SMatthew Knepley 55576b2cf59SMatthew Knepley Level: developer 55676b2cf59SMatthew Knepley 557db781477SPatrick Sanan .seealso: `SNESSetFromOptions()` 55876b2cf59SMatthew Knepley @*/ 5597087cfbeSBarry Smith PetscErrorCode SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 56076b2cf59SMatthew Knepley { 56176b2cf59SMatthew Knepley PetscFunctionBegin; 56263a3b9bcSJacob Faibussowitsch PetscCheck(numberofsetfromoptions < MAXSETFROMOPTIONS,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %d allowed", MAXSETFROMOPTIONS); 56376b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 56476b2cf59SMatthew Knepley PetscFunctionReturn(0); 56576b2cf59SMatthew Knepley } 56676b2cf59SMatthew Knepley 56725acbd8eSLisandro Dalcin PETSC_INTERN PetscErrorCode SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 568aa3661deSLisandro Dalcin 569ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool hasOperator, PetscInt version) 570aa3661deSLisandro Dalcin { 571aa3661deSLisandro Dalcin Mat J; 572895c21f2SBarry Smith MatNullSpace nullsp; 573aa3661deSLisandro Dalcin 574aa3661deSLisandro Dalcin PetscFunctionBegin; 5750700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 576aa3661deSLisandro Dalcin 57798613b67SLisandro Dalcin if (!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) { 57898613b67SLisandro Dalcin Mat A = snes->jacobian, B = snes->jacobian_pre; 5799566063dSJacob Faibussowitsch PetscCall(MatCreateVecs(A ? A : B, NULL,&snes->vec_func)); 58098613b67SLisandro Dalcin } 58198613b67SLisandro Dalcin 582aa3661deSLisandro Dalcin if (version == 1) { 5839566063dSJacob Faibussowitsch PetscCall(MatCreateSNESMF(snes,&J)); 5849566063dSJacob Faibussowitsch PetscCall(MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix)); 5859566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(J)); 5861e2ae407SBarry Smith /* TODO: the version 2 code should be merged into the MatCreateSNESMF() and MatCreateMFFD() infrastructure and then removed */ 587aa3661deSLisandro Dalcin } else if (version == 2) { 5885f80ce2aSJacob Faibussowitsch PetscCheck(snes->vec_func,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first"); 589570b7f6dSBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) && !defined(PETSC_USE_REAL___FP16) 5909566063dSJacob Faibussowitsch PetscCall(SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J)); 591aa3661deSLisandro Dalcin #else 5922479783cSJose E. Roman SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator routines (version 2)"); 593aa3661deSLisandro Dalcin #endif 5942479783cSJose E. Roman } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator routines, only version 1 and 2"); 595aa3661deSLisandro Dalcin 596895c21f2SBarry Smith /* attach any user provided null space that was on Amat to the newly created matrix free matrix */ 597895c21f2SBarry Smith if (snes->jacobian) { 5989566063dSJacob Faibussowitsch PetscCall(MatGetNullSpace(snes->jacobian,&nullsp)); 599*1baa6e33SBarry Smith if (nullsp) PetscCall(MatSetNullSpace(J,nullsp)); 600895c21f2SBarry Smith } 601895c21f2SBarry Smith 60263a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Setting default matrix-free operator routines (version %" PetscInt_FMT ")\n", version)); 603d3462f78SMatthew Knepley if (hasOperator) { 6043232da50SPeter Brune 605aa3661deSLisandro Dalcin /* This version replaces the user provided Jacobian matrix with a 606aa3661deSLisandro Dalcin matrix-free version but still employs the user-provided preconditioner matrix. */ 6079566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,J,NULL,NULL,NULL)); 608aa3661deSLisandro Dalcin } else { 609aa3661deSLisandro Dalcin /* This version replaces both the user-provided Jacobian and the user- 6103232da50SPeter Brune provided preconditioner Jacobian with the default matrix free version. */ 611b552625fSStefano Zampini if (snes->npcside == PC_LEFT && snes->npc) { 6129566063dSJacob Faibussowitsch if (!snes->jacobian) PetscCall(SNESSetJacobian(snes,J,NULL,NULL,NULL)); 613172a4300SPeter Brune } else { 614789d8953SBarry Smith KSP ksp; 615789d8953SBarry Smith PC pc; 616789d8953SBarry Smith PetscBool match; 617789d8953SBarry Smith 6189566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,NULL)); 619aa3661deSLisandro Dalcin /* Force no preconditioner */ 6209566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&ksp)); 6219566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp,&pc)); 6222698c518SLisandro Dalcin PetscCall(PetscObjectTypeCompareAny((PetscObject)pc,&match,PCSHELL,PCH2OPUS,"")); 623aa3661deSLisandro Dalcin if (!match) { 6249566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n")); 6259566063dSJacob Faibussowitsch PetscCall(PCSetType(pc,PCNONE)); 626aa3661deSLisandro Dalcin } 627aa3661deSLisandro Dalcin } 628789d8953SBarry Smith } 6299566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); 630aa3661deSLisandro Dalcin PetscFunctionReturn(0); 631aa3661deSLisandro Dalcin } 632aa3661deSLisandro Dalcin 633dfe15315SJed Brown static PetscErrorCode DMRestrictHook_SNESVecSol(DM dmfine,Mat Restrict,Vec Rscale,Mat Inject,DM dmcoarse,void *ctx) 634dfe15315SJed Brown { 635dfe15315SJed Brown SNES snes = (SNES)ctx; 6360298fd71SBarry Smith Vec Xfine,Xfine_named = NULL,Xcoarse; 637dfe15315SJed Brown 638dfe15315SJed Brown PetscFunctionBegin; 63916ebb321SJed Brown if (PetscLogPrintInfo) { 64016ebb321SJed Brown PetscInt finelevel,coarselevel,fineclevel,coarseclevel; 6419566063dSJacob Faibussowitsch PetscCall(DMGetRefineLevel(dmfine,&finelevel)); 6429566063dSJacob Faibussowitsch PetscCall(DMGetCoarsenLevel(dmfine,&fineclevel)); 6439566063dSJacob Faibussowitsch PetscCall(DMGetRefineLevel(dmcoarse,&coarselevel)); 6449566063dSJacob Faibussowitsch PetscCall(DMGetCoarsenLevel(dmcoarse,&coarseclevel)); 64563a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(dmfine,"Restricting SNES solution vector from level %" PetscInt_FMT "-%" PetscInt_FMT " to level %" PetscInt_FMT "-%" PetscInt_FMT "\n",finelevel,fineclevel,coarselevel,coarseclevel)); 64616ebb321SJed Brown } 647dfe15315SJed Brown if (dmfine == snes->dm) Xfine = snes->vec_sol; 648dfe15315SJed Brown else { 6499566063dSJacob Faibussowitsch PetscCall(DMGetNamedGlobalVector(dmfine,"SNESVecSol",&Xfine_named)); 650dfe15315SJed Brown Xfine = Xfine_named; 651dfe15315SJed Brown } 6529566063dSJacob Faibussowitsch PetscCall(DMGetNamedGlobalVector(dmcoarse,"SNESVecSol",&Xcoarse)); 653907f5c5aSLawrence Mitchell if (Inject) { 6549566063dSJacob Faibussowitsch PetscCall(MatRestrict(Inject,Xfine,Xcoarse)); 655907f5c5aSLawrence Mitchell } else { 6569566063dSJacob Faibussowitsch PetscCall(MatRestrict(Restrict,Xfine,Xcoarse)); 6579566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(Xcoarse,Xcoarse,Rscale)); 658907f5c5aSLawrence Mitchell } 6599566063dSJacob Faibussowitsch PetscCall(DMRestoreNamedGlobalVector(dmcoarse,"SNESVecSol",&Xcoarse)); 6609566063dSJacob Faibussowitsch if (Xfine_named) PetscCall(DMRestoreNamedGlobalVector(dmfine,"SNESVecSol",&Xfine_named)); 661dfe15315SJed Brown PetscFunctionReturn(0); 662dfe15315SJed Brown } 663dfe15315SJed Brown 66416ebb321SJed Brown static PetscErrorCode DMCoarsenHook_SNESVecSol(DM dm,DM dmc,void *ctx) 66516ebb321SJed Brown { 66616ebb321SJed Brown PetscFunctionBegin; 6679566063dSJacob Faibussowitsch PetscCall(DMCoarsenHookAdd(dmc,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,ctx)); 66816ebb321SJed Brown PetscFunctionReturn(0); 66916ebb321SJed Brown } 67016ebb321SJed Brown 671a6950cb2SJed Brown /* This may be called to rediscretize the operator on levels of linear multigrid. The DM shuffle is so the user can 672a6950cb2SJed Brown * safely call SNESGetDM() in their residual evaluation routine. */ 67323ee1639SBarry Smith static PetscErrorCode KSPComputeOperators_SNES(KSP ksp,Mat A,Mat B,void *ctx) 674caa4e7f2SJed Brown { 675caa4e7f2SJed Brown SNES snes = (SNES)ctx; 6760298fd71SBarry Smith Vec X,Xnamed = NULL; 677dfe15315SJed Brown DM dmsave; 6784e269d77SPeter Brune void *ctxsave; 67925ce1634SJed Brown PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*) = NULL; 680caa4e7f2SJed Brown 681caa4e7f2SJed Brown PetscFunctionBegin; 682dfe15315SJed Brown dmsave = snes->dm; 6839566063dSJacob Faibussowitsch PetscCall(KSPGetDM(ksp,&snes->dm)); 684dfe15315SJed Brown if (dmsave == snes->dm) X = snes->vec_sol; /* We are on the finest level */ 685dfe15315SJed Brown else { /* We are on a coarser level, this vec was initialized using a DM restrict hook */ 6869566063dSJacob Faibussowitsch PetscCall(DMGetNamedGlobalVector(snes->dm,"SNESVecSol",&Xnamed)); 687dfe15315SJed Brown X = Xnamed; 6889566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,NULL,NULL,&jac,&ctxsave)); 6894e269d77SPeter Brune /* If the DM's don't match up, the MatFDColoring context needed for the jacobian won't match up either -- fixit. */ 6908d359177SBarry Smith if (jac == SNESComputeJacobianDefaultColor) { 6919566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,NULL,NULL,SNESComputeJacobianDefaultColor,NULL)); 692dfe15315SJed Brown } 6934e269d77SPeter Brune } 6944dde8bb0SMatthew G. Knepley /* Make sure KSP DM has the Jacobian computation routine */ 6954dde8bb0SMatthew G. Knepley { 6964dde8bb0SMatthew G. Knepley DMSNES sdm; 6974e269d77SPeter Brune 6989566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(snes->dm, &sdm)); 6994dde8bb0SMatthew G. Knepley if (!sdm->ops->computejacobian) { 7009566063dSJacob Faibussowitsch PetscCall(DMCopyDMSNES(dmsave, snes->dm)); 7014dde8bb0SMatthew G. Knepley } 7024dde8bb0SMatthew G. Knepley } 7032b93b426SMatthew G. Knepley /* Compute the operators */ 7049566063dSJacob Faibussowitsch PetscCall(SNESComputeJacobian(snes,X,A,B)); 7052b93b426SMatthew G. Knepley /* Put the previous context back */ 7068d359177SBarry Smith if (snes->dm != dmsave && jac == SNESComputeJacobianDefaultColor) { 7079566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,NULL,NULL,jac,ctxsave)); 7084e269d77SPeter Brune } 7094e269d77SPeter Brune 7109566063dSJacob Faibussowitsch if (Xnamed) PetscCall(DMRestoreNamedGlobalVector(snes->dm,"SNESVecSol",&Xnamed)); 711dfe15315SJed Brown snes->dm = dmsave; 712caa4e7f2SJed Brown PetscFunctionReturn(0); 713caa4e7f2SJed Brown } 714caa4e7f2SJed Brown 7156cab3a1bSJed Brown /*@ 7166cab3a1bSJed Brown SNESSetUpMatrices - ensures that matrices are available for SNES, to be called by SNESSetUp_XXX() 7176cab3a1bSJed Brown 7186cab3a1bSJed Brown Collective 7196cab3a1bSJed Brown 7204165533cSJose E. Roman Input Parameter: 7216cab3a1bSJed Brown . snes - snes to configure 7226cab3a1bSJed Brown 7236cab3a1bSJed Brown Level: developer 7246cab3a1bSJed Brown 725db781477SPatrick Sanan .seealso: `SNESSetUp()` 7266cab3a1bSJed Brown @*/ 7276cab3a1bSJed Brown PetscErrorCode SNESSetUpMatrices(SNES snes) 7286cab3a1bSJed Brown { 7296cab3a1bSJed Brown DM dm; 730942e3340SBarry Smith DMSNES sdm; 7316cab3a1bSJed Brown 7326cab3a1bSJed Brown PetscFunctionBegin; 7339566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 7349566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 73558b371f3SBarry Smith if (!snes->jacobian && snes->mf) { 7366cab3a1bSJed Brown Mat J; 7376cab3a1bSJed Brown void *functx; 7389566063dSJacob Faibussowitsch PetscCall(MatCreateSNESMF(snes,&J)); 7399566063dSJacob Faibussowitsch PetscCall(MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix)); 7409566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(J)); 7419566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,NULL,NULL,&functx)); 7429566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,J,J,NULL,NULL)); 7439566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); 744caa4e7f2SJed Brown } else if (snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) { 7456cab3a1bSJed Brown Mat J,B; 7469566063dSJacob Faibussowitsch PetscCall(MatCreateSNESMF(snes,&J)); 7479566063dSJacob Faibussowitsch PetscCall(MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix)); 7489566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(J)); 7499566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix(snes->dm,&B)); 75006f20277SJed Brown /* sdm->computejacobian was already set to reach here */ 7519566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,J,B,NULL,NULL)); 7529566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); 7539566063dSJacob Faibussowitsch PetscCall(MatDestroy(&B)); 754caa4e7f2SJed Brown } else if (!snes->jacobian_pre) { 7551ba9b98eSMatthew G. Knepley PetscDS prob; 7566cab3a1bSJed Brown Mat J, B; 7571ba9b98eSMatthew G. Knepley PetscBool hasPrec = PETSC_FALSE; 7581ba9b98eSMatthew G. Knepley 7596cab3a1bSJed Brown J = snes->jacobian; 7609566063dSJacob Faibussowitsch PetscCall(DMGetDS(dm, &prob)); 7619566063dSJacob Faibussowitsch if (prob) PetscCall(PetscDSHasJacobianPreconditioner(prob, &hasPrec)); 7629566063dSJacob Faibussowitsch if (J) PetscCall(PetscObjectReference((PetscObject) J)); 7639566063dSJacob Faibussowitsch else if (hasPrec) PetscCall(DMCreateMatrix(snes->dm, &J)); 7649566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix(snes->dm, &B)); 7659566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, J ? J : B, B, NULL, NULL)); 7669566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); 7679566063dSJacob Faibussowitsch PetscCall(MatDestroy(&B)); 7686cab3a1bSJed Brown } 769caa4e7f2SJed Brown { 770caa4e7f2SJed Brown KSP ksp; 7719566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&ksp)); 7729566063dSJacob Faibussowitsch PetscCall(KSPSetComputeOperators(ksp,KSPComputeOperators_SNES,snes)); 7739566063dSJacob Faibussowitsch PetscCall(DMCoarsenHookAdd(snes->dm,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,snes)); 774caa4e7f2SJed Brown } 7756cab3a1bSJed Brown PetscFunctionReturn(0); 7766cab3a1bSJed Brown } 7776cab3a1bSJed Brown 7785e7c47f3SMatthew G. Knepley static PetscErrorCode SNESMonitorPauseFinal_Internal(SNES snes) 7795e7c47f3SMatthew G. Knepley { 7805e7c47f3SMatthew G. Knepley PetscInt i; 7815e7c47f3SMatthew G. Knepley 7825e7c47f3SMatthew G. Knepley PetscFunctionBegin; 7835e7c47f3SMatthew G. Knepley if (!snes->pauseFinal) PetscFunctionReturn(0); 7845e7c47f3SMatthew G. Knepley for (i = 0; i < snes->numbermonitors; ++i) { 7855e7c47f3SMatthew G. Knepley PetscViewerAndFormat *vf = (PetscViewerAndFormat *) snes->monitorcontext[i]; 7865e7c47f3SMatthew G. Knepley PetscDraw draw; 7875e7c47f3SMatthew G. Knepley PetscReal lpause; 7885e7c47f3SMatthew G. Knepley 7895e7c47f3SMatthew G. Knepley if (!vf) continue; 7905e7c47f3SMatthew G. Knepley if (vf->lg) { 7915e7c47f3SMatthew G. Knepley if (!PetscCheckPointer(vf->lg, PETSC_OBJECT)) continue; 7925e7c47f3SMatthew G. Knepley if (((PetscObject) vf->lg)->classid != PETSC_DRAWLG_CLASSID) continue; 7939566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(vf->lg, &draw)); 7949566063dSJacob Faibussowitsch PetscCall(PetscDrawGetPause(draw, &lpause)); 7959566063dSJacob Faibussowitsch PetscCall(PetscDrawSetPause(draw, -1.0)); 7969566063dSJacob Faibussowitsch PetscCall(PetscDrawPause(draw)); 7979566063dSJacob Faibussowitsch PetscCall(PetscDrawSetPause(draw, lpause)); 7985e7c47f3SMatthew G. Knepley } else { 7995e7c47f3SMatthew G. Knepley PetscBool isdraw; 8005e7c47f3SMatthew G. Knepley 8015e7c47f3SMatthew G. Knepley if (!PetscCheckPointer(vf->viewer, PETSC_OBJECT)) continue; 8025e7c47f3SMatthew G. Knepley if (((PetscObject) vf->viewer)->classid != PETSC_VIEWER_CLASSID) continue; 8039566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject) vf->viewer, PETSCVIEWERDRAW, &isdraw)); 8045e7c47f3SMatthew G. Knepley if (!isdraw) continue; 8059566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(vf->viewer, 0, &draw)); 8069566063dSJacob Faibussowitsch PetscCall(PetscDrawGetPause(draw, &lpause)); 8079566063dSJacob Faibussowitsch PetscCall(PetscDrawSetPause(draw, -1.0)); 8089566063dSJacob Faibussowitsch PetscCall(PetscDrawPause(draw)); 8099566063dSJacob Faibussowitsch PetscCall(PetscDrawSetPause(draw, lpause)); 8105e7c47f3SMatthew G. Knepley } 8115e7c47f3SMatthew G. Knepley } 8125e7c47f3SMatthew G. Knepley PetscFunctionReturn(0); 8135e7c47f3SMatthew G. Knepley } 8145e7c47f3SMatthew G. Knepley 815fde5950dSBarry Smith /*@C 816fde5950dSBarry Smith SNESMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 817fde5950dSBarry Smith 818fde5950dSBarry Smith Collective on SNES 819fde5950dSBarry Smith 820fde5950dSBarry Smith Input Parameters: 821fde5950dSBarry Smith + snes - SNES object you wish to monitor 822fde5950dSBarry Smith . name - the monitor type one is seeking 823fde5950dSBarry Smith . help - message indicating what monitoring is done 824fde5950dSBarry Smith . manual - manual page for the monitor 825fde5950dSBarry Smith . monitor - the monitor function 826fde5950dSBarry Smith - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the SNES or PetscViewer objects 827fde5950dSBarry Smith 828fde5950dSBarry Smith Level: developer 829fde5950dSBarry Smith 830db781477SPatrick Sanan .seealso: `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`, 831db781477SPatrick Sanan `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()` 832db781477SPatrick Sanan `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`, 833db781477SPatrick Sanan `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`, 834c2e3fba1SPatrick Sanan `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`, 835db781477SPatrick Sanan `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`, 836db781477SPatrick Sanan `PetscOptionsFList()`, `PetscOptionsEList()` 837fde5950dSBarry Smith @*/ 838d43b4f6eSBarry Smith PetscErrorCode SNESMonitorSetFromOptions(SNES snes,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(SNES,PetscViewerAndFormat*)) 839fde5950dSBarry Smith { 840fde5950dSBarry Smith PetscViewer viewer; 841fde5950dSBarry Smith PetscViewerFormat format; 842fde5950dSBarry Smith PetscBool flg; 843fde5950dSBarry Smith 844fde5950dSBarry Smith PetscFunctionBegin; 8459566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,name,&viewer,&format,&flg)); 846fde5950dSBarry Smith if (flg) { 847d43b4f6eSBarry Smith PetscViewerAndFormat *vf; 8489566063dSJacob Faibussowitsch PetscCall(PetscViewerAndFormatCreate(viewer,format,&vf)); 8499566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)viewer)); 850*1baa6e33SBarry Smith if (monitorsetup) PetscCall((*monitorsetup)(snes,vf)); 8519566063dSJacob Faibussowitsch PetscCall(SNESMonitorSet(snes,(PetscErrorCode (*)(SNES,PetscInt,PetscReal,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy)); 852fde5950dSBarry Smith } 853fde5950dSBarry Smith PetscFunctionReturn(0); 854fde5950dSBarry Smith } 855fde5950dSBarry Smith 8560f0abf79SStefano Zampini PetscErrorCode SNESEWSetFromOptions_Private(SNESKSPEW* kctx, MPI_Comm comm, const char* prefix) 8570f0abf79SStefano Zampini { 8580f0abf79SStefano Zampini PetscFunctionBegin; 8590f0abf79SStefano Zampini PetscOptionsBegin(comm,prefix,"Eisenstat and Walker type forcing options","KSP"); 8600f0abf79SStefano Zampini PetscCall(PetscOptionsInt("-ksp_ew_version","Version 1, 2 or 3",NULL,kctx->version,&kctx->version,NULL)); 8610f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_rtol0","0 <= rtol0 < 1",NULL,kctx->rtol_0,&kctx->rtol_0,NULL)); 8620f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_rtolmax","0 <= rtolmax < 1",NULL,kctx->rtol_max,&kctx->rtol_max,NULL)); 8630f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_gamma","0 <= gamma <= 1",NULL,kctx->gamma,&kctx->gamma,NULL)); 8640f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_alpha","1 < alpha <= 2",NULL,kctx->alpha,&kctx->alpha,NULL)); 8650f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_alpha2","alpha2",NULL,kctx->alpha2,&kctx->alpha2,NULL)); 8660f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_threshold","0 < threshold < 1",NULL,kctx->threshold,&kctx->threshold,NULL)); 8670f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_p1","p1",NULL,kctx->v4_p1,&kctx->v4_p1,NULL)); 8680f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_p2","p2",NULL,kctx->v4_p2,&kctx->v4_p2,NULL)); 8690f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_p3","p3",NULL,kctx->v4_p3,&kctx->v4_p3,NULL)); 8700f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_m1","Scaling when rk-1 in [p2,p3)",NULL,kctx->v4_m1,&kctx->v4_m1,NULL)); 8710f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_m2","Scaling when rk-1 in [p3,+infty)",NULL,kctx->v4_m2,&kctx->v4_m2,NULL)); 8720f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_m3","Threshold for successive rtol (0.1 in Eq.7)",NULL,kctx->v4_m3,&kctx->v4_m3,NULL)); 8730f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_m4","Adaptation scaling (0.5 in Eq.7)",NULL,kctx->v4_m4,&kctx->v4_m4,NULL)); 8740f0abf79SStefano Zampini PetscOptionsEnd(); 8750f0abf79SStefano Zampini PetscFunctionReturn(0); 8760f0abf79SStefano Zampini } 8770f0abf79SStefano Zampini 8789b94acceSBarry Smith /*@ 87994b7f48cSBarry Smith SNESSetFromOptions - Sets various SNES and KSP parameters from user options. 8809b94acceSBarry Smith 881c7afd0dbSLois Curfman McInnes Collective on SNES 882c7afd0dbSLois Curfman McInnes 8839b94acceSBarry Smith Input Parameter: 8849b94acceSBarry Smith . snes - the SNES context 8859b94acceSBarry Smith 88636851e7fSLois Curfman McInnes Options Database Keys: 887722329fbSBarry Smith + -snes_type <type> - newtonls, newtontr, ngmres, ncg, nrichardson, qn, vi, fas, SNESType for complete list 88882738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 88982738288SBarry Smith of the change in the solution between steps 89070441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 891b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 892e4d06f11SPatrick Farrell . -snes_divergence_tolerance <divtol> - if the residual goes above divtol*rnorm0, exit with divergence 893be5caee7SBarry Smith . -snes_force_iteration <force> - force SNESSolve() to take at least one iteration 894b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 895b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 8964839bfe8SBarry Smith . -snes_max_fail <max_fail> - maximum number of line search failures allowed before stopping, default is none 897ddf469c8SBarry Smith . -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops 898a8054027SBarry Smith . -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild) 8993d5a8a6aSBarry Smith . -snes_lag_preconditioner_persists <true,false> - retains the -snes_lag_preconditioner information across multiple SNESSolve() 900e35cf81dSBarry Smith . -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild) 9013d5a8a6aSBarry Smith . -snes_lag_jacobian_persists <true,false> - retains the -snes_lag_jacobian information across multiple SNESSolve() 902b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 903f362779dSJed Brown . -snes_convergence_test - <default,skip,correct_pressure> convergence test in nonlinear solver. 904f362779dSJed Brown default SNESConvergedDefault(). skip SNESConvergedSkip() means continue iterating until max_it or some other criterion is reached, saving expense 905f362779dSJed Brown of convergence test. correct_pressure SNESConvergedCorrectPressure() has special handling of a pressure null space. 906fde5950dSBarry Smith . -snes_monitor [ascii][:filename][:viewer format] - prints residual norm at each iteration. if no filename given prints to stdout 907fde5950dSBarry Smith . -snes_monitor_solution [ascii binary draw][:filename][:viewer format] - plots solution at each iteration 908fde5950dSBarry Smith . -snes_monitor_residual [ascii binary draw][:filename][:viewer format] - plots residual (not its norm) at each iteration 909fde5950dSBarry Smith . -snes_monitor_solution_update [ascii binary draw][:filename][:viewer format] - plots update to solution at each iteration 9104619e776SBarry Smith . -snes_monitor_lg_residualnorm - plots residual norm at each iteration 911459f5d12SBarry Smith . -snes_monitor_lg_range - plots residual norm at each iteration 9125e7c47f3SMatthew G. Knepley . -snes_monitor_pause_final - Pauses all monitor drawing after the solver ends 913e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 914e2e60de9SPeter Brune . -snes_fd_color - use finite differences with coloring to compute Jacobian 9155968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 916b5badacbSBarry Smith . -snes_converged_reason - print the reason for convergence/divergence after each solve 917e62ac41dSBarry Smith . -npc_snes_type <type> - the SNES type to use as a nonlinear preconditioner 918e62ac41dSBarry Smith . -snes_test_jacobian <optional threshold> - compare the user provided Jacobian with one computed via finite differences to check for errors. If a threshold is given, display only those entries whose difference is greater than the threshold. 919e62ac41dSBarry Smith - -snes_test_jacobian_view - display the user provided Jacobian, the finite difference Jacobian and the difference between them to help users detect the location of errors in the user provided Jacobian. 92082738288SBarry Smith 92182738288SBarry Smith Options Database for Eisenstat-Walker method: 922fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 9234b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 92436851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 92536851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 92636851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 92736851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 92836851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 92936851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 93082738288SBarry Smith 93111ca99fdSLois Curfman McInnes Notes: 932ec5066bdSBarry Smith To see all options, run your program with the -help option or consult the users manual 933ec5066bdSBarry Smith 934ec5066bdSBarry Smith Notes: 935a5b23f4aSJose E. Roman SNES supports three approaches for computing (approximate) Jacobians: user provided via SNESSetJacobian(), matrix free, and computing explicitly with 936ec5066bdSBarry Smith finite differences and coloring using MatFDColoring. It is also possible to use automatic differentiation and the MatFDColoring object. 93783e2fdc7SBarry Smith 93836851e7fSLois Curfman McInnes Level: beginner 93936851e7fSLois Curfman McInnes 940db781477SPatrick Sanan .seealso: `SNESSetOptionsPrefix()`, `SNESResetFromOptions()`, `SNES`, `SNESCreate()` 9419b94acceSBarry Smith @*/ 9427087cfbeSBarry Smith PetscErrorCode SNESSetFromOptions(SNES snes) 9439b94acceSBarry Smith { 9448afaa268SBarry Smith PetscBool flg,pcset,persist,set; 945d8f46077SPeter Brune PetscInt i,indx,lag,grids; 94604d7464bSBarry Smith const char *deft = SNESNEWTONLS; 947649ef022SMatthew Knepley const char *convtests[] = {"default","skip","correct_pressure"}; 94885385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 9490f0abf79SStefano Zampini char type[256], monfilename[PETSC_MAX_PATH_LEN], ewprefix[256]; 950c40d0f55SPeter Brune PCSide pcside; 951a64e098fSPeter Brune const char *optionsprefix; 9529b94acceSBarry Smith 9533a40ed3dSBarry Smith PetscFunctionBegin; 9540700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 9559566063dSJacob Faibussowitsch PetscCall(SNESRegisterAll()); 956d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)snes); 957639ff905SBarry Smith if (((PetscObject)snes)->type_name) deft = ((PetscObject)snes)->type_name; 9589566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg)); 959d64ed03dSBarry Smith if (flg) { 9609566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes,type)); 9617adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 9629566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes,deft)); 963d64ed03dSBarry Smith } 9649566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->stol,&snes->stol,NULL)); 9659566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,NULL)); 966186905e3SBarry Smith 9679566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,NULL)); 9689566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_divergence_tolerance","Stop if residual norm increases by this factor","SNESSetDivergenceTolerance",snes->divtol,&snes->divtol,NULL)); 9699566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,NULL)); 9709566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,NULL)); 9719566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_max_fail","Maximum nonlinear step failures","SNESSetMaxNonlinearStepFailures",snes->maxFailures,&snes->maxFailures,NULL)); 9729566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,NULL)); 9739566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,NULL)); 9749566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_force_iteration","Force SNESSolve() to take at least one iteration","SNESSetForceIteration",snes->forceiteration,&snes->forceiteration,NULL)); 9759566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_check_jacobian_domain_error","Check Jacobian domain error after Jacobian evaluation","SNESCheckJacobianDomainError",snes->checkjacdomainerror,&snes->checkjacdomainerror,NULL)); 97685385478SLisandro Dalcin 9779566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg)); 978a8054027SBarry Smith if (flg) { 9795f80ce2aSJacob Faibussowitsch PetscCheck(lag != -1,PetscObjectComm((PetscObject)snes),PETSC_ERR_USER,"Cannot set the lag to -1 from the command line since the preconditioner must be built as least once, perhaps you mean -2"); 9809566063dSJacob Faibussowitsch PetscCall(SNESSetLagPreconditioner(snes,lag)); 981a8054027SBarry Smith } 9829566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_lag_preconditioner_persists","Preconditioner lagging through multiple SNES solves","SNESSetLagPreconditionerPersists",snes->lagjac_persist,&persist,&flg)); 983*1baa6e33SBarry Smith if (flg) PetscCall(SNESSetLagPreconditionerPersists(snes,persist)); 9849566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg)); 985e35cf81dSBarry Smith if (flg) { 9865f80ce2aSJacob Faibussowitsch PetscCheck(lag != -1,PetscObjectComm((PetscObject)snes),PETSC_ERR_USER,"Cannot set the lag to -1 from the command line since the Jacobian must be built as least once, perhaps you mean -2"); 9879566063dSJacob Faibussowitsch PetscCall(SNESSetLagJacobian(snes,lag)); 988e35cf81dSBarry Smith } 9899566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_lag_jacobian_persists","Jacobian lagging through multiple SNES solves","SNESSetLagJacobianPersists",snes->lagjac_persist,&persist,&flg)); 990*1baa6e33SBarry Smith if (flg) PetscCall(SNESSetLagJacobianPersists(snes,persist)); 99137ec4e1aSPeter Brune 9929566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg)); 993*1baa6e33SBarry Smith if (flg) PetscCall(SNESSetGridSequence(snes,grids)); 994a8054027SBarry Smith 9959566063dSJacob Faibussowitsch PetscCall(PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,sizeof(convtests)/sizeof(char*),"default",&indx,&flg)); 99685385478SLisandro Dalcin if (flg) { 99785385478SLisandro Dalcin switch (indx) { 9989566063dSJacob Faibussowitsch case 0: PetscCall(SNESSetConvergenceTest(snes,SNESConvergedDefault,NULL,NULL)); break; 9999566063dSJacob Faibussowitsch case 1: PetscCall(SNESSetConvergenceTest(snes,SNESConvergedSkip,NULL,NULL)); break; 10009566063dSJacob Faibussowitsch case 2: PetscCall(SNESSetConvergenceTest(snes,SNESConvergedCorrectPressure,NULL,NULL)); break; 100185385478SLisandro Dalcin } 100285385478SLisandro Dalcin } 100385385478SLisandro Dalcin 10049566063dSJacob Faibussowitsch PetscCall(PetscOptionsEList("-snes_norm_schedule","SNES Norm schedule","SNESSetNormSchedule",SNESNormSchedules,5,"function",&indx,&flg)); 10059566063dSJacob Faibussowitsch if (flg) PetscCall(SNESSetNormSchedule(snes,(SNESNormSchedule)indx)); 1006fdacfa88SPeter Brune 10079566063dSJacob Faibussowitsch PetscCall(PetscOptionsEList("-snes_function_type","SNES Norm schedule","SNESSetFunctionType",SNESFunctionTypes,2,"unpreconditioned",&indx,&flg)); 10089566063dSJacob Faibussowitsch if (flg) PetscCall(SNESSetFunctionType(snes,(SNESFunctionType)indx)); 1009186905e3SBarry Smith 101085385478SLisandro Dalcin kctx = (SNESKSPEW*)snes->kspconvctx; 101185385478SLisandro Dalcin 10129566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,NULL)); 1013186905e3SBarry Smith 10140f0abf79SStefano Zampini PetscCall(SNESGetOptionsPrefix(snes,&optionsprefix)); 10150f0abf79SStefano Zampini PetscCall(PetscSNPrintf(ewprefix,sizeof(ewprefix),"%s%s",optionsprefix ? optionsprefix : "","snes_")); 10160f0abf79SStefano Zampini PetscCall(SNESEWSetFromOptions_Private(kctx,PetscObjectComm((PetscObject)snes),ewprefix)); 10170f0abf79SStefano Zampini 10189566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,NULL)); 10199566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,NULL)); 10209566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,NULL)); 10219566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,NULL)); 10229566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,NULL)); 10239566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,NULL)); 10249566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,NULL)); 1025186905e3SBarry Smith 102690d69ab7SBarry Smith flg = PETSC_FALSE; 10279566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,&set)); 10289566063dSJacob Faibussowitsch if (set && flg) PetscCall(SNESMonitorCancel(snes)); 1029eabae89aSBarry Smith 10309566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor","Monitor norm of function","SNESMonitorDefault",SNESMonitorDefault,SNESMonitorDefaultSetUp)); 10319566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor_short","Monitor norm of function with fewer digits","SNESMonitorDefaultShort",SNESMonitorDefaultShort,NULL)); 10329566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor_range","Monitor range of elements of function","SNESMonitorRange",SNESMonitorRange,NULL)); 1033eabae89aSBarry Smith 10349566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor_ratio","Monitor ratios of the norm of function for consecutive steps","SNESMonitorRatio",SNESMonitorRatio,SNESMonitorRatioSetUp)); 10359566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor_field","Monitor norm of function (split into fields)","SNESMonitorDefaultField",SNESMonitorDefaultField,NULL)); 10369566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor_solution","View solution at each iteration","SNESMonitorSolution",SNESMonitorSolution,NULL)); 10379566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor_solution_update","View correction at each iteration","SNESMonitorSolutionUpdate",SNESMonitorSolutionUpdate,NULL)); 10389566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor_residual","View residual at each iteration","SNESMonitorResidual",SNESMonitorResidual,NULL)); 10399566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor_jacupdate_spectrum","Print the change in the spectrum of the Jacobian","SNESMonitorJacUpdateSpectrum",SNESMonitorJacUpdateSpectrum,NULL)); 10409566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes,"-snes_monitor_fields","Monitor norm of function per field","SNESMonitorSet",SNESMonitorFields,NULL)); 10419566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_monitor_pause_final", "Pauses all draw monitors at the final iterate", "SNESMonitorPauseFinal_Internal", PETSC_FALSE, &snes->pauseFinal, NULL)); 10422db13446SMatthew G. Knepley 10439566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",NULL,monfilename,sizeof(monfilename),&flg)); 10449566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)snes,monfilename)); 10455180491cSLisandro Dalcin 104690d69ab7SBarry Smith flg = PETSC_FALSE; 10479566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_monitor_lg_range","Plot function range at each iteration","SNESMonitorLGRange",flg,&flg,NULL)); 1048459f5d12SBarry Smith if (flg) { 1049459f5d12SBarry Smith PetscViewer ctx; 1050e24b481bSBarry Smith 10519566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&ctx)); 10529566063dSJacob Faibussowitsch PetscCall(SNESMonitorSet(snes,SNESMonitorLGRange,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy)); 1053459f5d12SBarry Smith } 10542e7541e6SPeter Brune 105590d69ab7SBarry Smith flg = PETSC_FALSE; 10569566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_converged_reason_view_cancel","Remove all converged reason viewers","SNESConvergedReasonViewCancel",flg,&flg,&set)); 10579566063dSJacob Faibussowitsch if (set && flg) PetscCall(SNESConvergedReasonViewCancel(snes)); 1058c4421ceaSFande Kong 1059c4421ceaSFande Kong flg = PETSC_FALSE; 10609566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESComputeJacobianDefault",flg,&flg,NULL)); 10614b27c08aSLois Curfman McInnes if (flg) { 10626cab3a1bSJed Brown void *functx; 1063b1f624c7SBarry Smith DM dm; 1064b1f624c7SBarry Smith DMSNES sdm; 10659566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 10669566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 1067b1f624c7SBarry Smith sdm->jacobianctx = NULL; 10689566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,NULL,NULL,&functx)); 10699566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESComputeJacobianDefault,functx)); 10709566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Setting default finite difference Jacobian matrix\n")); 10719b94acceSBarry Smith } 1072639f9d9dSBarry Smith 107344848bc4SPeter Brune flg = PETSC_FALSE; 10749566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_fd_function","Use finite differences (slow) to compute function from user objective","SNESObjectiveComputeFunctionDefaultFD",flg,&flg,NULL)); 1075*1baa6e33SBarry Smith if (flg) PetscCall(SNESSetFunction(snes,NULL,SNESObjectiveComputeFunctionDefaultFD,NULL)); 107697584545SPeter Brune 107797584545SPeter Brune flg = PETSC_FALSE; 10789566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_fd_color","Use finite differences with coloring to compute Jacobian","SNESComputeJacobianDefaultColor",flg,&flg,NULL)); 107944848bc4SPeter Brune if (flg) { 1080c52e227fSPeter Brune DM dm; 1081c52e227fSPeter Brune DMSNES sdm; 10829566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 10839566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 1084aace71b7SPeter Brune sdm->jacobianctx = NULL; 10859566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESComputeJacobianDefaultColor,NULL)); 10869566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Setting default finite difference coloring Jacobian matrix\n")); 108744848bc4SPeter Brune } 108844848bc4SPeter Brune 1089aa3661deSLisandro Dalcin flg = PETSC_FALSE; 10909566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","SNESSetUseMatrixFree",PETSC_FALSE,&snes->mf_operator,&flg)); 1091d8f46077SPeter Brune if (flg && snes->mf_operator) { 1092a8248277SBarry Smith snes->mf_operator = PETSC_TRUE; 1093d8f46077SPeter Brune snes->mf = PETSC_TRUE; 1094a8248277SBarry Smith } 1095aa3661deSLisandro Dalcin flg = PETSC_FALSE; 10969566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","SNESSetUseMatrixFree",PETSC_FALSE,&snes->mf,&flg)); 1097d8f46077SPeter Brune if (!flg && snes->mf_operator) snes->mf = PETSC_TRUE; 10989566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",snes->mf_version,&snes->mf_version,NULL)); 1099d28543b3SPeter Brune 1100c40d0f55SPeter Brune flg = PETSC_FALSE; 11019566063dSJacob Faibussowitsch PetscCall(SNESGetNPCSide(snes,&pcside)); 11029566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-snes_npc_side","SNES nonlinear preconditioner side","SNESSetNPCSide",PCSides,(PetscEnum)pcside,(PetscEnum*)&pcside,&flg)); 11039566063dSJacob Faibussowitsch if (flg) PetscCall(SNESSetNPCSide(snes,pcside)); 1104c40d0f55SPeter Brune 1105e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 11068a70d858SHong Zhang /* 11078a70d858SHong Zhang Publish convergence information using SAWs 11088a70d858SHong Zhang */ 11098a70d858SHong Zhang flg = PETSC_FALSE; 11109566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_monitor_saws","Publish SNES progress using SAWs","SNESMonitorSet",flg,&flg,NULL)); 11118a70d858SHong Zhang if (flg) { 11128a70d858SHong Zhang void *ctx; 11139566063dSJacob Faibussowitsch PetscCall(SNESMonitorSAWsCreate(snes,&ctx)); 11149566063dSJacob Faibussowitsch PetscCall(SNESMonitorSet(snes,SNESMonitorSAWs,ctx,SNESMonitorSAWsDestroy)); 11158a70d858SHong Zhang } 11168a70d858SHong Zhang #endif 11178a70d858SHong Zhang #if defined(PETSC_HAVE_SAWS) 1118b90c6cbeSBarry Smith { 1119b90c6cbeSBarry Smith PetscBool set; 1120b90c6cbeSBarry Smith flg = PETSC_FALSE; 11219566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_saws_block","Block for SAWs at end of SNESSolve","PetscObjectSAWsBlock",((PetscObject)snes)->amspublishblock,&flg,&set)); 1122*1baa6e33SBarry Smith if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)snes,flg)); 1123b90c6cbeSBarry Smith } 1124b90c6cbeSBarry Smith #endif 1125b90c6cbeSBarry Smith 112676b2cf59SMatthew Knepley for (i = 0; i < numberofsetfromoptions; i++) { 11279566063dSJacob Faibussowitsch PetscCall((*othersetfromoptions[i])(snes)); 112876b2cf59SMatthew Knepley } 112976b2cf59SMatthew Knepley 1130*1baa6e33SBarry Smith if (snes->ops->setfromoptions) PetscCall((*snes->ops->setfromoptions)(PetscOptionsObject,snes)); 11315d973c19SBarry Smith 11325d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 11339566063dSJacob Faibussowitsch PetscCall(PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)snes)); 1134d0609cedSBarry Smith PetscOptionsEnd(); 11354bbc92c1SBarry Smith 1136d8d34be6SBarry Smith if (snes->linesearch) { 11379566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &snes->linesearch)); 11389566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetFromOptions(snes->linesearch)); 1139d8d34be6SBarry Smith } 11409e764e56SPeter Brune 11416aa5e7e9SBarry Smith if (snes->usesksp) { 11429566063dSJacob Faibussowitsch if (!snes->ksp) PetscCall(SNESGetKSP(snes,&snes->ksp)); 11439566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre)); 11449566063dSJacob Faibussowitsch PetscCall(KSPSetFromOptions(snes->ksp)); 11456aa5e7e9SBarry Smith } 11466991f827SBarry Smith 1147b5badacbSBarry Smith /* if user has set the SNES NPC type via options database, create it. */ 11489566063dSJacob Faibussowitsch PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix)); 11499566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject)snes)->options,optionsprefix, "-npc_snes_type", &pcset)); 1150efd4aadfSBarry Smith if (pcset && (!snes->npc)) { 11519566063dSJacob Faibussowitsch PetscCall(SNESGetNPC(snes, &snes->npc)); 115251e86f29SPeter Brune } 1153*1baa6e33SBarry Smith if (snes->npc) PetscCall(SNESSetFromOptions(snes->npc)); 1154b3cd9a81SMatthew G. Knepley snes->setfromoptionscalled++; 1155b3cd9a81SMatthew G. Knepley PetscFunctionReturn(0); 1156b3cd9a81SMatthew G. Knepley } 1157b3cd9a81SMatthew G. Knepley 1158b3cd9a81SMatthew G. Knepley /*@ 1159b3cd9a81SMatthew G. Knepley SNESResetFromOptions - Sets various SNES and KSP parameters from user options ONLY if the SNES was previously set from options 1160b3cd9a81SMatthew G. Knepley 1161b3cd9a81SMatthew G. Knepley Collective on SNES 1162b3cd9a81SMatthew G. Knepley 1163b3cd9a81SMatthew G. Knepley Input Parameter: 1164b3cd9a81SMatthew G. Knepley . snes - the SNES context 1165b3cd9a81SMatthew G. Knepley 1166b3cd9a81SMatthew G. Knepley Level: beginner 1167b3cd9a81SMatthew G. Knepley 1168db781477SPatrick Sanan .seealso: `SNESSetFromOptions()`, `SNESSetOptionsPrefix()` 1169b3cd9a81SMatthew G. Knepley @*/ 1170b3cd9a81SMatthew G. Knepley PetscErrorCode SNESResetFromOptions(SNES snes) 1171b3cd9a81SMatthew G. Knepley { 1172b3cd9a81SMatthew G. Knepley PetscFunctionBegin; 11739566063dSJacob Faibussowitsch if (snes->setfromoptionscalled) PetscCall(SNESSetFromOptions(snes)); 11743a40ed3dSBarry Smith PetscFunctionReturn(0); 11759b94acceSBarry Smith } 11769b94acceSBarry Smith 1177bb9467b5SJed Brown /*@C 1178d25893d9SBarry Smith SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for 1179d25893d9SBarry Smith the nonlinear solvers. 1180d25893d9SBarry Smith 1181d25893d9SBarry Smith Logically Collective on SNES 1182d25893d9SBarry Smith 1183d25893d9SBarry Smith Input Parameters: 1184d25893d9SBarry Smith + snes - the SNES context 1185d25893d9SBarry Smith . compute - function to compute the context 1186d25893d9SBarry Smith - destroy - function to destroy the context 1187d25893d9SBarry Smith 1188d25893d9SBarry Smith Level: intermediate 1189d25893d9SBarry Smith 1190bb9467b5SJed Brown Notes: 1191bb9467b5SJed Brown This function is currently not available from Fortran. 1192bb9467b5SJed Brown 1193db781477SPatrick Sanan .seealso: `SNESGetApplicationContext()`, `SNESSetComputeApplicationContext()`, `SNESGetApplicationContext()` 1194d25893d9SBarry Smith @*/ 1195d25893d9SBarry Smith PetscErrorCode SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**)) 1196d25893d9SBarry Smith { 1197d25893d9SBarry Smith PetscFunctionBegin; 1198d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1199d25893d9SBarry Smith snes->ops->usercompute = compute; 1200d25893d9SBarry Smith snes->ops->userdestroy = destroy; 1201d25893d9SBarry Smith PetscFunctionReturn(0); 1202d25893d9SBarry Smith } 1203a847f771SSatish Balay 1204b07ff414SBarry Smith /*@ 12059b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 12069b94acceSBarry Smith the nonlinear solvers. 12079b94acceSBarry Smith 12083f9fe445SBarry Smith Logically Collective on SNES 1209fee21e36SBarry Smith 1210c7afd0dbSLois Curfman McInnes Input Parameters: 1211c7afd0dbSLois Curfman McInnes + snes - the SNES context 1212c7afd0dbSLois Curfman McInnes - usrP - optional user context 1213c7afd0dbSLois Curfman McInnes 121436851e7fSLois Curfman McInnes Level: intermediate 121536851e7fSLois Curfman McInnes 121695452b02SPatrick Sanan Fortran Notes: 121795452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 1218daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1219daf670e6SBarry Smith 1220db781477SPatrick Sanan .seealso: `SNESGetApplicationContext()` 12219b94acceSBarry Smith @*/ 12227087cfbeSBarry Smith PetscErrorCode SNESSetApplicationContext(SNES snes,void *usrP) 12239b94acceSBarry Smith { 1224b07ff414SBarry Smith KSP ksp; 12251b2093e4SBarry Smith 12263a40ed3dSBarry Smith PetscFunctionBegin; 12270700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12289566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&ksp)); 12299566063dSJacob Faibussowitsch PetscCall(KSPSetApplicationContext(ksp,usrP)); 12309b94acceSBarry Smith snes->user = usrP; 12313a40ed3dSBarry Smith PetscFunctionReturn(0); 12329b94acceSBarry Smith } 123374679c65SBarry Smith 1234b07ff414SBarry Smith /*@ 12359b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 12369b94acceSBarry Smith nonlinear solvers. 12379b94acceSBarry Smith 1238c7afd0dbSLois Curfman McInnes Not Collective 1239c7afd0dbSLois Curfman McInnes 12409b94acceSBarry Smith Input Parameter: 12419b94acceSBarry Smith . snes - SNES context 12429b94acceSBarry Smith 12439b94acceSBarry Smith Output Parameter: 12449b94acceSBarry Smith . usrP - user context 12459b94acceSBarry Smith 124695452b02SPatrick Sanan Fortran Notes: 124795452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 1248daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1249daf670e6SBarry Smith 125036851e7fSLois Curfman McInnes Level: intermediate 125136851e7fSLois Curfman McInnes 1252db781477SPatrick Sanan .seealso: `SNESSetApplicationContext()` 12539b94acceSBarry Smith @*/ 1254e71120c6SJed Brown PetscErrorCode SNESGetApplicationContext(SNES snes,void *usrP) 12559b94acceSBarry Smith { 12563a40ed3dSBarry Smith PetscFunctionBegin; 12570700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1258e71120c6SJed Brown *(void**)usrP = snes->user; 12593a40ed3dSBarry Smith PetscFunctionReturn(0); 12609b94acceSBarry Smith } 126174679c65SBarry Smith 12629b94acceSBarry Smith /*@ 1263ec5066bdSBarry Smith SNESSetUseMatrixFree - indicates that SNES should use matrix free finite difference matrix vector products internally to apply the Jacobian. 12643565c898SBarry Smith 12653565c898SBarry Smith Collective on SNES 12663565c898SBarry Smith 12673565c898SBarry Smith Input Parameters: 12683565c898SBarry Smith + snes - SNES context 12694ddffce6SLisandro Dalcin . mf_operator - use matrix-free only for the Amat used by SNESSetJacobian(), this means the user provided Pmat will continue to be used 12704ddffce6SLisandro Dalcin - mf - use matrix-free for both the Amat and Pmat used by SNESSetJacobian(), both the Amat and Pmat set in SNESSetJacobian() will be ignored 12713565c898SBarry Smith 12723565c898SBarry Smith Options Database: 12733565c898SBarry Smith + -snes_mf - use matrix free for both the mat and pmat operator 1274ec5066bdSBarry Smith . -snes_mf_operator - use matrix free only for the mat operator 1275ec5066bdSBarry Smith . -snes_fd_color - compute the Jacobian via coloring and finite differences. 1276ec5066bdSBarry Smith - -snes_fd - compute the Jacobian via finite differences (slow) 12773565c898SBarry Smith 12783565c898SBarry Smith Level: intermediate 12793565c898SBarry Smith 1280ec5066bdSBarry Smith Notes: 1281a5b23f4aSJose E. Roman SNES supports three approaches for computing (approximate) Jacobians: user provided via SNESSetJacobian(), matrix free, and computing explicitly with 1282ec5066bdSBarry Smith finite differences and coloring using MatFDColoring. It is also possible to use automatic differentiation and the MatFDColoring object. 1283ec5066bdSBarry Smith 1284db781477SPatrick Sanan .seealso: `SNESGetUseMatrixFree()`, `MatCreateSNESMF()`, `SNESComputeJacobianDefaultColor()` 12853565c898SBarry Smith @*/ 12863565c898SBarry Smith PetscErrorCode SNESSetUseMatrixFree(SNES snes,PetscBool mf_operator,PetscBool mf) 12873565c898SBarry Smith { 12883565c898SBarry Smith PetscFunctionBegin; 12893565c898SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 129088b4c220SStefano Zampini PetscValidLogicalCollectiveBool(snes,mf_operator,2); 129188b4c220SStefano Zampini PetscValidLogicalCollectiveBool(snes,mf,3); 12924ddffce6SLisandro Dalcin snes->mf = mf_operator ? PETSC_TRUE : mf; 12933565c898SBarry Smith snes->mf_operator = mf_operator; 12943565c898SBarry Smith PetscFunctionReturn(0); 12953565c898SBarry Smith } 12963565c898SBarry Smith 12973565c898SBarry Smith /*@ 1298ec5066bdSBarry Smith SNESGetUseMatrixFree - indicates if the SNES uses matrix free finite difference matrix vector products to apply the Jacobian. 12993565c898SBarry Smith 13003565c898SBarry Smith Collective on SNES 13013565c898SBarry Smith 13023565c898SBarry Smith Input Parameter: 13033565c898SBarry Smith . snes - SNES context 13043565c898SBarry Smith 13053565c898SBarry Smith Output Parameters: 13064ddffce6SLisandro Dalcin + mf_operator - use matrix-free only for the Amat used by SNESSetJacobian(), this means the user provided Pmat will continue to be used 13074ddffce6SLisandro Dalcin - mf - use matrix-free for both the Amat and Pmat used by SNESSetJacobian(), both the Amat and Pmat set in SNESSetJacobian() will be ignored 13083565c898SBarry Smith 13093565c898SBarry Smith Options Database: 13103565c898SBarry Smith + -snes_mf - use matrix free for both the mat and pmat operator 13113565c898SBarry Smith - -snes_mf_operator - use matrix free only for the mat operator 13123565c898SBarry Smith 13133565c898SBarry Smith Level: intermediate 13143565c898SBarry Smith 1315db781477SPatrick Sanan .seealso: `SNESSetUseMatrixFree()`, `MatCreateSNESMF()` 13163565c898SBarry Smith @*/ 13173565c898SBarry Smith PetscErrorCode SNESGetUseMatrixFree(SNES snes,PetscBool *mf_operator,PetscBool *mf) 13183565c898SBarry Smith { 13193565c898SBarry Smith PetscFunctionBegin; 13203565c898SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13213565c898SBarry Smith if (mf) *mf = snes->mf; 13223565c898SBarry Smith if (mf_operator) *mf_operator = snes->mf_operator; 13233565c898SBarry Smith PetscFunctionReturn(0); 13243565c898SBarry Smith } 13253565c898SBarry Smith 13263565c898SBarry Smith /*@ 1327c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 1328c8228a4eSBarry Smith at this time. 13299b94acceSBarry Smith 1330c7afd0dbSLois Curfman McInnes Not Collective 1331c7afd0dbSLois Curfman McInnes 13329b94acceSBarry Smith Input Parameter: 13339b94acceSBarry Smith . snes - SNES context 13349b94acceSBarry Smith 13359b94acceSBarry Smith Output Parameter: 13369b94acceSBarry Smith . iter - iteration number 13379b94acceSBarry Smith 1338c8228a4eSBarry Smith Notes: 1339c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 1340c8228a4eSBarry Smith 1341c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 134208405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 134308405cd6SLois Curfman McInnes .vb 134408405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 134508405cd6SLois Curfman McInnes if (!(it % 2)) { 134608405cd6SLois Curfman McInnes [compute Jacobian here] 134708405cd6SLois Curfman McInnes } 134808405cd6SLois Curfman McInnes .ve 1349c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 135008405cd6SLois Curfman McInnes recomputed every second SNES iteration. 1351c8228a4eSBarry Smith 1352c04deec6SBarry Smith After the SNES solve is complete this will return the number of nonlinear iterations used. 1353c04deec6SBarry Smith 135436851e7fSLois Curfman McInnes Level: intermediate 135536851e7fSLois Curfman McInnes 1356db781477SPatrick Sanan .seealso: `SNESGetLinearSolveIterations()` 13579b94acceSBarry Smith @*/ 13587087cfbeSBarry Smith PetscErrorCode SNESGetIterationNumber(SNES snes,PetscInt *iter) 13599b94acceSBarry Smith { 13603a40ed3dSBarry Smith PetscFunctionBegin; 13610700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13624482741eSBarry Smith PetscValidIntPointer(iter,2); 13639b94acceSBarry Smith *iter = snes->iter; 13643a40ed3dSBarry Smith PetscFunctionReturn(0); 13659b94acceSBarry Smith } 136674679c65SBarry Smith 1367360c497dSPeter Brune /*@ 1368360c497dSPeter Brune SNESSetIterationNumber - Sets the current iteration number. 1369360c497dSPeter Brune 1370360c497dSPeter Brune Not Collective 1371360c497dSPeter Brune 1372d8d19677SJose E. Roman Input Parameters: 1373a2b725a8SWilliam Gropp + snes - SNES context 1374a2b725a8SWilliam Gropp - iter - iteration number 1375360c497dSPeter Brune 1376360c497dSPeter Brune Level: developer 1377360c497dSPeter Brune 1378db781477SPatrick Sanan .seealso: `SNESGetLinearSolveIterations()` 1379360c497dSPeter Brune @*/ 1380360c497dSPeter Brune PetscErrorCode SNESSetIterationNumber(SNES snes,PetscInt iter) 1381360c497dSPeter Brune { 1382360c497dSPeter Brune PetscFunctionBegin; 1383360c497dSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13849566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes)); 1385360c497dSPeter Brune snes->iter = iter; 13869566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes)); 1387360c497dSPeter Brune PetscFunctionReturn(0); 1388360c497dSPeter Brune } 1389360c497dSPeter Brune 13909b94acceSBarry Smith /*@ 1391b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 13929b94acceSBarry Smith attempted by the nonlinear solver. 13939b94acceSBarry Smith 1394c7afd0dbSLois Curfman McInnes Not Collective 1395c7afd0dbSLois Curfman McInnes 13969b94acceSBarry Smith Input Parameter: 13979b94acceSBarry Smith . snes - SNES context 13989b94acceSBarry Smith 13999b94acceSBarry Smith Output Parameter: 14009b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 14019b94acceSBarry Smith 1402c96a6f78SLois Curfman McInnes Notes: 1403c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 1404c96a6f78SLois Curfman McInnes 140536851e7fSLois Curfman McInnes Level: intermediate 140636851e7fSLois Curfman McInnes 1407db781477SPatrick Sanan .seealso: `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`, 1408db781477SPatrick Sanan `SNESSetMaxNonlinearStepFailures()`, `SNESGetMaxNonlinearStepFailures()` 14099b94acceSBarry Smith @*/ 14107087cfbeSBarry Smith PetscErrorCode SNESGetNonlinearStepFailures(SNES snes,PetscInt *nfails) 14119b94acceSBarry Smith { 14123a40ed3dSBarry Smith PetscFunctionBegin; 14130700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14144482741eSBarry Smith PetscValidIntPointer(nfails,2); 141550ffb88aSMatthew Knepley *nfails = snes->numFailures; 141650ffb88aSMatthew Knepley PetscFunctionReturn(0); 141750ffb88aSMatthew Knepley } 141850ffb88aSMatthew Knepley 141950ffb88aSMatthew Knepley /*@ 1420b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 142150ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 142250ffb88aSMatthew Knepley 142350ffb88aSMatthew Knepley Not Collective 142450ffb88aSMatthew Knepley 142550ffb88aSMatthew Knepley Input Parameters: 142650ffb88aSMatthew Knepley + snes - SNES context 142750ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 142850ffb88aSMatthew Knepley 142950ffb88aSMatthew Knepley Level: intermediate 143050ffb88aSMatthew Knepley 1431db781477SPatrick Sanan .seealso: `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`, 1432db781477SPatrick Sanan `SNESGetMaxNonlinearStepFailures()`, `SNESGetNonlinearStepFailures()` 143350ffb88aSMatthew Knepley @*/ 14347087cfbeSBarry Smith PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 143550ffb88aSMatthew Knepley { 143650ffb88aSMatthew Knepley PetscFunctionBegin; 14370700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 143850ffb88aSMatthew Knepley snes->maxFailures = maxFails; 143950ffb88aSMatthew Knepley PetscFunctionReturn(0); 144050ffb88aSMatthew Knepley } 144150ffb88aSMatthew Knepley 144250ffb88aSMatthew Knepley /*@ 1443b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 144450ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 144550ffb88aSMatthew Knepley 144650ffb88aSMatthew Knepley Not Collective 144750ffb88aSMatthew Knepley 144850ffb88aSMatthew Knepley Input Parameter: 144950ffb88aSMatthew Knepley . snes - SNES context 145050ffb88aSMatthew Knepley 145150ffb88aSMatthew Knepley Output Parameter: 145250ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 145350ffb88aSMatthew Knepley 145450ffb88aSMatthew Knepley Level: intermediate 145550ffb88aSMatthew Knepley 1456db781477SPatrick Sanan .seealso: `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`, 1457db781477SPatrick Sanan `SNESSetMaxNonlinearStepFailures()`, `SNESGetNonlinearStepFailures()` 145858ebbce7SBarry Smith 145950ffb88aSMatthew Knepley @*/ 14607087cfbeSBarry Smith PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 146150ffb88aSMatthew Knepley { 146250ffb88aSMatthew Knepley PetscFunctionBegin; 14630700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14644482741eSBarry Smith PetscValidIntPointer(maxFails,2); 146550ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 14663a40ed3dSBarry Smith PetscFunctionReturn(0); 14679b94acceSBarry Smith } 1468a847f771SSatish Balay 14692541af92SBarry Smith /*@ 14702541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 14712541af92SBarry Smith done by SNES. 14722541af92SBarry Smith 14732541af92SBarry Smith Not Collective 14742541af92SBarry Smith 14752541af92SBarry Smith Input Parameter: 14762541af92SBarry Smith . snes - SNES context 14772541af92SBarry Smith 14782541af92SBarry Smith Output Parameter: 14792541af92SBarry Smith . nfuncs - number of evaluations 14802541af92SBarry Smith 14812541af92SBarry Smith Level: intermediate 14822541af92SBarry Smith 148395452b02SPatrick Sanan Notes: 148495452b02SPatrick Sanan Reset every time SNESSolve is called unless SNESSetCountersReset() is used. 1485971e163fSPeter Brune 1486db781477SPatrick Sanan .seealso: `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`, `SNESSetCountersReset()` 14872541af92SBarry Smith @*/ 14887087cfbeSBarry Smith PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 14892541af92SBarry Smith { 14902541af92SBarry Smith PetscFunctionBegin; 14910700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14922541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 14932541af92SBarry Smith *nfuncs = snes->nfuncs; 14942541af92SBarry Smith PetscFunctionReturn(0); 14952541af92SBarry Smith } 14962541af92SBarry Smith 14973d4c4710SBarry Smith /*@ 14983d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 14993d4c4710SBarry Smith linear solvers. 15003d4c4710SBarry Smith 15013d4c4710SBarry Smith Not Collective 15023d4c4710SBarry Smith 15033d4c4710SBarry Smith Input Parameter: 15043d4c4710SBarry Smith . snes - SNES context 15053d4c4710SBarry Smith 15063d4c4710SBarry Smith Output Parameter: 15073d4c4710SBarry Smith . nfails - number of failed solves 15083d4c4710SBarry Smith 15099d85da0cSMatthew G. Knepley Level: intermediate 15109d85da0cSMatthew G. Knepley 15119d85da0cSMatthew G. Knepley Options Database Keys: 15129d85da0cSMatthew G. Knepley . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated 15139d85da0cSMatthew G. Knepley 15143d4c4710SBarry Smith Notes: 15153d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 15163d4c4710SBarry Smith 1517db781477SPatrick Sanan .seealso: `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()` 15183d4c4710SBarry Smith @*/ 15197087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveFailures(SNES snes,PetscInt *nfails) 15203d4c4710SBarry Smith { 15213d4c4710SBarry Smith PetscFunctionBegin; 15220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15233d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 15243d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 15253d4c4710SBarry Smith PetscFunctionReturn(0); 15263d4c4710SBarry Smith } 15273d4c4710SBarry Smith 15283d4c4710SBarry Smith /*@ 15293d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 15303d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 15313d4c4710SBarry Smith 15323f9fe445SBarry Smith Logically Collective on SNES 15333d4c4710SBarry Smith 15343d4c4710SBarry Smith Input Parameters: 15353d4c4710SBarry Smith + snes - SNES context 15363d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 15373d4c4710SBarry Smith 15383d4c4710SBarry Smith Level: intermediate 15393d4c4710SBarry Smith 15409d85da0cSMatthew G. Knepley Options Database Keys: 15419d85da0cSMatthew G. Knepley . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated 15429d85da0cSMatthew G. Knepley 154395452b02SPatrick Sanan Notes: 154495452b02SPatrick Sanan By default this is 0; that is SNES returns on the first failed linear solve 15453d4c4710SBarry Smith 1546db781477SPatrick Sanan .seealso: `SNESGetLinearSolveFailures()`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()` 15473d4c4710SBarry Smith @*/ 15487087cfbeSBarry Smith PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 15493d4c4710SBarry Smith { 15503d4c4710SBarry Smith PetscFunctionBegin; 15510700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1552c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxFails,2); 15533d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 15543d4c4710SBarry Smith PetscFunctionReturn(0); 15553d4c4710SBarry Smith } 15563d4c4710SBarry Smith 15573d4c4710SBarry Smith /*@ 15583d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 15593d4c4710SBarry Smith are allowed before SNES terminates 15603d4c4710SBarry Smith 15613d4c4710SBarry Smith Not Collective 15623d4c4710SBarry Smith 15633d4c4710SBarry Smith Input Parameter: 15643d4c4710SBarry Smith . snes - SNES context 15653d4c4710SBarry Smith 15663d4c4710SBarry Smith Output Parameter: 15673d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 15683d4c4710SBarry Smith 15693d4c4710SBarry Smith Level: intermediate 15703d4c4710SBarry Smith 157195452b02SPatrick Sanan Notes: 157295452b02SPatrick Sanan By default this is 1; that is SNES returns on the first failed linear solve 15733d4c4710SBarry Smith 1574db781477SPatrick Sanan .seealso: `SNESGetLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, 15753d4c4710SBarry Smith @*/ 15767087cfbeSBarry Smith PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 15773d4c4710SBarry Smith { 15783d4c4710SBarry Smith PetscFunctionBegin; 15790700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15803d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 15813d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 15823d4c4710SBarry Smith PetscFunctionReturn(0); 15833d4c4710SBarry Smith } 15843d4c4710SBarry Smith 1585c96a6f78SLois Curfman McInnes /*@ 1586b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 1587c96a6f78SLois Curfman McInnes used by the nonlinear solver. 1588c96a6f78SLois Curfman McInnes 1589c7afd0dbSLois Curfman McInnes Not Collective 1590c7afd0dbSLois Curfman McInnes 1591c96a6f78SLois Curfman McInnes Input Parameter: 1592c96a6f78SLois Curfman McInnes . snes - SNES context 1593c96a6f78SLois Curfman McInnes 1594c96a6f78SLois Curfman McInnes Output Parameter: 1595c96a6f78SLois Curfman McInnes . lits - number of linear iterations 1596c96a6f78SLois Curfman McInnes 1597c96a6f78SLois Curfman McInnes Notes: 1598971e163fSPeter Brune This counter is reset to zero for each successive call to SNESSolve() unless SNESSetCountersReset() is used. 1599c96a6f78SLois Curfman McInnes 1600010be392SBarry Smith If the linear solver fails inside the SNESSolve() the iterations for that call to the linear solver are not included. If you wish to count them 1601010be392SBarry Smith then call KSPGetIterationNumber() after the failed solve. 1602010be392SBarry Smith 160336851e7fSLois Curfman McInnes Level: intermediate 160436851e7fSLois Curfman McInnes 1605db781477SPatrick Sanan .seealso: `SNESGetIterationNumber()`, `SNESGetLinearSolveFailures()`, `SNESGetMaxLinearSolveFailures()`, `SNESSetCountersReset()` 1606c96a6f78SLois Curfman McInnes @*/ 16077087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveIterations(SNES snes,PetscInt *lits) 1608c96a6f78SLois Curfman McInnes { 16093a40ed3dSBarry Smith PetscFunctionBegin; 16100700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 16114482741eSBarry Smith PetscValidIntPointer(lits,2); 1612c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 16133a40ed3dSBarry Smith PetscFunctionReturn(0); 1614c96a6f78SLois Curfman McInnes } 1615c96a6f78SLois Curfman McInnes 1616971e163fSPeter Brune /*@ 1617971e163fSPeter Brune SNESSetCountersReset - Sets whether or not the counters for linear iterations and function evaluations 1618971e163fSPeter Brune are reset every time SNESSolve() is called. 1619971e163fSPeter Brune 1620971e163fSPeter Brune Logically Collective on SNES 1621971e163fSPeter Brune 1622d8d19677SJose E. Roman Input Parameters: 1623971e163fSPeter Brune + snes - SNES context 1624971e163fSPeter Brune - reset - whether to reset the counters or not 1625971e163fSPeter Brune 1626971e163fSPeter Brune Notes: 1627fa19ca70SBarry Smith This defaults to PETSC_TRUE 1628971e163fSPeter Brune 1629971e163fSPeter Brune Level: developer 1630971e163fSPeter Brune 1631db781477SPatrick Sanan .seealso: `SNESGetNumberFunctionEvals()`, `SNESGetLinearSolveIterations()`, `SNESGetNPC()` 1632971e163fSPeter Brune @*/ 1633971e163fSPeter Brune PetscErrorCode SNESSetCountersReset(SNES snes,PetscBool reset) 1634971e163fSPeter Brune { 1635971e163fSPeter Brune PetscFunctionBegin; 1636971e163fSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1637971e163fSPeter Brune PetscValidLogicalCollectiveBool(snes,reset,2); 1638971e163fSPeter Brune snes->counters_reset = reset; 1639971e163fSPeter Brune PetscFunctionReturn(0); 1640971e163fSPeter Brune } 1641971e163fSPeter Brune 16422999313aSBarry Smith /*@ 16432999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 16442999313aSBarry Smith 16452999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 16462999313aSBarry Smith 16472999313aSBarry Smith Input Parameters: 16482999313aSBarry Smith + snes - the SNES context 16492999313aSBarry Smith - ksp - the KSP context 16502999313aSBarry Smith 16512999313aSBarry Smith Notes: 16522999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 16532999313aSBarry Smith so this routine is rarely needed. 16542999313aSBarry Smith 16552999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 16562999313aSBarry Smith decreased by one. 16572999313aSBarry Smith 16582999313aSBarry Smith Level: developer 16592999313aSBarry Smith 1660db781477SPatrick Sanan .seealso: `KSPGetPC()`, `SNESCreate()`, `KSPCreate()`, `SNESSetKSP()` 16612999313aSBarry Smith @*/ 16627087cfbeSBarry Smith PetscErrorCode SNESSetKSP(SNES snes,KSP ksp) 16632999313aSBarry Smith { 16642999313aSBarry Smith PetscFunctionBegin; 16650700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 16660700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 16672999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 16689566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ksp)); 16699566063dSJacob Faibussowitsch if (snes->ksp) PetscCall(PetscObjectDereference((PetscObject)snes->ksp)); 16702999313aSBarry Smith snes->ksp = ksp; 16712999313aSBarry Smith PetscFunctionReturn(0); 16722999313aSBarry Smith } 16732999313aSBarry Smith 16749b94acceSBarry Smith /* -----------------------------------------------------------*/ 167552baeb72SSatish Balay /*@ 16769b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 16779b94acceSBarry Smith 1678d083f849SBarry Smith Collective 1679c7afd0dbSLois Curfman McInnes 1680c7afd0dbSLois Curfman McInnes Input Parameters: 1681906ed7ccSBarry Smith . comm - MPI communicator 16829b94acceSBarry Smith 16839b94acceSBarry Smith Output Parameter: 16849b94acceSBarry Smith . outsnes - the new SNES context 16859b94acceSBarry Smith 1686c7afd0dbSLois Curfman McInnes Options Database Keys: 1687c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 1688c7afd0dbSLois Curfman McInnes and no preconditioning matrix 1689c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 1690c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 1691c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 1692c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 1693c1f60f51SBarry Smith 169436851e7fSLois Curfman McInnes Level: beginner 169536851e7fSLois Curfman McInnes 169695452b02SPatrick Sanan Developer Notes: 169795452b02SPatrick Sanan SNES always creates a KSP object even though many SNES methods do not use it. This is 1698efd4aadfSBarry Smith unfortunate and should be fixed at some point. The flag snes->usesksp indicates if the 1699efd4aadfSBarry Smith particular method does use KSP and regulates if the information about the KSP is printed 1700efd4aadfSBarry Smith in SNESView(). TSSetFromOptions() does call SNESSetFromOptions() which can lead to users being confused 1701efd4aadfSBarry Smith by help messages about meaningless SNES options. 1702efd4aadfSBarry Smith 1703efd4aadfSBarry Smith SNES always creates the snes->kspconvctx even though it is used by only one type. This should 1704efd4aadfSBarry Smith be fixed. 1705efd4aadfSBarry Smith 1706db781477SPatrick Sanan .seealso: `SNESSolve()`, `SNESDestroy()`, `SNES`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobian()` 1707a8054027SBarry Smith 17089b94acceSBarry Smith @*/ 17097087cfbeSBarry Smith PetscErrorCode SNESCreate(MPI_Comm comm,SNES *outsnes) 17109b94acceSBarry Smith { 17119b94acceSBarry Smith SNES snes; 1712fa9f3622SBarry Smith SNESKSPEW *kctx; 171337fcc0dbSBarry Smith 17143a40ed3dSBarry Smith PetscFunctionBegin; 1715ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 17160298fd71SBarry Smith *outsnes = NULL; 17179566063dSJacob Faibussowitsch PetscCall(SNESInitializePackage()); 17188ba1e511SMatthew Knepley 17199566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(snes,SNES_CLASSID,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView)); 17207adad957SLisandro Dalcin 17218d359177SBarry Smith snes->ops->converged = SNESConvergedDefault; 17222c155ee1SBarry Smith snes->usesksp = PETSC_TRUE; 172388976e71SPeter Brune snes->tolerancesset = PETSC_FALSE; 17249b94acceSBarry Smith snes->max_its = 50; 17259750a799SBarry Smith snes->max_funcs = 10000; 17269b94acceSBarry Smith snes->norm = 0.0; 1727c1e67a49SFande Kong snes->xnorm = 0.0; 1728c1e67a49SFande Kong snes->ynorm = 0.0; 1729365a6726SPeter Brune snes->normschedule = SNES_NORM_ALWAYS; 17306c67d002SPeter Brune snes->functype = SNES_FUNCTION_DEFAULT; 17313a2046daSBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 17323a2046daSBarry Smith snes->rtol = 1.e-5; 17333a2046daSBarry Smith #else 1734b4874afaSBarry Smith snes->rtol = 1.e-8; 17353a2046daSBarry Smith #endif 1736b4874afaSBarry Smith snes->ttol = 0.0; 17373a2046daSBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 17383a2046daSBarry Smith snes->abstol = 1.e-25; 17393a2046daSBarry Smith #else 174070441072SBarry Smith snes->abstol = 1.e-50; 17413a2046daSBarry Smith #endif 17427cd0ae37SLisandro Dalcin #if defined(PETSC_USE_REAL_SINGLE) 17437cd0ae37SLisandro Dalcin snes->stol = 1.e-5; 17447cd0ae37SLisandro Dalcin #else 1745c60f73f4SPeter Brune snes->stol = 1.e-8; 17467cd0ae37SLisandro Dalcin #endif 17473a2046daSBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 17483a2046daSBarry Smith snes->deltatol = 1.e-6; 17493a2046daSBarry Smith #else 17504b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 17513a2046daSBarry Smith #endif 1752e37c518bSBarry Smith snes->divtol = 1.e4; 1753e37c518bSBarry Smith snes->rnorm0 = 0; 17549b94acceSBarry Smith snes->nfuncs = 0; 175550ffb88aSMatthew Knepley snes->numFailures = 0; 175650ffb88aSMatthew Knepley snes->maxFailures = 1; 17577a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1758e35cf81dSBarry Smith snes->lagjacobian = 1; 175937ec4e1aSPeter Brune snes->jac_iter = 0; 176037ec4e1aSPeter Brune snes->lagjac_persist = PETSC_FALSE; 1761a8054027SBarry Smith snes->lagpreconditioner = 1; 176237ec4e1aSPeter Brune snes->pre_iter = 0; 176337ec4e1aSPeter Brune snes->lagpre_persist = PETSC_FALSE; 1764639f9d9dSBarry Smith snes->numbermonitors = 0; 1765c4421ceaSFande Kong snes->numberreasonviews = 0; 17669e5d0892SLisandro Dalcin snes->data = NULL; 17674dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1768186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 17696f24a144SLois Curfman McInnes snes->nwork = 0; 17709e5d0892SLisandro Dalcin snes->work = NULL; 177158c9b817SLisandro Dalcin snes->nvwork = 0; 17729e5d0892SLisandro Dalcin snes->vwork = NULL; 1773758f92a0SBarry Smith snes->conv_hist_len = 0; 1774758f92a0SBarry Smith snes->conv_hist_max = 0; 17750298fd71SBarry Smith snes->conv_hist = NULL; 17760298fd71SBarry Smith snes->conv_hist_its = NULL; 1777758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1778971e163fSPeter Brune snes->counters_reset = PETSC_TRUE; 1779e4ed7901SPeter Brune snes->vec_func_init_set = PETSC_FALSE; 1780184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 1781efd4aadfSBarry Smith snes->npcside = PC_RIGHT; 1782b3cd9a81SMatthew G. Knepley snes->setfromoptionscalled = 0; 1783c40d0f55SPeter Brune 1784d8f46077SPeter Brune snes->mf = PETSC_FALSE; 1785d8f46077SPeter Brune snes->mf_operator = PETSC_FALSE; 1786d8f46077SPeter Brune snes->mf_version = 1; 1787d8f46077SPeter Brune 17883d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 17893d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 17903d4c4710SBarry Smith 1791349187a7SBarry Smith snes->vizerotolerance = 1.e-8; 179276bd3646SJed Brown snes->checkjacdomainerror = PetscDefined(USE_DEBUG) ? PETSC_TRUE : PETSC_FALSE; 1793349187a7SBarry Smith 17944fc747eaSLawrence Mitchell /* Set this to true if the implementation of SNESSolve_XXX does compute the residual at the final solution. */ 17954fc747eaSLawrence Mitchell snes->alwayscomputesfinalresidual = PETSC_FALSE; 17964fc747eaSLawrence Mitchell 17979b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 17989566063dSJacob Faibussowitsch PetscCall(PetscNewLog(snes,&kctx)); 1799f5af7f23SKarl Rupp 18009b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 18019b94acceSBarry Smith kctx->version = 2; 18020f0abf79SStefano Zampini kctx->rtol_0 = 0.3; /* Eisenstat and Walker suggest rtol_0=.5, but 18039b94acceSBarry Smith this was too large for some test cases */ 180475567043SBarry Smith kctx->rtol_last = 0.0; 18050f0abf79SStefano Zampini kctx->rtol_max = 0.9; 18069b94acceSBarry Smith kctx->gamma = 1.0; 18070f0abf79SStefano Zampini kctx->alpha = 0.5*(1.0 + PetscSqrtReal(5.0)); 180871f87433Sdalcinl kctx->alpha2 = kctx->alpha; 18090f0abf79SStefano Zampini kctx->threshold = 0.1; 181075567043SBarry Smith kctx->lresid_last = 0.0; 181175567043SBarry Smith kctx->norm_last = 0.0; 18129b94acceSBarry Smith 18130f0abf79SStefano Zampini kctx->rk_last = 0.0; 18140f0abf79SStefano Zampini kctx->rk_last_2 = 0.0; 18150f0abf79SStefano Zampini kctx->rtol_last_2 = 0.0; 18160f0abf79SStefano Zampini kctx->v4_p1 = 0.1; 18170f0abf79SStefano Zampini kctx->v4_p2 = 0.4; 18180f0abf79SStefano Zampini kctx->v4_p3 = 0.7; 18190f0abf79SStefano Zampini kctx->v4_m1 = 0.8; 18200f0abf79SStefano Zampini kctx->v4_m2 = 0.5; 18210f0abf79SStefano Zampini kctx->v4_m3 = 0.1; 18220f0abf79SStefano Zampini kctx->v4_m4 = 0.5; 18230f0abf79SStefano Zampini 18249b94acceSBarry Smith *outsnes = snes; 18253a40ed3dSBarry Smith PetscFunctionReturn(0); 18269b94acceSBarry Smith } 18279b94acceSBarry Smith 182888f0584fSBarry Smith /*MC 1829411c0326SBarry Smith SNESFunction - Functional form used to convey the nonlinear function to be solved by SNES 183088f0584fSBarry Smith 183188f0584fSBarry Smith Synopsis: 1832411c0326SBarry Smith #include "petscsnes.h" 1833411c0326SBarry Smith PetscErrorCode SNESFunction(SNES snes,Vec x,Vec f,void *ctx); 183488f0584fSBarry Smith 18351843f636SBarry Smith Collective on snes 18361843f636SBarry Smith 183788f0584fSBarry Smith Input Parameters: 183888f0584fSBarry Smith + snes - the SNES context 183988f0584fSBarry Smith . x - state at which to evaluate residual 184088f0584fSBarry Smith - ctx - optional user-defined function context, passed in with SNESSetFunction() 184188f0584fSBarry Smith 184288f0584fSBarry Smith Output Parameter: 184388f0584fSBarry Smith . f - vector to put residual (function value) 184488f0584fSBarry Smith 1845878cb397SSatish Balay Level: intermediate 1846878cb397SSatish Balay 1847db781477SPatrick Sanan .seealso: `SNESSetFunction()`, `SNESGetFunction()` 184888f0584fSBarry Smith M*/ 184988f0584fSBarry Smith 18509b94acceSBarry Smith /*@C 18519b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 18529b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 18539b94acceSBarry Smith equations. 18549b94acceSBarry Smith 18553f9fe445SBarry Smith Logically Collective on SNES 1856fee21e36SBarry Smith 1857c7afd0dbSLois Curfman McInnes Input Parameters: 1858c7afd0dbSLois Curfman McInnes + snes - the SNES context 18596b7fb656SBarry Smith . r - vector to store function values, may be NULL 1860f8b49ee9SBarry Smith . f - function evaluation routine; see SNESFunction for calling sequence details 1861c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 18620298fd71SBarry Smith function evaluation routine (may be NULL) 18639b94acceSBarry Smith 18649b94acceSBarry Smith Notes: 18659b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 18669b94acceSBarry Smith $ f'(x) x = -f(x), 1867c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 18689b94acceSBarry Smith 186936851e7fSLois Curfman McInnes Level: beginner 187036851e7fSLois Curfman McInnes 1871db781477SPatrick Sanan .seealso: `SNESGetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESSetPicard()`, `SNESFunction` 18729b94acceSBarry Smith @*/ 1873f8b49ee9SBarry Smith PetscErrorCode SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*f)(SNES,Vec,Vec,void*),void *ctx) 18749b94acceSBarry Smith { 18756cab3a1bSJed Brown DM dm; 18766cab3a1bSJed Brown 18773a40ed3dSBarry Smith PetscFunctionBegin; 18780700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1879d2a683ecSLisandro Dalcin if (r) { 1880d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1881d2a683ecSLisandro Dalcin PetscCheckSameComm(snes,1,r,2); 18829566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)r)); 18839566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_func)); 188485385478SLisandro Dalcin snes->vec_func = r; 1885d2a683ecSLisandro Dalcin } 18869566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 18879566063dSJacob Faibussowitsch PetscCall(DMSNESSetFunction(dm,f,ctx)); 1888bbc1464cSBarry Smith if (f == SNESPicardComputeFunction) { 18899566063dSJacob Faibussowitsch PetscCall(DMSNESSetMFFunction(dm,SNESPicardComputeMFFunction,ctx)); 1890bbc1464cSBarry Smith } 18913a40ed3dSBarry Smith PetscFunctionReturn(0); 18929b94acceSBarry Smith } 18939b94acceSBarry Smith 1894e4ed7901SPeter Brune /*@C 1895e4ed7901SPeter Brune SNESSetInitialFunction - Sets the function vector to be used as the 1896e4ed7901SPeter Brune function norm at the initialization of the method. In some 1897e4ed7901SPeter Brune instances, the user has precomputed the function before calling 1898e4ed7901SPeter Brune SNESSolve. This function allows one to avoid a redundant call 1899e4ed7901SPeter Brune to SNESComputeFunction in that case. 1900e4ed7901SPeter Brune 1901e4ed7901SPeter Brune Logically Collective on SNES 1902e4ed7901SPeter Brune 1903e4ed7901SPeter Brune Input Parameters: 1904e4ed7901SPeter Brune + snes - the SNES context 1905e4ed7901SPeter Brune - f - vector to store function value 1906e4ed7901SPeter Brune 1907e4ed7901SPeter Brune Notes: 1908e4ed7901SPeter Brune This should not be modified during the solution procedure. 1909e4ed7901SPeter Brune 1910e4ed7901SPeter Brune This is used extensively in the SNESFAS hierarchy and in nonlinear preconditioning. 1911e4ed7901SPeter Brune 1912e4ed7901SPeter Brune Level: developer 1913e4ed7901SPeter Brune 1914db781477SPatrick Sanan .seealso: `SNESSetFunction()`, `SNESComputeFunction()`, `SNESSetInitialFunctionNorm()` 1915e4ed7901SPeter Brune @*/ 1916e4ed7901SPeter Brune PetscErrorCode SNESSetInitialFunction(SNES snes, Vec f) 1917e4ed7901SPeter Brune { 1918e4ed7901SPeter Brune Vec vec_func; 1919e4ed7901SPeter Brune 1920e4ed7901SPeter Brune PetscFunctionBegin; 1921e4ed7901SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1922e4ed7901SPeter Brune PetscValidHeaderSpecific(f,VEC_CLASSID,2); 1923e4ed7901SPeter Brune PetscCheckSameComm(snes,1,f,2); 1924efd4aadfSBarry Smith if (snes->npcside == PC_LEFT && snes->functype == SNES_FUNCTION_PRECONDITIONED) { 1925902f982fSPeter Brune snes->vec_func_init_set = PETSC_FALSE; 1926902f982fSPeter Brune PetscFunctionReturn(0); 1927902f982fSPeter Brune } 19289566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,&vec_func,NULL,NULL)); 19299566063dSJacob Faibussowitsch PetscCall(VecCopy(f,vec_func)); 1930f5af7f23SKarl Rupp 1931217b9c2eSPeter Brune snes->vec_func_init_set = PETSC_TRUE; 1932e4ed7901SPeter Brune PetscFunctionReturn(0); 1933e4ed7901SPeter Brune } 1934e4ed7901SPeter Brune 1935534ebe21SPeter Brune /*@ 1936a5b23f4aSJose E. Roman SNESSetNormSchedule - Sets the SNESNormSchedule used in convergence and monitoring 1937534ebe21SPeter Brune of the SNES method. 1938534ebe21SPeter Brune 1939534ebe21SPeter Brune Logically Collective on SNES 1940534ebe21SPeter Brune 1941534ebe21SPeter Brune Input Parameters: 1942534ebe21SPeter Brune + snes - the SNES context 1943365a6726SPeter Brune - normschedule - the frequency of norm computation 1944534ebe21SPeter Brune 1945517f1916SMatthew G. Knepley Options Database Key: 194667b8a455SSatish Balay . -snes_norm_schedule <none, always, initialonly, finalonly, initialfinalonly> - set the schedule 1947517f1916SMatthew G. Knepley 1948534ebe21SPeter Brune Notes: 1949365a6726SPeter Brune Only certain SNES methods support certain SNESNormSchedules. Most require evaluation 1950534ebe21SPeter Brune of the nonlinear function and the taking of its norm at every iteration to 1951534ebe21SPeter Brune even ensure convergence at all. However, methods such as custom Gauss-Seidel methods 1952a5b23f4aSJose E. Roman (SNESNGS) and the like do not require the norm of the function to be computed, and therefore 1953534ebe21SPeter Brune may either be monitored for convergence or not. As these are often used as nonlinear 1954534ebe21SPeter Brune preconditioners, monitoring the norm of their error is not a useful enterprise within 1955534ebe21SPeter Brune their solution. 1956534ebe21SPeter Brune 1957534ebe21SPeter Brune Level: developer 1958534ebe21SPeter Brune 1959db781477SPatrick Sanan .seealso: `SNESGetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 1960534ebe21SPeter Brune @*/ 1961365a6726SPeter Brune PetscErrorCode SNESSetNormSchedule(SNES snes, SNESNormSchedule normschedule) 1962534ebe21SPeter Brune { 1963534ebe21SPeter Brune PetscFunctionBegin; 1964534ebe21SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1965365a6726SPeter Brune snes->normschedule = normschedule; 1966534ebe21SPeter Brune PetscFunctionReturn(0); 1967534ebe21SPeter Brune } 1968534ebe21SPeter Brune 1969534ebe21SPeter Brune /*@ 1970a5b23f4aSJose E. Roman SNESGetNormSchedule - Gets the SNESNormSchedule used in convergence and monitoring 1971534ebe21SPeter Brune of the SNES method. 1972534ebe21SPeter Brune 1973534ebe21SPeter Brune Logically Collective on SNES 1974534ebe21SPeter Brune 1975534ebe21SPeter Brune Input Parameters: 1976534ebe21SPeter Brune + snes - the SNES context 1977365a6726SPeter Brune - normschedule - the type of the norm used 1978534ebe21SPeter Brune 1979534ebe21SPeter Brune Level: advanced 1980534ebe21SPeter Brune 1981db781477SPatrick Sanan .seealso: `SNESSetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 1982534ebe21SPeter Brune @*/ 1983365a6726SPeter Brune PetscErrorCode SNESGetNormSchedule(SNES snes, SNESNormSchedule *normschedule) 1984534ebe21SPeter Brune { 1985534ebe21SPeter Brune PetscFunctionBegin; 1986534ebe21SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1987365a6726SPeter Brune *normschedule = snes->normschedule; 1988534ebe21SPeter Brune PetscFunctionReturn(0); 1989534ebe21SPeter Brune } 1990534ebe21SPeter Brune 1991c5ce4427SMatthew G. Knepley /*@ 1992c5ce4427SMatthew G. Knepley SNESSetFunctionNorm - Sets the last computed residual norm. 1993c5ce4427SMatthew G. Knepley 1994c5ce4427SMatthew G. Knepley Logically Collective on SNES 1995c5ce4427SMatthew G. Knepley 1996c5ce4427SMatthew G. Knepley Input Parameters: 1997c5ce4427SMatthew G. Knepley + snes - the SNES context 1998c5ce4427SMatthew G. Knepley 1999c5ce4427SMatthew G. Knepley - normschedule - the frequency of norm computation 2000c5ce4427SMatthew G. Knepley 2001c5ce4427SMatthew G. Knepley Level: developer 2002c5ce4427SMatthew G. Knepley 2003db781477SPatrick Sanan .seealso: `SNESGetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 2004c5ce4427SMatthew G. Knepley @*/ 2005c5ce4427SMatthew G. Knepley PetscErrorCode SNESSetFunctionNorm(SNES snes, PetscReal norm) 2006c5ce4427SMatthew G. Knepley { 2007c5ce4427SMatthew G. Knepley PetscFunctionBegin; 2008c5ce4427SMatthew G. Knepley PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2009c5ce4427SMatthew G. Knepley snes->norm = norm; 2010c5ce4427SMatthew G. Knepley PetscFunctionReturn(0); 2011c5ce4427SMatthew G. Knepley } 2012c5ce4427SMatthew G. Knepley 2013c5ce4427SMatthew G. Knepley /*@ 2014c5ce4427SMatthew G. Knepley SNESGetFunctionNorm - Gets the last computed norm of the residual 2015c5ce4427SMatthew G. Knepley 2016c5ce4427SMatthew G. Knepley Not Collective 2017c5ce4427SMatthew G. Knepley 2018c5ce4427SMatthew G. Knepley Input Parameter: 2019c5ce4427SMatthew G. Knepley . snes - the SNES context 2020c5ce4427SMatthew G. Knepley 2021c5ce4427SMatthew G. Knepley Output Parameter: 2022c5ce4427SMatthew G. Knepley . norm - the last computed residual norm 2023c5ce4427SMatthew G. Knepley 2024c5ce4427SMatthew G. Knepley Level: developer 2025c5ce4427SMatthew G. Knepley 2026db781477SPatrick Sanan .seealso: `SNESSetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 2027c5ce4427SMatthew G. Knepley @*/ 2028c5ce4427SMatthew G. Knepley PetscErrorCode SNESGetFunctionNorm(SNES snes, PetscReal *norm) 2029c5ce4427SMatthew G. Knepley { 2030c5ce4427SMatthew G. Knepley PetscFunctionBegin; 2031c5ce4427SMatthew G. Knepley PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2032dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 2); 2033c5ce4427SMatthew G. Knepley *norm = snes->norm; 2034c5ce4427SMatthew G. Knepley PetscFunctionReturn(0); 2035c5ce4427SMatthew G. Knepley } 2036c5ce4427SMatthew G. Knepley 2037c1e67a49SFande Kong /*@ 2038c1e67a49SFande Kong SNESGetUpdateNorm - Gets the last computed norm of the Newton update 2039c1e67a49SFande Kong 2040c1e67a49SFande Kong Not Collective 2041c1e67a49SFande Kong 2042c1e67a49SFande Kong Input Parameter: 2043c1e67a49SFande Kong . snes - the SNES context 2044c1e67a49SFande Kong 2045c1e67a49SFande Kong Output Parameter: 2046c1e67a49SFande Kong . ynorm - the last computed update norm 2047c1e67a49SFande Kong 2048c1e67a49SFande Kong Level: developer 2049c1e67a49SFande Kong 2050db781477SPatrick Sanan .seealso: `SNESSetNormSchedule()`, `SNESComputeFunction()`, `SNESGetFunctionNorm()` 2051c1e67a49SFande Kong @*/ 2052c1e67a49SFande Kong PetscErrorCode SNESGetUpdateNorm(SNES snes, PetscReal *ynorm) 2053c1e67a49SFande Kong { 2054c1e67a49SFande Kong PetscFunctionBegin; 2055c1e67a49SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2056dadcf809SJacob Faibussowitsch PetscValidRealPointer(ynorm, 2); 2057c1e67a49SFande Kong *ynorm = snes->ynorm; 2058c1e67a49SFande Kong PetscFunctionReturn(0); 2059c1e67a49SFande Kong } 2060c1e67a49SFande Kong 2061c1e67a49SFande Kong /*@ 20624591eaf2SFande Kong SNESGetSolutionNorm - Gets the last computed norm of the solution 2063c1e67a49SFande Kong 2064c1e67a49SFande Kong Not Collective 2065c1e67a49SFande Kong 2066c1e67a49SFande Kong Input Parameter: 2067c1e67a49SFande Kong . snes - the SNES context 2068c1e67a49SFande Kong 2069c1e67a49SFande Kong Output Parameter: 2070c1e67a49SFande Kong . xnorm - the last computed solution norm 2071c1e67a49SFande Kong 2072c1e67a49SFande Kong Level: developer 2073c1e67a49SFande Kong 2074db781477SPatrick Sanan .seealso: `SNESSetNormSchedule()`, `SNESComputeFunction()`, `SNESGetFunctionNorm()`, `SNESGetUpdateNorm()` 2075c1e67a49SFande Kong @*/ 2076c1e67a49SFande Kong PetscErrorCode SNESGetSolutionNorm(SNES snes, PetscReal *xnorm) 2077c1e67a49SFande Kong { 2078c1e67a49SFande Kong PetscFunctionBegin; 2079c1e67a49SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2080dadcf809SJacob Faibussowitsch PetscValidRealPointer(xnorm, 2); 2081c1e67a49SFande Kong *xnorm = snes->xnorm; 2082c1e67a49SFande Kong PetscFunctionReturn(0); 2083c1e67a49SFande Kong } 2084c1e67a49SFande Kong 208547073ea2SPeter Brune /*@C 2086a5b23f4aSJose E. Roman SNESSetFunctionType - Sets the SNESNormSchedule used in convergence and monitoring 208747073ea2SPeter Brune of the SNES method. 208847073ea2SPeter Brune 208947073ea2SPeter Brune Logically Collective on SNES 209047073ea2SPeter Brune 209147073ea2SPeter Brune Input Parameters: 209247073ea2SPeter Brune + snes - the SNES context 209347073ea2SPeter Brune - normschedule - the frequency of norm computation 209447073ea2SPeter Brune 209547073ea2SPeter Brune Notes: 209647073ea2SPeter Brune Only certain SNES methods support certain SNESNormSchedules. Most require evaluation 209747073ea2SPeter Brune of the nonlinear function and the taking of its norm at every iteration to 209847073ea2SPeter Brune even ensure convergence at all. However, methods such as custom Gauss-Seidel methods 2099a5b23f4aSJose E. Roman (SNESNGS) and the like do not require the norm of the function to be computed, and therefore 210047073ea2SPeter Brune may either be monitored for convergence or not. As these are often used as nonlinear 210147073ea2SPeter Brune preconditioners, monitoring the norm of their error is not a useful enterprise within 210247073ea2SPeter Brune their solution. 210347073ea2SPeter Brune 210447073ea2SPeter Brune Level: developer 210547073ea2SPeter Brune 2106db781477SPatrick Sanan .seealso: `SNESGetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 210747073ea2SPeter Brune @*/ 210847073ea2SPeter Brune PetscErrorCode SNESSetFunctionType(SNES snes, SNESFunctionType type) 210947073ea2SPeter Brune { 211047073ea2SPeter Brune PetscFunctionBegin; 211147073ea2SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 211247073ea2SPeter Brune snes->functype = type; 211347073ea2SPeter Brune PetscFunctionReturn(0); 211447073ea2SPeter Brune } 211547073ea2SPeter Brune 211647073ea2SPeter Brune /*@C 2117a5b23f4aSJose E. Roman SNESGetFunctionType - Gets the SNESNormSchedule used in convergence and monitoring 211847073ea2SPeter Brune of the SNES method. 211947073ea2SPeter Brune 212047073ea2SPeter Brune Logically Collective on SNES 212147073ea2SPeter Brune 212247073ea2SPeter Brune Input Parameters: 212347073ea2SPeter Brune + snes - the SNES context 212447073ea2SPeter Brune - normschedule - the type of the norm used 212547073ea2SPeter Brune 212647073ea2SPeter Brune Level: advanced 212747073ea2SPeter Brune 2128db781477SPatrick Sanan .seealso: `SNESSetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 212947073ea2SPeter Brune @*/ 213047073ea2SPeter Brune PetscErrorCode SNESGetFunctionType(SNES snes, SNESFunctionType *type) 213147073ea2SPeter Brune { 213247073ea2SPeter Brune PetscFunctionBegin; 213347073ea2SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 213447073ea2SPeter Brune *type = snes->functype; 2135534ebe21SPeter Brune PetscFunctionReturn(0); 2136534ebe21SPeter Brune } 2137534ebe21SPeter Brune 2138bf388a1fSBarry Smith /*MC 2139be95d8f1SBarry Smith SNESNGSFunction - function used to convey a Gauss-Seidel sweep on the nonlinear function 2140bf388a1fSBarry Smith 2141bf388a1fSBarry Smith Synopsis: 2142aaa7dc30SBarry Smith #include <petscsnes.h> 2143be95d8f1SBarry Smith $ SNESNGSFunction(SNES snes,Vec x,Vec b,void *ctx); 2144bf388a1fSBarry Smith 21451843f636SBarry Smith Collective on snes 21461843f636SBarry Smith 21471843f636SBarry Smith Input Parameters: 2148bf388a1fSBarry Smith + X - solution vector 2149bf388a1fSBarry Smith . B - RHS vector 2150bf388a1fSBarry Smith - ctx - optional user-defined Gauss-Seidel context 2151bf388a1fSBarry Smith 21521843f636SBarry Smith Output Parameter: 21531843f636SBarry Smith . X - solution vector 21541843f636SBarry Smith 2155878cb397SSatish Balay Level: intermediate 2156878cb397SSatish Balay 2157db781477SPatrick Sanan .seealso: `SNESSetNGS()`, `SNESGetNGS()` 2158bf388a1fSBarry Smith M*/ 2159bf388a1fSBarry Smith 2160c79ef259SPeter Brune /*@C 2161be95d8f1SBarry Smith SNESSetNGS - Sets the user nonlinear Gauss-Seidel routine for 2162c79ef259SPeter Brune use with composed nonlinear solvers. 2163c79ef259SPeter Brune 2164c79ef259SPeter Brune Input Parameters: 2165c79ef259SPeter Brune + snes - the SNES context 2166be95d8f1SBarry Smith . f - function evaluation routine to apply Gauss-Seidel see SNESNGSFunction 2167c79ef259SPeter Brune - ctx - [optional] user-defined context for private data for the 21680298fd71SBarry Smith smoother evaluation routine (may be NULL) 2169c79ef259SPeter Brune 2170c79ef259SPeter Brune Notes: 2171be95d8f1SBarry Smith The NGS routines are used by the composed nonlinear solver to generate 2172c79ef259SPeter Brune a problem appropriate update to the solution, particularly FAS. 2173c79ef259SPeter Brune 2174d28543b3SPeter Brune Level: intermediate 2175c79ef259SPeter Brune 2176db781477SPatrick Sanan .seealso: `SNESGetFunction()`, `SNESComputeNGS()` 2177c79ef259SPeter Brune @*/ 2178be95d8f1SBarry Smith PetscErrorCode SNESSetNGS(SNES snes,PetscErrorCode (*f)(SNES,Vec,Vec,void*),void *ctx) 21796cab3a1bSJed Brown { 21806cab3a1bSJed Brown DM dm; 21816cab3a1bSJed Brown 2182646217ecSPeter Brune PetscFunctionBegin; 21836cab3a1bSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 21849566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 21859566063dSJacob Faibussowitsch PetscCall(DMSNESSetNGS(dm,f,ctx)); 2186646217ecSPeter Brune PetscFunctionReturn(0); 2187646217ecSPeter Brune } 2188646217ecSPeter Brune 2189bbc1464cSBarry Smith /* 2190bbc1464cSBarry Smith This is used for -snes_mf_operator; it uses a duplicate of snes->jacobian_pre because snes->jacobian_pre cannot be 2191bbc1464cSBarry Smith changed during the KSPSolve() 2192bbc1464cSBarry Smith */ 2193bbc1464cSBarry Smith PetscErrorCode SNESPicardComputeMFFunction(SNES snes,Vec x,Vec f,void *ctx) 2194bbc1464cSBarry Smith { 2195bbc1464cSBarry Smith DM dm; 2196bbc1464cSBarry Smith DMSNES sdm; 2197bbc1464cSBarry Smith 2198bbc1464cSBarry Smith PetscFunctionBegin; 21999566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 22009566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 22015f80ce2aSJacob Faibussowitsch PetscCheck(sdm->ops->computepjacobian,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetPicard() to provide Picard Jacobian."); 2202bbc1464cSBarry Smith /* A(x)*x - b(x) */ 2203bbc1464cSBarry Smith if (sdm->ops->computepfunction) { 2204bbc1464cSBarry Smith PetscStackPush("SNES Picard user function"); 22059566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computepfunction)(snes,x,f,sdm->pctx)); 2206bbc1464cSBarry Smith PetscStackPop; 22079566063dSJacob Faibussowitsch PetscCall(VecScale(f,-1.0)); 2208bbc1464cSBarry Smith if (!snes->picard) { 22090df40c35SBarry Smith /* Cannot share nonzero pattern because of the possible use of SNESComputeJacobianDefault() */ 22109566063dSJacob Faibussowitsch PetscCall(MatDuplicate(snes->jacobian_pre,MAT_DO_NOT_COPY_VALUES,&snes->picard)); 2211bbc1464cSBarry Smith } 2212bbc1464cSBarry Smith PetscStackPush("SNES Picard user Jacobian"); 22139566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computepjacobian)(snes,x,snes->picard,snes->picard,sdm->pctx)); 2214bbc1464cSBarry Smith PetscStackPop; 22159566063dSJacob Faibussowitsch PetscCall(MatMultAdd(snes->picard,x,f,f)); 2216bbc1464cSBarry Smith } else { 2217bbc1464cSBarry Smith PetscStackPush("SNES Picard user Jacobian"); 22189566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computepjacobian)(snes,x,snes->picard,snes->picard,sdm->pctx)); 2219bbc1464cSBarry Smith PetscStackPop; 22209566063dSJacob Faibussowitsch PetscCall(MatMult(snes->picard,x,f)); 2221bbc1464cSBarry Smith } 2222bbc1464cSBarry Smith PetscFunctionReturn(0); 2223bbc1464cSBarry Smith } 2224bbc1464cSBarry Smith 222525acbd8eSLisandro Dalcin PetscErrorCode SNESPicardComputeFunction(SNES snes,Vec x,Vec f,void *ctx) 22268b0a5094SBarry Smith { 2227e03ab78fSPeter Brune DM dm; 2228942e3340SBarry Smith DMSNES sdm; 22296cab3a1bSJed Brown 22308b0a5094SBarry Smith PetscFunctionBegin; 22319566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 22329566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 22335f80ce2aSJacob Faibussowitsch PetscCheck(sdm->ops->computepjacobian,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetPicard() to provide Picard Jacobian."); 22348b0a5094SBarry Smith /* A(x)*x - b(x) */ 2235bbc1464cSBarry Smith if (sdm->ops->computepfunction) { 223625acbd8eSLisandro Dalcin PetscStackPush("SNES Picard user function"); 22379566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computepfunction)(snes,x,f,sdm->pctx)); 223825acbd8eSLisandro Dalcin PetscStackPop; 22399566063dSJacob Faibussowitsch PetscCall(VecScale(f,-1.0)); 224025acbd8eSLisandro Dalcin PetscStackPush("SNES Picard user Jacobian"); 22419566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computepjacobian)(snes,x,snes->jacobian,snes->jacobian_pre,sdm->pctx)); 224225acbd8eSLisandro Dalcin PetscStackPop; 22439566063dSJacob Faibussowitsch PetscCall(MatMultAdd(snes->jacobian_pre,x,f,f)); 2244bbc1464cSBarry Smith } else { 2245bbc1464cSBarry Smith PetscStackPush("SNES Picard user Jacobian"); 22469566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computepjacobian)(snes,x,snes->jacobian,snes->jacobian_pre,sdm->pctx)); 2247bbc1464cSBarry Smith PetscStackPop; 22489566063dSJacob Faibussowitsch PetscCall(MatMult(snes->jacobian_pre,x,f)); 2249bbc1464cSBarry Smith } 22508b0a5094SBarry Smith PetscFunctionReturn(0); 22518b0a5094SBarry Smith } 22528b0a5094SBarry Smith 225325acbd8eSLisandro Dalcin PetscErrorCode SNESPicardComputeJacobian(SNES snes,Vec x1,Mat J,Mat B,void *ctx) 22548b0a5094SBarry Smith { 22558b0a5094SBarry Smith PetscFunctionBegin; 2256e03ab78fSPeter Brune /* the jacobian matrix should be pre-filled in SNESPicardComputeFunction */ 2257bbc1464cSBarry Smith /* must assembly if matrix-free to get the last SNES solution */ 22589566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY)); 22599566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY)); 22608b0a5094SBarry Smith PetscFunctionReturn(0); 22618b0a5094SBarry Smith } 22628b0a5094SBarry Smith 22638b0a5094SBarry Smith /*@C 2264bbc1464cSBarry Smith SNESSetPicard - Use SNES to solve the system A(x) x = bp(x) + b via a Picard type iteration (Picard linearization) 22658b0a5094SBarry Smith 22668b0a5094SBarry Smith Logically Collective on SNES 22678b0a5094SBarry Smith 22688b0a5094SBarry Smith Input Parameters: 22698b0a5094SBarry Smith + snes - the SNES context 22706b7fb656SBarry Smith . r - vector to store function values, may be NULL 22716b7fb656SBarry Smith . bp - function evaluation routine, may be NULL 22726b7fb656SBarry Smith . Amat - matrix with which A(x) x - bp(x) - b is to be computed 2273e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is computed (usually the same as Amat) 22746b7fb656SBarry Smith . J - function to compute matrix values, see SNESJacobianFunction() for details on its calling sequence 22756b7fb656SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL) 22768b0a5094SBarry Smith 22778b0a5094SBarry Smith Notes: 22786b7fb656SBarry Smith It is often better to provide the nonlinear function F() and some approximation to its Jacobian directly and use 2279f450aa47SBarry Smith an approximate Newton solver. This interface is provided to allow porting/testing a previous Picard based code in PETSc before converting it to approximate Newton. 2280f450aa47SBarry Smith 22818b0a5094SBarry Smith One can call SNESSetPicard() or SNESSetFunction() (and possibly SNESSetJacobian()) but cannot call both 22828b0a5094SBarry Smith 22836b7fb656SBarry Smith $ Solves the equation A(x) x = bp(x) - b via the defect correction algorithm A(x^{n}) (x^{n+1} - x^{n}) = bp(x^{n}) + b - A(x^{n})x^{n} 22846b7fb656SBarry Smith $ Note that when an exact solver is used this corresponds to the "classic" Picard A(x^{n}) x^{n+1} = bp(x^{n}) + b iteration. 22858b0a5094SBarry Smith 22868b0a5094SBarry Smith Run with -snes_mf_operator to solve the system with Newton's method using A(x^{n}) to construct the preconditioner. 22878b0a5094SBarry Smith 22880d04baf8SBarry Smith We implement the defect correction form of the Picard iteration because it converges much more generally when inexact linear solvers are used then 22896b7fb656SBarry Smith the direct Picard iteration A(x^n) x^{n+1} = bp(x^n) + b 22908b0a5094SBarry Smith 22918b0a5094SBarry 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 22928b0a5094SBarry 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 22938b0a5094SBarry Smith different please contact us at petsc-dev@mcs.anl.gov and we'll have an entirely new argument :-). 22948b0a5094SBarry Smith 22956b7fb656SBarry Smith When used with -snes_mf_operator this will run matrix-free Newton's method where the matrix-vector product is of the true Jacobian of A(x)x - bp(x) -b. 22966b7fb656SBarry Smith 22976b7fb656SBarry Smith When used with -snes_fd this will compute the true Jacobian (very slowly one column at at time) and thus represent Newton's method. 22986b7fb656SBarry Smith 22996b7fb656SBarry Smith When used with -snes_fd_coloring this will compute the Jacobian via coloring and thus represent a faster implementation of Newton's method. But the 23006b7fb656SBarry Smith the nonzero structure of the Jacobian is, in general larger than that of the Picard matrix A so you must provide in A the needed nonzero structure for the correct 23016b7fb656SBarry Smith coloring. When using DMDA this may mean creating the matrix A with DMCreateMatrix() using a wider stencil than strictly needed for A or with a DMDA_STENCIL_BOX. 23026b7fb656SBarry Smith See the commment in src/snes/tutorials/ex15.c. 2303bbc1464cSBarry Smith 2304f450aa47SBarry Smith Level: intermediate 23058b0a5094SBarry Smith 2306db781477SPatrick Sanan .seealso: `SNESGetFunction()`, `SNESSetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESGetPicard()`, `SNESLineSearchPreCheckPicard()`, `SNESJacobianFunction` 23078b0a5094SBarry Smith @*/ 2308bbc1464cSBarry Smith PetscErrorCode SNESSetPicard(SNES snes,Vec r,PetscErrorCode (*bp)(SNES,Vec,Vec,void*),Mat Amat, Mat Pmat, PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx) 23098b0a5094SBarry Smith { 2310e03ab78fSPeter Brune DM dm; 2311e03ab78fSPeter Brune 23128b0a5094SBarry Smith PetscFunctionBegin; 23138b0a5094SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23149566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 23159566063dSJacob Faibussowitsch PetscCall(DMSNESSetPicard(dm,bp,J,ctx)); 23169566063dSJacob Faibussowitsch PetscCall(DMSNESSetMFFunction(dm,SNESPicardComputeMFFunction,ctx)); 23179566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes,r,SNESPicardComputeFunction,ctx)); 23189566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes,Amat,Pmat,SNESPicardComputeJacobian,ctx)); 23198b0a5094SBarry Smith PetscFunctionReturn(0); 23208b0a5094SBarry Smith } 23218b0a5094SBarry Smith 23227971a8bfSPeter Brune /*@C 23237971a8bfSPeter Brune SNESGetPicard - Returns the context for the Picard iteration 23247971a8bfSPeter Brune 23257971a8bfSPeter Brune Not Collective, but Vec is parallel if SNES is parallel. Collective if Vec is requested, but has not been created yet. 23267971a8bfSPeter Brune 23277971a8bfSPeter Brune Input Parameter: 23287971a8bfSPeter Brune . snes - the SNES context 23297971a8bfSPeter Brune 2330d8d19677SJose E. Roman Output Parameters: 23310298fd71SBarry Smith + r - the function (or NULL) 2332f8b49ee9SBarry Smith . f - the function (or NULL); see SNESFunction for calling sequence details 2333e4357dc4SBarry Smith . Amat - the matrix used to defined the operation A(x) x - b(x) (or NULL) 2334e4357dc4SBarry Smith . Pmat - the matrix from which the preconditioner will be constructed (or NULL) 2335f8b49ee9SBarry Smith . J - the function for matrix evaluation (or NULL); see SNESJacobianFunction for calling sequence details 23360298fd71SBarry Smith - ctx - the function context (or NULL) 23377971a8bfSPeter Brune 23387971a8bfSPeter Brune Level: advanced 23397971a8bfSPeter Brune 2340db781477SPatrick Sanan .seealso: `SNESSetPicard()`, `SNESGetFunction()`, `SNESGetJacobian()`, `SNESGetDM()`, `SNESFunction`, `SNESJacobianFunction` 23417971a8bfSPeter Brune @*/ 2342d1e9a80fSBarry Smith PetscErrorCode SNESGetPicard(SNES snes,Vec *r,PetscErrorCode (**f)(SNES,Vec,Vec,void*),Mat *Amat, Mat *Pmat, PetscErrorCode (**J)(SNES,Vec,Mat,Mat,void*),void **ctx) 23437971a8bfSPeter Brune { 23447971a8bfSPeter Brune DM dm; 23457971a8bfSPeter Brune 23467971a8bfSPeter Brune PetscFunctionBegin; 23477971a8bfSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23489566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,r,NULL,NULL)); 23499566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,Amat,Pmat,NULL,NULL)); 23509566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 23519566063dSJacob Faibussowitsch PetscCall(DMSNESGetPicard(dm,f,J,ctx)); 23527971a8bfSPeter Brune PetscFunctionReturn(0); 23537971a8bfSPeter Brune } 23547971a8bfSPeter Brune 2355d25893d9SBarry Smith /*@C 2356d25893d9SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem 2357d25893d9SBarry Smith 2358d25893d9SBarry Smith Logically Collective on SNES 2359d25893d9SBarry Smith 2360d25893d9SBarry Smith Input Parameters: 2361d25893d9SBarry Smith + snes - the SNES context 2362d25893d9SBarry Smith . func - function evaluation routine 2363d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 23640298fd71SBarry Smith function evaluation routine (may be NULL) 2365d25893d9SBarry Smith 2366d25893d9SBarry Smith Calling sequence of func: 2367d25893d9SBarry Smith $ func (SNES snes,Vec x,void *ctx); 2368d25893d9SBarry Smith 2369d25893d9SBarry Smith . f - function vector 2370d25893d9SBarry Smith - ctx - optional user-defined function context 2371d25893d9SBarry Smith 2372d25893d9SBarry Smith Level: intermediate 2373d25893d9SBarry Smith 2374db781477SPatrick Sanan .seealso: `SNESGetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()` 2375d25893d9SBarry Smith @*/ 2376d25893d9SBarry Smith PetscErrorCode SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx) 2377d25893d9SBarry Smith { 2378d25893d9SBarry Smith PetscFunctionBegin; 2379d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2380d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 2381d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 2382d25893d9SBarry Smith PetscFunctionReturn(0); 2383d25893d9SBarry Smith } 2384d25893d9SBarry Smith 23853ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 23861096aae1SMatthew Knepley /*@C 23871096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 23881096aae1SMatthew Knepley it assumes a zero right hand side. 23891096aae1SMatthew Knepley 23903f9fe445SBarry Smith Logically Collective on SNES 23911096aae1SMatthew Knepley 23921096aae1SMatthew Knepley Input Parameter: 23931096aae1SMatthew Knepley . snes - the SNES context 23941096aae1SMatthew Knepley 23951096aae1SMatthew Knepley Output Parameter: 23960298fd71SBarry Smith . rhs - the right hand side vector or NULL if the right hand side vector is null 23971096aae1SMatthew Knepley 23981096aae1SMatthew Knepley Level: intermediate 23991096aae1SMatthew Knepley 2400db781477SPatrick Sanan .seealso: `SNESGetSolution()`, `SNESGetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESSetFunction()` 24011096aae1SMatthew Knepley @*/ 24027087cfbeSBarry Smith PetscErrorCode SNESGetRhs(SNES snes,Vec *rhs) 24031096aae1SMatthew Knepley { 24041096aae1SMatthew Knepley PetscFunctionBegin; 24050700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 24061096aae1SMatthew Knepley PetscValidPointer(rhs,2); 240785385478SLisandro Dalcin *rhs = snes->vec_rhs; 24081096aae1SMatthew Knepley PetscFunctionReturn(0); 24091096aae1SMatthew Knepley } 24101096aae1SMatthew Knepley 24119b94acceSBarry Smith /*@ 2412bf388a1fSBarry Smith SNESComputeFunction - Calls the function that has been set with SNESSetFunction(). 24139b94acceSBarry Smith 2414c7afd0dbSLois Curfman McInnes Collective on SNES 2415c7afd0dbSLois Curfman McInnes 24169b94acceSBarry Smith Input Parameters: 2417c7afd0dbSLois Curfman McInnes + snes - the SNES context 2418c7afd0dbSLois Curfman McInnes - x - input vector 24199b94acceSBarry Smith 24209b94acceSBarry Smith Output Parameter: 24213638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 24229b94acceSBarry Smith 24231bffabb2SLois Curfman McInnes Notes: 242436851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 2425bbc1464cSBarry Smith implementations, so users would not generally call this routine themselves. 242636851e7fSLois Curfman McInnes 242736851e7fSLois Curfman McInnes Level: developer 242836851e7fSLois Curfman McInnes 2429db781477SPatrick Sanan .seealso: `SNESSetFunction()`, `SNESGetFunction()`, `SNESComputeMFFunction()` 24309b94acceSBarry Smith @*/ 24317087cfbeSBarry Smith PetscErrorCode SNESComputeFunction(SNES snes,Vec x,Vec y) 24329b94acceSBarry Smith { 24336cab3a1bSJed Brown DM dm; 2434942e3340SBarry Smith DMSNES sdm; 24359b94acceSBarry Smith 24363a40ed3dSBarry Smith PetscFunctionBegin; 24370700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 24380700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 24390700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2440c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 2441c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 24429566063dSJacob Faibussowitsch PetscCall(VecValidValues(x,2,PETSC_TRUE)); 2443184914b5SBarry Smith 24449566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 24459566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 244632f3f7c2SPeter Brune if (sdm->ops->computefunction) { 244794db00ebSBarry Smith if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) { 24489566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0)); 244994db00ebSBarry Smith } 24509566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(x)); 2451d64ed03dSBarry Smith PetscStackPush("SNES user function"); 24528ddeebeaSSteve Benbow /* ensure domainerror is false prior to computefunction evaluation (may not have been reset) */ 24538ddeebeaSSteve Benbow snes->domainerror = PETSC_FALSE; 24549566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computefunction)(snes,x,y,sdm->functionctx)); 2455d64ed03dSBarry Smith PetscStackPop; 24569566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(x)); 245794db00ebSBarry Smith if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) { 24589566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0)); 245994db00ebSBarry Smith } 2460c90fad12SPeter Brune } else if (snes->vec_rhs) { 24619566063dSJacob Faibussowitsch PetscCall(MatMult(snes->jacobian, x, y)); 2462644e2e5bSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve()."); 2463*1baa6e33SBarry Smith if (snes->vec_rhs) PetscCall(VecAXPY(y,-1.0,snes->vec_rhs)); 2464ae3c334cSLois Curfman McInnes snes->nfuncs++; 2465422a814eSBarry Smith /* 2466422a814eSBarry Smith domainerror might not be set on all processes; so we tag vector locally with Inf and the next inner product or norm will 2467422a814eSBarry Smith propagate the value to all processes 2468422a814eSBarry Smith */ 2469*1baa6e33SBarry Smith if (snes->domainerror) PetscCall(VecSetInf(y)); 24703a40ed3dSBarry Smith PetscFunctionReturn(0); 24719b94acceSBarry Smith } 24729b94acceSBarry Smith 2473c79ef259SPeter Brune /*@ 2474bbc1464cSBarry Smith SNESComputeMFFunction - Calls the function that has been set with SNESSetMFFunction(). 2475bbc1464cSBarry Smith 2476bbc1464cSBarry Smith Collective on SNES 2477bbc1464cSBarry Smith 2478bbc1464cSBarry Smith Input Parameters: 2479bbc1464cSBarry Smith + snes - the SNES context 2480bbc1464cSBarry Smith - x - input vector 2481bbc1464cSBarry Smith 2482bbc1464cSBarry Smith Output Parameter: 2483bbc1464cSBarry Smith . y - function vector, as set by SNESSetMFFunction() 2484bbc1464cSBarry Smith 2485bbc1464cSBarry Smith Notes: 2486bbc1464cSBarry Smith SNESComputeMFFunction() is used within the matrix vector products called by the matrix created with MatCreateSNESMF() 2487bbc1464cSBarry Smith so users would not generally call this routine themselves. 2488bbc1464cSBarry Smith 2489bbc1464cSBarry Smith Since this function is intended for use with finite differencing it does not subtract the right hand side vector provided with SNESSolve() 2490bbc1464cSBarry Smith while SNESComputeFunction() does. As such, this routine cannot be used with MatMFFDSetBase() with a provided F function value even if it applies the 2491bbc1464cSBarry Smith same function as SNESComputeFunction() if a SNESSolve() right hand side vector is use because the two functions difference would include this right hand side function. 2492bbc1464cSBarry Smith 2493bbc1464cSBarry Smith Level: developer 2494bbc1464cSBarry Smith 2495db781477SPatrick Sanan .seealso: `SNESSetFunction()`, `SNESGetFunction()`, `SNESComputeFunction()`, `MatCreateSNESMF` 2496bbc1464cSBarry Smith @*/ 2497bbc1464cSBarry Smith PetscErrorCode SNESComputeMFFunction(SNES snes,Vec x,Vec y) 2498bbc1464cSBarry Smith { 2499bbc1464cSBarry Smith DM dm; 2500bbc1464cSBarry Smith DMSNES sdm; 2501bbc1464cSBarry Smith 2502bbc1464cSBarry Smith PetscFunctionBegin; 2503bbc1464cSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2504bbc1464cSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2505bbc1464cSBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2506bbc1464cSBarry Smith PetscCheckSameComm(snes,1,x,2); 2507bbc1464cSBarry Smith PetscCheckSameComm(snes,1,y,3); 25089566063dSJacob Faibussowitsch PetscCall(VecValidValues(x,2,PETSC_TRUE)); 2509bbc1464cSBarry Smith 25109566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 25119566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 25129566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0)); 25139566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(x)); 2514bbc1464cSBarry Smith PetscStackPush("SNES user function"); 2515bbc1464cSBarry Smith /* ensure domainerror is false prior to computefunction evaluation (may not have been reset) */ 2516bbc1464cSBarry Smith snes->domainerror = PETSC_FALSE; 25179566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computemffunction)(snes,x,y,sdm->mffunctionctx)); 2518bbc1464cSBarry Smith PetscStackPop; 25199566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(x)); 25209566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0)); 2521bbc1464cSBarry Smith snes->nfuncs++; 2522bbc1464cSBarry Smith /* 2523bbc1464cSBarry Smith domainerror might not be set on all processes; so we tag vector locally with Inf and the next inner product or norm will 2524bbc1464cSBarry Smith propagate the value to all processes 2525bbc1464cSBarry Smith */ 2526*1baa6e33SBarry Smith if (snes->domainerror) PetscCall(VecSetInf(y)); 2527bbc1464cSBarry Smith PetscFunctionReturn(0); 2528bbc1464cSBarry Smith } 2529bbc1464cSBarry Smith 2530bbc1464cSBarry Smith /*@ 2531be95d8f1SBarry Smith SNESComputeNGS - Calls the Gauss-Seidel function that has been set with SNESSetNGS(). 2532c79ef259SPeter Brune 2533c79ef259SPeter Brune Collective on SNES 2534c79ef259SPeter Brune 2535c79ef259SPeter Brune Input Parameters: 2536c79ef259SPeter Brune + snes - the SNES context 2537c79ef259SPeter Brune . x - input vector 2538c79ef259SPeter Brune - b - rhs vector 2539c79ef259SPeter Brune 2540c79ef259SPeter Brune Output Parameter: 2541c79ef259SPeter Brune . x - new solution vector 2542c79ef259SPeter Brune 2543c79ef259SPeter Brune Notes: 2544be95d8f1SBarry Smith SNESComputeNGS() is typically used within composed nonlinear solver 2545c79ef259SPeter Brune implementations, so most users would not generally call this routine 2546c79ef259SPeter Brune themselves. 2547c79ef259SPeter Brune 2548c79ef259SPeter Brune Level: developer 2549c79ef259SPeter Brune 2550db781477SPatrick Sanan .seealso: `SNESSetNGS()`, `SNESComputeFunction()` 2551c79ef259SPeter Brune @*/ 2552be95d8f1SBarry Smith PetscErrorCode SNESComputeNGS(SNES snes,Vec b,Vec x) 2553646217ecSPeter Brune { 25546cab3a1bSJed Brown DM dm; 2555942e3340SBarry Smith DMSNES sdm; 2556646217ecSPeter Brune 2557646217ecSPeter Brune PetscFunctionBegin; 2558646217ecSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2559064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2560064a246eSJacob Faibussowitsch if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 2561064a246eSJacob Faibussowitsch PetscCheckSameComm(snes,1,x,3); 2562064a246eSJacob Faibussowitsch if (b) PetscCheckSameComm(snes,1,b,2); 25639566063dSJacob Faibussowitsch if (b) PetscCall(VecValidValues(b,2,PETSC_TRUE)); 25649566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_NGSEval,snes,x,b,0)); 25659566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 25669566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 256722c6f798SBarry Smith if (sdm->ops->computegs) { 25689566063dSJacob Faibussowitsch if (b) PetscCall(VecLockReadPush(b)); 2569be95d8f1SBarry Smith PetscStackPush("SNES user NGS"); 25709566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computegs)(snes,x,b,sdm->gsctx)); 2571646217ecSPeter Brune PetscStackPop; 25729566063dSJacob Faibussowitsch if (b) PetscCall(VecLockReadPop(b)); 2573be95d8f1SBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetNGS() before SNESComputeNGS(), likely called from SNESSolve()."); 25749566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_NGSEval,snes,x,b,0)); 2575646217ecSPeter Brune PetscFunctionReturn(0); 2576646217ecSPeter Brune } 2577646217ecSPeter Brune 2578e885f1abSBarry Smith PetscErrorCode SNESTestJacobian(SNES snes) 2579e885f1abSBarry Smith { 258012837594SBarry Smith Mat A,B,C,D,jacobian; 2581e885f1abSBarry Smith Vec x = snes->vec_sol,f = snes->vec_func; 2582e885f1abSBarry Smith PetscReal nrm,gnorm; 258381e7118cSBarry Smith PetscReal threshold = 1.e-5; 25840e276705SLisandro Dalcin MatType mattype; 2585e885f1abSBarry Smith PetscInt m,n,M,N; 2586e885f1abSBarry Smith void *functx; 25872cd624f9SStefano Zampini PetscBool complete_print = PETSC_FALSE,threshold_print = PETSC_FALSE,test = PETSC_FALSE,flg,istranspose; 25883325ff46SBarry Smith PetscViewer viewer,mviewer; 2589e885f1abSBarry Smith MPI_Comm comm; 2590e885f1abSBarry Smith PetscInt tabs; 259112837594SBarry Smith static PetscBool directionsprinted = PETSC_FALSE; 25923325ff46SBarry Smith PetscViewerFormat format; 2593e885f1abSBarry Smith 2594e885f1abSBarry Smith PetscFunctionBegin; 2595d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)snes); 25969566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-snes_test_jacobian","Compare hand-coded and finite difference Jacobians","None",&test)); 25979566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_test_jacobian", "Threshold for element difference between hand-coded and finite difference being meaningful", "None", threshold, &threshold,NULL)); 25989566063dSJacob Faibussowitsch PetscCall(PetscOptionsViewer("-snes_test_jacobian_view","View difference between hand-coded and finite difference Jacobians element entries","None",&mviewer,&format,&complete_print)); 259918d89885SKarl Rupp if (!complete_print) { 26009566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-snes_test_jacobian_display","-snes_test_jacobian_view","3.13",NULL)); 26019566063dSJacob Faibussowitsch PetscCall(PetscOptionsViewer("-snes_test_jacobian_display","Display difference between hand-coded and finite difference Jacobians","None",&mviewer,&format,&complete_print)); 260218d89885SKarl Rupp } 260318d89885SKarl Rupp /* for compatibility with PETSc 3.9 and older. */ 26049566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-snes_test_jacobian_display_threshold","-snes_test_jacobian","3.13","-snes_test_jacobian accepts an optional threshold (since v3.10)")); 26059566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_test_jacobian_display_threshold", "Display difference between hand-coded and finite difference Jacobians which exceed input threshold", "None", threshold, &threshold, &threshold_print)); 2606d0609cedSBarry Smith PetscOptionsEnd(); 2607e885f1abSBarry Smith if (!test) PetscFunctionReturn(0); 2608e885f1abSBarry Smith 26099566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)snes,&comm)); 26109566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(comm,&viewer)); 26119566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 26129566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)snes)->tablevel)); 26139566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," ---------- Testing Jacobian -------------\n")); 261412837594SBarry Smith if (!complete_print && !directionsprinted) { 26159566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Run with -snes_test_jacobian_view and optionally -snes_test_jacobian <threshold> to show difference\n")); 26169566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," of hand-coded and finite difference Jacobian entries greater than <threshold>.\n")); 261712837594SBarry Smith } 261812837594SBarry Smith if (!directionsprinted) { 26199566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Testing hand-coded Jacobian, if (for double precision runs) ||J - Jfd||_F/||J||_F is\n")); 26209566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," O(1.e-8), the hand-coded Jacobian is probably correct.\n")); 262112837594SBarry Smith directionsprinted = PETSC_TRUE; 2622e885f1abSBarry Smith } 2623*1baa6e33SBarry Smith if (complete_print) PetscCall(PetscViewerPushFormat(mviewer,format)); 2624e885f1abSBarry Smith 26259566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)snes->jacobian,MATMFFD,&flg)); 262612837594SBarry Smith if (!flg) jacobian = snes->jacobian; 262712837594SBarry Smith else jacobian = snes->jacobian_pre; 262812837594SBarry Smith 2629a82339d0SMatthew G. Knepley if (!x) { 26309566063dSJacob Faibussowitsch PetscCall(MatCreateVecs(jacobian, &x, NULL)); 2631a82339d0SMatthew G. Knepley } else { 26329566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) x)); 2633a82339d0SMatthew G. Knepley } 2634a82339d0SMatthew G. Knepley if (!f) { 26359566063dSJacob Faibussowitsch PetscCall(VecDuplicate(x, &f)); 2636a82339d0SMatthew G. Knepley } else { 26379566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) f)); 2638a82339d0SMatthew G. Knepley } 2639a82339d0SMatthew G. Knepley /* evaluate the function at this point because SNESComputeJacobianDefault() assumes that the function has been evaluated and put into snes->vec_func */ 26409566063dSJacob Faibussowitsch PetscCall(SNESComputeFunction(snes,x,f)); 26419566063dSJacob Faibussowitsch PetscCall(VecDestroy(&f)); 26429566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)snes,SNESKSPTRANSPOSEONLY,&istranspose)); 264312837594SBarry Smith while (jacobian) { 26442cd624f9SStefano Zampini Mat JT = NULL, Jsave = NULL; 26452cd624f9SStefano Zampini 26462cd624f9SStefano Zampini if (istranspose) { 26479566063dSJacob Faibussowitsch PetscCall(MatCreateTranspose(jacobian,&JT)); 26482cd624f9SStefano Zampini Jsave = jacobian; 26492cd624f9SStefano Zampini jacobian = JT; 26502cd624f9SStefano Zampini } 26519566063dSJacob Faibussowitsch PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)jacobian,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPISBAIJ,"")); 265212837594SBarry Smith if (flg) { 265312837594SBarry Smith A = jacobian; 26549566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 265512837594SBarry Smith } else { 26569566063dSJacob Faibussowitsch PetscCall(MatComputeOperator(jacobian,MATAIJ,&A)); 265712837594SBarry Smith } 2658e885f1abSBarry Smith 26599566063dSJacob Faibussowitsch PetscCall(MatGetType(A,&mattype)); 26609566063dSJacob Faibussowitsch PetscCall(MatGetSize(A,&M,&N)); 26619566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(A,&m,&n)); 26629566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A),&B)); 26639566063dSJacob Faibussowitsch PetscCall(MatSetType(B,mattype)); 26649566063dSJacob Faibussowitsch PetscCall(MatSetSizes(B,m,n,M,N)); 26659566063dSJacob Faibussowitsch PetscCall(MatSetBlockSizesFromMats(B,A,A)); 26669566063dSJacob Faibussowitsch PetscCall(MatSetUp(B)); 26679566063dSJacob Faibussowitsch PetscCall(MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE)); 2668e885f1abSBarry Smith 26699566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,NULL,NULL,&functx)); 26709566063dSJacob Faibussowitsch PetscCall(SNESComputeJacobianDefault(snes,x,B,B,functx)); 267112837594SBarry Smith 26729566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B,MAT_COPY_VALUES,&D)); 26739566063dSJacob Faibussowitsch PetscCall(MatAYPX(D,-1.0,A,DIFFERENT_NONZERO_PATTERN)); 26749566063dSJacob Faibussowitsch PetscCall(MatNorm(D,NORM_FROBENIUS,&nrm)); 26759566063dSJacob Faibussowitsch PetscCall(MatNorm(A,NORM_FROBENIUS,&gnorm)); 26769566063dSJacob Faibussowitsch PetscCall(MatDestroy(&D)); 267712837594SBarry Smith if (!gnorm) gnorm = 1; /* just in case */ 26789566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," ||J - Jfd||_F/||J||_F = %g, ||J - Jfd||_F = %g\n",(double)(nrm/gnorm),(double)nrm)); 267912837594SBarry Smith 2680e885f1abSBarry Smith if (complete_print) { 26819566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Hand-coded Jacobian ----------\n")); 26829566063dSJacob Faibussowitsch PetscCall(MatView(A,mviewer)); 26839566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Finite difference Jacobian ----------\n")); 26849566063dSJacob Faibussowitsch PetscCall(MatView(B,mviewer)); 2685e885f1abSBarry Smith } 2686e885f1abSBarry Smith 2687df10fb39SFande Kong if (threshold_print || complete_print) { 2688e885f1abSBarry Smith PetscInt Istart, Iend, *ccols, bncols, cncols, j, row; 2689e885f1abSBarry Smith PetscScalar *cvals; 2690e885f1abSBarry Smith const PetscInt *bcols; 2691e885f1abSBarry Smith const PetscScalar *bvals; 2692e885f1abSBarry Smith 26939566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A),&C)); 26949566063dSJacob Faibussowitsch PetscCall(MatSetType(C,mattype)); 26959566063dSJacob Faibussowitsch PetscCall(MatSetSizes(C,m,n,M,N)); 26969566063dSJacob Faibussowitsch PetscCall(MatSetBlockSizesFromMats(C,A,A)); 26979566063dSJacob Faibussowitsch PetscCall(MatSetUp(C)); 26989566063dSJacob Faibussowitsch PetscCall(MatSetOption(C,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE)); 26990e276705SLisandro Dalcin 27009566063dSJacob Faibussowitsch PetscCall(MatAYPX(B,-1.0,A,DIFFERENT_NONZERO_PATTERN)); 27019566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(B,&Istart,&Iend)); 2702e885f1abSBarry Smith 2703e885f1abSBarry Smith for (row = Istart; row < Iend; row++) { 27049566063dSJacob Faibussowitsch PetscCall(MatGetRow(B,row,&bncols,&bcols,&bvals)); 27059566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(bncols,&ccols,bncols,&cvals)); 2706e885f1abSBarry Smith for (j = 0, cncols = 0; j < bncols; j++) { 270723a52b1dSBarry Smith if (PetscAbsScalar(bvals[j]) > threshold) { 2708e885f1abSBarry Smith ccols[cncols] = bcols[j]; 2709e885f1abSBarry Smith cvals[cncols] = bvals[j]; 2710e885f1abSBarry Smith cncols += 1; 2711e885f1abSBarry Smith } 2712e885f1abSBarry Smith } 2713e885f1abSBarry Smith if (cncols) { 27149566063dSJacob Faibussowitsch PetscCall(MatSetValues(C,1,&row,cncols,ccols,cvals,INSERT_VALUES)); 2715e885f1abSBarry Smith } 27169566063dSJacob Faibussowitsch PetscCall(MatRestoreRow(B,row,&bncols,&bcols,&bvals)); 27179566063dSJacob Faibussowitsch PetscCall(PetscFree2(ccols,cvals)); 2718e885f1abSBarry Smith } 27199566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY)); 27209566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY)); 27219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," Hand-coded minus finite-difference Jacobian with tolerance %g ----------\n",(double)threshold)); 27229566063dSJacob Faibussowitsch PetscCall(MatView(C,complete_print ? mviewer : viewer)); 27239566063dSJacob Faibussowitsch PetscCall(MatDestroy(&C)); 2724e885f1abSBarry Smith } 27259566063dSJacob Faibussowitsch PetscCall(MatDestroy(&A)); 27269566063dSJacob Faibussowitsch PetscCall(MatDestroy(&B)); 27279566063dSJacob Faibussowitsch PetscCall(MatDestroy(&JT)); 27282cd624f9SStefano Zampini if (Jsave) jacobian = Jsave; 272912837594SBarry Smith if (jacobian != snes->jacobian_pre) { 273012837594SBarry Smith jacobian = snes->jacobian_pre; 27319566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer," ---------- Testing Jacobian for preconditioner -------------\n")); 273212837594SBarry Smith } 273312837594SBarry Smith else jacobian = NULL; 273412837594SBarry Smith } 27359566063dSJacob Faibussowitsch PetscCall(VecDestroy(&x)); 2736*1baa6e33SBarry Smith if (complete_print) PetscCall(PetscViewerPopFormat(mviewer)); 27379566063dSJacob Faibussowitsch if (mviewer) PetscCall(PetscViewerDestroy(&mviewer)); 27389566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer,tabs)); 2739e885f1abSBarry Smith PetscFunctionReturn(0); 2740e885f1abSBarry Smith } 2741e885f1abSBarry Smith 274262fef451SLois Curfman McInnes /*@ 2743bf388a1fSBarry Smith SNESComputeJacobian - Computes the Jacobian matrix that has been set with SNESSetJacobian(). 274462fef451SLois Curfman McInnes 2745d083f849SBarry Smith Collective on SNES 2746c7afd0dbSLois Curfman McInnes 274762fef451SLois Curfman McInnes Input Parameters: 2748c7afd0dbSLois Curfman McInnes + snes - the SNES context 2749c7afd0dbSLois Curfman McInnes - x - input vector 275062fef451SLois Curfman McInnes 275162fef451SLois Curfman McInnes Output Parameters: 2752c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 2753d1e9a80fSBarry Smith - B - optional preconditioning matrix 2754fee21e36SBarry Smith 2755e35cf81dSBarry Smith Options Database Keys: 275667b8a455SSatish Balay + -snes_lag_preconditioner <lag> - how often to rebuild preconditioner 275767b8a455SSatish Balay . -snes_lag_jacobian <lag> - how often to rebuild Jacobian 2758455a5933SJed Brown . -snes_test_jacobian <optional threshold> - compare the user provided Jacobian with one compute via finite differences to check for errors. If a threshold is given, display only those entries whose difference is greater than the threshold. 2759455a5933SJed Brown . -snes_test_jacobian_view - display the user provided Jacobian, the finite difference Jacobian and the difference between them to help users detect the location of errors in the user provided Jacobian 2760693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 2761693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 2762693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 27634c30e9fbSJed Brown . -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 276494d6a431SBarry Smith . -snes_compare_coloring - Compute the finite difference Jacobian using coloring and display norms of difference 2765a5b23f4aSJose E. Roman . -snes_compare_coloring_display - Compute the finite difference Jacobian using coloring and display verbose differences 2766c01495d3SJed Brown . -snes_compare_coloring_threshold - Display only those matrix entries that differ by more than a given threshold 2767c01495d3SJed Brown . -snes_compare_coloring_threshold_atol - Absolute tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 2768c01495d3SJed Brown . -snes_compare_coloring_threshold_rtol - Relative tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 2769a5b23f4aSJose E. Roman . -snes_compare_coloring_draw - Compute the finite difference Jacobian using coloring and draw differences 2770a5b23f4aSJose E. Roman - -snes_compare_coloring_draw_contour - Compute the finite difference Jacobian using coloring and show contours of matrices and differences 2771c01495d3SJed Brown 277262fef451SLois Curfman McInnes Notes: 277362fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 277462fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 277562fef451SLois Curfman McInnes 277695452b02SPatrick Sanan Developer Notes: 277795452b02SPatrick Sanan This has duplicative ways of checking the accuracy of the user provided Jacobian (see the options above). This is for historical reasons, the routine SNESTestJacobian() use to used 2778e885f1abSBarry Smith for with the SNESType of test that has been removed. 2779e885f1abSBarry Smith 278036851e7fSLois Curfman McInnes Level: developer 278136851e7fSLois Curfman McInnes 2782db781477SPatrick Sanan .seealso: `SNESSetJacobian()`, `KSPSetOperators()`, `MatStructure`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobian()` 278362fef451SLois Curfman McInnes @*/ 2784d1e9a80fSBarry Smith PetscErrorCode SNESComputeJacobian(SNES snes,Vec X,Mat A,Mat B) 27859b94acceSBarry Smith { 2786ace3abfcSBarry Smith PetscBool flag; 27876cab3a1bSJed Brown DM dm; 2788942e3340SBarry Smith DMSNES sdm; 2789e0e3a89bSBarry Smith KSP ksp; 27903a40ed3dSBarry Smith 27913a40ed3dSBarry Smith PetscFunctionBegin; 27920700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 27930700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 2794c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 27959566063dSJacob Faibussowitsch PetscCall(VecValidValues(X,2,PETSC_TRUE)); 27969566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 27979566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 27983232da50SPeter Brune 27995f80ce2aSJacob Faibussowitsch PetscCheck(sdm->ops->computejacobian,PetscObjectComm((PetscObject)snes),PETSC_ERR_USER,"Must call SNESSetJacobian(), DMSNESSetJacobian(), DMDASNESSetJacobianLocal(), etc"); 2800ebd3b9afSBarry Smith 2801ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 2802ebd3b9afSBarry Smith 2803fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 2804fe3ffe1eSBarry Smith snes->lagjacobian = -1; 2805f5af7f23SKarl Rupp 28069566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n")); 2807fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 28089566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n")); 28099566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)A,MATMFFD,&flag)); 2810ebd3b9afSBarry Smith if (flag) { 28119566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 28129566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 2813ebd3b9afSBarry Smith } 2814e35cf81dSBarry Smith PetscFunctionReturn(0); 281537ec4e1aSPeter Brune } else if (snes->lagjacobian > 1 && (snes->iter + snes->jac_iter) % snes->lagjacobian) { 281663a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is %" PetscInt_FMT " and SNES iteration is %" PetscInt_FMT "\n",snes->lagjacobian,snes->iter)); 28179566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)A,MATMFFD,&flag)); 2818ebd3b9afSBarry Smith if (flag) { 28199566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 28209566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 2821ebd3b9afSBarry Smith } 2822e35cf81dSBarry Smith PetscFunctionReturn(0); 2823e35cf81dSBarry Smith } 2824efd4aadfSBarry Smith if (snes->npc && snes->npcside == PC_LEFT) { 28259566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY)); 28269566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY)); 2827d728fb7dSPeter Brune PetscFunctionReturn(0); 2828d728fb7dSPeter Brune } 2829e35cf81dSBarry Smith 28309566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_JacobianEval,snes,X,A,B)); 28319566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(X)); 2832d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 28339566063dSJacob Faibussowitsch PetscCall((*sdm->ops->computejacobian)(snes,X,A,B,sdm->jacobianctx)); 2834d64ed03dSBarry Smith PetscStackPop; 28359566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(X)); 28369566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_JacobianEval,snes,X,A,B)); 283728d58a37SPierre Jolivet 283828d58a37SPierre Jolivet /* attach latest linearization point to the preconditioning matrix */ 28399566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)B,"__SNES_latest_X",(PetscObject)X)); 2840a8054027SBarry Smith 2841e0e3a89bSBarry Smith /* the next line ensures that snes->ksp exists */ 28429566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&ksp)); 28433b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 28449566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n")); 28459566063dSJacob Faibussowitsch PetscCall(KSPSetReusePreconditioner(snes->ksp,PETSC_FALSE)); 28463b4f5425SBarry Smith snes->lagpreconditioner = -1; 28473b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 28489566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Reusing preconditioner because lag is -1\n")); 28499566063dSJacob Faibussowitsch PetscCall(KSPSetReusePreconditioner(snes->ksp,PETSC_TRUE)); 285037ec4e1aSPeter Brune } else if (snes->lagpreconditioner > 1 && (snes->iter + snes->pre_iter) % snes->lagpreconditioner) { 285163a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Reusing preconditioner because lag is %" PetscInt_FMT " and SNES iteration is %" PetscInt_FMT "\n",snes->lagpreconditioner,snes->iter)); 28529566063dSJacob Faibussowitsch PetscCall(KSPSetReusePreconditioner(snes->ksp,PETSC_TRUE)); 2853d1e9a80fSBarry Smith } else { 28549566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes,"Rebuilding preconditioner\n")); 28559566063dSJacob Faibussowitsch PetscCall(KSPSetReusePreconditioner(snes->ksp,PETSC_FALSE)); 2856a8054027SBarry Smith } 2857a8054027SBarry Smith 28589566063dSJacob Faibussowitsch PetscCall(SNESTestJacobian(snes)); 28596d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 286094ab13aaSBarry Smith /* PetscValidHeaderSpecific(A,MAT_CLASSID,3); 286194ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); */ 2862693365a8SJed Brown { 2863693365a8SJed Brown PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE; 28649566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_explicit",NULL,NULL,&flag)); 28659566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",NULL,NULL,&flag_draw)); 28669566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",NULL,NULL,&flag_contour)); 28679566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_operator",NULL,NULL,&flag_operator)); 2868693365a8SJed Brown if (flag || flag_draw || flag_contour) { 28690298fd71SBarry Smith Mat Bexp_mine = NULL,Bexp,FDexp; 2870693365a8SJed Brown PetscViewer vdraw,vstdout; 28716b3a5b13SJed Brown PetscBool flg; 2872693365a8SJed Brown if (flag_operator) { 28739566063dSJacob Faibussowitsch PetscCall(MatComputeOperator(A,MATAIJ,&Bexp_mine)); 2874693365a8SJed Brown Bexp = Bexp_mine; 2875693365a8SJed Brown } else { 2876693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 28779566063dSJacob Faibussowitsch PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"")); 287894ab13aaSBarry Smith if (flg) Bexp = B; 2879693365a8SJed Brown else { 2880693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 28819566063dSJacob Faibussowitsch PetscCall(MatComputeOperator(B,MATAIJ,&Bexp_mine)); 2882693365a8SJed Brown Bexp = Bexp_mine; 2883693365a8SJed Brown } 2884693365a8SJed Brown } 28859566063dSJacob Faibussowitsch PetscCall(MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp)); 28869566063dSJacob Faibussowitsch PetscCall(SNESComputeJacobianDefault(snes,X,FDexp,FDexp,NULL)); 28879566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&vstdout)); 2888693365a8SJed Brown if (flag_draw || flag_contour) { 28899566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes),NULL,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw)); 28909566063dSJacob Faibussowitsch if (flag_contour) PetscCall(PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR)); 28910298fd71SBarry Smith } else vdraw = NULL; 28929566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator ? "Jacobian" : "preconditioning Jacobian")); 28939566063dSJacob Faibussowitsch if (flag) PetscCall(MatView(Bexp,vstdout)); 28949566063dSJacob Faibussowitsch if (vdraw) PetscCall(MatView(Bexp,vdraw)); 28959566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n")); 28969566063dSJacob Faibussowitsch if (flag) PetscCall(MatView(FDexp,vstdout)); 28979566063dSJacob Faibussowitsch if (vdraw) PetscCall(MatView(FDexp,vdraw)); 28989566063dSJacob Faibussowitsch PetscCall(MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN)); 28999566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n")); 29009566063dSJacob Faibussowitsch if (flag) PetscCall(MatView(FDexp,vstdout)); 2901693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 29029566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR)); 29039566063dSJacob Faibussowitsch PetscCall(MatView(FDexp,vdraw)); 29049566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(vdraw)); 2905693365a8SJed Brown } 29069566063dSJacob Faibussowitsch if (flag_contour) PetscCall(PetscViewerPopFormat(vdraw)); 29079566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&vdraw)); 29089566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Bexp_mine)); 29099566063dSJacob Faibussowitsch PetscCall(MatDestroy(&FDexp)); 2910693365a8SJed Brown } 2911693365a8SJed Brown } 29124c30e9fbSJed Brown { 29136719d8e4SJed Brown PetscBool flag = PETSC_FALSE,flag_display = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_threshold = PETSC_FALSE; 29146719d8e4SJed Brown PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON,threshold_rtol = 10*PETSC_SQRT_MACHINE_EPSILON; 29159566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring",NULL,NULL,&flag)); 29169566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_display",NULL,NULL,&flag_display)); 29179566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_draw",NULL,NULL,&flag_draw)); 29189566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_draw_contour",NULL,NULL,&flag_contour)); 29199566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold",NULL,NULL,&flag_threshold)); 292027b0f280SBarry Smith if (flag_threshold) { 29219566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_rtol",&threshold_rtol,NULL)); 29229566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_atol",&threshold_atol,NULL)); 292327b0f280SBarry Smith } 29246719d8e4SJed Brown if (flag || flag_display || flag_draw || flag_contour || flag_threshold) { 29254c30e9fbSJed Brown Mat Bfd; 29264c30e9fbSJed Brown PetscViewer vdraw,vstdout; 2927335efc43SPeter Brune MatColoring coloring; 29284c30e9fbSJed Brown ISColoring iscoloring; 29294c30e9fbSJed Brown MatFDColoring matfdcoloring; 29304c30e9fbSJed Brown PetscErrorCode (*func)(SNES,Vec,Vec,void*); 29314c30e9fbSJed Brown void *funcctx; 29326719d8e4SJed Brown PetscReal norm1,norm2,normmax; 29334c30e9fbSJed Brown 29349566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&Bfd)); 29359566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(Bfd,&coloring)); 29369566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(coloring,MATCOLORINGSL)); 29379566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(coloring)); 29389566063dSJacob Faibussowitsch PetscCall(MatColoringApply(coloring,&iscoloring)); 29399566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&coloring)); 29409566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(Bfd,iscoloring,&matfdcoloring)); 29419566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(matfdcoloring)); 29429566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(Bfd,iscoloring,matfdcoloring)); 29439566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 29444c30e9fbSJed Brown 29454c30e9fbSJed Brown /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */ 29469566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,NULL,&func,&funcctx)); 29479566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode (*)(void))func,funcctx)); 29489566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring,((PetscObject)snes)->prefix)); 29499566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring,"coloring_")); 29509566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(matfdcoloring)); 29519566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(Bfd,matfdcoloring,X,snes)); 29529566063dSJacob Faibussowitsch PetscCall(MatFDColoringDestroy(&matfdcoloring)); 29534c30e9fbSJed Brown 29549566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&vstdout)); 29554c30e9fbSJed Brown if (flag_draw || flag_contour) { 29569566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes),NULL,"Colored Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw)); 29579566063dSJacob Faibussowitsch if (flag_contour) PetscCall(PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR)); 29580298fd71SBarry Smith } else vdraw = NULL; 29599566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout,"Explicit preconditioning Jacobian\n")); 29609566063dSJacob Faibussowitsch if (flag_display) PetscCall(MatView(B,vstdout)); 29619566063dSJacob Faibussowitsch if (vdraw) PetscCall(MatView(B,vdraw)); 29629566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout,"Colored Finite difference Jacobian\n")); 29639566063dSJacob Faibussowitsch if (flag_display) PetscCall(MatView(Bfd,vstdout)); 29649566063dSJacob Faibussowitsch if (vdraw) PetscCall(MatView(Bfd,vdraw)); 29659566063dSJacob Faibussowitsch PetscCall(MatAYPX(Bfd,-1.0,B,SAME_NONZERO_PATTERN)); 29669566063dSJacob Faibussowitsch PetscCall(MatNorm(Bfd,NORM_1,&norm1)); 29679566063dSJacob Faibussowitsch PetscCall(MatNorm(Bfd,NORM_FROBENIUS,&norm2)); 29689566063dSJacob Faibussowitsch PetscCall(MatNorm(Bfd,NORM_MAX,&normmax)); 29699566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian, norm1=%g normFrob=%g normmax=%g\n",(double)norm1,(double)norm2,(double)normmax)); 29709566063dSJacob Faibussowitsch if (flag_display) PetscCall(MatView(Bfd,vstdout)); 29714c30e9fbSJed Brown if (vdraw) { /* Always use contour for the difference */ 29729566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR)); 29739566063dSJacob Faibussowitsch PetscCall(MatView(Bfd,vdraw)); 29749566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(vdraw)); 29754c30e9fbSJed Brown } 29769566063dSJacob Faibussowitsch if (flag_contour) PetscCall(PetscViewerPopFormat(vdraw)); 29776719d8e4SJed Brown 29786719d8e4SJed Brown if (flag_threshold) { 29796719d8e4SJed Brown PetscInt bs,rstart,rend,i; 29809566063dSJacob Faibussowitsch PetscCall(MatGetBlockSize(B,&bs)); 29819566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(B,&rstart,&rend)); 29826719d8e4SJed Brown for (i=rstart; i<rend; i++) { 29836719d8e4SJed Brown const PetscScalar *ba,*ca; 29846719d8e4SJed Brown const PetscInt *bj,*cj; 29856719d8e4SJed Brown PetscInt bn,cn,j,maxentrycol = -1,maxdiffcol = -1,maxrdiffcol = -1; 29866719d8e4SJed Brown PetscReal maxentry = 0,maxdiff = 0,maxrdiff = 0; 29879566063dSJacob Faibussowitsch PetscCall(MatGetRow(B,i,&bn,&bj,&ba)); 29889566063dSJacob Faibussowitsch PetscCall(MatGetRow(Bfd,i,&cn,&cj,&ca)); 29895f80ce2aSJacob Faibussowitsch PetscCheck(bn == cn,((PetscObject)A)->comm,PETSC_ERR_PLIB,"Unexpected different nonzero pattern in -snes_compare_coloring_threshold"); 29906719d8e4SJed Brown for (j=0; j<bn; j++) { 29916719d8e4SJed Brown PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 29926719d8e4SJed Brown if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) { 29936719d8e4SJed Brown maxentrycol = bj[j]; 29946719d8e4SJed Brown maxentry = PetscRealPart(ba[j]); 29956719d8e4SJed Brown } 29966719d8e4SJed Brown if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) { 29976719d8e4SJed Brown maxdiffcol = bj[j]; 29986719d8e4SJed Brown maxdiff = PetscRealPart(ca[j]); 29996719d8e4SJed Brown } 30006719d8e4SJed Brown if (rdiff > maxrdiff) { 30016719d8e4SJed Brown maxrdiffcol = bj[j]; 30026719d8e4SJed Brown maxrdiff = rdiff; 30036719d8e4SJed Brown } 30046719d8e4SJed Brown } 30056719d8e4SJed Brown if (maxrdiff > 1) { 300663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout,"row %" PetscInt_FMT " (maxentry=%g at %" PetscInt_FMT ", maxdiff=%g at %" PetscInt_FMT ", maxrdiff=%g at %" PetscInt_FMT "):",i,(double)maxentry,maxentrycol,(double)maxdiff,maxdiffcol,(double)maxrdiff,maxrdiffcol)); 30076719d8e4SJed Brown for (j=0; j<bn; j++) { 30086719d8e4SJed Brown PetscReal rdiff; 30096719d8e4SJed Brown rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 30106719d8e4SJed Brown if (rdiff > 1) { 301163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout," (%" PetscInt_FMT ",%g:%g)",bj[j],(double)PetscRealPart(ba[j]),(double)PetscRealPart(ca[j]))); 30126719d8e4SJed Brown } 30136719d8e4SJed Brown } 301463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout,"\n")); 30156719d8e4SJed Brown } 30169566063dSJacob Faibussowitsch PetscCall(MatRestoreRow(B,i,&bn,&bj,&ba)); 30179566063dSJacob Faibussowitsch PetscCall(MatRestoreRow(Bfd,i,&cn,&cj,&ca)); 30186719d8e4SJed Brown } 30196719d8e4SJed Brown } 30209566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&vdraw)); 30219566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Bfd)); 30224c30e9fbSJed Brown } 30234c30e9fbSJed Brown } 30243a40ed3dSBarry Smith PetscFunctionReturn(0); 30259b94acceSBarry Smith } 30269b94acceSBarry Smith 3027bf388a1fSBarry Smith /*MC 3028411c0326SBarry Smith SNESJacobianFunction - Function used to convey the nonlinear Jacobian of the function to be solved by SNES 3029bf388a1fSBarry Smith 3030bf388a1fSBarry Smith Synopsis: 3031411c0326SBarry Smith #include "petscsnes.h" 3032411c0326SBarry Smith PetscErrorCode SNESJacobianFunction(SNES snes,Vec x,Mat Amat,Mat Pmat,void *ctx); 3033bf388a1fSBarry Smith 30341843f636SBarry Smith Collective on snes 30351843f636SBarry Smith 30361843f636SBarry Smith Input Parameters: 30371843f636SBarry Smith + x - input vector, the Jacobian is to be computed at this value 3038bf388a1fSBarry Smith - ctx - [optional] user-defined Jacobian context 3039bf388a1fSBarry Smith 30401843f636SBarry Smith Output Parameters: 30411843f636SBarry Smith + Amat - the matrix that defines the (approximate) Jacobian 30421843f636SBarry Smith - Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat. 30431843f636SBarry Smith 3044878cb397SSatish Balay Level: intermediate 3045878cb397SSatish Balay 3046db781477SPatrick Sanan .seealso: `SNESSetFunction()`, `SNESGetFunction()`, `SNESSetJacobian()`, `SNESGetJacobian()` 3047bf388a1fSBarry Smith M*/ 3048bf388a1fSBarry Smith 30499b94acceSBarry Smith /*@C 30509b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 3051044dda88SLois Curfman McInnes location to store the matrix. 30529b94acceSBarry Smith 3053d083f849SBarry Smith Logically Collective on SNES 3054c7afd0dbSLois Curfman McInnes 30559b94acceSBarry Smith Input Parameters: 3056c7afd0dbSLois Curfman McInnes + snes - the SNES context 3057e5d3d808SBarry Smith . Amat - the matrix that defines the (approximate) Jacobian 3058e5d3d808SBarry Smith . Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat. 3059411c0326SBarry Smith . J - Jacobian evaluation routine (if NULL then SNES retains any previously set value), see SNESJacobianFunction for details 3060c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 30610298fd71SBarry Smith Jacobian evaluation routine (may be NULL) (if NULL then SNES retains any previously set value) 30629b94acceSBarry Smith 30639b94acceSBarry Smith Notes: 3064e5d3d808SBarry Smith If the Amat matrix and Pmat matrix are different you must call MatAssemblyBegin/End() on 306516913363SBarry Smith each matrix. 306616913363SBarry Smith 3067895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 3068895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 3069895c21f2SBarry Smith 30708d359177SBarry Smith If using SNESComputeJacobianDefaultColor() to assemble a Jacobian, the ctx argument 3071a8a26c1eSJed Brown must be a MatFDColoring. 3072a8a26c1eSJed Brown 3073c3cc8fd1SJed Brown Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian. One common 3074c3cc8fd1SJed Brown example is to use the "Picard linearization" which only differentiates through the highest order parts of each term. 3075c3cc8fd1SJed Brown 307636851e7fSLois Curfman McInnes Level: beginner 307736851e7fSLois Curfman McInnes 3078db781477SPatrick Sanan .seealso: `KSPSetOperators()`, `SNESSetFunction()`, `MatMFFDComputeJacobian()`, `SNESComputeJacobianDefaultColor()`, `MatStructure`, `J`, 3079db781477SPatrick Sanan `SNESSetPicard()`, `SNESJacobianFunction` 30809b94acceSBarry Smith @*/ 3081d1e9a80fSBarry Smith PetscErrorCode SNESSetJacobian(SNES snes,Mat Amat,Mat Pmat,PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx) 30829b94acceSBarry Smith { 30836cab3a1bSJed Brown DM dm; 30843a7fca6bSBarry Smith 30853a40ed3dSBarry Smith PetscFunctionBegin; 30860700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3087e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 3088e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 3089e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(snes,1,Amat,2); 3090e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(snes,1,Pmat,3); 30919566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 30929566063dSJacob Faibussowitsch PetscCall(DMSNESSetJacobian(dm,J,ctx)); 3093e5d3d808SBarry Smith if (Amat) { 30949566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Amat)); 30959566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->jacobian)); 3096f5af7f23SKarl Rupp 3097e5d3d808SBarry Smith snes->jacobian = Amat; 30983a7fca6bSBarry Smith } 3099e5d3d808SBarry Smith if (Pmat) { 31009566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Pmat)); 31019566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->jacobian_pre)); 3102f5af7f23SKarl Rupp 3103e5d3d808SBarry Smith snes->jacobian_pre = Pmat; 31043a7fca6bSBarry Smith } 31053a40ed3dSBarry Smith PetscFunctionReturn(0); 31069b94acceSBarry Smith } 310762fef451SLois Curfman McInnes 3108c2aafc4cSSatish Balay /*@C 3109b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 3110b4fd4287SBarry Smith provided context for evaluating the Jacobian. 3111b4fd4287SBarry Smith 3112c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 3113c7afd0dbSLois Curfman McInnes 3114b4fd4287SBarry Smith Input Parameter: 3115b4fd4287SBarry Smith . snes - the nonlinear solver context 3116b4fd4287SBarry Smith 3117b4fd4287SBarry Smith Output Parameters: 3118e5d3d808SBarry Smith + Amat - location to stash (approximate) Jacobian matrix (or NULL) 3119e5d3d808SBarry Smith . Pmat - location to stash matrix used to compute the preconditioner (or NULL) 3120411c0326SBarry Smith . J - location to put Jacobian function (or NULL), see SNESJacobianFunction for details on its calling sequence 31210298fd71SBarry Smith - ctx - location to stash Jacobian ctx (or NULL) 3122fee21e36SBarry Smith 312336851e7fSLois Curfman McInnes Level: advanced 312436851e7fSLois Curfman McInnes 3125db781477SPatrick Sanan .seealso: `SNESSetJacobian()`, `SNESComputeJacobian()`, `SNESJacobianFunction`, `SNESGetFunction()` 3126b4fd4287SBarry Smith @*/ 3127d1e9a80fSBarry Smith PetscErrorCode SNESGetJacobian(SNES snes,Mat *Amat,Mat *Pmat,PetscErrorCode (**J)(SNES,Vec,Mat,Mat,void*),void **ctx) 3128b4fd4287SBarry Smith { 31296cab3a1bSJed Brown DM dm; 3130942e3340SBarry Smith DMSNES sdm; 31316cab3a1bSJed Brown 31323a40ed3dSBarry Smith PetscFunctionBegin; 31330700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3134e5d3d808SBarry Smith if (Amat) *Amat = snes->jacobian; 3135e5d3d808SBarry Smith if (Pmat) *Pmat = snes->jacobian_pre; 31369566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 31379566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 3138f8b49ee9SBarry Smith if (J) *J = sdm->ops->computejacobian; 31396cab3a1bSJed Brown if (ctx) *ctx = sdm->jacobianctx; 31403a40ed3dSBarry Smith PetscFunctionReturn(0); 3141b4fd4287SBarry Smith } 3142b4fd4287SBarry Smith 314358b371f3SBarry Smith static PetscErrorCode SNESSetDefaultComputeJacobian(SNES snes) 314458b371f3SBarry Smith { 314558b371f3SBarry Smith DM dm; 314658b371f3SBarry Smith DMSNES sdm; 314758b371f3SBarry Smith 314858b371f3SBarry Smith PetscFunctionBegin; 31499566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 31509566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 315158b371f3SBarry Smith if (!sdm->ops->computejacobian && snes->jacobian_pre) { 315258b371f3SBarry Smith DM dm; 315358b371f3SBarry Smith PetscBool isdense,ismf; 315458b371f3SBarry Smith 31559566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 31569566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)snes->jacobian_pre,&isdense,MATSEQDENSE,MATMPIDENSE,MATDENSE,NULL)); 31579566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)snes->jacobian_pre,&ismf,MATMFFD,MATSHELL,NULL)); 315858b371f3SBarry Smith if (isdense) { 31599566063dSJacob Faibussowitsch PetscCall(DMSNESSetJacobian(dm,SNESComputeJacobianDefault,NULL)); 316058b371f3SBarry Smith } else if (!ismf) { 31619566063dSJacob Faibussowitsch PetscCall(DMSNESSetJacobian(dm,SNESComputeJacobianDefaultColor,NULL)); 316258b371f3SBarry Smith } 316358b371f3SBarry Smith } 316458b371f3SBarry Smith PetscFunctionReturn(0); 316558b371f3SBarry Smith } 316658b371f3SBarry Smith 31679b94acceSBarry Smith /*@ 31689b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 3169272ac6f2SLois Curfman McInnes of a nonlinear solver. 31709b94acceSBarry Smith 3171fee21e36SBarry Smith Collective on SNES 3172fee21e36SBarry Smith 3173c7afd0dbSLois Curfman McInnes Input Parameters: 317470e92668SMatthew Knepley . snes - the SNES context 3175c7afd0dbSLois Curfman McInnes 3176272ac6f2SLois Curfman McInnes Notes: 3177272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 3178272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 3179272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 3180272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 3181272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 3182272ac6f2SLois Curfman McInnes 318336851e7fSLois Curfman McInnes Level: advanced 318436851e7fSLois Curfman McInnes 3185db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESSolve()`, `SNESDestroy()` 31869b94acceSBarry Smith @*/ 31877087cfbeSBarry Smith PetscErrorCode SNESSetUp(SNES snes) 31889b94acceSBarry Smith { 31896cab3a1bSJed Brown DM dm; 3190942e3340SBarry Smith DMSNES sdm; 3191c35f09e5SBarry Smith SNESLineSearch linesearch, pclinesearch; 31926e2a1849SPeter Brune void *lsprectx,*lspostctx; 31936b2b7091SBarry Smith PetscErrorCode (*precheck)(SNESLineSearch,Vec,Vec,PetscBool*,void*); 31946b2b7091SBarry Smith PetscErrorCode (*postcheck)(SNESLineSearch,Vec,Vec,Vec,PetscBool*,PetscBool*,void*); 31956e2a1849SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 31966e2a1849SPeter Brune Vec f,fpc; 31976e2a1849SPeter Brune void *funcctx; 3198d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 31991eb13d49SPeter Brune void *jacctx,*appctx; 320032b97717SPeter Brune Mat j,jpre; 32013a40ed3dSBarry Smith 32023a40ed3dSBarry Smith PetscFunctionBegin; 32030700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 32044dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 32059566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_Setup,snes,0,0,0)); 32069b94acceSBarry Smith 32077adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 32089566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes,SNESNEWTONLS)); 320985385478SLisandro Dalcin } 321085385478SLisandro Dalcin 32119566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,&snes->vec_func,NULL,NULL)); 321258c9b817SLisandro Dalcin 32139566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 32149566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm,&sdm)); 32155f80ce2aSJacob Faibussowitsch PetscCheck(sdm->ops->computefunction,PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Function never provided to SNES object"); 32169566063dSJacob Faibussowitsch PetscCall(SNESSetDefaultComputeJacobian(snes)); 321758b371f3SBarry Smith 32186cab3a1bSJed Brown if (!snes->vec_func) { 32199566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(dm,&snes->vec_func)); 3220214df951SJed Brown } 3221efd51863SBarry Smith 322222d28d08SBarry Smith if (!snes->ksp) { 32239566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, &snes->ksp)); 322422d28d08SBarry Smith } 3225b710008aSBarry Smith 3226d8d34be6SBarry Smith if (snes->linesearch) { 32279566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &snes->linesearch)); 32289566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetFunction(snes->linesearch,SNESComputeFunction)); 3229d8d34be6SBarry Smith } 32309e764e56SPeter Brune 3231b552625fSStefano Zampini if (snes->npc && snes->npcside == PC_LEFT) { 3232172a4300SPeter Brune snes->mf = PETSC_TRUE; 3233172a4300SPeter Brune snes->mf_operator = PETSC_FALSE; 3234172a4300SPeter Brune } 3235d8f46077SPeter Brune 3236efd4aadfSBarry Smith if (snes->npc) { 32376e2a1849SPeter Brune /* copy the DM over */ 32389566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 32399566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes->npc,dm)); 32406e2a1849SPeter Brune 32419566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes,&f,&func,&funcctx)); 32429566063dSJacob Faibussowitsch PetscCall(VecDuplicate(f,&fpc)); 32439566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes->npc,fpc,func,funcctx)); 32449566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes,&j,&jpre,&jac,&jacctx)); 32459566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes->npc,j,jpre,jac,jacctx)); 32469566063dSJacob Faibussowitsch PetscCall(SNESGetApplicationContext(snes,&appctx)); 32479566063dSJacob Faibussowitsch PetscCall(SNESSetApplicationContext(snes->npc,appctx)); 32489566063dSJacob Faibussowitsch PetscCall(VecDestroy(&fpc)); 32496e2a1849SPeter Brune 32506e2a1849SPeter Brune /* copy the function pointers over */ 32519566063dSJacob Faibussowitsch PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)snes,(PetscObject)snes->npc)); 32526e2a1849SPeter Brune 32536e2a1849SPeter Brune /* default to 1 iteration */ 32549566063dSJacob Faibussowitsch PetscCall(SNESSetTolerances(snes->npc,0.0,0.0,0.0,1,snes->npc->max_funcs)); 3255efd4aadfSBarry Smith if (snes->npcside == PC_RIGHT) { 32569566063dSJacob Faibussowitsch PetscCall(SNESSetNormSchedule(snes->npc,SNES_NORM_FINAL_ONLY)); 3257a9936a0cSPeter Brune } else { 32589566063dSJacob Faibussowitsch PetscCall(SNESSetNormSchedule(snes->npc,SNES_NORM_NONE)); 3259a9936a0cSPeter Brune } 32609566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(snes->npc)); 32616e2a1849SPeter Brune 32626e2a1849SPeter Brune /* copy the line search context over */ 3263d8d34be6SBarry Smith if (snes->linesearch && snes->npc->linesearch) { 32649566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes,&linesearch)); 32659566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes->npc,&pclinesearch)); 32669566063dSJacob Faibussowitsch PetscCall(SNESLineSearchGetPreCheck(linesearch,&precheck,&lsprectx)); 32679566063dSJacob Faibussowitsch PetscCall(SNESLineSearchGetPostCheck(linesearch,&postcheck,&lspostctx)); 32689566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetPreCheck(pclinesearch,precheck,lsprectx)); 32699566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetPostCheck(pclinesearch,postcheck,lspostctx)); 32709566063dSJacob Faibussowitsch PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)pclinesearch)); 32716e2a1849SPeter Brune } 3272d8d34be6SBarry Smith } 3273*1baa6e33SBarry Smith if (snes->mf) PetscCall(SNESSetUpMatrixFree_Private(snes, snes->mf_operator, snes->mf_version)); 327432b97717SPeter Brune if (snes->ops->usercompute && !snes->user) { 32759566063dSJacob Faibussowitsch PetscCall((*snes->ops->usercompute)(snes,(void**)&snes->user)); 327632b97717SPeter Brune } 32776e2a1849SPeter Brune 327837ec4e1aSPeter Brune snes->jac_iter = 0; 327937ec4e1aSPeter Brune snes->pre_iter = 0; 328037ec4e1aSPeter Brune 3281*1baa6e33SBarry Smith if (snes->ops->setup) PetscCall((*snes->ops->setup)(snes)); 328258c9b817SLisandro Dalcin 32839566063dSJacob Faibussowitsch PetscCall(SNESSetDefaultComputeJacobian(snes)); 328458b371f3SBarry Smith 3285b552625fSStefano Zampini if (snes->npc && snes->npcside == PC_LEFT) { 32866c67d002SPeter Brune if (snes->functype == SNES_FUNCTION_PRECONDITIONED) { 3287d8d34be6SBarry Smith if (snes->linesearch) { 32889566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes,&linesearch)); 32899566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetFunction(linesearch,SNESComputeFunctionDefaultNPC)); 32906c67d002SPeter Brune } 32916c67d002SPeter Brune } 3292d8d34be6SBarry Smith } 32939566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_Setup,snes,0,0,0)); 32947aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 32953a40ed3dSBarry Smith PetscFunctionReturn(0); 32969b94acceSBarry Smith } 32979b94acceSBarry Smith 329837596af1SLisandro Dalcin /*@ 329937596af1SLisandro Dalcin SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats 330037596af1SLisandro Dalcin 330137596af1SLisandro Dalcin Collective on SNES 330237596af1SLisandro Dalcin 330337596af1SLisandro Dalcin Input Parameter: 330437596af1SLisandro Dalcin . snes - iterative context obtained from SNESCreate() 330537596af1SLisandro Dalcin 3306d25893d9SBarry Smith Level: intermediate 3307d25893d9SBarry Smith 330895452b02SPatrick Sanan Notes: 330995452b02SPatrick Sanan Also calls the application context destroy routine set with SNESSetComputeApplicationContext() 331037596af1SLisandro Dalcin 3311db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESSetUp()`, `SNESSolve()` 331237596af1SLisandro Dalcin @*/ 331337596af1SLisandro Dalcin PetscErrorCode SNESReset(SNES snes) 331437596af1SLisandro Dalcin { 331537596af1SLisandro Dalcin PetscFunctionBegin; 331637596af1SLisandro Dalcin PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3317d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 33189566063dSJacob Faibussowitsch PetscCall((*snes->ops->userdestroy)((void**)&snes->user)); 33190298fd71SBarry Smith snes->user = NULL; 3320d25893d9SBarry Smith } 3321*1baa6e33SBarry Smith if (snes->npc) PetscCall(SNESReset(snes->npc)); 33228a23116dSBarry Smith 3323*1baa6e33SBarry Smith if (snes->ops->reset) PetscCall((*snes->ops->reset)(snes)); 3324*1baa6e33SBarry Smith if (snes->ksp) PetscCall(KSPReset(snes->ksp)); 33259e764e56SPeter Brune 3326*1baa6e33SBarry Smith if (snes->linesearch) PetscCall(SNESLineSearchReset(snes->linesearch)); 33279e764e56SPeter Brune 33289566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_rhs)); 33299566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_sol)); 33309566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_sol_update)); 33319566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_func)); 33329566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->jacobian)); 33339566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->jacobian_pre)); 33349566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->picard)); 33359566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(snes->nwork,&snes->work)); 33369566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(snes->nvwork,&snes->vwork)); 3337f5af7f23SKarl Rupp 333840fdac6aSLawrence Mitchell snes->alwayscomputesfinalresidual = PETSC_FALSE; 333940fdac6aSLawrence Mitchell 334037596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 334137596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 334237596af1SLisandro Dalcin PetscFunctionReturn(0); 334337596af1SLisandro Dalcin } 334437596af1SLisandro Dalcin 334552baeb72SSatish Balay /*@ 3346c4421ceaSFande Kong SNESConvergedReasonViewCancel - Clears all the reasonview functions for a SNES object. 3347c4421ceaSFande Kong 3348c4421ceaSFande Kong Collective on SNES 3349c4421ceaSFande Kong 3350c4421ceaSFande Kong Input Parameter: 3351c4421ceaSFande Kong . snes - iterative context obtained from SNESCreate() 3352c4421ceaSFande Kong 3353c4421ceaSFande Kong Level: intermediate 3354c4421ceaSFande Kong 3355db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESDestroy()`, `SNESReset()` 3356c4421ceaSFande Kong @*/ 3357c4421ceaSFande Kong PetscErrorCode SNESConvergedReasonViewCancel(SNES snes) 3358c4421ceaSFande Kong { 3359c4421ceaSFande Kong PetscInt i; 3360c4421ceaSFande Kong 3361c4421ceaSFande Kong PetscFunctionBegin; 3362c4421ceaSFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3363c4421ceaSFande Kong for (i=0; i<snes->numberreasonviews; i++) { 3364c4421ceaSFande Kong if (snes->reasonviewdestroy[i]) { 33659566063dSJacob Faibussowitsch PetscCall((*snes->reasonviewdestroy[i])(&snes->reasonviewcontext[i])); 3366c4421ceaSFande Kong } 3367c4421ceaSFande Kong } 3368c4421ceaSFande Kong snes->numberreasonviews = 0; 3369c4421ceaSFande Kong PetscFunctionReturn(0); 3370c4421ceaSFande Kong } 3371c4421ceaSFande Kong 33721fb7b255SJunchao Zhang /*@C 33739b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 33749b94acceSBarry Smith with SNESCreate(). 33759b94acceSBarry Smith 3376c7afd0dbSLois Curfman McInnes Collective on SNES 3377c7afd0dbSLois Curfman McInnes 33789b94acceSBarry Smith Input Parameter: 33799b94acceSBarry Smith . snes - the SNES context 33809b94acceSBarry Smith 338136851e7fSLois Curfman McInnes Level: beginner 338236851e7fSLois Curfman McInnes 3383db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESSolve()` 33849b94acceSBarry Smith @*/ 33856bf464f9SBarry Smith PetscErrorCode SNESDestroy(SNES *snes) 33869b94acceSBarry Smith { 33873a40ed3dSBarry Smith PetscFunctionBegin; 33886bf464f9SBarry Smith if (!*snes) PetscFunctionReturn(0); 33896bf464f9SBarry Smith PetscValidHeaderSpecific((*snes),SNES_CLASSID,1); 33909e5d0892SLisandro Dalcin if (--((PetscObject)(*snes))->refct > 0) {*snes = NULL; PetscFunctionReturn(0);} 3391d4bb536fSBarry Smith 33929566063dSJacob Faibussowitsch PetscCall(SNESReset((*snes))); 33939566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&(*snes)->npc)); 33946b8b9a38SLisandro Dalcin 3395e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 33969566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*snes)); 33979566063dSJacob Faibussowitsch if ((*snes)->ops->destroy) PetscCall((*((*snes))->ops->destroy)((*snes))); 33986d4c513bSLisandro Dalcin 33999566063dSJacob Faibussowitsch if ((*snes)->dm) PetscCall(DMCoarsenHookRemove((*snes)->dm,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,*snes)); 34009566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*snes)->dm)); 34019566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&(*snes)->ksp)); 34029566063dSJacob Faibussowitsch PetscCall(SNESLineSearchDestroy(&(*snes)->linesearch)); 34036b8b9a38SLisandro Dalcin 34049566063dSJacob Faibussowitsch PetscCall(PetscFree((*snes)->kspconvctx)); 34056bf464f9SBarry Smith if ((*snes)->ops->convergeddestroy) { 34069566063dSJacob Faibussowitsch PetscCall((*(*snes)->ops->convergeddestroy)((*snes)->cnvP)); 34076b8b9a38SLisandro Dalcin } 3408071fcb05SBarry Smith if ((*snes)->conv_hist_alloc) { 34099566063dSJacob Faibussowitsch PetscCall(PetscFree2((*snes)->conv_hist,(*snes)->conv_hist_its)); 341058c9b817SLisandro Dalcin } 34119566063dSJacob Faibussowitsch PetscCall(SNESMonitorCancel((*snes))); 34129566063dSJacob Faibussowitsch PetscCall(SNESConvergedReasonViewCancel((*snes))); 34139566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(snes)); 34143a40ed3dSBarry Smith PetscFunctionReturn(0); 34159b94acceSBarry Smith } 34169b94acceSBarry Smith 34179b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 34189b94acceSBarry Smith 3419a8054027SBarry Smith /*@ 3420a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 3421a8054027SBarry Smith 34223f9fe445SBarry Smith Logically Collective on SNES 3423a8054027SBarry Smith 3424a8054027SBarry Smith Input Parameters: 3425a8054027SBarry Smith + snes - the SNES context 3426d8e291bfSBarry Smith - lag - 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time 34273b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 3428a8054027SBarry Smith 3429a8054027SBarry Smith Options Database Keys: 34303d5a8a6aSBarry Smith + -snes_lag_jacobian_persists <true,false> - sets the persistence 34313d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 34323d5a8a6aSBarry Smith . -snes_lag_preconditioner_persists <true,false> - sets the persistence 34333d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag 3434a8054027SBarry Smith 3435a8054027SBarry Smith Notes: 3436a8054027SBarry Smith The default is 1 34373d5a8a6aSBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 or SNESSetLagPreconditionerPersists() was called 3438d8e291bfSBarry Smith 3439d8e291bfSBarry Smith SNESSetLagPreconditionerPersists() allows using the same uniform lagging (for example every second solve) across multiple solves. 3440a8054027SBarry Smith 3441a8054027SBarry Smith Level: intermediate 3442a8054027SBarry Smith 3443db781477SPatrick Sanan .seealso: `SNESSetTrustRegionTolerance()`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESSetLagPreconditionerPersists()`, 3444db781477SPatrick Sanan `SNESSetLagJacobianPersists()` 3445a8054027SBarry Smith 3446a8054027SBarry Smith @*/ 34477087cfbeSBarry Smith PetscErrorCode SNESSetLagPreconditioner(SNES snes,PetscInt lag) 3448a8054027SBarry Smith { 3449a8054027SBarry Smith PetscFunctionBegin; 34500700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 34515f80ce2aSJacob Faibussowitsch PetscCheck(lag >= -2,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 34525f80ce2aSJacob Faibussowitsch PetscCheck(lag,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 3453c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 3454a8054027SBarry Smith snes->lagpreconditioner = lag; 3455a8054027SBarry Smith PetscFunctionReturn(0); 3456a8054027SBarry Smith } 3457a8054027SBarry Smith 3458efd51863SBarry Smith /*@ 3459efd51863SBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does 3460efd51863SBarry Smith 3461efd51863SBarry Smith Logically Collective on SNES 3462efd51863SBarry Smith 3463efd51863SBarry Smith Input Parameters: 3464efd51863SBarry Smith + snes - the SNES context 3465efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 3466efd51863SBarry Smith 3467efd51863SBarry Smith Options Database Keys: 346867b8a455SSatish Balay . -snes_grid_sequence <steps> - Use grid sequencing to generate initial guess 3469efd51863SBarry Smith 3470efd51863SBarry Smith Level: intermediate 3471efd51863SBarry Smith 3472c0df2a02SJed Brown Notes: 3473c0df2a02SJed Brown Use SNESGetSolution() to extract the fine grid solution after grid sequencing. 3474c0df2a02SJed Brown 3475db781477SPatrick Sanan .seealso: `SNESSetTrustRegionTolerance()`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESGetGridSequence()` 3476efd51863SBarry Smith 3477efd51863SBarry Smith @*/ 3478efd51863SBarry Smith PetscErrorCode SNESSetGridSequence(SNES snes,PetscInt steps) 3479efd51863SBarry Smith { 3480efd51863SBarry Smith PetscFunctionBegin; 3481efd51863SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3482efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes,steps,2); 3483efd51863SBarry Smith snes->gridsequence = steps; 3484efd51863SBarry Smith PetscFunctionReturn(0); 3485efd51863SBarry Smith } 3486efd51863SBarry Smith 3487fa19ca70SBarry Smith /*@ 3488fa19ca70SBarry Smith SNESGetGridSequence - gets the number of steps of grid sequencing that SNES does 3489fa19ca70SBarry Smith 3490fa19ca70SBarry Smith Logically Collective on SNES 3491fa19ca70SBarry Smith 3492fa19ca70SBarry Smith Input Parameter: 3493fa19ca70SBarry Smith . snes - the SNES context 3494fa19ca70SBarry Smith 3495fa19ca70SBarry Smith Output Parameter: 3496fa19ca70SBarry Smith . steps - the number of refinements to do, defaults to 0 3497fa19ca70SBarry Smith 3498fa19ca70SBarry Smith Options Database Keys: 349967b8a455SSatish Balay . -snes_grid_sequence <steps> - set number of refinements 3500fa19ca70SBarry Smith 3501fa19ca70SBarry Smith Level: intermediate 3502fa19ca70SBarry Smith 3503fa19ca70SBarry Smith Notes: 3504fa19ca70SBarry Smith Use SNESGetSolution() to extract the fine grid solution after grid sequencing. 3505fa19ca70SBarry Smith 3506db781477SPatrick Sanan .seealso: `SNESSetTrustRegionTolerance()`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESSetGridSequence()` 3507fa19ca70SBarry Smith 3508fa19ca70SBarry Smith @*/ 3509fa19ca70SBarry Smith PetscErrorCode SNESGetGridSequence(SNES snes,PetscInt *steps) 3510fa19ca70SBarry Smith { 3511fa19ca70SBarry Smith PetscFunctionBegin; 3512fa19ca70SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3513fa19ca70SBarry Smith *steps = snes->gridsequence; 3514fa19ca70SBarry Smith PetscFunctionReturn(0); 3515fa19ca70SBarry Smith } 3516fa19ca70SBarry Smith 3517a8054027SBarry Smith /*@ 3518a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 3519a8054027SBarry Smith 35203f9fe445SBarry Smith Not Collective 3521a8054027SBarry Smith 3522a8054027SBarry Smith Input Parameter: 3523a8054027SBarry Smith . snes - the SNES context 3524a8054027SBarry Smith 3525a8054027SBarry Smith Output Parameter: 3526a8054027SBarry 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 35273b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 3528a8054027SBarry Smith 3529a8054027SBarry Smith Options Database Keys: 35303d5a8a6aSBarry Smith + -snes_lag_jacobian_persists <true,false> - sets the persistence 35313d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 35323d5a8a6aSBarry Smith . -snes_lag_preconditioner_persists <true,false> - sets the persistence 35333d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag 3534a8054027SBarry Smith 3535a8054027SBarry Smith Notes: 3536a8054027SBarry Smith The default is 1 3537a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 3538a8054027SBarry Smith 3539a8054027SBarry Smith Level: intermediate 3540a8054027SBarry Smith 3541db781477SPatrick Sanan .seealso: `SNESSetTrustRegionTolerance()`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobianPersists()`, `SNESSetLagPreconditionerPersists()` 3542a8054027SBarry Smith 3543a8054027SBarry Smith @*/ 35447087cfbeSBarry Smith PetscErrorCode SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 3545a8054027SBarry Smith { 3546a8054027SBarry Smith PetscFunctionBegin; 35470700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3548a8054027SBarry Smith *lag = snes->lagpreconditioner; 3549a8054027SBarry Smith PetscFunctionReturn(0); 3550a8054027SBarry Smith } 3551a8054027SBarry Smith 3552e35cf81dSBarry Smith /*@ 3553e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 3554e35cf81dSBarry Smith often the preconditioner is rebuilt. 3555e35cf81dSBarry Smith 35563f9fe445SBarry Smith Logically Collective on SNES 3557e35cf81dSBarry Smith 3558e35cf81dSBarry Smith Input Parameters: 3559e35cf81dSBarry Smith + snes - the SNES context 3560e35cf81dSBarry 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 3561fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 3562e35cf81dSBarry Smith 3563e35cf81dSBarry Smith Options Database Keys: 35643d5a8a6aSBarry Smith + -snes_lag_jacobian_persists <true,false> - sets the persistence 35653d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 35663d5a8a6aSBarry Smith . -snes_lag_preconditioner_persists <true,false> - sets the persistence 35673d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag. 3568e35cf81dSBarry Smith 3569e35cf81dSBarry Smith Notes: 3570e35cf81dSBarry Smith The default is 1 3571e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 3572fe3ffe1eSBarry 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 3573fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 3574e35cf81dSBarry Smith 3575e35cf81dSBarry Smith Level: intermediate 3576e35cf81dSBarry Smith 3577db781477SPatrick Sanan .seealso: `SNESSetTrustRegionTolerance()`, `SNESGetLagPreconditioner()`, `SNESSetLagPreconditioner()`, `SNESGetLagJacobianPersists()`, `SNESSetLagPreconditionerPersists()` 3578e35cf81dSBarry Smith 3579e35cf81dSBarry Smith @*/ 35807087cfbeSBarry Smith PetscErrorCode SNESSetLagJacobian(SNES snes,PetscInt lag) 3581e35cf81dSBarry Smith { 3582e35cf81dSBarry Smith PetscFunctionBegin; 35830700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 35845f80ce2aSJacob Faibussowitsch PetscCheck(lag >= -2,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 35855f80ce2aSJacob Faibussowitsch PetscCheck(lag,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 3586c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 3587e35cf81dSBarry Smith snes->lagjacobian = lag; 3588e35cf81dSBarry Smith PetscFunctionReturn(0); 3589e35cf81dSBarry Smith } 3590e35cf81dSBarry Smith 3591e35cf81dSBarry Smith /*@ 3592e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 3593e35cf81dSBarry Smith 35943f9fe445SBarry Smith Not Collective 3595e35cf81dSBarry Smith 3596e35cf81dSBarry Smith Input Parameter: 3597e35cf81dSBarry Smith . snes - the SNES context 3598e35cf81dSBarry Smith 3599e35cf81dSBarry Smith Output Parameter: 3600e35cf81dSBarry 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 3601e35cf81dSBarry Smith the Jacobian is built etc. 3602e35cf81dSBarry Smith 3603e35cf81dSBarry Smith Notes: 3604e35cf81dSBarry Smith The default is 1 36053d5a8a6aSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 or SNESSetLagJacobianPersists() was called. 3606e35cf81dSBarry Smith 3607e35cf81dSBarry Smith Level: intermediate 3608e35cf81dSBarry Smith 3609db781477SPatrick Sanan .seealso: `SNESSetTrustRegionTolerance()`, `SNESSetLagJacobian()`, `SNESSetLagPreconditioner()`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobianPersists()`, `SNESSetLagPreconditionerPersists()` 3610e35cf81dSBarry Smith 3611e35cf81dSBarry Smith @*/ 36127087cfbeSBarry Smith PetscErrorCode SNESGetLagJacobian(SNES snes,PetscInt *lag) 3613e35cf81dSBarry Smith { 3614e35cf81dSBarry Smith PetscFunctionBegin; 36150700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3616e35cf81dSBarry Smith *lag = snes->lagjacobian; 3617e35cf81dSBarry Smith PetscFunctionReturn(0); 3618e35cf81dSBarry Smith } 3619e35cf81dSBarry Smith 362037ec4e1aSPeter Brune /*@ 362137ec4e1aSPeter Brune SNESSetLagJacobianPersists - Set whether or not the Jacobian lagging persists through multiple solves 362237ec4e1aSPeter Brune 362337ec4e1aSPeter Brune Logically collective on SNES 362437ec4e1aSPeter Brune 3625d8d19677SJose E. Roman Input Parameters: 362637ec4e1aSPeter Brune + snes - the SNES context 36279d7e2deaSPeter Brune - flg - jacobian lagging persists if true 362837ec4e1aSPeter Brune 362937ec4e1aSPeter Brune Options Database Keys: 36303d5a8a6aSBarry Smith + -snes_lag_jacobian_persists <true,false> - sets the persistence 36313d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 36323d5a8a6aSBarry Smith . -snes_lag_preconditioner_persists <true,false> - sets the persistence 36333d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag 36343d5a8a6aSBarry Smith 363595452b02SPatrick Sanan Notes: 363695452b02SPatrick Sanan This is useful both for nonlinear preconditioning, where it's appropriate to have the Jacobian be stale by 363737ec4e1aSPeter Brune several solves, and for implicit time-stepping, where Jacobian lagging in the inner nonlinear solve over several 363837ec4e1aSPeter Brune timesteps may present huge efficiency gains. 363937ec4e1aSPeter Brune 364037ec4e1aSPeter Brune Level: developer 364137ec4e1aSPeter Brune 3642db781477SPatrick Sanan .seealso: `SNESSetLagPreconditionerPersists()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESGetNPC()`, `SNESSetLagJacobianPersists()` 364337ec4e1aSPeter Brune 364437ec4e1aSPeter Brune @*/ 364537ec4e1aSPeter Brune PetscErrorCode SNESSetLagJacobianPersists(SNES snes,PetscBool flg) 364637ec4e1aSPeter Brune { 364737ec4e1aSPeter Brune PetscFunctionBegin; 364837ec4e1aSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 364937ec4e1aSPeter Brune PetscValidLogicalCollectiveBool(snes,flg,2); 365037ec4e1aSPeter Brune snes->lagjac_persist = flg; 365137ec4e1aSPeter Brune PetscFunctionReturn(0); 365237ec4e1aSPeter Brune } 365337ec4e1aSPeter Brune 365437ec4e1aSPeter Brune /*@ 3655d8e291bfSBarry Smith SNESSetLagPreconditionerPersists - Set whether or not the preconditioner lagging persists through multiple nonlinear solves 365637ec4e1aSPeter Brune 365737ec4e1aSPeter Brune Logically Collective on SNES 365837ec4e1aSPeter Brune 3659d8d19677SJose E. Roman Input Parameters: 366037ec4e1aSPeter Brune + snes - the SNES context 36619d7e2deaSPeter Brune - flg - preconditioner lagging persists if true 366237ec4e1aSPeter Brune 366337ec4e1aSPeter Brune Options Database Keys: 36643d5a8a6aSBarry Smith + -snes_lag_jacobian_persists <true,false> - sets the persistence 36653d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 36663d5a8a6aSBarry Smith . -snes_lag_preconditioner_persists <true,false> - sets the persistence 36673d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag 366837ec4e1aSPeter Brune 366995452b02SPatrick Sanan Notes: 367095452b02SPatrick Sanan This is useful both for nonlinear preconditioning, where it's appropriate to have the preconditioner be stale 367137ec4e1aSPeter Brune by several solves, and for implicit time-stepping, where preconditioner lagging in the inner nonlinear solve over 367237ec4e1aSPeter Brune several timesteps may present huge efficiency gains. 367337ec4e1aSPeter Brune 367437ec4e1aSPeter Brune Level: developer 367537ec4e1aSPeter Brune 3676db781477SPatrick Sanan .seealso: `SNESSetLagJacobianPersists()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESGetNPC()`, `SNESSetLagPreconditioner()` 367737ec4e1aSPeter Brune 367837ec4e1aSPeter Brune @*/ 367937ec4e1aSPeter Brune PetscErrorCode SNESSetLagPreconditionerPersists(SNES snes,PetscBool flg) 368037ec4e1aSPeter Brune { 368137ec4e1aSPeter Brune PetscFunctionBegin; 368237ec4e1aSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 368337ec4e1aSPeter Brune PetscValidLogicalCollectiveBool(snes,flg,2); 368437ec4e1aSPeter Brune snes->lagpre_persist = flg; 368537ec4e1aSPeter Brune PetscFunctionReturn(0); 368637ec4e1aSPeter Brune } 368737ec4e1aSPeter Brune 36889b94acceSBarry Smith /*@ 3689be5caee7SBarry Smith SNESSetForceIteration - force SNESSolve() to take at least one iteration regardless of the initial residual norm 3690be5caee7SBarry Smith 3691be5caee7SBarry Smith Logically Collective on SNES 3692be5caee7SBarry Smith 3693be5caee7SBarry Smith Input Parameters: 3694be5caee7SBarry Smith + snes - the SNES context 3695be5caee7SBarry Smith - force - PETSC_TRUE require at least one iteration 3696be5caee7SBarry Smith 3697be5caee7SBarry Smith Options Database Keys: 3698be5caee7SBarry Smith . -snes_force_iteration <force> - Sets forcing an iteration 3699be5caee7SBarry Smith 3700be5caee7SBarry Smith Notes: 3701be5caee7SBarry Smith This is used sometimes with TS to prevent TS from detecting a false steady state solution 3702be5caee7SBarry Smith 3703be5caee7SBarry Smith Level: intermediate 3704be5caee7SBarry Smith 3705db781477SPatrick Sanan .seealso: `SNESSetTrustRegionTolerance()`, `SNESSetDivergenceTolerance()` 3706be5caee7SBarry Smith @*/ 3707be5caee7SBarry Smith PetscErrorCode SNESSetForceIteration(SNES snes,PetscBool force) 3708be5caee7SBarry Smith { 3709be5caee7SBarry Smith PetscFunctionBegin; 3710be5caee7SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3711be5caee7SBarry Smith snes->forceiteration = force; 3712be5caee7SBarry Smith PetscFunctionReturn(0); 3713be5caee7SBarry Smith } 3714be5caee7SBarry Smith 371585216dc7SFande Kong /*@ 371685216dc7SFande Kong SNESGetForceIteration - Whether or not to force SNESSolve() take at least one iteration regardless of the initial residual norm 371785216dc7SFande Kong 371885216dc7SFande Kong Logically Collective on SNES 371985216dc7SFande Kong 372085216dc7SFande Kong Input Parameters: 372185216dc7SFande Kong . snes - the SNES context 372285216dc7SFande Kong 372385216dc7SFande Kong Output Parameter: 372485216dc7SFande Kong . force - PETSC_TRUE requires at least one iteration. 372585216dc7SFande Kong 372606dd6b0eSSatish Balay Level: intermediate 372706dd6b0eSSatish Balay 3728db781477SPatrick Sanan .seealso: `SNESSetForceIteration()`, `SNESSetTrustRegionTolerance()`, `SNESSetDivergenceTolerance()` 372985216dc7SFande Kong @*/ 373085216dc7SFande Kong PetscErrorCode SNESGetForceIteration(SNES snes,PetscBool *force) 373185216dc7SFande Kong { 373285216dc7SFande Kong PetscFunctionBegin; 373385216dc7SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 373485216dc7SFande Kong *force = snes->forceiteration; 373585216dc7SFande Kong PetscFunctionReturn(0); 373685216dc7SFande Kong } 3737be5caee7SBarry Smith 3738be5caee7SBarry Smith /*@ 3739d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 37409b94acceSBarry Smith 37413f9fe445SBarry Smith Logically Collective on SNES 3742c7afd0dbSLois Curfman McInnes 37439b94acceSBarry Smith Input Parameters: 3744c7afd0dbSLois Curfman McInnes + snes - the SNES context 374570441072SBarry Smith . abstol - absolute convergence tolerance 374633174efeSLois Curfman McInnes . rtol - relative convergence tolerance 37475358d0d4SBarry Smith . stol - convergence tolerance in terms of the norm of the change in the solution between steps, || delta x || < stol*|| x || 374833174efeSLois Curfman McInnes . maxit - maximum number of iterations 3749e71169deSBarry Smith - maxf - maximum number of function evaluations (-1 indicates no limit) 3750fee21e36SBarry Smith 375133174efeSLois Curfman McInnes Options Database Keys: 375270441072SBarry Smith + -snes_atol <abstol> - Sets abstol 3753c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 3754c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 3755c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 3756c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 37579b94acceSBarry Smith 3758d7a720efSLois Curfman McInnes Notes: 37599b94acceSBarry Smith The default maximum number of iterations is 50. 37609b94acceSBarry Smith The default maximum number of function evaluations is 1000. 37619b94acceSBarry Smith 376236851e7fSLois Curfman McInnes Level: intermediate 376336851e7fSLois Curfman McInnes 3764db781477SPatrick Sanan .seealso: `SNESSetTrustRegionTolerance()`, `SNESSetDivergenceTolerance()`, `SNESSetForceIteration()` 37659b94acceSBarry Smith @*/ 37667087cfbeSBarry Smith PetscErrorCode SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 37679b94acceSBarry Smith { 37683a40ed3dSBarry Smith PetscFunctionBegin; 37690700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3770c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,abstol,2); 3771c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol,3); 3772c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,stol,4); 3773c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxit,5); 3774c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxf,6); 3775c5eb9154SBarry Smith 3776ab54825eSJed Brown if (abstol != PETSC_DEFAULT) { 37775f80ce2aSJacob Faibussowitsch PetscCheck(abstol >= 0.0,PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %g must be non-negative",(double)abstol); 3778ab54825eSJed Brown snes->abstol = abstol; 3779ab54825eSJed Brown } 3780ab54825eSJed Brown if (rtol != PETSC_DEFAULT) { 37815f80ce2aSJacob Faibussowitsch PetscCheck(rtol >= 0.0 && 1.0 > rtol,PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Relative tolerance %g must be non-negative and less than 1.0",(double)rtol); 3782ab54825eSJed Brown snes->rtol = rtol; 3783ab54825eSJed Brown } 3784ab54825eSJed Brown if (stol != PETSC_DEFAULT) { 37855f80ce2aSJacob Faibussowitsch PetscCheck(stol >= 0.0,PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %g must be non-negative",(double)stol); 3786c60f73f4SPeter Brune snes->stol = stol; 3787ab54825eSJed Brown } 3788ab54825eSJed Brown if (maxit != PETSC_DEFAULT) { 378963a3b9bcSJacob Faibussowitsch PetscCheck(maxit >= 0,PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %" PetscInt_FMT " must be non-negative",maxit); 3790ab54825eSJed Brown snes->max_its = maxit; 3791ab54825eSJed Brown } 3792ab54825eSJed Brown if (maxf != PETSC_DEFAULT) { 379363a3b9bcSJacob Faibussowitsch PetscCheck(maxf >= -1,PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %" PetscInt_FMT " must be -1 or nonnegative",maxf); 3794ab54825eSJed Brown snes->max_funcs = maxf; 3795ab54825eSJed Brown } 379688976e71SPeter Brune snes->tolerancesset = PETSC_TRUE; 37973a40ed3dSBarry Smith PetscFunctionReturn(0); 37989b94acceSBarry Smith } 37999b94acceSBarry Smith 3800e4d06f11SPatrick Farrell /*@ 3801e4d06f11SPatrick Farrell SNESSetDivergenceTolerance - Sets the divergence tolerance used for the SNES divergence test. 3802e4d06f11SPatrick Farrell 3803e4d06f11SPatrick Farrell Logically Collective on SNES 3804e4d06f11SPatrick Farrell 3805e4d06f11SPatrick Farrell Input Parameters: 3806e4d06f11SPatrick Farrell + snes - the SNES context 3807e4d06f11SPatrick Farrell - divtol - the divergence tolerance. Use -1 to deactivate the test. 3808e4d06f11SPatrick Farrell 3809e4d06f11SPatrick Farrell Options Database Keys: 3810a2b725a8SWilliam Gropp . -snes_divergence_tolerance <divtol> - Sets divtol 3811e4d06f11SPatrick Farrell 3812e4d06f11SPatrick Farrell Notes: 3813e4d06f11SPatrick Farrell The default divergence tolerance is 1e4. 3814e4d06f11SPatrick Farrell 3815e4d06f11SPatrick Farrell Level: intermediate 3816e4d06f11SPatrick Farrell 3817db781477SPatrick Sanan .seealso: `SNESSetTolerances()`, `SNESGetDivergenceTolerance` 3818e4d06f11SPatrick Farrell @*/ 3819e4d06f11SPatrick Farrell PetscErrorCode SNESSetDivergenceTolerance(SNES snes,PetscReal divtol) 3820e4d06f11SPatrick Farrell { 3821e4d06f11SPatrick Farrell PetscFunctionBegin; 3822e4d06f11SPatrick Farrell PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3823e4d06f11SPatrick Farrell PetscValidLogicalCollectiveReal(snes,divtol,2); 3824e4d06f11SPatrick Farrell 3825e4d06f11SPatrick Farrell if (divtol != PETSC_DEFAULT) { 3826e4d06f11SPatrick Farrell snes->divtol = divtol; 3827e4d06f11SPatrick Farrell } 3828e4d06f11SPatrick Farrell else { 3829e4d06f11SPatrick Farrell snes->divtol = 1.0e4; 3830e4d06f11SPatrick Farrell } 3831e4d06f11SPatrick Farrell PetscFunctionReturn(0); 3832e4d06f11SPatrick Farrell } 3833e4d06f11SPatrick Farrell 38349b94acceSBarry Smith /*@ 383533174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 383633174efeSLois Curfman McInnes 3837c7afd0dbSLois Curfman McInnes Not Collective 3838c7afd0dbSLois Curfman McInnes 383933174efeSLois Curfman McInnes Input Parameters: 3840c7afd0dbSLois Curfman McInnes + snes - the SNES context 384185385478SLisandro Dalcin . atol - absolute convergence tolerance 384233174efeSLois Curfman McInnes . rtol - relative convergence tolerance 384333174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 384433174efeSLois Curfman McInnes of the change in the solution between steps 384533174efeSLois Curfman McInnes . maxit - maximum number of iterations 3846c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 3847fee21e36SBarry Smith 384833174efeSLois Curfman McInnes Notes: 38490298fd71SBarry Smith The user can specify NULL for any parameter that is not needed. 385033174efeSLois Curfman McInnes 385136851e7fSLois Curfman McInnes Level: intermediate 385236851e7fSLois Curfman McInnes 3853db781477SPatrick Sanan .seealso: `SNESSetTolerances()` 385433174efeSLois Curfman McInnes @*/ 38557087cfbeSBarry Smith PetscErrorCode SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 385633174efeSLois Curfman McInnes { 38573a40ed3dSBarry Smith PetscFunctionBegin; 38580700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 385985385478SLisandro Dalcin if (atol) *atol = snes->abstol; 386033174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 3861c60f73f4SPeter Brune if (stol) *stol = snes->stol; 386233174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 386333174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 38643a40ed3dSBarry Smith PetscFunctionReturn(0); 386533174efeSLois Curfman McInnes } 386633174efeSLois Curfman McInnes 3867e4d06f11SPatrick Farrell /*@ 3868e4d06f11SPatrick Farrell SNESGetDivergenceTolerance - Gets divergence tolerance used in divergence test. 3869e4d06f11SPatrick Farrell 3870e4d06f11SPatrick Farrell Not Collective 3871e4d06f11SPatrick Farrell 3872e4d06f11SPatrick Farrell Input Parameters: 3873e4d06f11SPatrick Farrell + snes - the SNES context 3874e4d06f11SPatrick Farrell - divtol - divergence tolerance 3875e4d06f11SPatrick Farrell 3876e4d06f11SPatrick Farrell Level: intermediate 3877e4d06f11SPatrick Farrell 3878db781477SPatrick Sanan .seealso: `SNESSetDivergenceTolerance()` 3879e4d06f11SPatrick Farrell @*/ 3880e4d06f11SPatrick Farrell PetscErrorCode SNESGetDivergenceTolerance(SNES snes,PetscReal *divtol) 3881e4d06f11SPatrick Farrell { 3882e4d06f11SPatrick Farrell PetscFunctionBegin; 3883e4d06f11SPatrick Farrell PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3884e4d06f11SPatrick Farrell if (divtol) *divtol = snes->divtol; 3885e4d06f11SPatrick Farrell PetscFunctionReturn(0); 3886e4d06f11SPatrick Farrell } 3887e4d06f11SPatrick Farrell 388833174efeSLois Curfman McInnes /*@ 38899b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 38909b94acceSBarry Smith 38913f9fe445SBarry Smith Logically Collective on SNES 3892fee21e36SBarry Smith 3893c7afd0dbSLois Curfman McInnes Input Parameters: 3894c7afd0dbSLois Curfman McInnes + snes - the SNES context 3895c7afd0dbSLois Curfman McInnes - tol - tolerance 3896c7afd0dbSLois Curfman McInnes 38979b94acceSBarry Smith Options Database Key: 3898c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 38999b94acceSBarry Smith 390036851e7fSLois Curfman McInnes Level: intermediate 390136851e7fSLois Curfman McInnes 3902db781477SPatrick Sanan .seealso: `SNESSetTolerances()` 39039b94acceSBarry Smith @*/ 39047087cfbeSBarry Smith PetscErrorCode SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 39059b94acceSBarry Smith { 39063a40ed3dSBarry Smith PetscFunctionBegin; 39070700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3908c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,tol,2); 39099b94acceSBarry Smith snes->deltatol = tol; 39103a40ed3dSBarry Smith PetscFunctionReturn(0); 39119b94acceSBarry Smith } 39129b94acceSBarry Smith 39136ba87a44SLisandro Dalcin PETSC_INTERN PetscErrorCode SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 39146ba87a44SLisandro Dalcin 39157087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 3916b271bb04SBarry Smith { 3917b271bb04SBarry Smith PetscDrawLG lg; 3918b271bb04SBarry Smith PetscReal x,y,per; 3919b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 3920b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 3921b271bb04SBarry Smith PetscDraw draw; 3922b271bb04SBarry Smith 3923459f5d12SBarry Smith PetscFunctionBegin; 39244d4332d5SBarry Smith PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,4); 39259566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawLG(v,0,&lg)); 39269566063dSJacob Faibussowitsch if (!n) PetscCall(PetscDrawLGReset(lg)); 39279566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(lg,&draw)); 39289566063dSJacob Faibussowitsch PetscCall(PetscDrawSetTitle(draw,"Residual norm")); 3929b271bb04SBarry Smith x = (PetscReal)n; 393077b4d14cSPeter Brune if (rnorm > 0.0) y = PetscLog10Real(rnorm); 393194c9c6d3SKarl Rupp else y = -15.0; 39329566063dSJacob Faibussowitsch PetscCall(PetscDrawLGAddPoint(lg,&x,&y)); 39336934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 39349566063dSJacob Faibussowitsch PetscCall(PetscDrawLGDraw(lg)); 39359566063dSJacob Faibussowitsch PetscCall(PetscDrawLGSave(lg)); 3936b271bb04SBarry Smith } 3937b271bb04SBarry Smith 39389566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawLG(v,1,&lg)); 39399566063dSJacob Faibussowitsch if (!n) PetscCall(PetscDrawLGReset(lg)); 39409566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(lg,&draw)); 39419566063dSJacob Faibussowitsch PetscCall(PetscDrawSetTitle(draw,"% elemts > .2*max elemt")); 39429566063dSJacob Faibussowitsch PetscCall(SNESMonitorRange_Private(snes,n,&per)); 3943b271bb04SBarry Smith x = (PetscReal)n; 3944b271bb04SBarry Smith y = 100.0*per; 39459566063dSJacob Faibussowitsch PetscCall(PetscDrawLGAddPoint(lg,&x,&y)); 39466934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 39479566063dSJacob Faibussowitsch PetscCall(PetscDrawLGDraw(lg)); 39489566063dSJacob Faibussowitsch PetscCall(PetscDrawLGSave(lg)); 3949b271bb04SBarry Smith } 3950b271bb04SBarry Smith 39519566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawLG(v,2,&lg)); 39529566063dSJacob Faibussowitsch if (!n) {prev = rnorm;PetscCall(PetscDrawLGReset(lg));} 39539566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(lg,&draw)); 39549566063dSJacob Faibussowitsch PetscCall(PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm")); 3955b271bb04SBarry Smith x = (PetscReal)n; 3956b271bb04SBarry Smith y = (prev - rnorm)/prev; 39579566063dSJacob Faibussowitsch PetscCall(PetscDrawLGAddPoint(lg,&x,&y)); 39586934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 39599566063dSJacob Faibussowitsch PetscCall(PetscDrawLGDraw(lg)); 39609566063dSJacob Faibussowitsch PetscCall(PetscDrawLGSave(lg)); 3961b271bb04SBarry Smith } 3962b271bb04SBarry Smith 39639566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawLG(v,3,&lg)); 39649566063dSJacob Faibussowitsch if (!n) PetscCall(PetscDrawLGReset(lg)); 39659566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(lg,&draw)); 39669566063dSJacob Faibussowitsch PetscCall(PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)")); 3967b271bb04SBarry Smith x = (PetscReal)n; 3968b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 3969b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 39709566063dSJacob Faibussowitsch PetscCall(PetscDrawLGAddPoint(lg,&x,&y)); 3971b271bb04SBarry Smith } 39726934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 39739566063dSJacob Faibussowitsch PetscCall(PetscDrawLGDraw(lg)); 39749566063dSJacob Faibussowitsch PetscCall(PetscDrawLGSave(lg)); 3975b271bb04SBarry Smith } 3976b271bb04SBarry Smith prev = rnorm; 3977b271bb04SBarry Smith PetscFunctionReturn(0); 3978b271bb04SBarry Smith } 3979b271bb04SBarry Smith 3980228d79bcSJed Brown /*@ 3981228d79bcSJed Brown SNESMonitor - runs the user provided monitor routines, if they exist 3982228d79bcSJed Brown 3983228d79bcSJed Brown Collective on SNES 3984228d79bcSJed Brown 3985228d79bcSJed Brown Input Parameters: 3986228d79bcSJed Brown + snes - nonlinear solver context obtained from SNESCreate() 3987228d79bcSJed Brown . iter - iteration number 3988228d79bcSJed Brown - rnorm - relative norm of the residual 3989228d79bcSJed Brown 3990228d79bcSJed Brown Notes: 3991228d79bcSJed Brown This routine is called by the SNES implementations. 3992228d79bcSJed Brown It does not typically need to be called by the user. 3993228d79bcSJed Brown 3994228d79bcSJed Brown Level: developer 3995228d79bcSJed Brown 3996db781477SPatrick Sanan .seealso: `SNESMonitorSet()` 3997228d79bcSJed Brown @*/ 39987a03ce2fSLisandro Dalcin PetscErrorCode SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm) 39997a03ce2fSLisandro Dalcin { 40007a03ce2fSLisandro Dalcin PetscInt i,n = snes->numbermonitors; 40017a03ce2fSLisandro Dalcin 40027a03ce2fSLisandro Dalcin PetscFunctionBegin; 40039566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(snes->vec_sol)); 40047a03ce2fSLisandro Dalcin for (i=0; i<n; i++) { 40059566063dSJacob Faibussowitsch PetscCall((*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i])); 40067a03ce2fSLisandro Dalcin } 40079566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(snes->vec_sol)); 40087a03ce2fSLisandro Dalcin PetscFunctionReturn(0); 40097a03ce2fSLisandro Dalcin } 40107a03ce2fSLisandro Dalcin 40119b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 40129b94acceSBarry Smith 4013bf388a1fSBarry Smith /*MC 4014bf388a1fSBarry Smith SNESMonitorFunction - functional form passed to SNESMonitorSet() to monitor convergence of nonlinear solver 4015bf388a1fSBarry Smith 4016bf388a1fSBarry Smith Synopsis: 4017aaa7dc30SBarry Smith #include <petscsnes.h> 4018bf388a1fSBarry Smith $ PetscErrorCode SNESMonitorFunction(SNES snes,PetscInt its, PetscReal norm,void *mctx) 4019bf388a1fSBarry Smith 40201843f636SBarry Smith Collective on snes 40211843f636SBarry Smith 40221843f636SBarry Smith Input Parameters: 4023bf388a1fSBarry Smith + snes - the SNES context 4024bf388a1fSBarry Smith . its - iteration number 4025bf388a1fSBarry Smith . norm - 2-norm function value (may be estimated) 4026bf388a1fSBarry Smith - mctx - [optional] monitoring context 4027bf388a1fSBarry Smith 4028878cb397SSatish Balay Level: advanced 4029878cb397SSatish Balay 4030db781477SPatrick Sanan .seealso: `SNESMonitorSet()`, `SNESMonitorGet()` 4031bf388a1fSBarry Smith M*/ 4032bf388a1fSBarry Smith 40339b94acceSBarry Smith /*@C 4034a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 40359b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 40369b94acceSBarry Smith progress. 40379b94acceSBarry Smith 40383f9fe445SBarry Smith Logically Collective on SNES 4039fee21e36SBarry Smith 4040c7afd0dbSLois Curfman McInnes Input Parameters: 4041c7afd0dbSLois Curfman McInnes + snes - the SNES context 40426e4dcb14SBarry Smith . f - the monitor function, see SNESMonitorFunction for the calling sequence 4043b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 40440298fd71SBarry Smith monitor routine (use NULL if no context is desired) 4045b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 40460298fd71SBarry Smith (may be NULL) 40479b94acceSBarry Smith 40489665c990SLois Curfman McInnes Options Database Keys: 4049a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 4050798534f6SMatthew G. Knepley . -snes_monitor draw::draw_lg - sets line graph monitor, 4051cca6129bSJed Brown - -snes_monitor_cancel - cancels all monitors that have 4052c7afd0dbSLois Curfman McInnes been hardwired into a code by 4053a6570f20SBarry Smith calls to SNESMonitorSet(), but 4054c7afd0dbSLois Curfman McInnes does not cancel those set via 4055c7afd0dbSLois Curfman McInnes the options database. 40569665c990SLois Curfman McInnes 4057639f9d9dSBarry Smith Notes: 40586bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 4059a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 40606bc08f3fSLois Curfman McInnes order in which they were set. 4061639f9d9dSBarry Smith 406295452b02SPatrick Sanan Fortran Notes: 406395452b02SPatrick Sanan Only a single monitor function can be set for each SNES object 4064025f1a04SBarry Smith 406536851e7fSLois Curfman McInnes Level: intermediate 406636851e7fSLois Curfman McInnes 4067db781477SPatrick Sanan .seealso: `SNESMonitorDefault()`, `SNESMonitorCancel()`, `SNESMonitorFunction` 40689b94acceSBarry Smith @*/ 40696e4dcb14SBarry Smith PetscErrorCode SNESMonitorSet(SNES snes,PetscErrorCode (*f)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 40709b94acceSBarry Smith { 4071b90d0a6eSBarry Smith PetscInt i; 407278064530SBarry Smith PetscBool identical; 4073b90d0a6eSBarry Smith 40743a40ed3dSBarry Smith PetscFunctionBegin; 40750700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4076b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 40779566063dSJacob Faibussowitsch PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))f,mctx,monitordestroy,(PetscErrorCode (*)(void))snes->monitor[i],snes->monitorcontext[i],snes->monitordestroy[i],&identical)); 407878064530SBarry Smith if (identical) PetscFunctionReturn(0); 4079649052a6SBarry Smith } 40805f80ce2aSJacob Faibussowitsch PetscCheck(snes->numbermonitors < MAXSNESMONITORS,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 40816e4dcb14SBarry Smith snes->monitor[snes->numbermonitors] = f; 4082b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 4083639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 40843a40ed3dSBarry Smith PetscFunctionReturn(0); 40859b94acceSBarry Smith } 40869b94acceSBarry Smith 4087a278d85bSSatish Balay /*@ 4088a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 40895cd90555SBarry Smith 40903f9fe445SBarry Smith Logically Collective on SNES 4091c7afd0dbSLois Curfman McInnes 40925cd90555SBarry Smith Input Parameters: 40935cd90555SBarry Smith . snes - the SNES context 40945cd90555SBarry Smith 40951a480d89SAdministrator Options Database Key: 4096a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 4097a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 4098c7afd0dbSLois Curfman McInnes set via the options database 40995cd90555SBarry Smith 41005cd90555SBarry Smith Notes: 41015cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 41025cd90555SBarry Smith 410336851e7fSLois Curfman McInnes Level: intermediate 410436851e7fSLois Curfman McInnes 4105db781477SPatrick Sanan .seealso: `SNESMonitorDefault()`, `SNESMonitorSet()` 41065cd90555SBarry Smith @*/ 41077087cfbeSBarry Smith PetscErrorCode SNESMonitorCancel(SNES snes) 41085cd90555SBarry Smith { 4109d952e501SBarry Smith PetscInt i; 4110d952e501SBarry Smith 41115cd90555SBarry Smith PetscFunctionBegin; 41120700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4113d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 4114d952e501SBarry Smith if (snes->monitordestroy[i]) { 41159566063dSJacob Faibussowitsch PetscCall((*snes->monitordestroy[i])(&snes->monitorcontext[i])); 4116d952e501SBarry Smith } 4117d952e501SBarry Smith } 41185cd90555SBarry Smith snes->numbermonitors = 0; 41195cd90555SBarry Smith PetscFunctionReturn(0); 41205cd90555SBarry Smith } 41215cd90555SBarry Smith 4122bf388a1fSBarry Smith /*MC 4123bf388a1fSBarry Smith SNESConvergenceTestFunction - functional form used for testing of convergence of nonlinear solver 4124bf388a1fSBarry Smith 4125bf388a1fSBarry Smith Synopsis: 4126aaa7dc30SBarry Smith #include <petscsnes.h> 4127bf388a1fSBarry Smith $ PetscErrorCode SNESConvergenceTest(SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 4128bf388a1fSBarry Smith 41291843f636SBarry Smith Collective on snes 41301843f636SBarry Smith 41311843f636SBarry Smith Input Parameters: 4132bf388a1fSBarry Smith + snes - the SNES context 4133bf388a1fSBarry Smith . it - current iteration (0 is the first and is before any Newton step) 4134bf388a1fSBarry Smith . xnorm - 2-norm of current iterate 4135bf388a1fSBarry Smith . gnorm - 2-norm of current step 41361843f636SBarry Smith . f - 2-norm of function 41371843f636SBarry Smith - cctx - [optional] convergence context 41381843f636SBarry Smith 41391843f636SBarry Smith Output Parameter: 41401843f636SBarry Smith . reason - reason for convergence/divergence, only needs to be set when convergence or divergence is detected 4141bf388a1fSBarry Smith 4142878cb397SSatish Balay Level: intermediate 4143bf388a1fSBarry Smith 4144db781477SPatrick Sanan .seealso: `SNESSetConvergenceTest()`, `SNESGetConvergenceTest()` 4145bf388a1fSBarry Smith M*/ 4146bf388a1fSBarry Smith 41479b94acceSBarry Smith /*@C 41489b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 41499b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 41509b94acceSBarry Smith 41513f9fe445SBarry Smith Logically Collective on SNES 4152fee21e36SBarry Smith 4153c7afd0dbSLois Curfman McInnes Input Parameters: 4154c7afd0dbSLois Curfman McInnes + snes - the SNES context 4155bf388a1fSBarry Smith . SNESConvergenceTestFunction - routine to test for convergence 41560298fd71SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be NULL) 4157cf90aa19SBarry Smith - destroy - [optional] destructor for the context (may be NULL; PETSC_NULL_FUNCTION in Fortran) 41589b94acceSBarry Smith 415936851e7fSLois Curfman McInnes Level: advanced 416036851e7fSLois Curfman McInnes 4161db781477SPatrick Sanan .seealso: `SNESConvergedDefault()`, `SNESConvergedSkip()`, `SNESConvergenceTestFunction` 41629b94acceSBarry Smith @*/ 4163bf388a1fSBarry Smith PetscErrorCode SNESSetConvergenceTest(SNES snes,PetscErrorCode (*SNESConvergenceTestFunction)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 41649b94acceSBarry Smith { 41653a40ed3dSBarry Smith PetscFunctionBegin; 41660700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4167e2a6519dSDmitry Karpeev if (!SNESConvergenceTestFunction) SNESConvergenceTestFunction = SNESConvergedSkip; 4168*1baa6e33SBarry Smith if (snes->ops->convergeddestroy) PetscCall((*snes->ops->convergeddestroy)(snes->cnvP)); 4169bf388a1fSBarry Smith snes->ops->converged = SNESConvergenceTestFunction; 41707f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 417185385478SLisandro Dalcin snes->cnvP = cctx; 41723a40ed3dSBarry Smith PetscFunctionReturn(0); 41739b94acceSBarry Smith } 41749b94acceSBarry Smith 417552baeb72SSatish Balay /*@ 4176184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 4177184914b5SBarry Smith 4178184914b5SBarry Smith Not Collective 4179184914b5SBarry Smith 4180184914b5SBarry Smith Input Parameter: 4181184914b5SBarry Smith . snes - the SNES context 4182184914b5SBarry Smith 4183184914b5SBarry Smith Output Parameter: 41844d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 4185184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 4186184914b5SBarry Smith 41876a4d7782SBarry Smith Options Database: 41886a4d7782SBarry Smith . -snes_converged_reason - prints the reason to standard out 41896a4d7782SBarry Smith 4190184914b5SBarry Smith Level: intermediate 4191184914b5SBarry Smith 419295452b02SPatrick Sanan Notes: 419395452b02SPatrick Sanan Should only be called after the call the SNESSolve() is complete, if it is called earlier it returns the value SNES__CONVERGED_ITERATING. 4194184914b5SBarry Smith 4195db781477SPatrick Sanan .seealso: `SNESSetConvergenceTest()`, `SNESSetConvergedReason()`, `SNESConvergedReason` 4196184914b5SBarry Smith @*/ 41977087cfbeSBarry Smith PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 4198184914b5SBarry Smith { 4199184914b5SBarry Smith PetscFunctionBegin; 42000700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 42014482741eSBarry Smith PetscValidPointer(reason,2); 4202184914b5SBarry Smith *reason = snes->reason; 4203184914b5SBarry Smith PetscFunctionReturn(0); 4204184914b5SBarry Smith } 4205184914b5SBarry Smith 4206c4421ceaSFande Kong /*@C 4207c4421ceaSFande Kong SNESGetConvergedReasonString - Return a human readable string for snes converged reason 4208c4421ceaSFande Kong 4209c4421ceaSFande Kong Not Collective 4210c4421ceaSFande Kong 4211c4421ceaSFande Kong Input Parameter: 4212c4421ceaSFande Kong . snes - the SNES context 4213c4421ceaSFande Kong 4214c4421ceaSFande Kong Output Parameter: 4215c4421ceaSFande Kong . strreason - a human readable string that describes SNES converged reason 4216c4421ceaSFande Kong 421799c90e12SSatish Balay Level: beginner 4218c4421ceaSFande Kong 4219db781477SPatrick Sanan .seealso: `SNESGetConvergedReason()` 4220c4421ceaSFande Kong @*/ 4221c4421ceaSFande Kong PetscErrorCode SNESGetConvergedReasonString(SNES snes, const char** strreason) 4222c4421ceaSFande Kong { 4223c4421ceaSFande Kong PetscFunctionBegin; 4224c4421ceaSFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4225dadcf809SJacob Faibussowitsch PetscValidPointer(strreason,2); 4226c4421ceaSFande Kong *strreason = SNESConvergedReasons[snes->reason]; 4227c4421ceaSFande Kong PetscFunctionReturn(0); 4228c4421ceaSFande Kong } 4229c4421ceaSFande Kong 423033866048SMatthew G. Knepley /*@ 423133866048SMatthew G. Knepley SNESSetConvergedReason - Sets the reason the SNES iteration was stopped. 423233866048SMatthew G. Knepley 423333866048SMatthew G. Knepley Not Collective 423433866048SMatthew G. Knepley 423533866048SMatthew G. Knepley Input Parameters: 423633866048SMatthew G. Knepley + snes - the SNES context 423733866048SMatthew G. Knepley - reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 423833866048SMatthew G. Knepley manual pages for the individual convergence tests for complete lists 423933866048SMatthew G. Knepley 424033866048SMatthew G. Knepley Level: intermediate 424133866048SMatthew G. Knepley 4242db781477SPatrick Sanan .seealso: `SNESGetConvergedReason()`, `SNESSetConvergenceTest()`, `SNESConvergedReason` 424333866048SMatthew G. Knepley @*/ 424433866048SMatthew G. Knepley PetscErrorCode SNESSetConvergedReason(SNES snes,SNESConvergedReason reason) 424533866048SMatthew G. Knepley { 424633866048SMatthew G. Knepley PetscFunctionBegin; 424733866048SMatthew G. Knepley PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 424833866048SMatthew G. Knepley snes->reason = reason; 424933866048SMatthew G. Knepley PetscFunctionReturn(0); 425033866048SMatthew G. Knepley } 425133866048SMatthew G. Knepley 4252c9005455SLois Curfman McInnes /*@ 4253c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 4254c9005455SLois Curfman McInnes 42553f9fe445SBarry Smith Logically Collective on SNES 4256fee21e36SBarry Smith 4257c7afd0dbSLois Curfman McInnes Input Parameters: 4258c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 42598c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 4260cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 4261758f92a0SBarry Smith . na - size of a and its 426264731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 4263758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 4264c7afd0dbSLois Curfman McInnes 4265308dcc3eSBarry Smith Notes: 42660298fd71SBarry Smith If 'a' and 'its' are NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a 4267308dcc3eSBarry Smith default array of length 10000 is allocated. 4268308dcc3eSBarry Smith 4269c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 4270c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 4271c9005455SLois Curfman McInnes during the section of code that is being timed. 4272c9005455SLois Curfman McInnes 427336851e7fSLois Curfman McInnes Level: intermediate 427436851e7fSLois Curfman McInnes 4275db781477SPatrick Sanan .seealso: `SNESGetConvergenceHistory()` 4276758f92a0SBarry Smith 4277c9005455SLois Curfman McInnes @*/ 42787087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset) 4279c9005455SLois Curfman McInnes { 42803a40ed3dSBarry Smith PetscFunctionBegin; 42810700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4282064a246eSJacob Faibussowitsch if (a) PetscValidRealPointer(a,2); 4283a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 42847a1ec6d4SBarry Smith if (!a) { 4285308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 42869566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(na,&a,na,&its)); 4287071fcb05SBarry Smith snes->conv_hist_alloc = PETSC_TRUE; 4288308dcc3eSBarry Smith } 4289c9005455SLois Curfman McInnes snes->conv_hist = a; 4290758f92a0SBarry Smith snes->conv_hist_its = its; 4291115dd874SBarry Smith snes->conv_hist_max = (size_t)na; 4292a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 4293758f92a0SBarry Smith snes->conv_hist_reset = reset; 4294758f92a0SBarry Smith PetscFunctionReturn(0); 4295758f92a0SBarry Smith } 4296758f92a0SBarry Smith 4297308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4298c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 4299c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 430099e0435eSBarry Smith 43018cc058d9SJed Brown PETSC_EXTERN mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 4302308dcc3eSBarry Smith { 4303308dcc3eSBarry Smith mxArray *mat; 4304308dcc3eSBarry Smith PetscInt i; 4305308dcc3eSBarry Smith PetscReal *ar; 4306308dcc3eSBarry Smith 4307308dcc3eSBarry Smith PetscFunctionBegin; 4308308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL); 4309308dcc3eSBarry Smith ar = (PetscReal*) mxGetData(mat); 4310f5af7f23SKarl Rupp for (i=0; i<snes->conv_hist_len; i++) ar[i] = snes->conv_hist[i]; 4311308dcc3eSBarry Smith PetscFunctionReturn(mat); 4312308dcc3eSBarry Smith } 4313308dcc3eSBarry Smith #endif 4314308dcc3eSBarry Smith 43150c4c9dddSBarry Smith /*@C 4316758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 4317758f92a0SBarry Smith 43183f9fe445SBarry Smith Not Collective 4319758f92a0SBarry Smith 4320758f92a0SBarry Smith Input Parameter: 4321758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 4322758f92a0SBarry Smith 4323758f92a0SBarry Smith Output Parameters: 4324a2b725a8SWilliam Gropp + a - array to hold history 4325758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 4326758f92a0SBarry Smith negative if not converged) for each solve. 4327758f92a0SBarry Smith - na - size of a and its 4328758f92a0SBarry Smith 4329758f92a0SBarry Smith Notes: 4330758f92a0SBarry Smith The calling sequence for this routine in Fortran is 4331758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 4332758f92a0SBarry Smith 4333758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 4334758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 4335758f92a0SBarry Smith during the section of code that is being timed. 4336758f92a0SBarry Smith 4337758f92a0SBarry Smith Level: intermediate 4338758f92a0SBarry Smith 4339db781477SPatrick Sanan .seealso: `SNESSetConvergenceHistory()` 4340758f92a0SBarry Smith 4341758f92a0SBarry Smith @*/ 43427087cfbeSBarry Smith PetscErrorCode SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 4343758f92a0SBarry Smith { 4344758f92a0SBarry Smith PetscFunctionBegin; 43450700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4346758f92a0SBarry Smith if (a) *a = snes->conv_hist; 4347758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 4348115dd874SBarry Smith if (na) *na = (PetscInt) snes->conv_hist_len; 43493a40ed3dSBarry Smith PetscFunctionReturn(0); 4350c9005455SLois Curfman McInnes } 4351c9005455SLois Curfman McInnes 4352ac226902SBarry Smith /*@C 435376b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 4354eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 43557e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 435676b2cf59SMatthew Knepley 43573f9fe445SBarry Smith Logically Collective on SNES 435876b2cf59SMatthew Knepley 435976b2cf59SMatthew Knepley Input Parameters: 4360a2b725a8SWilliam Gropp + snes - The nonlinear solver context 4361a2b725a8SWilliam Gropp - func - The function 436276b2cf59SMatthew Knepley 436376b2cf59SMatthew Knepley Calling sequence of func: 4364a2b725a8SWilliam Gropp $ func (SNES snes, PetscInt step); 436576b2cf59SMatthew Knepley 436676b2cf59SMatthew Knepley . step - The current step of the iteration 436776b2cf59SMatthew Knepley 4368fe97e370SBarry Smith Level: advanced 4369fe97e370SBarry Smith 43706b7fb656SBarry Smith Note: 43716b7fb656SBarry Smith 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() 4372fe97e370SBarry Smith This is not used by most users. 437376b2cf59SMatthew Knepley 43746b7fb656SBarry Smith There are a varity of function hooks one many set that are called at different stages of the nonlinear solution process, see the functions listed below. 43756b7fb656SBarry Smith 4376db781477SPatrick Sanan .seealso `SNESSetJacobian()`, `SNESSolve()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()`, `SNESNewtonTRSetPreCheck()`, `SNESNewtonTRSetPostCheck()`, 4377db781477SPatrick Sanan `SNESMonitorSet()`, `SNESSetDivergenceTest()` 437876b2cf59SMatthew Knepley @*/ 43797087cfbeSBarry Smith PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 438076b2cf59SMatthew Knepley { 438176b2cf59SMatthew Knepley PetscFunctionBegin; 43820700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 4383e7788613SBarry Smith snes->ops->update = func; 438476b2cf59SMatthew Knepley PetscFunctionReturn(0); 438576b2cf59SMatthew Knepley } 438676b2cf59SMatthew Knepley 43879b94acceSBarry Smith /* 43889b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 43899b94acceSBarry Smith positive parameter delta. 43909b94acceSBarry Smith 43919b94acceSBarry Smith Input Parameters: 4392c7afd0dbSLois Curfman McInnes + snes - the SNES context 43939b94acceSBarry Smith . y - approximate solution of linear system 43949b94acceSBarry Smith . fnorm - 2-norm of current function 4395c7afd0dbSLois Curfman McInnes - delta - trust region size 43969b94acceSBarry Smith 43979b94acceSBarry Smith Output Parameters: 4398c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 43999b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 44009b94acceSBarry Smith region, and exceeds zero otherwise. 4401c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 44029b94acceSBarry Smith 44039b94acceSBarry Smith Note: 440404d7464bSBarry Smith For non-trust region methods such as SNESNEWTONLS, the parameter delta 44059b94acceSBarry Smith is set to be the maximum allowable step size. 44069b94acceSBarry Smith 44079b94acceSBarry Smith */ 4408dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 44099b94acceSBarry Smith { 4410064f8208SBarry Smith PetscReal nrm; 4411ea709b57SSatish Balay PetscScalar cnorm; 44123a40ed3dSBarry Smith 44133a40ed3dSBarry Smith PetscFunctionBegin; 44140700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 44150700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 4416c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 4417184914b5SBarry Smith 44189566063dSJacob Faibussowitsch PetscCall(VecNorm(y,NORM_2,&nrm)); 4419064f8208SBarry Smith if (nrm > *delta) { 4420064f8208SBarry Smith nrm = *delta/nrm; 4421064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 4422064f8208SBarry Smith cnorm = nrm; 44239566063dSJacob Faibussowitsch PetscCall(VecScale(y,cnorm)); 44249b94acceSBarry Smith *ynorm = *delta; 44259b94acceSBarry Smith } else { 44269b94acceSBarry Smith *gpnorm = 0.0; 4427064f8208SBarry Smith *ynorm = nrm; 44289b94acceSBarry Smith } 44293a40ed3dSBarry Smith PetscFunctionReturn(0); 44309b94acceSBarry Smith } 44319b94acceSBarry Smith 443291f3e32bSBarry Smith /*@C 443319a666eeSBarry Smith SNESConvergedReasonView - Displays the reason a SNES solve converged or diverged to a viewer 44342a359c20SBarry Smith 44352a359c20SBarry Smith Collective on SNES 44362a359c20SBarry Smith 44372a359c20SBarry Smith Parameter: 44382a359c20SBarry Smith + snes - iterative context obtained from SNESCreate() 44392a359c20SBarry Smith - viewer - the viewer to display the reason 44402a359c20SBarry Smith 44412a359c20SBarry Smith Options Database Keys: 4442ee300463SSatish Balay + -snes_converged_reason - print reason for converged or diverged, also prints number of iterations 4443ee300463SSatish Balay - -snes_converged_reason ::failed - only print reason and number of iterations when diverged 4444eafd5ff0SAlex Lindsay 444519a666eeSBarry Smith Notes: 444619a666eeSBarry Smith To change the format of the output call PetscViewerPushFormat(viewer,format) before this call. Use PETSC_VIEWER_DEFAULT for the default, 444719a666eeSBarry Smith use PETSC_VIEWER_FAILED to only display a reason if it fails. 44482a359c20SBarry Smith 44492a359c20SBarry Smith Level: beginner 44502a359c20SBarry Smith 4451db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESSetUp()`, `SNESDestroy()`, `SNESSetTolerances()`, `SNESConvergedDefault()`, `SNESGetConvergedReason()`, `SNESConvergedReasonViewFromOptions()`, 4452db781477SPatrick Sanan `PetscViewerPushFormat()`, `PetscViewerPopFormat()` 44532a359c20SBarry Smith 44542a359c20SBarry Smith @*/ 445519a666eeSBarry Smith PetscErrorCode SNESConvergedReasonView(SNES snes,PetscViewer viewer) 44562a359c20SBarry Smith { 445775cca76cSMatthew G. Knepley PetscViewerFormat format; 44582a359c20SBarry Smith PetscBool isAscii; 44592a359c20SBarry Smith 44602a359c20SBarry Smith PetscFunctionBegin; 446119a666eeSBarry Smith if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)); 44629566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isAscii)); 44632a359c20SBarry Smith if (isAscii) { 44649566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(viewer, &format)); 44659566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel)); 446675cca76cSMatthew G. Knepley if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 446775cca76cSMatthew G. Knepley DM dm; 446875cca76cSMatthew G. Knepley Vec u; 446975cca76cSMatthew G. Knepley PetscDS prob; 447075cca76cSMatthew G. Knepley PetscInt Nf, f; 447195cbbfd3SMatthew G. Knepley PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *); 447295cbbfd3SMatthew G. Knepley void **exactCtx; 447375cca76cSMatthew G. Knepley PetscReal error; 447475cca76cSMatthew G. Knepley 44759566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 44769566063dSJacob Faibussowitsch PetscCall(SNESGetSolution(snes, &u)); 44779566063dSJacob Faibussowitsch PetscCall(DMGetDS(dm, &prob)); 44789566063dSJacob Faibussowitsch PetscCall(PetscDSGetNumFields(prob, &Nf)); 44799566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(Nf, &exactSol, Nf, &exactCtx)); 44809566063dSJacob Faibussowitsch for (f = 0; f < Nf; ++f) PetscCall(PetscDSGetExactSolution(prob, f, &exactSol[f], &exactCtx[f])); 44819566063dSJacob Faibussowitsch PetscCall(DMComputeL2Diff(dm, 0.0, exactSol, exactCtx, u, &error)); 44829566063dSJacob Faibussowitsch PetscCall(PetscFree2(exactSol, exactCtx)); 44839566063dSJacob Faibussowitsch if (error < 1.0e-11) PetscCall(PetscViewerASCIIPrintf(viewer, "L_2 Error: < 1.0e-11\n")); 448463a3b9bcSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "L_2 Error: %g\n", (double)error)); 448575cca76cSMatthew G. Knepley } 4486eafd5ff0SAlex Lindsay if (snes->reason > 0 && format != PETSC_VIEWER_FAILED) { 44872a359c20SBarry Smith if (((PetscObject) snes)->prefix) { 448863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer,"Nonlinear %s solve converged due to %s iterations %" PetscInt_FMT "\n",((PetscObject) snes)->prefix,SNESConvergedReasons[snes->reason],snes->iter)); 44892a359c20SBarry Smith } else { 449063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer,"Nonlinear solve converged due to %s iterations %" PetscInt_FMT "\n",SNESConvergedReasons[snes->reason],snes->iter)); 44912a359c20SBarry Smith } 4492eafd5ff0SAlex Lindsay } else if (snes->reason <= 0) { 44932a359c20SBarry Smith if (((PetscObject) snes)->prefix) { 449463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer,"Nonlinear %s solve did not converge due to %s iterations %" PetscInt_FMT "\n",((PetscObject) snes)->prefix,SNESConvergedReasons[snes->reason],snes->iter)); 44952a359c20SBarry Smith } else { 449663a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer,"Nonlinear solve did not converge due to %s iterations %" PetscInt_FMT "\n",SNESConvergedReasons[snes->reason],snes->iter)); 44972a359c20SBarry Smith } 44982a359c20SBarry Smith } 44999566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel)); 45002a359c20SBarry Smith } 45012a359c20SBarry Smith PetscFunctionReturn(0); 45022a359c20SBarry Smith } 45032a359c20SBarry Smith 4504c4421ceaSFande Kong /*@C 4505c4421ceaSFande Kong SNESConvergedReasonViewSet - Sets an ADDITIONAL function that is to be used at the 4506c4421ceaSFande Kong end of the nonlinear solver to display the conver reason of the nonlinear solver. 4507c4421ceaSFande Kong 4508c4421ceaSFande Kong Logically Collective on SNES 4509c4421ceaSFande Kong 4510c4421ceaSFande Kong Input Parameters: 4511c4421ceaSFande Kong + snes - the SNES context 4512c4421ceaSFande Kong . f - the snes converged reason view function 4513c4421ceaSFande Kong . vctx - [optional] user-defined context for private data for the 4514c4421ceaSFande Kong snes converged reason view routine (use NULL if no context is desired) 4515c4421ceaSFande Kong - reasonviewdestroy - [optional] routine that frees reasonview context 4516c4421ceaSFande Kong (may be NULL) 4517c4421ceaSFande Kong 4518c4421ceaSFande Kong Options Database Keys: 4519c4421ceaSFande Kong + -snes_converged_reason - sets a default SNESConvergedReasonView() 4520c4421ceaSFande Kong - -snes_converged_reason_view_cancel - cancels all converged reason viewers that have 4521c4421ceaSFande Kong been hardwired into a code by 4522c4421ceaSFande Kong calls to SNESConvergedReasonViewSet(), but 4523c4421ceaSFande Kong does not cancel those set via 4524c4421ceaSFande Kong the options database. 4525c4421ceaSFande Kong 4526c4421ceaSFande Kong Notes: 4527c4421ceaSFande Kong Several different converged reason view routines may be set by calling 4528c4421ceaSFande Kong SNESConvergedReasonViewSet() multiple times; all will be called in the 4529c4421ceaSFande Kong order in which they were set. 4530c4421ceaSFande Kong 4531c4421ceaSFande Kong Level: intermediate 4532c4421ceaSFande Kong 4533db781477SPatrick Sanan .seealso: `SNESConvergedReasonView()`, `SNESConvergedReasonViewCancel()` 4534c4421ceaSFande Kong @*/ 4535c4421ceaSFande Kong PetscErrorCode SNESConvergedReasonViewSet(SNES snes,PetscErrorCode (*f)(SNES,void*),void *vctx,PetscErrorCode (*reasonviewdestroy)(void**)) 4536c4421ceaSFande Kong { 4537c4421ceaSFande Kong PetscInt i; 4538c4421ceaSFande Kong PetscBool identical; 4539c4421ceaSFande Kong 4540c4421ceaSFande Kong PetscFunctionBegin; 4541c4421ceaSFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4542c4421ceaSFande Kong for (i=0; i<snes->numberreasonviews;i++) { 45439566063dSJacob Faibussowitsch PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))f,vctx,reasonviewdestroy,(PetscErrorCode (*)(void))snes->reasonview[i],snes->reasonviewcontext[i],snes->reasonviewdestroy[i],&identical)); 4544c4421ceaSFande Kong if (identical) PetscFunctionReturn(0); 4545c4421ceaSFande Kong } 45465f80ce2aSJacob Faibussowitsch PetscCheck(snes->numberreasonviews < MAXSNESREASONVIEWS,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many SNES reasonview set"); 4547c4421ceaSFande Kong snes->reasonview[snes->numberreasonviews] = f; 4548c4421ceaSFande Kong snes->reasonviewdestroy[snes->numberreasonviews] = reasonviewdestroy; 4549c4421ceaSFande Kong snes->reasonviewcontext[snes->numberreasonviews++] = (void*)vctx; 4550c4421ceaSFande Kong PetscFunctionReturn(0); 4551c4421ceaSFande Kong } 4552c4421ceaSFande Kong 455391f3e32bSBarry Smith /*@ 455419a666eeSBarry Smith SNESConvergedReasonViewFromOptions - Processes command line options to determine if/how a SNESReason is to be viewed. 4555c4421ceaSFande Kong All the user-provided convergedReasonView routines will be involved as well, if they exist. 45562a359c20SBarry Smith 45572a359c20SBarry Smith Collective on SNES 45582a359c20SBarry Smith 45592a359c20SBarry Smith Input Parameters: 45602a359c20SBarry Smith . snes - the SNES object 45612a359c20SBarry Smith 45622a359c20SBarry Smith Level: intermediate 45632a359c20SBarry Smith 4564db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESSetUp()`, `SNESDestroy()`, `SNESSetTolerances()`, `SNESConvergedDefault()`, `SNESGetConvergedReason()`, `SNESConvergedReasonView()` 456519a666eeSBarry Smith 45662a359c20SBarry Smith @*/ 456719a666eeSBarry Smith PetscErrorCode SNESConvergedReasonViewFromOptions(SNES snes) 45682a359c20SBarry Smith { 45692a359c20SBarry Smith PetscViewer viewer; 45702a359c20SBarry Smith PetscBool flg; 45712a359c20SBarry Smith static PetscBool incall = PETSC_FALSE; 45722a359c20SBarry Smith PetscViewerFormat format; 4573c4421ceaSFande Kong PetscInt i; 45742a359c20SBarry Smith 45752a359c20SBarry Smith PetscFunctionBegin; 45762a359c20SBarry Smith if (incall) PetscFunctionReturn(0); 45772a359c20SBarry Smith incall = PETSC_TRUE; 4578c4421ceaSFande Kong 4579c4421ceaSFande Kong /* All user-provided viewers are called first, if they exist. */ 4580c4421ceaSFande Kong for (i=0; i<snes->numberreasonviews; i++) { 45819566063dSJacob Faibussowitsch PetscCall((*snes->reasonview[i])(snes,snes->reasonviewcontext[i])); 4582c4421ceaSFande Kong } 4583c4421ceaSFande Kong 4584c4421ceaSFande Kong /* Call PETSc default routine if users ask for it */ 45859566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_converged_reason",&viewer,&format,&flg)); 45862a359c20SBarry Smith if (flg) { 45879566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer,format)); 45889566063dSJacob Faibussowitsch PetscCall(SNESConvergedReasonView(snes,viewer)); 45899566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 45909566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 45912a359c20SBarry Smith } 45922a359c20SBarry Smith incall = PETSC_FALSE; 45932a359c20SBarry Smith PetscFunctionReturn(0); 45942a359c20SBarry Smith } 45952a359c20SBarry Smith 4596487a658cSBarry Smith /*@ 4597f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 4598f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 45999b94acceSBarry Smith 4600c7afd0dbSLois Curfman McInnes Collective on SNES 4601c7afd0dbSLois Curfman McInnes 4602b2002411SLois Curfman McInnes Input Parameters: 4603c7afd0dbSLois Curfman McInnes + snes - the SNES context 46040298fd71SBarry Smith . b - the constant part of the equation F(x) = b, or NULL to use zero. 460585385478SLisandro Dalcin - x - the solution vector. 46069b94acceSBarry Smith 4607b2002411SLois Curfman McInnes Notes: 46088ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 46096b7fb656SBarry Smith for the nonlinear solve prior to calling SNESSolve(). In particular, 46108ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 46118ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 46128ddd3da0SLois Curfman McInnes 461336851e7fSLois Curfman McInnes Level: beginner 461436851e7fSLois Curfman McInnes 4615db781477SPatrick Sanan .seealso: `SNESCreate()`, `SNESDestroy()`, `SNESSetFunction()`, `SNESSetJacobian()`, `SNESSetGridSequence()`, `SNESGetSolution()`, 4616db781477SPatrick Sanan `SNESNewtonTRSetPreCheck()`, `SNESNewtonTRGetPreCheck()`, `SNESNewtonTRSetPostCheck()`, `SNESNewtonTRGetPostCheck()`, 4617db781477SPatrick Sanan `SNESLineSearchSetPostCheck()`, `SNESLineSearchGetPostCheck()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchGetPreCheck()` 46189b94acceSBarry Smith @*/ 46197087cfbeSBarry Smith PetscErrorCode SNESSolve(SNES snes,Vec b,Vec x) 46209b94acceSBarry Smith { 4621ace3abfcSBarry Smith PetscBool flg; 4622efd51863SBarry Smith PetscInt grid; 46230298fd71SBarry Smith Vec xcreated = NULL; 4624caa4e7f2SJed Brown DM dm; 4625052efed2SBarry Smith 46263a40ed3dSBarry Smith PetscFunctionBegin; 46270700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4628a69afd8bSBarry Smith if (x) PetscValidHeaderSpecific(x,VEC_CLASSID,3); 4629a69afd8bSBarry Smith if (x) PetscCheckSameComm(snes,1,x,3); 46300700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 463185385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 463285385478SLisandro Dalcin 463334b4d3a8SMatthew G. Knepley /* High level operations using the nonlinear solver */ 463406fc46c8SMatthew G. Knepley { 463506fc46c8SMatthew G. Knepley PetscViewer viewer; 463606fc46c8SMatthew G. Knepley PetscViewerFormat format; 46377c88af5aSMatthew G. Knepley PetscInt num; 463806fc46c8SMatthew G. Knepley PetscBool flg; 463906fc46c8SMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 464006fc46c8SMatthew G. Knepley 464106fc46c8SMatthew G. Knepley if (!incall) { 464234b4d3a8SMatthew G. Knepley /* Estimate the convergence rate of the discretization */ 46439566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject) snes),((PetscObject)snes)->options, ((PetscObject) snes)->prefix, "-snes_convergence_estimate", &viewer, &format, &flg)); 464406fc46c8SMatthew G. Knepley if (flg) { 464506fc46c8SMatthew G. Knepley PetscConvEst conv; 464646079b62SMatthew G. Knepley DM dm; 464746079b62SMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 464846079b62SMatthew G. Knepley PetscInt Nf; 464906fc46c8SMatthew G. Knepley 465006fc46c8SMatthew G. Knepley incall = PETSC_TRUE; 46519566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 46529566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 46539566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(Nf, &alpha)); 46549566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject) snes), &conv)); 46559566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject) snes)); 46569566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 46579566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 46589566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 46599566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 46609566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 46619566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 46629566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 46639566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 46649566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 466506fc46c8SMatthew G. Knepley incall = PETSC_FALSE; 466606fc46c8SMatthew G. Knepley } 466734b4d3a8SMatthew G. Knepley /* Adaptively refine the initial grid */ 4668b2588ea6SMatthew G. Knepley num = 1; 46699566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL, ((PetscObject) snes)->prefix, "-snes_adapt_initial", &num, &flg)); 467034b4d3a8SMatthew G. Knepley if (flg) { 467134b4d3a8SMatthew G. Knepley DMAdaptor adaptor; 467234b4d3a8SMatthew G. Knepley 467334b4d3a8SMatthew G. Knepley incall = PETSC_TRUE; 46749566063dSJacob Faibussowitsch PetscCall(DMAdaptorCreate(PetscObjectComm((PetscObject)snes), &adaptor)); 46759566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetSolver(adaptor, snes)); 46769566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetSequenceLength(adaptor, num)); 46779566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetFromOptions(adaptor)); 46789566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetUp(adaptor)); 46799566063dSJacob Faibussowitsch PetscCall(DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_INITIAL, &dm, &x)); 46809566063dSJacob Faibussowitsch PetscCall(DMAdaptorDestroy(&adaptor)); 468134b4d3a8SMatthew G. Knepley incall = PETSC_FALSE; 468234b4d3a8SMatthew G. Knepley } 46837c88af5aSMatthew G. Knepley /* Use grid sequencing to adapt */ 46847c88af5aSMatthew G. Knepley num = 0; 46859566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL, ((PetscObject) snes)->prefix, "-snes_adapt_sequence", &num, NULL)); 46867c88af5aSMatthew G. Knepley if (num) { 46877c88af5aSMatthew G. Knepley DMAdaptor adaptor; 46887c88af5aSMatthew G. Knepley 46897c88af5aSMatthew G. Knepley incall = PETSC_TRUE; 46909566063dSJacob Faibussowitsch PetscCall(DMAdaptorCreate(PetscObjectComm((PetscObject)snes), &adaptor)); 46919566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetSolver(adaptor, snes)); 46929566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetSequenceLength(adaptor, num)); 46939566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetFromOptions(adaptor)); 46949566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetUp(adaptor)); 46959566063dSJacob Faibussowitsch PetscCall(DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_SEQUENTIAL, &dm, &x)); 46969566063dSJacob Faibussowitsch PetscCall(DMAdaptorDestroy(&adaptor)); 46977c88af5aSMatthew G. Knepley incall = PETSC_FALSE; 46987c88af5aSMatthew G. Knepley } 469906fc46c8SMatthew G. Knepley } 470006fc46c8SMatthew G. Knepley } 470141e867a5SStefano Zampini if (!x) { x = snes->vec_sol; } 4702caa4e7f2SJed Brown if (!x) { 47039566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 47049566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(dm,&xcreated)); 4705a69afd8bSBarry Smith x = xcreated; 4706a69afd8bSBarry Smith } 47079566063dSJacob Faibussowitsch PetscCall(SNESViewFromOptions(snes,NULL,"-snes_view_pre")); 4708f05ece33SBarry Smith 47099566063dSJacob Faibussowitsch for (grid=0; grid<snes->gridsequence; grid++) PetscCall(PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)))); 4710efd51863SBarry Smith for (grid=0; grid<snes->gridsequence+1; grid++) { 4711efd51863SBarry Smith 471285385478SLisandro Dalcin /* set solution vector */ 47139566063dSJacob Faibussowitsch if (!grid) PetscCall(PetscObjectReference((PetscObject)x)); 47149566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_sol)); 471585385478SLisandro Dalcin snes->vec_sol = x; 47169566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 4717caa4e7f2SJed Brown 4718caa4e7f2SJed Brown /* set affine vector if provided */ 47199566063dSJacob Faibussowitsch if (b) PetscCall(PetscObjectReference((PetscObject)b)); 47209566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_rhs)); 472185385478SLisandro Dalcin snes->vec_rhs = b; 472285385478SLisandro Dalcin 47235f80ce2aSJacob Faibussowitsch if (snes->vec_rhs) PetscCheck(snes->vec_func != snes->vec_rhs,PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Right hand side vector cannot be function vector"); 47245f80ce2aSJacob Faibussowitsch PetscCheck(snes->vec_func != snes->vec_sol,PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 47255f80ce2aSJacob Faibussowitsch PetscCheck(snes->vec_rhs != snes->vec_sol,PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector"); 4726154060b5SMatthew G. Knepley if (!snes->vec_sol_update /* && snes->vec_sol */) { 47279566063dSJacob Faibussowitsch PetscCall(VecDuplicate(snes->vec_sol,&snes->vec_sol_update)); 47289566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)snes,(PetscObject)snes->vec_sol_update)); 4729154060b5SMatthew G. Knepley } 47309566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm,snes->vec_sol)); 47319566063dSJacob Faibussowitsch PetscCall(SNESSetUp(snes)); 47323f149594SLisandro Dalcin 47337eee914bSBarry Smith if (!grid) { 4734*1baa6e33SBarry Smith if (snes->ops->computeinitialguess) PetscCall((*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP)); 4735dd568438SSatish Balay } 4736d25893d9SBarry Smith 4737abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 4738971e163fSPeter Brune if (snes->counters_reset) {snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;} 4739d5e45103SBarry Smith 47409566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_Solve,snes,0,0,0)); 47419566063dSJacob Faibussowitsch PetscCall((*snes->ops->solve)(snes)); 47429566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_Solve,snes,0,0,0)); 47435f80ce2aSJacob Faibussowitsch PetscCheck(snes->reason,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 4744422a814eSBarry Smith snes->domainerror = PETSC_FALSE; /* clear the flag if it has been set */ 47453f149594SLisandro Dalcin 474637ec4e1aSPeter Brune if (snes->lagjac_persist) snes->jac_iter += snes->iter; 474737ec4e1aSPeter Brune if (snes->lagpre_persist) snes->pre_iter += snes->iter; 474837ec4e1aSPeter Brune 47499566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_test_local_min",NULL,NULL,&flg)); 47509566063dSJacob Faibussowitsch if (flg && !PetscPreLoadingOn) PetscCall(SNESTestLocalMin(snes)); 4751c4421ceaSFande Kong /* Call converged reason views. This may involve user-provided viewers as well */ 47529566063dSJacob Faibussowitsch PetscCall(SNESConvergedReasonViewFromOptions(snes)); 47535968eb51SBarry Smith 47545f80ce2aSJacob Faibussowitsch if (snes->errorifnotconverged) PetscCheck(snes->reason >= 0,PetscObjectComm((PetscObject)snes),PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged"); 47559c8e83a9SBarry Smith if (snes->reason < 0) break; 4756efd51863SBarry Smith if (grid < snes->gridsequence) { 4757efd51863SBarry Smith DM fine; 4758efd51863SBarry Smith Vec xnew; 4759efd51863SBarry Smith Mat interp; 4760efd51863SBarry Smith 47619566063dSJacob Faibussowitsch PetscCall(DMRefine(snes->dm,PetscObjectComm((PetscObject)snes),&fine)); 47625f80ce2aSJacob Faibussowitsch PetscCheck(fine,PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_INCOMP,"DMRefine() did not perform any refinement, cannot continue grid sequencing"); 47639566063dSJacob Faibussowitsch PetscCall(DMCreateInterpolation(snes->dm,fine,&interp,NULL)); 47649566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(fine,&xnew)); 47659566063dSJacob Faibussowitsch PetscCall(MatInterpolate(interp,x,xnew)); 47669566063dSJacob Faibussowitsch PetscCall(DMInterpolate(snes->dm,interp,fine)); 47679566063dSJacob Faibussowitsch PetscCall(MatDestroy(&interp)); 4768efd51863SBarry Smith x = xnew; 4769efd51863SBarry Smith 47709566063dSJacob Faibussowitsch PetscCall(SNESReset(snes)); 47719566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes,fine)); 47729566063dSJacob Faibussowitsch PetscCall(SNESResetFromOptions(snes)); 47739566063dSJacob Faibussowitsch PetscCall(DMDestroy(&fine)); 47749566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)))); 4775efd51863SBarry Smith } 4776efd51863SBarry Smith } 47779566063dSJacob Faibussowitsch PetscCall(SNESViewFromOptions(snes,NULL,"-snes_view")); 47789566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(snes->vec_sol,(PetscObject)snes,"-snes_view_solution")); 47799566063dSJacob Faibussowitsch PetscCall(DMMonitor(snes->dm)); 47809566063dSJacob Faibussowitsch PetscCall(SNESMonitorPauseFinal_Internal(snes)); 47813f7e2da0SPeter Brune 47829566063dSJacob Faibussowitsch PetscCall(VecDestroy(&xcreated)); 47839566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)snes)); 47843a40ed3dSBarry Smith PetscFunctionReturn(0); 47859b94acceSBarry Smith } 47869b94acceSBarry Smith 47879b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 47889b94acceSBarry Smith 478982bf6240SBarry Smith /*@C 47904b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 47919b94acceSBarry Smith 4792fee21e36SBarry Smith Collective on SNES 4793fee21e36SBarry Smith 4794c7afd0dbSLois Curfman McInnes Input Parameters: 4795c7afd0dbSLois Curfman McInnes + snes - the SNES context 4796454a90a3SBarry Smith - type - a known method 4797c7afd0dbSLois Curfman McInnes 4798c7afd0dbSLois Curfman McInnes Options Database Key: 4799454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 480004d7464bSBarry Smith of available methods (for instance, newtonls or newtontr) 4801ae12b187SLois Curfman McInnes 48029b94acceSBarry Smith Notes: 4803e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 480404d7464bSBarry Smith + SNESNEWTONLS - Newton's method with line search 4805c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 4806a2b725a8SWilliam Gropp - SNESNEWTONTR - Newton's method with trust region 4807c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 48089b94acceSBarry Smith 4809ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 4810ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 4811ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 4812ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 4813ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 4814ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 4815ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 4816ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 4817ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 4818b0a32e0cSBarry Smith appropriate method. 481936851e7fSLois Curfman McInnes 482095452b02SPatrick Sanan Developer Notes: 482195452b02SPatrick Sanan SNESRegister() adds a constructor for a new SNESType to SNESList, SNESSetType() locates 48228f6c3df8SBarry Smith the constructor in that list and calls it to create the spexific object. 48238f6c3df8SBarry Smith 482436851e7fSLois Curfman McInnes Level: intermediate 4825a703fe33SLois Curfman McInnes 4826db781477SPatrick Sanan .seealso: `SNESType`, `SNESCreate()`, `SNESDestroy()`, `SNESGetType()`, `SNESSetFromOptions()` 4827435da068SBarry Smith 48289b94acceSBarry Smith @*/ 482919fd82e9SBarry Smith PetscErrorCode SNESSetType(SNES snes,SNESType type) 48309b94acceSBarry Smith { 4831ace3abfcSBarry Smith PetscBool match; 48325f80ce2aSJacob Faibussowitsch PetscErrorCode (*r)(SNES); 48333a40ed3dSBarry Smith 48343a40ed3dSBarry Smith PetscFunctionBegin; 48350700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 48364482741eSBarry Smith PetscValidCharPointer(type,2); 483782bf6240SBarry Smith 48389566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)snes,type,&match)); 48390f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 484092ff6ae8SBarry Smith 48419566063dSJacob Faibussowitsch PetscCall(PetscFunctionListFind(SNESList,type,&r)); 48425f80ce2aSJacob Faibussowitsch PetscCheck(r,PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 484375396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 4844a958fbfcSStefano Zampini if (snes->ops->destroy) PetscCall((*snes->ops->destroy)(snes)); 484575396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 48469e5d0892SLisandro Dalcin snes->ops->setup = NULL; 48479e5d0892SLisandro Dalcin snes->ops->solve = NULL; 48489e5d0892SLisandro Dalcin snes->ops->view = NULL; 48499e5d0892SLisandro Dalcin snes->ops->setfromoptions = NULL; 48509e5d0892SLisandro Dalcin snes->ops->destroy = NULL; 48517fe760d5SStefano Zampini 48527fe760d5SStefano Zampini /* It may happen the user has customized the line search before calling SNESSetType */ 48539566063dSJacob Faibussowitsch if (((PetscObject)snes)->type_name) PetscCall(SNESLineSearchDestroy(&snes->linesearch)); 48547fe760d5SStefano Zampini 485575396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 485675396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 4857f5af7f23SKarl Rupp 48589566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)snes,type)); 48599566063dSJacob Faibussowitsch PetscCall((*r)(snes)); 48603a40ed3dSBarry Smith PetscFunctionReturn(0); 48619b94acceSBarry Smith } 48629b94acceSBarry Smith 48639b94acceSBarry Smith /*@C 48649a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 48659b94acceSBarry Smith 4866c7afd0dbSLois Curfman McInnes Not Collective 4867c7afd0dbSLois Curfman McInnes 48689b94acceSBarry Smith Input Parameter: 48694b0e389bSBarry Smith . snes - nonlinear solver context 48709b94acceSBarry Smith 48719b94acceSBarry Smith Output Parameter: 48723a7fca6bSBarry Smith . type - SNES method (a character string) 48739b94acceSBarry Smith 487436851e7fSLois Curfman McInnes Level: intermediate 487536851e7fSLois Curfman McInnes 48769b94acceSBarry Smith @*/ 487719fd82e9SBarry Smith PetscErrorCode SNESGetType(SNES snes,SNESType *type) 48789b94acceSBarry Smith { 48793a40ed3dSBarry Smith PetscFunctionBegin; 48800700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 48814482741eSBarry Smith PetscValidPointer(type,2); 48827adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 48833a40ed3dSBarry Smith PetscFunctionReturn(0); 48849b94acceSBarry Smith } 48859b94acceSBarry Smith 48863cd8a7caSMatthew G. Knepley /*@ 48873cd8a7caSMatthew G. Knepley SNESSetSolution - Sets the solution vector for use by the SNES routines. 48883cd8a7caSMatthew G. Knepley 4889d083f849SBarry Smith Logically Collective on SNES 48903cd8a7caSMatthew G. Knepley 48913cd8a7caSMatthew G. Knepley Input Parameters: 48923cd8a7caSMatthew G. Knepley + snes - the SNES context obtained from SNESCreate() 48933cd8a7caSMatthew G. Knepley - u - the solution vector 48943cd8a7caSMatthew G. Knepley 48953cd8a7caSMatthew G. Knepley Level: beginner 48963cd8a7caSMatthew G. Knepley 48973cd8a7caSMatthew G. Knepley @*/ 48983cd8a7caSMatthew G. Knepley PetscErrorCode SNESSetSolution(SNES snes, Vec u) 48993cd8a7caSMatthew G. Knepley { 49003cd8a7caSMatthew G. Knepley DM dm; 49013cd8a7caSMatthew G. Knepley 49023cd8a7caSMatthew G. Knepley PetscFunctionBegin; 49033cd8a7caSMatthew G. Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 49043cd8a7caSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 49059566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) u)); 49069566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_sol)); 49073cd8a7caSMatthew G. Knepley 49083cd8a7caSMatthew G. Knepley snes->vec_sol = u; 49093cd8a7caSMatthew G. Knepley 49109566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 49119566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm, u)); 49123cd8a7caSMatthew G. Knepley PetscFunctionReturn(0); 49133cd8a7caSMatthew G. Knepley } 49143cd8a7caSMatthew G. Knepley 491552baeb72SSatish Balay /*@ 49169b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 4917c0df2a02SJed Brown stored. This is the fine grid solution when using SNESSetGridSequence(). 49189b94acceSBarry Smith 4919c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 4920c7afd0dbSLois Curfman McInnes 49219b94acceSBarry Smith Input Parameter: 49229b94acceSBarry Smith . snes - the SNES context 49239b94acceSBarry Smith 49249b94acceSBarry Smith Output Parameter: 49259b94acceSBarry Smith . x - the solution 49269b94acceSBarry Smith 492770e92668SMatthew Knepley Level: intermediate 492836851e7fSLois Curfman McInnes 4929db781477SPatrick Sanan .seealso: `SNESGetSolutionUpdate()`, `SNESGetFunction()` 49309b94acceSBarry Smith @*/ 49317087cfbeSBarry Smith PetscErrorCode SNESGetSolution(SNES snes,Vec *x) 49329b94acceSBarry Smith { 49333a40ed3dSBarry Smith PetscFunctionBegin; 49340700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 49354482741eSBarry Smith PetscValidPointer(x,2); 493685385478SLisandro Dalcin *x = snes->vec_sol; 493770e92668SMatthew Knepley PetscFunctionReturn(0); 493870e92668SMatthew Knepley } 493970e92668SMatthew Knepley 494052baeb72SSatish Balay /*@ 49419b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 49429b94acceSBarry Smith stored. 49439b94acceSBarry Smith 4944c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 4945c7afd0dbSLois Curfman McInnes 49469b94acceSBarry Smith Input Parameter: 49479b94acceSBarry Smith . snes - the SNES context 49489b94acceSBarry Smith 49499b94acceSBarry Smith Output Parameter: 49509b94acceSBarry Smith . x - the solution update 49519b94acceSBarry Smith 495236851e7fSLois Curfman McInnes Level: advanced 495336851e7fSLois Curfman McInnes 4954db781477SPatrick Sanan .seealso: `SNESGetSolution()`, `SNESGetFunction()` 49559b94acceSBarry Smith @*/ 49567087cfbeSBarry Smith PetscErrorCode SNESGetSolutionUpdate(SNES snes,Vec *x) 49579b94acceSBarry Smith { 49583a40ed3dSBarry Smith PetscFunctionBegin; 49590700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 49604482741eSBarry Smith PetscValidPointer(x,2); 496185385478SLisandro Dalcin *x = snes->vec_sol_update; 49623a40ed3dSBarry Smith PetscFunctionReturn(0); 49639b94acceSBarry Smith } 49649b94acceSBarry Smith 49659b94acceSBarry Smith /*@C 49663638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 49679b94acceSBarry Smith 4968a63bb30eSJed Brown Not Collective, but Vec is parallel if SNES is parallel. Collective if Vec is requested, but has not been created yet. 4969c7afd0dbSLois Curfman McInnes 49709b94acceSBarry Smith Input Parameter: 49719b94acceSBarry Smith . snes - the SNES context 49729b94acceSBarry Smith 4973d8d19677SJose E. Roman Output Parameters: 49740298fd71SBarry Smith + r - the vector that is used to store residuals (or NULL if you don't want it) 4975f8b49ee9SBarry Smith . f - the function (or NULL if you don't want it); see SNESFunction for calling sequence details 49760298fd71SBarry Smith - ctx - the function context (or NULL if you don't want it) 49779b94acceSBarry Smith 497836851e7fSLois Curfman McInnes Level: advanced 497936851e7fSLois Curfman McInnes 498004edfde5SBarry Smith Notes: The vector r DOES NOT, in general contain the current value of the SNES nonlinear function 498104edfde5SBarry Smith 4982db781477SPatrick Sanan .seealso: `SNESSetFunction()`, `SNESGetSolution()`, `SNESFunction` 49839b94acceSBarry Smith @*/ 4984f8b49ee9SBarry Smith PetscErrorCode SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**f)(SNES,Vec,Vec,void*),void **ctx) 49859b94acceSBarry Smith { 49866cab3a1bSJed Brown DM dm; 4987a63bb30eSJed Brown 49883a40ed3dSBarry Smith PetscFunctionBegin; 49890700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4990a63bb30eSJed Brown if (r) { 4991a63bb30eSJed Brown if (!snes->vec_func) { 4992a63bb30eSJed Brown if (snes->vec_rhs) { 49939566063dSJacob Faibussowitsch PetscCall(VecDuplicate(snes->vec_rhs,&snes->vec_func)); 4994a63bb30eSJed Brown } else if (snes->vec_sol) { 49959566063dSJacob Faibussowitsch PetscCall(VecDuplicate(snes->vec_sol,&snes->vec_func)); 4996a63bb30eSJed Brown } else if (snes->dm) { 49979566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(snes->dm,&snes->vec_func)); 4998a63bb30eSJed Brown } 4999a63bb30eSJed Brown } 5000a63bb30eSJed Brown *r = snes->vec_func; 5001a63bb30eSJed Brown } 50029566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 50039566063dSJacob Faibussowitsch PetscCall(DMSNESGetFunction(dm,f,ctx)); 50043a40ed3dSBarry Smith PetscFunctionReturn(0); 50059b94acceSBarry Smith } 50069b94acceSBarry Smith 5007c79ef259SPeter Brune /*@C 5008be95d8f1SBarry Smith SNESGetNGS - Returns the NGS function and context. 5009c79ef259SPeter Brune 5010c79ef259SPeter Brune Input Parameter: 5011c79ef259SPeter Brune . snes - the SNES context 5012c79ef259SPeter Brune 5013d8d19677SJose E. Roman Output Parameters: 5014be95d8f1SBarry Smith + f - the function (or NULL) see SNESNGSFunction for details 50150298fd71SBarry Smith - ctx - the function context (or NULL) 5016c79ef259SPeter Brune 5017c79ef259SPeter Brune Level: advanced 5018c79ef259SPeter Brune 5019db781477SPatrick Sanan .seealso: `SNESSetNGS()`, `SNESGetFunction()` 5020c79ef259SPeter Brune @*/ 5021c79ef259SPeter Brune 5022be95d8f1SBarry Smith PetscErrorCode SNESGetNGS (SNES snes, PetscErrorCode (**f)(SNES, Vec, Vec, void*), void ** ctx) 5023646217ecSPeter Brune { 50246cab3a1bSJed Brown DM dm; 50256cab3a1bSJed Brown 5026646217ecSPeter Brune PetscFunctionBegin; 5027646217ecSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50289566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes,&dm)); 50299566063dSJacob Faibussowitsch PetscCall(DMSNESGetNGS(dm,f,ctx)); 5030646217ecSPeter Brune PetscFunctionReturn(0); 5031646217ecSPeter Brune } 5032646217ecSPeter Brune 50333c7409f5SSatish Balay /*@C 50343c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 5035d850072dSLois Curfman McInnes SNES options in the database. 50363c7409f5SSatish Balay 50373f9fe445SBarry Smith Logically Collective on SNES 5038fee21e36SBarry Smith 5039d8d19677SJose E. Roman Input Parameters: 5040c7afd0dbSLois Curfman McInnes + snes - the SNES context 5041c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 5042c7afd0dbSLois Curfman McInnes 5043d850072dSLois Curfman McInnes Notes: 5044a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 5045c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 5046d850072dSLois Curfman McInnes 504736851e7fSLois Curfman McInnes Level: advanced 504836851e7fSLois Curfman McInnes 5049db781477SPatrick Sanan .seealso: `SNESSetFromOptions()` 50503c7409f5SSatish Balay @*/ 50517087cfbeSBarry Smith PetscErrorCode SNESSetOptionsPrefix(SNES snes,const char prefix[]) 50523c7409f5SSatish Balay { 50533a40ed3dSBarry Smith PetscFunctionBegin; 50540700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50559566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)snes,prefix)); 50569566063dSJacob Faibussowitsch if (!snes->ksp) PetscCall(SNESGetKSP(snes,&snes->ksp)); 505735f5d045SPeter Brune if (snes->linesearch) { 50589566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes,&snes->linesearch)); 50599566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)snes->linesearch,prefix)); 506035f5d045SPeter Brune } 50619566063dSJacob Faibussowitsch PetscCall(KSPSetOptionsPrefix(snes->ksp,prefix)); 50623a40ed3dSBarry Smith PetscFunctionReturn(0); 50633c7409f5SSatish Balay } 50643c7409f5SSatish Balay 50653c7409f5SSatish Balay /*@C 5066f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 5067d850072dSLois Curfman McInnes SNES options in the database. 50683c7409f5SSatish Balay 50693f9fe445SBarry Smith Logically Collective on SNES 5070fee21e36SBarry Smith 5071c7afd0dbSLois Curfman McInnes Input Parameters: 5072c7afd0dbSLois Curfman McInnes + snes - the SNES context 5073c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 5074c7afd0dbSLois Curfman McInnes 5075d850072dSLois Curfman McInnes Notes: 5076a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 5077c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 5078d850072dSLois Curfman McInnes 507936851e7fSLois Curfman McInnes Level: advanced 508036851e7fSLois Curfman McInnes 5081db781477SPatrick Sanan .seealso: `SNESGetOptionsPrefix()` 50823c7409f5SSatish Balay @*/ 50837087cfbeSBarry Smith PetscErrorCode SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 50843c7409f5SSatish Balay { 50853a40ed3dSBarry Smith PetscFunctionBegin; 50860700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 50879566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix)); 50889566063dSJacob Faibussowitsch if (!snes->ksp) PetscCall(SNESGetKSP(snes,&snes->ksp)); 508935f5d045SPeter Brune if (snes->linesearch) { 50909566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes,&snes->linesearch)); 50919566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)snes->linesearch,prefix)); 509235f5d045SPeter Brune } 50939566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(snes->ksp,prefix)); 50943a40ed3dSBarry Smith PetscFunctionReturn(0); 50953c7409f5SSatish Balay } 50963c7409f5SSatish Balay 50979ab63eb5SSatish Balay /*@C 50983c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 50993c7409f5SSatish Balay SNES options in the database. 51003c7409f5SSatish Balay 5101c7afd0dbSLois Curfman McInnes Not Collective 5102c7afd0dbSLois Curfman McInnes 51033c7409f5SSatish Balay Input Parameter: 51043c7409f5SSatish Balay . snes - the SNES context 51053c7409f5SSatish Balay 51063c7409f5SSatish Balay Output Parameter: 51073c7409f5SSatish Balay . prefix - pointer to the prefix string used 51083c7409f5SSatish Balay 510995452b02SPatrick Sanan Notes: 511095452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prefix' of 51119ab63eb5SSatish Balay sufficient length to hold the prefix. 51129ab63eb5SSatish Balay 511336851e7fSLois Curfman McInnes Level: advanced 511436851e7fSLois Curfman McInnes 5115db781477SPatrick Sanan .seealso: `SNESAppendOptionsPrefix()` 51163c7409f5SSatish Balay @*/ 51177087cfbeSBarry Smith PetscErrorCode SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 51183c7409f5SSatish Balay { 51193a40ed3dSBarry Smith PetscFunctionBegin; 51200700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 51219566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)snes,prefix)); 51223a40ed3dSBarry Smith PetscFunctionReturn(0); 51233c7409f5SSatish Balay } 51243c7409f5SSatish Balay 51253cea93caSBarry Smith /*@C 51261c84c290SBarry Smith SNESRegister - Adds a method to the nonlinear solver package. 51271c84c290SBarry Smith 51281c84c290SBarry Smith Not collective 51291c84c290SBarry Smith 51301c84c290SBarry Smith Input Parameters: 51311c84c290SBarry Smith + name_solver - name of a new user-defined solver 51321c84c290SBarry Smith - routine_create - routine to create method context 51331c84c290SBarry Smith 51341c84c290SBarry Smith Notes: 51351c84c290SBarry Smith SNESRegister() may be called multiple times to add several user-defined solvers. 51361c84c290SBarry Smith 51371c84c290SBarry Smith Sample usage: 51381c84c290SBarry Smith .vb 5139bdf89e91SBarry Smith SNESRegister("my_solver",MySolverCreate); 51401c84c290SBarry Smith .ve 51411c84c290SBarry Smith 51421c84c290SBarry Smith Then, your solver can be chosen with the procedural interface via 51431c84c290SBarry Smith $ SNESSetType(snes,"my_solver") 51441c84c290SBarry Smith or at runtime via the option 51451c84c290SBarry Smith $ -snes_type my_solver 51461c84c290SBarry Smith 51471c84c290SBarry Smith Level: advanced 51481c84c290SBarry Smith 51491c84c290SBarry Smith Note: If your function is not being put into a shared library then use SNESRegister() instead 51501c84c290SBarry Smith 5151db781477SPatrick Sanan .seealso: `SNESRegisterAll()`, `SNESRegisterDestroy()` 51523cea93caSBarry Smith 51537f6c08e0SMatthew Knepley Level: advanced 51543cea93caSBarry Smith @*/ 5155bdf89e91SBarry Smith PetscErrorCode SNESRegister(const char sname[],PetscErrorCode (*function)(SNES)) 5156b2002411SLois Curfman McInnes { 5157b2002411SLois Curfman McInnes PetscFunctionBegin; 51589566063dSJacob Faibussowitsch PetscCall(SNESInitializePackage()); 51599566063dSJacob Faibussowitsch PetscCall(PetscFunctionListAdd(&SNESList,sname,function)); 5160b2002411SLois Curfman McInnes PetscFunctionReturn(0); 5161b2002411SLois Curfman McInnes } 5162da9b6338SBarry Smith 51637087cfbeSBarry Smith PetscErrorCode SNESTestLocalMin(SNES snes) 5164da9b6338SBarry Smith { 516577431f27SBarry Smith PetscInt N,i,j; 5166da9b6338SBarry Smith Vec u,uh,fh; 5167da9b6338SBarry Smith PetscScalar value; 5168da9b6338SBarry Smith PetscReal norm; 5169da9b6338SBarry Smith 5170da9b6338SBarry Smith PetscFunctionBegin; 51719566063dSJacob Faibussowitsch PetscCall(SNESGetSolution(snes,&u)); 51729566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&uh)); 51739566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u,&fh)); 5174da9b6338SBarry Smith 5175da9b6338SBarry Smith /* currently only works for sequential */ 51769566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PetscObjectComm((PetscObject)snes),"Testing FormFunction() for local min\n")); 51779566063dSJacob Faibussowitsch PetscCall(VecGetSize(u,&N)); 5178da9b6338SBarry Smith for (i=0; i<N; i++) { 51799566063dSJacob Faibussowitsch PetscCall(VecCopy(u,uh)); 518063a3b9bcSJacob Faibussowitsch PetscCall(PetscPrintf(PetscObjectComm((PetscObject)snes),"i = %" PetscInt_FMT "\n",i)); 5181da9b6338SBarry Smith for (j=-10; j<11; j++) { 51828b49ba18SBarry Smith value = PetscSign(j)*PetscExpReal(PetscAbs(j)-10.0); 51839566063dSJacob Faibussowitsch PetscCall(VecSetValue(uh,i,value,ADD_VALUES)); 51849566063dSJacob Faibussowitsch PetscCall(SNESComputeFunction(snes,uh,fh)); 51859566063dSJacob Faibussowitsch PetscCall(VecNorm(fh,NORM_2,&norm)); 518663a3b9bcSJacob Faibussowitsch PetscCall(PetscPrintf(PetscObjectComm((PetscObject)snes)," j norm %" PetscInt_FMT " %18.16e\n",j,(double)norm)); 5187da9b6338SBarry Smith value = -value; 51889566063dSJacob Faibussowitsch PetscCall(VecSetValue(uh,i,value,ADD_VALUES)); 5189da9b6338SBarry Smith } 5190da9b6338SBarry Smith } 51919566063dSJacob Faibussowitsch PetscCall(VecDestroy(&uh)); 51929566063dSJacob Faibussowitsch PetscCall(VecDestroy(&fh)); 5193da9b6338SBarry Smith PetscFunctionReturn(0); 5194da9b6338SBarry Smith } 519571f87433Sdalcinl 519671f87433Sdalcinl /*@ 5197fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 519871f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 519971f87433Sdalcinl Newton method. 520071f87433Sdalcinl 52013f9fe445SBarry Smith Logically Collective on SNES 520271f87433Sdalcinl 520371f87433Sdalcinl Input Parameters: 520471f87433Sdalcinl + snes - SNES context 520571f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 520671f87433Sdalcinl 520764ba62caSBarry Smith Options Database: 520864ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 520964ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 521064ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 521164ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 521264ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 521364ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 521464ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 521564ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 521664ba62caSBarry Smith 521771f87433Sdalcinl Notes: 521871f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 521971f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 522071f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 522171f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 522271f87433Sdalcinl solver. 522371f87433Sdalcinl 522471f87433Sdalcinl Level: advanced 522571f87433Sdalcinl 522671f87433Sdalcinl Reference: 522771f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 522871f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 522971f87433Sdalcinl 5230db781477SPatrick Sanan .seealso: `SNESKSPGetUseEW()`, `SNESKSPGetParametersEW()`, `SNESKSPSetParametersEW()` 523171f87433Sdalcinl @*/ 52327087cfbeSBarry Smith PetscErrorCode SNESKSPSetUseEW(SNES snes,PetscBool flag) 523371f87433Sdalcinl { 523471f87433Sdalcinl PetscFunctionBegin; 52350700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5236acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flag,2); 523771f87433Sdalcinl snes->ksp_ewconv = flag; 523871f87433Sdalcinl PetscFunctionReturn(0); 523971f87433Sdalcinl } 524071f87433Sdalcinl 524171f87433Sdalcinl /*@ 5242fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 524371f87433Sdalcinl for computing relative tolerance for linear solvers within an 524471f87433Sdalcinl inexact Newton method. 524571f87433Sdalcinl 524671f87433Sdalcinl Not Collective 524771f87433Sdalcinl 524871f87433Sdalcinl Input Parameter: 524971f87433Sdalcinl . snes - SNES context 525071f87433Sdalcinl 525171f87433Sdalcinl Output Parameter: 525271f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 525371f87433Sdalcinl 525471f87433Sdalcinl Notes: 525571f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 525671f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 525771f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 525871f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 525971f87433Sdalcinl solver. 526071f87433Sdalcinl 526171f87433Sdalcinl Level: advanced 526271f87433Sdalcinl 526371f87433Sdalcinl Reference: 526471f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 526571f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 526671f87433Sdalcinl 5267db781477SPatrick Sanan .seealso: `SNESKSPSetUseEW()`, `SNESKSPGetParametersEW()`, `SNESKSPSetParametersEW()` 526871f87433Sdalcinl @*/ 52697087cfbeSBarry Smith PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 527071f87433Sdalcinl { 527171f87433Sdalcinl PetscFunctionBegin; 52720700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5273534a8f05SLisandro Dalcin PetscValidBoolPointer(flag,2); 527471f87433Sdalcinl *flag = snes->ksp_ewconv; 527571f87433Sdalcinl PetscFunctionReturn(0); 527671f87433Sdalcinl } 527771f87433Sdalcinl 527871f87433Sdalcinl /*@ 5279fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 528071f87433Sdalcinl convergence criteria for the linear solvers within an inexact 528171f87433Sdalcinl Newton method. 528271f87433Sdalcinl 52833f9fe445SBarry Smith Logically Collective on SNES 528471f87433Sdalcinl 528571f87433Sdalcinl Input Parameters: 528671f87433Sdalcinl + snes - SNES context 52870f0abf79SStefano Zampini . version - version 1, 2 (default is 2), 3 or 4 528871f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 528971f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 529071f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 529171f87433Sdalcinl (0 <= gamma2 <= 1) 529271f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 529371f87433Sdalcinl . alpha2 - power for safeguard 529471f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 529571f87433Sdalcinl 529671f87433Sdalcinl Note: 529771f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 529871f87433Sdalcinl 529971f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 530071f87433Sdalcinl 530171f87433Sdalcinl Level: advanced 530271f87433Sdalcinl 530371f87433Sdalcinl Reference: 530471f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 530571f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 530671f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 530771f87433Sdalcinl 5308db781477SPatrick Sanan .seealso: `SNESKSPSetUseEW()`, `SNESKSPGetUseEW()`, `SNESKSPGetParametersEW()` 530971f87433Sdalcinl @*/ 5310f5af7f23SKarl Rupp PetscErrorCode SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 531171f87433Sdalcinl { 5312fa9f3622SBarry Smith SNESKSPEW *kctx; 53135fd66863SKarl Rupp 531471f87433Sdalcinl PetscFunctionBegin; 53150700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5316fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 53175f80ce2aSJacob Faibussowitsch PetscCheck(kctx,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 5318c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,version,2); 5319c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_0,3); 5320c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_max,4); 5321c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,gamma,5); 5322c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha,6); 5323c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha2,7); 5324c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,threshold,8); 532571f87433Sdalcinl 532671f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 532771f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 532871f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 532971f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 533071f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 533171f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 533271f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 533371f87433Sdalcinl 53340f0abf79SStefano Zampini PetscCheck(kctx->version >= 1 && kctx->version <= 4,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1 to 4 are supported: %" PetscInt_FMT,kctx->version); 53350b121fc5SBarry Smith PetscCheck(kctx->rtol_0 >= 0.0 && kctx->rtol_0 < 1.0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %g",(double)kctx->rtol_0); 53360b121fc5SBarry Smith PetscCheck(kctx->rtol_max >= 0.0 && kctx->rtol_max < 1.0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%g) < 1.0",(double)kctx->rtol_max); 53370b121fc5SBarry Smith PetscCheck(kctx->gamma >= 0.0 && kctx->gamma <= 1.0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%g) <= 1.0",(double)kctx->gamma); 53380b121fc5SBarry Smith PetscCheck(kctx->alpha > 1.0 && kctx->alpha <= 2.0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%g) <= 2.0",(double)kctx->alpha); 53390b121fc5SBarry Smith PetscCheck(kctx->threshold > 0.0 && kctx->threshold < 1.0,PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%g) < 1.0",(double)kctx->threshold); 534071f87433Sdalcinl PetscFunctionReturn(0); 534171f87433Sdalcinl } 534271f87433Sdalcinl 534371f87433Sdalcinl /*@ 5344fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 534571f87433Sdalcinl convergence criteria for the linear solvers within an inexact 534671f87433Sdalcinl Newton method. 534771f87433Sdalcinl 534871f87433Sdalcinl Not Collective 534971f87433Sdalcinl 535097bb3fdcSJose E. Roman Input Parameter: 53516b867d5aSJose E. Roman . snes - SNES context 535271f87433Sdalcinl 535371f87433Sdalcinl Output Parameters: 53540f0abf79SStefano Zampini + version - version 1, 2 (default is 2), 3 or 4 535571f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 535671f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 5357bf388a1fSBarry Smith . gamma - multiplicative factor for version 2 rtol computation (0 <= gamma2 <= 1) 535871f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 535971f87433Sdalcinl . alpha2 - power for safeguard 536071f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 536171f87433Sdalcinl 536271f87433Sdalcinl Level: advanced 536371f87433Sdalcinl 5364db781477SPatrick Sanan .seealso: `SNESKSPSetUseEW()`, `SNESKSPGetUseEW()`, `SNESKSPSetParametersEW()` 536571f87433Sdalcinl @*/ 5366bf388a1fSBarry Smith PetscErrorCode SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 536771f87433Sdalcinl { 5368fa9f3622SBarry Smith SNESKSPEW *kctx; 53695fd66863SKarl Rupp 537071f87433Sdalcinl PetscFunctionBegin; 53710700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5372fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 53735f80ce2aSJacob Faibussowitsch PetscCheck(kctx,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 537471f87433Sdalcinl if (version) *version = kctx->version; 537571f87433Sdalcinl if (rtol_0) *rtol_0 = kctx->rtol_0; 537671f87433Sdalcinl if (rtol_max) *rtol_max = kctx->rtol_max; 537771f87433Sdalcinl if (gamma) *gamma = kctx->gamma; 537871f87433Sdalcinl if (alpha) *alpha = kctx->alpha; 537971f87433Sdalcinl if (alpha2) *alpha2 = kctx->alpha2; 538071f87433Sdalcinl if (threshold) *threshold = kctx->threshold; 538171f87433Sdalcinl PetscFunctionReturn(0); 538271f87433Sdalcinl } 538371f87433Sdalcinl 5384d5378b5fSDmitry Karpeev PetscErrorCode KSPPreSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes) 538571f87433Sdalcinl { 5386fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 538771f87433Sdalcinl PetscReal rtol = PETSC_DEFAULT,stol; 538871f87433Sdalcinl 538971f87433Sdalcinl PetscFunctionBegin; 5390d4211eb9SBarry Smith if (!snes->ksp_ewconv) PetscFunctionReturn(0); 539130058271SDmitry Karpeev if (!snes->iter) { 539230058271SDmitry Karpeev rtol = kctx->rtol_0; /* first time in, so use the original user rtol */ 53939566063dSJacob Faibussowitsch PetscCall(VecNorm(snes->vec_func,NORM_2,&kctx->norm_first)); 53940f0abf79SStefano Zampini } else { 539571f87433Sdalcinl if (kctx->version == 1) { 53960f0abf79SStefano Zampini rtol = PetscAbsReal(snes->norm - kctx->lresid_last)/kctx->norm_last; 539785ec1a3cSBarry Smith stol = PetscPowReal(kctx->rtol_last,kctx->alpha2); 539871f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 539971f87433Sdalcinl } else if (kctx->version == 2) { 540085ec1a3cSBarry Smith rtol = kctx->gamma * PetscPowReal(snes->norm/kctx->norm_last,kctx->alpha); 540185ec1a3cSBarry Smith stol = kctx->gamma * PetscPowReal(kctx->rtol_last,kctx->alpha); 540271f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 540371f87433Sdalcinl } else if (kctx->version == 3) { /* contributed by Luis Chacon, June 2006. */ 540485ec1a3cSBarry Smith rtol = kctx->gamma * PetscPowReal(snes->norm/kctx->norm_last,kctx->alpha); 540571f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 540685ec1a3cSBarry Smith stol = kctx->gamma*PetscPowReal(kctx->rtol_last,kctx->alpha); 540771f87433Sdalcinl stol = PetscMax(rtol,stol); 540871f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 540971f87433Sdalcinl /* safeguard: avoid oversolving */ 541030058271SDmitry Karpeev stol = kctx->gamma*(kctx->norm_first*snes->rtol)/snes->norm; 541171f87433Sdalcinl stol = PetscMax(rtol,stol); 541271f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 54130f0abf79SStefano Zampini } else if (kctx->version == 4) { /* H.-B. An et al. Journal of Computational and Applied Mathematics 200 (2007) 47-60 */ 54140f0abf79SStefano Zampini PetscReal ared = PetscAbsReal(kctx->norm_last - snes->norm); 54150f0abf79SStefano Zampini PetscReal pred = PetscAbsReal(kctx->norm_last - kctx->lresid_last); 54160f0abf79SStefano Zampini PetscReal rk = ared / pred; 54170f0abf79SStefano Zampini if (rk < kctx->v4_p1) rtol = 1. - 2.*kctx->v4_p1; 54180f0abf79SStefano Zampini else if (rk < kctx->v4_p2) rtol = kctx->rtol_last; 54190f0abf79SStefano Zampini else if (rk < kctx->v4_p3) rtol = kctx->v4_m1 * kctx->rtol_last; 54200f0abf79SStefano Zampini else rtol = kctx->v4_m2 * kctx->rtol_last; 54210f0abf79SStefano Zampini 54220f0abf79SStefano Zampini if (kctx->rtol_last_2 > kctx->v4_m3 && kctx->rtol_last > kctx->v4_m3 && 54230f0abf79SStefano Zampini kctx->rk_last_2 < kctx->v4_p1 && kctx->rk_last < kctx->v4_p1) { 54240f0abf79SStefano Zampini rtol = kctx->v4_m4 * kctx->rtol_last; 54250f0abf79SStefano Zampini //printf("iter %" PetscInt_FMT ", Eisenstat-Walker (version %" PetscInt_FMT ") KSP rtol=%g (rk %g ps %g %g %g) (AD)\n",snes->iter,kctx->version,(double)rtol,rk,kctx->v4_p1,kctx->v4_p2,kctx->v4_p3); 54260f0abf79SStefano Zampini } else { 54270f0abf79SStefano Zampini //printf("iter %" PetscInt_FMT ", Eisenstat-Walker (version %" PetscInt_FMT ") KSP rtol=%g (rk %g ps %g %g %g)\n",snes->iter,kctx->version,(double)rtol,rk,kctx->v4_p1,kctx->v4_p2,kctx->v4_p3); 542871f87433Sdalcinl } 54290f0abf79SStefano Zampini kctx->rtol_last_2 = kctx->rtol_last; 54300f0abf79SStefano Zampini kctx->rk_last_2 = kctx->rk_last; 54310f0abf79SStefano Zampini kctx->rk_last = rk; 54320f0abf79SStefano Zampini } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1-4 are supported: %" PetscInt_FMT,kctx->version); 54330f0abf79SStefano Zampini } 54340f0abf79SStefano Zampini /* safeguard: avoid rtol greater than rtol_max */ 543571f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 54369566063dSJacob Faibussowitsch PetscCall(KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT)); 543763a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes,"iter %" PetscInt_FMT ", Eisenstat-Walker (version %" PetscInt_FMT ") KSP rtol=%g\n",snes->iter,kctx->version,(double)rtol)); 543871f87433Sdalcinl PetscFunctionReturn(0); 543971f87433Sdalcinl } 544071f87433Sdalcinl 5441d5378b5fSDmitry Karpeev PetscErrorCode KSPPostSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes) 544271f87433Sdalcinl { 5443fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 544471f87433Sdalcinl PCSide pcside; 544571f87433Sdalcinl Vec lres; 544671f87433Sdalcinl 544771f87433Sdalcinl PetscFunctionBegin; 5448d4211eb9SBarry Smith if (!snes->ksp_ewconv) PetscFunctionReturn(0); 54499566063dSJacob Faibussowitsch PetscCall(KSPGetTolerances(ksp,&kctx->rtol_last,NULL,NULL,NULL)); 545071dbe336SPeter Brune kctx->norm_last = snes->norm; 54510f0abf79SStefano Zampini if (kctx->version == 1 || kctx->version == 4) { 54524f00ce20SMatthew G. Knepley PC pc; 54530f0abf79SStefano Zampini PetscBool getRes; 54544f00ce20SMatthew G. Knepley 54559566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp,&pc)); 54560f0abf79SStefano Zampini PetscCall(PetscObjectTypeCompare((PetscObject)pc,PCNONE,&getRes)); 54570f0abf79SStefano Zampini if (!getRes) { 54580f0abf79SStefano Zampini KSPNormType normtype; 54590f0abf79SStefano Zampini 54600f0abf79SStefano Zampini PetscCall(KSPGetNormType(ksp,&normtype)); 54610f0abf79SStefano Zampini getRes = (PetscBool)(normtype == KSP_NORM_UNPRECONDITIONED); 54620f0abf79SStefano Zampini } 54639566063dSJacob Faibussowitsch PetscCall(KSPGetPCSide(ksp,&pcside)); 54640f0abf79SStefano Zampini if (pcside == PC_RIGHT || getRes) { /* KSP residual is true linear residual */ 54659566063dSJacob Faibussowitsch PetscCall(KSPGetResidualNorm(ksp,&kctx->lresid_last)); 546671f87433Sdalcinl } else { 546771f87433Sdalcinl /* KSP residual is preconditioned residual */ 546871f87433Sdalcinl /* compute true linear residual norm */ 54690f0abf79SStefano Zampini Mat J; 54700f0abf79SStefano Zampini PetscCall(KSPGetOperators(ksp,&J,NULL)); 54719566063dSJacob Faibussowitsch PetscCall(VecDuplicate(b,&lres)); 54720f0abf79SStefano Zampini PetscCall(MatMult(J,x,lres)); 54739566063dSJacob Faibussowitsch PetscCall(VecAYPX(lres,-1.0,b)); 54749566063dSJacob Faibussowitsch PetscCall(VecNorm(lres,NORM_2,&kctx->lresid_last)); 54759566063dSJacob Faibussowitsch PetscCall(VecDestroy(&lres)); 547671f87433Sdalcinl } 547771f87433Sdalcinl } 547871f87433Sdalcinl PetscFunctionReturn(0); 547971f87433Sdalcinl } 548071f87433Sdalcinl 5481d4211eb9SBarry Smith /*@ 5482d4211eb9SBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 5483d4211eb9SBarry Smith 5484d4211eb9SBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 5485d4211eb9SBarry Smith 5486d4211eb9SBarry Smith Input Parameter: 5487d4211eb9SBarry Smith . snes - the SNES context 5488d4211eb9SBarry Smith 5489d4211eb9SBarry Smith Output Parameter: 5490d4211eb9SBarry Smith . ksp - the KSP context 5491d4211eb9SBarry Smith 5492d4211eb9SBarry Smith Notes: 5493d4211eb9SBarry Smith The user can then directly manipulate the KSP context to set various 5494d4211eb9SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 5495d4211eb9SBarry Smith PC contexts as well. 5496d4211eb9SBarry Smith 5497d4211eb9SBarry Smith Level: beginner 5498d4211eb9SBarry Smith 5499db781477SPatrick Sanan .seealso: `KSPGetPC()`, `SNESCreate()`, `KSPCreate()`, `SNESSetKSP()` 5500d4211eb9SBarry Smith @*/ 5501d4211eb9SBarry Smith PetscErrorCode SNESGetKSP(SNES snes,KSP *ksp) 550271f87433Sdalcinl { 550371f87433Sdalcinl PetscFunctionBegin; 5504d4211eb9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5505d4211eb9SBarry Smith PetscValidPointer(ksp,2); 5506d4211eb9SBarry Smith 5507d4211eb9SBarry Smith if (!snes->ksp) { 55089566063dSJacob Faibussowitsch PetscCall(KSPCreate(PetscObjectComm((PetscObject)snes),&snes->ksp)); 55099566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1)); 55109566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)snes,(PetscObject)snes->ksp)); 5511d4211eb9SBarry Smith 55129566063dSJacob Faibussowitsch PetscCall(KSPSetPreSolve(snes->ksp,(PetscErrorCode (*)(KSP,Vec,Vec,void*))KSPPreSolve_SNESEW,snes)); 55139566063dSJacob Faibussowitsch PetscCall(KSPSetPostSolve(snes->ksp,(PetscErrorCode (*)(KSP,Vec,Vec,void*))KSPPostSolve_SNESEW,snes)); 5514a5c2985bSBarry Smith 55159566063dSJacob Faibussowitsch PetscCall(KSPMonitorSetFromOptions(snes->ksp, "-snes_monitor_ksp", "snes_preconditioned_residual", snes)); 55169566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptions((PetscObject)snes->ksp,((PetscObject)snes)->options)); 5517d4211eb9SBarry Smith } 5518d4211eb9SBarry Smith *ksp = snes->ksp; 551971f87433Sdalcinl PetscFunctionReturn(0); 552071f87433Sdalcinl } 55216c699258SBarry Smith 5522af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 55236c699258SBarry Smith /*@ 55242a808120SBarry Smith SNESSetDM - Sets the DM that may be used by some nonlinear solvers or their underlying preconditioners 55256c699258SBarry Smith 55263f9fe445SBarry Smith Logically Collective on SNES 55276c699258SBarry Smith 55286c699258SBarry Smith Input Parameters: 55292a808120SBarry Smith + snes - the nonlinear solver context 55302a808120SBarry Smith - dm - the dm, cannot be NULL 55316c699258SBarry Smith 5532e03a659cSJed Brown Notes: 5533e03a659cSJed Brown A DM can only be used for solving one problem at a time because information about the problem is stored on the DM, 5534e03a659cSJed Brown even when not using interfaces like DMSNESSetFunction(). Use DMClone() to get a distinct DM when solving different 5535e03a659cSJed Brown problems using the same function space. 5536e03a659cSJed Brown 55376c699258SBarry Smith Level: intermediate 55386c699258SBarry Smith 5539db781477SPatrick Sanan .seealso: `SNESGetDM()`, `KSPSetDM()`, `KSPGetDM()` 55406c699258SBarry Smith @*/ 55417087cfbeSBarry Smith PetscErrorCode SNESSetDM(SNES snes,DM dm) 55426c699258SBarry Smith { 5543345fed2cSBarry Smith KSP ksp; 5544942e3340SBarry Smith DMSNES sdm; 55456c699258SBarry Smith 55466c699258SBarry Smith PetscFunctionBegin; 55470700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 55482a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 55499566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 5550942e3340SBarry Smith if (snes->dm) { /* Move the DMSNES context over to the new DM unless the new DM already has one */ 555151f4b3c7SToby Isaac if (snes->dm->dmsnes && !dm->dmsnes) { 55529566063dSJacob Faibussowitsch PetscCall(DMCopyDMSNES(snes->dm,dm)); 55539566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(snes->dm,&sdm)); 5554f5af7f23SKarl Rupp if (sdm->originaldm == snes->dm) sdm->originaldm = dm; /* Grant write privileges to the replacement DM */ 55556cab3a1bSJed Brown } 55569566063dSJacob Faibussowitsch PetscCall(DMCoarsenHookRemove(snes->dm,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,snes)); 55579566063dSJacob Faibussowitsch PetscCall(DMDestroy(&snes->dm)); 55586cab3a1bSJed Brown } 55596c699258SBarry Smith snes->dm = dm; 5560116d1032SJed Brown snes->dmAuto = PETSC_FALSE; 5561f5af7f23SKarl Rupp 55629566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes,&ksp)); 55639566063dSJacob Faibussowitsch PetscCall(KSPSetDM(ksp,dm)); 55649566063dSJacob Faibussowitsch PetscCall(KSPSetDMActive(ksp,PETSC_FALSE)); 5565efd4aadfSBarry Smith if (snes->npc) { 55669566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes->npc,snes->dm)); 55679566063dSJacob Faibussowitsch PetscCall(SNESSetNPCSide(snes,snes->npcside)); 55682c155ee1SBarry Smith } 55696c699258SBarry Smith PetscFunctionReturn(0); 55706c699258SBarry Smith } 55716c699258SBarry Smith 55726c699258SBarry Smith /*@ 55736c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 55746c699258SBarry Smith 55753f9fe445SBarry Smith Not Collective but DM obtained is parallel on SNES 55766c699258SBarry Smith 55776c699258SBarry Smith Input Parameter: 55786c699258SBarry Smith . snes - the preconditioner context 55796c699258SBarry Smith 55806c699258SBarry Smith Output Parameter: 55816c699258SBarry Smith . dm - the dm 55826c699258SBarry Smith 55836c699258SBarry Smith Level: intermediate 55846c699258SBarry Smith 5585db781477SPatrick Sanan .seealso: `SNESSetDM()`, `KSPSetDM()`, `KSPGetDM()` 55866c699258SBarry Smith @*/ 55877087cfbeSBarry Smith PetscErrorCode SNESGetDM(SNES snes,DM *dm) 55886c699258SBarry Smith { 55896c699258SBarry Smith PetscFunctionBegin; 55900700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 55916cab3a1bSJed Brown if (!snes->dm) { 55929566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)snes),&snes->dm)); 5593116d1032SJed Brown snes->dmAuto = PETSC_TRUE; 55946cab3a1bSJed Brown } 55956c699258SBarry Smith *dm = snes->dm; 55966c699258SBarry Smith PetscFunctionReturn(0); 55976c699258SBarry Smith } 55980807856dSBarry Smith 559931823bd8SMatthew G Knepley /*@ 5600be95d8f1SBarry Smith SNESSetNPC - Sets the nonlinear preconditioner to be used. 560131823bd8SMatthew G Knepley 560231823bd8SMatthew G Knepley Collective on SNES 560331823bd8SMatthew G Knepley 560431823bd8SMatthew G Knepley Input Parameters: 560531823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate() 560631823bd8SMatthew G Knepley - pc - the preconditioner object 560731823bd8SMatthew G Knepley 560831823bd8SMatthew G Knepley Notes: 5609be95d8f1SBarry Smith Use SNESGetNPC() to retrieve the preconditioner context (for example, 561031823bd8SMatthew G Knepley to configure it using the API). 561131823bd8SMatthew G Knepley 561231823bd8SMatthew G Knepley Level: developer 561331823bd8SMatthew G Knepley 5614db781477SPatrick Sanan .seealso: `SNESGetNPC()`, `SNESHasNPC()` 561531823bd8SMatthew G Knepley @*/ 5616be95d8f1SBarry Smith PetscErrorCode SNESSetNPC(SNES snes, SNES pc) 561731823bd8SMatthew G Knepley { 561831823bd8SMatthew G Knepley PetscFunctionBegin; 561931823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 562031823bd8SMatthew G Knepley PetscValidHeaderSpecific(pc, SNES_CLASSID, 2); 562131823bd8SMatthew G Knepley PetscCheckSameComm(snes, 1, pc, 2); 56229566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) pc)); 56239566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&snes->npc)); 5624efd4aadfSBarry Smith snes->npc = pc; 56259566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)snes, (PetscObject)snes->npc)); 562631823bd8SMatthew G Knepley PetscFunctionReturn(0); 562731823bd8SMatthew G Knepley } 562831823bd8SMatthew G Knepley 562931823bd8SMatthew G Knepley /*@ 5630be95d8f1SBarry Smith SNESGetNPC - Creates a nonlinear preconditioning solver (SNES) to be used to precondition the nonlinear solver. 563131823bd8SMatthew G Knepley 5632951fe5abSBarry Smith Not Collective; but any changes to the obtained SNES object must be applied collectively 563331823bd8SMatthew G Knepley 563431823bd8SMatthew G Knepley Input Parameter: 563531823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate() 563631823bd8SMatthew G Knepley 563731823bd8SMatthew G Knepley Output Parameter: 563831823bd8SMatthew G Knepley . pc - preconditioner context 563931823bd8SMatthew G Knepley 5640b5badacbSBarry Smith Options Database: 5641b5badacbSBarry Smith . -npc_snes_type <type> - set the type of the SNES to use as the nonlinear preconditioner 5642b5badacbSBarry Smith 564395452b02SPatrick Sanan Notes: 5644b5badacbSBarry Smith If a SNES was previously set with SNESSetNPC() then that SNES is returned, otherwise a new SNES object is created. 5645be95d8f1SBarry Smith 5646951fe5abSBarry Smith The (preconditioner) SNES returned automatically inherits the same nonlinear function and Jacobian supplied to the original 5647951fe5abSBarry Smith SNES during SNESSetUp() 5648951fe5abSBarry Smith 564931823bd8SMatthew G Knepley Level: developer 565031823bd8SMatthew G Knepley 5651db781477SPatrick Sanan .seealso: `SNESSetNPC()`, `SNESHasNPC()`, `SNES`, `SNESCreate()` 565231823bd8SMatthew G Knepley @*/ 5653be95d8f1SBarry Smith PetscErrorCode SNESGetNPC(SNES snes, SNES *pc) 565431823bd8SMatthew G Knepley { 5655a64e098fSPeter Brune const char *optionsprefix; 565631823bd8SMatthew G Knepley 565731823bd8SMatthew G Knepley PetscFunctionBegin; 565831823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 565931823bd8SMatthew G Knepley PetscValidPointer(pc, 2); 5660efd4aadfSBarry Smith if (!snes->npc) { 56619566063dSJacob Faibussowitsch PetscCall(SNESCreate(PetscObjectComm((PetscObject)snes),&snes->npc)); 56629566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)snes->npc,(PetscObject)snes,1)); 56639566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)snes,(PetscObject)snes->npc)); 56649566063dSJacob Faibussowitsch PetscCall(SNESGetOptionsPrefix(snes,&optionsprefix)); 56659566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes->npc,optionsprefix)); 56669566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes->npc,"npc_")); 56679566063dSJacob Faibussowitsch PetscCall(SNESSetCountersReset(snes->npc,PETSC_FALSE)); 566831823bd8SMatthew G Knepley } 5669efd4aadfSBarry Smith *pc = snes->npc; 567031823bd8SMatthew G Knepley PetscFunctionReturn(0); 567131823bd8SMatthew G Knepley } 567231823bd8SMatthew G Knepley 56733ad1a0b9SPatrick Farrell /*@ 56743ad1a0b9SPatrick Farrell SNESHasNPC - Returns whether a nonlinear preconditioner exists 56753ad1a0b9SPatrick Farrell 56763ad1a0b9SPatrick Farrell Not Collective 56773ad1a0b9SPatrick Farrell 56783ad1a0b9SPatrick Farrell Input Parameter: 56793ad1a0b9SPatrick Farrell . snes - iterative context obtained from SNESCreate() 56803ad1a0b9SPatrick Farrell 56813ad1a0b9SPatrick Farrell Output Parameter: 56823ad1a0b9SPatrick Farrell . has_npc - whether the SNES has an NPC or not 56833ad1a0b9SPatrick Farrell 56843ad1a0b9SPatrick Farrell Level: developer 56853ad1a0b9SPatrick Farrell 5686db781477SPatrick Sanan .seealso: `SNESSetNPC()`, `SNESGetNPC()` 56873ad1a0b9SPatrick Farrell @*/ 56883ad1a0b9SPatrick Farrell PetscErrorCode SNESHasNPC(SNES snes, PetscBool *has_npc) 56893ad1a0b9SPatrick Farrell { 56903ad1a0b9SPatrick Farrell PetscFunctionBegin; 56913ad1a0b9SPatrick Farrell PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5692efd4aadfSBarry Smith *has_npc = (PetscBool) (snes->npc ? PETSC_TRUE : PETSC_FALSE); 56933ad1a0b9SPatrick Farrell PetscFunctionReturn(0); 56943ad1a0b9SPatrick Farrell } 56953ad1a0b9SPatrick Farrell 5696c40d0f55SPeter Brune /*@ 5697be95d8f1SBarry Smith SNESSetNPCSide - Sets the preconditioning side. 5698c40d0f55SPeter Brune 5699c40d0f55SPeter Brune Logically Collective on SNES 5700c40d0f55SPeter Brune 5701c40d0f55SPeter Brune Input Parameter: 5702c40d0f55SPeter Brune . snes - iterative context obtained from SNESCreate() 5703c40d0f55SPeter Brune 5704c40d0f55SPeter Brune Output Parameter: 5705c40d0f55SPeter Brune . side - the preconditioning side, where side is one of 5706c40d0f55SPeter Brune .vb 57072d547940SBarry Smith PC_LEFT - left preconditioning 57082d547940SBarry Smith PC_RIGHT - right preconditioning (default for most nonlinear solvers) 5709c40d0f55SPeter Brune .ve 5710c40d0f55SPeter Brune 5711c40d0f55SPeter Brune Options Database Keys: 571267b8a455SSatish Balay . -snes_npc_side <right,left> - nonlinear preconditioner side 5713c40d0f55SPeter Brune 571495452b02SPatrick Sanan Notes: 571595452b02SPatrick Sanan SNESNRICHARDSON and SNESNCG only support left preconditioning. 57162d547940SBarry Smith 5717c40d0f55SPeter Brune Level: intermediate 5718c40d0f55SPeter Brune 5719db781477SPatrick Sanan .seealso: `SNESGetNPCSide()`, `KSPSetPCSide()` 5720c40d0f55SPeter Brune @*/ 5721be95d8f1SBarry Smith PetscErrorCode SNESSetNPCSide(SNES snes,PCSide side) 5722c40d0f55SPeter Brune { 5723c40d0f55SPeter Brune PetscFunctionBegin; 5724c40d0f55SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5725c40d0f55SPeter Brune PetscValidLogicalCollectiveEnum(snes,side,2); 5726b552625fSStefano Zampini if (side == PC_SIDE_DEFAULT) side = PC_RIGHT; 572754c59aa7SJacob Faibussowitsch PetscCheck((side == PC_LEFT) || (side == PC_RIGHT),PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONG,"Only PC_LEFT and PC_RIGHT are supported"); 5728efd4aadfSBarry Smith snes->npcside = side; 5729c40d0f55SPeter Brune PetscFunctionReturn(0); 5730c40d0f55SPeter Brune } 5731c40d0f55SPeter Brune 5732c40d0f55SPeter Brune /*@ 5733be95d8f1SBarry Smith SNESGetNPCSide - Gets the preconditioning side. 5734c40d0f55SPeter Brune 5735c40d0f55SPeter Brune Not Collective 5736c40d0f55SPeter Brune 5737c40d0f55SPeter Brune Input Parameter: 5738c40d0f55SPeter Brune . snes - iterative context obtained from SNESCreate() 5739c40d0f55SPeter Brune 5740c40d0f55SPeter Brune Output Parameter: 5741c40d0f55SPeter Brune . side - the preconditioning side, where side is one of 5742c40d0f55SPeter Brune .vb 57432d547940SBarry Smith PC_LEFT - left preconditioning 57442d547940SBarry Smith PC_RIGHT - right preconditioning (default for most nonlinear solvers) 5745c40d0f55SPeter Brune .ve 5746c40d0f55SPeter Brune 5747c40d0f55SPeter Brune Level: intermediate 5748c40d0f55SPeter Brune 5749db781477SPatrick Sanan .seealso: `SNESSetNPCSide()`, `KSPGetPCSide()` 5750c40d0f55SPeter Brune @*/ 5751be95d8f1SBarry Smith PetscErrorCode SNESGetNPCSide(SNES snes,PCSide *side) 5752c40d0f55SPeter Brune { 5753c40d0f55SPeter Brune PetscFunctionBegin; 5754c40d0f55SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5755c40d0f55SPeter Brune PetscValidPointer(side,2); 5756efd4aadfSBarry Smith *side = snes->npcside; 5757c40d0f55SPeter Brune PetscFunctionReturn(0); 5758c40d0f55SPeter Brune } 5759c40d0f55SPeter Brune 57609e764e56SPeter Brune /*@ 57617601faf0SJed Brown SNESSetLineSearch - Sets the linesearch on the SNES instance. 57629e764e56SPeter Brune 57639e764e56SPeter Brune Collective on SNES 57649e764e56SPeter Brune 57659e764e56SPeter Brune Input Parameters: 57669e764e56SPeter Brune + snes - iterative context obtained from SNESCreate() 57679e764e56SPeter Brune - linesearch - the linesearch object 57689e764e56SPeter Brune 57699e764e56SPeter Brune Notes: 57707601faf0SJed Brown Use SNESGetLineSearch() to retrieve the preconditioner context (for example, 57719e764e56SPeter Brune to configure it using the API). 57729e764e56SPeter Brune 57739e764e56SPeter Brune Level: developer 57749e764e56SPeter Brune 5775db781477SPatrick Sanan .seealso: `SNESGetLineSearch()` 57769e764e56SPeter Brune @*/ 57777601faf0SJed Brown PetscErrorCode SNESSetLineSearch(SNES snes, SNESLineSearch linesearch) 57789e764e56SPeter Brune { 57799e764e56SPeter Brune PetscFunctionBegin; 57809e764e56SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5781f1c6b773SPeter Brune PetscValidHeaderSpecific(linesearch, SNESLINESEARCH_CLASSID, 2); 57829e764e56SPeter Brune PetscCheckSameComm(snes, 1, linesearch, 2); 57839566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject) linesearch)); 57849566063dSJacob Faibussowitsch PetscCall(SNESLineSearchDestroy(&snes->linesearch)); 5785f5af7f23SKarl Rupp 57869e764e56SPeter Brune snes->linesearch = linesearch; 5787f5af7f23SKarl Rupp 57889566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)snes, (PetscObject)snes->linesearch)); 57899e764e56SPeter Brune PetscFunctionReturn(0); 57909e764e56SPeter Brune } 57919e764e56SPeter Brune 5792a34ceb2aSJed Brown /*@ 57937601faf0SJed Brown SNESGetLineSearch - Returns a pointer to the line search context set with SNESSetLineSearch() 57948141a3b9SPeter Brune or creates a default line search instance associated with the SNES and returns it. 57959e764e56SPeter Brune 57969e764e56SPeter Brune Not Collective 57979e764e56SPeter Brune 57989e764e56SPeter Brune Input Parameter: 57999e764e56SPeter Brune . snes - iterative context obtained from SNESCreate() 58009e764e56SPeter Brune 58019e764e56SPeter Brune Output Parameter: 58029e764e56SPeter Brune . linesearch - linesearch context 58039e764e56SPeter Brune 5804162e0bf5SPeter Brune Level: beginner 58059e764e56SPeter Brune 5806db781477SPatrick Sanan .seealso: `SNESSetLineSearch()`, `SNESLineSearchCreate()` 58079e764e56SPeter Brune @*/ 58087601faf0SJed Brown PetscErrorCode SNESGetLineSearch(SNES snes, SNESLineSearch *linesearch) 58099e764e56SPeter Brune { 58109e764e56SPeter Brune const char *optionsprefix; 58119e764e56SPeter Brune 58129e764e56SPeter Brune PetscFunctionBegin; 58139e764e56SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 58149e764e56SPeter Brune PetscValidPointer(linesearch, 2); 58159e764e56SPeter Brune if (!snes->linesearch) { 58169566063dSJacob Faibussowitsch PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix)); 58179566063dSJacob Faibussowitsch PetscCall(SNESLineSearchCreate(PetscObjectComm((PetscObject)snes), &snes->linesearch)); 58189566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetSNES(snes->linesearch, snes)); 58199566063dSJacob Faibussowitsch PetscCall(SNESLineSearchAppendOptionsPrefix(snes->linesearch, optionsprefix)); 58209566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject) snes->linesearch, (PetscObject) snes, 1)); 58219566063dSJacob Faibussowitsch PetscCall(PetscLogObjectParent((PetscObject)snes, (PetscObject)snes->linesearch)); 58229e764e56SPeter Brune } 58239e764e56SPeter Brune *linesearch = snes->linesearch; 58249e764e56SPeter Brune PetscFunctionReturn(0); 58259e764e56SPeter Brune } 5826