1*f1af5d2fSBarry Smith /*$Id: snes.c,v 1.201 1999/10/24 14:03:31 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]; 151*f1af5d2fSBarry Smith int ierr; 152*f1af5d2fSBarry 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 { 2269b94acceSBarry Smith double tmp; 2279b94acceSBarry Smith SLES sles; 228*f1af5d2fSBarry Smith PetscTruth flg; 229*f1af5d2fSBarry Smith int ierr,i,loc[4],nmax = 4; 2303c7409f5SSatish Balay int version = PETSC_DEFAULT; 2319b94acceSBarry Smith double rtol_0 = PETSC_DEFAULT; 2329b94acceSBarry Smith double rtol_max = PETSC_DEFAULT; 2339b94acceSBarry Smith double gamma2 = PETSC_DEFAULT; 2349b94acceSBarry Smith double alpha = PETSC_DEFAULT; 2359b94acceSBarry Smith double alpha2 = PETSC_DEFAULT; 2369b94acceSBarry Smith double 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 } 255*f1af5d2fSBarry Smith ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, PETSC_NULL);CHKERRQ(ierr); 256*f1af5d2fSBarry 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; } 263*f1af5d2fSBarry Smith ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,PETSC_NULL);CHKERRQ(ierr); 264*f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,PETSC_NULL);CHKERRQ(ierr); 265*f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,PETSC_NULL);CHKERRQ(ierr); 266*f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,PETSC_NULL);CHKERRQ(ierr); 267*f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,PETSC_NULL);CHKERRQ(ierr); 268*f1af5d2fSBarry Smith ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,PETSC_NULL);CHKERRQ(ierr); 269*f1af5d2fSBarry 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); 285b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitorUpdate,0,0);CHKERRQ(ierr);} 28681f57ec7SBarry Smith ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr); 2873c7409f5SSatish Balay if (flg) { 28817699dbbSLois Curfman McInnes int rank = 0; 289d7e8b826SBarry Smith DrawLG lg; 29017699dbbSLois Curfman McInnes MPI_Initialized(&rank); 291d132466eSBarry Smith if (rank) {ierr = MPI_Comm_rank(snes->comm,&rank);CHKERRQ(ierr);} 29217699dbbSLois Curfman McInnes if (!rank) { 29381f57ec7SBarry Smith ierr = SNESLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg);CHKERRQ(ierr); 294b8a78c4aSBarry Smith ierr = SNESSetMonitor(snes,SNESLGMonitor,lg,( int (*)(void *))SNESLGMonitorDestroy);CHKERRQ(ierr); 2959b94acceSBarry Smith PLogObjectParent(snes,lg); 2969b94acceSBarry Smith } 2979b94acceSBarry Smith } 298e24b481bSBarry Smith 2993c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);CHKERRQ(ierr); 3003c7409f5SSatish Balay if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 3019b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 3029b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 303981c4779SBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 304981c4779SBarry Smith } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 30531615d04SLois Curfman McInnes ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 30631615d04SLois Curfman McInnes SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr); 307d64ed03dSBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 3089b94acceSBarry Smith } 309639f9d9dSBarry Smith 310639f9d9dSBarry Smith for ( i=0; i<numberofsetfromoptions; i++ ) { 311639f9d9dSBarry Smith ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 312639f9d9dSBarry Smith } 313639f9d9dSBarry Smith 3149b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 3159b94acceSBarry Smith ierr = SLESSetFromOptions(sles);CHKERRQ(ierr); 31693993e2dSLois Curfman McInnes 317e24b481bSBarry Smith /* set the special KSP monitor for matrix-free application */ 318e24b481bSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg);CHKERRQ(ierr); 319e24b481bSBarry Smith if (flg) { 320e24b481bSBarry Smith KSP ksp; 321e24b481bSBarry Smith ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 322b8a78c4aSBarry Smith ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr); 323e24b481bSBarry Smith } 324e24b481bSBarry Smith 32582bf6240SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help", &flg);CHKERRQ(ierr); 32682bf6240SBarry Smith if (flg) { ierr = SNESPrintHelp(snes);CHKERRQ(ierr);} 32782bf6240SBarry Smith 3283a40ed3dSBarry Smith if (snes->setfromoptions) { 3293a40ed3dSBarry Smith ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr); 3303a40ed3dSBarry Smith } 3313a40ed3dSBarry Smith PetscFunctionReturn(0); 3329b94acceSBarry Smith } 3339b94acceSBarry Smith 334a847f771SSatish Balay 3355615d1e5SSatish Balay #undef __FUNC__ 336d4bb536fSBarry Smith #define __FUNC__ "SNESSetApplicationContext" 3379b94acceSBarry Smith /*@ 3389b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3399b94acceSBarry Smith the nonlinear solvers. 3409b94acceSBarry Smith 341fee21e36SBarry Smith Collective on SNES 342fee21e36SBarry Smith 343c7afd0dbSLois Curfman McInnes Input Parameters: 344c7afd0dbSLois Curfman McInnes + snes - the SNES context 345c7afd0dbSLois Curfman McInnes - usrP - optional user context 346c7afd0dbSLois Curfman McInnes 34736851e7fSLois Curfman McInnes Level: intermediate 34836851e7fSLois Curfman McInnes 3499b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3509b94acceSBarry Smith 3519b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3529b94acceSBarry Smith @*/ 3539b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 3549b94acceSBarry Smith { 3553a40ed3dSBarry Smith PetscFunctionBegin; 35677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3579b94acceSBarry Smith snes->user = usrP; 3583a40ed3dSBarry Smith PetscFunctionReturn(0); 3599b94acceSBarry Smith } 36074679c65SBarry Smith 3615615d1e5SSatish Balay #undef __FUNC__ 362d4bb536fSBarry Smith #define __FUNC__ "SNESGetApplicationContext" 3639b94acceSBarry Smith /*@C 3649b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3659b94acceSBarry Smith nonlinear solvers. 3669b94acceSBarry Smith 367c7afd0dbSLois Curfman McInnes Not Collective 368c7afd0dbSLois Curfman McInnes 3699b94acceSBarry Smith Input Parameter: 3709b94acceSBarry Smith . snes - SNES context 3719b94acceSBarry Smith 3729b94acceSBarry Smith Output Parameter: 3739b94acceSBarry Smith . usrP - user context 3749b94acceSBarry Smith 37536851e7fSLois Curfman McInnes Level: intermediate 37636851e7fSLois Curfman McInnes 3779b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3789b94acceSBarry Smith 3799b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3809b94acceSBarry Smith @*/ 3819b94acceSBarry Smith int SNESGetApplicationContext( SNES snes, void **usrP ) 3829b94acceSBarry Smith { 3833a40ed3dSBarry Smith PetscFunctionBegin; 38477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3859b94acceSBarry Smith *usrP = snes->user; 3863a40ed3dSBarry Smith PetscFunctionReturn(0); 3879b94acceSBarry Smith } 38874679c65SBarry Smith 3895615d1e5SSatish Balay #undef __FUNC__ 390d4bb536fSBarry Smith #define __FUNC__ "SNESGetIterationNumber" 3919b94acceSBarry Smith /*@ 392c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 393c8228a4eSBarry Smith at this time. 3949b94acceSBarry Smith 395c7afd0dbSLois Curfman McInnes Not Collective 396c7afd0dbSLois Curfman McInnes 3979b94acceSBarry Smith Input Parameter: 3989b94acceSBarry Smith . snes - SNES context 3999b94acceSBarry Smith 4009b94acceSBarry Smith Output Parameter: 4019b94acceSBarry Smith . iter - iteration number 4029b94acceSBarry Smith 403c8228a4eSBarry Smith Notes: 404c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 405c8228a4eSBarry Smith 406c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 40708405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 40808405cd6SLois Curfman McInnes .vb 40908405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 41008405cd6SLois Curfman McInnes if (!(it % 2)) { 41108405cd6SLois Curfman McInnes [compute Jacobian here] 41208405cd6SLois Curfman McInnes } 41308405cd6SLois Curfman McInnes .ve 414c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 41508405cd6SLois Curfman McInnes recomputed every second SNES iteration. 416c8228a4eSBarry Smith 41736851e7fSLois Curfman McInnes Level: intermediate 41836851e7fSLois Curfman McInnes 4199b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 4209b94acceSBarry Smith @*/ 4219b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 4229b94acceSBarry Smith { 4233a40ed3dSBarry Smith PetscFunctionBegin; 42477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 42574679c65SBarry Smith PetscValidIntPointer(iter); 4269b94acceSBarry Smith *iter = snes->iter; 4273a40ed3dSBarry Smith PetscFunctionReturn(0); 4289b94acceSBarry Smith } 42974679c65SBarry Smith 4305615d1e5SSatish Balay #undef __FUNC__ 4315615d1e5SSatish Balay #define __FUNC__ "SNESGetFunctionNorm" 4329b94acceSBarry Smith /*@ 4339b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4349b94acceSBarry Smith with SNESSSetFunction(). 4359b94acceSBarry Smith 436c7afd0dbSLois Curfman McInnes Collective on SNES 437c7afd0dbSLois Curfman McInnes 4389b94acceSBarry Smith Input Parameter: 4399b94acceSBarry Smith . snes - SNES context 4409b94acceSBarry Smith 4419b94acceSBarry Smith Output Parameter: 4429b94acceSBarry Smith . fnorm - 2-norm of function 4439b94acceSBarry Smith 4449b94acceSBarry Smith Note: 4459b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 446a86d99e1SLois Curfman McInnes A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 447a86d99e1SLois Curfman McInnes SNESGetGradientNorm(). 4489b94acceSBarry Smith 44936851e7fSLois Curfman McInnes Level: intermediate 45036851e7fSLois Curfman McInnes 4519b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 452a86d99e1SLois Curfman McInnes 45308405cd6SLois Curfman McInnes .seealso: SNESGetFunction() 4549b94acceSBarry Smith @*/ 4559b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 4569b94acceSBarry Smith { 4573a40ed3dSBarry Smith PetscFunctionBegin; 45877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 45974679c65SBarry Smith PetscValidScalarPointer(fnorm); 46074679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 461d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only"); 46274679c65SBarry Smith } 4639b94acceSBarry Smith *fnorm = snes->norm; 4643a40ed3dSBarry Smith PetscFunctionReturn(0); 4659b94acceSBarry Smith } 46674679c65SBarry Smith 4675615d1e5SSatish Balay #undef __FUNC__ 4685615d1e5SSatish Balay #define __FUNC__ "SNESGetGradientNorm" 4699b94acceSBarry Smith /*@ 4709b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 4719b94acceSBarry Smith with SNESSSetGradient(). 4729b94acceSBarry Smith 473c7afd0dbSLois Curfman McInnes Collective on SNES 474c7afd0dbSLois Curfman McInnes 4759b94acceSBarry Smith Input Parameter: 4769b94acceSBarry Smith . snes - SNES context 4779b94acceSBarry Smith 4789b94acceSBarry Smith Output Parameter: 4799b94acceSBarry Smith . fnorm - 2-norm of gradient 4809b94acceSBarry Smith 4819b94acceSBarry Smith Note: 4829b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 483a86d99e1SLois Curfman McInnes methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 484a86d99e1SLois Curfman McInnes is SNESGetFunctionNorm(). 4859b94acceSBarry Smith 48636851e7fSLois Curfman McInnes Level: intermediate 48736851e7fSLois Curfman McInnes 4889b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 489a86d99e1SLois Curfman McInnes 490a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient() 4919b94acceSBarry Smith @*/ 4929b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 4939b94acceSBarry Smith { 4943a40ed3dSBarry Smith PetscFunctionBegin; 49577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 49674679c65SBarry Smith PetscValidScalarPointer(gnorm); 49774679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 498d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 49974679c65SBarry Smith } 5009b94acceSBarry Smith *gnorm = snes->norm; 5013a40ed3dSBarry Smith PetscFunctionReturn(0); 5029b94acceSBarry Smith } 50374679c65SBarry Smith 5045615d1e5SSatish Balay #undef __FUNC__ 505d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberUnsuccessfulSteps" 5069b94acceSBarry Smith /*@ 5079b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 5089b94acceSBarry Smith attempted by the nonlinear solver. 5099b94acceSBarry Smith 510c7afd0dbSLois Curfman McInnes Not Collective 511c7afd0dbSLois Curfman McInnes 5129b94acceSBarry Smith Input Parameter: 5139b94acceSBarry Smith . snes - SNES context 5149b94acceSBarry Smith 5159b94acceSBarry Smith Output Parameter: 5169b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 5179b94acceSBarry Smith 518c96a6f78SLois Curfman McInnes Notes: 519c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 520c96a6f78SLois Curfman McInnes 52136851e7fSLois Curfman McInnes Level: intermediate 52236851e7fSLois Curfman McInnes 5239b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 5249b94acceSBarry Smith @*/ 5259b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 5269b94acceSBarry Smith { 5273a40ed3dSBarry Smith PetscFunctionBegin; 52877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 52974679c65SBarry Smith PetscValidIntPointer(nfails); 5309b94acceSBarry Smith *nfails = snes->nfailures; 5313a40ed3dSBarry Smith PetscFunctionReturn(0); 5329b94acceSBarry Smith } 533a847f771SSatish Balay 5345615d1e5SSatish Balay #undef __FUNC__ 535d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberLinearIterations" 536c96a6f78SLois Curfman McInnes /*@ 537c96a6f78SLois Curfman McInnes SNESGetNumberLinearIterations - Gets the total number of linear iterations 538c96a6f78SLois Curfman McInnes used by the nonlinear solver. 539c96a6f78SLois Curfman McInnes 540c7afd0dbSLois Curfman McInnes Not Collective 541c7afd0dbSLois Curfman McInnes 542c96a6f78SLois Curfman McInnes Input Parameter: 543c96a6f78SLois Curfman McInnes . snes - SNES context 544c96a6f78SLois Curfman McInnes 545c96a6f78SLois Curfman McInnes Output Parameter: 546c96a6f78SLois Curfman McInnes . lits - number of linear iterations 547c96a6f78SLois Curfman McInnes 548c96a6f78SLois Curfman McInnes Notes: 549c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 550c96a6f78SLois Curfman McInnes 55136851e7fSLois Curfman McInnes Level: intermediate 55236851e7fSLois Curfman McInnes 553c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 554c96a6f78SLois Curfman McInnes @*/ 55586bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits) 556c96a6f78SLois Curfman McInnes { 5573a40ed3dSBarry Smith PetscFunctionBegin; 558c96a6f78SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 559c96a6f78SLois Curfman McInnes PetscValidIntPointer(lits); 560c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 5613a40ed3dSBarry Smith PetscFunctionReturn(0); 562c96a6f78SLois Curfman McInnes } 563c96a6f78SLois Curfman McInnes 564c96a6f78SLois Curfman McInnes #undef __FUNC__ 565d4bb536fSBarry Smith #define __FUNC__ "SNESGetSLES" 5669b94acceSBarry Smith /*@C 5679b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 5689b94acceSBarry Smith 569c7afd0dbSLois Curfman McInnes Not Collective, but if SNES object is parallel, then SLES object is parallel 570c7afd0dbSLois Curfman McInnes 5719b94acceSBarry Smith Input Parameter: 5729b94acceSBarry Smith . snes - the SNES context 5739b94acceSBarry Smith 5749b94acceSBarry Smith Output Parameter: 5759b94acceSBarry Smith . sles - the SLES context 5769b94acceSBarry Smith 5779b94acceSBarry Smith Notes: 5789b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 5799b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 5809b94acceSBarry Smith KSP and PC contexts as well. 5819b94acceSBarry Smith 58236851e7fSLois Curfman McInnes Level: beginner 58336851e7fSLois Curfman McInnes 5849b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 5859b94acceSBarry Smith 5869b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 5879b94acceSBarry Smith @*/ 5889b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 5899b94acceSBarry Smith { 5903a40ed3dSBarry Smith PetscFunctionBegin; 59177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5929b94acceSBarry Smith *sles = snes->sles; 5933a40ed3dSBarry Smith PetscFunctionReturn(0); 5949b94acceSBarry Smith } 59582bf6240SBarry Smith 596e24b481bSBarry Smith #undef __FUNC__ 597e24b481bSBarry Smith #define __FUNC__ "SNESPublish_Petsc" 598454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj) 599e24b481bSBarry Smith { 600aa482453SBarry Smith #if defined(PETSC_HAVE_AMS) 601454a90a3SBarry Smith SNES v = (SNES) obj; 602e24b481bSBarry Smith int ierr; 60343d6d2cbSBarry Smith #endif 604e24b481bSBarry Smith 605e24b481bSBarry Smith PetscFunctionBegin; 606e24b481bSBarry Smith 60743d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS) 608e24b481bSBarry Smith /* if it is already published then return */ 609e24b481bSBarry Smith if (v->amem >=0 ) PetscFunctionReturn(0); 610e24b481bSBarry Smith 611454a90a3SBarry Smith ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr); 612e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ, 613e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 614e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ, 615e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 616454a90a3SBarry Smith ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr); 617433994e6SBarry Smith #endif 618e24b481bSBarry Smith PetscFunctionReturn(0); 619e24b481bSBarry Smith } 620e24b481bSBarry Smith 6219b94acceSBarry Smith /* -----------------------------------------------------------*/ 6225615d1e5SSatish Balay #undef __FUNC__ 6235615d1e5SSatish Balay #define __FUNC__ "SNESCreate" 6249b94acceSBarry Smith /*@C 6259b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 6269b94acceSBarry Smith 627c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 628c7afd0dbSLois Curfman McInnes 629c7afd0dbSLois Curfman McInnes Input Parameters: 630c7afd0dbSLois Curfman McInnes + comm - MPI communicator 631c7afd0dbSLois Curfman McInnes - type - type of method, either 632c7afd0dbSLois Curfman McInnes SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations) 633c7afd0dbSLois Curfman McInnes or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization) 6349b94acceSBarry Smith 6359b94acceSBarry Smith Output Parameter: 6369b94acceSBarry Smith . outsnes - the new SNES context 6379b94acceSBarry Smith 638c7afd0dbSLois Curfman McInnes Options Database Keys: 639c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 640c7afd0dbSLois Curfman McInnes and no preconditioning matrix 641c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 642c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 643c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 644c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 645c1f60f51SBarry Smith 64636851e7fSLois Curfman McInnes Level: beginner 64736851e7fSLois Curfman McInnes 6489b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 6499b94acceSBarry Smith 65063a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy() 6519b94acceSBarry Smith @*/ 6524b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 6539b94acceSBarry Smith { 6549b94acceSBarry Smith int ierr; 6559b94acceSBarry Smith SNES snes; 6569b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 65737fcc0dbSBarry Smith 6583a40ed3dSBarry Smith PetscFunctionBegin; 6599b94acceSBarry Smith *outsnes = 0; 660d64ed03dSBarry Smith if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){ 661d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type"); 662d64ed03dSBarry Smith } 6633f1db9ecSBarry Smith PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView); 6649b94acceSBarry Smith PLogObjectCreate(snes); 665e24b481bSBarry Smith snes->bops->publish = SNESPublish_Petsc; 6669b94acceSBarry Smith snes->max_its = 50; 6679750a799SBarry Smith snes->max_funcs = 10000; 6689b94acceSBarry Smith snes->norm = 0.0; 6695a2d0531SBarry Smith if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 670b18e04deSLois Curfman McInnes snes->rtol = 1.e-8; 671b18e04deSLois Curfman McInnes snes->ttol = 0.0; 6729b94acceSBarry Smith snes->atol = 1.e-10; 673d64ed03dSBarry Smith } else { 674b4874afaSBarry Smith snes->rtol = 1.e-8; 675b4874afaSBarry Smith snes->ttol = 0.0; 676b4874afaSBarry Smith snes->atol = 1.e-50; 677b4874afaSBarry Smith } 6789b94acceSBarry Smith snes->xtol = 1.e-8; 679b18e04deSLois Curfman McInnes snes->trunctol = 1.e-12; /* no longer used */ 6809b94acceSBarry Smith snes->nfuncs = 0; 6819b94acceSBarry Smith snes->nfailures = 0; 6827a00f4a9SLois Curfman McInnes snes->linear_its = 0; 683639f9d9dSBarry Smith snes->numbermonitors = 0; 6849b94acceSBarry Smith snes->data = 0; 6859b94acceSBarry Smith snes->view = 0; 6869b94acceSBarry Smith snes->computeumfunction = 0; 6879b94acceSBarry Smith snes->umfunP = 0; 6889b94acceSBarry Smith snes->fc = 0; 6899b94acceSBarry Smith snes->deltatol = 1.e-12; 6909b94acceSBarry Smith snes->fmin = -1.e30; 6919b94acceSBarry Smith snes->method_class = type; 6929b94acceSBarry Smith snes->set_method_called = 0; 69382bf6240SBarry Smith snes->setupcalled = 0; 6949b94acceSBarry Smith snes->ksp_ewconv = 0; 6956f24a144SLois Curfman McInnes snes->vwork = 0; 6966f24a144SLois Curfman McInnes snes->nwork = 0; 697758f92a0SBarry Smith snes->conv_hist_len = 0; 698758f92a0SBarry Smith snes->conv_hist_max = 0; 699758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 700758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 701758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 702184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 7039b94acceSBarry Smith 7049b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 7050452661fSBarry Smith kctx = PetscNew(SNES_KSP_EW_ConvCtx);CHKPTRQ(kctx); 706eed86810SBarry Smith PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx)); 7079b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 7089b94acceSBarry Smith kctx->version = 2; 7099b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 7109b94acceSBarry Smith this was too large for some test cases */ 7119b94acceSBarry Smith kctx->rtol_last = 0; 7129b94acceSBarry Smith kctx->rtol_max = .9; 7139b94acceSBarry Smith kctx->gamma = 1.0; 7149b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 7159b94acceSBarry Smith kctx->alpha = kctx->alpha2; 7169b94acceSBarry Smith kctx->threshold = .1; 7179b94acceSBarry Smith kctx->lresid_last = 0; 7189b94acceSBarry Smith kctx->norm_last = 0; 7199b94acceSBarry Smith 7209b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr); 7219b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 7225334005bSBarry Smith 7239b94acceSBarry Smith *outsnes = snes; 724e24b481bSBarry Smith PetscPublishAll(snes); 7253a40ed3dSBarry Smith PetscFunctionReturn(0); 7269b94acceSBarry Smith } 7279b94acceSBarry Smith 7289b94acceSBarry Smith /* --------------------------------------------------------------- */ 7295615d1e5SSatish Balay #undef __FUNC__ 730d4bb536fSBarry Smith #define __FUNC__ "SNESSetFunction" 7319b94acceSBarry Smith /*@C 7329b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 7339b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 7349b94acceSBarry Smith equations. 7359b94acceSBarry Smith 736fee21e36SBarry Smith Collective on SNES 737fee21e36SBarry Smith 738c7afd0dbSLois Curfman McInnes Input Parameters: 739c7afd0dbSLois Curfman McInnes + snes - the SNES context 740c7afd0dbSLois Curfman McInnes . func - function evaluation routine 741c7afd0dbSLois Curfman McInnes . r - vector to store function value 742c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 743c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 7449b94acceSBarry Smith 745c7afd0dbSLois Curfman McInnes Calling sequence of func: 7468d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 747c7afd0dbSLois Curfman McInnes 748313e4042SLois Curfman McInnes . f - function vector 749c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 7509b94acceSBarry Smith 7519b94acceSBarry Smith Notes: 7529b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 7539b94acceSBarry Smith $ f'(x) x = -f(x), 754c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 7559b94acceSBarry Smith 7569b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7579b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7589b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 7599b94acceSBarry Smith 76036851e7fSLois Curfman McInnes Level: beginner 76136851e7fSLois Curfman McInnes 7629b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 7639b94acceSBarry Smith 764a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 7659b94acceSBarry Smith @*/ 7665334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx) 7679b94acceSBarry Smith { 7683a40ed3dSBarry Smith PetscFunctionBegin; 76977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 770184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 771184914b5SBarry Smith PetscCheckSameComm(snes,r); 772a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 773a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 774a8c6a408SBarry Smith } 775184914b5SBarry Smith 7769b94acceSBarry Smith snes->computefunction = func; 7779b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 7789b94acceSBarry Smith snes->funP = ctx; 7793a40ed3dSBarry Smith PetscFunctionReturn(0); 7809b94acceSBarry Smith } 7819b94acceSBarry Smith 7825615d1e5SSatish Balay #undef __FUNC__ 7835615d1e5SSatish Balay #define __FUNC__ "SNESComputeFunction" 7849b94acceSBarry Smith /*@ 78536851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 7869b94acceSBarry Smith SNESSetFunction(). 7879b94acceSBarry Smith 788c7afd0dbSLois Curfman McInnes Collective on SNES 789c7afd0dbSLois Curfman McInnes 7909b94acceSBarry Smith Input Parameters: 791c7afd0dbSLois Curfman McInnes + snes - the SNES context 792c7afd0dbSLois Curfman McInnes - x - input vector 7939b94acceSBarry Smith 7949b94acceSBarry Smith Output Parameter: 7953638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 7969b94acceSBarry Smith 7971bffabb2SLois Curfman McInnes Notes: 7989b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7999b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 8009b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 8019b94acceSBarry Smith 80236851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 80336851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 80436851e7fSLois Curfman McInnes themselves. 80536851e7fSLois Curfman McInnes 80636851e7fSLois Curfman McInnes Level: developer 80736851e7fSLois Curfman McInnes 8089b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 8099b94acceSBarry Smith 810a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 8119b94acceSBarry Smith @*/ 8129b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y) 8139b94acceSBarry Smith { 8149b94acceSBarry Smith int ierr; 8159b94acceSBarry Smith 8163a40ed3dSBarry Smith PetscFunctionBegin; 817184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 818184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 819184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 820184914b5SBarry Smith PetscCheckSameComm(snes,x); 821184914b5SBarry Smith PetscCheckSameComm(snes,y); 822d4bb536fSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 823a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 824d4bb536fSBarry Smith } 825184914b5SBarry Smith 8269b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 827d64ed03dSBarry Smith PetscStackPush("SNES user function"); 8289b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 829d64ed03dSBarry Smith PetscStackPop; 830ae3c334cSLois Curfman McInnes snes->nfuncs++; 8319b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 8323a40ed3dSBarry Smith PetscFunctionReturn(0); 8339b94acceSBarry Smith } 8349b94acceSBarry Smith 8355615d1e5SSatish Balay #undef __FUNC__ 836d4bb536fSBarry Smith #define __FUNC__ "SNESSetMinimizationFunction" 8379b94acceSBarry Smith /*@C 8389b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 8399b94acceSBarry Smith unconstrained minimization. 8409b94acceSBarry Smith 841fee21e36SBarry Smith Collective on SNES 842fee21e36SBarry Smith 843c7afd0dbSLois Curfman McInnes Input Parameters: 844c7afd0dbSLois Curfman McInnes + snes - the SNES context 845c7afd0dbSLois Curfman McInnes . func - function evaluation routine 846c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 847c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8489b94acceSBarry Smith 849c7afd0dbSLois Curfman McInnes Calling sequence of func: 8508d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,double *f,void *ctx); 851c7afd0dbSLois Curfman McInnes 852c7afd0dbSLois Curfman McInnes + x - input vector 8539b94acceSBarry Smith . f - function 854c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined function context 8559b94acceSBarry Smith 85636851e7fSLois Curfman McInnes Level: beginner 85736851e7fSLois Curfman McInnes 8589b94acceSBarry Smith Notes: 8599b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 8609b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 8619b94acceSBarry Smith SNESSetFunction(). 8629b94acceSBarry Smith 8639b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 8649b94acceSBarry Smith 865a86d99e1SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 866a86d99e1SLois Curfman McInnes SNESSetHessian(), SNESSetGradient() 8679b94acceSBarry Smith @*/ 868454a90a3SBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),void *ctx) 8699b94acceSBarry Smith { 8703a40ed3dSBarry Smith PetscFunctionBegin; 87177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 872a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 873a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 874a8c6a408SBarry Smith } 8759b94acceSBarry Smith snes->computeumfunction = func; 8769b94acceSBarry Smith snes->umfunP = ctx; 8773a40ed3dSBarry Smith PetscFunctionReturn(0); 8789b94acceSBarry Smith } 8799b94acceSBarry Smith 8805615d1e5SSatish Balay #undef __FUNC__ 8815615d1e5SSatish Balay #define __FUNC__ "SNESComputeMinimizationFunction" 8829b94acceSBarry Smith /*@ 8839b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 8849b94acceSBarry Smith set with SNESSetMinimizationFunction(). 8859b94acceSBarry Smith 886c7afd0dbSLois Curfman McInnes Collective on SNES 887c7afd0dbSLois Curfman McInnes 8889b94acceSBarry Smith Input Parameters: 889c7afd0dbSLois Curfman McInnes + snes - the SNES context 890c7afd0dbSLois Curfman McInnes - x - input vector 8919b94acceSBarry Smith 8929b94acceSBarry Smith Output Parameter: 8939b94acceSBarry Smith . y - function value 8949b94acceSBarry Smith 8959b94acceSBarry Smith Notes: 8969b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 8979b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 8989b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 899a86d99e1SLois Curfman McInnes 90036851e7fSLois Curfman McInnes SNESComputeMinimizationFunction() is typically used within minimization 90136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 90236851e7fSLois Curfman McInnes themselves. 90336851e7fSLois Curfman McInnes 90436851e7fSLois Curfman McInnes Level: developer 90536851e7fSLois Curfman McInnes 906a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function 907a86d99e1SLois Curfman McInnes 908a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 909a86d99e1SLois Curfman McInnes SNESComputeGradient(), SNESComputeHessian() 9109b94acceSBarry Smith @*/ 9119b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 9129b94acceSBarry Smith { 9139b94acceSBarry Smith int ierr; 9143a40ed3dSBarry Smith 9153a40ed3dSBarry Smith PetscFunctionBegin; 916184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 917184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 918184914b5SBarry Smith PetscCheckSameComm(snes,x); 919a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 920a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 921a8c6a408SBarry Smith } 922184914b5SBarry Smith 9239b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 924d64ed03dSBarry Smith PetscStackPush("SNES user minimzation function"); 9259b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr); 926d64ed03dSBarry Smith PetscStackPop; 927ae3c334cSLois Curfman McInnes snes->nfuncs++; 9289b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 9293a40ed3dSBarry Smith PetscFunctionReturn(0); 9309b94acceSBarry Smith } 9319b94acceSBarry Smith 9325615d1e5SSatish Balay #undef __FUNC__ 933d4bb536fSBarry Smith #define __FUNC__ "SNESSetGradient" 9349b94acceSBarry Smith /*@C 9359b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 9369b94acceSBarry Smith vector for use by the SNES routines. 9379b94acceSBarry Smith 938c7afd0dbSLois Curfman McInnes Collective on SNES 939c7afd0dbSLois Curfman McInnes 9409b94acceSBarry Smith Input Parameters: 941c7afd0dbSLois Curfman McInnes + snes - the SNES context 9429b94acceSBarry Smith . func - function evaluation routine 943044dda88SLois Curfman McInnes . ctx - optional user-defined context for private data for the 944044dda88SLois Curfman McInnes gradient evaluation routine (may be PETSC_NULL) 945c7afd0dbSLois Curfman McInnes - r - vector to store gradient value 946fee21e36SBarry Smith 9479b94acceSBarry Smith Calling sequence of func: 9488d76a1e5SLois Curfman McInnes $ func (SNES, Vec x, Vec g, void *ctx); 9499b94acceSBarry Smith 950c7afd0dbSLois Curfman McInnes + x - input vector 9519b94acceSBarry Smith . g - gradient vector 952c7afd0dbSLois Curfman McInnes - ctx - optional user-defined gradient context 9539b94acceSBarry Smith 9549b94acceSBarry Smith Notes: 9559b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 9569b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 9579b94acceSBarry Smith SNESSetFunction(). 9589b94acceSBarry Smith 95936851e7fSLois Curfman McInnes Level: beginner 96036851e7fSLois Curfman McInnes 9619b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9629b94acceSBarry Smith 963a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 964a86d99e1SLois Curfman McInnes SNESSetMinimizationFunction(), 9659b94acceSBarry Smith @*/ 96674679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 9679b94acceSBarry Smith { 9683a40ed3dSBarry Smith PetscFunctionBegin; 96977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 970184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 971184914b5SBarry Smith PetscCheckSameComm(snes,r); 972a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 973a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 974a8c6a408SBarry Smith } 9759b94acceSBarry Smith snes->computefunction = func; 9769b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 9779b94acceSBarry Smith snes->funP = ctx; 9783a40ed3dSBarry Smith PetscFunctionReturn(0); 9799b94acceSBarry Smith } 9809b94acceSBarry Smith 9815615d1e5SSatish Balay #undef __FUNC__ 9825615d1e5SSatish Balay #define __FUNC__ "SNESComputeGradient" 9839b94acceSBarry Smith /*@ 984a86d99e1SLois Curfman McInnes SNESComputeGradient - Computes the gradient that has been set with 985a86d99e1SLois Curfman McInnes SNESSetGradient(). 9869b94acceSBarry Smith 987c7afd0dbSLois Curfman McInnes Collective on SNES 988c7afd0dbSLois Curfman McInnes 9899b94acceSBarry Smith Input Parameters: 990c7afd0dbSLois Curfman McInnes + snes - the SNES context 991c7afd0dbSLois Curfman McInnes - x - input vector 9929b94acceSBarry Smith 9939b94acceSBarry Smith Output Parameter: 9949b94acceSBarry Smith . y - gradient vector 9959b94acceSBarry Smith 9969b94acceSBarry Smith Notes: 9979b94acceSBarry Smith SNESComputeGradient() is valid only for 9989b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 9999b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 1000a86d99e1SLois Curfman McInnes 100136851e7fSLois Curfman McInnes SNESComputeGradient() is typically used within minimization 100236851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 100336851e7fSLois Curfman McInnes themselves. 100436851e7fSLois Curfman McInnes 100536851e7fSLois Curfman McInnes Level: developer 100636851e7fSLois Curfman McInnes 1007a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient 1008a86d99e1SLois Curfman McInnes 1009a86d99e1SLois Curfman McInnes .seealso: SNESSetGradient(), SNESGetGradient(), 1010a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction(), SNESComputeHessian() 10119b94acceSBarry Smith @*/ 10129b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y) 10139b94acceSBarry Smith { 10149b94acceSBarry Smith int ierr; 10153a40ed3dSBarry Smith 10163a40ed3dSBarry Smith PetscFunctionBegin; 1017184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1018184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1019184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1020184914b5SBarry Smith PetscCheckSameComm(snes,x); 1021184914b5SBarry Smith PetscCheckSameComm(snes,y); 10223a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1023a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 10243a40ed3dSBarry Smith } 10259b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 1026d64ed03dSBarry Smith PetscStackPush("SNES user gradient function"); 10279b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1028d64ed03dSBarry Smith PetscStackPop; 10299b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 10303a40ed3dSBarry Smith PetscFunctionReturn(0); 10319b94acceSBarry Smith } 10329b94acceSBarry Smith 10335615d1e5SSatish Balay #undef __FUNC__ 10345615d1e5SSatish Balay #define __FUNC__ "SNESComputeJacobian" 103562fef451SLois Curfman McInnes /*@ 103662fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 103762fef451SLois Curfman McInnes set with SNESSetJacobian(). 103862fef451SLois Curfman McInnes 1039c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1040c7afd0dbSLois Curfman McInnes 104162fef451SLois Curfman McInnes Input Parameters: 1042c7afd0dbSLois Curfman McInnes + snes - the SNES context 1043c7afd0dbSLois Curfman McInnes - x - input vector 104462fef451SLois Curfman McInnes 104562fef451SLois Curfman McInnes Output Parameters: 1046c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 104762fef451SLois Curfman McInnes . B - optional preconditioning matrix 1048c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1049fee21e36SBarry Smith 105062fef451SLois Curfman McInnes Notes: 105162fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 105262fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 105362fef451SLois Curfman McInnes 1054dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1055dc5a77f8SLois Curfman McInnes flag parameter. 105662fef451SLois Curfman McInnes 105762fef451SLois Curfman McInnes SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 105862fef451SLois Curfman McInnes methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 1059005c665bSBarry Smith methods is SNESComputeHessian(). 106062fef451SLois Curfman McInnes 106136851e7fSLois Curfman McInnes SNESComputeJacobian() is typically used within nonlinear solver 106236851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 106336851e7fSLois Curfman McInnes themselves. 106436851e7fSLois Curfman McInnes 106536851e7fSLois Curfman McInnes Level: developer 106636851e7fSLois Curfman McInnes 106762fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 106862fef451SLois Curfman McInnes 106962fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 107062fef451SLois Curfman McInnes @*/ 10719b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10729b94acceSBarry Smith { 10739b94acceSBarry Smith int ierr; 10743a40ed3dSBarry Smith 10753a40ed3dSBarry Smith PetscFunctionBegin; 1076184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1077184914b5SBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE); 1078184914b5SBarry Smith PetscCheckSameComm(snes,X); 10793a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1080a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 10813a40ed3dSBarry Smith } 10823a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 10839b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 1084c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1085d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 10869b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1087d64ed03dSBarry Smith PetscStackPop; 10889b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 10896d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 109077c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 109177c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 10923a40ed3dSBarry Smith PetscFunctionReturn(0); 10939b94acceSBarry Smith } 10949b94acceSBarry Smith 10955615d1e5SSatish Balay #undef __FUNC__ 10965615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian" 109762fef451SLois Curfman McInnes /*@ 109862fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 109962fef451SLois Curfman McInnes set with SNESSetHessian(). 110062fef451SLois Curfman McInnes 1101c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1102c7afd0dbSLois Curfman McInnes 110362fef451SLois Curfman McInnes Input Parameters: 1104c7afd0dbSLois Curfman McInnes + snes - the SNES context 1105c7afd0dbSLois Curfman McInnes - x - input vector 110662fef451SLois Curfman McInnes 110762fef451SLois Curfman McInnes Output Parameters: 1108c7afd0dbSLois Curfman McInnes + A - Hessian matrix 110962fef451SLois Curfman McInnes . B - optional preconditioning matrix 1110c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1111fee21e36SBarry Smith 111262fef451SLois Curfman McInnes Notes: 111362fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 111462fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 111562fef451SLois Curfman McInnes 1116dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1117dc5a77f8SLois Curfman McInnes flag parameter. 111862fef451SLois Curfman McInnes 111962fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 112062fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 112162fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 112262fef451SLois Curfman McInnes 112336851e7fSLois Curfman McInnes SNESComputeHessian() is typically used within minimization 112436851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 112536851e7fSLois Curfman McInnes themselves. 112636851e7fSLois Curfman McInnes 112736851e7fSLois Curfman McInnes Level: developer 112836851e7fSLois Curfman McInnes 112962fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 113062fef451SLois Curfman McInnes 1131a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 1132a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 113362fef451SLois Curfman McInnes @*/ 113462fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 11359b94acceSBarry Smith { 11369b94acceSBarry Smith int ierr; 11373a40ed3dSBarry Smith 11383a40ed3dSBarry Smith PetscFunctionBegin; 1139184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1140184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1141184914b5SBarry Smith PetscCheckSameComm(snes,x); 11423a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1143a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 11443a40ed3dSBarry Smith } 11453a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 114662fef451SLois Curfman McInnes PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 11470f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 1148d64ed03dSBarry Smith PetscStackPush("SNES user Hessian function"); 114962fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr); 1150d64ed03dSBarry Smith PetscStackPop; 115162fef451SLois Curfman McInnes PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 11520f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 115377c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 115477c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 11553a40ed3dSBarry Smith PetscFunctionReturn(0); 11569b94acceSBarry Smith } 11579b94acceSBarry Smith 11585615d1e5SSatish Balay #undef __FUNC__ 1159d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian" 11609b94acceSBarry Smith /*@C 11619b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1162044dda88SLois Curfman McInnes location to store the matrix. 11639b94acceSBarry Smith 1164c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1165c7afd0dbSLois Curfman McInnes 11669b94acceSBarry Smith Input Parameters: 1167c7afd0dbSLois Curfman McInnes + snes - the SNES context 11689b94acceSBarry Smith . A - Jacobian matrix 11699b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11709b94acceSBarry Smith . func - Jacobian evaluation routine 1171c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11722cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11739b94acceSBarry Smith 11749b94acceSBarry Smith Calling sequence of func: 11758d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11769b94acceSBarry Smith 1177c7afd0dbSLois Curfman McInnes + x - input vector 11789b94acceSBarry Smith . A - Jacobian matrix 11799b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1180ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1181ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1182c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11839b94acceSBarry Smith 11849b94acceSBarry Smith Notes: 1185dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 11862cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1187ac21db08SLois Curfman McInnes 1188ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11899b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11909b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11919b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11929b94acceSBarry Smith throughout the global iterations. 11939b94acceSBarry Smith 119436851e7fSLois Curfman McInnes Level: beginner 119536851e7fSLois Curfman McInnes 11969b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11979b94acceSBarry Smith 1198ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 11999b94acceSBarry Smith @*/ 1200454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 12019b94acceSBarry Smith { 12023a40ed3dSBarry Smith PetscFunctionBegin; 120377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1204184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1205184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1206184914b5SBarry Smith PetscCheckSameComm(snes,A); 1207184914b5SBarry Smith PetscCheckSameComm(snes,B); 1208a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1209a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 1210a8c6a408SBarry Smith } 1211184914b5SBarry Smith 12129b94acceSBarry Smith snes->computejacobian = func; 12139b94acceSBarry Smith snes->jacP = ctx; 12149b94acceSBarry Smith snes->jacobian = A; 12159b94acceSBarry Smith snes->jacobian_pre = B; 12163a40ed3dSBarry Smith PetscFunctionReturn(0); 12179b94acceSBarry Smith } 121862fef451SLois Curfman McInnes 12195615d1e5SSatish Balay #undef __FUNC__ 1220d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian" 1221c2aafc4cSSatish Balay /*@C 1222b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1223b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1224b4fd4287SBarry Smith 1225c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1226c7afd0dbSLois Curfman McInnes 1227b4fd4287SBarry Smith Input Parameter: 1228b4fd4287SBarry Smith . snes - the nonlinear solver context 1229b4fd4287SBarry Smith 1230b4fd4287SBarry Smith Output Parameters: 1231c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1232b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 1233c7afd0dbSLois Curfman McInnes - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1234fee21e36SBarry Smith 123536851e7fSLois Curfman McInnes Level: advanced 123636851e7fSLois Curfman McInnes 1237b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1238b4fd4287SBarry Smith @*/ 1239b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx) 1240b4fd4287SBarry Smith { 12413a40ed3dSBarry Smith PetscFunctionBegin; 1242184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 12433a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1244a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 12453a40ed3dSBarry Smith } 1246b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1247b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1248b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 12493a40ed3dSBarry Smith PetscFunctionReturn(0); 1250b4fd4287SBarry Smith } 1251b4fd4287SBarry Smith 12525615d1e5SSatish Balay #undef __FUNC__ 1253d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian" 12549b94acceSBarry Smith /*@C 12559b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 1256044dda88SLois Curfman McInnes location to store the matrix. 12579b94acceSBarry Smith 1258c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1259c7afd0dbSLois Curfman McInnes 12609b94acceSBarry Smith Input Parameters: 1261c7afd0dbSLois Curfman McInnes + snes - the SNES context 12629b94acceSBarry Smith . A - Hessian matrix 12639b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 12649b94acceSBarry Smith . func - Jacobian evaluation routine 1265c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1266313e4042SLois Curfman McInnes Hessian evaluation routine (may be PETSC_NULL) 12679b94acceSBarry Smith 12689b94acceSBarry Smith Calling sequence of func: 12698d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 12709b94acceSBarry Smith 1271c7afd0dbSLois Curfman McInnes + x - input vector 12729b94acceSBarry Smith . A - Hessian matrix 12739b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1274ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1275ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1276c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Hessian context 12779b94acceSBarry Smith 12789b94acceSBarry Smith Notes: 1279dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 12802cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1281ac21db08SLois Curfman McInnes 12829b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 12839b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 12849b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 12859b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 12869b94acceSBarry Smith throughout the global iterations. 12879b94acceSBarry Smith 128836851e7fSLois Curfman McInnes Level: beginner 128936851e7fSLois Curfman McInnes 12909b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 12919b94acceSBarry Smith 1292ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 12939b94acceSBarry Smith @*/ 1294454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 12959b94acceSBarry Smith { 12963a40ed3dSBarry Smith PetscFunctionBegin; 129777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1298184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1299184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1300184914b5SBarry Smith PetscCheckSameComm(snes,A); 1301184914b5SBarry Smith PetscCheckSameComm(snes,B); 1302d4bb536fSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1303a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1304d4bb536fSBarry Smith } 13059b94acceSBarry Smith snes->computejacobian = func; 13069b94acceSBarry Smith snes->jacP = ctx; 13079b94acceSBarry Smith snes->jacobian = A; 13089b94acceSBarry Smith snes->jacobian_pre = B; 13093a40ed3dSBarry Smith PetscFunctionReturn(0); 13109b94acceSBarry Smith } 13119b94acceSBarry Smith 13125615d1e5SSatish Balay #undef __FUNC__ 1313d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian" 131462fef451SLois Curfman McInnes /*@ 131562fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 131662fef451SLois Curfman McInnes provided context for evaluating the Hessian. 131762fef451SLois Curfman McInnes 1318c7afd0dbSLois Curfman McInnes Not Collective, but Mat object is parallel if SNES object is parallel 1319c7afd0dbSLois Curfman McInnes 132062fef451SLois Curfman McInnes Input Parameter: 132162fef451SLois Curfman McInnes . snes - the nonlinear solver context 132262fef451SLois Curfman McInnes 132362fef451SLois Curfman McInnes Output Parameters: 1324c7afd0dbSLois Curfman McInnes + A - location to stash Hessian matrix (or PETSC_NULL) 132562fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 1326c7afd0dbSLois Curfman McInnes - ctx - location to stash Hessian ctx (or PETSC_NULL) 1327fee21e36SBarry Smith 132836851e7fSLois Curfman McInnes Level: advanced 132936851e7fSLois Curfman McInnes 133062fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 1331c7afd0dbSLois Curfman McInnes 1332c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian 133362fef451SLois Curfman McInnes @*/ 133462fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx) 133562fef451SLois Curfman McInnes { 13363a40ed3dSBarry Smith PetscFunctionBegin; 1337184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 13383a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){ 1339a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 13403a40ed3dSBarry Smith } 134162fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 134262fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 134362fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 13443a40ed3dSBarry Smith PetscFunctionReturn(0); 134562fef451SLois Curfman McInnes } 134662fef451SLois Curfman McInnes 13479b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 13489b94acceSBarry Smith 13495615d1e5SSatish Balay #undef __FUNC__ 13505615d1e5SSatish Balay #define __FUNC__ "SNESSetUp" 13519b94acceSBarry Smith /*@ 13529b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1353272ac6f2SLois Curfman McInnes of a nonlinear solver. 13549b94acceSBarry Smith 1355fee21e36SBarry Smith Collective on SNES 1356fee21e36SBarry Smith 1357c7afd0dbSLois Curfman McInnes Input Parameters: 1358c7afd0dbSLois Curfman McInnes + snes - the SNES context 1359c7afd0dbSLois Curfman McInnes - x - the solution vector 1360c7afd0dbSLois Curfman McInnes 1361272ac6f2SLois Curfman McInnes Notes: 1362272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1363272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1364272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1365272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1366272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1367272ac6f2SLois Curfman McInnes 136836851e7fSLois Curfman McInnes Level: advanced 136936851e7fSLois Curfman McInnes 13709b94acceSBarry Smith .keywords: SNES, nonlinear, setup 13719b94acceSBarry Smith 13729b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 13739b94acceSBarry Smith @*/ 13748ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 13759b94acceSBarry Smith { 1376*f1af5d2fSBarry Smith int ierr; 1377*f1af5d2fSBarry Smith PetscTruth flg; 13783a40ed3dSBarry Smith 13793a40ed3dSBarry Smith PetscFunctionBegin; 138077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 138177c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1382184914b5SBarry Smith PetscCheckSameComm(snes,x); 13838ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 13849b94acceSBarry Smith 1385c1f60f51SBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);CHKERRQ(ierr); 1386c1f60f51SBarry Smith /* 1387c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1388dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1389c1f60f51SBarry Smith */ 1390c1f60f51SBarry Smith if (flg) { 1391c1f60f51SBarry Smith Mat J; 13925a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1393c1f60f51SBarry Smith PLogObjectParent(snes,J); 1394c1f60f51SBarry Smith snes->mfshell = J; 1395c1f60f51SBarry Smith snes->jacobian = J; 1396c2aafc4cSSatish Balay if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1397c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n"); 1398d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1399c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n"); 1400d4bb536fSBarry Smith } else { 1401a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option"); 1402d4bb536fSBarry Smith } 14035a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1404c1f60f51SBarry Smith } 1405272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);CHKERRQ(ierr); 1406c1f60f51SBarry Smith /* 1407dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1408c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1409c1f60f51SBarry Smith */ 141031615d04SLois Curfman McInnes if (flg) { 1411272ac6f2SLois Curfman McInnes Mat J; 14125a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1413272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 1414272ac6f2SLois Curfman McInnes snes->mfshell = J; 141583e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 141683e56afdSLois Curfman McInnes ierr = SNESSetJacobian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 141794a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 1418d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 141983e56afdSLois Curfman McInnes ierr = SNESSetHessian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 142094a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 1421d4bb536fSBarry Smith } else { 1422a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option"); 1423d4bb536fSBarry Smith } 14245a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1425272ac6f2SLois Curfman McInnes } 14269b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 14276831982aSBarry Smith PetscTruth iseqtr; 14286831982aSBarry Smith 1429a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 1430a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 14315a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 1432a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 1433a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector"); 1434a8c6a408SBarry Smith } 1435a703fe33SLois Curfman McInnes 1436a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 14376831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr); 14386831982aSBarry Smith if (snes->ksp_ewconv && !iseqtr) { 1439a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 1440a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 1441a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 14425a655dc6SBarry Smith ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr); 1443a703fe33SLois Curfman McInnes } 14449b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 1445a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1446a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1447a8c6a408SBarry Smith if (!snes->computeumfunction) { 1448a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first"); 1449a8c6a408SBarry Smith } 14505a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()"); 1451d4bb536fSBarry Smith } else { 1452a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class"); 1453d4bb536fSBarry Smith } 1454a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);} 145582bf6240SBarry Smith snes->setupcalled = 1; 14563a40ed3dSBarry Smith PetscFunctionReturn(0); 14579b94acceSBarry Smith } 14589b94acceSBarry Smith 14595615d1e5SSatish Balay #undef __FUNC__ 1460d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy" 14619b94acceSBarry Smith /*@C 14629b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 14639b94acceSBarry Smith with SNESCreate(). 14649b94acceSBarry Smith 1465c7afd0dbSLois Curfman McInnes Collective on SNES 1466c7afd0dbSLois Curfman McInnes 14679b94acceSBarry Smith Input Parameter: 14689b94acceSBarry Smith . snes - the SNES context 14699b94acceSBarry Smith 147036851e7fSLois Curfman McInnes Level: beginner 147136851e7fSLois Curfman McInnes 14729b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 14739b94acceSBarry Smith 147463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 14759b94acceSBarry Smith @*/ 14769b94acceSBarry Smith int SNESDestroy(SNES snes) 14779b94acceSBarry Smith { 1478b8a78c4aSBarry Smith int i,ierr; 14793a40ed3dSBarry Smith 14803a40ed3dSBarry Smith PetscFunctionBegin; 148177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14823a40ed3dSBarry Smith if (--snes->refct > 0) PetscFunctionReturn(0); 1483d4bb536fSBarry Smith 1484be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 14850f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1486be0abb6dSBarry Smith 1487e1311b90SBarry Smith if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);} 1488606d414cSSatish Balay if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);} 1489522c5e43SBarry Smith if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);} 14909b94acceSBarry Smith ierr = SLESDestroy(snes->sles);CHKERRQ(ierr); 1491522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1492b8a78c4aSBarry Smith for (i=0; i<snes->numbermonitors; i++ ) { 1493b8a78c4aSBarry Smith if (snes->monitordestroy[i]) { 1494b8a78c4aSBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1495b8a78c4aSBarry Smith } 1496b8a78c4aSBarry Smith } 14979b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 14980452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 14993a40ed3dSBarry Smith PetscFunctionReturn(0); 15009b94acceSBarry Smith } 15019b94acceSBarry Smith 15029b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 15039b94acceSBarry Smith 15045615d1e5SSatish Balay #undef __FUNC__ 15055615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances" 15069b94acceSBarry Smith /*@ 1507d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 15089b94acceSBarry Smith 1509c7afd0dbSLois Curfman McInnes Collective on SNES 1510c7afd0dbSLois Curfman McInnes 15119b94acceSBarry Smith Input Parameters: 1512c7afd0dbSLois Curfman McInnes + snes - the SNES context 151333174efeSLois Curfman McInnes . atol - absolute convergence tolerance 151433174efeSLois Curfman McInnes . rtol - relative convergence tolerance 151533174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 151633174efeSLois Curfman McInnes of the change in the solution between steps 151733174efeSLois Curfman McInnes . maxit - maximum number of iterations 1518c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1519fee21e36SBarry Smith 152033174efeSLois Curfman McInnes Options Database Keys: 1521c7afd0dbSLois Curfman McInnes + -snes_atol <atol> - Sets atol 1522c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1523c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1524c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1525c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15269b94acceSBarry Smith 1527d7a720efSLois Curfman McInnes Notes: 15289b94acceSBarry Smith The default maximum number of iterations is 50. 15299b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15309b94acceSBarry Smith 153136851e7fSLois Curfman McInnes Level: intermediate 153236851e7fSLois Curfman McInnes 153333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15349b94acceSBarry Smith 1535d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 15369b94acceSBarry Smith @*/ 1537d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf) 15389b94acceSBarry Smith { 15393a40ed3dSBarry Smith PetscFunctionBegin; 154077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1541d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1542d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1543d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1544d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1545d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15463a40ed3dSBarry Smith PetscFunctionReturn(0); 15479b94acceSBarry Smith } 15489b94acceSBarry Smith 15495615d1e5SSatish Balay #undef __FUNC__ 15505615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances" 15519b94acceSBarry Smith /*@ 155233174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 155333174efeSLois Curfman McInnes 1554c7afd0dbSLois Curfman McInnes Not Collective 1555c7afd0dbSLois Curfman McInnes 155633174efeSLois Curfman McInnes Input Parameters: 1557c7afd0dbSLois Curfman McInnes + snes - the SNES context 155833174efeSLois Curfman McInnes . atol - absolute convergence tolerance 155933174efeSLois Curfman McInnes . rtol - relative convergence tolerance 156033174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 156133174efeSLois Curfman McInnes of the change in the solution between steps 156233174efeSLois Curfman McInnes . maxit - maximum number of iterations 1563c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1564fee21e36SBarry Smith 156533174efeSLois Curfman McInnes Notes: 156633174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 156733174efeSLois Curfman McInnes 156836851e7fSLois Curfman McInnes Level: intermediate 156936851e7fSLois Curfman McInnes 157033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 157133174efeSLois Curfman McInnes 157233174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 157333174efeSLois Curfman McInnes @*/ 157433174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf) 157533174efeSLois Curfman McInnes { 15763a40ed3dSBarry Smith PetscFunctionBegin; 157733174efeSLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 157833174efeSLois Curfman McInnes if (atol) *atol = snes->atol; 157933174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 158033174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 158133174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 158233174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 15833a40ed3dSBarry Smith PetscFunctionReturn(0); 158433174efeSLois Curfman McInnes } 158533174efeSLois Curfman McInnes 15865615d1e5SSatish Balay #undef __FUNC__ 15875615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance" 158833174efeSLois Curfman McInnes /*@ 15899b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 15909b94acceSBarry Smith 1591fee21e36SBarry Smith Collective on SNES 1592fee21e36SBarry Smith 1593c7afd0dbSLois Curfman McInnes Input Parameters: 1594c7afd0dbSLois Curfman McInnes + snes - the SNES context 1595c7afd0dbSLois Curfman McInnes - tol - tolerance 1596c7afd0dbSLois Curfman McInnes 15979b94acceSBarry Smith Options Database Key: 1598c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 15999b94acceSBarry Smith 160036851e7fSLois Curfman McInnes Level: intermediate 160136851e7fSLois Curfman McInnes 16029b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 16039b94acceSBarry Smith 1604d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 16059b94acceSBarry Smith @*/ 16069b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol) 16079b94acceSBarry Smith { 16083a40ed3dSBarry Smith PetscFunctionBegin; 160977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16109b94acceSBarry Smith snes->deltatol = tol; 16113a40ed3dSBarry Smith PetscFunctionReturn(0); 16129b94acceSBarry Smith } 16139b94acceSBarry Smith 16145615d1e5SSatish Balay #undef __FUNC__ 16155615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance" 16169b94acceSBarry Smith /*@ 161777c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 16189b94acceSBarry Smith for unconstrained minimization solvers. 16199b94acceSBarry Smith 1620fee21e36SBarry Smith Collective on SNES 1621fee21e36SBarry Smith 1622c7afd0dbSLois Curfman McInnes Input Parameters: 1623c7afd0dbSLois Curfman McInnes + snes - the SNES context 1624c7afd0dbSLois Curfman McInnes - ftol - minimum function tolerance 1625c7afd0dbSLois Curfman McInnes 16269b94acceSBarry Smith Options Database Key: 1627c7afd0dbSLois Curfman McInnes . -snes_fmin <ftol> - Sets ftol 16289b94acceSBarry Smith 16299b94acceSBarry Smith Note: 163077c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 16319b94acceSBarry Smith methods only. 16329b94acceSBarry Smith 163336851e7fSLois Curfman McInnes Level: intermediate 163436851e7fSLois Curfman McInnes 16359b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 16369b94acceSBarry Smith 1637d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 16389b94acceSBarry Smith @*/ 163977c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol) 16409b94acceSBarry Smith { 16413a40ed3dSBarry Smith PetscFunctionBegin; 164277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16439b94acceSBarry Smith snes->fmin = ftol; 16443a40ed3dSBarry Smith PetscFunctionReturn(0); 16459b94acceSBarry Smith } 1646df9fa365SBarry Smith /* 1647df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1648df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1649df9fa365SBarry Smith macros instead of functions 1650df9fa365SBarry Smith */ 1651ce1608b8SBarry Smith #undef __FUNC__ 1652ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor" 1653ce1608b8SBarry Smith int SNESLGMonitor(SNES snes,int it,double norm,void *ctx) 1654ce1608b8SBarry Smith { 1655ce1608b8SBarry Smith int ierr; 1656ce1608b8SBarry Smith 1657ce1608b8SBarry Smith PetscFunctionBegin; 1658184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1659ce1608b8SBarry Smith ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1660ce1608b8SBarry Smith PetscFunctionReturn(0); 1661ce1608b8SBarry Smith } 1662ce1608b8SBarry Smith 1663df9fa365SBarry Smith #undef __FUNC__ 1664df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorCreate" 1665df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n, DrawLG *draw) 1666df9fa365SBarry Smith { 1667df9fa365SBarry Smith int ierr; 1668df9fa365SBarry Smith 1669df9fa365SBarry Smith PetscFunctionBegin; 1670df9fa365SBarry Smith ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1671df9fa365SBarry Smith PetscFunctionReturn(0); 1672df9fa365SBarry Smith } 1673df9fa365SBarry Smith 1674df9fa365SBarry Smith #undef __FUNC__ 1675df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorDestroy" 1676df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw) 1677df9fa365SBarry Smith { 1678df9fa365SBarry Smith int ierr; 1679df9fa365SBarry Smith 1680df9fa365SBarry Smith PetscFunctionBegin; 1681df9fa365SBarry Smith ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr); 1682df9fa365SBarry Smith PetscFunctionReturn(0); 1683df9fa365SBarry Smith } 1684df9fa365SBarry Smith 16859b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 16869b94acceSBarry Smith 16875615d1e5SSatish Balay #undef __FUNC__ 1688d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor" 16899b94acceSBarry Smith /*@C 16905cd90555SBarry Smith SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every 16919b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 16929b94acceSBarry Smith progress. 16939b94acceSBarry Smith 1694fee21e36SBarry Smith Collective on SNES 1695fee21e36SBarry Smith 1696c7afd0dbSLois Curfman McInnes Input Parameters: 1697c7afd0dbSLois Curfman McInnes + snes - the SNES context 1698c7afd0dbSLois Curfman McInnes . func - monitoring routine 1699b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1700c7afd0dbSLois Curfman McInnes monitor routine (may be PETSC_NULL) 1701b8a78c4aSBarry Smith - monitordestroy - options routine that frees monitor context 17029b94acceSBarry Smith 1703c7afd0dbSLois Curfman McInnes Calling sequence of func: 170440a0c1c6SLois Curfman McInnes $ int func(SNES snes,int its, double norm,void *mctx) 1705c7afd0dbSLois Curfman McInnes 1706c7afd0dbSLois Curfman McInnes + snes - the SNES context 1707c7afd0dbSLois Curfman McInnes . its - iteration number 1708c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 1709c7afd0dbSLois Curfman McInnes for SNES_NONLINEAR_EQUATIONS methods 171040a0c1c6SLois Curfman McInnes . norm - 2-norm gradient value (may be estimated) 1711c7afd0dbSLois Curfman McInnes for SNES_UNCONSTRAINED_MINIMIZATION methods 171240a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 17139b94acceSBarry Smith 17149665c990SLois Curfman McInnes Options Database Keys: 1715c7afd0dbSLois Curfman McInnes + -snes_monitor - sets SNESDefaultMonitor() 1716c7afd0dbSLois Curfman McInnes . -snes_xmonitor - sets line graph monitor, 1717c7afd0dbSLois Curfman McInnes uses SNESLGMonitorCreate() 1718c7afd0dbSLois Curfman McInnes _ -snes_cancelmonitors - cancels all monitors that have 1719c7afd0dbSLois Curfman McInnes been hardwired into a code by 1720c7afd0dbSLois Curfman McInnes calls to SNESSetMonitor(), but 1721c7afd0dbSLois Curfman McInnes does not cancel those set via 1722c7afd0dbSLois Curfman McInnes the options database. 17239665c990SLois Curfman McInnes 1724639f9d9dSBarry Smith Notes: 17256bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 17266bc08f3fSLois Curfman McInnes SNESSetMonitor() multiple times; all will be called in the 17276bc08f3fSLois Curfman McInnes order in which they were set. 1728639f9d9dSBarry Smith 172936851e7fSLois Curfman McInnes Level: intermediate 173036851e7fSLois Curfman McInnes 17319b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 17329b94acceSBarry Smith 17335cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor() 17349b94acceSBarry Smith @*/ 1735b8a78c4aSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx,int (*monitordestroy)(void *)) 17369b94acceSBarry Smith { 17373a40ed3dSBarry Smith PetscFunctionBegin; 1738184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1739639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 1740a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set"); 1741639f9d9dSBarry Smith } 1742639f9d9dSBarry Smith 1743639f9d9dSBarry Smith snes->monitor[snes->numbermonitors] = func; 1744b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1745639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 17463a40ed3dSBarry Smith PetscFunctionReturn(0); 17479b94acceSBarry Smith } 17489b94acceSBarry Smith 17495615d1e5SSatish Balay #undef __FUNC__ 17505cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor" 17515cd90555SBarry Smith /*@C 17525cd90555SBarry Smith SNESClearMonitor - Clears all the monitor functions for a SNES object. 17535cd90555SBarry Smith 1754c7afd0dbSLois Curfman McInnes Collective on SNES 1755c7afd0dbSLois Curfman McInnes 17565cd90555SBarry Smith Input Parameters: 17575cd90555SBarry Smith . snes - the SNES context 17585cd90555SBarry Smith 17595cd90555SBarry Smith Options Database: 1760c7afd0dbSLois Curfman McInnes . -snes_cancelmonitors - cancels all monitors that have been hardwired 1761c7afd0dbSLois Curfman McInnes into a code by calls to SNESSetMonitor(), but does not cancel those 1762c7afd0dbSLois Curfman McInnes set via the options database 17635cd90555SBarry Smith 17645cd90555SBarry Smith Notes: 17655cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 17665cd90555SBarry Smith 176736851e7fSLois Curfman McInnes Level: intermediate 176836851e7fSLois Curfman McInnes 17695cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 17705cd90555SBarry Smith 17715cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor() 17725cd90555SBarry Smith @*/ 17735cd90555SBarry Smith int SNESClearMonitor( SNES snes ) 17745cd90555SBarry Smith { 17755cd90555SBarry Smith PetscFunctionBegin; 1776184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 17775cd90555SBarry Smith snes->numbermonitors = 0; 17785cd90555SBarry Smith PetscFunctionReturn(0); 17795cd90555SBarry Smith } 17805cd90555SBarry Smith 17815cd90555SBarry Smith #undef __FUNC__ 1782d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest" 17839b94acceSBarry Smith /*@C 17849b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 17859b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 17869b94acceSBarry Smith 1787fee21e36SBarry Smith Collective on SNES 1788fee21e36SBarry Smith 1789c7afd0dbSLois Curfman McInnes Input Parameters: 1790c7afd0dbSLois Curfman McInnes + snes - the SNES context 1791c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1792c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1793c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 17949b94acceSBarry Smith 1795c7afd0dbSLois Curfman McInnes Calling sequence of func: 1796184914b5SBarry Smith $ int func (SNES snes,double xnorm,double gnorm,double f,SNESConvergedReason *reason,void *cctx) 1797c7afd0dbSLois Curfman McInnes 1798c7afd0dbSLois Curfman McInnes + snes - the SNES context 1799c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1800184914b5SBarry Smith . reason - reason for convergence/divergence 1801c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 1802c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods) 1803c7afd0dbSLois Curfman McInnes . f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods) 1804c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods) 1805c7afd0dbSLois Curfman McInnes - f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods) 18069b94acceSBarry Smith 180736851e7fSLois Curfman McInnes Level: advanced 180836851e7fSLois Curfman McInnes 18099b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 18109b94acceSBarry Smith 181140191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 181240191667SLois Curfman McInnes SNESConverged_UM_LS(), SNESConverged_UM_TR() 18139b94acceSBarry Smith @*/ 1814184914b5SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,SNESConvergedReason*,void*),void *cctx) 18159b94acceSBarry Smith { 18163a40ed3dSBarry Smith PetscFunctionBegin; 1817184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 18189b94acceSBarry Smith (snes)->converged = func; 18199b94acceSBarry Smith (snes)->cnvP = cctx; 18203a40ed3dSBarry Smith PetscFunctionReturn(0); 18219b94acceSBarry Smith } 18229b94acceSBarry Smith 18235615d1e5SSatish Balay #undef __FUNC__ 1824184914b5SBarry Smith #define __FUNC__ "SNESGetConvergedReason" 1825184914b5SBarry Smith /*@C 1826184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1827184914b5SBarry Smith 1828184914b5SBarry Smith Not Collective 1829184914b5SBarry Smith 1830184914b5SBarry Smith Input Parameter: 1831184914b5SBarry Smith . snes - the SNES context 1832184914b5SBarry Smith 1833184914b5SBarry Smith Output Parameter: 1834184914b5SBarry Smith . reason - negative value indicates diverged, positive value converged, see snes.h or the 1835184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1836184914b5SBarry Smith 1837184914b5SBarry Smith Level: intermediate 1838184914b5SBarry Smith 1839184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1840184914b5SBarry Smith 1841184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1842184914b5SBarry Smith 1843184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 1844184914b5SBarry Smith SNESConverged_UM_LS(), SNESConverged_UM_TR() 1845184914b5SBarry Smith @*/ 1846184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1847184914b5SBarry Smith { 1848184914b5SBarry Smith PetscFunctionBegin; 1849184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1850184914b5SBarry Smith *reason = snes->reason; 1851184914b5SBarry Smith PetscFunctionReturn(0); 1852184914b5SBarry Smith } 1853184914b5SBarry Smith 1854184914b5SBarry Smith #undef __FUNC__ 1855d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory" 1856c9005455SLois Curfman McInnes /*@ 1857c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1858c9005455SLois Curfman McInnes 1859fee21e36SBarry Smith Collective on SNES 1860fee21e36SBarry Smith 1861c7afd0dbSLois Curfman McInnes Input Parameters: 1862c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1863c7afd0dbSLois Curfman McInnes . a - array to hold history 1864758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1865758f92a0SBarry Smith negative if not converged) for each solve. 1866758f92a0SBarry Smith . na - size of a and its 1867758f92a0SBarry Smith - reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero, 1868758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1869c7afd0dbSLois Curfman McInnes 1870c9005455SLois Curfman McInnes Notes: 1871c9005455SLois Curfman McInnes If set, this array will contain the function norms (for 1872c9005455SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods) or gradient norms 1873c9005455SLois Curfman McInnes (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed 1874c9005455SLois Curfman McInnes at each step. 1875c9005455SLois Curfman McInnes 1876c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1877c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1878c9005455SLois Curfman McInnes during the section of code that is being timed. 1879c9005455SLois Curfman McInnes 188036851e7fSLois Curfman McInnes Level: intermediate 188136851e7fSLois Curfman McInnes 1882c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1883758f92a0SBarry Smith 188408405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1885758f92a0SBarry Smith 1886c9005455SLois Curfman McInnes @*/ 1887758f92a0SBarry Smith int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset) 1888c9005455SLois Curfman McInnes { 18893a40ed3dSBarry Smith PetscFunctionBegin; 1890c9005455SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 1891c9005455SLois Curfman McInnes if (na) PetscValidScalarPointer(a); 1892c9005455SLois Curfman McInnes snes->conv_hist = a; 1893758f92a0SBarry Smith snes->conv_hist_its = its; 1894758f92a0SBarry Smith snes->conv_hist_max = na; 1895758f92a0SBarry Smith snes->conv_hist_reset = reset; 1896758f92a0SBarry Smith PetscFunctionReturn(0); 1897758f92a0SBarry Smith } 1898758f92a0SBarry Smith 1899758f92a0SBarry Smith #undef __FUNC__ 1900758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory" 19010c4c9dddSBarry Smith /*@C 1902758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1903758f92a0SBarry Smith 1904758f92a0SBarry Smith Collective on SNES 1905758f92a0SBarry Smith 1906758f92a0SBarry Smith Input Parameter: 1907758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1908758f92a0SBarry Smith 1909758f92a0SBarry Smith Output Parameters: 1910758f92a0SBarry Smith . a - array to hold history 1911758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1912758f92a0SBarry Smith negative if not converged) for each solve. 1913758f92a0SBarry Smith - na - size of a and its 1914758f92a0SBarry Smith 1915758f92a0SBarry Smith Notes: 1916758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1917758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1918758f92a0SBarry Smith 1919758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1920758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1921758f92a0SBarry Smith during the section of code that is being timed. 1922758f92a0SBarry Smith 1923758f92a0SBarry Smith Level: intermediate 1924758f92a0SBarry Smith 1925758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1926758f92a0SBarry Smith 1927758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1928758f92a0SBarry Smith 1929758f92a0SBarry Smith @*/ 1930758f92a0SBarry Smith int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na) 1931758f92a0SBarry Smith { 1932758f92a0SBarry Smith PetscFunctionBegin; 1933758f92a0SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1934758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1935758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1936758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 19373a40ed3dSBarry Smith PetscFunctionReturn(0); 1938c9005455SLois Curfman McInnes } 1939c9005455SLois Curfman McInnes 1940c9005455SLois Curfman McInnes #undef __FUNC__ 19415615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private" 19429b94acceSBarry Smith /* 19439b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 19449b94acceSBarry Smith positive parameter delta. 19459b94acceSBarry Smith 19469b94acceSBarry Smith Input Parameters: 1947c7afd0dbSLois Curfman McInnes + snes - the SNES context 19489b94acceSBarry Smith . y - approximate solution of linear system 19499b94acceSBarry Smith . fnorm - 2-norm of current function 1950c7afd0dbSLois Curfman McInnes - delta - trust region size 19519b94acceSBarry Smith 19529b94acceSBarry Smith Output Parameters: 1953c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 19549b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 19559b94acceSBarry Smith region, and exceeds zero otherwise. 1956c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 19579b94acceSBarry Smith 19589b94acceSBarry Smith Note: 19596831982aSBarry Smith For non-trust region methods such as SNESEQLS, the parameter delta 19609b94acceSBarry Smith is set to be the maximum allowable step size. 19619b94acceSBarry Smith 19629b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 19639b94acceSBarry Smith */ 19649b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 19659b94acceSBarry Smith double *gpnorm,double *ynorm) 19669b94acceSBarry Smith { 19679b94acceSBarry Smith double norm; 19689b94acceSBarry Smith Scalar cnorm; 19693a40ed3dSBarry Smith int ierr; 19703a40ed3dSBarry Smith 19713a40ed3dSBarry Smith PetscFunctionBegin; 1972184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1973184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1974184914b5SBarry Smith PetscCheckSameComm(snes,y); 1975184914b5SBarry Smith 19763a40ed3dSBarry Smith ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr); 19779b94acceSBarry Smith if (norm > *delta) { 19789b94acceSBarry Smith norm = *delta/norm; 19799b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 19809b94acceSBarry Smith cnorm = norm; 19819b94acceSBarry Smith VecScale( &cnorm, y ); 19829b94acceSBarry Smith *ynorm = *delta; 19839b94acceSBarry Smith } else { 19849b94acceSBarry Smith *gpnorm = 0.0; 19859b94acceSBarry Smith *ynorm = norm; 19869b94acceSBarry Smith } 19873a40ed3dSBarry Smith PetscFunctionReturn(0); 19889b94acceSBarry Smith } 19899b94acceSBarry Smith 19905615d1e5SSatish Balay #undef __FUNC__ 19915615d1e5SSatish Balay #define __FUNC__ "SNESSolve" 19929b94acceSBarry Smith /*@ 19939b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 199463a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 19959b94acceSBarry Smith 1996c7afd0dbSLois Curfman McInnes Collective on SNES 1997c7afd0dbSLois Curfman McInnes 1998b2002411SLois Curfman McInnes Input Parameters: 1999c7afd0dbSLois Curfman McInnes + snes - the SNES context 2000c7afd0dbSLois Curfman McInnes - x - the solution vector 20019b94acceSBarry Smith 20029b94acceSBarry Smith Output Parameter: 2003b2002411SLois Curfman McInnes . its - number of iterations until termination 20049b94acceSBarry Smith 2005b2002411SLois Curfman McInnes Notes: 20068ddd3da0SLois Curfman McInnes The user should initialize the vector, x, with the initial guess 20078ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 20088ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 20098ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 20108ddd3da0SLois Curfman McInnes 201136851e7fSLois Curfman McInnes Level: beginner 201236851e7fSLois Curfman McInnes 20139b94acceSBarry Smith .keywords: SNES, nonlinear, solve 20149b94acceSBarry Smith 201563a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 20169b94acceSBarry Smith @*/ 20178ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 20189b94acceSBarry Smith { 2019*f1af5d2fSBarry Smith int ierr; 2020*f1af5d2fSBarry Smith PetscTruth flg; 2021052efed2SBarry Smith 20223a40ed3dSBarry Smith PetscFunctionBegin; 202377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2024184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 2025184914b5SBarry Smith PetscCheckSameComm(snes,x); 202674679c65SBarry Smith PetscValidIntPointer(its); 202782bf6240SBarry Smith if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);} 2028c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 2029758f92a0SBarry Smith if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0; 20309b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 2031c96a6f78SLois Curfman McInnes snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0; 20329b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr); 20339b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 20340f5bd95cSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_view", &flg);CHKERRQ(ierr); 20356d4a8577SBarry Smith if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } 20363a40ed3dSBarry Smith PetscFunctionReturn(0); 20379b94acceSBarry Smith } 20389b94acceSBarry Smith 20399b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 20409b94acceSBarry Smith 20415615d1e5SSatish Balay #undef __FUNC__ 20425615d1e5SSatish Balay #define __FUNC__ "SNESSetType" 204382bf6240SBarry Smith /*@C 20444b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 20459b94acceSBarry Smith 2046fee21e36SBarry Smith Collective on SNES 2047fee21e36SBarry Smith 2048c7afd0dbSLois Curfman McInnes Input Parameters: 2049c7afd0dbSLois Curfman McInnes + snes - the SNES context 2050454a90a3SBarry Smith - type - a known method 2051c7afd0dbSLois Curfman McInnes 2052c7afd0dbSLois Curfman McInnes Options Database Key: 2053454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2054c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2055ae12b187SLois Curfman McInnes 20569b94acceSBarry Smith Notes: 20579b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 20586831982aSBarry Smith + SNESEQLS - Newton's method with line search 2059c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20606831982aSBarry Smith . SNESEQTR - Newton's method with trust region 2061c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 20626831982aSBarry Smith . SNESUMTR - Newton's method with trust region 2063c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20646831982aSBarry Smith - SNESUMLS - Newton's method with line search 2065c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20669b94acceSBarry Smith 2067ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2068ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2069ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2070ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2071ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2072ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2073ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2074ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2075ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 207636851e7fSLois Curfman McInnes appropriate method. In other words, this routine is not for beginners. 207736851e7fSLois Curfman McInnes 207836851e7fSLois Curfman McInnes Level: intermediate 2079a703fe33SLois Curfman McInnes 2080454a90a3SBarry Smith .keywords: SNES, set, type 20819b94acceSBarry Smith @*/ 2082454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type) 20839b94acceSBarry Smith { 20840f5bd95cSBarry Smith int ierr, (*r)(SNES); 20856831982aSBarry Smith PetscTruth match; 20863a40ed3dSBarry Smith 20873a40ed3dSBarry Smith PetscFunctionBegin; 208877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 20890f5bd95cSBarry Smith PetscValidCharPointer(type); 209082bf6240SBarry Smith 20916831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 20920f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 209382bf6240SBarry Smith 209482bf6240SBarry Smith if (snes->setupcalled) { 2095e1311b90SBarry Smith ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr); 209682bf6240SBarry Smith snes->data = 0; 209782bf6240SBarry Smith } 20987d1a2b2bSBarry Smith 20999b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 210082bf6240SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 210182bf6240SBarry Smith 2102454a90a3SBarry Smith ierr = FListFind(snes->comm, SNESList, type,(int (**)(void *)) &r );CHKERRQ(ierr); 210382bf6240SBarry Smith 2104454a90a3SBarry Smith if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",type); 21051548b14aSBarry Smith 2106606d414cSSatish Balay if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);} 210782bf6240SBarry Smith snes->data = 0; 21083a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 210982bf6240SBarry Smith 2110454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 211182bf6240SBarry Smith snes->set_method_called = 1; 211282bf6240SBarry Smith 21133a40ed3dSBarry Smith PetscFunctionReturn(0); 21149b94acceSBarry Smith } 21159b94acceSBarry Smith 2116a847f771SSatish Balay 21179b94acceSBarry Smith /* --------------------------------------------------------------------- */ 21185615d1e5SSatish Balay #undef __FUNC__ 2119d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy" 21209b94acceSBarry Smith /*@C 21219b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2122*f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 21239b94acceSBarry Smith 2124fee21e36SBarry Smith Not Collective 2125fee21e36SBarry Smith 212636851e7fSLois Curfman McInnes Level: advanced 212736851e7fSLois Curfman McInnes 21289b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 21299b94acceSBarry Smith 21309b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 21319b94acceSBarry Smith @*/ 2132cf256101SBarry Smith int SNESRegisterDestroy(void) 21339b94acceSBarry Smith { 213482bf6240SBarry Smith int ierr; 213582bf6240SBarry Smith 21363a40ed3dSBarry Smith PetscFunctionBegin; 213782bf6240SBarry Smith if (SNESList) { 2138488ecbafSBarry Smith ierr = FListDestroy( SNESList );CHKERRQ(ierr); 213982bf6240SBarry Smith SNESList = 0; 21409b94acceSBarry Smith } 214184cb2905SBarry Smith SNESRegisterAllCalled = 0; 21423a40ed3dSBarry Smith PetscFunctionReturn(0); 21439b94acceSBarry Smith } 21449b94acceSBarry Smith 21455615d1e5SSatish Balay #undef __FUNC__ 2146d4bb536fSBarry Smith #define __FUNC__ "SNESGetType" 21479b94acceSBarry Smith /*@C 21489a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 21499b94acceSBarry Smith 2150c7afd0dbSLois Curfman McInnes Not Collective 2151c7afd0dbSLois Curfman McInnes 21529b94acceSBarry Smith Input Parameter: 21534b0e389bSBarry Smith . snes - nonlinear solver context 21549b94acceSBarry Smith 21559b94acceSBarry Smith Output Parameter: 2156454a90a3SBarry Smith . type - SNES method (a charactor string) 21579b94acceSBarry Smith 215836851e7fSLois Curfman McInnes Level: intermediate 215936851e7fSLois Curfman McInnes 2160454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 21619b94acceSBarry Smith @*/ 2162454a90a3SBarry Smith int SNESGetType(SNES snes, SNESType *type) 21639b94acceSBarry Smith { 21643a40ed3dSBarry Smith PetscFunctionBegin; 2165184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2166454a90a3SBarry Smith *type = snes->type_name; 21673a40ed3dSBarry Smith PetscFunctionReturn(0); 21689b94acceSBarry Smith } 21699b94acceSBarry Smith 21705615d1e5SSatish Balay #undef __FUNC__ 2171d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution" 21729b94acceSBarry Smith /*@C 21739b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 21749b94acceSBarry Smith stored. 21759b94acceSBarry Smith 2176c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2177c7afd0dbSLois Curfman McInnes 21789b94acceSBarry Smith Input Parameter: 21799b94acceSBarry Smith . snes - the SNES context 21809b94acceSBarry Smith 21819b94acceSBarry Smith Output Parameter: 21829b94acceSBarry Smith . x - the solution 21839b94acceSBarry Smith 218436851e7fSLois Curfman McInnes Level: advanced 218536851e7fSLois Curfman McInnes 21869b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 21879b94acceSBarry Smith 2188a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 21899b94acceSBarry Smith @*/ 21909b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 21919b94acceSBarry Smith { 21923a40ed3dSBarry Smith PetscFunctionBegin; 219377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 21949b94acceSBarry Smith *x = snes->vec_sol_always; 21953a40ed3dSBarry Smith PetscFunctionReturn(0); 21969b94acceSBarry Smith } 21979b94acceSBarry Smith 21985615d1e5SSatish Balay #undef __FUNC__ 2199d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate" 22009b94acceSBarry Smith /*@C 22019b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 22029b94acceSBarry Smith stored. 22039b94acceSBarry Smith 2204c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2205c7afd0dbSLois Curfman McInnes 22069b94acceSBarry Smith Input Parameter: 22079b94acceSBarry Smith . snes - the SNES context 22089b94acceSBarry Smith 22099b94acceSBarry Smith Output Parameter: 22109b94acceSBarry Smith . x - the solution update 22119b94acceSBarry Smith 221236851e7fSLois Curfman McInnes Level: advanced 221336851e7fSLois Curfman McInnes 22149b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 22159b94acceSBarry Smith 22169b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 22179b94acceSBarry Smith @*/ 22189b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 22199b94acceSBarry Smith { 22203a40ed3dSBarry Smith PetscFunctionBegin; 222177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22229b94acceSBarry Smith *x = snes->vec_sol_update_always; 22233a40ed3dSBarry Smith PetscFunctionReturn(0); 22249b94acceSBarry Smith } 22259b94acceSBarry Smith 22265615d1e5SSatish Balay #undef __FUNC__ 2227d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction" 22289b94acceSBarry Smith /*@C 22293638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 22309b94acceSBarry Smith 2231c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2232c7afd0dbSLois Curfman McInnes 22339b94acceSBarry Smith Input Parameter: 22349b94acceSBarry Smith . snes - the SNES context 22359b94acceSBarry Smith 22369b94acceSBarry Smith Output Parameter: 22377bf4e008SBarry Smith + r - the function (or PETSC_NULL) 22387bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 22399b94acceSBarry Smith 22409b94acceSBarry Smith Notes: 22419b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 22429b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 22439b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 22449b94acceSBarry Smith 224536851e7fSLois Curfman McInnes Level: advanced 224636851e7fSLois Curfman McInnes 2247a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 22489b94acceSBarry Smith 224931615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 225031615d04SLois Curfman McInnes SNESGetGradient() 22517bf4e008SBarry Smith 22529b94acceSBarry Smith @*/ 22537bf4e008SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx) 22549b94acceSBarry Smith { 22553a40ed3dSBarry Smith PetscFunctionBegin; 225677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2257a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 2258a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 2259a8c6a408SBarry Smith } 22607bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22617bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22623a40ed3dSBarry Smith PetscFunctionReturn(0); 22639b94acceSBarry Smith } 22649b94acceSBarry Smith 22655615d1e5SSatish Balay #undef __FUNC__ 2266d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient" 22679b94acceSBarry Smith /*@C 22683638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 22699b94acceSBarry Smith 2270c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2271c7afd0dbSLois Curfman McInnes 22729b94acceSBarry Smith Input Parameter: 22739b94acceSBarry Smith . snes - the SNES context 22749b94acceSBarry Smith 22759b94acceSBarry Smith Output Parameter: 22767bf4e008SBarry Smith + r - the gradient (or PETSC_NULL) 22777bf4e008SBarry Smith - ctx - the gradient context (or PETSC_NULL) 22789b94acceSBarry Smith 22799b94acceSBarry Smith Notes: 22809b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 22819b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 22829b94acceSBarry Smith SNESGetFunction(). 22839b94acceSBarry Smith 228436851e7fSLois Curfman McInnes Level: advanced 228536851e7fSLois Curfman McInnes 22869b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 22879b94acceSBarry Smith 22887bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(), 22897bf4e008SBarry Smith SNESSetGradient(), SNESSetFunction() 22907bf4e008SBarry Smith 22919b94acceSBarry Smith @*/ 22927bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx) 22939b94acceSBarry Smith { 22943a40ed3dSBarry Smith PetscFunctionBegin; 229577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22963a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2297a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 22983a40ed3dSBarry Smith } 22997bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 23007bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 23013a40ed3dSBarry Smith PetscFunctionReturn(0); 23029b94acceSBarry Smith } 23039b94acceSBarry Smith 23045615d1e5SSatish Balay #undef __FUNC__ 2305d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction" 23067bf4e008SBarry Smith /*@C 23079b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 23089b94acceSBarry Smith unconstrained minimization problems. 23099b94acceSBarry Smith 2310c7afd0dbSLois Curfman McInnes Not Collective 2311c7afd0dbSLois Curfman McInnes 23129b94acceSBarry Smith Input Parameter: 23139b94acceSBarry Smith . snes - the SNES context 23149b94acceSBarry Smith 23159b94acceSBarry Smith Output Parameter: 23167bf4e008SBarry Smith + r - the function (or PETSC_NULL) 23177bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 23189b94acceSBarry Smith 23199b94acceSBarry Smith Notes: 23209b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 23219b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 23229b94acceSBarry Smith SNESGetFunction(). 23239b94acceSBarry Smith 232436851e7fSLois Curfman McInnes Level: advanced 232536851e7fSLois Curfman McInnes 23269b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 23279b94acceSBarry Smith 23287bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction() 23297bf4e008SBarry Smith 23309b94acceSBarry Smith @*/ 23317bf4e008SBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r,void **ctx) 23329b94acceSBarry Smith { 23333a40ed3dSBarry Smith PetscFunctionBegin; 233477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 233574679c65SBarry Smith PetscValidScalarPointer(r); 23363a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2337a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 23383a40ed3dSBarry Smith } 23397bf4e008SBarry Smith if (r) *r = snes->fc; 23407bf4e008SBarry Smith if (ctx) *ctx = snes->umfunP; 23413a40ed3dSBarry Smith PetscFunctionReturn(0); 23429b94acceSBarry Smith } 23439b94acceSBarry Smith 23445615d1e5SSatish Balay #undef __FUNC__ 2345d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix" 23463c7409f5SSatish Balay /*@C 23473c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2348d850072dSLois Curfman McInnes SNES options in the database. 23493c7409f5SSatish Balay 2350fee21e36SBarry Smith Collective on SNES 2351fee21e36SBarry Smith 2352c7afd0dbSLois Curfman McInnes Input Parameter: 2353c7afd0dbSLois Curfman McInnes + snes - the SNES context 2354c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2355c7afd0dbSLois Curfman McInnes 2356d850072dSLois Curfman McInnes Notes: 2357a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2358c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2359d850072dSLois Curfman McInnes 236036851e7fSLois Curfman McInnes Level: advanced 236136851e7fSLois Curfman McInnes 23623c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2363a86d99e1SLois Curfman McInnes 2364a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 23653c7409f5SSatish Balay @*/ 23663c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 23673c7409f5SSatish Balay { 23683c7409f5SSatish Balay int ierr; 23693c7409f5SSatish Balay 23703a40ed3dSBarry Smith PetscFunctionBegin; 237177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2372639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 23733c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 23743a40ed3dSBarry Smith PetscFunctionReturn(0); 23753c7409f5SSatish Balay } 23763c7409f5SSatish Balay 23775615d1e5SSatish Balay #undef __FUNC__ 2378d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix" 23793c7409f5SSatish Balay /*@C 2380f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2381d850072dSLois Curfman McInnes SNES options in the database. 23823c7409f5SSatish Balay 2383fee21e36SBarry Smith Collective on SNES 2384fee21e36SBarry Smith 2385c7afd0dbSLois Curfman McInnes Input Parameters: 2386c7afd0dbSLois Curfman McInnes + snes - the SNES context 2387c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2388c7afd0dbSLois Curfman McInnes 2389d850072dSLois Curfman McInnes Notes: 2390a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2391c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2392d850072dSLois Curfman McInnes 239336851e7fSLois Curfman McInnes Level: advanced 239436851e7fSLois Curfman McInnes 23953c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2396a86d99e1SLois Curfman McInnes 2397a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 23983c7409f5SSatish Balay @*/ 23993c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 24003c7409f5SSatish Balay { 24013c7409f5SSatish Balay int ierr; 24023c7409f5SSatish Balay 24033a40ed3dSBarry Smith PetscFunctionBegin; 240477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2405639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 24063c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 24073a40ed3dSBarry Smith PetscFunctionReturn(0); 24083c7409f5SSatish Balay } 24093c7409f5SSatish Balay 24105615d1e5SSatish Balay #undef __FUNC__ 2411d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix" 24129ab63eb5SSatish Balay /*@C 24133c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 24143c7409f5SSatish Balay SNES options in the database. 24153c7409f5SSatish Balay 2416c7afd0dbSLois Curfman McInnes Not Collective 2417c7afd0dbSLois Curfman McInnes 24183c7409f5SSatish Balay Input Parameter: 24193c7409f5SSatish Balay . snes - the SNES context 24203c7409f5SSatish Balay 24213c7409f5SSatish Balay Output Parameter: 24223c7409f5SSatish Balay . prefix - pointer to the prefix string used 24233c7409f5SSatish Balay 24249ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 24259ab63eb5SSatish Balay sufficient length to hold the prefix. 24269ab63eb5SSatish Balay 242736851e7fSLois Curfman McInnes Level: advanced 242836851e7fSLois Curfman McInnes 24293c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2430a86d99e1SLois Curfman McInnes 2431a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 24323c7409f5SSatish Balay @*/ 24333c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 24343c7409f5SSatish Balay { 24353c7409f5SSatish Balay int ierr; 24363c7409f5SSatish Balay 24373a40ed3dSBarry Smith PetscFunctionBegin; 243877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2439639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 24403a40ed3dSBarry Smith PetscFunctionReturn(0); 24413c7409f5SSatish Balay } 24423c7409f5SSatish Balay 2443ca161407SBarry Smith #undef __FUNC__ 2444ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp" 2445ca161407SBarry Smith /*@ 2446ca161407SBarry Smith SNESPrintHelp - Prints all options for the SNES component. 2447ca161407SBarry Smith 2448c7afd0dbSLois Curfman McInnes Collective on SNES 2449c7afd0dbSLois Curfman McInnes 2450ca161407SBarry Smith Input Parameter: 2451ca161407SBarry Smith . snes - the SNES context 2452ca161407SBarry Smith 2453ca161407SBarry Smith Options Database Keys: 2454c7afd0dbSLois Curfman McInnes + -help - Prints SNES options 2455c7afd0dbSLois Curfman McInnes - -h - Prints SNES options 2456ca161407SBarry Smith 245736851e7fSLois Curfman McInnes Level: beginner 245836851e7fSLois Curfman McInnes 2459ca161407SBarry Smith .keywords: SNES, nonlinear, help 2460ca161407SBarry Smith 2461ca161407SBarry Smith .seealso: SNESSetFromOptions() 2462ca161407SBarry Smith @*/ 2463ca161407SBarry Smith int SNESPrintHelp(SNES snes) 2464ca161407SBarry Smith { 2465ca161407SBarry Smith char p[64]; 2466ca161407SBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2467ca161407SBarry Smith int ierr; 2468ca161407SBarry Smith 2469ca161407SBarry Smith PetscFunctionBegin; 2470ca161407SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2471ca161407SBarry Smith 2472549d3d68SSatish Balay ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 2473ca161407SBarry Smith if (snes->prefix) PetscStrcat(p, snes->prefix); 2474ca161407SBarry Smith 2475ca161407SBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2476ca161407SBarry Smith 2477ebb8b11fSBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2478d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr); 2479488ecbafSBarry Smith ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr); 2480d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr); 2481d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr); 2482d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr); 2483d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr); 2484d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr); 2485d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr); 2486d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr); 2487d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr); 2488d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr); 2489d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_monitor: use default SNES convergence monitor, prints\n\ 2490d132466eSBarry Smith residual norm at each iteration.\n",p);CHKERRQ(ierr); 2491d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_smonitor: same as the above, but prints fewer digits of the\n\ 2492ca161407SBarry Smith residual norm for small residual norms. This is useful to conceal\n\ 2493d132466eSBarry Smith meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr); 2494d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr); 2495d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr); 2496d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr); 2497ca161407SBarry Smith if (snes->type == SNES_NONLINEAR_EQUATIONS) { 2498d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2499d132466eSBarry Smith " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr); 2500d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr); 2501d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr); 2502d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr); 2503d132466eSBarry 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); 2504d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr); 2505d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr); 2506d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2507d132466eSBarry Smith " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr); 2508d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2509d132466eSBarry Smith " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr); 2510d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2511d132466eSBarry Smith " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr); 2512d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2513d132466eSBarry Smith " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr); 2514d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2515d132466eSBarry Smith " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr); 2516d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2517d132466eSBarry Smith " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr); 2518d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2519d132466eSBarry Smith " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr); 2520ca161407SBarry Smith } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 2521d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr); 2522d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr); 2523d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr); 2524d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr); 2525ca161407SBarry Smith } 2526454a90a3SBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <type> for help on ",p);CHKERRQ(ierr); 2527d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr); 2528ca161407SBarry Smith if (snes->printhelp) { 2529ca161407SBarry Smith ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr); 2530ca161407SBarry Smith } 2531ca161407SBarry Smith PetscFunctionReturn(0); 2532ca161407SBarry Smith } 25333c7409f5SSatish Balay 2534acb85ae6SSatish Balay /*MC 2535*f1af5d2fSBarry Smith SNESRegisterDynamic - Adds a method to the nonlinear solver package. 25369b94acceSBarry Smith 2537b2002411SLois Curfman McInnes Synopsis: 2538*f1af5d2fSBarry Smith SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES)) 25399b94acceSBarry Smith 25408d76a1e5SLois Curfman McInnes Not collective 25418d76a1e5SLois Curfman McInnes 2542b2002411SLois Curfman McInnes Input Parameters: 2543c7afd0dbSLois Curfman McInnes + name_solver - name of a new user-defined solver 2544b2002411SLois Curfman McInnes . path - path (either absolute or relative) the library containing this solver 2545b2002411SLois Curfman McInnes . name_create - name of routine to create method context 2546c7afd0dbSLois Curfman McInnes - routine_create - routine to create method context 25479b94acceSBarry Smith 2548b2002411SLois Curfman McInnes Notes: 2549*f1af5d2fSBarry Smith SNESRegisterDynamic() may be called multiple times to add several user-defined solvers. 25503a40ed3dSBarry Smith 2551b2002411SLois Curfman McInnes If dynamic libraries are used, then the fourth input argument (routine_create) 2552b2002411SLois Curfman McInnes is ignored. 2553b2002411SLois Curfman McInnes 2554b2002411SLois Curfman McInnes Sample usage: 255569225d78SLois Curfman McInnes .vb 2556*f1af5d2fSBarry Smith SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a, 2557b2002411SLois Curfman McInnes "MySolverCreate",MySolverCreate); 255869225d78SLois Curfman McInnes .ve 2559b2002411SLois Curfman McInnes 2560b2002411SLois Curfman McInnes Then, your solver can be chosen with the procedural interface via 2561b2002411SLois Curfman McInnes $ SNESSetType(snes,"my_solver") 2562b2002411SLois Curfman McInnes or at runtime via the option 2563b2002411SLois Curfman McInnes $ -snes_type my_solver 2564b2002411SLois Curfman McInnes 256536851e7fSLois Curfman McInnes Level: advanced 256636851e7fSLois Curfman McInnes 256785614651SBarry Smith $PETSC_ARCH, $PETSC_DIR, $PETSC_LDIR, and $BOPT occuring in pathname will be replaced with appropriate values. 2568dd438238SBarry Smith 2569b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register 2570b2002411SLois Curfman McInnes 2571b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy() 2572b2002411SLois Curfman McInnes M*/ 2573b2002411SLois Curfman McInnes 2574b2002411SLois Curfman McInnes #undef __FUNC__ 2575*f1af5d2fSBarry Smith #define __FUNC__ "SNESRegister" 2576*f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES)) 2577b2002411SLois Curfman McInnes { 2578b2002411SLois Curfman McInnes char fullname[256]; 2579b2002411SLois Curfman McInnes int ierr; 2580b2002411SLois Curfman McInnes 2581b2002411SLois Curfman McInnes PetscFunctionBegin; 2582*f1af5d2fSBarry Smith ierr = FListConcat(path,name,fullname); CHKERRQ(ierr); 2583*f1af5d2fSBarry Smith ierr = FListAdd(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr); 2584b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2585b2002411SLois Curfman McInnes } 2586