1*1d1367b7SBarry Smith /*$Id: snes.c,v 1.216 2000/08/01 20:57:16 bsmith Exp bsmith $*/ 29b94acceSBarry Smith 3e090d566SSatish Balay #include "src/snes/snesimpl.h" /*I "petscsnes.h" I*/ 49b94acceSBarry Smith 54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE; 6488ecbafSBarry Smith FList SNESList = 0; 782bf6240SBarry Smith 85615d1e5SSatish Balay #undef __FUNC__ 9b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESView" 107e2c5f70SBarry Smith /*@C 119b94acceSBarry Smith SNESView - Prints the SNES data structure. 129b94acceSBarry Smith 134c49b128SBarry Smith Collective on SNES 14fee21e36SBarry Smith 15c7afd0dbSLois Curfman McInnes Input Parameters: 16c7afd0dbSLois Curfman McInnes + SNES - the SNES context 17c7afd0dbSLois Curfman McInnes - viewer - visualization context 18c7afd0dbSLois Curfman McInnes 199b94acceSBarry Smith Options Database Key: 20c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 219b94acceSBarry Smith 229b94acceSBarry Smith Notes: 239b94acceSBarry Smith The available visualization contexts include 24c7afd0dbSLois Curfman McInnes + VIEWER_STDOUT_SELF - standard output (default) 25c7afd0dbSLois Curfman McInnes - VIEWER_STDOUT_WORLD - synchronized standard 26c8a8ba5cSLois Curfman McInnes output where only the first processor opens 27c8a8ba5cSLois Curfman McInnes the file. All other processors send their 28c8a8ba5cSLois Curfman McInnes data to the first processor to print. 299b94acceSBarry Smith 303e081fefSLois Curfman McInnes The user can open an alternative visualization context with 3177ed5343SBarry Smith ViewerASCIIOpen() - output to a specified file. 329b94acceSBarry Smith 3336851e7fSLois Curfman McInnes Level: beginner 3436851e7fSLois Curfman McInnes 359b94acceSBarry Smith .keywords: SNES, view 369b94acceSBarry Smith 3777ed5343SBarry Smith .seealso: ViewerASCIIOpen() 389b94acceSBarry Smith @*/ 399b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer) 409b94acceSBarry Smith { 419b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 429b94acceSBarry Smith int ierr; 439b94acceSBarry Smith SLES sles; 44454a90a3SBarry Smith char *type; 456831982aSBarry Smith PetscTruth isascii,isstring; 469b94acceSBarry Smith 473a40ed3dSBarry Smith PetscFunctionBegin; 4874679c65SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 493eda8832SBarry Smith if (!viewer) viewer = VIEWER_STDOUT_(snes->comm); 500f5bd95cSBarry Smith PetscValidHeaderSpecific(viewer,VIEWER_COOKIE); 516831982aSBarry Smith PetscCheckSameComm(snes,viewer); 5274679c65SBarry Smith 536831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr); 546831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,STRING_VIEWER,&isstring);CHKERRQ(ierr); 550f5bd95cSBarry Smith if (isascii) { 56d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr); 57454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 58454a90a3SBarry Smith if (type) { 59454a90a3SBarry Smith ierr = ViewerASCIIPrintf(viewer," type: %s\n",type);CHKERRQ(ierr); 60184914b5SBarry Smith } else { 61454a90a3SBarry Smith ierr = ViewerASCIIPrintf(viewer," type: not set yet\n");CHKERRQ(ierr); 62184914b5SBarry Smith } 630ef38995SBarry Smith if (snes->view) { 640ef38995SBarry Smith ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 650ef38995SBarry Smith ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr); 660ef38995SBarry Smith ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 670ef38995SBarry Smith } 68d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 69d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 70d132466eSBarry Smith snes->rtol,snes->atol,snes->trunctol,snes->xtol);CHKERRQ(ierr); 71d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr); 72d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr); 730ef38995SBarry Smith if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 74d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr); 750ef38995SBarry Smith } 769b94acceSBarry Smith if (snes->ksp_ewconv) { 779b94acceSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 789b94acceSBarry Smith if (kctx) { 79d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr); 80d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 81d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 829b94acceSBarry Smith } 839b94acceSBarry Smith } 840f5bd95cSBarry Smith } else if (isstring) { 85454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 86454a90a3SBarry Smith ierr = ViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 8719bcc07fSBarry Smith } 8877ed5343SBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 890ef38995SBarry Smith ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 909b94acceSBarry Smith ierr = SLESView(sles,viewer);CHKERRQ(ierr); 910ef38995SBarry Smith ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 923a40ed3dSBarry Smith PetscFunctionReturn(0); 939b94acceSBarry Smith } 949b94acceSBarry Smith 95639f9d9dSBarry Smith /* 96639f9d9dSBarry Smith We retain a list of functions that also take SNES command 97639f9d9dSBarry Smith line options. These are called at the end SNESSetFromOptions() 98639f9d9dSBarry Smith */ 99639f9d9dSBarry Smith #define MAXSETFROMOPTIONS 5 100639f9d9dSBarry Smith static int numberofsetfromoptions; 101639f9d9dSBarry Smith static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 102639f9d9dSBarry Smith 1035615d1e5SSatish Balay #undef __FUNC__ 104b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESAddOptionsChecker" 105639f9d9dSBarry Smith /*@ 106639f9d9dSBarry Smith SNESAddOptionsChecker - Adds an additional function to check for SNES options. 107639f9d9dSBarry Smith 108c7afd0dbSLois Curfman McInnes Not Collective 109c7afd0dbSLois Curfman McInnes 110639f9d9dSBarry Smith Input Parameter: 111639f9d9dSBarry Smith . snescheck - function that checks for options 112639f9d9dSBarry Smith 11336851e7fSLois Curfman McInnes Level: developer 11436851e7fSLois Curfman McInnes 115639f9d9dSBarry Smith .seealso: SNESSetFromOptions() 116639f9d9dSBarry Smith @*/ 117639f9d9dSBarry Smith int SNESAddOptionsChecker(int (*snescheck)(SNES)) 118639f9d9dSBarry Smith { 1193a40ed3dSBarry Smith PetscFunctionBegin; 120639f9d9dSBarry Smith if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 121a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many options checkers, only 5 allowed"); 122639f9d9dSBarry Smith } 123639f9d9dSBarry Smith 124639f9d9dSBarry Smith othersetfromoptions[numberofsetfromoptions++] = snescheck; 1253a40ed3dSBarry Smith PetscFunctionReturn(0); 126639f9d9dSBarry Smith } 127639f9d9dSBarry Smith 1285615d1e5SSatish Balay #undef __FUNC__ 129b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetTypeFromOptions" 13015091d37SBarry Smith /*@ 13115091d37SBarry Smith SNESSetTypeFromOptions - Sets the SNES solver type from the options database, 13215091d37SBarry Smith or sets a default if none is give. 13315091d37SBarry Smith 13415091d37SBarry Smith Collective on SNES 13515091d37SBarry Smith 13615091d37SBarry Smith Input Parameter: 13715091d37SBarry Smith . snes - the SNES context 13815091d37SBarry Smith 13915091d37SBarry Smith Options Database Keys: 1406831982aSBarry Smith . -snes_type <type> - ls, tr, umls, umtr, test 14115091d37SBarry Smith 14215091d37SBarry Smith Level: beginner 14315091d37SBarry Smith 14415091d37SBarry Smith .keywords: SNES, nonlinear, set, options, database 14515091d37SBarry Smith 14615091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetFromOptions() 14715091d37SBarry Smith @*/ 14815091d37SBarry Smith int SNESSetTypeFromOptions(SNES snes) 14915091d37SBarry Smith { 150454a90a3SBarry Smith char type[256]; 151f1af5d2fSBarry Smith int ierr; 152f1af5d2fSBarry Smith PetscTruth flg; 15315091d37SBarry Smith 15415091d37SBarry Smith PetscFunctionBegin; 15515091d37SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15615091d37SBarry Smith if (snes->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to SNESSetUp()"); 157454a90a3SBarry Smith ierr = OptionsGetString(snes->prefix,"-snes_type",type,256,&flg);CHKERRQ(ierr); 15815091d37SBarry Smith if (flg) { 159454a90a3SBarry Smith ierr = SNESSetType(snes,(SNESType) type);CHKERRQ(ierr); 16015091d37SBarry Smith } 16115091d37SBarry Smith /* 16215091d37SBarry Smith If SNES type has not yet been set, set it now 16315091d37SBarry Smith */ 16415091d37SBarry Smith if (!snes->type_name) { 16515091d37SBarry Smith if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1666831982aSBarry Smith ierr = SNESSetType(snes,SNESEQLS);CHKERRQ(ierr); 16715091d37SBarry Smith } else { 1686831982aSBarry Smith ierr = SNESSetType(snes,SNESUMTR);CHKERRQ(ierr); 16915091d37SBarry Smith } 17015091d37SBarry Smith } 17115091d37SBarry Smith PetscFunctionReturn(0); 17215091d37SBarry Smith } 17315091d37SBarry Smith 17415091d37SBarry Smith #undef __FUNC__ 175b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetFromOptions" 1769b94acceSBarry Smith /*@ 177682d7d0cSBarry Smith SNESSetFromOptions - Sets various SNES and SLES parameters from user options. 1789b94acceSBarry Smith 179c7afd0dbSLois Curfman McInnes Collective on SNES 180c7afd0dbSLois Curfman McInnes 1819b94acceSBarry Smith Input Parameter: 1829b94acceSBarry Smith . snes - the SNES context 1839b94acceSBarry Smith 18436851e7fSLois Curfman McInnes Options Database Keys: 1856831982aSBarry Smith + -snes_type <type> - ls, tr, umls, umtr, test 18682738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 18782738288SBarry Smith of the change in the solution between steps 188b39c3a46SLois Curfman McInnes . -snes_atol <atol> - absolute tolerance of residual norm 189b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 190b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 191b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 192b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 19382738288SBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear or minimization 19482738288SBarry Smith solver; hence iterations will continue until max_it 1951fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 19682738288SBarry Smith of convergence test 19782738288SBarry Smith . -snes_monitor - prints residual norm at each iteration 198d132466eSBarry Smith . -snes_vecmonitor - plots solution at each iteration 199d132466eSBarry Smith . -snes_vecmonitor_update - plots update to solution at each iteration 20082738288SBarry Smith . -snes_xmonitor - plots residual norm at each iteration 201e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 20236851e7fSLois Curfman McInnes - -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 20382738288SBarry Smith 20482738288SBarry Smith Options Database for Eisenstat-Walker method: 20582738288SBarry Smith + -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence 20682738288SBarry Smith . -snes_ksp_eq_version ver - version of Eisenstat-Walker method 20736851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 20836851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 20936851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 21036851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 21136851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 21236851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 21382738288SBarry Smith 21411ca99fdSLois Curfman McInnes Notes: 21511ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 21611ca99fdSLois Curfman McInnes the users manual. 21783e2fdc7SBarry Smith 21836851e7fSLois Curfman McInnes Level: beginner 21936851e7fSLois Curfman McInnes 2209b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 2219b94acceSBarry Smith 22215091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetTypeFromOptions() 2239b94acceSBarry Smith @*/ 2249b94acceSBarry Smith int SNESSetFromOptions(SNES snes) 2259b94acceSBarry Smith { 226329f5518SBarry Smith PetscReal tmp; 2279b94acceSBarry Smith SLES sles; 228f1af5d2fSBarry Smith PetscTruth flg; 229f1af5d2fSBarry Smith int ierr,i,loc[4],nmax = 4; 2303c7409f5SSatish Balay int version = PETSC_DEFAULT; 231329f5518SBarry Smith PetscReal rtol_0 = PETSC_DEFAULT; 232329f5518SBarry Smith PetscReal rtol_max = PETSC_DEFAULT; 233329f5518SBarry Smith PetscReal gamma2 = PETSC_DEFAULT; 234329f5518SBarry Smith PetscReal alpha = PETSC_DEFAULT; 235329f5518SBarry Smith PetscReal alpha2 = PETSC_DEFAULT; 236329f5518SBarry Smith PetscReal threshold = PETSC_DEFAULT; 2379b94acceSBarry Smith 2383a40ed3dSBarry Smith PetscFunctionBegin; 23977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 24015091d37SBarry Smith ierr = SNESSetTypeFromOptions(snes);CHKERRQ(ierr); 241ca161407SBarry Smith 24215091d37SBarry Smith loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300; 2433c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp,&flg);CHKERRQ(ierr); 244d64ed03dSBarry Smith if (flg) { 245d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 246d64ed03dSBarry Smith } 2473c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp,&flg);CHKERRQ(ierr); 248d64ed03dSBarry Smith if (flg) { 249d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 250d64ed03dSBarry Smith } 2513c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp,&flg);CHKERRQ(ierr); 252d64ed03dSBarry Smith if (flg) { 253d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 254d64ed03dSBarry Smith } 255f1af5d2fSBarry Smith ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 256f1af5d2fSBarry Smith ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 257d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp,&flg);CHKERRQ(ierr); 258d64ed03dSBarry Smith if (flg) { ierr = SNESSetTrustRegionTolerance(snes,tmp);CHKERRQ(ierr); } 259d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp,&flg);CHKERRQ(ierr); 260d64ed03dSBarry Smith if (flg) { ierr = SNESSetMinimizationFunctionTolerance(snes,tmp);CHKERRQ(ierr);} 2613c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv",&flg);CHKERRQ(ierr); 2623c7409f5SSatish Balay if (flg) { snes->ksp_ewconv = 1; } 263f1af5d2fSBarry Smith ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,PETSC_NULL);CHKERRQ(ierr); 264f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,PETSC_NULL);CHKERRQ(ierr); 265f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,PETSC_NULL);CHKERRQ(ierr); 266f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,PETSC_NULL);CHKERRQ(ierr); 267f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,PETSC_NULL);CHKERRQ(ierr); 268f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,PETSC_NULL);CHKERRQ(ierr); 269f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,PETSC_NULL);CHKERRQ(ierr); 27093c39befSBarry Smith 27193c39befSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_no_convergence_test",&flg);CHKERRQ(ierr); 27293c39befSBarry Smith if (flg) {snes->converged = 0;} 27393c39befSBarry Smith 2749b94acceSBarry Smith ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 2759b94acceSBarry Smith alpha2,threshold);CHKERRQ(ierr); 2765bbad29bSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_cancelmonitors",&flg);CHKERRQ(ierr); 2775cd90555SBarry Smith if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);} 2783c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg);CHKERRQ(ierr); 279b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);} 2803c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg);CHKERRQ(ierr); 281b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);} 2823f1db9ecSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor",&flg);CHKERRQ(ierr); 283b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);} 284d132466eSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor_update",&flg);CHKERRQ(ierr); 2857c922b88SBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewUpdateMonitor,0,0);CHKERRQ(ierr);} 28681f57ec7SBarry Smith ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr); 2873c7409f5SSatish Balay if (flg) { 288c38d4ed2SBarry Smith ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 2899b94acceSBarry Smith } 290e24b481bSBarry Smith 2913c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_fd",&flg);CHKERRQ(ierr); 2923c7409f5SSatish Balay if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 2939b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 2949b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 295981c4779SBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 296981c4779SBarry Smith } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 29731615d04SLois Curfman McInnes ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 29831615d04SLois Curfman McInnes SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr); 299d64ed03dSBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 3009b94acceSBarry Smith } 301639f9d9dSBarry Smith 302639f9d9dSBarry Smith for (i=0; i<numberofsetfromoptions; i++) { 303639f9d9dSBarry Smith ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 304639f9d9dSBarry Smith } 305639f9d9dSBarry Smith 3069b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 3079b94acceSBarry Smith ierr = SLESSetFromOptions(sles);CHKERRQ(ierr); 30893993e2dSLois Curfman McInnes 309e24b481bSBarry Smith /* set the special KSP monitor for matrix-free application */ 310e24b481bSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg);CHKERRQ(ierr); 311e24b481bSBarry Smith if (flg) { 312e24b481bSBarry Smith KSP ksp; 313e24b481bSBarry Smith ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 314b8a78c4aSBarry Smith ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr); 315e24b481bSBarry Smith } 316e24b481bSBarry Smith 31782bf6240SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr); 31882bf6240SBarry Smith if (flg) { ierr = SNESPrintHelp(snes);CHKERRQ(ierr);} 31982bf6240SBarry Smith 3203a40ed3dSBarry Smith if (snes->setfromoptions) { 3213a40ed3dSBarry Smith ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr); 3223a40ed3dSBarry Smith } 3233a40ed3dSBarry Smith PetscFunctionReturn(0); 3249b94acceSBarry Smith } 3259b94acceSBarry Smith 326a847f771SSatish Balay 3275615d1e5SSatish Balay #undef __FUNC__ 328b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetApplicationContext" 3299b94acceSBarry Smith /*@ 3309b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3319b94acceSBarry Smith the nonlinear solvers. 3329b94acceSBarry Smith 333fee21e36SBarry Smith Collective on SNES 334fee21e36SBarry Smith 335c7afd0dbSLois Curfman McInnes Input Parameters: 336c7afd0dbSLois Curfman McInnes + snes - the SNES context 337c7afd0dbSLois Curfman McInnes - usrP - optional user context 338c7afd0dbSLois Curfman McInnes 33936851e7fSLois Curfman McInnes Level: intermediate 34036851e7fSLois Curfman McInnes 3419b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3429b94acceSBarry Smith 3439b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3449b94acceSBarry Smith @*/ 3459b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 3469b94acceSBarry Smith { 3473a40ed3dSBarry Smith PetscFunctionBegin; 34877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3499b94acceSBarry Smith snes->user = usrP; 3503a40ed3dSBarry Smith PetscFunctionReturn(0); 3519b94acceSBarry Smith } 35274679c65SBarry Smith 3535615d1e5SSatish Balay #undef __FUNC__ 354b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetApplicationContext" 3559b94acceSBarry Smith /*@C 3569b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3579b94acceSBarry Smith nonlinear solvers. 3589b94acceSBarry Smith 359c7afd0dbSLois Curfman McInnes Not Collective 360c7afd0dbSLois Curfman McInnes 3619b94acceSBarry Smith Input Parameter: 3629b94acceSBarry Smith . snes - SNES context 3639b94acceSBarry Smith 3649b94acceSBarry Smith Output Parameter: 3659b94acceSBarry Smith . usrP - user context 3669b94acceSBarry Smith 36736851e7fSLois Curfman McInnes Level: intermediate 36836851e7fSLois Curfman McInnes 3699b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3709b94acceSBarry Smith 3719b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3729b94acceSBarry Smith @*/ 3739b94acceSBarry Smith int SNESGetApplicationContext(SNES snes,void **usrP) 3749b94acceSBarry Smith { 3753a40ed3dSBarry Smith PetscFunctionBegin; 37677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3779b94acceSBarry Smith *usrP = snes->user; 3783a40ed3dSBarry Smith PetscFunctionReturn(0); 3799b94acceSBarry Smith } 38074679c65SBarry Smith 3815615d1e5SSatish Balay #undef __FUNC__ 382b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetIterationNumber" 3839b94acceSBarry Smith /*@ 384c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 385c8228a4eSBarry Smith at this time. 3869b94acceSBarry Smith 387c7afd0dbSLois Curfman McInnes Not Collective 388c7afd0dbSLois Curfman McInnes 3899b94acceSBarry Smith Input Parameter: 3909b94acceSBarry Smith . snes - SNES context 3919b94acceSBarry Smith 3929b94acceSBarry Smith Output Parameter: 3939b94acceSBarry Smith . iter - iteration number 3949b94acceSBarry Smith 395c8228a4eSBarry Smith Notes: 396c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 397c8228a4eSBarry Smith 398c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 39908405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 40008405cd6SLois Curfman McInnes .vb 40108405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 40208405cd6SLois Curfman McInnes if (!(it % 2)) { 40308405cd6SLois Curfman McInnes [compute Jacobian here] 40408405cd6SLois Curfman McInnes } 40508405cd6SLois Curfman McInnes .ve 406c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 40708405cd6SLois Curfman McInnes recomputed every second SNES iteration. 408c8228a4eSBarry Smith 40936851e7fSLois Curfman McInnes Level: intermediate 41036851e7fSLois Curfman McInnes 4119b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 4129b94acceSBarry Smith @*/ 4139b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 4149b94acceSBarry Smith { 4153a40ed3dSBarry Smith PetscFunctionBegin; 41677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 41774679c65SBarry Smith PetscValidIntPointer(iter); 4189b94acceSBarry Smith *iter = snes->iter; 4193a40ed3dSBarry Smith PetscFunctionReturn(0); 4209b94acceSBarry Smith } 42174679c65SBarry Smith 4225615d1e5SSatish Balay #undef __FUNC__ 423b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetFunctionNorm" 4249b94acceSBarry Smith /*@ 4259b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4269b94acceSBarry Smith with SNESSSetFunction(). 4279b94acceSBarry Smith 428c7afd0dbSLois Curfman McInnes Collective on SNES 429c7afd0dbSLois Curfman McInnes 4309b94acceSBarry Smith Input Parameter: 4319b94acceSBarry Smith . snes - SNES context 4329b94acceSBarry Smith 4339b94acceSBarry Smith Output Parameter: 4349b94acceSBarry Smith . fnorm - 2-norm of function 4359b94acceSBarry Smith 4369b94acceSBarry Smith Note: 4379b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 438a86d99e1SLois Curfman McInnes A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 439a86d99e1SLois Curfman McInnes SNESGetGradientNorm(). 4409b94acceSBarry Smith 44136851e7fSLois Curfman McInnes Level: intermediate 44236851e7fSLois Curfman McInnes 4439b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 444a86d99e1SLois Curfman McInnes 44508405cd6SLois Curfman McInnes .seealso: SNESGetFunction() 4469b94acceSBarry Smith @*/ 4479b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 4489b94acceSBarry Smith { 4493a40ed3dSBarry Smith PetscFunctionBegin; 45077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 45174679c65SBarry Smith PetscValidScalarPointer(fnorm); 45274679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 453d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only"); 45474679c65SBarry Smith } 4559b94acceSBarry Smith *fnorm = snes->norm; 4563a40ed3dSBarry Smith PetscFunctionReturn(0); 4579b94acceSBarry Smith } 45874679c65SBarry Smith 4595615d1e5SSatish Balay #undef __FUNC__ 460b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetGradientNorm" 4619b94acceSBarry Smith /*@ 4629b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 4639b94acceSBarry Smith with SNESSSetGradient(). 4649b94acceSBarry Smith 465c7afd0dbSLois Curfman McInnes Collective on SNES 466c7afd0dbSLois Curfman McInnes 4679b94acceSBarry Smith Input Parameter: 4689b94acceSBarry Smith . snes - SNES context 4699b94acceSBarry Smith 4709b94acceSBarry Smith Output Parameter: 4719b94acceSBarry Smith . fnorm - 2-norm of gradient 4729b94acceSBarry Smith 4739b94acceSBarry Smith Note: 4749b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 475a86d99e1SLois Curfman McInnes methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 476a86d99e1SLois Curfman McInnes is SNESGetFunctionNorm(). 4779b94acceSBarry Smith 47836851e7fSLois Curfman McInnes Level: intermediate 47936851e7fSLois Curfman McInnes 4809b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 481a86d99e1SLois Curfman McInnes 482a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient() 4839b94acceSBarry Smith @*/ 4849b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 4859b94acceSBarry Smith { 4863a40ed3dSBarry Smith PetscFunctionBegin; 48777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 48874679c65SBarry Smith PetscValidScalarPointer(gnorm); 48974679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 490d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 49174679c65SBarry Smith } 4929b94acceSBarry Smith *gnorm = snes->norm; 4933a40ed3dSBarry Smith PetscFunctionReturn(0); 4949b94acceSBarry Smith } 49574679c65SBarry Smith 4965615d1e5SSatish Balay #undef __FUNC__ 497b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetNumberUnsuccessfulSteps" 4989b94acceSBarry Smith /*@ 4999b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 5009b94acceSBarry Smith attempted by the nonlinear solver. 5019b94acceSBarry Smith 502c7afd0dbSLois Curfman McInnes Not Collective 503c7afd0dbSLois Curfman McInnes 5049b94acceSBarry Smith Input Parameter: 5059b94acceSBarry Smith . snes - SNES context 5069b94acceSBarry Smith 5079b94acceSBarry Smith Output Parameter: 5089b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 5099b94acceSBarry Smith 510c96a6f78SLois Curfman McInnes Notes: 511c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 512c96a6f78SLois Curfman McInnes 51336851e7fSLois Curfman McInnes Level: intermediate 51436851e7fSLois Curfman McInnes 5159b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 5169b94acceSBarry Smith @*/ 5179b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 5189b94acceSBarry Smith { 5193a40ed3dSBarry Smith PetscFunctionBegin; 52077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 52174679c65SBarry Smith PetscValidIntPointer(nfails); 5229b94acceSBarry Smith *nfails = snes->nfailures; 5233a40ed3dSBarry Smith PetscFunctionReturn(0); 5249b94acceSBarry Smith } 525a847f771SSatish Balay 5265615d1e5SSatish Balay #undef __FUNC__ 527b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetNumberLinearIterations" 528c96a6f78SLois Curfman McInnes /*@ 529c96a6f78SLois Curfman McInnes SNESGetNumberLinearIterations - Gets the total number of linear iterations 530c96a6f78SLois Curfman McInnes used by the nonlinear solver. 531c96a6f78SLois Curfman McInnes 532c7afd0dbSLois Curfman McInnes Not Collective 533c7afd0dbSLois Curfman McInnes 534c96a6f78SLois Curfman McInnes Input Parameter: 535c96a6f78SLois Curfman McInnes . snes - SNES context 536c96a6f78SLois Curfman McInnes 537c96a6f78SLois Curfman McInnes Output Parameter: 538c96a6f78SLois Curfman McInnes . lits - number of linear iterations 539c96a6f78SLois Curfman McInnes 540c96a6f78SLois Curfman McInnes Notes: 541c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 542c96a6f78SLois Curfman McInnes 54336851e7fSLois Curfman McInnes Level: intermediate 54436851e7fSLois Curfman McInnes 545c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 546c96a6f78SLois Curfman McInnes @*/ 54786bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits) 548c96a6f78SLois Curfman McInnes { 5493a40ed3dSBarry Smith PetscFunctionBegin; 550c96a6f78SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 551c96a6f78SLois Curfman McInnes PetscValidIntPointer(lits); 552c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 5533a40ed3dSBarry Smith PetscFunctionReturn(0); 554c96a6f78SLois Curfman McInnes } 555c96a6f78SLois Curfman McInnes 556c96a6f78SLois Curfman McInnes #undef __FUNC__ 557b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetSLES" 5589b94acceSBarry Smith /*@C 5599b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 5609b94acceSBarry Smith 561c7afd0dbSLois Curfman McInnes Not Collective, but if SNES object is parallel, then SLES object is parallel 562c7afd0dbSLois Curfman McInnes 5639b94acceSBarry Smith Input Parameter: 5649b94acceSBarry Smith . snes - the SNES context 5659b94acceSBarry Smith 5669b94acceSBarry Smith Output Parameter: 5679b94acceSBarry Smith . sles - the SLES context 5689b94acceSBarry Smith 5699b94acceSBarry Smith Notes: 5709b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 5719b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 5729b94acceSBarry Smith KSP and PC contexts as well. 5739b94acceSBarry Smith 57436851e7fSLois Curfman McInnes Level: beginner 57536851e7fSLois Curfman McInnes 5769b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 5779b94acceSBarry Smith 5789b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 5799b94acceSBarry Smith @*/ 5809b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 5819b94acceSBarry Smith { 5823a40ed3dSBarry Smith PetscFunctionBegin; 58377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5849b94acceSBarry Smith *sles = snes->sles; 5853a40ed3dSBarry Smith PetscFunctionReturn(0); 5869b94acceSBarry Smith } 58782bf6240SBarry Smith 588e24b481bSBarry Smith #undef __FUNC__ 589b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESPublish_Petsc" 590454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj) 591e24b481bSBarry Smith { 592aa482453SBarry Smith #if defined(PETSC_HAVE_AMS) 593454a90a3SBarry Smith SNES v = (SNES) obj; 594e24b481bSBarry Smith int ierr; 59543d6d2cbSBarry Smith #endif 596e24b481bSBarry Smith 597e24b481bSBarry Smith PetscFunctionBegin; 598e24b481bSBarry Smith 59943d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS) 600e24b481bSBarry Smith /* if it is already published then return */ 601e24b481bSBarry Smith if (v->amem >=0) PetscFunctionReturn(0); 602e24b481bSBarry Smith 603454a90a3SBarry Smith ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr); 604e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ, 605e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 606e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ, 607e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 608454a90a3SBarry Smith ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr); 609433994e6SBarry Smith #endif 610e24b481bSBarry Smith PetscFunctionReturn(0); 611e24b481bSBarry Smith } 612e24b481bSBarry Smith 6139b94acceSBarry Smith /* -----------------------------------------------------------*/ 6145615d1e5SSatish Balay #undef __FUNC__ 615b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESCreate" 6169b94acceSBarry Smith /*@C 6179b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 6189b94acceSBarry Smith 619c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 620c7afd0dbSLois Curfman McInnes 621c7afd0dbSLois Curfman McInnes Input Parameters: 622c7afd0dbSLois Curfman McInnes + comm - MPI communicator 623c7afd0dbSLois Curfman McInnes - type - type of method, either 624c7afd0dbSLois Curfman McInnes SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations) 625c7afd0dbSLois Curfman McInnes or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization) 6269b94acceSBarry Smith 6279b94acceSBarry Smith Output Parameter: 6289b94acceSBarry Smith . outsnes - the new SNES context 6299b94acceSBarry Smith 630c7afd0dbSLois Curfman McInnes Options Database Keys: 631c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 632c7afd0dbSLois Curfman McInnes and no preconditioning matrix 633c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 634c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 635c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 636c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 637c1f60f51SBarry Smith 63836851e7fSLois Curfman McInnes Level: beginner 63936851e7fSLois Curfman McInnes 6409b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 6419b94acceSBarry Smith 64263a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy() 6439b94acceSBarry Smith @*/ 6444b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 6459b94acceSBarry Smith { 6469b94acceSBarry Smith int ierr; 6479b94acceSBarry Smith SNES snes; 6489b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 64937fcc0dbSBarry Smith 6503a40ed3dSBarry Smith PetscFunctionBegin; 6519b94acceSBarry Smith *outsnes = 0; 652d64ed03dSBarry Smith if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){ 653d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type"); 654d64ed03dSBarry Smith } 6553f1db9ecSBarry Smith PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView); 6569b94acceSBarry Smith PLogObjectCreate(snes); 657e24b481bSBarry Smith snes->bops->publish = SNESPublish_Petsc; 6589b94acceSBarry Smith snes->max_its = 50; 6599750a799SBarry Smith snes->max_funcs = 10000; 6609b94acceSBarry Smith snes->norm = 0.0; 6615a2d0531SBarry Smith if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 662b18e04deSLois Curfman McInnes snes->rtol = 1.e-8; 663b18e04deSLois Curfman McInnes snes->ttol = 0.0; 6649b94acceSBarry Smith snes->atol = 1.e-10; 665d64ed03dSBarry Smith } else { 666b4874afaSBarry Smith snes->rtol = 1.e-8; 667b4874afaSBarry Smith snes->ttol = 0.0; 668b4874afaSBarry Smith snes->atol = 1.e-50; 669b4874afaSBarry Smith } 6709b94acceSBarry Smith snes->xtol = 1.e-8; 671b18e04deSLois Curfman McInnes snes->trunctol = 1.e-12; /* no longer used */ 6729b94acceSBarry Smith snes->nfuncs = 0; 6739b94acceSBarry Smith snes->nfailures = 0; 6747a00f4a9SLois Curfman McInnes snes->linear_its = 0; 675639f9d9dSBarry Smith snes->numbermonitors = 0; 6769b94acceSBarry Smith snes->data = 0; 6779b94acceSBarry Smith snes->view = 0; 6789b94acceSBarry Smith snes->computeumfunction = 0; 6799b94acceSBarry Smith snes->umfunP = 0; 6809b94acceSBarry Smith snes->fc = 0; 6819b94acceSBarry Smith snes->deltatol = 1.e-12; 6829b94acceSBarry Smith snes->fmin = -1.e30; 6839b94acceSBarry Smith snes->method_class = type; 6849b94acceSBarry Smith snes->set_method_called = 0; 68582bf6240SBarry Smith snes->setupcalled = 0; 6869b94acceSBarry Smith snes->ksp_ewconv = 0; 6876f24a144SLois Curfman McInnes snes->vwork = 0; 6886f24a144SLois Curfman McInnes snes->nwork = 0; 689758f92a0SBarry Smith snes->conv_hist_len = 0; 690758f92a0SBarry Smith snes->conv_hist_max = 0; 691758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 692758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 693758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 694184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 6959b94acceSBarry Smith 6969b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 6970452661fSBarry Smith kctx = PetscNew(SNES_KSP_EW_ConvCtx);CHKPTRQ(kctx); 698eed86810SBarry Smith PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx)); 6999b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 7009b94acceSBarry Smith kctx->version = 2; 7019b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 7029b94acceSBarry Smith this was too large for some test cases */ 7039b94acceSBarry Smith kctx->rtol_last = 0; 7049b94acceSBarry Smith kctx->rtol_max = .9; 7059b94acceSBarry Smith kctx->gamma = 1.0; 7069b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 7079b94acceSBarry Smith kctx->alpha = kctx->alpha2; 7089b94acceSBarry Smith kctx->threshold = .1; 7099b94acceSBarry Smith kctx->lresid_last = 0; 7109b94acceSBarry Smith kctx->norm_last = 0; 7119b94acceSBarry Smith 7129b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr); 7139b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 7145334005bSBarry Smith 7159b94acceSBarry Smith *outsnes = snes; 71600036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 7173a40ed3dSBarry Smith PetscFunctionReturn(0); 7189b94acceSBarry Smith } 7199b94acceSBarry Smith 7209b94acceSBarry Smith /* --------------------------------------------------------------- */ 7215615d1e5SSatish Balay #undef __FUNC__ 722b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetFunction" 7239b94acceSBarry Smith /*@C 7249b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 7259b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 7269b94acceSBarry Smith equations. 7279b94acceSBarry Smith 728fee21e36SBarry Smith Collective on SNES 729fee21e36SBarry Smith 730c7afd0dbSLois Curfman McInnes Input Parameters: 731c7afd0dbSLois Curfman McInnes + snes - the SNES context 732c7afd0dbSLois Curfman McInnes . func - function evaluation routine 733c7afd0dbSLois Curfman McInnes . r - vector to store function value 734c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 735c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 7369b94acceSBarry Smith 737c7afd0dbSLois Curfman McInnes Calling sequence of func: 7388d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 739c7afd0dbSLois Curfman McInnes 740313e4042SLois Curfman McInnes . f - function vector 741c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 7429b94acceSBarry Smith 7439b94acceSBarry Smith Notes: 7449b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 7459b94acceSBarry Smith $ f'(x) x = -f(x), 746c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 7479b94acceSBarry Smith 7489b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7499b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7509b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 7519b94acceSBarry Smith 75236851e7fSLois Curfman McInnes Level: beginner 75336851e7fSLois Curfman McInnes 7549b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 7559b94acceSBarry Smith 756a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 7579b94acceSBarry Smith @*/ 7585334005bSBarry Smith int SNESSetFunction(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 7599b94acceSBarry Smith { 7603a40ed3dSBarry Smith PetscFunctionBegin; 76177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 762184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 763184914b5SBarry Smith PetscCheckSameComm(snes,r); 764a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 765a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 766a8c6a408SBarry Smith } 767184914b5SBarry Smith 7689b94acceSBarry Smith snes->computefunction = func; 7699b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 7709b94acceSBarry Smith snes->funP = ctx; 7713a40ed3dSBarry Smith PetscFunctionReturn(0); 7729b94acceSBarry Smith } 7739b94acceSBarry Smith 7745615d1e5SSatish Balay #undef __FUNC__ 775b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeFunction" 7769b94acceSBarry Smith /*@ 77736851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 7789b94acceSBarry Smith SNESSetFunction(). 7799b94acceSBarry Smith 780c7afd0dbSLois Curfman McInnes Collective on SNES 781c7afd0dbSLois Curfman McInnes 7829b94acceSBarry Smith Input Parameters: 783c7afd0dbSLois Curfman McInnes + snes - the SNES context 784c7afd0dbSLois Curfman McInnes - x - input vector 7859b94acceSBarry Smith 7869b94acceSBarry Smith Output Parameter: 7873638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 7889b94acceSBarry Smith 7891bffabb2SLois Curfman McInnes Notes: 7909b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7919b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7929b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 7939b94acceSBarry Smith 79436851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 79536851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 79636851e7fSLois Curfman McInnes themselves. 79736851e7fSLois Curfman McInnes 79836851e7fSLois Curfman McInnes Level: developer 79936851e7fSLois Curfman McInnes 8009b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 8019b94acceSBarry Smith 802a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 8039b94acceSBarry Smith @*/ 8049b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x,Vec y) 8059b94acceSBarry Smith { 8069b94acceSBarry Smith int ierr; 8079b94acceSBarry Smith 8083a40ed3dSBarry Smith PetscFunctionBegin; 809184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 810184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 811184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 812184914b5SBarry Smith PetscCheckSameComm(snes,x); 813184914b5SBarry Smith PetscCheckSameComm(snes,y); 814d4bb536fSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 815a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 816d4bb536fSBarry Smith } 817184914b5SBarry Smith 8189b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 819d64ed03dSBarry Smith PetscStackPush("SNES user function"); 8209b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 821d64ed03dSBarry Smith PetscStackPop; 822ae3c334cSLois Curfman McInnes snes->nfuncs++; 8239b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 8243a40ed3dSBarry Smith PetscFunctionReturn(0); 8259b94acceSBarry Smith } 8269b94acceSBarry Smith 8275615d1e5SSatish Balay #undef __FUNC__ 828b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetMinimizationFunction" 8299b94acceSBarry Smith /*@C 8309b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 8319b94acceSBarry Smith unconstrained minimization. 8329b94acceSBarry Smith 833fee21e36SBarry Smith Collective on SNES 834fee21e36SBarry Smith 835c7afd0dbSLois Curfman McInnes Input Parameters: 836c7afd0dbSLois Curfman McInnes + snes - the SNES context 837c7afd0dbSLois Curfman McInnes . func - function evaluation routine 838c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 839c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8409b94acceSBarry Smith 841c7afd0dbSLois Curfman McInnes Calling sequence of func: 842329f5518SBarry Smith $ func (SNES snes,Vec x,PetscReal *f,void *ctx); 843c7afd0dbSLois Curfman McInnes 844c7afd0dbSLois Curfman McInnes + x - input vector 8459b94acceSBarry Smith . f - function 846c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined function context 8479b94acceSBarry Smith 84836851e7fSLois Curfman McInnes Level: beginner 84936851e7fSLois Curfman McInnes 8509b94acceSBarry Smith Notes: 8519b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 8529b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 8539b94acceSBarry Smith SNESSetFunction(). 8549b94acceSBarry Smith 8559b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 8569b94acceSBarry Smith 857a86d99e1SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 858a86d99e1SLois Curfman McInnes SNESSetHessian(), SNESSetGradient() 8599b94acceSBarry Smith @*/ 860329f5518SBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,PetscReal*,void*),void *ctx) 8619b94acceSBarry Smith { 8623a40ed3dSBarry Smith PetscFunctionBegin; 86377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 864a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 865a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 866a8c6a408SBarry Smith } 8679b94acceSBarry Smith snes->computeumfunction = func; 8689b94acceSBarry Smith snes->umfunP = ctx; 8693a40ed3dSBarry Smith PetscFunctionReturn(0); 8709b94acceSBarry Smith } 8719b94acceSBarry Smith 8725615d1e5SSatish Balay #undef __FUNC__ 873b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeMinimizationFunction" 8749b94acceSBarry Smith /*@ 8759b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 8769b94acceSBarry Smith set with SNESSetMinimizationFunction(). 8779b94acceSBarry Smith 878c7afd0dbSLois Curfman McInnes Collective on SNES 879c7afd0dbSLois Curfman McInnes 8809b94acceSBarry Smith Input Parameters: 881c7afd0dbSLois Curfman McInnes + snes - the SNES context 882c7afd0dbSLois Curfman McInnes - x - input vector 8839b94acceSBarry Smith 8849b94acceSBarry Smith Output Parameter: 8859b94acceSBarry Smith . y - function value 8869b94acceSBarry Smith 8879b94acceSBarry Smith Notes: 8889b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 8899b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 8909b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 891a86d99e1SLois Curfman McInnes 89236851e7fSLois Curfman McInnes SNESComputeMinimizationFunction() is typically used within minimization 89336851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 89436851e7fSLois Curfman McInnes themselves. 89536851e7fSLois Curfman McInnes 89636851e7fSLois Curfman McInnes Level: developer 89736851e7fSLois Curfman McInnes 898a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function 899a86d99e1SLois Curfman McInnes 900a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 901a86d99e1SLois Curfman McInnes SNESComputeGradient(), SNESComputeHessian() 9029b94acceSBarry Smith @*/ 903329f5518SBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,PetscReal *y) 9049b94acceSBarry Smith { 9059b94acceSBarry Smith int ierr; 9063a40ed3dSBarry Smith 9073a40ed3dSBarry Smith PetscFunctionBegin; 908184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 909184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 910184914b5SBarry Smith PetscCheckSameComm(snes,x); 911a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 912a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 913a8c6a408SBarry Smith } 914184914b5SBarry Smith 9159b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 916d64ed03dSBarry Smith PetscStackPush("SNES user minimzation function"); 9179b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr); 918d64ed03dSBarry Smith PetscStackPop; 919ae3c334cSLois Curfman McInnes snes->nfuncs++; 9209b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 9213a40ed3dSBarry Smith PetscFunctionReturn(0); 9229b94acceSBarry Smith } 9239b94acceSBarry Smith 9245615d1e5SSatish Balay #undef __FUNC__ 925b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetGradient" 9269b94acceSBarry Smith /*@C 9279b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 9289b94acceSBarry Smith vector for use by the SNES routines. 9299b94acceSBarry Smith 930c7afd0dbSLois Curfman McInnes Collective on SNES 931c7afd0dbSLois Curfman McInnes 9329b94acceSBarry Smith Input Parameters: 933c7afd0dbSLois Curfman McInnes + snes - the SNES context 9349b94acceSBarry Smith . func - function evaluation routine 935044dda88SLois Curfman McInnes . ctx - optional user-defined context for private data for the 936044dda88SLois Curfman McInnes gradient evaluation routine (may be PETSC_NULL) 937c7afd0dbSLois Curfman McInnes - r - vector to store gradient value 938fee21e36SBarry Smith 9399b94acceSBarry Smith Calling sequence of func: 9408d76a1e5SLois Curfman McInnes $ func (SNES, Vec x, Vec g, void *ctx); 9419b94acceSBarry Smith 942c7afd0dbSLois Curfman McInnes + x - input vector 9439b94acceSBarry Smith . g - gradient vector 944c7afd0dbSLois Curfman McInnes - ctx - optional user-defined gradient context 9459b94acceSBarry Smith 9469b94acceSBarry Smith Notes: 9479b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 9489b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 9499b94acceSBarry Smith SNESSetFunction(). 9509b94acceSBarry Smith 95136851e7fSLois Curfman McInnes Level: beginner 95236851e7fSLois Curfman McInnes 9539b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9549b94acceSBarry Smith 955a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 956a86d99e1SLois Curfman McInnes SNESSetMinimizationFunction(), 9579b94acceSBarry Smith @*/ 95874679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 9599b94acceSBarry Smith { 9603a40ed3dSBarry Smith PetscFunctionBegin; 96177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 962184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 963184914b5SBarry Smith PetscCheckSameComm(snes,r); 964a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 965a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 966a8c6a408SBarry Smith } 9679b94acceSBarry Smith snes->computefunction = func; 9689b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 9699b94acceSBarry Smith snes->funP = ctx; 9703a40ed3dSBarry Smith PetscFunctionReturn(0); 9719b94acceSBarry Smith } 9729b94acceSBarry Smith 9735615d1e5SSatish Balay #undef __FUNC__ 974b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeGradient" 9759b94acceSBarry Smith /*@ 976a86d99e1SLois Curfman McInnes SNESComputeGradient - Computes the gradient that has been set with 977a86d99e1SLois Curfman McInnes SNESSetGradient(). 9789b94acceSBarry Smith 979c7afd0dbSLois Curfman McInnes Collective on SNES 980c7afd0dbSLois Curfman McInnes 9819b94acceSBarry Smith Input Parameters: 982c7afd0dbSLois Curfman McInnes + snes - the SNES context 983c7afd0dbSLois Curfman McInnes - x - input vector 9849b94acceSBarry Smith 9859b94acceSBarry Smith Output Parameter: 9869b94acceSBarry Smith . y - gradient vector 9879b94acceSBarry Smith 9889b94acceSBarry Smith Notes: 9899b94acceSBarry Smith SNESComputeGradient() is valid only for 9909b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 9919b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 992a86d99e1SLois Curfman McInnes 99336851e7fSLois Curfman McInnes SNESComputeGradient() is typically used within minimization 99436851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 99536851e7fSLois Curfman McInnes themselves. 99636851e7fSLois Curfman McInnes 99736851e7fSLois Curfman McInnes Level: developer 99836851e7fSLois Curfman McInnes 999a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient 1000a86d99e1SLois Curfman McInnes 1001a86d99e1SLois Curfman McInnes .seealso: SNESSetGradient(), SNESGetGradient(), 1002a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction(), SNESComputeHessian() 10039b94acceSBarry Smith @*/ 10049b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x,Vec y) 10059b94acceSBarry Smith { 10069b94acceSBarry Smith int ierr; 10073a40ed3dSBarry Smith 10083a40ed3dSBarry Smith PetscFunctionBegin; 1009184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1010184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1011184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1012184914b5SBarry Smith PetscCheckSameComm(snes,x); 1013184914b5SBarry Smith PetscCheckSameComm(snes,y); 10143a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1015a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 10163a40ed3dSBarry Smith } 10179b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 1018d64ed03dSBarry Smith PetscStackPush("SNES user gradient function"); 10199b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1020d64ed03dSBarry Smith PetscStackPop; 10219b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 10223a40ed3dSBarry Smith PetscFunctionReturn(0); 10239b94acceSBarry Smith } 10249b94acceSBarry Smith 10255615d1e5SSatish Balay #undef __FUNC__ 1026b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeJacobian" 102762fef451SLois Curfman McInnes /*@ 102862fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 102962fef451SLois Curfman McInnes set with SNESSetJacobian(). 103062fef451SLois Curfman McInnes 1031c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1032c7afd0dbSLois Curfman McInnes 103362fef451SLois Curfman McInnes Input Parameters: 1034c7afd0dbSLois Curfman McInnes + snes - the SNES context 1035c7afd0dbSLois Curfman McInnes - x - input vector 103662fef451SLois Curfman McInnes 103762fef451SLois Curfman McInnes Output Parameters: 1038c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 103962fef451SLois Curfman McInnes . B - optional preconditioning matrix 1040c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1041fee21e36SBarry Smith 104262fef451SLois Curfman McInnes Notes: 104362fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 104462fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 104562fef451SLois Curfman McInnes 1046dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1047dc5a77f8SLois Curfman McInnes flag parameter. 104862fef451SLois Curfman McInnes 104962fef451SLois Curfman McInnes SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 105062fef451SLois Curfman McInnes methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 1051005c665bSBarry Smith methods is SNESComputeHessian(). 105262fef451SLois Curfman McInnes 105336851e7fSLois Curfman McInnes Level: developer 105436851e7fSLois Curfman McInnes 105562fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 105662fef451SLois Curfman McInnes 105762fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 105862fef451SLois Curfman McInnes @*/ 10599b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10609b94acceSBarry Smith { 10619b94acceSBarry Smith int ierr; 10623a40ed3dSBarry Smith 10633a40ed3dSBarry Smith PetscFunctionBegin; 1064184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1065184914b5SBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE); 1066184914b5SBarry Smith PetscCheckSameComm(snes,X); 10673a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1068a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 10693a40ed3dSBarry Smith } 10703a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 10719b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 1072c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1073d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 10749b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1075d64ed03dSBarry Smith PetscStackPop; 10769b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 10776d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 107877c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 107977c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 10803a40ed3dSBarry Smith PetscFunctionReturn(0); 10819b94acceSBarry Smith } 10829b94acceSBarry Smith 10835615d1e5SSatish Balay #undef __FUNC__ 1084b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeHessian" 108562fef451SLois Curfman McInnes /*@ 108662fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 108762fef451SLois Curfman McInnes set with SNESSetHessian(). 108862fef451SLois Curfman McInnes 1089c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1090c7afd0dbSLois Curfman McInnes 109162fef451SLois Curfman McInnes Input Parameters: 1092c7afd0dbSLois Curfman McInnes + snes - the SNES context 1093c7afd0dbSLois Curfman McInnes - x - input vector 109462fef451SLois Curfman McInnes 109562fef451SLois Curfman McInnes Output Parameters: 1096c7afd0dbSLois Curfman McInnes + A - Hessian matrix 109762fef451SLois Curfman McInnes . B - optional preconditioning matrix 1098c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1099fee21e36SBarry Smith 110062fef451SLois Curfman McInnes Notes: 110162fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 110262fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 110362fef451SLois Curfman McInnes 1104dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1105dc5a77f8SLois Curfman McInnes flag parameter. 110662fef451SLois Curfman McInnes 110762fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 110862fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 110962fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 111062fef451SLois Curfman McInnes 111136851e7fSLois Curfman McInnes SNESComputeHessian() is typically used within minimization 111236851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 111336851e7fSLois Curfman McInnes themselves. 111436851e7fSLois Curfman McInnes 111536851e7fSLois Curfman McInnes Level: developer 111636851e7fSLois Curfman McInnes 111762fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 111862fef451SLois Curfman McInnes 1119a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 1120a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 112162fef451SLois Curfman McInnes @*/ 112262fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 11239b94acceSBarry Smith { 11249b94acceSBarry Smith int ierr; 11253a40ed3dSBarry Smith 11263a40ed3dSBarry Smith PetscFunctionBegin; 1127184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1128184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1129184914b5SBarry Smith PetscCheckSameComm(snes,x); 11303a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1131a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 11323a40ed3dSBarry Smith } 11333a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 113462fef451SLois Curfman McInnes PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 11350f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 1136d64ed03dSBarry Smith PetscStackPush("SNES user Hessian function"); 113762fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr); 1138d64ed03dSBarry Smith PetscStackPop; 113962fef451SLois Curfman McInnes PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 11400f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 114177c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 114277c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 11433a40ed3dSBarry Smith PetscFunctionReturn(0); 11449b94acceSBarry Smith } 11459b94acceSBarry Smith 11465615d1e5SSatish Balay #undef __FUNC__ 1147b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetJacobian" 11489b94acceSBarry Smith /*@C 11499b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1150044dda88SLois Curfman McInnes location to store the matrix. 11519b94acceSBarry Smith 1152c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1153c7afd0dbSLois Curfman McInnes 11549b94acceSBarry Smith Input Parameters: 1155c7afd0dbSLois Curfman McInnes + snes - the SNES context 11569b94acceSBarry Smith . A - Jacobian matrix 11579b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11589b94acceSBarry Smith . func - Jacobian evaluation routine 1159c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11602cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11619b94acceSBarry Smith 11629b94acceSBarry Smith Calling sequence of func: 11638d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11649b94acceSBarry Smith 1165c7afd0dbSLois Curfman McInnes + x - input vector 11669b94acceSBarry Smith . A - Jacobian matrix 11679b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1168ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1169ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1170c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11719b94acceSBarry Smith 11729b94acceSBarry Smith Notes: 1173dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 11742cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1175ac21db08SLois Curfman McInnes 1176ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11779b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11789b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11799b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11809b94acceSBarry Smith throughout the global iterations. 11819b94acceSBarry Smith 118236851e7fSLois Curfman McInnes Level: beginner 118336851e7fSLois Curfman McInnes 11849b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11859b94acceSBarry Smith 1186ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 11879b94acceSBarry Smith @*/ 1188454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 11899b94acceSBarry Smith { 11903a40ed3dSBarry Smith PetscFunctionBegin; 119177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 119200036973SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE); 119300036973SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE); 119400036973SBarry Smith if (A) PetscCheckSameComm(snes,A); 119500036973SBarry Smith if (B) PetscCheckSameComm(snes,B); 1196a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1197a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 1198a8c6a408SBarry Smith } 1199184914b5SBarry Smith 12009b94acceSBarry Smith snes->computejacobian = func; 12019b94acceSBarry Smith snes->jacP = ctx; 12029b94acceSBarry Smith snes->jacobian = A; 12039b94acceSBarry Smith snes->jacobian_pre = B; 12043a40ed3dSBarry Smith PetscFunctionReturn(0); 12059b94acceSBarry Smith } 120662fef451SLois Curfman McInnes 12075615d1e5SSatish Balay #undef __FUNC__ 1208b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetJacobian" 1209c2aafc4cSSatish Balay /*@C 1210b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1211b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1212b4fd4287SBarry Smith 1213c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1214c7afd0dbSLois Curfman McInnes 1215b4fd4287SBarry Smith Input Parameter: 1216b4fd4287SBarry Smith . snes - the nonlinear solver context 1217b4fd4287SBarry Smith 1218b4fd4287SBarry Smith Output Parameters: 1219c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1220b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 122100036973SBarry Smith . ctx - location to stash Jacobian ctx (or PETSC_NULL) 122200036973SBarry Smith - func - location to put Jacobian function (or PETSC_NULL) 1223fee21e36SBarry Smith 122436851e7fSLois Curfman McInnes Level: advanced 122536851e7fSLois Curfman McInnes 1226b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1227b4fd4287SBarry Smith @*/ 122800036973SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx,int (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*)) 1229b4fd4287SBarry Smith { 12303a40ed3dSBarry Smith PetscFunctionBegin; 1231184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 12323a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1233a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 12343a40ed3dSBarry Smith } 1235b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1236b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1237b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 123800036973SBarry Smith if (func) *func = snes->computejacobian; 12393a40ed3dSBarry Smith PetscFunctionReturn(0); 1240b4fd4287SBarry Smith } 1241b4fd4287SBarry Smith 12425615d1e5SSatish Balay #undef __FUNC__ 1243b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetHessian" 12449b94acceSBarry Smith /*@C 12459b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 1246044dda88SLois Curfman McInnes location to store the matrix. 12479b94acceSBarry Smith 1248c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1249c7afd0dbSLois Curfman McInnes 12509b94acceSBarry Smith Input Parameters: 1251c7afd0dbSLois Curfman McInnes + snes - the SNES context 12529b94acceSBarry Smith . A - Hessian matrix 12539b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 12549b94acceSBarry Smith . func - Jacobian evaluation routine 1255c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1256313e4042SLois Curfman McInnes Hessian evaluation routine (may be PETSC_NULL) 12579b94acceSBarry Smith 12589b94acceSBarry Smith Calling sequence of func: 12598d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 12609b94acceSBarry Smith 1261c7afd0dbSLois Curfman McInnes + x - input vector 12629b94acceSBarry Smith . A - Hessian matrix 12639b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1264ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1265ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1266c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Hessian context 12679b94acceSBarry Smith 12689b94acceSBarry Smith Notes: 1269dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 12702cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1271ac21db08SLois Curfman McInnes 12729b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 12739b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 12749b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 12759b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 12769b94acceSBarry Smith throughout the global iterations. 12779b94acceSBarry Smith 127836851e7fSLois Curfman McInnes Level: beginner 127936851e7fSLois Curfman McInnes 12809b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 12819b94acceSBarry Smith 1282ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 12839b94acceSBarry Smith @*/ 1284454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 12859b94acceSBarry Smith { 12863a40ed3dSBarry Smith PetscFunctionBegin; 128777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1288184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1289184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1290184914b5SBarry Smith PetscCheckSameComm(snes,A); 1291184914b5SBarry Smith PetscCheckSameComm(snes,B); 1292d4bb536fSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1293a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1294d4bb536fSBarry Smith } 12959b94acceSBarry Smith snes->computejacobian = func; 12969b94acceSBarry Smith snes->jacP = ctx; 12979b94acceSBarry Smith snes->jacobian = A; 12989b94acceSBarry Smith snes->jacobian_pre = B; 12993a40ed3dSBarry Smith PetscFunctionReturn(0); 13009b94acceSBarry Smith } 13019b94acceSBarry Smith 13025615d1e5SSatish Balay #undef __FUNC__ 1303b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetHessian" 130462fef451SLois Curfman McInnes /*@ 130562fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 130662fef451SLois Curfman McInnes provided context for evaluating the Hessian. 130762fef451SLois Curfman McInnes 1308c7afd0dbSLois Curfman McInnes Not Collective, but Mat object is parallel if SNES object is parallel 1309c7afd0dbSLois Curfman McInnes 131062fef451SLois Curfman McInnes Input Parameter: 131162fef451SLois Curfman McInnes . snes - the nonlinear solver context 131262fef451SLois Curfman McInnes 131362fef451SLois Curfman McInnes Output Parameters: 1314c7afd0dbSLois Curfman McInnes + A - location to stash Hessian matrix (or PETSC_NULL) 131562fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 1316c7afd0dbSLois Curfman McInnes - ctx - location to stash Hessian ctx (or PETSC_NULL) 1317fee21e36SBarry Smith 131836851e7fSLois Curfman McInnes Level: advanced 131936851e7fSLois Curfman McInnes 132062fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 1321c7afd0dbSLois Curfman McInnes 1322c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian 132362fef451SLois Curfman McInnes @*/ 132462fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B,void **ctx) 132562fef451SLois Curfman McInnes { 13263a40ed3dSBarry Smith PetscFunctionBegin; 1327184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 13283a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){ 1329a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 13303a40ed3dSBarry Smith } 133162fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 133262fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 133362fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 13343a40ed3dSBarry Smith PetscFunctionReturn(0); 133562fef451SLois Curfman McInnes } 133662fef451SLois Curfman McInnes 13379b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 13389b94acceSBarry Smith 13395615d1e5SSatish Balay #undef __FUNC__ 1340b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetUp" 13419b94acceSBarry Smith /*@ 13429b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1343272ac6f2SLois Curfman McInnes of a nonlinear solver. 13449b94acceSBarry Smith 1345fee21e36SBarry Smith Collective on SNES 1346fee21e36SBarry Smith 1347c7afd0dbSLois Curfman McInnes Input Parameters: 1348c7afd0dbSLois Curfman McInnes + snes - the SNES context 1349c7afd0dbSLois Curfman McInnes - x - the solution vector 1350c7afd0dbSLois Curfman McInnes 1351272ac6f2SLois Curfman McInnes Notes: 1352272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1353272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1354272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1355272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1356272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1357272ac6f2SLois Curfman McInnes 135836851e7fSLois Curfman McInnes Level: advanced 135936851e7fSLois Curfman McInnes 13609b94acceSBarry Smith .keywords: SNES, nonlinear, setup 13619b94acceSBarry Smith 13629b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 13639b94acceSBarry Smith @*/ 13648ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 13659b94acceSBarry Smith { 1366f1af5d2fSBarry Smith int ierr; 1367f1af5d2fSBarry Smith PetscTruth flg; 13683a40ed3dSBarry Smith 13693a40ed3dSBarry Smith PetscFunctionBegin; 137077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 137177c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1372184914b5SBarry Smith PetscCheckSameComm(snes,x); 13738ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 13749b94acceSBarry Smith 1375c1f60f51SBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1376c1f60f51SBarry Smith /* 1377c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1378dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1379c1f60f51SBarry Smith */ 1380c1f60f51SBarry Smith if (flg) { 1381c1f60f51SBarry Smith Mat J; 13825a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1383c1f60f51SBarry Smith PLogObjectParent(snes,J); 1384c1f60f51SBarry Smith snes->mfshell = J; 1385c1f60f51SBarry Smith snes->jacobian = J; 1386c2aafc4cSSatish Balay if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1387c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n"); 1388d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1389c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n"); 1390d4bb536fSBarry Smith } else { 1391a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option"); 1392d4bb536fSBarry Smith } 13935a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1394c1f60f51SBarry Smith } 1395272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1396c1f60f51SBarry Smith /* 1397dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1398c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1399c1f60f51SBarry Smith */ 140031615d04SLois Curfman McInnes if (flg) { 1401272ac6f2SLois Curfman McInnes Mat J; 14025a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1403272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 1404272ac6f2SLois Curfman McInnes snes->mfshell = J; 140583e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1406*1d1367b7SBarry Smith ierr = SNESSetJacobian(snes,J,J,MatSNESMFFormJacobian,snes->funP);CHKERRQ(ierr); 140794a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 1408d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1409*1d1367b7SBarry Smith ierr = SNESSetHessian(snes,J,J,MatSNESMFFormJacobian,snes->funP);CHKERRQ(ierr); 141094a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 1411d4bb536fSBarry Smith } else { 1412a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option"); 1413d4bb536fSBarry Smith } 14145a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1415272ac6f2SLois Curfman McInnes } 14169b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 14176831982aSBarry Smith PetscTruth iseqtr; 14186831982aSBarry Smith 1419a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 1420a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 14215a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 1422a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 1423a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector"); 1424a8c6a408SBarry Smith } 1425a703fe33SLois Curfman McInnes 1426a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 14276831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr); 14286831982aSBarry Smith if (snes->ksp_ewconv && !iseqtr) { 1429a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 1430a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 1431a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 14325a655dc6SBarry Smith ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr); 1433a703fe33SLois Curfman McInnes } 14349b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 1435a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1436a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1437a8c6a408SBarry Smith if (!snes->computeumfunction) { 1438a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first"); 1439a8c6a408SBarry Smith } 14405a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()"); 1441d4bb536fSBarry Smith } else { 1442a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class"); 1443d4bb536fSBarry Smith } 1444a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);} 144582bf6240SBarry Smith snes->setupcalled = 1; 14463a40ed3dSBarry Smith PetscFunctionReturn(0); 14479b94acceSBarry Smith } 14489b94acceSBarry Smith 14495615d1e5SSatish Balay #undef __FUNC__ 1450b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESDestroy" 14519b94acceSBarry Smith /*@C 14529b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 14539b94acceSBarry Smith with SNESCreate(). 14549b94acceSBarry Smith 1455c7afd0dbSLois Curfman McInnes Collective on SNES 1456c7afd0dbSLois Curfman McInnes 14579b94acceSBarry Smith Input Parameter: 14589b94acceSBarry Smith . snes - the SNES context 14599b94acceSBarry Smith 146036851e7fSLois Curfman McInnes Level: beginner 146136851e7fSLois Curfman McInnes 14629b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 14639b94acceSBarry Smith 146463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 14659b94acceSBarry Smith @*/ 14669b94acceSBarry Smith int SNESDestroy(SNES snes) 14679b94acceSBarry Smith { 1468b8a78c4aSBarry Smith int i,ierr; 14693a40ed3dSBarry Smith 14703a40ed3dSBarry Smith PetscFunctionBegin; 147177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14723a40ed3dSBarry Smith if (--snes->refct > 0) PetscFunctionReturn(0); 1473d4bb536fSBarry Smith 1474be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 14750f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1476be0abb6dSBarry Smith 1477e1311b90SBarry Smith if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);} 1478606d414cSSatish Balay if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);} 1479522c5e43SBarry Smith if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);} 14809b94acceSBarry Smith ierr = SLESDestroy(snes->sles);CHKERRQ(ierr); 1481522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1482b8a78c4aSBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1483b8a78c4aSBarry Smith if (snes->monitordestroy[i]) { 1484b8a78c4aSBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1485b8a78c4aSBarry Smith } 1486b8a78c4aSBarry Smith } 14879b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 14880452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 14893a40ed3dSBarry Smith PetscFunctionReturn(0); 14909b94acceSBarry Smith } 14919b94acceSBarry Smith 14929b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 14939b94acceSBarry Smith 14945615d1e5SSatish Balay #undef __FUNC__ 1495b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetTolerances" 14969b94acceSBarry Smith /*@ 1497d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 14989b94acceSBarry Smith 1499c7afd0dbSLois Curfman McInnes Collective on SNES 1500c7afd0dbSLois Curfman McInnes 15019b94acceSBarry Smith Input Parameters: 1502c7afd0dbSLois Curfman McInnes + snes - the SNES context 150333174efeSLois Curfman McInnes . atol - absolute convergence tolerance 150433174efeSLois Curfman McInnes . rtol - relative convergence tolerance 150533174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 150633174efeSLois Curfman McInnes of the change in the solution between steps 150733174efeSLois Curfman McInnes . maxit - maximum number of iterations 1508c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1509fee21e36SBarry Smith 151033174efeSLois Curfman McInnes Options Database Keys: 1511c7afd0dbSLois Curfman McInnes + -snes_atol <atol> - Sets atol 1512c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1513c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1514c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1515c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15169b94acceSBarry Smith 1517d7a720efSLois Curfman McInnes Notes: 15189b94acceSBarry Smith The default maximum number of iterations is 50. 15199b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15209b94acceSBarry Smith 152136851e7fSLois Curfman McInnes Level: intermediate 152236851e7fSLois Curfman McInnes 152333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15249b94acceSBarry Smith 1525d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 15269b94acceSBarry Smith @*/ 1527329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf) 15289b94acceSBarry Smith { 15293a40ed3dSBarry Smith PetscFunctionBegin; 153077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1531d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1532d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1533d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1534d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1535d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15363a40ed3dSBarry Smith PetscFunctionReturn(0); 15379b94acceSBarry Smith } 15389b94acceSBarry Smith 15395615d1e5SSatish Balay #undef __FUNC__ 1540b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetTolerances" 15419b94acceSBarry Smith /*@ 154233174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 154333174efeSLois Curfman McInnes 1544c7afd0dbSLois Curfman McInnes Not Collective 1545c7afd0dbSLois Curfman McInnes 154633174efeSLois Curfman McInnes Input Parameters: 1547c7afd0dbSLois Curfman McInnes + snes - the SNES context 154833174efeSLois Curfman McInnes . atol - absolute convergence tolerance 154933174efeSLois Curfman McInnes . rtol - relative convergence tolerance 155033174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 155133174efeSLois Curfman McInnes of the change in the solution between steps 155233174efeSLois Curfman McInnes . maxit - maximum number of iterations 1553c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1554fee21e36SBarry Smith 155533174efeSLois Curfman McInnes Notes: 155633174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 155733174efeSLois Curfman McInnes 155836851e7fSLois Curfman McInnes Level: intermediate 155936851e7fSLois Curfman McInnes 156033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 156133174efeSLois Curfman McInnes 156233174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 156333174efeSLois Curfman McInnes @*/ 1564329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf) 156533174efeSLois Curfman McInnes { 15663a40ed3dSBarry Smith PetscFunctionBegin; 156733174efeSLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 156833174efeSLois Curfman McInnes if (atol) *atol = snes->atol; 156933174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 157033174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 157133174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 157233174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 15733a40ed3dSBarry Smith PetscFunctionReturn(0); 157433174efeSLois Curfman McInnes } 157533174efeSLois Curfman McInnes 15765615d1e5SSatish Balay #undef __FUNC__ 1577b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetTrustRegionTolerance" 157833174efeSLois Curfman McInnes /*@ 15799b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 15809b94acceSBarry Smith 1581fee21e36SBarry Smith Collective on SNES 1582fee21e36SBarry Smith 1583c7afd0dbSLois Curfman McInnes Input Parameters: 1584c7afd0dbSLois Curfman McInnes + snes - the SNES context 1585c7afd0dbSLois Curfman McInnes - tol - tolerance 1586c7afd0dbSLois Curfman McInnes 15879b94acceSBarry Smith Options Database Key: 1588c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 15899b94acceSBarry Smith 159036851e7fSLois Curfman McInnes Level: intermediate 159136851e7fSLois Curfman McInnes 15929b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 15939b94acceSBarry Smith 1594d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 15959b94acceSBarry Smith @*/ 1596329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 15979b94acceSBarry Smith { 15983a40ed3dSBarry Smith PetscFunctionBegin; 159977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16009b94acceSBarry Smith snes->deltatol = tol; 16013a40ed3dSBarry Smith PetscFunctionReturn(0); 16029b94acceSBarry Smith } 16039b94acceSBarry Smith 16045615d1e5SSatish Balay #undef __FUNC__ 1605b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetMinimizationFunctionTolerance" 16069b94acceSBarry Smith /*@ 160777c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 16089b94acceSBarry Smith for unconstrained minimization solvers. 16099b94acceSBarry Smith 1610fee21e36SBarry Smith Collective on SNES 1611fee21e36SBarry Smith 1612c7afd0dbSLois Curfman McInnes Input Parameters: 1613c7afd0dbSLois Curfman McInnes + snes - the SNES context 1614c7afd0dbSLois Curfman McInnes - ftol - minimum function tolerance 1615c7afd0dbSLois Curfman McInnes 16169b94acceSBarry Smith Options Database Key: 1617c7afd0dbSLois Curfman McInnes . -snes_fmin <ftol> - Sets ftol 16189b94acceSBarry Smith 16199b94acceSBarry Smith Note: 162077c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 16219b94acceSBarry Smith methods only. 16229b94acceSBarry Smith 162336851e7fSLois Curfman McInnes Level: intermediate 162436851e7fSLois Curfman McInnes 16259b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 16269b94acceSBarry Smith 1627d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 16289b94acceSBarry Smith @*/ 1629329f5518SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,PetscReal ftol) 16309b94acceSBarry Smith { 16313a40ed3dSBarry Smith PetscFunctionBegin; 163277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16339b94acceSBarry Smith snes->fmin = ftol; 16343a40ed3dSBarry Smith PetscFunctionReturn(0); 16359b94acceSBarry Smith } 1636df9fa365SBarry Smith /* 1637df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1638df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1639df9fa365SBarry Smith macros instead of functions 1640df9fa365SBarry Smith */ 1641ce1608b8SBarry Smith #undef __FUNC__ 1642b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESLGMonitor" 1643329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx) 1644ce1608b8SBarry Smith { 1645ce1608b8SBarry Smith int ierr; 1646ce1608b8SBarry Smith 1647ce1608b8SBarry Smith PetscFunctionBegin; 1648184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1649ce1608b8SBarry Smith ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1650ce1608b8SBarry Smith PetscFunctionReturn(0); 1651ce1608b8SBarry Smith } 1652ce1608b8SBarry Smith 1653df9fa365SBarry Smith #undef __FUNC__ 1654b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESLGMonitorCreate" 1655df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,DrawLG *draw) 1656df9fa365SBarry Smith { 1657df9fa365SBarry Smith int ierr; 1658df9fa365SBarry Smith 1659df9fa365SBarry Smith PetscFunctionBegin; 1660df9fa365SBarry Smith ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1661df9fa365SBarry Smith PetscFunctionReturn(0); 1662df9fa365SBarry Smith } 1663df9fa365SBarry Smith 1664df9fa365SBarry Smith #undef __FUNC__ 1665b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESLGMonitorDestroy" 1666df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw) 1667df9fa365SBarry Smith { 1668df9fa365SBarry Smith int ierr; 1669df9fa365SBarry Smith 1670df9fa365SBarry Smith PetscFunctionBegin; 1671df9fa365SBarry Smith ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr); 1672df9fa365SBarry Smith PetscFunctionReturn(0); 1673df9fa365SBarry Smith } 1674df9fa365SBarry Smith 16759b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 16769b94acceSBarry Smith 16775615d1e5SSatish Balay #undef __FUNC__ 1678b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetMonitor" 16799b94acceSBarry Smith /*@C 16805cd90555SBarry Smith SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every 16819b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 16829b94acceSBarry Smith progress. 16839b94acceSBarry Smith 1684fee21e36SBarry Smith Collective on SNES 1685fee21e36SBarry Smith 1686c7afd0dbSLois Curfman McInnes Input Parameters: 1687c7afd0dbSLois Curfman McInnes + snes - the SNES context 1688c7afd0dbSLois Curfman McInnes . func - monitoring routine 1689b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1690b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desitre) 1691b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1692b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 16939b94acceSBarry Smith 1694c7afd0dbSLois Curfman McInnes Calling sequence of func: 1695329f5518SBarry Smith $ int func(SNES snes,int its, PetscReal norm,void *mctx) 1696c7afd0dbSLois Curfman McInnes 1697c7afd0dbSLois Curfman McInnes + snes - the SNES context 1698c7afd0dbSLois Curfman McInnes . its - iteration number 1699c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 1700c7afd0dbSLois Curfman McInnes for SNES_NONLINEAR_EQUATIONS methods 170140a0c1c6SLois Curfman McInnes . norm - 2-norm gradient value (may be estimated) 1702c7afd0dbSLois Curfman McInnes for SNES_UNCONSTRAINED_MINIMIZATION methods 170340a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 17049b94acceSBarry Smith 17059665c990SLois Curfman McInnes Options Database Keys: 1706c7afd0dbSLois Curfman McInnes + -snes_monitor - sets SNESDefaultMonitor() 1707c7afd0dbSLois Curfman McInnes . -snes_xmonitor - sets line graph monitor, 1708c7afd0dbSLois Curfman McInnes uses SNESLGMonitorCreate() 1709c7afd0dbSLois Curfman McInnes _ -snes_cancelmonitors - cancels all monitors that have 1710c7afd0dbSLois Curfman McInnes been hardwired into a code by 1711c7afd0dbSLois Curfman McInnes calls to SNESSetMonitor(), but 1712c7afd0dbSLois Curfman McInnes does not cancel those set via 1713c7afd0dbSLois Curfman McInnes the options database. 17149665c990SLois Curfman McInnes 1715639f9d9dSBarry Smith Notes: 17166bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 17176bc08f3fSLois Curfman McInnes SNESSetMonitor() multiple times; all will be called in the 17186bc08f3fSLois Curfman McInnes order in which they were set. 1719639f9d9dSBarry Smith 172036851e7fSLois Curfman McInnes Level: intermediate 172136851e7fSLois Curfman McInnes 17229b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 17239b94acceSBarry Smith 17245cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor() 17259b94acceSBarry Smith @*/ 1726329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *)) 17279b94acceSBarry Smith { 17283a40ed3dSBarry Smith PetscFunctionBegin; 1729184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1730639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 1731a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set"); 1732639f9d9dSBarry Smith } 1733639f9d9dSBarry Smith 1734639f9d9dSBarry Smith snes->monitor[snes->numbermonitors] = func; 1735b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1736639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 17373a40ed3dSBarry Smith PetscFunctionReturn(0); 17389b94acceSBarry Smith } 17399b94acceSBarry Smith 17405615d1e5SSatish Balay #undef __FUNC__ 1741b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESClearMonitor" 17425cd90555SBarry Smith /*@C 17435cd90555SBarry Smith SNESClearMonitor - Clears all the monitor functions for a SNES object. 17445cd90555SBarry Smith 1745c7afd0dbSLois Curfman McInnes Collective on SNES 1746c7afd0dbSLois Curfman McInnes 17475cd90555SBarry Smith Input Parameters: 17485cd90555SBarry Smith . snes - the SNES context 17495cd90555SBarry Smith 17505cd90555SBarry Smith Options Database: 1751c7afd0dbSLois Curfman McInnes . -snes_cancelmonitors - cancels all monitors that have been hardwired 1752c7afd0dbSLois Curfman McInnes into a code by calls to SNESSetMonitor(), but does not cancel those 1753c7afd0dbSLois Curfman McInnes set via the options database 17545cd90555SBarry Smith 17555cd90555SBarry Smith Notes: 17565cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 17575cd90555SBarry Smith 175836851e7fSLois Curfman McInnes Level: intermediate 175936851e7fSLois Curfman McInnes 17605cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 17615cd90555SBarry Smith 17625cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor() 17635cd90555SBarry Smith @*/ 17645cd90555SBarry Smith int SNESClearMonitor(SNES snes) 17655cd90555SBarry Smith { 17665cd90555SBarry Smith PetscFunctionBegin; 1767184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 17685cd90555SBarry Smith snes->numbermonitors = 0; 17695cd90555SBarry Smith PetscFunctionReturn(0); 17705cd90555SBarry Smith } 17715cd90555SBarry Smith 17725cd90555SBarry Smith #undef __FUNC__ 1773b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetConvergenceTest" 17749b94acceSBarry Smith /*@C 17759b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 17769b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 17779b94acceSBarry Smith 1778fee21e36SBarry Smith Collective on SNES 1779fee21e36SBarry Smith 1780c7afd0dbSLois Curfman McInnes Input Parameters: 1781c7afd0dbSLois Curfman McInnes + snes - the SNES context 1782c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1783c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1784c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 17859b94acceSBarry Smith 1786c7afd0dbSLois Curfman McInnes Calling sequence of func: 1787329f5518SBarry Smith $ int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1788c7afd0dbSLois Curfman McInnes 1789c7afd0dbSLois Curfman McInnes + snes - the SNES context 1790c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1791184914b5SBarry Smith . reason - reason for convergence/divergence 1792c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 1793c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods) 1794c7afd0dbSLois Curfman McInnes . f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods) 1795c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods) 1796c7afd0dbSLois Curfman McInnes - f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods) 17979b94acceSBarry Smith 179836851e7fSLois Curfman McInnes Level: advanced 179936851e7fSLois Curfman McInnes 18009b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 18019b94acceSBarry Smith 180240191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 180340191667SLois Curfman McInnes SNESConverged_UM_LS(), SNESConverged_UM_TR() 18049b94acceSBarry Smith @*/ 1805329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx) 18069b94acceSBarry Smith { 18073a40ed3dSBarry Smith PetscFunctionBegin; 1808184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 18099b94acceSBarry Smith (snes)->converged = func; 18109b94acceSBarry Smith (snes)->cnvP = cctx; 18113a40ed3dSBarry Smith PetscFunctionReturn(0); 18129b94acceSBarry Smith } 18139b94acceSBarry Smith 18145615d1e5SSatish Balay #undef __FUNC__ 1815b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetConvergedReason" 1816184914b5SBarry Smith /*@C 1817184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1818184914b5SBarry Smith 1819184914b5SBarry Smith Not Collective 1820184914b5SBarry Smith 1821184914b5SBarry Smith Input Parameter: 1822184914b5SBarry Smith . snes - the SNES context 1823184914b5SBarry Smith 1824184914b5SBarry Smith Output Parameter: 1825e090d566SSatish Balay . reason - negative value indicates diverged, positive value converged, see petscsnes.h or the 1826184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1827184914b5SBarry Smith 1828184914b5SBarry Smith Level: intermediate 1829184914b5SBarry Smith 1830184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1831184914b5SBarry Smith 1832184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1833184914b5SBarry Smith 1834184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 1835184914b5SBarry Smith SNESConverged_UM_LS(), SNESConverged_UM_TR() 1836184914b5SBarry Smith @*/ 1837184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1838184914b5SBarry Smith { 1839184914b5SBarry Smith PetscFunctionBegin; 1840184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1841184914b5SBarry Smith *reason = snes->reason; 1842184914b5SBarry Smith PetscFunctionReturn(0); 1843184914b5SBarry Smith } 1844184914b5SBarry Smith 1845184914b5SBarry Smith #undef __FUNC__ 1846b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetConvergenceHistory" 1847c9005455SLois Curfman McInnes /*@ 1848c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1849c9005455SLois Curfman McInnes 1850fee21e36SBarry Smith Collective on SNES 1851fee21e36SBarry Smith 1852c7afd0dbSLois Curfman McInnes Input Parameters: 1853c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1854c7afd0dbSLois Curfman McInnes . a - array to hold history 1855758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1856758f92a0SBarry Smith negative if not converged) for each solve. 1857758f92a0SBarry Smith . na - size of a and its 1858758f92a0SBarry Smith - reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero, 1859758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1860c7afd0dbSLois Curfman McInnes 1861c9005455SLois Curfman McInnes Notes: 1862c9005455SLois Curfman McInnes If set, this array will contain the function norms (for 1863c9005455SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods) or gradient norms 1864c9005455SLois Curfman McInnes (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed 1865c9005455SLois Curfman McInnes at each step. 1866c9005455SLois Curfman McInnes 1867c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1868c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1869c9005455SLois Curfman McInnes during the section of code that is being timed. 1870c9005455SLois Curfman McInnes 187136851e7fSLois Curfman McInnes Level: intermediate 187236851e7fSLois Curfman McInnes 1873c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1874758f92a0SBarry Smith 187508405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1876758f92a0SBarry Smith 1877c9005455SLois Curfman McInnes @*/ 1878329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset) 1879c9005455SLois Curfman McInnes { 18803a40ed3dSBarry Smith PetscFunctionBegin; 1881c9005455SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 1882c9005455SLois Curfman McInnes if (na) PetscValidScalarPointer(a); 1883c9005455SLois Curfman McInnes snes->conv_hist = a; 1884758f92a0SBarry Smith snes->conv_hist_its = its; 1885758f92a0SBarry Smith snes->conv_hist_max = na; 1886758f92a0SBarry Smith snes->conv_hist_reset = reset; 1887758f92a0SBarry Smith PetscFunctionReturn(0); 1888758f92a0SBarry Smith } 1889758f92a0SBarry Smith 1890758f92a0SBarry Smith #undef __FUNC__ 1891b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetConvergenceHistory" 18920c4c9dddSBarry Smith /*@C 1893758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1894758f92a0SBarry Smith 1895758f92a0SBarry Smith Collective on SNES 1896758f92a0SBarry Smith 1897758f92a0SBarry Smith Input Parameter: 1898758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1899758f92a0SBarry Smith 1900758f92a0SBarry Smith Output Parameters: 1901758f92a0SBarry Smith . a - array to hold history 1902758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1903758f92a0SBarry Smith negative if not converged) for each solve. 1904758f92a0SBarry Smith - na - size of a and its 1905758f92a0SBarry Smith 1906758f92a0SBarry Smith Notes: 1907758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1908758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1909758f92a0SBarry Smith 1910758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1911758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1912758f92a0SBarry Smith during the section of code that is being timed. 1913758f92a0SBarry Smith 1914758f92a0SBarry Smith Level: intermediate 1915758f92a0SBarry Smith 1916758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1917758f92a0SBarry Smith 1918758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1919758f92a0SBarry Smith 1920758f92a0SBarry Smith @*/ 1921329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na) 1922758f92a0SBarry Smith { 1923758f92a0SBarry Smith PetscFunctionBegin; 1924758f92a0SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1925758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1926758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1927758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 19283a40ed3dSBarry Smith PetscFunctionReturn(0); 1929c9005455SLois Curfman McInnes } 1930c9005455SLois Curfman McInnes 1931c9005455SLois Curfman McInnes #undef __FUNC__ 1932b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESScaleStep_Private" 19339b94acceSBarry Smith /* 19349b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 19359b94acceSBarry Smith positive parameter delta. 19369b94acceSBarry Smith 19379b94acceSBarry Smith Input Parameters: 1938c7afd0dbSLois Curfman McInnes + snes - the SNES context 19399b94acceSBarry Smith . y - approximate solution of linear system 19409b94acceSBarry Smith . fnorm - 2-norm of current function 1941c7afd0dbSLois Curfman McInnes - delta - trust region size 19429b94acceSBarry Smith 19439b94acceSBarry Smith Output Parameters: 1944c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 19459b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 19469b94acceSBarry Smith region, and exceeds zero otherwise. 1947c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 19489b94acceSBarry Smith 19499b94acceSBarry Smith Note: 19506831982aSBarry Smith For non-trust region methods such as SNESEQLS, the parameter delta 19519b94acceSBarry Smith is set to be the maximum allowable step size. 19529b94acceSBarry Smith 19539b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 19549b94acceSBarry Smith */ 1955329f5518SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta, 1956329f5518SBarry Smith PetscReal *gpnorm,PetscReal *ynorm) 19579b94acceSBarry Smith { 1958329f5518SBarry Smith PetscReal norm; 19599b94acceSBarry Smith Scalar cnorm; 19603a40ed3dSBarry Smith int ierr; 19613a40ed3dSBarry Smith 19623a40ed3dSBarry Smith PetscFunctionBegin; 1963184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1964184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1965184914b5SBarry Smith PetscCheckSameComm(snes,y); 1966184914b5SBarry Smith 19673a40ed3dSBarry Smith ierr = VecNorm(y,NORM_2,&norm);CHKERRQ(ierr); 19689b94acceSBarry Smith if (norm > *delta) { 19699b94acceSBarry Smith norm = *delta/norm; 19709b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 19719b94acceSBarry Smith cnorm = norm; 1972329f5518SBarry Smith ierr = VecScale(&cnorm,y);CHKERRQ(ierr); 19739b94acceSBarry Smith *ynorm = *delta; 19749b94acceSBarry Smith } else { 19759b94acceSBarry Smith *gpnorm = 0.0; 19769b94acceSBarry Smith *ynorm = norm; 19779b94acceSBarry Smith } 19783a40ed3dSBarry Smith PetscFunctionReturn(0); 19799b94acceSBarry Smith } 19809b94acceSBarry Smith 19815615d1e5SSatish Balay #undef __FUNC__ 1982b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSolve" 19839b94acceSBarry Smith /*@ 19849b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 198563a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 19869b94acceSBarry Smith 1987c7afd0dbSLois Curfman McInnes Collective on SNES 1988c7afd0dbSLois Curfman McInnes 1989b2002411SLois Curfman McInnes Input Parameters: 1990c7afd0dbSLois Curfman McInnes + snes - the SNES context 1991c7afd0dbSLois Curfman McInnes - x - the solution vector 19929b94acceSBarry Smith 19939b94acceSBarry Smith Output Parameter: 1994b2002411SLois Curfman McInnes . its - number of iterations until termination 19959b94acceSBarry Smith 1996b2002411SLois Curfman McInnes Notes: 19978ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 19988ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 19998ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 20008ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 20018ddd3da0SLois Curfman McInnes 200236851e7fSLois Curfman McInnes Level: beginner 200336851e7fSLois Curfman McInnes 20049b94acceSBarry Smith .keywords: SNES, nonlinear, solve 20059b94acceSBarry Smith 200663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 20079b94acceSBarry Smith @*/ 20088ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 20099b94acceSBarry Smith { 2010f1af5d2fSBarry Smith int ierr; 2011f1af5d2fSBarry Smith PetscTruth flg; 2012052efed2SBarry Smith 20133a40ed3dSBarry Smith PetscFunctionBegin; 201477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2015184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 2016184914b5SBarry Smith PetscCheckSameComm(snes,x); 201774679c65SBarry Smith PetscValidIntPointer(its); 201874637425SBarry Smith if (!snes->solve) SETERRQ(1,1,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()"); 201974637425SBarry Smith 202082bf6240SBarry Smith if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);} 2021c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 2022758f92a0SBarry Smith if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0; 20239b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 2024c96a6f78SLois Curfman McInnes snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0; 20259b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr); 20269b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 20270f5bd95cSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr); 20286d4a8577SBarry Smith if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } 20293a40ed3dSBarry Smith PetscFunctionReturn(0); 20309b94acceSBarry Smith } 20319b94acceSBarry Smith 20329b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 20339b94acceSBarry Smith 20345615d1e5SSatish Balay #undef __FUNC__ 2035b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetType" 203682bf6240SBarry Smith /*@C 20374b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 20389b94acceSBarry Smith 2039fee21e36SBarry Smith Collective on SNES 2040fee21e36SBarry Smith 2041c7afd0dbSLois Curfman McInnes Input Parameters: 2042c7afd0dbSLois Curfman McInnes + snes - the SNES context 2043454a90a3SBarry Smith - type - a known method 2044c7afd0dbSLois Curfman McInnes 2045c7afd0dbSLois Curfman McInnes Options Database Key: 2046454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2047c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2048ae12b187SLois Curfman McInnes 20499b94acceSBarry Smith Notes: 2050e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 20516831982aSBarry Smith + SNESEQLS - Newton's method with line search 2052c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20536831982aSBarry Smith . SNESEQTR - Newton's method with trust region 2054c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20556831982aSBarry Smith . SNESUMTR - Newton's method with trust region 2056c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20576831982aSBarry Smith - SNESUMLS - Newton's method with line search 2058c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20599b94acceSBarry Smith 2060ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2061ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2062ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2063ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2064ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2065ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2066ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2067ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2068ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 206936851e7fSLois Curfman McInnes appropriate method. In other words, this routine is not for beginners. 207036851e7fSLois Curfman McInnes 207136851e7fSLois Curfman McInnes Level: intermediate 2072a703fe33SLois Curfman McInnes 2073454a90a3SBarry Smith .keywords: SNES, set, type 20749b94acceSBarry Smith @*/ 2075454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type) 20769b94acceSBarry Smith { 20770f5bd95cSBarry Smith int ierr,(*r)(SNES); 20786831982aSBarry Smith PetscTruth match; 20793a40ed3dSBarry Smith 20803a40ed3dSBarry Smith PetscFunctionBegin; 208177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 20820f5bd95cSBarry Smith PetscValidCharPointer(type); 208382bf6240SBarry Smith 20846831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 20850f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 208682bf6240SBarry Smith 208782bf6240SBarry Smith if (snes->setupcalled) { 2088e1311b90SBarry Smith ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr); 208982bf6240SBarry Smith snes->data = 0; 209082bf6240SBarry Smith } 20917d1a2b2bSBarry Smith 20929b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 209382bf6240SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 209482bf6240SBarry Smith 2095454a90a3SBarry Smith ierr = FListFind(snes->comm,SNESList,type,(int (**)(void *)) &r);CHKERRQ(ierr); 209682bf6240SBarry Smith 2097454a90a3SBarry Smith if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",type); 20981548b14aSBarry Smith 2099606d414cSSatish Balay if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);} 210082bf6240SBarry Smith snes->data = 0; 21013a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 210282bf6240SBarry Smith 2103454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 210482bf6240SBarry Smith snes->set_method_called = 1; 210582bf6240SBarry Smith 21063a40ed3dSBarry Smith PetscFunctionReturn(0); 21079b94acceSBarry Smith } 21089b94acceSBarry Smith 2109a847f771SSatish Balay 21109b94acceSBarry Smith /* --------------------------------------------------------------------- */ 21115615d1e5SSatish Balay #undef __FUNC__ 2112b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESRegisterDestroy" 21139b94acceSBarry Smith /*@C 21149b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2115f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 21169b94acceSBarry Smith 2117fee21e36SBarry Smith Not Collective 2118fee21e36SBarry Smith 211936851e7fSLois Curfman McInnes Level: advanced 212036851e7fSLois Curfman McInnes 21219b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 21229b94acceSBarry Smith 21239b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 21249b94acceSBarry Smith @*/ 2125cf256101SBarry Smith int SNESRegisterDestroy(void) 21269b94acceSBarry Smith { 212782bf6240SBarry Smith int ierr; 212882bf6240SBarry Smith 21293a40ed3dSBarry Smith PetscFunctionBegin; 213082bf6240SBarry Smith if (SNESList) { 2131*1d1367b7SBarry Smith ierr = FListDestroy(&SNESList);CHKERRQ(ierr); 213282bf6240SBarry Smith SNESList = 0; 21339b94acceSBarry Smith } 21344c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 21353a40ed3dSBarry Smith PetscFunctionReturn(0); 21369b94acceSBarry Smith } 21379b94acceSBarry Smith 21385615d1e5SSatish Balay #undef __FUNC__ 2139b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetType" 21409b94acceSBarry Smith /*@C 21419a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 21429b94acceSBarry Smith 2143c7afd0dbSLois Curfman McInnes Not Collective 2144c7afd0dbSLois Curfman McInnes 21459b94acceSBarry Smith Input Parameter: 21464b0e389bSBarry Smith . snes - nonlinear solver context 21479b94acceSBarry Smith 21489b94acceSBarry Smith Output Parameter: 2149454a90a3SBarry Smith . type - SNES method (a charactor string) 21509b94acceSBarry Smith 215136851e7fSLois Curfman McInnes Level: intermediate 215236851e7fSLois Curfman McInnes 2153454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 21549b94acceSBarry Smith @*/ 2155454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type) 21569b94acceSBarry Smith { 21573a40ed3dSBarry Smith PetscFunctionBegin; 2158184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2159454a90a3SBarry Smith *type = snes->type_name; 21603a40ed3dSBarry Smith PetscFunctionReturn(0); 21619b94acceSBarry Smith } 21629b94acceSBarry Smith 21635615d1e5SSatish Balay #undef __FUNC__ 2164b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetSolution" 21659b94acceSBarry Smith /*@C 21669b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 21679b94acceSBarry Smith stored. 21689b94acceSBarry Smith 2169c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2170c7afd0dbSLois Curfman McInnes 21719b94acceSBarry Smith Input Parameter: 21729b94acceSBarry Smith . snes - the SNES context 21739b94acceSBarry Smith 21749b94acceSBarry Smith Output Parameter: 21759b94acceSBarry Smith . x - the solution 21769b94acceSBarry Smith 217736851e7fSLois Curfman McInnes Level: advanced 217836851e7fSLois Curfman McInnes 21799b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 21809b94acceSBarry Smith 2181a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 21829b94acceSBarry Smith @*/ 21839b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 21849b94acceSBarry Smith { 21853a40ed3dSBarry Smith PetscFunctionBegin; 218677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 21879b94acceSBarry Smith *x = snes->vec_sol_always; 21883a40ed3dSBarry Smith PetscFunctionReturn(0); 21899b94acceSBarry Smith } 21909b94acceSBarry Smith 21915615d1e5SSatish Balay #undef __FUNC__ 2192b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetSolutionUpdate" 21939b94acceSBarry Smith /*@C 21949b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 21959b94acceSBarry Smith stored. 21969b94acceSBarry Smith 2197c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2198c7afd0dbSLois Curfman McInnes 21999b94acceSBarry Smith Input Parameter: 22009b94acceSBarry Smith . snes - the SNES context 22019b94acceSBarry Smith 22029b94acceSBarry Smith Output Parameter: 22039b94acceSBarry Smith . x - the solution update 22049b94acceSBarry Smith 220536851e7fSLois Curfman McInnes Level: advanced 220636851e7fSLois Curfman McInnes 22079b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 22089b94acceSBarry Smith 22099b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 22109b94acceSBarry Smith @*/ 22119b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 22129b94acceSBarry Smith { 22133a40ed3dSBarry Smith PetscFunctionBegin; 221477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22159b94acceSBarry Smith *x = snes->vec_sol_update_always; 22163a40ed3dSBarry Smith PetscFunctionReturn(0); 22179b94acceSBarry Smith } 22189b94acceSBarry Smith 22195615d1e5SSatish Balay #undef __FUNC__ 2220b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetFunction" 22219b94acceSBarry Smith /*@C 22223638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 22239b94acceSBarry Smith 2224c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2225c7afd0dbSLois Curfman McInnes 22269b94acceSBarry Smith Input Parameter: 22279b94acceSBarry Smith . snes - the SNES context 22289b94acceSBarry Smith 22299b94acceSBarry Smith Output Parameter: 22307bf4e008SBarry Smith + r - the function (or PETSC_NULL) 223100036973SBarry Smith . ctx - the function context (or PETSC_NULL) 223200036973SBarry Smith - func - the function (or PETSC_NULL) 22339b94acceSBarry Smith 22349b94acceSBarry Smith Notes: 22359b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 22369b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 22379b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 22389b94acceSBarry Smith 223936851e7fSLois Curfman McInnes Level: advanced 224036851e7fSLois Curfman McInnes 2241a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 22429b94acceSBarry Smith 224331615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 224431615d04SLois Curfman McInnes SNESGetGradient() 22457bf4e008SBarry Smith 22469b94acceSBarry Smith @*/ 224700036973SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx,int (**func)(SNES,Vec,Vec,void*)) 22489b94acceSBarry Smith { 22493a40ed3dSBarry Smith PetscFunctionBegin; 225077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2251a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 2252a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 2253a8c6a408SBarry Smith } 22547bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22557bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 225600036973SBarry Smith if (func) *func = snes->computefunction; 22573a40ed3dSBarry Smith PetscFunctionReturn(0); 22589b94acceSBarry Smith } 22599b94acceSBarry Smith 22605615d1e5SSatish Balay #undef __FUNC__ 2261b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetGradient" 22629b94acceSBarry Smith /*@C 22633638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 22649b94acceSBarry Smith 2265c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2266c7afd0dbSLois Curfman McInnes 22679b94acceSBarry Smith Input Parameter: 22689b94acceSBarry Smith . snes - the SNES context 22699b94acceSBarry Smith 22709b94acceSBarry Smith Output Parameter: 22717bf4e008SBarry Smith + r - the gradient (or PETSC_NULL) 22727bf4e008SBarry Smith - ctx - the gradient context (or PETSC_NULL) 22739b94acceSBarry Smith 22749b94acceSBarry Smith Notes: 22759b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 22769b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 22779b94acceSBarry Smith SNESGetFunction(). 22789b94acceSBarry Smith 227936851e7fSLois Curfman McInnes Level: advanced 228036851e7fSLois Curfman McInnes 22819b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 22829b94acceSBarry Smith 22837bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(), 22847bf4e008SBarry Smith SNESSetGradient(), SNESSetFunction() 22857bf4e008SBarry Smith 22869b94acceSBarry Smith @*/ 22877bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx) 22889b94acceSBarry Smith { 22893a40ed3dSBarry Smith PetscFunctionBegin; 229077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22913a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2292a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 22933a40ed3dSBarry Smith } 22947bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22957bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22963a40ed3dSBarry Smith PetscFunctionReturn(0); 22979b94acceSBarry Smith } 22989b94acceSBarry Smith 22995615d1e5SSatish Balay #undef __FUNC__ 2300b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetMinimizationFunction" 23017bf4e008SBarry Smith /*@C 23029b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 23039b94acceSBarry Smith unconstrained minimization problems. 23049b94acceSBarry Smith 2305c7afd0dbSLois Curfman McInnes Not Collective 2306c7afd0dbSLois Curfman McInnes 23079b94acceSBarry Smith Input Parameter: 23089b94acceSBarry Smith . snes - the SNES context 23099b94acceSBarry Smith 23109b94acceSBarry Smith Output Parameter: 23117bf4e008SBarry Smith + r - the function (or PETSC_NULL) 23127bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 23139b94acceSBarry Smith 23149b94acceSBarry Smith Notes: 23159b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 23169b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 23179b94acceSBarry Smith SNESGetFunction(). 23189b94acceSBarry Smith 231936851e7fSLois Curfman McInnes Level: advanced 232036851e7fSLois Curfman McInnes 23219b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 23229b94acceSBarry Smith 23237bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction() 23247bf4e008SBarry Smith 23259b94acceSBarry Smith @*/ 2326329f5518SBarry Smith int SNESGetMinimizationFunction(SNES snes,PetscReal *r,void **ctx) 23279b94acceSBarry Smith { 23283a40ed3dSBarry Smith PetscFunctionBegin; 232977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 233074679c65SBarry Smith PetscValidScalarPointer(r); 23313a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2332a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 23333a40ed3dSBarry Smith } 23347bf4e008SBarry Smith if (r) *r = snes->fc; 23357bf4e008SBarry Smith if (ctx) *ctx = snes->umfunP; 23363a40ed3dSBarry Smith PetscFunctionReturn(0); 23379b94acceSBarry Smith } 23389b94acceSBarry Smith 23395615d1e5SSatish Balay #undef __FUNC__ 2340b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetOptionsPrefix" 23413c7409f5SSatish Balay /*@C 23423c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2343d850072dSLois Curfman McInnes SNES options in the database. 23443c7409f5SSatish Balay 2345fee21e36SBarry Smith Collective on SNES 2346fee21e36SBarry Smith 2347c7afd0dbSLois Curfman McInnes Input Parameter: 2348c7afd0dbSLois Curfman McInnes + snes - the SNES context 2349c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2350c7afd0dbSLois Curfman McInnes 2351d850072dSLois Curfman McInnes Notes: 2352a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2353c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2354d850072dSLois Curfman McInnes 235536851e7fSLois Curfman McInnes Level: advanced 235636851e7fSLois Curfman McInnes 23573c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2358a86d99e1SLois Curfman McInnes 2359a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 23603c7409f5SSatish Balay @*/ 23613c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 23623c7409f5SSatish Balay { 23633c7409f5SSatish Balay int ierr; 23643c7409f5SSatish Balay 23653a40ed3dSBarry Smith PetscFunctionBegin; 236677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2367639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 23683c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 23693a40ed3dSBarry Smith PetscFunctionReturn(0); 23703c7409f5SSatish Balay } 23713c7409f5SSatish Balay 23725615d1e5SSatish Balay #undef __FUNC__ 2373b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESAppendOptionsPrefix" 23743c7409f5SSatish Balay /*@C 2375f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2376d850072dSLois Curfman McInnes SNES options in the database. 23773c7409f5SSatish Balay 2378fee21e36SBarry Smith Collective on SNES 2379fee21e36SBarry Smith 2380c7afd0dbSLois Curfman McInnes Input Parameters: 2381c7afd0dbSLois Curfman McInnes + snes - the SNES context 2382c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2383c7afd0dbSLois Curfman McInnes 2384d850072dSLois Curfman McInnes Notes: 2385a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2386c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2387d850072dSLois Curfman McInnes 238836851e7fSLois Curfman McInnes Level: advanced 238936851e7fSLois Curfman McInnes 23903c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2391a86d99e1SLois Curfman McInnes 2392a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 23933c7409f5SSatish Balay @*/ 23943c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 23953c7409f5SSatish Balay { 23963c7409f5SSatish Balay int ierr; 23973c7409f5SSatish Balay 23983a40ed3dSBarry Smith PetscFunctionBegin; 239977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2400639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 24013c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 24023a40ed3dSBarry Smith PetscFunctionReturn(0); 24033c7409f5SSatish Balay } 24043c7409f5SSatish Balay 24055615d1e5SSatish Balay #undef __FUNC__ 2406b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetOptionsPrefix" 24079ab63eb5SSatish Balay /*@C 24083c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 24093c7409f5SSatish Balay SNES options in the database. 24103c7409f5SSatish Balay 2411c7afd0dbSLois Curfman McInnes Not Collective 2412c7afd0dbSLois Curfman McInnes 24133c7409f5SSatish Balay Input Parameter: 24143c7409f5SSatish Balay . snes - the SNES context 24153c7409f5SSatish Balay 24163c7409f5SSatish Balay Output Parameter: 24173c7409f5SSatish Balay . prefix - pointer to the prefix string used 24183c7409f5SSatish Balay 24199ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 24209ab63eb5SSatish Balay sufficient length to hold the prefix. 24219ab63eb5SSatish Balay 242236851e7fSLois Curfman McInnes Level: advanced 242336851e7fSLois Curfman McInnes 24243c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2425a86d99e1SLois Curfman McInnes 2426a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 24273c7409f5SSatish Balay @*/ 24283c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 24293c7409f5SSatish Balay { 24303c7409f5SSatish Balay int ierr; 24313c7409f5SSatish Balay 24323a40ed3dSBarry Smith PetscFunctionBegin; 243377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2434639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 24353a40ed3dSBarry Smith PetscFunctionReturn(0); 24363c7409f5SSatish Balay } 24373c7409f5SSatish Balay 2438ca161407SBarry Smith #undef __FUNC__ 2439b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESPrintHelp" 2440ca161407SBarry Smith /*@ 2441ca161407SBarry Smith SNESPrintHelp - Prints all options for the SNES component. 2442ca161407SBarry Smith 2443c7afd0dbSLois Curfman McInnes Collective on SNES 2444c7afd0dbSLois Curfman McInnes 2445ca161407SBarry Smith Input Parameter: 2446ca161407SBarry Smith . snes - the SNES context 2447ca161407SBarry Smith 2448ca161407SBarry Smith Options Database Keys: 2449c7afd0dbSLois Curfman McInnes + -help - Prints SNES options 2450c7afd0dbSLois Curfman McInnes - -h - Prints SNES options 2451ca161407SBarry Smith 245236851e7fSLois Curfman McInnes Level: beginner 245336851e7fSLois Curfman McInnes 2454ca161407SBarry Smith .keywords: SNES, nonlinear, help 2455ca161407SBarry Smith 2456ca161407SBarry Smith .seealso: SNESSetFromOptions() 2457ca161407SBarry Smith @*/ 2458ca161407SBarry Smith int SNESPrintHelp(SNES snes) 2459ca161407SBarry Smith { 2460ca161407SBarry Smith char p[64]; 2461ca161407SBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2462ca161407SBarry Smith int ierr; 2463ca161407SBarry Smith 2464ca161407SBarry Smith PetscFunctionBegin; 2465ca161407SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2466ca161407SBarry Smith 2467549d3d68SSatish Balay ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 2468ca161407SBarry Smith if (snes->prefix) PetscStrcat(p,snes->prefix); 2469ca161407SBarry Smith 2470ca161407SBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2471ca161407SBarry Smith 2472ebb8b11fSBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2473d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr); 2474488ecbafSBarry Smith ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr); 2475d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr); 2476d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr); 2477d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr); 2478d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr); 2479d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr); 2480d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr); 2481d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr); 2482d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr); 2483d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr); 2484d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_monitor: use default SNES convergence monitor, prints\n\ 2485d132466eSBarry Smith residual norm at each iteration.\n",p);CHKERRQ(ierr); 2486d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_smonitor: same as the above, but prints fewer digits of the\n\ 2487ca161407SBarry Smith residual norm for small residual norms. This is useful to conceal\n\ 2488d132466eSBarry Smith meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr); 2489d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr); 2490d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr); 2491d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr); 2492ca161407SBarry Smith if (snes->type == SNES_NONLINEAR_EQUATIONS) { 2493d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2494d132466eSBarry Smith " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr); 2495d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr); 2496d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr); 2497d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr); 2498d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf_ksp_monitor - if using matrix-free multiply then prints h at each KSP iteration\n",p);CHKERRQ(ierr); 2499d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr); 2500d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr); 2501d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2502d132466eSBarry Smith " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr); 2503d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2504d132466eSBarry Smith " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr); 2505d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2506d132466eSBarry Smith " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr); 2507d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2508d132466eSBarry Smith " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr); 2509d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2510d132466eSBarry Smith " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr); 2511d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2512d132466eSBarry Smith " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr); 2513d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2514d132466eSBarry Smith " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr); 2515ca161407SBarry Smith } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 2516d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr); 2517d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr); 2518d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr); 2519d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr); 2520ca161407SBarry Smith } 2521454a90a3SBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <type> for help on ",p);CHKERRQ(ierr); 2522d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr); 2523ca161407SBarry Smith if (snes->printhelp) { 2524ca161407SBarry Smith ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr); 2525ca161407SBarry Smith } 2526ca161407SBarry Smith PetscFunctionReturn(0); 2527ca161407SBarry Smith } 25283c7409f5SSatish Balay 2529acb85ae6SSatish Balay /*MC 2530f1af5d2fSBarry Smith SNESRegisterDynamic - Adds a method to the nonlinear solver package. 25319b94acceSBarry Smith 2532b2002411SLois Curfman McInnes Synopsis: 2533f1af5d2fSBarry Smith SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES)) 25349b94acceSBarry Smith 25358d76a1e5SLois Curfman McInnes Not collective 25368d76a1e5SLois Curfman McInnes 2537b2002411SLois Curfman McInnes Input Parameters: 2538c7afd0dbSLois Curfman McInnes + name_solver - name of a new user-defined solver 2539b2002411SLois Curfman McInnes . path - path (either absolute or relative) the library containing this solver 2540b2002411SLois Curfman McInnes . name_create - name of routine to create method context 2541c7afd0dbSLois Curfman McInnes - routine_create - routine to create method context 25429b94acceSBarry Smith 2543b2002411SLois Curfman McInnes Notes: 2544f1af5d2fSBarry Smith SNESRegisterDynamic() may be called multiple times to add several user-defined solvers. 25453a40ed3dSBarry Smith 2546b2002411SLois Curfman McInnes If dynamic libraries are used, then the fourth input argument (routine_create) 2547b2002411SLois Curfman McInnes is ignored. 2548b2002411SLois Curfman McInnes 25495ba88a07SLois Curfman McInnes Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LDIR}, ${BOPT}, 25505ba88a07SLois Curfman McInnes and others of the form ${any_environmental_variable} occuring in pathname will be 25515ba88a07SLois Curfman McInnes replaced with appropriate values. 25525ba88a07SLois Curfman McInnes 2553b2002411SLois Curfman McInnes Sample usage: 255469225d78SLois Curfman McInnes .vb 2555f1af5d2fSBarry Smith SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a, 2556b2002411SLois Curfman McInnes "MySolverCreate",MySolverCreate); 255769225d78SLois Curfman McInnes .ve 2558b2002411SLois Curfman McInnes 2559b2002411SLois Curfman McInnes Then, your solver can be chosen with the procedural interface via 2560b2002411SLois Curfman McInnes $ SNESSetType(snes,"my_solver") 2561b2002411SLois Curfman McInnes or at runtime via the option 2562b2002411SLois Curfman McInnes $ -snes_type my_solver 2563b2002411SLois Curfman McInnes 256436851e7fSLois Curfman McInnes Level: advanced 256536851e7fSLois Curfman McInnes 2566b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register 2567b2002411SLois Curfman McInnes 2568b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy() 2569b2002411SLois Curfman McInnes M*/ 2570b2002411SLois Curfman McInnes 2571b2002411SLois Curfman McInnes #undef __FUNC__ 2572b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESRegister" 2573f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES)) 2574b2002411SLois Curfman McInnes { 2575b2002411SLois Curfman McInnes char fullname[256]; 2576b2002411SLois Curfman McInnes int ierr; 2577b2002411SLois Curfman McInnes 2578b2002411SLois Curfman McInnes PetscFunctionBegin; 2579f1af5d2fSBarry Smith ierr = FListConcat(path,name,fullname);CHKERRQ(ierr); 2580f1af5d2fSBarry Smith ierr = FListAdd(&SNESList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr); 2581b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2582b2002411SLois Curfman McInnes } 2583