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; 13fc8bc0e3SRichard Tran Mills PetscLogEvent SNES_Solve, SNES_SetUp, SNES_FunctionEval, SNES_JacobianEval, SNES_NGSEval, SNES_NGSFuncEval, SNES_NPCSolve, SNES_ObjectiveEval; 14a09944afSBarry Smith 15e113a28aSBarry Smith /*@ 16dc4c0fb0SBarry Smith SNESSetErrorIfNotConverged - Causes `SNESSolve()` to generate an error immediately if the solver has not converged. 17e113a28aSBarry Smith 18c3339decSBarry Smith Logically Collective 19e113a28aSBarry Smith 20e113a28aSBarry Smith Input Parameters: 21f6dfbefdSBarry Smith + snes - iterative context obtained from `SNESCreate()` 22f6dfbefdSBarry Smith - flg - `PETSC_TRUE` indicates you want the error generated 23e113a28aSBarry Smith 2420f4b53cSBarry Smith Options Database Key: 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 29f6dfbefdSBarry Smith Note: 30f6dfbefdSBarry Smith Normally PETSc continues if a solver fails to converge, you can call `SNESGetConvergedReason()` after a `SNESSolve()` 31f6dfbefdSBarry Smith to determine if it has converged. Otherwise the solution may be inaccurate or wrong 32e113a28aSBarry Smith 33dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetErrorIfNotConverged()`, `KSPGetErrorIfNotConverged()`, `KSPSetErrorIfNotConverged()` 34e113a28aSBarry Smith @*/ 35d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetErrorIfNotConverged(SNES snes, PetscBool flg) 36d71ae5a4SJacob Faibussowitsch { 37e113a28aSBarry Smith PetscFunctionBegin; 38e113a28aSBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 39acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes, flg, 2); 40e113a28aSBarry Smith snes->errorifnotconverged = flg; 413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 42e113a28aSBarry Smith } 43e113a28aSBarry Smith 44e113a28aSBarry Smith /*@ 45f6dfbefdSBarry Smith SNESGetErrorIfNotConverged - Indicates if `SNESSolve()` will generate an error if the solver does not converge? 46e113a28aSBarry Smith 47e113a28aSBarry Smith Not Collective 48e113a28aSBarry Smith 49e113a28aSBarry Smith Input Parameter: 50f6dfbefdSBarry Smith . snes - iterative context obtained from `SNESCreate()` 51e113a28aSBarry Smith 52e113a28aSBarry Smith Output Parameter: 53f6dfbefdSBarry Smith . flag - `PETSC_TRUE` if it will generate an error, else `PETSC_FALSE` 54e113a28aSBarry Smith 55e113a28aSBarry Smith Level: intermediate 56e113a28aSBarry Smith 57dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESSetErrorIfNotConverged()`, `KSPGetErrorIfNotConverged()`, `KSPSetErrorIfNotConverged()` 58e113a28aSBarry Smith @*/ 59d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetErrorIfNotConverged(SNES snes, PetscBool *flag) 60d71ae5a4SJacob Faibussowitsch { 61e113a28aSBarry Smith PetscFunctionBegin; 62e113a28aSBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 63534a8f05SLisandro Dalcin PetscValidBoolPointer(flag, 2); 64e113a28aSBarry Smith *flag = snes->errorifnotconverged; 653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 66e113a28aSBarry Smith } 67e113a28aSBarry Smith 684fc747eaSLawrence Mitchell /*@ 69dc4c0fb0SBarry Smith SNESSetAlwaysComputesFinalResidual - tells the `SNES` to always compute the residual (nonlinear function value) at the final solution 704fc747eaSLawrence Mitchell 71c3339decSBarry Smith Logically Collective 724fc747eaSLawrence Mitchell 734fc747eaSLawrence Mitchell Input Parameters: 74f6dfbefdSBarry Smith + snes - the shell `SNES` 75f6dfbefdSBarry Smith - flg - `PETSC_TRUE` to always compute the residual 764fc747eaSLawrence Mitchell 774fc747eaSLawrence Mitchell Level: advanced 784fc747eaSLawrence Mitchell 79f6dfbefdSBarry Smith Note: 80f6dfbefdSBarry Smith Some solvers (such as smoothers in a `SNESFAS`) do not need the residual computed at the final solution so skip computing it 81f6dfbefdSBarry Smith to save time. 82f6dfbefdSBarry Smith 83dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESGetAlwaysComputesFinalResidual()` 844fc747eaSLawrence Mitchell @*/ 85d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetAlwaysComputesFinalResidual(SNES snes, PetscBool flg) 86d71ae5a4SJacob Faibussowitsch { 874fc747eaSLawrence Mitchell PetscFunctionBegin; 884fc747eaSLawrence Mitchell PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 894fc747eaSLawrence Mitchell snes->alwayscomputesfinalresidual = flg; 903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 914fc747eaSLawrence Mitchell } 924fc747eaSLawrence Mitchell 934fc747eaSLawrence Mitchell /*@ 94f6dfbefdSBarry Smith SNESGetAlwaysComputesFinalResidual - checks if the `SNES` always computes the residual at the final solution 954fc747eaSLawrence Mitchell 96c3339decSBarry Smith Logically Collective 974fc747eaSLawrence Mitchell 984fc747eaSLawrence Mitchell Input Parameter: 99f6dfbefdSBarry Smith . snes - the `SNES` context 1004fc747eaSLawrence Mitchell 1014fc747eaSLawrence Mitchell Output Parameter: 102f6dfbefdSBarry Smith . flg - `PETSC_TRUE` if the residual is computed 1034fc747eaSLawrence Mitchell 1044fc747eaSLawrence Mitchell Level: advanced 1054fc747eaSLawrence Mitchell 106dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESSetAlwaysComputesFinalResidual()` 1074fc747eaSLawrence Mitchell @*/ 108d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetAlwaysComputesFinalResidual(SNES snes, PetscBool *flg) 109d71ae5a4SJacob Faibussowitsch { 1104fc747eaSLawrence Mitchell PetscFunctionBegin; 1114fc747eaSLawrence Mitchell PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1124fc747eaSLawrence Mitchell *flg = snes->alwayscomputesfinalresidual; 1133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1144fc747eaSLawrence Mitchell } 1154fc747eaSLawrence Mitchell 116e725d27bSBarry Smith /*@ 117f6dfbefdSBarry Smith SNESSetFunctionDomainError - tells `SNES` that the input vector, a proposed new solution, to your function you provided to `SNESSetFunction()` is not 118f0b84518SBarry Smith in the functions domain. For example, a step with negative pressure. 1194936397dSBarry Smith 120c3339decSBarry Smith Logically Collective 1214936397dSBarry Smith 1222fe279fdSBarry Smith Input Parameter: 123f6dfbefdSBarry Smith . snes - the `SNES` context 1244936397dSBarry Smith 12528529972SSatish Balay Level: advanced 1264936397dSBarry Smith 127f0b84518SBarry Smith Note: 128f0b84518SBarry Smith You can direct `SNES` to avoid certain steps by using `SNESVISetVariableBounds()`, `SNESVISetComputeVariableBounds()` or 129f0b84518SBarry Smith `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()` 130f0b84518SBarry Smith 131dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESCreate()`, `SNESSetFunction()`, `SNESFunction`, `SNESSetJacobianDomainError()`, `SNESVISetVariableBounds()`, 132f0b84518SBarry Smith `SNESVISetComputeVariableBounds()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()` 1334936397dSBarry Smith @*/ 134d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetFunctionDomainError(SNES snes) 135d71ae5a4SJacob Faibussowitsch { 1364936397dSBarry Smith PetscFunctionBegin; 1370700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1385f80ce2aSJacob Faibussowitsch PetscCheck(!snes->errorifnotconverged, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "User code indicates input vector is not in the function domain"); 1394936397dSBarry Smith snes->domainerror = PETSC_TRUE; 1403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1414936397dSBarry Smith } 1424936397dSBarry Smith 1436a388c36SPeter Brune /*@ 144f6dfbefdSBarry Smith SNESSetJacobianDomainError - tells `SNES` that the function you provided to `SNESSetJacobian()` at the proposed step. For example there is a negative element transformation. 14507b62357SFande Kong 146c3339decSBarry Smith Logically Collective 14707b62357SFande Kong 1482fe279fdSBarry Smith Input Parameter: 149f6dfbefdSBarry Smith . snes - the `SNES` context 15007b62357SFande Kong 15107b62357SFande Kong Level: advanced 15207b62357SFande Kong 153f0b84518SBarry Smith Note: 154f0b84518SBarry Smith You can direct `SNES` to avoid certain steps by using `SNESVISetVariableBounds()`, `SNESVISetComputeVariableBounds()` or 155f0b84518SBarry Smith `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()` 156f0b84518SBarry Smith 157dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESCreate()`, `SNESSetFunction()`, `SNESFunction()`, `SNESSetFunctionDomainError()`, `SNESVISetVariableBounds()`, 158f0b84518SBarry Smith `SNESVISetComputeVariableBounds()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()` 15907b62357SFande Kong @*/ 160d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetJacobianDomainError(SNES snes) 161d71ae5a4SJacob Faibussowitsch { 16207b62357SFande Kong PetscFunctionBegin; 16307b62357SFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1645f80ce2aSJacob Faibussowitsch PetscCheck(!snes->errorifnotconverged, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "User code indicates computeJacobian does not make sense"); 16507b62357SFande Kong snes->jacobiandomainerror = PETSC_TRUE; 1663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16707b62357SFande Kong } 16807b62357SFande Kong 16907b62357SFande Kong /*@ 170f6dfbefdSBarry Smith SNESSetCheckJacobianDomainError - tells `SNESSolve()` whether to check if the user called `SNESSetJacobianDomainError()` Jacobian domain error after 171f6dfbefdSBarry Smith each Jacobian evaluation. By default, we check Jacobian domain error in the debug mode, and do not check it in the optimized mode. 172b351a90bSFande Kong 173c3339decSBarry Smith Logically Collective 174b351a90bSFande Kong 175b351a90bSFande Kong Input Parameters: 17620f4b53cSBarry Smith + snes - the `SNES` context 177f6dfbefdSBarry Smith - flg - indicates if or not to check Jacobian domain error after each Jacobian evaluation 178b351a90bSFande Kong 179b351a90bSFande Kong Level: advanced 180b351a90bSFande Kong 181f6dfbefdSBarry Smith Note: 182f6dfbefdSBarry Smith Checks require one extra parallel synchronization for each Jacobian evaluation 183f6dfbefdSBarry Smith 184dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESConvergedReason`, `SNESCreate()`, `SNESSetFunction()`, `SNESFunction()`, `SNESSetFunctionDomainError()`, `SNESGetCheckJacobianDomainError()` 185b351a90bSFande Kong @*/ 186d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetCheckJacobianDomainError(SNES snes, PetscBool flg) 187d71ae5a4SJacob Faibussowitsch { 188b351a90bSFande Kong PetscFunctionBegin; 189b351a90bSFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 190b351a90bSFande Kong snes->checkjacdomainerror = flg; 1913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 192b351a90bSFande Kong } 193b351a90bSFande Kong 194b351a90bSFande Kong /*@ 1958383d7d7SFande Kong SNESGetCheckJacobianDomainError - Get an indicator whether or not we are checking Jacobian domain errors after each Jacobian evaluation. 1968383d7d7SFande Kong 197c3339decSBarry Smith Logically Collective 1988383d7d7SFande Kong 1992fe279fdSBarry Smith Input Parameter: 200f6dfbefdSBarry Smith . snes - the `SNES` context 2018383d7d7SFande Kong 2022fe279fdSBarry Smith Output Parameter: 203f6dfbefdSBarry Smith . flg - `PETSC_FALSE` indicates that we don't check Jacobian domain errors after each Jacobian evaluation 2048383d7d7SFande Kong 2058383d7d7SFande Kong Level: advanced 2068383d7d7SFande Kong 207dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESCreate()`, `SNESSetFunction()`, `SNESFunction()`, `SNESSetFunctionDomainError()`, `SNESSetCheckJacobianDomainError()` 2088383d7d7SFande Kong @*/ 209d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetCheckJacobianDomainError(SNES snes, PetscBool *flg) 210d71ae5a4SJacob Faibussowitsch { 2118383d7d7SFande Kong PetscFunctionBegin; 2128383d7d7SFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 213534a8f05SLisandro Dalcin PetscValidBoolPointer(flg, 2); 2148383d7d7SFande Kong *flg = snes->checkjacdomainerror; 2153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2168383d7d7SFande Kong } 2178383d7d7SFande Kong 2188383d7d7SFande Kong /*@ 219f6dfbefdSBarry Smith SNESGetFunctionDomainError - Gets the status of the domain error after a call to `SNESComputeFunction()`; 2206a388c36SPeter Brune 221f6dfbefdSBarry Smith Logically Collective 2226a388c36SPeter Brune 2232fe279fdSBarry Smith Input Parameter: 224f6dfbefdSBarry Smith . snes - the `SNES` context 2256a388c36SPeter Brune 2262fe279fdSBarry Smith Output Parameter: 227f6dfbefdSBarry Smith . domainerror - Set to `PETSC_TRUE` if there's a domain error; `PETSC_FALSE` otherwise. 2286a388c36SPeter Brune 229f6dfbefdSBarry Smith Level: developer 2306a388c36SPeter Brune 231dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetFunctionDomainError()`, `SNESComputeFunction()` 2326a388c36SPeter Brune @*/ 233d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetFunctionDomainError(SNES snes, PetscBool *domainerror) 234d71ae5a4SJacob Faibussowitsch { 2356a388c36SPeter Brune PetscFunctionBegin; 2366a388c36SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 237534a8f05SLisandro Dalcin PetscValidBoolPointer(domainerror, 2); 2386a388c36SPeter Brune *domainerror = snes->domainerror; 2393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2406a388c36SPeter Brune } 2416a388c36SPeter Brune 24207b62357SFande Kong /*@ 243f6dfbefdSBarry Smith SNESGetJacobianDomainError - Gets the status of the Jacobian domain error after a call to `SNESComputeJacobian()`; 24407b62357SFande Kong 245c3339decSBarry Smith Logically Collective 24607b62357SFande Kong 2472fe279fdSBarry Smith Input Parameter: 248f6dfbefdSBarry Smith . snes - the `SNES` context 24907b62357SFande Kong 2502fe279fdSBarry Smith Output Parameter: 251f6dfbefdSBarry Smith . domainerror - Set to `PETSC_TRUE` if there's a Jacobian domain error; `PETSC_FALSE` otherwise. 25207b62357SFande Kong 25307b62357SFande Kong Level: advanced 25407b62357SFande Kong 255dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetFunctionDomainError()`, `SNESComputeFunction()`, `SNESGetFunctionDomainError()` 25607b62357SFande Kong @*/ 257d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetJacobianDomainError(SNES snes, PetscBool *domainerror) 258d71ae5a4SJacob Faibussowitsch { 25907b62357SFande Kong PetscFunctionBegin; 26007b62357SFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 261534a8f05SLisandro Dalcin PetscValidBoolPointer(domainerror, 2); 26207b62357SFande Kong *domainerror = snes->jacobiandomainerror; 2633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26407b62357SFande Kong } 26507b62357SFande Kong 26655849f57SBarry Smith /*@C 267f6dfbefdSBarry Smith SNESLoad - Loads a `SNES` that has been stored in `PETSCVIEWERBINARY` with `SNESView()`. 26855849f57SBarry Smith 269c3339decSBarry Smith Collective 27055849f57SBarry Smith 27155849f57SBarry Smith Input Parameters: 272f6dfbefdSBarry Smith + newdm - the newly loaded `SNES`, this needs to have been created with `SNESCreate()` or 273f6dfbefdSBarry Smith some related function before a call to `SNESLoad()`. 274f6dfbefdSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` 27555849f57SBarry Smith 27655849f57SBarry Smith Level: intermediate 27755849f57SBarry Smith 278f6dfbefdSBarry Smith Note: 279f6dfbefdSBarry Smith The type is determined by the data in the file, any type set into the `SNES` before this call is ignored. 28055849f57SBarry Smith 281dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `PetscViewer`, `SNESCreate()`, `SNESType`, `PetscViewerBinaryOpen()`, `SNESView()`, `MatLoad()`, `VecLoad()` 28255849f57SBarry Smith @*/ 283d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESLoad(SNES snes, PetscViewer viewer) 284d71ae5a4SJacob Faibussowitsch { 28555849f57SBarry Smith PetscBool isbinary; 286060da220SMatthew G. Knepley PetscInt classid; 28755849f57SBarry Smith char type[256]; 28855849f57SBarry Smith KSP ksp; 2892d53ad75SBarry Smith DM dm; 2902d53ad75SBarry Smith DMSNES dmsnes; 29155849f57SBarry Smith 29255849f57SBarry Smith PetscFunctionBegin; 2932d53ad75SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 29455849f57SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 2959566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 2965f80ce2aSJacob Faibussowitsch PetscCheck(isbinary, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 29755849f57SBarry Smith 2989566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT)); 2995f80ce2aSJacob Faibussowitsch PetscCheck(classid == SNES_FILE_CLASSID, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_WRONG, "Not SNES next in file"); 3009566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR)); 3019566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes, type)); 302dbbe0bcdSBarry Smith PetscTryTypeMethod(snes, load, viewer); 3039566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 3049566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &dmsnes)); 3059566063dSJacob Faibussowitsch PetscCall(DMSNESLoad(dmsnes, viewer)); 3069566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, &ksp)); 3079566063dSJacob Faibussowitsch PetscCall(KSPLoad(ksp, viewer)); 3083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 30955849f57SBarry Smith } 3106a388c36SPeter Brune 3119804daf3SBarry Smith #include <petscdraw.h> 312e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 313e04113cfSBarry Smith #include <petscviewersaws.h> 314bfb97211SBarry Smith #endif 3158404b7f3SBarry Smith 316fe2efc57SMark /*@C 317dc4c0fb0SBarry Smith SNESViewFromOptions - View a `SNES` based on values in the options database 318fe2efc57SMark 319c3339decSBarry Smith Collective 320fe2efc57SMark 321fe2efc57SMark Input Parameters: 322f6dfbefdSBarry Smith + A - the `SNES` context 323dc4c0fb0SBarry Smith . obj - Optional object that provides the options prefix for the checks 324736c3998SJose E. Roman - name - command line option 325fe2efc57SMark 326fe2efc57SMark Level: intermediate 327f6dfbefdSBarry Smith 328dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESView`, `PetscObjectViewFromOptions()`, `SNESCreate()` 329fe2efc57SMark @*/ 330d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESViewFromOptions(SNES A, PetscObject obj, const char name[]) 331d71ae5a4SJacob Faibussowitsch { 332fe2efc57SMark PetscFunctionBegin; 333fe2efc57SMark PetscValidHeaderSpecific(A, SNES_CLASSID, 1); 3349566063dSJacob Faibussowitsch PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name)); 3353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 336fe2efc57SMark } 337fe2efc57SMark 338789d8953SBarry Smith PETSC_EXTERN PetscErrorCode SNESComputeJacobian_DMDA(SNES, Vec, Mat, Mat, void *); 339789d8953SBarry Smith 3407e2c5f70SBarry Smith /*@C 341dc4c0fb0SBarry Smith SNESView - Prints or visualizes the `SNES` data structure. 3429b94acceSBarry Smith 343c3339decSBarry Smith Collective 344fee21e36SBarry Smith 345c7afd0dbSLois Curfman McInnes Input Parameters: 346f6dfbefdSBarry Smith + snes - the `SNES` context 347f6dfbefdSBarry Smith - viewer - the `PetscViewer` 348c7afd0dbSLois Curfman McInnes 3499b94acceSBarry Smith Options Database Key: 350f6dfbefdSBarry Smith . -snes_view - Calls `SNESView()` at end of `SNESSolve()` 3519b94acceSBarry Smith 352dc4c0fb0SBarry Smith Level: beginner 353dc4c0fb0SBarry Smith 3549b94acceSBarry Smith Notes: 3559b94acceSBarry Smith The available visualization contexts include 356f6dfbefdSBarry Smith + `PETSC_VIEWER_STDOUT_SELF` - standard output (default) 357f6dfbefdSBarry Smith - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard 358c8a8ba5cSLois Curfman McInnes output where only the first processor opens 359c8a8ba5cSLois Curfman McInnes the file. All other processors send their 360c8a8ba5cSLois Curfman McInnes data to the first processor to print. 3619b94acceSBarry Smith 362052bf0daSPierre Jolivet The available formats include 363f6dfbefdSBarry Smith + `PETSC_VIEWER_DEFAULT` - standard output (default) 364f6dfbefdSBarry Smith - `PETSC_VIEWER_ASCII_INFO_DETAIL` - more verbose output for `SNESNASM` 365052bf0daSPierre Jolivet 3663e081fefSLois Curfman McInnes The user can open an alternative visualization context with 367f6dfbefdSBarry Smith `PetscViewerASCIIOpen()` - output to a specified file. 3689b94acceSBarry Smith 369f6dfbefdSBarry Smith In the debugger you can do "call `SNESView`(snes,0)" to display the `SNES` solver. (The same holds for any PETSc object viewer). 370595c91d4SBarry Smith 371dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESLoad()`, `SNESCreate()`, `PetscViewerASCIIOpen()` 3729b94acceSBarry Smith @*/ 373d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESView(SNES snes, PetscViewer viewer) 374d71ae5a4SJacob Faibussowitsch { 375fa9f3622SBarry Smith SNESKSPEW *kctx; 37694b7f48cSBarry Smith KSP ksp; 3777f1410a3SPeter Brune SNESLineSearch linesearch; 37872a02f06SBarry Smith PetscBool iascii, isstring, isbinary, isdraw; 3792d53ad75SBarry Smith DMSNES dmsnes; 380e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 381536b137fSBarry Smith PetscBool issaws; 382bfb97211SBarry Smith #endif 3839b94acceSBarry Smith 3843a40ed3dSBarry Smith PetscFunctionBegin; 3850700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 38648a46eb9SPierre Jolivet if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes), &viewer)); 3870700a824SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 388c9780b6fSBarry Smith PetscCheckSameComm(snes, 1, viewer, 2); 38974679c65SBarry Smith 3909566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 3919566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring)); 3929566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 3939566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw)); 394e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 3959566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws)); 396bfb97211SBarry Smith #endif 39732077d6dSBarry Smith if (iascii) { 398dc0571f2SMatthew G. Knepley SNESNormSchedule normschedule; 3998404b7f3SBarry Smith DM dm; 4008404b7f3SBarry Smith PetscErrorCode (*cJ)(SNES, Vec, Mat, Mat, void *); 4018404b7f3SBarry Smith void *ctx; 402789d8953SBarry Smith const char *pre = ""; 403dc0571f2SMatthew G. Knepley 4049566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)snes, viewer)); 40548a46eb9SPierre Jolivet if (!snes->setupcalled) PetscCall(PetscViewerASCIIPrintf(viewer, " SNES has not been set up so information may be incomplete\n")); 406e7788613SBarry Smith if (snes->ops->view) { 4079566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 408dbbe0bcdSBarry Smith PetscUseTypeMethod(snes, view, viewer); 4099566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 4100ef38995SBarry Smith } 41163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " maximum iterations=%" PetscInt_FMT ", maximum function evaluations=%" PetscInt_FMT "\n", snes->max_its, snes->max_funcs)); 4129566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " tolerances: relative=%g, absolute=%g, solution=%g\n", (double)snes->rtol, (double)snes->abstol, (double)snes->stol)); 41348a46eb9SPierre Jolivet if (snes->usesksp) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of linear solver iterations=%" PetscInt_FMT "\n", snes->linear_its)); 41463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of function evaluations=%" PetscInt_FMT "\n", snes->nfuncs)); 4159566063dSJacob Faibussowitsch PetscCall(SNESGetNormSchedule(snes, &normschedule)); 4169566063dSJacob Faibussowitsch if (normschedule > 0) PetscCall(PetscViewerASCIIPrintf(viewer, " norm schedule %s\n", SNESNormSchedules[normschedule])); 41748a46eb9SPierre Jolivet if (snes->gridsequence) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of grid sequence refinements=%" PetscInt_FMT "\n", snes->gridsequence)); 4189b94acceSBarry Smith if (snes->ksp_ewconv) { 419fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 4209b94acceSBarry Smith if (kctx) { 42163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Eisenstat-Walker computation of KSP relative tolerance (version %" PetscInt_FMT ")\n", kctx->version)); 4229566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " rtol_0=%g, rtol_max=%g, threshold=%g\n", (double)kctx->rtol_0, (double)kctx->rtol_max, (double)kctx->threshold)); 4239566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " gamma=%g, alpha=%g, alpha2=%g\n", (double)kctx->gamma, (double)kctx->alpha, (double)kctx->alpha2)); 4249b94acceSBarry Smith } 4259b94acceSBarry Smith } 426eb1f6c34SBarry Smith if (snes->lagpreconditioner == -1) { 4279566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Preconditioned is never rebuilt\n")); 428eb1f6c34SBarry Smith } else if (snes->lagpreconditioner > 1) { 42963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Preconditioned is rebuilt every %" PetscInt_FMT " new Jacobians\n", snes->lagpreconditioner)); 430eb1f6c34SBarry Smith } 431eb1f6c34SBarry Smith if (snes->lagjacobian == -1) { 4329566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Jacobian is never rebuilt\n")); 433eb1f6c34SBarry Smith } else if (snes->lagjacobian > 1) { 43463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Jacobian is rebuilt every %" PetscInt_FMT " SNES iterations\n", snes->lagjacobian)); 435eb1f6c34SBarry Smith } 4369566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 4379566063dSJacob Faibussowitsch PetscCall(DMSNESGetJacobian(dm, &cJ, &ctx)); 438789d8953SBarry Smith if (snes->mf_operator) { 4399566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Jacobian is applied matrix-free with differencing\n")); 440789d8953SBarry Smith pre = "Preconditioning "; 441789d8953SBarry Smith } 4428404b7f3SBarry Smith if (cJ == SNESComputeJacobianDefault) { 4439566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " %sJacobian is built using finite differences one column at a time\n", pre)); 4448404b7f3SBarry Smith } else if (cJ == SNESComputeJacobianDefaultColor) { 4459566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " %sJacobian is built using finite differences with coloring\n", pre)); 446789d8953SBarry Smith /* it slightly breaks data encapsulation for access the DMDA information directly */ 447789d8953SBarry Smith } else if (cJ == SNESComputeJacobian_DMDA) { 448789d8953SBarry Smith MatFDColoring fdcoloring; 4499566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)dm, "DMDASNES_FDCOLORING", (PetscObject *)&fdcoloring)); 450789d8953SBarry Smith if (fdcoloring) { 4519566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " %sJacobian is built using colored finite differences on a DMDA\n", pre)); 452789d8953SBarry Smith } else { 4539566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " %sJacobian is built using a DMDA local Jacobian\n", pre)); 454789d8953SBarry Smith } 455996e1cbcSBarry Smith } else if (snes->mf && !snes->mf_operator) { 4569566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Jacobian is applied matrix-free with differencing, no explicit Jacobian\n")); 4578404b7f3SBarry Smith } 4580f5bd95cSBarry Smith } else if (isstring) { 459317d6ea6SBarry Smith const char *type; 4609566063dSJacob Faibussowitsch PetscCall(SNESGetType(snes, &type)); 4619566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer, " SNESType: %-7.7s", type)); 462dbbe0bcdSBarry Smith PetscTryTypeMethod(snes, view, viewer); 46355849f57SBarry Smith } else if (isbinary) { 46455849f57SBarry Smith PetscInt classid = SNES_FILE_CLASSID; 46555849f57SBarry Smith MPI_Comm comm; 46655849f57SBarry Smith PetscMPIInt rank; 46755849f57SBarry Smith char type[256]; 46855849f57SBarry Smith 4699566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)snes, &comm)); 4709566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 471dd400576SPatrick Sanan if (rank == 0) { 4729566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, &classid, 1, PETSC_INT)); 4739566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(type, ((PetscObject)snes)->type_name, sizeof(type))); 4749566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, type, sizeof(type), PETSC_CHAR)); 47555849f57SBarry Smith } 476dbbe0bcdSBarry Smith PetscTryTypeMethod(snes, view, viewer); 47772a02f06SBarry Smith } else if (isdraw) { 47872a02f06SBarry Smith PetscDraw draw; 47972a02f06SBarry Smith char str[36]; 48089fd9fafSBarry Smith PetscReal x, y, bottom, h; 48172a02f06SBarry Smith 4829566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw)); 4839566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y)); 4849566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(str, "SNES: ", sizeof(str))); 4859566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(str, ((PetscObject)snes)->type_name, sizeof(str))); 4869566063dSJacob Faibussowitsch PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_BLUE, PETSC_DRAW_BLACK, str, NULL, &h)); 48789fd9fafSBarry Smith bottom = y - h; 4889566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom)); 489dbbe0bcdSBarry Smith PetscTryTypeMethod(snes, view, viewer); 490e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 491536b137fSBarry Smith } else if (issaws) { 492d45a07a7SBarry Smith PetscMPIInt rank; 4932657e9d9SBarry Smith const char *name; 494d45a07a7SBarry Smith 4959566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)snes, &name)); 4969566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 497dd400576SPatrick Sanan if (!((PetscObject)snes)->amsmem && rank == 0) { 498d45a07a7SBarry Smith char dir[1024]; 499d45a07a7SBarry Smith 5009566063dSJacob Faibussowitsch PetscCall(PetscObjectViewSAWs((PetscObject)snes, viewer)); 5019566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/its", name)); 502792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, &snes->iter, 1, SAWs_READ, SAWs_INT)); 50348a46eb9SPierre Jolivet if (!snes->conv_hist) PetscCall(SNESSetConvergenceHistory(snes, NULL, NULL, PETSC_DECIDE, PETSC_TRUE)); 5049566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/conv_hist", name)); 505792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, snes->conv_hist, 10, SAWs_READ, SAWs_DOUBLE)); 506f05ece33SBarry Smith } 507bfb97211SBarry Smith #endif 50872a02f06SBarry Smith } 50972a02f06SBarry Smith if (snes->linesearch) { 5109566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &linesearch)); 5119566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 5129566063dSJacob Faibussowitsch PetscCall(SNESLineSearchView(linesearch, viewer)); 5139566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 51419bcc07fSBarry Smith } 515efd4aadfSBarry Smith if (snes->npc && snes->usesnpc) { 5169566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 5179566063dSJacob Faibussowitsch PetscCall(SNESView(snes->npc, viewer)); 5189566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 5194a0c5b0cSMatthew G Knepley } 5209566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 5219566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(snes->dm, &dmsnes)); 5229566063dSJacob Faibussowitsch PetscCall(DMSNESView(dmsnes, viewer)); 5239566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 5242c155ee1SBarry Smith if (snes->usesksp) { 5259566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, &ksp)); 5269566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 5279566063dSJacob Faibussowitsch PetscCall(KSPView(ksp, viewer)); 5289566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 5292c155ee1SBarry Smith } 53072a02f06SBarry Smith if (isdraw) { 53172a02f06SBarry Smith PetscDraw draw; 5329566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw)); 5339566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 5347f1410a3SPeter Brune } 5353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5369b94acceSBarry Smith } 5379b94acceSBarry Smith 53876b2cf59SMatthew Knepley /* 53976b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 54076b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 54176b2cf59SMatthew Knepley */ 54276b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 543a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 5446849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 54576b2cf59SMatthew Knepley 546ac226902SBarry Smith /*@C 547f6dfbefdSBarry Smith SNESAddOptionsChecker - Adds an additional function to check for `SNES` options. 54876b2cf59SMatthew Knepley 54976b2cf59SMatthew Knepley Not Collective 55076b2cf59SMatthew Knepley 55176b2cf59SMatthew Knepley Input Parameter: 55276b2cf59SMatthew Knepley . snescheck - function that checks for options 55376b2cf59SMatthew Knepley 55476b2cf59SMatthew Knepley Level: developer 55576b2cf59SMatthew Knepley 556dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetFromOptions()` 55776b2cf59SMatthew Knepley @*/ 558d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 559d71ae5a4SJacob Faibussowitsch { 56076b2cf59SMatthew Knepley PetscFunctionBegin; 56163a3b9bcSJacob Faibussowitsch PetscCheck(numberofsetfromoptions < MAXSETFROMOPTIONS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %d allowed", MAXSETFROMOPTIONS); 56276b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 5633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 56476b2cf59SMatthew Knepley } 56576b2cf59SMatthew Knepley 566d71ae5a4SJacob Faibussowitsch static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool hasOperator, PetscInt version) 567d71ae5a4SJacob Faibussowitsch { 568aa3661deSLisandro Dalcin Mat J; 569895c21f2SBarry Smith MatNullSpace nullsp; 570aa3661deSLisandro Dalcin 571aa3661deSLisandro Dalcin PetscFunctionBegin; 5720700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 573aa3661deSLisandro Dalcin 57498613b67SLisandro Dalcin if (!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) { 57598613b67SLisandro Dalcin Mat A = snes->jacobian, B = snes->jacobian_pre; 5769566063dSJacob Faibussowitsch PetscCall(MatCreateVecs(A ? A : B, NULL, &snes->vec_func)); 57798613b67SLisandro Dalcin } 57898613b67SLisandro Dalcin 5790fdf79fbSJacob Faibussowitsch PetscCheck(version == 1 || version == 2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator routines, only version 1 and 2"); 580aa3661deSLisandro Dalcin if (version == 1) { 5819566063dSJacob Faibussowitsch PetscCall(MatCreateSNESMF(snes, &J)); 5829566063dSJacob Faibussowitsch PetscCall(MatMFFDSetOptionsPrefix(J, ((PetscObject)snes)->prefix)); 5839566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(J)); 5841e2ae407SBarry Smith /* TODO: the version 2 code should be merged into the MatCreateSNESMF() and MatCreateMFFD() infrastructure and then removed */ 5850fdf79fbSJacob Faibussowitsch } else /* if (version == 2) */ { 5865f80ce2aSJacob Faibussowitsch PetscCheck(snes->vec_func, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "SNESSetFunction() must be called first"); 587570b7f6dSBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) && !defined(PETSC_USE_REAL___FP16) 588f6dfbefdSBarry Smith PetscCall(MatCreateSNESMFMore(snes, snes->vec_func, &J)); 589aa3661deSLisandro Dalcin #else 5902479783cSJose E. Roman SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "matrix-free operator routines (version 2)"); 591aa3661deSLisandro Dalcin #endif 5920fdf79fbSJacob Faibussowitsch } 593aa3661deSLisandro Dalcin 594895c21f2SBarry Smith /* attach any user provided null space that was on Amat to the newly created matrix free matrix */ 595895c21f2SBarry Smith if (snes->jacobian) { 5969566063dSJacob Faibussowitsch PetscCall(MatGetNullSpace(snes->jacobian, &nullsp)); 5971baa6e33SBarry Smith if (nullsp) PetscCall(MatSetNullSpace(J, nullsp)); 598895c21f2SBarry Smith } 599895c21f2SBarry Smith 60063a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Setting default matrix-free operator routines (version %" PetscInt_FMT ")\n", version)); 601d3462f78SMatthew Knepley if (hasOperator) { 602aa3661deSLisandro Dalcin /* This version replaces the user provided Jacobian matrix with a 603aa3661deSLisandro Dalcin matrix-free version but still employs the user-provided preconditioner matrix. */ 6049566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, J, NULL, NULL, NULL)); 605aa3661deSLisandro Dalcin } else { 606aa3661deSLisandro Dalcin /* This version replaces both the user-provided Jacobian and the user- 6073232da50SPeter Brune provided preconditioner Jacobian with the default matrix free version. */ 608b552625fSStefano Zampini if (snes->npcside == PC_LEFT && snes->npc) { 6099566063dSJacob Faibussowitsch if (!snes->jacobian) PetscCall(SNESSetJacobian(snes, J, NULL, NULL, NULL)); 610172a4300SPeter Brune } else { 611789d8953SBarry Smith KSP ksp; 612789d8953SBarry Smith PC pc; 613789d8953SBarry Smith PetscBool match; 614789d8953SBarry Smith 6159566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, J, J, MatMFFDComputeJacobian, NULL)); 616aa3661deSLisandro Dalcin /* Force no preconditioner */ 6179566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, &ksp)); 6189566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp, &pc)); 6192698c518SLisandro Dalcin PetscCall(PetscObjectTypeCompareAny((PetscObject)pc, &match, PCSHELL, PCH2OPUS, "")); 620aa3661deSLisandro Dalcin if (!match) { 6219566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n")); 6229566063dSJacob Faibussowitsch PetscCall(PCSetType(pc, PCNONE)); 623aa3661deSLisandro Dalcin } 624aa3661deSLisandro Dalcin } 625789d8953SBarry Smith } 6269566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); 6273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 628aa3661deSLisandro Dalcin } 629aa3661deSLisandro Dalcin 630d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMRestrictHook_SNESVecSol(DM dmfine, Mat Restrict, Vec Rscale, Mat Inject, DM dmcoarse, void *ctx) 631d71ae5a4SJacob Faibussowitsch { 632dfe15315SJed Brown SNES snes = (SNES)ctx; 6330298fd71SBarry Smith Vec Xfine, Xfine_named = NULL, Xcoarse; 634dfe15315SJed Brown 635dfe15315SJed Brown PetscFunctionBegin; 63616ebb321SJed Brown if (PetscLogPrintInfo) { 63716ebb321SJed Brown PetscInt finelevel, coarselevel, fineclevel, coarseclevel; 6389566063dSJacob Faibussowitsch PetscCall(DMGetRefineLevel(dmfine, &finelevel)); 6399566063dSJacob Faibussowitsch PetscCall(DMGetCoarsenLevel(dmfine, &fineclevel)); 6409566063dSJacob Faibussowitsch PetscCall(DMGetRefineLevel(dmcoarse, &coarselevel)); 6419566063dSJacob Faibussowitsch PetscCall(DMGetCoarsenLevel(dmcoarse, &coarseclevel)); 64263a3b9bcSJacob 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)); 64316ebb321SJed Brown } 644dfe15315SJed Brown if (dmfine == snes->dm) Xfine = snes->vec_sol; 645dfe15315SJed Brown else { 6469566063dSJacob Faibussowitsch PetscCall(DMGetNamedGlobalVector(dmfine, "SNESVecSol", &Xfine_named)); 647dfe15315SJed Brown Xfine = Xfine_named; 648dfe15315SJed Brown } 6499566063dSJacob Faibussowitsch PetscCall(DMGetNamedGlobalVector(dmcoarse, "SNESVecSol", &Xcoarse)); 650907f5c5aSLawrence Mitchell if (Inject) { 6519566063dSJacob Faibussowitsch PetscCall(MatRestrict(Inject, Xfine, Xcoarse)); 652907f5c5aSLawrence Mitchell } else { 6539566063dSJacob Faibussowitsch PetscCall(MatRestrict(Restrict, Xfine, Xcoarse)); 6549566063dSJacob Faibussowitsch PetscCall(VecPointwiseMult(Xcoarse, Xcoarse, Rscale)); 655907f5c5aSLawrence Mitchell } 6569566063dSJacob Faibussowitsch PetscCall(DMRestoreNamedGlobalVector(dmcoarse, "SNESVecSol", &Xcoarse)); 6579566063dSJacob Faibussowitsch if (Xfine_named) PetscCall(DMRestoreNamedGlobalVector(dmfine, "SNESVecSol", &Xfine_named)); 6583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 659dfe15315SJed Brown } 660dfe15315SJed Brown 661d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCoarsenHook_SNESVecSol(DM dm, DM dmc, void *ctx) 662d71ae5a4SJacob Faibussowitsch { 66316ebb321SJed Brown PetscFunctionBegin; 6649566063dSJacob Faibussowitsch PetscCall(DMCoarsenHookAdd(dmc, DMCoarsenHook_SNESVecSol, DMRestrictHook_SNESVecSol, ctx)); 6653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 66616ebb321SJed Brown } 66716ebb321SJed Brown 668a6950cb2SJed Brown /* This may be called to rediscretize the operator on levels of linear multigrid. The DM shuffle is so the user can 669a6950cb2SJed Brown * safely call SNESGetDM() in their residual evaluation routine. */ 670d71ae5a4SJacob Faibussowitsch static PetscErrorCode KSPComputeOperators_SNES(KSP ksp, Mat A, Mat B, void *ctx) 671d71ae5a4SJacob Faibussowitsch { 672caa4e7f2SJed Brown SNES snes = (SNES)ctx; 6730298fd71SBarry Smith Vec X, Xnamed = NULL; 674dfe15315SJed Brown DM dmsave; 6754e269d77SPeter Brune void *ctxsave; 67625ce1634SJed Brown PetscErrorCode (*jac)(SNES, Vec, Mat, Mat, void *) = NULL; 677caa4e7f2SJed Brown 678caa4e7f2SJed Brown PetscFunctionBegin; 679dfe15315SJed Brown dmsave = snes->dm; 6809566063dSJacob Faibussowitsch PetscCall(KSPGetDM(ksp, &snes->dm)); 681dfe15315SJed Brown if (dmsave == snes->dm) X = snes->vec_sol; /* We are on the finest level */ 6829371c9d4SSatish Balay else { /* We are on a coarser level, this vec was initialized using a DM restrict hook */ PetscCall(DMGetNamedGlobalVector(snes->dm, "SNESVecSol", &Xnamed)); 683dfe15315SJed Brown X = Xnamed; 6849566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, NULL, NULL, &jac, &ctxsave)); 6854e269d77SPeter Brune /* If the DM's don't match up, the MatFDColoring context needed for the jacobian won't match up either -- fixit. */ 68648a46eb9SPierre Jolivet if (jac == SNESComputeJacobianDefaultColor) PetscCall(SNESSetJacobian(snes, NULL, NULL, SNESComputeJacobianDefaultColor, NULL)); 6874e269d77SPeter Brune } 6884dde8bb0SMatthew G. Knepley /* Make sure KSP DM has the Jacobian computation routine */ 6894dde8bb0SMatthew G. Knepley { 6904dde8bb0SMatthew G. Knepley DMSNES sdm; 6914e269d77SPeter Brune 6929566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(snes->dm, &sdm)); 69348a46eb9SPierre Jolivet if (!sdm->ops->computejacobian) PetscCall(DMCopyDMSNES(dmsave, snes->dm)); 6944dde8bb0SMatthew G. Knepley } 6952b93b426SMatthew G. Knepley /* Compute the operators */ 6969566063dSJacob Faibussowitsch PetscCall(SNESComputeJacobian(snes, X, A, B)); 6972b93b426SMatthew G. Knepley /* Put the previous context back */ 69848a46eb9SPierre Jolivet if (snes->dm != dmsave && jac == SNESComputeJacobianDefaultColor) PetscCall(SNESSetJacobian(snes, NULL, NULL, jac, ctxsave)); 6994e269d77SPeter Brune 7009566063dSJacob Faibussowitsch if (Xnamed) PetscCall(DMRestoreNamedGlobalVector(snes->dm, "SNESVecSol", &Xnamed)); 701dfe15315SJed Brown snes->dm = dmsave; 7023ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 703caa4e7f2SJed Brown } 704caa4e7f2SJed Brown 7056cab3a1bSJed Brown /*@ 706dc4c0fb0SBarry Smith SNESSetUpMatrices - ensures that matrices are available for `SNES` Newton-like methods, this is called by `SNESSetUp_XXX()` 7076cab3a1bSJed Brown 7086cab3a1bSJed Brown Collective 7096cab3a1bSJed Brown 7104165533cSJose E. Roman Input Parameter: 71120f4b53cSBarry Smith . snes - `SNES` object to configure 7126cab3a1bSJed Brown 7136cab3a1bSJed Brown Level: developer 7146cab3a1bSJed Brown 715dc4c0fb0SBarry Smith Note: 716dc4c0fb0SBarry Smith If the matrices do not yet exist it attempts to create them based on options previously set for the `SNES` such as `-snes_mf` 717dc4c0fb0SBarry Smith 718dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetUp()` 7196cab3a1bSJed Brown @*/ 720d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetUpMatrices(SNES snes) 721d71ae5a4SJacob Faibussowitsch { 7226cab3a1bSJed Brown DM dm; 723942e3340SBarry Smith DMSNES sdm; 7246cab3a1bSJed Brown 7256cab3a1bSJed Brown PetscFunctionBegin; 7269566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 7279566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &sdm)); 72858b371f3SBarry Smith if (!snes->jacobian && snes->mf) { 7296cab3a1bSJed Brown Mat J; 7306cab3a1bSJed Brown void *functx; 7319566063dSJacob Faibussowitsch PetscCall(MatCreateSNESMF(snes, &J)); 7329566063dSJacob Faibussowitsch PetscCall(MatMFFDSetOptionsPrefix(J, ((PetscObject)snes)->prefix)); 7339566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(J)); 7349566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, NULL, NULL, &functx)); 7359566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, J, J, NULL, NULL)); 7369566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); 737caa4e7f2SJed Brown } else if (snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) { 7386cab3a1bSJed Brown Mat J, B; 7399566063dSJacob Faibussowitsch PetscCall(MatCreateSNESMF(snes, &J)); 7409566063dSJacob Faibussowitsch PetscCall(MatMFFDSetOptionsPrefix(J, ((PetscObject)snes)->prefix)); 7419566063dSJacob Faibussowitsch PetscCall(MatSetFromOptions(J)); 7429566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix(snes->dm, &B)); 74306f20277SJed Brown /* sdm->computejacobian was already set to reach here */ 7449566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, J, B, NULL, NULL)); 7459566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); 7469566063dSJacob Faibussowitsch PetscCall(MatDestroy(&B)); 747caa4e7f2SJed Brown } else if (!snes->jacobian_pre) { 7481ba9b98eSMatthew G. Knepley PetscDS prob; 7496cab3a1bSJed Brown Mat J, B; 7501ba9b98eSMatthew G. Knepley PetscBool hasPrec = PETSC_FALSE; 7511ba9b98eSMatthew G. Knepley 7526cab3a1bSJed Brown J = snes->jacobian; 7539566063dSJacob Faibussowitsch PetscCall(DMGetDS(dm, &prob)); 7549566063dSJacob Faibussowitsch if (prob) PetscCall(PetscDSHasJacobianPreconditioner(prob, &hasPrec)); 7559566063dSJacob Faibussowitsch if (J) PetscCall(PetscObjectReference((PetscObject)J)); 7569566063dSJacob Faibussowitsch else if (hasPrec) PetscCall(DMCreateMatrix(snes->dm, &J)); 7579566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix(snes->dm, &B)); 7589566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, J ? J : B, B, NULL, NULL)); 7599566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); 7609566063dSJacob Faibussowitsch PetscCall(MatDestroy(&B)); 7616cab3a1bSJed Brown } 762caa4e7f2SJed Brown { 763caa4e7f2SJed Brown KSP ksp; 7649566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, &ksp)); 7659566063dSJacob Faibussowitsch PetscCall(KSPSetComputeOperators(ksp, KSPComputeOperators_SNES, snes)); 7669566063dSJacob Faibussowitsch PetscCall(DMCoarsenHookAdd(snes->dm, DMCoarsenHook_SNESVecSol, DMRestrictHook_SNESVecSol, snes)); 767caa4e7f2SJed Brown } 7683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7696cab3a1bSJed Brown } 7706cab3a1bSJed Brown 771d71ae5a4SJacob Faibussowitsch static PetscErrorCode SNESMonitorPauseFinal_Internal(SNES snes) 772d71ae5a4SJacob Faibussowitsch { 7735e7c47f3SMatthew G. Knepley PetscInt i; 7745e7c47f3SMatthew G. Knepley 7755e7c47f3SMatthew G. Knepley PetscFunctionBegin; 7763ba16761SJacob Faibussowitsch if (!snes->pauseFinal) PetscFunctionReturn(PETSC_SUCCESS); 7775e7c47f3SMatthew G. Knepley for (i = 0; i < snes->numbermonitors; ++i) { 7785e7c47f3SMatthew G. Knepley PetscViewerAndFormat *vf = (PetscViewerAndFormat *)snes->monitorcontext[i]; 7795e7c47f3SMatthew G. Knepley PetscDraw draw; 7805e7c47f3SMatthew G. Knepley PetscReal lpause; 7815e7c47f3SMatthew G. Knepley 7825e7c47f3SMatthew G. Knepley if (!vf) continue; 7835e7c47f3SMatthew G. Knepley if (vf->lg) { 7845e7c47f3SMatthew G. Knepley if (!PetscCheckPointer(vf->lg, PETSC_OBJECT)) continue; 7855e7c47f3SMatthew G. Knepley if (((PetscObject)vf->lg)->classid != PETSC_DRAWLG_CLASSID) continue; 7869566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(vf->lg, &draw)); 7879566063dSJacob Faibussowitsch PetscCall(PetscDrawGetPause(draw, &lpause)); 7889566063dSJacob Faibussowitsch PetscCall(PetscDrawSetPause(draw, -1.0)); 7899566063dSJacob Faibussowitsch PetscCall(PetscDrawPause(draw)); 7909566063dSJacob Faibussowitsch PetscCall(PetscDrawSetPause(draw, lpause)); 7915e7c47f3SMatthew G. Knepley } else { 7925e7c47f3SMatthew G. Knepley PetscBool isdraw; 7935e7c47f3SMatthew G. Knepley 7945e7c47f3SMatthew G. Knepley if (!PetscCheckPointer(vf->viewer, PETSC_OBJECT)) continue; 7955e7c47f3SMatthew G. Knepley if (((PetscObject)vf->viewer)->classid != PETSC_VIEWER_CLASSID) continue; 7969566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)vf->viewer, PETSCVIEWERDRAW, &isdraw)); 7975e7c47f3SMatthew G. Knepley if (!isdraw) continue; 7989566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(vf->viewer, 0, &draw)); 7999566063dSJacob Faibussowitsch PetscCall(PetscDrawGetPause(draw, &lpause)); 8009566063dSJacob Faibussowitsch PetscCall(PetscDrawSetPause(draw, -1.0)); 8019566063dSJacob Faibussowitsch PetscCall(PetscDrawPause(draw)); 8029566063dSJacob Faibussowitsch PetscCall(PetscDrawSetPause(draw, lpause)); 8035e7c47f3SMatthew G. Knepley } 8045e7c47f3SMatthew G. Knepley } 8053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8065e7c47f3SMatthew G. Knepley } 8075e7c47f3SMatthew G. Knepley 808fde5950dSBarry Smith /*@C 809fde5950dSBarry Smith SNESMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 810fde5950dSBarry Smith 811c3339decSBarry Smith Collective 812fde5950dSBarry Smith 813fde5950dSBarry Smith Input Parameters: 814dc4c0fb0SBarry Smith + snes - `SNES` object you wish to monitor 815fde5950dSBarry Smith . name - the monitor type one is seeking 816fde5950dSBarry Smith . help - message indicating what monitoring is done 817fde5950dSBarry Smith . manual - manual page for the monitor 818fde5950dSBarry Smith . monitor - the monitor function 819f6dfbefdSBarry 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 820fde5950dSBarry Smith 821f6dfbefdSBarry Smith Options Database Key: 822f6dfbefdSBarry Smith . -name - trigger the use of this monitor in `SNESSetFromOptions()` 823f6dfbefdSBarry Smith 824f6dfbefdSBarry Smith Level: advanced 825fde5950dSBarry Smith 826dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`, 827db781477SPatrick Sanan `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()` 828db781477SPatrick Sanan `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`, `PetscOptionsBool()`, 829db781477SPatrick Sanan `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`, 830c2e3fba1SPatrick Sanan `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`, 831db781477SPatrick Sanan `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`, 832db781477SPatrick Sanan `PetscOptionsFList()`, `PetscOptionsEList()` 833fde5950dSBarry Smith @*/ 834d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESMonitorSetFromOptions(SNES snes, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(SNES, PetscInt, PetscReal, PetscViewerAndFormat *), PetscErrorCode (*monitorsetup)(SNES, PetscViewerAndFormat *)) 835d71ae5a4SJacob Faibussowitsch { 836fde5950dSBarry Smith PetscViewer viewer; 837fde5950dSBarry Smith PetscViewerFormat format; 838fde5950dSBarry Smith PetscBool flg; 839fde5950dSBarry Smith 840fde5950dSBarry Smith PetscFunctionBegin; 8419566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, name, &viewer, &format, &flg)); 842fde5950dSBarry Smith if (flg) { 843d43b4f6eSBarry Smith PetscViewerAndFormat *vf; 8449566063dSJacob Faibussowitsch PetscCall(PetscViewerAndFormatCreate(viewer, format, &vf)); 8459566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)viewer)); 8461baa6e33SBarry Smith if (monitorsetup) PetscCall((*monitorsetup)(snes, vf)); 8479566063dSJacob Faibussowitsch PetscCall(SNESMonitorSet(snes, (PetscErrorCode(*)(SNES, PetscInt, PetscReal, void *))monitor, vf, (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy)); 848fde5950dSBarry Smith } 8493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 850fde5950dSBarry Smith } 851fde5950dSBarry Smith 852a4598233SStefano Zampini PetscErrorCode SNESEWSetFromOptions_Private(SNESKSPEW *kctx, PetscBool print_api, MPI_Comm comm, const char *prefix) 853d71ae5a4SJacob Faibussowitsch { 854a4598233SStefano Zampini const char *api = print_api ? "SNESKSPSetParametersEW" : NULL; 855a4598233SStefano Zampini 8560f0abf79SStefano Zampini PetscFunctionBegin; 8570f0abf79SStefano Zampini PetscOptionsBegin(comm, prefix, "Eisenstat and Walker type forcing options", "KSP"); 858a4598233SStefano Zampini PetscCall(PetscOptionsInt("-ksp_ew_version", "Version 1, 2 or 3", api, kctx->version, &kctx->version, NULL)); 859a4598233SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_rtol0", "0 <= rtol0 < 1", api, kctx->rtol_0, &kctx->rtol_0, NULL)); 860a4598233SStefano Zampini kctx->rtol_max = PetscMax(kctx->rtol_0, kctx->rtol_max); 861a4598233SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_rtolmax", "0 <= rtolmax < 1", api, kctx->rtol_max, &kctx->rtol_max, NULL)); 862a4598233SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_gamma", "0 <= gamma <= 1", api, kctx->gamma, &kctx->gamma, NULL)); 863a4598233SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_alpha", "1 < alpha <= 2", api, kctx->alpha, &kctx->alpha, NULL)); 8640f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_alpha2", "alpha2", NULL, kctx->alpha2, &kctx->alpha2, NULL)); 865a4598233SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_threshold", "0 < threshold < 1", api, kctx->threshold, &kctx->threshold, NULL)); 8660f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_p1", "p1", NULL, kctx->v4_p1, &kctx->v4_p1, NULL)); 8670f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_p2", "p2", NULL, kctx->v4_p2, &kctx->v4_p2, NULL)); 8680f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_p3", "p3", NULL, kctx->v4_p3, &kctx->v4_p3, NULL)); 8690f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_m1", "Scaling when rk-1 in [p2,p3)", NULL, kctx->v4_m1, &kctx->v4_m1, NULL)); 8700f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_m2", "Scaling when rk-1 in [p3,+infty)", NULL, kctx->v4_m2, &kctx->v4_m2, NULL)); 8710f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_m3", "Threshold for successive rtol (0.1 in Eq.7)", NULL, kctx->v4_m3, &kctx->v4_m3, NULL)); 8720f0abf79SStefano Zampini PetscCall(PetscOptionsReal("-ksp_ew_v4_m4", "Adaptation scaling (0.5 in Eq.7)", NULL, kctx->v4_m4, &kctx->v4_m4, NULL)); 8730f0abf79SStefano Zampini PetscOptionsEnd(); 8743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8750f0abf79SStefano Zampini } 8760f0abf79SStefano Zampini 8779b94acceSBarry Smith /*@ 878f6dfbefdSBarry Smith SNESSetFromOptions - Sets various `SNES` and `KSP` parameters from user options. 8799b94acceSBarry Smith 880c3339decSBarry Smith Collective 881c7afd0dbSLois Curfman McInnes 8829b94acceSBarry Smith Input Parameter: 883f6dfbefdSBarry Smith . snes - the `SNES` context 8849b94acceSBarry Smith 88536851e7fSLois Curfman McInnes Options Database Keys: 886f6dfbefdSBarry Smith + -snes_type <type> - newtonls, newtontr, ngmres, ncg, nrichardson, qn, vi, fas, `SNESType` for complete list 88782738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 88882738288SBarry Smith of the change in the solution between steps 88970441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 890b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 891e4d06f11SPatrick Farrell . -snes_divergence_tolerance <divtol> - if the residual goes above divtol*rnorm0, exit with divergence 892be5caee7SBarry Smith . -snes_force_iteration <force> - force SNESSolve() to take at least one iteration 893b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 894b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 8954839bfe8SBarry Smith . -snes_max_fail <max_fail> - maximum number of line search failures allowed before stopping, default is none 896ddf469c8SBarry Smith . -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops 897a8054027SBarry Smith . -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild) 8983d5a8a6aSBarry Smith . -snes_lag_preconditioner_persists <true,false> - retains the -snes_lag_preconditioner information across multiple SNESSolve() 899e35cf81dSBarry Smith . -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild) 9003d5a8a6aSBarry Smith . -snes_lag_jacobian_persists <true,false> - retains the -snes_lag_jacobian information across multiple SNESSolve() 9014a221d59SStefano Zampini . -snes_tr_tol <trtol> - trust region tolerance 902f362779dSJed Brown . -snes_convergence_test - <default,skip,correct_pressure> convergence test in nonlinear solver. 903f6dfbefdSBarry Smith default `SNESConvergedDefault()`. skip `SNESConvergedSkip()` means continue iterating until max_it or some other criterion is reached, saving expense 904f6dfbefdSBarry Smith of convergence test. correct_pressure S`NESConvergedCorrectPressure()` has special handling of a pressure null space. 905fde5950dSBarry Smith . -snes_monitor [ascii][:filename][:viewer format] - prints residual norm at each iteration. if no filename given prints to stdout 906fde5950dSBarry Smith . -snes_monitor_solution [ascii binary draw][:filename][:viewer format] - plots solution at each iteration 907fde5950dSBarry Smith . -snes_monitor_residual [ascii binary draw][:filename][:viewer format] - plots residual (not its norm) at each iteration 908fde5950dSBarry Smith . -snes_monitor_solution_update [ascii binary draw][:filename][:viewer format] - plots update to solution at each iteration 9094619e776SBarry Smith . -snes_monitor_lg_residualnorm - plots residual norm at each iteration 910459f5d12SBarry Smith . -snes_monitor_lg_range - plots residual norm at each iteration 9115e7c47f3SMatthew G. Knepley . -snes_monitor_pause_final - Pauses all monitor drawing after the solver ends 912e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 913e2e60de9SPeter Brune . -snes_fd_color - use finite differences with coloring to compute Jacobian 9145968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 915b5badacbSBarry Smith . -snes_converged_reason - print the reason for convergence/divergence after each solve 916e62ac41dSBarry Smith . -npc_snes_type <type> - the SNES type to use as a nonlinear preconditioner 917e62ac41dSBarry 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. 918e62ac41dSBarry 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. 91982738288SBarry Smith 920f6dfbefdSBarry Smith Options Database Keys for Eisenstat-Walker method: 921fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 9224b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 92336851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 92436851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 92536851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 92636851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 92736851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 92836851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 92982738288SBarry Smith 930dc4c0fb0SBarry Smith Level: beginner 931dc4c0fb0SBarry Smith 93211ca99fdSLois Curfman McInnes Notes: 933ec5066bdSBarry Smith To see all options, run your program with the -help option or consult the users manual 934ec5066bdSBarry Smith 935f6dfbefdSBarry Smith `SNES` supports three approaches for computing (approximate) Jacobians: user provided via `SNESSetJacobian()`, matrix free, and computing explicitly with 936f6dfbefdSBarry Smith finite differences and coloring using `MatFDColoring`. It is also possible to use automatic differentiation and the `MatFDColoring` object. 93783e2fdc7SBarry Smith 938dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESType`, `SNESSetOptionsPrefix()`, `SNESResetFromOptions()`, `SNES`, `SNESCreate()` 9399b94acceSBarry Smith @*/ 940d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetFromOptions(SNES snes) 941d71ae5a4SJacob Faibussowitsch { 9428afaa268SBarry Smith PetscBool flg, pcset, persist, set; 943d8f46077SPeter Brune PetscInt i, indx, lag, grids; 94404d7464bSBarry Smith const char *deft = SNESNEWTONLS; 945649ef022SMatthew Knepley const char *convtests[] = {"default", "skip", "correct_pressure"}; 94685385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 9470f0abf79SStefano Zampini char type[256], monfilename[PETSC_MAX_PATH_LEN], ewprefix[256]; 948c40d0f55SPeter Brune PCSide pcside; 949a64e098fSPeter Brune const char *optionsprefix; 9509b94acceSBarry Smith 9513a40ed3dSBarry Smith PetscFunctionBegin; 9520700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 9539566063dSJacob Faibussowitsch PetscCall(SNESRegisterAll()); 954d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)snes); 955639ff905SBarry Smith if (((PetscObject)snes)->type_name) deft = ((PetscObject)snes)->type_name; 9569566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-snes_type", "Nonlinear solver method", "SNESSetType", SNESList, deft, type, 256, &flg)); 957d64ed03dSBarry Smith if (flg) { 9589566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes, type)); 9597adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 9609566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes, deft)); 961d64ed03dSBarry Smith } 9629566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_stol", "Stop if step length less than", "SNESSetTolerances", snes->stol, &snes->stol, NULL)); 9639566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_atol", "Stop if function norm less than", "SNESSetTolerances", snes->abstol, &snes->abstol, NULL)); 964186905e3SBarry Smith 9659566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_rtol", "Stop if decrease in function norm less than", "SNESSetTolerances", snes->rtol, &snes->rtol, NULL)); 9669566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_divergence_tolerance", "Stop if residual norm increases by this factor", "SNESSetDivergenceTolerance", snes->divtol, &snes->divtol, NULL)); 9679566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_max_it", "Maximum iterations", "SNESSetTolerances", snes->max_its, &snes->max_its, NULL)); 9689566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_max_funcs", "Maximum function evaluations", "SNESSetTolerances", snes->max_funcs, &snes->max_funcs, NULL)); 9699566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_max_fail", "Maximum nonlinear step failures", "SNESSetMaxNonlinearStepFailures", snes->maxFailures, &snes->maxFailures, NULL)); 9709566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_max_linear_solve_fail", "Maximum failures in linear solves allowed", "SNESSetMaxLinearSolveFailures", snes->maxLinearSolveFailures, &snes->maxLinearSolveFailures, NULL)); 9719566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_error_if_not_converged", "Generate error if solver does not converge", "SNESSetErrorIfNotConverged", snes->errorifnotconverged, &snes->errorifnotconverged, NULL)); 9729566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_force_iteration", "Force SNESSolve() to take at least one iteration", "SNESSetForceIteration", snes->forceiteration, &snes->forceiteration, NULL)); 9739566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_check_jacobian_domain_error", "Check Jacobian domain error after Jacobian evaluation", "SNESCheckJacobianDomainError", snes->checkjacdomainerror, &snes->checkjacdomainerror, NULL)); 97485385478SLisandro Dalcin 9759566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_lag_preconditioner", "How often to rebuild preconditioner", "SNESSetLagPreconditioner", snes->lagpreconditioner, &lag, &flg)); 976a8054027SBarry Smith if (flg) { 9775f80ce2aSJacob 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"); 9789566063dSJacob Faibussowitsch PetscCall(SNESSetLagPreconditioner(snes, lag)); 979a8054027SBarry Smith } 9809566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_lag_preconditioner_persists", "Preconditioner lagging through multiple SNES solves", "SNESSetLagPreconditionerPersists", snes->lagjac_persist, &persist, &flg)); 9811baa6e33SBarry Smith if (flg) PetscCall(SNESSetLagPreconditionerPersists(snes, persist)); 9829566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_lag_jacobian", "How often to rebuild Jacobian", "SNESSetLagJacobian", snes->lagjacobian, &lag, &flg)); 983e35cf81dSBarry Smith if (flg) { 9845f80ce2aSJacob 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"); 9859566063dSJacob Faibussowitsch PetscCall(SNESSetLagJacobian(snes, lag)); 986e35cf81dSBarry Smith } 9879566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_lag_jacobian_persists", "Jacobian lagging through multiple SNES solves", "SNESSetLagJacobianPersists", snes->lagjac_persist, &persist, &flg)); 9881baa6e33SBarry Smith if (flg) PetscCall(SNESSetLagJacobianPersists(snes, persist)); 98937ec4e1aSPeter Brune 9909566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_grid_sequence", "Use grid sequencing to generate initial guess", "SNESSetGridSequence", snes->gridsequence, &grids, &flg)); 9911baa6e33SBarry Smith if (flg) PetscCall(SNESSetGridSequence(snes, grids)); 992a8054027SBarry Smith 9939566063dSJacob Faibussowitsch PetscCall(PetscOptionsEList("-snes_convergence_test", "Convergence test", "SNESSetConvergenceTest", convtests, sizeof(convtests) / sizeof(char *), "default", &indx, &flg)); 99485385478SLisandro Dalcin if (flg) { 99585385478SLisandro Dalcin switch (indx) { 996d71ae5a4SJacob Faibussowitsch case 0: 997d71ae5a4SJacob Faibussowitsch PetscCall(SNESSetConvergenceTest(snes, SNESConvergedDefault, NULL, NULL)); 998d71ae5a4SJacob Faibussowitsch break; 999d71ae5a4SJacob Faibussowitsch case 1: 1000d71ae5a4SJacob Faibussowitsch PetscCall(SNESSetConvergenceTest(snes, SNESConvergedSkip, NULL, NULL)); 1001d71ae5a4SJacob Faibussowitsch break; 1002d71ae5a4SJacob Faibussowitsch case 2: 1003d71ae5a4SJacob Faibussowitsch PetscCall(SNESSetConvergenceTest(snes, SNESConvergedCorrectPressure, NULL, NULL)); 1004d71ae5a4SJacob Faibussowitsch break; 100585385478SLisandro Dalcin } 100685385478SLisandro Dalcin } 100785385478SLisandro Dalcin 10089566063dSJacob Faibussowitsch PetscCall(PetscOptionsEList("-snes_norm_schedule", "SNES Norm schedule", "SNESSetNormSchedule", SNESNormSchedules, 5, "function", &indx, &flg)); 10099566063dSJacob Faibussowitsch if (flg) PetscCall(SNESSetNormSchedule(snes, (SNESNormSchedule)indx)); 1010fdacfa88SPeter Brune 10119566063dSJacob Faibussowitsch PetscCall(PetscOptionsEList("-snes_function_type", "SNES Norm schedule", "SNESSetFunctionType", SNESFunctionTypes, 2, "unpreconditioned", &indx, &flg)); 10129566063dSJacob Faibussowitsch if (flg) PetscCall(SNESSetFunctionType(snes, (SNESFunctionType)indx)); 1013186905e3SBarry Smith 101485385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 101585385478SLisandro Dalcin 10169566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_ksp_ew", "Use Eisentat-Walker linear system convergence test", "SNESKSPSetUseEW", snes->ksp_ewconv, &snes->ksp_ewconv, NULL)); 1017186905e3SBarry Smith 10180f0abf79SStefano Zampini PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix)); 10190f0abf79SStefano Zampini PetscCall(PetscSNPrintf(ewprefix, sizeof(ewprefix), "%s%s", optionsprefix ? optionsprefix : "", "snes_")); 1020a4598233SStefano Zampini PetscCall(SNESEWSetFromOptions_Private(kctx, PETSC_TRUE, PetscObjectComm((PetscObject)snes), ewprefix)); 1021186905e3SBarry Smith 102290d69ab7SBarry Smith flg = PETSC_FALSE; 10239566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_monitor_cancel", "Remove all monitors", "SNESMonitorCancel", flg, &flg, &set)); 10249566063dSJacob Faibussowitsch if (set && flg) PetscCall(SNESMonitorCancel(snes)); 1025eabae89aSBarry Smith 10269566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor", "Monitor norm of function", "SNESMonitorDefault", SNESMonitorDefault, SNESMonitorDefaultSetUp)); 10279566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_short", "Monitor norm of function with fewer digits", "SNESMonitorDefaultShort", SNESMonitorDefaultShort, NULL)); 10289566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_range", "Monitor range of elements of function", "SNESMonitorRange", SNESMonitorRange, NULL)); 1029eabae89aSBarry Smith 10309566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_ratio", "Monitor ratios of the norm of function for consecutive steps", "SNESMonitorRatio", SNESMonitorRatio, SNESMonitorRatioSetUp)); 10319566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_field", "Monitor norm of function (split into fields)", "SNESMonitorDefaultField", SNESMonitorDefaultField, NULL)); 10329566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_solution", "View solution at each iteration", "SNESMonitorSolution", SNESMonitorSolution, NULL)); 10339566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_solution_update", "View correction at each iteration", "SNESMonitorSolutionUpdate", SNESMonitorSolutionUpdate, NULL)); 10349566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_residual", "View residual at each iteration", "SNESMonitorResidual", SNESMonitorResidual, NULL)); 10359566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_jacupdate_spectrum", "Print the change in the spectrum of the Jacobian", "SNESMonitorJacUpdateSpectrum", SNESMonitorJacUpdateSpectrum, NULL)); 10369566063dSJacob Faibussowitsch PetscCall(SNESMonitorSetFromOptions(snes, "-snes_monitor_fields", "Monitor norm of function per field", "SNESMonitorSet", SNESMonitorFields, NULL)); 10379566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_monitor_pause_final", "Pauses all draw monitors at the final iterate", "SNESMonitorPauseFinal_Internal", PETSC_FALSE, &snes->pauseFinal, NULL)); 10382db13446SMatthew G. Knepley 10399566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-snes_monitor_python", "Use Python function", "SNESMonitorSet", NULL, monfilename, sizeof(monfilename), &flg)); 10409566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)snes, monfilename)); 10415180491cSLisandro Dalcin 104290d69ab7SBarry Smith flg = PETSC_FALSE; 10439566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_monitor_lg_range", "Plot function range at each iteration", "SNESMonitorLGRange", flg, &flg, NULL)); 1044459f5d12SBarry Smith if (flg) { 1045459f5d12SBarry Smith PetscViewer ctx; 1046e24b481bSBarry Smith 10479566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, &ctx)); 10489566063dSJacob Faibussowitsch PetscCall(SNESMonitorSet(snes, SNESMonitorLGRange, ctx, (PetscErrorCode(*)(void **))PetscViewerDestroy)); 1049459f5d12SBarry Smith } 10502e7541e6SPeter Brune 105190d69ab7SBarry Smith flg = PETSC_FALSE; 10529566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_converged_reason_view_cancel", "Remove all converged reason viewers", "SNESConvergedReasonViewCancel", flg, &flg, &set)); 10539566063dSJacob Faibussowitsch if (set && flg) PetscCall(SNESConvergedReasonViewCancel(snes)); 1054c4421ceaSFande Kong 1055c4421ceaSFande Kong flg = PETSC_FALSE; 10569566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_fd", "Use finite differences (slow) to compute Jacobian", "SNESComputeJacobianDefault", flg, &flg, NULL)); 10574b27c08aSLois Curfman McInnes if (flg) { 10586cab3a1bSJed Brown void *functx; 1059b1f624c7SBarry Smith DM dm; 10609566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 1061800f99ffSJeremy L Thompson PetscCall(DMSNESUnsetJacobianContext_Internal(dm)); 10629566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, NULL, NULL, &functx)); 10639566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, snes->jacobian, snes->jacobian_pre, SNESComputeJacobianDefault, functx)); 10649566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Setting default finite difference Jacobian matrix\n")); 10659b94acceSBarry Smith } 1066639f9d9dSBarry Smith 106744848bc4SPeter Brune flg = PETSC_FALSE; 10689566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_fd_function", "Use finite differences (slow) to compute function from user objective", "SNESObjectiveComputeFunctionDefaultFD", flg, &flg, NULL)); 10691baa6e33SBarry Smith if (flg) PetscCall(SNESSetFunction(snes, NULL, SNESObjectiveComputeFunctionDefaultFD, NULL)); 107097584545SPeter Brune 107197584545SPeter Brune flg = PETSC_FALSE; 10729566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_fd_color", "Use finite differences with coloring to compute Jacobian", "SNESComputeJacobianDefaultColor", flg, &flg, NULL)); 107344848bc4SPeter Brune if (flg) { 1074c52e227fSPeter Brune DM dm; 10759566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 1076800f99ffSJeremy L Thompson PetscCall(DMSNESUnsetJacobianContext_Internal(dm)); 10779566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, snes->jacobian, snes->jacobian_pre, SNESComputeJacobianDefaultColor, NULL)); 10789566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Setting default finite difference coloring Jacobian matrix\n")); 107944848bc4SPeter Brune } 108044848bc4SPeter Brune 1081aa3661deSLisandro Dalcin flg = PETSC_FALSE; 10829566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_mf_operator", "Use a Matrix-Free Jacobian with user-provided preconditioner matrix", "SNESSetUseMatrixFree", PETSC_FALSE, &snes->mf_operator, &flg)); 1083d8f46077SPeter Brune if (flg && snes->mf_operator) { 1084a8248277SBarry Smith snes->mf_operator = PETSC_TRUE; 1085d8f46077SPeter Brune snes->mf = PETSC_TRUE; 1086a8248277SBarry Smith } 1087aa3661deSLisandro Dalcin flg = PETSC_FALSE; 10889566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_mf", "Use a Matrix-Free Jacobian with no preconditioner matrix", "SNESSetUseMatrixFree", PETSC_FALSE, &snes->mf, &flg)); 1089d8f46077SPeter Brune if (!flg && snes->mf_operator) snes->mf = PETSC_TRUE; 10909566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-snes_mf_version", "Matrix-Free routines version 1 or 2", "None", snes->mf_version, &snes->mf_version, NULL)); 1091d28543b3SPeter Brune 1092c40d0f55SPeter Brune flg = PETSC_FALSE; 10939566063dSJacob Faibussowitsch PetscCall(SNESGetNPCSide(snes, &pcside)); 10949566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-snes_npc_side", "SNES nonlinear preconditioner side", "SNESSetNPCSide", PCSides, (PetscEnum)pcside, (PetscEnum *)&pcside, &flg)); 10959566063dSJacob Faibussowitsch if (flg) PetscCall(SNESSetNPCSide(snes, pcside)); 1096c40d0f55SPeter Brune 1097e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 10988a70d858SHong Zhang /* 10998a70d858SHong Zhang Publish convergence information using SAWs 11008a70d858SHong Zhang */ 11018a70d858SHong Zhang flg = PETSC_FALSE; 11029566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_monitor_saws", "Publish SNES progress using SAWs", "SNESMonitorSet", flg, &flg, NULL)); 11038a70d858SHong Zhang if (flg) { 11048a70d858SHong Zhang void *ctx; 11059566063dSJacob Faibussowitsch PetscCall(SNESMonitorSAWsCreate(snes, &ctx)); 11069566063dSJacob Faibussowitsch PetscCall(SNESMonitorSet(snes, SNESMonitorSAWs, ctx, SNESMonitorSAWsDestroy)); 11078a70d858SHong Zhang } 11088a70d858SHong Zhang #endif 11098a70d858SHong Zhang #if defined(PETSC_HAVE_SAWS) 1110b90c6cbeSBarry Smith { 1111b90c6cbeSBarry Smith PetscBool set; 1112b90c6cbeSBarry Smith flg = PETSC_FALSE; 11139566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-snes_saws_block", "Block for SAWs at end of SNESSolve", "PetscObjectSAWsBlock", ((PetscObject)snes)->amspublishblock, &flg, &set)); 11141baa6e33SBarry Smith if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)snes, flg)); 1115b90c6cbeSBarry Smith } 1116b90c6cbeSBarry Smith #endif 1117b90c6cbeSBarry Smith 111848a46eb9SPierre Jolivet for (i = 0; i < numberofsetfromoptions; i++) PetscCall((*othersetfromoptions[i])(snes)); 111976b2cf59SMatthew Knepley 1120dbbe0bcdSBarry Smith PetscTryTypeMethod(snes, setfromoptions, PetscOptionsObject); 11215d973c19SBarry Smith 11225d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 1123dbbe0bcdSBarry Smith PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)snes, PetscOptionsObject)); 1124d0609cedSBarry Smith PetscOptionsEnd(); 11254bbc92c1SBarry Smith 1126d8d34be6SBarry Smith if (snes->linesearch) { 11279566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &snes->linesearch)); 11289566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetFromOptions(snes->linesearch)); 1129d8d34be6SBarry Smith } 11309e764e56SPeter Brune 11316aa5e7e9SBarry Smith if (snes->usesksp) { 11329566063dSJacob Faibussowitsch if (!snes->ksp) PetscCall(SNESGetKSP(snes, &snes->ksp)); 11339566063dSJacob Faibussowitsch PetscCall(KSPSetOperators(snes->ksp, snes->jacobian, snes->jacobian_pre)); 11349566063dSJacob Faibussowitsch PetscCall(KSPSetFromOptions(snes->ksp)); 11356aa5e7e9SBarry Smith } 11366991f827SBarry Smith 1137b5badacbSBarry Smith /* if user has set the SNES NPC type via options database, create it. */ 11389566063dSJacob Faibussowitsch PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix)); 11399566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(((PetscObject)snes)->options, optionsprefix, "-npc_snes_type", &pcset)); 114048a46eb9SPierre Jolivet if (pcset && (!snes->npc)) PetscCall(SNESGetNPC(snes, &snes->npc)); 11411baa6e33SBarry Smith if (snes->npc) PetscCall(SNESSetFromOptions(snes->npc)); 1142b3cd9a81SMatthew G. Knepley snes->setfromoptionscalled++; 11433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1144b3cd9a81SMatthew G. Knepley } 1145b3cd9a81SMatthew G. Knepley 1146b3cd9a81SMatthew G. Knepley /*@ 1147f6dfbefdSBarry Smith SNESResetFromOptions - Sets various `SNES` and `KSP` parameters from user options ONLY if the `SNESSetFromOptions()` was previously set from options 1148b3cd9a81SMatthew G. Knepley 1149c3339decSBarry Smith Collective 1150b3cd9a81SMatthew G. Knepley 1151b3cd9a81SMatthew G. Knepley Input Parameter: 1152f6dfbefdSBarry Smith . snes - the `SNES` context 1153b3cd9a81SMatthew G. Knepley 1154b3cd9a81SMatthew G. Knepley Level: beginner 1155b3cd9a81SMatthew G. Knepley 1156dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetFromOptions()`, `SNESSetOptionsPrefix()` 1157b3cd9a81SMatthew G. Knepley @*/ 1158d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESResetFromOptions(SNES snes) 1159d71ae5a4SJacob Faibussowitsch { 1160b3cd9a81SMatthew G. Knepley PetscFunctionBegin; 11619566063dSJacob Faibussowitsch if (snes->setfromoptionscalled) PetscCall(SNESSetFromOptions(snes)); 11623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 11639b94acceSBarry Smith } 11649b94acceSBarry Smith 1165bb9467b5SJed Brown /*@C 1166d25893d9SBarry Smith SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for 1167d25893d9SBarry Smith the nonlinear solvers. 1168d25893d9SBarry Smith 1169dc4c0fb0SBarry Smith Logically Collective; No Fortran Support 1170d25893d9SBarry Smith 1171d25893d9SBarry Smith Input Parameters: 1172f6dfbefdSBarry Smith + snes - the `SNES` context 1173d25893d9SBarry Smith . compute - function to compute the context 1174d25893d9SBarry Smith - destroy - function to destroy the context 1175d25893d9SBarry Smith 1176d25893d9SBarry Smith Level: intermediate 1177d25893d9SBarry Smith 1178f6dfbefdSBarry Smith Note: 1179f6dfbefdSBarry Smith This routine is useful if you are performing grid sequencing or using `SNESFAS` and need the appropriate context generated for each level. 1180f6dfbefdSBarry Smith 1181f6dfbefdSBarry Smith Use `SNESSetApplicationContext()` to see the context immediately 1182f6dfbefdSBarry Smith 1183dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESGetApplicationContext()`, `SNESSetComputeApplicationContext()`, `SNESSetApplicationContext()` 1184d25893d9SBarry Smith @*/ 1185d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetComputeApplicationContext(SNES snes, PetscErrorCode (*compute)(SNES, void **), PetscErrorCode (*destroy)(void **)) 1186d71ae5a4SJacob Faibussowitsch { 1187d25893d9SBarry Smith PetscFunctionBegin; 1188d25893d9SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1189d25893d9SBarry Smith snes->ops->usercompute = compute; 1190d25893d9SBarry Smith snes->ops->userdestroy = destroy; 11913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1192d25893d9SBarry Smith } 1193a847f771SSatish Balay 1194b07ff414SBarry Smith /*@ 1195f6dfbefdSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for the nonlinear solvers. 11969b94acceSBarry Smith 1197c3339decSBarry Smith Logically Collective 1198fee21e36SBarry Smith 1199c7afd0dbSLois Curfman McInnes Input Parameters: 1200f6dfbefdSBarry Smith + snes - the `SNES` context 1201c7afd0dbSLois Curfman McInnes - usrP - optional user context 1202c7afd0dbSLois Curfman McInnes 120336851e7fSLois Curfman McInnes Level: intermediate 120436851e7fSLois Curfman McInnes 1205f6dfbefdSBarry Smith Notes: 1206f6dfbefdSBarry Smith Users can provide a context when constructing the `SNES` options and then access it inside their function, Jacobian, or other evaluation function 1207f6dfbefdSBarry Smith with `SNESGetApplicationContext()` 1208f6dfbefdSBarry Smith 1209f6dfbefdSBarry Smith To provide a function that computes the context for you use `SNESSetComputeApplicationContext()` 1210f6dfbefdSBarry Smith 1211f6dfbefdSBarry Smith Fortran Note: 1212dc4c0fb0SBarry Smith You must write a Fortran interface definition for this 1213daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1214daf670e6SBarry Smith 1215dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetComputeApplicationContext()`, `SNESGetApplicationContext()` 12169b94acceSBarry Smith @*/ 1217d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetApplicationContext(SNES snes, void *usrP) 1218d71ae5a4SJacob Faibussowitsch { 1219b07ff414SBarry Smith KSP ksp; 12201b2093e4SBarry Smith 12213a40ed3dSBarry Smith PetscFunctionBegin; 12220700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 12239566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, &ksp)); 12249566063dSJacob Faibussowitsch PetscCall(KSPSetApplicationContext(ksp, usrP)); 12259b94acceSBarry Smith snes->user = usrP; 12263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12279b94acceSBarry Smith } 122874679c65SBarry Smith 1229b07ff414SBarry Smith /*@ 12309b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 1231f6dfbefdSBarry Smith nonlinear solvers set with `SNESGetApplicationContext()` or with `SNESSetComputeApplicationContext()` 12329b94acceSBarry Smith 1233c7afd0dbSLois Curfman McInnes Not Collective 1234c7afd0dbSLois Curfman McInnes 12359b94acceSBarry Smith Input Parameter: 1236f6dfbefdSBarry Smith . snes - `SNES` context 12379b94acceSBarry Smith 12389b94acceSBarry Smith Output Parameter: 12399b94acceSBarry Smith . usrP - user context 12409b94acceSBarry Smith 124136851e7fSLois Curfman McInnes Level: intermediate 124236851e7fSLois Curfman McInnes 1243dc4c0fb0SBarry Smith Fortran Note: 1244dc4c0fb0SBarry Smith You must write a Fortran interface definition for this 1245dc4c0fb0SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1246dc4c0fb0SBarry Smith 1247dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetApplicationContext()` 12489b94acceSBarry Smith @*/ 1249d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetApplicationContext(SNES snes, void *usrP) 1250d71ae5a4SJacob Faibussowitsch { 12513a40ed3dSBarry Smith PetscFunctionBegin; 12520700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1253e71120c6SJed Brown *(void **)usrP = snes->user; 12543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12559b94acceSBarry Smith } 125674679c65SBarry Smith 12579b94acceSBarry Smith /*@ 1258f6dfbefdSBarry Smith SNESSetUseMatrixFree - indicates that `SNES` should use matrix free finite difference matrix vector products to apply the Jacobian. 12593565c898SBarry Smith 1260dc4c0fb0SBarry Smith Logically Collective 12613565c898SBarry Smith 12623565c898SBarry Smith Input Parameters: 1263f6dfbefdSBarry Smith + snes - `SNES` context 1264f6dfbefdSBarry Smith . mf_operator - use matrix-free only for the Amat used by `SNESSetJacobian()`, this means the user provided Pmat will continue to be used 1265f6dfbefdSBarry Smith - mf - use matrix-free for both the Amat and Pmat used by `SNESSetJacobian()`, both the Amat and Pmat set in `SNESSetJacobian()` will be ignored. With 1266f6dfbefdSBarry Smith this option no matrix element based preconditioners can be used in the linear solve since the matrix won't be explicitly available 12673565c898SBarry Smith 1268f6dfbefdSBarry Smith Options Database Keys: 1269f6dfbefdSBarry Smith + -snes_mf_operator - use matrix free only for the mat operator 1270f6dfbefdSBarry Smith . -snes_mf - use matrix-free for both the mat and pmat operator 1271ec5066bdSBarry Smith . -snes_fd_color - compute the Jacobian via coloring and finite differences. 1272ec5066bdSBarry Smith - -snes_fd - compute the Jacobian via finite differences (slow) 12733565c898SBarry Smith 12743565c898SBarry Smith Level: intermediate 12753565c898SBarry Smith 1276f6dfbefdSBarry Smith Note: 1277dc4c0fb0SBarry Smith `SNES` supports three approaches for computing (approximate) Jacobians: user provided via `SNESSetJacobian()`, matrix-free, and computing explicitly with 1278f6dfbefdSBarry Smith finite differences and coloring using `MatFDColoring`. It is also possible to use automatic differentiation and the `MatFDColoring` object. 1279ec5066bdSBarry Smith 1280dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetUseMatrixFree()`, `MatCreateSNESMF()`, `SNESComputeJacobianDefaultColor()` 12813565c898SBarry Smith @*/ 1282d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetUseMatrixFree(SNES snes, PetscBool mf_operator, PetscBool mf) 1283d71ae5a4SJacob Faibussowitsch { 12843565c898SBarry Smith PetscFunctionBegin; 12853565c898SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 128688b4c220SStefano Zampini PetscValidLogicalCollectiveBool(snes, mf_operator, 2); 128788b4c220SStefano Zampini PetscValidLogicalCollectiveBool(snes, mf, 3); 12884ddffce6SLisandro Dalcin snes->mf = mf_operator ? PETSC_TRUE : mf; 12893565c898SBarry Smith snes->mf_operator = mf_operator; 12903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12913565c898SBarry Smith } 12923565c898SBarry Smith 12933565c898SBarry Smith /*@ 1294dc4c0fb0SBarry Smith SNESGetUseMatrixFree - indicates if the `SNES` uses matrix-free finite difference matrix vector products to apply the Jacobian. 12953565c898SBarry Smith 1296f6dfbefdSBarry Smith Not Collective, but the resulting flags will be the same on all MPI ranks 12973565c898SBarry Smith 12983565c898SBarry Smith Input Parameter: 1299f6dfbefdSBarry Smith . snes - `SNES` context 13003565c898SBarry Smith 13013565c898SBarry Smith Output Parameters: 1302f6dfbefdSBarry Smith + mf_operator - use matrix-free only for the Amat used by `SNESSetJacobian()`, this means the user provided Pmat will continue to be used 1303f6dfbefdSBarry Smith - mf - use matrix-free for both the Amat and Pmat used by `SNESSetJacobian()`, both the Amat and Pmat set in `SNESSetJacobian()` will be ignored 13043565c898SBarry Smith 13053565c898SBarry Smith Level: intermediate 13063565c898SBarry Smith 1307dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetUseMatrixFree()`, `MatCreateSNESMF()` 13083565c898SBarry Smith @*/ 1309d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetUseMatrixFree(SNES snes, PetscBool *mf_operator, PetscBool *mf) 1310d71ae5a4SJacob Faibussowitsch { 13113565c898SBarry Smith PetscFunctionBegin; 13123565c898SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 13133565c898SBarry Smith if (mf) *mf = snes->mf; 13143565c898SBarry Smith if (mf_operator) *mf_operator = snes->mf_operator; 13153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 13163565c898SBarry Smith } 13173565c898SBarry Smith 13183565c898SBarry Smith /*@ 1319c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 1320c8228a4eSBarry Smith at this time. 13219b94acceSBarry Smith 1322c7afd0dbSLois Curfman McInnes Not Collective 1323c7afd0dbSLois Curfman McInnes 13249b94acceSBarry Smith Input Parameter: 1325f6dfbefdSBarry Smith . snes - `SNES` context 13269b94acceSBarry Smith 13279b94acceSBarry Smith Output Parameter: 13289b94acceSBarry Smith . iter - iteration number 13299b94acceSBarry Smith 1330dc4c0fb0SBarry Smith Level: intermediate 1331dc4c0fb0SBarry Smith 1332c8228a4eSBarry Smith Notes: 1333c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 1334c8228a4eSBarry Smith 1335c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 1336f6dfbefdSBarry Smith Jacobian at each `SNES` iteration). For example, the code 133708405cd6SLois Curfman McInnes .vb 133808405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 133908405cd6SLois Curfman McInnes if (!(it % 2)) { 134008405cd6SLois Curfman McInnes [compute Jacobian here] 134108405cd6SLois Curfman McInnes } 134208405cd6SLois Curfman McInnes .ve 1343f6dfbefdSBarry Smith can be used in your function that computes the Jacobian to cause the Jacobian to be 1344f6dfbefdSBarry Smith recomputed every second `SNES` iteration. See also `SNESSetLagJacobian()` 1345c8228a4eSBarry Smith 1346f6dfbefdSBarry Smith After the `SNES` solve is complete this will return the number of nonlinear iterations used. 1347c04deec6SBarry Smith 1348dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESSetLagJacobian()`, `SNESGetLinearSolveIterations()` 13499b94acceSBarry Smith @*/ 1350d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetIterationNumber(SNES snes, PetscInt *iter) 1351d71ae5a4SJacob Faibussowitsch { 13523a40ed3dSBarry Smith PetscFunctionBegin; 13530700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 13544482741eSBarry Smith PetscValidIntPointer(iter, 2); 13559b94acceSBarry Smith *iter = snes->iter; 13563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 13579b94acceSBarry Smith } 135874679c65SBarry Smith 1359360c497dSPeter Brune /*@ 1360360c497dSPeter Brune SNESSetIterationNumber - Sets the current iteration number. 1361360c497dSPeter Brune 1362360c497dSPeter Brune Not Collective 1363360c497dSPeter Brune 1364d8d19677SJose E. Roman Input Parameters: 1365f6dfbefdSBarry Smith + snes - `SNES` context 1366a2b725a8SWilliam Gropp - iter - iteration number 1367360c497dSPeter Brune 1368360c497dSPeter Brune Level: developer 1369360c497dSPeter Brune 1370dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESGetLinearSolveIterations()` 1371360c497dSPeter Brune @*/ 1372d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetIterationNumber(SNES snes, PetscInt iter) 1373d71ae5a4SJacob Faibussowitsch { 1374360c497dSPeter Brune PetscFunctionBegin; 1375360c497dSPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 13769566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsTakeAccess((PetscObject)snes)); 1377360c497dSPeter Brune snes->iter = iter; 13789566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsGrantAccess((PetscObject)snes)); 13793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1380360c497dSPeter Brune } 1381360c497dSPeter Brune 13829b94acceSBarry Smith /*@ 1383b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 13849b94acceSBarry Smith attempted by the nonlinear solver. 13859b94acceSBarry Smith 1386c7afd0dbSLois Curfman McInnes Not Collective 1387c7afd0dbSLois Curfman McInnes 13889b94acceSBarry Smith Input Parameter: 1389f6dfbefdSBarry Smith . snes - `SNES` context 13909b94acceSBarry Smith 13919b94acceSBarry Smith Output Parameter: 13929b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 13939b94acceSBarry Smith 1394dc4c0fb0SBarry Smith Level: intermediate 1395dc4c0fb0SBarry Smith 1396f6dfbefdSBarry Smith Note: 1397f6dfbefdSBarry Smith This counter is reset to zero for each successive call to `SNESSolve()`. 1398c96a6f78SLois Curfman McInnes 1399dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`, 1400db781477SPatrick Sanan `SNESSetMaxNonlinearStepFailures()`, `SNESGetMaxNonlinearStepFailures()` 14019b94acceSBarry Smith @*/ 1402d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetNonlinearStepFailures(SNES snes, PetscInt *nfails) 1403d71ae5a4SJacob Faibussowitsch { 14043a40ed3dSBarry Smith PetscFunctionBegin; 14050700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 14064482741eSBarry Smith PetscValidIntPointer(nfails, 2); 140750ffb88aSMatthew Knepley *nfails = snes->numFailures; 14083ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 140950ffb88aSMatthew Knepley } 141050ffb88aSMatthew Knepley 141150ffb88aSMatthew Knepley /*@ 1412b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 1413f6dfbefdSBarry Smith attempted by the nonlinear solver before it gives up and generates an error 141450ffb88aSMatthew Knepley 141550ffb88aSMatthew Knepley Not Collective 141650ffb88aSMatthew Knepley 141750ffb88aSMatthew Knepley Input Parameters: 1418f6dfbefdSBarry Smith + snes - `SNES` context 141950ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 142050ffb88aSMatthew Knepley 142150ffb88aSMatthew Knepley Level: intermediate 142250ffb88aSMatthew Knepley 1423dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetErrorIfNotConverged()`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`, 1424db781477SPatrick Sanan `SNESGetMaxNonlinearStepFailures()`, `SNESGetNonlinearStepFailures()` 142550ffb88aSMatthew Knepley @*/ 1426d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 1427d71ae5a4SJacob Faibussowitsch { 142850ffb88aSMatthew Knepley PetscFunctionBegin; 14290700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 143050ffb88aSMatthew Knepley snes->maxFailures = maxFails; 14313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 143250ffb88aSMatthew Knepley } 143350ffb88aSMatthew Knepley 143450ffb88aSMatthew Knepley /*@ 1435b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 1436f6dfbefdSBarry Smith attempted by the nonlinear solver before it gives up and generates an error 143750ffb88aSMatthew Knepley 143850ffb88aSMatthew Knepley Not Collective 143950ffb88aSMatthew Knepley 144050ffb88aSMatthew Knepley Input Parameter: 144120f4b53cSBarry Smith . snes - `SNES` context 144250ffb88aSMatthew Knepley 144350ffb88aSMatthew Knepley Output Parameter: 144450ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 144550ffb88aSMatthew Knepley 144650ffb88aSMatthew Knepley Level: intermediate 144750ffb88aSMatthew Knepley 1448dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetErrorIfNotConverged()`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`, 1449db781477SPatrick Sanan `SNESSetMaxNonlinearStepFailures()`, `SNESGetNonlinearStepFailures()` 145050ffb88aSMatthew Knepley @*/ 1451d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 1452d71ae5a4SJacob Faibussowitsch { 145350ffb88aSMatthew Knepley PetscFunctionBegin; 14540700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 14554482741eSBarry Smith PetscValidIntPointer(maxFails, 2); 145650ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 14573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 14589b94acceSBarry Smith } 1459a847f771SSatish Balay 14602541af92SBarry Smith /*@ 14612541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 1462f6dfbefdSBarry Smith done by the `SNES` object 14632541af92SBarry Smith 14642541af92SBarry Smith Not Collective 14652541af92SBarry Smith 14662541af92SBarry Smith Input Parameter: 1467f6dfbefdSBarry Smith . snes - `SNES` context 14682541af92SBarry Smith 14692541af92SBarry Smith Output Parameter: 14702541af92SBarry Smith . nfuncs - number of evaluations 14712541af92SBarry Smith 14722541af92SBarry Smith Level: intermediate 14732541af92SBarry Smith 1474f6dfbefdSBarry Smith Note: 1475f6dfbefdSBarry Smith Reset every time `SNESSolve()` is called unless `SNESSetCountersReset()` is used. 1476971e163fSPeter Brune 1477dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, `SNESGetLinearSolveFailures()`, `SNESSetCountersReset()` 14782541af92SBarry Smith @*/ 1479d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 1480d71ae5a4SJacob Faibussowitsch { 14812541af92SBarry Smith PetscFunctionBegin; 14820700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 14832541af92SBarry Smith PetscValidIntPointer(nfuncs, 2); 14842541af92SBarry Smith *nfuncs = snes->nfuncs; 14853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 14862541af92SBarry Smith } 14872541af92SBarry Smith 14883d4c4710SBarry Smith /*@ 14893d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 14903d4c4710SBarry Smith linear solvers. 14913d4c4710SBarry Smith 14923d4c4710SBarry Smith Not Collective 14933d4c4710SBarry Smith 14943d4c4710SBarry Smith Input Parameter: 1495f6dfbefdSBarry Smith . snes - `SNES` context 14963d4c4710SBarry Smith 14973d4c4710SBarry Smith Output Parameter: 14983d4c4710SBarry Smith . nfails - number of failed solves 14993d4c4710SBarry Smith 1500f6dfbefdSBarry Smith Options Database Key: 15019d85da0cSMatthew G. Knepley . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated 15029d85da0cSMatthew G. Knepley 1503f6dfbefdSBarry Smith Level: intermediate 1504f6dfbefdSBarry Smith 1505f6dfbefdSBarry Smith Note: 1506f6dfbefdSBarry Smith This counter is reset to zero for each successive call to `SNESSolve()`. 15073d4c4710SBarry Smith 1508dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()` 15093d4c4710SBarry Smith @*/ 1510d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetLinearSolveFailures(SNES snes, PetscInt *nfails) 1511d71ae5a4SJacob Faibussowitsch { 15123d4c4710SBarry Smith PetscFunctionBegin; 15130700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 15143d4c4710SBarry Smith PetscValidIntPointer(nfails, 2); 15153d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 15163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15173d4c4710SBarry Smith } 15183d4c4710SBarry Smith 15193d4c4710SBarry Smith /*@ 15203d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 1521f6dfbefdSBarry Smith allowed before `SNES` returns with a diverged reason of `SNES_DIVERGED_LINEAR_SOLVE` 15223d4c4710SBarry Smith 1523c3339decSBarry Smith Logically Collective 15243d4c4710SBarry Smith 15253d4c4710SBarry Smith Input Parameters: 1526f6dfbefdSBarry Smith + snes - `SNES` context 15273d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 15283d4c4710SBarry Smith 1529f6dfbefdSBarry Smith Options Database Key: 15309d85da0cSMatthew G. Knepley . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated 15319d85da0cSMatthew G. Knepley 1532dc4c0fb0SBarry Smith Level: intermediate 1533dc4c0fb0SBarry Smith 1534f6dfbefdSBarry Smith Note: 1535f6dfbefdSBarry Smith By default this is 0; that is `SNES` returns on the first failed linear solve 15363d4c4710SBarry Smith 1537dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetErrorIfNotConverged()`, `SNESGetLinearSolveFailures()`, `SNESGetMaxLinearSolveFailures()`, `SNESGetLinearSolveIterations()` 15383d4c4710SBarry Smith @*/ 1539d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 1540d71ae5a4SJacob Faibussowitsch { 15413d4c4710SBarry Smith PetscFunctionBegin; 15420700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1543c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes, maxFails, 2); 15443d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 15453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15463d4c4710SBarry Smith } 15473d4c4710SBarry Smith 15483d4c4710SBarry Smith /*@ 15493d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 1550f6dfbefdSBarry Smith are allowed before `SNES` returns as unsuccessful 15513d4c4710SBarry Smith 15523d4c4710SBarry Smith Not Collective 15533d4c4710SBarry Smith 15543d4c4710SBarry Smith Input Parameter: 1555f6dfbefdSBarry Smith . snes - `SNES` context 15563d4c4710SBarry Smith 15573d4c4710SBarry Smith Output Parameter: 15583d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 15593d4c4710SBarry Smith 15603d4c4710SBarry Smith Level: intermediate 15613d4c4710SBarry Smith 1562f6dfbefdSBarry Smith Note: 1563f6dfbefdSBarry Smith By default this is 1; that is `SNES` returns on the first failed linear solve 15643d4c4710SBarry Smith 1565dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetErrorIfNotConverged()`, `SNESGetLinearSolveFailures()`, `SNESGetLinearSolveIterations()`, `SNESSetMaxLinearSolveFailures()`, 15663d4c4710SBarry Smith @*/ 1567d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 1568d71ae5a4SJacob Faibussowitsch { 15693d4c4710SBarry Smith PetscFunctionBegin; 15700700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 15713d4c4710SBarry Smith PetscValidIntPointer(maxFails, 2); 15723d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 15733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 15743d4c4710SBarry Smith } 15753d4c4710SBarry Smith 1576c96a6f78SLois Curfman McInnes /*@ 1577b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 1578c96a6f78SLois Curfman McInnes used by the nonlinear solver. 1579c96a6f78SLois Curfman McInnes 1580c7afd0dbSLois Curfman McInnes Not Collective 1581c7afd0dbSLois Curfman McInnes 1582c96a6f78SLois Curfman McInnes Input Parameter: 1583f6dfbefdSBarry Smith . snes - `SNES` context 1584c96a6f78SLois Curfman McInnes 1585c96a6f78SLois Curfman McInnes Output Parameter: 1586c96a6f78SLois Curfman McInnes . lits - number of linear iterations 1587c96a6f78SLois Curfman McInnes 1588dc4c0fb0SBarry Smith Level: intermediate 1589dc4c0fb0SBarry Smith 1590c96a6f78SLois Curfman McInnes Notes: 1591f6dfbefdSBarry Smith This counter is reset to zero for each successive call to `SNESSolve()` unless `SNESSetCountersReset()` is used. 1592c96a6f78SLois Curfman McInnes 1593f6dfbefdSBarry 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 1594f6dfbefdSBarry Smith then call `KSPGetIterationNumber()` after the failed solve. 1595010be392SBarry Smith 1596dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetIterationNumber()`, `SNESGetLinearSolveFailures()`, `SNESGetMaxLinearSolveFailures()`, `SNESSetCountersReset()` 1597c96a6f78SLois Curfman McInnes @*/ 1598d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetLinearSolveIterations(SNES snes, PetscInt *lits) 1599d71ae5a4SJacob Faibussowitsch { 16003a40ed3dSBarry Smith PetscFunctionBegin; 16010700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 16024482741eSBarry Smith PetscValidIntPointer(lits, 2); 1603c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 16043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1605c96a6f78SLois Curfman McInnes } 1606c96a6f78SLois Curfman McInnes 1607971e163fSPeter Brune /*@ 1608971e163fSPeter Brune SNESSetCountersReset - Sets whether or not the counters for linear iterations and function evaluations 1609f6dfbefdSBarry Smith are reset every time `SNESSolve()` is called. 1610971e163fSPeter Brune 1611c3339decSBarry Smith Logically Collective 1612971e163fSPeter Brune 1613d8d19677SJose E. Roman Input Parameters: 1614f6dfbefdSBarry Smith + snes - `SNES` context 1615f6dfbefdSBarry Smith - reset - whether to reset the counters or not, defaults to `PETSC_TRUE` 1616971e163fSPeter Brune 1617971e163fSPeter Brune Level: developer 1618971e163fSPeter Brune 1619dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESGetNumberFunctionEvals()`, `SNESGetLinearSolveIterations()`, `SNESGetNPC()` 1620971e163fSPeter Brune @*/ 1621d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetCountersReset(SNES snes, PetscBool reset) 1622d71ae5a4SJacob Faibussowitsch { 1623971e163fSPeter Brune PetscFunctionBegin; 1624971e163fSPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1625971e163fSPeter Brune PetscValidLogicalCollectiveBool(snes, reset, 2); 1626971e163fSPeter Brune snes->counters_reset = reset; 16273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1628971e163fSPeter Brune } 1629971e163fSPeter Brune 16302999313aSBarry Smith /*@ 1631f6dfbefdSBarry Smith SNESSetKSP - Sets a `KSP` context for the `SNES` object to use 16322999313aSBarry Smith 1633f6dfbefdSBarry Smith Not Collective, but the `SNES` and `KSP` objects must live on the same MPI_Comm 16342999313aSBarry Smith 16352999313aSBarry Smith Input Parameters: 1636f6dfbefdSBarry Smith + snes - the `SNES` context 1637f6dfbefdSBarry Smith - ksp - the `KSP` context 16382999313aSBarry Smith 1639dc4c0fb0SBarry Smith Level: developer 1640dc4c0fb0SBarry Smith 16412999313aSBarry Smith Notes: 1642f6dfbefdSBarry Smith The `SNES` object already has its `KSP` object, you can obtain with `SNESGetKSP()` 16432999313aSBarry Smith so this routine is rarely needed. 16442999313aSBarry Smith 1645f6dfbefdSBarry Smith The `KSP` object that is already in the `SNES` object has its reference count 16462999313aSBarry Smith decreased by one. 16472999313aSBarry Smith 1648dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `KSP`, `KSPGetPC()`, `SNESCreate()`, `KSPCreate()`, `SNESSetKSP()` 16492999313aSBarry Smith @*/ 1650d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetKSP(SNES snes, KSP ksp) 1651d71ae5a4SJacob Faibussowitsch { 16522999313aSBarry Smith PetscFunctionBegin; 16530700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 16540700a824SBarry Smith PetscValidHeaderSpecific(ksp, KSP_CLASSID, 2); 16552999313aSBarry Smith PetscCheckSameComm(snes, 1, ksp, 2); 16569566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ksp)); 16579566063dSJacob Faibussowitsch if (snes->ksp) PetscCall(PetscObjectDereference((PetscObject)snes->ksp)); 16582999313aSBarry Smith snes->ksp = ksp; 16593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 16602999313aSBarry Smith } 16612999313aSBarry Smith 166252baeb72SSatish Balay /*@ 1663dc4c0fb0SBarry Smith SNESCreate - Creates a nonlinear solver context used to manage a set of nonlinear solves 16649b94acceSBarry Smith 1665d083f849SBarry Smith Collective 1666c7afd0dbSLois Curfman McInnes 1667f6dfbefdSBarry Smith Input Parameter: 1668906ed7ccSBarry Smith . comm - MPI communicator 16699b94acceSBarry Smith 16709b94acceSBarry Smith Output Parameter: 167120f4b53cSBarry Smith . outsnes - the new `SNES` context 16729b94acceSBarry Smith 1673c7afd0dbSLois Curfman McInnes Options Database Keys: 1674dc4c0fb0SBarry Smith + -snes_mf - Activates default matrix-free Jacobian-vector products, and no preconditioning matrix 1675dc4c0fb0SBarry Smith . -snes_mf_operator - Activates default matrix-free Jacobian-vector products, and a user-provided preconditioning matrix 1676dc4c0fb0SBarry Smith as set by `SNESSetJacobian()` 1677dc4c0fb0SBarry Smith . -snes_fd_coloring - uses a relative fast computation of the Jacobian using finite differences and a graph coloring 1678c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 1679c1f60f51SBarry Smith 168036851e7fSLois Curfman McInnes Level: beginner 168136851e7fSLois Curfman McInnes 168295452b02SPatrick Sanan Developer Notes: 1683f6dfbefdSBarry Smith `SNES` always creates a `KSP` object even though many `SNES` methods do not use it. This is 1684efd4aadfSBarry Smith unfortunate and should be fixed at some point. The flag snes->usesksp indicates if the 1685f6dfbefdSBarry Smith particular method does use `KSP` and regulates if the information about the `KSP` is printed 1686f6dfbefdSBarry Smith in `SNESView()`. 1687efd4aadfSBarry Smith 1688f6dfbefdSBarry Smith `TSSetFromOptions()` does call `SNESSetFromOptions()` which can lead to users being confused 1689f6dfbefdSBarry Smith by help messages about meaningless `SNES` options. 1690f6dfbefdSBarry Smith 1691dc4c0fb0SBarry Smith `SNES` always creates the snes->kspconvctx even though it is used by only one type. This should be fixed. 1692efd4aadfSBarry Smith 1693dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESDestroy()`, `SNES`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobian()` 16949b94acceSBarry Smith @*/ 1695d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESCreate(MPI_Comm comm, SNES *outsnes) 1696d71ae5a4SJacob Faibussowitsch { 16979b94acceSBarry Smith SNES snes; 1698fa9f3622SBarry Smith SNESKSPEW *kctx; 169937fcc0dbSBarry Smith 17003a40ed3dSBarry Smith PetscFunctionBegin; 1701ed1caa07SMatthew Knepley PetscValidPointer(outsnes, 2); 17020298fd71SBarry Smith *outsnes = NULL; 17039566063dSJacob Faibussowitsch PetscCall(SNESInitializePackage()); 17048ba1e511SMatthew Knepley 17059566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(snes, SNES_CLASSID, "SNES", "Nonlinear solver", "SNES", comm, SNESDestroy, SNESView)); 17067adad957SLisandro Dalcin 17078d359177SBarry Smith snes->ops->converged = SNESConvergedDefault; 17082c155ee1SBarry Smith snes->usesksp = PETSC_TRUE; 170988976e71SPeter Brune snes->tolerancesset = PETSC_FALSE; 17109b94acceSBarry Smith snes->max_its = 50; 17119750a799SBarry Smith snes->max_funcs = 10000; 17129b94acceSBarry Smith snes->norm = 0.0; 1713c1e67a49SFande Kong snes->xnorm = 0.0; 1714c1e67a49SFande Kong snes->ynorm = 0.0; 1715365a6726SPeter Brune snes->normschedule = SNES_NORM_ALWAYS; 17166c67d002SPeter Brune snes->functype = SNES_FUNCTION_DEFAULT; 17178ca48ce9SPierre Jolivet snes->rtol = PetscDefined(USE_REAL_SINGLE) ? 1.e-5 : 1.e-8; 1718b4874afaSBarry Smith snes->ttol = 0.0; 17198ca48ce9SPierre Jolivet snes->abstol = PetscDefined(USE_REAL_SINGLE) ? 1.e-25 : 1.e-50; 17208ca48ce9SPierre Jolivet snes->stol = PetscDefined(USE_REAL_SINGLE) ? 1.e-5 : 1.e-8; 17218ca48ce9SPierre Jolivet snes->deltatol = PetscDefined(USE_REAL_SINGLE) ? 1.e-6 : 1.e-12; 1722e37c518bSBarry Smith snes->divtol = 1.e4; 1723e37c518bSBarry Smith snes->rnorm0 = 0; 17249b94acceSBarry Smith snes->nfuncs = 0; 172550ffb88aSMatthew Knepley snes->numFailures = 0; 172650ffb88aSMatthew Knepley snes->maxFailures = 1; 17277a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1728e35cf81dSBarry Smith snes->lagjacobian = 1; 172937ec4e1aSPeter Brune snes->jac_iter = 0; 173037ec4e1aSPeter Brune snes->lagjac_persist = PETSC_FALSE; 1731a8054027SBarry Smith snes->lagpreconditioner = 1; 173237ec4e1aSPeter Brune snes->pre_iter = 0; 173337ec4e1aSPeter Brune snes->lagpre_persist = PETSC_FALSE; 1734639f9d9dSBarry Smith snes->numbermonitors = 0; 1735c4421ceaSFande Kong snes->numberreasonviews = 0; 17369e5d0892SLisandro Dalcin snes->data = NULL; 17374dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1738186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 17396f24a144SLois Curfman McInnes snes->nwork = 0; 17409e5d0892SLisandro Dalcin snes->work = NULL; 174158c9b817SLisandro Dalcin snes->nvwork = 0; 17429e5d0892SLisandro Dalcin snes->vwork = NULL; 1743758f92a0SBarry Smith snes->conv_hist_len = 0; 1744758f92a0SBarry Smith snes->conv_hist_max = 0; 17450298fd71SBarry Smith snes->conv_hist = NULL; 17460298fd71SBarry Smith snes->conv_hist_its = NULL; 1747758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1748971e163fSPeter Brune snes->counters_reset = PETSC_TRUE; 1749e4ed7901SPeter Brune snes->vec_func_init_set = PETSC_FALSE; 1750184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 1751efd4aadfSBarry Smith snes->npcside = PC_RIGHT; 1752b3cd9a81SMatthew G. Knepley snes->setfromoptionscalled = 0; 1753c40d0f55SPeter Brune 1754d8f46077SPeter Brune snes->mf = PETSC_FALSE; 1755d8f46077SPeter Brune snes->mf_operator = PETSC_FALSE; 1756d8f46077SPeter Brune snes->mf_version = 1; 1757d8f46077SPeter Brune 17583d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 17593d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 17603d4c4710SBarry Smith 1761349187a7SBarry Smith snes->vizerotolerance = 1.e-8; 176276bd3646SJed Brown snes->checkjacdomainerror = PetscDefined(USE_DEBUG) ? PETSC_TRUE : PETSC_FALSE; 1763349187a7SBarry Smith 17644fc747eaSLawrence Mitchell /* Set this to true if the implementation of SNESSolve_XXX does compute the residual at the final solution. */ 17654fc747eaSLawrence Mitchell snes->alwayscomputesfinalresidual = PETSC_FALSE; 17664fc747eaSLawrence Mitchell 17679b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 17684dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&kctx)); 1769f5af7f23SKarl Rupp 17709b94acceSBarry Smith snes->kspconvctx = (void *)kctx; 17719b94acceSBarry Smith kctx->version = 2; 17720f0abf79SStefano Zampini kctx->rtol_0 = 0.3; /* Eisenstat and Walker suggest rtol_0=.5, but 17739b94acceSBarry Smith this was too large for some test cases */ 177475567043SBarry Smith kctx->rtol_last = 0.0; 17750f0abf79SStefano Zampini kctx->rtol_max = 0.9; 17769b94acceSBarry Smith kctx->gamma = 1.0; 17770f0abf79SStefano Zampini kctx->alpha = 0.5 * (1.0 + PetscSqrtReal(5.0)); 177871f87433Sdalcinl kctx->alpha2 = kctx->alpha; 17790f0abf79SStefano Zampini kctx->threshold = 0.1; 178075567043SBarry Smith kctx->lresid_last = 0.0; 178175567043SBarry Smith kctx->norm_last = 0.0; 17829b94acceSBarry Smith 17830f0abf79SStefano Zampini kctx->rk_last = 0.0; 17840f0abf79SStefano Zampini kctx->rk_last_2 = 0.0; 17850f0abf79SStefano Zampini kctx->rtol_last_2 = 0.0; 17860f0abf79SStefano Zampini kctx->v4_p1 = 0.1; 17870f0abf79SStefano Zampini kctx->v4_p2 = 0.4; 17880f0abf79SStefano Zampini kctx->v4_p3 = 0.7; 17890f0abf79SStefano Zampini kctx->v4_m1 = 0.8; 17900f0abf79SStefano Zampini kctx->v4_m2 = 0.5; 17910f0abf79SStefano Zampini kctx->v4_m3 = 0.1; 17920f0abf79SStefano Zampini kctx->v4_m4 = 0.5; 17930f0abf79SStefano Zampini 17949b94acceSBarry Smith *outsnes = snes; 17953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 17969b94acceSBarry Smith } 17979b94acceSBarry Smith 179888f0584fSBarry Smith /*MC 1799f6dfbefdSBarry Smith SNESFunction - Functional form used to convey the nonlinear function to `SNES` in `SNESSetFunction()` 180088f0584fSBarry Smith 180188f0584fSBarry Smith Synopsis: 1802411c0326SBarry Smith #include "petscsnes.h" 1803411c0326SBarry Smith PetscErrorCode SNESFunction(SNES snes,Vec x,Vec f,void *ctx); 180488f0584fSBarry Smith 1805c3339decSBarry Smith Collective 18061843f636SBarry Smith 180788f0584fSBarry Smith Input Parameters: 1808f6dfbefdSBarry Smith + snes - the `SNES` context 180988f0584fSBarry Smith . x - state at which to evaluate residual 1810f6dfbefdSBarry Smith - ctx - optional user-defined function context, passed in with `SNESSetFunction()` 181188f0584fSBarry Smith 181288f0584fSBarry Smith Output Parameter: 181388f0584fSBarry Smith . f - vector to put residual (function value) 181488f0584fSBarry Smith 1815878cb397SSatish Balay Level: intermediate 1816878cb397SSatish Balay 1817dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetFunction()`, `SNESGetFunction()` 181888f0584fSBarry Smith M*/ 181988f0584fSBarry Smith 18209b94acceSBarry Smith /*@C 18219b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 1822f6dfbefdSBarry Smith vector for use by the `SNES` routines in solving systems of nonlinear 18239b94acceSBarry Smith equations. 18249b94acceSBarry Smith 1825c3339decSBarry Smith Logically Collective 1826fee21e36SBarry Smith 1827c7afd0dbSLois Curfman McInnes Input Parameters: 1828f6dfbefdSBarry Smith + snes - the `SNES` context 1829dc4c0fb0SBarry Smith . r - vector to store function values, may be `NULL` 183020f4b53cSBarry Smith . f - function evaluation routine; for calling sequence see `SNESFunction` 1831c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1832dc4c0fb0SBarry Smith function evaluation routine (may be `NULL`) 18339b94acceSBarry Smith 183436851e7fSLois Curfman McInnes Level: beginner 183536851e7fSLois Curfman McInnes 1836dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESSetPicard()`, `SNESFunction` 18379b94acceSBarry Smith @*/ 1838d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetFunction(SNES snes, Vec r, PetscErrorCode (*f)(SNES, Vec, Vec, void *), void *ctx) 1839d71ae5a4SJacob Faibussowitsch { 18406cab3a1bSJed Brown DM dm; 18416cab3a1bSJed Brown 18423a40ed3dSBarry Smith PetscFunctionBegin; 18430700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1844d2a683ecSLisandro Dalcin if (r) { 1845d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r, VEC_CLASSID, 2); 1846d2a683ecSLisandro Dalcin PetscCheckSameComm(snes, 1, r, 2); 18479566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)r)); 18489566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_func)); 184985385478SLisandro Dalcin snes->vec_func = r; 1850d2a683ecSLisandro Dalcin } 18519566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 18529566063dSJacob Faibussowitsch PetscCall(DMSNESSetFunction(dm, f, ctx)); 185348a46eb9SPierre Jolivet if (f == SNESPicardComputeFunction) PetscCall(DMSNESSetMFFunction(dm, SNESPicardComputeMFFunction, ctx)); 18543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 18559b94acceSBarry Smith } 18569b94acceSBarry Smith 1857e4ed7901SPeter Brune /*@C 1858e4ed7901SPeter Brune SNESSetInitialFunction - Sets the function vector to be used as the 1859f6dfbefdSBarry Smith initial function value at the initialization of the method. In some 1860e4ed7901SPeter Brune instances, the user has precomputed the function before calling 1861f6dfbefdSBarry Smith `SNESSolve()`. This function allows one to avoid a redundant call 1862f6dfbefdSBarry Smith to `SNESComputeFunction()` in that case. 1863e4ed7901SPeter Brune 1864c3339decSBarry Smith Logically Collective 1865e4ed7901SPeter Brune 1866e4ed7901SPeter Brune Input Parameters: 1867f6dfbefdSBarry Smith + snes - the `SNES` context 1868e4ed7901SPeter Brune - f - vector to store function value 1869e4ed7901SPeter Brune 1870dc4c0fb0SBarry Smith Level: developer 1871dc4c0fb0SBarry Smith 1872e4ed7901SPeter Brune Notes: 1873e4ed7901SPeter Brune This should not be modified during the solution procedure. 1874e4ed7901SPeter Brune 1875f6dfbefdSBarry Smith This is used extensively in the `SNESFAS` hierarchy and in nonlinear preconditioning. 1876e4ed7901SPeter Brune 1877dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESFAS`, `SNESSetFunction()`, `SNESComputeFunction()`, `SNESSetInitialFunctionNorm()` 1878e4ed7901SPeter Brune @*/ 1879d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetInitialFunction(SNES snes, Vec f) 1880d71ae5a4SJacob Faibussowitsch { 1881e4ed7901SPeter Brune Vec vec_func; 1882e4ed7901SPeter Brune 1883e4ed7901SPeter Brune PetscFunctionBegin; 1884e4ed7901SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1885e4ed7901SPeter Brune PetscValidHeaderSpecific(f, VEC_CLASSID, 2); 1886e4ed7901SPeter Brune PetscCheckSameComm(snes, 1, f, 2); 1887efd4aadfSBarry Smith if (snes->npcside == PC_LEFT && snes->functype == SNES_FUNCTION_PRECONDITIONED) { 1888902f982fSPeter Brune snes->vec_func_init_set = PETSC_FALSE; 18893ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1890902f982fSPeter Brune } 18919566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, &vec_func, NULL, NULL)); 18929566063dSJacob Faibussowitsch PetscCall(VecCopy(f, vec_func)); 1893f5af7f23SKarl Rupp 1894217b9c2eSPeter Brune snes->vec_func_init_set = PETSC_TRUE; 18953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1896e4ed7901SPeter Brune } 1897e4ed7901SPeter Brune 1898534ebe21SPeter Brune /*@ 1899f6dfbefdSBarry Smith SNESSetNormSchedule - Sets the `SNESNormSchedule` used in convergence and monitoring 1900f6dfbefdSBarry Smith of the `SNES` method, when norms are computed in the solving process 1901534ebe21SPeter Brune 1902c3339decSBarry Smith Logically Collective 1903534ebe21SPeter Brune 1904534ebe21SPeter Brune Input Parameters: 1905f6dfbefdSBarry Smith + snes - the `SNES` context 1906365a6726SPeter Brune - normschedule - the frequency of norm computation 1907534ebe21SPeter Brune 1908517f1916SMatthew G. Knepley Options Database Key: 190967b8a455SSatish Balay . -snes_norm_schedule <none, always, initialonly, finalonly, initialfinalonly> - set the schedule 1910517f1916SMatthew G. Knepley 1911dc4c0fb0SBarry Smith Level: advanced 1912dc4c0fb0SBarry Smith 1913534ebe21SPeter Brune Notes: 1914f6dfbefdSBarry Smith Only certain `SNES` methods support certain `SNESNormSchedules`. Most require evaluation 1915534ebe21SPeter Brune of the nonlinear function and the taking of its norm at every iteration to 1916534ebe21SPeter Brune even ensure convergence at all. However, methods such as custom Gauss-Seidel methods 1917f6dfbefdSBarry Smith `SNESNGS` and the like do not require the norm of the function to be computed, and therefore 1918534ebe21SPeter Brune may either be monitored for convergence or not. As these are often used as nonlinear 1919534ebe21SPeter Brune preconditioners, monitoring the norm of their error is not a useful enterprise within 1920534ebe21SPeter Brune their solution. 1921534ebe21SPeter Brune 1922dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESNormSchedule`, `SNESGetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 1923534ebe21SPeter Brune @*/ 1924d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetNormSchedule(SNES snes, SNESNormSchedule normschedule) 1925d71ae5a4SJacob Faibussowitsch { 1926534ebe21SPeter Brune PetscFunctionBegin; 1927534ebe21SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1928365a6726SPeter Brune snes->normschedule = normschedule; 19293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1930534ebe21SPeter Brune } 1931534ebe21SPeter Brune 1932534ebe21SPeter Brune /*@ 1933f6dfbefdSBarry Smith SNESGetNormSchedule - Gets the `SNESNormSchedule` used in convergence and monitoring 1934f6dfbefdSBarry Smith of the `SNES` method. 1935534ebe21SPeter Brune 1936c3339decSBarry Smith Logically Collective 1937534ebe21SPeter Brune 1938534ebe21SPeter Brune Input Parameters: 1939f6dfbefdSBarry Smith + snes - the `SNES` context 1940365a6726SPeter Brune - normschedule - the type of the norm used 1941534ebe21SPeter Brune 1942534ebe21SPeter Brune Level: advanced 1943534ebe21SPeter Brune 1944dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 1945534ebe21SPeter Brune @*/ 1946d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetNormSchedule(SNES snes, SNESNormSchedule *normschedule) 1947d71ae5a4SJacob Faibussowitsch { 1948534ebe21SPeter Brune PetscFunctionBegin; 1949534ebe21SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1950365a6726SPeter Brune *normschedule = snes->normschedule; 19513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1952534ebe21SPeter Brune } 1953534ebe21SPeter Brune 1954c5ce4427SMatthew G. Knepley /*@ 1955c5ce4427SMatthew G. Knepley SNESSetFunctionNorm - Sets the last computed residual norm. 1956c5ce4427SMatthew G. Knepley 1957c3339decSBarry Smith Logically Collective 1958c5ce4427SMatthew G. Knepley 1959c5ce4427SMatthew G. Knepley Input Parameters: 1960f6dfbefdSBarry Smith + snes - the `SNES` context 1961f6dfbefdSBarry Smith - norm - the value of the norm 1962c5ce4427SMatthew G. Knepley 1963c5ce4427SMatthew G. Knepley Level: developer 1964c5ce4427SMatthew G. Knepley 1965dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 1966c5ce4427SMatthew G. Knepley @*/ 1967d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetFunctionNorm(SNES snes, PetscReal norm) 1968d71ae5a4SJacob Faibussowitsch { 1969c5ce4427SMatthew G. Knepley PetscFunctionBegin; 1970c5ce4427SMatthew G. Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1971c5ce4427SMatthew G. Knepley snes->norm = norm; 19723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1973c5ce4427SMatthew G. Knepley } 1974c5ce4427SMatthew G. Knepley 1975c5ce4427SMatthew G. Knepley /*@ 1976c5ce4427SMatthew G. Knepley SNESGetFunctionNorm - Gets the last computed norm of the residual 1977c5ce4427SMatthew G. Knepley 1978c5ce4427SMatthew G. Knepley Not Collective 1979c5ce4427SMatthew G. Knepley 1980c5ce4427SMatthew G. Knepley Input Parameter: 1981f6dfbefdSBarry Smith . snes - the `SNES` context 1982c5ce4427SMatthew G. Knepley 1983c5ce4427SMatthew G. Knepley Output Parameter: 1984c5ce4427SMatthew G. Knepley . norm - the last computed residual norm 1985c5ce4427SMatthew G. Knepley 1986c5ce4427SMatthew G. Knepley Level: developer 1987c5ce4427SMatthew G. Knepley 1988dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 1989c5ce4427SMatthew G. Knepley @*/ 1990d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetFunctionNorm(SNES snes, PetscReal *norm) 1991d71ae5a4SJacob Faibussowitsch { 1992c5ce4427SMatthew G. Knepley PetscFunctionBegin; 1993c5ce4427SMatthew G. Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 1994dadcf809SJacob Faibussowitsch PetscValidRealPointer(norm, 2); 1995c5ce4427SMatthew G. Knepley *norm = snes->norm; 19963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1997c5ce4427SMatthew G. Knepley } 1998c5ce4427SMatthew G. Knepley 1999c1e67a49SFande Kong /*@ 2000f6dfbefdSBarry Smith SNESGetUpdateNorm - Gets the last computed norm of the solution update 2001c1e67a49SFande Kong 2002c1e67a49SFande Kong Not Collective 2003c1e67a49SFande Kong 2004c1e67a49SFande Kong Input Parameter: 2005f6dfbefdSBarry Smith . snes - the `SNES` context 2006c1e67a49SFande Kong 2007c1e67a49SFande Kong Output Parameter: 2008c1e67a49SFande Kong . ynorm - the last computed update norm 2009c1e67a49SFande Kong 2010c1e67a49SFande Kong Level: developer 2011c1e67a49SFande Kong 2012f6dfbefdSBarry Smith Note: 2013f6dfbefdSBarry Smith The new solution is the current solution plus the update, so this norm is an indication of the size of the update 2014f6dfbefdSBarry Smith 2015dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `SNESGetFunctionNorm()` 2016c1e67a49SFande Kong @*/ 2017d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetUpdateNorm(SNES snes, PetscReal *ynorm) 2018d71ae5a4SJacob Faibussowitsch { 2019c1e67a49SFande Kong PetscFunctionBegin; 2020c1e67a49SFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 2021dadcf809SJacob Faibussowitsch PetscValidRealPointer(ynorm, 2); 2022c1e67a49SFande Kong *ynorm = snes->ynorm; 20233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2024c1e67a49SFande Kong } 2025c1e67a49SFande Kong 2026c1e67a49SFande Kong /*@ 20274591eaf2SFande Kong SNESGetSolutionNorm - Gets the last computed norm of the solution 2028c1e67a49SFande Kong 2029c1e67a49SFande Kong Not Collective 2030c1e67a49SFande Kong 2031c1e67a49SFande Kong Input Parameter: 2032f6dfbefdSBarry Smith . snes - the `SNES` context 2033c1e67a49SFande Kong 2034c1e67a49SFande Kong Output Parameter: 2035c1e67a49SFande Kong . xnorm - the last computed solution norm 2036c1e67a49SFande Kong 2037c1e67a49SFande Kong Level: developer 2038c1e67a49SFande Kong 2039dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `SNESGetFunctionNorm()`, `SNESGetUpdateNorm()` 2040c1e67a49SFande Kong @*/ 2041d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetSolutionNorm(SNES snes, PetscReal *xnorm) 2042d71ae5a4SJacob Faibussowitsch { 2043c1e67a49SFande Kong PetscFunctionBegin; 2044c1e67a49SFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 2045dadcf809SJacob Faibussowitsch PetscValidRealPointer(xnorm, 2); 2046c1e67a49SFande Kong *xnorm = snes->xnorm; 20473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2048c1e67a49SFande Kong } 2049c1e67a49SFande Kong 205047073ea2SPeter Brune /*@C 2051f6dfbefdSBarry Smith SNESSetFunctionType - Sets the `SNESFunctionType` 2052f6dfbefdSBarry Smith of the `SNES` method. 205347073ea2SPeter Brune 2054c3339decSBarry Smith Logically Collective 205547073ea2SPeter Brune 205647073ea2SPeter Brune Input Parameters: 2057f6dfbefdSBarry Smith + snes - the `SNES` context 2058f6dfbefdSBarry Smith - type - the function type 205947073ea2SPeter Brune 206047073ea2SPeter Brune Level: developer 206147073ea2SPeter Brune 2062f6dfbefdSBarry Smith Notes: 2063f6dfbefdSBarry Smith Possible values of the function type 2064f6dfbefdSBarry Smith + `SNES_FUNCTION_DEFAULT` - the default for the given `SNESType` 2065f6dfbefdSBarry Smith . `SNES_FUNCTION_UNPRECONDITIONED` - an unpreconditioned function evaluation (this is the function provided with `SNESSetFunction()` 2066f6dfbefdSBarry Smith - `SNES_FUNCTION_PRECONDITIONED` - a transformation of the function provided with `SNESSetFunction()` 2067f6dfbefdSBarry Smith 2068f6dfbefdSBarry Smith Different `SNESType`s use this value in different ways 2069f6dfbefdSBarry Smith 2070dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESFunctionType`, `SNESGetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 207147073ea2SPeter Brune @*/ 2072d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetFunctionType(SNES snes, SNESFunctionType type) 2073d71ae5a4SJacob Faibussowitsch { 207447073ea2SPeter Brune PetscFunctionBegin; 207547073ea2SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 207647073ea2SPeter Brune snes->functype = type; 20773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 207847073ea2SPeter Brune } 207947073ea2SPeter Brune 208047073ea2SPeter Brune /*@C 2081f6dfbefdSBarry Smith SNESGetFunctionType - Gets the `SNESFunctionType` used in convergence and monitoring set with `SNESSetFunctionType()` 208247073ea2SPeter Brune of the SNES method. 208347073ea2SPeter Brune 2084c3339decSBarry Smith Logically Collective 208547073ea2SPeter Brune 208647073ea2SPeter Brune Input Parameters: 2087f6dfbefdSBarry Smith + snes - the `SNES` context 2088f6dfbefdSBarry Smith - type - the type of the function evaluation, see `SNESSetFunctionType()` 208947073ea2SPeter Brune 209047073ea2SPeter Brune Level: advanced 209147073ea2SPeter Brune 2092dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetFunctionType()`, `SNESFunctionType`, `SNESSetNormSchedule()`, `SNESComputeFunction()`, `VecNorm()`, `SNESSetFunction()`, `SNESSetInitialFunction()`, `SNESNormSchedule` 209347073ea2SPeter Brune @*/ 2094d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetFunctionType(SNES snes, SNESFunctionType *type) 2095d71ae5a4SJacob Faibussowitsch { 209647073ea2SPeter Brune PetscFunctionBegin; 209747073ea2SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 209847073ea2SPeter Brune *type = snes->functype; 20993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2100534ebe21SPeter Brune } 2101534ebe21SPeter Brune 2102c79ef259SPeter Brune /*@C 2103be95d8f1SBarry Smith SNESSetNGS - Sets the user nonlinear Gauss-Seidel routine for 2104c79ef259SPeter Brune use with composed nonlinear solvers. 2105c79ef259SPeter Brune 2106c79ef259SPeter Brune Input Parameters: 2107dc4c0fb0SBarry Smith + snes - the `SNES` context 210837fdd005SBarry Smith . f - function evaluation routine to apply Gauss-Seidel 2109c79ef259SPeter Brune - ctx - [optional] user-defined context for private data for the 2110dc4c0fb0SBarry Smith smoother evaluation routine (may be `NULL`) 2111c79ef259SPeter Brune 211220f4b53cSBarry Smith Calling sequence of `f`: 211320f4b53cSBarry Smith $ PetscErrorCode f(SNES snes, Vec X, Vec B, void *ctx) 2114f6dfbefdSBarry Smith + snes - the `SNES` context 2115f6dfbefdSBarry Smith . X - the current solution 2116dc4c0fb0SBarry Smith . B - the right hand side vector (which may be `NULL`) 2117f6dfbefdSBarry Smith - ctx - a user provided context 2118f6dfbefdSBarry Smith 2119dc4c0fb0SBarry Smith Level: intermediate 2120dc4c0fb0SBarry Smith 2121f6dfbefdSBarry Smith Note: 2122f6dfbefdSBarry Smith The `SNESNGS` routines are used by the composed nonlinear solver to generate 2123f6dfbefdSBarry Smith a problem appropriate update to the solution, particularly `SNESFAS`. 2124c79ef259SPeter Brune 212537fdd005SBarry Smith .seealso: [](chapter_snes), `SNESGetNGS()`, `SNESNCG`, `SNESGetFunction()`, `SNESComputeNGS()` 2126c79ef259SPeter Brune @*/ 2127d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetNGS(SNES snes, PetscErrorCode (*f)(SNES, Vec, Vec, void *), void *ctx) 2128d71ae5a4SJacob Faibussowitsch { 21296cab3a1bSJed Brown DM dm; 21306cab3a1bSJed Brown 2131646217ecSPeter Brune PetscFunctionBegin; 21326cab3a1bSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 21339566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 21349566063dSJacob Faibussowitsch PetscCall(DMSNESSetNGS(dm, f, ctx)); 21353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2136646217ecSPeter Brune } 2137646217ecSPeter Brune 2138bbc1464cSBarry Smith /* 2139bbc1464cSBarry Smith This is used for -snes_mf_operator; it uses a duplicate of snes->jacobian_pre because snes->jacobian_pre cannot be 2140bbc1464cSBarry Smith changed during the KSPSolve() 2141bbc1464cSBarry Smith */ 2142d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESPicardComputeMFFunction(SNES snes, Vec x, Vec f, void *ctx) 2143d71ae5a4SJacob Faibussowitsch { 2144bbc1464cSBarry Smith DM dm; 2145bbc1464cSBarry Smith DMSNES sdm; 2146bbc1464cSBarry Smith 2147bbc1464cSBarry Smith PetscFunctionBegin; 21489566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 21499566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &sdm)); 2150bbc1464cSBarry Smith /* A(x)*x - b(x) */ 2151bbc1464cSBarry Smith if (sdm->ops->computepfunction) { 2152792fecdfSBarry Smith PetscCallBack("SNES Picard callback function", (*sdm->ops->computepfunction)(snes, x, f, sdm->pctx)); 21539566063dSJacob Faibussowitsch PetscCall(VecScale(f, -1.0)); 21540df40c35SBarry Smith /* Cannot share nonzero pattern because of the possible use of SNESComputeJacobianDefault() */ 2155ef1023bdSBarry Smith if (!snes->picard) PetscCall(MatDuplicate(snes->jacobian_pre, MAT_DO_NOT_COPY_VALUES, &snes->picard)); 2156792fecdfSBarry Smith PetscCallBack("SNES Picard callback Jacobian", (*sdm->ops->computepjacobian)(snes, x, snes->picard, snes->picard, sdm->pctx)); 21579566063dSJacob Faibussowitsch PetscCall(MatMultAdd(snes->picard, x, f, f)); 2158bbc1464cSBarry Smith } else { 2159792fecdfSBarry Smith PetscCallBack("SNES Picard callback Jacobian", (*sdm->ops->computepjacobian)(snes, x, snes->picard, snes->picard, sdm->pctx)); 21609566063dSJacob Faibussowitsch PetscCall(MatMult(snes->picard, x, f)); 2161bbc1464cSBarry Smith } 21623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2163bbc1464cSBarry Smith } 2164bbc1464cSBarry Smith 2165d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESPicardComputeFunction(SNES snes, Vec x, Vec f, void *ctx) 2166d71ae5a4SJacob Faibussowitsch { 2167e03ab78fSPeter Brune DM dm; 2168942e3340SBarry Smith DMSNES sdm; 21696cab3a1bSJed Brown 21708b0a5094SBarry Smith PetscFunctionBegin; 21719566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 21729566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &sdm)); 21738b0a5094SBarry Smith /* A(x)*x - b(x) */ 2174bbc1464cSBarry Smith if (sdm->ops->computepfunction) { 2175792fecdfSBarry Smith PetscCallBack("SNES Picard callback function", (*sdm->ops->computepfunction)(snes, x, f, sdm->pctx)); 21769566063dSJacob Faibussowitsch PetscCall(VecScale(f, -1.0)); 2177792fecdfSBarry Smith PetscCallBack("SNES Picard callback Jacobian", (*sdm->ops->computepjacobian)(snes, x, snes->jacobian, snes->jacobian_pre, sdm->pctx)); 21789566063dSJacob Faibussowitsch PetscCall(MatMultAdd(snes->jacobian_pre, x, f, f)); 2179bbc1464cSBarry Smith } else { 2180792fecdfSBarry Smith PetscCallBack("SNES Picard callback Jacobian", (*sdm->ops->computepjacobian)(snes, x, snes->jacobian, snes->jacobian_pre, sdm->pctx)); 21819566063dSJacob Faibussowitsch PetscCall(MatMult(snes->jacobian_pre, x, f)); 2182bbc1464cSBarry Smith } 21833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 21848b0a5094SBarry Smith } 21858b0a5094SBarry Smith 2186d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESPicardComputeJacobian(SNES snes, Vec x1, Mat J, Mat B, void *ctx) 2187d71ae5a4SJacob Faibussowitsch { 21888b0a5094SBarry Smith PetscFunctionBegin; 2189e03ab78fSPeter Brune /* the jacobian matrix should be pre-filled in SNESPicardComputeFunction */ 2190bbc1464cSBarry Smith /* must assembly if matrix-free to get the last SNES solution */ 21919566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 21929566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 21933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 21948b0a5094SBarry Smith } 21958b0a5094SBarry Smith 21968b0a5094SBarry Smith /*@C 2197f6dfbefdSBarry Smith SNESSetPicard - Use `SNES` to solve the system A(x) x = bp(x) + b via a Picard type iteration (Picard linearization) 21988b0a5094SBarry Smith 2199c3339decSBarry Smith Logically Collective 22008b0a5094SBarry Smith 22018b0a5094SBarry Smith Input Parameters: 2202f6dfbefdSBarry Smith + snes - the `SNES` context 2203dc4c0fb0SBarry Smith . r - vector to store function values, may be `NULL` 2204dc4c0fb0SBarry Smith . bp - function evaluation routine, may be `NULL` 22056b7fb656SBarry Smith . Amat - matrix with which A(x) x - bp(x) - b is to be computed 2206dc4c0fb0SBarry Smith . Pmat - matrix from which preconditioner is computed (usually the same as `Amat`) 220720f4b53cSBarry Smith . J - function to compute matrix values, for the calling sequence see `SNESJacobianFunction()` 2208dc4c0fb0SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`) 2209dc4c0fb0SBarry Smith 2210dc4c0fb0SBarry Smith Level: intermediate 22118b0a5094SBarry Smith 22128b0a5094SBarry Smith Notes: 22136b7fb656SBarry Smith It is often better to provide the nonlinear function F() and some approximation to its Jacobian directly and use 2214f450aa47SBarry 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. 2215f450aa47SBarry Smith 2216f6dfbefdSBarry Smith One can call `SNESSetPicard()` or `SNESSetFunction()` (and possibly `SNESSetJacobian()`) but cannot call both 22178b0a5094SBarry Smith 2218dc4c0fb0SBarry 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}. 2219dc4c0fb0SBarry Smith When an exact solver is used this corresponds to the "classic" Picard A(x^{n}) x^{n+1} = bp(x^{n}) + b iteration. 22208b0a5094SBarry Smith 2221dc4c0fb0SBarry Smith Run with `-snes_mf_operator` to solve the system with Newton's method using A(x^{n}) to construct the preconditioner. 22228b0a5094SBarry Smith 22230d04baf8SBarry Smith We implement the defect correction form of the Picard iteration because it converges much more generally when inexact linear solvers are used then 22246b7fb656SBarry Smith the direct Picard iteration A(x^n) x^{n+1} = bp(x^n) + b 22258b0a5094SBarry Smith 22268b0a5094SBarry 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 22278b0a5094SBarry 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 22288b0a5094SBarry Smith different please contact us at petsc-dev@mcs.anl.gov and we'll have an entirely new argument :-). 22298b0a5094SBarry Smith 2230dc4c0fb0SBarry 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 and 2231f6dfbefdSBarry Smith A(x^{n}) is used to build the preconditioner 22326b7fb656SBarry Smith 2233dc4c0fb0SBarry 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. 22346b7fb656SBarry Smith 2235dc4c0fb0SBarry 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 22366b7fb656SBarry 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 2237f6dfbefdSBarry 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`. 2238aaa8cc7dSPierre Jolivet See the comment in src/snes/tutorials/ex15.c. 2239bbc1464cSBarry Smith 2240dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetFunction()`, `SNESSetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESGetPicard()`, `SNESLineSearchPreCheckPicard()`, `SNESJacobianFunction` 22418b0a5094SBarry Smith @*/ 2242d71ae5a4SJacob Faibussowitsch 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) 2243d71ae5a4SJacob Faibussowitsch { 2244e03ab78fSPeter Brune DM dm; 2245e03ab78fSPeter Brune 22468b0a5094SBarry Smith PetscFunctionBegin; 22478b0a5094SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 22489566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 22499566063dSJacob Faibussowitsch PetscCall(DMSNESSetPicard(dm, bp, J, ctx)); 22509566063dSJacob Faibussowitsch PetscCall(DMSNESSetMFFunction(dm, SNESPicardComputeMFFunction, ctx)); 22519566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes, r, SNESPicardComputeFunction, ctx)); 22529566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESPicardComputeJacobian, ctx)); 22533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22548b0a5094SBarry Smith } 22558b0a5094SBarry Smith 22567971a8bfSPeter Brune /*@C 22577971a8bfSPeter Brune SNESGetPicard - Returns the context for the Picard iteration 22587971a8bfSPeter Brune 2259f6dfbefdSBarry Smith Not Collective, but `Vec` is parallel if `SNES` is parallel. Collective if `Vec` is requested, but has not been created yet. 22607971a8bfSPeter Brune 22617971a8bfSPeter Brune Input Parameter: 2262f6dfbefdSBarry Smith . snes - the `SNES` context 22637971a8bfSPeter Brune 2264d8d19677SJose E. Roman Output Parameters: 2265dc4c0fb0SBarry Smith + r - the function (or `NULL`) 226620f4b53cSBarry Smith . f - the function (or `NULL`); for calling sequence see `SNESFunction` 2267dc4c0fb0SBarry Smith . Amat - the matrix used to defined the operation A(x) x - b(x) (or `NULL`) 2268dc4c0fb0SBarry Smith . Pmat - the matrix from which the preconditioner will be constructed (or `NULL`) 226920f4b53cSBarry Smith . J - the function for matrix evaluation (or `NULL`); for calling sequence see `SNESJacobianFunction` 2270dc4c0fb0SBarry Smith - ctx - the function context (or `NULL`) 22717971a8bfSPeter Brune 22727971a8bfSPeter Brune Level: advanced 22737971a8bfSPeter Brune 2274dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetFunction()`, `SNESSetPicard()`, `SNESGetFunction()`, `SNESGetJacobian()`, `SNESGetDM()`, `SNESFunction`, `SNESJacobianFunction` 22757971a8bfSPeter Brune @*/ 2276d71ae5a4SJacob Faibussowitsch 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) 2277d71ae5a4SJacob Faibussowitsch { 22787971a8bfSPeter Brune DM dm; 22797971a8bfSPeter Brune 22807971a8bfSPeter Brune PetscFunctionBegin; 22817971a8bfSPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 22829566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 22839566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 22849566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 22859566063dSJacob Faibussowitsch PetscCall(DMSNESGetPicard(dm, f, J, ctx)); 22863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22877971a8bfSPeter Brune } 22887971a8bfSPeter Brune 2289d25893d9SBarry Smith /*@C 2290dc4c0fb0SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the nonlinear problem 2291d25893d9SBarry Smith 2292c3339decSBarry Smith Logically Collective 2293d25893d9SBarry Smith 2294d25893d9SBarry Smith Input Parameters: 2295f6dfbefdSBarry Smith + snes - the `SNES` context 2296d25893d9SBarry Smith . func - function evaluation routine 2297d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 2298dc4c0fb0SBarry Smith function evaluation routine (may be `NULL`) 2299d25893d9SBarry Smith 230020f4b53cSBarry Smith Calling sequence of `func`: 230120f4b53cSBarry Smith $ PetscErrorCode func(SNES snes, Vec x, void *ctx); 230220f4b53cSBarry Smith + snes - the `SNES` solver 230320f4b53cSBarry Smith . x - vector to put initial guess 2304d25893d9SBarry Smith - ctx - optional user-defined function context 2305d25893d9SBarry Smith 2306d25893d9SBarry Smith Level: intermediate 2307d25893d9SBarry Smith 2308dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESGetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()` 2309d25893d9SBarry Smith @*/ 2310d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetComputeInitialGuess(SNES snes, PetscErrorCode (*func)(SNES, Vec, void *), void *ctx) 2311d71ae5a4SJacob Faibussowitsch { 2312d25893d9SBarry Smith PetscFunctionBegin; 2313d25893d9SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 2314d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 2315d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 23163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2317d25893d9SBarry Smith } 2318d25893d9SBarry Smith 23191096aae1SMatthew Knepley /*@C 2320dc4c0fb0SBarry Smith SNESGetRhs - Gets the vector for solving F(x) = `rhs`. If `rhs` is not set 23211096aae1SMatthew Knepley it assumes a zero right hand side. 23221096aae1SMatthew Knepley 2323c3339decSBarry Smith Logically Collective 23241096aae1SMatthew Knepley 23251096aae1SMatthew Knepley Input Parameter: 2326f6dfbefdSBarry Smith . snes - the `SNES` context 23271096aae1SMatthew Knepley 23281096aae1SMatthew Knepley Output Parameter: 2329dc4c0fb0SBarry Smith . rhs - the right hand side vector or `NULL` if the right hand side vector is null 23301096aae1SMatthew Knepley 23311096aae1SMatthew Knepley Level: intermediate 23321096aae1SMatthew Knepley 2333dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetSolution()`, `SNESGetFunction()`, `SNESComputeFunction()`, `SNESSetJacobian()`, `SNESSetFunction()` 23341096aae1SMatthew Knepley @*/ 2335d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetRhs(SNES snes, Vec *rhs) 2336d71ae5a4SJacob Faibussowitsch { 23371096aae1SMatthew Knepley PetscFunctionBegin; 23380700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 23391096aae1SMatthew Knepley PetscValidPointer(rhs, 2); 234085385478SLisandro Dalcin *rhs = snes->vec_rhs; 23413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23421096aae1SMatthew Knepley } 23431096aae1SMatthew Knepley 23449b94acceSBarry Smith /*@ 2345f6dfbefdSBarry Smith SNESComputeFunction - Calls the function that has been set with `SNESSetFunction()`. 23469b94acceSBarry Smith 2347c3339decSBarry Smith Collective 2348c7afd0dbSLois Curfman McInnes 23499b94acceSBarry Smith Input Parameters: 2350f6dfbefdSBarry Smith + snes - the `SNES` context 2351c7afd0dbSLois Curfman McInnes - x - input vector 23529b94acceSBarry Smith 23539b94acceSBarry Smith Output Parameter: 2354f6dfbefdSBarry Smith . y - function vector, as set by `SNESSetFunction()` 23559b94acceSBarry Smith 2356dc4c0fb0SBarry Smith Level: developer 2357dc4c0fb0SBarry Smith 2358f6dfbefdSBarry Smith Note: 2359f6dfbefdSBarry Smith `SNESComputeFunction()` is typically used within nonlinear solvers 2360bbc1464cSBarry Smith implementations, so users would not generally call this routine themselves. 236136851e7fSLois Curfman McInnes 2362dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetFunction()`, `SNESGetFunction()`, `SNESComputeMFFunction()` 23639b94acceSBarry Smith @*/ 2364d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESComputeFunction(SNES snes, Vec x, Vec y) 2365d71ae5a4SJacob Faibussowitsch { 23666cab3a1bSJed Brown DM dm; 2367942e3340SBarry Smith DMSNES sdm; 23689b94acceSBarry Smith 23693a40ed3dSBarry Smith PetscFunctionBegin; 23700700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 23710700a824SBarry Smith PetscValidHeaderSpecific(x, VEC_CLASSID, 2); 23720700a824SBarry Smith PetscValidHeaderSpecific(y, VEC_CLASSID, 3); 2373c9780b6fSBarry Smith PetscCheckSameComm(snes, 1, x, 2); 2374c9780b6fSBarry Smith PetscCheckSameComm(snes, 1, y, 3); 2375e0f629ddSJacob Faibussowitsch PetscCall(VecValidValues_Internal(x, 2, PETSC_TRUE)); 2376184914b5SBarry Smith 23779566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 23789566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &sdm)); 23790fdf79fbSJacob Faibussowitsch PetscCheck(sdm->ops->computefunction || snes->vec_rhs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve()."); 238032f3f7c2SPeter Brune if (sdm->ops->computefunction) { 238148a46eb9SPierre Jolivet if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) PetscCall(PetscLogEventBegin(SNES_FunctionEval, snes, x, y, 0)); 23829566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(x)); 23838ddeebeaSSteve Benbow /* ensure domainerror is false prior to computefunction evaluation (may not have been reset) */ 23848ddeebeaSSteve Benbow snes->domainerror = PETSC_FALSE; 2385800f99ffSJeremy L Thompson { 2386800f99ffSJeremy L Thompson void *ctx; 2387800f99ffSJeremy L Thompson PetscErrorCode (*computefunction)(SNES, Vec, Vec, void *); 2388800f99ffSJeremy L Thompson PetscCall(DMSNESGetFunction(dm, &computefunction, &ctx)); 2389800f99ffSJeremy L Thompson PetscCallBack("SNES callback function", (*computefunction)(snes, x, y, ctx)); 2390800f99ffSJeremy L Thompson } 23919566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(x)); 239248a46eb9SPierre Jolivet if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) PetscCall(PetscLogEventEnd(SNES_FunctionEval, snes, x, y, 0)); 23930fdf79fbSJacob Faibussowitsch } else /* if (snes->vec_rhs) */ { 23949566063dSJacob Faibussowitsch PetscCall(MatMult(snes->jacobian, x, y)); 23950fdf79fbSJacob Faibussowitsch } 23961baa6e33SBarry Smith if (snes->vec_rhs) PetscCall(VecAXPY(y, -1.0, snes->vec_rhs)); 2397ae3c334cSLois Curfman McInnes snes->nfuncs++; 2398422a814eSBarry Smith /* 2399422a814eSBarry Smith domainerror might not be set on all processes; so we tag vector locally with Inf and the next inner product or norm will 2400422a814eSBarry Smith propagate the value to all processes 2401422a814eSBarry Smith */ 24021baa6e33SBarry Smith if (snes->domainerror) PetscCall(VecSetInf(y)); 24033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 24049b94acceSBarry Smith } 24059b94acceSBarry Smith 2406c79ef259SPeter Brune /*@ 2407f6dfbefdSBarry Smith SNESComputeMFFunction - Calls the function that has been set with `SNESSetMFFunction()`. 2408bbc1464cSBarry Smith 2409c3339decSBarry Smith Collective 2410bbc1464cSBarry Smith 2411bbc1464cSBarry Smith Input Parameters: 2412f6dfbefdSBarry Smith + snes - the `SNES` context 2413bbc1464cSBarry Smith - x - input vector 2414bbc1464cSBarry Smith 2415bbc1464cSBarry Smith Output Parameter: 2416f6dfbefdSBarry Smith . y - function vector, as set by `SNESSetMFFunction()` 2417bbc1464cSBarry Smith 2418dc4c0fb0SBarry Smith Level: developer 2419dc4c0fb0SBarry Smith 2420bbc1464cSBarry Smith Notes: 2421f6dfbefdSBarry Smith `SNESComputeMFFunction()` is used within the matrix vector products called by the matrix created with `MatCreateSNESMF()` 2422bbc1464cSBarry Smith so users would not generally call this routine themselves. 2423bbc1464cSBarry Smith 2424f6dfbefdSBarry Smith Since this function is intended for use with finite differencing it does not subtract the right hand side vector provided with `SNESSolve()` 2425f6dfbefdSBarry Smith while `SNESComputeFunction()` does. As such, this routine cannot be used with `MatMFFDSetBase()` with a provided F function value even if it applies the 2426f6dfbefdSBarry 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. 2427bbc1464cSBarry Smith 2428dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetFunction()`, `SNESGetFunction()`, `SNESComputeFunction()`, `MatCreateSNESMF` 2429bbc1464cSBarry Smith @*/ 2430d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESComputeMFFunction(SNES snes, Vec x, Vec y) 2431d71ae5a4SJacob Faibussowitsch { 2432bbc1464cSBarry Smith DM dm; 2433bbc1464cSBarry Smith DMSNES sdm; 2434bbc1464cSBarry Smith 2435bbc1464cSBarry Smith PetscFunctionBegin; 2436bbc1464cSBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 2437bbc1464cSBarry Smith PetscValidHeaderSpecific(x, VEC_CLASSID, 2); 2438bbc1464cSBarry Smith PetscValidHeaderSpecific(y, VEC_CLASSID, 3); 2439bbc1464cSBarry Smith PetscCheckSameComm(snes, 1, x, 2); 2440bbc1464cSBarry Smith PetscCheckSameComm(snes, 1, y, 3); 2441e0f629ddSJacob Faibussowitsch PetscCall(VecValidValues_Internal(x, 2, PETSC_TRUE)); 2442bbc1464cSBarry Smith 24439566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 24449566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &sdm)); 24459566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_FunctionEval, snes, x, y, 0)); 24469566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(x)); 2447bbc1464cSBarry Smith /* ensure domainerror is false prior to computefunction evaluation (may not have been reset) */ 2448bbc1464cSBarry Smith snes->domainerror = PETSC_FALSE; 2449792fecdfSBarry Smith PetscCallBack("SNES callback function", (*sdm->ops->computemffunction)(snes, x, y, sdm->mffunctionctx)); 24509566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(x)); 24519566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_FunctionEval, snes, x, y, 0)); 2452bbc1464cSBarry Smith snes->nfuncs++; 2453bbc1464cSBarry Smith /* 2454bbc1464cSBarry Smith domainerror might not be set on all processes; so we tag vector locally with Inf and the next inner product or norm will 2455bbc1464cSBarry Smith propagate the value to all processes 2456bbc1464cSBarry Smith */ 24571baa6e33SBarry Smith if (snes->domainerror) PetscCall(VecSetInf(y)); 24583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2459bbc1464cSBarry Smith } 2460bbc1464cSBarry Smith 2461bbc1464cSBarry Smith /*@ 2462f6dfbefdSBarry Smith SNESComputeNGS - Calls the Gauss-Seidel function that has been set with `SNESSetNGS()`. 2463c79ef259SPeter Brune 2464c3339decSBarry Smith Collective 2465c79ef259SPeter Brune 2466c79ef259SPeter Brune Input Parameters: 2467f6dfbefdSBarry Smith + snes - the `SNES` context 2468c79ef259SPeter Brune . x - input vector 2469c79ef259SPeter Brune - b - rhs vector 2470c79ef259SPeter Brune 2471c79ef259SPeter Brune Output Parameter: 2472c79ef259SPeter Brune . x - new solution vector 2473c79ef259SPeter Brune 2474dc4c0fb0SBarry Smith Level: developer 2475dc4c0fb0SBarry Smith 2476f6dfbefdSBarry Smith Note: 2477f6dfbefdSBarry Smith `SNESComputeNGS()` is typically used within composed nonlinear solver 2478c79ef259SPeter Brune implementations, so most users would not generally call this routine 2479c79ef259SPeter Brune themselves. 2480c79ef259SPeter Brune 2481dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESNGS`, `SNESSetNGS()`, `SNESComputeFunction()` 2482c79ef259SPeter Brune @*/ 2483d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESComputeNGS(SNES snes, Vec b, Vec x) 2484d71ae5a4SJacob Faibussowitsch { 24856cab3a1bSJed Brown DM dm; 2486942e3340SBarry Smith DMSNES sdm; 2487646217ecSPeter Brune 2488646217ecSPeter Brune PetscFunctionBegin; 2489646217ecSPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 2490064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 2491064a246eSJacob Faibussowitsch if (b) PetscValidHeaderSpecific(b, VEC_CLASSID, 2); 2492064a246eSJacob Faibussowitsch PetscCheckSameComm(snes, 1, x, 3); 2493064a246eSJacob Faibussowitsch if (b) PetscCheckSameComm(snes, 1, b, 2); 2494e0f629ddSJacob Faibussowitsch if (b) PetscCall(VecValidValues_Internal(b, 2, PETSC_TRUE)); 24959566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_NGSEval, snes, x, b, 0)); 24969566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 24979566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &sdm)); 24980fdf79fbSJacob Faibussowitsch PetscCheck(sdm->ops->computegs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetNGS() before SNESComputeNGS(), likely called from SNESSolve()."); 24999566063dSJacob Faibussowitsch if (b) PetscCall(VecLockReadPush(b)); 2500792fecdfSBarry Smith PetscCallBack("SNES callback NGS", (*sdm->ops->computegs)(snes, x, b, sdm->gsctx)); 25019566063dSJacob Faibussowitsch if (b) PetscCall(VecLockReadPop(b)); 25029566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_NGSEval, snes, x, b, 0)); 25033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2504646217ecSPeter Brune } 2505646217ecSPeter Brune 2506d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTestJacobian(SNES snes) 2507d71ae5a4SJacob Faibussowitsch { 250812837594SBarry Smith Mat A, B, C, D, jacobian; 2509e885f1abSBarry Smith Vec x = snes->vec_sol, f = snes->vec_func; 2510e885f1abSBarry Smith PetscReal nrm, gnorm; 251181e7118cSBarry Smith PetscReal threshold = 1.e-5; 25120e276705SLisandro Dalcin MatType mattype; 2513e885f1abSBarry Smith PetscInt m, n, M, N; 2514e885f1abSBarry Smith void *functx; 25152cd624f9SStefano Zampini PetscBool complete_print = PETSC_FALSE, threshold_print = PETSC_FALSE, test = PETSC_FALSE, flg, istranspose; 25163325ff46SBarry Smith PetscViewer viewer, mviewer; 2517e885f1abSBarry Smith MPI_Comm comm; 2518e885f1abSBarry Smith PetscInt tabs; 251912837594SBarry Smith static PetscBool directionsprinted = PETSC_FALSE; 25203325ff46SBarry Smith PetscViewerFormat format; 2521e885f1abSBarry Smith 2522e885f1abSBarry Smith PetscFunctionBegin; 2523d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)snes); 25249566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-snes_test_jacobian", "Compare hand-coded and finite difference Jacobians", "None", &test)); 25259566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-snes_test_jacobian", "Threshold for element difference between hand-coded and finite difference being meaningful", "None", threshold, &threshold, NULL)); 25269566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-snes_test_jacobian_display", "-snes_test_jacobian_view", "3.13", NULL)); 25274ead3382SBarry Smith PetscCall(PetscOptionsViewer("-snes_test_jacobian_view", "View difference between hand-coded and finite difference Jacobians element entries", "None", &mviewer, &format, &complete_print)); 25289566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-snes_test_jacobian_display_threshold", "-snes_test_jacobian", "3.13", "-snes_test_jacobian accepts an optional threshold (since v3.10)")); 25294ead3382SBarry Smith /* Cannot remove the what otherwise would be redundant call to (PetscOptionsReal("-snes_test_jacobian_display_threshold") below because its usage is different than the replacement usage */ 25309566063dSJacob 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)); 2531d0609cedSBarry Smith PetscOptionsEnd(); 25323ba16761SJacob Faibussowitsch if (!test) PetscFunctionReturn(PETSC_SUCCESS); 2533e885f1abSBarry Smith 25349566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)snes, &comm)); 25359566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(comm, &viewer)); 25369566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 25379566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)snes)->tablevel)); 25389566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ---------- Testing Jacobian -------------\n")); 253912837594SBarry Smith if (!complete_print && !directionsprinted) { 25409566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Run with -snes_test_jacobian_view and optionally -snes_test_jacobian <threshold> to show difference\n")); 25419566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " of hand-coded and finite difference Jacobian entries greater than <threshold>.\n")); 254212837594SBarry Smith } 254312837594SBarry Smith if (!directionsprinted) { 25449566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Testing hand-coded Jacobian, if (for double precision runs) ||J - Jfd||_F/||J||_F is\n")); 25459566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " O(1.e-8), the hand-coded Jacobian is probably correct.\n")); 254612837594SBarry Smith directionsprinted = PETSC_TRUE; 2547e885f1abSBarry Smith } 25481baa6e33SBarry Smith if (complete_print) PetscCall(PetscViewerPushFormat(mviewer, format)); 2549e885f1abSBarry Smith 25509566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)snes->jacobian, MATMFFD, &flg)); 255112837594SBarry Smith if (!flg) jacobian = snes->jacobian; 255212837594SBarry Smith else jacobian = snes->jacobian_pre; 255312837594SBarry Smith 2554a82339d0SMatthew G. Knepley if (!x) { 25559566063dSJacob Faibussowitsch PetscCall(MatCreateVecs(jacobian, &x, NULL)); 2556a82339d0SMatthew G. Knepley } else { 25579566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)x)); 2558a82339d0SMatthew G. Knepley } 2559a82339d0SMatthew G. Knepley if (!f) { 25609566063dSJacob Faibussowitsch PetscCall(VecDuplicate(x, &f)); 2561a82339d0SMatthew G. Knepley } else { 25629566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)f)); 2563a82339d0SMatthew G. Knepley } 2564a82339d0SMatthew G. Knepley /* evaluate the function at this point because SNESComputeJacobianDefault() assumes that the function has been evaluated and put into snes->vec_func */ 25659566063dSJacob Faibussowitsch PetscCall(SNESComputeFunction(snes, x, f)); 25669566063dSJacob Faibussowitsch PetscCall(VecDestroy(&f)); 25679566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)snes, SNESKSPTRANSPOSEONLY, &istranspose)); 256812837594SBarry Smith while (jacobian) { 25692cd624f9SStefano Zampini Mat JT = NULL, Jsave = NULL; 25702cd624f9SStefano Zampini 25712cd624f9SStefano Zampini if (istranspose) { 25729566063dSJacob Faibussowitsch PetscCall(MatCreateTranspose(jacobian, &JT)); 25732cd624f9SStefano Zampini Jsave = jacobian; 25742cd624f9SStefano Zampini jacobian = JT; 25752cd624f9SStefano Zampini } 25769566063dSJacob Faibussowitsch PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)jacobian, &flg, MATSEQAIJ, MATMPIAIJ, MATSEQDENSE, MATMPIDENSE, MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPISBAIJ, "")); 257712837594SBarry Smith if (flg) { 257812837594SBarry Smith A = jacobian; 25799566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 258012837594SBarry Smith } else { 25819566063dSJacob Faibussowitsch PetscCall(MatComputeOperator(jacobian, MATAIJ, &A)); 258212837594SBarry Smith } 2583e885f1abSBarry Smith 25849566063dSJacob Faibussowitsch PetscCall(MatGetType(A, &mattype)); 25859566063dSJacob Faibussowitsch PetscCall(MatGetSize(A, &M, &N)); 25869566063dSJacob Faibussowitsch PetscCall(MatGetLocalSize(A, &m, &n)); 25879566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B)); 25889566063dSJacob Faibussowitsch PetscCall(MatSetType(B, mattype)); 25899566063dSJacob Faibussowitsch PetscCall(MatSetSizes(B, m, n, M, N)); 25909566063dSJacob Faibussowitsch PetscCall(MatSetBlockSizesFromMats(B, A, A)); 25919566063dSJacob Faibussowitsch PetscCall(MatSetUp(B)); 25929566063dSJacob Faibussowitsch PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE)); 2593e885f1abSBarry Smith 25949566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, NULL, NULL, &functx)); 25959566063dSJacob Faibussowitsch PetscCall(SNESComputeJacobianDefault(snes, x, B, B, functx)); 259612837594SBarry Smith 25979566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &D)); 25989566063dSJacob Faibussowitsch PetscCall(MatAYPX(D, -1.0, A, DIFFERENT_NONZERO_PATTERN)); 25999566063dSJacob Faibussowitsch PetscCall(MatNorm(D, NORM_FROBENIUS, &nrm)); 26009566063dSJacob Faibussowitsch PetscCall(MatNorm(A, NORM_FROBENIUS, &gnorm)); 26019566063dSJacob Faibussowitsch PetscCall(MatDestroy(&D)); 260212837594SBarry Smith if (!gnorm) gnorm = 1; /* just in case */ 26039566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ||J - Jfd||_F/||J||_F = %g, ||J - Jfd||_F = %g\n", (double)(nrm / gnorm), (double)nrm)); 260412837594SBarry Smith 2605e885f1abSBarry Smith if (complete_print) { 26069566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Hand-coded Jacobian ----------\n")); 26079566063dSJacob Faibussowitsch PetscCall(MatView(A, mviewer)); 26089566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Finite difference Jacobian ----------\n")); 26099566063dSJacob Faibussowitsch PetscCall(MatView(B, mviewer)); 2610e885f1abSBarry Smith } 2611e885f1abSBarry Smith 2612df10fb39SFande Kong if (threshold_print || complete_print) { 2613e885f1abSBarry Smith PetscInt Istart, Iend, *ccols, bncols, cncols, j, row; 2614e885f1abSBarry Smith PetscScalar *cvals; 2615e885f1abSBarry Smith const PetscInt *bcols; 2616e885f1abSBarry Smith const PetscScalar *bvals; 2617e885f1abSBarry Smith 26189566063dSJacob Faibussowitsch PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C)); 26199566063dSJacob Faibussowitsch PetscCall(MatSetType(C, mattype)); 26209566063dSJacob Faibussowitsch PetscCall(MatSetSizes(C, m, n, M, N)); 26219566063dSJacob Faibussowitsch PetscCall(MatSetBlockSizesFromMats(C, A, A)); 26229566063dSJacob Faibussowitsch PetscCall(MatSetUp(C)); 26239566063dSJacob Faibussowitsch PetscCall(MatSetOption(C, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE)); 26240e276705SLisandro Dalcin 26259566063dSJacob Faibussowitsch PetscCall(MatAYPX(B, -1.0, A, DIFFERENT_NONZERO_PATTERN)); 26269566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(B, &Istart, &Iend)); 2627e885f1abSBarry Smith 2628e885f1abSBarry Smith for (row = Istart; row < Iend; row++) { 26299566063dSJacob Faibussowitsch PetscCall(MatGetRow(B, row, &bncols, &bcols, &bvals)); 26309566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(bncols, &ccols, bncols, &cvals)); 2631e885f1abSBarry Smith for (j = 0, cncols = 0; j < bncols; j++) { 263223a52b1dSBarry Smith if (PetscAbsScalar(bvals[j]) > threshold) { 2633e885f1abSBarry Smith ccols[cncols] = bcols[j]; 2634e885f1abSBarry Smith cvals[cncols] = bvals[j]; 2635e885f1abSBarry Smith cncols += 1; 2636e885f1abSBarry Smith } 2637e885f1abSBarry Smith } 263848a46eb9SPierre Jolivet if (cncols) PetscCall(MatSetValues(C, 1, &row, cncols, ccols, cvals, INSERT_VALUES)); 26399566063dSJacob Faibussowitsch PetscCall(MatRestoreRow(B, row, &bncols, &bcols, &bvals)); 26409566063dSJacob Faibussowitsch PetscCall(PetscFree2(ccols, cvals)); 2641e885f1abSBarry Smith } 26429566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY)); 26439566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY)); 26449566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " Hand-coded minus finite-difference Jacobian with tolerance %g ----------\n", (double)threshold)); 26459566063dSJacob Faibussowitsch PetscCall(MatView(C, complete_print ? mviewer : viewer)); 26469566063dSJacob Faibussowitsch PetscCall(MatDestroy(&C)); 2647e885f1abSBarry Smith } 26489566063dSJacob Faibussowitsch PetscCall(MatDestroy(&A)); 26499566063dSJacob Faibussowitsch PetscCall(MatDestroy(&B)); 26509566063dSJacob Faibussowitsch PetscCall(MatDestroy(&JT)); 26512cd624f9SStefano Zampini if (Jsave) jacobian = Jsave; 265212837594SBarry Smith if (jacobian != snes->jacobian_pre) { 265312837594SBarry Smith jacobian = snes->jacobian_pre; 26549566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " ---------- Testing Jacobian for preconditioner -------------\n")); 26559371c9d4SSatish Balay } else jacobian = NULL; 265612837594SBarry Smith } 26579566063dSJacob Faibussowitsch PetscCall(VecDestroy(&x)); 26581baa6e33SBarry Smith if (complete_print) PetscCall(PetscViewerPopFormat(mviewer)); 26599566063dSJacob Faibussowitsch if (mviewer) PetscCall(PetscViewerDestroy(&mviewer)); 26609566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 26613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2662e885f1abSBarry Smith } 2663e885f1abSBarry Smith 266462fef451SLois Curfman McInnes /*@ 2665f6dfbefdSBarry Smith SNESComputeJacobian - Computes the Jacobian matrix that has been set with `SNESSetJacobian()`. 266662fef451SLois Curfman McInnes 2667c3339decSBarry Smith Collective 2668c7afd0dbSLois Curfman McInnes 266962fef451SLois Curfman McInnes Input Parameters: 2670f6dfbefdSBarry Smith + snes - the `SNES` context 2671c7afd0dbSLois Curfman McInnes - x - input vector 267262fef451SLois Curfman McInnes 267362fef451SLois Curfman McInnes Output Parameters: 2674c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 2675f6dfbefdSBarry Smith - B - optional matrix for building the preconditioner 2676fee21e36SBarry Smith 2677e35cf81dSBarry Smith Options Database Keys: 267867b8a455SSatish Balay + -snes_lag_preconditioner <lag> - how often to rebuild preconditioner 267967b8a455SSatish Balay . -snes_lag_jacobian <lag> - how often to rebuild Jacobian 2680455a5933SJed 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. 2681455a5933SJed 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 2682693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 2683693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 2684693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 26854c30e9fbSJed Brown . -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 268694d6a431SBarry Smith . -snes_compare_coloring - Compute the finite difference Jacobian using coloring and display norms of difference 2687a5b23f4aSJose E. Roman . -snes_compare_coloring_display - Compute the finite difference Jacobian using coloring and display verbose differences 2688c01495d3SJed Brown . -snes_compare_coloring_threshold - Display only those matrix entries that differ by more than a given threshold 2689dc4c0fb0SBarry Smith . -snes_compare_coloring_threshold_atol - Absolute tolerance for difference in matrix entries to be displayed by `-snes_compare_coloring_threshold` 2690dc4c0fb0SBarry Smith . -snes_compare_coloring_threshold_rtol - Relative tolerance for difference in matrix entries to be displayed by `-snes_compare_coloring_threshold` 2691a5b23f4aSJose E. Roman . -snes_compare_coloring_draw - Compute the finite difference Jacobian using coloring and draw differences 2692a5b23f4aSJose E. Roman - -snes_compare_coloring_draw_contour - Compute the finite difference Jacobian using coloring and show contours of matrices and differences 2693c01495d3SJed Brown 2694dc4c0fb0SBarry Smith Level: developer 2695dc4c0fb0SBarry Smith 2696f6dfbefdSBarry Smith Note: 269762fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 269862fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 269962fef451SLois Curfman McInnes 2700f6dfbefdSBarry Smith Developer Note: 2701dc4c0fb0SBarry Smith 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 2702dc4c0fb0SBarry Smith for with the `SNESType` of test that has been removed. 2703e885f1abSBarry Smith 2704dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetJacobian()`, `KSPSetOperators()`, `MatStructure`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobian()` 270562fef451SLois Curfman McInnes @*/ 2706d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESComputeJacobian(SNES snes, Vec X, Mat A, Mat B) 2707d71ae5a4SJacob Faibussowitsch { 2708ace3abfcSBarry Smith PetscBool flag; 27096cab3a1bSJed Brown DM dm; 2710942e3340SBarry Smith DMSNES sdm; 2711e0e3a89bSBarry Smith KSP ksp; 27123a40ed3dSBarry Smith 27133a40ed3dSBarry Smith PetscFunctionBegin; 27140700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 27150700a824SBarry Smith PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 2716c9780b6fSBarry Smith PetscCheckSameComm(snes, 1, X, 2); 2717e0f629ddSJacob Faibussowitsch PetscCall(VecValidValues_Internal(X, 2, PETSC_TRUE)); 27189566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 27199566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &sdm)); 27203232da50SPeter Brune 2721ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 2722fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 2723fe3ffe1eSBarry Smith snes->lagjacobian = -1; 2724f5af7f23SKarl Rupp 27259566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n")); 2726fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 27279566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Reusing Jacobian/preconditioner because lag is -1\n")); 27289566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flag)); 2729ebd3b9afSBarry Smith if (flag) { 27309566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 27319566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 2732ebd3b9afSBarry Smith } 27333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 273437ec4e1aSPeter Brune } else if (snes->lagjacobian > 1 && (snes->iter + snes->jac_iter) % snes->lagjacobian) { 273563a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Reusing Jacobian/preconditioner because lag is %" PetscInt_FMT " and SNES iteration is %" PetscInt_FMT "\n", snes->lagjacobian, snes->iter)); 27369566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flag)); 2737ebd3b9afSBarry Smith if (flag) { 27389566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 27399566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 2740ebd3b9afSBarry Smith } 27413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2742e35cf81dSBarry Smith } 2743efd4aadfSBarry Smith if (snes->npc && snes->npcside == PC_LEFT) { 27449566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 27459566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 27463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2747d728fb7dSPeter Brune } 2748e35cf81dSBarry Smith 27499566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_JacobianEval, snes, X, A, B)); 27509566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(X)); 2751800f99ffSJeremy L Thompson { 2752800f99ffSJeremy L Thompson void *ctx; 2753800f99ffSJeremy L Thompson PetscErrorCode (*J)(SNES, Vec, Mat, Mat, void *); 2754800f99ffSJeremy L Thompson PetscCall(DMSNESGetJacobian(dm, &J, &ctx)); 2755800f99ffSJeremy L Thompson PetscCallBack("SNES callback Jacobian", (*J)(snes, X, A, B, ctx)); 2756800f99ffSJeremy L Thompson } 27579566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(X)); 27589566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_JacobianEval, snes, X, A, B)); 275928d58a37SPierre Jolivet 276028d58a37SPierre Jolivet /* attach latest linearization point to the preconditioning matrix */ 27619566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)B, "__SNES_latest_X", (PetscObject)X)); 2762a8054027SBarry Smith 2763e0e3a89bSBarry Smith /* the next line ensures that snes->ksp exists */ 27649566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, &ksp)); 27653b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 27669566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Rebuilding preconditioner exactly once since lag is -2\n")); 27679566063dSJacob Faibussowitsch PetscCall(KSPSetReusePreconditioner(snes->ksp, PETSC_FALSE)); 27683b4f5425SBarry Smith snes->lagpreconditioner = -1; 27693b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 27709566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Reusing preconditioner because lag is -1\n")); 27719566063dSJacob Faibussowitsch PetscCall(KSPSetReusePreconditioner(snes->ksp, PETSC_TRUE)); 277237ec4e1aSPeter Brune } else if (snes->lagpreconditioner > 1 && (snes->iter + snes->pre_iter) % snes->lagpreconditioner) { 277363a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Reusing preconditioner because lag is %" PetscInt_FMT " and SNES iteration is %" PetscInt_FMT "\n", snes->lagpreconditioner, snes->iter)); 27749566063dSJacob Faibussowitsch PetscCall(KSPSetReusePreconditioner(snes->ksp, PETSC_TRUE)); 2775d1e9a80fSBarry Smith } else { 27769566063dSJacob Faibussowitsch PetscCall(PetscInfo(snes, "Rebuilding preconditioner\n")); 27779566063dSJacob Faibussowitsch PetscCall(KSPSetReusePreconditioner(snes->ksp, PETSC_FALSE)); 2778a8054027SBarry Smith } 2779a8054027SBarry Smith 27809566063dSJacob Faibussowitsch PetscCall(SNESTestJacobian(snes)); 27816d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 278294ab13aaSBarry Smith /* PetscValidHeaderSpecific(A,MAT_CLASSID,3); 278394ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); */ 2784693365a8SJed Brown { 2785693365a8SJed Brown PetscBool flag = PETSC_FALSE, flag_draw = PETSC_FALSE, flag_contour = PETSC_FALSE, flag_operator = PETSC_FALSE; 27869566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_explicit", NULL, NULL, &flag)); 27879566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_explicit_draw", NULL, NULL, &flag_draw)); 27889566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_explicit_draw_contour", NULL, NULL, &flag_contour)); 27899566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_operator", NULL, NULL, &flag_operator)); 2790693365a8SJed Brown if (flag || flag_draw || flag_contour) { 27910298fd71SBarry Smith Mat Bexp_mine = NULL, Bexp, FDexp; 2792693365a8SJed Brown PetscViewer vdraw, vstdout; 27936b3a5b13SJed Brown PetscBool flg; 2794693365a8SJed Brown if (flag_operator) { 27959566063dSJacob Faibussowitsch PetscCall(MatComputeOperator(A, MATAIJ, &Bexp_mine)); 2796693365a8SJed Brown Bexp = Bexp_mine; 2797693365a8SJed Brown } else { 2798693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 27999566063dSJacob Faibussowitsch PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)B, &flg, MATSEQAIJ, MATMPIAIJ, MATSEQDENSE, MATMPIDENSE, MATSEQBAIJ, MATMPIBAIJ, MATSEQSBAIJ, MATMPIBAIJ, "")); 280094ab13aaSBarry Smith if (flg) Bexp = B; 2801693365a8SJed Brown else { 2802693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 28039566063dSJacob Faibussowitsch PetscCall(MatComputeOperator(B, MATAIJ, &Bexp_mine)); 2804693365a8SJed Brown Bexp = Bexp_mine; 2805693365a8SJed Brown } 2806693365a8SJed Brown } 28079566063dSJacob Faibussowitsch PetscCall(MatConvert(Bexp, MATSAME, MAT_INITIAL_MATRIX, &FDexp)); 28089566063dSJacob Faibussowitsch PetscCall(SNESComputeJacobianDefault(snes, X, FDexp, FDexp, NULL)); 28099566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes), &vstdout)); 2810693365a8SJed Brown if (flag_draw || flag_contour) { 28119566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes), NULL, "Explicit Jacobians", PETSC_DECIDE, PETSC_DECIDE, 300, 300, &vdraw)); 28129566063dSJacob Faibussowitsch if (flag_contour) PetscCall(PetscViewerPushFormat(vdraw, PETSC_VIEWER_DRAW_CONTOUR)); 28130298fd71SBarry Smith } else vdraw = NULL; 28149566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout, "Explicit %s\n", flag_operator ? "Jacobian" : "preconditioning Jacobian")); 28159566063dSJacob Faibussowitsch if (flag) PetscCall(MatView(Bexp, vstdout)); 28169566063dSJacob Faibussowitsch if (vdraw) PetscCall(MatView(Bexp, vdraw)); 28179566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout, "Finite difference Jacobian\n")); 28189566063dSJacob Faibussowitsch if (flag) PetscCall(MatView(FDexp, vstdout)); 28199566063dSJacob Faibussowitsch if (vdraw) PetscCall(MatView(FDexp, vdraw)); 28209566063dSJacob Faibussowitsch PetscCall(MatAYPX(FDexp, -1.0, Bexp, SAME_NONZERO_PATTERN)); 28219566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout, "User-provided matrix minus finite difference Jacobian\n")); 28229566063dSJacob Faibussowitsch if (flag) PetscCall(MatView(FDexp, vstdout)); 2823693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 28249566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(vdraw, PETSC_VIEWER_DRAW_CONTOUR)); 28259566063dSJacob Faibussowitsch PetscCall(MatView(FDexp, vdraw)); 28269566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(vdraw)); 2827693365a8SJed Brown } 28289566063dSJacob Faibussowitsch if (flag_contour) PetscCall(PetscViewerPopFormat(vdraw)); 28299566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&vdraw)); 28309566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Bexp_mine)); 28319566063dSJacob Faibussowitsch PetscCall(MatDestroy(&FDexp)); 2832693365a8SJed Brown } 2833693365a8SJed Brown } 28344c30e9fbSJed Brown { 28356719d8e4SJed Brown PetscBool flag = PETSC_FALSE, flag_display = PETSC_FALSE, flag_draw = PETSC_FALSE, flag_contour = PETSC_FALSE, flag_threshold = PETSC_FALSE; 28366719d8e4SJed Brown PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON, threshold_rtol = 10 * PETSC_SQRT_MACHINE_EPSILON; 28379566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring", NULL, NULL, &flag)); 28389566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_display", NULL, NULL, &flag_display)); 28399566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_draw", NULL, NULL, &flag_draw)); 28409566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_draw_contour", NULL, NULL, &flag_contour)); 28419566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_threshold", NULL, NULL, &flag_threshold)); 284227b0f280SBarry Smith if (flag_threshold) { 28439566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_threshold_rtol", &threshold_rtol, NULL)); 28449566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_compare_coloring_threshold_atol", &threshold_atol, NULL)); 284527b0f280SBarry Smith } 28466719d8e4SJed Brown if (flag || flag_display || flag_draw || flag_contour || flag_threshold) { 28474c30e9fbSJed Brown Mat Bfd; 28484c30e9fbSJed Brown PetscViewer vdraw, vstdout; 2849335efc43SPeter Brune MatColoring coloring; 28504c30e9fbSJed Brown ISColoring iscoloring; 28514c30e9fbSJed Brown MatFDColoring matfdcoloring; 28524c30e9fbSJed Brown PetscErrorCode (*func)(SNES, Vec, Vec, void *); 28534c30e9fbSJed Brown void *funcctx; 28546719d8e4SJed Brown PetscReal norm1, norm2, normmax; 28554c30e9fbSJed Brown 28569566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &Bfd)); 28579566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(Bfd, &coloring)); 28589566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(coloring, MATCOLORINGSL)); 28599566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(coloring)); 28609566063dSJacob Faibussowitsch PetscCall(MatColoringApply(coloring, &iscoloring)); 28619566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&coloring)); 28629566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(Bfd, iscoloring, &matfdcoloring)); 28639566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(matfdcoloring)); 28649566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(Bfd, iscoloring, matfdcoloring)); 28659566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 28664c30e9fbSJed Brown 28674c30e9fbSJed Brown /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */ 28689566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, NULL, &func, &funcctx)); 28699566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFunction(matfdcoloring, (PetscErrorCode(*)(void))func, funcctx)); 28709566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring, ((PetscObject)snes)->prefix)); 28719566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring, "coloring_")); 28729566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(matfdcoloring)); 28739566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(Bfd, matfdcoloring, X, snes)); 28749566063dSJacob Faibussowitsch PetscCall(MatFDColoringDestroy(&matfdcoloring)); 28754c30e9fbSJed Brown 28769566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes), &vstdout)); 28774c30e9fbSJed Brown if (flag_draw || flag_contour) { 28789566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes), NULL, "Colored Jacobians", PETSC_DECIDE, PETSC_DECIDE, 300, 300, &vdraw)); 28799566063dSJacob Faibussowitsch if (flag_contour) PetscCall(PetscViewerPushFormat(vdraw, PETSC_VIEWER_DRAW_CONTOUR)); 28800298fd71SBarry Smith } else vdraw = NULL; 28819566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout, "Explicit preconditioning Jacobian\n")); 28829566063dSJacob Faibussowitsch if (flag_display) PetscCall(MatView(B, vstdout)); 28839566063dSJacob Faibussowitsch if (vdraw) PetscCall(MatView(B, vdraw)); 28849566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout, "Colored Finite difference Jacobian\n")); 28859566063dSJacob Faibussowitsch if (flag_display) PetscCall(MatView(Bfd, vstdout)); 28869566063dSJacob Faibussowitsch if (vdraw) PetscCall(MatView(Bfd, vdraw)); 28879566063dSJacob Faibussowitsch PetscCall(MatAYPX(Bfd, -1.0, B, SAME_NONZERO_PATTERN)); 28889566063dSJacob Faibussowitsch PetscCall(MatNorm(Bfd, NORM_1, &norm1)); 28899566063dSJacob Faibussowitsch PetscCall(MatNorm(Bfd, NORM_FROBENIUS, &norm2)); 28909566063dSJacob Faibussowitsch PetscCall(MatNorm(Bfd, NORM_MAX, &normmax)); 28919566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout, "User-provided matrix minus finite difference Jacobian, norm1=%g normFrob=%g normmax=%g\n", (double)norm1, (double)norm2, (double)normmax)); 28929566063dSJacob Faibussowitsch if (flag_display) PetscCall(MatView(Bfd, vstdout)); 28934c30e9fbSJed Brown if (vdraw) { /* Always use contour for the difference */ 28949566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(vdraw, PETSC_VIEWER_DRAW_CONTOUR)); 28959566063dSJacob Faibussowitsch PetscCall(MatView(Bfd, vdraw)); 28969566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(vdraw)); 28974c30e9fbSJed Brown } 28989566063dSJacob Faibussowitsch if (flag_contour) PetscCall(PetscViewerPopFormat(vdraw)); 28996719d8e4SJed Brown 29006719d8e4SJed Brown if (flag_threshold) { 29016719d8e4SJed Brown PetscInt bs, rstart, rend, i; 29029566063dSJacob Faibussowitsch PetscCall(MatGetBlockSize(B, &bs)); 29039566063dSJacob Faibussowitsch PetscCall(MatGetOwnershipRange(B, &rstart, &rend)); 29046719d8e4SJed Brown for (i = rstart; i < rend; i++) { 29056719d8e4SJed Brown const PetscScalar *ba, *ca; 29066719d8e4SJed Brown const PetscInt *bj, *cj; 29076719d8e4SJed Brown PetscInt bn, cn, j, maxentrycol = -1, maxdiffcol = -1, maxrdiffcol = -1; 29086719d8e4SJed Brown PetscReal maxentry = 0, maxdiff = 0, maxrdiff = 0; 29099566063dSJacob Faibussowitsch PetscCall(MatGetRow(B, i, &bn, &bj, &ba)); 29109566063dSJacob Faibussowitsch PetscCall(MatGetRow(Bfd, i, &cn, &cj, &ca)); 29115f80ce2aSJacob Faibussowitsch PetscCheck(bn == cn, ((PetscObject)A)->comm, PETSC_ERR_PLIB, "Unexpected different nonzero pattern in -snes_compare_coloring_threshold"); 29126719d8e4SJed Brown for (j = 0; j < bn; j++) { 29136719d8e4SJed Brown PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol * PetscAbsScalar(ba[j])); 29146719d8e4SJed Brown if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) { 29156719d8e4SJed Brown maxentrycol = bj[j]; 29166719d8e4SJed Brown maxentry = PetscRealPart(ba[j]); 29176719d8e4SJed Brown } 29186719d8e4SJed Brown if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) { 29196719d8e4SJed Brown maxdiffcol = bj[j]; 29206719d8e4SJed Brown maxdiff = PetscRealPart(ca[j]); 29216719d8e4SJed Brown } 29226719d8e4SJed Brown if (rdiff > maxrdiff) { 29236719d8e4SJed Brown maxrdiffcol = bj[j]; 29246719d8e4SJed Brown maxrdiff = rdiff; 29256719d8e4SJed Brown } 29266719d8e4SJed Brown } 29276719d8e4SJed Brown if (maxrdiff > 1) { 292863a3b9bcSJacob 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)); 29296719d8e4SJed Brown for (j = 0; j < bn; j++) { 29306719d8e4SJed Brown PetscReal rdiff; 29316719d8e4SJed Brown rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol * PetscAbsScalar(ba[j])); 293248a46eb9SPierre Jolivet if (rdiff > 1) PetscCall(PetscViewerASCIIPrintf(vstdout, " (%" PetscInt_FMT ",%g:%g)", bj[j], (double)PetscRealPart(ba[j]), (double)PetscRealPart(ca[j]))); 29336719d8e4SJed Brown } 293463a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(vstdout, "\n")); 29356719d8e4SJed Brown } 29369566063dSJacob Faibussowitsch PetscCall(MatRestoreRow(B, i, &bn, &bj, &ba)); 29379566063dSJacob Faibussowitsch PetscCall(MatRestoreRow(Bfd, i, &cn, &cj, &ca)); 29386719d8e4SJed Brown } 29396719d8e4SJed Brown } 29409566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&vdraw)); 29419566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Bfd)); 29424c30e9fbSJed Brown } 29434c30e9fbSJed Brown } 29443ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 29459b94acceSBarry Smith } 29469b94acceSBarry Smith 2947bf388a1fSBarry Smith /*MC 2948f6dfbefdSBarry Smith SNESJacobianFunction - Function used by `SNES` to compute the nonlinear Jacobian of the function to be solved by `SNES` 2949bf388a1fSBarry Smith 2950bf388a1fSBarry Smith Synopsis: 2951411c0326SBarry Smith #include "petscsnes.h" 2952411c0326SBarry Smith PetscErrorCode SNESJacobianFunction(SNES snes,Vec x,Mat Amat,Mat Pmat,void *ctx); 2953bf388a1fSBarry Smith 2954c3339decSBarry Smith Collective 29551843f636SBarry Smith 29561843f636SBarry Smith Input Parameters: 29571843f636SBarry Smith + x - input vector, the Jacobian is to be computed at this value 2958bf388a1fSBarry Smith - ctx - [optional] user-defined Jacobian context 2959bf388a1fSBarry Smith 29601843f636SBarry Smith Output Parameters: 29611843f636SBarry Smith + Amat - the matrix that defines the (approximate) Jacobian 2962dc4c0fb0SBarry Smith - Pmat - the matrix to be used in constructing the preconditioner, usually the same as `Amat`. 29631843f636SBarry Smith 2964878cb397SSatish Balay Level: intermediate 2965878cb397SSatish Balay 2966dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetFunction()`, `SNESGetFunction()`, `SNESSetJacobian()`, `SNESGetJacobian()` 2967bf388a1fSBarry Smith M*/ 2968bf388a1fSBarry Smith 29699b94acceSBarry Smith /*@C 29709b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 2971044dda88SLois Curfman McInnes location to store the matrix. 29729b94acceSBarry Smith 2973c3339decSBarry Smith Logically Collective 2974c7afd0dbSLois Curfman McInnes 29759b94acceSBarry Smith Input Parameters: 2976f6dfbefdSBarry Smith + snes - the `SNES` context 2977e5d3d808SBarry Smith . Amat - the matrix that defines the (approximate) Jacobian 2978dc4c0fb0SBarry Smith . Pmat - the matrix to be used in constructing the preconditioner, usually the same as `Amat`. 2979dc4c0fb0SBarry Smith . J - Jacobian evaluation routine (if `NULL` then `SNES` retains any previously set value), see `SNESJacobianFunction` for details 2980c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 2981dc4c0fb0SBarry Smith Jacobian evaluation routine (may be `NULL`) (if `NULL` then `SNES` retains any previously set value) 2982dc4c0fb0SBarry Smith 2983dc4c0fb0SBarry Smith Level: beginner 29849b94acceSBarry Smith 29859b94acceSBarry Smith Notes: 2986dc4c0fb0SBarry Smith If the `Amat` matrix and `Pmat` matrix are different you must call `MatAssemblyBegin()`/`MatAssemblyEnd()` on 298716913363SBarry Smith each matrix. 298816913363SBarry Smith 2989dc4c0fb0SBarry Smith If you know the operator `Amat` has a null space you can use `MatSetNullSpace()` and `MatSetTransposeNullSpace()` to supply the null 2990dc4c0fb0SBarry Smith space to `Amat` and the `KSP` solvers will automatically use that null space as needed during the solution process. 2991895c21f2SBarry Smith 2992dc4c0fb0SBarry Smith If using `SNESComputeJacobianDefaultColor()` to assemble a Jacobian, the `ctx` argument 2993f6dfbefdSBarry Smith must be a `MatFDColoring`. 2994a8a26c1eSJed Brown 2995c3cc8fd1SJed Brown Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian. One common 2996f6dfbefdSBarry Smith example is to use the "Picard linearization" which only differentiates through the highest order parts of each term using `SNESSetPicard()` 2997c3cc8fd1SJed Brown 2998dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `KSPSetOperators()`, `SNESSetFunction()`, `MatMFFDComputeJacobian()`, `SNESComputeJacobianDefaultColor()`, `MatStructure`, 2999db781477SPatrick Sanan `SNESSetPicard()`, `SNESJacobianFunction` 30009b94acceSBarry Smith @*/ 3001d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetJacobian(SNES snes, Mat Amat, Mat Pmat, PetscErrorCode (*J)(SNES, Vec, Mat, Mat, void *), void *ctx) 3002d71ae5a4SJacob Faibussowitsch { 30036cab3a1bSJed Brown DM dm; 30043a7fca6bSBarry Smith 30053a40ed3dSBarry Smith PetscFunctionBegin; 30060700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3007e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2); 3008e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3); 3009e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(snes, 1, Amat, 2); 3010e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(snes, 1, Pmat, 3); 30119566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 30129566063dSJacob Faibussowitsch PetscCall(DMSNESSetJacobian(dm, J, ctx)); 3013e5d3d808SBarry Smith if (Amat) { 30149566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Amat)); 30159566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->jacobian)); 3016f5af7f23SKarl Rupp 3017e5d3d808SBarry Smith snes->jacobian = Amat; 30183a7fca6bSBarry Smith } 3019e5d3d808SBarry Smith if (Pmat) { 30209566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Pmat)); 30219566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->jacobian_pre)); 3022f5af7f23SKarl Rupp 3023e5d3d808SBarry Smith snes->jacobian_pre = Pmat; 30243a7fca6bSBarry Smith } 30253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 30269b94acceSBarry Smith } 302762fef451SLois Curfman McInnes 3028c2aafc4cSSatish Balay /*@C 3029b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 3030b4fd4287SBarry Smith provided context for evaluating the Jacobian. 3031b4fd4287SBarry Smith 3032f6dfbefdSBarry Smith Not Collective, but `Mat` object will be parallel if `SNES` object is 3033c7afd0dbSLois Curfman McInnes 3034b4fd4287SBarry Smith Input Parameter: 3035b4fd4287SBarry Smith . snes - the nonlinear solver context 3036b4fd4287SBarry Smith 3037b4fd4287SBarry Smith Output Parameters: 3038dc4c0fb0SBarry Smith + Amat - location to stash (approximate) Jacobian matrix (or `NULL`) 3039dc4c0fb0SBarry Smith . Pmat - location to stash matrix used to compute the preconditioner (or `NULL`) 304020f4b53cSBarry Smith . J - location to put Jacobian function (or `NULL`), for calling sequence see `SNESJacobianFunction` 3041dc4c0fb0SBarry Smith - ctx - location to stash Jacobian ctx (or `NULL`) 3042fee21e36SBarry Smith 304336851e7fSLois Curfman McInnes Level: advanced 304436851e7fSLois Curfman McInnes 3045dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `Mat`, `SNESSetJacobian()`, `SNESComputeJacobian()`, `SNESJacobianFunction`, `SNESGetFunction()` 3046b4fd4287SBarry Smith @*/ 3047d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetJacobian(SNES snes, Mat *Amat, Mat *Pmat, PetscErrorCode (**J)(SNES, Vec, Mat, Mat, void *), void **ctx) 3048d71ae5a4SJacob Faibussowitsch { 30496cab3a1bSJed Brown DM dm; 30506cab3a1bSJed Brown 30513a40ed3dSBarry Smith PetscFunctionBegin; 30520700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3053e5d3d808SBarry Smith if (Amat) *Amat = snes->jacobian; 3054e5d3d808SBarry Smith if (Pmat) *Pmat = snes->jacobian_pre; 30559566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 3056800f99ffSJeremy L Thompson PetscCall(DMSNESGetJacobian(dm, J, ctx)); 30573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3058b4fd4287SBarry Smith } 3059b4fd4287SBarry Smith 3060d71ae5a4SJacob Faibussowitsch static PetscErrorCode SNESSetDefaultComputeJacobian(SNES snes) 3061d71ae5a4SJacob Faibussowitsch { 306258b371f3SBarry Smith DM dm; 306358b371f3SBarry Smith DMSNES sdm; 306458b371f3SBarry Smith 306558b371f3SBarry Smith PetscFunctionBegin; 30669566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 30679566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &sdm)); 306858b371f3SBarry Smith if (!sdm->ops->computejacobian && snes->jacobian_pre) { 306958b371f3SBarry Smith DM dm; 307058b371f3SBarry Smith PetscBool isdense, ismf; 307158b371f3SBarry Smith 30729566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 30739566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)snes->jacobian_pre, &isdense, MATSEQDENSE, MATMPIDENSE, MATDENSE, NULL)); 30749566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)snes->jacobian_pre, &ismf, MATMFFD, MATSHELL, NULL)); 307558b371f3SBarry Smith if (isdense) { 30769566063dSJacob Faibussowitsch PetscCall(DMSNESSetJacobian(dm, SNESComputeJacobianDefault, NULL)); 307758b371f3SBarry Smith } else if (!ismf) { 30789566063dSJacob Faibussowitsch PetscCall(DMSNESSetJacobian(dm, SNESComputeJacobianDefaultColor, NULL)); 307958b371f3SBarry Smith } 308058b371f3SBarry Smith } 30813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 308258b371f3SBarry Smith } 308358b371f3SBarry Smith 30849b94acceSBarry Smith /*@ 30859b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 3086272ac6f2SLois Curfman McInnes of a nonlinear solver. 30879b94acceSBarry Smith 3088c3339decSBarry Smith Collective 3089fee21e36SBarry Smith 30902fe279fdSBarry Smith Input Parameter: 3091f6dfbefdSBarry Smith . snes - the `SNES` context 3092c7afd0dbSLois Curfman McInnes 3093dc4c0fb0SBarry Smith Level: advanced 3094dc4c0fb0SBarry Smith 3095f6dfbefdSBarry Smith Note: 3096f6dfbefdSBarry Smith For basic use of the `SNES` solvers the user need not explicitly call 3097f6dfbefdSBarry Smith `SNESSetUp()`, since these actions will automatically occur during 3098f6dfbefdSBarry Smith the call to `SNESSolve()`. However, if one wishes to control this 3099f6dfbefdSBarry Smith phase separately, `SNESSetUp()` should be called after `SNESCreate()` 3100f6dfbefdSBarry Smith and optional routines of the form SNESSetXXX(), but before `SNESSolve()`. 3101272ac6f2SLois Curfman McInnes 3102dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESCreate()`, `SNESSolve()`, `SNESDestroy()` 31039b94acceSBarry Smith @*/ 3104d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetUp(SNES snes) 3105d71ae5a4SJacob Faibussowitsch { 31066cab3a1bSJed Brown DM dm; 3107942e3340SBarry Smith DMSNES sdm; 3108c35f09e5SBarry Smith SNESLineSearch linesearch, pclinesearch; 31096e2a1849SPeter Brune void *lsprectx, *lspostctx; 31109b5c1c08SStefano Zampini PetscBool mf_operator, mf; 31119b5c1c08SStefano Zampini Vec f, fpc; 31129b5c1c08SStefano Zampini void *funcctx; 31139b5c1c08SStefano Zampini void *jacctx, *appctx; 31149b5c1c08SStefano Zampini Mat j, jpre; 31156b2b7091SBarry Smith PetscErrorCode (*precheck)(SNESLineSearch, Vec, Vec, PetscBool *, void *); 31166b2b7091SBarry Smith PetscErrorCode (*postcheck)(SNESLineSearch, Vec, Vec, Vec, PetscBool *, PetscBool *, void *); 31176e2a1849SPeter Brune PetscErrorCode (*func)(SNES, Vec, Vec, void *); 3118d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES, Vec, Mat, Mat, void *); 31193a40ed3dSBarry Smith 31203a40ed3dSBarry Smith PetscFunctionBegin; 31210700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 31223ba16761SJacob Faibussowitsch if (snes->setupcalled) PetscFunctionReturn(PETSC_SUCCESS); 3123fc8bc0e3SRichard Tran Mills PetscCall(PetscLogEventBegin(SNES_SetUp, snes, 0, 0, 0)); 31249b94acceSBarry Smith 312548a46eb9SPierre Jolivet if (!((PetscObject)snes)->type_name) PetscCall(SNESSetType(snes, SNESNEWTONLS)); 312685385478SLisandro Dalcin 31279566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, &snes->vec_func, NULL, NULL)); 312858c9b817SLisandro Dalcin 31299566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 31309566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(dm, &sdm)); 31319566063dSJacob Faibussowitsch PetscCall(SNESSetDefaultComputeJacobian(snes)); 313258b371f3SBarry Smith 313348a46eb9SPierre Jolivet if (!snes->vec_func) PetscCall(DMCreateGlobalVector(dm, &snes->vec_func)); 3134efd51863SBarry Smith 313548a46eb9SPierre Jolivet if (!snes->ksp) PetscCall(SNESGetKSP(snes, &snes->ksp)); 3136b710008aSBarry Smith 3137d8d34be6SBarry Smith if (snes->linesearch) { 31389566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &snes->linesearch)); 31399566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetFunction(snes->linesearch, SNESComputeFunction)); 3140d8d34be6SBarry Smith } 31419e764e56SPeter Brune 31429b5c1c08SStefano Zampini PetscCall(SNESGetUseMatrixFree(snes, &mf_operator, &mf)); 3143b552625fSStefano Zampini if (snes->npc && snes->npcside == PC_LEFT) { 3144172a4300SPeter Brune snes->mf = PETSC_TRUE; 3145172a4300SPeter Brune snes->mf_operator = PETSC_FALSE; 3146172a4300SPeter Brune } 3147d8f46077SPeter Brune 3148efd4aadfSBarry Smith if (snes->npc) { 31496e2a1849SPeter Brune /* copy the DM over */ 31509566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 31519566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes->npc, dm)); 31526e2a1849SPeter Brune 31539566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, &f, &func, &funcctx)); 31549566063dSJacob Faibussowitsch PetscCall(VecDuplicate(f, &fpc)); 31559566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes->npc, fpc, func, funcctx)); 31569566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, &j, &jpre, &jac, &jacctx)); 31579566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes->npc, j, jpre, jac, jacctx)); 31589566063dSJacob Faibussowitsch PetscCall(SNESGetApplicationContext(snes, &appctx)); 31599566063dSJacob Faibussowitsch PetscCall(SNESSetApplicationContext(snes->npc, appctx)); 31609b5c1c08SStefano Zampini PetscCall(SNESSetUseMatrixFree(snes->npc, mf_operator, mf)); 31619566063dSJacob Faibussowitsch PetscCall(VecDestroy(&fpc)); 31626e2a1849SPeter Brune 31636e2a1849SPeter Brune /* copy the function pointers over */ 31649566063dSJacob Faibussowitsch PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)snes, (PetscObject)snes->npc)); 31656e2a1849SPeter Brune 31666e2a1849SPeter Brune /* default to 1 iteration */ 31679566063dSJacob Faibussowitsch PetscCall(SNESSetTolerances(snes->npc, 0.0, 0.0, 0.0, 1, snes->npc->max_funcs)); 3168efd4aadfSBarry Smith if (snes->npcside == PC_RIGHT) { 31699566063dSJacob Faibussowitsch PetscCall(SNESSetNormSchedule(snes->npc, SNES_NORM_FINAL_ONLY)); 3170a9936a0cSPeter Brune } else { 31719566063dSJacob Faibussowitsch PetscCall(SNESSetNormSchedule(snes->npc, SNES_NORM_NONE)); 3172a9936a0cSPeter Brune } 31739566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(snes->npc)); 31746e2a1849SPeter Brune 31756e2a1849SPeter Brune /* copy the line search context over */ 3176d8d34be6SBarry Smith if (snes->linesearch && snes->npc->linesearch) { 31779566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &linesearch)); 31789566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes->npc, &pclinesearch)); 31799566063dSJacob Faibussowitsch PetscCall(SNESLineSearchGetPreCheck(linesearch, &precheck, &lsprectx)); 31809566063dSJacob Faibussowitsch PetscCall(SNESLineSearchGetPostCheck(linesearch, &postcheck, &lspostctx)); 31819566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetPreCheck(pclinesearch, precheck, lsprectx)); 31829566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetPostCheck(pclinesearch, postcheck, lspostctx)); 31839566063dSJacob Faibussowitsch PetscCall(PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)pclinesearch)); 31846e2a1849SPeter Brune } 3185d8d34be6SBarry Smith } 31861baa6e33SBarry Smith if (snes->mf) PetscCall(SNESSetUpMatrixFree_Private(snes, snes->mf_operator, snes->mf_version)); 318748a46eb9SPierre Jolivet if (snes->ops->usercompute && !snes->user) PetscCall((*snes->ops->usercompute)(snes, (void **)&snes->user)); 31886e2a1849SPeter Brune 318937ec4e1aSPeter Brune snes->jac_iter = 0; 319037ec4e1aSPeter Brune snes->pre_iter = 0; 319137ec4e1aSPeter Brune 3192dbbe0bcdSBarry Smith PetscTryTypeMethod(snes, setup); 319358c9b817SLisandro Dalcin 31949566063dSJacob Faibussowitsch PetscCall(SNESSetDefaultComputeJacobian(snes)); 319558b371f3SBarry Smith 3196b552625fSStefano Zampini if (snes->npc && snes->npcside == PC_LEFT) { 31976c67d002SPeter Brune if (snes->functype == SNES_FUNCTION_PRECONDITIONED) { 3198d8d34be6SBarry Smith if (snes->linesearch) { 31999566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &linesearch)); 32009566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetFunction(linesearch, SNESComputeFunctionDefaultNPC)); 32016c67d002SPeter Brune } 32026c67d002SPeter Brune } 3203d8d34be6SBarry Smith } 3204fc8bc0e3SRichard Tran Mills PetscCall(PetscLogEventEnd(SNES_SetUp, snes, 0, 0, 0)); 32057aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 32063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32079b94acceSBarry Smith } 32089b94acceSBarry Smith 320937596af1SLisandro Dalcin /*@ 3210f6dfbefdSBarry Smith SNESReset - Resets a `SNES` context to the snessetupcalled = 0 state and removes any allocated `Vec`s and `Mat`s 321137596af1SLisandro Dalcin 3212c3339decSBarry Smith Collective 321337596af1SLisandro Dalcin 321437596af1SLisandro Dalcin Input Parameter: 3215f6dfbefdSBarry Smith . snes - iterative context obtained from `SNESCreate()` 321637596af1SLisandro Dalcin 3217d25893d9SBarry Smith Level: intermediate 3218d25893d9SBarry Smith 321995452b02SPatrick Sanan Notes: 3220f6dfbefdSBarry Smith Call this if you wish to reuse a `SNES` but with different size vectors 322137596af1SLisandro Dalcin 3222f6dfbefdSBarry Smith Also calls the application context destroy routine set with `SNESSetComputeApplicationContext()` 3223f6dfbefdSBarry Smith 3224dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESDestroy()`, `SNESCreate()`, `SNESSetUp()`, `SNESSolve()` 322537596af1SLisandro Dalcin @*/ 3226d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESReset(SNES snes) 3227d71ae5a4SJacob Faibussowitsch { 322837596af1SLisandro Dalcin PetscFunctionBegin; 322937596af1SLisandro Dalcin PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3230d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 32319566063dSJacob Faibussowitsch PetscCall((*snes->ops->userdestroy)((void **)&snes->user)); 32320298fd71SBarry Smith snes->user = NULL; 3233d25893d9SBarry Smith } 32341baa6e33SBarry Smith if (snes->npc) PetscCall(SNESReset(snes->npc)); 32358a23116dSBarry Smith 3236dbbe0bcdSBarry Smith PetscTryTypeMethod(snes, reset); 32371baa6e33SBarry Smith if (snes->ksp) PetscCall(KSPReset(snes->ksp)); 32389e764e56SPeter Brune 32391baa6e33SBarry Smith if (snes->linesearch) PetscCall(SNESLineSearchReset(snes->linesearch)); 32409e764e56SPeter Brune 32419566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_rhs)); 32429566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_sol)); 32439566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_sol_update)); 32449566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_func)); 32459566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->jacobian)); 32469566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->jacobian_pre)); 32479566063dSJacob Faibussowitsch PetscCall(MatDestroy(&snes->picard)); 32489566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(snes->nwork, &snes->work)); 32499566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(snes->nvwork, &snes->vwork)); 3250f5af7f23SKarl Rupp 325140fdac6aSLawrence Mitchell snes->alwayscomputesfinalresidual = PETSC_FALSE; 325240fdac6aSLawrence Mitchell 325337596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 325437596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 32553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 325637596af1SLisandro Dalcin } 325737596af1SLisandro Dalcin 325852baeb72SSatish Balay /*@ 3259f6dfbefdSBarry Smith SNESConvergedReasonViewCancel - Clears all the reason view functions for a `SNES` object. 3260c4421ceaSFande Kong 3261c3339decSBarry Smith Collective 3262c4421ceaSFande Kong 3263c4421ceaSFande Kong Input Parameter: 3264f6dfbefdSBarry Smith . snes - iterative context obtained from `SNESCreate()` 3265c4421ceaSFande Kong 3266c4421ceaSFande Kong Level: intermediate 3267c4421ceaSFande Kong 3268dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESCreate()`, `SNESDestroy()`, `SNESReset()` 3269c4421ceaSFande Kong @*/ 3270d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESConvergedReasonViewCancel(SNES snes) 3271d71ae5a4SJacob Faibussowitsch { 3272c4421ceaSFande Kong PetscInt i; 3273c4421ceaSFande Kong 3274c4421ceaSFande Kong PetscFunctionBegin; 3275c4421ceaSFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3276c4421ceaSFande Kong for (i = 0; i < snes->numberreasonviews; i++) { 327748a46eb9SPierre Jolivet if (snes->reasonviewdestroy[i]) PetscCall((*snes->reasonviewdestroy[i])(&snes->reasonviewcontext[i])); 3278c4421ceaSFande Kong } 3279c4421ceaSFande Kong snes->numberreasonviews = 0; 32803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3281c4421ceaSFande Kong } 3282c4421ceaSFande Kong 32831fb7b255SJunchao Zhang /*@C 32849b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 3285f6dfbefdSBarry Smith with `SNESCreate()`. 32869b94acceSBarry Smith 3287c3339decSBarry Smith Collective 3288c7afd0dbSLois Curfman McInnes 32899b94acceSBarry Smith Input Parameter: 3290f6dfbefdSBarry Smith . snes - the `SNES` context 32919b94acceSBarry Smith 329236851e7fSLois Curfman McInnes Level: beginner 329336851e7fSLois Curfman McInnes 3294dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESCreate()`, `SNESSolve()` 32959b94acceSBarry Smith @*/ 3296d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESDestroy(SNES *snes) 3297d71ae5a4SJacob Faibussowitsch { 32983a40ed3dSBarry Smith PetscFunctionBegin; 32993ba16761SJacob Faibussowitsch if (!*snes) PetscFunctionReturn(PETSC_SUCCESS); 33006bf464f9SBarry Smith PetscValidHeaderSpecific((*snes), SNES_CLASSID, 1); 33019371c9d4SSatish Balay if (--((PetscObject)(*snes))->refct > 0) { 33029371c9d4SSatish Balay *snes = NULL; 33033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 33049371c9d4SSatish Balay } 3305d4bb536fSBarry Smith 33069566063dSJacob Faibussowitsch PetscCall(SNESReset((*snes))); 33079566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&(*snes)->npc)); 33086b8b9a38SLisandro Dalcin 3309e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 33109566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*snes)); 3311dbbe0bcdSBarry Smith PetscTryTypeMethod((*snes), destroy); 33126d4c513bSLisandro Dalcin 33139566063dSJacob Faibussowitsch if ((*snes)->dm) PetscCall(DMCoarsenHookRemove((*snes)->dm, DMCoarsenHook_SNESVecSol, DMRestrictHook_SNESVecSol, *snes)); 33149566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*snes)->dm)); 33159566063dSJacob Faibussowitsch PetscCall(KSPDestroy(&(*snes)->ksp)); 33169566063dSJacob Faibussowitsch PetscCall(SNESLineSearchDestroy(&(*snes)->linesearch)); 33176b8b9a38SLisandro Dalcin 33189566063dSJacob Faibussowitsch PetscCall(PetscFree((*snes)->kspconvctx)); 331948a46eb9SPierre Jolivet if ((*snes)->ops->convergeddestroy) PetscCall((*(*snes)->ops->convergeddestroy)((*snes)->cnvP)); 332048a46eb9SPierre Jolivet if ((*snes)->conv_hist_alloc) PetscCall(PetscFree2((*snes)->conv_hist, (*snes)->conv_hist_its)); 33219566063dSJacob Faibussowitsch PetscCall(SNESMonitorCancel((*snes))); 33229566063dSJacob Faibussowitsch PetscCall(SNESConvergedReasonViewCancel((*snes))); 33239566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(snes)); 33243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 33259b94acceSBarry Smith } 33269b94acceSBarry Smith 33279b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 33289b94acceSBarry Smith 3329a8054027SBarry Smith /*@ 3330a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 3331a8054027SBarry Smith 3332c3339decSBarry Smith Logically Collective 3333a8054027SBarry Smith 3334a8054027SBarry Smith Input Parameters: 3335f6dfbefdSBarry Smith + snes - the `SNES` context 3336d8e291bfSBarry Smith - lag - 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time 33373b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 3338a8054027SBarry Smith 3339a8054027SBarry Smith Options Database Keys: 334079a4f3cfSmarkadams4 + -snes_lag_jacobian_persists <true,false> - sets the persistence through multiple SNES solves 33413d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 334279a4f3cfSmarkadams4 . -snes_lag_preconditioner_persists <true,false> - sets the persistence through multiple SNES solves 33433d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag 3344a8054027SBarry Smith 3345a8054027SBarry Smith Notes: 3346dc4c0fb0SBarry Smith Level: intermediate 3347dc4c0fb0SBarry Smith 3348a8054027SBarry Smith The default is 1 3349f6dfbefdSBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 or `SNESSetLagPreconditionerPersists()` was called 3350d8e291bfSBarry Smith 3351f6dfbefdSBarry Smith `SNESSetLagPreconditionerPersists()` allows using the same uniform lagging (for example every second linear solve) across multiple nonlinear solves. 3352a8054027SBarry Smith 3353dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetTrustRegionTolerance()`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESSetLagPreconditionerPersists()`, 3354f6dfbefdSBarry Smith `SNESSetLagJacobianPersists()`, `SNES`, `SNESSolve()` 3355a8054027SBarry Smith @*/ 3356d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetLagPreconditioner(SNES snes, PetscInt lag) 3357d71ae5a4SJacob Faibussowitsch { 3358a8054027SBarry Smith PetscFunctionBegin; 33590700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 33605f80ce2aSJacob Faibussowitsch PetscCheck(lag >= -2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Lag must be -2, -1, 1 or greater"); 33615f80ce2aSJacob Faibussowitsch PetscCheck(lag, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Lag cannot be 0"); 3362c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes, lag, 2); 3363a8054027SBarry Smith snes->lagpreconditioner = lag; 33643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3365a8054027SBarry Smith } 3366a8054027SBarry Smith 3367efd51863SBarry Smith /*@ 3368f6dfbefdSBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that `SNES` will do 3369efd51863SBarry Smith 3370c3339decSBarry Smith Logically Collective 3371efd51863SBarry Smith 3372efd51863SBarry Smith Input Parameters: 3373f6dfbefdSBarry Smith + snes - the `SNES` context 3374efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 3375efd51863SBarry Smith 3376f6dfbefdSBarry Smith Options Database Key: 337767b8a455SSatish Balay . -snes_grid_sequence <steps> - Use grid sequencing to generate initial guess 3378efd51863SBarry Smith 3379efd51863SBarry Smith Level: intermediate 3380efd51863SBarry Smith 3381f6dfbefdSBarry Smith Note: 3382f6dfbefdSBarry Smith Use `SNESGetSolution()` to extract the fine grid solution after grid sequencing. 3383c0df2a02SJed Brown 3384dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetTrustRegionTolerance()`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESGetGridSequence()` 3385efd51863SBarry Smith @*/ 3386d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetGridSequence(SNES snes, PetscInt steps) 3387d71ae5a4SJacob Faibussowitsch { 3388efd51863SBarry Smith PetscFunctionBegin; 3389efd51863SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3390efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes, steps, 2); 3391efd51863SBarry Smith snes->gridsequence = steps; 33923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3393efd51863SBarry Smith } 3394efd51863SBarry Smith 3395fa19ca70SBarry Smith /*@ 3396f6dfbefdSBarry Smith SNESGetGridSequence - gets the number of steps of grid sequencing that `SNES` will do 3397fa19ca70SBarry Smith 3398c3339decSBarry Smith Logically Collective 3399fa19ca70SBarry Smith 3400fa19ca70SBarry Smith Input Parameter: 3401f6dfbefdSBarry Smith . snes - the `SNES` context 3402fa19ca70SBarry Smith 3403fa19ca70SBarry Smith Output Parameter: 3404fa19ca70SBarry Smith . steps - the number of refinements to do, defaults to 0 3405fa19ca70SBarry Smith 3406f6dfbefdSBarry Smith Options Database Key: 340767b8a455SSatish Balay . -snes_grid_sequence <steps> - set number of refinements 3408fa19ca70SBarry Smith 3409fa19ca70SBarry Smith Level: intermediate 3410fa19ca70SBarry Smith 3411f6dfbefdSBarry Smith Note: 3412f6dfbefdSBarry Smith Use `SNESGetSolution()` to extract the fine grid solution after grid sequencing. 3413fa19ca70SBarry Smith 3414dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetTrustRegionTolerance()`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESSetGridSequence()` 3415fa19ca70SBarry Smith @*/ 3416d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetGridSequence(SNES snes, PetscInt *steps) 3417d71ae5a4SJacob Faibussowitsch { 3418fa19ca70SBarry Smith PetscFunctionBegin; 3419fa19ca70SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3420fa19ca70SBarry Smith *steps = snes->gridsequence; 34213ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3422fa19ca70SBarry Smith } 3423fa19ca70SBarry Smith 3424a8054027SBarry Smith /*@ 3425f6dfbefdSBarry Smith SNESGetLagPreconditioner - Return how often the preconditioner is rebuilt 3426a8054027SBarry Smith 34273f9fe445SBarry Smith Not Collective 3428a8054027SBarry Smith 3429a8054027SBarry Smith Input Parameter: 3430f6dfbefdSBarry Smith . snes - the `SNES` context 3431a8054027SBarry Smith 3432a8054027SBarry Smith Output Parameter: 3433a8054027SBarry 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 34343b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 3435a8054027SBarry Smith 3436a8054027SBarry Smith Options Database Keys: 343779a4f3cfSmarkadams4 + -snes_lag_jacobian_persists <true,false> - sets the persistence through multiple SNES solves 34383d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 343979a4f3cfSmarkadams4 . -snes_lag_preconditioner_persists <true,false> - sets the persistence through multiple SNES solves 34403d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag 3441a8054027SBarry Smith 3442dc4c0fb0SBarry Smith Level: intermediate 3443dc4c0fb0SBarry Smith 3444a8054027SBarry Smith Notes: 3445a8054027SBarry Smith The default is 1 3446f6dfbefdSBarry Smith 3447a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 3448a8054027SBarry Smith 3449dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetTrustRegionTolerance()`, `SNESSetLagPreconditioner()`, `SNESSetLagJacobianPersists()`, `SNESSetLagPreconditionerPersists()` 3450a8054027SBarry Smith @*/ 3451d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetLagPreconditioner(SNES snes, PetscInt *lag) 3452d71ae5a4SJacob Faibussowitsch { 3453a8054027SBarry Smith PetscFunctionBegin; 34540700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3455a8054027SBarry Smith *lag = snes->lagpreconditioner; 34563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3457a8054027SBarry Smith } 3458a8054027SBarry Smith 3459e35cf81dSBarry Smith /*@ 3460f6dfbefdSBarry Smith SNESSetLagJacobian - Set when the Jacobian is rebuilt in the nonlinear solve. See `SNESSetLagPreconditioner()` for determining how 3461e35cf81dSBarry Smith often the preconditioner is rebuilt. 3462e35cf81dSBarry Smith 3463c3339decSBarry Smith Logically Collective 3464e35cf81dSBarry Smith 3465e35cf81dSBarry Smith Input Parameters: 3466f6dfbefdSBarry Smith + snes - the `SNES` context 3467e35cf81dSBarry 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 3468fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 3469e35cf81dSBarry Smith 3470e35cf81dSBarry Smith Options Database Keys: 347179a4f3cfSmarkadams4 + -snes_lag_jacobian_persists <true,false> - sets the persistence through multiple SNES solves 34723d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 347379a4f3cfSmarkadams4 . -snes_lag_preconditioner_persists <true,false> - sets the persistence through multiple SNES solves 34743d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag. 3475e35cf81dSBarry Smith 3476dc4c0fb0SBarry Smith Level: intermediate 3477dc4c0fb0SBarry Smith 3478e35cf81dSBarry Smith Notes: 3479e35cf81dSBarry Smith The default is 1 3480f6dfbefdSBarry Smith 3481e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 3482f6dfbefdSBarry Smith 3483fe3ffe1eSBarry 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 3484fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 3485e35cf81dSBarry Smith 3486dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetTrustRegionTolerance()`, `SNESGetLagPreconditioner()`, `SNESSetLagPreconditioner()`, `SNESGetLagJacobianPersists()`, `SNESSetLagPreconditionerPersists()` 3487e35cf81dSBarry Smith @*/ 3488d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetLagJacobian(SNES snes, PetscInt lag) 3489d71ae5a4SJacob Faibussowitsch { 3490e35cf81dSBarry Smith PetscFunctionBegin; 34910700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 34925f80ce2aSJacob Faibussowitsch PetscCheck(lag >= -2, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Lag must be -2, -1, 1 or greater"); 34935f80ce2aSJacob Faibussowitsch PetscCheck(lag, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Lag cannot be 0"); 3494c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes, lag, 2); 3495e35cf81dSBarry Smith snes->lagjacobian = lag; 34963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3497e35cf81dSBarry Smith } 3498e35cf81dSBarry Smith 3499e35cf81dSBarry Smith /*@ 3500f6dfbefdSBarry Smith SNESGetLagJacobian - Get how often the Jacobian is rebuilt. See `SNESGetLagPreconditioner()` to determine when the preconditioner is rebuilt 3501e35cf81dSBarry Smith 35023f9fe445SBarry Smith Not Collective 3503e35cf81dSBarry Smith 3504e35cf81dSBarry Smith Input Parameter: 3505f6dfbefdSBarry Smith . snes - the `SNES` context 3506e35cf81dSBarry Smith 3507e35cf81dSBarry Smith Output Parameter: 3508e35cf81dSBarry 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 3509e35cf81dSBarry Smith the Jacobian is built etc. 3510e35cf81dSBarry Smith 3511dc4c0fb0SBarry Smith Level: intermediate 3512dc4c0fb0SBarry Smith 3513e35cf81dSBarry Smith Notes: 3514e35cf81dSBarry Smith The default is 1 3515f6dfbefdSBarry Smith 3516f6dfbefdSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 or `SNESSetLagJacobianPersists()` was called. 3517e35cf81dSBarry Smith 3518dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetTrustRegionTolerance()`, `SNESSetLagJacobian()`, `SNESSetLagPreconditioner()`, `SNESGetLagPreconditioner()`, `SNESSetLagJacobianPersists()`, `SNESSetLagPreconditionerPersists()` 3519e35cf81dSBarry Smith 3520e35cf81dSBarry Smith @*/ 3521d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetLagJacobian(SNES snes, PetscInt *lag) 3522d71ae5a4SJacob Faibussowitsch { 3523e35cf81dSBarry Smith PetscFunctionBegin; 35240700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3525e35cf81dSBarry Smith *lag = snes->lagjacobian; 35263ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3527e35cf81dSBarry Smith } 3528e35cf81dSBarry Smith 352937ec4e1aSPeter Brune /*@ 3530f6dfbefdSBarry Smith SNESSetLagJacobianPersists - Set whether or not the Jacobian lagging persists through multiple nonlinear solves 353137ec4e1aSPeter Brune 3532c3339decSBarry Smith Logically collective 353337ec4e1aSPeter Brune 3534d8d19677SJose E. Roman Input Parameters: 3535f6dfbefdSBarry Smith + snes - the `SNES` context 35369d7e2deaSPeter Brune - flg - jacobian lagging persists if true 353737ec4e1aSPeter Brune 353837ec4e1aSPeter Brune Options Database Keys: 353979a4f3cfSmarkadams4 + -snes_lag_jacobian_persists <true,false> - sets the persistence through multiple SNES solves 35403d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 354179a4f3cfSmarkadams4 . -snes_lag_preconditioner_persists <true,false> - sets the persistence through multiple SNES solves 35423d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag 35433d5a8a6aSBarry Smith 3544dc4c0fb0SBarry Smith Level: advanced 3545dc4c0fb0SBarry Smith 354695452b02SPatrick Sanan Notes: 3547f6dfbefdSBarry Smith Normally when `SNESSetLagJacobian()` is used, the Jacobian is always rebuilt at the beginning of each new nonlinear solve, this removes that. 3548f6dfbefdSBarry Smith 354995452b02SPatrick Sanan This is useful both for nonlinear preconditioning, where it's appropriate to have the Jacobian be stale by 355037ec4e1aSPeter Brune several solves, and for implicit time-stepping, where Jacobian lagging in the inner nonlinear solve over several 355137ec4e1aSPeter Brune timesteps may present huge efficiency gains. 355237ec4e1aSPeter Brune 3553dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES, `SNESSetLagPreconditionerPersists()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESGetNPC()`, `SNESSetLagJacobianPersists()` 355437ec4e1aSPeter Brune @*/ 3555d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetLagJacobianPersists(SNES snes, PetscBool flg) 3556d71ae5a4SJacob Faibussowitsch { 355737ec4e1aSPeter Brune PetscFunctionBegin; 355837ec4e1aSPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 355937ec4e1aSPeter Brune PetscValidLogicalCollectiveBool(snes, flg, 2); 356037ec4e1aSPeter Brune snes->lagjac_persist = flg; 35613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 356237ec4e1aSPeter Brune } 356337ec4e1aSPeter Brune 356437ec4e1aSPeter Brune /*@ 3565d8e291bfSBarry Smith SNESSetLagPreconditionerPersists - Set whether or not the preconditioner lagging persists through multiple nonlinear solves 356637ec4e1aSPeter Brune 3567c3339decSBarry Smith Logically Collective 356837ec4e1aSPeter Brune 3569d8d19677SJose E. Roman Input Parameters: 3570f6dfbefdSBarry Smith + snes - the `SNES` context 35719d7e2deaSPeter Brune - flg - preconditioner lagging persists if true 357237ec4e1aSPeter Brune 357337ec4e1aSPeter Brune Options Database Keys: 357479a4f3cfSmarkadams4 + -snes_lag_jacobian_persists <true,false> - sets the persistence through multiple SNES solves 35753d5a8a6aSBarry Smith . -snes_lag_jacobian <-2,1,2,...> - sets the lag 357679a4f3cfSmarkadams4 . -snes_lag_preconditioner_persists <true,false> - sets the persistence through multiple SNES solves 35773d5a8a6aSBarry Smith - -snes_lag_preconditioner <-2,1,2,...> - sets the lag 357837ec4e1aSPeter Brune 3579dc4c0fb0SBarry Smith Level: developer 3580dc4c0fb0SBarry Smith 358195452b02SPatrick Sanan Notes: 3582f6dfbefdSBarry Smith Normally when `SNESSetLagPreconditioner()` is used, the preconditioner is always rebuilt at the beginning of each new nonlinear solve, this removes that. 3583f6dfbefdSBarry Smith 358495452b02SPatrick Sanan This is useful both for nonlinear preconditioning, where it's appropriate to have the preconditioner be stale 358537ec4e1aSPeter Brune by several solves, and for implicit time-stepping, where preconditioner lagging in the inner nonlinear solve over 358637ec4e1aSPeter Brune several timesteps may present huge efficiency gains. 358737ec4e1aSPeter Brune 3588dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetLagJacobianPersists()`, `SNESSetLagJacobian()`, `SNESGetLagJacobian()`, `SNESGetNPC()`, `SNESSetLagPreconditioner()` 358937ec4e1aSPeter Brune @*/ 3590d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetLagPreconditionerPersists(SNES snes, PetscBool flg) 3591d71ae5a4SJacob Faibussowitsch { 359237ec4e1aSPeter Brune PetscFunctionBegin; 359337ec4e1aSPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 359437ec4e1aSPeter Brune PetscValidLogicalCollectiveBool(snes, flg, 2); 359537ec4e1aSPeter Brune snes->lagpre_persist = flg; 35963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 359737ec4e1aSPeter Brune } 359837ec4e1aSPeter Brune 35999b94acceSBarry Smith /*@ 3600f6dfbefdSBarry Smith SNESSetForceIteration - force `SNESSolve()` to take at least one iteration regardless of the initial residual norm 3601be5caee7SBarry Smith 3602c3339decSBarry Smith Logically Collective 3603be5caee7SBarry Smith 3604be5caee7SBarry Smith Input Parameters: 3605f6dfbefdSBarry Smith + snes - the `SNES` context 3606f6dfbefdSBarry Smith - force - `PETSC_TRUE` require at least one iteration 3607be5caee7SBarry Smith 3608f6dfbefdSBarry Smith Options Database Key: 3609be5caee7SBarry Smith . -snes_force_iteration <force> - Sets forcing an iteration 3610be5caee7SBarry Smith 3611dc4c0fb0SBarry Smith Level: intermediate 3612dc4c0fb0SBarry Smith 3613f6dfbefdSBarry Smith Note: 3614f6dfbefdSBarry Smith This is used sometimes with `TS` to prevent `TS` from detecting a false steady state solution 3615be5caee7SBarry Smith 3616dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `TS`, `SNESSetTrustRegionTolerance()`, `SNESSetDivergenceTolerance()` 3617be5caee7SBarry Smith @*/ 3618d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetForceIteration(SNES snes, PetscBool force) 3619d71ae5a4SJacob Faibussowitsch { 3620be5caee7SBarry Smith PetscFunctionBegin; 3621be5caee7SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3622be5caee7SBarry Smith snes->forceiteration = force; 36233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3624be5caee7SBarry Smith } 3625be5caee7SBarry Smith 362685216dc7SFande Kong /*@ 3627f6dfbefdSBarry Smith SNESGetForceIteration - Check whether or not `SNESSolve()` take at least one iteration regardless of the initial residual norm 362885216dc7SFande Kong 3629c3339decSBarry Smith Logically Collective 363085216dc7SFande Kong 36312fe279fdSBarry Smith Input Parameter: 3632f6dfbefdSBarry Smith . snes - the `SNES` context 363385216dc7SFande Kong 363485216dc7SFande Kong Output Parameter: 3635dc4c0fb0SBarry Smith . force - `PETSC_TRUE` requires at least one iteration. 363685216dc7SFande Kong 363706dd6b0eSSatish Balay Level: intermediate 363806dd6b0eSSatish Balay 3639dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetForceIteration()`, `SNESSetTrustRegionTolerance()`, `SNESSetDivergenceTolerance()` 364085216dc7SFande Kong @*/ 3641d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetForceIteration(SNES snes, PetscBool *force) 3642d71ae5a4SJacob Faibussowitsch { 364385216dc7SFande Kong PetscFunctionBegin; 364485216dc7SFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 364585216dc7SFande Kong *force = snes->forceiteration; 36463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 364785216dc7SFande Kong } 3648be5caee7SBarry Smith 3649be5caee7SBarry Smith /*@ 3650f6dfbefdSBarry Smith SNESSetTolerances - Sets `SNES` various parameters used in convergence tests. 36519b94acceSBarry Smith 3652c3339decSBarry Smith Logically Collective 3653c7afd0dbSLois Curfman McInnes 36549b94acceSBarry Smith Input Parameters: 3655f6dfbefdSBarry Smith + snes - the `SNES` context 365670441072SBarry Smith . abstol - absolute convergence tolerance 365733174efeSLois Curfman McInnes . rtol - relative convergence tolerance 36585358d0d4SBarry Smith . stol - convergence tolerance in terms of the norm of the change in the solution between steps, || delta x || < stol*|| x || 3659f6dfbefdSBarry Smith . maxit - maximum number of iterations, default 50. 3660f6dfbefdSBarry Smith - maxf - maximum number of function evaluations (-1 indicates no limit), default 1000 3661fee21e36SBarry Smith 366233174efeSLois Curfman McInnes Options Database Keys: 366370441072SBarry Smith + -snes_atol <abstol> - Sets abstol 3664c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 3665c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 3666c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 3667c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 36689b94acceSBarry Smith 366936851e7fSLois Curfman McInnes Level: intermediate 367036851e7fSLois Curfman McInnes 3671dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESolve()`, `SNES`, `SNESSetTrustRegionTolerance()`, `SNESSetDivergenceTolerance()`, `SNESSetForceIteration()` 36729b94acceSBarry Smith @*/ 3673d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetTolerances(SNES snes, PetscReal abstol, PetscReal rtol, PetscReal stol, PetscInt maxit, PetscInt maxf) 3674d71ae5a4SJacob Faibussowitsch { 36753a40ed3dSBarry Smith PetscFunctionBegin; 36760700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3677c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, abstol, 2); 3678c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, rtol, 3); 3679c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, stol, 4); 3680c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes, maxit, 5); 3681c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes, maxf, 6); 3682c5eb9154SBarry Smith 368313bcc0bdSJacob Faibussowitsch if (abstol != (PetscReal)PETSC_DEFAULT) { 36845f80ce2aSJacob Faibussowitsch PetscCheck(abstol >= 0.0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Absolute tolerance %g must be non-negative", (double)abstol); 3685ab54825eSJed Brown snes->abstol = abstol; 3686ab54825eSJed Brown } 368713bcc0bdSJacob Faibussowitsch if (rtol != (PetscReal)PETSC_DEFAULT) { 36885f80ce2aSJacob 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); 3689ab54825eSJed Brown snes->rtol = rtol; 3690ab54825eSJed Brown } 369113bcc0bdSJacob Faibussowitsch if (stol != (PetscReal)PETSC_DEFAULT) { 36925f80ce2aSJacob Faibussowitsch PetscCheck(stol >= 0.0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Step tolerance %g must be non-negative", (double)stol); 3693c60f73f4SPeter Brune snes->stol = stol; 3694ab54825eSJed Brown } 3695ab54825eSJed Brown if (maxit != PETSC_DEFAULT) { 369663a3b9bcSJacob Faibussowitsch PetscCheck(maxit >= 0, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of iterations %" PetscInt_FMT " must be non-negative", maxit); 3697ab54825eSJed Brown snes->max_its = maxit; 3698ab54825eSJed Brown } 3699ab54825eSJed Brown if (maxf != PETSC_DEFAULT) { 370063a3b9bcSJacob Faibussowitsch PetscCheck(maxf >= -1, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of function evaluations %" PetscInt_FMT " must be -1 or nonnegative", maxf); 3701ab54825eSJed Brown snes->max_funcs = maxf; 3702ab54825eSJed Brown } 370388976e71SPeter Brune snes->tolerancesset = PETSC_TRUE; 37043ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 37059b94acceSBarry Smith } 37069b94acceSBarry Smith 3707e4d06f11SPatrick Farrell /*@ 3708f6dfbefdSBarry Smith SNESSetDivergenceTolerance - Sets the divergence tolerance used for the `SNES` divergence test. 3709e4d06f11SPatrick Farrell 3710c3339decSBarry Smith Logically Collective 3711e4d06f11SPatrick Farrell 3712e4d06f11SPatrick Farrell Input Parameters: 3713f6dfbefdSBarry Smith + snes - the `SNES` context 3714f6dfbefdSBarry Smith - divtol - the divergence tolerance. Use -1 to deactivate the test, default is 1e4 3715e4d06f11SPatrick Farrell 3716f6dfbefdSBarry Smith Options Database Key: 3717dc4c0fb0SBarry Smith . -snes_divergence_tolerance <divtol> - Sets `divtol` 3718e4d06f11SPatrick Farrell 3719e4d06f11SPatrick Farrell Level: intermediate 3720e4d06f11SPatrick Farrell 3721dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESSetTolerances()`, `SNESGetDivergenceTolerance` 3722e4d06f11SPatrick Farrell @*/ 3723d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetDivergenceTolerance(SNES snes, PetscReal divtol) 3724d71ae5a4SJacob Faibussowitsch { 3725e4d06f11SPatrick Farrell PetscFunctionBegin; 3726e4d06f11SPatrick Farrell PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3727e4d06f11SPatrick Farrell PetscValidLogicalCollectiveReal(snes, divtol, 2); 3728e4d06f11SPatrick Farrell 372913bcc0bdSJacob Faibussowitsch if (divtol != (PetscReal)PETSC_DEFAULT) { 3730e4d06f11SPatrick Farrell snes->divtol = divtol; 37319371c9d4SSatish Balay } else { 3732e4d06f11SPatrick Farrell snes->divtol = 1.0e4; 3733e4d06f11SPatrick Farrell } 37343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3735e4d06f11SPatrick Farrell } 3736e4d06f11SPatrick Farrell 37379b94acceSBarry Smith /*@ 373833174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 373933174efeSLois Curfman McInnes 3740c7afd0dbSLois Curfman McInnes Not Collective 3741c7afd0dbSLois Curfman McInnes 374233174efeSLois Curfman McInnes Input Parameters: 3743f6dfbefdSBarry Smith + snes - the `SNES` context 374485385478SLisandro Dalcin . atol - absolute convergence tolerance 374533174efeSLois Curfman McInnes . rtol - relative convergence tolerance 374633174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 374733174efeSLois Curfman McInnes of the change in the solution between steps 374833174efeSLois Curfman McInnes . maxit - maximum number of iterations 3749c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 3750fee21e36SBarry Smith 375136851e7fSLois Curfman McInnes Level: intermediate 375236851e7fSLois Curfman McInnes 3753dc4c0fb0SBarry Smith Note: 3754dc4c0fb0SBarry Smith The user can specify `NULL` for any parameter that is not needed. 3755dc4c0fb0SBarry Smith 3756dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetTolerances()` 375733174efeSLois Curfman McInnes @*/ 3758d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetTolerances(SNES snes, PetscReal *atol, PetscReal *rtol, PetscReal *stol, PetscInt *maxit, PetscInt *maxf) 3759d71ae5a4SJacob Faibussowitsch { 37603a40ed3dSBarry Smith PetscFunctionBegin; 37610700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 376285385478SLisandro Dalcin if (atol) *atol = snes->abstol; 376333174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 3764c60f73f4SPeter Brune if (stol) *stol = snes->stol; 376533174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 376633174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 37673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 376833174efeSLois Curfman McInnes } 376933174efeSLois Curfman McInnes 3770e4d06f11SPatrick Farrell /*@ 3771e4d06f11SPatrick Farrell SNESGetDivergenceTolerance - Gets divergence tolerance used in divergence test. 3772e4d06f11SPatrick Farrell 3773e4d06f11SPatrick Farrell Not Collective 3774e4d06f11SPatrick Farrell 3775e4d06f11SPatrick Farrell Input Parameters: 3776f6dfbefdSBarry Smith + snes - the `SNES` context 3777e4d06f11SPatrick Farrell - divtol - divergence tolerance 3778e4d06f11SPatrick Farrell 3779e4d06f11SPatrick Farrell Level: intermediate 3780e4d06f11SPatrick Farrell 3781dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetDivergenceTolerance()` 3782e4d06f11SPatrick Farrell @*/ 3783d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetDivergenceTolerance(SNES snes, PetscReal *divtol) 3784d71ae5a4SJacob Faibussowitsch { 3785e4d06f11SPatrick Farrell PetscFunctionBegin; 3786e4d06f11SPatrick Farrell PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3787e4d06f11SPatrick Farrell if (divtol) *divtol = snes->divtol; 37883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3789e4d06f11SPatrick Farrell } 3790e4d06f11SPatrick Farrell 379133174efeSLois Curfman McInnes /*@ 37929b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 37939b94acceSBarry Smith 3794c3339decSBarry Smith Logically Collective 3795fee21e36SBarry Smith 3796c7afd0dbSLois Curfman McInnes Input Parameters: 3797f6dfbefdSBarry Smith + snes - the `SNES` context 3798c7afd0dbSLois Curfman McInnes - tol - tolerance 3799c7afd0dbSLois Curfman McInnes 38009b94acceSBarry Smith Options Database Key: 38014a221d59SStefano Zampini . -snes_tr_tol <tol> - Sets tol 38029b94acceSBarry Smith 380336851e7fSLois Curfman McInnes Level: intermediate 380436851e7fSLois Curfman McInnes 38054a221d59SStefano Zampini .seealso: [](chapter_snes), `SNES`, `SNESNEWTONTR`, `SNESSetTolerances()` 38069b94acceSBarry Smith @*/ 3807d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetTrustRegionTolerance(SNES snes, PetscReal tol) 3808d71ae5a4SJacob Faibussowitsch { 38093a40ed3dSBarry Smith PetscFunctionBegin; 38100700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 3811c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, tol, 2); 38129b94acceSBarry Smith snes->deltatol = tol; 38133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 38149b94acceSBarry Smith } 38159b94acceSBarry Smith 38166ba87a44SLisandro Dalcin PETSC_INTERN PetscErrorCode SNESMonitorRange_Private(SNES, PetscInt, PetscReal *); 38176ba87a44SLisandro Dalcin 3818d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESMonitorLGRange(SNES snes, PetscInt n, PetscReal rnorm, void *monctx) 3819d71ae5a4SJacob Faibussowitsch { 3820b271bb04SBarry Smith PetscDrawLG lg; 3821b271bb04SBarry Smith PetscReal x, y, per; 3822b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 3823b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 3824b271bb04SBarry Smith PetscDraw draw; 3825b271bb04SBarry Smith 3826459f5d12SBarry Smith PetscFunctionBegin; 38274d4332d5SBarry Smith PetscValidHeaderSpecific(v, PETSC_VIEWER_CLASSID, 4); 38289566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawLG(v, 0, &lg)); 38299566063dSJacob Faibussowitsch if (!n) PetscCall(PetscDrawLGReset(lg)); 38309566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(lg, &draw)); 38319566063dSJacob Faibussowitsch PetscCall(PetscDrawSetTitle(draw, "Residual norm")); 3832b271bb04SBarry Smith x = (PetscReal)n; 383377b4d14cSPeter Brune if (rnorm > 0.0) y = PetscLog10Real(rnorm); 383494c9c6d3SKarl Rupp else y = -15.0; 38359566063dSJacob Faibussowitsch PetscCall(PetscDrawLGAddPoint(lg, &x, &y)); 38366934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 38379566063dSJacob Faibussowitsch PetscCall(PetscDrawLGDraw(lg)); 38389566063dSJacob Faibussowitsch PetscCall(PetscDrawLGSave(lg)); 3839b271bb04SBarry Smith } 3840b271bb04SBarry Smith 38419566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawLG(v, 1, &lg)); 38429566063dSJacob Faibussowitsch if (!n) PetscCall(PetscDrawLGReset(lg)); 38439566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(lg, &draw)); 3844aaa8cc7dSPierre Jolivet PetscCall(PetscDrawSetTitle(draw, "% elements > .2*max element")); 38459566063dSJacob Faibussowitsch PetscCall(SNESMonitorRange_Private(snes, n, &per)); 3846b271bb04SBarry Smith x = (PetscReal)n; 3847b271bb04SBarry Smith y = 100.0 * per; 38489566063dSJacob Faibussowitsch PetscCall(PetscDrawLGAddPoint(lg, &x, &y)); 38496934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 38509566063dSJacob Faibussowitsch PetscCall(PetscDrawLGDraw(lg)); 38519566063dSJacob Faibussowitsch PetscCall(PetscDrawLGSave(lg)); 3852b271bb04SBarry Smith } 3853b271bb04SBarry Smith 38549566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawLG(v, 2, &lg)); 38559371c9d4SSatish Balay if (!n) { 38569371c9d4SSatish Balay prev = rnorm; 38579371c9d4SSatish Balay PetscCall(PetscDrawLGReset(lg)); 38589371c9d4SSatish Balay } 38599566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(lg, &draw)); 38609566063dSJacob Faibussowitsch PetscCall(PetscDrawSetTitle(draw, "(norm -oldnorm)/oldnorm")); 3861b271bb04SBarry Smith x = (PetscReal)n; 3862b271bb04SBarry Smith y = (prev - rnorm) / prev; 38639566063dSJacob Faibussowitsch PetscCall(PetscDrawLGAddPoint(lg, &x, &y)); 38646934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 38659566063dSJacob Faibussowitsch PetscCall(PetscDrawLGDraw(lg)); 38669566063dSJacob Faibussowitsch PetscCall(PetscDrawLGSave(lg)); 3867b271bb04SBarry Smith } 3868b271bb04SBarry Smith 38699566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawLG(v, 3, &lg)); 38709566063dSJacob Faibussowitsch if (!n) PetscCall(PetscDrawLGReset(lg)); 38719566063dSJacob Faibussowitsch PetscCall(PetscDrawLGGetDraw(lg, &draw)); 38729566063dSJacob Faibussowitsch PetscCall(PetscDrawSetTitle(draw, "(norm -oldnorm)/oldnorm*(% > .2 max)")); 3873b271bb04SBarry Smith x = (PetscReal)n; 3874b271bb04SBarry Smith y = (prev - rnorm) / (prev * per); 3875b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 38769566063dSJacob Faibussowitsch PetscCall(PetscDrawLGAddPoint(lg, &x, &y)); 3877b271bb04SBarry Smith } 38786934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 38799566063dSJacob Faibussowitsch PetscCall(PetscDrawLGDraw(lg)); 38809566063dSJacob Faibussowitsch PetscCall(PetscDrawLGSave(lg)); 3881b271bb04SBarry Smith } 3882b271bb04SBarry Smith prev = rnorm; 38833ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3884b271bb04SBarry Smith } 3885b271bb04SBarry Smith 3886228d79bcSJed Brown /*@ 38872d157150SStefano Zampini SNESConverged - Run the convergence test and update the `SNESConvergedReason`. 38882d157150SStefano Zampini 38892d157150SStefano Zampini Collective 38902d157150SStefano Zampini 38912d157150SStefano Zampini Input Parameters: 38922d157150SStefano Zampini + snes - the `SNES` context 38932d157150SStefano Zampini . it - current iteration 38942d157150SStefano Zampini . xnorm - 2-norm of current iterate 38952d157150SStefano Zampini . snorm - 2-norm of current step 38962d157150SStefano Zampini - fnorm - 2-norm of function 38972d157150SStefano Zampini 38982d157150SStefano Zampini Level: developer 38992d157150SStefano Zampini 39002d157150SStefano Zampini Note: 39012d157150SStefano Zampini This routine is called by the `SNES` implementations. 39022d157150SStefano Zampini It does not typically need to be called by the user. 39032d157150SStefano Zampini 39042d157150SStefano Zampini .seealso: [](chapter_snes), `SNES`, `SNESSolve`, `SNESSetConvergenceTest()`, `SNESGetConvergenceTest()` 39052d157150SStefano Zampini @*/ 39062d157150SStefano Zampini PetscErrorCode SNESConverged(SNES snes, PetscInt it, PetscReal xnorm, PetscReal snorm, PetscReal fnorm) 39072d157150SStefano Zampini { 39082d157150SStefano Zampini PetscFunctionBegin; 39092d157150SStefano Zampini if (!snes->reason) { 39102d157150SStefano Zampini if (snes->normschedule == SNES_NORM_ALWAYS) PetscUseTypeMethod(snes, converged, it, xnorm, snorm, fnorm, &snes->reason, snes->cnvP); 39112d157150SStefano Zampini if (it == snes->max_its && !snes->reason) { 39122d157150SStefano Zampini if (snes->normschedule == SNES_NORM_ALWAYS) { 39132d157150SStefano Zampini PetscCall(PetscInfo(snes, "Maximum number of iterations has been reached: %" PetscInt_FMT "\n", snes->max_its)); 39142d157150SStefano Zampini snes->reason = SNES_DIVERGED_MAX_IT; 39152d157150SStefano Zampini } else snes->reason = SNES_CONVERGED_ITS; 39162d157150SStefano Zampini } 39172d157150SStefano Zampini } 39182d157150SStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 39192d157150SStefano Zampini } 39202d157150SStefano Zampini 39212d157150SStefano Zampini /*@ 3922228d79bcSJed Brown SNESMonitor - runs the user provided monitor routines, if they exist 3923228d79bcSJed Brown 3924c3339decSBarry Smith Collective 3925228d79bcSJed Brown 3926228d79bcSJed Brown Input Parameters: 3927f6dfbefdSBarry Smith + snes - nonlinear solver context obtained from `SNESCreate()` 3928228d79bcSJed Brown . iter - iteration number 3929228d79bcSJed Brown - rnorm - relative norm of the residual 3930228d79bcSJed Brown 3931dc4c0fb0SBarry Smith Level: developer 3932dc4c0fb0SBarry Smith 3933f6dfbefdSBarry Smith Note: 3934f6dfbefdSBarry Smith This routine is called by the `SNES` implementations. 3935228d79bcSJed Brown It does not typically need to be called by the user. 3936228d79bcSJed Brown 3937dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESMonitorSet()` 3938228d79bcSJed Brown @*/ 3939d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESMonitor(SNES snes, PetscInt iter, PetscReal rnorm) 3940d71ae5a4SJacob Faibussowitsch { 39417a03ce2fSLisandro Dalcin PetscInt i, n = snes->numbermonitors; 39427a03ce2fSLisandro Dalcin 39437a03ce2fSLisandro Dalcin PetscFunctionBegin; 39449566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(snes->vec_sol)); 394548a46eb9SPierre Jolivet for (i = 0; i < n; i++) PetscCall((*snes->monitor[i])(snes, iter, rnorm, snes->monitorcontext[i])); 39469566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(snes->vec_sol)); 39473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 39487a03ce2fSLisandro Dalcin } 39497a03ce2fSLisandro Dalcin 39509b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 39519b94acceSBarry Smith 3952bf388a1fSBarry Smith /*MC 3953f6dfbefdSBarry Smith SNESMonitorFunction - functional form passed to `SNESMonitorSet()` to monitor convergence of nonlinear solver 3954bf388a1fSBarry Smith 3955bf388a1fSBarry Smith Synopsis: 3956aaa7dc30SBarry Smith #include <petscsnes.h> 395737fdd005SBarry Smith PetscErrorCode SNESMonitorFunction(SNES snes, PetscInt its, PetscReal norm, void *mctx) 3958bf388a1fSBarry Smith 3959c3339decSBarry Smith Collective 39601843f636SBarry Smith 39611843f636SBarry Smith Input Parameters: 3962f6dfbefdSBarry Smith + snes - the `SNES` context 3963bf388a1fSBarry Smith . its - iteration number 3964bf388a1fSBarry Smith . norm - 2-norm function value (may be estimated) 3965bf388a1fSBarry Smith - mctx - [optional] monitoring context 3966bf388a1fSBarry Smith 3967878cb397SSatish Balay Level: advanced 3968878cb397SSatish Balay 3969dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESMonitorSet()`, `SNESMonitorSet()`, `SNESMonitorGet()` 3970bf388a1fSBarry Smith M*/ 3971bf388a1fSBarry Smith 39729b94acceSBarry Smith /*@C 3973a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 39749b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 39759b94acceSBarry Smith progress. 39769b94acceSBarry Smith 3977c3339decSBarry Smith Logically Collective 3978fee21e36SBarry Smith 3979c7afd0dbSLois Curfman McInnes Input Parameters: 3980f6dfbefdSBarry Smith + snes - the `SNES` context 398120f4b53cSBarry Smith . f - the monitor function, for the calling sequence see `SNESMonitorFunction` 3982b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 3983dc4c0fb0SBarry Smith monitor routine (use `NULL` if no context is desired) 3984dc4c0fb0SBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`) 39859b94acceSBarry Smith 39869665c990SLois Curfman McInnes Options Database Keys: 3987f6dfbefdSBarry Smith + -snes_monitor - sets `SNESMonitorDefault()` 3988798534f6SMatthew G. Knepley . -snes_monitor draw::draw_lg - sets line graph monitor, 3989dc4c0fb0SBarry Smith - -snes_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to `SNESMonitorSet()`, but does not cancel those set via 3990c7afd0dbSLois Curfman McInnes the options database. 39919665c990SLois Curfman McInnes 3992dc4c0fb0SBarry Smith Level: intermediate 3993dc4c0fb0SBarry Smith 3994f6dfbefdSBarry Smith Note: 39956bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 3996f6dfbefdSBarry Smith `SNESMonitorSet()` multiple times; all will be called in the 39976bc08f3fSLois Curfman McInnes order in which they were set. 3998639f9d9dSBarry Smith 3999f6dfbefdSBarry Smith Fortran Note: 4000f6dfbefdSBarry Smith Only a single monitor function can be set for each `SNES` object 4001025f1a04SBarry Smith 4002dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESMonitorDefault()`, `SNESMonitorCancel()`, `SNESMonitorFunction` 40039b94acceSBarry Smith @*/ 4004d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESMonitorSet(SNES snes, PetscErrorCode (*f)(SNES, PetscInt, PetscReal, void *), void *mctx, PetscErrorCode (*monitordestroy)(void **)) 4005d71ae5a4SJacob Faibussowitsch { 4006b90d0a6eSBarry Smith PetscInt i; 400778064530SBarry Smith PetscBool identical; 4008b90d0a6eSBarry Smith 40093a40ed3dSBarry Smith PetscFunctionBegin; 40100700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4011b90d0a6eSBarry Smith for (i = 0; i < snes->numbermonitors; i++) { 40129566063dSJacob Faibussowitsch PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, mctx, monitordestroy, (PetscErrorCode(*)(void))snes->monitor[i], snes->monitorcontext[i], snes->monitordestroy[i], &identical)); 40133ba16761SJacob Faibussowitsch if (identical) PetscFunctionReturn(PETSC_SUCCESS); 4014649052a6SBarry Smith } 40155f80ce2aSJacob Faibussowitsch PetscCheck(snes->numbermonitors < MAXSNESMONITORS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set"); 40166e4dcb14SBarry Smith snes->monitor[snes->numbermonitors] = f; 4017b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 4018639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void *)mctx; 40193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 40209b94acceSBarry Smith } 40219b94acceSBarry Smith 4022a278d85bSSatish Balay /*@ 4023f6dfbefdSBarry Smith SNESMonitorCancel - Clears all the monitor functions for a `SNES` object. 40245cd90555SBarry Smith 4025c3339decSBarry Smith Logically Collective 4026c7afd0dbSLois Curfman McInnes 40272fe279fdSBarry Smith Input Parameter: 4028f6dfbefdSBarry Smith . snes - the `SNES` context 40295cd90555SBarry Smith 40301a480d89SAdministrator Options Database Key: 4031a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 4032dc4c0fb0SBarry Smith into a code by calls to `SNESMonitorSet()`, but does not cancel those 4033c7afd0dbSLois Curfman McInnes set via the options database 40345cd90555SBarry Smith 4035dc4c0fb0SBarry Smith Level: intermediate 4036dc4c0fb0SBarry Smith 4037f6dfbefdSBarry Smith Note: 4038f6dfbefdSBarry Smith There is no way to clear one specific monitor from a `SNES` object. 40395cd90555SBarry Smith 4040dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESMonitorGet()`, `SNESMonitorDefault()`, `SNESMonitorSet()` 40415cd90555SBarry Smith @*/ 4042d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESMonitorCancel(SNES snes) 4043d71ae5a4SJacob Faibussowitsch { 4044d952e501SBarry Smith PetscInt i; 4045d952e501SBarry Smith 40465cd90555SBarry Smith PetscFunctionBegin; 40470700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4048d952e501SBarry Smith for (i = 0; i < snes->numbermonitors; i++) { 404948a46eb9SPierre Jolivet if (snes->monitordestroy[i]) PetscCall((*snes->monitordestroy[i])(&snes->monitorcontext[i])); 4050d952e501SBarry Smith } 40515cd90555SBarry Smith snes->numbermonitors = 0; 40523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 40535cd90555SBarry Smith } 40545cd90555SBarry Smith 4055bf388a1fSBarry Smith /*MC 4056bf388a1fSBarry Smith SNESConvergenceTestFunction - functional form used for testing of convergence of nonlinear solver 4057bf388a1fSBarry Smith 4058bf388a1fSBarry Smith Synopsis: 4059aaa7dc30SBarry Smith #include <petscsnes.h> 406037fdd005SBarry Smith PetscErrorCode SNESConvergenceTest(SNES snes, PetscInt it, PetscReal xnorm, PetscReal gnorm, PetscReal f, SNESConvergedReason *reason, void *cctx) 4061bf388a1fSBarry Smith 4062c3339decSBarry Smith Collective 40631843f636SBarry Smith 40641843f636SBarry Smith Input Parameters: 4065f6dfbefdSBarry Smith + snes - the `SNES` context 4066bf388a1fSBarry Smith . it - current iteration (0 is the first and is before any Newton step) 4067bf388a1fSBarry Smith . xnorm - 2-norm of current iterate 4068bf388a1fSBarry Smith . gnorm - 2-norm of current step 40691843f636SBarry Smith . f - 2-norm of function 40701843f636SBarry Smith - cctx - [optional] convergence context 40711843f636SBarry Smith 40721843f636SBarry Smith Output Parameter: 40731843f636SBarry Smith . reason - reason for convergence/divergence, only needs to be set when convergence or divergence is detected 4074bf388a1fSBarry Smith 4075878cb397SSatish Balay Level: intermediate 4076bf388a1fSBarry Smith 4077dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve`, `SNESSetConvergenceTest()`, `SNESGetConvergenceTest()` 4078bf388a1fSBarry Smith M*/ 4079bf388a1fSBarry Smith 40809b94acceSBarry Smith /*@C 40819b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 40829b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 40839b94acceSBarry Smith 4084c3339decSBarry Smith Logically Collective 4085fee21e36SBarry Smith 4086c7afd0dbSLois Curfman McInnes Input Parameters: 4087f6dfbefdSBarry Smith + snes - the `SNES` context 4088f6dfbefdSBarry Smith . `SNESConvergenceTestFunction` - routine to test for convergence 4089dc4c0fb0SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be `NULL`) 4090dc4c0fb0SBarry Smith - destroy - [optional] destructor for the context (may be `NULL`; `PETSC_NULL_FUNCTION` in Fortran) 40919b94acceSBarry Smith 409236851e7fSLois Curfman McInnes Level: advanced 409336851e7fSLois Curfman McInnes 4094dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESConvergedDefault()`, `SNESConvergedSkip()`, `SNESConvergenceTestFunction` 40959b94acceSBarry Smith @*/ 4096d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetConvergenceTest(SNES snes, PetscErrorCode (*SNESConvergenceTestFunction)(SNES, PetscInt, PetscReal, PetscReal, PetscReal, SNESConvergedReason *, void *), void *cctx, PetscErrorCode (*destroy)(void *)) 4097d71ae5a4SJacob Faibussowitsch { 40983a40ed3dSBarry Smith PetscFunctionBegin; 40990700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4100e2a6519dSDmitry Karpeev if (!SNESConvergenceTestFunction) SNESConvergenceTestFunction = SNESConvergedSkip; 41011baa6e33SBarry Smith if (snes->ops->convergeddestroy) PetscCall((*snes->ops->convergeddestroy)(snes->cnvP)); 4102bf388a1fSBarry Smith snes->ops->converged = SNESConvergenceTestFunction; 41037f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 410485385478SLisandro Dalcin snes->cnvP = cctx; 41053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 41069b94acceSBarry Smith } 41079b94acceSBarry Smith 410852baeb72SSatish Balay /*@ 4109f6dfbefdSBarry Smith SNESGetConvergedReason - Gets the reason the `SNES` iteration was stopped. 4110184914b5SBarry Smith 4111184914b5SBarry Smith Not Collective 4112184914b5SBarry Smith 4113184914b5SBarry Smith Input Parameter: 4114f6dfbefdSBarry Smith . snes - the `SNES` context 4115184914b5SBarry Smith 4116184914b5SBarry Smith Output Parameter: 4117f6dfbefdSBarry Smith . reason - negative value indicates diverged, positive value converged, see `SNESConvergedReason` for the individual convergence tests for complete lists 4118184914b5SBarry Smith 4119f6dfbefdSBarry Smith Options Database Key: 41206a4d7782SBarry Smith . -snes_converged_reason - prints the reason to standard out 41216a4d7782SBarry Smith 4122184914b5SBarry Smith Level: intermediate 4123184914b5SBarry Smith 4124f6dfbefdSBarry Smith Note: 4125f6dfbefdSBarry Smith Should only be called after the call the `SNESSolve()` is complete, if it is called earlier it returns the value `SNES__CONVERGED_ITERATING`. 4126184914b5SBarry Smith 4127dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSolve()`, `SNESSetConvergenceTest()`, `SNESSetConvergedReason()`, `SNESConvergedReason`, `SNESGetConvergedReasonString()` 4128184914b5SBarry Smith @*/ 4129d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetConvergedReason(SNES snes, SNESConvergedReason *reason) 4130d71ae5a4SJacob Faibussowitsch { 4131184914b5SBarry Smith PetscFunctionBegin; 41320700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 41334482741eSBarry Smith PetscValidPointer(reason, 2); 4134184914b5SBarry Smith *reason = snes->reason; 41353ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4136184914b5SBarry Smith } 4137184914b5SBarry Smith 4138c4421ceaSFande Kong /*@C 4139f6dfbefdSBarry Smith SNESGetConvergedReasonString - Return a human readable string for `SNESConvergedReason` 4140c4421ceaSFande Kong 4141c4421ceaSFande Kong Not Collective 4142c4421ceaSFande Kong 4143c4421ceaSFande Kong Input Parameter: 4144f6dfbefdSBarry Smith . snes - the `SNES` context 4145c4421ceaSFande Kong 4146c4421ceaSFande Kong Output Parameter: 4147dc4c0fb0SBarry Smith . strreason - a human readable string that describes `SNES` converged reason 4148c4421ceaSFande Kong 414999c90e12SSatish Balay Level: beginner 4150c4421ceaSFande Kong 4151dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetConvergedReason()` 4152c4421ceaSFande Kong @*/ 4153d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetConvergedReasonString(SNES snes, const char **strreason) 4154d71ae5a4SJacob Faibussowitsch { 4155c4421ceaSFande Kong PetscFunctionBegin; 4156c4421ceaSFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4157dadcf809SJacob Faibussowitsch PetscValidPointer(strreason, 2); 4158c4421ceaSFande Kong *strreason = SNESConvergedReasons[snes->reason]; 41593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4160c4421ceaSFande Kong } 4161c4421ceaSFande Kong 416233866048SMatthew G. Knepley /*@ 4163f6dfbefdSBarry Smith SNESSetConvergedReason - Sets the reason the `SNES` iteration was stopped. 416433866048SMatthew G. Knepley 416533866048SMatthew G. Knepley Not Collective 416633866048SMatthew G. Knepley 416733866048SMatthew G. Knepley Input Parameters: 4168f6dfbefdSBarry Smith + snes - the `SNES` context 4169f6dfbefdSBarry Smith - reason - negative value indicates diverged, positive value converged, see `SNESConvergedReason` or the 417033866048SMatthew G. Knepley manual pages for the individual convergence tests for complete lists 417133866048SMatthew G. Knepley 4172f6dfbefdSBarry Smith Level: developer 4173f6dfbefdSBarry Smith 4174f6dfbefdSBarry Smith Developer Note: 4175f6dfbefdSBarry Smith Called inside the various `SNESSolve()` implementations 417633866048SMatthew G. Knepley 4177dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESGetConvergedReason()`, `SNESSetConvergenceTest()`, `SNESConvergedReason` 417833866048SMatthew G. Knepley @*/ 4179d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetConvergedReason(SNES snes, SNESConvergedReason reason) 4180d71ae5a4SJacob Faibussowitsch { 418133866048SMatthew G. Knepley PetscFunctionBegin; 418233866048SMatthew G. Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 418333866048SMatthew G. Knepley snes->reason = reason; 41843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 418533866048SMatthew G. Knepley } 418633866048SMatthew G. Knepley 4187c9005455SLois Curfman McInnes /*@ 4188c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 4189c9005455SLois Curfman McInnes 4190c3339decSBarry Smith Logically Collective 4191fee21e36SBarry Smith 4192c7afd0dbSLois Curfman McInnes Input Parameters: 4193f6dfbefdSBarry Smith + snes - iterative context obtained from `SNESCreate()` 41948c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 4195cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 4196758f92a0SBarry Smith . na - size of a and its 4197f6dfbefdSBarry Smith - reset - `PETSC_TRUE` indicates each new nonlinear solve resets the history counter to zero, 4198758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 4199c7afd0dbSLois Curfman McInnes 4200dc4c0fb0SBarry Smith Level: intermediate 4201dc4c0fb0SBarry Smith 4202308dcc3eSBarry Smith Notes: 4203dc4c0fb0SBarry Smith If 'a' and 'its' are `NULL` then space is allocated for the history. If 'na' `PETSC_DECIDE` or `PETSC_DEFAULT` then a 4204308dcc3eSBarry Smith default array of length 10000 is allocated. 4205308dcc3eSBarry Smith 4206c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 4207c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 4208c9005455SLois Curfman McInnes during the section of code that is being timed. 4209c9005455SLois Curfman McInnes 4210dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESGetConvergenceHistory()` 4211c9005455SLois Curfman McInnes @*/ 4212d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetConvergenceHistory(SNES snes, PetscReal a[], PetscInt its[], PetscInt na, PetscBool reset) 4213d71ae5a4SJacob Faibussowitsch { 42143a40ed3dSBarry Smith PetscFunctionBegin; 42150700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4216064a246eSJacob Faibussowitsch if (a) PetscValidRealPointer(a, 2); 4217a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its, 3); 42187a1ec6d4SBarry Smith if (!a) { 4219308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 42209566063dSJacob Faibussowitsch PetscCall(PetscCalloc2(na, &a, na, &its)); 4221071fcb05SBarry Smith snes->conv_hist_alloc = PETSC_TRUE; 4222308dcc3eSBarry Smith } 4223c9005455SLois Curfman McInnes snes->conv_hist = a; 4224758f92a0SBarry Smith snes->conv_hist_its = its; 4225115dd874SBarry Smith snes->conv_hist_max = (size_t)na; 4226a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 4227758f92a0SBarry Smith snes->conv_hist_reset = reset; 42283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4229758f92a0SBarry Smith } 4230758f92a0SBarry Smith 4231d1e78c4fSBarry Smith #if defined(PETSC_HAVE_MATLAB) 4232c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 4233c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 423499e0435eSBarry Smith 4235d71ae5a4SJacob Faibussowitsch PETSC_EXTERN mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 4236d71ae5a4SJacob Faibussowitsch { 4237308dcc3eSBarry Smith mxArray *mat; 4238308dcc3eSBarry Smith PetscInt i; 4239308dcc3eSBarry Smith PetscReal *ar; 4240308dcc3eSBarry Smith 4241308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len, 1, mxREAL); 4242308dcc3eSBarry Smith ar = (PetscReal *)mxGetData(mat); 4243f5af7f23SKarl Rupp for (i = 0; i < snes->conv_hist_len; i++) ar[i] = snes->conv_hist[i]; 424411cc89d2SBarry Smith return mat; 4245308dcc3eSBarry Smith } 4246308dcc3eSBarry Smith #endif 4247308dcc3eSBarry Smith 42480c4c9dddSBarry Smith /*@C 4249758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 4250758f92a0SBarry Smith 42513f9fe445SBarry Smith Not Collective 4252758f92a0SBarry Smith 4253758f92a0SBarry Smith Input Parameter: 4254f6dfbefdSBarry Smith . snes - iterative context obtained from `SNESCreate()` 4255758f92a0SBarry Smith 4256758f92a0SBarry Smith Output Parameters: 4257f6dfbefdSBarry Smith + a - array to hold history, usually was set with `SNESSetConvergenceHistory()` 4258758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 4259758f92a0SBarry Smith negative if not converged) for each solve. 426020f4b53cSBarry Smith - na - size of `a` and `its` 4261758f92a0SBarry Smith 4262dc4c0fb0SBarry Smith Level: intermediate 4263dc4c0fb0SBarry Smith 426420f4b53cSBarry Smith Note: 426520f4b53cSBarry Smith This routine is useful, e.g., when running a code for purposes 426620f4b53cSBarry Smith of accurate performance monitoring, when no I/O should be done 426720f4b53cSBarry Smith during the section of code that is being timed. 426820f4b53cSBarry Smith 426920f4b53cSBarry Smith Fortran Note: 4270758f92a0SBarry Smith The calling sequence for this routine in Fortran is 4271dc4c0fb0SBarry Smith .vb 4272dc4c0fb0SBarry Smith call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 4273dc4c0fb0SBarry Smith .ve 4274758f92a0SBarry Smith 4275dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESSetConvergenceHistory()` 4276758f92a0SBarry Smith @*/ 4277d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetConvergenceHistory(SNES snes, PetscReal *a[], PetscInt *its[], PetscInt *na) 4278d71ae5a4SJacob Faibussowitsch { 4279758f92a0SBarry Smith PetscFunctionBegin; 42800700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4281758f92a0SBarry Smith if (a) *a = snes->conv_hist; 4282758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 4283115dd874SBarry Smith if (na) *na = (PetscInt)snes->conv_hist_len; 42843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4285c9005455SLois Curfman McInnes } 4286c9005455SLois Curfman McInnes 4287ac226902SBarry Smith /*@C 428876b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 4289eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 42907e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 429176b2cf59SMatthew Knepley 4292c3339decSBarry Smith Logically Collective 429376b2cf59SMatthew Knepley 429476b2cf59SMatthew Knepley Input Parameters: 4295a2b725a8SWilliam Gropp + snes - The nonlinear solver context 4296a2b725a8SWilliam Gropp - func - The function 429776b2cf59SMatthew Knepley 429820f4b53cSBarry Smith Calling sequence of `func`: 429920f4b53cSBarry Smith $ PetscErrorCode func(SNES snes, PetscInt step); 430020f4b53cSBarry Smith + snes - the nonlinear solver context 430120f4b53cSBarry Smith - step - The current step of the iteration 430276b2cf59SMatthew Knepley 4303fe97e370SBarry Smith Level: advanced 4304fe97e370SBarry Smith 43056b7fb656SBarry Smith Note: 4306f6dfbefdSBarry 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 function provided 4307f6dfbefdSBarry Smith to `SNESSetFunction()`, or `SNESSetPicard()` 4308fe97e370SBarry Smith This is not used by most users. 430976b2cf59SMatthew Knepley 4310aaa8cc7dSPierre Jolivet There are a variety of function hooks one many set that are called at different stages of the nonlinear solution process, see the functions listed below. 43116b7fb656SBarry Smith 4312dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESSetJacobian()`, `SNESSolve()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchSetPostCheck()`, `SNESNewtonTRSetPreCheck()`, `SNESNewtonTRSetPostCheck()`, 4313db781477SPatrick Sanan `SNESMonitorSet()`, `SNESSetDivergenceTest()` 431476b2cf59SMatthew Knepley @*/ 4315d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 4316d71ae5a4SJacob Faibussowitsch { 431776b2cf59SMatthew Knepley PetscFunctionBegin; 43180700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4319e7788613SBarry Smith snes->ops->update = func; 43203ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 432176b2cf59SMatthew Knepley } 432276b2cf59SMatthew Knepley 432391f3e32bSBarry Smith /*@C 4324f6dfbefdSBarry Smith SNESConvergedReasonView - Displays the reason a `SNES` solve converged or diverged to a viewer 43252a359c20SBarry Smith 4326c3339decSBarry Smith Collective 43272a359c20SBarry Smith 43282a359c20SBarry Smith Parameter: 4329f6dfbefdSBarry Smith + snes - iterative context obtained from `SNESCreate()` 43302a359c20SBarry Smith - viewer - the viewer to display the reason 43312a359c20SBarry Smith 43322a359c20SBarry Smith Options Database Keys: 4333ee300463SSatish Balay + -snes_converged_reason - print reason for converged or diverged, also prints number of iterations 4334ee300463SSatish Balay - -snes_converged_reason ::failed - only print reason and number of iterations when diverged 4335eafd5ff0SAlex Lindsay 4336f6dfbefdSBarry Smith Note: 4337f6dfbefdSBarry Smith To change the format of the output call `PetscViewerPushFormat`(viewer,format) before this call. Use `PETSC_VIEWER_DEFAULT` for the default, 4338f6dfbefdSBarry Smith use `PETSC_VIEWER_FAILED` to only display a reason if it fails. 43392a359c20SBarry Smith 43402a359c20SBarry Smith Level: beginner 43412a359c20SBarry Smith 4342dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESConvergedReason`, `PetscViewer`, `SNES`, 4343f6dfbefdSBarry Smith `SNESCreate()`, `SNESSetUp()`, `SNESDestroy()`, `SNESSetTolerances()`, `SNESConvergedDefault()`, `SNESGetConvergedReason()`, 4344f6dfbefdSBarry Smith `SNESConvergedReasonViewFromOptions()`, 4345db781477SPatrick Sanan `PetscViewerPushFormat()`, `PetscViewerPopFormat()` 43462a359c20SBarry Smith @*/ 4347d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESConvergedReasonView(SNES snes, PetscViewer viewer) 4348d71ae5a4SJacob Faibussowitsch { 434975cca76cSMatthew G. Knepley PetscViewerFormat format; 43502a359c20SBarry Smith PetscBool isAscii; 43512a359c20SBarry Smith 43522a359c20SBarry Smith PetscFunctionBegin; 435319a666eeSBarry Smith if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)); 43549566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isAscii)); 43552a359c20SBarry Smith if (isAscii) { 43569566063dSJacob Faibussowitsch PetscCall(PetscViewerGetFormat(viewer, &format)); 43579566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIAddTab(viewer, ((PetscObject)snes)->tablevel)); 435875cca76cSMatthew G. Knepley if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 435975cca76cSMatthew G. Knepley DM dm; 436075cca76cSMatthew G. Knepley Vec u; 436175cca76cSMatthew G. Knepley PetscDS prob; 436275cca76cSMatthew G. Knepley PetscInt Nf, f; 436395cbbfd3SMatthew G. Knepley PetscErrorCode (**exactSol)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *); 436495cbbfd3SMatthew G. Knepley void **exactCtx; 436575cca76cSMatthew G. Knepley PetscReal error; 436675cca76cSMatthew G. Knepley 43679566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 43689566063dSJacob Faibussowitsch PetscCall(SNESGetSolution(snes, &u)); 43699566063dSJacob Faibussowitsch PetscCall(DMGetDS(dm, &prob)); 43709566063dSJacob Faibussowitsch PetscCall(PetscDSGetNumFields(prob, &Nf)); 43719566063dSJacob Faibussowitsch PetscCall(PetscMalloc2(Nf, &exactSol, Nf, &exactCtx)); 43729566063dSJacob Faibussowitsch for (f = 0; f < Nf; ++f) PetscCall(PetscDSGetExactSolution(prob, f, &exactSol[f], &exactCtx[f])); 43739566063dSJacob Faibussowitsch PetscCall(DMComputeL2Diff(dm, 0.0, exactSol, exactCtx, u, &error)); 43749566063dSJacob Faibussowitsch PetscCall(PetscFree2(exactSol, exactCtx)); 43759566063dSJacob Faibussowitsch if (error < 1.0e-11) PetscCall(PetscViewerASCIIPrintf(viewer, "L_2 Error: < 1.0e-11\n")); 437663a3b9bcSJacob Faibussowitsch else PetscCall(PetscViewerASCIIPrintf(viewer, "L_2 Error: %g\n", (double)error)); 437775cca76cSMatthew G. Knepley } 4378eafd5ff0SAlex Lindsay if (snes->reason > 0 && format != PETSC_VIEWER_FAILED) { 43792a359c20SBarry Smith if (((PetscObject)snes)->prefix) { 438063a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Nonlinear %s solve converged due to %s iterations %" PetscInt_FMT "\n", ((PetscObject)snes)->prefix, SNESConvergedReasons[snes->reason], snes->iter)); 43812a359c20SBarry Smith } else { 438263a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Nonlinear solve converged due to %s iterations %" PetscInt_FMT "\n", SNESConvergedReasons[snes->reason], snes->iter)); 43832a359c20SBarry Smith } 4384eafd5ff0SAlex Lindsay } else if (snes->reason <= 0) { 43852a359c20SBarry Smith if (((PetscObject)snes)->prefix) { 438663a3b9bcSJacob 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)); 43872a359c20SBarry Smith } else { 438863a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, "Nonlinear solve did not converge due to %s iterations %" PetscInt_FMT "\n", SNESConvergedReasons[snes->reason], snes->iter)); 43892a359c20SBarry Smith } 43902a359c20SBarry Smith } 43919566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISubtractTab(viewer, ((PetscObject)snes)->tablevel)); 43922a359c20SBarry Smith } 43933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43942a359c20SBarry Smith } 43952a359c20SBarry Smith 4396c4421ceaSFande Kong /*@C 4397c4421ceaSFande Kong SNESConvergedReasonViewSet - Sets an ADDITIONAL function that is to be used at the 4398aaa8cc7dSPierre Jolivet end of the nonlinear solver to display the convergence reason of the nonlinear solver. 4399c4421ceaSFande Kong 4400c3339decSBarry Smith Logically Collective 4401c4421ceaSFande Kong 4402c4421ceaSFande Kong Input Parameters: 4403f6dfbefdSBarry Smith + snes - the `SNES` context 4404c4421ceaSFande Kong . f - the snes converged reason view function 4405c4421ceaSFande Kong . vctx - [optional] user-defined context for private data for the 4406dc4c0fb0SBarry Smith snes converged reason view routine (use `NULL` if no context is desired) 4407dc4c0fb0SBarry Smith - reasonviewdestroy - [optional] routine that frees reasonview context (may be `NULL`) 4408c4421ceaSFande Kong 4409c4421ceaSFande Kong Options Database Keys: 4410f6dfbefdSBarry Smith + -snes_converged_reason - sets a default `SNESConvergedReasonView()` 4411c4421ceaSFande Kong - -snes_converged_reason_view_cancel - cancels all converged reason viewers that have 4412c4421ceaSFande Kong been hardwired into a code by 4413f6dfbefdSBarry Smith calls to `SNESConvergedReasonViewSet()`, but 4414c4421ceaSFande Kong does not cancel those set via 4415c4421ceaSFande Kong the options database. 4416c4421ceaSFande Kong 4417dc4c0fb0SBarry Smith Level: intermediate 4418dc4c0fb0SBarry Smith 4419f6dfbefdSBarry Smith Note: 4420c4421ceaSFande Kong Several different converged reason view routines may be set by calling 4421f6dfbefdSBarry Smith `SNESConvergedReasonViewSet()` multiple times; all will be called in the 4422c4421ceaSFande Kong order in which they were set. 4423c4421ceaSFande Kong 4424dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESConvergedReason`, `SNESGetConvergedReason()`, `SNESConvergedReasonView()`, `SNESConvergedReasonViewCancel()` 4425c4421ceaSFande Kong @*/ 4426d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESConvergedReasonViewSet(SNES snes, PetscErrorCode (*f)(SNES, void *), void *vctx, PetscErrorCode (*reasonviewdestroy)(void **)) 4427d71ae5a4SJacob Faibussowitsch { 4428c4421ceaSFande Kong PetscInt i; 4429c4421ceaSFande Kong PetscBool identical; 4430c4421ceaSFande Kong 4431c4421ceaSFande Kong PetscFunctionBegin; 4432c4421ceaSFande Kong PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4433c4421ceaSFande Kong for (i = 0; i < snes->numberreasonviews; i++) { 44349566063dSJacob Faibussowitsch PetscCall(PetscMonitorCompare((PetscErrorCode(*)(void))f, vctx, reasonviewdestroy, (PetscErrorCode(*)(void))snes->reasonview[i], snes->reasonviewcontext[i], snes->reasonviewdestroy[i], &identical)); 44353ba16761SJacob Faibussowitsch if (identical) PetscFunctionReturn(PETSC_SUCCESS); 4436c4421ceaSFande Kong } 44375f80ce2aSJacob Faibussowitsch PetscCheck(snes->numberreasonviews < MAXSNESREASONVIEWS, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many SNES reasonview set"); 4438c4421ceaSFande Kong snes->reasonview[snes->numberreasonviews] = f; 4439c4421ceaSFande Kong snes->reasonviewdestroy[snes->numberreasonviews] = reasonviewdestroy; 4440c4421ceaSFande Kong snes->reasonviewcontext[snes->numberreasonviews++] = (void *)vctx; 44413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4442c4421ceaSFande Kong } 4443c4421ceaSFande Kong 444491f3e32bSBarry Smith /*@ 4445f6dfbefdSBarry Smith SNESConvergedReasonViewFromOptions - Processes command line options to determine if/how a `SNESConvergedReason` is to be viewed. 4446c4421ceaSFande Kong All the user-provided convergedReasonView routines will be involved as well, if they exist. 44472a359c20SBarry Smith 4448c3339decSBarry Smith Collective 44492a359c20SBarry Smith 44502fe279fdSBarry Smith Input Parameter: 4451f6dfbefdSBarry Smith . snes - the `SNES` object 44522a359c20SBarry Smith 4453f6dfbefdSBarry Smith Level: advanced 44542a359c20SBarry Smith 4455dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESConvergedReason`, `SNESConvergedReasonViewSet()`, `SNESCreate()`, `SNESSetUp()`, `SNESDestroy()`, 4456f6dfbefdSBarry Smith `SNESSetTolerances()`, `SNESConvergedDefault()`, `SNESGetConvergedReason()`, `SNESConvergedReasonView()` 44572a359c20SBarry Smith @*/ 4458d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESConvergedReasonViewFromOptions(SNES snes) 4459d71ae5a4SJacob Faibussowitsch { 44602a359c20SBarry Smith PetscViewer viewer; 44612a359c20SBarry Smith PetscBool flg; 44622a359c20SBarry Smith static PetscBool incall = PETSC_FALSE; 44632a359c20SBarry Smith PetscViewerFormat format; 4464c4421ceaSFande Kong PetscInt i; 44652a359c20SBarry Smith 44662a359c20SBarry Smith PetscFunctionBegin; 44673ba16761SJacob Faibussowitsch if (incall) PetscFunctionReturn(PETSC_SUCCESS); 44682a359c20SBarry Smith incall = PETSC_TRUE; 4469c4421ceaSFande Kong 4470c4421ceaSFande Kong /* All user-provided viewers are called first, if they exist. */ 447148a46eb9SPierre Jolivet for (i = 0; i < snes->numberreasonviews; i++) PetscCall((*snes->reasonview[i])(snes, snes->reasonviewcontext[i])); 4472c4421ceaSFande Kong 4473c4421ceaSFande Kong /* Call PETSc default routine if users ask for it */ 44749566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_converged_reason", &viewer, &format, &flg)); 44752a359c20SBarry Smith if (flg) { 44769566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 44779566063dSJacob Faibussowitsch PetscCall(SNESConvergedReasonView(snes, viewer)); 44789566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 44799566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 44802a359c20SBarry Smith } 44812a359c20SBarry Smith incall = PETSC_FALSE; 44823ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44832a359c20SBarry Smith } 44842a359c20SBarry Smith 4485487a658cSBarry Smith /*@ 4486f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 4487f6dfbefdSBarry Smith Call `SNESSolve()` after calling `SNESCreate()` and optional routines of the form `SNESSetXXX()`. 44889b94acceSBarry Smith 4489c3339decSBarry Smith Collective 4490c7afd0dbSLois Curfman McInnes 4491b2002411SLois Curfman McInnes Input Parameters: 4492f6dfbefdSBarry Smith + snes - the `SNES` context 4493dc4c0fb0SBarry Smith . b - the constant part of the equation F(x) = b, or `NULL` to use zero. 449485385478SLisandro Dalcin - x - the solution vector. 44959b94acceSBarry Smith 4496dc4c0fb0SBarry Smith Level: beginner 4497dc4c0fb0SBarry Smith 4498f6dfbefdSBarry Smith Note: 44998ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 4500f6dfbefdSBarry Smith for the nonlinear solve prior to calling `SNESSolve()`. In particular, 45018ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 4502f6dfbefdSBarry Smith this vector to zero by calling `VecSet()`. 45038ddd3da0SLois Curfman McInnes 4504dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESCreate()`, `SNESDestroy()`, `SNESSetFunction()`, `SNESSetJacobian()`, `SNESSetGridSequence()`, `SNESGetSolution()`, 4505db781477SPatrick Sanan `SNESNewtonTRSetPreCheck()`, `SNESNewtonTRGetPreCheck()`, `SNESNewtonTRSetPostCheck()`, `SNESNewtonTRGetPostCheck()`, 4506db781477SPatrick Sanan `SNESLineSearchSetPostCheck()`, `SNESLineSearchGetPostCheck()`, `SNESLineSearchSetPreCheck()`, `SNESLineSearchGetPreCheck()` 45079b94acceSBarry Smith @*/ 4508d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSolve(SNES snes, Vec b, Vec x) 4509d71ae5a4SJacob Faibussowitsch { 4510ace3abfcSBarry Smith PetscBool flg; 4511efd51863SBarry Smith PetscInt grid; 45120298fd71SBarry Smith Vec xcreated = NULL; 4513caa4e7f2SJed Brown DM dm; 4514052efed2SBarry Smith 45153a40ed3dSBarry Smith PetscFunctionBegin; 45160700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4517a69afd8bSBarry Smith if (x) PetscValidHeaderSpecific(x, VEC_CLASSID, 3); 4518a69afd8bSBarry Smith if (x) PetscCheckSameComm(snes, 1, x, 3); 45190700a824SBarry Smith if (b) PetscValidHeaderSpecific(b, VEC_CLASSID, 2); 452085385478SLisandro Dalcin if (b) PetscCheckSameComm(snes, 1, b, 2); 452185385478SLisandro Dalcin 452234b4d3a8SMatthew G. Knepley /* High level operations using the nonlinear solver */ 452306fc46c8SMatthew G. Knepley { 452406fc46c8SMatthew G. Knepley PetscViewer viewer; 452506fc46c8SMatthew G. Knepley PetscViewerFormat format; 45267c88af5aSMatthew G. Knepley PetscInt num; 452706fc46c8SMatthew G. Knepley PetscBool flg; 452806fc46c8SMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 452906fc46c8SMatthew G. Knepley 453006fc46c8SMatthew G. Knepley if (!incall) { 453134b4d3a8SMatthew G. Knepley /* Estimate the convergence rate of the discretization */ 45329566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_convergence_estimate", &viewer, &format, &flg)); 453306fc46c8SMatthew G. Knepley if (flg) { 453406fc46c8SMatthew G. Knepley PetscConvEst conv; 453546079b62SMatthew G. Knepley DM dm; 453646079b62SMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 453746079b62SMatthew G. Knepley PetscInt Nf; 453806fc46c8SMatthew G. Knepley 453906fc46c8SMatthew G. Knepley incall = PETSC_TRUE; 45409566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 45419566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 45429566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(Nf, &alpha)); 45439566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject)snes), &conv)); 45449566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject)snes)); 45459566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 45469566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 45479566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 45489566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 45499566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 45509566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 45519566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 45529566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 45539566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 455406fc46c8SMatthew G. Knepley incall = PETSC_FALSE; 455506fc46c8SMatthew G. Knepley } 455634b4d3a8SMatthew G. Knepley /* Adaptively refine the initial grid */ 4557b2588ea6SMatthew G. Knepley num = 1; 45589566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)snes)->prefix, "-snes_adapt_initial", &num, &flg)); 455934b4d3a8SMatthew G. Knepley if (flg) { 456034b4d3a8SMatthew G. Knepley DMAdaptor adaptor; 456134b4d3a8SMatthew G. Knepley 456234b4d3a8SMatthew G. Knepley incall = PETSC_TRUE; 45639566063dSJacob Faibussowitsch PetscCall(DMAdaptorCreate(PetscObjectComm((PetscObject)snes), &adaptor)); 45649566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetSolver(adaptor, snes)); 45659566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetSequenceLength(adaptor, num)); 45669566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetFromOptions(adaptor)); 45679566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetUp(adaptor)); 45689566063dSJacob Faibussowitsch PetscCall(DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_INITIAL, &dm, &x)); 45699566063dSJacob Faibussowitsch PetscCall(DMAdaptorDestroy(&adaptor)); 457034b4d3a8SMatthew G. Knepley incall = PETSC_FALSE; 457134b4d3a8SMatthew G. Knepley } 45727c88af5aSMatthew G. Knepley /* Use grid sequencing to adapt */ 45737c88af5aSMatthew G. Knepley num = 0; 45749566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL, ((PetscObject)snes)->prefix, "-snes_adapt_sequence", &num, NULL)); 45757c88af5aSMatthew G. Knepley if (num) { 45767c88af5aSMatthew G. Knepley DMAdaptor adaptor; 45777c88af5aSMatthew G. Knepley 45787c88af5aSMatthew G. Knepley incall = PETSC_TRUE; 45799566063dSJacob Faibussowitsch PetscCall(DMAdaptorCreate(PetscObjectComm((PetscObject)snes), &adaptor)); 45809566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetSolver(adaptor, snes)); 45819566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetSequenceLength(adaptor, num)); 45829566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetFromOptions(adaptor)); 45839566063dSJacob Faibussowitsch PetscCall(DMAdaptorSetUp(adaptor)); 45849566063dSJacob Faibussowitsch PetscCall(DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_SEQUENTIAL, &dm, &x)); 45859566063dSJacob Faibussowitsch PetscCall(DMAdaptorDestroy(&adaptor)); 45867c88af5aSMatthew G. Knepley incall = PETSC_FALSE; 45877c88af5aSMatthew G. Knepley } 458806fc46c8SMatthew G. Knepley } 458906fc46c8SMatthew G. Knepley } 4590ad540459SPierre Jolivet if (!x) x = snes->vec_sol; 4591caa4e7f2SJed Brown if (!x) { 45929566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 45939566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(dm, &xcreated)); 4594a69afd8bSBarry Smith x = xcreated; 4595a69afd8bSBarry Smith } 45969566063dSJacob Faibussowitsch PetscCall(SNESViewFromOptions(snes, NULL, "-snes_view_pre")); 4597f05ece33SBarry Smith 45989566063dSJacob Faibussowitsch for (grid = 0; grid < snes->gridsequence; grid++) PetscCall(PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)))); 4599efd51863SBarry Smith for (grid = 0; grid < snes->gridsequence + 1; grid++) { 460085385478SLisandro Dalcin /* set solution vector */ 46019566063dSJacob Faibussowitsch if (!grid) PetscCall(PetscObjectReference((PetscObject)x)); 46029566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_sol)); 460385385478SLisandro Dalcin snes->vec_sol = x; 46049566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 4605caa4e7f2SJed Brown 4606caa4e7f2SJed Brown /* set affine vector if provided */ 46079566063dSJacob Faibussowitsch if (b) PetscCall(PetscObjectReference((PetscObject)b)); 46089566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_rhs)); 460985385478SLisandro Dalcin snes->vec_rhs = b; 461085385478SLisandro Dalcin 46115f80ce2aSJacob 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"); 46125f80ce2aSJacob Faibussowitsch PetscCheck(snes->vec_func != snes->vec_sol, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "Solution vector cannot be function vector"); 46135f80ce2aSJacob Faibussowitsch PetscCheck(snes->vec_rhs != snes->vec_sol, PETSC_COMM_SELF, PETSC_ERR_ARG_IDN, "Solution vector cannot be right hand side vector"); 4614aa624791SPierre Jolivet if (!snes->vec_sol_update /* && snes->vec_sol */) PetscCall(VecDuplicate(snes->vec_sol, &snes->vec_sol_update)); 46159566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm, snes->vec_sol)); 46169566063dSJacob Faibussowitsch PetscCall(SNESSetUp(snes)); 46173f149594SLisandro Dalcin 46187eee914bSBarry Smith if (!grid) { 461925e27a38SBarry Smith if (snes->ops->computeinitialguess) PetscCallBack("SNES callback initial guess", (*snes->ops->computeinitialguess)(snes, snes->vec_sol, snes->initialguessP)); 4620dd568438SSatish Balay } 4621d25893d9SBarry Smith 4622abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 46239371c9d4SSatish Balay if (snes->counters_reset) { 46249371c9d4SSatish Balay snes->nfuncs = 0; 46259371c9d4SSatish Balay snes->linear_its = 0; 46269371c9d4SSatish Balay snes->numFailures = 0; 46279371c9d4SSatish Balay } 4628d5e45103SBarry Smith 46292d157150SStefano Zampini snes->reason = SNES_CONVERGED_ITERATING; 46309566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(SNES_Solve, snes, 0, 0, 0)); 4631dbbe0bcdSBarry Smith PetscUseTypeMethod(snes, solve); 46329566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(SNES_Solve, snes, 0, 0, 0)); 46332d157150SStefano Zampini PetscCheck(snes->reason, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Internal error, solver %s returned without setting converged reason", ((PetscObject)snes)->type_name); 4634422a814eSBarry Smith snes->domainerror = PETSC_FALSE; /* clear the flag if it has been set */ 46353f149594SLisandro Dalcin 463637ec4e1aSPeter Brune if (snes->lagjac_persist) snes->jac_iter += snes->iter; 463737ec4e1aSPeter Brune if (snes->lagpre_persist) snes->pre_iter += snes->iter; 463837ec4e1aSPeter Brune 46399566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes), ((PetscObject)snes)->options, ((PetscObject)snes)->prefix, "-snes_test_local_min", NULL, NULL, &flg)); 46409566063dSJacob Faibussowitsch if (flg && !PetscPreLoadingOn) PetscCall(SNESTestLocalMin(snes)); 4641c4421ceaSFande Kong /* Call converged reason views. This may involve user-provided viewers as well */ 46429566063dSJacob Faibussowitsch PetscCall(SNESConvergedReasonViewFromOptions(snes)); 46435968eb51SBarry Smith 46445f80ce2aSJacob Faibussowitsch if (snes->errorifnotconverged) PetscCheck(snes->reason >= 0, PetscObjectComm((PetscObject)snes), PETSC_ERR_NOT_CONVERGED, "SNESSolve has not converged"); 46459c8e83a9SBarry Smith if (snes->reason < 0) break; 4646efd51863SBarry Smith if (grid < snes->gridsequence) { 4647efd51863SBarry Smith DM fine; 4648efd51863SBarry Smith Vec xnew; 4649efd51863SBarry Smith Mat interp; 4650efd51863SBarry Smith 46519566063dSJacob Faibussowitsch PetscCall(DMRefine(snes->dm, PetscObjectComm((PetscObject)snes), &fine)); 46525f80ce2aSJacob Faibussowitsch PetscCheck(fine, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_INCOMP, "DMRefine() did not perform any refinement, cannot continue grid sequencing"); 46539566063dSJacob Faibussowitsch PetscCall(DMCreateInterpolation(snes->dm, fine, &interp, NULL)); 46549566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(fine, &xnew)); 46559566063dSJacob Faibussowitsch PetscCall(MatInterpolate(interp, x, xnew)); 46569566063dSJacob Faibussowitsch PetscCall(DMInterpolate(snes->dm, interp, fine)); 46579566063dSJacob Faibussowitsch PetscCall(MatDestroy(&interp)); 4658efd51863SBarry Smith x = xnew; 4659efd51863SBarry Smith 46609566063dSJacob Faibussowitsch PetscCall(SNESReset(snes)); 46619566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes, fine)); 46629566063dSJacob Faibussowitsch PetscCall(SNESResetFromOptions(snes)); 46639566063dSJacob Faibussowitsch PetscCall(DMDestroy(&fine)); 46649566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)))); 4665efd51863SBarry Smith } 4666efd51863SBarry Smith } 46679566063dSJacob Faibussowitsch PetscCall(SNESViewFromOptions(snes, NULL, "-snes_view")); 46689566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(snes->vec_sol, (PetscObject)snes, "-snes_view_solution")); 46699566063dSJacob Faibussowitsch PetscCall(DMMonitor(snes->dm)); 46709566063dSJacob Faibussowitsch PetscCall(SNESMonitorPauseFinal_Internal(snes)); 46713f7e2da0SPeter Brune 46729566063dSJacob Faibussowitsch PetscCall(VecDestroy(&xcreated)); 46739566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)snes)); 46743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46759b94acceSBarry Smith } 46769b94acceSBarry Smith 46779b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 46789b94acceSBarry Smith 467982bf6240SBarry Smith /*@C 46804b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 46819b94acceSBarry Smith 4682c3339decSBarry Smith Collective 4683fee21e36SBarry Smith 4684c7afd0dbSLois Curfman McInnes Input Parameters: 4685f6dfbefdSBarry Smith + snes - the `SNES` context 4686454a90a3SBarry Smith - type - a known method 4687c7afd0dbSLois Curfman McInnes 4688c7afd0dbSLois Curfman McInnes Options Database Key: 4689454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 469004d7464bSBarry Smith of available methods (for instance, newtonls or newtontr) 4691ae12b187SLois Curfman McInnes 4692dc4c0fb0SBarry Smith Level: intermediate 4693dc4c0fb0SBarry Smith 46949b94acceSBarry Smith Notes: 4695e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 4696f6dfbefdSBarry Smith + `SNESNEWTONLS` - Newton's method with line search 4697c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 46984a221d59SStefano Zampini - `SNESNEWTONTR` - Newton's method with trust region 4699c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 47009b94acceSBarry Smith 4701f6dfbefdSBarry Smith Normally, it is best to use the `SNESSetFromOptions()` command and then 4702f6dfbefdSBarry Smith set the `SNES` solver type from the options database rather than by using 4703ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 4704ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 4705f6dfbefdSBarry Smith The `SNESSetType()` routine is provided for those situations where it 4706ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 4707ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 4708ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 4709ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 4710b0a32e0cSBarry Smith appropriate method. 471136851e7fSLois Curfman McInnes 4712f6dfbefdSBarry Smith Developer Note: 4713f6dfbefdSBarry Smith `SNESRegister()` adds a constructor for a new `SNESType` to `SNESList`, `SNESSetType()` locates 4714f6dfbefdSBarry Smith the constructor in that list and calls it to create the specific object. 47158f6c3df8SBarry Smith 4716dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESType`, `SNESCreate()`, `SNESDestroy()`, `SNESGetType()`, `SNESSetFromOptions()` 47179b94acceSBarry Smith @*/ 4718d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetType(SNES snes, SNESType type) 4719d71ae5a4SJacob Faibussowitsch { 4720ace3abfcSBarry Smith PetscBool match; 47215f80ce2aSJacob Faibussowitsch PetscErrorCode (*r)(SNES); 47223a40ed3dSBarry Smith 47233a40ed3dSBarry Smith PetscFunctionBegin; 47240700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 47254482741eSBarry Smith PetscValidCharPointer(type, 2); 472682bf6240SBarry Smith 47279566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)snes, type, &match)); 47283ba16761SJacob Faibussowitsch if (match) PetscFunctionReturn(PETSC_SUCCESS); 472992ff6ae8SBarry Smith 47309566063dSJacob Faibussowitsch PetscCall(PetscFunctionListFind(SNESList, type, &r)); 4731*6adde796SStefano Zampini PetscCheck(r, PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unable to find requested SNES type %s", type); 473275396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 4733dbbe0bcdSBarry Smith PetscTryTypeMethod(snes, destroy); 473475396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 47359e5d0892SLisandro Dalcin snes->ops->setup = NULL; 47369e5d0892SLisandro Dalcin snes->ops->solve = NULL; 47379e5d0892SLisandro Dalcin snes->ops->view = NULL; 47389e5d0892SLisandro Dalcin snes->ops->setfromoptions = NULL; 47399e5d0892SLisandro Dalcin snes->ops->destroy = NULL; 47407fe760d5SStefano Zampini 47417fe760d5SStefano Zampini /* It may happen the user has customized the line search before calling SNESSetType */ 47429566063dSJacob Faibussowitsch if (((PetscObject)snes)->type_name) PetscCall(SNESLineSearchDestroy(&snes->linesearch)); 47437fe760d5SStefano Zampini 474475396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 474575396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 4746f5af7f23SKarl Rupp 47479566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)snes, type)); 47489566063dSJacob Faibussowitsch PetscCall((*r)(snes)); 47493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47509b94acceSBarry Smith } 47519b94acceSBarry Smith 47529b94acceSBarry Smith /*@C 4753f6dfbefdSBarry Smith SNESGetType - Gets the `SNES` method type and name (as a string). 47549b94acceSBarry Smith 4755c7afd0dbSLois Curfman McInnes Not Collective 4756c7afd0dbSLois Curfman McInnes 47579b94acceSBarry Smith Input Parameter: 47584b0e389bSBarry Smith . snes - nonlinear solver context 47599b94acceSBarry Smith 47609b94acceSBarry Smith Output Parameter: 4761f6dfbefdSBarry Smith . type - `SNES` method (a character string) 47629b94acceSBarry Smith 476336851e7fSLois Curfman McInnes Level: intermediate 476436851e7fSLois Curfman McInnes 4765dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetType()`, `SNESType`, `SNESSetFromOptions()`, `SNES` 47669b94acceSBarry Smith @*/ 4767d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetType(SNES snes, SNESType *type) 4768d71ae5a4SJacob Faibussowitsch { 47693a40ed3dSBarry Smith PetscFunctionBegin; 47700700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 47714482741eSBarry Smith PetscValidPointer(type, 2); 47727adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 47733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47749b94acceSBarry Smith } 47759b94acceSBarry Smith 47763cd8a7caSMatthew G. Knepley /*@ 4777f6dfbefdSBarry Smith SNESSetSolution - Sets the solution vector for use by the `SNES` routines. 47783cd8a7caSMatthew G. Knepley 4779c3339decSBarry Smith Logically Collective 47803cd8a7caSMatthew G. Knepley 47813cd8a7caSMatthew G. Knepley Input Parameters: 4782f6dfbefdSBarry Smith + snes - the `SNES` context obtained from `SNESCreate()` 47833cd8a7caSMatthew G. Knepley - u - the solution vector 47843cd8a7caSMatthew G. Knepley 47853cd8a7caSMatthew G. Knepley Level: beginner 47863cd8a7caSMatthew G. Knepley 4787dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSolve()`, `SNESGetSolution()`, `Vec` 47883cd8a7caSMatthew G. Knepley @*/ 4789d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetSolution(SNES snes, Vec u) 4790d71ae5a4SJacob Faibussowitsch { 47913cd8a7caSMatthew G. Knepley DM dm; 47923cd8a7caSMatthew G. Knepley 47933cd8a7caSMatthew G. Knepley PetscFunctionBegin; 47943cd8a7caSMatthew G. Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 47953cd8a7caSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 47969566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)u)); 47979566063dSJacob Faibussowitsch PetscCall(VecDestroy(&snes->vec_sol)); 47983cd8a7caSMatthew G. Knepley 47993cd8a7caSMatthew G. Knepley snes->vec_sol = u; 48003cd8a7caSMatthew G. Knepley 48019566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 48029566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm, u)); 48033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 48043cd8a7caSMatthew G. Knepley } 48053cd8a7caSMatthew G. Knepley 480652baeb72SSatish Balay /*@ 48079b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 4808f6dfbefdSBarry Smith stored. This is the fine grid solution when using `SNESSetGridSequence()`. 48099b94acceSBarry Smith 4810f6dfbefdSBarry Smith Not Collective, but x is parallel if snes is parallel 4811c7afd0dbSLois Curfman McInnes 48129b94acceSBarry Smith Input Parameter: 4813f6dfbefdSBarry Smith . snes - the `SNES` context 48149b94acceSBarry Smith 48159b94acceSBarry Smith Output Parameter: 48169b94acceSBarry Smith . x - the solution 48179b94acceSBarry Smith 481870e92668SMatthew Knepley Level: intermediate 481936851e7fSLois Curfman McInnes 4820dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetSolution()`, `SNESSolve()`, `SNES`, `SNESGetSolutionUpdate()`, `SNESGetFunction()` 48219b94acceSBarry Smith @*/ 4822d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetSolution(SNES snes, Vec *x) 4823d71ae5a4SJacob Faibussowitsch { 48243a40ed3dSBarry Smith PetscFunctionBegin; 48250700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 48264482741eSBarry Smith PetscValidPointer(x, 2); 482785385478SLisandro Dalcin *x = snes->vec_sol; 48283ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 482970e92668SMatthew Knepley } 483070e92668SMatthew Knepley 483152baeb72SSatish Balay /*@ 48329b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 48339b94acceSBarry Smith stored. 48349b94acceSBarry Smith 4835f6dfbefdSBarry Smith Not Collective, but x is parallel if snes is parallel 4836c7afd0dbSLois Curfman McInnes 48379b94acceSBarry Smith Input Parameter: 4838f6dfbefdSBarry Smith . snes - the `SNES` context 48399b94acceSBarry Smith 48409b94acceSBarry Smith Output Parameter: 48419b94acceSBarry Smith . x - the solution update 48429b94acceSBarry Smith 484336851e7fSLois Curfman McInnes Level: advanced 484436851e7fSLois Curfman McInnes 4845dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESGetSolution()`, `SNESGetFunction()` 48469b94acceSBarry Smith @*/ 4847d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetSolutionUpdate(SNES snes, Vec *x) 4848d71ae5a4SJacob Faibussowitsch { 48493a40ed3dSBarry Smith PetscFunctionBegin; 48500700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 48514482741eSBarry Smith PetscValidPointer(x, 2); 485285385478SLisandro Dalcin *x = snes->vec_sol_update; 48533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 48549b94acceSBarry Smith } 48559b94acceSBarry Smith 48569b94acceSBarry Smith /*@C 4857f6dfbefdSBarry Smith SNESGetFunction - Returns the function that defines the nonlinear system set with `SNESSetFunction()` 48589b94acceSBarry Smith 4859f6dfbefdSBarry Smith Not Collective, but r is parallel if snes is parallel. Collective if r is requested, but has not been created yet. 4860c7afd0dbSLois Curfman McInnes 48619b94acceSBarry Smith Input Parameter: 4862f6dfbefdSBarry Smith . snes - the `SNES` context 48639b94acceSBarry Smith 4864d8d19677SJose E. Roman Output Parameters: 4865dc4c0fb0SBarry Smith + r - the vector that is used to store residuals (or `NULL` if you don't want it) 486620f4b53cSBarry Smith . f - the function (or `NULL` if you don't want it); for calling sequence see `SNESFunction` 4867dc4c0fb0SBarry Smith - ctx - the function context (or `NULL` if you don't want it) 48689b94acceSBarry Smith 486936851e7fSLois Curfman McInnes Level: advanced 487036851e7fSLois Curfman McInnes 4871f6dfbefdSBarry Smith Note: 4872dc4c0fb0SBarry Smith The vector `r` DOES NOT, in general, contain the current value of the `SNES` nonlinear function 487304edfde5SBarry Smith 4874dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES, `SNESSolve()`, `SNESSetFunction()`, `SNESGetSolution()`, `SNESFunction` 48759b94acceSBarry Smith @*/ 4876d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetFunction(SNES snes, Vec *r, PetscErrorCode (**f)(SNES, Vec, Vec, void *), void **ctx) 4877d71ae5a4SJacob Faibussowitsch { 48786cab3a1bSJed Brown DM dm; 4879a63bb30eSJed Brown 48803a40ed3dSBarry Smith PetscFunctionBegin; 48810700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 4882a63bb30eSJed Brown if (r) { 4883a63bb30eSJed Brown if (!snes->vec_func) { 4884a63bb30eSJed Brown if (snes->vec_rhs) { 48859566063dSJacob Faibussowitsch PetscCall(VecDuplicate(snes->vec_rhs, &snes->vec_func)); 4886a63bb30eSJed Brown } else if (snes->vec_sol) { 48879566063dSJacob Faibussowitsch PetscCall(VecDuplicate(snes->vec_sol, &snes->vec_func)); 4888a63bb30eSJed Brown } else if (snes->dm) { 48899566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(snes->dm, &snes->vec_func)); 4890a63bb30eSJed Brown } 4891a63bb30eSJed Brown } 4892a63bb30eSJed Brown *r = snes->vec_func; 4893a63bb30eSJed Brown } 48949566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 48959566063dSJacob Faibussowitsch PetscCall(DMSNESGetFunction(dm, f, ctx)); 48963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 48979b94acceSBarry Smith } 48989b94acceSBarry Smith 4899c79ef259SPeter Brune /*@C 490037fdd005SBarry Smith SNESGetNGS - Returns the function and context set with `SNESSetNGS()` 4901c79ef259SPeter Brune 4902c79ef259SPeter Brune Input Parameter: 4903f6dfbefdSBarry Smith . snes - the `SNES` context 4904c79ef259SPeter Brune 4905d8d19677SJose E. Roman Output Parameters: 490637fdd005SBarry Smith + f - the function (or `NULL`) see `SNESSetNGS()` for details 4907dc4c0fb0SBarry Smith - ctx - the function context (or `NULL`) 4908c79ef259SPeter Brune 4909c79ef259SPeter Brune Level: advanced 4910c79ef259SPeter Brune 4911dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetNGS()`, `SNESGetFunction()` 4912c79ef259SPeter Brune @*/ 4913c79ef259SPeter Brune 4914d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetNGS(SNES snes, PetscErrorCode (**f)(SNES, Vec, Vec, void *), void **ctx) 4915d71ae5a4SJacob Faibussowitsch { 49166cab3a1bSJed Brown DM dm; 49176cab3a1bSJed Brown 4918646217ecSPeter Brune PetscFunctionBegin; 4919646217ecSPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 49209566063dSJacob Faibussowitsch PetscCall(SNESGetDM(snes, &dm)); 49219566063dSJacob Faibussowitsch PetscCall(DMSNESGetNGS(dm, f, ctx)); 49223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4923646217ecSPeter Brune } 4924646217ecSPeter Brune 49253c7409f5SSatish Balay /*@C 49263c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 4927f6dfbefdSBarry Smith `SNES` options in the database. 49283c7409f5SSatish Balay 4929c3339decSBarry Smith Logically Collective 4930fee21e36SBarry Smith 4931d8d19677SJose E. Roman Input Parameters: 4932f6dfbefdSBarry Smith + snes - the `SNES` context 4933c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 4934c7afd0dbSLois Curfman McInnes 4935dc4c0fb0SBarry Smith Level: advanced 4936dc4c0fb0SBarry Smith 4937f6dfbefdSBarry Smith Note: 4938a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 4939c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 4940d850072dSLois Curfman McInnes 4941dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetFromOptions()`, `SNESAppendOptionsPrefix()` 49423c7409f5SSatish Balay @*/ 4943d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetOptionsPrefix(SNES snes, const char prefix[]) 4944d71ae5a4SJacob Faibussowitsch { 49453a40ed3dSBarry Smith PetscFunctionBegin; 49460700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 49479566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)snes, prefix)); 49489566063dSJacob Faibussowitsch if (!snes->ksp) PetscCall(SNESGetKSP(snes, &snes->ksp)); 494935f5d045SPeter Brune if (snes->linesearch) { 49509566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &snes->linesearch)); 49519566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)snes->linesearch, prefix)); 495235f5d045SPeter Brune } 49539566063dSJacob Faibussowitsch PetscCall(KSPSetOptionsPrefix(snes->ksp, prefix)); 49543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 49553c7409f5SSatish Balay } 49563c7409f5SSatish Balay 49573c7409f5SSatish Balay /*@C 4958f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 4959f6dfbefdSBarry Smith `SNES` options in the database. 49603c7409f5SSatish Balay 4961c3339decSBarry Smith Logically Collective 4962fee21e36SBarry Smith 4963c7afd0dbSLois Curfman McInnes Input Parameters: 4964f6dfbefdSBarry Smith + snes - the `SNES` context 4965c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 4966c7afd0dbSLois Curfman McInnes 4967dc4c0fb0SBarry Smith Level: advanced 4968dc4c0fb0SBarry Smith 4969f6dfbefdSBarry Smith Note: 4970a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 4971c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 4972d850072dSLois Curfman McInnes 4973dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESGetOptionsPrefix()`, `SNESSetOptionsPrefix()` 49743c7409f5SSatish Balay @*/ 4975d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESAppendOptionsPrefix(SNES snes, const char prefix[]) 4976d71ae5a4SJacob Faibussowitsch { 49773a40ed3dSBarry Smith PetscFunctionBegin; 49780700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 49799566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix)); 49809566063dSJacob Faibussowitsch if (!snes->ksp) PetscCall(SNESGetKSP(snes, &snes->ksp)); 498135f5d045SPeter Brune if (snes->linesearch) { 49829566063dSJacob Faibussowitsch PetscCall(SNESGetLineSearch(snes, &snes->linesearch)); 49839566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)snes->linesearch, prefix)); 498435f5d045SPeter Brune } 49859566063dSJacob Faibussowitsch PetscCall(KSPAppendOptionsPrefix(snes->ksp, prefix)); 49863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 49873c7409f5SSatish Balay } 49883c7409f5SSatish Balay 49899ab63eb5SSatish Balay /*@C 4990f6dfbefdSBarry Smith SNESGetOptionsPrefix - Gets the prefix used for searching for all 4991f6dfbefdSBarry Smith `SNES` options in the database. 49923c7409f5SSatish Balay 4993c7afd0dbSLois Curfman McInnes Not Collective 4994c7afd0dbSLois Curfman McInnes 49953c7409f5SSatish Balay Input Parameter: 4996f6dfbefdSBarry Smith . snes - the `SNES` context 49973c7409f5SSatish Balay 49983c7409f5SSatish Balay Output Parameter: 49993c7409f5SSatish Balay . prefix - pointer to the prefix string used 50003c7409f5SSatish Balay 500136851e7fSLois Curfman McInnes Level: advanced 500236851e7fSLois Curfman McInnes 5003dc4c0fb0SBarry Smith Fortran Note: 5004dc4c0fb0SBarry Smith The user should pass in a string 'prefix' of 5005dc4c0fb0SBarry Smith sufficient length to hold the prefix. 5006dc4c0fb0SBarry Smith 5007dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetOptionsPrefix()`, `SNESAppendOptionsPrefix()` 50083c7409f5SSatish Balay @*/ 5009d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetOptionsPrefix(SNES snes, const char *prefix[]) 5010d71ae5a4SJacob Faibussowitsch { 50113a40ed3dSBarry Smith PetscFunctionBegin; 50120700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 50139566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)snes, prefix)); 50143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 50153c7409f5SSatish Balay } 50163c7409f5SSatish Balay 50173cea93caSBarry Smith /*@C 50181c84c290SBarry Smith SNESRegister - Adds a method to the nonlinear solver package. 50191c84c290SBarry Smith 502020f4b53cSBarry Smith Not Collective 50211c84c290SBarry Smith 50221c84c290SBarry Smith Input Parameters: 502320f4b53cSBarry Smith + sname - name of a new user-defined solver 502420f4b53cSBarry Smith - function - routine to create method context 50251c84c290SBarry Smith 5026dc4c0fb0SBarry Smith Level: advanced 5027dc4c0fb0SBarry Smith 5028f6dfbefdSBarry Smith Note: 5029f6dfbefdSBarry Smith `SNESRegister()` may be called multiple times to add several user-defined solvers. 50301c84c290SBarry Smith 50311c84c290SBarry Smith Sample usage: 50321c84c290SBarry Smith .vb 5033bdf89e91SBarry Smith SNESRegister("my_solver", MySolverCreate); 50341c84c290SBarry Smith .ve 50351c84c290SBarry Smith 50361c84c290SBarry Smith Then, your solver can be chosen with the procedural interface via 50371c84c290SBarry Smith $ SNESSetType(snes, "my_solver") 50381c84c290SBarry Smith or at runtime via the option 50391c84c290SBarry Smith $ -snes_type my_solver 50401c84c290SBarry Smith 5041dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESRegisterAll()`, `SNESRegisterDestroy()` 50423cea93caSBarry Smith @*/ 5043d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESRegister(const char sname[], PetscErrorCode (*function)(SNES)) 5044d71ae5a4SJacob Faibussowitsch { 5045b2002411SLois Curfman McInnes PetscFunctionBegin; 50469566063dSJacob Faibussowitsch PetscCall(SNESInitializePackage()); 50479566063dSJacob Faibussowitsch PetscCall(PetscFunctionListAdd(&SNESList, sname, function)); 50483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5049b2002411SLois Curfman McInnes } 5050da9b6338SBarry Smith 5051d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTestLocalMin(SNES snes) 5052d71ae5a4SJacob Faibussowitsch { 505377431f27SBarry Smith PetscInt N, i, j; 5054da9b6338SBarry Smith Vec u, uh, fh; 5055da9b6338SBarry Smith PetscScalar value; 5056da9b6338SBarry Smith PetscReal norm; 5057da9b6338SBarry Smith 5058da9b6338SBarry Smith PetscFunctionBegin; 50599566063dSJacob Faibussowitsch PetscCall(SNESGetSolution(snes, &u)); 50609566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &uh)); 50619566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &fh)); 5062da9b6338SBarry Smith 5063da9b6338SBarry Smith /* currently only works for sequential */ 50649566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PetscObjectComm((PetscObject)snes), "Testing FormFunction() for local min\n")); 50659566063dSJacob Faibussowitsch PetscCall(VecGetSize(u, &N)); 5066da9b6338SBarry Smith for (i = 0; i < N; i++) { 50679566063dSJacob Faibussowitsch PetscCall(VecCopy(u, uh)); 506863a3b9bcSJacob Faibussowitsch PetscCall(PetscPrintf(PetscObjectComm((PetscObject)snes), "i = %" PetscInt_FMT "\n", i)); 5069da9b6338SBarry Smith for (j = -10; j < 11; j++) { 50708b49ba18SBarry Smith value = PetscSign(j) * PetscExpReal(PetscAbs(j) - 10.0); 50719566063dSJacob Faibussowitsch PetscCall(VecSetValue(uh, i, value, ADD_VALUES)); 50729566063dSJacob Faibussowitsch PetscCall(SNESComputeFunction(snes, uh, fh)); 50739566063dSJacob Faibussowitsch PetscCall(VecNorm(fh, NORM_2, &norm)); 507463a3b9bcSJacob Faibussowitsch PetscCall(PetscPrintf(PetscObjectComm((PetscObject)snes), " j norm %" PetscInt_FMT " %18.16e\n", j, (double)norm)); 5075da9b6338SBarry Smith value = -value; 50769566063dSJacob Faibussowitsch PetscCall(VecSetValue(uh, i, value, ADD_VALUES)); 5077da9b6338SBarry Smith } 5078da9b6338SBarry Smith } 50799566063dSJacob Faibussowitsch PetscCall(VecDestroy(&uh)); 50809566063dSJacob Faibussowitsch PetscCall(VecDestroy(&fh)); 50813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5082da9b6338SBarry Smith } 508371f87433Sdalcinl 508471f87433Sdalcinl /*@ 5085f6dfbefdSBarry Smith SNESKSPSetUseEW - Sets `SNES` to the use Eisenstat-Walker method for 508671f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 508771f87433Sdalcinl Newton method. 508871f87433Sdalcinl 5089c3339decSBarry Smith Logically Collective 509071f87433Sdalcinl 509171f87433Sdalcinl Input Parameters: 5092f6dfbefdSBarry Smith + snes - `SNES` context 5093f6dfbefdSBarry Smith - flag - `PETSC_TRUE` or `PETSC_FALSE` 509471f87433Sdalcinl 5095f6dfbefdSBarry Smith Options Database Keys: 509664ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 509764ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 509864ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 509964ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 510064ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 510164ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 510264ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 510364ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 510464ba62caSBarry Smith 5105dc4c0fb0SBarry Smith Level: advanced 5106dc4c0fb0SBarry Smith 5107f6dfbefdSBarry Smith Note: 5108f6dfbefdSBarry Smith The default is to use a constant relative tolerance for 510971f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 511071f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 511171f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 511271f87433Sdalcinl solver. 511371f87433Sdalcinl 511471f87433Sdalcinl Reference: 5115f6dfbefdSBarry Smith . - * S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an inexact Newton method", SISC 17 (1), pp.16-32, 1996. 511671f87433Sdalcinl 5117dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `KSP`, `SNES`, `SNESKSPGetUseEW()`, `SNESKSPGetParametersEW()`, `SNESKSPSetParametersEW()` 511871f87433Sdalcinl @*/ 5119d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESKSPSetUseEW(SNES snes, PetscBool flag) 5120d71ae5a4SJacob Faibussowitsch { 512171f87433Sdalcinl PetscFunctionBegin; 51220700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5123acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes, flag, 2); 512471f87433Sdalcinl snes->ksp_ewconv = flag; 51253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 512671f87433Sdalcinl } 512771f87433Sdalcinl 512871f87433Sdalcinl /*@ 5129f6dfbefdSBarry Smith SNESKSPGetUseEW - Gets if `SNES` is using Eisenstat-Walker method 513071f87433Sdalcinl for computing relative tolerance for linear solvers within an 513171f87433Sdalcinl inexact Newton method. 513271f87433Sdalcinl 513371f87433Sdalcinl Not Collective 513471f87433Sdalcinl 513571f87433Sdalcinl Input Parameter: 5136f6dfbefdSBarry Smith . snes - `SNES` context 513771f87433Sdalcinl 513871f87433Sdalcinl Output Parameter: 5139f6dfbefdSBarry Smith . flag - `PETSC_TRUE` or `PETSC_FALSE` 514071f87433Sdalcinl 514171f87433Sdalcinl Level: advanced 514271f87433Sdalcinl 5143dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESKSPSetUseEW()`, `SNESKSPGetParametersEW()`, `SNESKSPSetParametersEW()` 514471f87433Sdalcinl @*/ 5145d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 5146d71ae5a4SJacob Faibussowitsch { 514771f87433Sdalcinl PetscFunctionBegin; 51480700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5149534a8f05SLisandro Dalcin PetscValidBoolPointer(flag, 2); 515071f87433Sdalcinl *flag = snes->ksp_ewconv; 51513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 515271f87433Sdalcinl } 515371f87433Sdalcinl 515471f87433Sdalcinl /*@ 5155fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 515671f87433Sdalcinl convergence criteria for the linear solvers within an inexact 515771f87433Sdalcinl Newton method. 515871f87433Sdalcinl 5159c3339decSBarry Smith Logically Collective 516071f87433Sdalcinl 516171f87433Sdalcinl Input Parameters: 5162f6dfbefdSBarry Smith + snes - `SNES` context 51630f0abf79SStefano Zampini . version - version 1, 2 (default is 2), 3 or 4 516471f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 516571f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 516671f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 516771f87433Sdalcinl (0 <= gamma2 <= 1) 516871f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 516971f87433Sdalcinl . alpha2 - power for safeguard 517071f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 517171f87433Sdalcinl 5172dc4c0fb0SBarry Smith Level: advanced 5173dc4c0fb0SBarry Smith 5174f6dfbefdSBarry Smith Notes: 517571f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 517671f87433Sdalcinl 5177f6dfbefdSBarry Smith Use `PETSC_DEFAULT` to retain the default for any of the parameters. 517871f87433Sdalcinl 5179dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESKSPSetUseEW()`, `SNESKSPGetUseEW()`, `SNESKSPGetParametersEW()` 518071f87433Sdalcinl @*/ 5181d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESKSPSetParametersEW(SNES snes, PetscInt version, PetscReal rtol_0, PetscReal rtol_max, PetscReal gamma, PetscReal alpha, PetscReal alpha2, PetscReal threshold) 5182d71ae5a4SJacob Faibussowitsch { 5183fa9f3622SBarry Smith SNESKSPEW *kctx; 51845fd66863SKarl Rupp 518571f87433Sdalcinl PetscFunctionBegin; 51860700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5187fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 51885f80ce2aSJacob Faibussowitsch PetscCheck(kctx, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "No Eisenstat-Walker context existing"); 5189c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes, version, 2); 5190c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, rtol_0, 3); 5191c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, rtol_max, 4); 5192c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, gamma, 5); 5193c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, alpha, 6); 5194c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, alpha2, 7); 5195c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes, threshold, 8); 519671f87433Sdalcinl 519771f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 519813bcc0bdSJacob Faibussowitsch if (rtol_0 != (PetscReal)PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 519913bcc0bdSJacob Faibussowitsch if (rtol_max != (PetscReal)PETSC_DEFAULT) kctx->rtol_max = rtol_max; 520013bcc0bdSJacob Faibussowitsch if (gamma != (PetscReal)PETSC_DEFAULT) kctx->gamma = gamma; 520113bcc0bdSJacob Faibussowitsch if (alpha != (PetscReal)PETSC_DEFAULT) kctx->alpha = alpha; 520213bcc0bdSJacob Faibussowitsch if (alpha2 != (PetscReal)PETSC_DEFAULT) kctx->alpha2 = alpha2; 520313bcc0bdSJacob Faibussowitsch if (threshold != (PetscReal)PETSC_DEFAULT) kctx->threshold = threshold; 520471f87433Sdalcinl 52050f0abf79SStefano 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); 52060b121fc5SBarry 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); 52070b121fc5SBarry 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); 52080b121fc5SBarry 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); 52090b121fc5SBarry 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); 52100b121fc5SBarry 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); 52113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 521271f87433Sdalcinl } 521371f87433Sdalcinl 521471f87433Sdalcinl /*@ 5215fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 521671f87433Sdalcinl convergence criteria for the linear solvers within an inexact 521771f87433Sdalcinl Newton method. 521871f87433Sdalcinl 521971f87433Sdalcinl Not Collective 522071f87433Sdalcinl 522197bb3fdcSJose E. Roman Input Parameter: 5222f6dfbefdSBarry Smith . snes - `SNES` context 522371f87433Sdalcinl 522471f87433Sdalcinl Output Parameters: 52250f0abf79SStefano Zampini + version - version 1, 2 (default is 2), 3 or 4 522671f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 522771f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 5228bf388a1fSBarry Smith . gamma - multiplicative factor for version 2 rtol computation (0 <= gamma2 <= 1) 522971f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 523071f87433Sdalcinl . alpha2 - power for safeguard 523171f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 523271f87433Sdalcinl 523371f87433Sdalcinl Level: advanced 523471f87433Sdalcinl 5235dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESKSPSetUseEW()`, `SNESKSPGetUseEW()`, `SNESKSPSetParametersEW()` 523671f87433Sdalcinl @*/ 5237d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESKSPGetParametersEW(SNES snes, PetscInt *version, PetscReal *rtol_0, PetscReal *rtol_max, PetscReal *gamma, PetscReal *alpha, PetscReal *alpha2, PetscReal *threshold) 5238d71ae5a4SJacob Faibussowitsch { 5239fa9f3622SBarry Smith SNESKSPEW *kctx; 52405fd66863SKarl Rupp 524171f87433Sdalcinl PetscFunctionBegin; 52420700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5243fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 52445f80ce2aSJacob Faibussowitsch PetscCheck(kctx, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "No Eisenstat-Walker context existing"); 524571f87433Sdalcinl if (version) *version = kctx->version; 524671f87433Sdalcinl if (rtol_0) *rtol_0 = kctx->rtol_0; 524771f87433Sdalcinl if (rtol_max) *rtol_max = kctx->rtol_max; 524871f87433Sdalcinl if (gamma) *gamma = kctx->gamma; 524971f87433Sdalcinl if (alpha) *alpha = kctx->alpha; 525071f87433Sdalcinl if (alpha2) *alpha2 = kctx->alpha2; 525171f87433Sdalcinl if (threshold) *threshold = kctx->threshold; 52523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 525371f87433Sdalcinl } 525471f87433Sdalcinl 5255d71ae5a4SJacob Faibussowitsch PetscErrorCode KSPPreSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes) 5256d71ae5a4SJacob Faibussowitsch { 5257fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW *)snes->kspconvctx; 525871f87433Sdalcinl PetscReal rtol = PETSC_DEFAULT, stol; 525971f87433Sdalcinl 526071f87433Sdalcinl PetscFunctionBegin; 52613ba16761SJacob Faibussowitsch if (!snes->ksp_ewconv) PetscFunctionReturn(PETSC_SUCCESS); 526230058271SDmitry Karpeev if (!snes->iter) { 526330058271SDmitry Karpeev rtol = kctx->rtol_0; /* first time in, so use the original user rtol */ 52649566063dSJacob Faibussowitsch PetscCall(VecNorm(snes->vec_func, NORM_2, &kctx->norm_first)); 52650f0abf79SStefano Zampini } else { 52660fdf79fbSJacob Faibussowitsch PetscCheck(kctx->version >= 1 && kctx->version <= 4, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Only versions 1-4 are supported: %" PetscInt_FMT, kctx->version); 526771f87433Sdalcinl if (kctx->version == 1) { 52680f0abf79SStefano Zampini rtol = PetscAbsReal(snes->norm - kctx->lresid_last) / kctx->norm_last; 526985ec1a3cSBarry Smith stol = PetscPowReal(kctx->rtol_last, kctx->alpha2); 527071f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol, stol); 527171f87433Sdalcinl } else if (kctx->version == 2) { 527285ec1a3cSBarry Smith rtol = kctx->gamma * PetscPowReal(snes->norm / kctx->norm_last, kctx->alpha); 527385ec1a3cSBarry Smith stol = kctx->gamma * PetscPowReal(kctx->rtol_last, kctx->alpha); 527471f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol, stol); 527571f87433Sdalcinl } else if (kctx->version == 3) { /* contributed by Luis Chacon, June 2006. */ 527685ec1a3cSBarry Smith rtol = kctx->gamma * PetscPowReal(snes->norm / kctx->norm_last, kctx->alpha); 527771f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 527885ec1a3cSBarry Smith stol = kctx->gamma * PetscPowReal(kctx->rtol_last, kctx->alpha); 527971f87433Sdalcinl stol = PetscMax(rtol, stol); 528071f87433Sdalcinl rtol = PetscMin(kctx->rtol_0, stol); 528171f87433Sdalcinl /* safeguard: avoid oversolving */ 528230058271SDmitry Karpeev stol = kctx->gamma * (kctx->norm_first * snes->rtol) / snes->norm; 528371f87433Sdalcinl stol = PetscMax(rtol, stol); 528471f87433Sdalcinl rtol = PetscMin(kctx->rtol_0, stol); 52850fdf79fbSJacob Faibussowitsch } else /* if (kctx->version == 4) */ { 52860fdf79fbSJacob Faibussowitsch /* H.-B. An et al. Journal of Computational and Applied Mathematics 200 (2007) 47-60 */ 52870f0abf79SStefano Zampini PetscReal ared = PetscAbsReal(kctx->norm_last - snes->norm); 52880f0abf79SStefano Zampini PetscReal pred = PetscAbsReal(kctx->norm_last - kctx->lresid_last); 52890f0abf79SStefano Zampini PetscReal rk = ared / pred; 52900f0abf79SStefano Zampini if (rk < kctx->v4_p1) rtol = 1. - 2. * kctx->v4_p1; 52910f0abf79SStefano Zampini else if (rk < kctx->v4_p2) rtol = kctx->rtol_last; 52920f0abf79SStefano Zampini else if (rk < kctx->v4_p3) rtol = kctx->v4_m1 * kctx->rtol_last; 52930f0abf79SStefano Zampini else rtol = kctx->v4_m2 * kctx->rtol_last; 52940f0abf79SStefano Zampini 5295a4598233SStefano Zampini if (kctx->rtol_last_2 > kctx->v4_m3 && kctx->rtol_last > kctx->v4_m3 && kctx->rk_last_2 < kctx->v4_p1 && kctx->rk_last < kctx->v4_p1) rtol = kctx->v4_m4 * kctx->rtol_last; 52960f0abf79SStefano Zampini kctx->rtol_last_2 = kctx->rtol_last; 52970f0abf79SStefano Zampini kctx->rk_last_2 = kctx->rk_last; 52980f0abf79SStefano Zampini kctx->rk_last = rk; 52990fdf79fbSJacob Faibussowitsch } 53000f0abf79SStefano Zampini } 53010f0abf79SStefano Zampini /* safeguard: avoid rtol greater than rtol_max */ 530271f87433Sdalcinl rtol = PetscMin(rtol, kctx->rtol_max); 53039566063dSJacob Faibussowitsch PetscCall(KSPSetTolerances(ksp, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT)); 530463a3b9bcSJacob Faibussowitsch PetscCall(PetscInfo(snes, "iter %" PetscInt_FMT ", Eisenstat-Walker (version %" PetscInt_FMT ") KSP rtol=%g\n", snes->iter, kctx->version, (double)rtol)); 53053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 530671f87433Sdalcinl } 530771f87433Sdalcinl 5308d71ae5a4SJacob Faibussowitsch PetscErrorCode KSPPostSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes) 5309d71ae5a4SJacob Faibussowitsch { 5310fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW *)snes->kspconvctx; 531171f87433Sdalcinl PCSide pcside; 531271f87433Sdalcinl Vec lres; 531371f87433Sdalcinl 531471f87433Sdalcinl PetscFunctionBegin; 53153ba16761SJacob Faibussowitsch if (!snes->ksp_ewconv) PetscFunctionReturn(PETSC_SUCCESS); 53169566063dSJacob Faibussowitsch PetscCall(KSPGetTolerances(ksp, &kctx->rtol_last, NULL, NULL, NULL)); 531771dbe336SPeter Brune kctx->norm_last = snes->norm; 53180f0abf79SStefano Zampini if (kctx->version == 1 || kctx->version == 4) { 53194f00ce20SMatthew G. Knepley PC pc; 53200f0abf79SStefano Zampini PetscBool getRes; 53214f00ce20SMatthew G. Knepley 53229566063dSJacob Faibussowitsch PetscCall(KSPGetPC(ksp, &pc)); 53230f0abf79SStefano Zampini PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCNONE, &getRes)); 53240f0abf79SStefano Zampini if (!getRes) { 53250f0abf79SStefano Zampini KSPNormType normtype; 53260f0abf79SStefano Zampini 53270f0abf79SStefano Zampini PetscCall(KSPGetNormType(ksp, &normtype)); 53280f0abf79SStefano Zampini getRes = (PetscBool)(normtype == KSP_NORM_UNPRECONDITIONED); 53290f0abf79SStefano Zampini } 53309566063dSJacob Faibussowitsch PetscCall(KSPGetPCSide(ksp, &pcside)); 53310f0abf79SStefano Zampini if (pcside == PC_RIGHT || getRes) { /* KSP residual is true linear residual */ 53329566063dSJacob Faibussowitsch PetscCall(KSPGetResidualNorm(ksp, &kctx->lresid_last)); 533371f87433Sdalcinl } else { 533471f87433Sdalcinl /* KSP residual is preconditioned residual */ 533571f87433Sdalcinl /* compute true linear residual norm */ 53360f0abf79SStefano Zampini Mat J; 53370f0abf79SStefano Zampini PetscCall(KSPGetOperators(ksp, &J, NULL)); 53389566063dSJacob Faibussowitsch PetscCall(VecDuplicate(b, &lres)); 53390f0abf79SStefano Zampini PetscCall(MatMult(J, x, lres)); 53409566063dSJacob Faibussowitsch PetscCall(VecAYPX(lres, -1.0, b)); 53419566063dSJacob Faibussowitsch PetscCall(VecNorm(lres, NORM_2, &kctx->lresid_last)); 53429566063dSJacob Faibussowitsch PetscCall(VecDestroy(&lres)); 534371f87433Sdalcinl } 534471f87433Sdalcinl } 53453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 534671f87433Sdalcinl } 534771f87433Sdalcinl 5348d4211eb9SBarry Smith /*@ 5349f6dfbefdSBarry Smith SNESGetKSP - Returns the `KSP` context for a `SNES` solver. 5350d4211eb9SBarry Smith 5351f6dfbefdSBarry Smith Not Collective, but if snes is parallel, then ksp is parallel 5352d4211eb9SBarry Smith 5353d4211eb9SBarry Smith Input Parameter: 5354f6dfbefdSBarry Smith . snes - the `SNES` context 5355d4211eb9SBarry Smith 5356d4211eb9SBarry Smith Output Parameter: 5357f6dfbefdSBarry Smith . ksp - the `KSP` context 5358d4211eb9SBarry Smith 5359dc4c0fb0SBarry Smith Level: beginner 5360dc4c0fb0SBarry Smith 5361d4211eb9SBarry Smith Notes: 5362f6dfbefdSBarry Smith The user can then directly manipulate the `KSP` context to set various 5363d4211eb9SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 5364f6dfbefdSBarry Smith `PC` contexts as well. 5365f6dfbefdSBarry Smith 5366f6dfbefdSBarry Smith Some `SNESType`s do not use a `KSP` but a `KSP` is still returned by this function 5367d4211eb9SBarry Smith 5368dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `KSP`, `PC`, `KSPGetPC()`, `SNESCreate()`, `KSPCreate()`, `SNESSetKSP()` 5369d4211eb9SBarry Smith @*/ 5370d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetKSP(SNES snes, KSP *ksp) 5371d71ae5a4SJacob Faibussowitsch { 537271f87433Sdalcinl PetscFunctionBegin; 5373d4211eb9SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5374d4211eb9SBarry Smith PetscValidPointer(ksp, 2); 5375d4211eb9SBarry Smith 5376d4211eb9SBarry Smith if (!snes->ksp) { 53779566063dSJacob Faibussowitsch PetscCall(KSPCreate(PetscObjectComm((PetscObject)snes), &snes->ksp)); 53789566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)snes->ksp, (PetscObject)snes, 1)); 5379d4211eb9SBarry Smith 53809566063dSJacob Faibussowitsch PetscCall(KSPSetPreSolve(snes->ksp, (PetscErrorCode(*)(KSP, Vec, Vec, void *))KSPPreSolve_SNESEW, snes)); 53819566063dSJacob Faibussowitsch PetscCall(KSPSetPostSolve(snes->ksp, (PetscErrorCode(*)(KSP, Vec, Vec, void *))KSPPostSolve_SNESEW, snes)); 5382a5c2985bSBarry Smith 53839566063dSJacob Faibussowitsch PetscCall(KSPMonitorSetFromOptions(snes->ksp, "-snes_monitor_ksp", "snes_preconditioned_residual", snes)); 53849566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptions((PetscObject)snes->ksp, ((PetscObject)snes)->options)); 5385d4211eb9SBarry Smith } 5386d4211eb9SBarry Smith *ksp = snes->ksp; 53873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 538871f87433Sdalcinl } 53896c699258SBarry Smith 5390af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 53916c699258SBarry Smith /*@ 5392f6dfbefdSBarry Smith SNESSetDM - Sets the `DM` that may be used by some nonlinear solvers or their underlying preconditioners 53936c699258SBarry Smith 5394c3339decSBarry Smith Logically Collective 53956c699258SBarry Smith 53966c699258SBarry Smith Input Parameters: 53972a808120SBarry Smith + snes - the nonlinear solver context 5398dc4c0fb0SBarry Smith - dm - the dm, cannot be `NULL` 5399dc4c0fb0SBarry Smith 5400dc4c0fb0SBarry Smith Level: intermediate 54016c699258SBarry Smith 5402f6dfbefdSBarry Smith Note: 5403f6dfbefdSBarry Smith A `DM` can only be used for solving one problem at a time because information about the problem is stored on the `DM`, 5404f6dfbefdSBarry Smith even when not using interfaces like `DMSNESSetFunction()`. Use `DMClone()` to get a distinct `DM` when solving different 5405e03a659cSJed Brown problems using the same function space. 5406e03a659cSJed Brown 5407dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `DM`, `SNESGetDM()`, `KSPSetDM()`, `KSPGetDM()` 54086c699258SBarry Smith @*/ 5409d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetDM(SNES snes, DM dm) 5410d71ae5a4SJacob Faibussowitsch { 5411345fed2cSBarry Smith KSP ksp; 5412942e3340SBarry Smith DMSNES sdm; 54136c699258SBarry Smith 54146c699258SBarry Smith PetscFunctionBegin; 54150700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 54162a808120SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 54179566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 5418942e3340SBarry Smith if (snes->dm) { /* Move the DMSNES context over to the new DM unless the new DM already has one */ 541951f4b3c7SToby Isaac if (snes->dm->dmsnes && !dm->dmsnes) { 54209566063dSJacob Faibussowitsch PetscCall(DMCopyDMSNES(snes->dm, dm)); 54219566063dSJacob Faibussowitsch PetscCall(DMGetDMSNES(snes->dm, &sdm)); 5422f5af7f23SKarl Rupp if (sdm->originaldm == snes->dm) sdm->originaldm = dm; /* Grant write privileges to the replacement DM */ 54236cab3a1bSJed Brown } 54249566063dSJacob Faibussowitsch PetscCall(DMCoarsenHookRemove(snes->dm, DMCoarsenHook_SNESVecSol, DMRestrictHook_SNESVecSol, snes)); 54259566063dSJacob Faibussowitsch PetscCall(DMDestroy(&snes->dm)); 54266cab3a1bSJed Brown } 54276c699258SBarry Smith snes->dm = dm; 5428116d1032SJed Brown snes->dmAuto = PETSC_FALSE; 5429f5af7f23SKarl Rupp 54309566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, &ksp)); 54319566063dSJacob Faibussowitsch PetscCall(KSPSetDM(ksp, dm)); 54329566063dSJacob Faibussowitsch PetscCall(KSPSetDMActive(ksp, PETSC_FALSE)); 5433efd4aadfSBarry Smith if (snes->npc) { 54349566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes->npc, snes->dm)); 54359566063dSJacob Faibussowitsch PetscCall(SNESSetNPCSide(snes, snes->npcside)); 54362c155ee1SBarry Smith } 54373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 54386c699258SBarry Smith } 54396c699258SBarry Smith 54406c699258SBarry Smith /*@ 5441f6dfbefdSBarry Smith SNESGetDM - Gets the `DM` that may be used by some preconditioners 54426c699258SBarry Smith 5443f6dfbefdSBarry Smith Not Collective but dm obtained is parallel on snes 54446c699258SBarry Smith 54456c699258SBarry Smith Input Parameter: 54466c699258SBarry Smith . snes - the preconditioner context 54476c699258SBarry Smith 54486c699258SBarry Smith Output Parameter: 54496c699258SBarry Smith . dm - the dm 54506c699258SBarry Smith 54516c699258SBarry Smith Level: intermediate 54526c699258SBarry Smith 5453dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `DM`, `SNESSetDM()`, `KSPSetDM()`, `KSPGetDM()` 54546c699258SBarry Smith @*/ 5455d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetDM(SNES snes, DM *dm) 5456d71ae5a4SJacob Faibussowitsch { 54576c699258SBarry Smith PetscFunctionBegin; 54580700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 54596cab3a1bSJed Brown if (!snes->dm) { 54609566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)snes), &snes->dm)); 5461116d1032SJed Brown snes->dmAuto = PETSC_TRUE; 54626cab3a1bSJed Brown } 54636c699258SBarry Smith *dm = snes->dm; 54643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 54656c699258SBarry Smith } 54660807856dSBarry Smith 546731823bd8SMatthew G Knepley /*@ 5468be95d8f1SBarry Smith SNESSetNPC - Sets the nonlinear preconditioner to be used. 546931823bd8SMatthew G Knepley 5470c3339decSBarry Smith Collective 547131823bd8SMatthew G Knepley 547231823bd8SMatthew G Knepley Input Parameters: 5473f6dfbefdSBarry Smith + snes - iterative context obtained from `SNESCreate()` 5474f6dfbefdSBarry Smith - npc - the preconditioner object 547531823bd8SMatthew G Knepley 5476dc4c0fb0SBarry Smith Level: developer 5477dc4c0fb0SBarry Smith 547831823bd8SMatthew G Knepley Notes: 5479f6dfbefdSBarry Smith Use `SNESGetNPC()` to retrieve the preconditioner context (for example, 548031823bd8SMatthew G Knepley to configure it using the API). 548131823bd8SMatthew G Knepley 5482f6dfbefdSBarry Smith Only some `SNESType` can use a nonlinear preconditioner 5483f6dfbefdSBarry Smith 5484dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESNGS`, `SNESFAS`, `SNESGetNPC()`, `SNESHasNPC()` 548531823bd8SMatthew G Knepley @*/ 5486d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetNPC(SNES snes, SNES npc) 5487d71ae5a4SJacob Faibussowitsch { 548831823bd8SMatthew G Knepley PetscFunctionBegin; 548931823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5490f6dfbefdSBarry Smith PetscValidHeaderSpecific(npc, SNES_CLASSID, 2); 5491f6dfbefdSBarry Smith PetscCheckSameComm(snes, 1, npc, 2); 5492f6dfbefdSBarry Smith PetscCall(PetscObjectReference((PetscObject)npc)); 54939566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&snes->npc)); 5494f6dfbefdSBarry Smith snes->npc = npc; 54953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 549631823bd8SMatthew G Knepley } 549731823bd8SMatthew G Knepley 549831823bd8SMatthew G Knepley /*@ 5499f6dfbefdSBarry Smith SNESGetNPC - Gets a nonlinear preconditioning solver SNES` to be used to precondition the original nonlinear solver. 550031823bd8SMatthew G Knepley 5501f6dfbefdSBarry Smith Not Collective; but any changes to the obtained the npc object must be applied collectively 550231823bd8SMatthew G Knepley 550331823bd8SMatthew G Knepley Input Parameter: 5504f6dfbefdSBarry Smith . snes - iterative context obtained from `SNESCreate()` 550531823bd8SMatthew G Knepley 550631823bd8SMatthew G Knepley Output Parameter: 5507f6dfbefdSBarry Smith . npc - preconditioner context 550831823bd8SMatthew G Knepley 5509f6dfbefdSBarry Smith Options Database Key: 5510f6dfbefdSBarry Smith . -npc_snes_type <type> - set the type of the `SNES` to use as the nonlinear preconditioner 5511b5badacbSBarry Smith 5512dc4c0fb0SBarry Smith Level: developer 5513dc4c0fb0SBarry Smith 551495452b02SPatrick Sanan Notes: 5515f6dfbefdSBarry Smith If a `SNES` was previously set with `SNESSetNPC()` then that value is returned, otherwise a new `SNES` object is created. 5516be95d8f1SBarry Smith 5517f6dfbefdSBarry Smith The (preconditioner) `SNES` returned automatically inherits the same nonlinear function and Jacobian supplied to the original 5518f6dfbefdSBarry Smith `SNES` 5519951fe5abSBarry Smith 5520dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetNPC()`, `SNESHasNPC()`, `SNES`, `SNESCreate()` 552131823bd8SMatthew G Knepley @*/ 5522d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetNPC(SNES snes, SNES *pc) 5523d71ae5a4SJacob Faibussowitsch { 5524a64e098fSPeter Brune const char *optionsprefix; 552531823bd8SMatthew G Knepley 552631823bd8SMatthew G Knepley PetscFunctionBegin; 552731823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 552831823bd8SMatthew G Knepley PetscValidPointer(pc, 2); 5529efd4aadfSBarry Smith if (!snes->npc) { 5530ec785e5bSStefano Zampini void *ctx; 5531ec785e5bSStefano Zampini 55329566063dSJacob Faibussowitsch PetscCall(SNESCreate(PetscObjectComm((PetscObject)snes), &snes->npc)); 55339566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)snes->npc, (PetscObject)snes, 1)); 55349566063dSJacob Faibussowitsch PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix)); 55359566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes->npc, optionsprefix)); 55369566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes->npc, "npc_")); 5537ec785e5bSStefano Zampini PetscCall(SNESGetApplicationContext(snes, &ctx)); 5538ec785e5bSStefano Zampini PetscCall(SNESSetApplicationContext(snes->npc, ctx)); 55399566063dSJacob Faibussowitsch PetscCall(SNESSetCountersReset(snes->npc, PETSC_FALSE)); 554031823bd8SMatthew G Knepley } 5541efd4aadfSBarry Smith *pc = snes->npc; 55423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 554331823bd8SMatthew G Knepley } 554431823bd8SMatthew G Knepley 55453ad1a0b9SPatrick Farrell /*@ 55463ad1a0b9SPatrick Farrell SNESHasNPC - Returns whether a nonlinear preconditioner exists 55473ad1a0b9SPatrick Farrell 55483ad1a0b9SPatrick Farrell Not Collective 55493ad1a0b9SPatrick Farrell 55503ad1a0b9SPatrick Farrell Input Parameter: 5551f6dfbefdSBarry Smith . snes - iterative context obtained from `SNESCreate()` 55523ad1a0b9SPatrick Farrell 55533ad1a0b9SPatrick Farrell Output Parameter: 5554f6dfbefdSBarry Smith . has_npc - whether the `SNES` has an NPC or not 55553ad1a0b9SPatrick Farrell 55563ad1a0b9SPatrick Farrell Level: developer 55573ad1a0b9SPatrick Farrell 5558dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESSetNPC()`, `SNESGetNPC()` 55593ad1a0b9SPatrick Farrell @*/ 5560d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESHasNPC(SNES snes, PetscBool *has_npc) 5561d71ae5a4SJacob Faibussowitsch { 55623ad1a0b9SPatrick Farrell PetscFunctionBegin; 55633ad1a0b9SPatrick Farrell PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5564efd4aadfSBarry Smith *has_npc = (PetscBool)(snes->npc ? PETSC_TRUE : PETSC_FALSE); 55653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 55663ad1a0b9SPatrick Farrell } 55673ad1a0b9SPatrick Farrell 5568c40d0f55SPeter Brune /*@ 5569be95d8f1SBarry Smith SNESSetNPCSide - Sets the preconditioning side. 5570c40d0f55SPeter Brune 5571c3339decSBarry Smith Logically Collective 5572c40d0f55SPeter Brune 5573c40d0f55SPeter Brune Input Parameter: 5574f6dfbefdSBarry Smith . snes - iterative context obtained from `SNESCreate()` 5575c40d0f55SPeter Brune 5576c40d0f55SPeter Brune Output Parameter: 5577c40d0f55SPeter Brune . side - the preconditioning side, where side is one of 5578c40d0f55SPeter Brune .vb 55792d547940SBarry Smith PC_LEFT - left preconditioning 55802d547940SBarry Smith PC_RIGHT - right preconditioning (default for most nonlinear solvers) 5581c40d0f55SPeter Brune .ve 5582c40d0f55SPeter Brune 5583f6dfbefdSBarry Smith Options Database Key: 558467b8a455SSatish Balay . -snes_npc_side <right,left> - nonlinear preconditioner side 5585c40d0f55SPeter Brune 5586dc4c0fb0SBarry Smith Level: intermediate 5587dc4c0fb0SBarry Smith 5588f6dfbefdSBarry Smith Note: 5589f6dfbefdSBarry Smith `SNESNRICHARDSON` and `SNESNCG` only support left preconditioning. 55902d547940SBarry Smith 5591dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESType`, `SNESGetNPCSide()`, `KSPSetPCSide()` 5592c40d0f55SPeter Brune @*/ 5593d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetNPCSide(SNES snes, PCSide side) 5594d71ae5a4SJacob Faibussowitsch { 5595c40d0f55SPeter Brune PetscFunctionBegin; 5596c40d0f55SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5597c40d0f55SPeter Brune PetscValidLogicalCollectiveEnum(snes, side, 2); 5598b552625fSStefano Zampini if (side == PC_SIDE_DEFAULT) side = PC_RIGHT; 559954c59aa7SJacob Faibussowitsch PetscCheck((side == PC_LEFT) || (side == PC_RIGHT), PetscObjectComm((PetscObject)snes), PETSC_ERR_ARG_WRONG, "Only PC_LEFT and PC_RIGHT are supported"); 5600efd4aadfSBarry Smith snes->npcside = side; 56013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5602c40d0f55SPeter Brune } 5603c40d0f55SPeter Brune 5604c40d0f55SPeter Brune /*@ 5605be95d8f1SBarry Smith SNESGetNPCSide - Gets the preconditioning side. 5606c40d0f55SPeter Brune 5607c40d0f55SPeter Brune Not Collective 5608c40d0f55SPeter Brune 5609c40d0f55SPeter Brune Input Parameter: 5610f6dfbefdSBarry Smith . snes - iterative context obtained from `SNESCreate()` 5611c40d0f55SPeter Brune 5612c40d0f55SPeter Brune Output Parameter: 5613c40d0f55SPeter Brune . side - the preconditioning side, where side is one of 5614c40d0f55SPeter Brune .vb 5615f6dfbefdSBarry Smith `PC_LEFT` - left preconditioning 5616f6dfbefdSBarry Smith `PC_RIGHT` - right preconditioning (default for most nonlinear solvers) 5617c40d0f55SPeter Brune .ve 5618c40d0f55SPeter Brune 5619c40d0f55SPeter Brune Level: intermediate 5620c40d0f55SPeter Brune 5621dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNES`, `SNESSetNPCSide()`, `KSPGetPCSide()` 5622c40d0f55SPeter Brune @*/ 5623d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetNPCSide(SNES snes, PCSide *side) 5624d71ae5a4SJacob Faibussowitsch { 5625c40d0f55SPeter Brune PetscFunctionBegin; 5626c40d0f55SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5627c40d0f55SPeter Brune PetscValidPointer(side, 2); 5628efd4aadfSBarry Smith *side = snes->npcside; 56293ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5630c40d0f55SPeter Brune } 5631c40d0f55SPeter Brune 56329e764e56SPeter Brune /*@ 5633f6dfbefdSBarry Smith SNESSetLineSearch - Sets the linesearch on the `SNES` instance. 56349e764e56SPeter Brune 5635c3339decSBarry Smith Collective 56369e764e56SPeter Brune 56379e764e56SPeter Brune Input Parameters: 5638f6dfbefdSBarry Smith + snes - iterative context obtained from `SNESCreate()` 56399e764e56SPeter Brune - linesearch - the linesearch object 56409e764e56SPeter Brune 5641dc4c0fb0SBarry Smith Level: developer 5642dc4c0fb0SBarry Smith 5643f6dfbefdSBarry Smith Note: 5644f6dfbefdSBarry Smith Use `SNESGetLineSearch()` to retrieve the preconditioner context (for example, 56459e764e56SPeter Brune to configure it using the API). 56469e764e56SPeter Brune 5647dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESGetLineSearch()` 56489e764e56SPeter Brune @*/ 5649d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESSetLineSearch(SNES snes, SNESLineSearch linesearch) 5650d71ae5a4SJacob Faibussowitsch { 56519e764e56SPeter Brune PetscFunctionBegin; 56529e764e56SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5653f1c6b773SPeter Brune PetscValidHeaderSpecific(linesearch, SNESLINESEARCH_CLASSID, 2); 56549e764e56SPeter Brune PetscCheckSameComm(snes, 1, linesearch, 2); 56559566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)linesearch)); 56569566063dSJacob Faibussowitsch PetscCall(SNESLineSearchDestroy(&snes->linesearch)); 5657f5af7f23SKarl Rupp 56589e764e56SPeter Brune snes->linesearch = linesearch; 5659f5af7f23SKarl Rupp 56603ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 56619e764e56SPeter Brune } 56629e764e56SPeter Brune 5663a34ceb2aSJed Brown /*@ 5664dc4c0fb0SBarry Smith SNESGetLineSearch - Returns the line search context set with `SNESSetLineSearch()` 5665f6dfbefdSBarry Smith or creates a default line search instance associated with the `SNES` and returns it. 56669e764e56SPeter Brune 56679e764e56SPeter Brune Not Collective 56689e764e56SPeter Brune 56699e764e56SPeter Brune Input Parameter: 5670f6dfbefdSBarry Smith . snes - iterative context obtained from `SNESCreate()` 56719e764e56SPeter Brune 56729e764e56SPeter Brune Output Parameter: 56739e764e56SPeter Brune . linesearch - linesearch context 56749e764e56SPeter Brune 5675162e0bf5SPeter Brune Level: beginner 56769e764e56SPeter Brune 5677dc4c0fb0SBarry Smith .seealso: [](chapter_snes), `SNESLineSearch`, `SNESSetLineSearch()`, `SNESLineSearchCreate()` 56789e764e56SPeter Brune @*/ 5679d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESGetLineSearch(SNES snes, SNESLineSearch *linesearch) 5680d71ae5a4SJacob Faibussowitsch { 56819e764e56SPeter Brune const char *optionsprefix; 56829e764e56SPeter Brune 56839e764e56SPeter Brune PetscFunctionBegin; 56849e764e56SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 56859e764e56SPeter Brune PetscValidPointer(linesearch, 2); 56869e764e56SPeter Brune if (!snes->linesearch) { 56879566063dSJacob Faibussowitsch PetscCall(SNESGetOptionsPrefix(snes, &optionsprefix)); 56889566063dSJacob Faibussowitsch PetscCall(SNESLineSearchCreate(PetscObjectComm((PetscObject)snes), &snes->linesearch)); 56899566063dSJacob Faibussowitsch PetscCall(SNESLineSearchSetSNES(snes->linesearch, snes)); 56909566063dSJacob Faibussowitsch PetscCall(SNESLineSearchAppendOptionsPrefix(snes->linesearch, optionsprefix)); 56919566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)snes->linesearch, (PetscObject)snes, 1)); 56929e764e56SPeter Brune } 56939e764e56SPeter Brune *linesearch = snes->linesearch; 56943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 56959e764e56SPeter Brune } 5696