1*329f5518SBarry Smith /*$Id: snes.c,v 1.204 1999/11/24 21:55:08 bsmith Exp bsmith $*/ 29b94acceSBarry Smith 370f55243SBarry Smith #include "src/snes/snesimpl.h" /*I "snes.h" I*/ 49b94acceSBarry Smith 584cb2905SBarry Smith int SNESRegisterAllCalled = 0; 6488ecbafSBarry Smith FList SNESList = 0; 782bf6240SBarry Smith 85615d1e5SSatish Balay #undef __FUNC__ 9d4bb536fSBarry Smith #define __FUNC__ "SNESView" 109b94acceSBarry Smith /*@ 119b94acceSBarry Smith SNESView - Prints the SNES data structure. 129b94acceSBarry Smith 13fee21e36SBarry Smith Collective on SNES, unless Viewer is VIEWER_STDOUT_SELF 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); 490f5bd95cSBarry Smith if (!viewer) viewer = VIEWER_STDOUT_SELF; 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__ 104d4bb536fSBarry Smith #define __FUNC__ "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__ 12915091d37SBarry Smith #define __FUNC__ "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__ 1755615d1e5SSatish Balay #define __FUNC__ "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 { 226*329f5518SBarry 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; 231*329f5518SBarry Smith PetscReal rtol_0 = PETSC_DEFAULT; 232*329f5518SBarry Smith PetscReal rtol_max = PETSC_DEFAULT; 233*329f5518SBarry Smith PetscReal gamma2 = PETSC_DEFAULT; 234*329f5518SBarry Smith PetscReal alpha = PETSC_DEFAULT; 235*329f5518SBarry Smith PetscReal alpha2 = PETSC_DEFAULT; 236*329f5518SBarry 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__ 328d4bb536fSBarry Smith #define __FUNC__ "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__ 354d4bb536fSBarry Smith #define __FUNC__ "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__ 382d4bb536fSBarry Smith #define __FUNC__ "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__ 4235615d1e5SSatish Balay #define __FUNC__ "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__ 4605615d1e5SSatish Balay #define __FUNC__ "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__ 497d4bb536fSBarry Smith #define __FUNC__ "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__ 527d4bb536fSBarry Smith #define __FUNC__ "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__ 557d4bb536fSBarry Smith #define __FUNC__ "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__ 589e24b481bSBarry Smith #define __FUNC__ "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__ 6155615d1e5SSatish Balay #define __FUNC__ "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__ 722d4bb536fSBarry Smith #define __FUNC__ "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__ 7755615d1e5SSatish Balay #define __FUNC__ "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__ 828d4bb536fSBarry Smith #define __FUNC__ "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: 842*329f5518SBarry 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 @*/ 860*329f5518SBarry 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__ 8735615d1e5SSatish Balay #define __FUNC__ "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 @*/ 903*329f5518SBarry 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__ 925d4bb536fSBarry Smith #define __FUNC__ "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__ 9745615d1e5SSatish Balay #define __FUNC__ "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__ 10265615d1e5SSatish Balay #define __FUNC__ "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 SNESComputeJacobian() is typically used within nonlinear solver 105436851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 105536851e7fSLois Curfman McInnes themselves. 105636851e7fSLois Curfman McInnes 105736851e7fSLois Curfman McInnes Level: developer 105836851e7fSLois Curfman McInnes 105962fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 106062fef451SLois Curfman McInnes 106162fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 106262fef451SLois Curfman McInnes @*/ 10639b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10649b94acceSBarry Smith { 10659b94acceSBarry Smith int ierr; 10663a40ed3dSBarry Smith 10673a40ed3dSBarry Smith PetscFunctionBegin; 1068184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1069184914b5SBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE); 1070184914b5SBarry Smith PetscCheckSameComm(snes,X); 10713a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1072a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 10733a40ed3dSBarry Smith } 10743a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 10759b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 1076c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1077d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 10789b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1079d64ed03dSBarry Smith PetscStackPop; 10809b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 10816d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 108277c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 108377c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 10843a40ed3dSBarry Smith PetscFunctionReturn(0); 10859b94acceSBarry Smith } 10869b94acceSBarry Smith 10875615d1e5SSatish Balay #undef __FUNC__ 10885615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian" 108962fef451SLois Curfman McInnes /*@ 109062fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 109162fef451SLois Curfman McInnes set with SNESSetHessian(). 109262fef451SLois Curfman McInnes 1093c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1094c7afd0dbSLois Curfman McInnes 109562fef451SLois Curfman McInnes Input Parameters: 1096c7afd0dbSLois Curfman McInnes + snes - the SNES context 1097c7afd0dbSLois Curfman McInnes - x - input vector 109862fef451SLois Curfman McInnes 109962fef451SLois Curfman McInnes Output Parameters: 1100c7afd0dbSLois Curfman McInnes + A - Hessian matrix 110162fef451SLois Curfman McInnes . B - optional preconditioning matrix 1102c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1103fee21e36SBarry Smith 110462fef451SLois Curfman McInnes Notes: 110562fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 110662fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 110762fef451SLois Curfman McInnes 1108dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1109dc5a77f8SLois Curfman McInnes flag parameter. 111062fef451SLois Curfman McInnes 111162fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 111262fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 111362fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 111462fef451SLois Curfman McInnes 111536851e7fSLois Curfman McInnes SNESComputeHessian() is typically used within minimization 111636851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 111736851e7fSLois Curfman McInnes themselves. 111836851e7fSLois Curfman McInnes 111936851e7fSLois Curfman McInnes Level: developer 112036851e7fSLois Curfman McInnes 112162fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 112262fef451SLois Curfman McInnes 1123a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 1124a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 112562fef451SLois Curfman McInnes @*/ 112662fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 11279b94acceSBarry Smith { 11289b94acceSBarry Smith int ierr; 11293a40ed3dSBarry Smith 11303a40ed3dSBarry Smith PetscFunctionBegin; 1131184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1132184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1133184914b5SBarry Smith PetscCheckSameComm(snes,x); 11343a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1135a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 11363a40ed3dSBarry Smith } 11373a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 113862fef451SLois Curfman McInnes PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 11390f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 1140d64ed03dSBarry Smith PetscStackPush("SNES user Hessian function"); 114162fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr); 1142d64ed03dSBarry Smith PetscStackPop; 114362fef451SLois Curfman McInnes PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 11440f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 114577c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 114677c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 11473a40ed3dSBarry Smith PetscFunctionReturn(0); 11489b94acceSBarry Smith } 11499b94acceSBarry Smith 11505615d1e5SSatish Balay #undef __FUNC__ 1151d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian" 11529b94acceSBarry Smith /*@C 11539b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1154044dda88SLois Curfman McInnes location to store the matrix. 11559b94acceSBarry Smith 1156c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1157c7afd0dbSLois Curfman McInnes 11589b94acceSBarry Smith Input Parameters: 1159c7afd0dbSLois Curfman McInnes + snes - the SNES context 11609b94acceSBarry Smith . A - Jacobian matrix 11619b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11629b94acceSBarry Smith . func - Jacobian evaluation routine 1163c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11642cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11659b94acceSBarry Smith 11669b94acceSBarry Smith Calling sequence of func: 11678d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11689b94acceSBarry Smith 1169c7afd0dbSLois Curfman McInnes + x - input vector 11709b94acceSBarry Smith . A - Jacobian matrix 11719b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1172ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1173ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1174c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11759b94acceSBarry Smith 11769b94acceSBarry Smith Notes: 1177dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 11782cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1179ac21db08SLois Curfman McInnes 1180ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11819b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11829b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11839b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11849b94acceSBarry Smith throughout the global iterations. 11859b94acceSBarry Smith 118636851e7fSLois Curfman McInnes Level: beginner 118736851e7fSLois Curfman McInnes 11889b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11899b94acceSBarry Smith 1190ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 11919b94acceSBarry Smith @*/ 1192454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 11939b94acceSBarry Smith { 11943a40ed3dSBarry Smith PetscFunctionBegin; 119577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1196184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1197184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1198184914b5SBarry Smith PetscCheckSameComm(snes,A); 1199184914b5SBarry Smith PetscCheckSameComm(snes,B); 1200a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1201a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 1202a8c6a408SBarry Smith } 1203184914b5SBarry Smith 12049b94acceSBarry Smith snes->computejacobian = func; 12059b94acceSBarry Smith snes->jacP = ctx; 12069b94acceSBarry Smith snes->jacobian = A; 12079b94acceSBarry Smith snes->jacobian_pre = B; 12083a40ed3dSBarry Smith PetscFunctionReturn(0); 12099b94acceSBarry Smith } 121062fef451SLois Curfman McInnes 12115615d1e5SSatish Balay #undef __FUNC__ 1212d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian" 1213c2aafc4cSSatish Balay /*@C 1214b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1215b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1216b4fd4287SBarry Smith 1217c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1218c7afd0dbSLois Curfman McInnes 1219b4fd4287SBarry Smith Input Parameter: 1220b4fd4287SBarry Smith . snes - the nonlinear solver context 1221b4fd4287SBarry Smith 1222b4fd4287SBarry Smith Output Parameters: 1223c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1224b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 1225c7afd0dbSLois Curfman McInnes - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1226fee21e36SBarry Smith 122736851e7fSLois Curfman McInnes Level: advanced 122836851e7fSLois Curfman McInnes 1229b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1230b4fd4287SBarry Smith @*/ 1231b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx) 1232b4fd4287SBarry Smith { 12333a40ed3dSBarry Smith PetscFunctionBegin; 1234184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 12353a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1236a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 12373a40ed3dSBarry Smith } 1238b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1239b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1240b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 12413a40ed3dSBarry Smith PetscFunctionReturn(0); 1242b4fd4287SBarry Smith } 1243b4fd4287SBarry Smith 12445615d1e5SSatish Balay #undef __FUNC__ 1245d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian" 12469b94acceSBarry Smith /*@C 12479b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 1248044dda88SLois Curfman McInnes location to store the matrix. 12499b94acceSBarry Smith 1250c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1251c7afd0dbSLois Curfman McInnes 12529b94acceSBarry Smith Input Parameters: 1253c7afd0dbSLois Curfman McInnes + snes - the SNES context 12549b94acceSBarry Smith . A - Hessian matrix 12559b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 12569b94acceSBarry Smith . func - Jacobian evaluation routine 1257c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1258313e4042SLois Curfman McInnes Hessian evaluation routine (may be PETSC_NULL) 12599b94acceSBarry Smith 12609b94acceSBarry Smith Calling sequence of func: 12618d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 12629b94acceSBarry Smith 1263c7afd0dbSLois Curfman McInnes + x - input vector 12649b94acceSBarry Smith . A - Hessian matrix 12659b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1266ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1267ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1268c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Hessian context 12699b94acceSBarry Smith 12709b94acceSBarry Smith Notes: 1271dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 12722cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1273ac21db08SLois Curfman McInnes 12749b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 12759b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 12769b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 12779b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 12789b94acceSBarry Smith throughout the global iterations. 12799b94acceSBarry Smith 128036851e7fSLois Curfman McInnes Level: beginner 128136851e7fSLois Curfman McInnes 12829b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 12839b94acceSBarry Smith 1284ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 12859b94acceSBarry Smith @*/ 1286454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 12879b94acceSBarry Smith { 12883a40ed3dSBarry Smith PetscFunctionBegin; 128977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1290184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1291184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1292184914b5SBarry Smith PetscCheckSameComm(snes,A); 1293184914b5SBarry Smith PetscCheckSameComm(snes,B); 1294d4bb536fSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1295a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1296d4bb536fSBarry Smith } 12979b94acceSBarry Smith snes->computejacobian = func; 12989b94acceSBarry Smith snes->jacP = ctx; 12999b94acceSBarry Smith snes->jacobian = A; 13009b94acceSBarry Smith snes->jacobian_pre = B; 13013a40ed3dSBarry Smith PetscFunctionReturn(0); 13029b94acceSBarry Smith } 13039b94acceSBarry Smith 13045615d1e5SSatish Balay #undef __FUNC__ 1305d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian" 130662fef451SLois Curfman McInnes /*@ 130762fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 130862fef451SLois Curfman McInnes provided context for evaluating the Hessian. 130962fef451SLois Curfman McInnes 1310c7afd0dbSLois Curfman McInnes Not Collective, but Mat object is parallel if SNES object is parallel 1311c7afd0dbSLois Curfman McInnes 131262fef451SLois Curfman McInnes Input Parameter: 131362fef451SLois Curfman McInnes . snes - the nonlinear solver context 131462fef451SLois Curfman McInnes 131562fef451SLois Curfman McInnes Output Parameters: 1316c7afd0dbSLois Curfman McInnes + A - location to stash Hessian matrix (or PETSC_NULL) 131762fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 1318c7afd0dbSLois Curfman McInnes - ctx - location to stash Hessian ctx (or PETSC_NULL) 1319fee21e36SBarry Smith 132036851e7fSLois Curfman McInnes Level: advanced 132136851e7fSLois Curfman McInnes 132262fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 1323c7afd0dbSLois Curfman McInnes 1324c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian 132562fef451SLois Curfman McInnes @*/ 132662fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B,void **ctx) 132762fef451SLois Curfman McInnes { 13283a40ed3dSBarry Smith PetscFunctionBegin; 1329184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 13303a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){ 1331a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 13323a40ed3dSBarry Smith } 133362fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 133462fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 133562fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 13363a40ed3dSBarry Smith PetscFunctionReturn(0); 133762fef451SLois Curfman McInnes } 133862fef451SLois Curfman McInnes 13399b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 13409b94acceSBarry Smith 13415615d1e5SSatish Balay #undef __FUNC__ 13425615d1e5SSatish Balay #define __FUNC__ "SNESSetUp" 13439b94acceSBarry Smith /*@ 13449b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1345272ac6f2SLois Curfman McInnes of a nonlinear solver. 13469b94acceSBarry Smith 1347fee21e36SBarry Smith Collective on SNES 1348fee21e36SBarry Smith 1349c7afd0dbSLois Curfman McInnes Input Parameters: 1350c7afd0dbSLois Curfman McInnes + snes - the SNES context 1351c7afd0dbSLois Curfman McInnes - x - the solution vector 1352c7afd0dbSLois Curfman McInnes 1353272ac6f2SLois Curfman McInnes Notes: 1354272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1355272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1356272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1357272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1358272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1359272ac6f2SLois Curfman McInnes 136036851e7fSLois Curfman McInnes Level: advanced 136136851e7fSLois Curfman McInnes 13629b94acceSBarry Smith .keywords: SNES, nonlinear, setup 13639b94acceSBarry Smith 13649b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 13659b94acceSBarry Smith @*/ 13668ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 13679b94acceSBarry Smith { 1368f1af5d2fSBarry Smith int ierr; 1369f1af5d2fSBarry Smith PetscTruth flg; 13703a40ed3dSBarry Smith 13713a40ed3dSBarry Smith PetscFunctionBegin; 137277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 137377c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1374184914b5SBarry Smith PetscCheckSameComm(snes,x); 13758ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 13769b94acceSBarry Smith 1377c1f60f51SBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1378c1f60f51SBarry Smith /* 1379c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1380dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1381c1f60f51SBarry Smith */ 1382c1f60f51SBarry Smith if (flg) { 1383c1f60f51SBarry Smith Mat J; 13845a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1385c1f60f51SBarry Smith PLogObjectParent(snes,J); 1386c1f60f51SBarry Smith snes->mfshell = J; 1387c1f60f51SBarry Smith snes->jacobian = J; 1388c2aafc4cSSatish Balay if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1389c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n"); 1390d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1391c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n"); 1392d4bb536fSBarry Smith } else { 1393a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option"); 1394d4bb536fSBarry Smith } 13955a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1396c1f60f51SBarry Smith } 1397272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1398c1f60f51SBarry Smith /* 1399dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1400c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1401c1f60f51SBarry Smith */ 140231615d04SLois Curfman McInnes if (flg) { 1403272ac6f2SLois Curfman McInnes Mat J; 14045a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1405272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 1406272ac6f2SLois Curfman McInnes snes->mfshell = J; 140783e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 140883e56afdSLois Curfman McInnes ierr = SNESSetJacobian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 140994a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 1410d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 141183e56afdSLois Curfman McInnes ierr = SNESSetHessian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 141294a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 1413d4bb536fSBarry Smith } else { 1414a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option"); 1415d4bb536fSBarry Smith } 14165a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1417272ac6f2SLois Curfman McInnes } 14189b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 14196831982aSBarry Smith PetscTruth iseqtr; 14206831982aSBarry Smith 1421a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 1422a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 14235a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 1424a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 1425a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector"); 1426a8c6a408SBarry Smith } 1427a703fe33SLois Curfman McInnes 1428a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 14296831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr); 14306831982aSBarry Smith if (snes->ksp_ewconv && !iseqtr) { 1431a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 1432a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 1433a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 14345a655dc6SBarry Smith ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr); 1435a703fe33SLois Curfman McInnes } 14369b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 1437a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1438a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1439a8c6a408SBarry Smith if (!snes->computeumfunction) { 1440a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first"); 1441a8c6a408SBarry Smith } 14425a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()"); 1443d4bb536fSBarry Smith } else { 1444a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class"); 1445d4bb536fSBarry Smith } 1446a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);} 144782bf6240SBarry Smith snes->setupcalled = 1; 14483a40ed3dSBarry Smith PetscFunctionReturn(0); 14499b94acceSBarry Smith } 14509b94acceSBarry Smith 14515615d1e5SSatish Balay #undef __FUNC__ 1452d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy" 14539b94acceSBarry Smith /*@C 14549b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 14559b94acceSBarry Smith with SNESCreate(). 14569b94acceSBarry Smith 1457c7afd0dbSLois Curfman McInnes Collective on SNES 1458c7afd0dbSLois Curfman McInnes 14599b94acceSBarry Smith Input Parameter: 14609b94acceSBarry Smith . snes - the SNES context 14619b94acceSBarry Smith 146236851e7fSLois Curfman McInnes Level: beginner 146336851e7fSLois Curfman McInnes 14649b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 14659b94acceSBarry Smith 146663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 14679b94acceSBarry Smith @*/ 14689b94acceSBarry Smith int SNESDestroy(SNES snes) 14699b94acceSBarry Smith { 1470b8a78c4aSBarry Smith int i,ierr; 14713a40ed3dSBarry Smith 14723a40ed3dSBarry Smith PetscFunctionBegin; 147377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14743a40ed3dSBarry Smith if (--snes->refct > 0) PetscFunctionReturn(0); 1475d4bb536fSBarry Smith 1476be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 14770f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1478be0abb6dSBarry Smith 1479e1311b90SBarry Smith if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);} 1480606d414cSSatish Balay if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);} 1481522c5e43SBarry Smith if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);} 14829b94acceSBarry Smith ierr = SLESDestroy(snes->sles);CHKERRQ(ierr); 1483522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1484b8a78c4aSBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1485b8a78c4aSBarry Smith if (snes->monitordestroy[i]) { 1486b8a78c4aSBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1487b8a78c4aSBarry Smith } 1488b8a78c4aSBarry Smith } 14899b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 14900452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 14913a40ed3dSBarry Smith PetscFunctionReturn(0); 14929b94acceSBarry Smith } 14939b94acceSBarry Smith 14949b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 14959b94acceSBarry Smith 14965615d1e5SSatish Balay #undef __FUNC__ 14975615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances" 14989b94acceSBarry Smith /*@ 1499d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 15009b94acceSBarry Smith 1501c7afd0dbSLois Curfman McInnes Collective on SNES 1502c7afd0dbSLois Curfman McInnes 15039b94acceSBarry Smith Input Parameters: 1504c7afd0dbSLois Curfman McInnes + snes - the SNES context 150533174efeSLois Curfman McInnes . atol - absolute convergence tolerance 150633174efeSLois Curfman McInnes . rtol - relative convergence tolerance 150733174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 150833174efeSLois Curfman McInnes of the change in the solution between steps 150933174efeSLois Curfman McInnes . maxit - maximum number of iterations 1510c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1511fee21e36SBarry Smith 151233174efeSLois Curfman McInnes Options Database Keys: 1513c7afd0dbSLois Curfman McInnes + -snes_atol <atol> - Sets atol 1514c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1515c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1516c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1517c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15189b94acceSBarry Smith 1519d7a720efSLois Curfman McInnes Notes: 15209b94acceSBarry Smith The default maximum number of iterations is 50. 15219b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15229b94acceSBarry Smith 152336851e7fSLois Curfman McInnes Level: intermediate 152436851e7fSLois Curfman McInnes 152533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15269b94acceSBarry Smith 1527d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 15289b94acceSBarry Smith @*/ 1529*329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf) 15309b94acceSBarry Smith { 15313a40ed3dSBarry Smith PetscFunctionBegin; 153277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1533d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1534d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1535d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1536d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1537d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15383a40ed3dSBarry Smith PetscFunctionReturn(0); 15399b94acceSBarry Smith } 15409b94acceSBarry Smith 15415615d1e5SSatish Balay #undef __FUNC__ 15425615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances" 15439b94acceSBarry Smith /*@ 154433174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 154533174efeSLois Curfman McInnes 1546c7afd0dbSLois Curfman McInnes Not Collective 1547c7afd0dbSLois Curfman McInnes 154833174efeSLois Curfman McInnes Input Parameters: 1549c7afd0dbSLois Curfman McInnes + snes - the SNES context 155033174efeSLois Curfman McInnes . atol - absolute convergence tolerance 155133174efeSLois Curfman McInnes . rtol - relative convergence tolerance 155233174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 155333174efeSLois Curfman McInnes of the change in the solution between steps 155433174efeSLois Curfman McInnes . maxit - maximum number of iterations 1555c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1556fee21e36SBarry Smith 155733174efeSLois Curfman McInnes Notes: 155833174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 155933174efeSLois Curfman McInnes 156036851e7fSLois Curfman McInnes Level: intermediate 156136851e7fSLois Curfman McInnes 156233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 156333174efeSLois Curfman McInnes 156433174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 156533174efeSLois Curfman McInnes @*/ 1566*329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf) 156733174efeSLois Curfman McInnes { 15683a40ed3dSBarry Smith PetscFunctionBegin; 156933174efeSLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 157033174efeSLois Curfman McInnes if (atol) *atol = snes->atol; 157133174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 157233174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 157333174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 157433174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 15753a40ed3dSBarry Smith PetscFunctionReturn(0); 157633174efeSLois Curfman McInnes } 157733174efeSLois Curfman McInnes 15785615d1e5SSatish Balay #undef __FUNC__ 15795615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance" 158033174efeSLois Curfman McInnes /*@ 15819b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 15829b94acceSBarry Smith 1583fee21e36SBarry Smith Collective on SNES 1584fee21e36SBarry Smith 1585c7afd0dbSLois Curfman McInnes Input Parameters: 1586c7afd0dbSLois Curfman McInnes + snes - the SNES context 1587c7afd0dbSLois Curfman McInnes - tol - tolerance 1588c7afd0dbSLois Curfman McInnes 15899b94acceSBarry Smith Options Database Key: 1590c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 15919b94acceSBarry Smith 159236851e7fSLois Curfman McInnes Level: intermediate 159336851e7fSLois Curfman McInnes 15949b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 15959b94acceSBarry Smith 1596d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 15979b94acceSBarry Smith @*/ 1598*329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 15999b94acceSBarry Smith { 16003a40ed3dSBarry Smith PetscFunctionBegin; 160177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16029b94acceSBarry Smith snes->deltatol = tol; 16033a40ed3dSBarry Smith PetscFunctionReturn(0); 16049b94acceSBarry Smith } 16059b94acceSBarry Smith 16065615d1e5SSatish Balay #undef __FUNC__ 16075615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance" 16089b94acceSBarry Smith /*@ 160977c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 16109b94acceSBarry Smith for unconstrained minimization solvers. 16119b94acceSBarry Smith 1612fee21e36SBarry Smith Collective on SNES 1613fee21e36SBarry Smith 1614c7afd0dbSLois Curfman McInnes Input Parameters: 1615c7afd0dbSLois Curfman McInnes + snes - the SNES context 1616c7afd0dbSLois Curfman McInnes - ftol - minimum function tolerance 1617c7afd0dbSLois Curfman McInnes 16189b94acceSBarry Smith Options Database Key: 1619c7afd0dbSLois Curfman McInnes . -snes_fmin <ftol> - Sets ftol 16209b94acceSBarry Smith 16219b94acceSBarry Smith Note: 162277c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 16239b94acceSBarry Smith methods only. 16249b94acceSBarry Smith 162536851e7fSLois Curfman McInnes Level: intermediate 162636851e7fSLois Curfman McInnes 16279b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 16289b94acceSBarry Smith 1629d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 16309b94acceSBarry Smith @*/ 1631*329f5518SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,PetscReal ftol) 16329b94acceSBarry Smith { 16333a40ed3dSBarry Smith PetscFunctionBegin; 163477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16359b94acceSBarry Smith snes->fmin = ftol; 16363a40ed3dSBarry Smith PetscFunctionReturn(0); 16379b94acceSBarry Smith } 1638df9fa365SBarry Smith /* 1639df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1640df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1641df9fa365SBarry Smith macros instead of functions 1642df9fa365SBarry Smith */ 1643ce1608b8SBarry Smith #undef __FUNC__ 1644ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor" 1645*329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx) 1646ce1608b8SBarry Smith { 1647ce1608b8SBarry Smith int ierr; 1648ce1608b8SBarry Smith 1649ce1608b8SBarry Smith PetscFunctionBegin; 1650184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1651ce1608b8SBarry Smith ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1652ce1608b8SBarry Smith PetscFunctionReturn(0); 1653ce1608b8SBarry Smith } 1654ce1608b8SBarry Smith 1655df9fa365SBarry Smith #undef __FUNC__ 1656df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorCreate" 1657df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,DrawLG *draw) 1658df9fa365SBarry Smith { 1659df9fa365SBarry Smith int ierr; 1660df9fa365SBarry Smith 1661df9fa365SBarry Smith PetscFunctionBegin; 1662df9fa365SBarry Smith ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1663df9fa365SBarry Smith PetscFunctionReturn(0); 1664df9fa365SBarry Smith } 1665df9fa365SBarry Smith 1666df9fa365SBarry Smith #undef __FUNC__ 1667df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorDestroy" 1668df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw) 1669df9fa365SBarry Smith { 1670df9fa365SBarry Smith int ierr; 1671df9fa365SBarry Smith 1672df9fa365SBarry Smith PetscFunctionBegin; 1673df9fa365SBarry Smith ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr); 1674df9fa365SBarry Smith PetscFunctionReturn(0); 1675df9fa365SBarry Smith } 1676df9fa365SBarry Smith 16779b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 16789b94acceSBarry Smith 16795615d1e5SSatish Balay #undef __FUNC__ 1680d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor" 16819b94acceSBarry Smith /*@C 16825cd90555SBarry Smith SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every 16839b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 16849b94acceSBarry Smith progress. 16859b94acceSBarry Smith 1686fee21e36SBarry Smith Collective on SNES 1687fee21e36SBarry Smith 1688c7afd0dbSLois Curfman McInnes Input Parameters: 1689c7afd0dbSLois Curfman McInnes + snes - the SNES context 1690c7afd0dbSLois Curfman McInnes . func - monitoring routine 1691b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1692c7afd0dbSLois Curfman McInnes monitor routine (may be PETSC_NULL) 1693b8a78c4aSBarry Smith - monitordestroy - options routine that frees monitor context 16949b94acceSBarry Smith 1695c7afd0dbSLois Curfman McInnes Calling sequence of func: 1696*329f5518SBarry Smith $ int func(SNES snes,int its, PetscReal norm,void *mctx) 1697c7afd0dbSLois Curfman McInnes 1698c7afd0dbSLois Curfman McInnes + snes - the SNES context 1699c7afd0dbSLois Curfman McInnes . its - iteration number 1700c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 1701c7afd0dbSLois Curfman McInnes for SNES_NONLINEAR_EQUATIONS methods 170240a0c1c6SLois Curfman McInnes . norm - 2-norm gradient value (may be estimated) 1703c7afd0dbSLois Curfman McInnes for SNES_UNCONSTRAINED_MINIMIZATION methods 170440a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 17059b94acceSBarry Smith 17069665c990SLois Curfman McInnes Options Database Keys: 1707c7afd0dbSLois Curfman McInnes + -snes_monitor - sets SNESDefaultMonitor() 1708c7afd0dbSLois Curfman McInnes . -snes_xmonitor - sets line graph monitor, 1709c7afd0dbSLois Curfman McInnes uses SNESLGMonitorCreate() 1710c7afd0dbSLois Curfman McInnes _ -snes_cancelmonitors - cancels all monitors that have 1711c7afd0dbSLois Curfman McInnes been hardwired into a code by 1712c7afd0dbSLois Curfman McInnes calls to SNESSetMonitor(), but 1713c7afd0dbSLois Curfman McInnes does not cancel those set via 1714c7afd0dbSLois Curfman McInnes the options database. 17159665c990SLois Curfman McInnes 1716639f9d9dSBarry Smith Notes: 17176bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 17186bc08f3fSLois Curfman McInnes SNESSetMonitor() multiple times; all will be called in the 17196bc08f3fSLois Curfman McInnes order in which they were set. 1720639f9d9dSBarry Smith 172136851e7fSLois Curfman McInnes Level: intermediate 172236851e7fSLois Curfman McInnes 17239b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 17249b94acceSBarry Smith 17255cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor() 17269b94acceSBarry Smith @*/ 1727*329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *)) 17289b94acceSBarry Smith { 17293a40ed3dSBarry Smith PetscFunctionBegin; 1730184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1731639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 1732a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set"); 1733639f9d9dSBarry Smith } 1734639f9d9dSBarry Smith 1735639f9d9dSBarry Smith snes->monitor[snes->numbermonitors] = func; 1736b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1737639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 17383a40ed3dSBarry Smith PetscFunctionReturn(0); 17399b94acceSBarry Smith } 17409b94acceSBarry Smith 17415615d1e5SSatish Balay #undef __FUNC__ 17425cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor" 17435cd90555SBarry Smith /*@C 17445cd90555SBarry Smith SNESClearMonitor - Clears all the monitor functions for a SNES object. 17455cd90555SBarry Smith 1746c7afd0dbSLois Curfman McInnes Collective on SNES 1747c7afd0dbSLois Curfman McInnes 17485cd90555SBarry Smith Input Parameters: 17495cd90555SBarry Smith . snes - the SNES context 17505cd90555SBarry Smith 17515cd90555SBarry Smith Options Database: 1752c7afd0dbSLois Curfman McInnes . -snes_cancelmonitors - cancels all monitors that have been hardwired 1753c7afd0dbSLois Curfman McInnes into a code by calls to SNESSetMonitor(), but does not cancel those 1754c7afd0dbSLois Curfman McInnes set via the options database 17555cd90555SBarry Smith 17565cd90555SBarry Smith Notes: 17575cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 17585cd90555SBarry Smith 175936851e7fSLois Curfman McInnes Level: intermediate 176036851e7fSLois Curfman McInnes 17615cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 17625cd90555SBarry Smith 17635cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor() 17645cd90555SBarry Smith @*/ 17655cd90555SBarry Smith int SNESClearMonitor(SNES snes) 17665cd90555SBarry Smith { 17675cd90555SBarry Smith PetscFunctionBegin; 1768184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 17695cd90555SBarry Smith snes->numbermonitors = 0; 17705cd90555SBarry Smith PetscFunctionReturn(0); 17715cd90555SBarry Smith } 17725cd90555SBarry Smith 17735cd90555SBarry Smith #undef __FUNC__ 1774d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest" 17759b94acceSBarry Smith /*@C 17769b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 17779b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 17789b94acceSBarry Smith 1779fee21e36SBarry Smith Collective on SNES 1780fee21e36SBarry Smith 1781c7afd0dbSLois Curfman McInnes Input Parameters: 1782c7afd0dbSLois Curfman McInnes + snes - the SNES context 1783c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1784c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1785c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 17869b94acceSBarry Smith 1787c7afd0dbSLois Curfman McInnes Calling sequence of func: 1788*329f5518SBarry Smith $ int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1789c7afd0dbSLois Curfman McInnes 1790c7afd0dbSLois Curfman McInnes + snes - the SNES context 1791c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1792184914b5SBarry Smith . reason - reason for convergence/divergence 1793c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 1794c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods) 1795c7afd0dbSLois Curfman McInnes . f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods) 1796c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods) 1797c7afd0dbSLois Curfman McInnes - f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods) 17989b94acceSBarry Smith 179936851e7fSLois Curfman McInnes Level: advanced 180036851e7fSLois Curfman McInnes 18019b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 18029b94acceSBarry Smith 180340191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 180440191667SLois Curfman McInnes SNESConverged_UM_LS(), SNESConverged_UM_TR() 18059b94acceSBarry Smith @*/ 1806*329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx) 18079b94acceSBarry Smith { 18083a40ed3dSBarry Smith PetscFunctionBegin; 1809184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 18109b94acceSBarry Smith (snes)->converged = func; 18119b94acceSBarry Smith (snes)->cnvP = cctx; 18123a40ed3dSBarry Smith PetscFunctionReturn(0); 18139b94acceSBarry Smith } 18149b94acceSBarry Smith 18155615d1e5SSatish Balay #undef __FUNC__ 1816184914b5SBarry Smith #define __FUNC__ "SNESGetConvergedReason" 1817184914b5SBarry Smith /*@C 1818184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1819184914b5SBarry Smith 1820184914b5SBarry Smith Not Collective 1821184914b5SBarry Smith 1822184914b5SBarry Smith Input Parameter: 1823184914b5SBarry Smith . snes - the SNES context 1824184914b5SBarry Smith 1825184914b5SBarry Smith Output Parameter: 1826184914b5SBarry Smith . reason - negative value indicates diverged, positive value converged, see snes.h or the 1827184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1828184914b5SBarry Smith 1829184914b5SBarry Smith Level: intermediate 1830184914b5SBarry Smith 1831184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1832184914b5SBarry Smith 1833184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1834184914b5SBarry Smith 1835184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 1836184914b5SBarry Smith SNESConverged_UM_LS(), SNESConverged_UM_TR() 1837184914b5SBarry Smith @*/ 1838184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1839184914b5SBarry Smith { 1840184914b5SBarry Smith PetscFunctionBegin; 1841184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1842184914b5SBarry Smith *reason = snes->reason; 1843184914b5SBarry Smith PetscFunctionReturn(0); 1844184914b5SBarry Smith } 1845184914b5SBarry Smith 1846184914b5SBarry Smith #undef __FUNC__ 1847d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory" 1848c9005455SLois Curfman McInnes /*@ 1849c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1850c9005455SLois Curfman McInnes 1851fee21e36SBarry Smith Collective on SNES 1852fee21e36SBarry Smith 1853c7afd0dbSLois Curfman McInnes Input Parameters: 1854c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1855c7afd0dbSLois Curfman McInnes . a - array to hold history 1856758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1857758f92a0SBarry Smith negative if not converged) for each solve. 1858758f92a0SBarry Smith . na - size of a and its 1859758f92a0SBarry Smith - reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero, 1860758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1861c7afd0dbSLois Curfman McInnes 1862c9005455SLois Curfman McInnes Notes: 1863c9005455SLois Curfman McInnes If set, this array will contain the function norms (for 1864c9005455SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods) or gradient norms 1865c9005455SLois Curfman McInnes (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed 1866c9005455SLois Curfman McInnes at each step. 1867c9005455SLois Curfman McInnes 1868c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1869c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1870c9005455SLois Curfman McInnes during the section of code that is being timed. 1871c9005455SLois Curfman McInnes 187236851e7fSLois Curfman McInnes Level: intermediate 187336851e7fSLois Curfman McInnes 1874c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1875758f92a0SBarry Smith 187608405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1877758f92a0SBarry Smith 1878c9005455SLois Curfman McInnes @*/ 1879*329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset) 1880c9005455SLois Curfman McInnes { 18813a40ed3dSBarry Smith PetscFunctionBegin; 1882c9005455SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 1883c9005455SLois Curfman McInnes if (na) PetscValidScalarPointer(a); 1884c9005455SLois Curfman McInnes snes->conv_hist = a; 1885758f92a0SBarry Smith snes->conv_hist_its = its; 1886758f92a0SBarry Smith snes->conv_hist_max = na; 1887758f92a0SBarry Smith snes->conv_hist_reset = reset; 1888758f92a0SBarry Smith PetscFunctionReturn(0); 1889758f92a0SBarry Smith } 1890758f92a0SBarry Smith 1891758f92a0SBarry Smith #undef __FUNC__ 1892758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory" 18930c4c9dddSBarry Smith /*@C 1894758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1895758f92a0SBarry Smith 1896758f92a0SBarry Smith Collective on SNES 1897758f92a0SBarry Smith 1898758f92a0SBarry Smith Input Parameter: 1899758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1900758f92a0SBarry Smith 1901758f92a0SBarry Smith Output Parameters: 1902758f92a0SBarry Smith . a - array to hold history 1903758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1904758f92a0SBarry Smith negative if not converged) for each solve. 1905758f92a0SBarry Smith - na - size of a and its 1906758f92a0SBarry Smith 1907758f92a0SBarry Smith Notes: 1908758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1909758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1910758f92a0SBarry Smith 1911758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1912758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1913758f92a0SBarry Smith during the section of code that is being timed. 1914758f92a0SBarry Smith 1915758f92a0SBarry Smith Level: intermediate 1916758f92a0SBarry Smith 1917758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1918758f92a0SBarry Smith 1919758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1920758f92a0SBarry Smith 1921758f92a0SBarry Smith @*/ 1922*329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na) 1923758f92a0SBarry Smith { 1924758f92a0SBarry Smith PetscFunctionBegin; 1925758f92a0SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1926758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1927758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1928758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 19293a40ed3dSBarry Smith PetscFunctionReturn(0); 1930c9005455SLois Curfman McInnes } 1931c9005455SLois Curfman McInnes 1932c9005455SLois Curfman McInnes #undef __FUNC__ 19335615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private" 19349b94acceSBarry Smith /* 19359b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 19369b94acceSBarry Smith positive parameter delta. 19379b94acceSBarry Smith 19389b94acceSBarry Smith Input Parameters: 1939c7afd0dbSLois Curfman McInnes + snes - the SNES context 19409b94acceSBarry Smith . y - approximate solution of linear system 19419b94acceSBarry Smith . fnorm - 2-norm of current function 1942c7afd0dbSLois Curfman McInnes - delta - trust region size 19439b94acceSBarry Smith 19449b94acceSBarry Smith Output Parameters: 1945c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 19469b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 19479b94acceSBarry Smith region, and exceeds zero otherwise. 1948c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 19499b94acceSBarry Smith 19509b94acceSBarry Smith Note: 19516831982aSBarry Smith For non-trust region methods such as SNESEQLS, the parameter delta 19529b94acceSBarry Smith is set to be the maximum allowable step size. 19539b94acceSBarry Smith 19549b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 19559b94acceSBarry Smith */ 1956*329f5518SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta, 1957*329f5518SBarry Smith PetscReal *gpnorm,PetscReal *ynorm) 19589b94acceSBarry Smith { 1959*329f5518SBarry Smith PetscReal norm; 19609b94acceSBarry Smith Scalar cnorm; 19613a40ed3dSBarry Smith int ierr; 19623a40ed3dSBarry Smith 19633a40ed3dSBarry Smith PetscFunctionBegin; 1964184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1965184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1966184914b5SBarry Smith PetscCheckSameComm(snes,y); 1967184914b5SBarry Smith 19683a40ed3dSBarry Smith ierr = VecNorm(y,NORM_2,&norm);CHKERRQ(ierr); 19699b94acceSBarry Smith if (norm > *delta) { 19709b94acceSBarry Smith norm = *delta/norm; 19719b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 19729b94acceSBarry Smith cnorm = norm; 1973*329f5518SBarry Smith ierr = VecScale(&cnorm,y);CHKERRQ(ierr); 19749b94acceSBarry Smith *ynorm = *delta; 19759b94acceSBarry Smith } else { 19769b94acceSBarry Smith *gpnorm = 0.0; 19779b94acceSBarry Smith *ynorm = norm; 19789b94acceSBarry Smith } 19793a40ed3dSBarry Smith PetscFunctionReturn(0); 19809b94acceSBarry Smith } 19819b94acceSBarry Smith 19825615d1e5SSatish Balay #undef __FUNC__ 19835615d1e5SSatish Balay #define __FUNC__ "SNESSolve" 19849b94acceSBarry Smith /*@ 19859b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 198663a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 19879b94acceSBarry Smith 1988c7afd0dbSLois Curfman McInnes Collective on SNES 1989c7afd0dbSLois Curfman McInnes 1990b2002411SLois Curfman McInnes Input Parameters: 1991c7afd0dbSLois Curfman McInnes + snes - the SNES context 1992c7afd0dbSLois Curfman McInnes - x - the solution vector 19939b94acceSBarry Smith 19949b94acceSBarry Smith Output Parameter: 1995b2002411SLois Curfman McInnes . its - number of iterations until termination 19969b94acceSBarry Smith 1997b2002411SLois Curfman McInnes Notes: 19988ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 19998ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 20008ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 20018ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 20028ddd3da0SLois Curfman McInnes 200336851e7fSLois Curfman McInnes Level: beginner 200436851e7fSLois Curfman McInnes 20059b94acceSBarry Smith .keywords: SNES, nonlinear, solve 20069b94acceSBarry Smith 200763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 20089b94acceSBarry Smith @*/ 20098ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 20109b94acceSBarry Smith { 2011f1af5d2fSBarry Smith int ierr; 2012f1af5d2fSBarry Smith PetscTruth flg; 2013052efed2SBarry Smith 20143a40ed3dSBarry Smith PetscFunctionBegin; 201577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2016184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 2017184914b5SBarry Smith PetscCheckSameComm(snes,x); 201874679c65SBarry Smith PetscValidIntPointer(its); 201982bf6240SBarry Smith if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);} 2020c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 2021758f92a0SBarry Smith if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0; 20229b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 2023c96a6f78SLois Curfman McInnes snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0; 20249b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr); 20259b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 20260f5bd95cSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr); 20276d4a8577SBarry Smith if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } 20283a40ed3dSBarry Smith PetscFunctionReturn(0); 20299b94acceSBarry Smith } 20309b94acceSBarry Smith 20319b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 20329b94acceSBarry Smith 20335615d1e5SSatish Balay #undef __FUNC__ 20345615d1e5SSatish Balay #define __FUNC__ "SNESSetType" 203582bf6240SBarry Smith /*@C 20364b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 20379b94acceSBarry Smith 2038fee21e36SBarry Smith Collective on SNES 2039fee21e36SBarry Smith 2040c7afd0dbSLois Curfman McInnes Input Parameters: 2041c7afd0dbSLois Curfman McInnes + snes - the SNES context 2042454a90a3SBarry Smith - type - a known method 2043c7afd0dbSLois Curfman McInnes 2044c7afd0dbSLois Curfman McInnes Options Database Key: 2045454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2046c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2047ae12b187SLois Curfman McInnes 20489b94acceSBarry Smith Notes: 20499b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 20506831982aSBarry Smith + SNESEQLS - Newton's method with line search 2051c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20526831982aSBarry Smith . SNESEQTR - Newton's method with trust region 2053c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20546831982aSBarry Smith . SNESUMTR - Newton's method with trust region 2055c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20566831982aSBarry Smith - SNESUMLS - Newton's method with line search 2057c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20589b94acceSBarry Smith 2059ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2060ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2061ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2062ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2063ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2064ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2065ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2066ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2067ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 206836851e7fSLois Curfman McInnes appropriate method. In other words, this routine is not for beginners. 206936851e7fSLois Curfman McInnes 207036851e7fSLois Curfman McInnes Level: intermediate 2071a703fe33SLois Curfman McInnes 2072454a90a3SBarry Smith .keywords: SNES, set, type 20739b94acceSBarry Smith @*/ 2074454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type) 20759b94acceSBarry Smith { 20760f5bd95cSBarry Smith int ierr,(*r)(SNES); 20776831982aSBarry Smith PetscTruth match; 20783a40ed3dSBarry Smith 20793a40ed3dSBarry Smith PetscFunctionBegin; 208077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 20810f5bd95cSBarry Smith PetscValidCharPointer(type); 208282bf6240SBarry Smith 20836831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 20840f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 208582bf6240SBarry Smith 208682bf6240SBarry Smith if (snes->setupcalled) { 2087e1311b90SBarry Smith ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr); 208882bf6240SBarry Smith snes->data = 0; 208982bf6240SBarry Smith } 20907d1a2b2bSBarry Smith 20919b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 209282bf6240SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 209382bf6240SBarry Smith 2094454a90a3SBarry Smith ierr = FListFind(snes->comm,SNESList,type,(int (**)(void *)) &r);CHKERRQ(ierr); 209582bf6240SBarry Smith 2096454a90a3SBarry Smith if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",type); 20971548b14aSBarry Smith 2098606d414cSSatish Balay if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);} 209982bf6240SBarry Smith snes->data = 0; 21003a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 210182bf6240SBarry Smith 2102454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 210382bf6240SBarry Smith snes->set_method_called = 1; 210482bf6240SBarry Smith 21053a40ed3dSBarry Smith PetscFunctionReturn(0); 21069b94acceSBarry Smith } 21079b94acceSBarry Smith 2108a847f771SSatish Balay 21099b94acceSBarry Smith /* --------------------------------------------------------------------- */ 21105615d1e5SSatish Balay #undef __FUNC__ 2111d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy" 21129b94acceSBarry Smith /*@C 21139b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2114f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 21159b94acceSBarry Smith 2116fee21e36SBarry Smith Not Collective 2117fee21e36SBarry Smith 211836851e7fSLois Curfman McInnes Level: advanced 211936851e7fSLois Curfman McInnes 21209b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 21219b94acceSBarry Smith 21229b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 21239b94acceSBarry Smith @*/ 2124cf256101SBarry Smith int SNESRegisterDestroy(void) 21259b94acceSBarry Smith { 212682bf6240SBarry Smith int ierr; 212782bf6240SBarry Smith 21283a40ed3dSBarry Smith PetscFunctionBegin; 212982bf6240SBarry Smith if (SNESList) { 2130488ecbafSBarry Smith ierr = FListDestroy(SNESList);CHKERRQ(ierr); 213182bf6240SBarry Smith SNESList = 0; 21329b94acceSBarry Smith } 213384cb2905SBarry Smith SNESRegisterAllCalled = 0; 21343a40ed3dSBarry Smith PetscFunctionReturn(0); 21359b94acceSBarry Smith } 21369b94acceSBarry Smith 21375615d1e5SSatish Balay #undef __FUNC__ 2138d4bb536fSBarry Smith #define __FUNC__ "SNESGetType" 21399b94acceSBarry Smith /*@C 21409a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 21419b94acceSBarry Smith 2142c7afd0dbSLois Curfman McInnes Not Collective 2143c7afd0dbSLois Curfman McInnes 21449b94acceSBarry Smith Input Parameter: 21454b0e389bSBarry Smith . snes - nonlinear solver context 21469b94acceSBarry Smith 21479b94acceSBarry Smith Output Parameter: 2148454a90a3SBarry Smith . type - SNES method (a charactor string) 21499b94acceSBarry Smith 215036851e7fSLois Curfman McInnes Level: intermediate 215136851e7fSLois Curfman McInnes 2152454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 21539b94acceSBarry Smith @*/ 2154454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type) 21559b94acceSBarry Smith { 21563a40ed3dSBarry Smith PetscFunctionBegin; 2157184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2158454a90a3SBarry Smith *type = snes->type_name; 21593a40ed3dSBarry Smith PetscFunctionReturn(0); 21609b94acceSBarry Smith } 21619b94acceSBarry Smith 21625615d1e5SSatish Balay #undef __FUNC__ 2163d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution" 21649b94acceSBarry Smith /*@C 21659b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 21669b94acceSBarry Smith stored. 21679b94acceSBarry Smith 2168c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2169c7afd0dbSLois Curfman McInnes 21709b94acceSBarry Smith Input Parameter: 21719b94acceSBarry Smith . snes - the SNES context 21729b94acceSBarry Smith 21739b94acceSBarry Smith Output Parameter: 21749b94acceSBarry Smith . x - the solution 21759b94acceSBarry Smith 217636851e7fSLois Curfman McInnes Level: advanced 217736851e7fSLois Curfman McInnes 21789b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 21799b94acceSBarry Smith 2180a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 21819b94acceSBarry Smith @*/ 21829b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 21839b94acceSBarry Smith { 21843a40ed3dSBarry Smith PetscFunctionBegin; 218577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 21869b94acceSBarry Smith *x = snes->vec_sol_always; 21873a40ed3dSBarry Smith PetscFunctionReturn(0); 21889b94acceSBarry Smith } 21899b94acceSBarry Smith 21905615d1e5SSatish Balay #undef __FUNC__ 2191d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate" 21929b94acceSBarry Smith /*@C 21939b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 21949b94acceSBarry Smith stored. 21959b94acceSBarry Smith 2196c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2197c7afd0dbSLois Curfman McInnes 21989b94acceSBarry Smith Input Parameter: 21999b94acceSBarry Smith . snes - the SNES context 22009b94acceSBarry Smith 22019b94acceSBarry Smith Output Parameter: 22029b94acceSBarry Smith . x - the solution update 22039b94acceSBarry Smith 220436851e7fSLois Curfman McInnes Level: advanced 220536851e7fSLois Curfman McInnes 22069b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 22079b94acceSBarry Smith 22089b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 22099b94acceSBarry Smith @*/ 22109b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 22119b94acceSBarry Smith { 22123a40ed3dSBarry Smith PetscFunctionBegin; 221377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22149b94acceSBarry Smith *x = snes->vec_sol_update_always; 22153a40ed3dSBarry Smith PetscFunctionReturn(0); 22169b94acceSBarry Smith } 22179b94acceSBarry Smith 22185615d1e5SSatish Balay #undef __FUNC__ 2219d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction" 22209b94acceSBarry Smith /*@C 22213638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 22229b94acceSBarry Smith 2223c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2224c7afd0dbSLois Curfman McInnes 22259b94acceSBarry Smith Input Parameter: 22269b94acceSBarry Smith . snes - the SNES context 22279b94acceSBarry Smith 22289b94acceSBarry Smith Output Parameter: 22297bf4e008SBarry Smith + r - the function (or PETSC_NULL) 22307bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 22319b94acceSBarry Smith 22329b94acceSBarry Smith Notes: 22339b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 22349b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 22359b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 22369b94acceSBarry Smith 223736851e7fSLois Curfman McInnes Level: advanced 223836851e7fSLois Curfman McInnes 2239a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 22409b94acceSBarry Smith 224131615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 224231615d04SLois Curfman McInnes SNESGetGradient() 22437bf4e008SBarry Smith 22449b94acceSBarry Smith @*/ 22457bf4e008SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx) 22469b94acceSBarry Smith { 22473a40ed3dSBarry Smith PetscFunctionBegin; 224877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2249a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 2250a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 2251a8c6a408SBarry Smith } 22527bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22537bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22543a40ed3dSBarry Smith PetscFunctionReturn(0); 22559b94acceSBarry Smith } 22569b94acceSBarry Smith 22575615d1e5SSatish Balay #undef __FUNC__ 2258d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient" 22599b94acceSBarry Smith /*@C 22603638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 22619b94acceSBarry Smith 2262c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2263c7afd0dbSLois Curfman McInnes 22649b94acceSBarry Smith Input Parameter: 22659b94acceSBarry Smith . snes - the SNES context 22669b94acceSBarry Smith 22679b94acceSBarry Smith Output Parameter: 22687bf4e008SBarry Smith + r - the gradient (or PETSC_NULL) 22697bf4e008SBarry Smith - ctx - the gradient context (or PETSC_NULL) 22709b94acceSBarry Smith 22719b94acceSBarry Smith Notes: 22729b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 22739b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 22749b94acceSBarry Smith SNESGetFunction(). 22759b94acceSBarry Smith 227636851e7fSLois Curfman McInnes Level: advanced 227736851e7fSLois Curfman McInnes 22789b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 22799b94acceSBarry Smith 22807bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(), 22817bf4e008SBarry Smith SNESSetGradient(), SNESSetFunction() 22827bf4e008SBarry Smith 22839b94acceSBarry Smith @*/ 22847bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx) 22859b94acceSBarry Smith { 22863a40ed3dSBarry Smith PetscFunctionBegin; 228777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22883a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2289a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 22903a40ed3dSBarry Smith } 22917bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22927bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22933a40ed3dSBarry Smith PetscFunctionReturn(0); 22949b94acceSBarry Smith } 22959b94acceSBarry Smith 22965615d1e5SSatish Balay #undef __FUNC__ 2297d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction" 22987bf4e008SBarry Smith /*@C 22999b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 23009b94acceSBarry Smith unconstrained minimization problems. 23019b94acceSBarry Smith 2302c7afd0dbSLois Curfman McInnes Not Collective 2303c7afd0dbSLois Curfman McInnes 23049b94acceSBarry Smith Input Parameter: 23059b94acceSBarry Smith . snes - the SNES context 23069b94acceSBarry Smith 23079b94acceSBarry Smith Output Parameter: 23087bf4e008SBarry Smith + r - the function (or PETSC_NULL) 23097bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 23109b94acceSBarry Smith 23119b94acceSBarry Smith Notes: 23129b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 23139b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 23149b94acceSBarry Smith SNESGetFunction(). 23159b94acceSBarry Smith 231636851e7fSLois Curfman McInnes Level: advanced 231736851e7fSLois Curfman McInnes 23189b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 23199b94acceSBarry Smith 23207bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction() 23217bf4e008SBarry Smith 23229b94acceSBarry Smith @*/ 2323*329f5518SBarry Smith int SNESGetMinimizationFunction(SNES snes,PetscReal *r,void **ctx) 23249b94acceSBarry Smith { 23253a40ed3dSBarry Smith PetscFunctionBegin; 232677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 232774679c65SBarry Smith PetscValidScalarPointer(r); 23283a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2329a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 23303a40ed3dSBarry Smith } 23317bf4e008SBarry Smith if (r) *r = snes->fc; 23327bf4e008SBarry Smith if (ctx) *ctx = snes->umfunP; 23333a40ed3dSBarry Smith PetscFunctionReturn(0); 23349b94acceSBarry Smith } 23359b94acceSBarry Smith 23365615d1e5SSatish Balay #undef __FUNC__ 2337d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix" 23383c7409f5SSatish Balay /*@C 23393c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2340d850072dSLois Curfman McInnes SNES options in the database. 23413c7409f5SSatish Balay 2342fee21e36SBarry Smith Collective on SNES 2343fee21e36SBarry Smith 2344c7afd0dbSLois Curfman McInnes Input Parameter: 2345c7afd0dbSLois Curfman McInnes + snes - the SNES context 2346c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2347c7afd0dbSLois Curfman McInnes 2348d850072dSLois Curfman McInnes Notes: 2349a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2350c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2351d850072dSLois Curfman McInnes 235236851e7fSLois Curfman McInnes Level: advanced 235336851e7fSLois Curfman McInnes 23543c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2355a86d99e1SLois Curfman McInnes 2356a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 23573c7409f5SSatish Balay @*/ 23583c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 23593c7409f5SSatish Balay { 23603c7409f5SSatish Balay int ierr; 23613c7409f5SSatish Balay 23623a40ed3dSBarry Smith PetscFunctionBegin; 236377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2364639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 23653c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 23663a40ed3dSBarry Smith PetscFunctionReturn(0); 23673c7409f5SSatish Balay } 23683c7409f5SSatish Balay 23695615d1e5SSatish Balay #undef __FUNC__ 2370d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix" 23713c7409f5SSatish Balay /*@C 2372f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2373d850072dSLois Curfman McInnes SNES options in the database. 23743c7409f5SSatish Balay 2375fee21e36SBarry Smith Collective on SNES 2376fee21e36SBarry Smith 2377c7afd0dbSLois Curfman McInnes Input Parameters: 2378c7afd0dbSLois Curfman McInnes + snes - the SNES context 2379c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2380c7afd0dbSLois Curfman McInnes 2381d850072dSLois Curfman McInnes Notes: 2382a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2383c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2384d850072dSLois Curfman McInnes 238536851e7fSLois Curfman McInnes Level: advanced 238636851e7fSLois Curfman McInnes 23873c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2388a86d99e1SLois Curfman McInnes 2389a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 23903c7409f5SSatish Balay @*/ 23913c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 23923c7409f5SSatish Balay { 23933c7409f5SSatish Balay int ierr; 23943c7409f5SSatish Balay 23953a40ed3dSBarry Smith PetscFunctionBegin; 239677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2397639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 23983c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 23993a40ed3dSBarry Smith PetscFunctionReturn(0); 24003c7409f5SSatish Balay } 24013c7409f5SSatish Balay 24025615d1e5SSatish Balay #undef __FUNC__ 2403d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix" 24049ab63eb5SSatish Balay /*@C 24053c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 24063c7409f5SSatish Balay SNES options in the database. 24073c7409f5SSatish Balay 2408c7afd0dbSLois Curfman McInnes Not Collective 2409c7afd0dbSLois Curfman McInnes 24103c7409f5SSatish Balay Input Parameter: 24113c7409f5SSatish Balay . snes - the SNES context 24123c7409f5SSatish Balay 24133c7409f5SSatish Balay Output Parameter: 24143c7409f5SSatish Balay . prefix - pointer to the prefix string used 24153c7409f5SSatish Balay 24169ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 24179ab63eb5SSatish Balay sufficient length to hold the prefix. 24189ab63eb5SSatish Balay 241936851e7fSLois Curfman McInnes Level: advanced 242036851e7fSLois Curfman McInnes 24213c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2422a86d99e1SLois Curfman McInnes 2423a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 24243c7409f5SSatish Balay @*/ 24253c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 24263c7409f5SSatish Balay { 24273c7409f5SSatish Balay int ierr; 24283c7409f5SSatish Balay 24293a40ed3dSBarry Smith PetscFunctionBegin; 243077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2431639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 24323a40ed3dSBarry Smith PetscFunctionReturn(0); 24333c7409f5SSatish Balay } 24343c7409f5SSatish Balay 2435ca161407SBarry Smith #undef __FUNC__ 2436ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp" 2437ca161407SBarry Smith /*@ 2438ca161407SBarry Smith SNESPrintHelp - Prints all options for the SNES component. 2439ca161407SBarry Smith 2440c7afd0dbSLois Curfman McInnes Collective on SNES 2441c7afd0dbSLois Curfman McInnes 2442ca161407SBarry Smith Input Parameter: 2443ca161407SBarry Smith . snes - the SNES context 2444ca161407SBarry Smith 2445ca161407SBarry Smith Options Database Keys: 2446c7afd0dbSLois Curfman McInnes + -help - Prints SNES options 2447c7afd0dbSLois Curfman McInnes - -h - Prints SNES options 2448ca161407SBarry Smith 244936851e7fSLois Curfman McInnes Level: beginner 245036851e7fSLois Curfman McInnes 2451ca161407SBarry Smith .keywords: SNES, nonlinear, help 2452ca161407SBarry Smith 2453ca161407SBarry Smith .seealso: SNESSetFromOptions() 2454ca161407SBarry Smith @*/ 2455ca161407SBarry Smith int SNESPrintHelp(SNES snes) 2456ca161407SBarry Smith { 2457ca161407SBarry Smith char p[64]; 2458ca161407SBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2459ca161407SBarry Smith int ierr; 2460ca161407SBarry Smith 2461ca161407SBarry Smith PetscFunctionBegin; 2462ca161407SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2463ca161407SBarry Smith 2464549d3d68SSatish Balay ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 2465ca161407SBarry Smith if (snes->prefix) PetscStrcat(p,snes->prefix); 2466ca161407SBarry Smith 2467ca161407SBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2468ca161407SBarry Smith 2469ebb8b11fSBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2470d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr); 2471488ecbafSBarry Smith ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr); 2472d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr); 2473d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr); 2474d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr); 2475d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr); 2476d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr); 2477d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr); 2478d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr); 2479d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr); 2480d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr); 2481d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_monitor: use default SNES convergence monitor, prints\n\ 2482d132466eSBarry Smith residual norm at each iteration.\n",p);CHKERRQ(ierr); 2483d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_smonitor: same as the above, but prints fewer digits of the\n\ 2484ca161407SBarry Smith residual norm for small residual norms. This is useful to conceal\n\ 2485d132466eSBarry Smith meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr); 2486d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr); 2487d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr); 2488d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr); 2489ca161407SBarry Smith if (snes->type == SNES_NONLINEAR_EQUATIONS) { 2490d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2491d132466eSBarry Smith " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr); 2492d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr); 2493d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr); 2494d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr); 2495d132466eSBarry 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); 2496d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr); 2497d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr); 2498d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2499d132466eSBarry Smith " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr); 2500d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2501d132466eSBarry Smith " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr); 2502d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2503d132466eSBarry Smith " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr); 2504d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2505d132466eSBarry Smith " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr); 2506d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2507d132466eSBarry Smith " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr); 2508d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2509d132466eSBarry Smith " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr); 2510d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2511d132466eSBarry Smith " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr); 2512ca161407SBarry Smith } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 2513d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr); 2514d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr); 2515d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr); 2516d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr); 2517ca161407SBarry Smith } 2518454a90a3SBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <type> for help on ",p);CHKERRQ(ierr); 2519d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr); 2520ca161407SBarry Smith if (snes->printhelp) { 2521ca161407SBarry Smith ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr); 2522ca161407SBarry Smith } 2523ca161407SBarry Smith PetscFunctionReturn(0); 2524ca161407SBarry Smith } 25253c7409f5SSatish Balay 2526acb85ae6SSatish Balay /*MC 2527f1af5d2fSBarry Smith SNESRegisterDynamic - Adds a method to the nonlinear solver package. 25289b94acceSBarry Smith 2529b2002411SLois Curfman McInnes Synopsis: 2530f1af5d2fSBarry Smith SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES)) 25319b94acceSBarry Smith 25328d76a1e5SLois Curfman McInnes Not collective 25338d76a1e5SLois Curfman McInnes 2534b2002411SLois Curfman McInnes Input Parameters: 2535c7afd0dbSLois Curfman McInnes + name_solver - name of a new user-defined solver 2536b2002411SLois Curfman McInnes . path - path (either absolute or relative) the library containing this solver 2537b2002411SLois Curfman McInnes . name_create - name of routine to create method context 2538c7afd0dbSLois Curfman McInnes - routine_create - routine to create method context 25399b94acceSBarry Smith 2540b2002411SLois Curfman McInnes Notes: 2541f1af5d2fSBarry Smith SNESRegisterDynamic() may be called multiple times to add several user-defined solvers. 25423a40ed3dSBarry Smith 2543b2002411SLois Curfman McInnes If dynamic libraries are used, then the fourth input argument (routine_create) 2544b2002411SLois Curfman McInnes is ignored. 2545b2002411SLois Curfman McInnes 2546b2002411SLois Curfman McInnes Sample usage: 254769225d78SLois Curfman McInnes .vb 2548f1af5d2fSBarry Smith SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a, 2549b2002411SLois Curfman McInnes "MySolverCreate",MySolverCreate); 255069225d78SLois Curfman McInnes .ve 2551b2002411SLois Curfman McInnes 2552b2002411SLois Curfman McInnes Then, your solver can be chosen with the procedural interface via 2553b2002411SLois Curfman McInnes $ SNESSetType(snes,"my_solver") 2554b2002411SLois Curfman McInnes or at runtime via the option 2555b2002411SLois Curfman McInnes $ -snes_type my_solver 2556b2002411SLois Curfman McInnes 255736851e7fSLois Curfman McInnes Level: advanced 255836851e7fSLois Curfman McInnes 255985614651SBarry Smith $PETSC_ARCH, $PETSC_DIR, $PETSC_LDIR, and $BOPT occuring in pathname will be replaced with appropriate values. 2560dd438238SBarry Smith 2561b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register 2562b2002411SLois Curfman McInnes 2563b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy() 2564b2002411SLois Curfman McInnes M*/ 2565b2002411SLois Curfman McInnes 2566b2002411SLois Curfman McInnes #undef __FUNC__ 2567f1af5d2fSBarry Smith #define __FUNC__ "SNESRegister" 2568f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES)) 2569b2002411SLois Curfman McInnes { 2570b2002411SLois Curfman McInnes char fullname[256]; 2571b2002411SLois Curfman McInnes int ierr; 2572b2002411SLois Curfman McInnes 2573b2002411SLois Curfman McInnes PetscFunctionBegin; 2574f1af5d2fSBarry Smith ierr = FListConcat(path,name,fullname);CHKERRQ(ierr); 2575f1af5d2fSBarry Smith ierr = FListAdd(&SNESList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr); 2576b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2577b2002411SLois Curfman McInnes } 2578