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; 1394db00ebSBarry Smith PetscLogEvent SNES_Solve, SNES_FunctionEval, SNES_JacobianEval, SNES_NGSEval, SNES_NGSFuncEval, SNES_NPCSolve, SNES_ObjectiveEval; 14a09944afSBarry Smith 15e113a28aSBarry Smith /*@ 16e113a28aSBarry Smith SNESSetErrorIfNotConverged - Causes SNESSolve() to generate an error if the solver has not converged. 17e113a28aSBarry Smith 183f9fe445SBarry Smith Logically Collective on SNES 19e113a28aSBarry Smith 20e113a28aSBarry Smith Input Parameters: 21e113a28aSBarry Smith + snes - iterative context obtained from SNESCreate() 22e113a28aSBarry Smith - flg - PETSC_TRUE indicates you want the error generated 23e113a28aSBarry Smith 24e113a28aSBarry Smith Options database keys: 25e113a28aSBarry Smith . -snes_error_if_not_converged : this takes an optional truth value (0/1/no/yes/true/false) 26e113a28aSBarry Smith 27e113a28aSBarry Smith Level: intermediate 28e113a28aSBarry Smith 29e113a28aSBarry Smith Notes: 30e113a28aSBarry Smith Normally PETSc continues if a linear solver fails to converge, you can call SNESGetConvergedReason() after a SNESSolve() 31e113a28aSBarry Smith to determine if it has converged. 32e113a28aSBarry Smith 33e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero 34e113a28aSBarry Smith 35e113a28aSBarry Smith .seealso: SNESGetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged() 36e113a28aSBarry Smith @*/ 377087cfbeSBarry Smith PetscErrorCode SNESSetErrorIfNotConverged(SNES snes,PetscBool flg) 38e113a28aSBarry Smith { 39e113a28aSBarry Smith PetscFunctionBegin; 40e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 41acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flg,2); 42e113a28aSBarry Smith snes->errorifnotconverged = flg; 43e113a28aSBarry Smith PetscFunctionReturn(0); 44e113a28aSBarry Smith } 45e113a28aSBarry Smith 46e113a28aSBarry Smith /*@ 47e113a28aSBarry Smith SNESGetErrorIfNotConverged - Will SNESSolve() generate an error if the solver does not converge? 48e113a28aSBarry Smith 49e113a28aSBarry Smith Not Collective 50e113a28aSBarry Smith 51e113a28aSBarry Smith Input Parameter: 52e113a28aSBarry Smith . snes - iterative context obtained from SNESCreate() 53e113a28aSBarry Smith 54e113a28aSBarry Smith Output Parameter: 55e113a28aSBarry Smith . flag - PETSC_TRUE if it will generate an error, else PETSC_FALSE 56e113a28aSBarry Smith 57e113a28aSBarry Smith Level: intermediate 58e113a28aSBarry Smith 59e113a28aSBarry Smith .keywords: SNES, set, initial guess, nonzero 60e113a28aSBarry Smith 61e113a28aSBarry Smith .seealso: SNESSetErrorIfNotConverged(), KSPGetErrorIfNotConverged(), KSPSetErrorIFNotConverged() 62e113a28aSBarry Smith @*/ 637087cfbeSBarry Smith PetscErrorCode SNESGetErrorIfNotConverged(SNES snes,PetscBool *flag) 64e113a28aSBarry Smith { 65e113a28aSBarry Smith PetscFunctionBegin; 66e113a28aSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 67e113a28aSBarry Smith PetscValidPointer(flag,2); 68e113a28aSBarry Smith *flag = snes->errorifnotconverged; 69e113a28aSBarry Smith PetscFunctionReturn(0); 70e113a28aSBarry Smith } 71e113a28aSBarry Smith 724fc747eaSLawrence Mitchell /*@ 734fc747eaSLawrence Mitchell SNESSetAlwaysComputesFinalResidual - does the SNES always compute the residual at the final solution? 744fc747eaSLawrence Mitchell 754fc747eaSLawrence Mitchell Logically Collective on SNES 764fc747eaSLawrence Mitchell 774fc747eaSLawrence Mitchell Input Parameters: 784fc747eaSLawrence Mitchell + snes - the shell SNES 794fc747eaSLawrence Mitchell - flg - is the residual computed? 804fc747eaSLawrence Mitchell 814fc747eaSLawrence Mitchell Level: advanced 824fc747eaSLawrence Mitchell 834fc747eaSLawrence Mitchell .seealso: SNESGetAlwaysComputesFinalResidual() 844fc747eaSLawrence Mitchell @*/ 854fc747eaSLawrence Mitchell PetscErrorCode SNESSetAlwaysComputesFinalResidual(SNES snes, PetscBool flg) 864fc747eaSLawrence Mitchell { 874fc747eaSLawrence Mitchell PetscFunctionBegin; 884fc747eaSLawrence Mitchell PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 894fc747eaSLawrence Mitchell snes->alwayscomputesfinalresidual = flg; 904fc747eaSLawrence Mitchell PetscFunctionReturn(0); 914fc747eaSLawrence Mitchell } 924fc747eaSLawrence Mitchell 934fc747eaSLawrence Mitchell /*@ 944fc747eaSLawrence Mitchell SNESGetAlwaysComputesFinalResidual - does the SNES always compute the residual at the final solution? 954fc747eaSLawrence Mitchell 964fc747eaSLawrence Mitchell Logically Collective on SNES 974fc747eaSLawrence Mitchell 984fc747eaSLawrence Mitchell Input Parameter: 994fc747eaSLawrence Mitchell . snes - the shell SNES 1004fc747eaSLawrence Mitchell 1014fc747eaSLawrence Mitchell Output Parameter: 1024fc747eaSLawrence Mitchell . flg - is the residual computed? 1034fc747eaSLawrence Mitchell 1044fc747eaSLawrence Mitchell Level: advanced 1054fc747eaSLawrence Mitchell 1064fc747eaSLawrence Mitchell .seealso: SNESSetAlwaysComputesFinalResidual() 1074fc747eaSLawrence Mitchell @*/ 1084fc747eaSLawrence Mitchell PetscErrorCode SNESGetAlwaysComputesFinalResidual(SNES snes, PetscBool *flg) 1094fc747eaSLawrence Mitchell { 1104fc747eaSLawrence Mitchell PetscFunctionBegin; 1114fc747eaSLawrence Mitchell PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1124fc747eaSLawrence Mitchell *flg = snes->alwayscomputesfinalresidual; 1134fc747eaSLawrence Mitchell PetscFunctionReturn(0); 1144fc747eaSLawrence Mitchell } 1154fc747eaSLawrence Mitchell 116e725d27bSBarry Smith /*@ 117bf388a1fSBarry Smith SNESSetFunctionDomainError - tells SNES that the input vector to your SNESFunction is not 1184936397dSBarry Smith in the functions domain. For example, negative pressure. 1194936397dSBarry Smith 1203f9fe445SBarry Smith Logically Collective on SNES 1214936397dSBarry Smith 1224936397dSBarry Smith Input Parameters: 1236a388c36SPeter Brune . snes - the SNES context 1244936397dSBarry Smith 12528529972SSatish Balay Level: advanced 1264936397dSBarry Smith 1274936397dSBarry Smith .keywords: SNES, view 1284936397dSBarry Smith 129bf388a1fSBarry Smith .seealso: SNESCreate(), SNESSetFunction(), SNESFunction 1304936397dSBarry Smith @*/ 1317087cfbeSBarry Smith PetscErrorCode SNESSetFunctionDomainError(SNES snes) 1324936397dSBarry Smith { 1334936397dSBarry Smith PetscFunctionBegin; 1340700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 135422a814eSBarry Smith if (snes->errorifnotconverged) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"User code indicates input vector is not in the function domain"); 1364936397dSBarry Smith snes->domainerror = PETSC_TRUE; 1374936397dSBarry Smith PetscFunctionReturn(0); 1384936397dSBarry Smith } 1394936397dSBarry Smith 1406a388c36SPeter Brune /*@ 14107b62357SFande Kong SNESSetJacobianDomainError - tells SNES that computeJacobian does not make sense any more. For example there is a negative element transformation. 14207b62357SFande Kong 14307b62357SFande Kong Logically Collective on SNES 14407b62357SFande Kong 14507b62357SFande Kong Input Parameters: 14607b62357SFande Kong . snes - the SNES context 14707b62357SFande Kong 14807b62357SFande Kong Level: advanced 14907b62357SFande Kong 15007b62357SFande Kong .keywords: SNES, view 15107b62357SFande Kong 15207b62357SFande Kong .seealso: SNESCreate(), SNESSetFunction(), SNESFunction(), SNESSetFunctionDomainError() 15307b62357SFande Kong @*/ 15407b62357SFande Kong PetscErrorCode SNESSetJacobianDomainError(SNES snes) 15507b62357SFande Kong { 15607b62357SFande Kong PetscFunctionBegin; 15707b62357SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15807b62357SFande Kong if (snes->errorifnotconverged) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"User code indicates computeJacobian does not make sense"); 15907b62357SFande Kong snes->jacobiandomainerror = PETSC_TRUE; 16007b62357SFande Kong PetscFunctionReturn(0); 16107b62357SFande Kong } 16207b62357SFande Kong 16307b62357SFande Kong /*@ 164*b351a90bSFande Kong SNESSetCheckJacobianDomainError - if or not to check jacobian domain error after each Jacobian evaluation. By default, we check Jacobian domain error 165*b351a90bSFande Kong in the debug mode, and do not check it in the optimized mode. 166*b351a90bSFande Kong 167*b351a90bSFande Kong Logically Collective on SNES 168*b351a90bSFande Kong 169*b351a90bSFande Kong Input Parameters: 170*b351a90bSFande Kong . snes - the SNES context 171*b351a90bSFande Kong . flg - indicates if or not to check jacobian domain error after each Jacobian evaluation 172*b351a90bSFande Kong 173*b351a90bSFande Kong Level: advanced 174*b351a90bSFande Kong 175*b351a90bSFande Kong .keywords: SNES, view 176*b351a90bSFande Kong 177*b351a90bSFande Kong .seealso: SNESCreate(), SNESSetFunction(), SNESFunction(), SNESSetFunctionDomainError() 178*b351a90bSFande Kong @*/ 179*b351a90bSFande Kong PetscErrorCode SNESSetCheckJacobianDomainError(SNES snes, PetscBool flg) 180*b351a90bSFande Kong { 181*b351a90bSFande Kong PetscFunctionBegin; 182*b351a90bSFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 183*b351a90bSFande Kong snes->checkjacdomainerror = flg; 184*b351a90bSFande Kong PetscFunctionReturn(0); 185*b351a90bSFande Kong } 186*b351a90bSFande Kong 187*b351a90bSFande Kong /*@ 188c77b2880SPeter Brune SNESGetFunctionDomainError - Gets the status of the domain error after a call to SNESComputeFunction; 1896a388c36SPeter Brune 1906a388c36SPeter Brune Logically Collective on SNES 1916a388c36SPeter Brune 1926a388c36SPeter Brune Input Parameters: 1936a388c36SPeter Brune . snes - the SNES context 1946a388c36SPeter Brune 1956a388c36SPeter Brune Output Parameters: 196bf388a1fSBarry Smith . domainerror - Set to PETSC_TRUE if there's a domain error; PETSC_FALSE otherwise. 1976a388c36SPeter Brune 1986a388c36SPeter Brune Level: advanced 1996a388c36SPeter Brune 2006a388c36SPeter Brune .keywords: SNES, view 2016a388c36SPeter Brune 202bf388a1fSBarry Smith .seealso: SNESSetFunctionDomainError(), SNESComputeFunction() 2036a388c36SPeter Brune @*/ 2046a388c36SPeter Brune PetscErrorCode SNESGetFunctionDomainError(SNES snes, PetscBool *domainerror) 2056a388c36SPeter Brune { 2066a388c36SPeter Brune PetscFunctionBegin; 2076a388c36SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2086a388c36SPeter Brune PetscValidPointer(domainerror, 2); 2096a388c36SPeter Brune *domainerror = snes->domainerror; 2106a388c36SPeter Brune PetscFunctionReturn(0); 2116a388c36SPeter Brune } 2126a388c36SPeter Brune 21307b62357SFande Kong /*@ 21407b62357SFande Kong SNESGetJacobianDomainError - Gets the status of the Jacobian domain error after a call to SNESComputeJacobian; 21507b62357SFande Kong 21607b62357SFande Kong Logically Collective on SNES 21707b62357SFande Kong 21807b62357SFande Kong Input Parameters: 21907b62357SFande Kong . snes - the SNES context 22007b62357SFande Kong 22107b62357SFande Kong Output Parameters: 22207b62357SFande Kong . domainerror - Set to PETSC_TRUE if there's a jacobian domain error; PETSC_FALSE otherwise. 22307b62357SFande Kong 22407b62357SFande Kong Level: advanced 22507b62357SFande Kong 22607b62357SFande Kong .keywords: SNES, view 22707b62357SFande Kong 22807b62357SFande Kong .seealso: SNESSetFunctionDomainError(), SNESComputeFunction(),SNESGetFunctionDomainError() 22907b62357SFande Kong @*/ 23007b62357SFande Kong PetscErrorCode SNESGetJacobianDomainError(SNES snes, PetscBool *domainerror) 23107b62357SFande Kong { 23207b62357SFande Kong PetscFunctionBegin; 23307b62357SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23407b62357SFande Kong PetscValidPointer(domainerror, 2); 23507b62357SFande Kong *domainerror = snes->jacobiandomainerror; 23607b62357SFande Kong PetscFunctionReturn(0); 23707b62357SFande Kong } 23807b62357SFande Kong 23955849f57SBarry Smith /*@C 24055849f57SBarry Smith SNESLoad - Loads a SNES that has been stored in binary with SNESView(). 24155849f57SBarry Smith 24255849f57SBarry Smith Collective on PetscViewer 24355849f57SBarry Smith 24455849f57SBarry Smith Input Parameters: 24555849f57SBarry Smith + newdm - the newly loaded SNES, this needs to have been created with SNESCreate() or 24655849f57SBarry Smith some related function before a call to SNESLoad(). 24755849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() 24855849f57SBarry Smith 24955849f57SBarry Smith Level: intermediate 25055849f57SBarry Smith 25155849f57SBarry Smith Notes: 25255849f57SBarry Smith The type is determined by the data in the file, any type set into the SNES before this call is ignored. 25355849f57SBarry Smith 25455849f57SBarry Smith Notes for advanced users: 25555849f57SBarry Smith Most users should not need to know the details of the binary storage 25655849f57SBarry Smith format, since SNESLoad() and TSView() completely hide these details. 25755849f57SBarry Smith But for anyone who's interested, the standard binary matrix storage 25855849f57SBarry Smith format is 25955849f57SBarry Smith .vb 26055849f57SBarry Smith has not yet been determined 26155849f57SBarry Smith .ve 26255849f57SBarry Smith 26355849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), SNESView(), MatLoad(), VecLoad() 26455849f57SBarry Smith @*/ 2652d53ad75SBarry Smith PetscErrorCode SNESLoad(SNES snes, PetscViewer viewer) 26655849f57SBarry Smith { 26755849f57SBarry Smith PetscErrorCode ierr; 26855849f57SBarry Smith PetscBool isbinary; 269060da220SMatthew G. Knepley PetscInt classid; 27055849f57SBarry Smith char type[256]; 27155849f57SBarry Smith KSP ksp; 2722d53ad75SBarry Smith DM dm; 2732d53ad75SBarry Smith DMSNES dmsnes; 27455849f57SBarry Smith 27555849f57SBarry Smith PetscFunctionBegin; 2762d53ad75SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 27755849f57SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 27855849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 27955849f57SBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 28055849f57SBarry Smith 281060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr); 282ce94432eSBarry Smith if (classid != SNES_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONG,"Not SNES next in file"); 283060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr); 2842d53ad75SBarry Smith ierr = SNESSetType(snes, type);CHKERRQ(ierr); 2852d53ad75SBarry Smith if (snes->ops->load) { 2862d53ad75SBarry Smith ierr = (*snes->ops->load)(snes,viewer);CHKERRQ(ierr); 287f2c2a1b9SBarry Smith } 2882d53ad75SBarry Smith ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 2892d53ad75SBarry Smith ierr = DMGetDMSNES(dm,&dmsnes);CHKERRQ(ierr); 2902d53ad75SBarry Smith ierr = DMSNESLoad(dmsnes,viewer);CHKERRQ(ierr); 2912d53ad75SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 29255849f57SBarry Smith ierr = KSPLoad(ksp,viewer);CHKERRQ(ierr); 29355849f57SBarry Smith PetscFunctionReturn(0); 29455849f57SBarry Smith } 2956a388c36SPeter Brune 2969804daf3SBarry Smith #include <petscdraw.h> 297e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 298e04113cfSBarry Smith #include <petscviewersaws.h> 299bfb97211SBarry Smith #endif 3008404b7f3SBarry Smith 3017e2c5f70SBarry Smith /*@C 3029b94acceSBarry Smith SNESView - Prints the SNES data structure. 3039b94acceSBarry Smith 3044c49b128SBarry Smith Collective on SNES 305fee21e36SBarry Smith 306c7afd0dbSLois Curfman McInnes Input Parameters: 307c7afd0dbSLois Curfman McInnes + SNES - the SNES context 308c7afd0dbSLois Curfman McInnes - viewer - visualization context 309c7afd0dbSLois Curfman McInnes 3109b94acceSBarry Smith Options Database Key: 311c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 3129b94acceSBarry Smith 3139b94acceSBarry Smith Notes: 3149b94acceSBarry Smith The available visualization contexts include 315b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 316b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 317c8a8ba5cSLois Curfman McInnes output where only the first processor opens 318c8a8ba5cSLois Curfman McInnes the file. All other processors send their 319c8a8ba5cSLois Curfman McInnes data to the first processor to print. 3209b94acceSBarry Smith 3213e081fefSLois Curfman McInnes The user can open an alternative visualization context with 322b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 3239b94acceSBarry Smith 32436851e7fSLois Curfman McInnes Level: beginner 32536851e7fSLois Curfman McInnes 3269b94acceSBarry Smith .keywords: SNES, view 3279b94acceSBarry Smith 328b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 3299b94acceSBarry Smith @*/ 3307087cfbeSBarry Smith PetscErrorCode SNESView(SNES snes,PetscViewer viewer) 3319b94acceSBarry Smith { 332fa9f3622SBarry Smith SNESKSPEW *kctx; 333dfbe8321SBarry Smith PetscErrorCode ierr; 33494b7f48cSBarry Smith KSP ksp; 3357f1410a3SPeter Brune SNESLineSearch linesearch; 33672a02f06SBarry Smith PetscBool iascii,isstring,isbinary,isdraw; 3372d53ad75SBarry Smith DMSNES dmsnes; 338e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 339536b137fSBarry Smith PetscBool issaws; 340bfb97211SBarry Smith #endif 3419b94acceSBarry Smith 3423a40ed3dSBarry Smith PetscFunctionBegin; 3430700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3443050cee2SBarry Smith if (!viewer) { 345ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&viewer);CHKERRQ(ierr); 3463050cee2SBarry Smith } 3470700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 348c9780b6fSBarry Smith PetscCheckSameComm(snes,1,viewer,2); 34974679c65SBarry Smith 350251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 351251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); 35255849f57SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 35372a02f06SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr); 354e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 355536b137fSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 356bfb97211SBarry Smith #endif 35732077d6dSBarry Smith if (iascii) { 358dc0571f2SMatthew G. Knepley SNESNormSchedule normschedule; 3598404b7f3SBarry Smith DM dm; 3608404b7f3SBarry Smith PetscErrorCode (*cJ)(SNES,Vec,Mat,Mat,void*); 3618404b7f3SBarry Smith void *ctx; 362dc0571f2SMatthew G. Knepley 363dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)snes,viewer);CHKERRQ(ierr); 364fce1e034SJed Brown if (!snes->setupcalled) { 365fce1e034SJed Brown ierr = PetscViewerASCIIPrintf(viewer," SNES has not been set up so information may be incomplete\n");CHKERRQ(ierr); 366fce1e034SJed Brown } 367e7788613SBarry Smith if (snes->ops->view) { 368b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 369e7788613SBarry Smith ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 370b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 3710ef38995SBarry Smith } 37277431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 37357622a8eSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%g, absolute=%g, solution=%g\n",(double)snes->rtol,(double)snes->abstol,(double)snes->stol);CHKERRQ(ierr); 374efd4aadfSBarry Smith if (snes->usesksp) { 37577431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr); 376efd4aadfSBarry Smith } 37777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr); 378dc0571f2SMatthew G. Knepley ierr = SNESGetNormSchedule(snes, &normschedule);CHKERRQ(ierr); 379dc0571f2SMatthew G. Knepley if (normschedule > 0) {ierr = PetscViewerASCIIPrintf(viewer," norm schedule %s\n",SNESNormSchedules[normschedule]);CHKERRQ(ierr);} 38017fe4bdfSPeter Brune if (snes->gridsequence) { 38117fe4bdfSPeter Brune ierr = PetscViewerASCIIPrintf(viewer," total number of grid sequence refinements=%D\n",snes->gridsequence);CHKERRQ(ierr); 38217fe4bdfSPeter Brune } 3839b94acceSBarry Smith if (snes->ksp_ewconv) { 384fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 3859b94acceSBarry Smith if (kctx) { 38677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr); 38757622a8eSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," rtol_0=%g, rtol_max=%g, threshold=%g\n",(double)kctx->rtol_0,(double)kctx->rtol_max,(double)kctx->threshold);CHKERRQ(ierr); 38857622a8eSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," gamma=%g, alpha=%g, alpha2=%g\n",(double)kctx->gamma,(double)kctx->alpha,(double)kctx->alpha2);CHKERRQ(ierr); 3899b94acceSBarry Smith } 3909b94acceSBarry Smith } 391eb1f6c34SBarry Smith if (snes->lagpreconditioner == -1) { 392eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is never rebuilt\n");CHKERRQ(ierr); 393eb1f6c34SBarry Smith } else if (snes->lagpreconditioner > 1) { 394eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr); 395eb1f6c34SBarry Smith } 396eb1f6c34SBarry Smith if (snes->lagjacobian == -1) { 397eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is never rebuilt\n");CHKERRQ(ierr); 398eb1f6c34SBarry Smith } else if (snes->lagjacobian > 1) { 39942f4f86dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is rebuilt every %D SNES iterations\n",snes->lagjacobian);CHKERRQ(ierr); 400eb1f6c34SBarry Smith } 4018404b7f3SBarry Smith ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 4028404b7f3SBarry Smith ierr = DMSNESGetJacobian(dm,&cJ,&ctx);CHKERRQ(ierr); 4038404b7f3SBarry Smith if (cJ == SNESComputeJacobianDefault) { 4048404b7f3SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is built using finite differences one column at a time\n");CHKERRQ(ierr); 4058404b7f3SBarry Smith } else if (cJ == SNESComputeJacobianDefaultColor) { 4068404b7f3SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is built using finite differences with coloring\n");CHKERRQ(ierr); 4078404b7f3SBarry Smith } 4080f5bd95cSBarry Smith } else if (isstring) { 409317d6ea6SBarry Smith const char *type; 410454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 411b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 41255849f57SBarry Smith } else if (isbinary) { 41355849f57SBarry Smith PetscInt classid = SNES_FILE_CLASSID; 41455849f57SBarry Smith MPI_Comm comm; 41555849f57SBarry Smith PetscMPIInt rank; 41655849f57SBarry Smith char type[256]; 41755849f57SBarry Smith 41855849f57SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 41955849f57SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 42055849f57SBarry Smith if (!rank) { 42155849f57SBarry Smith ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 42289d949e2SBarry Smith ierr = PetscStrncpy(type,((PetscObject)snes)->type_name,sizeof(type));CHKERRQ(ierr); 42389d949e2SBarry Smith ierr = PetscViewerBinaryWrite(viewer,type,sizeof(type),PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 42455849f57SBarry Smith } 42555849f57SBarry Smith if (snes->ops->view) { 42655849f57SBarry Smith ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 42755849f57SBarry Smith } 42872a02f06SBarry Smith } else if (isdraw) { 42972a02f06SBarry Smith PetscDraw draw; 43072a02f06SBarry Smith char str[36]; 43189fd9fafSBarry Smith PetscReal x,y,bottom,h; 43272a02f06SBarry Smith 43372a02f06SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 43472a02f06SBarry Smith ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr); 435a126751eSBarry Smith ierr = PetscStrncpy(str,"SNES: ",sizeof(str));CHKERRQ(ierr); 436a126751eSBarry Smith ierr = PetscStrlcat(str,((PetscObject)snes)->type_name,sizeof(str));CHKERRQ(ierr); 43751fa3d41SBarry Smith ierr = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLUE,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr); 43889fd9fafSBarry Smith bottom = y - h; 43972a02f06SBarry Smith ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr); 440c4646bacSPeter Brune if (snes->ops->view) { 441c4646bacSPeter Brune ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 442c4646bacSPeter Brune } 443e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 444536b137fSBarry Smith } else if (issaws) { 445d45a07a7SBarry Smith PetscMPIInt rank; 4462657e9d9SBarry Smith const char *name; 447d45a07a7SBarry Smith 4482657e9d9SBarry Smith ierr = PetscObjectGetName((PetscObject)snes,&name);CHKERRQ(ierr); 449d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 450d45a07a7SBarry Smith if (!((PetscObject)snes)->amsmem && !rank) { 451d45a07a7SBarry Smith char dir[1024]; 452d45a07a7SBarry Smith 453e04113cfSBarry Smith ierr = PetscObjectViewSAWs((PetscObject)snes,viewer);CHKERRQ(ierr); 4542657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/its",name);CHKERRQ(ierr); 4552657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,&snes->iter,1,SAWs_READ,SAWs_INT)); 456bfb97211SBarry Smith if (!snes->conv_hist) { 457a0931e03SBarry Smith ierr = SNESSetConvergenceHistory(snes,NULL,NULL,PETSC_DECIDE,PETSC_TRUE);CHKERRQ(ierr); 458bfb97211SBarry Smith } 4592657e9d9SBarry Smith ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/conv_hist",name);CHKERRQ(ierr); 4602657e9d9SBarry Smith PetscStackCallSAWs(SAWs_Register,(dir,snes->conv_hist,10,SAWs_READ,SAWs_DOUBLE)); 461f05ece33SBarry Smith } 462bfb97211SBarry Smith #endif 46372a02f06SBarry Smith } 46472a02f06SBarry Smith if (snes->linesearch) { 4657601faf0SJed Brown ierr = SNESGetLineSearch(snes, &linesearch);CHKERRQ(ierr); 46685928a98SStefano Zampini ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 46772a02f06SBarry Smith ierr = SNESLineSearchView(linesearch, viewer);CHKERRQ(ierr); 46885928a98SStefano Zampini ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 46919bcc07fSBarry Smith } 470efd4aadfSBarry Smith if (snes->npc && snes->usesnpc) { 47185928a98SStefano Zampini ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 472efd4aadfSBarry Smith ierr = SNESView(snes->npc, viewer);CHKERRQ(ierr); 47385928a98SStefano Zampini ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 4744a0c5b0cSMatthew G Knepley } 4752d53ad75SBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 4762d53ad75SBarry Smith ierr = DMGetDMSNES(snes->dm,&dmsnes);CHKERRQ(ierr); 4772d53ad75SBarry Smith ierr = DMSNESView(dmsnes, viewer);CHKERRQ(ierr); 4782d53ad75SBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 4792c155ee1SBarry Smith if (snes->usesksp) { 4802c155ee1SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 48185928a98SStefano Zampini ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 48294b7f48cSBarry Smith ierr = KSPView(ksp,viewer);CHKERRQ(ierr); 48385928a98SStefano Zampini ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 4842c155ee1SBarry Smith } 48572a02f06SBarry Smith if (isdraw) { 48672a02f06SBarry Smith PetscDraw draw; 48772a02f06SBarry Smith ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); 48872a02f06SBarry Smith ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr); 4897f1410a3SPeter Brune } 4903a40ed3dSBarry Smith PetscFunctionReturn(0); 4919b94acceSBarry Smith } 4929b94acceSBarry Smith 49376b2cf59SMatthew Knepley /* 49476b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 49576b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 49676b2cf59SMatthew Knepley */ 49776b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 498a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 4996849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 50076b2cf59SMatthew Knepley 501ac226902SBarry Smith /*@C 50276b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 50376b2cf59SMatthew Knepley 50476b2cf59SMatthew Knepley Not Collective 50576b2cf59SMatthew Knepley 50676b2cf59SMatthew Knepley Input Parameter: 50776b2cf59SMatthew Knepley . snescheck - function that checks for options 50876b2cf59SMatthew Knepley 50976b2cf59SMatthew Knepley Level: developer 51076b2cf59SMatthew Knepley 51176b2cf59SMatthew Knepley .seealso: SNESSetFromOptions() 51276b2cf59SMatthew Knepley @*/ 5137087cfbeSBarry Smith PetscErrorCode SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 51476b2cf59SMatthew Knepley { 51576b2cf59SMatthew Knepley PetscFunctionBegin; 516f23aa3ddSBarry Smith if (numberofsetfromoptions >= MAXSETFROMOPTIONS) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS); 51776b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 51876b2cf59SMatthew Knepley PetscFunctionReturn(0); 51976b2cf59SMatthew Knepley } 52076b2cf59SMatthew Knepley 52125acbd8eSLisandro Dalcin PETSC_INTERN PetscErrorCode SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 522aa3661deSLisandro Dalcin 523ace3abfcSBarry Smith static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscBool hasOperator, PetscInt version) 524aa3661deSLisandro Dalcin { 525aa3661deSLisandro Dalcin Mat J; 526aa3661deSLisandro Dalcin KSP ksp; 527aa3661deSLisandro Dalcin PC pc; 528ace3abfcSBarry Smith PetscBool match; 529aa3661deSLisandro Dalcin PetscErrorCode ierr; 530895c21f2SBarry Smith MatNullSpace nullsp; 531aa3661deSLisandro Dalcin 532aa3661deSLisandro Dalcin PetscFunctionBegin; 5330700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 534aa3661deSLisandro Dalcin 53598613b67SLisandro Dalcin if (!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) { 53698613b67SLisandro Dalcin Mat A = snes->jacobian, B = snes->jacobian_pre; 5372a7a6963SBarry Smith ierr = MatCreateVecs(A ? A : B, NULL,&snes->vec_func);CHKERRQ(ierr); 53898613b67SLisandro Dalcin } 53998613b67SLisandro Dalcin 540aa3661deSLisandro Dalcin if (version == 1) { 541aa3661deSLisandro Dalcin ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 54298613b67SLisandro Dalcin ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 5439c6ac3b3SBarry Smith ierr = MatSetFromOptions(J);CHKERRQ(ierr); 544aa3661deSLisandro Dalcin } else if (version == 2) { 545e32f2f54SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first"); 546570b7f6dSBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128) && !defined(PETSC_USE_REAL___FP16) 547aa3661deSLisandro Dalcin ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr); 548aa3661deSLisandro Dalcin #else 549e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)"); 550aa3661deSLisandro Dalcin #endif 551a8248277SBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2"); 552aa3661deSLisandro Dalcin 553895c21f2SBarry Smith /* attach any user provided null space that was on Amat to the newly created matrix free matrix */ 554895c21f2SBarry Smith if (snes->jacobian) { 555895c21f2SBarry Smith ierr = MatGetNullSpace(snes->jacobian,&nullsp);CHKERRQ(ierr); 556895c21f2SBarry Smith if (nullsp) { 557895c21f2SBarry Smith ierr = MatSetNullSpace(J,nullsp);CHKERRQ(ierr); 558895c21f2SBarry Smith } 559895c21f2SBarry Smith } 560895c21f2SBarry Smith 561aa3661deSLisandro Dalcin ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr); 562d3462f78SMatthew Knepley if (hasOperator) { 5633232da50SPeter Brune 564aa3661deSLisandro Dalcin /* This version replaces the user provided Jacobian matrix with a 565aa3661deSLisandro Dalcin matrix-free version but still employs the user-provided preconditioner matrix. */ 566aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 567aa3661deSLisandro Dalcin } else { 568aa3661deSLisandro Dalcin /* This version replaces both the user-provided Jacobian and the user- 5693232da50SPeter Brune provided preconditioner Jacobian with the default matrix free version. */ 570efd4aadfSBarry Smith if ((snes->npcside== PC_LEFT) && snes->npc) { 571d728fb7dSPeter Brune if (!snes->jacobian){ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);} 572172a4300SPeter Brune } else { 57328a52e04SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,0);CHKERRQ(ierr); 574172a4300SPeter Brune } 575aa3661deSLisandro Dalcin /* Force no preconditioner */ 576aa3661deSLisandro Dalcin ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 577aa3661deSLisandro Dalcin ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 578251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr); 579aa3661deSLisandro Dalcin if (!match) { 580aa3661deSLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 581aa3661deSLisandro Dalcin ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 582aa3661deSLisandro Dalcin } 583aa3661deSLisandro Dalcin } 5846bf464f9SBarry Smith ierr = MatDestroy(&J);CHKERRQ(ierr); 585aa3661deSLisandro Dalcin PetscFunctionReturn(0); 586aa3661deSLisandro Dalcin } 587aa3661deSLisandro Dalcin 588dfe15315SJed Brown static PetscErrorCode DMRestrictHook_SNESVecSol(DM dmfine,Mat Restrict,Vec Rscale,Mat Inject,DM dmcoarse,void *ctx) 589dfe15315SJed Brown { 590dfe15315SJed Brown SNES snes = (SNES)ctx; 591dfe15315SJed Brown PetscErrorCode ierr; 5920298fd71SBarry Smith Vec Xfine,Xfine_named = NULL,Xcoarse; 593dfe15315SJed Brown 594dfe15315SJed Brown PetscFunctionBegin; 59516ebb321SJed Brown if (PetscLogPrintInfo) { 59616ebb321SJed Brown PetscInt finelevel,coarselevel,fineclevel,coarseclevel; 59716ebb321SJed Brown ierr = DMGetRefineLevel(dmfine,&finelevel);CHKERRQ(ierr); 59816ebb321SJed Brown ierr = DMGetCoarsenLevel(dmfine,&fineclevel);CHKERRQ(ierr); 59916ebb321SJed Brown ierr = DMGetRefineLevel(dmcoarse,&coarselevel);CHKERRQ(ierr); 60016ebb321SJed Brown ierr = DMGetCoarsenLevel(dmcoarse,&coarseclevel);CHKERRQ(ierr); 60116ebb321SJed Brown ierr = PetscInfo4(dmfine,"Restricting SNES solution vector from level %D-%D to level %D-%D\n",finelevel,fineclevel,coarselevel,coarseclevel);CHKERRQ(ierr); 60216ebb321SJed Brown } 603dfe15315SJed Brown if (dmfine == snes->dm) Xfine = snes->vec_sol; 604dfe15315SJed Brown else { 605dfe15315SJed Brown ierr = DMGetNamedGlobalVector(dmfine,"SNESVecSol",&Xfine_named);CHKERRQ(ierr); 606dfe15315SJed Brown Xfine = Xfine_named; 607dfe15315SJed Brown } 608dfe15315SJed Brown ierr = DMGetNamedGlobalVector(dmcoarse,"SNESVecSol",&Xcoarse);CHKERRQ(ierr); 609907f5c5aSLawrence Mitchell if (Inject) { 610907f5c5aSLawrence Mitchell ierr = MatRestrict(Inject,Xfine,Xcoarse);CHKERRQ(ierr); 611907f5c5aSLawrence Mitchell } else { 612dfe15315SJed Brown ierr = MatRestrict(Restrict,Xfine,Xcoarse);CHKERRQ(ierr); 613dfe15315SJed Brown ierr = VecPointwiseMult(Xcoarse,Xcoarse,Rscale);CHKERRQ(ierr); 614907f5c5aSLawrence Mitchell } 615dfe15315SJed Brown ierr = DMRestoreNamedGlobalVector(dmcoarse,"SNESVecSol",&Xcoarse);CHKERRQ(ierr); 616dfe15315SJed Brown if (Xfine_named) {ierr = DMRestoreNamedGlobalVector(dmfine,"SNESVecSol",&Xfine_named);CHKERRQ(ierr);} 617dfe15315SJed Brown PetscFunctionReturn(0); 618dfe15315SJed Brown } 619dfe15315SJed Brown 62016ebb321SJed Brown static PetscErrorCode DMCoarsenHook_SNESVecSol(DM dm,DM dmc,void *ctx) 62116ebb321SJed Brown { 62216ebb321SJed Brown PetscErrorCode ierr; 62316ebb321SJed Brown 62416ebb321SJed Brown PetscFunctionBegin; 62516ebb321SJed Brown ierr = DMCoarsenHookAdd(dmc,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,ctx);CHKERRQ(ierr); 62616ebb321SJed Brown PetscFunctionReturn(0); 62716ebb321SJed Brown } 62816ebb321SJed Brown 629a6950cb2SJed Brown /* This may be called to rediscretize the operator on levels of linear multigrid. The DM shuffle is so the user can 630a6950cb2SJed Brown * safely call SNESGetDM() in their residual evaluation routine. */ 63123ee1639SBarry Smith static PetscErrorCode KSPComputeOperators_SNES(KSP ksp,Mat A,Mat B,void *ctx) 632caa4e7f2SJed Brown { 633caa4e7f2SJed Brown SNES snes = (SNES)ctx; 634caa4e7f2SJed Brown PetscErrorCode ierr; 6350298fd71SBarry Smith Vec X,Xnamed = NULL; 636dfe15315SJed Brown DM dmsave; 6374e269d77SPeter Brune void *ctxsave; 63825ce1634SJed Brown PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*) = NULL; 639caa4e7f2SJed Brown 640caa4e7f2SJed Brown PetscFunctionBegin; 641dfe15315SJed Brown dmsave = snes->dm; 642dfe15315SJed Brown ierr = KSPGetDM(ksp,&snes->dm);CHKERRQ(ierr); 643dfe15315SJed Brown if (dmsave == snes->dm) X = snes->vec_sol; /* We are on the finest level */ 644dfe15315SJed Brown else { /* We are on a coarser level, this vec was initialized using a DM restrict hook */ 645dfe15315SJed Brown ierr = DMGetNamedGlobalVector(snes->dm,"SNESVecSol",&Xnamed);CHKERRQ(ierr); 646dfe15315SJed Brown X = Xnamed; 6470298fd71SBarry Smith ierr = SNESGetJacobian(snes,NULL,NULL,&jac,&ctxsave);CHKERRQ(ierr); 6484e269d77SPeter Brune /* If the DM's don't match up, the MatFDColoring context needed for the jacobian won't match up either -- fixit. */ 6498d359177SBarry Smith if (jac == SNESComputeJacobianDefaultColor) { 6508d359177SBarry Smith ierr = SNESSetJacobian(snes,NULL,NULL,SNESComputeJacobianDefaultColor,0);CHKERRQ(ierr); 651dfe15315SJed Brown } 6524e269d77SPeter Brune } 6534dde8bb0SMatthew G. Knepley /* Make sure KSP DM has the Jacobian computation routine */ 6544dde8bb0SMatthew G. Knepley { 6554dde8bb0SMatthew G. Knepley DMSNES sdm; 6564e269d77SPeter Brune 6574dde8bb0SMatthew G. Knepley ierr = DMGetDMSNES(snes->dm, &sdm);CHKERRQ(ierr); 6584dde8bb0SMatthew G. Knepley if (!sdm->ops->computejacobian) { 6594dde8bb0SMatthew G. Knepley ierr = DMCopyDMSNES(dmsave, snes->dm);CHKERRQ(ierr); 6604dde8bb0SMatthew G. Knepley } 6614dde8bb0SMatthew G. Knepley } 6622b93b426SMatthew G. Knepley /* Compute the operators */ 663d1e9a80fSBarry Smith ierr = SNESComputeJacobian(snes,X,A,B);CHKERRQ(ierr); 6642b93b426SMatthew G. Knepley /* Put the previous context back */ 6658d359177SBarry Smith if (snes->dm != dmsave && jac == SNESComputeJacobianDefaultColor) { 6660298fd71SBarry Smith ierr = SNESSetJacobian(snes,NULL,NULL,jac,ctxsave);CHKERRQ(ierr); 6674e269d77SPeter Brune } 6684e269d77SPeter Brune 6692b93b426SMatthew G. Knepley if (Xnamed) {ierr = DMRestoreNamedGlobalVector(snes->dm,"SNESVecSol",&Xnamed);CHKERRQ(ierr);} 670dfe15315SJed Brown snes->dm = dmsave; 671caa4e7f2SJed Brown PetscFunctionReturn(0); 672caa4e7f2SJed Brown } 673caa4e7f2SJed Brown 6746cab3a1bSJed Brown /*@ 6756cab3a1bSJed Brown SNESSetUpMatrices - ensures that matrices are available for SNES, to be called by SNESSetUp_XXX() 6766cab3a1bSJed Brown 6776cab3a1bSJed Brown Collective 6786cab3a1bSJed Brown 6796cab3a1bSJed Brown Input Arguments: 6806cab3a1bSJed Brown . snes - snes to configure 6816cab3a1bSJed Brown 6826cab3a1bSJed Brown Level: developer 6836cab3a1bSJed Brown 6846cab3a1bSJed Brown .seealso: SNESSetUp() 6856cab3a1bSJed Brown @*/ 6866cab3a1bSJed Brown PetscErrorCode SNESSetUpMatrices(SNES snes) 6876cab3a1bSJed Brown { 6886cab3a1bSJed Brown PetscErrorCode ierr; 6896cab3a1bSJed Brown DM dm; 690942e3340SBarry Smith DMSNES sdm; 6916cab3a1bSJed Brown 6926cab3a1bSJed Brown PetscFunctionBegin; 6936cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 694942e3340SBarry Smith ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 695ce94432eSBarry Smith if (!sdm->ops->computejacobian) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_PLIB,"DMSNES not properly configured"); 696f5af7f23SKarl Rupp else if (!snes->jacobian && snes->mf) { 6976cab3a1bSJed Brown Mat J; 6986cab3a1bSJed Brown void *functx; 6996cab3a1bSJed Brown ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 7006cab3a1bSJed Brown ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 7016cab3a1bSJed Brown ierr = MatSetFromOptions(J);CHKERRQ(ierr); 7020298fd71SBarry Smith ierr = SNESGetFunction(snes,NULL,NULL,&functx);CHKERRQ(ierr); 7033232da50SPeter Brune ierr = SNESSetJacobian(snes,J,J,0,0);CHKERRQ(ierr); 7046cab3a1bSJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 705caa4e7f2SJed Brown } else if (snes->mf_operator && !snes->jacobian_pre && !snes->jacobian) { 7066cab3a1bSJed Brown Mat J,B; 7076cab3a1bSJed Brown ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 7086cab3a1bSJed Brown ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 7096cab3a1bSJed Brown ierr = MatSetFromOptions(J);CHKERRQ(ierr); 710b412c318SBarry Smith ierr = DMCreateMatrix(snes->dm,&B);CHKERRQ(ierr); 71106f20277SJed Brown /* sdm->computejacobian was already set to reach here */ 7120298fd71SBarry Smith ierr = SNESSetJacobian(snes,J,B,NULL,NULL);CHKERRQ(ierr); 7136cab3a1bSJed Brown ierr = MatDestroy(&J);CHKERRQ(ierr); 7146cab3a1bSJed Brown ierr = MatDestroy(&B);CHKERRQ(ierr); 715caa4e7f2SJed Brown } else if (!snes->jacobian_pre) { 7164f3e65b0SMatthew G. Knepley PetscErrorCode (*nspconstr)(DM, PetscInt, MatNullSpace *); 7171ba9b98eSMatthew G. Knepley PetscDS prob; 7186cab3a1bSJed Brown Mat J, B; 7194f3e65b0SMatthew G. Knepley MatNullSpace nullspace = NULL; 7201ba9b98eSMatthew G. Knepley PetscBool hasPrec = PETSC_FALSE; 7214f3e65b0SMatthew G. Knepley PetscInt Nf; 7221ba9b98eSMatthew G. Knepley 7236cab3a1bSJed Brown J = snes->jacobian; 7241ba9b98eSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 7251ba9b98eSMatthew G. Knepley if (prob) {ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);} 726ec9a985fSMatthew G. Knepley if (J) {ierr = PetscObjectReference((PetscObject) J);CHKERRQ(ierr);} 727ec9a985fSMatthew G. Knepley else if (hasPrec) {ierr = DMCreateMatrix(snes->dm, &J);CHKERRQ(ierr);} 728b412c318SBarry Smith ierr = DMCreateMatrix(snes->dm, &B);CHKERRQ(ierr); 7294f3e65b0SMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 7304f3e65b0SMatthew G. Knepley ierr = DMGetNullSpaceConstructor(snes->dm, Nf, &nspconstr);CHKERRQ(ierr); 7314f3e65b0SMatthew G. Knepley if (nspconstr) (*nspconstr)(snes->dm, -1, &nullspace); 7324f3e65b0SMatthew G. Knepley ierr = MatSetNullSpace(B, nullspace);CHKERRQ(ierr); 7334f3e65b0SMatthew G. Knepley ierr = MatNullSpaceDestroy(&nullspace);CHKERRQ(ierr); 7340298fd71SBarry Smith ierr = SNESSetJacobian(snes, J ? J : B, B, NULL, NULL);CHKERRQ(ierr); 7351ba9b98eSMatthew G. Knepley ierr = MatDestroy(&J);CHKERRQ(ierr); 7366cab3a1bSJed Brown ierr = MatDestroy(&B);CHKERRQ(ierr); 7376cab3a1bSJed Brown } 738caa4e7f2SJed Brown { 739caa4e7f2SJed Brown KSP ksp; 740caa4e7f2SJed Brown ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 741caa4e7f2SJed Brown ierr = KSPSetComputeOperators(ksp,KSPComputeOperators_SNES,snes);CHKERRQ(ierr); 74216ebb321SJed Brown ierr = DMCoarsenHookAdd(snes->dm,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,snes);CHKERRQ(ierr); 743caa4e7f2SJed Brown } 7446cab3a1bSJed Brown PetscFunctionReturn(0); 7456cab3a1bSJed Brown } 7466cab3a1bSJed Brown 747fde5950dSBarry Smith /*@C 748fde5950dSBarry Smith SNESMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user 749fde5950dSBarry Smith 750fde5950dSBarry Smith Collective on SNES 751fde5950dSBarry Smith 752fde5950dSBarry Smith Input Parameters: 753fde5950dSBarry Smith + snes - SNES object you wish to monitor 754fde5950dSBarry Smith . name - the monitor type one is seeking 755fde5950dSBarry Smith . help - message indicating what monitoring is done 756fde5950dSBarry Smith . manual - manual page for the monitor 757fde5950dSBarry Smith . monitor - the monitor function 758fde5950dSBarry 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 759fde5950dSBarry Smith 760fde5950dSBarry Smith Level: developer 761fde5950dSBarry Smith 762fde5950dSBarry Smith .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 763fde5950dSBarry Smith PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 764fde5950dSBarry Smith PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 765fde5950dSBarry Smith PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 766fde5950dSBarry Smith PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 767fde5950dSBarry Smith PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 768fde5950dSBarry Smith PetscOptionsFList(), PetscOptionsEList() 769fde5950dSBarry Smith @*/ 770d43b4f6eSBarry Smith PetscErrorCode SNESMonitorSetFromOptions(SNES snes,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(SNES,PetscViewerAndFormat*)) 771fde5950dSBarry Smith { 772fde5950dSBarry Smith PetscErrorCode ierr; 773fde5950dSBarry Smith PetscViewer viewer; 774fde5950dSBarry Smith PetscViewerFormat format; 775fde5950dSBarry Smith PetscBool flg; 776fde5950dSBarry Smith 777fde5950dSBarry Smith PetscFunctionBegin; 77816413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,name,&viewer,&format,&flg);CHKERRQ(ierr); 779fde5950dSBarry Smith if (flg) { 780d43b4f6eSBarry Smith PetscViewerAndFormat *vf; 781d43b4f6eSBarry Smith ierr = PetscViewerAndFormatCreate(viewer,format,&vf);CHKERRQ(ierr); 782d43b4f6eSBarry Smith ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 783fde5950dSBarry Smith if (monitorsetup) { 784d43b4f6eSBarry Smith ierr = (*monitorsetup)(snes,vf);CHKERRQ(ierr); 785fde5950dSBarry Smith } 786d43b4f6eSBarry Smith ierr = SNESMonitorSet(snes,(PetscErrorCode (*)(SNES,PetscInt,PetscReal,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr); 787fde5950dSBarry Smith } 788fde5950dSBarry Smith PetscFunctionReturn(0); 789fde5950dSBarry Smith } 790fde5950dSBarry Smith 7919b94acceSBarry Smith /*@ 79294b7f48cSBarry Smith SNESSetFromOptions - Sets various SNES and KSP parameters from user options. 7939b94acceSBarry Smith 794c7afd0dbSLois Curfman McInnes Collective on SNES 795c7afd0dbSLois Curfman McInnes 7969b94acceSBarry Smith Input Parameter: 7979b94acceSBarry Smith . snes - the SNES context 7989b94acceSBarry Smith 79936851e7fSLois Curfman McInnes Options Database Keys: 800722329fbSBarry Smith + -snes_type <type> - newtonls, newtontr, ngmres, ncg, nrichardson, qn, vi, fas, SNESType for complete list 80182738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 80282738288SBarry Smith of the change in the solution between steps 80370441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 804b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 805e4d06f11SPatrick Farrell . -snes_divergence_tolerance <divtol> - if the residual goes above divtol*rnorm0, exit with divergence 806be5caee7SBarry Smith . -snes_force_iteration <force> - force SNESSolve() to take at least one iteration 807b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 808b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 8094839bfe8SBarry Smith . -snes_max_fail <max_fail> - maximum number of line search failures allowed before stopping, default is none 810ddf469c8SBarry Smith . -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops 811a8054027SBarry Smith . -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild) 812e35cf81dSBarry Smith . -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild) 813b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 8142492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 81582738288SBarry Smith solver; hence iterations will continue until max_it 8161fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 81782738288SBarry Smith of convergence test 818fde5950dSBarry Smith . -snes_monitor [ascii][:filename][:viewer format] - prints residual norm at each iteration. if no filename given prints to stdout 819fde5950dSBarry Smith . -snes_monitor_solution [ascii binary draw][:filename][:viewer format] - plots solution at each iteration 820fde5950dSBarry Smith . -snes_monitor_residual [ascii binary draw][:filename][:viewer format] - plots residual (not its norm) at each iteration 821fde5950dSBarry Smith . -snes_monitor_solution_update [ascii binary draw][:filename][:viewer format] - plots update to solution at each iteration 8224619e776SBarry Smith . -snes_monitor_lg_residualnorm - plots residual norm at each iteration 823459f5d12SBarry Smith . -snes_monitor_lg_range - plots residual norm at each iteration 824e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 825e2e60de9SPeter Brune . -snes_fd_color - use finite differences with coloring to compute Jacobian 8265968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 827fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 82882738288SBarry Smith 82982738288SBarry Smith Options Database for Eisenstat-Walker method: 830fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 8314b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 83236851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 83336851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 83436851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 83536851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 83636851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 83736851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 83882738288SBarry Smith 83911ca99fdSLois Curfman McInnes Notes: 84011ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 841a7f22e61SSatish Balay Users-Manual: ch_snes 84283e2fdc7SBarry Smith 84336851e7fSLois Curfman McInnes Level: beginner 84436851e7fSLois Curfman McInnes 8459b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 8469b94acceSBarry Smith 847b3cd9a81SMatthew G. Knepley .seealso: SNESSetOptionsPrefix(), SNESResetFromOptions() 8489b94acceSBarry Smith @*/ 8497087cfbeSBarry Smith PetscErrorCode SNESSetFromOptions(SNES snes) 8509b94acceSBarry Smith { 8518afaa268SBarry Smith PetscBool flg,pcset,persist,set; 852d8f46077SPeter Brune PetscInt i,indx,lag,grids; 85304d7464bSBarry Smith const char *deft = SNESNEWTONLS; 85485385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 85585385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 856e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 85785385478SLisandro Dalcin PetscErrorCode ierr; 858c40d0f55SPeter Brune PCSide pcside; 859a64e098fSPeter Brune const char *optionsprefix; 8609b94acceSBarry Smith 8613a40ed3dSBarry Smith PetscFunctionBegin; 8620700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8630f51fdf8SToby Isaac ierr = SNESRegisterAll();CHKERRQ(ierr); 8643194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)snes);CHKERRQ(ierr); 865639ff905SBarry Smith if (((PetscObject)snes)->type_name) deft = ((PetscObject)snes)->type_name; 866a264d7a6SBarry Smith ierr = PetscOptionsFList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 867d64ed03dSBarry Smith if (flg) { 868186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 8697adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 870186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 871d64ed03dSBarry Smith } 87294ae4db5SBarry Smith ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->stol,&snes->stol,NULL);CHKERRQ(ierr); 87394ae4db5SBarry Smith ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,NULL);CHKERRQ(ierr); 874186905e3SBarry Smith 87594ae4db5SBarry Smith ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,NULL);CHKERRQ(ierr); 876e4d06f11SPatrick Farrell ierr = PetscOptionsReal("-snes_divergence_tolerance","Stop if residual norm increases by this factor","SNESSetDivergenceTolerance",snes->divtol,&snes->divtol,NULL);CHKERRQ(ierr); 8770298fd71SBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,NULL);CHKERRQ(ierr); 8780298fd71SBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,NULL);CHKERRQ(ierr); 8790298fd71SBarry Smith ierr = PetscOptionsInt("-snes_max_fail","Maximum nonlinear step failures","SNESSetMaxNonlinearStepFailures",snes->maxFailures,&snes->maxFailures,NULL);CHKERRQ(ierr); 8800298fd71SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,NULL);CHKERRQ(ierr); 8810298fd71SBarry Smith ierr = PetscOptionsBool("-snes_error_if_not_converged","Generate error if solver does not converge","SNESSetErrorIfNotConverged",snes->errorifnotconverged,&snes->errorifnotconverged,NULL);CHKERRQ(ierr); 8820d6f27a8SBarry Smith ierr = PetscOptionsBool("-snes_force_iteration","Force SNESSolve() to take at least one iteration","SNESSetForceIteration",snes->forceiteration,&snes->forceiteration,NULL);CHKERRQ(ierr); 883*b351a90bSFande Kong ierr = PetscOptionsBool("-snes_check_jacobian_domain_error","Check Jacobian domain error after Jacobian evaluation","SNESCheckJacobianDomainError",snes->checkjacdomainerror,&snes->checkjacdomainerror,NULL);CHKERRQ(ierr); 88485385478SLisandro Dalcin 885a8054027SBarry Smith ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr); 886a8054027SBarry Smith if (flg) { 887a8054027SBarry Smith ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr); 888a8054027SBarry Smith } 88937ec4e1aSPeter Brune ierr = PetscOptionsBool("-snes_lag_preconditioner_persists","Preconditioner lagging through multiple solves","SNESSetLagPreconditionerPersists",snes->lagjac_persist,&persist,&flg);CHKERRQ(ierr); 89037ec4e1aSPeter Brune if (flg) { 89137ec4e1aSPeter Brune ierr = SNESSetLagPreconditionerPersists(snes,persist);CHKERRQ(ierr); 89237ec4e1aSPeter Brune } 893e35cf81dSBarry Smith ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr); 894e35cf81dSBarry Smith if (flg) { 895e35cf81dSBarry Smith ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr); 896e35cf81dSBarry Smith } 89737ec4e1aSPeter Brune ierr = PetscOptionsBool("-snes_lag_jacobian_persists","Jacobian lagging through multiple solves","SNESSetLagJacobianPersists",snes->lagjac_persist,&persist,&flg);CHKERRQ(ierr); 89837ec4e1aSPeter Brune if (flg) { 89937ec4e1aSPeter Brune ierr = SNESSetLagJacobianPersists(snes,persist);CHKERRQ(ierr); 90037ec4e1aSPeter Brune } 90137ec4e1aSPeter Brune 902efd51863SBarry Smith ierr = PetscOptionsInt("-snes_grid_sequence","Use grid sequencing to generate initial guess","SNESSetGridSequence",snes->gridsequence,&grids,&flg);CHKERRQ(ierr); 903efd51863SBarry Smith if (flg) { 904efd51863SBarry Smith ierr = SNESSetGridSequence(snes,grids);CHKERRQ(ierr); 905efd51863SBarry Smith } 906a8054027SBarry Smith 90785385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 90885385478SLisandro Dalcin if (flg) { 90985385478SLisandro Dalcin switch (indx) { 9108d359177SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESConvergedDefault,NULL,NULL);CHKERRQ(ierr); break; 911e2a6519dSDmitry Karpeev case 1: ierr = SNESSetConvergenceTest(snes,SNESConvergedSkip,NULL,NULL);CHKERRQ(ierr); break; 91285385478SLisandro Dalcin } 91385385478SLisandro Dalcin } 91485385478SLisandro Dalcin 915365a6726SPeter Brune ierr = PetscOptionsEList("-snes_norm_schedule","SNES Norm schedule","SNESSetNormSchedule",SNESNormSchedules,5,"function",&indx,&flg);CHKERRQ(ierr); 916365a6726SPeter Brune if (flg) { ierr = SNESSetNormSchedule(snes,(SNESNormSchedule)indx);CHKERRQ(ierr); } 917fdacfa88SPeter Brune 91847073ea2SPeter Brune ierr = PetscOptionsEList("-snes_function_type","SNES Norm schedule","SNESSetFunctionType",SNESFunctionTypes,2,"unpreconditioned",&indx,&flg);CHKERRQ(ierr); 91947073ea2SPeter Brune if (flg) { ierr = SNESSetFunctionType(snes,(SNESFunctionType)indx);CHKERRQ(ierr); } 920186905e3SBarry Smith 92185385478SLisandro Dalcin kctx = (SNESKSPEW*)snes->kspconvctx; 92285385478SLisandro Dalcin 9230298fd71SBarry Smith ierr = PetscOptionsBool("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,NULL);CHKERRQ(ierr); 924186905e3SBarry Smith 92594ae4db5SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,NULL);CHKERRQ(ierr); 92694ae4db5SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,NULL);CHKERRQ(ierr); 92794ae4db5SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,NULL);CHKERRQ(ierr); 92894ae4db5SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,NULL);CHKERRQ(ierr); 92994ae4db5SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,NULL);CHKERRQ(ierr); 93094ae4db5SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,NULL);CHKERRQ(ierr); 93194ae4db5SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,NULL);CHKERRQ(ierr); 932186905e3SBarry Smith 93390d69ab7SBarry Smith flg = PETSC_FALSE; 9348afaa268SBarry Smith ierr = PetscOptionsBool("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,&set);CHKERRQ(ierr); 9358afaa268SBarry Smith if (set && flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 936eabae89aSBarry Smith 937fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor","Monitor norm of function","SNESMonitorDefault",SNESMonitorDefault,NULL);CHKERRQ(ierr); 938fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor_short","Monitor norm of function with fewer digits","SNESMonitorDefaultShort",SNESMonitorDefaultShort,NULL);CHKERRQ(ierr); 939fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor_range","Monitor range of elements of function","SNESMonitorRange",SNESMonitorRange,NULL);CHKERRQ(ierr); 940eabae89aSBarry Smith 941fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor_ratio","Monitor ratios of the norm of function for consecutive steps","SNESMonitorRatio",SNESMonitorRatio,SNESMonitorRatioSetUp);CHKERRQ(ierr); 942fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor_field","Monitor norm of function (split into fields)","SNESMonitorDefaultField",SNESMonitorDefaultField,NULL);CHKERRQ(ierr); 943fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor_solution","View solution at each iteration","SNESMonitorSolution",SNESMonitorSolution,NULL);CHKERRQ(ierr); 944fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor_solution_update","View correction at each iteration","SNESMonitorSolutionUpdate",SNESMonitorSolutionUpdate,NULL);CHKERRQ(ierr); 945fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor_residual","View residual at each iteration","SNESMonitorResidual",SNESMonitorResidual,NULL);CHKERRQ(ierr); 946fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor_jacupdate_spectrum","Print the change in the spectrum of the Jacobian","SNESMonitorJacUpdateSpectrum",SNESMonitorJacUpdateSpectrum,NULL);CHKERRQ(ierr); 947fde5950dSBarry Smith ierr = SNESMonitorSetFromOptions(snes,"-snes_monitor_fields","Monitor norm of function per field","SNESMonitorSet",SNESMonitorFields,NULL);CHKERRQ(ierr); 9482db13446SMatthew G. Knepley 9495180491cSLisandro Dalcin ierr = PetscOptionsString("-snes_monitor_python","Use Python function","SNESMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 9505180491cSLisandro Dalcin if (flg) {ierr = PetscPythonMonitorSet((PetscObject)snes,monfilename);CHKERRQ(ierr);} 9515180491cSLisandro Dalcin 952fde5950dSBarry Smith 95390d69ab7SBarry Smith flg = PETSC_FALSE; 9540298fd71SBarry Smith ierr = PetscOptionsBool("-snes_monitor_lg_residualnorm","Plot function norm at each iteration","SNESMonitorLGResidualNorm",flg,&flg,NULL);CHKERRQ(ierr); 955459f5d12SBarry Smith if (flg) { 956d96771aaSLisandro Dalcin PetscDrawLG ctx; 957459f5d12SBarry Smith 9586ba87a44SLisandro Dalcin ierr = SNESMonitorLGCreate(PetscObjectComm((PetscObject)snes),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&ctx);CHKERRQ(ierr); 959d96771aaSLisandro Dalcin ierr = SNESMonitorSet(snes,SNESMonitorLGResidualNorm,ctx,(PetscErrorCode (*)(void**))PetscDrawLGDestroy);CHKERRQ(ierr); 960459f5d12SBarry Smith } 96190d69ab7SBarry Smith flg = PETSC_FALSE; 9620298fd71SBarry Smith ierr = PetscOptionsBool("-snes_monitor_lg_range","Plot function range at each iteration","SNESMonitorLGRange",flg,&flg,NULL);CHKERRQ(ierr); 963459f5d12SBarry Smith if (flg) { 964459f5d12SBarry Smith PetscViewer ctx; 965e24b481bSBarry Smith 9666ba87a44SLisandro Dalcin ierr = PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&ctx);CHKERRQ(ierr); 967459f5d12SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorLGRange,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr); 968459f5d12SBarry Smith } 9692e7541e6SPeter Brune 9702e7541e6SPeter Brune 971cc0c4584SMatthew G. Knepley 97290d69ab7SBarry Smith flg = PETSC_FALSE; 9738d359177SBarry Smith ierr = PetscOptionsBool("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESComputeJacobianDefault",flg,&flg,NULL);CHKERRQ(ierr); 9744b27c08aSLois Curfman McInnes if (flg) { 9756cab3a1bSJed Brown void *functx; 976b1f624c7SBarry Smith DM dm; 977b1f624c7SBarry Smith DMSNES sdm; 978b1f624c7SBarry Smith ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 979b1f624c7SBarry Smith ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 980b1f624c7SBarry Smith sdm->jacobianctx = NULL; 9810298fd71SBarry Smith ierr = SNESGetFunction(snes,NULL,NULL,&functx);CHKERRQ(ierr); 9828d359177SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESComputeJacobianDefault,functx);CHKERRQ(ierr); 983ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 9849b94acceSBarry Smith } 985639f9d9dSBarry Smith 98644848bc4SPeter Brune flg = PETSC_FALSE; 9878d359177SBarry Smith ierr = PetscOptionsBool("-snes_fd_function","Use finite differences (slow) to compute function from user objective","SNESObjectiveComputeFunctionDefaultFD",flg,&flg,NULL);CHKERRQ(ierr); 98897584545SPeter Brune if (flg) { 9898d359177SBarry Smith ierr = SNESSetFunction(snes,NULL,SNESObjectiveComputeFunctionDefaultFD,NULL);CHKERRQ(ierr); 99097584545SPeter Brune } 99197584545SPeter Brune 99297584545SPeter Brune flg = PETSC_FALSE; 9938d359177SBarry Smith ierr = PetscOptionsBool("-snes_fd_color","Use finite differences with coloring to compute Jacobian","SNESComputeJacobianDefaultColor",flg,&flg,NULL);CHKERRQ(ierr); 99444848bc4SPeter Brune if (flg) { 995c52e227fSPeter Brune DM dm; 996c52e227fSPeter Brune DMSNES sdm; 997c52e227fSPeter Brune ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 998aace71b7SPeter Brune ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 999aace71b7SPeter Brune sdm->jacobianctx = NULL; 10008d359177SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESComputeJacobianDefaultColor,0);CHKERRQ(ierr); 100144848bc4SPeter Brune ierr = PetscInfo(snes,"Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr); 100244848bc4SPeter Brune } 100344848bc4SPeter Brune 1004aa3661deSLisandro Dalcin flg = PETSC_FALSE; 1005f871313dSBarry Smith ierr = PetscOptionsBool("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","SNESSetUseMatrixFree",PETSC_FALSE,&snes->mf_operator,&flg);CHKERRQ(ierr); 1006d8f46077SPeter Brune if (flg && snes->mf_operator) { 1007a8248277SBarry Smith snes->mf_operator = PETSC_TRUE; 1008d8f46077SPeter Brune snes->mf = PETSC_TRUE; 1009a8248277SBarry Smith } 1010aa3661deSLisandro Dalcin flg = PETSC_FALSE; 1011f871313dSBarry Smith ierr = PetscOptionsBool("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","SNESSetUseMatrixFree",PETSC_FALSE,&snes->mf,&flg);CHKERRQ(ierr); 1012d8f46077SPeter Brune if (!flg && snes->mf_operator) snes->mf = PETSC_TRUE; 1013d8f46077SPeter Brune ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",snes->mf_version,&snes->mf_version,0);CHKERRQ(ierr); 1014d28543b3SPeter Brune 1015c40d0f55SPeter Brune flg = PETSC_FALSE; 1016be95d8f1SBarry Smith ierr = SNESGetNPCSide(snes,&pcside);CHKERRQ(ierr); 1017be95d8f1SBarry Smith ierr = PetscOptionsEnum("-snes_npc_side","SNES nonlinear preconditioner side","SNESSetNPCSide",PCSides,(PetscEnum)pcside,(PetscEnum*)&pcside,&flg);CHKERRQ(ierr); 1018be95d8f1SBarry Smith if (flg) {ierr = SNESSetNPCSide(snes,pcside);CHKERRQ(ierr);} 1019c40d0f55SPeter Brune 1020e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 10218a70d858SHong Zhang /* 10228a70d858SHong Zhang Publish convergence information using SAWs 10238a70d858SHong Zhang */ 10248a70d858SHong Zhang flg = PETSC_FALSE; 10258a70d858SHong Zhang ierr = PetscOptionsBool("-snes_monitor_saws","Publish SNES progress using SAWs","SNESMonitorSet",flg,&flg,NULL);CHKERRQ(ierr); 10268a70d858SHong Zhang if (flg) { 10278a70d858SHong Zhang void *ctx; 10288a70d858SHong Zhang ierr = SNESMonitorSAWsCreate(snes,&ctx);CHKERRQ(ierr); 10298a70d858SHong Zhang ierr = SNESMonitorSet(snes,SNESMonitorSAWs,ctx,SNESMonitorSAWsDestroy);CHKERRQ(ierr); 10308a70d858SHong Zhang } 10318a70d858SHong Zhang #endif 10328a70d858SHong Zhang #if defined(PETSC_HAVE_SAWS) 1033b90c6cbeSBarry Smith { 1034b90c6cbeSBarry Smith PetscBool set; 1035b90c6cbeSBarry Smith flg = PETSC_FALSE; 10368a70d858SHong Zhang ierr = PetscOptionsBool("-snes_saws_block","Block for SAWs at end of SNESSolve","PetscObjectSAWsBlock",((PetscObject)snes)->amspublishblock,&flg,&set);CHKERRQ(ierr); 1037b90c6cbeSBarry Smith if (set) { 1038e04113cfSBarry Smith ierr = PetscObjectSAWsSetBlock((PetscObject)snes,flg);CHKERRQ(ierr); 1039b90c6cbeSBarry Smith } 1040b90c6cbeSBarry Smith } 1041b90c6cbeSBarry Smith #endif 1042b90c6cbeSBarry Smith 104376b2cf59SMatthew Knepley for (i = 0; i < numberofsetfromoptions; i++) { 104476b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 104576b2cf59SMatthew Knepley } 104676b2cf59SMatthew Knepley 1047e7788613SBarry Smith if (snes->ops->setfromoptions) { 1048e55864a3SBarry Smith ierr = (*snes->ops->setfromoptions)(PetscOptionsObject,snes);CHKERRQ(ierr); 1049639f9d9dSBarry Smith } 10505d973c19SBarry Smith 10515d973c19SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 10520633abcbSJed Brown ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)snes);CHKERRQ(ierr); 1053b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 10544bbc92c1SBarry Smith 10559e764e56SPeter Brune if (!snes->linesearch) { 10567601faf0SJed Brown ierr = SNESGetLineSearch(snes, &snes->linesearch);CHKERRQ(ierr); 10579e764e56SPeter Brune } 1058f1c6b773SPeter Brune ierr = SNESLineSearchSetFromOptions(snes->linesearch);CHKERRQ(ierr); 10599e764e56SPeter Brune 10606991f827SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 10616991f827SBarry Smith ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre);CHKERRQ(ierr); 10626991f827SBarry Smith ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 10636991f827SBarry Smith 1064be95d8f1SBarry Smith /* if someone has set the SNES NPC type, create it. */ 106551e86f29SPeter Brune ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr); 1066c5929fdfSBarry Smith ierr = PetscOptionsHasName(((PetscObject)snes)->options,optionsprefix, "-npc_snes_type", &pcset);CHKERRQ(ierr); 1067efd4aadfSBarry Smith if (pcset && (!snes->npc)) { 1068efd4aadfSBarry Smith ierr = SNESGetNPC(snes, &snes->npc);CHKERRQ(ierr); 106951e86f29SPeter Brune } 1070b3cd9a81SMatthew G. Knepley snes->setfromoptionscalled++; 1071b3cd9a81SMatthew G. Knepley PetscFunctionReturn(0); 1072b3cd9a81SMatthew G. Knepley } 1073b3cd9a81SMatthew G. Knepley 1074b3cd9a81SMatthew G. Knepley /*@ 1075b3cd9a81SMatthew G. Knepley SNESResetFromOptions - Sets various SNES and KSP parameters from user options ONLY if the SNES was previously set from options 1076b3cd9a81SMatthew G. Knepley 1077b3cd9a81SMatthew G. Knepley Collective on SNES 1078b3cd9a81SMatthew G. Knepley 1079b3cd9a81SMatthew G. Knepley Input Parameter: 1080b3cd9a81SMatthew G. Knepley . snes - the SNES context 1081b3cd9a81SMatthew G. Knepley 1082b3cd9a81SMatthew G. Knepley Level: beginner 1083b3cd9a81SMatthew G. Knepley 1084b3cd9a81SMatthew G. Knepley .keywords: SNES, nonlinear, set, options, database 1085b3cd9a81SMatthew G. Knepley 1086b3cd9a81SMatthew G. Knepley .seealso: SNESSetFromOptions(), SNESSetOptionsPrefix() 1087b3cd9a81SMatthew G. Knepley @*/ 1088b3cd9a81SMatthew G. Knepley PetscErrorCode SNESResetFromOptions(SNES snes) 1089b3cd9a81SMatthew G. Knepley { 1090b3cd9a81SMatthew G. Knepley PetscErrorCode ierr; 1091b3cd9a81SMatthew G. Knepley 1092b3cd9a81SMatthew G. Knepley PetscFunctionBegin; 1093b3cd9a81SMatthew G. Knepley if (snes->setfromoptionscalled) {ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);} 10943a40ed3dSBarry Smith PetscFunctionReturn(0); 10959b94acceSBarry Smith } 10969b94acceSBarry Smith 1097bb9467b5SJed Brown /*@C 1098d25893d9SBarry Smith SNESSetComputeApplicationContext - Sets an optional function to compute a user-defined context for 1099d25893d9SBarry Smith the nonlinear solvers. 1100d25893d9SBarry Smith 1101d25893d9SBarry Smith Logically Collective on SNES 1102d25893d9SBarry Smith 1103d25893d9SBarry Smith Input Parameters: 1104d25893d9SBarry Smith + snes - the SNES context 1105d25893d9SBarry Smith . compute - function to compute the context 1106d25893d9SBarry Smith - destroy - function to destroy the context 1107d25893d9SBarry Smith 1108d25893d9SBarry Smith Level: intermediate 1109d25893d9SBarry Smith 1110bb9467b5SJed Brown Notes: 1111bb9467b5SJed Brown This function is currently not available from Fortran. 1112bb9467b5SJed Brown 1113d25893d9SBarry Smith .keywords: SNES, nonlinear, set, application, context 1114d25893d9SBarry Smith 1115d25893d9SBarry Smith .seealso: SNESGetApplicationContext(), SNESSetComputeApplicationContext(), SNESGetApplicationContext() 1116d25893d9SBarry Smith @*/ 1117d25893d9SBarry Smith PetscErrorCode SNESSetComputeApplicationContext(SNES snes,PetscErrorCode (*compute)(SNES,void**),PetscErrorCode (*destroy)(void**)) 1118d25893d9SBarry Smith { 1119d25893d9SBarry Smith PetscFunctionBegin; 1120d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1121d25893d9SBarry Smith snes->ops->usercompute = compute; 1122d25893d9SBarry Smith snes->ops->userdestroy = destroy; 1123d25893d9SBarry Smith PetscFunctionReturn(0); 1124d25893d9SBarry Smith } 1125a847f771SSatish Balay 1126b07ff414SBarry Smith /*@ 11279b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 11289b94acceSBarry Smith the nonlinear solvers. 11299b94acceSBarry Smith 11303f9fe445SBarry Smith Logically Collective on SNES 1131fee21e36SBarry Smith 1132c7afd0dbSLois Curfman McInnes Input Parameters: 1133c7afd0dbSLois Curfman McInnes + snes - the SNES context 1134c7afd0dbSLois Curfman McInnes - usrP - optional user context 1135c7afd0dbSLois Curfman McInnes 113636851e7fSLois Curfman McInnes Level: intermediate 113736851e7fSLois Curfman McInnes 113895452b02SPatrick Sanan Fortran Notes: 113995452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 1140daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1141daf670e6SBarry Smith 11429b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 11439b94acceSBarry Smith 1144ecaffddaSVictor Eijkhout .seealso: SNESGetApplicationContext() 11459b94acceSBarry Smith @*/ 11467087cfbeSBarry Smith PetscErrorCode SNESSetApplicationContext(SNES snes,void *usrP) 11479b94acceSBarry Smith { 11481b2093e4SBarry Smith PetscErrorCode ierr; 1149b07ff414SBarry Smith KSP ksp; 11501b2093e4SBarry Smith 11513a40ed3dSBarry Smith PetscFunctionBegin; 11520700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1153b07ff414SBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 1154b07ff414SBarry Smith ierr = KSPSetApplicationContext(ksp,usrP);CHKERRQ(ierr); 11559b94acceSBarry Smith snes->user = usrP; 11563a40ed3dSBarry Smith PetscFunctionReturn(0); 11579b94acceSBarry Smith } 115874679c65SBarry Smith 1159b07ff414SBarry Smith /*@ 11609b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 11619b94acceSBarry Smith nonlinear solvers. 11629b94acceSBarry Smith 1163c7afd0dbSLois Curfman McInnes Not Collective 1164c7afd0dbSLois Curfman McInnes 11659b94acceSBarry Smith Input Parameter: 11669b94acceSBarry Smith . snes - SNES context 11679b94acceSBarry Smith 11689b94acceSBarry Smith Output Parameter: 11699b94acceSBarry Smith . usrP - user context 11709b94acceSBarry Smith 117195452b02SPatrick Sanan Fortran Notes: 117295452b02SPatrick Sanan To use this from Fortran you must write a Fortran interface definition for this 1173daf670e6SBarry Smith function that tells Fortran the Fortran derived data type that you are passing in as the ctx argument. 1174daf670e6SBarry Smith 117536851e7fSLois Curfman McInnes Level: intermediate 117636851e7fSLois Curfman McInnes 11779b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 11789b94acceSBarry Smith 11799b94acceSBarry Smith .seealso: SNESSetApplicationContext() 11809b94acceSBarry Smith @*/ 1181e71120c6SJed Brown PetscErrorCode SNESGetApplicationContext(SNES snes,void *usrP) 11829b94acceSBarry Smith { 11833a40ed3dSBarry Smith PetscFunctionBegin; 11840700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1185e71120c6SJed Brown *(void**)usrP = snes->user; 11863a40ed3dSBarry Smith PetscFunctionReturn(0); 11879b94acceSBarry Smith } 118874679c65SBarry Smith 11899b94acceSBarry Smith /*@ 11903565c898SBarry Smith SNESSetUseMatrixFree - indicates that SNES should use matrix free finite difference matrix vector products internally to apply 11913565c898SBarry Smith the Jacobian. 11923565c898SBarry Smith 11933565c898SBarry Smith Collective on SNES 11943565c898SBarry Smith 11953565c898SBarry Smith Input Parameters: 11963565c898SBarry Smith + snes - SNES context 11973565c898SBarry 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 11983565c898SBarry Smith - mf_operator - use matrix-free only for the Amat used by SNESSetJacobian(), this means the user provided Pmat will continue to be used 11993565c898SBarry Smith 12003565c898SBarry Smith Options Database: 12013565c898SBarry Smith + -snes_mf - use matrix free for both the mat and pmat operator 12023565c898SBarry Smith - -snes_mf_operator - use matrix free only for the mat operator 12033565c898SBarry Smith 12043565c898SBarry Smith Level: intermediate 12053565c898SBarry Smith 12063565c898SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 12073565c898SBarry Smith 12083565c898SBarry Smith .seealso: SNESGetUseMatrixFree(), MatCreateSNESMF() 12093565c898SBarry Smith @*/ 12103565c898SBarry Smith PetscErrorCode SNESSetUseMatrixFree(SNES snes,PetscBool mf_operator,PetscBool mf) 12113565c898SBarry Smith { 12123565c898SBarry Smith PetscFunctionBegin; 12133565c898SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 121488b4c220SStefano Zampini PetscValidLogicalCollectiveBool(snes,mf_operator,2); 121588b4c220SStefano Zampini PetscValidLogicalCollectiveBool(snes,mf,3); 12163565c898SBarry Smith if (mf && !mf_operator) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_INCOMP,"If using mf must also use mf_operator"); 12173565c898SBarry Smith snes->mf = mf; 12183565c898SBarry Smith snes->mf_operator = mf_operator; 12193565c898SBarry Smith PetscFunctionReturn(0); 12203565c898SBarry Smith } 12213565c898SBarry Smith 12223565c898SBarry Smith /*@ 12233565c898SBarry Smith SNESGetUseMatrixFree - indicates if the SNES uses matrix free finite difference matrix vector products to apply 12243565c898SBarry Smith the Jacobian. 12253565c898SBarry Smith 12263565c898SBarry Smith Collective on SNES 12273565c898SBarry Smith 12283565c898SBarry Smith Input Parameter: 12293565c898SBarry Smith . snes - SNES context 12303565c898SBarry Smith 12313565c898SBarry Smith Output Parameters: 12323565c898SBarry 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 12333565c898SBarry Smith - mf_operator - use matrix-free only for the Amat used by SNESSetJacobian(), this means the user provided Pmat will continue to be used 12343565c898SBarry Smith 12353565c898SBarry Smith Options Database: 12363565c898SBarry Smith + -snes_mf - use matrix free for both the mat and pmat operator 12373565c898SBarry Smith - -snes_mf_operator - use matrix free only for the mat operator 12383565c898SBarry Smith 12393565c898SBarry Smith Level: intermediate 12403565c898SBarry Smith 12413565c898SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 12423565c898SBarry Smith 12433565c898SBarry Smith .seealso: SNESSetUseMatrixFree(), MatCreateSNESMF() 12443565c898SBarry Smith @*/ 12453565c898SBarry Smith PetscErrorCode SNESGetUseMatrixFree(SNES snes,PetscBool *mf_operator,PetscBool *mf) 12463565c898SBarry Smith { 12473565c898SBarry Smith PetscFunctionBegin; 12483565c898SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12493565c898SBarry Smith if (mf) *mf = snes->mf; 12503565c898SBarry Smith if (mf_operator) *mf_operator = snes->mf_operator; 12513565c898SBarry Smith PetscFunctionReturn(0); 12523565c898SBarry Smith } 12533565c898SBarry Smith 12543565c898SBarry Smith /*@ 1255c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 1256c8228a4eSBarry Smith at this time. 12579b94acceSBarry Smith 1258c7afd0dbSLois Curfman McInnes Not Collective 1259c7afd0dbSLois Curfman McInnes 12609b94acceSBarry Smith Input Parameter: 12619b94acceSBarry Smith . snes - SNES context 12629b94acceSBarry Smith 12639b94acceSBarry Smith Output Parameter: 12649b94acceSBarry Smith . iter - iteration number 12659b94acceSBarry Smith 1266c8228a4eSBarry Smith Notes: 1267c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 1268c8228a4eSBarry Smith 1269c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 127008405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 127108405cd6SLois Curfman McInnes .vb 127208405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 127308405cd6SLois Curfman McInnes if (!(it % 2)) { 127408405cd6SLois Curfman McInnes [compute Jacobian here] 127508405cd6SLois Curfman McInnes } 127608405cd6SLois Curfman McInnes .ve 1277c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 127808405cd6SLois Curfman McInnes recomputed every second SNES iteration. 1279c8228a4eSBarry Smith 1280c04deec6SBarry Smith After the SNES solve is complete this will return the number of nonlinear iterations used. 1281c04deec6SBarry Smith 128236851e7fSLois Curfman McInnes Level: intermediate 128336851e7fSLois Curfman McInnes 12842b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 12852b668275SBarry Smith 128671dbe336SPeter Brune .seealso: SNESGetLinearSolveIterations() 12879b94acceSBarry Smith @*/ 12887087cfbeSBarry Smith PetscErrorCode SNESGetIterationNumber(SNES snes,PetscInt *iter) 12899b94acceSBarry Smith { 12903a40ed3dSBarry Smith PetscFunctionBegin; 12910700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 12924482741eSBarry Smith PetscValidIntPointer(iter,2); 12939b94acceSBarry Smith *iter = snes->iter; 12943a40ed3dSBarry Smith PetscFunctionReturn(0); 12959b94acceSBarry Smith } 129674679c65SBarry Smith 1297360c497dSPeter Brune /*@ 1298360c497dSPeter Brune SNESSetIterationNumber - Sets the current iteration number. 1299360c497dSPeter Brune 1300360c497dSPeter Brune Not Collective 1301360c497dSPeter Brune 1302360c497dSPeter Brune Input Parameter: 1303360c497dSPeter Brune . snes - SNES context 1304360c497dSPeter Brune . iter - iteration number 1305360c497dSPeter Brune 1306360c497dSPeter Brune Level: developer 1307360c497dSPeter Brune 1308360c497dSPeter Brune .keywords: SNES, nonlinear, set, iteration, number, 1309360c497dSPeter Brune 131071dbe336SPeter Brune .seealso: SNESGetLinearSolveIterations() 1311360c497dSPeter Brune @*/ 1312360c497dSPeter Brune PetscErrorCode SNESSetIterationNumber(SNES snes,PetscInt iter) 1313360c497dSPeter Brune { 1314360c497dSPeter Brune PetscErrorCode ierr; 1315360c497dSPeter Brune 1316360c497dSPeter Brune PetscFunctionBegin; 1317360c497dSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1318e04113cfSBarry Smith ierr = PetscObjectSAWsTakeAccess((PetscObject)snes);CHKERRQ(ierr); 1319360c497dSPeter Brune snes->iter = iter; 1320e04113cfSBarry Smith ierr = PetscObjectSAWsGrantAccess((PetscObject)snes);CHKERRQ(ierr); 1321360c497dSPeter Brune PetscFunctionReturn(0); 1322360c497dSPeter Brune } 1323360c497dSPeter Brune 13249b94acceSBarry Smith /*@ 1325b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 13269b94acceSBarry Smith attempted by the nonlinear solver. 13279b94acceSBarry Smith 1328c7afd0dbSLois Curfman McInnes Not Collective 1329c7afd0dbSLois Curfman McInnes 13309b94acceSBarry Smith Input Parameter: 13319b94acceSBarry Smith . snes - SNES context 13329b94acceSBarry Smith 13339b94acceSBarry Smith Output Parameter: 13349b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 13359b94acceSBarry Smith 1336c96a6f78SLois Curfman McInnes Notes: 1337c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 1338c96a6f78SLois Curfman McInnes 133936851e7fSLois Curfman McInnes Level: intermediate 134036851e7fSLois Curfman McInnes 13419b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 134258ebbce7SBarry Smith 1343e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 134458ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 13459b94acceSBarry Smith @*/ 13467087cfbeSBarry Smith PetscErrorCode SNESGetNonlinearStepFailures(SNES snes,PetscInt *nfails) 13479b94acceSBarry Smith { 13483a40ed3dSBarry Smith PetscFunctionBegin; 13490700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13504482741eSBarry Smith PetscValidIntPointer(nfails,2); 135150ffb88aSMatthew Knepley *nfails = snes->numFailures; 135250ffb88aSMatthew Knepley PetscFunctionReturn(0); 135350ffb88aSMatthew Knepley } 135450ffb88aSMatthew Knepley 135550ffb88aSMatthew Knepley /*@ 1356b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 135750ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 135850ffb88aSMatthew Knepley 135950ffb88aSMatthew Knepley Not Collective 136050ffb88aSMatthew Knepley 136150ffb88aSMatthew Knepley Input Parameters: 136250ffb88aSMatthew Knepley + snes - SNES context 136350ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 136450ffb88aSMatthew Knepley 136550ffb88aSMatthew Knepley Level: intermediate 136650ffb88aSMatthew Knepley 136750ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 136858ebbce7SBarry Smith 1369e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 137058ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 137150ffb88aSMatthew Knepley @*/ 13727087cfbeSBarry Smith PetscErrorCode SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 137350ffb88aSMatthew Knepley { 137450ffb88aSMatthew Knepley PetscFunctionBegin; 13750700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 137650ffb88aSMatthew Knepley snes->maxFailures = maxFails; 137750ffb88aSMatthew Knepley PetscFunctionReturn(0); 137850ffb88aSMatthew Knepley } 137950ffb88aSMatthew Knepley 138050ffb88aSMatthew Knepley /*@ 1381b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 138250ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 138350ffb88aSMatthew Knepley 138450ffb88aSMatthew Knepley Not Collective 138550ffb88aSMatthew Knepley 138650ffb88aSMatthew Knepley Input Parameter: 138750ffb88aSMatthew Knepley . snes - SNES context 138850ffb88aSMatthew Knepley 138950ffb88aSMatthew Knepley Output Parameter: 139050ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 139150ffb88aSMatthew Knepley 139250ffb88aSMatthew Knepley Level: intermediate 139350ffb88aSMatthew Knepley 139450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 139558ebbce7SBarry Smith 1396e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 139758ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 139858ebbce7SBarry Smith 139950ffb88aSMatthew Knepley @*/ 14007087cfbeSBarry Smith PetscErrorCode SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 140150ffb88aSMatthew Knepley { 140250ffb88aSMatthew Knepley PetscFunctionBegin; 14030700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14044482741eSBarry Smith PetscValidIntPointer(maxFails,2); 140550ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 14063a40ed3dSBarry Smith PetscFunctionReturn(0); 14079b94acceSBarry Smith } 1408a847f771SSatish Balay 14092541af92SBarry Smith /*@ 14102541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 14112541af92SBarry Smith done by SNES. 14122541af92SBarry Smith 14132541af92SBarry Smith Not Collective 14142541af92SBarry Smith 14152541af92SBarry Smith Input Parameter: 14162541af92SBarry Smith . snes - SNES context 14172541af92SBarry Smith 14182541af92SBarry Smith Output Parameter: 14192541af92SBarry Smith . nfuncs - number of evaluations 14202541af92SBarry Smith 14212541af92SBarry Smith Level: intermediate 14222541af92SBarry Smith 142395452b02SPatrick Sanan Notes: 142495452b02SPatrick Sanan Reset every time SNESSolve is called unless SNESSetCountersReset() is used. 1425971e163fSPeter Brune 14262541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 142758ebbce7SBarry Smith 1428971e163fSPeter Brune .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), SNESSetCountersReset() 14292541af92SBarry Smith @*/ 14307087cfbeSBarry Smith PetscErrorCode SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 14312541af92SBarry Smith { 14322541af92SBarry Smith PetscFunctionBegin; 14330700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14342541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 14352541af92SBarry Smith *nfuncs = snes->nfuncs; 14362541af92SBarry Smith PetscFunctionReturn(0); 14372541af92SBarry Smith } 14382541af92SBarry Smith 14393d4c4710SBarry Smith /*@ 14403d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 14413d4c4710SBarry Smith linear solvers. 14423d4c4710SBarry Smith 14433d4c4710SBarry Smith Not Collective 14443d4c4710SBarry Smith 14453d4c4710SBarry Smith Input Parameter: 14463d4c4710SBarry Smith . snes - SNES context 14473d4c4710SBarry Smith 14483d4c4710SBarry Smith Output Parameter: 14493d4c4710SBarry Smith . nfails - number of failed solves 14503d4c4710SBarry Smith 14519d85da0cSMatthew G. Knepley Level: intermediate 14529d85da0cSMatthew G. Knepley 14539d85da0cSMatthew G. Knepley Options Database Keys: 14549d85da0cSMatthew G. Knepley . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated 14559d85da0cSMatthew G. Knepley 14563d4c4710SBarry Smith Notes: 14573d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 14583d4c4710SBarry Smith 14593d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 146058ebbce7SBarry Smith 1461e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 14623d4c4710SBarry Smith @*/ 14637087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveFailures(SNES snes,PetscInt *nfails) 14643d4c4710SBarry Smith { 14653d4c4710SBarry Smith PetscFunctionBegin; 14660700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14673d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 14683d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 14693d4c4710SBarry Smith PetscFunctionReturn(0); 14703d4c4710SBarry Smith } 14713d4c4710SBarry Smith 14723d4c4710SBarry Smith /*@ 14733d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 14743d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 14753d4c4710SBarry Smith 14763f9fe445SBarry Smith Logically Collective on SNES 14773d4c4710SBarry Smith 14783d4c4710SBarry Smith Input Parameters: 14793d4c4710SBarry Smith + snes - SNES context 14803d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 14813d4c4710SBarry Smith 14823d4c4710SBarry Smith Level: intermediate 14833d4c4710SBarry Smith 14849d85da0cSMatthew G. Knepley Options Database Keys: 14859d85da0cSMatthew G. Knepley . -snes_max_linear_solve_fail <num> - The number of failures before the solve is terminated 14869d85da0cSMatthew G. Knepley 148795452b02SPatrick Sanan Notes: 148895452b02SPatrick Sanan By default this is 0; that is SNES returns on the first failed linear solve 14893d4c4710SBarry Smith 14903d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 14913d4c4710SBarry Smith 149258ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 14933d4c4710SBarry Smith @*/ 14947087cfbeSBarry Smith PetscErrorCode SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 14953d4c4710SBarry Smith { 14963d4c4710SBarry Smith PetscFunctionBegin; 14970700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1498c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxFails,2); 14993d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 15003d4c4710SBarry Smith PetscFunctionReturn(0); 15013d4c4710SBarry Smith } 15023d4c4710SBarry Smith 15033d4c4710SBarry Smith /*@ 15043d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 15053d4c4710SBarry Smith are allowed before SNES terminates 15063d4c4710SBarry Smith 15073d4c4710SBarry Smith Not Collective 15083d4c4710SBarry Smith 15093d4c4710SBarry Smith Input Parameter: 15103d4c4710SBarry Smith . snes - SNES context 15113d4c4710SBarry Smith 15123d4c4710SBarry Smith Output Parameter: 15133d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 15143d4c4710SBarry Smith 15153d4c4710SBarry Smith Level: intermediate 15163d4c4710SBarry Smith 151795452b02SPatrick Sanan Notes: 151895452b02SPatrick Sanan By default this is 1; that is SNES returns on the first failed linear solve 15193d4c4710SBarry Smith 15203d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 15213d4c4710SBarry Smith 1522e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 15233d4c4710SBarry Smith @*/ 15247087cfbeSBarry Smith PetscErrorCode SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 15253d4c4710SBarry Smith { 15263d4c4710SBarry Smith PetscFunctionBegin; 15270700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15283d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 15293d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 15303d4c4710SBarry Smith PetscFunctionReturn(0); 15313d4c4710SBarry Smith } 15323d4c4710SBarry Smith 1533c96a6f78SLois Curfman McInnes /*@ 1534b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 1535c96a6f78SLois Curfman McInnes used by the nonlinear solver. 1536c96a6f78SLois Curfman McInnes 1537c7afd0dbSLois Curfman McInnes Not Collective 1538c7afd0dbSLois Curfman McInnes 1539c96a6f78SLois Curfman McInnes Input Parameter: 1540c96a6f78SLois Curfman McInnes . snes - SNES context 1541c96a6f78SLois Curfman McInnes 1542c96a6f78SLois Curfman McInnes Output Parameter: 1543c96a6f78SLois Curfman McInnes . lits - number of linear iterations 1544c96a6f78SLois Curfman McInnes 1545c96a6f78SLois Curfman McInnes Notes: 1546971e163fSPeter Brune This counter is reset to zero for each successive call to SNESSolve() unless SNESSetCountersReset() is used. 1547c96a6f78SLois Curfman McInnes 1548010be392SBarry 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 1549010be392SBarry Smith then call KSPGetIterationNumber() after the failed solve. 1550010be392SBarry Smith 155136851e7fSLois Curfman McInnes Level: intermediate 155236851e7fSLois Curfman McInnes 1553c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 15542b668275SBarry Smith 155571dbe336SPeter Brune .seealso: SNESGetIterationNumber(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESSetCountersReset() 1556c96a6f78SLois Curfman McInnes @*/ 15577087cfbeSBarry Smith PetscErrorCode SNESGetLinearSolveIterations(SNES snes,PetscInt *lits) 1558c96a6f78SLois Curfman McInnes { 15593a40ed3dSBarry Smith PetscFunctionBegin; 15600700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 15614482741eSBarry Smith PetscValidIntPointer(lits,2); 1562c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 15633a40ed3dSBarry Smith PetscFunctionReturn(0); 1564c96a6f78SLois Curfman McInnes } 1565c96a6f78SLois Curfman McInnes 1566971e163fSPeter Brune /*@ 1567971e163fSPeter Brune SNESSetCountersReset - Sets whether or not the counters for linear iterations and function evaluations 1568971e163fSPeter Brune are reset every time SNESSolve() is called. 1569971e163fSPeter Brune 1570971e163fSPeter Brune Logically Collective on SNES 1571971e163fSPeter Brune 1572971e163fSPeter Brune Input Parameter: 1573971e163fSPeter Brune + snes - SNES context 1574971e163fSPeter Brune - reset - whether to reset the counters or not 1575971e163fSPeter Brune 1576971e163fSPeter Brune Notes: 1577fa19ca70SBarry Smith This defaults to PETSC_TRUE 1578971e163fSPeter Brune 1579971e163fSPeter Brune Level: developer 1580971e163fSPeter Brune 1581971e163fSPeter Brune .keywords: SNES, nonlinear, set, reset, number, linear, iterations 1582971e163fSPeter Brune 1583734794cfSBarry Smith .seealso: SNESGetNumberFunctionEvals(), SNESGetLinearSolveIterations(), SNESGetNPC() 1584971e163fSPeter Brune @*/ 1585971e163fSPeter Brune PetscErrorCode SNESSetCountersReset(SNES snes,PetscBool reset) 1586971e163fSPeter Brune { 1587971e163fSPeter Brune PetscFunctionBegin; 1588971e163fSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1589971e163fSPeter Brune PetscValidLogicalCollectiveBool(snes,reset,2); 1590971e163fSPeter Brune snes->counters_reset = reset; 1591971e163fSPeter Brune PetscFunctionReturn(0); 1592971e163fSPeter Brune } 1593971e163fSPeter Brune 159482bf6240SBarry Smith 15952999313aSBarry Smith /*@ 15962999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 15972999313aSBarry Smith 15982999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 15992999313aSBarry Smith 16002999313aSBarry Smith Input Parameters: 16012999313aSBarry Smith + snes - the SNES context 16022999313aSBarry Smith - ksp - the KSP context 16032999313aSBarry Smith 16042999313aSBarry Smith Notes: 16052999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 16062999313aSBarry Smith so this routine is rarely needed. 16072999313aSBarry Smith 16082999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 16092999313aSBarry Smith decreased by one. 16102999313aSBarry Smith 16112999313aSBarry Smith Level: developer 16122999313aSBarry Smith 16132999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 16142999313aSBarry Smith 16152999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 16162999313aSBarry Smith @*/ 16177087cfbeSBarry Smith PetscErrorCode SNESSetKSP(SNES snes,KSP ksp) 16182999313aSBarry Smith { 16192999313aSBarry Smith PetscErrorCode ierr; 16202999313aSBarry Smith 16212999313aSBarry Smith PetscFunctionBegin; 16220700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 16230700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 16242999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 16257dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 1626906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 16272999313aSBarry Smith snes->ksp = ksp; 16282999313aSBarry Smith PetscFunctionReturn(0); 16292999313aSBarry Smith } 16302999313aSBarry Smith 16319b94acceSBarry Smith /* -----------------------------------------------------------*/ 163252baeb72SSatish Balay /*@ 16339b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 16349b94acceSBarry Smith 1635c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 1636c7afd0dbSLois Curfman McInnes 1637c7afd0dbSLois Curfman McInnes Input Parameters: 1638906ed7ccSBarry Smith . comm - MPI communicator 16399b94acceSBarry Smith 16409b94acceSBarry Smith Output Parameter: 16419b94acceSBarry Smith . outsnes - the new SNES context 16429b94acceSBarry Smith 1643c7afd0dbSLois Curfman McInnes Options Database Keys: 1644c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 1645c7afd0dbSLois Curfman McInnes and no preconditioning matrix 1646c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 1647c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 1648c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 1649c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 1650c1f60f51SBarry Smith 165136851e7fSLois Curfman McInnes Level: beginner 165236851e7fSLois Curfman McInnes 165395452b02SPatrick Sanan Developer Notes: 165495452b02SPatrick Sanan SNES always creates a KSP object even though many SNES methods do not use it. This is 1655efd4aadfSBarry Smith unfortunate and should be fixed at some point. The flag snes->usesksp indicates if the 1656efd4aadfSBarry Smith particular method does use KSP and regulates if the information about the KSP is printed 1657efd4aadfSBarry Smith in SNESView(). TSSetFromOptions() does call SNESSetFromOptions() which can lead to users being confused 1658efd4aadfSBarry Smith by help messages about meaningless SNES options. 1659efd4aadfSBarry Smith 1660efd4aadfSBarry Smith SNES always creates the snes->kspconvctx even though it is used by only one type. This should 1661efd4aadfSBarry Smith be fixed. 1662efd4aadfSBarry Smith 16639b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 16649b94acceSBarry Smith 1665a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 1666a8054027SBarry Smith 16679b94acceSBarry Smith @*/ 16687087cfbeSBarry Smith PetscErrorCode SNESCreate(MPI_Comm comm,SNES *outsnes) 16699b94acceSBarry Smith { 1670dfbe8321SBarry Smith PetscErrorCode ierr; 16719b94acceSBarry Smith SNES snes; 1672fa9f3622SBarry Smith SNESKSPEW *kctx; 167337fcc0dbSBarry Smith 16743a40ed3dSBarry Smith PetscFunctionBegin; 1675ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 16760298fd71SBarry Smith *outsnes = NULL; 1677607a6623SBarry Smith ierr = SNESInitializePackage();CHKERRQ(ierr); 16788ba1e511SMatthew Knepley 167973107ff1SLisandro Dalcin ierr = PetscHeaderCreate(snes,SNES_CLASSID,"SNES","Nonlinear solver","SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 16807adad957SLisandro Dalcin 16818d359177SBarry Smith snes->ops->converged = SNESConvergedDefault; 16822c155ee1SBarry Smith snes->usesksp = PETSC_TRUE; 168388976e71SPeter Brune snes->tolerancesset = PETSC_FALSE; 16849b94acceSBarry Smith snes->max_its = 50; 16859750a799SBarry Smith snes->max_funcs = 10000; 16869b94acceSBarry Smith snes->norm = 0.0; 1687c1e67a49SFande Kong snes->xnorm = 0.0; 1688c1e67a49SFande Kong snes->ynorm = 0.0; 1689365a6726SPeter Brune snes->normschedule = SNES_NORM_ALWAYS; 16906c67d002SPeter Brune snes->functype = SNES_FUNCTION_DEFAULT; 16913a2046daSBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 16923a2046daSBarry Smith snes->rtol = 1.e-5; 16933a2046daSBarry Smith #else 1694b4874afaSBarry Smith snes->rtol = 1.e-8; 16953a2046daSBarry Smith #endif 1696b4874afaSBarry Smith snes->ttol = 0.0; 16973a2046daSBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 16983a2046daSBarry Smith snes->abstol = 1.e-25; 16993a2046daSBarry Smith #else 170070441072SBarry Smith snes->abstol = 1.e-50; 17013a2046daSBarry Smith #endif 17027cd0ae37SLisandro Dalcin #if defined(PETSC_USE_REAL_SINGLE) 17037cd0ae37SLisandro Dalcin snes->stol = 1.e-5; 17047cd0ae37SLisandro Dalcin #else 1705c60f73f4SPeter Brune snes->stol = 1.e-8; 17067cd0ae37SLisandro Dalcin #endif 17073a2046daSBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 17083a2046daSBarry Smith snes->deltatol = 1.e-6; 17093a2046daSBarry Smith #else 17104b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 17113a2046daSBarry Smith #endif 1712e37c518bSBarry Smith snes->divtol = 1.e4; 1713e37c518bSBarry Smith snes->rnorm0 = 0; 17149b94acceSBarry Smith snes->nfuncs = 0; 171550ffb88aSMatthew Knepley snes->numFailures = 0; 171650ffb88aSMatthew Knepley snes->maxFailures = 1; 17177a00f4a9SLois Curfman McInnes snes->linear_its = 0; 1718e35cf81dSBarry Smith snes->lagjacobian = 1; 171937ec4e1aSPeter Brune snes->jac_iter = 0; 172037ec4e1aSPeter Brune snes->lagjac_persist = PETSC_FALSE; 1721a8054027SBarry Smith snes->lagpreconditioner = 1; 172237ec4e1aSPeter Brune snes->pre_iter = 0; 172337ec4e1aSPeter Brune snes->lagpre_persist = PETSC_FALSE; 1724639f9d9dSBarry Smith snes->numbermonitors = 0; 17259b94acceSBarry Smith snes->data = 0; 17264dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 1727186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 17286f24a144SLois Curfman McInnes snes->nwork = 0; 172958c9b817SLisandro Dalcin snes->work = 0; 173058c9b817SLisandro Dalcin snes->nvwork = 0; 173158c9b817SLisandro Dalcin snes->vwork = 0; 1732758f92a0SBarry Smith snes->conv_hist_len = 0; 1733758f92a0SBarry Smith snes->conv_hist_max = 0; 17340298fd71SBarry Smith snes->conv_hist = NULL; 17350298fd71SBarry Smith snes->conv_hist_its = NULL; 1736758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 1737971e163fSPeter Brune snes->counters_reset = PETSC_TRUE; 1738e4ed7901SPeter Brune snes->vec_func_init_set = PETSC_FALSE; 1739184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 1740efd4aadfSBarry Smith snes->npcside = PC_RIGHT; 1741b3cd9a81SMatthew G. Knepley snes->setfromoptionscalled = 0; 1742c40d0f55SPeter Brune 1743d8f46077SPeter Brune snes->mf = PETSC_FALSE; 1744d8f46077SPeter Brune snes->mf_operator = PETSC_FALSE; 1745d8f46077SPeter Brune snes->mf_version = 1; 1746d8f46077SPeter Brune 17473d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 17483d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 17493d4c4710SBarry Smith 1750349187a7SBarry Smith snes->vizerotolerance = 1.e-8; 1751*b351a90bSFande Kong #if defined(PETSC_USE_DEBUG) 1752*b351a90bSFande Kong snes->checkjacdomainerror = PETSC_TRUE; 1753*b351a90bSFande Kong #else 1754*b351a90bSFande Kong snes->checkjacdomainerror = PETSC_FALSE; 1755*b351a90bSFande Kong #endif 1756349187a7SBarry Smith 17574fc747eaSLawrence Mitchell /* Set this to true if the implementation of SNESSolve_XXX does compute the residual at the final solution. */ 17584fc747eaSLawrence Mitchell snes->alwayscomputesfinalresidual = PETSC_FALSE; 17594fc747eaSLawrence Mitchell 17609b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 1761b00a9115SJed Brown ierr = PetscNewLog(snes,&kctx);CHKERRQ(ierr); 1762f5af7f23SKarl Rupp 17639b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 17649b94acceSBarry Smith kctx->version = 2; 17659b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 17669b94acceSBarry Smith this was too large for some test cases */ 176775567043SBarry Smith kctx->rtol_last = 0.0; 17689b94acceSBarry Smith kctx->rtol_max = .9; 17699b94acceSBarry Smith kctx->gamma = 1.0; 177062d1f40fSBarry Smith kctx->alpha = .5*(1.0 + PetscSqrtReal(5.0)); 177171f87433Sdalcinl kctx->alpha2 = kctx->alpha; 17729b94acceSBarry Smith kctx->threshold = .1; 177375567043SBarry Smith kctx->lresid_last = 0.0; 177475567043SBarry Smith kctx->norm_last = 0.0; 17759b94acceSBarry Smith 17769b94acceSBarry Smith *outsnes = snes; 17773a40ed3dSBarry Smith PetscFunctionReturn(0); 17789b94acceSBarry Smith } 17799b94acceSBarry Smith 178088f0584fSBarry Smith /*MC 1781411c0326SBarry Smith SNESFunction - Functional form used to convey the nonlinear function to be solved by SNES 178288f0584fSBarry Smith 178388f0584fSBarry Smith Synopsis: 1784411c0326SBarry Smith #include "petscsnes.h" 1785411c0326SBarry Smith PetscErrorCode SNESFunction(SNES snes,Vec x,Vec f,void *ctx); 178688f0584fSBarry Smith 178788f0584fSBarry Smith Input Parameters: 178888f0584fSBarry Smith + snes - the SNES context 178988f0584fSBarry Smith . x - state at which to evaluate residual 179088f0584fSBarry Smith - ctx - optional user-defined function context, passed in with SNESSetFunction() 179188f0584fSBarry Smith 179288f0584fSBarry Smith Output Parameter: 179388f0584fSBarry Smith . f - vector to put residual (function value) 179488f0584fSBarry Smith 1795878cb397SSatish Balay Level: intermediate 1796878cb397SSatish Balay 179788f0584fSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 179888f0584fSBarry Smith M*/ 179988f0584fSBarry Smith 18009b94acceSBarry Smith /*@C 18019b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 18029b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 18039b94acceSBarry Smith equations. 18049b94acceSBarry Smith 18053f9fe445SBarry Smith Logically Collective on SNES 1806fee21e36SBarry Smith 1807c7afd0dbSLois Curfman McInnes Input Parameters: 1808c7afd0dbSLois Curfman McInnes + snes - the SNES context 1809c7afd0dbSLois Curfman McInnes . r - vector to store function value 1810f8b49ee9SBarry Smith . f - function evaluation routine; see SNESFunction for calling sequence details 1811c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 18120298fd71SBarry Smith function evaluation routine (may be NULL) 18139b94acceSBarry Smith 18149b94acceSBarry Smith Notes: 18159b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 18169b94acceSBarry Smith $ f'(x) x = -f(x), 1817c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 18189b94acceSBarry Smith 181936851e7fSLois Curfman McInnes Level: beginner 182036851e7fSLois Curfman McInnes 18219b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 18229b94acceSBarry Smith 1823bf388a1fSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetPicard(), SNESFunction 18249b94acceSBarry Smith @*/ 1825f8b49ee9SBarry Smith PetscErrorCode SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*f)(SNES,Vec,Vec,void*),void *ctx) 18269b94acceSBarry Smith { 182785385478SLisandro Dalcin PetscErrorCode ierr; 18286cab3a1bSJed Brown DM dm; 18296cab3a1bSJed Brown 18303a40ed3dSBarry Smith PetscFunctionBegin; 18310700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1832d2a683ecSLisandro Dalcin if (r) { 1833d2a683ecSLisandro Dalcin PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1834d2a683ecSLisandro Dalcin PetscCheckSameComm(snes,1,r,2); 183585385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 18366bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 1837f5af7f23SKarl Rupp 183885385478SLisandro Dalcin snes->vec_func = r; 1839d2a683ecSLisandro Dalcin } 18406cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 1841f8b49ee9SBarry Smith ierr = DMSNESSetFunction(dm,f,ctx);CHKERRQ(ierr); 18423a40ed3dSBarry Smith PetscFunctionReturn(0); 18439b94acceSBarry Smith } 18449b94acceSBarry Smith 1845646217ecSPeter Brune 1846e4ed7901SPeter Brune /*@C 1847e4ed7901SPeter Brune SNESSetInitialFunction - Sets the function vector to be used as the 1848e4ed7901SPeter Brune function norm at the initialization of the method. In some 1849e4ed7901SPeter Brune instances, the user has precomputed the function before calling 1850e4ed7901SPeter Brune SNESSolve. This function allows one to avoid a redundant call 1851e4ed7901SPeter Brune to SNESComputeFunction in that case. 1852e4ed7901SPeter Brune 1853e4ed7901SPeter Brune Logically Collective on SNES 1854e4ed7901SPeter Brune 1855e4ed7901SPeter Brune Input Parameters: 1856e4ed7901SPeter Brune + snes - the SNES context 1857e4ed7901SPeter Brune - f - vector to store function value 1858e4ed7901SPeter Brune 1859e4ed7901SPeter Brune Notes: 1860e4ed7901SPeter Brune This should not be modified during the solution procedure. 1861e4ed7901SPeter Brune 1862e4ed7901SPeter Brune This is used extensively in the SNESFAS hierarchy and in nonlinear preconditioning. 1863e4ed7901SPeter Brune 1864e4ed7901SPeter Brune Level: developer 1865e4ed7901SPeter Brune 1866e4ed7901SPeter Brune .keywords: SNES, nonlinear, set, function 1867e4ed7901SPeter Brune 1868e4ed7901SPeter Brune .seealso: SNESSetFunction(), SNESComputeFunction(), SNESSetInitialFunctionNorm() 1869e4ed7901SPeter Brune @*/ 1870e4ed7901SPeter Brune PetscErrorCode SNESSetInitialFunction(SNES snes, Vec f) 1871e4ed7901SPeter Brune { 1872e4ed7901SPeter Brune PetscErrorCode ierr; 1873e4ed7901SPeter Brune Vec vec_func; 1874e4ed7901SPeter Brune 1875e4ed7901SPeter Brune PetscFunctionBegin; 1876e4ed7901SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1877e4ed7901SPeter Brune PetscValidHeaderSpecific(f,VEC_CLASSID,2); 1878e4ed7901SPeter Brune PetscCheckSameComm(snes,1,f,2); 1879efd4aadfSBarry Smith if (snes->npcside== PC_LEFT && snes->functype == SNES_FUNCTION_PRECONDITIONED) { 1880902f982fSPeter Brune snes->vec_func_init_set = PETSC_FALSE; 1881902f982fSPeter Brune PetscFunctionReturn(0); 1882902f982fSPeter Brune } 18830298fd71SBarry Smith ierr = SNESGetFunction(snes,&vec_func,NULL,NULL);CHKERRQ(ierr); 1884e4ed7901SPeter Brune ierr = VecCopy(f, vec_func);CHKERRQ(ierr); 1885f5af7f23SKarl Rupp 1886217b9c2eSPeter Brune snes->vec_func_init_set = PETSC_TRUE; 1887e4ed7901SPeter Brune PetscFunctionReturn(0); 1888e4ed7901SPeter Brune } 1889e4ed7901SPeter Brune 1890534ebe21SPeter Brune /*@ 1891365a6726SPeter Brune SNESSetNormSchedule - Sets the SNESNormSchedule used in covergence and monitoring 1892534ebe21SPeter Brune of the SNES method. 1893534ebe21SPeter Brune 1894534ebe21SPeter Brune Logically Collective on SNES 1895534ebe21SPeter Brune 1896534ebe21SPeter Brune Input Parameters: 1897534ebe21SPeter Brune + snes - the SNES context 1898365a6726SPeter Brune - normschedule - the frequency of norm computation 1899534ebe21SPeter Brune 1900517f1916SMatthew G. Knepley Options Database Key: 1901517f1916SMatthew G. Knepley . -snes_norm_schedule <none, always, initialonly, finalonly, initalfinalonly> 1902517f1916SMatthew G. Knepley 1903534ebe21SPeter Brune Notes: 1904365a6726SPeter Brune Only certain SNES methods support certain SNESNormSchedules. Most require evaluation 1905534ebe21SPeter Brune of the nonlinear function and the taking of its norm at every iteration to 1906534ebe21SPeter Brune even ensure convergence at all. However, methods such as custom Gauss-Seidel methods 1907be95d8f1SBarry Smith (SNESNGS) and the like do not require the norm of the function to be computed, and therfore 1908534ebe21SPeter Brune may either be monitored for convergence or not. As these are often used as nonlinear 1909534ebe21SPeter Brune preconditioners, monitoring the norm of their error is not a useful enterprise within 1910534ebe21SPeter Brune their solution. 1911534ebe21SPeter Brune 1912534ebe21SPeter Brune Level: developer 1913534ebe21SPeter Brune 1914534ebe21SPeter Brune .keywords: SNES, nonlinear, set, function, norm, type 1915534ebe21SPeter Brune 1916365a6726SPeter Brune .seealso: SNESGetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule 1917534ebe21SPeter Brune @*/ 1918365a6726SPeter Brune PetscErrorCode SNESSetNormSchedule(SNES snes, SNESNormSchedule normschedule) 1919534ebe21SPeter Brune { 1920534ebe21SPeter Brune PetscFunctionBegin; 1921534ebe21SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1922365a6726SPeter Brune snes->normschedule = normschedule; 1923534ebe21SPeter Brune PetscFunctionReturn(0); 1924534ebe21SPeter Brune } 1925534ebe21SPeter Brune 1926534ebe21SPeter Brune 1927534ebe21SPeter Brune /*@ 1928365a6726SPeter Brune SNESGetNormSchedule - Gets the SNESNormSchedule used in covergence and monitoring 1929534ebe21SPeter Brune of the SNES method. 1930534ebe21SPeter Brune 1931534ebe21SPeter Brune Logically Collective on SNES 1932534ebe21SPeter Brune 1933534ebe21SPeter Brune Input Parameters: 1934534ebe21SPeter Brune + snes - the SNES context 1935365a6726SPeter Brune - normschedule - the type of the norm used 1936534ebe21SPeter Brune 1937534ebe21SPeter Brune Level: advanced 1938534ebe21SPeter Brune 1939534ebe21SPeter Brune .keywords: SNES, nonlinear, set, function, norm, type 1940534ebe21SPeter Brune 1941365a6726SPeter Brune .seealso: SNESSetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule 1942534ebe21SPeter Brune @*/ 1943365a6726SPeter Brune PetscErrorCode SNESGetNormSchedule(SNES snes, SNESNormSchedule *normschedule) 1944534ebe21SPeter Brune { 1945534ebe21SPeter Brune PetscFunctionBegin; 1946534ebe21SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1947365a6726SPeter Brune *normschedule = snes->normschedule; 1948534ebe21SPeter Brune PetscFunctionReturn(0); 1949534ebe21SPeter Brune } 1950534ebe21SPeter Brune 195147073ea2SPeter Brune 1952c5ce4427SMatthew G. Knepley /*@ 1953c5ce4427SMatthew G. Knepley SNESSetFunctionNorm - Sets the last computed residual norm. 1954c5ce4427SMatthew G. Knepley 1955c5ce4427SMatthew G. Knepley Logically Collective on SNES 1956c5ce4427SMatthew G. Knepley 1957c5ce4427SMatthew G. Knepley Input Parameters: 1958c5ce4427SMatthew G. Knepley + snes - the SNES context 1959c5ce4427SMatthew G. Knepley 1960c5ce4427SMatthew G. Knepley - normschedule - the frequency of norm computation 1961c5ce4427SMatthew G. Knepley 1962c5ce4427SMatthew G. Knepley Level: developer 1963c5ce4427SMatthew G. Knepley 1964c5ce4427SMatthew G. Knepley .keywords: SNES, nonlinear, set, function, norm, type 1965c5ce4427SMatthew G. Knepley .seealso: SNESGetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule 1966c5ce4427SMatthew G. Knepley @*/ 1967c5ce4427SMatthew G. Knepley PetscErrorCode SNESSetFunctionNorm(SNES snes, PetscReal norm) 1968c5ce4427SMatthew G. Knepley { 1969c5ce4427SMatthew G. Knepley PetscFunctionBegin; 1970c5ce4427SMatthew G. Knepley PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1971c5ce4427SMatthew G. Knepley snes->norm = norm; 1972c5ce4427SMatthew G. Knepley PetscFunctionReturn(0); 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: 1981c5ce4427SMatthew G. Knepley . 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 1988c5ce4427SMatthew G. Knepley .keywords: SNES, nonlinear, set, function, norm, type 1989c5ce4427SMatthew G. Knepley .seealso: SNESSetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule 1990c5ce4427SMatthew G. Knepley @*/ 1991c5ce4427SMatthew G. Knepley PetscErrorCode SNESGetFunctionNorm(SNES snes, PetscReal *norm) 1992c5ce4427SMatthew G. Knepley { 1993c5ce4427SMatthew G. Knepley PetscFunctionBegin; 1994c5ce4427SMatthew G. Knepley PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1995c5ce4427SMatthew G. Knepley PetscValidPointer(norm, 2); 1996c5ce4427SMatthew G. Knepley *norm = snes->norm; 1997c5ce4427SMatthew G. Knepley PetscFunctionReturn(0); 1998c5ce4427SMatthew G. Knepley } 1999c5ce4427SMatthew G. Knepley 2000c1e67a49SFande Kong /*@ 2001c1e67a49SFande Kong SNESGetUpdateNorm - Gets the last computed norm of the Newton update 2002c1e67a49SFande Kong 2003c1e67a49SFande Kong Not Collective 2004c1e67a49SFande Kong 2005c1e67a49SFande Kong Input Parameter: 2006c1e67a49SFande Kong . snes - the SNES context 2007c1e67a49SFande Kong 2008c1e67a49SFande Kong Output Parameter: 2009c1e67a49SFande Kong . ynorm - the last computed update norm 2010c1e67a49SFande Kong 2011c1e67a49SFande Kong Level: developer 2012c1e67a49SFande Kong 2013c1e67a49SFande Kong .keywords: SNES, nonlinear, set, function, norm, type 2014c1e67a49SFande Kong .seealso: SNESSetNormSchedule(), SNESComputeFunction(), SNESGetFunctionNorm() 2015c1e67a49SFande Kong @*/ 2016c1e67a49SFande Kong PetscErrorCode SNESGetUpdateNorm(SNES snes, PetscReal *ynorm) 2017c1e67a49SFande Kong { 2018c1e67a49SFande Kong PetscFunctionBegin; 2019c1e67a49SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2020c1e67a49SFande Kong PetscValidPointer(ynorm, 2); 2021c1e67a49SFande Kong *ynorm = snes->ynorm; 2022c1e67a49SFande Kong PetscFunctionReturn(0); 2023c1e67a49SFande Kong } 2024c1e67a49SFande Kong 2025c1e67a49SFande Kong /*@ 20264591eaf2SFande Kong SNESGetSolutionNorm - Gets the last computed norm of the solution 2027c1e67a49SFande Kong 2028c1e67a49SFande Kong Not Collective 2029c1e67a49SFande Kong 2030c1e67a49SFande Kong Input Parameter: 2031c1e67a49SFande Kong . snes - the SNES context 2032c1e67a49SFande Kong 2033c1e67a49SFande Kong Output Parameter: 2034c1e67a49SFande Kong . xnorm - the last computed solution norm 2035c1e67a49SFande Kong 2036c1e67a49SFande Kong Level: developer 2037c1e67a49SFande Kong 2038c1e67a49SFande Kong .keywords: SNES, nonlinear, set, function, norm, type 2039c1e67a49SFande Kong .seealso: SNESSetNormSchedule(), SNESComputeFunction(), SNESGetFunctionNorm(), SNESGetUpdateNorm() 2040c1e67a49SFande Kong @*/ 2041c1e67a49SFande Kong PetscErrorCode SNESGetSolutionNorm(SNES snes, PetscReal *xnorm) 2042c1e67a49SFande Kong { 2043c1e67a49SFande Kong PetscFunctionBegin; 2044c1e67a49SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2045c1e67a49SFande Kong PetscValidPointer(xnorm, 2); 2046c1e67a49SFande Kong *xnorm = snes->xnorm; 2047c1e67a49SFande Kong PetscFunctionReturn(0); 2048c1e67a49SFande Kong } 2049c1e67a49SFande Kong 205047073ea2SPeter Brune /*@C 205147073ea2SPeter Brune SNESSetFunctionType - Sets the SNESNormSchedule used in covergence and monitoring 205247073ea2SPeter Brune of the SNES method. 205347073ea2SPeter Brune 205447073ea2SPeter Brune Logically Collective on SNES 205547073ea2SPeter Brune 205647073ea2SPeter Brune Input Parameters: 205747073ea2SPeter Brune + snes - the SNES context 205847073ea2SPeter Brune - normschedule - the frequency of norm computation 205947073ea2SPeter Brune 206047073ea2SPeter Brune Notes: 206147073ea2SPeter Brune Only certain SNES methods support certain SNESNormSchedules. Most require evaluation 206247073ea2SPeter Brune of the nonlinear function and the taking of its norm at every iteration to 206347073ea2SPeter Brune even ensure convergence at all. However, methods such as custom Gauss-Seidel methods 2064be95d8f1SBarry Smith (SNESNGS) and the like do not require the norm of the function to be computed, and therfore 206547073ea2SPeter Brune may either be monitored for convergence or not. As these are often used as nonlinear 206647073ea2SPeter Brune preconditioners, monitoring the norm of their error is not a useful enterprise within 206747073ea2SPeter Brune their solution. 206847073ea2SPeter Brune 206947073ea2SPeter Brune Level: developer 207047073ea2SPeter Brune 207147073ea2SPeter Brune .keywords: SNES, nonlinear, set, function, norm, type 207247073ea2SPeter Brune 207347073ea2SPeter Brune .seealso: SNESGetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule 207447073ea2SPeter Brune @*/ 207547073ea2SPeter Brune PetscErrorCode SNESSetFunctionType(SNES snes, SNESFunctionType type) 207647073ea2SPeter Brune { 207747073ea2SPeter Brune PetscFunctionBegin; 207847073ea2SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 207947073ea2SPeter Brune snes->functype = type; 208047073ea2SPeter Brune PetscFunctionReturn(0); 208147073ea2SPeter Brune } 208247073ea2SPeter Brune 208347073ea2SPeter Brune 208447073ea2SPeter Brune /*@C 208547073ea2SPeter Brune SNESGetFunctionType - Gets the SNESNormSchedule used in covergence and monitoring 208647073ea2SPeter Brune of the SNES method. 208747073ea2SPeter Brune 208847073ea2SPeter Brune Logically Collective on SNES 208947073ea2SPeter Brune 209047073ea2SPeter Brune Input Parameters: 209147073ea2SPeter Brune + snes - the SNES context 209247073ea2SPeter Brune - normschedule - the type of the norm used 209347073ea2SPeter Brune 209447073ea2SPeter Brune Level: advanced 209547073ea2SPeter Brune 209647073ea2SPeter Brune .keywords: SNES, nonlinear, set, function, norm, type 209747073ea2SPeter Brune 209847073ea2SPeter Brune .seealso: SNESSetNormSchedule(), SNESComputeFunction(), VecNorm(), SNESSetFunction(), SNESSetInitialFunction(), SNESNormSchedule 209947073ea2SPeter Brune @*/ 210047073ea2SPeter Brune PetscErrorCode SNESGetFunctionType(SNES snes, SNESFunctionType *type) 210147073ea2SPeter Brune { 210247073ea2SPeter Brune PetscFunctionBegin; 210347073ea2SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 210447073ea2SPeter Brune *type = snes->functype; 2105534ebe21SPeter Brune PetscFunctionReturn(0); 2106534ebe21SPeter Brune } 2107534ebe21SPeter Brune 2108bf388a1fSBarry Smith /*MC 2109be95d8f1SBarry Smith SNESNGSFunction - function used to convey a Gauss-Seidel sweep on the nonlinear function 2110bf388a1fSBarry Smith 2111bf388a1fSBarry Smith Synopsis: 2112aaa7dc30SBarry Smith #include <petscsnes.h> 2113be95d8f1SBarry Smith $ SNESNGSFunction(SNES snes,Vec x,Vec b,void *ctx); 2114bf388a1fSBarry Smith 2115bf388a1fSBarry Smith + X - solution vector 2116bf388a1fSBarry Smith . B - RHS vector 2117bf388a1fSBarry Smith - ctx - optional user-defined Gauss-Seidel context 2118bf388a1fSBarry Smith 2119878cb397SSatish Balay Level: intermediate 2120878cb397SSatish Balay 2121be95d8f1SBarry Smith .seealso: SNESSetNGS(), SNESGetNGS() 2122bf388a1fSBarry Smith M*/ 2123bf388a1fSBarry Smith 2124c79ef259SPeter Brune /*@C 2125be95d8f1SBarry Smith SNESSetNGS - Sets the user nonlinear Gauss-Seidel routine for 2126c79ef259SPeter Brune use with composed nonlinear solvers. 2127c79ef259SPeter Brune 2128c79ef259SPeter Brune Input Parameters: 2129c79ef259SPeter Brune + snes - the SNES context 2130be95d8f1SBarry Smith . f - function evaluation routine to apply Gauss-Seidel see SNESNGSFunction 2131c79ef259SPeter Brune - ctx - [optional] user-defined context for private data for the 21320298fd71SBarry Smith smoother evaluation routine (may be NULL) 2133c79ef259SPeter Brune 2134c79ef259SPeter Brune Notes: 2135be95d8f1SBarry Smith The NGS routines are used by the composed nonlinear solver to generate 2136c79ef259SPeter Brune a problem appropriate update to the solution, particularly FAS. 2137c79ef259SPeter Brune 2138d28543b3SPeter Brune Level: intermediate 2139c79ef259SPeter Brune 2140d28543b3SPeter Brune .keywords: SNES, nonlinear, set, Gauss-Seidel 2141c79ef259SPeter Brune 2142be95d8f1SBarry Smith .seealso: SNESGetFunction(), SNESComputeNGS() 2143c79ef259SPeter Brune @*/ 2144be95d8f1SBarry Smith PetscErrorCode SNESSetNGS(SNES snes,PetscErrorCode (*f)(SNES,Vec,Vec,void*),void *ctx) 21456cab3a1bSJed Brown { 21466cab3a1bSJed Brown PetscErrorCode ierr; 21476cab3a1bSJed Brown DM dm; 21486cab3a1bSJed Brown 2149646217ecSPeter Brune PetscFunctionBegin; 21506cab3a1bSJed Brown PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 21516cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 2152be95d8f1SBarry Smith ierr = DMSNESSetNGS(dm,f,ctx);CHKERRQ(ierr); 2153646217ecSPeter Brune PetscFunctionReturn(0); 2154646217ecSPeter Brune } 2155646217ecSPeter Brune 215625acbd8eSLisandro Dalcin PetscErrorCode SNESPicardComputeFunction(SNES snes,Vec x,Vec f,void *ctx) 21578b0a5094SBarry Smith { 21588b0a5094SBarry Smith PetscErrorCode ierr; 2159e03ab78fSPeter Brune DM dm; 2160942e3340SBarry Smith DMSNES sdm; 21616cab3a1bSJed Brown 21628b0a5094SBarry Smith PetscFunctionBegin; 2163e03ab78fSPeter Brune ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 2164942e3340SBarry Smith ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 216525acbd8eSLisandro Dalcin if (!sdm->ops->computepfunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetPicard() to provide Picard function."); 216625acbd8eSLisandro Dalcin if (!sdm->ops->computepjacobian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetPicard() to provide Picard Jacobian."); 21678b0a5094SBarry Smith /* A(x)*x - b(x) */ 216825acbd8eSLisandro Dalcin PetscStackPush("SNES Picard user function"); 216922c6f798SBarry Smith ierr = (*sdm->ops->computepfunction)(snes,x,f,sdm->pctx);CHKERRQ(ierr); 217025acbd8eSLisandro Dalcin PetscStackPop; 217125acbd8eSLisandro Dalcin PetscStackPush("SNES Picard user Jacobian"); 2172d1e9a80fSBarry Smith ierr = (*sdm->ops->computepjacobian)(snes,x,snes->jacobian,snes->jacobian_pre,sdm->pctx);CHKERRQ(ierr); 217325acbd8eSLisandro Dalcin PetscStackPop; 21748b0a5094SBarry Smith ierr = VecScale(f,-1.0);CHKERRQ(ierr); 217595eabcedSBarry Smith ierr = MatMultAdd(snes->jacobian,x,f,f);CHKERRQ(ierr); 21768b0a5094SBarry Smith PetscFunctionReturn(0); 21778b0a5094SBarry Smith } 21788b0a5094SBarry Smith 217925acbd8eSLisandro Dalcin PetscErrorCode SNESPicardComputeJacobian(SNES snes,Vec x1,Mat J,Mat B,void *ctx) 21808b0a5094SBarry Smith { 21818b0a5094SBarry Smith PetscFunctionBegin; 2182e03ab78fSPeter Brune /* the jacobian matrix should be pre-filled in SNESPicardComputeFunction */ 21838b0a5094SBarry Smith PetscFunctionReturn(0); 21848b0a5094SBarry Smith } 21858b0a5094SBarry Smith 21868b0a5094SBarry Smith /*@C 21870d04baf8SBarry Smith SNESSetPicard - Use SNES to solve the semilinear-system A(x) x = b(x) via a Picard type iteration (Picard linearization) 21888b0a5094SBarry Smith 21898b0a5094SBarry Smith Logically Collective on SNES 21908b0a5094SBarry Smith 21918b0a5094SBarry Smith Input Parameters: 21928b0a5094SBarry Smith + snes - the SNES context 21938b0a5094SBarry Smith . r - vector to store function value 2194f8b49ee9SBarry Smith . b - function evaluation routine 2195e5d3d808SBarry Smith . Amat - matrix with which A(x) x - b(x) is to be computed 2196e5d3d808SBarry Smith . Pmat - matrix from which preconditioner is computed (usually the same as Amat) 2197411c0326SBarry Smith . J - function to compute matrix value, see SNESJacobianFunction for details on its calling sequence 21988b0a5094SBarry Smith - ctx - [optional] user-defined context for private data for the 21990298fd71SBarry Smith function evaluation routine (may be NULL) 22008b0a5094SBarry Smith 22018b0a5094SBarry Smith Notes: 2202f450aa47SBarry Smith We do not recomemend using this routine. It is far better to provide the nonlinear function F() and some approximation to the Jacobian and use 2203f450aa47SBarry 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. 2204f450aa47SBarry Smith 22058b0a5094SBarry Smith One can call SNESSetPicard() or SNESSetFunction() (and possibly SNESSetJacobian()) but cannot call both 22068b0a5094SBarry Smith 22078b0a5094SBarry Smith $ Solves the equation A(x) x = b(x) via the defect correction algorithm A(x^{n}) (x^{n+1} - x^{n}) = b(x^{n}) - A(x^{n})x^{n} 22088b0a5094SBarry Smith $ Note that when an exact solver is used this corresponds to the "classic" Picard A(x^{n}) x^{n+1} = b(x^{n}) iteration. 22098b0a5094SBarry Smith 22108b0a5094SBarry Smith Run with -snes_mf_operator to solve the system with Newton's method using A(x^{n}) to construct the preconditioner. 22118b0a5094SBarry Smith 22120d04baf8SBarry Smith We implement the defect correction form of the Picard iteration because it converges much more generally when inexact linear solvers are used then 22130d04baf8SBarry Smith the direct Picard iteration A(x^n) x^{n+1} = b(x^n) 22148b0a5094SBarry Smith 22158b0a5094SBarry 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 22168b0a5094SBarry 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 22178b0a5094SBarry Smith different please contact us at petsc-dev@mcs.anl.gov and we'll have an entirely new argument :-). 22188b0a5094SBarry Smith 2219f450aa47SBarry Smith Level: intermediate 22208b0a5094SBarry Smith 22218b0a5094SBarry Smith .keywords: SNES, nonlinear, set, function 22228b0a5094SBarry Smith 2223411c0326SBarry Smith .seealso: SNESGetFunction(), SNESSetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESGetPicard(), SNESLineSearchPreCheckPicard(), SNESJacobianFunction 22248b0a5094SBarry Smith @*/ 2225d1e9a80fSBarry Smith PetscErrorCode SNESSetPicard(SNES snes,Vec r,PetscErrorCode (*b)(SNES,Vec,Vec,void*),Mat Amat, Mat Pmat, PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx) 22268b0a5094SBarry Smith { 22278b0a5094SBarry Smith PetscErrorCode ierr; 2228e03ab78fSPeter Brune DM dm; 2229e03ab78fSPeter Brune 22308b0a5094SBarry Smith PetscFunctionBegin; 22318b0a5094SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2232e03ab78fSPeter Brune ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 2233f8b49ee9SBarry Smith ierr = DMSNESSetPicard(dm,b,J,ctx);CHKERRQ(ierr); 22348b0a5094SBarry Smith ierr = SNESSetFunction(snes,r,SNESPicardComputeFunction,ctx);CHKERRQ(ierr); 2235e5d3d808SBarry Smith ierr = SNESSetJacobian(snes,Amat,Pmat,SNESPicardComputeJacobian,ctx);CHKERRQ(ierr); 22368b0a5094SBarry Smith PetscFunctionReturn(0); 22378b0a5094SBarry Smith } 22388b0a5094SBarry Smith 22397971a8bfSPeter Brune /*@C 22407971a8bfSPeter Brune SNESGetPicard - Returns the context for the Picard iteration 22417971a8bfSPeter Brune 22427971a8bfSPeter Brune Not Collective, but Vec is parallel if SNES is parallel. Collective if Vec is requested, but has not been created yet. 22437971a8bfSPeter Brune 22447971a8bfSPeter Brune Input Parameter: 22457971a8bfSPeter Brune . snes - the SNES context 22467971a8bfSPeter Brune 22477971a8bfSPeter Brune Output Parameter: 22480298fd71SBarry Smith + r - the function (or NULL) 2249f8b49ee9SBarry Smith . f - the function (or NULL); see SNESFunction for calling sequence details 2250e4357dc4SBarry Smith . Amat - the matrix used to defined the operation A(x) x - b(x) (or NULL) 2251e4357dc4SBarry Smith . Pmat - the matrix from which the preconditioner will be constructed (or NULL) 2252f8b49ee9SBarry Smith . J - the function for matrix evaluation (or NULL); see SNESJacobianFunction for calling sequence details 22530298fd71SBarry Smith - ctx - the function context (or NULL) 22547971a8bfSPeter Brune 22557971a8bfSPeter Brune Level: advanced 22567971a8bfSPeter Brune 22577971a8bfSPeter Brune .keywords: SNES, nonlinear, get, function 22587971a8bfSPeter Brune 2259e4357dc4SBarry Smith .seealso: SNESSetPicard(), SNESGetFunction(), SNESGetJacobian(), SNESGetDM(), SNESFunction, SNESJacobianFunction 22607971a8bfSPeter Brune @*/ 2261d1e9a80fSBarry Smith PetscErrorCode SNESGetPicard(SNES snes,Vec *r,PetscErrorCode (**f)(SNES,Vec,Vec,void*),Mat *Amat, Mat *Pmat, PetscErrorCode (**J)(SNES,Vec,Mat,Mat,void*),void **ctx) 22627971a8bfSPeter Brune { 22637971a8bfSPeter Brune PetscErrorCode ierr; 22647971a8bfSPeter Brune DM dm; 22657971a8bfSPeter Brune 22667971a8bfSPeter Brune PetscFunctionBegin; 22677971a8bfSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 22680298fd71SBarry Smith ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr); 2269e4357dc4SBarry Smith ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr); 22707971a8bfSPeter Brune ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 2271f8b49ee9SBarry Smith ierr = DMSNESGetPicard(dm,f,J,ctx);CHKERRQ(ierr); 22727971a8bfSPeter Brune PetscFunctionReturn(0); 22737971a8bfSPeter Brune } 22747971a8bfSPeter Brune 2275d25893d9SBarry Smith /*@C 2276d25893d9SBarry Smith SNESSetComputeInitialGuess - Sets a routine used to compute an initial guess for the problem 2277d25893d9SBarry Smith 2278d25893d9SBarry Smith Logically Collective on SNES 2279d25893d9SBarry Smith 2280d25893d9SBarry Smith Input Parameters: 2281d25893d9SBarry Smith + snes - the SNES context 2282d25893d9SBarry Smith . func - function evaluation routine 2283d25893d9SBarry Smith - ctx - [optional] user-defined context for private data for the 22840298fd71SBarry Smith function evaluation routine (may be NULL) 2285d25893d9SBarry Smith 2286d25893d9SBarry Smith Calling sequence of func: 2287d25893d9SBarry Smith $ func (SNES snes,Vec x,void *ctx); 2288d25893d9SBarry Smith 2289d25893d9SBarry Smith . f - function vector 2290d25893d9SBarry Smith - ctx - optional user-defined function context 2291d25893d9SBarry Smith 2292d25893d9SBarry Smith Level: intermediate 2293d25893d9SBarry Smith 2294d25893d9SBarry Smith .keywords: SNES, nonlinear, set, function 2295d25893d9SBarry Smith 2296d25893d9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 2297d25893d9SBarry Smith @*/ 2298d25893d9SBarry Smith PetscErrorCode SNESSetComputeInitialGuess(SNES snes,PetscErrorCode (*func)(SNES,Vec,void*),void *ctx) 2299d25893d9SBarry Smith { 2300d25893d9SBarry Smith PetscFunctionBegin; 2301d25893d9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2302d25893d9SBarry Smith if (func) snes->ops->computeinitialguess = func; 2303d25893d9SBarry Smith if (ctx) snes->initialguessP = ctx; 2304d25893d9SBarry Smith PetscFunctionReturn(0); 2305d25893d9SBarry Smith } 2306d25893d9SBarry Smith 23073ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 23081096aae1SMatthew Knepley /*@C 23091096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 23101096aae1SMatthew Knepley it assumes a zero right hand side. 23111096aae1SMatthew Knepley 23123f9fe445SBarry Smith Logically Collective on SNES 23131096aae1SMatthew Knepley 23141096aae1SMatthew Knepley Input Parameter: 23151096aae1SMatthew Knepley . snes - the SNES context 23161096aae1SMatthew Knepley 23171096aae1SMatthew Knepley Output Parameter: 23180298fd71SBarry Smith . rhs - the right hand side vector or NULL if the right hand side vector is null 23191096aae1SMatthew Knepley 23201096aae1SMatthew Knepley Level: intermediate 23211096aae1SMatthew Knepley 23221096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 23231096aae1SMatthew Knepley 232485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 23251096aae1SMatthew Knepley @*/ 23267087cfbeSBarry Smith PetscErrorCode SNESGetRhs(SNES snes,Vec *rhs) 23271096aae1SMatthew Knepley { 23281096aae1SMatthew Knepley PetscFunctionBegin; 23290700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23301096aae1SMatthew Knepley PetscValidPointer(rhs,2); 233185385478SLisandro Dalcin *rhs = snes->vec_rhs; 23321096aae1SMatthew Knepley PetscFunctionReturn(0); 23331096aae1SMatthew Knepley } 23341096aae1SMatthew Knepley 23359b94acceSBarry Smith /*@ 2336bf388a1fSBarry Smith SNESComputeFunction - Calls the function that has been set with SNESSetFunction(). 23379b94acceSBarry Smith 2338c7afd0dbSLois Curfman McInnes Collective on SNES 2339c7afd0dbSLois Curfman McInnes 23409b94acceSBarry Smith Input Parameters: 2341c7afd0dbSLois Curfman McInnes + snes - the SNES context 2342c7afd0dbSLois Curfman McInnes - x - input vector 23439b94acceSBarry Smith 23449b94acceSBarry Smith Output Parameter: 23453638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 23469b94acceSBarry Smith 23471bffabb2SLois Curfman McInnes Notes: 234836851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 234936851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 235036851e7fSLois Curfman McInnes themselves. 235136851e7fSLois Curfman McInnes 235236851e7fSLois Curfman McInnes Level: developer 235336851e7fSLois Curfman McInnes 23549b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 23559b94acceSBarry Smith 2356a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 23579b94acceSBarry Smith @*/ 23587087cfbeSBarry Smith PetscErrorCode SNESComputeFunction(SNES snes,Vec x,Vec y) 23599b94acceSBarry Smith { 2360dfbe8321SBarry Smith PetscErrorCode ierr; 23616cab3a1bSJed Brown DM dm; 2362942e3340SBarry Smith DMSNES sdm; 23639b94acceSBarry Smith 23643a40ed3dSBarry Smith PetscFunctionBegin; 23650700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23660700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 23670700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 2368c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 2369c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 237062796dfbSBarry Smith ierr = VecValidValues(x,2,PETSC_TRUE);CHKERRQ(ierr); 2371184914b5SBarry Smith 23726cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 2373942e3340SBarry Smith ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 237432f3f7c2SPeter Brune if (sdm->ops->computefunction) { 237594db00ebSBarry Smith if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) { 2376ccf3c845SPeter Brune ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 237794db00ebSBarry Smith } 23785edff71fSBarry Smith ierr = VecLockPush(x);CHKERRQ(ierr); 2379d64ed03dSBarry Smith PetscStackPush("SNES user function"); 238022c6f798SBarry Smith ierr = (*sdm->ops->computefunction)(snes,x,y,sdm->functionctx);CHKERRQ(ierr); 2381d64ed03dSBarry Smith PetscStackPop; 23825edff71fSBarry Smith ierr = VecLockPop(x);CHKERRQ(ierr); 238394db00ebSBarry Smith if (sdm->ops->computefunction != SNESObjectiveComputeFunctionDefaultFD) { 2384ccf3c845SPeter Brune ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 238594db00ebSBarry Smith } 2386c90fad12SPeter Brune } else if (snes->vec_rhs) { 2387c90fad12SPeter Brune ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 2388644e2e5bSBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() or SNESSetDM() before SNESComputeFunction(), likely called from SNESSolve()."); 238985385478SLisandro Dalcin if (snes->vec_rhs) { 239085385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 23913ab0aad5SBarry Smith } 2392ae3c334cSLois Curfman McInnes snes->nfuncs++; 2393422a814eSBarry Smith /* 2394422a814eSBarry Smith domainerror might not be set on all processes; so we tag vector locally with Inf and the next inner product or norm will 2395422a814eSBarry Smith propagate the value to all processes 2396422a814eSBarry Smith */ 2397422a814eSBarry Smith if (snes->domainerror) { 2398422a814eSBarry Smith ierr = VecSetInf(y);CHKERRQ(ierr); 2399422a814eSBarry Smith } 24003a40ed3dSBarry Smith PetscFunctionReturn(0); 24019b94acceSBarry Smith } 24029b94acceSBarry Smith 2403c79ef259SPeter Brune /*@ 2404be95d8f1SBarry Smith SNESComputeNGS - Calls the Gauss-Seidel function that has been set with SNESSetNGS(). 2405c79ef259SPeter Brune 2406c79ef259SPeter Brune Collective on SNES 2407c79ef259SPeter Brune 2408c79ef259SPeter Brune Input Parameters: 2409c79ef259SPeter Brune + snes - the SNES context 2410c79ef259SPeter Brune . x - input vector 2411c79ef259SPeter Brune - b - rhs vector 2412c79ef259SPeter Brune 2413c79ef259SPeter Brune Output Parameter: 2414c79ef259SPeter Brune . x - new solution vector 2415c79ef259SPeter Brune 2416c79ef259SPeter Brune Notes: 2417be95d8f1SBarry Smith SNESComputeNGS() is typically used within composed nonlinear solver 2418c79ef259SPeter Brune implementations, so most users would not generally call this routine 2419c79ef259SPeter Brune themselves. 2420c79ef259SPeter Brune 2421c79ef259SPeter Brune Level: developer 2422c79ef259SPeter Brune 2423c79ef259SPeter Brune .keywords: SNES, nonlinear, compute, function 2424c79ef259SPeter Brune 2425be95d8f1SBarry Smith .seealso: SNESSetNGS(), SNESComputeFunction() 2426c79ef259SPeter Brune @*/ 2427be95d8f1SBarry Smith PetscErrorCode SNESComputeNGS(SNES snes,Vec b,Vec x) 2428646217ecSPeter Brune { 2429646217ecSPeter Brune PetscErrorCode ierr; 24306cab3a1bSJed Brown DM dm; 2431942e3340SBarry Smith DMSNES sdm; 2432646217ecSPeter Brune 2433646217ecSPeter Brune PetscFunctionBegin; 2434646217ecSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2435646217ecSPeter Brune PetscValidHeaderSpecific(x,VEC_CLASSID,2); 2436646217ecSPeter Brune if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,3); 2437646217ecSPeter Brune PetscCheckSameComm(snes,1,x,2); 2438646217ecSPeter Brune if (b) PetscCheckSameComm(snes,1,b,3); 243962796dfbSBarry Smith if (b) {ierr = VecValidValues(b,2,PETSC_TRUE);CHKERRQ(ierr);} 2440be95d8f1SBarry Smith ierr = PetscLogEventBegin(SNES_NGSEval,snes,x,b,0);CHKERRQ(ierr); 24416cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 2442942e3340SBarry Smith ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 244322c6f798SBarry Smith if (sdm->ops->computegs) { 24445edff71fSBarry Smith if (b) {ierr = VecLockPush(b);CHKERRQ(ierr);} 2445be95d8f1SBarry Smith PetscStackPush("SNES user NGS"); 244622c6f798SBarry Smith ierr = (*sdm->ops->computegs)(snes,x,b,sdm->gsctx);CHKERRQ(ierr); 2447646217ecSPeter Brune PetscStackPop; 24485edff71fSBarry Smith if (b) {ierr = VecLockPop(b);CHKERRQ(ierr);} 2449be95d8f1SBarry Smith } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetNGS() before SNESComputeNGS(), likely called from SNESSolve()."); 2450be95d8f1SBarry Smith ierr = PetscLogEventEnd(SNES_NGSEval,snes,x,b,0);CHKERRQ(ierr); 2451646217ecSPeter Brune PetscFunctionReturn(0); 2452646217ecSPeter Brune } 2453646217ecSPeter Brune 2454e885f1abSBarry Smith PetscErrorCode SNESTestJacobian(SNES snes) 2455e885f1abSBarry Smith { 245612837594SBarry Smith Mat A,B,C,D,jacobian; 2457e885f1abSBarry Smith Vec x = snes->vec_sol,f = snes->vec_func; 2458e885f1abSBarry Smith PetscErrorCode ierr; 2459e885f1abSBarry Smith PetscReal nrm,gnorm; 246081e7118cSBarry Smith PetscReal threshold = 1.e-5; 2461e885f1abSBarry Smith PetscInt m,n,M,N; 2462e885f1abSBarry Smith void *functx; 246318d89885SKarl Rupp PetscBool complete_print = PETSC_FALSE,threshold_print = PETSC_FALSE,test = PETSC_FALSE,flg; 24643325ff46SBarry Smith PetscViewer viewer,mviewer; 2465e885f1abSBarry Smith MPI_Comm comm; 2466e885f1abSBarry Smith PetscInt tabs; 246712837594SBarry Smith static PetscBool directionsprinted = PETSC_FALSE; 24683325ff46SBarry Smith PetscViewerFormat format; 2469e885f1abSBarry Smith 2470e885f1abSBarry Smith PetscFunctionBegin; 2471fc35ed60SBarry Smith ierr = PetscObjectOptionsBegin((PetscObject)snes);CHKERRQ(ierr); 247212837594SBarry Smith ierr = PetscOptionsName("-snes_test_jacobian","Compare hand-coded and finite difference Jacobians","None",&test);CHKERRQ(ierr); 247312837594SBarry Smith ierr = PetscOptionsReal("-snes_test_jacobian", "Threshold for element difference between hand-coded and finite difference being meaningful", "None", threshold, &threshold,NULL);CHKERRQ(ierr); 24743325ff46SBarry Smith ierr = PetscOptionsViewer("-snes_test_jacobian_view","View difference between hand-coded and finite difference Jacobians element entries","None",&mviewer,&format,&complete_print);CHKERRQ(ierr); 247518d89885SKarl Rupp if (!complete_print) { 247618d89885SKarl Rupp ierr = PetscOptionsViewer("-snes_test_jacobian_display","Display difference between hand-coded and finite difference Jacobians","None",&mviewer,&format,&complete_print);CHKERRQ(ierr); 247718d89885SKarl Rupp } 247818d89885SKarl Rupp /* for compatibility with PETSc 3.9 and older. */ 247918d89885SKarl Rupp ierr = PetscOptionsReal("-snes_test_jacobian_display_threshold", "Display difference between hand-coded and finite difference Jacobians which exceed input threshold", "None", threshold, &threshold, &threshold_print);CHKERRQ(ierr); 2480e885f1abSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 2481e885f1abSBarry Smith if (!test) PetscFunctionReturn(0); 2482e885f1abSBarry Smith 2483e885f1abSBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 2484e885f1abSBarry Smith ierr = PetscViewerASCIIGetStdout(comm,&viewer);CHKERRQ(ierr); 2485e885f1abSBarry Smith ierr = PetscViewerASCIIGetTab(viewer, &tabs);CHKERRQ(ierr); 2486e885f1abSBarry Smith ierr = PetscViewerASCIISetTab(viewer, ((PetscObject)snes)->tablevel);CHKERRQ(ierr); 248712837594SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," ---------- Testing Jacobian -------------\n");CHKERRQ(ierr); 248812837594SBarry Smith if (!complete_print && !directionsprinted) { 248912837594SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Run with -snes_test_jacobian_view and optionally -snes_test_jacobian <threshold> to show difference\n");CHKERRQ(ierr); 2490fc35ed60SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," of hand-coded and finite difference Jacobian entries greater than <threshold>.\n");CHKERRQ(ierr); 249112837594SBarry Smith } 249212837594SBarry Smith if (!directionsprinted) { 249312837594SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Testing hand-coded Jacobian, if (for double precision runs) ||J - Jfd||_F/||J||_F is\n");CHKERRQ(ierr); 2494e885f1abSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," O(1.e-8), the hand-coded Jacobian is probably correct.\n");CHKERRQ(ierr); 249512837594SBarry Smith directionsprinted = PETSC_TRUE; 2496e885f1abSBarry Smith } 24973325ff46SBarry Smith if (complete_print) { 24983325ff46SBarry Smith ierr = PetscViewerPushFormat(mviewer,format);CHKERRQ(ierr); 2499e885f1abSBarry Smith } 2500e885f1abSBarry Smith 250112837594SBarry Smith /* evaluate the function at this point because SNESComputeJacobianDefault() assumes that the function has been evaluated and put into snes->vec_func */ 2502e885f1abSBarry Smith ierr = SNESComputeFunction(snes,x,f);CHKERRQ(ierr); 250312837594SBarry Smith 250412837594SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)snes->jacobian,MATMFFD,&flg);CHKERRQ(ierr); 250512837594SBarry Smith if (!flg) jacobian = snes->jacobian; 250612837594SBarry Smith else jacobian = snes->jacobian_pre; 250712837594SBarry Smith 250812837594SBarry Smith while (jacobian) { 250912837594SBarry Smith ierr = PetscObjectTypeCompareAny((PetscObject)jacobian,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr); 251012837594SBarry Smith if (flg) { 251112837594SBarry Smith A = jacobian; 251212837594SBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 251312837594SBarry Smith } else { 251412837594SBarry Smith ierr = MatComputeExplicitOperator(jacobian,&A);CHKERRQ(ierr); 251512837594SBarry Smith } 2516e885f1abSBarry Smith 2517e885f1abSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr); 2518e885f1abSBarry Smith ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 2519e885f1abSBarry Smith ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); 2520e885f1abSBarry Smith ierr = MatSetSizes(B,m,n,M,N);CHKERRQ(ierr); 2521e885f1abSBarry Smith ierr = MatSetType(B,((PetscObject)A)->type_name);CHKERRQ(ierr); 2522e885f1abSBarry Smith ierr = MatSetUp(B);CHKERRQ(ierr); 2523e885f1abSBarry Smith ierr = MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 2524e885f1abSBarry Smith 2525e885f1abSBarry Smith ierr = SNESGetFunction(snes,NULL,NULL,&functx);CHKERRQ(ierr); 2526e885f1abSBarry Smith ierr = SNESComputeJacobianDefault(snes,x,B,B,functx);CHKERRQ(ierr); 252712837594SBarry Smith 252812837594SBarry Smith ierr = MatDuplicate(B,MAT_COPY_VALUES,&D);CHKERRQ(ierr); 252912837594SBarry Smith ierr = MatAYPX(D,-1.0,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); 253012837594SBarry Smith ierr = MatNorm(D,NORM_FROBENIUS,&nrm);CHKERRQ(ierr); 2531e885f1abSBarry Smith ierr = MatNorm(A,NORM_FROBENIUS,&gnorm);CHKERRQ(ierr); 253212837594SBarry Smith ierr = MatDestroy(&D);CHKERRQ(ierr); 253312837594SBarry Smith if (!gnorm) gnorm = 1; /* just in case */ 253412837594SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," ||J - Jfd||_F/||J||_F = %g, ||J - Jfd||_F = %g\n",(double)(nrm/gnorm),(double)nrm);CHKERRQ(ierr); 253512837594SBarry Smith 2536e885f1abSBarry Smith if (complete_print) { 253712837594SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Hand-coded Jacobian ----------\n");CHKERRQ(ierr); 25383325ff46SBarry Smith ierr = MatView(jacobian,mviewer);CHKERRQ(ierr); 253912837594SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Finite difference Jacobian ----------\n");CHKERRQ(ierr); 25403325ff46SBarry Smith ierr = MatView(B,mviewer);CHKERRQ(ierr); 2541e885f1abSBarry Smith } 2542e885f1abSBarry Smith 2543df10fb39SFande Kong if (threshold_print || complete_print) { 2544e885f1abSBarry Smith PetscInt Istart, Iend, *ccols, bncols, cncols, j, row; 2545e885f1abSBarry Smith PetscScalar *cvals; 2546e885f1abSBarry Smith const PetscInt *bcols; 2547e885f1abSBarry Smith const PetscScalar *bvals; 2548e885f1abSBarry Smith 254912837594SBarry Smith ierr = MatAYPX(B,-1.0,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); 2550e885f1abSBarry Smith ierr = MatCreate(PetscObjectComm((PetscObject)A),&C);CHKERRQ(ierr); 2551e885f1abSBarry Smith ierr = MatSetSizes(C,m,n,M,N);CHKERRQ(ierr); 2552e885f1abSBarry Smith ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr); 2553e885f1abSBarry Smith ierr = MatSetUp(C);CHKERRQ(ierr); 2554e885f1abSBarry Smith ierr = MatSetOption(C,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); 2555e885f1abSBarry Smith ierr = MatGetOwnershipRange(B,&Istart,&Iend);CHKERRQ(ierr); 2556e885f1abSBarry Smith 2557e885f1abSBarry Smith for (row = Istart; row < Iend; row++) { 2558e885f1abSBarry Smith ierr = MatGetRow(B,row,&bncols,&bcols,&bvals);CHKERRQ(ierr); 2559e885f1abSBarry Smith ierr = PetscMalloc2(bncols,&ccols,bncols,&cvals);CHKERRQ(ierr); 2560e885f1abSBarry Smith for (j = 0, cncols = 0; j < bncols; j++) { 256123a52b1dSBarry Smith if (PetscAbsScalar(bvals[j]) > threshold) { 2562e885f1abSBarry Smith ccols[cncols] = bcols[j]; 2563e885f1abSBarry Smith cvals[cncols] = bvals[j]; 2564e885f1abSBarry Smith cncols += 1; 2565e885f1abSBarry Smith } 2566e885f1abSBarry Smith } 2567e885f1abSBarry Smith if (cncols) { 2568e885f1abSBarry Smith ierr = MatSetValues(C,1,&row,cncols,ccols,cvals,INSERT_VALUES);CHKERRQ(ierr); 2569e885f1abSBarry Smith } 2570e885f1abSBarry Smith ierr = MatRestoreRow(B,row,&bncols,&bcols,&bvals);CHKERRQ(ierr); 2571e885f1abSBarry Smith ierr = PetscFree2(ccols,cvals);CHKERRQ(ierr); 2572e885f1abSBarry Smith } 2573e885f1abSBarry Smith ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2574e885f1abSBarry Smith ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 257512837594SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Hand-coded minus finite-difference Jacobian with tolerance %g ----------\n",(double)threshold);CHKERRQ(ierr); 257618d89885SKarl Rupp ierr = MatView(C,complete_print ? mviewer : viewer);CHKERRQ(ierr); 2577e885f1abSBarry Smith ierr = MatDestroy(&C);CHKERRQ(ierr); 2578e885f1abSBarry Smith } 257912837594SBarry Smith ierr = MatDestroy(&A);CHKERRQ(ierr); 2580e885f1abSBarry Smith ierr = MatDestroy(&B);CHKERRQ(ierr); 258112837594SBarry Smith 258212837594SBarry Smith if (jacobian != snes->jacobian_pre) { 258312837594SBarry Smith jacobian = snes->jacobian_pre; 258412837594SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," ---------- Testing Jacobian for preconditioner -------------\n");CHKERRQ(ierr); 258512837594SBarry Smith } 258612837594SBarry Smith else jacobian = NULL; 258712837594SBarry Smith } 25883325ff46SBarry Smith if (complete_print) { 25893325ff46SBarry Smith ierr = PetscViewerPopFormat(mviewer);CHKERRQ(ierr); 25903325ff46SBarry Smith } 2591a0c90127SFande Kong if (mviewer) { ierr = PetscViewerDestroy(&mviewer);CHKERRQ(ierr); } 2592e885f1abSBarry Smith ierr = PetscViewerASCIISetTab(viewer,tabs);CHKERRQ(ierr); 2593e885f1abSBarry Smith PetscFunctionReturn(0); 2594e885f1abSBarry Smith } 2595e885f1abSBarry Smith 259662fef451SLois Curfman McInnes /*@ 2597bf388a1fSBarry Smith SNESComputeJacobian - Computes the Jacobian matrix that has been set with SNESSetJacobian(). 259862fef451SLois Curfman McInnes 2599c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 2600c7afd0dbSLois Curfman McInnes 260162fef451SLois Curfman McInnes Input Parameters: 2602c7afd0dbSLois Curfman McInnes + snes - the SNES context 2603c7afd0dbSLois Curfman McInnes - x - input vector 260462fef451SLois Curfman McInnes 260562fef451SLois Curfman McInnes Output Parameters: 2606c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 2607d1e9a80fSBarry Smith - B - optional preconditioning matrix 2608fee21e36SBarry Smith 2609e35cf81dSBarry Smith Options Database Keys: 2610e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 2611693365a8SJed Brown . -snes_lag_jacobian <lag> 2612e885f1abSBarry Smith . -snes_test_jacobian - compare the user provided Jacobian with one compute via finite differences to check for errors 2613e885f1abSBarry Smith . -snes_test_jacobian_display - 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 2614e885f1abSBarry Smith . -snes_test_jacobian_display_threshold <numerical value> - display entries in the difference between the user provided Jacobian and finite difference Jacobian that are greater than a certain value to help users detect errors 2615693365a8SJed Brown . -snes_compare_explicit - Compare the computed Jacobian to the finite difference Jacobian and output the differences 2616693365a8SJed Brown . -snes_compare_explicit_draw - Compare the computed Jacobian to the finite difference Jacobian and draw the result 2617693365a8SJed Brown . -snes_compare_explicit_contour - Compare the computed Jacobian to the finite difference Jacobian and draw a contour plot with the result 26184c30e9fbSJed Brown . -snes_compare_operator - Make the comparison options above use the operator instead of the preconditioning matrix 261994d6a431SBarry Smith . -snes_compare_coloring - Compute the finite difference Jacobian using coloring and display norms of difference 2620c01495d3SJed Brown . -snes_compare_coloring_display - Compute the finite differece Jacobian using coloring and display verbose differences 2621c01495d3SJed Brown . -snes_compare_coloring_threshold - Display only those matrix entries that differ by more than a given threshold 2622c01495d3SJed Brown . -snes_compare_coloring_threshold_atol - Absolute tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 2623c01495d3SJed Brown . -snes_compare_coloring_threshold_rtol - Relative tolerance for difference in matrix entries to be displayed by -snes_compare_coloring_threshold 26244c30e9fbSJed Brown . -snes_compare_coloring_draw - Compute the finite differece Jacobian using coloring and draw differences 2625c01495d3SJed Brown - -snes_compare_coloring_draw_contour - Compute the finite differece Jacobian using coloring and show contours of matrices and differences 2626c01495d3SJed Brown 2627e35cf81dSBarry Smith 262862fef451SLois Curfman McInnes Notes: 262962fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 263062fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 263162fef451SLois Curfman McInnes 263295452b02SPatrick Sanan Developer Notes: 263395452b02SPatrick Sanan This has duplicative ways of checking the accuracy of the user provided Jacobian (see the options above). This is for historical reasons, the routine SNESTestJacobian() use to used 2634e885f1abSBarry Smith for with the SNESType of test that has been removed. 2635e885f1abSBarry Smith 263636851e7fSLois Curfman McInnes Level: developer 263736851e7fSLois Curfman McInnes 263862fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 263962fef451SLois Curfman McInnes 2640e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 264162fef451SLois Curfman McInnes @*/ 2642d1e9a80fSBarry Smith PetscErrorCode SNESComputeJacobian(SNES snes,Vec X,Mat A,Mat B) 26439b94acceSBarry Smith { 2644dfbe8321SBarry Smith PetscErrorCode ierr; 2645ace3abfcSBarry Smith PetscBool flag; 26466cab3a1bSJed Brown DM dm; 2647942e3340SBarry Smith DMSNES sdm; 2648e0e3a89bSBarry Smith KSP ksp; 26493a40ed3dSBarry Smith 26503a40ed3dSBarry Smith PetscFunctionBegin; 26510700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 26520700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 2653c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 265462796dfbSBarry Smith ierr = VecValidValues(X,2,PETSC_TRUE);CHKERRQ(ierr); 26556cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 2656942e3340SBarry Smith ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 26573232da50SPeter Brune 2658ce94432eSBarry Smith if (!sdm->ops->computejacobian) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_USER,"Must call SNESSetJacobian(), DMSNESSetJacobian(), DMDASNESSetJacobianLocal(), etc"); 2659ebd3b9afSBarry Smith 2660ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 2661ebd3b9afSBarry Smith 2662fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 2663fe3ffe1eSBarry Smith snes->lagjacobian = -1; 2664f5af7f23SKarl Rupp 2665fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 2666fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 2667e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 266894ab13aaSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)A,MATMFFD,&flag);CHKERRQ(ierr); 2669ebd3b9afSBarry Smith if (flag) { 267094ab13aaSBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 267194ab13aaSBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2672ebd3b9afSBarry Smith } 2673e35cf81dSBarry Smith PetscFunctionReturn(0); 267437ec4e1aSPeter Brune } else if (snes->lagjacobian > 1 && (snes->iter + snes->jac_iter) % snes->lagjacobian) { 2675e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 267694ab13aaSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)A,MATMFFD,&flag);CHKERRQ(ierr); 2677ebd3b9afSBarry Smith if (flag) { 267894ab13aaSBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 267994ab13aaSBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2680ebd3b9afSBarry Smith } 2681e35cf81dSBarry Smith PetscFunctionReturn(0); 2682e35cf81dSBarry Smith } 2683efd4aadfSBarry Smith if (snes->npc && snes->npcside== PC_LEFT) { 268494ab13aaSBarry Smith ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 268594ab13aaSBarry Smith ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2686d728fb7dSPeter Brune PetscFunctionReturn(0); 2687d728fb7dSPeter Brune } 2688e35cf81dSBarry Smith 268994ab13aaSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,A,B);CHKERRQ(ierr); 26905edff71fSBarry Smith ierr = VecLockPush(X);CHKERRQ(ierr); 2691d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 2692d1e9a80fSBarry Smith ierr = (*sdm->ops->computejacobian)(snes,X,A,B,sdm->jacobianctx);CHKERRQ(ierr); 2693d64ed03dSBarry Smith PetscStackPop; 26945edff71fSBarry Smith ierr = VecLockPop(X);CHKERRQ(ierr); 269594ab13aaSBarry Smith ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,A,B);CHKERRQ(ierr); 2696a8054027SBarry Smith 2697e0e3a89bSBarry Smith /* the next line ensures that snes->ksp exists */ 2698e0e3a89bSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 26993b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 27003b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 2701d1e9a80fSBarry Smith ierr = KSPSetReusePreconditioner(snes->ksp,PETSC_FALSE);CHKERRQ(ierr); 27023b4f5425SBarry Smith snes->lagpreconditioner = -1; 27033b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 2704a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 2705d1e9a80fSBarry Smith ierr = KSPSetReusePreconditioner(snes->ksp,PETSC_TRUE);CHKERRQ(ierr); 270637ec4e1aSPeter Brune } else if (snes->lagpreconditioner > 1 && (snes->iter + snes->pre_iter) % snes->lagpreconditioner) { 2707a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 2708d1e9a80fSBarry Smith ierr = KSPSetReusePreconditioner(snes->ksp,PETSC_TRUE);CHKERRQ(ierr); 2709d1e9a80fSBarry Smith } else { 2710d1e9a80fSBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner\n");CHKERRQ(ierr); 2711d1e9a80fSBarry Smith ierr = KSPSetReusePreconditioner(snes->ksp,PETSC_FALSE);CHKERRQ(ierr); 2712a8054027SBarry Smith } 2713a8054027SBarry Smith 2714e885f1abSBarry Smith ierr = SNESTestJacobian(snes);CHKERRQ(ierr); 27156d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 271694ab13aaSBarry Smith /* PetscValidHeaderSpecific(A,MAT_CLASSID,3); 271794ab13aaSBarry Smith PetscValidHeaderSpecific(B,MAT_CLASSID,4); */ 2718693365a8SJed Brown { 2719693365a8SJed Brown PetscBool flag = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_operator = PETSC_FALSE; 272016413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_explicit",NULL,NULL,&flag);CHKERRQ(ierr); 272116413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_explicit_draw",NULL,NULL,&flag_draw);CHKERRQ(ierr); 272216413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_explicit_draw_contour",NULL,NULL,&flag_contour);CHKERRQ(ierr); 272316413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject) snes)->options,((PetscObject)snes)->prefix,"-snes_compare_operator",NULL,NULL,&flag_operator);CHKERRQ(ierr); 2724693365a8SJed Brown if (flag || flag_draw || flag_contour) { 27250298fd71SBarry Smith Mat Bexp_mine = NULL,Bexp,FDexp; 2726693365a8SJed Brown PetscViewer vdraw,vstdout; 27276b3a5b13SJed Brown PetscBool flg; 2728693365a8SJed Brown if (flag_operator) { 272994ab13aaSBarry Smith ierr = MatComputeExplicitOperator(A,&Bexp_mine);CHKERRQ(ierr); 2730693365a8SJed Brown Bexp = Bexp_mine; 2731693365a8SJed Brown } else { 2732693365a8SJed Brown /* See if the preconditioning matrix can be viewed and added directly */ 273394ab13aaSBarry Smith ierr = PetscObjectTypeCompareAny((PetscObject)B,&flg,MATSEQAIJ,MATMPIAIJ,MATSEQDENSE,MATMPIDENSE,MATSEQBAIJ,MATMPIBAIJ,MATSEQSBAIJ,MATMPIBAIJ,"");CHKERRQ(ierr); 273494ab13aaSBarry Smith if (flg) Bexp = B; 2735693365a8SJed Brown else { 2736693365a8SJed Brown /* If the "preconditioning" matrix is itself MATSHELL or some other type without direct support */ 273794ab13aaSBarry Smith ierr = MatComputeExplicitOperator(B,&Bexp_mine);CHKERRQ(ierr); 2738693365a8SJed Brown Bexp = Bexp_mine; 2739693365a8SJed Brown } 2740693365a8SJed Brown } 2741693365a8SJed Brown ierr = MatConvert(Bexp,MATSAME,MAT_INITIAL_MATRIX,&FDexp);CHKERRQ(ierr); 2742d1e9a80fSBarry Smith ierr = SNESComputeJacobianDefault(snes,X,FDexp,FDexp,NULL);CHKERRQ(ierr); 2743ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&vstdout);CHKERRQ(ierr); 2744693365a8SJed Brown if (flag_draw || flag_contour) { 2745ce94432eSBarry Smith ierr = PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes),0,"Explicit Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 2746693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 27470298fd71SBarry Smith } else vdraw = NULL; 2748693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit %s\n",flag_operator ? "Jacobian" : "preconditioning Jacobian");CHKERRQ(ierr); 2749693365a8SJed Brown if (flag) {ierr = MatView(Bexp,vstdout);CHKERRQ(ierr);} 2750693365a8SJed Brown if (vdraw) {ierr = MatView(Bexp,vdraw);CHKERRQ(ierr);} 2751693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Finite difference Jacobian\n");CHKERRQ(ierr); 2752693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 2753693365a8SJed Brown if (vdraw) {ierr = MatView(FDexp,vdraw);CHKERRQ(ierr);} 2754693365a8SJed Brown ierr = MatAYPX(FDexp,-1.0,Bexp,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 2755693365a8SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian\n");CHKERRQ(ierr); 2756693365a8SJed Brown if (flag) {ierr = MatView(FDexp,vstdout);CHKERRQ(ierr);} 2757693365a8SJed Brown if (vdraw) { /* Always use contour for the difference */ 2758693365a8SJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 2759693365a8SJed Brown ierr = MatView(FDexp,vdraw);CHKERRQ(ierr); 2760693365a8SJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 2761693365a8SJed Brown } 2762693365a8SJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 2763693365a8SJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 2764693365a8SJed Brown ierr = MatDestroy(&Bexp_mine);CHKERRQ(ierr); 2765693365a8SJed Brown ierr = MatDestroy(&FDexp);CHKERRQ(ierr); 2766693365a8SJed Brown } 2767693365a8SJed Brown } 27684c30e9fbSJed Brown { 27696719d8e4SJed Brown PetscBool flag = PETSC_FALSE,flag_display = PETSC_FALSE,flag_draw = PETSC_FALSE,flag_contour = PETSC_FALSE,flag_threshold = PETSC_FALSE; 27706719d8e4SJed Brown PetscReal threshold_atol = PETSC_SQRT_MACHINE_EPSILON,threshold_rtol = 10*PETSC_SQRT_MACHINE_EPSILON; 277116413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring",NULL,NULL,&flag);CHKERRQ(ierr); 277216413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_display",NULL,NULL,&flag_display);CHKERRQ(ierr); 277316413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_draw",NULL,NULL,&flag_draw);CHKERRQ(ierr); 277416413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_draw_contour",NULL,NULL,&flag_contour);CHKERRQ(ierr); 277516413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold",NULL,NULL,&flag_threshold);CHKERRQ(ierr); 277627b0f280SBarry Smith if (flag_threshold) { 2777c5929fdfSBarry Smith ierr = PetscOptionsGetReal(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_rtol",&threshold_rtol,NULL);CHKERRQ(ierr); 2778c5929fdfSBarry Smith ierr = PetscOptionsGetReal(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_compare_coloring_threshold_atol",&threshold_atol,NULL);CHKERRQ(ierr); 277927b0f280SBarry Smith } 27806719d8e4SJed Brown if (flag || flag_display || flag_draw || flag_contour || flag_threshold) { 27814c30e9fbSJed Brown Mat Bfd; 27824c30e9fbSJed Brown PetscViewer vdraw,vstdout; 2783335efc43SPeter Brune MatColoring coloring; 27844c30e9fbSJed Brown ISColoring iscoloring; 27854c30e9fbSJed Brown MatFDColoring matfdcoloring; 27864c30e9fbSJed Brown PetscErrorCode (*func)(SNES,Vec,Vec,void*); 27874c30e9fbSJed Brown void *funcctx; 27886719d8e4SJed Brown PetscReal norm1,norm2,normmax; 27894c30e9fbSJed Brown 279094ab13aaSBarry Smith ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&Bfd);CHKERRQ(ierr); 2791335efc43SPeter Brune ierr = MatColoringCreate(Bfd,&coloring);CHKERRQ(ierr); 2792335efc43SPeter Brune ierr = MatColoringSetType(coloring,MATCOLORINGSL);CHKERRQ(ierr); 2793335efc43SPeter Brune ierr = MatColoringSetFromOptions(coloring);CHKERRQ(ierr); 2794335efc43SPeter Brune ierr = MatColoringApply(coloring,&iscoloring);CHKERRQ(ierr); 2795335efc43SPeter Brune ierr = MatColoringDestroy(&coloring);CHKERRQ(ierr); 27964c30e9fbSJed Brown ierr = MatFDColoringCreate(Bfd,iscoloring,&matfdcoloring);CHKERRQ(ierr); 2797f86b9fbaSHong Zhang ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr); 2798f86b9fbaSHong Zhang ierr = MatFDColoringSetUp(Bfd,iscoloring,matfdcoloring);CHKERRQ(ierr); 27994c30e9fbSJed Brown ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr); 28004c30e9fbSJed Brown 28014c30e9fbSJed Brown /* This method of getting the function is currently unreliable since it doesn't work for DM local functions. */ 28020298fd71SBarry Smith ierr = SNESGetFunction(snes,NULL,&func,&funcctx);CHKERRQ(ierr); 28034c30e9fbSJed Brown ierr = MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode (*)(void))func,funcctx);CHKERRQ(ierr); 28044c30e9fbSJed Brown ierr = PetscObjectSetOptionsPrefix((PetscObject)matfdcoloring,((PetscObject)snes)->prefix);CHKERRQ(ierr); 28054c30e9fbSJed Brown ierr = PetscObjectAppendOptionsPrefix((PetscObject)matfdcoloring,"coloring_");CHKERRQ(ierr); 28064c30e9fbSJed Brown ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr); 2807d1e9a80fSBarry Smith ierr = MatFDColoringApply(Bfd,matfdcoloring,X,snes);CHKERRQ(ierr); 28084c30e9fbSJed Brown ierr = MatFDColoringDestroy(&matfdcoloring);CHKERRQ(ierr); 28094c30e9fbSJed Brown 2810ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)snes),&vstdout);CHKERRQ(ierr); 28114c30e9fbSJed Brown if (flag_draw || flag_contour) { 2812ce94432eSBarry Smith ierr = PetscViewerDrawOpen(PetscObjectComm((PetscObject)snes),0,"Colored Jacobians",PETSC_DECIDE,PETSC_DECIDE,300,300,&vdraw);CHKERRQ(ierr); 28134c30e9fbSJed Brown if (flag_contour) {ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);} 28140298fd71SBarry Smith } else vdraw = NULL; 28154c30e9fbSJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Explicit preconditioning Jacobian\n");CHKERRQ(ierr); 281694ab13aaSBarry Smith if (flag_display) {ierr = MatView(B,vstdout);CHKERRQ(ierr);} 281794ab13aaSBarry Smith if (vdraw) {ierr = MatView(B,vdraw);CHKERRQ(ierr);} 28184c30e9fbSJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"Colored Finite difference Jacobian\n");CHKERRQ(ierr); 28196719d8e4SJed Brown if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);} 28204c30e9fbSJed Brown if (vdraw) {ierr = MatView(Bfd,vdraw);CHKERRQ(ierr);} 282194ab13aaSBarry Smith ierr = MatAYPX(Bfd,-1.0,B,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 28224c30e9fbSJed Brown ierr = MatNorm(Bfd,NORM_1,&norm1);CHKERRQ(ierr); 28236719d8e4SJed Brown ierr = MatNorm(Bfd,NORM_FROBENIUS,&norm2);CHKERRQ(ierr); 28244c30e9fbSJed Brown ierr = MatNorm(Bfd,NORM_MAX,&normmax);CHKERRQ(ierr); 282557622a8eSBarry Smith ierr = PetscViewerASCIIPrintf(vstdout,"User-provided matrix minus finite difference Jacobian, norm1=%g normFrob=%g normmax=%g\n",(double)norm1,(double)norm2,(double)normmax);CHKERRQ(ierr); 28266719d8e4SJed Brown if (flag_display) {ierr = MatView(Bfd,vstdout);CHKERRQ(ierr);} 28274c30e9fbSJed Brown if (vdraw) { /* Always use contour for the difference */ 28284c30e9fbSJed Brown ierr = PetscViewerPushFormat(vdraw,PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 28294c30e9fbSJed Brown ierr = MatView(Bfd,vdraw);CHKERRQ(ierr); 28304c30e9fbSJed Brown ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr); 28314c30e9fbSJed Brown } 28324c30e9fbSJed Brown if (flag_contour) {ierr = PetscViewerPopFormat(vdraw);CHKERRQ(ierr);} 28336719d8e4SJed Brown 28346719d8e4SJed Brown if (flag_threshold) { 28356719d8e4SJed Brown PetscInt bs,rstart,rend,i; 283694ab13aaSBarry Smith ierr = MatGetBlockSize(B,&bs);CHKERRQ(ierr); 283794ab13aaSBarry Smith ierr = MatGetOwnershipRange(B,&rstart,&rend);CHKERRQ(ierr); 28386719d8e4SJed Brown for (i=rstart; i<rend; i++) { 28396719d8e4SJed Brown const PetscScalar *ba,*ca; 28406719d8e4SJed Brown const PetscInt *bj,*cj; 28416719d8e4SJed Brown PetscInt bn,cn,j,maxentrycol = -1,maxdiffcol = -1,maxrdiffcol = -1; 28426719d8e4SJed Brown PetscReal maxentry = 0,maxdiff = 0,maxrdiff = 0; 284394ab13aaSBarry Smith ierr = MatGetRow(B,i,&bn,&bj,&ba);CHKERRQ(ierr); 28446719d8e4SJed Brown ierr = MatGetRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr); 284594ab13aaSBarry Smith if (bn != cn) SETERRQ(((PetscObject)A)->comm,PETSC_ERR_PLIB,"Unexpected different nonzero pattern in -snes_compare_coloring_threshold"); 28466719d8e4SJed Brown for (j=0; j<bn; j++) { 28476719d8e4SJed Brown PetscReal rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 28486719d8e4SJed Brown if (PetscAbsScalar(ba[j]) > PetscAbs(maxentry)) { 28496719d8e4SJed Brown maxentrycol = bj[j]; 28506719d8e4SJed Brown maxentry = PetscRealPart(ba[j]); 28516719d8e4SJed Brown } 28526719d8e4SJed Brown if (PetscAbsScalar(ca[j]) > PetscAbs(maxdiff)) { 28536719d8e4SJed Brown maxdiffcol = bj[j]; 28546719d8e4SJed Brown maxdiff = PetscRealPart(ca[j]); 28556719d8e4SJed Brown } 28566719d8e4SJed Brown if (rdiff > maxrdiff) { 28576719d8e4SJed Brown maxrdiffcol = bj[j]; 28586719d8e4SJed Brown maxrdiff = rdiff; 28596719d8e4SJed Brown } 28606719d8e4SJed Brown } 28616719d8e4SJed Brown if (maxrdiff > 1) { 286257622a8eSBarry Smith ierr = PetscViewerASCIIPrintf(vstdout,"row %D (maxentry=%g at %D, maxdiff=%g at %D, maxrdiff=%g at %D):",i,(double)maxentry,maxentrycol,(double)maxdiff,maxdiffcol,(double)maxrdiff,maxrdiffcol);CHKERRQ(ierr); 28636719d8e4SJed Brown for (j=0; j<bn; j++) { 28646719d8e4SJed Brown PetscReal rdiff; 28656719d8e4SJed Brown rdiff = PetscAbsScalar(ca[j]) / (threshold_atol + threshold_rtol*PetscAbsScalar(ba[j])); 28666719d8e4SJed Brown if (rdiff > 1) { 286757622a8eSBarry Smith ierr = PetscViewerASCIIPrintf(vstdout," (%D,%g:%g)",bj[j],(double)PetscRealPart(ba[j]),(double)PetscRealPart(ca[j]));CHKERRQ(ierr); 28686719d8e4SJed Brown } 28696719d8e4SJed Brown } 28706719d8e4SJed Brown ierr = PetscViewerASCIIPrintf(vstdout,"\n",i,maxentry,maxdiff,maxrdiff);CHKERRQ(ierr); 28716719d8e4SJed Brown } 287294ab13aaSBarry Smith ierr = MatRestoreRow(B,i,&bn,&bj,&ba);CHKERRQ(ierr); 28736719d8e4SJed Brown ierr = MatRestoreRow(Bfd,i,&cn,&cj,&ca);CHKERRQ(ierr); 28746719d8e4SJed Brown } 28756719d8e4SJed Brown } 28764c30e9fbSJed Brown ierr = PetscViewerDestroy(&vdraw);CHKERRQ(ierr); 28774c30e9fbSJed Brown ierr = MatDestroy(&Bfd);CHKERRQ(ierr); 28784c30e9fbSJed Brown } 28794c30e9fbSJed Brown } 28803a40ed3dSBarry Smith PetscFunctionReturn(0); 28819b94acceSBarry Smith } 28829b94acceSBarry Smith 2883bf388a1fSBarry Smith /*MC 2884411c0326SBarry Smith SNESJacobianFunction - Function used to convey the nonlinear Jacobian of the function to be solved by SNES 2885bf388a1fSBarry Smith 2886bf388a1fSBarry Smith Synopsis: 2887411c0326SBarry Smith #include "petscsnes.h" 2888411c0326SBarry Smith PetscErrorCode SNESJacobianFunction(SNES snes,Vec x,Mat Amat,Mat Pmat,void *ctx); 2889bf388a1fSBarry Smith 2890bf388a1fSBarry Smith + x - input vector 2891e5d3d808SBarry Smith . Amat - the matrix that defines the (approximate) Jacobian 2892e5d3d808SBarry Smith . Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat. 2893bf388a1fSBarry Smith - ctx - [optional] user-defined Jacobian context 2894bf388a1fSBarry Smith 2895878cb397SSatish Balay Level: intermediate 2896878cb397SSatish Balay 2897bf388a1fSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction(), SNESSetJacobian(), SNESGetJacobian() 2898bf388a1fSBarry Smith M*/ 2899bf388a1fSBarry Smith 29009b94acceSBarry Smith /*@C 29019b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 2902044dda88SLois Curfman McInnes location to store the matrix. 29039b94acceSBarry Smith 29043f9fe445SBarry Smith Logically Collective on SNES and Mat 2905c7afd0dbSLois Curfman McInnes 29069b94acceSBarry Smith Input Parameters: 2907c7afd0dbSLois Curfman McInnes + snes - the SNES context 2908e5d3d808SBarry Smith . Amat - the matrix that defines the (approximate) Jacobian 2909e5d3d808SBarry Smith . Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat. 2910411c0326SBarry Smith . J - Jacobian evaluation routine (if NULL then SNES retains any previously set value), see SNESJacobianFunction for details 2911c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 29120298fd71SBarry Smith Jacobian evaluation routine (may be NULL) (if NULL then SNES retains any previously set value) 29139b94acceSBarry Smith 29149b94acceSBarry Smith Notes: 2915e5d3d808SBarry Smith If the Amat matrix and Pmat matrix are different you must call MatAssemblyBegin/End() on 291616913363SBarry Smith each matrix. 291716913363SBarry Smith 2918895c21f2SBarry Smith If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null 2919895c21f2SBarry Smith space to Amat and the KSP solvers will automatically use that null space as needed during the solution process. 2920895c21f2SBarry Smith 29218d359177SBarry Smith If using SNESComputeJacobianDefaultColor() to assemble a Jacobian, the ctx argument 2922a8a26c1eSJed Brown must be a MatFDColoring. 2923a8a26c1eSJed Brown 2924c3cc8fd1SJed Brown Other defect-correction schemes can be used by computing a different matrix in place of the Jacobian. One common 2925c3cc8fd1SJed Brown example is to use the "Picard linearization" which only differentiates through the highest order parts of each term. 2926c3cc8fd1SJed Brown 292736851e7fSLois Curfman McInnes Level: beginner 292836851e7fSLois Curfman McInnes 29299b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 29309b94acceSBarry Smith 2931411c0326SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESComputeJacobianDefaultColor(), MatStructure, J, 2932411c0326SBarry Smith SNESSetPicard(), SNESJacobianFunction 29339b94acceSBarry Smith @*/ 2934d1e9a80fSBarry Smith PetscErrorCode SNESSetJacobian(SNES snes,Mat Amat,Mat Pmat,PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx) 29359b94acceSBarry Smith { 2936dfbe8321SBarry Smith PetscErrorCode ierr; 29376cab3a1bSJed Brown DM dm; 29383a7fca6bSBarry Smith 29393a40ed3dSBarry Smith PetscFunctionBegin; 29400700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2941e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 2942e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 2943e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(snes,1,Amat,2); 2944e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(snes,1,Pmat,3); 29456cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 2946f8b49ee9SBarry Smith ierr = DMSNESSetJacobian(dm,J,ctx);CHKERRQ(ierr); 2947e5d3d808SBarry Smith if (Amat) { 2948e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr); 29496bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 2950f5af7f23SKarl Rupp 2951e5d3d808SBarry Smith snes->jacobian = Amat; 29523a7fca6bSBarry Smith } 2953e5d3d808SBarry Smith if (Pmat) { 2954e5d3d808SBarry Smith ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr); 29556bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 2956f5af7f23SKarl Rupp 2957e5d3d808SBarry Smith snes->jacobian_pre = Pmat; 29583a7fca6bSBarry Smith } 29593a40ed3dSBarry Smith PetscFunctionReturn(0); 29609b94acceSBarry Smith } 296162fef451SLois Curfman McInnes 2962c2aafc4cSSatish Balay /*@C 2963b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 2964b4fd4287SBarry Smith provided context for evaluating the Jacobian. 2965b4fd4287SBarry Smith 2966c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 2967c7afd0dbSLois Curfman McInnes 2968b4fd4287SBarry Smith Input Parameter: 2969b4fd4287SBarry Smith . snes - the nonlinear solver context 2970b4fd4287SBarry Smith 2971b4fd4287SBarry Smith Output Parameters: 2972e5d3d808SBarry Smith + Amat - location to stash (approximate) Jacobian matrix (or NULL) 2973e5d3d808SBarry Smith . Pmat - location to stash matrix used to compute the preconditioner (or NULL) 2974411c0326SBarry Smith . J - location to put Jacobian function (or NULL), see SNESJacobianFunction for details on its calling sequence 29750298fd71SBarry Smith - ctx - location to stash Jacobian ctx (or NULL) 2976fee21e36SBarry Smith 297736851e7fSLois Curfman McInnes Level: advanced 297836851e7fSLois Curfman McInnes 2979411c0326SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian(), SNESJacobianFunction, SNESGetFunction() 2980b4fd4287SBarry Smith @*/ 2981d1e9a80fSBarry Smith PetscErrorCode SNESGetJacobian(SNES snes,Mat *Amat,Mat *Pmat,PetscErrorCode (**J)(SNES,Vec,Mat,Mat,void*),void **ctx) 2982b4fd4287SBarry Smith { 29836cab3a1bSJed Brown PetscErrorCode ierr; 29846cab3a1bSJed Brown DM dm; 2985942e3340SBarry Smith DMSNES sdm; 29866cab3a1bSJed Brown 29873a40ed3dSBarry Smith PetscFunctionBegin; 29880700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2989e5d3d808SBarry Smith if (Amat) *Amat = snes->jacobian; 2990e5d3d808SBarry Smith if (Pmat) *Pmat = snes->jacobian_pre; 29916cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 2992942e3340SBarry Smith ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 2993f8b49ee9SBarry Smith if (J) *J = sdm->ops->computejacobian; 29946cab3a1bSJed Brown if (ctx) *ctx = sdm->jacobianctx; 29953a40ed3dSBarry Smith PetscFunctionReturn(0); 2996b4fd4287SBarry Smith } 2997b4fd4287SBarry Smith 29989b94acceSBarry Smith /*@ 29999b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 3000272ac6f2SLois Curfman McInnes of a nonlinear solver. 30019b94acceSBarry Smith 3002fee21e36SBarry Smith Collective on SNES 3003fee21e36SBarry Smith 3004c7afd0dbSLois Curfman McInnes Input Parameters: 300570e92668SMatthew Knepley . snes - the SNES context 3006c7afd0dbSLois Curfman McInnes 3007272ac6f2SLois Curfman McInnes Notes: 3008272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 3009272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 3010272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 3011272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 3012272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 3013272ac6f2SLois Curfman McInnes 301436851e7fSLois Curfman McInnes Level: advanced 301536851e7fSLois Curfman McInnes 30169b94acceSBarry Smith .keywords: SNES, nonlinear, setup 30179b94acceSBarry Smith 30189b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 30199b94acceSBarry Smith @*/ 30207087cfbeSBarry Smith PetscErrorCode SNESSetUp(SNES snes) 30219b94acceSBarry Smith { 3022dfbe8321SBarry Smith PetscErrorCode ierr; 30236cab3a1bSJed Brown DM dm; 3024942e3340SBarry Smith DMSNES sdm; 3025c35f09e5SBarry Smith SNESLineSearch linesearch, pclinesearch; 30266e2a1849SPeter Brune void *lsprectx,*lspostctx; 30276b2b7091SBarry Smith PetscErrorCode (*precheck)(SNESLineSearch,Vec,Vec,PetscBool*,void*); 30286b2b7091SBarry Smith PetscErrorCode (*postcheck)(SNESLineSearch,Vec,Vec,Vec,PetscBool*,PetscBool*,void*); 30296e2a1849SPeter Brune PetscErrorCode (*func)(SNES,Vec,Vec,void*); 30306e2a1849SPeter Brune Vec f,fpc; 30316e2a1849SPeter Brune void *funcctx; 3032d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*); 30331eb13d49SPeter Brune void *jacctx,*appctx; 303432b97717SPeter Brune Mat j,jpre; 30353a40ed3dSBarry Smith 30363a40ed3dSBarry Smith PetscFunctionBegin; 30370700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 30384dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 30399b94acceSBarry Smith 30407adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 304104d7464bSBarry Smith ierr = SNESSetType(snes,SNESNEWTONLS);CHKERRQ(ierr); 304285385478SLisandro Dalcin } 304385385478SLisandro Dalcin 30440298fd71SBarry Smith ierr = SNESGetFunction(snes,&snes->vec_func,NULL,NULL);CHKERRQ(ierr); 304558c9b817SLisandro Dalcin 30466cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 3047942e3340SBarry Smith ierr = DMGetDMSNES(dm,&sdm);CHKERRQ(ierr); 3048ce94432eSBarry Smith if (!sdm->ops->computefunction) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Function never provided to SNES object"); 304922c6f798SBarry Smith if (!sdm->ops->computejacobian) { 30508d359177SBarry Smith ierr = DMSNESSetJacobian(dm,SNESComputeJacobianDefaultColor,NULL);CHKERRQ(ierr); 305119f7a02aSBarry Smith } 30526cab3a1bSJed Brown if (!snes->vec_func) { 30536cab3a1bSJed Brown ierr = DMCreateGlobalVector(dm,&snes->vec_func);CHKERRQ(ierr); 3054214df951SJed Brown } 3055efd51863SBarry Smith 305622d28d08SBarry Smith if (!snes->ksp) { 305722d28d08SBarry Smith ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr); 305822d28d08SBarry Smith } 3059b710008aSBarry Smith 306022d28d08SBarry Smith if (!snes->linesearch) { 30617601faf0SJed Brown ierr = SNESGetLineSearch(snes, &snes->linesearch);CHKERRQ(ierr); 306222d28d08SBarry Smith } 3063ed07d7d7SPeter Brune ierr = SNESLineSearchSetFunction(snes->linesearch,SNESComputeFunction);CHKERRQ(ierr); 30649e764e56SPeter Brune 3065efd4aadfSBarry Smith if (snes->npc && (snes->npcside== PC_LEFT)) { 3066172a4300SPeter Brune snes->mf = PETSC_TRUE; 3067172a4300SPeter Brune snes->mf_operator = PETSC_FALSE; 3068172a4300SPeter Brune } 3069d8f46077SPeter Brune 3070efd4aadfSBarry Smith if (snes->npc) { 30716e2a1849SPeter Brune /* copy the DM over */ 30726e2a1849SPeter Brune ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 3073efd4aadfSBarry Smith ierr = SNESSetDM(snes->npc,dm);CHKERRQ(ierr); 30746e2a1849SPeter Brune 30756e2a1849SPeter Brune ierr = SNESGetFunction(snes,&f,&func,&funcctx);CHKERRQ(ierr); 30766e2a1849SPeter Brune ierr = VecDuplicate(f,&fpc);CHKERRQ(ierr); 3077efd4aadfSBarry Smith ierr = SNESSetFunction(snes->npc,fpc,func,funcctx);CHKERRQ(ierr); 307832b97717SPeter Brune ierr = SNESGetJacobian(snes,&j,&jpre,&jac,&jacctx);CHKERRQ(ierr); 3079efd4aadfSBarry Smith ierr = SNESSetJacobian(snes->npc,j,jpre,jac,jacctx);CHKERRQ(ierr); 30801eb13d49SPeter Brune ierr = SNESGetApplicationContext(snes,&appctx);CHKERRQ(ierr); 3081efd4aadfSBarry Smith ierr = SNESSetApplicationContext(snes->npc,appctx);CHKERRQ(ierr); 30826e2a1849SPeter Brune ierr = VecDestroy(&fpc);CHKERRQ(ierr); 30836e2a1849SPeter Brune 30846e2a1849SPeter Brune /* copy the function pointers over */ 3085efd4aadfSBarry Smith ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)snes,(PetscObject)snes->npc);CHKERRQ(ierr); 30866e2a1849SPeter Brune 30876e2a1849SPeter Brune /* default to 1 iteration */ 3088efd4aadfSBarry Smith ierr = SNESSetTolerances(snes->npc,0.0,0.0,0.0,1,snes->npc->max_funcs);CHKERRQ(ierr); 3089efd4aadfSBarry Smith if (snes->npcside==PC_RIGHT) { 3090efd4aadfSBarry Smith ierr = SNESSetNormSchedule(snes->npc,SNES_NORM_FINAL_ONLY);CHKERRQ(ierr); 3091a9936a0cSPeter Brune } else { 3092efd4aadfSBarry Smith ierr = SNESSetNormSchedule(snes->npc,SNES_NORM_NONE);CHKERRQ(ierr); 3093a9936a0cSPeter Brune } 3094efd4aadfSBarry Smith ierr = SNESSetFromOptions(snes->npc);CHKERRQ(ierr); 30956e2a1849SPeter Brune 30966e2a1849SPeter Brune /* copy the line search context over */ 30977601faf0SJed Brown ierr = SNESGetLineSearch(snes,&linesearch);CHKERRQ(ierr); 3098efd4aadfSBarry Smith ierr = SNESGetLineSearch(snes->npc,&pclinesearch);CHKERRQ(ierr); 30996b2b7091SBarry Smith ierr = SNESLineSearchGetPreCheck(linesearch,&precheck,&lsprectx);CHKERRQ(ierr); 31006b2b7091SBarry Smith ierr = SNESLineSearchGetPostCheck(linesearch,&postcheck,&lspostctx);CHKERRQ(ierr); 31016b2b7091SBarry Smith ierr = SNESLineSearchSetPreCheck(pclinesearch,precheck,lsprectx);CHKERRQ(ierr); 31026b2b7091SBarry Smith ierr = SNESLineSearchSetPostCheck(pclinesearch,postcheck,lspostctx);CHKERRQ(ierr); 31036e2a1849SPeter Brune ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)linesearch, (PetscObject)pclinesearch);CHKERRQ(ierr); 31046e2a1849SPeter Brune } 310532b97717SPeter Brune if (snes->mf) { 310632b97717SPeter Brune ierr = SNESSetUpMatrixFree_Private(snes, snes->mf_operator, snes->mf_version);CHKERRQ(ierr); 310732b97717SPeter Brune } 310832b97717SPeter Brune if (snes->ops->usercompute && !snes->user) { 310932b97717SPeter Brune ierr = (*snes->ops->usercompute)(snes,(void**)&snes->user);CHKERRQ(ierr); 311032b97717SPeter Brune } 31116e2a1849SPeter Brune 311237ec4e1aSPeter Brune snes->jac_iter = 0; 311337ec4e1aSPeter Brune snes->pre_iter = 0; 311437ec4e1aSPeter Brune 3115410397dcSLisandro Dalcin if (snes->ops->setup) { 3116410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 3117410397dcSLisandro Dalcin } 311858c9b817SLisandro Dalcin 3119efd4aadfSBarry Smith if (snes->npc && (snes->npcside== PC_LEFT)) { 31206c67d002SPeter Brune if (snes->functype == SNES_FUNCTION_PRECONDITIONED) { 312155d4788fSPeter Brune ierr = SNESGetLineSearch(snes,&linesearch);CHKERRQ(ierr); 3122be95d8f1SBarry Smith ierr = SNESLineSearchSetFunction(linesearch,SNESComputeFunctionDefaultNPC);CHKERRQ(ierr); 31236c67d002SPeter Brune } 31246c67d002SPeter Brune } 31256c67d002SPeter Brune 31267aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 31273a40ed3dSBarry Smith PetscFunctionReturn(0); 31289b94acceSBarry Smith } 31299b94acceSBarry Smith 313037596af1SLisandro Dalcin /*@ 313137596af1SLisandro Dalcin SNESReset - Resets a SNES context to the snessetupcalled = 0 state and removes any allocated Vecs and Mats 313237596af1SLisandro Dalcin 313337596af1SLisandro Dalcin Collective on SNES 313437596af1SLisandro Dalcin 313537596af1SLisandro Dalcin Input Parameter: 313637596af1SLisandro Dalcin . snes - iterative context obtained from SNESCreate() 313737596af1SLisandro Dalcin 3138d25893d9SBarry Smith Level: intermediate 3139d25893d9SBarry Smith 314095452b02SPatrick Sanan Notes: 314195452b02SPatrick Sanan Also calls the application context destroy routine set with SNESSetComputeApplicationContext() 314237596af1SLisandro Dalcin 314337596af1SLisandro Dalcin .keywords: SNES, destroy 314437596af1SLisandro Dalcin 314537596af1SLisandro Dalcin .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 314637596af1SLisandro Dalcin @*/ 314737596af1SLisandro Dalcin PetscErrorCode SNESReset(SNES snes) 314837596af1SLisandro Dalcin { 314937596af1SLisandro Dalcin PetscErrorCode ierr; 315037596af1SLisandro Dalcin 315137596af1SLisandro Dalcin PetscFunctionBegin; 315237596af1SLisandro Dalcin PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3153d25893d9SBarry Smith if (snes->ops->userdestroy && snes->user) { 3154d25893d9SBarry Smith ierr = (*snes->ops->userdestroy)((void**)&snes->user);CHKERRQ(ierr); 31550298fd71SBarry Smith snes->user = NULL; 3156d25893d9SBarry Smith } 3157efd4aadfSBarry Smith if (snes->npc) { 3158efd4aadfSBarry Smith ierr = SNESReset(snes->npc);CHKERRQ(ierr); 31598a23116dSBarry Smith } 31608a23116dSBarry Smith 316137596af1SLisandro Dalcin if (snes->ops->reset) { 316237596af1SLisandro Dalcin ierr = (*snes->ops->reset)(snes);CHKERRQ(ierr); 316337596af1SLisandro Dalcin } 31649e764e56SPeter Brune if (snes->ksp) { 31659e764e56SPeter Brune ierr = KSPReset(snes->ksp);CHKERRQ(ierr); 31669e764e56SPeter Brune } 31679e764e56SPeter Brune 31689e764e56SPeter Brune if (snes->linesearch) { 3169f1c6b773SPeter Brune ierr = SNESLineSearchReset(snes->linesearch);CHKERRQ(ierr); 31709e764e56SPeter Brune } 31719e764e56SPeter Brune 31726bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 31736bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 31746bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol_update);CHKERRQ(ierr); 31756bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_func);CHKERRQ(ierr); 31766bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian);CHKERRQ(ierr); 31776bf464f9SBarry Smith ierr = MatDestroy(&snes->jacobian_pre);CHKERRQ(ierr); 3178c41d97abSJed Brown ierr = VecDestroyVecs(snes->nwork,&snes->work);CHKERRQ(ierr); 3179c41d97abSJed Brown ierr = VecDestroyVecs(snes->nvwork,&snes->vwork);CHKERRQ(ierr); 3180f5af7f23SKarl Rupp 318140fdac6aSLawrence Mitchell snes->alwayscomputesfinalresidual = PETSC_FALSE; 318240fdac6aSLawrence Mitchell 318337596af1SLisandro Dalcin snes->nwork = snes->nvwork = 0; 318437596af1SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 318537596af1SLisandro Dalcin PetscFunctionReturn(0); 318637596af1SLisandro Dalcin } 318737596af1SLisandro Dalcin 318852baeb72SSatish Balay /*@ 31899b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 31909b94acceSBarry Smith with SNESCreate(). 31919b94acceSBarry Smith 3192c7afd0dbSLois Curfman McInnes Collective on SNES 3193c7afd0dbSLois Curfman McInnes 31949b94acceSBarry Smith Input Parameter: 31959b94acceSBarry Smith . snes - the SNES context 31969b94acceSBarry Smith 319736851e7fSLois Curfman McInnes Level: beginner 319836851e7fSLois Curfman McInnes 31999b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 32009b94acceSBarry Smith 320163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 32029b94acceSBarry Smith @*/ 32036bf464f9SBarry Smith PetscErrorCode SNESDestroy(SNES *snes) 32049b94acceSBarry Smith { 32056849ba73SBarry Smith PetscErrorCode ierr; 32063a40ed3dSBarry Smith 32073a40ed3dSBarry Smith PetscFunctionBegin; 32086bf464f9SBarry Smith if (!*snes) PetscFunctionReturn(0); 32096bf464f9SBarry Smith PetscValidHeaderSpecific((*snes),SNES_CLASSID,1); 32106bf464f9SBarry Smith if (--((PetscObject)(*snes))->refct > 0) {*snes = 0; PetscFunctionReturn(0);} 3211d4bb536fSBarry Smith 32126bf464f9SBarry Smith ierr = SNESReset((*snes));CHKERRQ(ierr); 3213efd4aadfSBarry Smith ierr = SNESDestroy(&(*snes)->npc);CHKERRQ(ierr); 32146b8b9a38SLisandro Dalcin 3215e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 3216e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*snes);CHKERRQ(ierr); 32176bf464f9SBarry Smith if ((*snes)->ops->destroy) {ierr = (*((*snes))->ops->destroy)((*snes));CHKERRQ(ierr);} 32186d4c513bSLisandro Dalcin 321933124788SMatthew G. Knepley if ((*snes)->dm) {ierr = DMCoarsenHookRemove((*snes)->dm,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,*snes);CHKERRQ(ierr);} 32206bf464f9SBarry Smith ierr = DMDestroy(&(*snes)->dm);CHKERRQ(ierr); 32216bf464f9SBarry Smith ierr = KSPDestroy(&(*snes)->ksp);CHKERRQ(ierr); 3222f1c6b773SPeter Brune ierr = SNESLineSearchDestroy(&(*snes)->linesearch);CHKERRQ(ierr); 32236b8b9a38SLisandro Dalcin 32246bf464f9SBarry Smith ierr = PetscFree((*snes)->kspconvctx);CHKERRQ(ierr); 32256bf464f9SBarry Smith if ((*snes)->ops->convergeddestroy) { 32266bf464f9SBarry Smith ierr = (*(*snes)->ops->convergeddestroy)((*snes)->cnvP);CHKERRQ(ierr); 32276b8b9a38SLisandro Dalcin } 32286bf464f9SBarry Smith if ((*snes)->conv_malloc) { 32296bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist);CHKERRQ(ierr); 32306bf464f9SBarry Smith ierr = PetscFree((*snes)->conv_hist_its);CHKERRQ(ierr); 323158c9b817SLisandro Dalcin } 32326bf464f9SBarry Smith ierr = SNESMonitorCancel((*snes));CHKERRQ(ierr); 3233a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 32343a40ed3dSBarry Smith PetscFunctionReturn(0); 32359b94acceSBarry Smith } 32369b94acceSBarry Smith 32379b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 32389b94acceSBarry Smith 3239a8054027SBarry Smith /*@ 3240a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 3241a8054027SBarry Smith 32423f9fe445SBarry Smith Logically Collective on SNES 3243a8054027SBarry Smith 3244a8054027SBarry Smith Input Parameters: 3245a8054027SBarry Smith + snes - the SNES context 3246a8054027SBarry 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 32473b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 3248a8054027SBarry Smith 3249a8054027SBarry Smith Options Database Keys: 3250a8054027SBarry Smith . -snes_lag_preconditioner <lag> 3251a8054027SBarry Smith 3252a8054027SBarry Smith Notes: 3253a8054027SBarry Smith The default is 1 3254a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 3255a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 3256a8054027SBarry Smith 3257a8054027SBarry Smith Level: intermediate 3258a8054027SBarry Smith 3259a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 3260a8054027SBarry Smith 3261e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 3262a8054027SBarry Smith 3263a8054027SBarry Smith @*/ 32647087cfbeSBarry Smith PetscErrorCode SNESSetLagPreconditioner(SNES snes,PetscInt lag) 3265a8054027SBarry Smith { 3266a8054027SBarry Smith PetscFunctionBegin; 32670700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3268e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 3269e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 3270c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 3271a8054027SBarry Smith snes->lagpreconditioner = lag; 3272a8054027SBarry Smith PetscFunctionReturn(0); 3273a8054027SBarry Smith } 3274a8054027SBarry Smith 3275efd51863SBarry Smith /*@ 3276efd51863SBarry Smith SNESSetGridSequence - sets the number of steps of grid sequencing that SNES does 3277efd51863SBarry Smith 3278efd51863SBarry Smith Logically Collective on SNES 3279efd51863SBarry Smith 3280efd51863SBarry Smith Input Parameters: 3281efd51863SBarry Smith + snes - the SNES context 3282efd51863SBarry Smith - steps - the number of refinements to do, defaults to 0 3283efd51863SBarry Smith 3284efd51863SBarry Smith Options Database Keys: 3285efd51863SBarry Smith . -snes_grid_sequence <steps> 3286efd51863SBarry Smith 3287efd51863SBarry Smith Level: intermediate 3288efd51863SBarry Smith 3289c0df2a02SJed Brown Notes: 3290c0df2a02SJed Brown Use SNESGetSolution() to extract the fine grid solution after grid sequencing. 3291c0df2a02SJed Brown 3292efd51863SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 3293efd51863SBarry Smith 3294fa19ca70SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian(), SNESGetGridSequence() 3295efd51863SBarry Smith 3296efd51863SBarry Smith @*/ 3297efd51863SBarry Smith PetscErrorCode SNESSetGridSequence(SNES snes,PetscInt steps) 3298efd51863SBarry Smith { 3299efd51863SBarry Smith PetscFunctionBegin; 3300efd51863SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3301efd51863SBarry Smith PetscValidLogicalCollectiveInt(snes,steps,2); 3302efd51863SBarry Smith snes->gridsequence = steps; 3303efd51863SBarry Smith PetscFunctionReturn(0); 3304efd51863SBarry Smith } 3305efd51863SBarry Smith 3306fa19ca70SBarry Smith /*@ 3307fa19ca70SBarry Smith SNESGetGridSequence - gets the number of steps of grid sequencing that SNES does 3308fa19ca70SBarry Smith 3309fa19ca70SBarry Smith Logically Collective on SNES 3310fa19ca70SBarry Smith 3311fa19ca70SBarry Smith Input Parameter: 3312fa19ca70SBarry Smith . snes - the SNES context 3313fa19ca70SBarry Smith 3314fa19ca70SBarry Smith Output Parameter: 3315fa19ca70SBarry Smith . steps - the number of refinements to do, defaults to 0 3316fa19ca70SBarry Smith 3317fa19ca70SBarry Smith Options Database Keys: 3318fa19ca70SBarry Smith . -snes_grid_sequence <steps> 3319fa19ca70SBarry Smith 3320fa19ca70SBarry Smith Level: intermediate 3321fa19ca70SBarry Smith 3322fa19ca70SBarry Smith Notes: 3323fa19ca70SBarry Smith Use SNESGetSolution() to extract the fine grid solution after grid sequencing. 3324fa19ca70SBarry Smith 3325fa19ca70SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 3326fa19ca70SBarry Smith 3327fa19ca70SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian(), SNESSetGridSequence() 3328fa19ca70SBarry Smith 3329fa19ca70SBarry Smith @*/ 3330fa19ca70SBarry Smith PetscErrorCode SNESGetGridSequence(SNES snes,PetscInt *steps) 3331fa19ca70SBarry Smith { 3332fa19ca70SBarry Smith PetscFunctionBegin; 3333fa19ca70SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3334fa19ca70SBarry Smith *steps = snes->gridsequence; 3335fa19ca70SBarry Smith PetscFunctionReturn(0); 3336fa19ca70SBarry Smith } 3337fa19ca70SBarry Smith 3338a8054027SBarry Smith /*@ 3339a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 3340a8054027SBarry Smith 33413f9fe445SBarry Smith Not Collective 3342a8054027SBarry Smith 3343a8054027SBarry Smith Input Parameter: 3344a8054027SBarry Smith . snes - the SNES context 3345a8054027SBarry Smith 3346a8054027SBarry Smith Output Parameter: 3347a8054027SBarry 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 33483b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 3349a8054027SBarry Smith 3350a8054027SBarry Smith Options Database Keys: 3351a8054027SBarry Smith . -snes_lag_preconditioner <lag> 3352a8054027SBarry Smith 3353a8054027SBarry Smith Notes: 3354a8054027SBarry Smith The default is 1 3355a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 3356a8054027SBarry Smith 3357a8054027SBarry Smith Level: intermediate 3358a8054027SBarry Smith 3359a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 3360a8054027SBarry Smith 3361a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 3362a8054027SBarry Smith 3363a8054027SBarry Smith @*/ 33647087cfbeSBarry Smith PetscErrorCode SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 3365a8054027SBarry Smith { 3366a8054027SBarry Smith PetscFunctionBegin; 33670700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3368a8054027SBarry Smith *lag = snes->lagpreconditioner; 3369a8054027SBarry Smith PetscFunctionReturn(0); 3370a8054027SBarry Smith } 3371a8054027SBarry Smith 3372e35cf81dSBarry Smith /*@ 3373e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 3374e35cf81dSBarry Smith often the preconditioner is rebuilt. 3375e35cf81dSBarry Smith 33763f9fe445SBarry Smith Logically Collective on SNES 3377e35cf81dSBarry Smith 3378e35cf81dSBarry Smith Input Parameters: 3379e35cf81dSBarry Smith + snes - the SNES context 3380e35cf81dSBarry 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 3381fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 3382e35cf81dSBarry Smith 3383e35cf81dSBarry Smith Options Database Keys: 3384e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 3385e35cf81dSBarry Smith 3386e35cf81dSBarry Smith Notes: 3387e35cf81dSBarry Smith The default is 1 3388e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 3389fe3ffe1eSBarry 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 3390fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 3391e35cf81dSBarry Smith 3392e35cf81dSBarry Smith Level: intermediate 3393e35cf81dSBarry Smith 3394e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 3395e35cf81dSBarry Smith 3396e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 3397e35cf81dSBarry Smith 3398e35cf81dSBarry Smith @*/ 33997087cfbeSBarry Smith PetscErrorCode SNESSetLagJacobian(SNES snes,PetscInt lag) 3400e35cf81dSBarry Smith { 3401e35cf81dSBarry Smith PetscFunctionBegin; 34020700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3403e32f2f54SBarry Smith if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 3404e32f2f54SBarry Smith if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 3405c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,lag,2); 3406e35cf81dSBarry Smith snes->lagjacobian = lag; 3407e35cf81dSBarry Smith PetscFunctionReturn(0); 3408e35cf81dSBarry Smith } 3409e35cf81dSBarry Smith 3410e35cf81dSBarry Smith /*@ 3411e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 3412e35cf81dSBarry Smith 34133f9fe445SBarry Smith Not Collective 3414e35cf81dSBarry Smith 3415e35cf81dSBarry Smith Input Parameter: 3416e35cf81dSBarry Smith . snes - the SNES context 3417e35cf81dSBarry Smith 3418e35cf81dSBarry Smith Output Parameter: 3419e35cf81dSBarry 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 3420e35cf81dSBarry Smith the Jacobian is built etc. 3421e35cf81dSBarry Smith 3422e35cf81dSBarry Smith Options Database Keys: 3423e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 3424e35cf81dSBarry Smith 3425e35cf81dSBarry Smith Notes: 3426e35cf81dSBarry Smith The default is 1 3427e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 3428e35cf81dSBarry Smith 3429e35cf81dSBarry Smith Level: intermediate 3430e35cf81dSBarry Smith 3431e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 3432e35cf81dSBarry Smith 3433e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 3434e35cf81dSBarry Smith 3435e35cf81dSBarry Smith @*/ 34367087cfbeSBarry Smith PetscErrorCode SNESGetLagJacobian(SNES snes,PetscInt *lag) 3437e35cf81dSBarry Smith { 3438e35cf81dSBarry Smith PetscFunctionBegin; 34390700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3440e35cf81dSBarry Smith *lag = snes->lagjacobian; 3441e35cf81dSBarry Smith PetscFunctionReturn(0); 3442e35cf81dSBarry Smith } 3443e35cf81dSBarry Smith 344437ec4e1aSPeter Brune /*@ 344537ec4e1aSPeter Brune SNESSetLagJacobianPersists - Set whether or not the Jacobian lagging persists through multiple solves 344637ec4e1aSPeter Brune 344737ec4e1aSPeter Brune Logically collective on SNES 344837ec4e1aSPeter Brune 344937ec4e1aSPeter Brune Input Parameter: 345037ec4e1aSPeter Brune + snes - the SNES context 34519d7e2deaSPeter Brune - flg - jacobian lagging persists if true 345237ec4e1aSPeter Brune 345337ec4e1aSPeter Brune Options Database Keys: 345437ec4e1aSPeter Brune . -snes_lag_jacobian_persists <flg> 345537ec4e1aSPeter Brune 345695452b02SPatrick Sanan Notes: 345795452b02SPatrick Sanan This is useful both for nonlinear preconditioning, where it's appropriate to have the Jacobian be stale by 345837ec4e1aSPeter Brune several solves, and for implicit time-stepping, where Jacobian lagging in the inner nonlinear solve over several 345937ec4e1aSPeter Brune timesteps may present huge efficiency gains. 346037ec4e1aSPeter Brune 346137ec4e1aSPeter Brune Level: developer 346237ec4e1aSPeter Brune 3463be95d8f1SBarry Smith .keywords: SNES, nonlinear, lag 346437ec4e1aSPeter Brune 3465be95d8f1SBarry Smith .seealso: SNESSetLagPreconditionerPersists(), SNESSetLagJacobian(), SNESGetLagJacobian(), SNESGetNPC() 346637ec4e1aSPeter Brune 346737ec4e1aSPeter Brune @*/ 346837ec4e1aSPeter Brune PetscErrorCode SNESSetLagJacobianPersists(SNES snes,PetscBool flg) 346937ec4e1aSPeter Brune { 347037ec4e1aSPeter Brune PetscFunctionBegin; 347137ec4e1aSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 347237ec4e1aSPeter Brune PetscValidLogicalCollectiveBool(snes,flg,2); 347337ec4e1aSPeter Brune snes->lagjac_persist = flg; 347437ec4e1aSPeter Brune PetscFunctionReturn(0); 347537ec4e1aSPeter Brune } 347637ec4e1aSPeter Brune 347737ec4e1aSPeter Brune /*@ 347837ec4e1aSPeter Brune SNESSetLagPreconditionerPersists - Set whether or not the preconditioner lagging persists through multiple solves 347937ec4e1aSPeter Brune 348037ec4e1aSPeter Brune Logically Collective on SNES 348137ec4e1aSPeter Brune 348237ec4e1aSPeter Brune Input Parameter: 348337ec4e1aSPeter Brune + snes - the SNES context 34849d7e2deaSPeter Brune - flg - preconditioner lagging persists if true 348537ec4e1aSPeter Brune 348637ec4e1aSPeter Brune Options Database Keys: 348737ec4e1aSPeter Brune . -snes_lag_jacobian_persists <flg> 348837ec4e1aSPeter Brune 348995452b02SPatrick Sanan Notes: 349095452b02SPatrick Sanan This is useful both for nonlinear preconditioning, where it's appropriate to have the preconditioner be stale 349137ec4e1aSPeter Brune by several solves, and for implicit time-stepping, where preconditioner lagging in the inner nonlinear solve over 349237ec4e1aSPeter Brune several timesteps may present huge efficiency gains. 349337ec4e1aSPeter Brune 349437ec4e1aSPeter Brune Level: developer 349537ec4e1aSPeter Brune 3496be95d8f1SBarry Smith .keywords: SNES, nonlinear, lag 349737ec4e1aSPeter Brune 3498be95d8f1SBarry Smith .seealso: SNESSetLagJacobianPersists(), SNESSetLagJacobian(), SNESGetLagJacobian(), SNESGetNPC() 349937ec4e1aSPeter Brune 350037ec4e1aSPeter Brune @*/ 350137ec4e1aSPeter Brune PetscErrorCode SNESSetLagPreconditionerPersists(SNES snes,PetscBool flg) 350237ec4e1aSPeter Brune { 350337ec4e1aSPeter Brune PetscFunctionBegin; 350437ec4e1aSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 350537ec4e1aSPeter Brune PetscValidLogicalCollectiveBool(snes,flg,2); 350637ec4e1aSPeter Brune snes->lagpre_persist = flg; 350737ec4e1aSPeter Brune PetscFunctionReturn(0); 350837ec4e1aSPeter Brune } 350937ec4e1aSPeter Brune 35109b94acceSBarry Smith /*@ 3511be5caee7SBarry Smith SNESSetForceIteration - force SNESSolve() to take at least one iteration regardless of the initial residual norm 3512be5caee7SBarry Smith 3513be5caee7SBarry Smith Logically Collective on SNES 3514be5caee7SBarry Smith 3515be5caee7SBarry Smith Input Parameters: 3516be5caee7SBarry Smith + snes - the SNES context 3517be5caee7SBarry Smith - force - PETSC_TRUE require at least one iteration 3518be5caee7SBarry Smith 3519be5caee7SBarry Smith Options Database Keys: 3520be5caee7SBarry Smith . -snes_force_iteration <force> - Sets forcing an iteration 3521be5caee7SBarry Smith 3522be5caee7SBarry Smith Notes: 3523be5caee7SBarry Smith This is used sometimes with TS to prevent TS from detecting a false steady state solution 3524be5caee7SBarry Smith 3525be5caee7SBarry Smith Level: intermediate 3526be5caee7SBarry Smith 3527be5caee7SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 3528be5caee7SBarry Smith 3529be5caee7SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetDivergenceTolerance() 3530be5caee7SBarry Smith @*/ 3531be5caee7SBarry Smith PetscErrorCode SNESSetForceIteration(SNES snes,PetscBool force) 3532be5caee7SBarry Smith { 3533be5caee7SBarry Smith PetscFunctionBegin; 3534be5caee7SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3535be5caee7SBarry Smith snes->forceiteration = force; 3536be5caee7SBarry Smith PetscFunctionReturn(0); 3537be5caee7SBarry Smith } 3538be5caee7SBarry Smith 353985216dc7SFande Kong /*@ 354085216dc7SFande Kong SNESGetForceIteration - Whether or not to force SNESSolve() take at least one iteration regardless of the initial residual norm 354185216dc7SFande Kong 354285216dc7SFande Kong Logically Collective on SNES 354385216dc7SFande Kong 354485216dc7SFande Kong Input Parameters: 354585216dc7SFande Kong . snes - the SNES context 354685216dc7SFande Kong 354785216dc7SFande Kong Output Parameter: 354885216dc7SFande Kong . force - PETSC_TRUE requires at least one iteration. 354985216dc7SFande Kong 355085216dc7SFande Kong .keywords: SNES, nonlinear, set, convergence, tolerances 355185216dc7SFande Kong 355206dd6b0eSSatish Balay Level: intermediate 355306dd6b0eSSatish Balay 355485216dc7SFande Kong .seealso: SNESSetForceIteration(), SNESSetTrustRegionTolerance(), SNESSetDivergenceTolerance() 355585216dc7SFande Kong @*/ 355685216dc7SFande Kong PetscErrorCode SNESGetForceIteration(SNES snes,PetscBool *force) 355785216dc7SFande Kong { 355885216dc7SFande Kong PetscFunctionBegin; 355985216dc7SFande Kong PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 356085216dc7SFande Kong *force = snes->forceiteration; 356185216dc7SFande Kong PetscFunctionReturn(0); 356285216dc7SFande Kong } 3563be5caee7SBarry Smith 3564be5caee7SBarry Smith /*@ 3565d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 35669b94acceSBarry Smith 35673f9fe445SBarry Smith Logically Collective on SNES 3568c7afd0dbSLois Curfman McInnes 35699b94acceSBarry Smith Input Parameters: 3570c7afd0dbSLois Curfman McInnes + snes - the SNES context 357170441072SBarry Smith . abstol - absolute convergence tolerance 357233174efeSLois Curfman McInnes . rtol - relative convergence tolerance 35735358d0d4SBarry Smith . stol - convergence tolerance in terms of the norm of the change in the solution between steps, || delta x || < stol*|| x || 357433174efeSLois Curfman McInnes . maxit - maximum number of iterations 3575e71169deSBarry Smith - maxf - maximum number of function evaluations (-1 indicates no limit) 3576fee21e36SBarry Smith 357733174efeSLois Curfman McInnes Options Database Keys: 357870441072SBarry Smith + -snes_atol <abstol> - Sets abstol 3579c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 3580c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 3581c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 3582c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 35839b94acceSBarry Smith 3584d7a720efSLois Curfman McInnes Notes: 35859b94acceSBarry Smith The default maximum number of iterations is 50. 35869b94acceSBarry Smith The default maximum number of function evaluations is 1000. 35879b94acceSBarry Smith 358836851e7fSLois Curfman McInnes Level: intermediate 358936851e7fSLois Curfman McInnes 359033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 35919b94acceSBarry Smith 3592be5caee7SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetDivergenceTolerance(), SNESSetForceIteration() 35939b94acceSBarry Smith @*/ 35947087cfbeSBarry Smith PetscErrorCode SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 35959b94acceSBarry Smith { 35963a40ed3dSBarry Smith PetscFunctionBegin; 35970700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3598c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,abstol,2); 3599c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol,3); 3600c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,stol,4); 3601c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxit,5); 3602c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,maxf,6); 3603c5eb9154SBarry Smith 3604ab54825eSJed Brown if (abstol != PETSC_DEFAULT) { 360557622a8eSBarry Smith if (abstol < 0.0) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Absolute tolerance %g must be non-negative",(double)abstol); 3606ab54825eSJed Brown snes->abstol = abstol; 3607ab54825eSJed Brown } 3608ab54825eSJed Brown if (rtol != PETSC_DEFAULT) { 360957622a8eSBarry Smith if (rtol < 0.0 || 1.0 <= rtol) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Relative tolerance %g must be non-negative and less than 1.0",(double)rtol); 3610ab54825eSJed Brown snes->rtol = rtol; 3611ab54825eSJed Brown } 3612ab54825eSJed Brown if (stol != PETSC_DEFAULT) { 361357622a8eSBarry Smith if (stol < 0.0) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Step tolerance %g must be non-negative",(double)stol); 3614c60f73f4SPeter Brune snes->stol = stol; 3615ab54825eSJed Brown } 3616ab54825eSJed Brown if (maxit != PETSC_DEFAULT) { 3617ce94432eSBarry Smith if (maxit < 0) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of iterations %D must be non-negative",maxit); 3618ab54825eSJed Brown snes->max_its = maxit; 3619ab54825eSJed Brown } 3620ab54825eSJed Brown if (maxf != PETSC_DEFAULT) { 3621e71169deSBarry Smith if (maxf < -1) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"Maximum number of function evaluations %D must be -1 or nonnegative",maxf); 3622ab54825eSJed Brown snes->max_funcs = maxf; 3623ab54825eSJed Brown } 362488976e71SPeter Brune snes->tolerancesset = PETSC_TRUE; 36253a40ed3dSBarry Smith PetscFunctionReturn(0); 36269b94acceSBarry Smith } 36279b94acceSBarry Smith 3628e4d06f11SPatrick Farrell /*@ 3629e4d06f11SPatrick Farrell SNESSetDivergenceTolerance - Sets the divergence tolerance used for the SNES divergence test. 3630e4d06f11SPatrick Farrell 3631e4d06f11SPatrick Farrell Logically Collective on SNES 3632e4d06f11SPatrick Farrell 3633e4d06f11SPatrick Farrell Input Parameters: 3634e4d06f11SPatrick Farrell + snes - the SNES context 3635e4d06f11SPatrick Farrell - divtol - the divergence tolerance. Use -1 to deactivate the test. 3636e4d06f11SPatrick Farrell 3637e4d06f11SPatrick Farrell Options Database Keys: 3638e4d06f11SPatrick Farrell + -snes_divergence_tolerance <divtol> - Sets divtol 3639e4d06f11SPatrick Farrell 3640e4d06f11SPatrick Farrell Notes: 3641e4d06f11SPatrick Farrell The default divergence tolerance is 1e4. 3642e4d06f11SPatrick Farrell 3643e4d06f11SPatrick Farrell Level: intermediate 3644e4d06f11SPatrick Farrell 3645e4d06f11SPatrick Farrell .keywords: SNES, nonlinear, set, divergence, tolerance 3646e4d06f11SPatrick Farrell 3647e4d06f11SPatrick Farrell .seealso: SNESSetTolerances(), SNESGetDivergenceTolerance 3648e4d06f11SPatrick Farrell @*/ 3649e4d06f11SPatrick Farrell PetscErrorCode SNESSetDivergenceTolerance(SNES snes,PetscReal divtol) 3650e4d06f11SPatrick Farrell { 3651e4d06f11SPatrick Farrell PetscFunctionBegin; 3652e4d06f11SPatrick Farrell PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3653e4d06f11SPatrick Farrell PetscValidLogicalCollectiveReal(snes,divtol,2); 3654e4d06f11SPatrick Farrell 3655e4d06f11SPatrick Farrell if (divtol != PETSC_DEFAULT) { 3656e4d06f11SPatrick Farrell snes->divtol = divtol; 3657e4d06f11SPatrick Farrell } 3658e4d06f11SPatrick Farrell else { 3659e4d06f11SPatrick Farrell snes->divtol = 1.0e4; 3660e4d06f11SPatrick Farrell } 3661e4d06f11SPatrick Farrell PetscFunctionReturn(0); 3662e4d06f11SPatrick Farrell } 3663e4d06f11SPatrick Farrell 36649b94acceSBarry Smith /*@ 366533174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 366633174efeSLois Curfman McInnes 3667c7afd0dbSLois Curfman McInnes Not Collective 3668c7afd0dbSLois Curfman McInnes 366933174efeSLois Curfman McInnes Input Parameters: 3670c7afd0dbSLois Curfman McInnes + snes - the SNES context 367185385478SLisandro Dalcin . atol - absolute convergence tolerance 367233174efeSLois Curfman McInnes . rtol - relative convergence tolerance 367333174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 367433174efeSLois Curfman McInnes of the change in the solution between steps 367533174efeSLois Curfman McInnes . maxit - maximum number of iterations 3676c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 3677fee21e36SBarry Smith 367833174efeSLois Curfman McInnes Notes: 36790298fd71SBarry Smith The user can specify NULL for any parameter that is not needed. 368033174efeSLois Curfman McInnes 368136851e7fSLois Curfman McInnes Level: intermediate 368236851e7fSLois Curfman McInnes 368333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 368433174efeSLois Curfman McInnes 368533174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 368633174efeSLois Curfman McInnes @*/ 36877087cfbeSBarry Smith PetscErrorCode SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 368833174efeSLois Curfman McInnes { 36893a40ed3dSBarry Smith PetscFunctionBegin; 36900700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 369185385478SLisandro Dalcin if (atol) *atol = snes->abstol; 369233174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 3693c60f73f4SPeter Brune if (stol) *stol = snes->stol; 369433174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 369533174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 36963a40ed3dSBarry Smith PetscFunctionReturn(0); 369733174efeSLois Curfman McInnes } 369833174efeSLois Curfman McInnes 3699e4d06f11SPatrick Farrell /*@ 3700e4d06f11SPatrick Farrell SNESGetDivergenceTolerance - Gets divergence tolerance used in divergence test. 3701e4d06f11SPatrick Farrell 3702e4d06f11SPatrick Farrell Not Collective 3703e4d06f11SPatrick Farrell 3704e4d06f11SPatrick Farrell Input Parameters: 3705e4d06f11SPatrick Farrell + snes - the SNES context 3706e4d06f11SPatrick Farrell - divtol - divergence tolerance 3707e4d06f11SPatrick Farrell 3708e4d06f11SPatrick Farrell Level: intermediate 3709e4d06f11SPatrick Farrell 3710e4d06f11SPatrick Farrell .keywords: SNES, nonlinear, get, divergence, tolerance 3711e4d06f11SPatrick Farrell 3712e4d06f11SPatrick Farrell .seealso: SNESSetDivergenceTolerance() 3713e4d06f11SPatrick Farrell @*/ 3714e4d06f11SPatrick Farrell PetscErrorCode SNESGetDivergenceTolerance(SNES snes,PetscReal *divtol) 3715e4d06f11SPatrick Farrell { 3716e4d06f11SPatrick Farrell PetscFunctionBegin; 3717e4d06f11SPatrick Farrell PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3718e4d06f11SPatrick Farrell if (divtol) *divtol = snes->divtol; 3719e4d06f11SPatrick Farrell PetscFunctionReturn(0); 3720e4d06f11SPatrick Farrell } 3721e4d06f11SPatrick Farrell 372233174efeSLois Curfman McInnes /*@ 37239b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 37249b94acceSBarry Smith 37253f9fe445SBarry Smith Logically Collective on SNES 3726fee21e36SBarry Smith 3727c7afd0dbSLois Curfman McInnes Input Parameters: 3728c7afd0dbSLois Curfman McInnes + snes - the SNES context 3729c7afd0dbSLois Curfman McInnes - tol - tolerance 3730c7afd0dbSLois Curfman McInnes 37319b94acceSBarry Smith Options Database Key: 3732c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 37339b94acceSBarry Smith 373436851e7fSLois Curfman McInnes Level: intermediate 373536851e7fSLois Curfman McInnes 37369b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 37379b94acceSBarry Smith 37382492ecdbSBarry Smith .seealso: SNESSetTolerances() 37399b94acceSBarry Smith @*/ 37407087cfbeSBarry Smith PetscErrorCode SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 37419b94acceSBarry Smith { 37423a40ed3dSBarry Smith PetscFunctionBegin; 37430700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3744c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,tol,2); 37459b94acceSBarry Smith snes->deltatol = tol; 37463a40ed3dSBarry Smith PetscFunctionReturn(0); 37479b94acceSBarry Smith } 37489b94acceSBarry Smith 3749df9fa365SBarry Smith /* 3750df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 3751df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 3752df9fa365SBarry Smith macros instead of functions 3753df9fa365SBarry Smith */ 3754d96771aaSLisandro Dalcin PetscErrorCode SNESMonitorLGResidualNorm(SNES snes,PetscInt it,PetscReal norm,void *ctx) 3755ce1608b8SBarry Smith { 3756dfbe8321SBarry Smith PetscErrorCode ierr; 3757ce1608b8SBarry Smith 3758ce1608b8SBarry Smith PetscFunctionBegin; 37590700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3760d96771aaSLisandro Dalcin ierr = KSPMonitorLGResidualNorm((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 3761ce1608b8SBarry Smith PetscFunctionReturn(0); 3762ce1608b8SBarry Smith } 3763ce1608b8SBarry Smith 3764d96771aaSLisandro Dalcin PetscErrorCode SNESMonitorLGCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *lgctx) 3765df9fa365SBarry Smith { 3766dfbe8321SBarry Smith PetscErrorCode ierr; 3767df9fa365SBarry Smith 3768df9fa365SBarry Smith PetscFunctionBegin; 3769d96771aaSLisandro Dalcin ierr = KSPMonitorLGResidualNormCreate(comm,host,label,x,y,m,n,lgctx);CHKERRQ(ierr); 3770df9fa365SBarry Smith PetscFunctionReturn(0); 3771df9fa365SBarry Smith } 3772df9fa365SBarry Smith 37736ba87a44SLisandro Dalcin PETSC_INTERN PetscErrorCode SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 37746ba87a44SLisandro Dalcin 37757087cfbeSBarry Smith PetscErrorCode SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 3776b271bb04SBarry Smith { 3777b271bb04SBarry Smith PetscDrawLG lg; 3778b271bb04SBarry Smith PetscErrorCode ierr; 3779b271bb04SBarry Smith PetscReal x,y,per; 3780b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 3781b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 3782b271bb04SBarry Smith PetscDraw draw; 3783b271bb04SBarry Smith 3784459f5d12SBarry Smith PetscFunctionBegin; 37854d4332d5SBarry Smith PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,4); 3786b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 3787b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 3788b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 3789b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 3790b271bb04SBarry Smith x = (PetscReal)n; 379177b4d14cSPeter Brune if (rnorm > 0.0) y = PetscLog10Real(rnorm); 379294c9c6d3SKarl Rupp else y = -15.0; 3793b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 37946934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 3795b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 37966934998bSLisandro Dalcin ierr = PetscDrawLGSave(lg);CHKERRQ(ierr); 3797b271bb04SBarry Smith } 3798b271bb04SBarry Smith 3799b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 3800b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 3801b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 3802b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 3803b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 3804b271bb04SBarry Smith x = (PetscReal)n; 3805b271bb04SBarry Smith y = 100.0*per; 3806b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 38076934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 3808b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 38096934998bSLisandro Dalcin ierr = PetscDrawLGSave(lg);CHKERRQ(ierr); 3810b271bb04SBarry Smith } 3811b271bb04SBarry Smith 3812b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 3813b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 3814b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 3815b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 3816b271bb04SBarry Smith x = (PetscReal)n; 3817b271bb04SBarry Smith y = (prev - rnorm)/prev; 3818b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 38196934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 3820b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 38216934998bSLisandro Dalcin ierr = PetscDrawLGSave(lg);CHKERRQ(ierr); 3822b271bb04SBarry Smith } 3823b271bb04SBarry Smith 3824b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 3825b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 3826b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 3827b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 3828b271bb04SBarry Smith x = (PetscReal)n; 3829b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 3830b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 3831b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 3832b271bb04SBarry Smith } 38336934998bSLisandro Dalcin if (n < 20 || !(n % 5) || snes->reason) { 3834b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 38356934998bSLisandro Dalcin ierr = PetscDrawLGSave(lg);CHKERRQ(ierr); 3836b271bb04SBarry Smith } 3837b271bb04SBarry Smith prev = rnorm; 3838b271bb04SBarry Smith PetscFunctionReturn(0); 3839b271bb04SBarry Smith } 3840b271bb04SBarry Smith 3841228d79bcSJed Brown /*@ 3842228d79bcSJed Brown SNESMonitor - runs the user provided monitor routines, if they exist 3843228d79bcSJed Brown 3844228d79bcSJed Brown Collective on SNES 3845228d79bcSJed Brown 3846228d79bcSJed Brown Input Parameters: 3847228d79bcSJed Brown + snes - nonlinear solver context obtained from SNESCreate() 3848228d79bcSJed Brown . iter - iteration number 3849228d79bcSJed Brown - rnorm - relative norm of the residual 3850228d79bcSJed Brown 3851228d79bcSJed Brown Notes: 3852228d79bcSJed Brown This routine is called by the SNES implementations. 3853228d79bcSJed Brown It does not typically need to be called by the user. 3854228d79bcSJed Brown 3855228d79bcSJed Brown Level: developer 3856228d79bcSJed Brown 3857228d79bcSJed Brown .seealso: SNESMonitorSet() 3858228d79bcSJed Brown @*/ 38597a03ce2fSLisandro Dalcin PetscErrorCode SNESMonitor(SNES snes,PetscInt iter,PetscReal rnorm) 38607a03ce2fSLisandro Dalcin { 38617a03ce2fSLisandro Dalcin PetscErrorCode ierr; 38627a03ce2fSLisandro Dalcin PetscInt i,n = snes->numbermonitors; 38637a03ce2fSLisandro Dalcin 38647a03ce2fSLisandro Dalcin PetscFunctionBegin; 38655edff71fSBarry Smith ierr = VecLockPush(snes->vec_sol);CHKERRQ(ierr); 38667a03ce2fSLisandro Dalcin for (i=0; i<n; i++) { 38677a03ce2fSLisandro Dalcin ierr = (*snes->monitor[i])(snes,iter,rnorm,snes->monitorcontext[i]);CHKERRQ(ierr); 38687a03ce2fSLisandro Dalcin } 38695edff71fSBarry Smith ierr = VecLockPop(snes->vec_sol);CHKERRQ(ierr); 38707a03ce2fSLisandro Dalcin PetscFunctionReturn(0); 38717a03ce2fSLisandro Dalcin } 38727a03ce2fSLisandro Dalcin 38739b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 38749b94acceSBarry Smith 3875bf388a1fSBarry Smith /*MC 3876bf388a1fSBarry Smith SNESMonitorFunction - functional form passed to SNESMonitorSet() to monitor convergence of nonlinear solver 3877bf388a1fSBarry Smith 3878bf388a1fSBarry Smith Synopsis: 3879aaa7dc30SBarry Smith #include <petscsnes.h> 3880bf388a1fSBarry Smith $ PetscErrorCode SNESMonitorFunction(SNES snes,PetscInt its, PetscReal norm,void *mctx) 3881bf388a1fSBarry Smith 3882bf388a1fSBarry Smith + snes - the SNES context 3883bf388a1fSBarry Smith . its - iteration number 3884bf388a1fSBarry Smith . norm - 2-norm function value (may be estimated) 3885bf388a1fSBarry Smith - mctx - [optional] monitoring context 3886bf388a1fSBarry Smith 3887878cb397SSatish Balay Level: advanced 3888878cb397SSatish Balay 3889bf388a1fSBarry Smith .seealso: SNESMonitorSet(), SNESMonitorGet() 3890bf388a1fSBarry Smith M*/ 3891bf388a1fSBarry Smith 38929b94acceSBarry Smith /*@C 3893a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 38949b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 38959b94acceSBarry Smith progress. 38969b94acceSBarry Smith 38973f9fe445SBarry Smith Logically Collective on SNES 3898fee21e36SBarry Smith 3899c7afd0dbSLois Curfman McInnes Input Parameters: 3900c7afd0dbSLois Curfman McInnes + snes - the SNES context 39016e4dcb14SBarry Smith . f - the monitor function, see SNESMonitorFunction for the calling sequence 3902b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 39030298fd71SBarry Smith monitor routine (use NULL if no context is desired) 3904b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 39050298fd71SBarry Smith (may be NULL) 39069b94acceSBarry Smith 39079665c990SLois Curfman McInnes Options Database Keys: 3908a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 39094619e776SBarry Smith . -snes_monitor_lg_residualnorm - sets line graph monitor, 3910a6570f20SBarry Smith uses SNESMonitorLGCreate() 3911cca6129bSJed Brown - -snes_monitor_cancel - cancels all monitors that have 3912c7afd0dbSLois Curfman McInnes been hardwired into a code by 3913a6570f20SBarry Smith calls to SNESMonitorSet(), but 3914c7afd0dbSLois Curfman McInnes does not cancel those set via 3915c7afd0dbSLois Curfman McInnes the options database. 39169665c990SLois Curfman McInnes 3917639f9d9dSBarry Smith Notes: 39186bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 3919a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 39206bc08f3fSLois Curfman McInnes order in which they were set. 3921639f9d9dSBarry Smith 392295452b02SPatrick Sanan Fortran Notes: 392395452b02SPatrick Sanan Only a single monitor function can be set for each SNES object 3924025f1a04SBarry Smith 392536851e7fSLois Curfman McInnes Level: intermediate 392636851e7fSLois Curfman McInnes 39279b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 39289b94acceSBarry Smith 3929bf388a1fSBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel(), SNESMonitorFunction 39309b94acceSBarry Smith @*/ 39316e4dcb14SBarry Smith PetscErrorCode SNESMonitorSet(SNES snes,PetscErrorCode (*f)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 39329b94acceSBarry Smith { 3933b90d0a6eSBarry Smith PetscInt i; 3934649052a6SBarry Smith PetscErrorCode ierr; 393578064530SBarry Smith PetscBool identical; 3936b90d0a6eSBarry Smith 39373a40ed3dSBarry Smith PetscFunctionBegin; 39380700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3939b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 394078064530SBarry Smith ierr = PetscMonitorCompare((PetscErrorCode (*)(void))f,mctx,monitordestroy,(PetscErrorCode (*)(void))snes->monitor[i],snes->monitorcontext[i],snes->monitordestroy[i],&identical);CHKERRQ(ierr); 394178064530SBarry Smith if (identical) PetscFunctionReturn(0); 3942649052a6SBarry Smith } 394378064530SBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 39446e4dcb14SBarry Smith snes->monitor[snes->numbermonitors] = f; 3945b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 3946639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 39473a40ed3dSBarry Smith PetscFunctionReturn(0); 39489b94acceSBarry Smith } 39499b94acceSBarry Smith 3950a278d85bSSatish Balay /*@ 3951a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 39525cd90555SBarry Smith 39533f9fe445SBarry Smith Logically Collective on SNES 3954c7afd0dbSLois Curfman McInnes 39555cd90555SBarry Smith Input Parameters: 39565cd90555SBarry Smith . snes - the SNES context 39575cd90555SBarry Smith 39581a480d89SAdministrator Options Database Key: 3959a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 3960a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 3961c7afd0dbSLois Curfman McInnes set via the options database 39625cd90555SBarry Smith 39635cd90555SBarry Smith Notes: 39645cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 39655cd90555SBarry Smith 396636851e7fSLois Curfman McInnes Level: intermediate 396736851e7fSLois Curfman McInnes 39685cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 39695cd90555SBarry Smith 3970a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 39715cd90555SBarry Smith @*/ 39727087cfbeSBarry Smith PetscErrorCode SNESMonitorCancel(SNES snes) 39735cd90555SBarry Smith { 3974d952e501SBarry Smith PetscErrorCode ierr; 3975d952e501SBarry Smith PetscInt i; 3976d952e501SBarry Smith 39775cd90555SBarry Smith PetscFunctionBegin; 39780700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 3979d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 3980d952e501SBarry Smith if (snes->monitordestroy[i]) { 39813c4aec1bSBarry Smith ierr = (*snes->monitordestroy[i])(&snes->monitorcontext[i]);CHKERRQ(ierr); 3982d952e501SBarry Smith } 3983d952e501SBarry Smith } 39845cd90555SBarry Smith snes->numbermonitors = 0; 39855cd90555SBarry Smith PetscFunctionReturn(0); 39865cd90555SBarry Smith } 39875cd90555SBarry Smith 3988bf388a1fSBarry Smith /*MC 3989bf388a1fSBarry Smith SNESConvergenceTestFunction - functional form used for testing of convergence of nonlinear solver 3990bf388a1fSBarry Smith 3991bf388a1fSBarry Smith Synopsis: 3992aaa7dc30SBarry Smith #include <petscsnes.h> 3993bf388a1fSBarry Smith $ PetscErrorCode SNESConvergenceTest(SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 3994bf388a1fSBarry Smith 3995bf388a1fSBarry Smith + snes - the SNES context 3996bf388a1fSBarry Smith . it - current iteration (0 is the first and is before any Newton step) 3997bf388a1fSBarry Smith . cctx - [optional] convergence context 3998bf388a1fSBarry Smith . reason - reason for convergence/divergence 3999bf388a1fSBarry Smith . xnorm - 2-norm of current iterate 4000bf388a1fSBarry Smith . gnorm - 2-norm of current step 4001bf388a1fSBarry Smith - f - 2-norm of function 4002bf388a1fSBarry Smith 4003878cb397SSatish Balay Level: intermediate 4004bf388a1fSBarry Smith 4005bf388a1fSBarry Smith .seealso: SNESSetConvergenceTest(), SNESGetConvergenceTest() 4006bf388a1fSBarry Smith M*/ 4007bf388a1fSBarry Smith 40089b94acceSBarry Smith /*@C 40099b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 40109b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 40119b94acceSBarry Smith 40123f9fe445SBarry Smith Logically Collective on SNES 4013fee21e36SBarry Smith 4014c7afd0dbSLois Curfman McInnes Input Parameters: 4015c7afd0dbSLois Curfman McInnes + snes - the SNES context 4016bf388a1fSBarry Smith . SNESConvergenceTestFunction - routine to test for convergence 40170298fd71SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be NULL) 4018cf90aa19SBarry Smith - destroy - [optional] destructor for the context (may be NULL; PETSC_NULL_FUNCTION in Fortran) 40199b94acceSBarry Smith 402036851e7fSLois Curfman McInnes Level: advanced 402136851e7fSLois Curfman McInnes 40229b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 40239b94acceSBarry Smith 4024e2a6519dSDmitry Karpeev .seealso: SNESConvergedDefault(), SNESConvergedSkip(), SNESConvergenceTestFunction 40259b94acceSBarry Smith @*/ 4026bf388a1fSBarry Smith PetscErrorCode SNESSetConvergenceTest(SNES snes,PetscErrorCode (*SNESConvergenceTestFunction)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 40279b94acceSBarry Smith { 40287f7931b9SBarry Smith PetscErrorCode ierr; 40297f7931b9SBarry Smith 40303a40ed3dSBarry Smith PetscFunctionBegin; 40310700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4032e2a6519dSDmitry Karpeev if (!SNESConvergenceTestFunction) SNESConvergenceTestFunction = SNESConvergedSkip; 40337f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 40347f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 40357f7931b9SBarry Smith } 4036bf388a1fSBarry Smith snes->ops->converged = SNESConvergenceTestFunction; 40377f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 403885385478SLisandro Dalcin snes->cnvP = cctx; 40393a40ed3dSBarry Smith PetscFunctionReturn(0); 40409b94acceSBarry Smith } 40419b94acceSBarry Smith 404252baeb72SSatish Balay /*@ 4043184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 4044184914b5SBarry Smith 4045184914b5SBarry Smith Not Collective 4046184914b5SBarry Smith 4047184914b5SBarry Smith Input Parameter: 4048184914b5SBarry Smith . snes - the SNES context 4049184914b5SBarry Smith 4050184914b5SBarry Smith Output Parameter: 40514d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 4052184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 4053184914b5SBarry Smith 40546a4d7782SBarry Smith Options Database: 40556a4d7782SBarry Smith . -snes_converged_reason - prints the reason to standard out 40566a4d7782SBarry Smith 4057184914b5SBarry Smith Level: intermediate 4058184914b5SBarry Smith 405995452b02SPatrick Sanan Notes: 406095452b02SPatrick Sanan Should only be called after the call the SNESSolve() is complete, if it is called earlier it returns the value SNES__CONVERGED_ITERATING. 4061184914b5SBarry Smith 4062184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 4063184914b5SBarry Smith 406433866048SMatthew G. Knepley .seealso: SNESSetConvergenceTest(), SNESSetConvergedReason(), SNESConvergedReason 4065184914b5SBarry Smith @*/ 40667087cfbeSBarry Smith PetscErrorCode SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 4067184914b5SBarry Smith { 4068184914b5SBarry Smith PetscFunctionBegin; 40690700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 40704482741eSBarry Smith PetscValidPointer(reason,2); 4071184914b5SBarry Smith *reason = snes->reason; 4072184914b5SBarry Smith PetscFunctionReturn(0); 4073184914b5SBarry Smith } 4074184914b5SBarry Smith 407533866048SMatthew G. Knepley /*@ 407633866048SMatthew G. Knepley SNESSetConvergedReason - Sets the reason the SNES iteration was stopped. 407733866048SMatthew G. Knepley 407833866048SMatthew G. Knepley Not Collective 407933866048SMatthew G. Knepley 408033866048SMatthew G. Knepley Input Parameters: 408133866048SMatthew G. Knepley + snes - the SNES context 408233866048SMatthew G. Knepley - reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 408333866048SMatthew G. Knepley manual pages for the individual convergence tests for complete lists 408433866048SMatthew G. Knepley 408533866048SMatthew G. Knepley Level: intermediate 408633866048SMatthew G. Knepley 408733866048SMatthew G. Knepley .keywords: SNES, nonlinear, set, convergence, test 408833866048SMatthew G. Knepley .seealso: SNESGetConvergedReason(), SNESSetConvergenceTest(), SNESConvergedReason 408933866048SMatthew G. Knepley @*/ 409033866048SMatthew G. Knepley PetscErrorCode SNESSetConvergedReason(SNES snes,SNESConvergedReason reason) 409133866048SMatthew G. Knepley { 409233866048SMatthew G. Knepley PetscFunctionBegin; 409333866048SMatthew G. Knepley PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 409433866048SMatthew G. Knepley snes->reason = reason; 409533866048SMatthew G. Knepley PetscFunctionReturn(0); 409633866048SMatthew G. Knepley } 409733866048SMatthew G. Knepley 4098c9005455SLois Curfman McInnes /*@ 4099c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 4100c9005455SLois Curfman McInnes 41013f9fe445SBarry Smith Logically Collective on SNES 4102fee21e36SBarry Smith 4103c7afd0dbSLois Curfman McInnes Input Parameters: 4104c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 41058c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 4106cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 4107758f92a0SBarry Smith . na - size of a and its 410864731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 4109758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 4110c7afd0dbSLois Curfman McInnes 4111308dcc3eSBarry Smith Notes: 41120298fd71SBarry Smith If 'a' and 'its' are NULL then space is allocated for the history. If 'na' PETSC_DECIDE or PETSC_DEFAULT then a 4113308dcc3eSBarry Smith default array of length 10000 is allocated. 4114308dcc3eSBarry Smith 4115c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 4116c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 4117c9005455SLois Curfman McInnes during the section of code that is being timed. 4118c9005455SLois Curfman McInnes 411936851e7fSLois Curfman McInnes Level: intermediate 412036851e7fSLois Curfman McInnes 4121c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 4122758f92a0SBarry Smith 412308405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 4124758f92a0SBarry Smith 4125c9005455SLois Curfman McInnes @*/ 41267087cfbeSBarry Smith PetscErrorCode SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscBool reset) 4127c9005455SLois Curfman McInnes { 4128308dcc3eSBarry Smith PetscErrorCode ierr; 4129308dcc3eSBarry Smith 41303a40ed3dSBarry Smith PetscFunctionBegin; 41310700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 41327a1ec6d4SBarry Smith if (a) PetscValidScalarPointer(a,2); 4133a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 41347a1ec6d4SBarry Smith if (!a) { 4135308dcc3eSBarry Smith if (na == PETSC_DECIDE || na == PETSC_DEFAULT) na = 1000; 41367fdeb8b9SBarry Smith ierr = PetscCalloc1(na,&a);CHKERRQ(ierr); 41377fdeb8b9SBarry Smith ierr = PetscCalloc1(na,&its);CHKERRQ(ierr); 4138f5af7f23SKarl Rupp 4139308dcc3eSBarry Smith snes->conv_malloc = PETSC_TRUE; 4140308dcc3eSBarry Smith } 4141c9005455SLois Curfman McInnes snes->conv_hist = a; 4142758f92a0SBarry Smith snes->conv_hist_its = its; 4143758f92a0SBarry Smith snes->conv_hist_max = na; 4144a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 4145758f92a0SBarry Smith snes->conv_hist_reset = reset; 4146758f92a0SBarry Smith PetscFunctionReturn(0); 4147758f92a0SBarry Smith } 4148758f92a0SBarry Smith 4149308dcc3eSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 4150c6db04a5SJed Brown #include <engine.h> /* MATLAB include file */ 4151c6db04a5SJed Brown #include <mex.h> /* MATLAB include file */ 415299e0435eSBarry Smith 41538cc058d9SJed Brown PETSC_EXTERN mxArray *SNESGetConvergenceHistoryMatlab(SNES snes) 4154308dcc3eSBarry Smith { 4155308dcc3eSBarry Smith mxArray *mat; 4156308dcc3eSBarry Smith PetscInt i; 4157308dcc3eSBarry Smith PetscReal *ar; 4158308dcc3eSBarry Smith 4159308dcc3eSBarry Smith PetscFunctionBegin; 4160308dcc3eSBarry Smith mat = mxCreateDoubleMatrix(snes->conv_hist_len,1,mxREAL); 4161308dcc3eSBarry Smith ar = (PetscReal*) mxGetData(mat); 4162f5af7f23SKarl Rupp for (i=0; i<snes->conv_hist_len; i++) ar[i] = snes->conv_hist[i]; 4163308dcc3eSBarry Smith PetscFunctionReturn(mat); 4164308dcc3eSBarry Smith } 4165308dcc3eSBarry Smith #endif 4166308dcc3eSBarry Smith 41670c4c9dddSBarry Smith /*@C 4168758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 4169758f92a0SBarry Smith 41703f9fe445SBarry Smith Not Collective 4171758f92a0SBarry Smith 4172758f92a0SBarry Smith Input Parameter: 4173758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 4174758f92a0SBarry Smith 4175758f92a0SBarry Smith Output Parameters: 4176758f92a0SBarry Smith . a - array to hold history 4177758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 4178758f92a0SBarry Smith negative if not converged) for each solve. 4179758f92a0SBarry Smith - na - size of a and its 4180758f92a0SBarry Smith 4181758f92a0SBarry Smith Notes: 4182758f92a0SBarry Smith The calling sequence for this routine in Fortran is 4183758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 4184758f92a0SBarry Smith 4185758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 4186758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 4187758f92a0SBarry Smith during the section of code that is being timed. 4188758f92a0SBarry Smith 4189758f92a0SBarry Smith Level: intermediate 4190758f92a0SBarry Smith 4191758f92a0SBarry Smith .keywords: SNES, get, convergence, history 4192758f92a0SBarry Smith 4193758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 4194758f92a0SBarry Smith 4195758f92a0SBarry Smith @*/ 41967087cfbeSBarry Smith PetscErrorCode SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 4197758f92a0SBarry Smith { 4198758f92a0SBarry Smith PetscFunctionBegin; 41990700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4200758f92a0SBarry Smith if (a) *a = snes->conv_hist; 4201758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 4202758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 42033a40ed3dSBarry Smith PetscFunctionReturn(0); 4204c9005455SLois Curfman McInnes } 4205c9005455SLois Curfman McInnes 4206ac226902SBarry Smith /*@C 420776b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 4208eec8f646SJed Brown at the beginning of every iteration of the nonlinear solve. Specifically 42097e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 421076b2cf59SMatthew Knepley 42113f9fe445SBarry Smith Logically Collective on SNES 421276b2cf59SMatthew Knepley 421376b2cf59SMatthew Knepley Input Parameters: 421476b2cf59SMatthew Knepley . snes - The nonlinear solver context 421576b2cf59SMatthew Knepley . func - The function 421676b2cf59SMatthew Knepley 421776b2cf59SMatthew Knepley Calling sequence of func: 4218b5d30489SBarry Smith . func (SNES snes, PetscInt step); 421976b2cf59SMatthew Knepley 422076b2cf59SMatthew Knepley . step - The current step of the iteration 422176b2cf59SMatthew Knepley 4222fe97e370SBarry Smith Level: advanced 4223fe97e370SBarry Smith 4224fe97e370SBarry Smith Note: This is NOT what one uses to update the ghost points before a function evaluation, that should be done at the beginning of your FormFunction() 4225fe97e370SBarry Smith This is not used by most users. 422676b2cf59SMatthew Knepley 422776b2cf59SMatthew Knepley .keywords: SNES, update 4228b5d30489SBarry Smith 42298d359177SBarry Smith .seealso SNESSetJacobian(), SNESSolve() 423076b2cf59SMatthew Knepley @*/ 42317087cfbeSBarry Smith PetscErrorCode SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 423276b2cf59SMatthew Knepley { 423376b2cf59SMatthew Knepley PetscFunctionBegin; 42340700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 4235e7788613SBarry Smith snes->ops->update = func; 423676b2cf59SMatthew Knepley PetscFunctionReturn(0); 423776b2cf59SMatthew Knepley } 423876b2cf59SMatthew Knepley 42399b94acceSBarry Smith /* 42409b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 42419b94acceSBarry Smith positive parameter delta. 42429b94acceSBarry Smith 42439b94acceSBarry Smith Input Parameters: 4244c7afd0dbSLois Curfman McInnes + snes - the SNES context 42459b94acceSBarry Smith . y - approximate solution of linear system 42469b94acceSBarry Smith . fnorm - 2-norm of current function 4247c7afd0dbSLois Curfman McInnes - delta - trust region size 42489b94acceSBarry Smith 42499b94acceSBarry Smith Output Parameters: 4250c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 42519b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 42529b94acceSBarry Smith region, and exceeds zero otherwise. 4253c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 42549b94acceSBarry Smith 42559b94acceSBarry Smith Note: 425604d7464bSBarry Smith For non-trust region methods such as SNESNEWTONLS, the parameter delta 42579b94acceSBarry Smith is set to be the maximum allowable step size. 42589b94acceSBarry Smith 42599b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 42609b94acceSBarry Smith */ 4261dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 42629b94acceSBarry Smith { 4263064f8208SBarry Smith PetscReal nrm; 4264ea709b57SSatish Balay PetscScalar cnorm; 4265dfbe8321SBarry Smith PetscErrorCode ierr; 42663a40ed3dSBarry Smith 42673a40ed3dSBarry Smith PetscFunctionBegin; 42680700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 42690700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 4270c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 4271184914b5SBarry Smith 4272064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 4273064f8208SBarry Smith if (nrm > *delta) { 4274064f8208SBarry Smith nrm = *delta/nrm; 4275064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 4276064f8208SBarry Smith cnorm = nrm; 42772dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 42789b94acceSBarry Smith *ynorm = *delta; 42799b94acceSBarry Smith } else { 42809b94acceSBarry Smith *gpnorm = 0.0; 4281064f8208SBarry Smith *ynorm = nrm; 42829b94acceSBarry Smith } 42833a40ed3dSBarry Smith PetscFunctionReturn(0); 42849b94acceSBarry Smith } 42859b94acceSBarry Smith 42862a359c20SBarry Smith /*@ 42872a359c20SBarry Smith SNESReasonView - Displays the reason a SNES solve converged or diverged to a viewer 42882a359c20SBarry Smith 42892a359c20SBarry Smith Collective on SNES 42902a359c20SBarry Smith 42912a359c20SBarry Smith Parameter: 42922a359c20SBarry Smith + snes - iterative context obtained from SNESCreate() 42932a359c20SBarry Smith - viewer - the viewer to display the reason 42942a359c20SBarry Smith 42952a359c20SBarry Smith 42962a359c20SBarry Smith Options Database Keys: 42972a359c20SBarry Smith . -snes_converged_reason - print reason for converged or diverged, also prints number of iterations 42982a359c20SBarry Smith 42992a359c20SBarry Smith Level: beginner 43002a359c20SBarry Smith 43012a359c20SBarry Smith .keywords: SNES, solve, linear system 43022a359c20SBarry Smith 43032a359c20SBarry Smith .seealso: SNESCreate(), SNESSetUp(), SNESDestroy(), SNESSetTolerances(), SNESConvergedDefault() 43042a359c20SBarry Smith 43052a359c20SBarry Smith @*/ 43062a359c20SBarry Smith PetscErrorCode SNESReasonView(SNES snes,PetscViewer viewer) 43072a359c20SBarry Smith { 430875cca76cSMatthew G. Knepley PetscViewerFormat format; 43092a359c20SBarry Smith PetscBool isAscii; 431075cca76cSMatthew G. Knepley PetscErrorCode ierr; 43112a359c20SBarry Smith 43122a359c20SBarry Smith PetscFunctionBegin; 43132a359c20SBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isAscii);CHKERRQ(ierr); 43142a359c20SBarry Smith if (isAscii) { 431575cca76cSMatthew G. Knepley ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr); 43162a359c20SBarry Smith ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 431775cca76cSMatthew G. Knepley if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 431875cca76cSMatthew G. Knepley DM dm; 431975cca76cSMatthew G. Knepley Vec u; 432075cca76cSMatthew G. Knepley PetscDS prob; 432175cca76cSMatthew G. Knepley PetscInt Nf, f; 432275cca76cSMatthew G. Knepley PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar[], void *); 432375cca76cSMatthew G. Knepley PetscReal error; 432475cca76cSMatthew G. Knepley 432575cca76cSMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 432675cca76cSMatthew G. Knepley ierr = SNESGetSolution(snes, &u);CHKERRQ(ierr); 432775cca76cSMatthew G. Knepley ierr = DMGetDS(dm, &prob);CHKERRQ(ierr); 432875cca76cSMatthew G. Knepley ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 432975cca76cSMatthew G. Knepley ierr = PetscMalloc1(Nf, &exactFuncs);CHKERRQ(ierr); 433075cca76cSMatthew G. Knepley for (f = 0; f < Nf; ++f) {ierr = PetscDSGetExactSolution(prob, f, &exactFuncs[f]);CHKERRQ(ierr);} 433175cca76cSMatthew G. Knepley ierr = DMComputeL2Diff(dm, 0.0, exactFuncs, NULL, u, &error);CHKERRQ(ierr); 433275cca76cSMatthew G. Knepley ierr = PetscFree(exactFuncs);CHKERRQ(ierr); 433375cca76cSMatthew G. Knepley if (error < 1.0e-11) {ierr = PetscViewerASCIIPrintf(viewer, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);} 433475cca76cSMatthew G. Knepley else {ierr = PetscViewerASCIIPrintf(viewer, "L_2 Error: %g\n", error);CHKERRQ(ierr);} 433575cca76cSMatthew G. Knepley } 43362a359c20SBarry Smith if (snes->reason > 0) { 43372a359c20SBarry Smith if (((PetscObject) snes)->prefix) { 43382a359c20SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Nonlinear %s solve converged due to %s iterations %D\n",((PetscObject) snes)->prefix,SNESConvergedReasons[snes->reason],snes->iter);CHKERRQ(ierr); 43392a359c20SBarry Smith } else { 43402a359c20SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Nonlinear solve converged due to %s iterations %D\n",SNESConvergedReasons[snes->reason],snes->iter);CHKERRQ(ierr); 43412a359c20SBarry Smith } 43422a359c20SBarry Smith } else { 43432a359c20SBarry Smith if (((PetscObject) snes)->prefix) { 43442a359c20SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Nonlinear %s solve did not converge due to %s iterations %D\n",((PetscObject) snes)->prefix,SNESConvergedReasons[snes->reason],snes->iter);CHKERRQ(ierr); 43452a359c20SBarry Smith } else { 43462a359c20SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Nonlinear solve did not converge due to %s iterations %D\n",SNESConvergedReasons[snes->reason],snes->iter);CHKERRQ(ierr); 43472a359c20SBarry Smith } 43482a359c20SBarry Smith } 43492a359c20SBarry Smith ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);CHKERRQ(ierr); 43502a359c20SBarry Smith } 43512a359c20SBarry Smith PetscFunctionReturn(0); 43522a359c20SBarry Smith } 43532a359c20SBarry Smith 43542a359c20SBarry Smith /*@C 43552a359c20SBarry Smith SNESReasonViewFromOptions - Processes command line options to determine if/how a SNESReason is to be viewed. 43562a359c20SBarry Smith 43572a359c20SBarry Smith Collective on SNES 43582a359c20SBarry Smith 43592a359c20SBarry Smith Input Parameters: 43602a359c20SBarry Smith . snes - the SNES object 43612a359c20SBarry Smith 43622a359c20SBarry Smith Level: intermediate 43632a359c20SBarry Smith 43642a359c20SBarry Smith @*/ 43652a359c20SBarry Smith PetscErrorCode SNESReasonViewFromOptions(SNES snes) 43662a359c20SBarry Smith { 43672a359c20SBarry Smith PetscErrorCode ierr; 43682a359c20SBarry Smith PetscViewer viewer; 43692a359c20SBarry Smith PetscBool flg; 43702a359c20SBarry Smith static PetscBool incall = PETSC_FALSE; 43712a359c20SBarry Smith PetscViewerFormat format; 43722a359c20SBarry Smith 43732a359c20SBarry Smith PetscFunctionBegin; 43742a359c20SBarry Smith if (incall) PetscFunctionReturn(0); 43752a359c20SBarry Smith incall = PETSC_TRUE; 437616413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_converged_reason",&viewer,&format,&flg);CHKERRQ(ierr); 43772a359c20SBarry Smith if (flg) { 43782a359c20SBarry Smith ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 43792a359c20SBarry Smith ierr = SNESReasonView(snes,viewer);CHKERRQ(ierr); 43802a359c20SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 43812a359c20SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 43822a359c20SBarry Smith } 43832a359c20SBarry Smith incall = PETSC_FALSE; 43842a359c20SBarry Smith PetscFunctionReturn(0); 43852a359c20SBarry Smith } 43862a359c20SBarry Smith 4387487a658cSBarry Smith /*@ 4388f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 4389f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 43909b94acceSBarry Smith 4391c7afd0dbSLois Curfman McInnes Collective on SNES 4392c7afd0dbSLois Curfman McInnes 4393b2002411SLois Curfman McInnes Input Parameters: 4394c7afd0dbSLois Curfman McInnes + snes - the SNES context 43950298fd71SBarry Smith . b - the constant part of the equation F(x) = b, or NULL to use zero. 439685385478SLisandro Dalcin - x - the solution vector. 43979b94acceSBarry Smith 4398b2002411SLois Curfman McInnes Notes: 43998ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 44008ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 44018ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 44028ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 44038ddd3da0SLois Curfman McInnes 440436851e7fSLois Curfman McInnes Level: beginner 440536851e7fSLois Curfman McInnes 44069b94acceSBarry Smith .keywords: SNES, nonlinear, solve 44079b94acceSBarry Smith 4408c0df2a02SJed Brown .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetGridSequence(), SNESGetSolution() 44099b94acceSBarry Smith @*/ 44107087cfbeSBarry Smith PetscErrorCode SNESSolve(SNES snes,Vec b,Vec x) 44119b94acceSBarry Smith { 4412dfbe8321SBarry Smith PetscErrorCode ierr; 4413ace3abfcSBarry Smith PetscBool flg; 4414efd51863SBarry Smith PetscInt grid; 44150298fd71SBarry Smith Vec xcreated = NULL; 4416caa4e7f2SJed Brown DM dm; 4417052efed2SBarry Smith 44183a40ed3dSBarry Smith PetscFunctionBegin; 44190700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4420a69afd8bSBarry Smith if (x) PetscValidHeaderSpecific(x,VEC_CLASSID,3); 4421a69afd8bSBarry Smith if (x) PetscCheckSameComm(snes,1,x,3); 44220700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 442385385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 442485385478SLisandro Dalcin 442534b4d3a8SMatthew G. Knepley /* High level operations using the nonlinear solver */ 442606fc46c8SMatthew G. Knepley { 442706fc46c8SMatthew G. Knepley PetscViewer viewer; 442806fc46c8SMatthew G. Knepley PetscViewerFormat format; 44297c88af5aSMatthew G. Knepley PetscInt num; 443006fc46c8SMatthew G. Knepley PetscBool flg; 443106fc46c8SMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 443206fc46c8SMatthew G. Knepley 443306fc46c8SMatthew G. Knepley if (!incall) { 443434b4d3a8SMatthew G. Knepley /* Estimate the convergence rate of the discretization */ 443516413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) snes),((PetscObject)snes)->options, ((PetscObject) snes)->prefix, "-snes_convergence_estimate", &viewer, &format, &flg);CHKERRQ(ierr); 443606fc46c8SMatthew G. Knepley if (flg) { 443706fc46c8SMatthew G. Knepley PetscConvEst conv; 443846079b62SMatthew G. Knepley DM dm; 443946079b62SMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 444046079b62SMatthew G. Knepley PetscInt Nf; 444106fc46c8SMatthew G. Knepley 444206fc46c8SMatthew G. Knepley incall = PETSC_TRUE; 444346079b62SMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 444446079b62SMatthew G. Knepley ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr); 444546079b62SMatthew G. Knepley ierr = PetscMalloc1(Nf, &alpha);CHKERRQ(ierr); 444606fc46c8SMatthew G. Knepley ierr = PetscConvEstCreate(PetscObjectComm((PetscObject) snes), &conv);CHKERRQ(ierr); 444706fc46c8SMatthew G. Knepley ierr = PetscConvEstSetSolver(conv, snes);CHKERRQ(ierr); 444806fc46c8SMatthew G. Knepley ierr = PetscConvEstSetFromOptions(conv);CHKERRQ(ierr); 44490955ed61SMatthew G. Knepley ierr = PetscConvEstSetUp(conv);CHKERRQ(ierr); 445046079b62SMatthew G. Knepley ierr = PetscConvEstGetConvRate(conv, alpha);CHKERRQ(ierr); 445106fc46c8SMatthew G. Knepley ierr = PetscViewerPushFormat(viewer, format);CHKERRQ(ierr); 445206fc46c8SMatthew G. Knepley ierr = PetscConvEstRateView(conv, alpha, viewer);CHKERRQ(ierr); 445306fc46c8SMatthew G. Knepley ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 445406fc46c8SMatthew G. Knepley ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 445506fc46c8SMatthew G. Knepley ierr = PetscConvEstDestroy(&conv);CHKERRQ(ierr); 445646079b62SMatthew G. Knepley ierr = PetscFree(alpha);CHKERRQ(ierr); 445706fc46c8SMatthew G. Knepley incall = PETSC_FALSE; 445806fc46c8SMatthew G. Knepley } 445934b4d3a8SMatthew G. Knepley /* Adaptively refine the initial grid */ 4460b2588ea6SMatthew G. Knepley num = 1; 4461b2588ea6SMatthew G. Knepley ierr = PetscOptionsGetInt(NULL, ((PetscObject) snes)->prefix, "-snes_adapt_initial", &num, &flg);CHKERRQ(ierr); 446234b4d3a8SMatthew G. Knepley if (flg) { 446334b4d3a8SMatthew G. Knepley DMAdaptor adaptor; 446434b4d3a8SMatthew G. Knepley 446534b4d3a8SMatthew G. Knepley incall = PETSC_TRUE; 446634b4d3a8SMatthew G. Knepley ierr = DMAdaptorCreate(PETSC_COMM_WORLD, &adaptor);CHKERRQ(ierr); 446734b4d3a8SMatthew G. Knepley ierr = DMAdaptorSetSolver(adaptor, snes);CHKERRQ(ierr); 4468b2588ea6SMatthew G. Knepley ierr = DMAdaptorSetSequenceLength(adaptor, num);CHKERRQ(ierr); 446934b4d3a8SMatthew G. Knepley ierr = DMAdaptorSetFromOptions(adaptor);CHKERRQ(ierr); 447034b4d3a8SMatthew G. Knepley ierr = DMAdaptorSetUp(adaptor);CHKERRQ(ierr); 447134b4d3a8SMatthew G. Knepley ierr = DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_INITIAL, &dm, &x);CHKERRQ(ierr); 447234b4d3a8SMatthew G. Knepley ierr = DMAdaptorDestroy(&adaptor);CHKERRQ(ierr); 447334b4d3a8SMatthew G. Knepley incall = PETSC_FALSE; 447434b4d3a8SMatthew G. Knepley } 44757c88af5aSMatthew G. Knepley /* Use grid sequencing to adapt */ 44767c88af5aSMatthew G. Knepley num = 0; 44777c88af5aSMatthew G. Knepley ierr = PetscOptionsGetInt(NULL, ((PetscObject) snes)->prefix, "-snes_adapt_sequence", &num, NULL);CHKERRQ(ierr); 44787c88af5aSMatthew G. Knepley if (num) { 44797c88af5aSMatthew G. Knepley DMAdaptor adaptor; 44807c88af5aSMatthew G. Knepley 44817c88af5aSMatthew G. Knepley incall = PETSC_TRUE; 44827c88af5aSMatthew G. Knepley ierr = DMAdaptorCreate(PETSC_COMM_WORLD, &adaptor);CHKERRQ(ierr); 44837c88af5aSMatthew G. Knepley ierr = DMAdaptorSetSolver(adaptor, snes);CHKERRQ(ierr); 44847c88af5aSMatthew G. Knepley ierr = DMAdaptorSetSequenceLength(adaptor, num);CHKERRQ(ierr); 44857c88af5aSMatthew G. Knepley ierr = DMAdaptorSetFromOptions(adaptor);CHKERRQ(ierr); 44867c88af5aSMatthew G. Knepley ierr = DMAdaptorSetUp(adaptor);CHKERRQ(ierr); 44877c88af5aSMatthew G. Knepley ierr = DMAdaptorAdapt(adaptor, x, DM_ADAPTATION_SEQUENTIAL, &dm, &x);CHKERRQ(ierr); 44887c88af5aSMatthew G. Knepley ierr = DMAdaptorDestroy(&adaptor);CHKERRQ(ierr); 44897c88af5aSMatthew G. Knepley incall = PETSC_FALSE; 44907c88af5aSMatthew G. Knepley } 449106fc46c8SMatthew G. Knepley } 449206fc46c8SMatthew G. Knepley } 4493caa4e7f2SJed Brown if (!x) { 4494caa4e7f2SJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 4495caa4e7f2SJed Brown ierr = DMCreateGlobalVector(dm,&xcreated);CHKERRQ(ierr); 4496a69afd8bSBarry Smith x = xcreated; 4497a69afd8bSBarry Smith } 4498ce1779c8SBarry Smith ierr = SNESViewFromOptions(snes,NULL,"-snes_view_pre");CHKERRQ(ierr); 4499f05ece33SBarry Smith 4500ce94432eSBarry Smith for (grid=0; grid<snes->gridsequence; grid++) {ierr = PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)));CHKERRQ(ierr);} 4501efd51863SBarry Smith for (grid=0; grid<snes->gridsequence+1; grid++) { 4502efd51863SBarry Smith 450385385478SLisandro Dalcin /* set solution vector */ 4504efd51863SBarry Smith if (!grid) {ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);} 45056bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 450685385478SLisandro Dalcin snes->vec_sol = x; 4507caa4e7f2SJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 4508caa4e7f2SJed Brown 4509caa4e7f2SJed Brown /* set affine vector if provided */ 451085385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 45116bf464f9SBarry Smith ierr = VecDestroy(&snes->vec_rhs);CHKERRQ(ierr); 451285385478SLisandro Dalcin snes->vec_rhs = b; 451385385478SLisandro Dalcin 4514154060b5SMatthew G. Knepley if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 4515154060b5SMatthew G. Knepley if (snes->vec_rhs == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be right hand side vector"); 4516154060b5SMatthew G. Knepley if (!snes->vec_sol_update /* && snes->vec_sol */) { 4517154060b5SMatthew G. Knepley ierr = VecDuplicate(snes->vec_sol,&snes->vec_sol_update);CHKERRQ(ierr); 4518154060b5SMatthew G. Knepley ierr = PetscLogObjectParent((PetscObject)snes,(PetscObject)snes->vec_sol_update);CHKERRQ(ierr); 4519154060b5SMatthew G. Knepley } 4520154060b5SMatthew G. Knepley ierr = DMShellSetGlobalVector(dm,snes->vec_sol);CHKERRQ(ierr); 452170e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 45223f149594SLisandro Dalcin 45237eee914bSBarry Smith if (!grid) { 45247eee914bSBarry Smith if (snes->ops->computeinitialguess) { 4525d25893d9SBarry Smith ierr = (*snes->ops->computeinitialguess)(snes,snes->vec_sol,snes->initialguessP);CHKERRQ(ierr); 4526d25893d9SBarry Smith } 4527dd568438SSatish Balay } 4528d25893d9SBarry Smith 4529abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 4530971e163fSPeter Brune if (snes->counters_reset) {snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;} 4531d5e45103SBarry Smith 45323f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 45334936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 453485385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 453517186662SBarry Smith if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 4536422a814eSBarry Smith snes->domainerror = PETSC_FALSE; /* clear the flag if it has been set */ 45373f149594SLisandro Dalcin 453837ec4e1aSPeter Brune if (snes->lagjac_persist) snes->jac_iter += snes->iter; 453937ec4e1aSPeter Brune if (snes->lagpre_persist) snes->pre_iter += snes->iter; 454037ec4e1aSPeter Brune 454116413a6aSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)snes),((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-snes_test_local_min",NULL,NULL,&flg);CHKERRQ(ierr); 4542da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 45432a359c20SBarry Smith ierr = SNESReasonViewFromOptions(snes);CHKERRQ(ierr); 45445968eb51SBarry Smith 4545ce94432eSBarry Smith if (snes->errorifnotconverged && snes->reason < 0) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_NOT_CONVERGED,"SNESSolve has not converged"); 45469c8e83a9SBarry Smith if (snes->reason < 0) break; 4547efd51863SBarry Smith if (grid < snes->gridsequence) { 4548efd51863SBarry Smith DM fine; 4549efd51863SBarry Smith Vec xnew; 4550efd51863SBarry Smith Mat interp; 4551efd51863SBarry Smith 4552ce94432eSBarry Smith ierr = DMRefine(snes->dm,PetscObjectComm((PetscObject)snes),&fine);CHKERRQ(ierr); 4553ce94432eSBarry Smith if (!fine) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_INCOMP,"DMRefine() did not perform any refinement, cannot continue grid sequencing"); 45540298fd71SBarry Smith ierr = DMCreateInterpolation(snes->dm,fine,&interp,NULL);CHKERRQ(ierr); 4555efd51863SBarry Smith ierr = DMCreateGlobalVector(fine,&xnew);CHKERRQ(ierr); 4556efd51863SBarry Smith ierr = MatInterpolate(interp,x,xnew);CHKERRQ(ierr); 4557c833c3b5SJed Brown ierr = DMInterpolate(snes->dm,interp,fine);CHKERRQ(ierr); 4558efd51863SBarry Smith ierr = MatDestroy(&interp);CHKERRQ(ierr); 4559efd51863SBarry Smith x = xnew; 4560efd51863SBarry Smith 4561efd51863SBarry Smith ierr = SNESReset(snes);CHKERRQ(ierr); 4562efd51863SBarry Smith ierr = SNESSetDM(snes,fine);CHKERRQ(ierr); 4563352f405dSMatthew G. Knepley ierr = SNESResetFromOptions(snes);CHKERRQ(ierr); 4564efd51863SBarry Smith ierr = DMDestroy(&fine);CHKERRQ(ierr); 4565ce94432eSBarry Smith ierr = PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)));CHKERRQ(ierr); 4566efd51863SBarry Smith } 4567efd51863SBarry Smith } 4568ce1779c8SBarry Smith ierr = SNESViewFromOptions(snes,NULL,"-snes_view");CHKERRQ(ierr); 4569685405a1SBarry Smith ierr = VecViewFromOptions(snes->vec_sol,(PetscObject)snes,"-snes_view_solution");CHKERRQ(ierr); 45703f7e2da0SPeter Brune 4571a69afd8bSBarry Smith ierr = VecDestroy(&xcreated);CHKERRQ(ierr); 4572e04113cfSBarry Smith ierr = PetscObjectSAWsBlock((PetscObject)snes);CHKERRQ(ierr); 45733a40ed3dSBarry Smith PetscFunctionReturn(0); 45749b94acceSBarry Smith } 45759b94acceSBarry Smith 45769b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 45779b94acceSBarry Smith 457882bf6240SBarry Smith /*@C 45794b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 45809b94acceSBarry Smith 4581fee21e36SBarry Smith Collective on SNES 4582fee21e36SBarry Smith 4583c7afd0dbSLois Curfman McInnes Input Parameters: 4584c7afd0dbSLois Curfman McInnes + snes - the SNES context 4585454a90a3SBarry Smith - type - a known method 4586c7afd0dbSLois Curfman McInnes 4587c7afd0dbSLois Curfman McInnes Options Database Key: 4588454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 458904d7464bSBarry Smith of available methods (for instance, newtonls or newtontr) 4590ae12b187SLois Curfman McInnes 45919b94acceSBarry Smith Notes: 4592e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 459304d7464bSBarry Smith + SNESNEWTONLS - Newton's method with line search 4594c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 459504d7464bSBarry Smith . SNESNEWTONTR - Newton's method with trust region 4596c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 45979b94acceSBarry Smith 4598ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 4599ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 4600ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 4601ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 4602ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 4603ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 4604ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 4605ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 4606ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 4607b0a32e0cSBarry Smith appropriate method. 460836851e7fSLois Curfman McInnes 460995452b02SPatrick Sanan Developer Notes: 461095452b02SPatrick Sanan SNESRegister() adds a constructor for a new SNESType to SNESList, SNESSetType() locates 46118f6c3df8SBarry Smith the constructor in that list and calls it to create the spexific object. 46128f6c3df8SBarry Smith 461336851e7fSLois Curfman McInnes Level: intermediate 4614a703fe33SLois Curfman McInnes 4615454a90a3SBarry Smith .keywords: SNES, set, type 4616435da068SBarry Smith 46178f6c3df8SBarry Smith .seealso: SNESType, SNESCreate(), SNESDestroy(), SNESGetType(), SNESSetFromOptions() 4618435da068SBarry Smith 46199b94acceSBarry Smith @*/ 462019fd82e9SBarry Smith PetscErrorCode SNESSetType(SNES snes,SNESType type) 46219b94acceSBarry Smith { 4622dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 4623ace3abfcSBarry Smith PetscBool match; 46243a40ed3dSBarry Smith 46253a40ed3dSBarry Smith PetscFunctionBegin; 46260700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 46274482741eSBarry Smith PetscValidCharPointer(type,2); 462882bf6240SBarry Smith 4629251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 46300f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 463192ff6ae8SBarry Smith 46321c9cd337SJed Brown ierr = PetscFunctionListFind(SNESList,type,&r);CHKERRQ(ierr); 4633e32f2f54SBarry Smith if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 463475396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 4635b5c23020SJed Brown if (snes->ops->destroy) { 4636b5c23020SJed Brown ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); 46370298fd71SBarry Smith snes->ops->destroy = NULL; 4638b5c23020SJed Brown } 463975396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 464075396ef9SLisandro Dalcin snes->ops->setup = 0; 464175396ef9SLisandro Dalcin snes->ops->solve = 0; 464275396ef9SLisandro Dalcin snes->ops->view = 0; 464375396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 464475396ef9SLisandro Dalcin snes->ops->destroy = 0; 464575396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 464675396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 4647f5af7f23SKarl Rupp 4648454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 464903bfa161SLisandro Dalcin ierr = (*r)(snes);CHKERRQ(ierr); 46503a40ed3dSBarry Smith PetscFunctionReturn(0); 46519b94acceSBarry Smith } 46529b94acceSBarry Smith 46539b94acceSBarry Smith /*@C 46549a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 46559b94acceSBarry Smith 4656c7afd0dbSLois Curfman McInnes Not Collective 4657c7afd0dbSLois Curfman McInnes 46589b94acceSBarry Smith Input Parameter: 46594b0e389bSBarry Smith . snes - nonlinear solver context 46609b94acceSBarry Smith 46619b94acceSBarry Smith Output Parameter: 46623a7fca6bSBarry Smith . type - SNES method (a character string) 46639b94acceSBarry Smith 466436851e7fSLois Curfman McInnes Level: intermediate 466536851e7fSLois Curfman McInnes 4666454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 46679b94acceSBarry Smith @*/ 466819fd82e9SBarry Smith PetscErrorCode SNESGetType(SNES snes,SNESType *type) 46699b94acceSBarry Smith { 46703a40ed3dSBarry Smith PetscFunctionBegin; 46710700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 46724482741eSBarry Smith PetscValidPointer(type,2); 46737adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 46743a40ed3dSBarry Smith PetscFunctionReturn(0); 46759b94acceSBarry Smith } 46769b94acceSBarry Smith 46773cd8a7caSMatthew G. Knepley /*@ 46783cd8a7caSMatthew G. Knepley SNESSetSolution - Sets the solution vector for use by the SNES routines. 46793cd8a7caSMatthew G. Knepley 46803cd8a7caSMatthew G. Knepley Logically Collective on SNES and Vec 46813cd8a7caSMatthew G. Knepley 46823cd8a7caSMatthew G. Knepley Input Parameters: 46833cd8a7caSMatthew G. Knepley + snes - the SNES context obtained from SNESCreate() 46843cd8a7caSMatthew G. Knepley - u - the solution vector 46853cd8a7caSMatthew G. Knepley 46863cd8a7caSMatthew G. Knepley Level: beginner 46873cd8a7caSMatthew G. Knepley 46883cd8a7caSMatthew G. Knepley .keywords: SNES, set, solution 46893cd8a7caSMatthew G. Knepley @*/ 46903cd8a7caSMatthew G. Knepley PetscErrorCode SNESSetSolution(SNES snes, Vec u) 46913cd8a7caSMatthew G. Knepley { 46923cd8a7caSMatthew G. Knepley DM dm; 46933cd8a7caSMatthew G. Knepley PetscErrorCode ierr; 46943cd8a7caSMatthew G. Knepley 46953cd8a7caSMatthew G. Knepley PetscFunctionBegin; 46963cd8a7caSMatthew G. Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 46973cd8a7caSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 46983cd8a7caSMatthew G. Knepley ierr = PetscObjectReference((PetscObject) u);CHKERRQ(ierr); 46993cd8a7caSMatthew G. Knepley ierr = VecDestroy(&snes->vec_sol);CHKERRQ(ierr); 47003cd8a7caSMatthew G. Knepley 47013cd8a7caSMatthew G. Knepley snes->vec_sol = u; 47023cd8a7caSMatthew G. Knepley 47033cd8a7caSMatthew G. Knepley ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr); 47043cd8a7caSMatthew G. Knepley ierr = DMShellSetGlobalVector(dm, u);CHKERRQ(ierr); 47053cd8a7caSMatthew G. Knepley PetscFunctionReturn(0); 47063cd8a7caSMatthew G. Knepley } 47073cd8a7caSMatthew G. Knepley 470852baeb72SSatish Balay /*@ 47099b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 4710c0df2a02SJed Brown stored. This is the fine grid solution when using SNESSetGridSequence(). 47119b94acceSBarry Smith 4712c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 4713c7afd0dbSLois Curfman McInnes 47149b94acceSBarry Smith Input Parameter: 47159b94acceSBarry Smith . snes - the SNES context 47169b94acceSBarry Smith 47179b94acceSBarry Smith Output Parameter: 47189b94acceSBarry Smith . x - the solution 47199b94acceSBarry Smith 472070e92668SMatthew Knepley Level: intermediate 472136851e7fSLois Curfman McInnes 47229b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 47239b94acceSBarry Smith 472485385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 47259b94acceSBarry Smith @*/ 47267087cfbeSBarry Smith PetscErrorCode SNESGetSolution(SNES snes,Vec *x) 47279b94acceSBarry Smith { 47283a40ed3dSBarry Smith PetscFunctionBegin; 47290700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 47304482741eSBarry Smith PetscValidPointer(x,2); 473185385478SLisandro Dalcin *x = snes->vec_sol; 473270e92668SMatthew Knepley PetscFunctionReturn(0); 473370e92668SMatthew Knepley } 473470e92668SMatthew Knepley 473552baeb72SSatish Balay /*@ 47369b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 47379b94acceSBarry Smith stored. 47389b94acceSBarry Smith 4739c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 4740c7afd0dbSLois Curfman McInnes 47419b94acceSBarry Smith Input Parameter: 47429b94acceSBarry Smith . snes - the SNES context 47439b94acceSBarry Smith 47449b94acceSBarry Smith Output Parameter: 47459b94acceSBarry Smith . x - the solution update 47469b94acceSBarry Smith 474736851e7fSLois Curfman McInnes Level: advanced 474836851e7fSLois Curfman McInnes 47499b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 47509b94acceSBarry Smith 475185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 47529b94acceSBarry Smith @*/ 47537087cfbeSBarry Smith PetscErrorCode SNESGetSolutionUpdate(SNES snes,Vec *x) 47549b94acceSBarry Smith { 47553a40ed3dSBarry Smith PetscFunctionBegin; 47560700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 47574482741eSBarry Smith PetscValidPointer(x,2); 475885385478SLisandro Dalcin *x = snes->vec_sol_update; 47593a40ed3dSBarry Smith PetscFunctionReturn(0); 47609b94acceSBarry Smith } 47619b94acceSBarry Smith 47629b94acceSBarry Smith /*@C 47633638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 47649b94acceSBarry Smith 4765a63bb30eSJed Brown Not Collective, but Vec is parallel if SNES is parallel. Collective if Vec is requested, but has not been created yet. 4766c7afd0dbSLois Curfman McInnes 47679b94acceSBarry Smith Input Parameter: 47689b94acceSBarry Smith . snes - the SNES context 47699b94acceSBarry Smith 47709b94acceSBarry Smith Output Parameter: 47710298fd71SBarry Smith + r - the vector that is used to store residuals (or NULL if you don't want it) 4772f8b49ee9SBarry Smith . f - the function (or NULL if you don't want it); see SNESFunction for calling sequence details 47730298fd71SBarry Smith - ctx - the function context (or NULL if you don't want it) 47749b94acceSBarry Smith 477536851e7fSLois Curfman McInnes Level: advanced 477636851e7fSLois Curfman McInnes 477704edfde5SBarry Smith Notes: The vector r DOES NOT, in general contain the current value of the SNES nonlinear function 477804edfde5SBarry Smith 4779a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 47809b94acceSBarry Smith 4781bf388a1fSBarry Smith .seealso: SNESSetFunction(), SNESGetSolution(), SNESFunction 47829b94acceSBarry Smith @*/ 4783f8b49ee9SBarry Smith PetscErrorCode SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**f)(SNES,Vec,Vec,void*),void **ctx) 47849b94acceSBarry Smith { 4785a63bb30eSJed Brown PetscErrorCode ierr; 47866cab3a1bSJed Brown DM dm; 4787a63bb30eSJed Brown 47883a40ed3dSBarry Smith PetscFunctionBegin; 47890700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4790a63bb30eSJed Brown if (r) { 4791a63bb30eSJed Brown if (!snes->vec_func) { 4792a63bb30eSJed Brown if (snes->vec_rhs) { 4793a63bb30eSJed Brown ierr = VecDuplicate(snes->vec_rhs,&snes->vec_func);CHKERRQ(ierr); 4794a63bb30eSJed Brown } else if (snes->vec_sol) { 4795a63bb30eSJed Brown ierr = VecDuplicate(snes->vec_sol,&snes->vec_func);CHKERRQ(ierr); 4796a63bb30eSJed Brown } else if (snes->dm) { 4797a63bb30eSJed Brown ierr = DMCreateGlobalVector(snes->dm,&snes->vec_func);CHKERRQ(ierr); 4798a63bb30eSJed Brown } 4799a63bb30eSJed Brown } 4800a63bb30eSJed Brown *r = snes->vec_func; 4801a63bb30eSJed Brown } 48026cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 4803f8b49ee9SBarry Smith ierr = DMSNESGetFunction(dm,f,ctx);CHKERRQ(ierr); 48043a40ed3dSBarry Smith PetscFunctionReturn(0); 48059b94acceSBarry Smith } 48069b94acceSBarry Smith 4807c79ef259SPeter Brune /*@C 4808be95d8f1SBarry Smith SNESGetNGS - Returns the NGS function and context. 4809c79ef259SPeter Brune 4810c79ef259SPeter Brune Input Parameter: 4811c79ef259SPeter Brune . snes - the SNES context 4812c79ef259SPeter Brune 4813c79ef259SPeter Brune Output Parameter: 4814be95d8f1SBarry Smith + f - the function (or NULL) see SNESNGSFunction for details 48150298fd71SBarry Smith - ctx - the function context (or NULL) 4816c79ef259SPeter Brune 4817c79ef259SPeter Brune Level: advanced 4818c79ef259SPeter Brune 4819c79ef259SPeter Brune .keywords: SNES, nonlinear, get, function 4820c79ef259SPeter Brune 4821be95d8f1SBarry Smith .seealso: SNESSetNGS(), SNESGetFunction() 4822c79ef259SPeter Brune @*/ 4823c79ef259SPeter Brune 4824be95d8f1SBarry Smith PetscErrorCode SNESGetNGS (SNES snes, PetscErrorCode (**f)(SNES, Vec, Vec, void*), void ** ctx) 4825646217ecSPeter Brune { 48266cab3a1bSJed Brown PetscErrorCode ierr; 48276cab3a1bSJed Brown DM dm; 48286cab3a1bSJed Brown 4829646217ecSPeter Brune PetscFunctionBegin; 4830646217ecSPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 48316cab3a1bSJed Brown ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 4832be95d8f1SBarry Smith ierr = DMSNESGetNGS(dm,f,ctx);CHKERRQ(ierr); 4833646217ecSPeter Brune PetscFunctionReturn(0); 4834646217ecSPeter Brune } 4835646217ecSPeter Brune 48363c7409f5SSatish Balay /*@C 48373c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 4838d850072dSLois Curfman McInnes SNES options in the database. 48393c7409f5SSatish Balay 48403f9fe445SBarry Smith Logically Collective on SNES 4841fee21e36SBarry Smith 4842c7afd0dbSLois Curfman McInnes Input Parameter: 4843c7afd0dbSLois Curfman McInnes + snes - the SNES context 4844c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 4845c7afd0dbSLois Curfman McInnes 4846d850072dSLois Curfman McInnes Notes: 4847a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 4848c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 4849d850072dSLois Curfman McInnes 485036851e7fSLois Curfman McInnes Level: advanced 485136851e7fSLois Curfman McInnes 48523c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 4853a86d99e1SLois Curfman McInnes 4854a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 48553c7409f5SSatish Balay @*/ 48567087cfbeSBarry Smith PetscErrorCode SNESSetOptionsPrefix(SNES snes,const char prefix[]) 48573c7409f5SSatish Balay { 4858dfbe8321SBarry Smith PetscErrorCode ierr; 48593c7409f5SSatish Balay 48603a40ed3dSBarry Smith PetscFunctionBegin; 48610700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4862639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 48631cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 486435f5d045SPeter Brune if (snes->linesearch) { 48657601faf0SJed Brown ierr = SNESGetLineSearch(snes,&snes->linesearch);CHKERRQ(ierr); 486608b6c495SPeter Brune ierr = PetscObjectSetOptionsPrefix((PetscObject)snes->linesearch,prefix);CHKERRQ(ierr); 486735f5d045SPeter Brune } 486835f5d045SPeter Brune ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 48693a40ed3dSBarry Smith PetscFunctionReturn(0); 48703c7409f5SSatish Balay } 48713c7409f5SSatish Balay 48723c7409f5SSatish Balay /*@C 4873f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 4874d850072dSLois Curfman McInnes SNES options in the database. 48753c7409f5SSatish Balay 48763f9fe445SBarry Smith Logically Collective on SNES 4877fee21e36SBarry Smith 4878c7afd0dbSLois Curfman McInnes Input Parameters: 4879c7afd0dbSLois Curfman McInnes + snes - the SNES context 4880c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 4881c7afd0dbSLois Curfman McInnes 4882d850072dSLois Curfman McInnes Notes: 4883a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 4884c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 4885d850072dSLois Curfman McInnes 488636851e7fSLois Curfman McInnes Level: advanced 488736851e7fSLois Curfman McInnes 48883c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 4889a86d99e1SLois Curfman McInnes 4890a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 48913c7409f5SSatish Balay @*/ 48927087cfbeSBarry Smith PetscErrorCode SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 48933c7409f5SSatish Balay { 4894dfbe8321SBarry Smith PetscErrorCode ierr; 48953c7409f5SSatish Balay 48963a40ed3dSBarry Smith PetscFunctionBegin; 48970700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4898639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 48991cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 490035f5d045SPeter Brune if (snes->linesearch) { 49017601faf0SJed Brown ierr = SNESGetLineSearch(snes,&snes->linesearch);CHKERRQ(ierr); 490208b6c495SPeter Brune ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes->linesearch,prefix);CHKERRQ(ierr); 490335f5d045SPeter Brune } 490435f5d045SPeter Brune ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 49053a40ed3dSBarry Smith PetscFunctionReturn(0); 49063c7409f5SSatish Balay } 49073c7409f5SSatish Balay 49089ab63eb5SSatish Balay /*@C 49093c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 49103c7409f5SSatish Balay SNES options in the database. 49113c7409f5SSatish Balay 4912c7afd0dbSLois Curfman McInnes Not Collective 4913c7afd0dbSLois Curfman McInnes 49143c7409f5SSatish Balay Input Parameter: 49153c7409f5SSatish Balay . snes - the SNES context 49163c7409f5SSatish Balay 49173c7409f5SSatish Balay Output Parameter: 49183c7409f5SSatish Balay . prefix - pointer to the prefix string used 49193c7409f5SSatish Balay 492095452b02SPatrick Sanan Notes: 492195452b02SPatrick Sanan On the fortran side, the user should pass in a string 'prefix' of 49229ab63eb5SSatish Balay sufficient length to hold the prefix. 49239ab63eb5SSatish Balay 492436851e7fSLois Curfman McInnes Level: advanced 492536851e7fSLois Curfman McInnes 49263c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 4927a86d99e1SLois Curfman McInnes 4928a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 49293c7409f5SSatish Balay @*/ 49307087cfbeSBarry Smith PetscErrorCode SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 49313c7409f5SSatish Balay { 4932dfbe8321SBarry Smith PetscErrorCode ierr; 49333c7409f5SSatish Balay 49343a40ed3dSBarry Smith PetscFunctionBegin; 49350700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4936639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 49373a40ed3dSBarry Smith PetscFunctionReturn(0); 49383c7409f5SSatish Balay } 49393c7409f5SSatish Balay 4940b2002411SLois Curfman McInnes 49413cea93caSBarry Smith /*@C 49421c84c290SBarry Smith SNESRegister - Adds a method to the nonlinear solver package. 49431c84c290SBarry Smith 49441c84c290SBarry Smith Not collective 49451c84c290SBarry Smith 49461c84c290SBarry Smith Input Parameters: 49471c84c290SBarry Smith + name_solver - name of a new user-defined solver 49481c84c290SBarry Smith - routine_create - routine to create method context 49491c84c290SBarry Smith 49501c84c290SBarry Smith Notes: 49511c84c290SBarry Smith SNESRegister() may be called multiple times to add several user-defined solvers. 49521c84c290SBarry Smith 49531c84c290SBarry Smith Sample usage: 49541c84c290SBarry Smith .vb 4955bdf89e91SBarry Smith SNESRegister("my_solver",MySolverCreate); 49561c84c290SBarry Smith .ve 49571c84c290SBarry Smith 49581c84c290SBarry Smith Then, your solver can be chosen with the procedural interface via 49591c84c290SBarry Smith $ SNESSetType(snes,"my_solver") 49601c84c290SBarry Smith or at runtime via the option 49611c84c290SBarry Smith $ -snes_type my_solver 49621c84c290SBarry Smith 49631c84c290SBarry Smith Level: advanced 49641c84c290SBarry Smith 49651c84c290SBarry Smith Note: If your function is not being put into a shared library then use SNESRegister() instead 49661c84c290SBarry Smith 49671c84c290SBarry Smith .keywords: SNES, nonlinear, register 49681c84c290SBarry Smith 49691c84c290SBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy() 49703cea93caSBarry Smith 49717f6c08e0SMatthew Knepley Level: advanced 49723cea93caSBarry Smith @*/ 4973bdf89e91SBarry Smith PetscErrorCode SNESRegister(const char sname[],PetscErrorCode (*function)(SNES)) 4974b2002411SLois Curfman McInnes { 4975dfbe8321SBarry Smith PetscErrorCode ierr; 4976b2002411SLois Curfman McInnes 4977b2002411SLois Curfman McInnes PetscFunctionBegin; 49781d36bdfdSBarry Smith ierr = SNESInitializePackage();CHKERRQ(ierr); 4979a240a19fSJed Brown ierr = PetscFunctionListAdd(&SNESList,sname,function);CHKERRQ(ierr); 4980b2002411SLois Curfman McInnes PetscFunctionReturn(0); 4981b2002411SLois Curfman McInnes } 4982da9b6338SBarry Smith 49837087cfbeSBarry Smith PetscErrorCode SNESTestLocalMin(SNES snes) 4984da9b6338SBarry Smith { 4985dfbe8321SBarry Smith PetscErrorCode ierr; 498677431f27SBarry Smith PetscInt N,i,j; 4987da9b6338SBarry Smith Vec u,uh,fh; 4988da9b6338SBarry Smith PetscScalar value; 4989da9b6338SBarry Smith PetscReal norm; 4990da9b6338SBarry Smith 4991da9b6338SBarry Smith PetscFunctionBegin; 4992da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 4993da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 4994da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 4995da9b6338SBarry Smith 4996da9b6338SBarry Smith /* currently only works for sequential */ 499722d28d08SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");CHKERRQ(ierr); 4998da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 4999da9b6338SBarry Smith for (i=0; i<N; i++) { 5000da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 500177431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 5002da9b6338SBarry Smith for (j=-10; j<11; j++) { 50038b49ba18SBarry Smith value = PetscSign(j)*PetscExpReal(PetscAbs(j)-10.0); 5004da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 50053ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 5006da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 500777431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 5008da9b6338SBarry Smith value = -value; 5009da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 5010da9b6338SBarry Smith } 5011da9b6338SBarry Smith } 50126bf464f9SBarry Smith ierr = VecDestroy(&uh);CHKERRQ(ierr); 50136bf464f9SBarry Smith ierr = VecDestroy(&fh);CHKERRQ(ierr); 5014da9b6338SBarry Smith PetscFunctionReturn(0); 5015da9b6338SBarry Smith } 501671f87433Sdalcinl 501771f87433Sdalcinl /*@ 5018fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 501971f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 502071f87433Sdalcinl Newton method. 502171f87433Sdalcinl 50223f9fe445SBarry Smith Logically Collective on SNES 502371f87433Sdalcinl 502471f87433Sdalcinl Input Parameters: 502571f87433Sdalcinl + snes - SNES context 502671f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 502771f87433Sdalcinl 502864ba62caSBarry Smith Options Database: 502964ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 503064ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 503164ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 503264ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 503364ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 503464ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 503564ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 503664ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 503764ba62caSBarry Smith 503871f87433Sdalcinl Notes: 503971f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 504071f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 504171f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 504271f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 504371f87433Sdalcinl solver. 504471f87433Sdalcinl 504571f87433Sdalcinl Level: advanced 504671f87433Sdalcinl 504771f87433Sdalcinl Reference: 504871f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 504971f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 505071f87433Sdalcinl 505171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 505271f87433Sdalcinl 5053fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 505471f87433Sdalcinl @*/ 50557087cfbeSBarry Smith PetscErrorCode SNESKSPSetUseEW(SNES snes,PetscBool flag) 505671f87433Sdalcinl { 505771f87433Sdalcinl PetscFunctionBegin; 50580700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5059acfcf0e5SJed Brown PetscValidLogicalCollectiveBool(snes,flag,2); 506071f87433Sdalcinl snes->ksp_ewconv = flag; 506171f87433Sdalcinl PetscFunctionReturn(0); 506271f87433Sdalcinl } 506371f87433Sdalcinl 506471f87433Sdalcinl /*@ 5065fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 506671f87433Sdalcinl for computing relative tolerance for linear solvers within an 506771f87433Sdalcinl inexact Newton method. 506871f87433Sdalcinl 506971f87433Sdalcinl Not Collective 507071f87433Sdalcinl 507171f87433Sdalcinl Input Parameter: 507271f87433Sdalcinl . snes - SNES context 507371f87433Sdalcinl 507471f87433Sdalcinl Output Parameter: 507571f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 507671f87433Sdalcinl 507771f87433Sdalcinl Notes: 507871f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 507971f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 508071f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 508171f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 508271f87433Sdalcinl solver. 508371f87433Sdalcinl 508471f87433Sdalcinl Level: advanced 508571f87433Sdalcinl 508671f87433Sdalcinl Reference: 508771f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 508871f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 508971f87433Sdalcinl 509071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 509171f87433Sdalcinl 5092fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 509371f87433Sdalcinl @*/ 50947087cfbeSBarry Smith PetscErrorCode SNESKSPGetUseEW(SNES snes, PetscBool *flag) 509571f87433Sdalcinl { 509671f87433Sdalcinl PetscFunctionBegin; 50970700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 509871f87433Sdalcinl PetscValidPointer(flag,2); 509971f87433Sdalcinl *flag = snes->ksp_ewconv; 510071f87433Sdalcinl PetscFunctionReturn(0); 510171f87433Sdalcinl } 510271f87433Sdalcinl 510371f87433Sdalcinl /*@ 5104fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 510571f87433Sdalcinl convergence criteria for the linear solvers within an inexact 510671f87433Sdalcinl Newton method. 510771f87433Sdalcinl 51083f9fe445SBarry Smith Logically Collective on SNES 510971f87433Sdalcinl 511071f87433Sdalcinl Input Parameters: 511171f87433Sdalcinl + snes - SNES context 511271f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 511371f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 511471f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 511571f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 511671f87433Sdalcinl (0 <= gamma2 <= 1) 511771f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 511871f87433Sdalcinl . alpha2 - power for safeguard 511971f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 512071f87433Sdalcinl 512171f87433Sdalcinl Note: 512271f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 512371f87433Sdalcinl 512471f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 512571f87433Sdalcinl 512671f87433Sdalcinl Level: advanced 512771f87433Sdalcinl 512871f87433Sdalcinl Reference: 512971f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 513071f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 513171f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 513271f87433Sdalcinl 513371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 513471f87433Sdalcinl 5135fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 513671f87433Sdalcinl @*/ 5137f5af7f23SKarl Rupp PetscErrorCode SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 513871f87433Sdalcinl { 5139fa9f3622SBarry Smith SNESKSPEW *kctx; 51405fd66863SKarl Rupp 514171f87433Sdalcinl PetscFunctionBegin; 51420700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5143fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 5144e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 5145c5eb9154SBarry Smith PetscValidLogicalCollectiveInt(snes,version,2); 5146c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_0,3); 5147c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,rtol_max,4); 5148c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,gamma,5); 5149c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha,6); 5150c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,alpha2,7); 5151c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(snes,threshold,8); 515271f87433Sdalcinl 515371f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 515471f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 515571f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 515671f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 515771f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 515871f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 515971f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 516071f87433Sdalcinl 5161f23aa3ddSBarry Smith if (kctx->version < 1 || kctx->version > 3) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 516257622a8eSBarry Smith if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %g",(double)kctx->rtol_0); 516357622a8eSBarry Smith if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%g) < 1.0\n",(double)kctx->rtol_max); 516457622a8eSBarry Smith if (kctx->gamma < 0.0 || kctx->gamma > 1.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%g) <= 1.0\n",(double)kctx->gamma); 516557622a8eSBarry Smith if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%g) <= 2.0\n",(double)kctx->alpha); 516657622a8eSBarry Smith if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%g) < 1.0\n",(double)kctx->threshold); 516771f87433Sdalcinl PetscFunctionReturn(0); 516871f87433Sdalcinl } 516971f87433Sdalcinl 517071f87433Sdalcinl /*@ 5171fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 517271f87433Sdalcinl convergence criteria for the linear solvers within an inexact 517371f87433Sdalcinl Newton method. 517471f87433Sdalcinl 517571f87433Sdalcinl Not Collective 517671f87433Sdalcinl 517771f87433Sdalcinl Input Parameters: 517871f87433Sdalcinl snes - SNES context 517971f87433Sdalcinl 518071f87433Sdalcinl Output Parameters: 518171f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 518271f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 518371f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 5184bf388a1fSBarry Smith . gamma - multiplicative factor for version 2 rtol computation (0 <= gamma2 <= 1) 518571f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 518671f87433Sdalcinl . alpha2 - power for safeguard 518771f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 518871f87433Sdalcinl 518971f87433Sdalcinl Level: advanced 519071f87433Sdalcinl 519171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 519271f87433Sdalcinl 5193fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 519471f87433Sdalcinl @*/ 5195bf388a1fSBarry Smith PetscErrorCode SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 519671f87433Sdalcinl { 5197fa9f3622SBarry Smith SNESKSPEW *kctx; 51985fd66863SKarl Rupp 519971f87433Sdalcinl PetscFunctionBegin; 52000700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5201fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 5202e32f2f54SBarry Smith if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 520371f87433Sdalcinl if (version) *version = kctx->version; 520471f87433Sdalcinl if (rtol_0) *rtol_0 = kctx->rtol_0; 520571f87433Sdalcinl if (rtol_max) *rtol_max = kctx->rtol_max; 520671f87433Sdalcinl if (gamma) *gamma = kctx->gamma; 520771f87433Sdalcinl if (alpha) *alpha = kctx->alpha; 520871f87433Sdalcinl if (alpha2) *alpha2 = kctx->alpha2; 520971f87433Sdalcinl if (threshold) *threshold = kctx->threshold; 521071f87433Sdalcinl PetscFunctionReturn(0); 521171f87433Sdalcinl } 521271f87433Sdalcinl 5213d5378b5fSDmitry Karpeev PetscErrorCode KSPPreSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes) 521471f87433Sdalcinl { 521571f87433Sdalcinl PetscErrorCode ierr; 5216fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 521771f87433Sdalcinl PetscReal rtol = PETSC_DEFAULT,stol; 521871f87433Sdalcinl 521971f87433Sdalcinl PetscFunctionBegin; 5220d4211eb9SBarry Smith if (!snes->ksp_ewconv) PetscFunctionReturn(0); 522130058271SDmitry Karpeev if (!snes->iter) { 522230058271SDmitry Karpeev rtol = kctx->rtol_0; /* first time in, so use the original user rtol */ 522330058271SDmitry Karpeev ierr = VecNorm(snes->vec_func,NORM_2,&kctx->norm_first);CHKERRQ(ierr); 522430058271SDmitry Karpeev } 5225f5af7f23SKarl Rupp else { 522671f87433Sdalcinl if (kctx->version == 1) { 522771f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 522871f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 522985ec1a3cSBarry Smith stol = PetscPowReal(kctx->rtol_last,kctx->alpha2); 523071f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 523171f87433Sdalcinl } else if (kctx->version == 2) { 523285ec1a3cSBarry Smith rtol = kctx->gamma * PetscPowReal(snes->norm/kctx->norm_last,kctx->alpha); 523385ec1a3cSBarry Smith stol = kctx->gamma * PetscPowReal(kctx->rtol_last,kctx->alpha); 523471f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 523571f87433Sdalcinl } else if (kctx->version == 3) { /* contributed by Luis Chacon, June 2006. */ 523685ec1a3cSBarry Smith rtol = kctx->gamma * PetscPowReal(snes->norm/kctx->norm_last,kctx->alpha); 523771f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 523885ec1a3cSBarry Smith stol = kctx->gamma*PetscPowReal(kctx->rtol_last,kctx->alpha); 523971f87433Sdalcinl stol = PetscMax(rtol,stol); 524071f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 524171f87433Sdalcinl /* safeguard: avoid oversolving */ 524230058271SDmitry Karpeev stol = kctx->gamma*(kctx->norm_first*snes->rtol)/snes->norm; 524371f87433Sdalcinl stol = PetscMax(rtol,stol); 524471f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 5245e32f2f54SBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 524671f87433Sdalcinl } 524771f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 524871f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 524971f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 525057622a8eSBarry Smith ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%g\n",snes->iter,kctx->version,(double)rtol);CHKERRQ(ierr); 525171f87433Sdalcinl PetscFunctionReturn(0); 525271f87433Sdalcinl } 525371f87433Sdalcinl 5254d5378b5fSDmitry Karpeev PetscErrorCode KSPPostSolve_SNESEW(KSP ksp, Vec b, Vec x, SNES snes) 525571f87433Sdalcinl { 525671f87433Sdalcinl PetscErrorCode ierr; 5257fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 525871f87433Sdalcinl PCSide pcside; 525971f87433Sdalcinl Vec lres; 526071f87433Sdalcinl 526171f87433Sdalcinl PetscFunctionBegin; 5262d4211eb9SBarry Smith if (!snes->ksp_ewconv) PetscFunctionReturn(0); 526371f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 526471dbe336SPeter Brune kctx->norm_last = snes->norm; 526571f87433Sdalcinl if (kctx->version == 1) { 52664f00ce20SMatthew G. Knepley PC pc; 52674f00ce20SMatthew G. Knepley PetscBool isNone; 52684f00ce20SMatthew G. Knepley 52694f00ce20SMatthew G. Knepley ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); 52704f00ce20SMatthew G. Knepley ierr = PetscObjectTypeCompare((PetscObject) pc, PCNONE, &isNone);CHKERRQ(ierr); 5271b037da10SBarry Smith ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr); 52724f00ce20SMatthew G. Knepley if (pcside == PC_RIGHT || isNone) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 527371f87433Sdalcinl /* KSP residual is true linear residual */ 527471f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 527571f87433Sdalcinl } else { 527671f87433Sdalcinl /* KSP residual is preconditioned residual */ 527771f87433Sdalcinl /* compute true linear residual norm */ 527871f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 527971f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 528071f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 528171f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 52826bf464f9SBarry Smith ierr = VecDestroy(&lres);CHKERRQ(ierr); 528371f87433Sdalcinl } 528471f87433Sdalcinl } 528571f87433Sdalcinl PetscFunctionReturn(0); 528671f87433Sdalcinl } 528771f87433Sdalcinl 5288d4211eb9SBarry Smith /*@ 5289d4211eb9SBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 5290d4211eb9SBarry Smith 5291d4211eb9SBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 5292d4211eb9SBarry Smith 5293d4211eb9SBarry Smith Input Parameter: 5294d4211eb9SBarry Smith . snes - the SNES context 5295d4211eb9SBarry Smith 5296d4211eb9SBarry Smith Output Parameter: 5297d4211eb9SBarry Smith . ksp - the KSP context 5298d4211eb9SBarry Smith 5299d4211eb9SBarry Smith Notes: 5300d4211eb9SBarry Smith The user can then directly manipulate the KSP context to set various 5301d4211eb9SBarry Smith options, etc. Likewise, the user can then extract and manipulate the 5302d4211eb9SBarry Smith PC contexts as well. 5303d4211eb9SBarry Smith 5304d4211eb9SBarry Smith Level: beginner 5305d4211eb9SBarry Smith 5306d4211eb9SBarry Smith .keywords: SNES, nonlinear, get, KSP, context 5307d4211eb9SBarry Smith 5308d4211eb9SBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 5309d4211eb9SBarry Smith @*/ 5310d4211eb9SBarry Smith PetscErrorCode SNESGetKSP(SNES snes,KSP *ksp) 531171f87433Sdalcinl { 531271f87433Sdalcinl PetscErrorCode ierr; 531371f87433Sdalcinl 531471f87433Sdalcinl PetscFunctionBegin; 5315d4211eb9SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5316d4211eb9SBarry Smith PetscValidPointer(ksp,2); 5317d4211eb9SBarry Smith 5318d4211eb9SBarry Smith if (!snes->ksp) { 5319a5c2985bSBarry Smith PetscBool monitor = PETSC_FALSE; 5320a5c2985bSBarry Smith 5321d4211eb9SBarry Smith ierr = KSPCreate(PetscObjectComm((PetscObject)snes),&snes->ksp);CHKERRQ(ierr); 5322d4211eb9SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 53233bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)snes,(PetscObject)snes->ksp);CHKERRQ(ierr); 5324d4211eb9SBarry Smith 5325d5378b5fSDmitry Karpeev ierr = KSPSetPreSolve(snes->ksp,(PetscErrorCode (*)(KSP,Vec,Vec,void*))KSPPreSolve_SNESEW,snes);CHKERRQ(ierr); 5326d5378b5fSDmitry Karpeev ierr = KSPSetPostSolve(snes->ksp,(PetscErrorCode (*)(KSP,Vec,Vec,void*))KSPPostSolve_SNESEW,snes);CHKERRQ(ierr); 5327a5c2985bSBarry Smith 5328c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-ksp_monitor_snes",&monitor,NULL);CHKERRQ(ierr); 5329a5c2985bSBarry Smith if (monitor) { 5330a5c2985bSBarry Smith ierr = KSPMonitorSet(snes->ksp,KSPMonitorSNES,snes,NULL);CHKERRQ(ierr); 5331a5c2985bSBarry Smith } 5332e5f7ee39SBarry Smith monitor = PETSC_FALSE; 5333c5929fdfSBarry Smith ierr = PetscOptionsGetBool(((PetscObject)snes)->options,((PetscObject)snes)->prefix,"-ksp_monitor_snes_lg",&monitor,NULL);CHKERRQ(ierr); 5334e5f7ee39SBarry Smith if (monitor) { 5335e5f7ee39SBarry Smith PetscObject *objs; 53368b0b5a47SLisandro Dalcin ierr = KSPMonitorSNESLGResidualNormCreate(PetscObjectComm((PetscObject)snes),NULL,NULL,PETSC_DECIDE,PETSC_DECIDE,600,600,&objs);CHKERRQ(ierr); 5337e5f7ee39SBarry Smith objs[0] = (PetscObject) snes; 5338e5f7ee39SBarry Smith ierr = KSPMonitorSet(snes->ksp,(PetscErrorCode (*)(KSP,PetscInt,PetscReal,void*))KSPMonitorSNESLGResidualNorm,objs,(PetscErrorCode (*)(void**))KSPMonitorSNESLGResidualNormDestroy);CHKERRQ(ierr); 5339e5f7ee39SBarry Smith } 534016413a6aSBarry Smith ierr = PetscObjectSetOptions((PetscObject)snes->ksp,((PetscObject)snes)->options);CHKERRQ(ierr); 5341d4211eb9SBarry Smith } 5342d4211eb9SBarry Smith *ksp = snes->ksp; 534371f87433Sdalcinl PetscFunctionReturn(0); 534471f87433Sdalcinl } 53456c699258SBarry Smith 5346d4211eb9SBarry Smith 5347af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 53486c699258SBarry Smith /*@ 53492a808120SBarry Smith SNESSetDM - Sets the DM that may be used by some nonlinear solvers or their underlying preconditioners 53506c699258SBarry Smith 53513f9fe445SBarry Smith Logically Collective on SNES 53526c699258SBarry Smith 53536c699258SBarry Smith Input Parameters: 53542a808120SBarry Smith + snes - the nonlinear solver context 53552a808120SBarry Smith - dm - the dm, cannot be NULL 53566c699258SBarry Smith 5357e03a659cSJed Brown Notes: 5358e03a659cSJed Brown A DM can only be used for solving one problem at a time because information about the problem is stored on the DM, 5359e03a659cSJed Brown even when not using interfaces like DMSNESSetFunction(). Use DMClone() to get a distinct DM when solving different 5360e03a659cSJed Brown problems using the same function space. 5361e03a659cSJed Brown 53626c699258SBarry Smith Level: intermediate 53636c699258SBarry Smith 53644c2026ceSFande Kong .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM() 53656c699258SBarry Smith @*/ 53667087cfbeSBarry Smith PetscErrorCode SNESSetDM(SNES snes,DM dm) 53676c699258SBarry Smith { 53686c699258SBarry Smith PetscErrorCode ierr; 5369345fed2cSBarry Smith KSP ksp; 5370942e3340SBarry Smith DMSNES sdm; 53716c699258SBarry Smith 53726c699258SBarry Smith PetscFunctionBegin; 53730700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 53742a808120SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,2); 53752a808120SBarry Smith ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr); 5376942e3340SBarry Smith if (snes->dm) { /* Move the DMSNES context over to the new DM unless the new DM already has one */ 537751f4b3c7SToby Isaac if (snes->dm->dmsnes && !dm->dmsnes) { 5378942e3340SBarry Smith ierr = DMCopyDMSNES(snes->dm,dm);CHKERRQ(ierr); 5379942e3340SBarry Smith ierr = DMGetDMSNES(snes->dm,&sdm);CHKERRQ(ierr); 5380f5af7f23SKarl Rupp if (sdm->originaldm == snes->dm) sdm->originaldm = dm; /* Grant write privileges to the replacement DM */ 53816cab3a1bSJed Brown } 5382dc822a44SJed Brown ierr = DMCoarsenHookRemove(snes->dm,DMCoarsenHook_SNESVecSol,DMRestrictHook_SNESVecSol,snes);CHKERRQ(ierr); 53836bf464f9SBarry Smith ierr = DMDestroy(&snes->dm);CHKERRQ(ierr); 53846cab3a1bSJed Brown } 53856c699258SBarry Smith snes->dm = dm; 5386116d1032SJed Brown snes->dmAuto = PETSC_FALSE; 5387f5af7f23SKarl Rupp 5388345fed2cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 5389345fed2cSBarry Smith ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr); 5390f22f69f0SBarry Smith ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr); 5391efd4aadfSBarry Smith if (snes->npc) { 5392efd4aadfSBarry Smith ierr = SNESSetDM(snes->npc, snes->dm);CHKERRQ(ierr); 5393efd4aadfSBarry Smith ierr = SNESSetNPCSide(snes,snes->npcside);CHKERRQ(ierr); 53942c155ee1SBarry Smith } 53956c699258SBarry Smith PetscFunctionReturn(0); 53966c699258SBarry Smith } 53976c699258SBarry Smith 53986c699258SBarry Smith /*@ 53996c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 54006c699258SBarry Smith 54013f9fe445SBarry Smith Not Collective but DM obtained is parallel on SNES 54026c699258SBarry Smith 54036c699258SBarry Smith Input Parameter: 54046c699258SBarry Smith . snes - the preconditioner context 54056c699258SBarry Smith 54066c699258SBarry Smith Output Parameter: 54076c699258SBarry Smith . dm - the dm 54086c699258SBarry Smith 54096c699258SBarry Smith Level: intermediate 54106c699258SBarry Smith 54114c2026ceSFande Kong .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM() 54126c699258SBarry Smith @*/ 54137087cfbeSBarry Smith PetscErrorCode SNESGetDM(SNES snes,DM *dm) 54146c699258SBarry Smith { 54156cab3a1bSJed Brown PetscErrorCode ierr; 54166cab3a1bSJed Brown 54176c699258SBarry Smith PetscFunctionBegin; 54180700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 54196cab3a1bSJed Brown if (!snes->dm) { 5420ce94432eSBarry Smith ierr = DMShellCreate(PetscObjectComm((PetscObject)snes),&snes->dm);CHKERRQ(ierr); 5421116d1032SJed Brown snes->dmAuto = PETSC_TRUE; 54226cab3a1bSJed Brown } 54236c699258SBarry Smith *dm = snes->dm; 54246c699258SBarry Smith PetscFunctionReturn(0); 54256c699258SBarry Smith } 54260807856dSBarry Smith 542731823bd8SMatthew G Knepley /*@ 5428be95d8f1SBarry Smith SNESSetNPC - Sets the nonlinear preconditioner to be used. 542931823bd8SMatthew G Knepley 543031823bd8SMatthew G Knepley Collective on SNES 543131823bd8SMatthew G Knepley 543231823bd8SMatthew G Knepley Input Parameters: 543331823bd8SMatthew G Knepley + snes - iterative context obtained from SNESCreate() 543431823bd8SMatthew G Knepley - pc - the preconditioner object 543531823bd8SMatthew G Knepley 543631823bd8SMatthew G Knepley Notes: 5437be95d8f1SBarry Smith Use SNESGetNPC() to retrieve the preconditioner context (for example, 543831823bd8SMatthew G Knepley to configure it using the API). 543931823bd8SMatthew G Knepley 544031823bd8SMatthew G Knepley Level: developer 544131823bd8SMatthew G Knepley 544231823bd8SMatthew G Knepley .keywords: SNES, set, precondition 54433ad1a0b9SPatrick Farrell .seealso: SNESGetNPC(), SNESHasNPC() 544431823bd8SMatthew G Knepley @*/ 5445be95d8f1SBarry Smith PetscErrorCode SNESSetNPC(SNES snes, SNES pc) 544631823bd8SMatthew G Knepley { 544731823bd8SMatthew G Knepley PetscErrorCode ierr; 544831823bd8SMatthew G Knepley 544931823bd8SMatthew G Knepley PetscFunctionBegin; 545031823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 545131823bd8SMatthew G Knepley PetscValidHeaderSpecific(pc, SNES_CLASSID, 2); 545231823bd8SMatthew G Knepley PetscCheckSameComm(snes, 1, pc, 2); 545331823bd8SMatthew G Knepley ierr = PetscObjectReference((PetscObject) pc);CHKERRQ(ierr); 5454efd4aadfSBarry Smith ierr = SNESDestroy(&snes->npc);CHKERRQ(ierr); 5455efd4aadfSBarry Smith snes->npc = pc; 5456efd4aadfSBarry Smith ierr = PetscLogObjectParent((PetscObject)snes, (PetscObject)snes->npc);CHKERRQ(ierr); 545731823bd8SMatthew G Knepley PetscFunctionReturn(0); 545831823bd8SMatthew G Knepley } 545931823bd8SMatthew G Knepley 546031823bd8SMatthew G Knepley /*@ 5461be95d8f1SBarry Smith SNESGetNPC - Creates a nonlinear preconditioning solver (SNES) to be used to precondition the nonlinear solver. 546231823bd8SMatthew G Knepley 546331823bd8SMatthew G Knepley Not Collective 546431823bd8SMatthew G Knepley 546531823bd8SMatthew G Knepley Input Parameter: 546631823bd8SMatthew G Knepley . snes - iterative context obtained from SNESCreate() 546731823bd8SMatthew G Knepley 546831823bd8SMatthew G Knepley Output Parameter: 546931823bd8SMatthew G Knepley . pc - preconditioner context 547031823bd8SMatthew G Knepley 547195452b02SPatrick Sanan Notes: 547295452b02SPatrick Sanan If a SNES was previously set with SNESSetNPC() then that SNES is returned. 5473be95d8f1SBarry Smith 547431823bd8SMatthew G Knepley Level: developer 547531823bd8SMatthew G Knepley 547631823bd8SMatthew G Knepley .keywords: SNES, get, preconditioner 54773ad1a0b9SPatrick Farrell .seealso: SNESSetNPC(), SNESHasNPC() 547831823bd8SMatthew G Knepley @*/ 5479be95d8f1SBarry Smith PetscErrorCode SNESGetNPC(SNES snes, SNES *pc) 548031823bd8SMatthew G Knepley { 548131823bd8SMatthew G Knepley PetscErrorCode ierr; 5482a64e098fSPeter Brune const char *optionsprefix; 548331823bd8SMatthew G Knepley 548431823bd8SMatthew G Knepley PetscFunctionBegin; 548531823bd8SMatthew G Knepley PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 548631823bd8SMatthew G Knepley PetscValidPointer(pc, 2); 5487efd4aadfSBarry Smith if (!snes->npc) { 5488efd4aadfSBarry Smith ierr = SNESCreate(PetscObjectComm((PetscObject)snes),&snes->npc);CHKERRQ(ierr); 5489efd4aadfSBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->npc,(PetscObject)snes,1);CHKERRQ(ierr); 5490efd4aadfSBarry Smith ierr = PetscLogObjectParent((PetscObject)snes,(PetscObject)snes->npc);CHKERRQ(ierr); 5491a64e098fSPeter Brune ierr = SNESGetOptionsPrefix(snes,&optionsprefix);CHKERRQ(ierr); 5492efd4aadfSBarry Smith ierr = SNESSetOptionsPrefix(snes->npc,optionsprefix);CHKERRQ(ierr); 5493efd4aadfSBarry Smith ierr = SNESAppendOptionsPrefix(snes->npc,"npc_");CHKERRQ(ierr); 5494efd4aadfSBarry Smith ierr = SNESSetCountersReset(snes->npc,PETSC_FALSE);CHKERRQ(ierr); 549531823bd8SMatthew G Knepley } 5496efd4aadfSBarry Smith *pc = snes->npc; 549731823bd8SMatthew G Knepley PetscFunctionReturn(0); 549831823bd8SMatthew G Knepley } 549931823bd8SMatthew G Knepley 55003ad1a0b9SPatrick Farrell /*@ 55013ad1a0b9SPatrick Farrell SNESHasNPC - Returns whether a nonlinear preconditioner exists 55023ad1a0b9SPatrick Farrell 55033ad1a0b9SPatrick Farrell Not Collective 55043ad1a0b9SPatrick Farrell 55053ad1a0b9SPatrick Farrell Input Parameter: 55063ad1a0b9SPatrick Farrell . snes - iterative context obtained from SNESCreate() 55073ad1a0b9SPatrick Farrell 55083ad1a0b9SPatrick Farrell Output Parameter: 55093ad1a0b9SPatrick Farrell . has_npc - whether the SNES has an NPC or not 55103ad1a0b9SPatrick Farrell 55113ad1a0b9SPatrick Farrell Level: developer 55123ad1a0b9SPatrick Farrell 55133ad1a0b9SPatrick Farrell .keywords: SNES, has, preconditioner 55143ad1a0b9SPatrick Farrell .seealso: SNESSetNPC(), SNESGetNPC() 55153ad1a0b9SPatrick Farrell @*/ 55163ad1a0b9SPatrick Farrell PetscErrorCode SNESHasNPC(SNES snes, PetscBool *has_npc) 55173ad1a0b9SPatrick Farrell { 55183ad1a0b9SPatrick Farrell PetscFunctionBegin; 55193ad1a0b9SPatrick Farrell PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5520efd4aadfSBarry Smith *has_npc = (PetscBool) (snes->npc ? PETSC_TRUE : PETSC_FALSE); 55213ad1a0b9SPatrick Farrell PetscFunctionReturn(0); 55223ad1a0b9SPatrick Farrell } 55233ad1a0b9SPatrick Farrell 5524c40d0f55SPeter Brune /*@ 5525be95d8f1SBarry Smith SNESSetNPCSide - Sets the preconditioning side. 5526c40d0f55SPeter Brune 5527c40d0f55SPeter Brune Logically Collective on SNES 5528c40d0f55SPeter Brune 5529c40d0f55SPeter Brune Input Parameter: 5530c40d0f55SPeter Brune . snes - iterative context obtained from SNESCreate() 5531c40d0f55SPeter Brune 5532c40d0f55SPeter Brune Output Parameter: 5533c40d0f55SPeter Brune . side - the preconditioning side, where side is one of 5534c40d0f55SPeter Brune .vb 55352d547940SBarry Smith PC_LEFT - left preconditioning 55362d547940SBarry Smith PC_RIGHT - right preconditioning (default for most nonlinear solvers) 5537c40d0f55SPeter Brune .ve 5538c40d0f55SPeter Brune 5539c40d0f55SPeter Brune Options Database Keys: 5540c40d0f55SPeter Brune . -snes_pc_side <right,left> 5541c40d0f55SPeter Brune 554295452b02SPatrick Sanan Notes: 554395452b02SPatrick Sanan SNESNRICHARDSON and SNESNCG only support left preconditioning. 55442d547940SBarry Smith 5545c40d0f55SPeter Brune Level: intermediate 5546c40d0f55SPeter Brune 5547c40d0f55SPeter Brune .keywords: SNES, set, right, left, side, preconditioner, flag 5548c40d0f55SPeter Brune 5549be95d8f1SBarry Smith .seealso: SNESGetNPCSide(), KSPSetPCSide() 5550c40d0f55SPeter Brune @*/ 5551be95d8f1SBarry Smith PetscErrorCode SNESSetNPCSide(SNES snes,PCSide side) 5552c40d0f55SPeter Brune { 5553c40d0f55SPeter Brune PetscFunctionBegin; 5554c40d0f55SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5555c40d0f55SPeter Brune PetscValidLogicalCollectiveEnum(snes,side,2); 5556efd4aadfSBarry Smith snes->npcside= side; 5557c40d0f55SPeter Brune PetscFunctionReturn(0); 5558c40d0f55SPeter Brune } 5559c40d0f55SPeter Brune 5560c40d0f55SPeter Brune /*@ 5561be95d8f1SBarry Smith SNESGetNPCSide - Gets the preconditioning side. 5562c40d0f55SPeter Brune 5563c40d0f55SPeter Brune Not Collective 5564c40d0f55SPeter Brune 5565c40d0f55SPeter Brune Input Parameter: 5566c40d0f55SPeter Brune . snes - iterative context obtained from SNESCreate() 5567c40d0f55SPeter Brune 5568c40d0f55SPeter Brune Output Parameter: 5569c40d0f55SPeter Brune . side - the preconditioning side, where side is one of 5570c40d0f55SPeter Brune .vb 55712d547940SBarry Smith PC_LEFT - left preconditioning 55722d547940SBarry Smith PC_RIGHT - right preconditioning (default for most nonlinear solvers) 5573c40d0f55SPeter Brune .ve 5574c40d0f55SPeter Brune 5575c40d0f55SPeter Brune Level: intermediate 5576c40d0f55SPeter Brune 5577c40d0f55SPeter Brune .keywords: SNES, get, right, left, side, preconditioner, flag 5578c40d0f55SPeter Brune 5579be95d8f1SBarry Smith .seealso: SNESSetNPCSide(), KSPGetPCSide() 5580c40d0f55SPeter Brune @*/ 5581be95d8f1SBarry Smith PetscErrorCode SNESGetNPCSide(SNES snes,PCSide *side) 5582c40d0f55SPeter Brune { 5583c40d0f55SPeter Brune PetscFunctionBegin; 5584c40d0f55SPeter Brune PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5585c40d0f55SPeter Brune PetscValidPointer(side,2); 5586efd4aadfSBarry Smith *side = snes->npcside; 5587c40d0f55SPeter Brune PetscFunctionReturn(0); 5588c40d0f55SPeter Brune } 5589c40d0f55SPeter Brune 55909e764e56SPeter Brune /*@ 55917601faf0SJed Brown SNESSetLineSearch - Sets the linesearch on the SNES instance. 55929e764e56SPeter Brune 55939e764e56SPeter Brune Collective on SNES 55949e764e56SPeter Brune 55959e764e56SPeter Brune Input Parameters: 55969e764e56SPeter Brune + snes - iterative context obtained from SNESCreate() 55979e764e56SPeter Brune - linesearch - the linesearch object 55989e764e56SPeter Brune 55999e764e56SPeter Brune Notes: 56007601faf0SJed Brown Use SNESGetLineSearch() to retrieve the preconditioner context (for example, 56019e764e56SPeter Brune to configure it using the API). 56029e764e56SPeter Brune 56039e764e56SPeter Brune Level: developer 56049e764e56SPeter Brune 56059e764e56SPeter Brune .keywords: SNES, set, linesearch 56067601faf0SJed Brown .seealso: SNESGetLineSearch() 56079e764e56SPeter Brune @*/ 56087601faf0SJed Brown PetscErrorCode SNESSetLineSearch(SNES snes, SNESLineSearch linesearch) 56099e764e56SPeter Brune { 56109e764e56SPeter Brune PetscErrorCode ierr; 56119e764e56SPeter Brune 56129e764e56SPeter Brune PetscFunctionBegin; 56139e764e56SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 5614f1c6b773SPeter Brune PetscValidHeaderSpecific(linesearch, SNESLINESEARCH_CLASSID, 2); 56159e764e56SPeter Brune PetscCheckSameComm(snes, 1, linesearch, 2); 56169e764e56SPeter Brune ierr = PetscObjectReference((PetscObject) linesearch);CHKERRQ(ierr); 5617f1c6b773SPeter Brune ierr = SNESLineSearchDestroy(&snes->linesearch);CHKERRQ(ierr); 5618f5af7f23SKarl Rupp 56199e764e56SPeter Brune snes->linesearch = linesearch; 5620f5af7f23SKarl Rupp 56213bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)snes, (PetscObject)snes->linesearch);CHKERRQ(ierr); 56229e764e56SPeter Brune PetscFunctionReturn(0); 56239e764e56SPeter Brune } 56249e764e56SPeter Brune 5625a34ceb2aSJed Brown /*@ 56267601faf0SJed Brown SNESGetLineSearch - Returns a pointer to the line search context set with SNESSetLineSearch() 56278141a3b9SPeter Brune or creates a default line search instance associated with the SNES and returns it. 56289e764e56SPeter Brune 56299e764e56SPeter Brune Not Collective 56309e764e56SPeter Brune 56319e764e56SPeter Brune Input Parameter: 56329e764e56SPeter Brune . snes - iterative context obtained from SNESCreate() 56339e764e56SPeter Brune 56349e764e56SPeter Brune Output Parameter: 56359e764e56SPeter Brune . linesearch - linesearch context 56369e764e56SPeter Brune 5637162e0bf5SPeter Brune Level: beginner 56389e764e56SPeter Brune 56399e764e56SPeter Brune .keywords: SNES, get, linesearch 5640162e0bf5SPeter Brune .seealso: SNESSetLineSearch(), SNESLineSearchCreate() 56419e764e56SPeter Brune @*/ 56427601faf0SJed Brown PetscErrorCode SNESGetLineSearch(SNES snes, SNESLineSearch *linesearch) 56439e764e56SPeter Brune { 56449e764e56SPeter Brune PetscErrorCode ierr; 56459e764e56SPeter Brune const char *optionsprefix; 56469e764e56SPeter Brune 56479e764e56SPeter Brune PetscFunctionBegin; 56489e764e56SPeter Brune PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 56499e764e56SPeter Brune PetscValidPointer(linesearch, 2); 56509e764e56SPeter Brune if (!snes->linesearch) { 56519e764e56SPeter Brune ierr = SNESGetOptionsPrefix(snes, &optionsprefix);CHKERRQ(ierr); 565282f516ccSBarry Smith ierr = SNESLineSearchCreate(PetscObjectComm((PetscObject)snes), &snes->linesearch);CHKERRQ(ierr); 5653f1c6b773SPeter Brune ierr = SNESLineSearchSetSNES(snes->linesearch, snes);CHKERRQ(ierr); 5654b1dca40bSPeter Brune ierr = SNESLineSearchAppendOptionsPrefix(snes->linesearch, optionsprefix);CHKERRQ(ierr); 56559e764e56SPeter Brune ierr = PetscObjectIncrementTabLevel((PetscObject) snes->linesearch, (PetscObject) snes, 1);CHKERRQ(ierr); 56563bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)snes, (PetscObject)snes->linesearch);CHKERRQ(ierr); 56579e764e56SPeter Brune } 56589e764e56SPeter Brune *linesearch = snes->linesearch; 56599e764e56SPeter Brune PetscFunctionReturn(0); 56609e764e56SPeter Brune } 56619e764e56SPeter Brune 566269b4f73cSBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE) 5663c6db04a5SJed Brown #include <mex.h> 566469b4f73cSBarry Smith 56658f6e6473SBarry Smith typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext; 56668f6e6473SBarry Smith 56670807856dSBarry Smith /* 5668bf388a1fSBarry Smith SNESComputeFunction_Matlab - Calls the function that has been set with SNESSetFunctionMatlab(). 56690807856dSBarry Smith 56700807856dSBarry Smith Collective on SNES 56710807856dSBarry Smith 56720807856dSBarry Smith Input Parameters: 56730807856dSBarry Smith + snes - the SNES context 56740807856dSBarry Smith - x - input vector 56750807856dSBarry Smith 56760807856dSBarry Smith Output Parameter: 56770807856dSBarry Smith . y - function vector, as set by SNESSetFunction() 56780807856dSBarry Smith 56790807856dSBarry Smith Notes: 56800807856dSBarry Smith SNESComputeFunction() is typically used within nonlinear solvers 56810807856dSBarry Smith implementations, so most users would not generally call this routine 56820807856dSBarry Smith themselves. 56830807856dSBarry Smith 56840807856dSBarry Smith Level: developer 56850807856dSBarry Smith 56860807856dSBarry Smith .keywords: SNES, nonlinear, compute, function 56870807856dSBarry Smith 56880807856dSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 568961b2408cSBarry Smith */ 56907087cfbeSBarry Smith PetscErrorCode SNESComputeFunction_Matlab(SNES snes,Vec x,Vec y, void *ctx) 56910807856dSBarry Smith { 5692e650e774SBarry Smith PetscErrorCode ierr; 56938f6e6473SBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext*)ctx; 56948f6e6473SBarry Smith int nlhs = 1,nrhs = 5; 56958f6e6473SBarry Smith mxArray *plhs[1],*prhs[5]; 569691621f2eSBarry Smith long long int lx = 0,ly = 0,ls = 0; 5697e650e774SBarry Smith 56980807856dSBarry Smith PetscFunctionBegin; 56990807856dSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 57000807856dSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 57010807856dSBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 57020807856dSBarry Smith PetscCheckSameComm(snes,1,x,2); 57030807856dSBarry Smith PetscCheckSameComm(snes,1,y,3); 57040807856dSBarry Smith 57050807856dSBarry Smith /* call Matlab function in ctx with arguments x and y */ 5706e650e774SBarry Smith 570791621f2eSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 5708e650e774SBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 5709e650e774SBarry Smith ierr = PetscMemcpy(&ly,&y,sizeof(x));CHKERRQ(ierr); 571091621f2eSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 571191621f2eSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 571291621f2eSBarry Smith prhs[2] = mxCreateDoubleScalar((double)ly); 57138f6e6473SBarry Smith prhs[3] = mxCreateString(sctx->funcname); 57148f6e6473SBarry Smith prhs[4] = sctx->ctx; 5715b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeFunctionInternal");CHKERRQ(ierr); 5716e650e774SBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5717e650e774SBarry Smith mxDestroyArray(prhs[0]); 5718e650e774SBarry Smith mxDestroyArray(prhs[1]); 5719e650e774SBarry Smith mxDestroyArray(prhs[2]); 57208f6e6473SBarry Smith mxDestroyArray(prhs[3]); 5721e650e774SBarry Smith mxDestroyArray(plhs[0]); 57220807856dSBarry Smith PetscFunctionReturn(0); 57230807856dSBarry Smith } 57240807856dSBarry Smith 572561b2408cSBarry Smith /* 57260807856dSBarry Smith SNESSetFunctionMatlab - Sets the function evaluation routine and function 57270807856dSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 5728e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 57290807856dSBarry Smith 57300807856dSBarry Smith Logically Collective on SNES 57310807856dSBarry Smith 57320807856dSBarry Smith Input Parameters: 57330807856dSBarry Smith + snes - the SNES context 57340807856dSBarry Smith . r - vector to store function value 5735f8b49ee9SBarry Smith - f - function evaluation routine 57360807856dSBarry Smith 57370807856dSBarry Smith Notes: 57380807856dSBarry Smith The Newton-like methods typically solve linear systems of the form 57390807856dSBarry Smith $ f'(x) x = -f(x), 57400807856dSBarry Smith where f'(x) denotes the Jacobian matrix and f(x) is the function. 57410807856dSBarry Smith 57420807856dSBarry Smith Level: beginner 57430807856dSBarry Smith 5744c5b75c40SBarry Smith Developer Note: This bleeds the allocated memory SNESMatlabContext *sctx; 5745c5b75c40SBarry Smith 57460807856dSBarry Smith .keywords: SNES, nonlinear, set, function 57470807856dSBarry Smith 57480807856dSBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 574961b2408cSBarry Smith */ 5750f8b49ee9SBarry Smith PetscErrorCode SNESSetFunctionMatlab(SNES snes,Vec r,const char *f,mxArray *ctx) 57510807856dSBarry Smith { 57520807856dSBarry Smith PetscErrorCode ierr; 57538f6e6473SBarry Smith SNESMatlabContext *sctx; 57540807856dSBarry Smith 57550807856dSBarry Smith PetscFunctionBegin; 57568f6e6473SBarry Smith /* currently sctx is memory bleed */ 5757854ce69bSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 5758f8b49ee9SBarry Smith ierr = PetscStrallocpy(f,&sctx->funcname);CHKERRQ(ierr); 57598f6e6473SBarry Smith /* 57608f6e6473SBarry Smith This should work, but it doesn't 57618f6e6473SBarry Smith sctx->ctx = ctx; 57628f6e6473SBarry Smith mexMakeArrayPersistent(sctx->ctx); 57638f6e6473SBarry Smith */ 57648f6e6473SBarry Smith sctx->ctx = mxDuplicateArray(ctx); 57658f6e6473SBarry Smith ierr = SNESSetFunction(snes,r,SNESComputeFunction_Matlab,sctx);CHKERRQ(ierr); 57660807856dSBarry Smith PetscFunctionReturn(0); 57670807856dSBarry Smith } 576869b4f73cSBarry Smith 576961b2408cSBarry Smith /* 5770bf388a1fSBarry Smith SNESComputeJacobian_Matlab - Calls the function that has been set with SNESSetJacobianMatlab(). 577161b2408cSBarry Smith 577261b2408cSBarry Smith Collective on SNES 577361b2408cSBarry Smith 577461b2408cSBarry Smith Input Parameters: 577561b2408cSBarry Smith + snes - the SNES context 577661b2408cSBarry Smith . x - input vector 577761b2408cSBarry Smith . A, B - the matrices 577861b2408cSBarry Smith - ctx - user context 577961b2408cSBarry Smith 578061b2408cSBarry Smith Level: developer 578161b2408cSBarry Smith 578261b2408cSBarry Smith .keywords: SNES, nonlinear, compute, function 578361b2408cSBarry Smith 578461b2408cSBarry Smith .seealso: SNESSetFunction(), SNESGetFunction() 578561b2408cSBarry Smith @*/ 5786f3229a78SSatish Balay PetscErrorCode SNESComputeJacobian_Matlab(SNES snes,Vec x,Mat A,Mat B,void *ctx) 578761b2408cSBarry Smith { 578861b2408cSBarry Smith PetscErrorCode ierr; 578961b2408cSBarry Smith SNESMatlabContext *sctx = (SNESMatlabContext*)ctx; 579061b2408cSBarry Smith int nlhs = 2,nrhs = 6; 579161b2408cSBarry Smith mxArray *plhs[2],*prhs[6]; 579261b2408cSBarry Smith long long int lx = 0,lA = 0,ls = 0, lB = 0; 579361b2408cSBarry Smith 579461b2408cSBarry Smith PetscFunctionBegin; 579561b2408cSBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 579661b2408cSBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 579761b2408cSBarry Smith 579861b2408cSBarry Smith /* call Matlab function in ctx with arguments x and y */ 579961b2408cSBarry Smith 580061b2408cSBarry Smith ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 580161b2408cSBarry Smith ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 580261b2408cSBarry Smith ierr = PetscMemcpy(&lA,A,sizeof(x));CHKERRQ(ierr); 580361b2408cSBarry Smith ierr = PetscMemcpy(&lB,B,sizeof(x));CHKERRQ(ierr); 580461b2408cSBarry Smith prhs[0] = mxCreateDoubleScalar((double)ls); 580561b2408cSBarry Smith prhs[1] = mxCreateDoubleScalar((double)lx); 580661b2408cSBarry Smith prhs[2] = mxCreateDoubleScalar((double)lA); 580761b2408cSBarry Smith prhs[3] = mxCreateDoubleScalar((double)lB); 580861b2408cSBarry Smith prhs[4] = mxCreateString(sctx->funcname); 580961b2408cSBarry Smith prhs[5] = sctx->ctx; 5810b807a863SBarry Smith ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESComputeJacobianInternal");CHKERRQ(ierr); 581161b2408cSBarry Smith ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 581261b2408cSBarry Smith mxDestroyArray(prhs[0]); 581361b2408cSBarry Smith mxDestroyArray(prhs[1]); 581461b2408cSBarry Smith mxDestroyArray(prhs[2]); 581561b2408cSBarry Smith mxDestroyArray(prhs[3]); 581661b2408cSBarry Smith mxDestroyArray(prhs[4]); 581761b2408cSBarry Smith mxDestroyArray(plhs[0]); 581861b2408cSBarry Smith mxDestroyArray(plhs[1]); 581961b2408cSBarry Smith PetscFunctionReturn(0); 582061b2408cSBarry Smith } 582161b2408cSBarry Smith 582261b2408cSBarry Smith /* 582361b2408cSBarry Smith SNESSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices 582461b2408cSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 5825e3c5b3baSBarry Smith equations from MATLAB. Here the function is a string containing the name of a MATLAB function 582661b2408cSBarry Smith 582761b2408cSBarry Smith Logically Collective on SNES 582861b2408cSBarry Smith 582961b2408cSBarry Smith Input Parameters: 583061b2408cSBarry Smith + snes - the SNES context 583161b2408cSBarry Smith . A,B - Jacobian matrices 5832f8b49ee9SBarry Smith . J - function evaluation routine 583361b2408cSBarry Smith - ctx - user context 583461b2408cSBarry Smith 583561b2408cSBarry Smith Level: developer 583661b2408cSBarry Smith 5837c5b75c40SBarry Smith Developer Note: This bleeds the allocated memory SNESMatlabContext *sctx; 5838c5b75c40SBarry Smith 583961b2408cSBarry Smith .keywords: SNES, nonlinear, set, function 584061b2408cSBarry Smith 5841f8b49ee9SBarry Smith .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction(), J 584261b2408cSBarry Smith */ 5843f8b49ee9SBarry Smith PetscErrorCode SNESSetJacobianMatlab(SNES snes,Mat A,Mat B,const char *J,mxArray *ctx) 584461b2408cSBarry Smith { 584561b2408cSBarry Smith PetscErrorCode ierr; 584661b2408cSBarry Smith SNESMatlabContext *sctx; 584761b2408cSBarry Smith 584861b2408cSBarry Smith PetscFunctionBegin; 584961b2408cSBarry Smith /* currently sctx is memory bleed */ 5850854ce69bSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 5851f8b49ee9SBarry Smith ierr = PetscStrallocpy(J,&sctx->funcname);CHKERRQ(ierr); 585261b2408cSBarry Smith /* 585361b2408cSBarry Smith This should work, but it doesn't 585461b2408cSBarry Smith sctx->ctx = ctx; 585561b2408cSBarry Smith mexMakeArrayPersistent(sctx->ctx); 585661b2408cSBarry Smith */ 585761b2408cSBarry Smith sctx->ctx = mxDuplicateArray(ctx); 585861b2408cSBarry Smith ierr = SNESSetJacobian(snes,A,B,SNESComputeJacobian_Matlab,sctx);CHKERRQ(ierr); 585961b2408cSBarry Smith PetscFunctionReturn(0); 586061b2408cSBarry Smith } 586169b4f73cSBarry Smith 5862f9eb7ae2SShri Abhyankar /* 5863f9eb7ae2SShri Abhyankar SNESMonitor_Matlab - Calls the function that has been set with SNESMonitorSetMatlab(). 5864f9eb7ae2SShri Abhyankar 5865f9eb7ae2SShri Abhyankar Collective on SNES 5866f9eb7ae2SShri Abhyankar 5867f9eb7ae2SShri Abhyankar .seealso: SNESSetFunction(), SNESGetFunction() 5868f9eb7ae2SShri Abhyankar @*/ 58697087cfbeSBarry Smith PetscErrorCode SNESMonitor_Matlab(SNES snes,PetscInt it, PetscReal fnorm, void *ctx) 5870f9eb7ae2SShri Abhyankar { 5871f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 587248f37e70SShri Abhyankar SNESMatlabContext *sctx = (SNESMatlabContext*)ctx; 5873f9eb7ae2SShri Abhyankar int nlhs = 1,nrhs = 6; 5874f9eb7ae2SShri Abhyankar mxArray *plhs[1],*prhs[6]; 5875f9eb7ae2SShri Abhyankar long long int lx = 0,ls = 0; 5876f9eb7ae2SShri Abhyankar Vec x = snes->vec_sol; 5877f9eb7ae2SShri Abhyankar 5878f9eb7ae2SShri Abhyankar PetscFunctionBegin; 5879f9eb7ae2SShri Abhyankar PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5880f9eb7ae2SShri Abhyankar 5881f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr); 5882f9eb7ae2SShri Abhyankar ierr = PetscMemcpy(&lx,&x,sizeof(x));CHKERRQ(ierr); 5883f9eb7ae2SShri Abhyankar prhs[0] = mxCreateDoubleScalar((double)ls); 5884f9eb7ae2SShri Abhyankar prhs[1] = mxCreateDoubleScalar((double)it); 5885f9eb7ae2SShri Abhyankar prhs[2] = mxCreateDoubleScalar((double)fnorm); 5886f9eb7ae2SShri Abhyankar prhs[3] = mxCreateDoubleScalar((double)lx); 5887f9eb7ae2SShri Abhyankar prhs[4] = mxCreateString(sctx->funcname); 5888f9eb7ae2SShri Abhyankar prhs[5] = sctx->ctx; 5889f9eb7ae2SShri Abhyankar ierr = mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESMonitorInternal");CHKERRQ(ierr); 5890f9eb7ae2SShri Abhyankar ierr = mxGetScalar(plhs[0]);CHKERRQ(ierr); 5891f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[0]); 5892f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[1]); 5893f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[2]); 5894f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[3]); 5895f9eb7ae2SShri Abhyankar mxDestroyArray(prhs[4]); 5896f9eb7ae2SShri Abhyankar mxDestroyArray(plhs[0]); 5897f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 5898f9eb7ae2SShri Abhyankar } 5899f9eb7ae2SShri Abhyankar 5900f9eb7ae2SShri Abhyankar /* 5901e3c5b3baSBarry Smith SNESMonitorSetMatlab - Sets the monitor function from MATLAB 5902f9eb7ae2SShri Abhyankar 5903f9eb7ae2SShri Abhyankar Level: developer 5904f9eb7ae2SShri Abhyankar 5905c5b75c40SBarry Smith Developer Note: This bleeds the allocated memory SNESMatlabContext *sctx; 5906c5b75c40SBarry Smith 5907f9eb7ae2SShri Abhyankar .keywords: SNES, nonlinear, set, function 5908f9eb7ae2SShri Abhyankar 5909f9eb7ae2SShri Abhyankar .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 5910f9eb7ae2SShri Abhyankar */ 59116e4dcb14SBarry Smith PetscErrorCode SNESMonitorSetMatlab(SNES snes,const char *f,mxArray *ctx) 5912f9eb7ae2SShri Abhyankar { 5913f9eb7ae2SShri Abhyankar PetscErrorCode ierr; 5914f9eb7ae2SShri Abhyankar SNESMatlabContext *sctx; 5915f9eb7ae2SShri Abhyankar 5916f9eb7ae2SShri Abhyankar PetscFunctionBegin; 5917f9eb7ae2SShri Abhyankar /* currently sctx is memory bleed */ 5918854ce69bSBarry Smith ierr = PetscNew(&sctx);CHKERRQ(ierr); 59196e4dcb14SBarry Smith ierr = PetscStrallocpy(f,&sctx->funcname);CHKERRQ(ierr); 5920f9eb7ae2SShri Abhyankar /* 5921f9eb7ae2SShri Abhyankar This should work, but it doesn't 5922f9eb7ae2SShri Abhyankar sctx->ctx = ctx; 5923f9eb7ae2SShri Abhyankar mexMakeArrayPersistent(sctx->ctx); 5924f9eb7ae2SShri Abhyankar */ 5925f9eb7ae2SShri Abhyankar sctx->ctx = mxDuplicateArray(ctx); 59260298fd71SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitor_Matlab,sctx,NULL);CHKERRQ(ierr); 5927f9eb7ae2SShri Abhyankar PetscFunctionReturn(0); 5928f9eb7ae2SShri Abhyankar } 5929f9eb7ae2SShri Abhyankar 593069b4f73cSBarry Smith #endif 5931