163dd3a1aSKris Buschelman #define PETSCSNES_DLL 29b94acceSBarry Smith 37c4f633dSBarry Smith #include "private/snesimpl.h" /*I "petscsnes.h" I*/ 49b94acceSBarry Smith 54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE; 68ba1e511SMatthew Knepley PetscFList SNESList = PETSC_NULL; 78ba1e511SMatthew Knepley 88ba1e511SMatthew Knepley /* Logging support */ 9*0700a824SBarry Smith PetscClassId PETSCSNES_DLLEXPORT SNES_CLASSID; 10166c7f25SBarry Smith PetscLogEvent SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval; 11a09944afSBarry Smith 12a09944afSBarry Smith #undef __FUNCT__ 134936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError" 14e725d27bSBarry Smith /*@ 154936397dSBarry Smith SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not 164936397dSBarry Smith in the functions domain. For example, negative pressure. 174936397dSBarry Smith 184936397dSBarry Smith Collective on SNES 194936397dSBarry Smith 204936397dSBarry Smith Input Parameters: 214936397dSBarry Smith . SNES - the SNES context 224936397dSBarry Smith 2328529972SSatish Balay Level: advanced 244936397dSBarry Smith 254936397dSBarry Smith .keywords: SNES, view 264936397dSBarry Smith 274936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction() 284936397dSBarry Smith @*/ 294936397dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunctionDomainError(SNES snes) 304936397dSBarry Smith { 314936397dSBarry Smith PetscFunctionBegin; 32*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 334936397dSBarry Smith snes->domainerror = PETSC_TRUE; 344936397dSBarry Smith PetscFunctionReturn(0); 354936397dSBarry Smith } 364936397dSBarry Smith 374936397dSBarry Smith #undef __FUNCT__ 384a2ae208SSatish Balay #define __FUNCT__ "SNESView" 397e2c5f70SBarry Smith /*@C 409b94acceSBarry Smith SNESView - Prints the SNES data structure. 419b94acceSBarry Smith 424c49b128SBarry Smith Collective on SNES 43fee21e36SBarry Smith 44c7afd0dbSLois Curfman McInnes Input Parameters: 45c7afd0dbSLois Curfman McInnes + SNES - the SNES context 46c7afd0dbSLois Curfman McInnes - viewer - visualization context 47c7afd0dbSLois Curfman McInnes 489b94acceSBarry Smith Options Database Key: 49c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 509b94acceSBarry Smith 519b94acceSBarry Smith Notes: 529b94acceSBarry Smith The available visualization contexts include 53b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 54b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 55c8a8ba5cSLois Curfman McInnes output where only the first processor opens 56c8a8ba5cSLois Curfman McInnes the file. All other processors send their 57c8a8ba5cSLois Curfman McInnes data to the first processor to print. 589b94acceSBarry Smith 593e081fefSLois Curfman McInnes The user can open an alternative visualization context with 60b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 619b94acceSBarry Smith 6236851e7fSLois Curfman McInnes Level: beginner 6336851e7fSLois Curfman McInnes 649b94acceSBarry Smith .keywords: SNES, view 659b94acceSBarry Smith 66b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 679b94acceSBarry Smith @*/ 6863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer) 699b94acceSBarry Smith { 70fa9f3622SBarry Smith SNESKSPEW *kctx; 71dfbe8321SBarry Smith PetscErrorCode ierr; 7294b7f48cSBarry Smith KSP ksp; 73a313700dSBarry Smith const SNESType type; 7432077d6dSBarry Smith PetscTruth iascii,isstring; 759b94acceSBarry Smith 763a40ed3dSBarry Smith PetscFunctionBegin; 77*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 783050cee2SBarry Smith if (!viewer) { 797adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr); 803050cee2SBarry Smith } 81*0700a824SBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 82c9780b6fSBarry Smith PetscCheckSameComm(snes,1,viewer,2); 8374679c65SBarry Smith 8432077d6dSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 85b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr); 8632077d6dSBarry Smith if (iascii) { 877adad957SLisandro Dalcin if (((PetscObject)snes)->prefix) { 887adad957SLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",((PetscObject)snes)->prefix);CHKERRQ(ierr); 893a7fca6bSBarry Smith } else { 90b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr); 913a7fca6bSBarry Smith } 92454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 93454a90a3SBarry Smith if (type) { 94b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," type: %s\n",type);CHKERRQ(ierr); 95184914b5SBarry Smith } else { 96b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," type: not set yet\n");CHKERRQ(ierr); 97184914b5SBarry Smith } 98e7788613SBarry Smith if (snes->ops->view) { 99b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 100e7788613SBarry Smith ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr); 101b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1020ef38995SBarry Smith } 10377431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 104a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, solution=%G\n", 10570441072SBarry Smith snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr); 10677431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr); 10777431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr); 1089b94acceSBarry Smith if (snes->ksp_ewconv) { 109fa9f3622SBarry Smith kctx = (SNESKSPEW *)snes->kspconvctx; 1109b94acceSBarry Smith if (kctx) { 11177431f27SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr); 112a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 113a83599f4SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 1149b94acceSBarry Smith } 1159b94acceSBarry Smith } 116eb1f6c34SBarry Smith if (snes->lagpreconditioner == -1) { 117eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is never rebuilt\n");CHKERRQ(ierr); 118eb1f6c34SBarry Smith } else if (snes->lagpreconditioner > 1) { 119eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr); 120eb1f6c34SBarry Smith } 121eb1f6c34SBarry Smith if (snes->lagjacobian == -1) { 122eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is never rebuilt\n");CHKERRQ(ierr); 123eb1f6c34SBarry Smith } else if (snes->lagjacobian > 1) { 124eb1f6c34SBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Jacobian is rebuilt every %D Newton iterations\n",snes->lagjacobian);CHKERRQ(ierr); 125eb1f6c34SBarry Smith } 1260f5bd95cSBarry Smith } else if (isstring) { 127454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 128b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 12919bcc07fSBarry Smith } 13094b7f48cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 131b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 13294b7f48cSBarry Smith ierr = KSPView(ksp,viewer);CHKERRQ(ierr); 133b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1343a40ed3dSBarry Smith PetscFunctionReturn(0); 1359b94acceSBarry Smith } 1369b94acceSBarry Smith 13776b2cf59SMatthew Knepley /* 13876b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 13976b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 14076b2cf59SMatthew Knepley */ 14176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 142a7cc72afSBarry Smith static PetscInt numberofsetfromoptions; 1436849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 14476b2cf59SMatthew Knepley 145e74ef692SMatthew Knepley #undef __FUNCT__ 146e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker" 147ac226902SBarry Smith /*@C 14876b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 14976b2cf59SMatthew Knepley 15076b2cf59SMatthew Knepley Not Collective 15176b2cf59SMatthew Knepley 15276b2cf59SMatthew Knepley Input Parameter: 15376b2cf59SMatthew Knepley . snescheck - function that checks for options 15476b2cf59SMatthew Knepley 15576b2cf59SMatthew Knepley Level: developer 15676b2cf59SMatthew Knepley 15776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions() 15876b2cf59SMatthew Knepley @*/ 15963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES)) 16076b2cf59SMatthew Knepley { 16176b2cf59SMatthew Knepley PetscFunctionBegin; 16276b2cf59SMatthew Knepley if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 16377431f27SBarry Smith SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS); 16476b2cf59SMatthew Knepley } 16576b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 16676b2cf59SMatthew Knepley PetscFunctionReturn(0); 16776b2cf59SMatthew Knepley } 16876b2cf59SMatthew Knepley 169aa3661deSLisandro Dalcin EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*); 170aa3661deSLisandro Dalcin 171aa3661deSLisandro Dalcin #undef __FUNCT__ 172aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private" 173d3462f78SMatthew Knepley static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscTruth hasOperator, PetscInt version) 174aa3661deSLisandro Dalcin { 175aa3661deSLisandro Dalcin Mat J; 176aa3661deSLisandro Dalcin KSP ksp; 177aa3661deSLisandro Dalcin PC pc; 178aa3661deSLisandro Dalcin PetscTruth match; 179aa3661deSLisandro Dalcin PetscErrorCode ierr; 180aa3661deSLisandro Dalcin 181aa3661deSLisandro Dalcin PetscFunctionBegin; 182*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 183aa3661deSLisandro Dalcin 18498613b67SLisandro Dalcin if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) { 18598613b67SLisandro Dalcin Mat A = snes->jacobian, B = snes->jacobian_pre; 18698613b67SLisandro Dalcin ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr); 18798613b67SLisandro Dalcin } 18898613b67SLisandro Dalcin 189aa3661deSLisandro Dalcin if (version == 1) { 190aa3661deSLisandro Dalcin ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr); 19198613b67SLisandro Dalcin ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr); 192aa3661deSLisandro Dalcin ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr); 193aa3661deSLisandro Dalcin } else if (version == 2) { 194aa3661deSLisandro Dalcin if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first"); 19565460251SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE) && !defined(PETSC_USE_SCALAR_LONG_DOUBLE) && !defined(PETSC_USE_SCALAR_INT) 196aa3661deSLisandro Dalcin ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr); 197aa3661deSLisandro Dalcin #else 198aa3661deSLisandro Dalcin SETERRQ(PETSC_ERR_SUP, "matrix-free operator rutines (version 2)"); 199aa3661deSLisandro Dalcin #endif 200aa3661deSLisandro Dalcin } else { 201aa3661deSLisandro Dalcin SETERRQ(PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2"); 202aa3661deSLisandro Dalcin } 203aa3661deSLisandro Dalcin 204aa3661deSLisandro Dalcin ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr); 205d3462f78SMatthew Knepley if (hasOperator) { 206aa3661deSLisandro Dalcin /* This version replaces the user provided Jacobian matrix with a 207aa3661deSLisandro Dalcin matrix-free version but still employs the user-provided preconditioner matrix. */ 208aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 209aa3661deSLisandro Dalcin } else { 210aa3661deSLisandro Dalcin /* This version replaces both the user-provided Jacobian and the user- 211aa3661deSLisandro Dalcin provided preconditioner matrix with the default matrix free version. */ 212aa3661deSLisandro Dalcin ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr); 213aa3661deSLisandro Dalcin /* Force no preconditioner */ 214aa3661deSLisandro Dalcin ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 215aa3661deSLisandro Dalcin ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); 216aa3661deSLisandro Dalcin ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr); 217aa3661deSLisandro Dalcin if (!match) { 218aa3661deSLisandro Dalcin ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr); 219aa3661deSLisandro Dalcin ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 220aa3661deSLisandro Dalcin } 221aa3661deSLisandro Dalcin } 222aa3661deSLisandro Dalcin ierr = MatDestroy(J);CHKERRQ(ierr); 223aa3661deSLisandro Dalcin 224aa3661deSLisandro Dalcin PetscFunctionReturn(0); 225aa3661deSLisandro Dalcin } 226aa3661deSLisandro Dalcin 2274a2ae208SSatish Balay #undef __FUNCT__ 2284a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions" 2299b94acceSBarry Smith /*@ 23094b7f48cSBarry Smith SNESSetFromOptions - Sets various SNES and KSP parameters from user options. 2319b94acceSBarry Smith 232c7afd0dbSLois Curfman McInnes Collective on SNES 233c7afd0dbSLois Curfman McInnes 2349b94acceSBarry Smith Input Parameter: 2359b94acceSBarry Smith . snes - the SNES context 2369b94acceSBarry Smith 23736851e7fSLois Curfman McInnes Options Database Keys: 2386831982aSBarry Smith + -snes_type <type> - ls, tr, umls, umtr, test 23982738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 24082738288SBarry Smith of the change in the solution between steps 24170441072SBarry Smith . -snes_atol <abstol> - absolute tolerance of residual norm 242b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 243b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 244b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 24550ffb88aSMatthew Knepley . -snes_max_fail <max_fail> - maximum number of failures 246ddf469c8SBarry Smith . -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops 247a8054027SBarry Smith . -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild) 248e35cf81dSBarry Smith . -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild) 249b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 2502492ecdbSBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear 25182738288SBarry Smith solver; hence iterations will continue until max_it 2521fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 25382738288SBarry Smith of convergence test 254e8105e01SRichard Katz . -snes_monitor <optional filename> - prints residual norm at each iteration. if no 255e8105e01SRichard Katz filename given prints to stdout 256a6570f20SBarry Smith . -snes_monitor_solution - plots solution at each iteration 257a6570f20SBarry Smith . -snes_monitor_residual - plots residual (not its norm) at each iteration 258a6570f20SBarry Smith . -snes_monitor_solution_update - plots update to solution at each iteration 259a6570f20SBarry Smith . -snes_monitor_draw - plots residual norm at each iteration 260e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 2615968eb51SBarry Smith . -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 262fee2055bSBarry Smith - -snes_converged_reason - print the reason for convergence/divergence after each solve 26382738288SBarry Smith 26482738288SBarry Smith Options Database for Eisenstat-Walker method: 265fa9f3622SBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 2664b27c08aSLois Curfman McInnes . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 26736851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 26836851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 26936851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 27036851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 27136851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 27236851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 27382738288SBarry Smith 27411ca99fdSLois Curfman McInnes Notes: 27511ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 27611ca99fdSLois Curfman McInnes the users manual. 27783e2fdc7SBarry Smith 27836851e7fSLois Curfman McInnes Level: beginner 27936851e7fSLois Curfman McInnes 2809b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 2819b94acceSBarry Smith 28269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 2839b94acceSBarry Smith @*/ 28463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes) 2859b94acceSBarry Smith { 286aa3661deSLisandro Dalcin PetscTruth flg,mf,mf_operator; 287aa3661deSLisandro Dalcin PetscInt i,indx,lag,mf_version; 288aa3661deSLisandro Dalcin MatStructure matflag; 28985385478SLisandro Dalcin const char *deft = SNESLS; 29085385478SLisandro Dalcin const char *convtests[] = {"default","skip"}; 29185385478SLisandro Dalcin SNESKSPEW *kctx = NULL; 292e8105e01SRichard Katz char type[256], monfilename[PETSC_MAX_PATH_LEN]; 29323d894e5SBarry Smith PetscViewerASCIIMonitor monviewer; 29485385478SLisandro Dalcin PetscErrorCode ierr; 2959b94acceSBarry Smith 2963a40ed3dSBarry Smith PetscFunctionBegin; 297*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 298ca161407SBarry Smith 299186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 300cce0b1b2SLisandro Dalcin ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr); 3017adad957SLisandro Dalcin if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; } 302b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 303d64ed03dSBarry Smith if (flg) { 304186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 3057adad957SLisandro Dalcin } else if (!((PetscObject)snes)->type_name) { 306186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 307d64ed03dSBarry Smith } 30890d69ab7SBarry Smith /* not used here, but called so will go into help messaage */ 309909c8a9fSBarry Smith ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr); 31093c39befSBarry Smith 31157034d6fSHong Zhang ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 31257034d6fSHong Zhang ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr); 313186905e3SBarry Smith 31457034d6fSHong Zhang ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 315b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 316b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 31750ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 318ddf469c8SBarry Smith ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr); 31985385478SLisandro Dalcin 320a8054027SBarry Smith ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr); 321a8054027SBarry Smith if (flg) { 322a8054027SBarry Smith ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr); 323a8054027SBarry Smith } 324e35cf81dSBarry Smith ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr); 325e35cf81dSBarry Smith if (flg) { 326e35cf81dSBarry Smith ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr); 327e35cf81dSBarry Smith } 328a8054027SBarry Smith 32985385478SLisandro Dalcin ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr); 33085385478SLisandro Dalcin if (flg) { 33185385478SLisandro Dalcin switch (indx) { 3327f7931b9SBarry Smith case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 3337f7931b9SBarry Smith case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break; 33485385478SLisandro Dalcin } 33585385478SLisandro Dalcin } 33685385478SLisandro Dalcin 33790d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr); 338186905e3SBarry Smith 33985385478SLisandro Dalcin kctx = (SNESKSPEW *)snes->kspconvctx; 34085385478SLisandro Dalcin 341fa9f3622SBarry Smith ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr); 342186905e3SBarry Smith 343fa9f3622SBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 344fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 345fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 346fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 347fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 348fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 349fa9f3622SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 350186905e3SBarry Smith 35190d69ab7SBarry Smith flg = PETSC_FALSE; 35290d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 353a6570f20SBarry Smith if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);} 354eabae89aSBarry Smith 355a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 356e8105e01SRichard Katz if (flg) { 357050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 35823d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 359e8105e01SRichard Katz } 360eabae89aSBarry Smith 361b271bb04SBarry Smith ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 362b271bb04SBarry Smith if (flg) { 363b271bb04SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr); 364b271bb04SBarry Smith } 365b271bb04SBarry Smith 366a6570f20SBarry Smith ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 367eabae89aSBarry Smith if (flg) { 368050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 369f1bef1bcSMatthew Knepley ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr); 370e8105e01SRichard Katz } 371eabae89aSBarry Smith 372a6570f20SBarry Smith ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 373eabae89aSBarry Smith if (flg) { 374050a712dSBarry Smith ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr); 37523d894e5SBarry Smith ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr); 376eabae89aSBarry Smith } 377eabae89aSBarry Smith 37890d69ab7SBarry Smith flg = PETSC_FALSE; 37990d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 380a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);} 38190d69ab7SBarry Smith flg = PETSC_FALSE; 38290d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 383a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);} 38490d69ab7SBarry Smith flg = PETSC_FALSE; 38590d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 386a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);} 38790d69ab7SBarry Smith flg = PETSC_FALSE; 38890d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 389a6570f20SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 39090d69ab7SBarry Smith flg = PETSC_FALSE; 39190d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 392b271bb04SBarry Smith if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 393e24b481bSBarry Smith 39490d69ab7SBarry Smith flg = PETSC_FALSE; 39590d69ab7SBarry Smith ierr = PetscOptionsTruth("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr); 3964b27c08aSLois Curfman McInnes if (flg) { 397186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 398ae15b995SBarry Smith ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr); 3999b94acceSBarry Smith } 400639f9d9dSBarry Smith 401aa3661deSLisandro Dalcin mf = mf_operator = PETSC_FALSE; 402aa3661deSLisandro Dalcin flg = PETSC_FALSE; 403aa3661deSLisandro Dalcin ierr = PetscOptionsTruth("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr); 404aa3661deSLisandro Dalcin if (flg && mf_operator) mf = PETSC_TRUE; 405aa3661deSLisandro Dalcin flg = PETSC_FALSE; 406aa3661deSLisandro Dalcin ierr = PetscOptionsTruth("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr); 407aa3661deSLisandro Dalcin if (!flg && mf_operator) mf = PETSC_TRUE; 408aa3661deSLisandro Dalcin mf_version = 1; 409aa3661deSLisandro Dalcin ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr); 410aa3661deSLisandro Dalcin 41176b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 41276b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 41376b2cf59SMatthew Knepley } 41476b2cf59SMatthew Knepley 415e7788613SBarry Smith if (snes->ops->setfromoptions) { 416e7788613SBarry Smith ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr); 417639f9d9dSBarry Smith } 418b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 4194bbc92c1SBarry Smith 420aa3661deSLisandro Dalcin if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); } 4211cee3971SBarry Smith 4221cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 423aa3661deSLisandro Dalcin ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag); 424aa3661deSLisandro Dalcin ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr); 42585385478SLisandro Dalcin ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr); 42693993e2dSLois Curfman McInnes 4273a40ed3dSBarry Smith PetscFunctionReturn(0); 4289b94acceSBarry Smith } 4299b94acceSBarry Smith 430a847f771SSatish Balay 4314a2ae208SSatish Balay #undef __FUNCT__ 4324a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 4339b94acceSBarry Smith /*@ 4349b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 4359b94acceSBarry Smith the nonlinear solvers. 4369b94acceSBarry Smith 437fee21e36SBarry Smith Collective on SNES 438fee21e36SBarry Smith 439c7afd0dbSLois Curfman McInnes Input Parameters: 440c7afd0dbSLois Curfman McInnes + snes - the SNES context 441c7afd0dbSLois Curfman McInnes - usrP - optional user context 442c7afd0dbSLois Curfman McInnes 44336851e7fSLois Curfman McInnes Level: intermediate 44436851e7fSLois Curfman McInnes 4459b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 4469b94acceSBarry Smith 4479b94acceSBarry Smith .seealso: SNESGetApplicationContext() 4489b94acceSBarry Smith @*/ 44963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP) 4509b94acceSBarry Smith { 4513a40ed3dSBarry Smith PetscFunctionBegin; 452*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4539b94acceSBarry Smith snes->user = usrP; 4543a40ed3dSBarry Smith PetscFunctionReturn(0); 4559b94acceSBarry Smith } 45674679c65SBarry Smith 4574a2ae208SSatish Balay #undef __FUNCT__ 4584a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 4599b94acceSBarry Smith /*@C 4609b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 4619b94acceSBarry Smith nonlinear solvers. 4629b94acceSBarry Smith 463c7afd0dbSLois Curfman McInnes Not Collective 464c7afd0dbSLois Curfman McInnes 4659b94acceSBarry Smith Input Parameter: 4669b94acceSBarry Smith . snes - SNES context 4679b94acceSBarry Smith 4689b94acceSBarry Smith Output Parameter: 4699b94acceSBarry Smith . usrP - user context 4709b94acceSBarry Smith 47136851e7fSLois Curfman McInnes Level: intermediate 47236851e7fSLois Curfman McInnes 4739b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 4749b94acceSBarry Smith 4759b94acceSBarry Smith .seealso: SNESSetApplicationContext() 4769b94acceSBarry Smith @*/ 47763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP) 4789b94acceSBarry Smith { 4793a40ed3dSBarry Smith PetscFunctionBegin; 480*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 4819b94acceSBarry Smith *usrP = snes->user; 4823a40ed3dSBarry Smith PetscFunctionReturn(0); 4839b94acceSBarry Smith } 48474679c65SBarry Smith 4854a2ae208SSatish Balay #undef __FUNCT__ 4864a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 4879b94acceSBarry Smith /*@ 488c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 489c8228a4eSBarry Smith at this time. 4909b94acceSBarry Smith 491c7afd0dbSLois Curfman McInnes Not Collective 492c7afd0dbSLois Curfman McInnes 4939b94acceSBarry Smith Input Parameter: 4949b94acceSBarry Smith . snes - SNES context 4959b94acceSBarry Smith 4969b94acceSBarry Smith Output Parameter: 4979b94acceSBarry Smith . iter - iteration number 4989b94acceSBarry Smith 499c8228a4eSBarry Smith Notes: 500c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 501c8228a4eSBarry Smith 502c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 50308405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 50408405cd6SLois Curfman McInnes .vb 50508405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 50608405cd6SLois Curfman McInnes if (!(it % 2)) { 50708405cd6SLois Curfman McInnes [compute Jacobian here] 50808405cd6SLois Curfman McInnes } 50908405cd6SLois Curfman McInnes .ve 510c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 51108405cd6SLois Curfman McInnes recomputed every second SNES iteration. 512c8228a4eSBarry Smith 51336851e7fSLois Curfman McInnes Level: intermediate 51436851e7fSLois Curfman McInnes 5152b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number, 5162b668275SBarry Smith 517b850b91aSLisandro Dalcin .seealso: SNESGetFunctionNorm(), SNESGetLinearSolveIterations() 5189b94acceSBarry Smith @*/ 51963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter) 5209b94acceSBarry Smith { 5213a40ed3dSBarry Smith PetscFunctionBegin; 522*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5234482741eSBarry Smith PetscValidIntPointer(iter,2); 5249b94acceSBarry Smith *iter = snes->iter; 5253a40ed3dSBarry Smith PetscFunctionReturn(0); 5269b94acceSBarry Smith } 52774679c65SBarry Smith 5284a2ae208SSatish Balay #undef __FUNCT__ 5294a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 5309b94acceSBarry Smith /*@ 5319b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 5329b94acceSBarry Smith with SNESSSetFunction(). 5339b94acceSBarry Smith 534c7afd0dbSLois Curfman McInnes Collective on SNES 535c7afd0dbSLois Curfman McInnes 5369b94acceSBarry Smith Input Parameter: 5379b94acceSBarry Smith . snes - SNES context 5389b94acceSBarry Smith 5399b94acceSBarry Smith Output Parameter: 5409b94acceSBarry Smith . fnorm - 2-norm of function 5419b94acceSBarry Smith 54236851e7fSLois Curfman McInnes Level: intermediate 54336851e7fSLois Curfman McInnes 5449b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 545a86d99e1SLois Curfman McInnes 546b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations() 5479b94acceSBarry Smith @*/ 54871f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm) 5499b94acceSBarry Smith { 5503a40ed3dSBarry Smith PetscFunctionBegin; 551*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5524482741eSBarry Smith PetscValidScalarPointer(fnorm,2); 5539b94acceSBarry Smith *fnorm = snes->norm; 5543a40ed3dSBarry Smith PetscFunctionReturn(0); 5559b94acceSBarry Smith } 55674679c65SBarry Smith 5574a2ae208SSatish Balay #undef __FUNCT__ 558b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures" 5599b94acceSBarry Smith /*@ 560b850b91aSLisandro Dalcin SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps 5619b94acceSBarry Smith attempted by the nonlinear solver. 5629b94acceSBarry Smith 563c7afd0dbSLois Curfman McInnes Not Collective 564c7afd0dbSLois Curfman McInnes 5659b94acceSBarry Smith Input Parameter: 5669b94acceSBarry Smith . snes - SNES context 5679b94acceSBarry Smith 5689b94acceSBarry Smith Output Parameter: 5699b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 5709b94acceSBarry Smith 571c96a6f78SLois Curfman McInnes Notes: 572c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 573c96a6f78SLois Curfman McInnes 57436851e7fSLois Curfman McInnes Level: intermediate 57536851e7fSLois Curfman McInnes 5769b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 57758ebbce7SBarry Smith 578e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 57958ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures() 5809b94acceSBarry Smith @*/ 581b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails) 5829b94acceSBarry Smith { 5833a40ed3dSBarry Smith PetscFunctionBegin; 584*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 5854482741eSBarry Smith PetscValidIntPointer(nfails,2); 58650ffb88aSMatthew Knepley *nfails = snes->numFailures; 58750ffb88aSMatthew Knepley PetscFunctionReturn(0); 58850ffb88aSMatthew Knepley } 58950ffb88aSMatthew Knepley 59050ffb88aSMatthew Knepley #undef __FUNCT__ 591b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures" 59250ffb88aSMatthew Knepley /*@ 593b850b91aSLisandro Dalcin SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps 59450ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 59550ffb88aSMatthew Knepley 59650ffb88aSMatthew Knepley Not Collective 59750ffb88aSMatthew Knepley 59850ffb88aSMatthew Knepley Input Parameters: 59950ffb88aSMatthew Knepley + snes - SNES context 60050ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 60150ffb88aSMatthew Knepley 60250ffb88aSMatthew Knepley Level: intermediate 60350ffb88aSMatthew Knepley 60450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 60558ebbce7SBarry Smith 606e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 60758ebbce7SBarry Smith SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 60850ffb88aSMatthew Knepley @*/ 609b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails) 61050ffb88aSMatthew Knepley { 61150ffb88aSMatthew Knepley PetscFunctionBegin; 612*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 61350ffb88aSMatthew Knepley snes->maxFailures = maxFails; 61450ffb88aSMatthew Knepley PetscFunctionReturn(0); 61550ffb88aSMatthew Knepley } 61650ffb88aSMatthew Knepley 61750ffb88aSMatthew Knepley #undef __FUNCT__ 618b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures" 61950ffb88aSMatthew Knepley /*@ 620b850b91aSLisandro Dalcin SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps 62150ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 62250ffb88aSMatthew Knepley 62350ffb88aSMatthew Knepley Not Collective 62450ffb88aSMatthew Knepley 62550ffb88aSMatthew Knepley Input Parameter: 62650ffb88aSMatthew Knepley . snes - SNES context 62750ffb88aSMatthew Knepley 62850ffb88aSMatthew Knepley Output Parameter: 62950ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 63050ffb88aSMatthew Knepley 63150ffb88aSMatthew Knepley Level: intermediate 63250ffb88aSMatthew Knepley 63350ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 63458ebbce7SBarry Smith 635e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(), 63658ebbce7SBarry Smith SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures() 63758ebbce7SBarry Smith 63850ffb88aSMatthew Knepley @*/ 639b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails) 64050ffb88aSMatthew Knepley { 64150ffb88aSMatthew Knepley PetscFunctionBegin; 642*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6434482741eSBarry Smith PetscValidIntPointer(maxFails,2); 64450ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 6453a40ed3dSBarry Smith PetscFunctionReturn(0); 6469b94acceSBarry Smith } 647a847f771SSatish Balay 6484a2ae208SSatish Balay #undef __FUNCT__ 6492541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals" 6502541af92SBarry Smith /*@ 6512541af92SBarry Smith SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations 6522541af92SBarry Smith done by SNES. 6532541af92SBarry Smith 6542541af92SBarry Smith Not Collective 6552541af92SBarry Smith 6562541af92SBarry Smith Input Parameter: 6572541af92SBarry Smith . snes - SNES context 6582541af92SBarry Smith 6592541af92SBarry Smith Output Parameter: 6602541af92SBarry Smith . nfuncs - number of evaluations 6612541af92SBarry Smith 6622541af92SBarry Smith Level: intermediate 6632541af92SBarry Smith 6642541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 66558ebbce7SBarry Smith 666e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures() 6672541af92SBarry Smith @*/ 6682541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs) 6692541af92SBarry Smith { 6702541af92SBarry Smith PetscFunctionBegin; 671*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 6722541af92SBarry Smith PetscValidIntPointer(nfuncs,2); 6732541af92SBarry Smith *nfuncs = snes->nfuncs; 6742541af92SBarry Smith PetscFunctionReturn(0); 6752541af92SBarry Smith } 6762541af92SBarry Smith 6772541af92SBarry Smith #undef __FUNCT__ 6783d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures" 6793d4c4710SBarry Smith /*@ 6803d4c4710SBarry Smith SNESGetLinearSolveFailures - Gets the number of failed (non-converged) 6813d4c4710SBarry Smith linear solvers. 6823d4c4710SBarry Smith 6833d4c4710SBarry Smith Not Collective 6843d4c4710SBarry Smith 6853d4c4710SBarry Smith Input Parameter: 6863d4c4710SBarry Smith . snes - SNES context 6873d4c4710SBarry Smith 6883d4c4710SBarry Smith Output Parameter: 6893d4c4710SBarry Smith . nfails - number of failed solves 6903d4c4710SBarry Smith 6913d4c4710SBarry Smith Notes: 6923d4c4710SBarry Smith This counter is reset to zero for each successive call to SNESSolve(). 6933d4c4710SBarry Smith 6943d4c4710SBarry Smith Level: intermediate 6953d4c4710SBarry Smith 6963d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 69758ebbce7SBarry Smith 698e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures() 6993d4c4710SBarry Smith @*/ 7003d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails) 7013d4c4710SBarry Smith { 7023d4c4710SBarry Smith PetscFunctionBegin; 703*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7043d4c4710SBarry Smith PetscValidIntPointer(nfails,2); 7053d4c4710SBarry Smith *nfails = snes->numLinearSolveFailures; 7063d4c4710SBarry Smith PetscFunctionReturn(0); 7073d4c4710SBarry Smith } 7083d4c4710SBarry Smith 7093d4c4710SBarry Smith #undef __FUNCT__ 7103d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures" 7113d4c4710SBarry Smith /*@ 7123d4c4710SBarry Smith SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts 7133d4c4710SBarry Smith allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE 7143d4c4710SBarry Smith 7153d4c4710SBarry Smith Collective on SNES 7163d4c4710SBarry Smith 7173d4c4710SBarry Smith Input Parameters: 7183d4c4710SBarry Smith + snes - SNES context 7193d4c4710SBarry Smith - maxFails - maximum allowed linear solve failures 7203d4c4710SBarry Smith 7213d4c4710SBarry Smith Level: intermediate 7223d4c4710SBarry Smith 723a6796414SBarry Smith Notes: By default this is 0; that is SNES returns on the first failed linear solve 7243d4c4710SBarry Smith 7253d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 7263d4c4710SBarry Smith 72758ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations() 7283d4c4710SBarry Smith @*/ 7293d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails) 7303d4c4710SBarry Smith { 7313d4c4710SBarry Smith PetscFunctionBegin; 732*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7333d4c4710SBarry Smith snes->maxLinearSolveFailures = maxFails; 7343d4c4710SBarry Smith PetscFunctionReturn(0); 7353d4c4710SBarry Smith } 7363d4c4710SBarry Smith 7373d4c4710SBarry Smith #undef __FUNCT__ 7383d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures" 7393d4c4710SBarry Smith /*@ 7403d4c4710SBarry Smith SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that 7413d4c4710SBarry Smith are allowed before SNES terminates 7423d4c4710SBarry Smith 7433d4c4710SBarry Smith Not Collective 7443d4c4710SBarry Smith 7453d4c4710SBarry Smith Input Parameter: 7463d4c4710SBarry Smith . snes - SNES context 7473d4c4710SBarry Smith 7483d4c4710SBarry Smith Output Parameter: 7493d4c4710SBarry Smith . maxFails - maximum of unsuccessful solves allowed 7503d4c4710SBarry Smith 7513d4c4710SBarry Smith Level: intermediate 7523d4c4710SBarry Smith 7533d4c4710SBarry Smith Notes: By default this is 1; that is SNES returns on the first failed linear solve 7543d4c4710SBarry Smith 7553d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 7563d4c4710SBarry Smith 757e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), 7583d4c4710SBarry Smith @*/ 7593d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails) 7603d4c4710SBarry Smith { 7613d4c4710SBarry Smith PetscFunctionBegin; 762*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7633d4c4710SBarry Smith PetscValidIntPointer(maxFails,2); 7643d4c4710SBarry Smith *maxFails = snes->maxLinearSolveFailures; 7653d4c4710SBarry Smith PetscFunctionReturn(0); 7663d4c4710SBarry Smith } 7673d4c4710SBarry Smith 7683d4c4710SBarry Smith #undef __FUNCT__ 769b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations" 770c96a6f78SLois Curfman McInnes /*@ 771b850b91aSLisandro Dalcin SNESGetLinearSolveIterations - Gets the total number of linear iterations 772c96a6f78SLois Curfman McInnes used by the nonlinear solver. 773c96a6f78SLois Curfman McInnes 774c7afd0dbSLois Curfman McInnes Not Collective 775c7afd0dbSLois Curfman McInnes 776c96a6f78SLois Curfman McInnes Input Parameter: 777c96a6f78SLois Curfman McInnes . snes - SNES context 778c96a6f78SLois Curfman McInnes 779c96a6f78SLois Curfman McInnes Output Parameter: 780c96a6f78SLois Curfman McInnes . lits - number of linear iterations 781c96a6f78SLois Curfman McInnes 782c96a6f78SLois Curfman McInnes Notes: 783c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 784c96a6f78SLois Curfman McInnes 78536851e7fSLois Curfman McInnes Level: intermediate 78636851e7fSLois Curfman McInnes 787c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 7882b668275SBarry Smith 7898c7482ecSBarry Smith .seealso: SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures() 790c96a6f78SLois Curfman McInnes @*/ 791b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits) 792c96a6f78SLois Curfman McInnes { 7933a40ed3dSBarry Smith PetscFunctionBegin; 794*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 7954482741eSBarry Smith PetscValidIntPointer(lits,2); 796c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 7973a40ed3dSBarry Smith PetscFunctionReturn(0); 798c96a6f78SLois Curfman McInnes } 799c96a6f78SLois Curfman McInnes 8004a2ae208SSatish Balay #undef __FUNCT__ 80194b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP" 80252baeb72SSatish Balay /*@ 80394b7f48cSBarry Smith SNESGetKSP - Returns the KSP context for a SNES solver. 8049b94acceSBarry Smith 80594b7f48cSBarry Smith Not Collective, but if SNES object is parallel, then KSP object is parallel 806c7afd0dbSLois Curfman McInnes 8079b94acceSBarry Smith Input Parameter: 8089b94acceSBarry Smith . snes - the SNES context 8099b94acceSBarry Smith 8109b94acceSBarry Smith Output Parameter: 81194b7f48cSBarry Smith . ksp - the KSP context 8129b94acceSBarry Smith 8139b94acceSBarry Smith Notes: 81494b7f48cSBarry Smith The user can then directly manipulate the KSP context to set various 8159b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 8162999313aSBarry Smith PC contexts as well. 8179b94acceSBarry Smith 81836851e7fSLois Curfman McInnes Level: beginner 81936851e7fSLois Curfman McInnes 82094b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 8219b94acceSBarry Smith 8222999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 8239b94acceSBarry Smith @*/ 82463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp) 8259b94acceSBarry Smith { 8261cee3971SBarry Smith PetscErrorCode ierr; 8271cee3971SBarry Smith 8283a40ed3dSBarry Smith PetscFunctionBegin; 829*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 8304482741eSBarry Smith PetscValidPointer(ksp,2); 8311cee3971SBarry Smith 8321cee3971SBarry Smith if (!snes->ksp) { 8331cee3971SBarry Smith ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr); 8341cee3971SBarry Smith ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr); 8351cee3971SBarry Smith ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr); 8361cee3971SBarry Smith } 83794b7f48cSBarry Smith *ksp = snes->ksp; 8383a40ed3dSBarry Smith PetscFunctionReturn(0); 8399b94acceSBarry Smith } 84082bf6240SBarry Smith 8414a2ae208SSatish Balay #undef __FUNCT__ 8422999313aSBarry Smith #define __FUNCT__ "SNESSetKSP" 8432999313aSBarry Smith /*@ 8442999313aSBarry Smith SNESSetKSP - Sets a KSP context for the SNES object to use 8452999313aSBarry Smith 8462999313aSBarry Smith Not Collective, but the SNES and KSP objects must live on the same MPI_Comm 8472999313aSBarry Smith 8482999313aSBarry Smith Input Parameters: 8492999313aSBarry Smith + snes - the SNES context 8502999313aSBarry Smith - ksp - the KSP context 8512999313aSBarry Smith 8522999313aSBarry Smith Notes: 8532999313aSBarry Smith The SNES object already has its KSP object, you can obtain with SNESGetKSP() 8542999313aSBarry Smith so this routine is rarely needed. 8552999313aSBarry Smith 8562999313aSBarry Smith The KSP object that is already in the SNES object has its reference count 8572999313aSBarry Smith decreased by one. 8582999313aSBarry Smith 8592999313aSBarry Smith Level: developer 8602999313aSBarry Smith 8612999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context 8622999313aSBarry Smith 8632999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP() 8642999313aSBarry Smith @*/ 8652999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp) 8662999313aSBarry Smith { 8672999313aSBarry Smith PetscErrorCode ierr; 8682999313aSBarry Smith 8692999313aSBarry Smith PetscFunctionBegin; 870*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 871*0700a824SBarry Smith PetscValidHeaderSpecific(ksp,KSP_CLASSID,2); 8722999313aSBarry Smith PetscCheckSameComm(snes,1,ksp,2); 8737dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr); 874906ed7ccSBarry Smith if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);} 8752999313aSBarry Smith snes->ksp = ksp; 8762999313aSBarry Smith PetscFunctionReturn(0); 8772999313aSBarry Smith } 8782999313aSBarry Smith 8797adad957SLisandro Dalcin #if 0 8802999313aSBarry Smith #undef __FUNCT__ 8814a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 8826849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj) 883e24b481bSBarry Smith { 884e24b481bSBarry Smith PetscFunctionBegin; 885e24b481bSBarry Smith PetscFunctionReturn(0); 886e24b481bSBarry Smith } 8877adad957SLisandro Dalcin #endif 888e24b481bSBarry Smith 8899b94acceSBarry Smith /* -----------------------------------------------------------*/ 8904a2ae208SSatish Balay #undef __FUNCT__ 8914a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 89252baeb72SSatish Balay /*@ 8939b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 8949b94acceSBarry Smith 895c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 896c7afd0dbSLois Curfman McInnes 897c7afd0dbSLois Curfman McInnes Input Parameters: 898906ed7ccSBarry Smith . comm - MPI communicator 8999b94acceSBarry Smith 9009b94acceSBarry Smith Output Parameter: 9019b94acceSBarry Smith . outsnes - the new SNES context 9029b94acceSBarry Smith 903c7afd0dbSLois Curfman McInnes Options Database Keys: 904c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 905c7afd0dbSLois Curfman McInnes and no preconditioning matrix 906c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 907c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 908c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 909c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 910c1f60f51SBarry Smith 91136851e7fSLois Curfman McInnes Level: beginner 91236851e7fSLois Curfman McInnes 9139b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 9149b94acceSBarry Smith 915a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner() 916a8054027SBarry Smith 9179b94acceSBarry Smith @*/ 91863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes) 9199b94acceSBarry Smith { 920dfbe8321SBarry Smith PetscErrorCode ierr; 9219b94acceSBarry Smith SNES snes; 922fa9f3622SBarry Smith SNESKSPEW *kctx; 92337fcc0dbSBarry Smith 9243a40ed3dSBarry Smith PetscFunctionBegin; 925ed1caa07SMatthew Knepley PetscValidPointer(outsnes,2); 9268ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 9278ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 9288ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr); 9298ba1e511SMatthew Knepley #endif 9308ba1e511SMatthew Knepley 931*0700a824SBarry Smith ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr); 9327adad957SLisandro Dalcin 93385385478SLisandro Dalcin snes->ops->converged = SNESDefaultConverged; 9349b94acceSBarry Smith snes->max_its = 50; 9359750a799SBarry Smith snes->max_funcs = 10000; 9369b94acceSBarry Smith snes->norm = 0.0; 937b4874afaSBarry Smith snes->rtol = 1.e-8; 938b4874afaSBarry Smith snes->ttol = 0.0; 93970441072SBarry Smith snes->abstol = 1.e-50; 9409b94acceSBarry Smith snes->xtol = 1.e-8; 9414b27c08aSLois Curfman McInnes snes->deltatol = 1.e-12; 9429b94acceSBarry Smith snes->nfuncs = 0; 94350ffb88aSMatthew Knepley snes->numFailures = 0; 94450ffb88aSMatthew Knepley snes->maxFailures = 1; 9457a00f4a9SLois Curfman McInnes snes->linear_its = 0; 946e35cf81dSBarry Smith snes->lagjacobian = 1; 947a8054027SBarry Smith snes->lagpreconditioner = 1; 948639f9d9dSBarry Smith snes->numbermonitors = 0; 9499b94acceSBarry Smith snes->data = 0; 9504dc4c822SBarry Smith snes->setupcalled = PETSC_FALSE; 951186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 9526f24a144SLois Curfman McInnes snes->vwork = 0; 9536f24a144SLois Curfman McInnes snes->nwork = 0; 954758f92a0SBarry Smith snes->conv_hist_len = 0; 955758f92a0SBarry Smith snes->conv_hist_max = 0; 956758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 957758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 958758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 959184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 9609b94acceSBarry Smith 9613d4c4710SBarry Smith snes->numLinearSolveFailures = 0; 9623d4c4710SBarry Smith snes->maxLinearSolveFailures = 1; 9633d4c4710SBarry Smith 9649b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 96538f2d2fdSLisandro Dalcin ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr); 9669b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 9679b94acceSBarry Smith kctx->version = 2; 9689b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 9699b94acceSBarry Smith this was too large for some test cases */ 97075567043SBarry Smith kctx->rtol_last = 0.0; 9719b94acceSBarry Smith kctx->rtol_max = .9; 9729b94acceSBarry Smith kctx->gamma = 1.0; 97371f87433Sdalcinl kctx->alpha = .5*(1.0 + sqrt(5.0)); 97471f87433Sdalcinl kctx->alpha2 = kctx->alpha; 9759b94acceSBarry Smith kctx->threshold = .1; 97675567043SBarry Smith kctx->lresid_last = 0.0; 97775567043SBarry Smith kctx->norm_last = 0.0; 9789b94acceSBarry Smith 9799b94acceSBarry Smith *outsnes = snes; 98000036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 9813a40ed3dSBarry Smith PetscFunctionReturn(0); 9829b94acceSBarry Smith } 9839b94acceSBarry Smith 9844a2ae208SSatish Balay #undef __FUNCT__ 9854a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 9869b94acceSBarry Smith /*@C 9879b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 9889b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 9899b94acceSBarry Smith equations. 9909b94acceSBarry Smith 991fee21e36SBarry Smith Collective on SNES 992fee21e36SBarry Smith 993c7afd0dbSLois Curfman McInnes Input Parameters: 994c7afd0dbSLois Curfman McInnes + snes - the SNES context 995c7afd0dbSLois Curfman McInnes . r - vector to store function value 996de044059SHong Zhang . func - function evaluation routine 997c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 998c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 9999b94acceSBarry Smith 1000c7afd0dbSLois Curfman McInnes Calling sequence of func: 10018d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 1002c7afd0dbSLois Curfman McInnes 1003313e4042SLois Curfman McInnes . f - function vector 1004c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 10059b94acceSBarry Smith 10069b94acceSBarry Smith Notes: 10079b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 10089b94acceSBarry Smith $ f'(x) x = -f(x), 1009c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 10109b94acceSBarry Smith 101136851e7fSLois Curfman McInnes Level: beginner 101236851e7fSLois Curfman McInnes 10139b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 10149b94acceSBarry Smith 1015a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 10169b94acceSBarry Smith @*/ 101763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx) 10189b94acceSBarry Smith { 101985385478SLisandro Dalcin PetscErrorCode ierr; 10203a40ed3dSBarry Smith PetscFunctionBegin; 1021*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1022*0700a824SBarry Smith PetscValidHeaderSpecific(r,VEC_CLASSID,2); 1023c9780b6fSBarry Smith PetscCheckSameComm(snes,1,r,2); 102485385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr); 102585385478SLisandro Dalcin if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); } 1026e7788613SBarry Smith snes->ops->computefunction = func; 102785385478SLisandro Dalcin snes->vec_func = r; 10289b94acceSBarry Smith snes->funP = ctx; 10293a40ed3dSBarry Smith PetscFunctionReturn(0); 10309b94acceSBarry Smith } 10319b94acceSBarry Smith 10323ab0aad5SBarry Smith /* --------------------------------------------------------------- */ 10333ab0aad5SBarry Smith #undef __FUNCT__ 10341096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs" 10351096aae1SMatthew Knepley /*@C 10361096aae1SMatthew Knepley SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set 10371096aae1SMatthew Knepley it assumes a zero right hand side. 10381096aae1SMatthew Knepley 10391096aae1SMatthew Knepley Collective on SNES 10401096aae1SMatthew Knepley 10411096aae1SMatthew Knepley Input Parameter: 10421096aae1SMatthew Knepley . snes - the SNES context 10431096aae1SMatthew Knepley 10441096aae1SMatthew Knepley Output Parameter: 1045bc08b0f1SBarry Smith . rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null 10461096aae1SMatthew Knepley 10471096aae1SMatthew Knepley Level: intermediate 10481096aae1SMatthew Knepley 10491096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side 10501096aae1SMatthew Knepley 105185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction() 10521096aae1SMatthew Knepley @*/ 10531096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs) 10541096aae1SMatthew Knepley { 10551096aae1SMatthew Knepley PetscFunctionBegin; 1056*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 10571096aae1SMatthew Knepley PetscValidPointer(rhs,2); 105885385478SLisandro Dalcin *rhs = snes->vec_rhs; 10591096aae1SMatthew Knepley PetscFunctionReturn(0); 10601096aae1SMatthew Knepley } 10611096aae1SMatthew Knepley 10621096aae1SMatthew Knepley #undef __FUNCT__ 10634a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 10649b94acceSBarry Smith /*@ 106536851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 10669b94acceSBarry Smith SNESSetFunction(). 10679b94acceSBarry Smith 1068c7afd0dbSLois Curfman McInnes Collective on SNES 1069c7afd0dbSLois Curfman McInnes 10709b94acceSBarry Smith Input Parameters: 1071c7afd0dbSLois Curfman McInnes + snes - the SNES context 1072c7afd0dbSLois Curfman McInnes - x - input vector 10739b94acceSBarry Smith 10749b94acceSBarry Smith Output Parameter: 10753638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 10769b94acceSBarry Smith 10771bffabb2SLois Curfman McInnes Notes: 107836851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 107936851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 108036851e7fSLois Curfman McInnes themselves. 108136851e7fSLois Curfman McInnes 108236851e7fSLois Curfman McInnes Level: developer 108336851e7fSLois Curfman McInnes 10849b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 10859b94acceSBarry Smith 1086a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 10879b94acceSBarry Smith @*/ 108863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y) 10899b94acceSBarry Smith { 1090dfbe8321SBarry Smith PetscErrorCode ierr; 10919b94acceSBarry Smith 10923a40ed3dSBarry Smith PetscFunctionBegin; 1093*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1094*0700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,2); 1095*0700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,3); 1096c9780b6fSBarry Smith PetscCheckSameComm(snes,1,x,2); 1097c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,3); 1098184914b5SBarry Smith 1099d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 1100e7788613SBarry Smith if (snes->ops->computefunction) { 1101d64ed03dSBarry Smith PetscStackPush("SNES user function"); 1102e9a2bbcdSBarry Smith CHKMEMQ; 1103e7788613SBarry Smith ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP); 1104e9a2bbcdSBarry Smith CHKMEMQ; 1105d64ed03dSBarry Smith PetscStackPop; 1106d5e45103SBarry Smith if (PetscExceptionValue(ierr)) { 110719717074SBarry Smith PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr); 110819717074SBarry Smith } 1109d5e45103SBarry Smith CHKERRQ(ierr); 111085385478SLisandro Dalcin } else if (snes->vec_rhs) { 11111096aae1SMatthew Knepley ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr); 11121096aae1SMatthew Knepley } else { 11131096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve()."); 11141096aae1SMatthew Knepley } 111585385478SLisandro Dalcin if (snes->vec_rhs) { 111685385478SLisandro Dalcin ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr); 11173ab0aad5SBarry Smith } 1118ae3c334cSLois Curfman McInnes snes->nfuncs++; 1119d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 11203a40ed3dSBarry Smith PetscFunctionReturn(0); 11219b94acceSBarry Smith } 11229b94acceSBarry Smith 11234a2ae208SSatish Balay #undef __FUNCT__ 11244a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 112562fef451SLois Curfman McInnes /*@ 112662fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 112762fef451SLois Curfman McInnes set with SNESSetJacobian(). 112862fef451SLois Curfman McInnes 1129c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1130c7afd0dbSLois Curfman McInnes 113162fef451SLois Curfman McInnes Input Parameters: 1132c7afd0dbSLois Curfman McInnes + snes - the SNES context 1133c7afd0dbSLois Curfman McInnes - x - input vector 113462fef451SLois Curfman McInnes 113562fef451SLois Curfman McInnes Output Parameters: 1136c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 113762fef451SLois Curfman McInnes . B - optional preconditioning matrix 11382b668275SBarry Smith - flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER) 1139fee21e36SBarry Smith 1140e35cf81dSBarry Smith Options Database Keys: 1141e35cf81dSBarry Smith + -snes_lag_preconditioner <lag> 1142e35cf81dSBarry Smith - -snes_lag_jacobian <lag> 1143e35cf81dSBarry Smith 114462fef451SLois Curfman McInnes Notes: 114562fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 114662fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 114762fef451SLois Curfman McInnes 114894b7f48cSBarry Smith See KSPSetOperators() for important information about setting the 1149dc5a77f8SLois Curfman McInnes flag parameter. 115062fef451SLois Curfman McInnes 115136851e7fSLois Curfman McInnes Level: developer 115236851e7fSLois Curfman McInnes 115362fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 115462fef451SLois Curfman McInnes 1155e35cf81dSBarry Smith .seealso: SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian() 115662fef451SLois Curfman McInnes @*/ 115763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 11589b94acceSBarry Smith { 1159dfbe8321SBarry Smith PetscErrorCode ierr; 1160ebd3b9afSBarry Smith PetscTruth flag; 11613a40ed3dSBarry Smith 11623a40ed3dSBarry Smith PetscFunctionBegin; 1163*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1164*0700a824SBarry Smith PetscValidHeaderSpecific(X,VEC_CLASSID,2); 11654482741eSBarry Smith PetscValidPointer(flg,5); 1166c9780b6fSBarry Smith PetscCheckSameComm(snes,1,X,2); 1167e7788613SBarry Smith if (!snes->ops->computejacobian) PetscFunctionReturn(0); 1168ebd3b9afSBarry Smith 1169ebd3b9afSBarry Smith /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */ 1170ebd3b9afSBarry Smith 1171fe3ffe1eSBarry Smith if (snes->lagjacobian == -2) { 1172fe3ffe1eSBarry Smith snes->lagjacobian = -1; 1173fe3ffe1eSBarry Smith ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr); 1174fe3ffe1eSBarry Smith } else if (snes->lagjacobian == -1) { 1175e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1176e35cf81dSBarry Smith ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr); 1177ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1178ebd3b9afSBarry Smith if (flag) { 1179ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1180ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1181ebd3b9afSBarry Smith } 1182e35cf81dSBarry Smith PetscFunctionReturn(0); 1183e35cf81dSBarry Smith } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) { 1184e35cf81dSBarry Smith *flg = SAME_PRECONDITIONER; 1185e35cf81dSBarry Smith ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr); 1186ebd3b9afSBarry Smith ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr); 1187ebd3b9afSBarry Smith if (flag) { 1188ebd3b9afSBarry Smith ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1189ebd3b9afSBarry Smith ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 1190ebd3b9afSBarry Smith } 1191e35cf81dSBarry Smith PetscFunctionReturn(0); 1192e35cf81dSBarry Smith } 1193e35cf81dSBarry Smith 1194c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1195e35cf81dSBarry Smith ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1196d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 1197dc67937bSBarry Smith CHKMEMQ; 1198e7788613SBarry Smith ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1199dc67937bSBarry Smith CHKMEMQ; 1200d64ed03dSBarry Smith PetscStackPop; 1201d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1202a8054027SBarry Smith 12033b4f5425SBarry Smith if (snes->lagpreconditioner == -2) { 12043b4f5425SBarry Smith ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr); 12053b4f5425SBarry Smith snes->lagpreconditioner = -1; 12063b4f5425SBarry Smith } else if (snes->lagpreconditioner == -1) { 1207a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1208a8054027SBarry Smith ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr); 1209a8054027SBarry Smith } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) { 1210a8054027SBarry Smith *flg = SAME_PRECONDITIONER; 1211a8054027SBarry Smith ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr); 1212a8054027SBarry Smith } 1213a8054027SBarry Smith 12146d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 1215*0700a824SBarry Smith /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3); 1216*0700a824SBarry Smith PetscValidHeaderSpecific(*B,MAT_CLASSID,4); */ 12173a40ed3dSBarry Smith PetscFunctionReturn(0); 12189b94acceSBarry Smith } 12199b94acceSBarry Smith 12204a2ae208SSatish Balay #undef __FUNCT__ 12214a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 12229b94acceSBarry Smith /*@C 12239b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1224044dda88SLois Curfman McInnes location to store the matrix. 12259b94acceSBarry Smith 1226c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1227c7afd0dbSLois Curfman McInnes 12289b94acceSBarry Smith Input Parameters: 1229c7afd0dbSLois Curfman McInnes + snes - the SNES context 12309b94acceSBarry Smith . A - Jacobian matrix 12319b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 12329b94acceSBarry Smith . func - Jacobian evaluation routine 1233c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 12342cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 12359b94acceSBarry Smith 12369b94acceSBarry Smith Calling sequence of func: 12378d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 12389b94acceSBarry Smith 1239c7afd0dbSLois Curfman McInnes + x - input vector 12409b94acceSBarry Smith . A - Jacobian matrix 12419b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1242ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 12432b668275SBarry Smith structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER 1244c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 12459b94acceSBarry Smith 12469b94acceSBarry Smith Notes: 124794b7f48cSBarry Smith See KSPSetOperators() for important information about setting the flag 12482cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1249ac21db08SLois Curfman McInnes 1250ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 12519b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 12529b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 12539b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 12549b94acceSBarry Smith throughout the global iterations. 12559b94acceSBarry Smith 125616913363SBarry Smith If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on 125716913363SBarry Smith each matrix. 125816913363SBarry Smith 125936851e7fSLois Curfman McInnes Level: beginner 126036851e7fSLois Curfman McInnes 12619b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 12629b94acceSBarry Smith 12633ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure 12649b94acceSBarry Smith @*/ 126563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 12669b94acceSBarry Smith { 1267dfbe8321SBarry Smith PetscErrorCode ierr; 12683a7fca6bSBarry Smith 12693a40ed3dSBarry Smith PetscFunctionBegin; 1270*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1271*0700a824SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2); 1272*0700a824SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3); 1273c9780b6fSBarry Smith if (A) PetscCheckSameComm(snes,1,A,2); 127406975374SJed Brown if (B) PetscCheckSameComm(snes,1,B,3); 1275e7788613SBarry Smith if (func) snes->ops->computejacobian = func; 12763a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 12773a7fca6bSBarry Smith if (A) { 12787dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 12793a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 12809b94acceSBarry Smith snes->jacobian = A; 12813a7fca6bSBarry Smith } 12823a7fca6bSBarry Smith if (B) { 12837dcf0eaaSdalcinl ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 12843a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 12859b94acceSBarry Smith snes->jacobian_pre = B; 12863a7fca6bSBarry Smith } 12873a40ed3dSBarry Smith PetscFunctionReturn(0); 12889b94acceSBarry Smith } 128962fef451SLois Curfman McInnes 12904a2ae208SSatish Balay #undef __FUNCT__ 12914a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1292c2aafc4cSSatish Balay /*@C 1293b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1294b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1295b4fd4287SBarry Smith 1296c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1297c7afd0dbSLois Curfman McInnes 1298b4fd4287SBarry Smith Input Parameter: 1299b4fd4287SBarry Smith . snes - the nonlinear solver context 1300b4fd4287SBarry Smith 1301b4fd4287SBarry Smith Output Parameters: 1302c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1303b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 130470e92668SMatthew Knepley . func - location to put Jacobian function (or PETSC_NULL) 130570e92668SMatthew Knepley - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1306fee21e36SBarry Smith 130736851e7fSLois Curfman McInnes Level: advanced 130836851e7fSLois Curfman McInnes 1309b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1310b4fd4287SBarry Smith @*/ 131170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx) 1312b4fd4287SBarry Smith { 13133a40ed3dSBarry Smith PetscFunctionBegin; 1314*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1315b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1316b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1317e7788613SBarry Smith if (func) *func = snes->ops->computejacobian; 131870e92668SMatthew Knepley if (ctx) *ctx = snes->jacP; 13193a40ed3dSBarry Smith PetscFunctionReturn(0); 1320b4fd4287SBarry Smith } 1321b4fd4287SBarry Smith 13229b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 13239b94acceSBarry Smith 13244a2ae208SSatish Balay #undef __FUNCT__ 13254a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 13269b94acceSBarry Smith /*@ 13279b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1328272ac6f2SLois Curfman McInnes of a nonlinear solver. 13299b94acceSBarry Smith 1330fee21e36SBarry Smith Collective on SNES 1331fee21e36SBarry Smith 1332c7afd0dbSLois Curfman McInnes Input Parameters: 133370e92668SMatthew Knepley . snes - the SNES context 1334c7afd0dbSLois Curfman McInnes 1335272ac6f2SLois Curfman McInnes Notes: 1336272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1337272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1338272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1339272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1340272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1341272ac6f2SLois Curfman McInnes 134236851e7fSLois Curfman McInnes Level: advanced 134336851e7fSLois Curfman McInnes 13449b94acceSBarry Smith .keywords: SNES, nonlinear, setup 13459b94acceSBarry Smith 13469b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 13479b94acceSBarry Smith @*/ 134870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes) 13499b94acceSBarry Smith { 1350dfbe8321SBarry Smith PetscErrorCode ierr; 13513a40ed3dSBarry Smith 13523a40ed3dSBarry Smith PetscFunctionBegin; 1353*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 13544dc4c822SBarry Smith if (snes->setupcalled) PetscFunctionReturn(0); 13559b94acceSBarry Smith 13567adad957SLisandro Dalcin if (!((PetscObject)snes)->type_name) { 135785385478SLisandro Dalcin ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr); 135885385478SLisandro Dalcin } 135985385478SLisandro Dalcin 136085385478SLisandro Dalcin if (!snes->vec_func && !snes->vec_rhs) { 13611096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 13621096aae1SMatthew Knepley } 136385385478SLisandro Dalcin if (!snes->ops->computefunction && !snes->vec_rhs) { 13641096aae1SMatthew Knepley SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 13651096aae1SMatthew Knepley } 1366a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 136729bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1368a8c6a408SBarry Smith } 1369a703fe33SLois Curfman McInnes 1370b710008aSBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);} 1371b710008aSBarry Smith 1372410397dcSLisandro Dalcin if (snes->ops->setup) { 1373410397dcSLisandro Dalcin ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr); 1374410397dcSLisandro Dalcin } 13757aaed0d8SBarry Smith snes->setupcalled = PETSC_TRUE; 13763a40ed3dSBarry Smith PetscFunctionReturn(0); 13779b94acceSBarry Smith } 13789b94acceSBarry Smith 13794a2ae208SSatish Balay #undef __FUNCT__ 13804a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 138152baeb72SSatish Balay /*@ 13829b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 13839b94acceSBarry Smith with SNESCreate(). 13849b94acceSBarry Smith 1385c7afd0dbSLois Curfman McInnes Collective on SNES 1386c7afd0dbSLois Curfman McInnes 13879b94acceSBarry Smith Input Parameter: 13889b94acceSBarry Smith . snes - the SNES context 13899b94acceSBarry Smith 139036851e7fSLois Curfman McInnes Level: beginner 139136851e7fSLois Curfman McInnes 13929b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 13939b94acceSBarry Smith 139463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 13959b94acceSBarry Smith @*/ 139663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes) 13979b94acceSBarry Smith { 13986849ba73SBarry Smith PetscErrorCode ierr; 13993a40ed3dSBarry Smith 14003a40ed3dSBarry Smith PetscFunctionBegin; 1401*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14027adad957SLisandro Dalcin if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0); 1403d4bb536fSBarry Smith 1404be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 14050f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 14066c699258SBarry Smith 14076c699258SBarry Smith if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);} 1408e7788613SBarry Smith if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);} 140985385478SLisandro Dalcin 141085385478SLisandro Dalcin if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);} 141185385478SLisandro Dalcin if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);} 141285385478SLisandro Dalcin if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);} 14133a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 14143a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 14151cee3971SBarry Smith if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);} 141685385478SLisandro Dalcin ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr); 1417522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1418a6570f20SBarry Smith ierr = SNESMonitorCancel(snes);CHKERRQ(ierr); 14197f7931b9SBarry Smith if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);} 1420a79aaaedSSatish Balay ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr); 14213a40ed3dSBarry Smith PetscFunctionReturn(0); 14229b94acceSBarry Smith } 14239b94acceSBarry Smith 14249b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 14259b94acceSBarry Smith 14264a2ae208SSatish Balay #undef __FUNCT__ 1427a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner" 1428a8054027SBarry Smith /*@ 1429a8054027SBarry Smith SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve. 1430a8054027SBarry Smith 1431a8054027SBarry Smith Collective on SNES 1432a8054027SBarry Smith 1433a8054027SBarry Smith Input Parameters: 1434a8054027SBarry Smith + snes - the SNES context 1435a8054027SBarry 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 14363b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1437a8054027SBarry Smith 1438a8054027SBarry Smith Options Database Keys: 1439a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1440a8054027SBarry Smith 1441a8054027SBarry Smith Notes: 1442a8054027SBarry Smith The default is 1 1443a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1444a8054027SBarry Smith If -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use 1445a8054027SBarry Smith 1446a8054027SBarry Smith Level: intermediate 1447a8054027SBarry Smith 1448a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1449a8054027SBarry Smith 1450e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian() 1451a8054027SBarry Smith 1452a8054027SBarry Smith @*/ 1453a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag) 1454a8054027SBarry Smith { 1455a8054027SBarry Smith PetscFunctionBegin; 1456*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 14573b4f5425SBarry Smith if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1458a8054027SBarry Smith if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1459a8054027SBarry Smith snes->lagpreconditioner = lag; 1460a8054027SBarry Smith PetscFunctionReturn(0); 1461a8054027SBarry Smith } 1462a8054027SBarry Smith 1463a8054027SBarry Smith #undef __FUNCT__ 1464a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner" 1465a8054027SBarry Smith /*@ 1466a8054027SBarry Smith SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt 1467a8054027SBarry Smith 1468a8054027SBarry Smith Collective on SNES 1469a8054027SBarry Smith 1470a8054027SBarry Smith Input Parameter: 1471a8054027SBarry Smith . snes - the SNES context 1472a8054027SBarry Smith 1473a8054027SBarry Smith Output Parameter: 1474a8054027SBarry 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 14753b4f5425SBarry Smith the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that 1476a8054027SBarry Smith 1477a8054027SBarry Smith Options Database Keys: 1478a8054027SBarry Smith . -snes_lag_preconditioner <lag> 1479a8054027SBarry Smith 1480a8054027SBarry Smith Notes: 1481a8054027SBarry Smith The default is 1 1482a8054027SBarry Smith The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1483a8054027SBarry Smith 1484a8054027SBarry Smith Level: intermediate 1485a8054027SBarry Smith 1486a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1487a8054027SBarry Smith 1488a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner() 1489a8054027SBarry Smith 1490a8054027SBarry Smith @*/ 1491a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag) 1492a8054027SBarry Smith { 1493a8054027SBarry Smith PetscFunctionBegin; 1494*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1495a8054027SBarry Smith *lag = snes->lagpreconditioner; 1496a8054027SBarry Smith PetscFunctionReturn(0); 1497a8054027SBarry Smith } 1498a8054027SBarry Smith 1499a8054027SBarry Smith #undef __FUNCT__ 1500e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian" 1501e35cf81dSBarry Smith /*@ 1502e35cf81dSBarry Smith SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how 1503e35cf81dSBarry Smith often the preconditioner is rebuilt. 1504e35cf81dSBarry Smith 1505e35cf81dSBarry Smith Collective on SNES 1506e35cf81dSBarry Smith 1507e35cf81dSBarry Smith Input Parameters: 1508e35cf81dSBarry Smith + snes - the SNES context 1509e35cf81dSBarry 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 1510fe3ffe1eSBarry Smith the Jacobian is built etc. -2 means rebuild at next chance but then never again 1511e35cf81dSBarry Smith 1512e35cf81dSBarry Smith Options Database Keys: 1513e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1514e35cf81dSBarry Smith 1515e35cf81dSBarry Smith Notes: 1516e35cf81dSBarry Smith The default is 1 1517e35cf81dSBarry Smith The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1518fe3ffe1eSBarry 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 1519fe3ffe1eSBarry Smith at the next Newton step but never again (unless it is reset to another value) 1520e35cf81dSBarry Smith 1521e35cf81dSBarry Smith Level: intermediate 1522e35cf81dSBarry Smith 1523e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1524e35cf81dSBarry Smith 1525e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian() 1526e35cf81dSBarry Smith 1527e35cf81dSBarry Smith @*/ 1528e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag) 1529e35cf81dSBarry Smith { 1530e35cf81dSBarry Smith PetscFunctionBegin; 1531*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1532fe3ffe1eSBarry Smith if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater"); 1533e35cf81dSBarry Smith if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0"); 1534e35cf81dSBarry Smith snes->lagjacobian = lag; 1535e35cf81dSBarry Smith PetscFunctionReturn(0); 1536e35cf81dSBarry Smith } 1537e35cf81dSBarry Smith 1538e35cf81dSBarry Smith #undef __FUNCT__ 1539e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian" 1540e35cf81dSBarry Smith /*@ 1541e35cf81dSBarry Smith SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt 1542e35cf81dSBarry Smith 1543e35cf81dSBarry Smith Collective on SNES 1544e35cf81dSBarry Smith 1545e35cf81dSBarry Smith Input Parameter: 1546e35cf81dSBarry Smith . snes - the SNES context 1547e35cf81dSBarry Smith 1548e35cf81dSBarry Smith Output Parameter: 1549e35cf81dSBarry 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 1550e35cf81dSBarry Smith the Jacobian is built etc. 1551e35cf81dSBarry Smith 1552e35cf81dSBarry Smith Options Database Keys: 1553e35cf81dSBarry Smith . -snes_lag_jacobian <lag> 1554e35cf81dSBarry Smith 1555e35cf81dSBarry Smith Notes: 1556e35cf81dSBarry Smith The default is 1 1557e35cf81dSBarry Smith The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1 1558e35cf81dSBarry Smith 1559e35cf81dSBarry Smith Level: intermediate 1560e35cf81dSBarry Smith 1561e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances 1562e35cf81dSBarry Smith 1563e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner() 1564e35cf81dSBarry Smith 1565e35cf81dSBarry Smith @*/ 1566e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag) 1567e35cf81dSBarry Smith { 1568e35cf81dSBarry Smith PetscFunctionBegin; 1569*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1570e35cf81dSBarry Smith *lag = snes->lagjacobian; 1571e35cf81dSBarry Smith PetscFunctionReturn(0); 1572e35cf81dSBarry Smith } 1573e35cf81dSBarry Smith 1574e35cf81dSBarry Smith #undef __FUNCT__ 15754a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 15769b94acceSBarry Smith /*@ 1577d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 15789b94acceSBarry Smith 1579c7afd0dbSLois Curfman McInnes Collective on SNES 1580c7afd0dbSLois Curfman McInnes 15819b94acceSBarry Smith Input Parameters: 1582c7afd0dbSLois Curfman McInnes + snes - the SNES context 158370441072SBarry Smith . abstol - absolute convergence tolerance 158433174efeSLois Curfman McInnes . rtol - relative convergence tolerance 158533174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 158633174efeSLois Curfman McInnes of the change in the solution between steps 158733174efeSLois Curfman McInnes . maxit - maximum number of iterations 1588c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1589fee21e36SBarry Smith 159033174efeSLois Curfman McInnes Options Database Keys: 159170441072SBarry Smith + -snes_atol <abstol> - Sets abstol 1592c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1593c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1594c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1595c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15969b94acceSBarry Smith 1597d7a720efSLois Curfman McInnes Notes: 15989b94acceSBarry Smith The default maximum number of iterations is 50. 15999b94acceSBarry Smith The default maximum number of function evaluations is 1000. 16009b94acceSBarry Smith 160136851e7fSLois Curfman McInnes Level: intermediate 160236851e7fSLois Curfman McInnes 160333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 16049b94acceSBarry Smith 16052492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance() 16069b94acceSBarry Smith @*/ 160763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf) 16089b94acceSBarry Smith { 16093a40ed3dSBarry Smith PetscFunctionBegin; 1610*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 161170441072SBarry Smith if (abstol != PETSC_DEFAULT) snes->abstol = abstol; 1612d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1613d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1614d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1615d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 16163a40ed3dSBarry Smith PetscFunctionReturn(0); 16179b94acceSBarry Smith } 16189b94acceSBarry Smith 16194a2ae208SSatish Balay #undef __FUNCT__ 16204a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 16219b94acceSBarry Smith /*@ 162233174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 162333174efeSLois Curfman McInnes 1624c7afd0dbSLois Curfman McInnes Not Collective 1625c7afd0dbSLois Curfman McInnes 162633174efeSLois Curfman McInnes Input Parameters: 1627c7afd0dbSLois Curfman McInnes + snes - the SNES context 162885385478SLisandro Dalcin . atol - absolute convergence tolerance 162933174efeSLois Curfman McInnes . rtol - relative convergence tolerance 163033174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 163133174efeSLois Curfman McInnes of the change in the solution between steps 163233174efeSLois Curfman McInnes . maxit - maximum number of iterations 1633c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1634fee21e36SBarry Smith 163533174efeSLois Curfman McInnes Notes: 163633174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 163733174efeSLois Curfman McInnes 163836851e7fSLois Curfman McInnes Level: intermediate 163936851e7fSLois Curfman McInnes 164033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 164133174efeSLois Curfman McInnes 164233174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 164333174efeSLois Curfman McInnes @*/ 164485385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf) 164533174efeSLois Curfman McInnes { 16463a40ed3dSBarry Smith PetscFunctionBegin; 1647*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 164885385478SLisandro Dalcin if (atol) *atol = snes->abstol; 164933174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 165033174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 165133174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 165233174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 16533a40ed3dSBarry Smith PetscFunctionReturn(0); 165433174efeSLois Curfman McInnes } 165533174efeSLois Curfman McInnes 16564a2ae208SSatish Balay #undef __FUNCT__ 16574a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 165833174efeSLois Curfman McInnes /*@ 16599b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 16609b94acceSBarry Smith 1661fee21e36SBarry Smith Collective on SNES 1662fee21e36SBarry Smith 1663c7afd0dbSLois Curfman McInnes Input Parameters: 1664c7afd0dbSLois Curfman McInnes + snes - the SNES context 1665c7afd0dbSLois Curfman McInnes - tol - tolerance 1666c7afd0dbSLois Curfman McInnes 16679b94acceSBarry Smith Options Database Key: 1668c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 16699b94acceSBarry Smith 167036851e7fSLois Curfman McInnes Level: intermediate 167136851e7fSLois Curfman McInnes 16729b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 16739b94acceSBarry Smith 16742492ecdbSBarry Smith .seealso: SNESSetTolerances() 16759b94acceSBarry Smith @*/ 167663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 16779b94acceSBarry Smith { 16783a40ed3dSBarry Smith PetscFunctionBegin; 1679*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 16809b94acceSBarry Smith snes->deltatol = tol; 16813a40ed3dSBarry Smith PetscFunctionReturn(0); 16829b94acceSBarry Smith } 16839b94acceSBarry Smith 1684df9fa365SBarry Smith /* 1685df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1686df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1687df9fa365SBarry Smith macros instead of functions 1688df9fa365SBarry Smith */ 16894a2ae208SSatish Balay #undef __FUNCT__ 1690a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG" 1691a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx) 1692ce1608b8SBarry Smith { 1693dfbe8321SBarry Smith PetscErrorCode ierr; 1694ce1608b8SBarry Smith 1695ce1608b8SBarry Smith PetscFunctionBegin; 1696*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1697a6570f20SBarry Smith ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1698ce1608b8SBarry Smith PetscFunctionReturn(0); 1699ce1608b8SBarry Smith } 1700ce1608b8SBarry Smith 17014a2ae208SSatish Balay #undef __FUNCT__ 1702a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate" 1703a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1704df9fa365SBarry Smith { 1705dfbe8321SBarry Smith PetscErrorCode ierr; 1706df9fa365SBarry Smith 1707df9fa365SBarry Smith PetscFunctionBegin; 1708a6570f20SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1709df9fa365SBarry Smith PetscFunctionReturn(0); 1710df9fa365SBarry Smith } 1711df9fa365SBarry Smith 17124a2ae208SSatish Balay #undef __FUNCT__ 1713a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy" 1714a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw) 1715df9fa365SBarry Smith { 1716dfbe8321SBarry Smith PetscErrorCode ierr; 1717df9fa365SBarry Smith 1718df9fa365SBarry Smith PetscFunctionBegin; 1719a6570f20SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1720df9fa365SBarry Smith PetscFunctionReturn(0); 1721df9fa365SBarry Smith } 1722df9fa365SBarry Smith 1723b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*); 1724b271bb04SBarry Smith #undef __FUNCT__ 1725b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange" 1726b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx) 1727b271bb04SBarry Smith { 1728b271bb04SBarry Smith PetscDrawLG lg; 1729b271bb04SBarry Smith PetscErrorCode ierr; 1730b271bb04SBarry Smith PetscReal x,y,per; 1731b271bb04SBarry Smith PetscViewer v = (PetscViewer)monctx; 1732b271bb04SBarry Smith static PetscReal prev; /* should be in the context */ 1733b271bb04SBarry Smith PetscDraw draw; 1734b271bb04SBarry Smith PetscFunctionBegin; 1735b271bb04SBarry Smith if (!monctx) { 1736b271bb04SBarry Smith MPI_Comm comm; 1737b271bb04SBarry Smith 1738b271bb04SBarry Smith ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr); 1739b271bb04SBarry Smith v = PETSC_VIEWER_DRAW_(comm); 1740b271bb04SBarry Smith } 1741b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr); 1742b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1743b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1744b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr); 1745b271bb04SBarry Smith x = (PetscReal) n; 1746b271bb04SBarry Smith if (rnorm > 0.0) y = log10(rnorm); else y = -15.0; 1747b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1748b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1749b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1750b271bb04SBarry Smith } 1751b271bb04SBarry Smith 1752b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr); 1753b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1754b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1755b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr); 1756b271bb04SBarry Smith ierr = SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr); 1757b271bb04SBarry Smith x = (PetscReal) n; 1758b271bb04SBarry Smith y = 100.0*per; 1759b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1760b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1761b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1762b271bb04SBarry Smith } 1763b271bb04SBarry Smith 1764b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr); 1765b271bb04SBarry Smith if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1766b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1767b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr); 1768b271bb04SBarry Smith x = (PetscReal) n; 1769b271bb04SBarry Smith y = (prev - rnorm)/prev; 1770b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1771b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1772b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1773b271bb04SBarry Smith } 1774b271bb04SBarry Smith 1775b271bb04SBarry Smith ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr); 1776b271bb04SBarry Smith if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);} 1777b271bb04SBarry Smith ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr); 1778b271bb04SBarry Smith ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr); 1779b271bb04SBarry Smith x = (PetscReal) n; 1780b271bb04SBarry Smith y = (prev - rnorm)/(prev*per); 1781b271bb04SBarry Smith if (n > 2) { /*skip initial crazy value */ 1782b271bb04SBarry Smith ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr); 1783b271bb04SBarry Smith } 1784b271bb04SBarry Smith if (n < 20 || !(n % 5)) { 1785b271bb04SBarry Smith ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); 1786b271bb04SBarry Smith } 1787b271bb04SBarry Smith prev = rnorm; 1788b271bb04SBarry Smith PetscFunctionReturn(0); 1789b271bb04SBarry Smith } 1790b271bb04SBarry Smith 1791b271bb04SBarry Smith #undef __FUNCT__ 1792b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate" 1793b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw) 1794b271bb04SBarry Smith { 1795b271bb04SBarry Smith PetscErrorCode ierr; 1796b271bb04SBarry Smith 1797b271bb04SBarry Smith PetscFunctionBegin; 1798b271bb04SBarry Smith ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1799b271bb04SBarry Smith PetscFunctionReturn(0); 1800b271bb04SBarry Smith } 1801b271bb04SBarry Smith 1802b271bb04SBarry Smith #undef __FUNCT__ 1803b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy" 1804b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw) 1805b271bb04SBarry Smith { 1806b271bb04SBarry Smith PetscErrorCode ierr; 1807b271bb04SBarry Smith 1808b271bb04SBarry Smith PetscFunctionBegin; 1809b271bb04SBarry Smith ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr); 1810b271bb04SBarry Smith PetscFunctionReturn(0); 1811b271bb04SBarry Smith } 1812b271bb04SBarry Smith 18139b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 18149b94acceSBarry Smith 18154a2ae208SSatish Balay #undef __FUNCT__ 1816a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet" 18179b94acceSBarry Smith /*@C 1818a6570f20SBarry Smith SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every 18199b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 18209b94acceSBarry Smith progress. 18219b94acceSBarry Smith 1822fee21e36SBarry Smith Collective on SNES 1823fee21e36SBarry Smith 1824c7afd0dbSLois Curfman McInnes Input Parameters: 1825c7afd0dbSLois Curfman McInnes + snes - the SNES context 1826c7afd0dbSLois Curfman McInnes . func - monitoring routine 1827b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1828e8105e01SRichard Katz monitor routine (use PETSC_NULL if no context is desired) 1829b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1830b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 18319b94acceSBarry Smith 1832c7afd0dbSLois Curfman McInnes Calling sequence of func: 1833a7cc72afSBarry Smith $ int func(SNES snes,PetscInt its, PetscReal norm,void *mctx) 1834c7afd0dbSLois Curfman McInnes 1835c7afd0dbSLois Curfman McInnes + snes - the SNES context 1836c7afd0dbSLois Curfman McInnes . its - iteration number 1837c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 183840a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 18399b94acceSBarry Smith 18409665c990SLois Curfman McInnes Options Database Keys: 1841a6570f20SBarry Smith + -snes_monitor - sets SNESMonitorDefault() 1842a6570f20SBarry Smith . -snes_monitor_draw - sets line graph monitor, 1843a6570f20SBarry Smith uses SNESMonitorLGCreate() 1844a6570f20SBarry Smith _ -snes_monitor_cancel - cancels all monitors that have 1845c7afd0dbSLois Curfman McInnes been hardwired into a code by 1846a6570f20SBarry Smith calls to SNESMonitorSet(), but 1847c7afd0dbSLois Curfman McInnes does not cancel those set via 1848c7afd0dbSLois Curfman McInnes the options database. 18499665c990SLois Curfman McInnes 1850639f9d9dSBarry Smith Notes: 18516bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 1852a6570f20SBarry Smith SNESMonitorSet() multiple times; all will be called in the 18536bc08f3fSLois Curfman McInnes order in which they were set. 1854639f9d9dSBarry Smith 1855025f1a04SBarry Smith Fortran notes: Only a single monitor function can be set for each SNES object 1856025f1a04SBarry Smith 185736851e7fSLois Curfman McInnes Level: intermediate 185836851e7fSLois Curfman McInnes 18599b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 18609b94acceSBarry Smith 1861a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel() 18629b94acceSBarry Smith @*/ 1863b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 18649b94acceSBarry Smith { 1865b90d0a6eSBarry Smith PetscInt i; 1866b90d0a6eSBarry Smith 18673a40ed3dSBarry Smith PetscFunctionBegin; 1868*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1869639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 187029bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1871639f9d9dSBarry Smith } 1872b90d0a6eSBarry Smith for (i=0; i<snes->numbermonitors;i++) { 1873b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0); 1874b90d0a6eSBarry Smith 1875b90d0a6eSBarry Smith /* check if both default monitors that share common ASCII viewer */ 1876b90d0a6eSBarry Smith if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) { 1877b90d0a6eSBarry Smith if (mctx && snes->monitorcontext[i]) { 1878b90d0a6eSBarry Smith PetscErrorCode ierr; 1879b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx; 1880b90d0a6eSBarry Smith PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i]; 1881b90d0a6eSBarry Smith if (viewer1->viewer == viewer2->viewer) { 1882b90d0a6eSBarry Smith ierr = (*monitordestroy)(mctx);CHKERRQ(ierr); 1883b90d0a6eSBarry Smith PetscFunctionReturn(0); 1884b90d0a6eSBarry Smith } 1885b90d0a6eSBarry Smith } 1886b90d0a6eSBarry Smith } 1887b90d0a6eSBarry Smith } 1888b90d0a6eSBarry Smith snes->monitor[snes->numbermonitors] = monitor; 1889b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1890639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 18913a40ed3dSBarry Smith PetscFunctionReturn(0); 18929b94acceSBarry Smith } 18939b94acceSBarry Smith 18944a2ae208SSatish Balay #undef __FUNCT__ 1895a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel" 18965cd90555SBarry Smith /*@C 1897a6570f20SBarry Smith SNESMonitorCancel - Clears all the monitor functions for a SNES object. 18985cd90555SBarry Smith 1899c7afd0dbSLois Curfman McInnes Collective on SNES 1900c7afd0dbSLois Curfman McInnes 19015cd90555SBarry Smith Input Parameters: 19025cd90555SBarry Smith . snes - the SNES context 19035cd90555SBarry Smith 19041a480d89SAdministrator Options Database Key: 1905a6570f20SBarry Smith . -snes_monitor_cancel - cancels all monitors that have been hardwired 1906a6570f20SBarry Smith into a code by calls to SNESMonitorSet(), but does not cancel those 1907c7afd0dbSLois Curfman McInnes set via the options database 19085cd90555SBarry Smith 19095cd90555SBarry Smith Notes: 19105cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 19115cd90555SBarry Smith 191236851e7fSLois Curfman McInnes Level: intermediate 191336851e7fSLois Curfman McInnes 19145cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 19155cd90555SBarry Smith 1916a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet() 19175cd90555SBarry Smith @*/ 1918a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes) 19195cd90555SBarry Smith { 1920d952e501SBarry Smith PetscErrorCode ierr; 1921d952e501SBarry Smith PetscInt i; 1922d952e501SBarry Smith 19235cd90555SBarry Smith PetscFunctionBegin; 1924*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 1925d952e501SBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1926d952e501SBarry Smith if (snes->monitordestroy[i]) { 1927d952e501SBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1928d952e501SBarry Smith } 1929d952e501SBarry Smith } 19305cd90555SBarry Smith snes->numbermonitors = 0; 19315cd90555SBarry Smith PetscFunctionReturn(0); 19325cd90555SBarry Smith } 19335cd90555SBarry Smith 19344a2ae208SSatish Balay #undef __FUNCT__ 19354a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 19369b94acceSBarry Smith /*@C 19379b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 19389b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 19399b94acceSBarry Smith 1940fee21e36SBarry Smith Collective on SNES 1941fee21e36SBarry Smith 1942c7afd0dbSLois Curfman McInnes Input Parameters: 1943c7afd0dbSLois Curfman McInnes + snes - the SNES context 1944c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 19457f7931b9SBarry Smith . cctx - [optional] context for private data for the convergence routine (may be PETSC_NULL) 19467f7931b9SBarry Smith - destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran) 19479b94acceSBarry Smith 1948c7afd0dbSLois Curfman McInnes Calling sequence of func: 194906ee9f85SBarry Smith $ PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1950c7afd0dbSLois Curfman McInnes 1951c7afd0dbSLois Curfman McInnes + snes - the SNES context 195206ee9f85SBarry Smith . it - current iteration (0 is the first and is before any Newton step) 1953c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1954184914b5SBarry Smith . reason - reason for convergence/divergence 1955c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 19564b27c08aSLois Curfman McInnes . gnorm - 2-norm of current step 19574b27c08aSLois Curfman McInnes - f - 2-norm of function 19589b94acceSBarry Smith 195936851e7fSLois Curfman McInnes Level: advanced 196036851e7fSLois Curfman McInnes 19619b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 19629b94acceSBarry Smith 196385385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged() 19649b94acceSBarry Smith @*/ 19657f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*)) 19669b94acceSBarry Smith { 19677f7931b9SBarry Smith PetscErrorCode ierr; 19687f7931b9SBarry Smith 19693a40ed3dSBarry Smith PetscFunctionBegin; 1970*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 197185385478SLisandro Dalcin if (!func) func = SNESSkipConverged; 19727f7931b9SBarry Smith if (snes->ops->convergeddestroy) { 19737f7931b9SBarry Smith ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr); 19747f7931b9SBarry Smith } 197585385478SLisandro Dalcin snes->ops->converged = func; 19767f7931b9SBarry Smith snes->ops->convergeddestroy = destroy; 197785385478SLisandro Dalcin snes->cnvP = cctx; 19783a40ed3dSBarry Smith PetscFunctionReturn(0); 19799b94acceSBarry Smith } 19809b94acceSBarry Smith 19814a2ae208SSatish Balay #undef __FUNCT__ 19824a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 198352baeb72SSatish Balay /*@ 1984184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1985184914b5SBarry Smith 1986184914b5SBarry Smith Not Collective 1987184914b5SBarry Smith 1988184914b5SBarry Smith Input Parameter: 1989184914b5SBarry Smith . snes - the SNES context 1990184914b5SBarry Smith 1991184914b5SBarry Smith Output Parameter: 19924d0a8057SBarry Smith . reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the 1993184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1994184914b5SBarry Smith 1995184914b5SBarry Smith Level: intermediate 1996184914b5SBarry Smith 1997184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1998184914b5SBarry Smith 1999184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 2000184914b5SBarry Smith 200185385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason 2002184914b5SBarry Smith @*/ 200363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 2004184914b5SBarry Smith { 2005184914b5SBarry Smith PetscFunctionBegin; 2006*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 20074482741eSBarry Smith PetscValidPointer(reason,2); 2008184914b5SBarry Smith *reason = snes->reason; 2009184914b5SBarry Smith PetscFunctionReturn(0); 2010184914b5SBarry Smith } 2011184914b5SBarry Smith 20124a2ae208SSatish Balay #undef __FUNCT__ 20134a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 2014c9005455SLois Curfman McInnes /*@ 2015c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 2016c9005455SLois Curfman McInnes 2017fee21e36SBarry Smith Collective on SNES 2018fee21e36SBarry Smith 2019c7afd0dbSLois Curfman McInnes Input Parameters: 2020c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 20218c7482ecSBarry Smith . a - array to hold history, this array will contain the function norms computed at each step 2022cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 2023758f92a0SBarry Smith . na - size of a and its 202464731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 2025758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 2026c7afd0dbSLois Curfman McInnes 2027c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 2028c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 2029c9005455SLois Curfman McInnes during the section of code that is being timed. 2030c9005455SLois Curfman McInnes 203136851e7fSLois Curfman McInnes Level: intermediate 203236851e7fSLois Curfman McInnes 2033c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 2034758f92a0SBarry Smith 203508405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 2036758f92a0SBarry Smith 2037c9005455SLois Curfman McInnes @*/ 2038a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset) 2039c9005455SLois Curfman McInnes { 20403a40ed3dSBarry Smith PetscFunctionBegin; 2041*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 20424482741eSBarry Smith if (na) PetscValidScalarPointer(a,2); 2043a562a398SLisandro Dalcin if (its) PetscValidIntPointer(its,3); 2044c9005455SLois Curfman McInnes snes->conv_hist = a; 2045758f92a0SBarry Smith snes->conv_hist_its = its; 2046758f92a0SBarry Smith snes->conv_hist_max = na; 2047a12bc48fSLisandro Dalcin snes->conv_hist_len = 0; 2048758f92a0SBarry Smith snes->conv_hist_reset = reset; 2049758f92a0SBarry Smith PetscFunctionReturn(0); 2050758f92a0SBarry Smith } 2051758f92a0SBarry Smith 20524a2ae208SSatish Balay #undef __FUNCT__ 20534a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 20540c4c9dddSBarry Smith /*@C 2055758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 2056758f92a0SBarry Smith 2057758f92a0SBarry Smith Collective on SNES 2058758f92a0SBarry Smith 2059758f92a0SBarry Smith Input Parameter: 2060758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 2061758f92a0SBarry Smith 2062758f92a0SBarry Smith Output Parameters: 2063758f92a0SBarry Smith . a - array to hold history 2064758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 2065758f92a0SBarry Smith negative if not converged) for each solve. 2066758f92a0SBarry Smith - na - size of a and its 2067758f92a0SBarry Smith 2068758f92a0SBarry Smith Notes: 2069758f92a0SBarry Smith The calling sequence for this routine in Fortran is 2070758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 2071758f92a0SBarry Smith 2072758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 2073758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 2074758f92a0SBarry Smith during the section of code that is being timed. 2075758f92a0SBarry Smith 2076758f92a0SBarry Smith Level: intermediate 2077758f92a0SBarry Smith 2078758f92a0SBarry Smith .keywords: SNES, get, convergence, history 2079758f92a0SBarry Smith 2080758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 2081758f92a0SBarry Smith 2082758f92a0SBarry Smith @*/ 208363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na) 2084758f92a0SBarry Smith { 2085758f92a0SBarry Smith PetscFunctionBegin; 2086*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2087758f92a0SBarry Smith if (a) *a = snes->conv_hist; 2088758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 2089758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 20903a40ed3dSBarry Smith PetscFunctionReturn(0); 2091c9005455SLois Curfman McInnes } 2092c9005455SLois Curfman McInnes 2093e74ef692SMatthew Knepley #undef __FUNCT__ 2094e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 2095ac226902SBarry Smith /*@C 209676b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 20977e4bb74cSBarry Smith at the beginning o every iteration of the nonlinear solve. Specifically 20987e4bb74cSBarry Smith it is called just before the Jacobian is "evaluated". 209976b2cf59SMatthew Knepley 210076b2cf59SMatthew Knepley Collective on SNES 210176b2cf59SMatthew Knepley 210276b2cf59SMatthew Knepley Input Parameters: 210376b2cf59SMatthew Knepley . snes - The nonlinear solver context 210476b2cf59SMatthew Knepley . func - The function 210576b2cf59SMatthew Knepley 210676b2cf59SMatthew Knepley Calling sequence of func: 2107b5d30489SBarry Smith . func (SNES snes, PetscInt step); 210876b2cf59SMatthew Knepley 210976b2cf59SMatthew Knepley . step - The current step of the iteration 211076b2cf59SMatthew Knepley 2111fe97e370SBarry Smith Level: advanced 2112fe97e370SBarry Smith 2113fe97e370SBarry 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() 2114fe97e370SBarry Smith This is not used by most users. 211576b2cf59SMatthew Knepley 211676b2cf59SMatthew Knepley .keywords: SNES, update 2117b5d30489SBarry Smith 211885385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve() 211976b2cf59SMatthew Knepley @*/ 212063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt)) 212176b2cf59SMatthew Knepley { 212276b2cf59SMatthew Knepley PetscFunctionBegin; 2123*0700a824SBarry Smith PetscValidHeaderSpecific(snes, SNES_CLASSID,1); 2124e7788613SBarry Smith snes->ops->update = func; 212576b2cf59SMatthew Knepley PetscFunctionReturn(0); 212676b2cf59SMatthew Knepley } 212776b2cf59SMatthew Knepley 2128e74ef692SMatthew Knepley #undef __FUNCT__ 2129e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 213076b2cf59SMatthew Knepley /*@ 213176b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 213276b2cf59SMatthew Knepley 213376b2cf59SMatthew Knepley Not collective 213476b2cf59SMatthew Knepley 213576b2cf59SMatthew Knepley Input Parameters: 213676b2cf59SMatthew Knepley . snes - The nonlinear solver context 213776b2cf59SMatthew Knepley . step - The current step of the iteration 213876b2cf59SMatthew Knepley 2139205452f4SMatthew Knepley Level: intermediate 2140205452f4SMatthew Knepley 214176b2cf59SMatthew Knepley .keywords: SNES, update 2142a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC() 214376b2cf59SMatthew Knepley @*/ 214463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step) 214576b2cf59SMatthew Knepley { 214676b2cf59SMatthew Knepley PetscFunctionBegin; 214776b2cf59SMatthew Knepley PetscFunctionReturn(0); 214876b2cf59SMatthew Knepley } 214976b2cf59SMatthew Knepley 21504a2ae208SSatish Balay #undef __FUNCT__ 21514a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 21529b94acceSBarry Smith /* 21539b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 21549b94acceSBarry Smith positive parameter delta. 21559b94acceSBarry Smith 21569b94acceSBarry Smith Input Parameters: 2157c7afd0dbSLois Curfman McInnes + snes - the SNES context 21589b94acceSBarry Smith . y - approximate solution of linear system 21599b94acceSBarry Smith . fnorm - 2-norm of current function 2160c7afd0dbSLois Curfman McInnes - delta - trust region size 21619b94acceSBarry Smith 21629b94acceSBarry Smith Output Parameters: 2163c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 21649b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 21659b94acceSBarry Smith region, and exceeds zero otherwise. 2166c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 21679b94acceSBarry Smith 21689b94acceSBarry Smith Note: 21694b27c08aSLois Curfman McInnes For non-trust region methods such as SNESLS, the parameter delta 21709b94acceSBarry Smith is set to be the maximum allowable step size. 21719b94acceSBarry Smith 21729b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 21739b94acceSBarry Smith */ 2174dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm) 21759b94acceSBarry Smith { 2176064f8208SBarry Smith PetscReal nrm; 2177ea709b57SSatish Balay PetscScalar cnorm; 2178dfbe8321SBarry Smith PetscErrorCode ierr; 21793a40ed3dSBarry Smith 21803a40ed3dSBarry Smith PetscFunctionBegin; 2181*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2182*0700a824SBarry Smith PetscValidHeaderSpecific(y,VEC_CLASSID,2); 2183c9780b6fSBarry Smith PetscCheckSameComm(snes,1,y,2); 2184184914b5SBarry Smith 2185064f8208SBarry Smith ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr); 2186064f8208SBarry Smith if (nrm > *delta) { 2187064f8208SBarry Smith nrm = *delta/nrm; 2188064f8208SBarry Smith *gpnorm = (1.0 - nrm)*(*fnorm); 2189064f8208SBarry Smith cnorm = nrm; 21902dcb1b2aSMatthew Knepley ierr = VecScale(y,cnorm);CHKERRQ(ierr); 21919b94acceSBarry Smith *ynorm = *delta; 21929b94acceSBarry Smith } else { 21939b94acceSBarry Smith *gpnorm = 0.0; 2194064f8208SBarry Smith *ynorm = nrm; 21959b94acceSBarry Smith } 21963a40ed3dSBarry Smith PetscFunctionReturn(0); 21979b94acceSBarry Smith } 21989b94acceSBarry Smith 21994a2ae208SSatish Balay #undef __FUNCT__ 22004a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 22016ce558aeSBarry Smith /*@C 2202f69a0ea3SMatthew Knepley SNESSolve - Solves a nonlinear system F(x) = b. 2203f69a0ea3SMatthew Knepley Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX(). 22049b94acceSBarry Smith 2205c7afd0dbSLois Curfman McInnes Collective on SNES 2206c7afd0dbSLois Curfman McInnes 2207b2002411SLois Curfman McInnes Input Parameters: 2208c7afd0dbSLois Curfman McInnes + snes - the SNES context 2209f69a0ea3SMatthew Knepley . b - the constant part of the equation, or PETSC_NULL to use zero. 221085385478SLisandro Dalcin - x - the solution vector. 22119b94acceSBarry Smith 2212b2002411SLois Curfman McInnes Notes: 22138ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 22148ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 22158ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 22168ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 22178ddd3da0SLois Curfman McInnes 221836851e7fSLois Curfman McInnes Level: beginner 221936851e7fSLois Curfman McInnes 22209b94acceSBarry Smith .keywords: SNES, nonlinear, solve 22219b94acceSBarry Smith 222285385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian() 22239b94acceSBarry Smith @*/ 2224f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x) 22259b94acceSBarry Smith { 2226dfbe8321SBarry Smith PetscErrorCode ierr; 2227f1af5d2fSBarry Smith PetscTruth flg; 2228eabae89aSBarry Smith char filename[PETSC_MAX_PATH_LEN]; 2229eabae89aSBarry Smith PetscViewer viewer; 2230052efed2SBarry Smith 22313a40ed3dSBarry Smith PetscFunctionBegin; 2232*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2233*0700a824SBarry Smith PetscValidHeaderSpecific(x,VEC_CLASSID,3); 2234f69a0ea3SMatthew Knepley PetscCheckSameComm(snes,1,x,3); 2235*0700a824SBarry Smith if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2); 223685385478SLisandro Dalcin if (b) PetscCheckSameComm(snes,1,b,2); 223785385478SLisandro Dalcin 223885385478SLisandro Dalcin /* set solution vector */ 223985385478SLisandro Dalcin ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr); 224085385478SLisandro Dalcin if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); } 224185385478SLisandro Dalcin snes->vec_sol = x; 224285385478SLisandro Dalcin /* set afine vector if provided */ 224385385478SLisandro Dalcin if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); } 224485385478SLisandro Dalcin if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); } 224585385478SLisandro Dalcin snes->vec_rhs = b; 224685385478SLisandro Dalcin 224785385478SLisandro Dalcin if (!snes->vec_func && snes->vec_rhs) { 224885385478SLisandro Dalcin ierr = VecDuplicate(b, &snes->vec_func);CHKERRQ(ierr); 224970e92668SMatthew Knepley } 22503f149594SLisandro Dalcin 225170e92668SMatthew Knepley ierr = SNESSetUp(snes);CHKERRQ(ierr); 22523f149594SLisandro Dalcin 2253abc0a331SBarry Smith if (snes->conv_hist_reset) snes->conv_hist_len = 0; 225450ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 2255d5e45103SBarry Smith 22563f149594SLisandro Dalcin ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 22574936397dSBarry Smith ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr); 225885385478SLisandro Dalcin ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 22594936397dSBarry Smith if (snes->domainerror){ 22604936397dSBarry Smith snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 22614936397dSBarry Smith snes->domainerror = PETSC_FALSE; 22624936397dSBarry Smith } 226385385478SLisandro Dalcin 22643f149594SLisandro Dalcin if (!snes->reason) { 22653f149594SLisandro Dalcin SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); 22663f149594SLisandro Dalcin } 22673f149594SLisandro Dalcin 22687adad957SLisandro Dalcin ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 2269eabae89aSBarry Smith if (flg && !PetscPreLoadingOn) { 22707adad957SLisandro Dalcin ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr); 2271eabae89aSBarry Smith ierr = SNESView(snes,viewer);CHKERRQ(ierr); 2272eabae89aSBarry Smith ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 2273eabae89aSBarry Smith } 2274eabae89aSBarry Smith 227590d69ab7SBarry Smith flg = PETSC_FALSE; 227690d69ab7SBarry Smith ierr = PetscOptionsGetTruth(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr); 2277da9b6338SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); } 22785968eb51SBarry Smith if (snes->printreason) { 22795968eb51SBarry Smith if (snes->reason > 0) { 22807adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 22815968eb51SBarry Smith } else { 22827adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr); 22835968eb51SBarry Smith } 22845968eb51SBarry Smith } 22855968eb51SBarry Smith 22863a40ed3dSBarry Smith PetscFunctionReturn(0); 22879b94acceSBarry Smith } 22889b94acceSBarry Smith 22899b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 22909b94acceSBarry Smith 22914a2ae208SSatish Balay #undef __FUNCT__ 22924a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 229382bf6240SBarry Smith /*@C 22944b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 22959b94acceSBarry Smith 2296fee21e36SBarry Smith Collective on SNES 2297fee21e36SBarry Smith 2298c7afd0dbSLois Curfman McInnes Input Parameters: 2299c7afd0dbSLois Curfman McInnes + snes - the SNES context 2300454a90a3SBarry Smith - type - a known method 2301c7afd0dbSLois Curfman McInnes 2302c7afd0dbSLois Curfman McInnes Options Database Key: 2303454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2304c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2305ae12b187SLois Curfman McInnes 23069b94acceSBarry Smith Notes: 2307e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 23084b27c08aSLois Curfman McInnes + SNESLS - Newton's method with line search 2309c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 23104b27c08aSLois Curfman McInnes . SNESTR - Newton's method with trust region 2311c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 23129b94acceSBarry Smith 2313ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2314ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2315ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2316ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2317ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2318ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2319ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2320ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2321ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2322b0a32e0cSBarry Smith appropriate method. 232336851e7fSLois Curfman McInnes 232436851e7fSLois Curfman McInnes Level: intermediate 2325a703fe33SLois Curfman McInnes 2326454a90a3SBarry Smith .keywords: SNES, set, type 2327435da068SBarry Smith 2328435da068SBarry Smith .seealso: SNESType, SNESCreate() 2329435da068SBarry Smith 23309b94acceSBarry Smith @*/ 2331a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type) 23329b94acceSBarry Smith { 2333dfbe8321SBarry Smith PetscErrorCode ierr,(*r)(SNES); 23346831982aSBarry Smith PetscTruth match; 23353a40ed3dSBarry Smith 23363a40ed3dSBarry Smith PetscFunctionBegin; 2337*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 23384482741eSBarry Smith PetscValidCharPointer(type,2); 233982bf6240SBarry Smith 23406831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 23410f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 234292ff6ae8SBarry Smith 23437adad957SLisandro Dalcin ierr = PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 2344958c9bccSBarry Smith if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type); 234575396ef9SLisandro Dalcin /* Destroy the previous private SNES context */ 234675396ef9SLisandro Dalcin if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); } 234775396ef9SLisandro Dalcin /* Reinitialize function pointers in SNESOps structure */ 234875396ef9SLisandro Dalcin snes->ops->setup = 0; 234975396ef9SLisandro Dalcin snes->ops->solve = 0; 235075396ef9SLisandro Dalcin snes->ops->view = 0; 235175396ef9SLisandro Dalcin snes->ops->setfromoptions = 0; 235275396ef9SLisandro Dalcin snes->ops->destroy = 0; 235375396ef9SLisandro Dalcin /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */ 235475396ef9SLisandro Dalcin snes->setupcalled = PETSC_FALSE; 23553a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 2356454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 23573a40ed3dSBarry Smith PetscFunctionReturn(0); 23589b94acceSBarry Smith } 23599b94acceSBarry Smith 2360a847f771SSatish Balay 23619b94acceSBarry Smith /* --------------------------------------------------------------------- */ 23624a2ae208SSatish Balay #undef __FUNCT__ 23634a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 236452baeb72SSatish Balay /*@ 23659b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2366f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 23679b94acceSBarry Smith 2368fee21e36SBarry Smith Not Collective 2369fee21e36SBarry Smith 237036851e7fSLois Curfman McInnes Level: advanced 237136851e7fSLois Curfman McInnes 23729b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 23739b94acceSBarry Smith 23749b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 23759b94acceSBarry Smith @*/ 237663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void) 23779b94acceSBarry Smith { 2378dfbe8321SBarry Smith PetscErrorCode ierr; 237982bf6240SBarry Smith 23803a40ed3dSBarry Smith PetscFunctionBegin; 23811441b1d3SBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 23824c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 23833a40ed3dSBarry Smith PetscFunctionReturn(0); 23849b94acceSBarry Smith } 23859b94acceSBarry Smith 23864a2ae208SSatish Balay #undef __FUNCT__ 23874a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 23889b94acceSBarry Smith /*@C 23899a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 23909b94acceSBarry Smith 2391c7afd0dbSLois Curfman McInnes Not Collective 2392c7afd0dbSLois Curfman McInnes 23939b94acceSBarry Smith Input Parameter: 23944b0e389bSBarry Smith . snes - nonlinear solver context 23959b94acceSBarry Smith 23969b94acceSBarry Smith Output Parameter: 23973a7fca6bSBarry Smith . type - SNES method (a character string) 23989b94acceSBarry Smith 239936851e7fSLois Curfman McInnes Level: intermediate 240036851e7fSLois Curfman McInnes 2401454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 24029b94acceSBarry Smith @*/ 2403a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type) 24049b94acceSBarry Smith { 24053a40ed3dSBarry Smith PetscFunctionBegin; 2406*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 24074482741eSBarry Smith PetscValidPointer(type,2); 24087adad957SLisandro Dalcin *type = ((PetscObject)snes)->type_name; 24093a40ed3dSBarry Smith PetscFunctionReturn(0); 24109b94acceSBarry Smith } 24119b94acceSBarry Smith 24124a2ae208SSatish Balay #undef __FUNCT__ 24134a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 241452baeb72SSatish Balay /*@ 24159b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 24169b94acceSBarry Smith stored. 24179b94acceSBarry Smith 2418c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2419c7afd0dbSLois Curfman McInnes 24209b94acceSBarry Smith Input Parameter: 24219b94acceSBarry Smith . snes - the SNES context 24229b94acceSBarry Smith 24239b94acceSBarry Smith Output Parameter: 24249b94acceSBarry Smith . x - the solution 24259b94acceSBarry Smith 242670e92668SMatthew Knepley Level: intermediate 242736851e7fSLois Curfman McInnes 24289b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 24299b94acceSBarry Smith 243085385478SLisandro Dalcin .seealso: SNESGetSolutionUpdate(), SNESGetFunction() 24319b94acceSBarry Smith @*/ 243263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x) 24339b94acceSBarry Smith { 24343a40ed3dSBarry Smith PetscFunctionBegin; 2435*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 24364482741eSBarry Smith PetscValidPointer(x,2); 243785385478SLisandro Dalcin *x = snes->vec_sol; 243870e92668SMatthew Knepley PetscFunctionReturn(0); 243970e92668SMatthew Knepley } 244070e92668SMatthew Knepley 244170e92668SMatthew Knepley #undef __FUNCT__ 24424a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 244352baeb72SSatish Balay /*@ 24449b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 24459b94acceSBarry Smith stored. 24469b94acceSBarry Smith 2447c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2448c7afd0dbSLois Curfman McInnes 24499b94acceSBarry Smith Input Parameter: 24509b94acceSBarry Smith . snes - the SNES context 24519b94acceSBarry Smith 24529b94acceSBarry Smith Output Parameter: 24539b94acceSBarry Smith . x - the solution update 24549b94acceSBarry Smith 245536851e7fSLois Curfman McInnes Level: advanced 245636851e7fSLois Curfman McInnes 24579b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 24589b94acceSBarry Smith 245985385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction() 24609b94acceSBarry Smith @*/ 246163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x) 24629b94acceSBarry Smith { 24633a40ed3dSBarry Smith PetscFunctionBegin; 2464*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 24654482741eSBarry Smith PetscValidPointer(x,2); 246685385478SLisandro Dalcin *x = snes->vec_sol_update; 24673a40ed3dSBarry Smith PetscFunctionReturn(0); 24689b94acceSBarry Smith } 24699b94acceSBarry Smith 24704a2ae208SSatish Balay #undef __FUNCT__ 24714a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 24729b94acceSBarry Smith /*@C 24733638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 24749b94acceSBarry Smith 2475c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2476c7afd0dbSLois Curfman McInnes 24779b94acceSBarry Smith Input Parameter: 24789b94acceSBarry Smith . snes - the SNES context 24799b94acceSBarry Smith 24809b94acceSBarry Smith Output Parameter: 24817bf4e008SBarry Smith + r - the function (or PETSC_NULL) 248270e92668SMatthew Knepley . func - the function (or PETSC_NULL) 248370e92668SMatthew Knepley - ctx - the function context (or PETSC_NULL) 24849b94acceSBarry Smith 248536851e7fSLois Curfman McInnes Level: advanced 248636851e7fSLois Curfman McInnes 2487a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 24889b94acceSBarry Smith 24894b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution() 24909b94acceSBarry Smith @*/ 249170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx) 24929b94acceSBarry Smith { 24933a40ed3dSBarry Smith PetscFunctionBegin; 2494*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 249585385478SLisandro Dalcin if (r) *r = snes->vec_func; 2496e7788613SBarry Smith if (func) *func = snes->ops->computefunction; 249770e92668SMatthew Knepley if (ctx) *ctx = snes->funP; 24983a40ed3dSBarry Smith PetscFunctionReturn(0); 24999b94acceSBarry Smith } 25009b94acceSBarry Smith 25014a2ae208SSatish Balay #undef __FUNCT__ 25024a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 25033c7409f5SSatish Balay /*@C 25043c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2505d850072dSLois Curfman McInnes SNES options in the database. 25063c7409f5SSatish Balay 2507fee21e36SBarry Smith Collective on SNES 2508fee21e36SBarry Smith 2509c7afd0dbSLois Curfman McInnes Input Parameter: 2510c7afd0dbSLois Curfman McInnes + snes - the SNES context 2511c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2512c7afd0dbSLois Curfman McInnes 2513d850072dSLois Curfman McInnes Notes: 2514a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2515c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2516d850072dSLois Curfman McInnes 251736851e7fSLois Curfman McInnes Level: advanced 251836851e7fSLois Curfman McInnes 25193c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2520a86d99e1SLois Curfman McInnes 2521a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 25223c7409f5SSatish Balay @*/ 252363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[]) 25243c7409f5SSatish Balay { 2525dfbe8321SBarry Smith PetscErrorCode ierr; 25263c7409f5SSatish Balay 25273a40ed3dSBarry Smith PetscFunctionBegin; 2528*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2529639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 25301cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 253194b7f48cSBarry Smith ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 25323a40ed3dSBarry Smith PetscFunctionReturn(0); 25333c7409f5SSatish Balay } 25343c7409f5SSatish Balay 25354a2ae208SSatish Balay #undef __FUNCT__ 25364a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 25373c7409f5SSatish Balay /*@C 2538f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2539d850072dSLois Curfman McInnes SNES options in the database. 25403c7409f5SSatish Balay 2541fee21e36SBarry Smith Collective on SNES 2542fee21e36SBarry Smith 2543c7afd0dbSLois Curfman McInnes Input Parameters: 2544c7afd0dbSLois Curfman McInnes + snes - the SNES context 2545c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2546c7afd0dbSLois Curfman McInnes 2547d850072dSLois Curfman McInnes Notes: 2548a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2549c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2550d850072dSLois Curfman McInnes 255136851e7fSLois Curfman McInnes Level: advanced 255236851e7fSLois Curfman McInnes 25533c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2554a86d99e1SLois Curfman McInnes 2555a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 25563c7409f5SSatish Balay @*/ 255763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[]) 25583c7409f5SSatish Balay { 2559dfbe8321SBarry Smith PetscErrorCode ierr; 25603c7409f5SSatish Balay 25613a40ed3dSBarry Smith PetscFunctionBegin; 2562*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2563639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 25641cee3971SBarry Smith if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);} 256594b7f48cSBarry Smith ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr); 25663a40ed3dSBarry Smith PetscFunctionReturn(0); 25673c7409f5SSatish Balay } 25683c7409f5SSatish Balay 25694a2ae208SSatish Balay #undef __FUNCT__ 25704a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 25719ab63eb5SSatish Balay /*@C 25723c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 25733c7409f5SSatish Balay SNES options in the database. 25743c7409f5SSatish Balay 2575c7afd0dbSLois Curfman McInnes Not Collective 2576c7afd0dbSLois Curfman McInnes 25773c7409f5SSatish Balay Input Parameter: 25783c7409f5SSatish Balay . snes - the SNES context 25793c7409f5SSatish Balay 25803c7409f5SSatish Balay Output Parameter: 25813c7409f5SSatish Balay . prefix - pointer to the prefix string used 25823c7409f5SSatish Balay 25834ef407dbSRichard Tran Mills Notes: On the fortran side, the user should pass in a string 'prefix' of 25849ab63eb5SSatish Balay sufficient length to hold the prefix. 25859ab63eb5SSatish Balay 258636851e7fSLois Curfman McInnes Level: advanced 258736851e7fSLois Curfman McInnes 25883c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2589a86d99e1SLois Curfman McInnes 2590a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 25913c7409f5SSatish Balay @*/ 2592e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[]) 25933c7409f5SSatish Balay { 2594dfbe8321SBarry Smith PetscErrorCode ierr; 25953c7409f5SSatish Balay 25963a40ed3dSBarry Smith PetscFunctionBegin; 2597*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2598639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 25993a40ed3dSBarry Smith PetscFunctionReturn(0); 26003c7409f5SSatish Balay } 26013c7409f5SSatish Balay 2602b2002411SLois Curfman McInnes 26034a2ae208SSatish Balay #undef __FUNCT__ 26044a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 26053cea93caSBarry Smith /*@C 26063cea93caSBarry Smith SNESRegister - See SNESRegisterDynamic() 26073cea93caSBarry Smith 26087f6c08e0SMatthew Knepley Level: advanced 26093cea93caSBarry Smith @*/ 261063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES)) 2611b2002411SLois Curfman McInnes { 2612e2d1d2b7SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2613dfbe8321SBarry Smith PetscErrorCode ierr; 2614b2002411SLois Curfman McInnes 2615b2002411SLois Curfman McInnes PetscFunctionBegin; 2616b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2617c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2618b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2619b2002411SLois Curfman McInnes } 2620da9b6338SBarry Smith 2621da9b6338SBarry Smith #undef __FUNCT__ 2622da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin" 262363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes) 2624da9b6338SBarry Smith { 2625dfbe8321SBarry Smith PetscErrorCode ierr; 262677431f27SBarry Smith PetscInt N,i,j; 2627da9b6338SBarry Smith Vec u,uh,fh; 2628da9b6338SBarry Smith PetscScalar value; 2629da9b6338SBarry Smith PetscReal norm; 2630da9b6338SBarry Smith 2631da9b6338SBarry Smith PetscFunctionBegin; 2632da9b6338SBarry Smith ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr); 2633da9b6338SBarry Smith ierr = VecDuplicate(u,&uh);CHKERRQ(ierr); 2634da9b6338SBarry Smith ierr = VecDuplicate(u,&fh);CHKERRQ(ierr); 2635da9b6338SBarry Smith 2636da9b6338SBarry Smith /* currently only works for sequential */ 2637da9b6338SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n"); 2638da9b6338SBarry Smith ierr = VecGetSize(u,&N);CHKERRQ(ierr); 2639da9b6338SBarry Smith for (i=0; i<N; i++) { 2640da9b6338SBarry Smith ierr = VecCopy(u,uh);CHKERRQ(ierr); 264177431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr); 2642da9b6338SBarry Smith for (j=-10; j<11; j++) { 2643ccae9161SBarry Smith value = PetscSign(j)*exp(PetscAbs(j)-10.0); 2644da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 26453ab0aad5SBarry Smith ierr = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr); 2646da9b6338SBarry Smith ierr = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr); 264777431f27SBarry Smith ierr = PetscPrintf(PETSC_COMM_WORLD," j norm %D %18.16e\n",j,norm);CHKERRQ(ierr); 2648da9b6338SBarry Smith value = -value; 2649da9b6338SBarry Smith ierr = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr); 2650da9b6338SBarry Smith } 2651da9b6338SBarry Smith } 2652da9b6338SBarry Smith ierr = VecDestroy(uh);CHKERRQ(ierr); 2653da9b6338SBarry Smith ierr = VecDestroy(fh);CHKERRQ(ierr); 2654da9b6338SBarry Smith PetscFunctionReturn(0); 2655da9b6338SBarry Smith } 265671f87433Sdalcinl 265771f87433Sdalcinl #undef __FUNCT__ 2658fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW" 265971f87433Sdalcinl /*@ 2660fa9f3622SBarry Smith SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for 266171f87433Sdalcinl computing relative tolerance for linear solvers within an inexact 266271f87433Sdalcinl Newton method. 266371f87433Sdalcinl 266471f87433Sdalcinl Collective on SNES 266571f87433Sdalcinl 266671f87433Sdalcinl Input Parameters: 266771f87433Sdalcinl + snes - SNES context 266871f87433Sdalcinl - flag - PETSC_TRUE or PETSC_FALSE 266971f87433Sdalcinl 267064ba62caSBarry Smith Options Database: 267164ba62caSBarry Smith + -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence 267264ba62caSBarry Smith . -snes_ksp_ew_version ver - version of Eisenstat-Walker method 267364ba62caSBarry Smith . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 267464ba62caSBarry Smith . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 267564ba62caSBarry Smith . -snes_ksp_ew_gamma <gamma> - Sets gamma 267664ba62caSBarry Smith . -snes_ksp_ew_alpha <alpha> - Sets alpha 267764ba62caSBarry Smith . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 267864ba62caSBarry Smith - -snes_ksp_ew_threshold <threshold> - Sets threshold 267964ba62caSBarry Smith 268071f87433Sdalcinl Notes: 268171f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 268271f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 268371f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 268471f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 268571f87433Sdalcinl solver. 268671f87433Sdalcinl 268771f87433Sdalcinl Level: advanced 268871f87433Sdalcinl 268971f87433Sdalcinl Reference: 269071f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 269171f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 269271f87433Sdalcinl 269371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 269471f87433Sdalcinl 2695fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 269671f87433Sdalcinl @*/ 2697fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag) 269871f87433Sdalcinl { 269971f87433Sdalcinl PetscFunctionBegin; 2700*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 270171f87433Sdalcinl snes->ksp_ewconv = flag; 270271f87433Sdalcinl PetscFunctionReturn(0); 270371f87433Sdalcinl } 270471f87433Sdalcinl 270571f87433Sdalcinl #undef __FUNCT__ 2706fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW" 270771f87433Sdalcinl /*@ 2708fa9f3622SBarry Smith SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method 270971f87433Sdalcinl for computing relative tolerance for linear solvers within an 271071f87433Sdalcinl inexact Newton method. 271171f87433Sdalcinl 271271f87433Sdalcinl Not Collective 271371f87433Sdalcinl 271471f87433Sdalcinl Input Parameter: 271571f87433Sdalcinl . snes - SNES context 271671f87433Sdalcinl 271771f87433Sdalcinl Output Parameter: 271871f87433Sdalcinl . flag - PETSC_TRUE or PETSC_FALSE 271971f87433Sdalcinl 272071f87433Sdalcinl Notes: 272171f87433Sdalcinl Currently, the default is to use a constant relative tolerance for 272271f87433Sdalcinl the inner linear solvers. Alternatively, one can use the 272371f87433Sdalcinl Eisenstat-Walker method, where the relative convergence tolerance 272471f87433Sdalcinl is reset at each Newton iteration according progress of the nonlinear 272571f87433Sdalcinl solver. 272671f87433Sdalcinl 272771f87433Sdalcinl Level: advanced 272871f87433Sdalcinl 272971f87433Sdalcinl Reference: 273071f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 273171f87433Sdalcinl inexact Newton method", SISC 17 (1), pp.16-32, 1996. 273271f87433Sdalcinl 273371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 273471f87433Sdalcinl 2735fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW() 273671f87433Sdalcinl @*/ 2737fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag) 273871f87433Sdalcinl { 273971f87433Sdalcinl PetscFunctionBegin; 2740*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 274171f87433Sdalcinl PetscValidPointer(flag,2); 274271f87433Sdalcinl *flag = snes->ksp_ewconv; 274371f87433Sdalcinl PetscFunctionReturn(0); 274471f87433Sdalcinl } 274571f87433Sdalcinl 274671f87433Sdalcinl #undef __FUNCT__ 2747fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW" 274871f87433Sdalcinl /*@ 2749fa9f3622SBarry Smith SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker 275071f87433Sdalcinl convergence criteria for the linear solvers within an inexact 275171f87433Sdalcinl Newton method. 275271f87433Sdalcinl 275371f87433Sdalcinl Collective on SNES 275471f87433Sdalcinl 275571f87433Sdalcinl Input Parameters: 275671f87433Sdalcinl + snes - SNES context 275771f87433Sdalcinl . version - version 1, 2 (default is 2) or 3 275871f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 275971f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 276071f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 276171f87433Sdalcinl (0 <= gamma2 <= 1) 276271f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 276371f87433Sdalcinl . alpha2 - power for safeguard 276471f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 276571f87433Sdalcinl 276671f87433Sdalcinl Note: 276771f87433Sdalcinl Version 3 was contributed by Luis Chacon, June 2006. 276871f87433Sdalcinl 276971f87433Sdalcinl Use PETSC_DEFAULT to retain the default for any of the parameters. 277071f87433Sdalcinl 277171f87433Sdalcinl Level: advanced 277271f87433Sdalcinl 277371f87433Sdalcinl Reference: 277471f87433Sdalcinl S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 277571f87433Sdalcinl inexact Newton method", Utah State University Math. Stat. Dept. Res. 277671f87433Sdalcinl Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 277771f87433Sdalcinl 277871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 277971f87433Sdalcinl 2780fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW() 278171f87433Sdalcinl @*/ 2782fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max, 278371f87433Sdalcinl PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold) 278471f87433Sdalcinl { 2785fa9f3622SBarry Smith SNESKSPEW *kctx; 278671f87433Sdalcinl PetscFunctionBegin; 2787*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2788fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 278971f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 279071f87433Sdalcinl 279171f87433Sdalcinl if (version != PETSC_DEFAULT) kctx->version = version; 279271f87433Sdalcinl if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 279371f87433Sdalcinl if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 279471f87433Sdalcinl if (gamma != PETSC_DEFAULT) kctx->gamma = gamma; 279571f87433Sdalcinl if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 279671f87433Sdalcinl if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 279771f87433Sdalcinl if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 279871f87433Sdalcinl 279971f87433Sdalcinl if (kctx->version < 1 || kctx->version > 3) { 280071f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version); 280171f87433Sdalcinl } 280271f87433Sdalcinl if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) { 280371f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0); 280471f87433Sdalcinl } 280571f87433Sdalcinl if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) { 280671f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max); 280771f87433Sdalcinl } 280871f87433Sdalcinl if (kctx->gamma < 0.0 || kctx->gamma > 1.0) { 280971f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma); 281071f87433Sdalcinl } 281171f87433Sdalcinl if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) { 281271f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha); 281371f87433Sdalcinl } 281471f87433Sdalcinl if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) { 281571f87433Sdalcinl SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold); 281671f87433Sdalcinl } 281771f87433Sdalcinl PetscFunctionReturn(0); 281871f87433Sdalcinl } 281971f87433Sdalcinl 282071f87433Sdalcinl #undef __FUNCT__ 2821fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW" 282271f87433Sdalcinl /*@ 2823fa9f3622SBarry Smith SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker 282471f87433Sdalcinl convergence criteria for the linear solvers within an inexact 282571f87433Sdalcinl Newton method. 282671f87433Sdalcinl 282771f87433Sdalcinl Not Collective 282871f87433Sdalcinl 282971f87433Sdalcinl Input Parameters: 283071f87433Sdalcinl snes - SNES context 283171f87433Sdalcinl 283271f87433Sdalcinl Output Parameters: 283371f87433Sdalcinl + version - version 1, 2 (default is 2) or 3 283471f87433Sdalcinl . rtol_0 - initial relative tolerance (0 <= rtol_0 < 1) 283571f87433Sdalcinl . rtol_max - maximum relative tolerance (0 <= rtol_max < 1) 283671f87433Sdalcinl . gamma - multiplicative factor for version 2 rtol computation 283771f87433Sdalcinl (0 <= gamma2 <= 1) 283871f87433Sdalcinl . alpha - power for version 2 rtol computation (1 < alpha <= 2) 283971f87433Sdalcinl . alpha2 - power for safeguard 284071f87433Sdalcinl - threshold - threshold for imposing safeguard (0 < threshold < 1) 284171f87433Sdalcinl 284271f87433Sdalcinl Level: advanced 284371f87433Sdalcinl 284471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters 284571f87433Sdalcinl 2846fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW() 284771f87433Sdalcinl @*/ 2848fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max, 284971f87433Sdalcinl PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold) 285071f87433Sdalcinl { 2851fa9f3622SBarry Smith SNESKSPEW *kctx; 285271f87433Sdalcinl PetscFunctionBegin; 2853*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 2854fa9f3622SBarry Smith kctx = (SNESKSPEW*)snes->kspconvctx; 285571f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing"); 285671f87433Sdalcinl if(version) *version = kctx->version; 285771f87433Sdalcinl if(rtol_0) *rtol_0 = kctx->rtol_0; 285871f87433Sdalcinl if(rtol_max) *rtol_max = kctx->rtol_max; 285971f87433Sdalcinl if(gamma) *gamma = kctx->gamma; 286071f87433Sdalcinl if(alpha) *alpha = kctx->alpha; 286171f87433Sdalcinl if(alpha2) *alpha2 = kctx->alpha2; 286271f87433Sdalcinl if(threshold) *threshold = kctx->threshold; 286371f87433Sdalcinl PetscFunctionReturn(0); 286471f87433Sdalcinl } 286571f87433Sdalcinl 286671f87433Sdalcinl #undef __FUNCT__ 2867fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve" 2868fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x) 286971f87433Sdalcinl { 287071f87433Sdalcinl PetscErrorCode ierr; 2871fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 287271f87433Sdalcinl PetscReal rtol=PETSC_DEFAULT,stol; 287371f87433Sdalcinl 287471f87433Sdalcinl PetscFunctionBegin; 287571f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 287671f87433Sdalcinl if (!snes->iter) { /* first time in, so use the original user rtol */ 287771f87433Sdalcinl rtol = kctx->rtol_0; 287871f87433Sdalcinl } else { 287971f87433Sdalcinl if (kctx->version == 1) { 288071f87433Sdalcinl rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 288171f87433Sdalcinl if (rtol < 0.0) rtol = -rtol; 288271f87433Sdalcinl stol = pow(kctx->rtol_last,kctx->alpha2); 288371f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 288471f87433Sdalcinl } else if (kctx->version == 2) { 288571f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 288671f87433Sdalcinl stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 288771f87433Sdalcinl if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 288871f87433Sdalcinl } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */ 288971f87433Sdalcinl rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 289071f87433Sdalcinl /* safeguard: avoid sharp decrease of rtol */ 289171f87433Sdalcinl stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha); 289271f87433Sdalcinl stol = PetscMax(rtol,stol); 289371f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 289471f87433Sdalcinl /* safeguard: avoid oversolving */ 289571f87433Sdalcinl stol = kctx->gamma*(snes->ttol)/snes->norm; 289671f87433Sdalcinl stol = PetscMax(rtol,stol); 289771f87433Sdalcinl rtol = PetscMin(kctx->rtol_0,stol); 289871f87433Sdalcinl } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version); 289971f87433Sdalcinl } 290071f87433Sdalcinl /* safeguard: avoid rtol greater than one */ 290171f87433Sdalcinl rtol = PetscMin(rtol,kctx->rtol_max); 290271f87433Sdalcinl ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 290371f87433Sdalcinl ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr); 290471f87433Sdalcinl PetscFunctionReturn(0); 290571f87433Sdalcinl } 290671f87433Sdalcinl 290771f87433Sdalcinl #undef __FUNCT__ 2908fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve" 2909fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x) 291071f87433Sdalcinl { 291171f87433Sdalcinl PetscErrorCode ierr; 2912fa9f3622SBarry Smith SNESKSPEW *kctx = (SNESKSPEW*)snes->kspconvctx; 291371f87433Sdalcinl PCSide pcside; 291471f87433Sdalcinl Vec lres; 291571f87433Sdalcinl 291671f87433Sdalcinl PetscFunctionBegin; 291771f87433Sdalcinl if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists"); 291871f87433Sdalcinl ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr); 291971f87433Sdalcinl ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr); 292071f87433Sdalcinl if (kctx->version == 1) { 292171f87433Sdalcinl ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr); 292271f87433Sdalcinl if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */ 292371f87433Sdalcinl /* KSP residual is true linear residual */ 292471f87433Sdalcinl ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr); 292571f87433Sdalcinl } else { 292671f87433Sdalcinl /* KSP residual is preconditioned residual */ 292771f87433Sdalcinl /* compute true linear residual norm */ 292871f87433Sdalcinl ierr = VecDuplicate(b,&lres);CHKERRQ(ierr); 292971f87433Sdalcinl ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr); 293071f87433Sdalcinl ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr); 293171f87433Sdalcinl ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr); 293271f87433Sdalcinl ierr = VecDestroy(lres);CHKERRQ(ierr); 293371f87433Sdalcinl } 293471f87433Sdalcinl } 293571f87433Sdalcinl PetscFunctionReturn(0); 293671f87433Sdalcinl } 293771f87433Sdalcinl 293871f87433Sdalcinl #undef __FUNCT__ 293971f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve" 294071f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x) 294171f87433Sdalcinl { 294271f87433Sdalcinl PetscErrorCode ierr; 294371f87433Sdalcinl 294471f87433Sdalcinl PetscFunctionBegin; 2945fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr); } 294671f87433Sdalcinl ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); 2947fa9f3622SBarry Smith if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); } 294871f87433Sdalcinl PetscFunctionReturn(0); 294971f87433Sdalcinl } 29506c699258SBarry Smith 29516c699258SBarry Smith #undef __FUNCT__ 29526c699258SBarry Smith #define __FUNCT__ "SNESSetDM" 29536c699258SBarry Smith /*@ 29546c699258SBarry Smith SNESSetDM - Sets the DM that may be used by some preconditioners 29556c699258SBarry Smith 29566c699258SBarry Smith Collective on SNES 29576c699258SBarry Smith 29586c699258SBarry Smith Input Parameters: 29596c699258SBarry Smith + snes - the preconditioner context 29606c699258SBarry Smith - dm - the dm 29616c699258SBarry Smith 29626c699258SBarry Smith Level: intermediate 29636c699258SBarry Smith 29646c699258SBarry Smith 29656c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM() 29666c699258SBarry Smith @*/ 29676c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetDM(SNES snes,DM dm) 29686c699258SBarry Smith { 29696c699258SBarry Smith PetscErrorCode ierr; 2970345fed2cSBarry Smith KSP ksp; 29716c699258SBarry Smith 29726c699258SBarry Smith PetscFunctionBegin; 2973*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 29746c699258SBarry Smith if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);} 29756c699258SBarry Smith snes->dm = dm; 29766c699258SBarry Smith ierr = PetscObjectReference((PetscObject)snes->dm);CHKERRQ(ierr); 2977345fed2cSBarry Smith ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr); 2978345fed2cSBarry Smith ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr); 29796c699258SBarry Smith PetscFunctionReturn(0); 29806c699258SBarry Smith } 29816c699258SBarry Smith 29826c699258SBarry Smith #undef __FUNCT__ 29836c699258SBarry Smith #define __FUNCT__ "SNESGetDM" 29846c699258SBarry Smith /*@ 29856c699258SBarry Smith SNESGetDM - Gets the DM that may be used by some preconditioners 29866c699258SBarry Smith 29876c699258SBarry Smith Collective on SNES 29886c699258SBarry Smith 29896c699258SBarry Smith Input Parameter: 29906c699258SBarry Smith . snes - the preconditioner context 29916c699258SBarry Smith 29926c699258SBarry Smith Output Parameter: 29936c699258SBarry Smith . dm - the dm 29946c699258SBarry Smith 29956c699258SBarry Smith Level: intermediate 29966c699258SBarry Smith 29976c699258SBarry Smith 29986c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM() 29996c699258SBarry Smith @*/ 30006c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetDM(SNES snes,DM *dm) 30016c699258SBarry Smith { 30026c699258SBarry Smith PetscFunctionBegin; 3003*0700a824SBarry Smith PetscValidHeaderSpecific(snes,SNES_CLASSID,1); 30046c699258SBarry Smith *dm = snes->dm; 30056c699258SBarry Smith PetscFunctionReturn(0); 30066c699258SBarry Smith } 3007