1*e090d566SSatish Balay /*$Id: snes.c,v 1.213 2000/04/30 22:17:08 bsmith Exp balay $*/ 29b94acceSBarry Smith 3*e090d566SSatish 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; 716e24b481bSBarry Smith PetscPublishAll(snes); 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); 1192184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1193184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1194184914b5SBarry Smith PetscCheckSameComm(snes,A); 1195184914b5SBarry Smith 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) 1221c7afd0dbSLois Curfman McInnes - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1222fee21e36SBarry Smith 122336851e7fSLois Curfman McInnes Level: advanced 122436851e7fSLois Curfman McInnes 1225b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1226b4fd4287SBarry Smith @*/ 1227b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx) 1228b4fd4287SBarry Smith { 12293a40ed3dSBarry Smith PetscFunctionBegin; 1230184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 12313a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1232a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 12333a40ed3dSBarry Smith } 1234b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1235b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1236b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 12373a40ed3dSBarry Smith PetscFunctionReturn(0); 1238b4fd4287SBarry Smith } 1239b4fd4287SBarry Smith 12405615d1e5SSatish Balay #undef __FUNC__ 1241b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetHessian" 12429b94acceSBarry Smith /*@C 12439b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 1244044dda88SLois Curfman McInnes location to store the matrix. 12459b94acceSBarry Smith 1246c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1247c7afd0dbSLois Curfman McInnes 12489b94acceSBarry Smith Input Parameters: 1249c7afd0dbSLois Curfman McInnes + snes - the SNES context 12509b94acceSBarry Smith . A - Hessian matrix 12519b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 12529b94acceSBarry Smith . func - Jacobian evaluation routine 1253c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1254313e4042SLois Curfman McInnes Hessian evaluation routine (may be PETSC_NULL) 12559b94acceSBarry Smith 12569b94acceSBarry Smith Calling sequence of func: 12578d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 12589b94acceSBarry Smith 1259c7afd0dbSLois Curfman McInnes + x - input vector 12609b94acceSBarry Smith . A - Hessian matrix 12619b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1262ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1263ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1264c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Hessian context 12659b94acceSBarry Smith 12669b94acceSBarry Smith Notes: 1267dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 12682cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1269ac21db08SLois Curfman McInnes 12709b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 12719b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 12729b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 12739b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 12749b94acceSBarry Smith throughout the global iterations. 12759b94acceSBarry Smith 127636851e7fSLois Curfman McInnes Level: beginner 127736851e7fSLois Curfman McInnes 12789b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 12799b94acceSBarry Smith 1280ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 12819b94acceSBarry Smith @*/ 1282454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 12839b94acceSBarry Smith { 12843a40ed3dSBarry Smith PetscFunctionBegin; 128577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1286184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1287184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1288184914b5SBarry Smith PetscCheckSameComm(snes,A); 1289184914b5SBarry Smith PetscCheckSameComm(snes,B); 1290d4bb536fSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1291a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1292d4bb536fSBarry Smith } 12939b94acceSBarry Smith snes->computejacobian = func; 12949b94acceSBarry Smith snes->jacP = ctx; 12959b94acceSBarry Smith snes->jacobian = A; 12969b94acceSBarry Smith snes->jacobian_pre = B; 12973a40ed3dSBarry Smith PetscFunctionReturn(0); 12989b94acceSBarry Smith } 12999b94acceSBarry Smith 13005615d1e5SSatish Balay #undef __FUNC__ 1301b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetHessian" 130262fef451SLois Curfman McInnes /*@ 130362fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 130462fef451SLois Curfman McInnes provided context for evaluating the Hessian. 130562fef451SLois Curfman McInnes 1306c7afd0dbSLois Curfman McInnes Not Collective, but Mat object is parallel if SNES object is parallel 1307c7afd0dbSLois Curfman McInnes 130862fef451SLois Curfman McInnes Input Parameter: 130962fef451SLois Curfman McInnes . snes - the nonlinear solver context 131062fef451SLois Curfman McInnes 131162fef451SLois Curfman McInnes Output Parameters: 1312c7afd0dbSLois Curfman McInnes + A - location to stash Hessian matrix (or PETSC_NULL) 131362fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 1314c7afd0dbSLois Curfman McInnes - ctx - location to stash Hessian ctx (or PETSC_NULL) 1315fee21e36SBarry Smith 131636851e7fSLois Curfman McInnes Level: advanced 131736851e7fSLois Curfman McInnes 131862fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 1319c7afd0dbSLois Curfman McInnes 1320c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian 132162fef451SLois Curfman McInnes @*/ 132262fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B,void **ctx) 132362fef451SLois Curfman McInnes { 13243a40ed3dSBarry Smith PetscFunctionBegin; 1325184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 13263a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){ 1327a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 13283a40ed3dSBarry Smith } 132962fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 133062fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 133162fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 13323a40ed3dSBarry Smith PetscFunctionReturn(0); 133362fef451SLois Curfman McInnes } 133462fef451SLois Curfman McInnes 13359b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 13369b94acceSBarry Smith 13375615d1e5SSatish Balay #undef __FUNC__ 1338b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetUp" 13399b94acceSBarry Smith /*@ 13409b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1341272ac6f2SLois Curfman McInnes of a nonlinear solver. 13429b94acceSBarry Smith 1343fee21e36SBarry Smith Collective on SNES 1344fee21e36SBarry Smith 1345c7afd0dbSLois Curfman McInnes Input Parameters: 1346c7afd0dbSLois Curfman McInnes + snes - the SNES context 1347c7afd0dbSLois Curfman McInnes - x - the solution vector 1348c7afd0dbSLois Curfman McInnes 1349272ac6f2SLois Curfman McInnes Notes: 1350272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1351272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1352272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1353272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1354272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1355272ac6f2SLois Curfman McInnes 135636851e7fSLois Curfman McInnes Level: advanced 135736851e7fSLois Curfman McInnes 13589b94acceSBarry Smith .keywords: SNES, nonlinear, setup 13599b94acceSBarry Smith 13609b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 13619b94acceSBarry Smith @*/ 13628ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 13639b94acceSBarry Smith { 1364f1af5d2fSBarry Smith int ierr; 1365f1af5d2fSBarry Smith PetscTruth flg; 13663a40ed3dSBarry Smith 13673a40ed3dSBarry Smith PetscFunctionBegin; 136877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 136977c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1370184914b5SBarry Smith PetscCheckSameComm(snes,x); 13718ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 13729b94acceSBarry Smith 1373c1f60f51SBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1374c1f60f51SBarry Smith /* 1375c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1376dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1377c1f60f51SBarry Smith */ 1378c1f60f51SBarry Smith if (flg) { 1379c1f60f51SBarry Smith Mat J; 13805a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1381c1f60f51SBarry Smith PLogObjectParent(snes,J); 1382c1f60f51SBarry Smith snes->mfshell = J; 1383c1f60f51SBarry Smith snes->jacobian = J; 1384c2aafc4cSSatish Balay if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1385c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n"); 1386d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1387c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n"); 1388d4bb536fSBarry Smith } else { 1389a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option"); 1390d4bb536fSBarry Smith } 13915a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1392c1f60f51SBarry Smith } 1393272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1394c1f60f51SBarry Smith /* 1395dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1396c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1397c1f60f51SBarry Smith */ 139831615d04SLois Curfman McInnes if (flg) { 1399272ac6f2SLois Curfman McInnes Mat J; 14005a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1401272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 1402272ac6f2SLois Curfman McInnes snes->mfshell = J; 140383e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 140483e56afdSLois Curfman McInnes ierr = SNESSetJacobian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 140594a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 1406d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 140783e56afdSLois Curfman McInnes ierr = SNESSetHessian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 140894a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 1409d4bb536fSBarry Smith } else { 1410a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option"); 1411d4bb536fSBarry Smith } 14125a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1413272ac6f2SLois Curfman McInnes } 14149b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 14156831982aSBarry Smith PetscTruth iseqtr; 14166831982aSBarry Smith 1417a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 1418a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 14195a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 1420a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 1421a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector"); 1422a8c6a408SBarry Smith } 1423a703fe33SLois Curfman McInnes 1424a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 14256831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr); 14266831982aSBarry Smith if (snes->ksp_ewconv && !iseqtr) { 1427a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 1428a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 1429a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 14305a655dc6SBarry Smith ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr); 1431a703fe33SLois Curfman McInnes } 14329b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 1433a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1434a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1435a8c6a408SBarry Smith if (!snes->computeumfunction) { 1436a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first"); 1437a8c6a408SBarry Smith } 14385a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()"); 1439d4bb536fSBarry Smith } else { 1440a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class"); 1441d4bb536fSBarry Smith } 1442a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);} 144382bf6240SBarry Smith snes->setupcalled = 1; 14443a40ed3dSBarry Smith PetscFunctionReturn(0); 14459b94acceSBarry Smith } 14469b94acceSBarry Smith 14475615d1e5SSatish Balay #undef __FUNC__ 1448b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESDestroy" 14499b94acceSBarry Smith /*@C 14509b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 14519b94acceSBarry Smith with SNESCreate(). 14529b94acceSBarry Smith 1453c7afd0dbSLois Curfman McInnes Collective on SNES 1454c7afd0dbSLois Curfman McInnes 14559b94acceSBarry Smith Input Parameter: 14569b94acceSBarry Smith . snes - the SNES context 14579b94acceSBarry Smith 145836851e7fSLois Curfman McInnes Level: beginner 145936851e7fSLois Curfman McInnes 14609b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 14619b94acceSBarry Smith 146263a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 14639b94acceSBarry Smith @*/ 14649b94acceSBarry Smith int SNESDestroy(SNES snes) 14659b94acceSBarry Smith { 1466b8a78c4aSBarry Smith int i,ierr; 14673a40ed3dSBarry Smith 14683a40ed3dSBarry Smith PetscFunctionBegin; 146977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14703a40ed3dSBarry Smith if (--snes->refct > 0) PetscFunctionReturn(0); 1471d4bb536fSBarry Smith 1472be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 14730f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1474be0abb6dSBarry Smith 1475e1311b90SBarry Smith if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);} 1476606d414cSSatish Balay if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);} 1477522c5e43SBarry Smith if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);} 14789b94acceSBarry Smith ierr = SLESDestroy(snes->sles);CHKERRQ(ierr); 1479522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1480b8a78c4aSBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1481b8a78c4aSBarry Smith if (snes->monitordestroy[i]) { 1482b8a78c4aSBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1483b8a78c4aSBarry Smith } 1484b8a78c4aSBarry Smith } 14859b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 14860452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 14873a40ed3dSBarry Smith PetscFunctionReturn(0); 14889b94acceSBarry Smith } 14899b94acceSBarry Smith 14909b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 14919b94acceSBarry Smith 14925615d1e5SSatish Balay #undef __FUNC__ 1493b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetTolerances" 14949b94acceSBarry Smith /*@ 1495d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 14969b94acceSBarry Smith 1497c7afd0dbSLois Curfman McInnes Collective on SNES 1498c7afd0dbSLois Curfman McInnes 14999b94acceSBarry Smith Input Parameters: 1500c7afd0dbSLois Curfman McInnes + snes - the SNES context 150133174efeSLois Curfman McInnes . atol - absolute convergence tolerance 150233174efeSLois Curfman McInnes . rtol - relative convergence tolerance 150333174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 150433174efeSLois Curfman McInnes of the change in the solution between steps 150533174efeSLois Curfman McInnes . maxit - maximum number of iterations 1506c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1507fee21e36SBarry Smith 150833174efeSLois Curfman McInnes Options Database Keys: 1509c7afd0dbSLois Curfman McInnes + -snes_atol <atol> - Sets atol 1510c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1511c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1512c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1513c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15149b94acceSBarry Smith 1515d7a720efSLois Curfman McInnes Notes: 15169b94acceSBarry Smith The default maximum number of iterations is 50. 15179b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15189b94acceSBarry Smith 151936851e7fSLois Curfman McInnes Level: intermediate 152036851e7fSLois Curfman McInnes 152133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15229b94acceSBarry Smith 1523d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 15249b94acceSBarry Smith @*/ 1525329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf) 15269b94acceSBarry Smith { 15273a40ed3dSBarry Smith PetscFunctionBegin; 152877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1529d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1530d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1531d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1532d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1533d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15343a40ed3dSBarry Smith PetscFunctionReturn(0); 15359b94acceSBarry Smith } 15369b94acceSBarry Smith 15375615d1e5SSatish Balay #undef __FUNC__ 1538b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetTolerances" 15399b94acceSBarry Smith /*@ 154033174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 154133174efeSLois Curfman McInnes 1542c7afd0dbSLois Curfman McInnes Not Collective 1543c7afd0dbSLois Curfman McInnes 154433174efeSLois Curfman McInnes Input Parameters: 1545c7afd0dbSLois Curfman McInnes + snes - the SNES context 154633174efeSLois Curfman McInnes . atol - absolute convergence tolerance 154733174efeSLois Curfman McInnes . rtol - relative convergence tolerance 154833174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 154933174efeSLois Curfman McInnes of the change in the solution between steps 155033174efeSLois Curfman McInnes . maxit - maximum number of iterations 1551c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1552fee21e36SBarry Smith 155333174efeSLois Curfman McInnes Notes: 155433174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 155533174efeSLois Curfman McInnes 155636851e7fSLois Curfman McInnes Level: intermediate 155736851e7fSLois Curfman McInnes 155833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 155933174efeSLois Curfman McInnes 156033174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 156133174efeSLois Curfman McInnes @*/ 1562329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf) 156333174efeSLois Curfman McInnes { 15643a40ed3dSBarry Smith PetscFunctionBegin; 156533174efeSLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 156633174efeSLois Curfman McInnes if (atol) *atol = snes->atol; 156733174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 156833174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 156933174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 157033174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 15713a40ed3dSBarry Smith PetscFunctionReturn(0); 157233174efeSLois Curfman McInnes } 157333174efeSLois Curfman McInnes 15745615d1e5SSatish Balay #undef __FUNC__ 1575b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetTrustRegionTolerance" 157633174efeSLois Curfman McInnes /*@ 15779b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 15789b94acceSBarry Smith 1579fee21e36SBarry Smith Collective on SNES 1580fee21e36SBarry Smith 1581c7afd0dbSLois Curfman McInnes Input Parameters: 1582c7afd0dbSLois Curfman McInnes + snes - the SNES context 1583c7afd0dbSLois Curfman McInnes - tol - tolerance 1584c7afd0dbSLois Curfman McInnes 15859b94acceSBarry Smith Options Database Key: 1586c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 15879b94acceSBarry Smith 158836851e7fSLois Curfman McInnes Level: intermediate 158936851e7fSLois Curfman McInnes 15909b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 15919b94acceSBarry Smith 1592d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 15939b94acceSBarry Smith @*/ 1594329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 15959b94acceSBarry Smith { 15963a40ed3dSBarry Smith PetscFunctionBegin; 159777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15989b94acceSBarry Smith snes->deltatol = tol; 15993a40ed3dSBarry Smith PetscFunctionReturn(0); 16009b94acceSBarry Smith } 16019b94acceSBarry Smith 16025615d1e5SSatish Balay #undef __FUNC__ 1603b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetMinimizationFunctionTolerance" 16049b94acceSBarry Smith /*@ 160577c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 16069b94acceSBarry Smith for unconstrained minimization solvers. 16079b94acceSBarry Smith 1608fee21e36SBarry Smith Collective on SNES 1609fee21e36SBarry Smith 1610c7afd0dbSLois Curfman McInnes Input Parameters: 1611c7afd0dbSLois Curfman McInnes + snes - the SNES context 1612c7afd0dbSLois Curfman McInnes - ftol - minimum function tolerance 1613c7afd0dbSLois Curfman McInnes 16149b94acceSBarry Smith Options Database Key: 1615c7afd0dbSLois Curfman McInnes . -snes_fmin <ftol> - Sets ftol 16169b94acceSBarry Smith 16179b94acceSBarry Smith Note: 161877c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 16199b94acceSBarry Smith methods only. 16209b94acceSBarry Smith 162136851e7fSLois Curfman McInnes Level: intermediate 162236851e7fSLois Curfman McInnes 16239b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 16249b94acceSBarry Smith 1625d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 16269b94acceSBarry Smith @*/ 1627329f5518SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,PetscReal ftol) 16289b94acceSBarry Smith { 16293a40ed3dSBarry Smith PetscFunctionBegin; 163077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16319b94acceSBarry Smith snes->fmin = ftol; 16323a40ed3dSBarry Smith PetscFunctionReturn(0); 16339b94acceSBarry Smith } 1634df9fa365SBarry Smith /* 1635df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1636df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1637df9fa365SBarry Smith macros instead of functions 1638df9fa365SBarry Smith */ 1639ce1608b8SBarry Smith #undef __FUNC__ 1640b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESLGMonitor" 1641329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx) 1642ce1608b8SBarry Smith { 1643ce1608b8SBarry Smith int ierr; 1644ce1608b8SBarry Smith 1645ce1608b8SBarry Smith PetscFunctionBegin; 1646184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1647ce1608b8SBarry Smith ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1648ce1608b8SBarry Smith PetscFunctionReturn(0); 1649ce1608b8SBarry Smith } 1650ce1608b8SBarry Smith 1651df9fa365SBarry Smith #undef __FUNC__ 1652b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESLGMonitorCreate" 1653df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,DrawLG *draw) 1654df9fa365SBarry Smith { 1655df9fa365SBarry Smith int ierr; 1656df9fa365SBarry Smith 1657df9fa365SBarry Smith PetscFunctionBegin; 1658df9fa365SBarry Smith ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1659df9fa365SBarry Smith PetscFunctionReturn(0); 1660df9fa365SBarry Smith } 1661df9fa365SBarry Smith 1662df9fa365SBarry Smith #undef __FUNC__ 1663b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESLGMonitorDestroy" 1664df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw) 1665df9fa365SBarry Smith { 1666df9fa365SBarry Smith int ierr; 1667df9fa365SBarry Smith 1668df9fa365SBarry Smith PetscFunctionBegin; 1669df9fa365SBarry Smith ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr); 1670df9fa365SBarry Smith PetscFunctionReturn(0); 1671df9fa365SBarry Smith } 1672df9fa365SBarry Smith 16739b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 16749b94acceSBarry Smith 16755615d1e5SSatish Balay #undef __FUNC__ 1676b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetMonitor" 16779b94acceSBarry Smith /*@C 16785cd90555SBarry Smith SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every 16799b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 16809b94acceSBarry Smith progress. 16819b94acceSBarry Smith 1682fee21e36SBarry Smith Collective on SNES 1683fee21e36SBarry Smith 1684c7afd0dbSLois Curfman McInnes Input Parameters: 1685c7afd0dbSLois Curfman McInnes + snes - the SNES context 1686c7afd0dbSLois Curfman McInnes . func - monitoring routine 1687b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1688b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desitre) 1689b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1690b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 16919b94acceSBarry Smith 1692c7afd0dbSLois Curfman McInnes Calling sequence of func: 1693329f5518SBarry Smith $ int func(SNES snes,int its, PetscReal norm,void *mctx) 1694c7afd0dbSLois Curfman McInnes 1695c7afd0dbSLois Curfman McInnes + snes - the SNES context 1696c7afd0dbSLois Curfman McInnes . its - iteration number 1697c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 1698c7afd0dbSLois Curfman McInnes for SNES_NONLINEAR_EQUATIONS methods 169940a0c1c6SLois Curfman McInnes . norm - 2-norm gradient value (may be estimated) 1700c7afd0dbSLois Curfman McInnes for SNES_UNCONSTRAINED_MINIMIZATION methods 170140a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 17029b94acceSBarry Smith 17039665c990SLois Curfman McInnes Options Database Keys: 1704c7afd0dbSLois Curfman McInnes + -snes_monitor - sets SNESDefaultMonitor() 1705c7afd0dbSLois Curfman McInnes . -snes_xmonitor - sets line graph monitor, 1706c7afd0dbSLois Curfman McInnes uses SNESLGMonitorCreate() 1707c7afd0dbSLois Curfman McInnes _ -snes_cancelmonitors - cancels all monitors that have 1708c7afd0dbSLois Curfman McInnes been hardwired into a code by 1709c7afd0dbSLois Curfman McInnes calls to SNESSetMonitor(), but 1710c7afd0dbSLois Curfman McInnes does not cancel those set via 1711c7afd0dbSLois Curfman McInnes the options database. 17129665c990SLois Curfman McInnes 1713639f9d9dSBarry Smith Notes: 17146bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 17156bc08f3fSLois Curfman McInnes SNESSetMonitor() multiple times; all will be called in the 17166bc08f3fSLois Curfman McInnes order in which they were set. 1717639f9d9dSBarry Smith 171836851e7fSLois Curfman McInnes Level: intermediate 171936851e7fSLois Curfman McInnes 17209b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 17219b94acceSBarry Smith 17225cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor() 17239b94acceSBarry Smith @*/ 1724329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *)) 17259b94acceSBarry Smith { 17263a40ed3dSBarry Smith PetscFunctionBegin; 1727184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1728639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 1729a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set"); 1730639f9d9dSBarry Smith } 1731639f9d9dSBarry Smith 1732639f9d9dSBarry Smith snes->monitor[snes->numbermonitors] = func; 1733b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1734639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 17353a40ed3dSBarry Smith PetscFunctionReturn(0); 17369b94acceSBarry Smith } 17379b94acceSBarry Smith 17385615d1e5SSatish Balay #undef __FUNC__ 1739b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESClearMonitor" 17405cd90555SBarry Smith /*@C 17415cd90555SBarry Smith SNESClearMonitor - Clears all the monitor functions for a SNES object. 17425cd90555SBarry Smith 1743c7afd0dbSLois Curfman McInnes Collective on SNES 1744c7afd0dbSLois Curfman McInnes 17455cd90555SBarry Smith Input Parameters: 17465cd90555SBarry Smith . snes - the SNES context 17475cd90555SBarry Smith 17485cd90555SBarry Smith Options Database: 1749c7afd0dbSLois Curfman McInnes . -snes_cancelmonitors - cancels all monitors that have been hardwired 1750c7afd0dbSLois Curfman McInnes into a code by calls to SNESSetMonitor(), but does not cancel those 1751c7afd0dbSLois Curfman McInnes set via the options database 17525cd90555SBarry Smith 17535cd90555SBarry Smith Notes: 17545cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 17555cd90555SBarry Smith 175636851e7fSLois Curfman McInnes Level: intermediate 175736851e7fSLois Curfman McInnes 17585cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 17595cd90555SBarry Smith 17605cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor() 17615cd90555SBarry Smith @*/ 17625cd90555SBarry Smith int SNESClearMonitor(SNES snes) 17635cd90555SBarry Smith { 17645cd90555SBarry Smith PetscFunctionBegin; 1765184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 17665cd90555SBarry Smith snes->numbermonitors = 0; 17675cd90555SBarry Smith PetscFunctionReturn(0); 17685cd90555SBarry Smith } 17695cd90555SBarry Smith 17705cd90555SBarry Smith #undef __FUNC__ 1771b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetConvergenceTest" 17729b94acceSBarry Smith /*@C 17739b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 17749b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 17759b94acceSBarry Smith 1776fee21e36SBarry Smith Collective on SNES 1777fee21e36SBarry Smith 1778c7afd0dbSLois Curfman McInnes Input Parameters: 1779c7afd0dbSLois Curfman McInnes + snes - the SNES context 1780c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1781c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1782c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 17839b94acceSBarry Smith 1784c7afd0dbSLois Curfman McInnes Calling sequence of func: 1785329f5518SBarry Smith $ int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1786c7afd0dbSLois Curfman McInnes 1787c7afd0dbSLois Curfman McInnes + snes - the SNES context 1788c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1789184914b5SBarry Smith . reason - reason for convergence/divergence 1790c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 1791c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods) 1792c7afd0dbSLois Curfman McInnes . f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods) 1793c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods) 1794c7afd0dbSLois Curfman McInnes - f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods) 17959b94acceSBarry Smith 179636851e7fSLois Curfman McInnes Level: advanced 179736851e7fSLois Curfman McInnes 17989b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 17999b94acceSBarry Smith 180040191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 180140191667SLois Curfman McInnes SNESConverged_UM_LS(), SNESConverged_UM_TR() 18029b94acceSBarry Smith @*/ 1803329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx) 18049b94acceSBarry Smith { 18053a40ed3dSBarry Smith PetscFunctionBegin; 1806184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 18079b94acceSBarry Smith (snes)->converged = func; 18089b94acceSBarry Smith (snes)->cnvP = cctx; 18093a40ed3dSBarry Smith PetscFunctionReturn(0); 18109b94acceSBarry Smith } 18119b94acceSBarry Smith 18125615d1e5SSatish Balay #undef __FUNC__ 1813b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetConvergedReason" 1814184914b5SBarry Smith /*@C 1815184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1816184914b5SBarry Smith 1817184914b5SBarry Smith Not Collective 1818184914b5SBarry Smith 1819184914b5SBarry Smith Input Parameter: 1820184914b5SBarry Smith . snes - the SNES context 1821184914b5SBarry Smith 1822184914b5SBarry Smith Output Parameter: 1823*e090d566SSatish Balay . reason - negative value indicates diverged, positive value converged, see petscsnes.h or the 1824184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1825184914b5SBarry Smith 1826184914b5SBarry Smith Level: intermediate 1827184914b5SBarry Smith 1828184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1829184914b5SBarry Smith 1830184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1831184914b5SBarry Smith 1832184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 1833184914b5SBarry Smith SNESConverged_UM_LS(), SNESConverged_UM_TR() 1834184914b5SBarry Smith @*/ 1835184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1836184914b5SBarry Smith { 1837184914b5SBarry Smith PetscFunctionBegin; 1838184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1839184914b5SBarry Smith *reason = snes->reason; 1840184914b5SBarry Smith PetscFunctionReturn(0); 1841184914b5SBarry Smith } 1842184914b5SBarry Smith 1843184914b5SBarry Smith #undef __FUNC__ 1844b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetConvergenceHistory" 1845c9005455SLois Curfman McInnes /*@ 1846c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1847c9005455SLois Curfman McInnes 1848fee21e36SBarry Smith Collective on SNES 1849fee21e36SBarry Smith 1850c7afd0dbSLois Curfman McInnes Input Parameters: 1851c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1852c7afd0dbSLois Curfman McInnes . a - array to hold history 1853758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1854758f92a0SBarry Smith negative if not converged) for each solve. 1855758f92a0SBarry Smith . na - size of a and its 1856758f92a0SBarry Smith - reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero, 1857758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1858c7afd0dbSLois Curfman McInnes 1859c9005455SLois Curfman McInnes Notes: 1860c9005455SLois Curfman McInnes If set, this array will contain the function norms (for 1861c9005455SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods) or gradient norms 1862c9005455SLois Curfman McInnes (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed 1863c9005455SLois Curfman McInnes at each step. 1864c9005455SLois Curfman McInnes 1865c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1866c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1867c9005455SLois Curfman McInnes during the section of code that is being timed. 1868c9005455SLois Curfman McInnes 186936851e7fSLois Curfman McInnes Level: intermediate 187036851e7fSLois Curfman McInnes 1871c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1872758f92a0SBarry Smith 187308405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1874758f92a0SBarry Smith 1875c9005455SLois Curfman McInnes @*/ 1876329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset) 1877c9005455SLois Curfman McInnes { 18783a40ed3dSBarry Smith PetscFunctionBegin; 1879c9005455SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 1880c9005455SLois Curfman McInnes if (na) PetscValidScalarPointer(a); 1881c9005455SLois Curfman McInnes snes->conv_hist = a; 1882758f92a0SBarry Smith snes->conv_hist_its = its; 1883758f92a0SBarry Smith snes->conv_hist_max = na; 1884758f92a0SBarry Smith snes->conv_hist_reset = reset; 1885758f92a0SBarry Smith PetscFunctionReturn(0); 1886758f92a0SBarry Smith } 1887758f92a0SBarry Smith 1888758f92a0SBarry Smith #undef __FUNC__ 1889b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetConvergenceHistory" 18900c4c9dddSBarry Smith /*@C 1891758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1892758f92a0SBarry Smith 1893758f92a0SBarry Smith Collective on SNES 1894758f92a0SBarry Smith 1895758f92a0SBarry Smith Input Parameter: 1896758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1897758f92a0SBarry Smith 1898758f92a0SBarry Smith Output Parameters: 1899758f92a0SBarry Smith . a - array to hold history 1900758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1901758f92a0SBarry Smith negative if not converged) for each solve. 1902758f92a0SBarry Smith - na - size of a and its 1903758f92a0SBarry Smith 1904758f92a0SBarry Smith Notes: 1905758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1906758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1907758f92a0SBarry Smith 1908758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1909758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1910758f92a0SBarry Smith during the section of code that is being timed. 1911758f92a0SBarry Smith 1912758f92a0SBarry Smith Level: intermediate 1913758f92a0SBarry Smith 1914758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1915758f92a0SBarry Smith 1916758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1917758f92a0SBarry Smith 1918758f92a0SBarry Smith @*/ 1919329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na) 1920758f92a0SBarry Smith { 1921758f92a0SBarry Smith PetscFunctionBegin; 1922758f92a0SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1923758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1924758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1925758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 19263a40ed3dSBarry Smith PetscFunctionReturn(0); 1927c9005455SLois Curfman McInnes } 1928c9005455SLois Curfman McInnes 1929c9005455SLois Curfman McInnes #undef __FUNC__ 1930b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESScaleStep_Private" 19319b94acceSBarry Smith /* 19329b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 19339b94acceSBarry Smith positive parameter delta. 19349b94acceSBarry Smith 19359b94acceSBarry Smith Input Parameters: 1936c7afd0dbSLois Curfman McInnes + snes - the SNES context 19379b94acceSBarry Smith . y - approximate solution of linear system 19389b94acceSBarry Smith . fnorm - 2-norm of current function 1939c7afd0dbSLois Curfman McInnes - delta - trust region size 19409b94acceSBarry Smith 19419b94acceSBarry Smith Output Parameters: 1942c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 19439b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 19449b94acceSBarry Smith region, and exceeds zero otherwise. 1945c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 19469b94acceSBarry Smith 19479b94acceSBarry Smith Note: 19486831982aSBarry Smith For non-trust region methods such as SNESEQLS, the parameter delta 19499b94acceSBarry Smith is set to be the maximum allowable step size. 19509b94acceSBarry Smith 19519b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 19529b94acceSBarry Smith */ 1953329f5518SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta, 1954329f5518SBarry Smith PetscReal *gpnorm,PetscReal *ynorm) 19559b94acceSBarry Smith { 1956329f5518SBarry Smith PetscReal norm; 19579b94acceSBarry Smith Scalar cnorm; 19583a40ed3dSBarry Smith int ierr; 19593a40ed3dSBarry Smith 19603a40ed3dSBarry Smith PetscFunctionBegin; 1961184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1962184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1963184914b5SBarry Smith PetscCheckSameComm(snes,y); 1964184914b5SBarry Smith 19653a40ed3dSBarry Smith ierr = VecNorm(y,NORM_2,&norm);CHKERRQ(ierr); 19669b94acceSBarry Smith if (norm > *delta) { 19679b94acceSBarry Smith norm = *delta/norm; 19689b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 19699b94acceSBarry Smith cnorm = norm; 1970329f5518SBarry Smith ierr = VecScale(&cnorm,y);CHKERRQ(ierr); 19719b94acceSBarry Smith *ynorm = *delta; 19729b94acceSBarry Smith } else { 19739b94acceSBarry Smith *gpnorm = 0.0; 19749b94acceSBarry Smith *ynorm = norm; 19759b94acceSBarry Smith } 19763a40ed3dSBarry Smith PetscFunctionReturn(0); 19779b94acceSBarry Smith } 19789b94acceSBarry Smith 19795615d1e5SSatish Balay #undef __FUNC__ 1980b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSolve" 19819b94acceSBarry Smith /*@ 19829b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 198363a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 19849b94acceSBarry Smith 1985c7afd0dbSLois Curfman McInnes Collective on SNES 1986c7afd0dbSLois Curfman McInnes 1987b2002411SLois Curfman McInnes Input Parameters: 1988c7afd0dbSLois Curfman McInnes + snes - the SNES context 1989c7afd0dbSLois Curfman McInnes - x - the solution vector 19909b94acceSBarry Smith 19919b94acceSBarry Smith Output Parameter: 1992b2002411SLois Curfman McInnes . its - number of iterations until termination 19939b94acceSBarry Smith 1994b2002411SLois Curfman McInnes Notes: 19958ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 19968ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 19978ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 19988ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 19998ddd3da0SLois Curfman McInnes 200036851e7fSLois Curfman McInnes Level: beginner 200136851e7fSLois Curfman McInnes 20029b94acceSBarry Smith .keywords: SNES, nonlinear, solve 20039b94acceSBarry Smith 200463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 20059b94acceSBarry Smith @*/ 20068ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 20079b94acceSBarry Smith { 2008f1af5d2fSBarry Smith int ierr; 2009f1af5d2fSBarry Smith PetscTruth flg; 2010052efed2SBarry Smith 20113a40ed3dSBarry Smith PetscFunctionBegin; 201277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2013184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 2014184914b5SBarry Smith PetscCheckSameComm(snes,x); 201574679c65SBarry Smith PetscValidIntPointer(its); 201682bf6240SBarry Smith if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);} 2017c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 2018758f92a0SBarry Smith if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0; 20199b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 2020c96a6f78SLois Curfman McInnes snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0; 20219b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr); 20229b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 20230f5bd95cSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr); 20246d4a8577SBarry Smith if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } 20253a40ed3dSBarry Smith PetscFunctionReturn(0); 20269b94acceSBarry Smith } 20279b94acceSBarry Smith 20289b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 20299b94acceSBarry Smith 20305615d1e5SSatish Balay #undef __FUNC__ 2031b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetType" 203282bf6240SBarry Smith /*@C 20334b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 20349b94acceSBarry Smith 2035fee21e36SBarry Smith Collective on SNES 2036fee21e36SBarry Smith 2037c7afd0dbSLois Curfman McInnes Input Parameters: 2038c7afd0dbSLois Curfman McInnes + snes - the SNES context 2039454a90a3SBarry Smith - type - a known method 2040c7afd0dbSLois Curfman McInnes 2041c7afd0dbSLois Curfman McInnes Options Database Key: 2042454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2043c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2044ae12b187SLois Curfman McInnes 20459b94acceSBarry Smith Notes: 2046*e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 20476831982aSBarry Smith + SNESEQLS - Newton's method with line search 2048c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20496831982aSBarry Smith . SNESEQTR - Newton's method with trust region 2050c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20516831982aSBarry Smith . SNESUMTR - Newton's method with trust region 2052c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20536831982aSBarry Smith - SNESUMLS - Newton's method with line search 2054c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20559b94acceSBarry Smith 2056ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2057ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2058ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2059ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2060ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2061ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2062ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2063ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2064ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 206536851e7fSLois Curfman McInnes appropriate method. In other words, this routine is not for beginners. 206636851e7fSLois Curfman McInnes 206736851e7fSLois Curfman McInnes Level: intermediate 2068a703fe33SLois Curfman McInnes 2069454a90a3SBarry Smith .keywords: SNES, set, type 20709b94acceSBarry Smith @*/ 2071454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type) 20729b94acceSBarry Smith { 20730f5bd95cSBarry Smith int ierr,(*r)(SNES); 20746831982aSBarry Smith PetscTruth match; 20753a40ed3dSBarry Smith 20763a40ed3dSBarry Smith PetscFunctionBegin; 207777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 20780f5bd95cSBarry Smith PetscValidCharPointer(type); 207982bf6240SBarry Smith 20806831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 20810f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 208282bf6240SBarry Smith 208382bf6240SBarry Smith if (snes->setupcalled) { 2084e1311b90SBarry Smith ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr); 208582bf6240SBarry Smith snes->data = 0; 208682bf6240SBarry Smith } 20877d1a2b2bSBarry Smith 20889b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 208982bf6240SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 209082bf6240SBarry Smith 2091454a90a3SBarry Smith ierr = FListFind(snes->comm,SNESList,type,(int (**)(void *)) &r);CHKERRQ(ierr); 209282bf6240SBarry Smith 2093454a90a3SBarry Smith if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",type); 20941548b14aSBarry Smith 2095606d414cSSatish Balay if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);} 209682bf6240SBarry Smith snes->data = 0; 20973a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 209882bf6240SBarry Smith 2099454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 210082bf6240SBarry Smith snes->set_method_called = 1; 210182bf6240SBarry Smith 21023a40ed3dSBarry Smith PetscFunctionReturn(0); 21039b94acceSBarry Smith } 21049b94acceSBarry Smith 2105a847f771SSatish Balay 21069b94acceSBarry Smith /* --------------------------------------------------------------------- */ 21075615d1e5SSatish Balay #undef __FUNC__ 2108b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESRegisterDestroy" 21099b94acceSBarry Smith /*@C 21109b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2111f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 21129b94acceSBarry Smith 2113fee21e36SBarry Smith Not Collective 2114fee21e36SBarry Smith 211536851e7fSLois Curfman McInnes Level: advanced 211636851e7fSLois Curfman McInnes 21179b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 21189b94acceSBarry Smith 21199b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 21209b94acceSBarry Smith @*/ 2121cf256101SBarry Smith int SNESRegisterDestroy(void) 21229b94acceSBarry Smith { 212382bf6240SBarry Smith int ierr; 212482bf6240SBarry Smith 21253a40ed3dSBarry Smith PetscFunctionBegin; 212682bf6240SBarry Smith if (SNESList) { 2127488ecbafSBarry Smith ierr = FListDestroy(SNESList);CHKERRQ(ierr); 212882bf6240SBarry Smith SNESList = 0; 21299b94acceSBarry Smith } 21304c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 21313a40ed3dSBarry Smith PetscFunctionReturn(0); 21329b94acceSBarry Smith } 21339b94acceSBarry Smith 21345615d1e5SSatish Balay #undef __FUNC__ 2135b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetType" 21369b94acceSBarry Smith /*@C 21379a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 21389b94acceSBarry Smith 2139c7afd0dbSLois Curfman McInnes Not Collective 2140c7afd0dbSLois Curfman McInnes 21419b94acceSBarry Smith Input Parameter: 21424b0e389bSBarry Smith . snes - nonlinear solver context 21439b94acceSBarry Smith 21449b94acceSBarry Smith Output Parameter: 2145454a90a3SBarry Smith . type - SNES method (a charactor string) 21469b94acceSBarry Smith 214736851e7fSLois Curfman McInnes Level: intermediate 214836851e7fSLois Curfman McInnes 2149454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 21509b94acceSBarry Smith @*/ 2151454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type) 21529b94acceSBarry Smith { 21533a40ed3dSBarry Smith PetscFunctionBegin; 2154184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2155454a90a3SBarry Smith *type = snes->type_name; 21563a40ed3dSBarry Smith PetscFunctionReturn(0); 21579b94acceSBarry Smith } 21589b94acceSBarry Smith 21595615d1e5SSatish Balay #undef __FUNC__ 2160b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetSolution" 21619b94acceSBarry Smith /*@C 21629b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 21639b94acceSBarry Smith stored. 21649b94acceSBarry Smith 2165c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2166c7afd0dbSLois Curfman McInnes 21679b94acceSBarry Smith Input Parameter: 21689b94acceSBarry Smith . snes - the SNES context 21699b94acceSBarry Smith 21709b94acceSBarry Smith Output Parameter: 21719b94acceSBarry Smith . x - the solution 21729b94acceSBarry Smith 217336851e7fSLois Curfman McInnes Level: advanced 217436851e7fSLois Curfman McInnes 21759b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 21769b94acceSBarry Smith 2177a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 21789b94acceSBarry Smith @*/ 21799b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 21809b94acceSBarry Smith { 21813a40ed3dSBarry Smith PetscFunctionBegin; 218277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 21839b94acceSBarry Smith *x = snes->vec_sol_always; 21843a40ed3dSBarry Smith PetscFunctionReturn(0); 21859b94acceSBarry Smith } 21869b94acceSBarry Smith 21875615d1e5SSatish Balay #undef __FUNC__ 2188b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetSolutionUpdate" 21899b94acceSBarry Smith /*@C 21909b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 21919b94acceSBarry Smith stored. 21929b94acceSBarry Smith 2193c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2194c7afd0dbSLois Curfman McInnes 21959b94acceSBarry Smith Input Parameter: 21969b94acceSBarry Smith . snes - the SNES context 21979b94acceSBarry Smith 21989b94acceSBarry Smith Output Parameter: 21999b94acceSBarry Smith . x - the solution update 22009b94acceSBarry Smith 220136851e7fSLois Curfman McInnes Level: advanced 220236851e7fSLois Curfman McInnes 22039b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 22049b94acceSBarry Smith 22059b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 22069b94acceSBarry Smith @*/ 22079b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 22089b94acceSBarry Smith { 22093a40ed3dSBarry Smith PetscFunctionBegin; 221077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22119b94acceSBarry Smith *x = snes->vec_sol_update_always; 22123a40ed3dSBarry Smith PetscFunctionReturn(0); 22139b94acceSBarry Smith } 22149b94acceSBarry Smith 22155615d1e5SSatish Balay #undef __FUNC__ 2216b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetFunction" 22179b94acceSBarry Smith /*@C 22183638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 22199b94acceSBarry Smith 2220c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2221c7afd0dbSLois Curfman McInnes 22229b94acceSBarry Smith Input Parameter: 22239b94acceSBarry Smith . snes - the SNES context 22249b94acceSBarry Smith 22259b94acceSBarry Smith Output Parameter: 22267bf4e008SBarry Smith + r - the function (or PETSC_NULL) 22277bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 22289b94acceSBarry Smith 22299b94acceSBarry Smith Notes: 22309b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 22319b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 22329b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 22339b94acceSBarry Smith 223436851e7fSLois Curfman McInnes Level: advanced 223536851e7fSLois Curfman McInnes 2236a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 22379b94acceSBarry Smith 223831615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 223931615d04SLois Curfman McInnes SNESGetGradient() 22407bf4e008SBarry Smith 22419b94acceSBarry Smith @*/ 22427bf4e008SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx) 22439b94acceSBarry Smith { 22443a40ed3dSBarry Smith PetscFunctionBegin; 224577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2246a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 2247a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 2248a8c6a408SBarry Smith } 22497bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22507bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22513a40ed3dSBarry Smith PetscFunctionReturn(0); 22529b94acceSBarry Smith } 22539b94acceSBarry Smith 22545615d1e5SSatish Balay #undef __FUNC__ 2255b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetGradient" 22569b94acceSBarry Smith /*@C 22573638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 22589b94acceSBarry Smith 2259c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2260c7afd0dbSLois Curfman McInnes 22619b94acceSBarry Smith Input Parameter: 22629b94acceSBarry Smith . snes - the SNES context 22639b94acceSBarry Smith 22649b94acceSBarry Smith Output Parameter: 22657bf4e008SBarry Smith + r - the gradient (or PETSC_NULL) 22667bf4e008SBarry Smith - ctx - the gradient context (or PETSC_NULL) 22679b94acceSBarry Smith 22689b94acceSBarry Smith Notes: 22699b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 22709b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 22719b94acceSBarry Smith SNESGetFunction(). 22729b94acceSBarry Smith 227336851e7fSLois Curfman McInnes Level: advanced 227436851e7fSLois Curfman McInnes 22759b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 22769b94acceSBarry Smith 22777bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(), 22787bf4e008SBarry Smith SNESSetGradient(), SNESSetFunction() 22797bf4e008SBarry Smith 22809b94acceSBarry Smith @*/ 22817bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx) 22829b94acceSBarry Smith { 22833a40ed3dSBarry Smith PetscFunctionBegin; 228477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22853a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2286a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 22873a40ed3dSBarry Smith } 22887bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22897bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22903a40ed3dSBarry Smith PetscFunctionReturn(0); 22919b94acceSBarry Smith } 22929b94acceSBarry Smith 22935615d1e5SSatish Balay #undef __FUNC__ 2294b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetMinimizationFunction" 22957bf4e008SBarry Smith /*@C 22969b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 22979b94acceSBarry Smith unconstrained minimization problems. 22989b94acceSBarry Smith 2299c7afd0dbSLois Curfman McInnes Not Collective 2300c7afd0dbSLois Curfman McInnes 23019b94acceSBarry Smith Input Parameter: 23029b94acceSBarry Smith . snes - the SNES context 23039b94acceSBarry Smith 23049b94acceSBarry Smith Output Parameter: 23057bf4e008SBarry Smith + r - the function (or PETSC_NULL) 23067bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 23079b94acceSBarry Smith 23089b94acceSBarry Smith Notes: 23099b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 23109b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 23119b94acceSBarry Smith SNESGetFunction(). 23129b94acceSBarry Smith 231336851e7fSLois Curfman McInnes Level: advanced 231436851e7fSLois Curfman McInnes 23159b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 23169b94acceSBarry Smith 23177bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction() 23187bf4e008SBarry Smith 23199b94acceSBarry Smith @*/ 2320329f5518SBarry Smith int SNESGetMinimizationFunction(SNES snes,PetscReal *r,void **ctx) 23219b94acceSBarry Smith { 23223a40ed3dSBarry Smith PetscFunctionBegin; 232377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 232474679c65SBarry Smith PetscValidScalarPointer(r); 23253a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2326a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 23273a40ed3dSBarry Smith } 23287bf4e008SBarry Smith if (r) *r = snes->fc; 23297bf4e008SBarry Smith if (ctx) *ctx = snes->umfunP; 23303a40ed3dSBarry Smith PetscFunctionReturn(0); 23319b94acceSBarry Smith } 23329b94acceSBarry Smith 23335615d1e5SSatish Balay #undef __FUNC__ 2334b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetOptionsPrefix" 23353c7409f5SSatish Balay /*@C 23363c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2337d850072dSLois Curfman McInnes SNES options in the database. 23383c7409f5SSatish Balay 2339fee21e36SBarry Smith Collective on SNES 2340fee21e36SBarry Smith 2341c7afd0dbSLois Curfman McInnes Input Parameter: 2342c7afd0dbSLois Curfman McInnes + snes - the SNES context 2343c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2344c7afd0dbSLois Curfman McInnes 2345d850072dSLois Curfman McInnes Notes: 2346a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2347c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2348d850072dSLois Curfman McInnes 234936851e7fSLois Curfman McInnes Level: advanced 235036851e7fSLois Curfman McInnes 23513c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2352a86d99e1SLois Curfman McInnes 2353a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 23543c7409f5SSatish Balay @*/ 23553c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 23563c7409f5SSatish Balay { 23573c7409f5SSatish Balay int ierr; 23583c7409f5SSatish Balay 23593a40ed3dSBarry Smith PetscFunctionBegin; 236077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2361639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 23623c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 23633a40ed3dSBarry Smith PetscFunctionReturn(0); 23643c7409f5SSatish Balay } 23653c7409f5SSatish Balay 23665615d1e5SSatish Balay #undef __FUNC__ 2367b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESAppendOptionsPrefix" 23683c7409f5SSatish Balay /*@C 2369f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2370d850072dSLois Curfman McInnes SNES options in the database. 23713c7409f5SSatish Balay 2372fee21e36SBarry Smith Collective on SNES 2373fee21e36SBarry Smith 2374c7afd0dbSLois Curfman McInnes Input Parameters: 2375c7afd0dbSLois Curfman McInnes + snes - the SNES context 2376c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2377c7afd0dbSLois Curfman McInnes 2378d850072dSLois Curfman McInnes Notes: 2379a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2380c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2381d850072dSLois Curfman McInnes 238236851e7fSLois Curfman McInnes Level: advanced 238336851e7fSLois Curfman McInnes 23843c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2385a86d99e1SLois Curfman McInnes 2386a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 23873c7409f5SSatish Balay @*/ 23883c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 23893c7409f5SSatish Balay { 23903c7409f5SSatish Balay int ierr; 23913c7409f5SSatish Balay 23923a40ed3dSBarry Smith PetscFunctionBegin; 239377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2394639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 23953c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 23963a40ed3dSBarry Smith PetscFunctionReturn(0); 23973c7409f5SSatish Balay } 23983c7409f5SSatish Balay 23995615d1e5SSatish Balay #undef __FUNC__ 2400b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetOptionsPrefix" 24019ab63eb5SSatish Balay /*@C 24023c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 24033c7409f5SSatish Balay SNES options in the database. 24043c7409f5SSatish Balay 2405c7afd0dbSLois Curfman McInnes Not Collective 2406c7afd0dbSLois Curfman McInnes 24073c7409f5SSatish Balay Input Parameter: 24083c7409f5SSatish Balay . snes - the SNES context 24093c7409f5SSatish Balay 24103c7409f5SSatish Balay Output Parameter: 24113c7409f5SSatish Balay . prefix - pointer to the prefix string used 24123c7409f5SSatish Balay 24139ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 24149ab63eb5SSatish Balay sufficient length to hold the prefix. 24159ab63eb5SSatish Balay 241636851e7fSLois Curfman McInnes Level: advanced 241736851e7fSLois Curfman McInnes 24183c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2419a86d99e1SLois Curfman McInnes 2420a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 24213c7409f5SSatish Balay @*/ 24223c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 24233c7409f5SSatish Balay { 24243c7409f5SSatish Balay int ierr; 24253c7409f5SSatish Balay 24263a40ed3dSBarry Smith PetscFunctionBegin; 242777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2428639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 24293a40ed3dSBarry Smith PetscFunctionReturn(0); 24303c7409f5SSatish Balay } 24313c7409f5SSatish Balay 2432ca161407SBarry Smith #undef __FUNC__ 2433b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESPrintHelp" 2434ca161407SBarry Smith /*@ 2435ca161407SBarry Smith SNESPrintHelp - Prints all options for the SNES component. 2436ca161407SBarry Smith 2437c7afd0dbSLois Curfman McInnes Collective on SNES 2438c7afd0dbSLois Curfman McInnes 2439ca161407SBarry Smith Input Parameter: 2440ca161407SBarry Smith . snes - the SNES context 2441ca161407SBarry Smith 2442ca161407SBarry Smith Options Database Keys: 2443c7afd0dbSLois Curfman McInnes + -help - Prints SNES options 2444c7afd0dbSLois Curfman McInnes - -h - Prints SNES options 2445ca161407SBarry Smith 244636851e7fSLois Curfman McInnes Level: beginner 244736851e7fSLois Curfman McInnes 2448ca161407SBarry Smith .keywords: SNES, nonlinear, help 2449ca161407SBarry Smith 2450ca161407SBarry Smith .seealso: SNESSetFromOptions() 2451ca161407SBarry Smith @*/ 2452ca161407SBarry Smith int SNESPrintHelp(SNES snes) 2453ca161407SBarry Smith { 2454ca161407SBarry Smith char p[64]; 2455ca161407SBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2456ca161407SBarry Smith int ierr; 2457ca161407SBarry Smith 2458ca161407SBarry Smith PetscFunctionBegin; 2459ca161407SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2460ca161407SBarry Smith 2461549d3d68SSatish Balay ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 2462ca161407SBarry Smith if (snes->prefix) PetscStrcat(p,snes->prefix); 2463ca161407SBarry Smith 2464ca161407SBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2465ca161407SBarry Smith 2466ebb8b11fSBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2467d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr); 2468488ecbafSBarry Smith ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr); 2469d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr); 2470d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr); 2471d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr); 2472d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr); 2473d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr); 2474d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr); 2475d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr); 2476d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr); 2477d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr); 2478d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_monitor: use default SNES convergence monitor, prints\n\ 2479d132466eSBarry Smith residual norm at each iteration.\n",p);CHKERRQ(ierr); 2480d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_smonitor: same as the above, but prints fewer digits of the\n\ 2481ca161407SBarry Smith residual norm for small residual norms. This is useful to conceal\n\ 2482d132466eSBarry Smith meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr); 2483d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr); 2484d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr); 2485d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr); 2486ca161407SBarry Smith if (snes->type == SNES_NONLINEAR_EQUATIONS) { 2487d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2488d132466eSBarry Smith " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr); 2489d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr); 2490d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr); 2491d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr); 2492d132466eSBarry 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); 2493d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr); 2494d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr); 2495d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2496d132466eSBarry Smith " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr); 2497d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2498d132466eSBarry Smith " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr); 2499d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2500d132466eSBarry Smith " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr); 2501d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2502d132466eSBarry Smith " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr); 2503d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2504d132466eSBarry Smith " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr); 2505d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2506d132466eSBarry Smith " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr); 2507d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2508d132466eSBarry Smith " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr); 2509ca161407SBarry Smith } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 2510d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr); 2511d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr); 2512d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr); 2513d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr); 2514ca161407SBarry Smith } 2515454a90a3SBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <type> for help on ",p);CHKERRQ(ierr); 2516d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr); 2517ca161407SBarry Smith if (snes->printhelp) { 2518ca161407SBarry Smith ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr); 2519ca161407SBarry Smith } 2520ca161407SBarry Smith PetscFunctionReturn(0); 2521ca161407SBarry Smith } 25223c7409f5SSatish Balay 2523acb85ae6SSatish Balay /*MC 2524f1af5d2fSBarry Smith SNESRegisterDynamic - Adds a method to the nonlinear solver package. 25259b94acceSBarry Smith 2526b2002411SLois Curfman McInnes Synopsis: 2527f1af5d2fSBarry Smith SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES)) 25289b94acceSBarry Smith 25298d76a1e5SLois Curfman McInnes Not collective 25308d76a1e5SLois Curfman McInnes 2531b2002411SLois Curfman McInnes Input Parameters: 2532c7afd0dbSLois Curfman McInnes + name_solver - name of a new user-defined solver 2533b2002411SLois Curfman McInnes . path - path (either absolute or relative) the library containing this solver 2534b2002411SLois Curfman McInnes . name_create - name of routine to create method context 2535c7afd0dbSLois Curfman McInnes - routine_create - routine to create method context 25369b94acceSBarry Smith 2537b2002411SLois Curfman McInnes Notes: 2538f1af5d2fSBarry Smith SNESRegisterDynamic() may be called multiple times to add several user-defined solvers. 25393a40ed3dSBarry Smith 2540b2002411SLois Curfman McInnes If dynamic libraries are used, then the fourth input argument (routine_create) 2541b2002411SLois Curfman McInnes is ignored. 2542b2002411SLois Curfman McInnes 25435ba88a07SLois Curfman McInnes Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LDIR}, ${BOPT}, 25445ba88a07SLois Curfman McInnes and others of the form ${any_environmental_variable} occuring in pathname will be 25455ba88a07SLois Curfman McInnes replaced with appropriate values. 25465ba88a07SLois Curfman McInnes 2547b2002411SLois Curfman McInnes Sample usage: 254869225d78SLois Curfman McInnes .vb 2549f1af5d2fSBarry Smith SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a, 2550b2002411SLois Curfman McInnes "MySolverCreate",MySolverCreate); 255169225d78SLois Curfman McInnes .ve 2552b2002411SLois Curfman McInnes 2553b2002411SLois Curfman McInnes Then, your solver can be chosen with the procedural interface via 2554b2002411SLois Curfman McInnes $ SNESSetType(snes,"my_solver") 2555b2002411SLois Curfman McInnes or at runtime via the option 2556b2002411SLois Curfman McInnes $ -snes_type my_solver 2557b2002411SLois Curfman McInnes 255836851e7fSLois Curfman McInnes Level: advanced 255936851e7fSLois Curfman McInnes 2560b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register 2561b2002411SLois Curfman McInnes 2562b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy() 2563b2002411SLois Curfman McInnes M*/ 2564b2002411SLois Curfman McInnes 2565b2002411SLois Curfman McInnes #undef __FUNC__ 2566b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESRegister" 2567f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES)) 2568b2002411SLois Curfman McInnes { 2569b2002411SLois Curfman McInnes char fullname[256]; 2570b2002411SLois Curfman McInnes int ierr; 2571b2002411SLois Curfman McInnes 2572b2002411SLois Curfman McInnes PetscFunctionBegin; 2573f1af5d2fSBarry Smith ierr = FListConcat(path,name,fullname);CHKERRQ(ierr); 2574f1af5d2fSBarry Smith ierr = FListAdd(&SNESList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr); 2575b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2576b2002411SLois Curfman McInnes } 2577