1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*0f5bd95cSBarry Smith static char vcid[] = "$Id: snes.c,v 1.198 1999/10/06 23:41:24 balay Exp bsmith $"; 39b94acceSBarry Smith #endif 49b94acceSBarry Smith 570f55243SBarry Smith #include "src/snes/snesimpl.h" /*I "snes.h" I*/ 69b94acceSBarry Smith 784cb2905SBarry Smith int SNESRegisterAllCalled = 0; 8488ecbafSBarry Smith FList SNESList = 0; 982bf6240SBarry Smith 105615d1e5SSatish Balay #undef __FUNC__ 11d4bb536fSBarry Smith #define __FUNC__ "SNESView" 129b94acceSBarry Smith /*@ 139b94acceSBarry Smith SNESView - Prints the SNES data structure. 149b94acceSBarry Smith 15fee21e36SBarry Smith Collective on SNES, unless Viewer is VIEWER_STDOUT_SELF 16fee21e36SBarry Smith 17c7afd0dbSLois Curfman McInnes Input Parameters: 18c7afd0dbSLois Curfman McInnes + SNES - the SNES context 19c7afd0dbSLois Curfman McInnes - viewer - visualization context 20c7afd0dbSLois Curfman McInnes 219b94acceSBarry Smith Options Database Key: 22c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 239b94acceSBarry Smith 249b94acceSBarry Smith Notes: 259b94acceSBarry Smith The available visualization contexts include 26c7afd0dbSLois Curfman McInnes + VIEWER_STDOUT_SELF - standard output (default) 27c7afd0dbSLois Curfman McInnes - VIEWER_STDOUT_WORLD - synchronized standard 28c8a8ba5cSLois Curfman McInnes output where only the first processor opens 29c8a8ba5cSLois Curfman McInnes the file. All other processors send their 30c8a8ba5cSLois Curfman McInnes data to the first processor to print. 319b94acceSBarry Smith 323e081fefSLois Curfman McInnes The user can open an alternative visualization context with 3377ed5343SBarry Smith ViewerASCIIOpen() - output to a specified file. 349b94acceSBarry Smith 3536851e7fSLois Curfman McInnes Level: beginner 3636851e7fSLois Curfman McInnes 379b94acceSBarry Smith .keywords: SNES, view 389b94acceSBarry Smith 3977ed5343SBarry Smith .seealso: ViewerASCIIOpen() 409b94acceSBarry Smith @*/ 419b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer) 429b94acceSBarry Smith { 439b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 449b94acceSBarry Smith int ierr; 459b94acceSBarry Smith SLES sles; 46454a90a3SBarry Smith char *type; 47*0f5bd95cSBarry Smith int isascii,isstring; 489b94acceSBarry Smith 493a40ed3dSBarry Smith PetscFunctionBegin; 5074679c65SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 51*0f5bd95cSBarry Smith if (!viewer) viewer = VIEWER_STDOUT_SELF; 52*0f5bd95cSBarry Smith PetscValidHeaderSpecific(viewer,VIEWER_COOKIE); 5374679c65SBarry Smith 54*0f5bd95cSBarry Smith isascii = PetscTypeCompare(viewer,ASCII_VIEWER); 55*0f5bd95cSBarry Smith isstring = PetscTypeCompare(viewer,STRING_VIEWER); 56*0f5bd95cSBarry Smith if (isascii) { 57d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr); 58454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 59454a90a3SBarry Smith if (type) { 60454a90a3SBarry Smith ierr = ViewerASCIIPrintf(viewer," type: %s\n",type);CHKERRQ(ierr); 61184914b5SBarry Smith } else { 62454a90a3SBarry Smith ierr = ViewerASCIIPrintf(viewer," type: not set yet\n");CHKERRQ(ierr); 63184914b5SBarry Smith } 640ef38995SBarry Smith if (snes->view) { 650ef38995SBarry Smith ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 660ef38995SBarry Smith ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr); 670ef38995SBarry Smith ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 680ef38995SBarry Smith } 69d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 70d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 71d132466eSBarry Smith snes->rtol, snes->atol, snes->trunctol, snes->xtol);CHKERRQ(ierr); 72d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr); 73d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr); 740ef38995SBarry Smith if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 75d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr); 760ef38995SBarry Smith } 779b94acceSBarry Smith if (snes->ksp_ewconv) { 789b94acceSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 799b94acceSBarry Smith if (kctx) { 80d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr); 81d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 82d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 839b94acceSBarry Smith } 849b94acceSBarry Smith } 85*0f5bd95cSBarry Smith } else if (isstring) { 86454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 87454a90a3SBarry Smith ierr = ViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 8819bcc07fSBarry Smith } 8977ed5343SBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 900ef38995SBarry Smith ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 919b94acceSBarry Smith ierr = SLESView(sles,viewer);CHKERRQ(ierr); 920ef38995SBarry Smith ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 933a40ed3dSBarry Smith PetscFunctionReturn(0); 949b94acceSBarry Smith } 959b94acceSBarry Smith 96639f9d9dSBarry Smith /* 97639f9d9dSBarry Smith We retain a list of functions that also take SNES command 98639f9d9dSBarry Smith line options. These are called at the end SNESSetFromOptions() 99639f9d9dSBarry Smith */ 100639f9d9dSBarry Smith #define MAXSETFROMOPTIONS 5 101639f9d9dSBarry Smith static int numberofsetfromoptions; 102639f9d9dSBarry Smith static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 103639f9d9dSBarry Smith 1045615d1e5SSatish Balay #undef __FUNC__ 105d4bb536fSBarry Smith #define __FUNC__ "SNESAddOptionsChecker" 106639f9d9dSBarry Smith /*@ 107639f9d9dSBarry Smith SNESAddOptionsChecker - Adds an additional function to check for SNES options. 108639f9d9dSBarry Smith 109c7afd0dbSLois Curfman McInnes Not Collective 110c7afd0dbSLois Curfman McInnes 111639f9d9dSBarry Smith Input Parameter: 112639f9d9dSBarry Smith . snescheck - function that checks for options 113639f9d9dSBarry Smith 11436851e7fSLois Curfman McInnes Level: developer 11536851e7fSLois Curfman McInnes 116639f9d9dSBarry Smith .seealso: SNESSetFromOptions() 117639f9d9dSBarry Smith @*/ 118639f9d9dSBarry Smith int SNESAddOptionsChecker(int (*snescheck)(SNES) ) 119639f9d9dSBarry Smith { 1203a40ed3dSBarry Smith PetscFunctionBegin; 121639f9d9dSBarry Smith if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 122a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many options checkers, only 5 allowed"); 123639f9d9dSBarry Smith } 124639f9d9dSBarry Smith 125639f9d9dSBarry Smith othersetfromoptions[numberofsetfromoptions++] = snescheck; 1263a40ed3dSBarry Smith PetscFunctionReturn(0); 127639f9d9dSBarry Smith } 128639f9d9dSBarry Smith 1295615d1e5SSatish Balay #undef __FUNC__ 13015091d37SBarry Smith #define __FUNC__ "SNESSetTypeFromOptions" 13115091d37SBarry Smith /*@ 13215091d37SBarry Smith SNESSetTypeFromOptions - Sets the SNES solver type from the options database, 13315091d37SBarry Smith or sets a default if none is give. 13415091d37SBarry Smith 13515091d37SBarry Smith Collective on SNES 13615091d37SBarry Smith 13715091d37SBarry Smith Input Parameter: 13815091d37SBarry Smith . snes - the SNES context 13915091d37SBarry Smith 14015091d37SBarry Smith Options Database Keys: 14115091d37SBarry Smith . -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc 14215091d37SBarry Smith 14315091d37SBarry Smith Level: beginner 14415091d37SBarry Smith 14515091d37SBarry Smith .keywords: SNES, nonlinear, set, options, database 14615091d37SBarry Smith 14715091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetFromOptions() 14815091d37SBarry Smith @*/ 14915091d37SBarry Smith int SNESSetTypeFromOptions(SNES snes) 15015091d37SBarry Smith { 151454a90a3SBarry Smith char type[256]; 15215091d37SBarry Smith int ierr, 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) { 16615091d37SBarry Smith ierr = SNESSetType(snes,SNES_EQ_LS);CHKERRQ(ierr); 16715091d37SBarry Smith } else { 16815091d37SBarry Smith ierr = SNESSetType(snes,SNES_UM_TR);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: 185b39c3a46SLois Curfman McInnes + -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc 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; 22881f57ec7SBarry Smith int ierr, flg,i,loc[4],nmax = 4; 2293c7409f5SSatish Balay int version = PETSC_DEFAULT; 2309b94acceSBarry Smith double rtol_0 = PETSC_DEFAULT; 2319b94acceSBarry Smith double rtol_max = PETSC_DEFAULT; 2329b94acceSBarry Smith double gamma2 = PETSC_DEFAULT; 2339b94acceSBarry Smith double alpha = PETSC_DEFAULT; 2349b94acceSBarry Smith double alpha2 = PETSC_DEFAULT; 2359b94acceSBarry Smith double threshold = PETSC_DEFAULT; 2369b94acceSBarry Smith 2373a40ed3dSBarry Smith PetscFunctionBegin; 23877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 23915091d37SBarry Smith ierr = SNESSetTypeFromOptions(snes);CHKERRQ(ierr); 240ca161407SBarry Smith 24115091d37SBarry Smith loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300; 2423c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg);CHKERRQ(ierr); 243d64ed03dSBarry Smith if (flg) { 244d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 245d64ed03dSBarry Smith } 2463c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg);CHKERRQ(ierr); 247d64ed03dSBarry Smith if (flg) { 248d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 249d64ed03dSBarry Smith } 2503c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg);CHKERRQ(ierr); 251d64ed03dSBarry Smith if (flg) { 252d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 253d64ed03dSBarry Smith } 2543c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg);CHKERRQ(ierr); 2553c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr); 256d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg);CHKERRQ(ierr); 257d64ed03dSBarry Smith if (flg) { ierr = SNESSetTrustRegionTolerance(snes,tmp);CHKERRQ(ierr); } 258d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg);CHKERRQ(ierr); 259d64ed03dSBarry Smith if (flg) { ierr = SNESSetMinimizationFunctionTolerance(snes,tmp);CHKERRQ(ierr);} 2603c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg);CHKERRQ(ierr); 2613c7409f5SSatish Balay if (flg) { snes->ksp_ewconv = 1; } 262b18e04deSLois Curfman McInnes ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg);CHKERRQ(ierr); 263b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg);CHKERRQ(ierr); 264b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr); 265b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg);CHKERRQ(ierr); 266b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg);CHKERRQ(ierr); 267b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg);CHKERRQ(ierr); 268b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr); 26993c39befSBarry Smith 27093c39befSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_no_convergence_test",&flg);CHKERRQ(ierr); 27193c39befSBarry Smith if (flg) {snes->converged = 0;} 27293c39befSBarry Smith 2739b94acceSBarry Smith ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 2749b94acceSBarry Smith alpha2,threshold);CHKERRQ(ierr); 2755bbad29bSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_cancelmonitors",&flg);CHKERRQ(ierr); 2765cd90555SBarry Smith if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);} 2773c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg);CHKERRQ(ierr); 278b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);} 2793c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg);CHKERRQ(ierr); 280b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);} 2813f1db9ecSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor",&flg);CHKERRQ(ierr); 282b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);} 283d132466eSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor_update",&flg);CHKERRQ(ierr); 284b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitorUpdate,0,0);CHKERRQ(ierr);} 28581f57ec7SBarry Smith ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr); 2863c7409f5SSatish Balay if (flg) { 28717699dbbSLois Curfman McInnes int rank = 0; 288d7e8b826SBarry Smith DrawLG lg; 28917699dbbSLois Curfman McInnes MPI_Initialized(&rank); 290d132466eSBarry Smith if (rank) {ierr = MPI_Comm_rank(snes->comm,&rank);CHKERRQ(ierr);} 29117699dbbSLois Curfman McInnes if (!rank) { 29281f57ec7SBarry Smith ierr = SNESLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg);CHKERRQ(ierr); 293b8a78c4aSBarry Smith ierr = SNESSetMonitor(snes,SNESLGMonitor,lg,( int (*)(void *))SNESLGMonitorDestroy);CHKERRQ(ierr); 2949b94acceSBarry Smith PLogObjectParent(snes,lg); 2959b94acceSBarry Smith } 2969b94acceSBarry Smith } 297e24b481bSBarry Smith 2983c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);CHKERRQ(ierr); 2993c7409f5SSatish Balay if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 3009b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 3019b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 302981c4779SBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 303981c4779SBarry Smith } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 30431615d04SLois Curfman McInnes ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 30531615d04SLois Curfman McInnes SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr); 306d64ed03dSBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 3079b94acceSBarry Smith } 308639f9d9dSBarry Smith 309639f9d9dSBarry Smith for ( i=0; i<numberofsetfromoptions; i++ ) { 310639f9d9dSBarry Smith ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 311639f9d9dSBarry Smith } 312639f9d9dSBarry Smith 3139b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 3149b94acceSBarry Smith ierr = SLESSetFromOptions(sles);CHKERRQ(ierr); 31593993e2dSLois Curfman McInnes 316e24b481bSBarry Smith /* set the special KSP monitor for matrix-free application */ 317e24b481bSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg);CHKERRQ(ierr); 318e24b481bSBarry Smith if (flg) { 319e24b481bSBarry Smith KSP ksp; 320e24b481bSBarry Smith ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 321b8a78c4aSBarry Smith ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr); 322e24b481bSBarry Smith } 323e24b481bSBarry Smith 32482bf6240SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help", &flg);CHKERRQ(ierr); 32582bf6240SBarry Smith if (flg) { ierr = SNESPrintHelp(snes);CHKERRQ(ierr);} 32682bf6240SBarry Smith 3273a40ed3dSBarry Smith if (snes->setfromoptions) { 3283a40ed3dSBarry Smith ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr); 3293a40ed3dSBarry Smith } 3303a40ed3dSBarry Smith PetscFunctionReturn(0); 3319b94acceSBarry Smith } 3329b94acceSBarry Smith 333a847f771SSatish Balay 3345615d1e5SSatish Balay #undef __FUNC__ 335d4bb536fSBarry Smith #define __FUNC__ "SNESSetApplicationContext" 3369b94acceSBarry Smith /*@ 3379b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3389b94acceSBarry Smith the nonlinear solvers. 3399b94acceSBarry Smith 340fee21e36SBarry Smith Collective on SNES 341fee21e36SBarry Smith 342c7afd0dbSLois Curfman McInnes Input Parameters: 343c7afd0dbSLois Curfman McInnes + snes - the SNES context 344c7afd0dbSLois Curfman McInnes - usrP - optional user context 345c7afd0dbSLois Curfman McInnes 34636851e7fSLois Curfman McInnes Level: intermediate 34736851e7fSLois Curfman McInnes 3489b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3499b94acceSBarry Smith 3509b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3519b94acceSBarry Smith @*/ 3529b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 3539b94acceSBarry Smith { 3543a40ed3dSBarry Smith PetscFunctionBegin; 35577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3569b94acceSBarry Smith snes->user = usrP; 3573a40ed3dSBarry Smith PetscFunctionReturn(0); 3589b94acceSBarry Smith } 35974679c65SBarry Smith 3605615d1e5SSatish Balay #undef __FUNC__ 361d4bb536fSBarry Smith #define __FUNC__ "SNESGetApplicationContext" 3629b94acceSBarry Smith /*@C 3639b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3649b94acceSBarry Smith nonlinear solvers. 3659b94acceSBarry Smith 366c7afd0dbSLois Curfman McInnes Not Collective 367c7afd0dbSLois Curfman McInnes 3689b94acceSBarry Smith Input Parameter: 3699b94acceSBarry Smith . snes - SNES context 3709b94acceSBarry Smith 3719b94acceSBarry Smith Output Parameter: 3729b94acceSBarry Smith . usrP - user context 3739b94acceSBarry Smith 37436851e7fSLois Curfman McInnes Level: intermediate 37536851e7fSLois Curfman McInnes 3769b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3779b94acceSBarry Smith 3789b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3799b94acceSBarry Smith @*/ 3809b94acceSBarry Smith int SNESGetApplicationContext( SNES snes, void **usrP ) 3819b94acceSBarry Smith { 3823a40ed3dSBarry Smith PetscFunctionBegin; 38377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3849b94acceSBarry Smith *usrP = snes->user; 3853a40ed3dSBarry Smith PetscFunctionReturn(0); 3869b94acceSBarry Smith } 38774679c65SBarry Smith 3885615d1e5SSatish Balay #undef __FUNC__ 389d4bb536fSBarry Smith #define __FUNC__ "SNESGetIterationNumber" 3909b94acceSBarry Smith /*@ 391c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 392c8228a4eSBarry Smith at this time. 3939b94acceSBarry Smith 394c7afd0dbSLois Curfman McInnes Not Collective 395c7afd0dbSLois Curfman McInnes 3969b94acceSBarry Smith Input Parameter: 3979b94acceSBarry Smith . snes - SNES context 3989b94acceSBarry Smith 3999b94acceSBarry Smith Output Parameter: 4009b94acceSBarry Smith . iter - iteration number 4019b94acceSBarry Smith 402c8228a4eSBarry Smith Notes: 403c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 404c8228a4eSBarry Smith 405c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 40608405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 40708405cd6SLois Curfman McInnes .vb 40808405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 40908405cd6SLois Curfman McInnes if (!(it % 2)) { 41008405cd6SLois Curfman McInnes [compute Jacobian here] 41108405cd6SLois Curfman McInnes } 41208405cd6SLois Curfman McInnes .ve 413c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 41408405cd6SLois Curfman McInnes recomputed every second SNES iteration. 415c8228a4eSBarry Smith 41636851e7fSLois Curfman McInnes Level: intermediate 41736851e7fSLois Curfman McInnes 4189b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 4199b94acceSBarry Smith @*/ 4209b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 4219b94acceSBarry Smith { 4223a40ed3dSBarry Smith PetscFunctionBegin; 42377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 42474679c65SBarry Smith PetscValidIntPointer(iter); 4259b94acceSBarry Smith *iter = snes->iter; 4263a40ed3dSBarry Smith PetscFunctionReturn(0); 4279b94acceSBarry Smith } 42874679c65SBarry Smith 4295615d1e5SSatish Balay #undef __FUNC__ 4305615d1e5SSatish Balay #define __FUNC__ "SNESGetFunctionNorm" 4319b94acceSBarry Smith /*@ 4329b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4339b94acceSBarry Smith with SNESSSetFunction(). 4349b94acceSBarry Smith 435c7afd0dbSLois Curfman McInnes Collective on SNES 436c7afd0dbSLois Curfman McInnes 4379b94acceSBarry Smith Input Parameter: 4389b94acceSBarry Smith . snes - SNES context 4399b94acceSBarry Smith 4409b94acceSBarry Smith Output Parameter: 4419b94acceSBarry Smith . fnorm - 2-norm of function 4429b94acceSBarry Smith 4439b94acceSBarry Smith Note: 4449b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 445a86d99e1SLois Curfman McInnes A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 446a86d99e1SLois Curfman McInnes SNESGetGradientNorm(). 4479b94acceSBarry Smith 44836851e7fSLois Curfman McInnes Level: intermediate 44936851e7fSLois Curfman McInnes 4509b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 451a86d99e1SLois Curfman McInnes 45208405cd6SLois Curfman McInnes .seealso: SNESGetFunction() 4539b94acceSBarry Smith @*/ 4549b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 4559b94acceSBarry Smith { 4563a40ed3dSBarry Smith PetscFunctionBegin; 45777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 45874679c65SBarry Smith PetscValidScalarPointer(fnorm); 45974679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 460d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only"); 46174679c65SBarry Smith } 4629b94acceSBarry Smith *fnorm = snes->norm; 4633a40ed3dSBarry Smith PetscFunctionReturn(0); 4649b94acceSBarry Smith } 46574679c65SBarry Smith 4665615d1e5SSatish Balay #undef __FUNC__ 4675615d1e5SSatish Balay #define __FUNC__ "SNESGetGradientNorm" 4689b94acceSBarry Smith /*@ 4699b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 4709b94acceSBarry Smith with SNESSSetGradient(). 4719b94acceSBarry Smith 472c7afd0dbSLois Curfman McInnes Collective on SNES 473c7afd0dbSLois Curfman McInnes 4749b94acceSBarry Smith Input Parameter: 4759b94acceSBarry Smith . snes - SNES context 4769b94acceSBarry Smith 4779b94acceSBarry Smith Output Parameter: 4789b94acceSBarry Smith . fnorm - 2-norm of gradient 4799b94acceSBarry Smith 4809b94acceSBarry Smith Note: 4819b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 482a86d99e1SLois Curfman McInnes methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 483a86d99e1SLois Curfman McInnes is SNESGetFunctionNorm(). 4849b94acceSBarry Smith 48536851e7fSLois Curfman McInnes Level: intermediate 48636851e7fSLois Curfman McInnes 4879b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 488a86d99e1SLois Curfman McInnes 489a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient() 4909b94acceSBarry Smith @*/ 4919b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 4929b94acceSBarry Smith { 4933a40ed3dSBarry Smith PetscFunctionBegin; 49477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 49574679c65SBarry Smith PetscValidScalarPointer(gnorm); 49674679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 497d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 49874679c65SBarry Smith } 4999b94acceSBarry Smith *gnorm = snes->norm; 5003a40ed3dSBarry Smith PetscFunctionReturn(0); 5019b94acceSBarry Smith } 50274679c65SBarry Smith 5035615d1e5SSatish Balay #undef __FUNC__ 504d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberUnsuccessfulSteps" 5059b94acceSBarry Smith /*@ 5069b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 5079b94acceSBarry Smith attempted by the nonlinear solver. 5089b94acceSBarry Smith 509c7afd0dbSLois Curfman McInnes Not Collective 510c7afd0dbSLois Curfman McInnes 5119b94acceSBarry Smith Input Parameter: 5129b94acceSBarry Smith . snes - SNES context 5139b94acceSBarry Smith 5149b94acceSBarry Smith Output Parameter: 5159b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 5169b94acceSBarry Smith 517c96a6f78SLois Curfman McInnes Notes: 518c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 519c96a6f78SLois Curfman McInnes 52036851e7fSLois Curfman McInnes Level: intermediate 52136851e7fSLois Curfman McInnes 5229b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 5239b94acceSBarry Smith @*/ 5249b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 5259b94acceSBarry Smith { 5263a40ed3dSBarry Smith PetscFunctionBegin; 52777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 52874679c65SBarry Smith PetscValidIntPointer(nfails); 5299b94acceSBarry Smith *nfails = snes->nfailures; 5303a40ed3dSBarry Smith PetscFunctionReturn(0); 5319b94acceSBarry Smith } 532a847f771SSatish Balay 5335615d1e5SSatish Balay #undef __FUNC__ 534d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberLinearIterations" 535c96a6f78SLois Curfman McInnes /*@ 536c96a6f78SLois Curfman McInnes SNESGetNumberLinearIterations - Gets the total number of linear iterations 537c96a6f78SLois Curfman McInnes used by the nonlinear solver. 538c96a6f78SLois Curfman McInnes 539c7afd0dbSLois Curfman McInnes Not Collective 540c7afd0dbSLois Curfman McInnes 541c96a6f78SLois Curfman McInnes Input Parameter: 542c96a6f78SLois Curfman McInnes . snes - SNES context 543c96a6f78SLois Curfman McInnes 544c96a6f78SLois Curfman McInnes Output Parameter: 545c96a6f78SLois Curfman McInnes . lits - number of linear iterations 546c96a6f78SLois Curfman McInnes 547c96a6f78SLois Curfman McInnes Notes: 548c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 549c96a6f78SLois Curfman McInnes 55036851e7fSLois Curfman McInnes Level: intermediate 55136851e7fSLois Curfman McInnes 552c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 553c96a6f78SLois Curfman McInnes @*/ 55486bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits) 555c96a6f78SLois Curfman McInnes { 5563a40ed3dSBarry Smith PetscFunctionBegin; 557c96a6f78SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 558c96a6f78SLois Curfman McInnes PetscValidIntPointer(lits); 559c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 5603a40ed3dSBarry Smith PetscFunctionReturn(0); 561c96a6f78SLois Curfman McInnes } 562c96a6f78SLois Curfman McInnes 563c96a6f78SLois Curfman McInnes #undef __FUNC__ 564d4bb536fSBarry Smith #define __FUNC__ "SNESGetSLES" 5659b94acceSBarry Smith /*@C 5669b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 5679b94acceSBarry Smith 568c7afd0dbSLois Curfman McInnes Not Collective, but if SNES object is parallel, then SLES object is parallel 569c7afd0dbSLois Curfman McInnes 5709b94acceSBarry Smith Input Parameter: 5719b94acceSBarry Smith . snes - the SNES context 5729b94acceSBarry Smith 5739b94acceSBarry Smith Output Parameter: 5749b94acceSBarry Smith . sles - the SLES context 5759b94acceSBarry Smith 5769b94acceSBarry Smith Notes: 5779b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 5789b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 5799b94acceSBarry Smith KSP and PC contexts as well. 5809b94acceSBarry Smith 58136851e7fSLois Curfman McInnes Level: beginner 58236851e7fSLois Curfman McInnes 5839b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 5849b94acceSBarry Smith 5859b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 5869b94acceSBarry Smith @*/ 5879b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 5889b94acceSBarry Smith { 5893a40ed3dSBarry Smith PetscFunctionBegin; 59077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5919b94acceSBarry Smith *sles = snes->sles; 5923a40ed3dSBarry Smith PetscFunctionReturn(0); 5939b94acceSBarry Smith } 59482bf6240SBarry Smith 595e24b481bSBarry Smith #undef __FUNC__ 596e24b481bSBarry Smith #define __FUNC__ "SNESPublish_Petsc" 597454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj) 598e24b481bSBarry Smith { 599aa482453SBarry Smith #if defined(PETSC_HAVE_AMS) 600454a90a3SBarry Smith SNES v = (SNES) obj; 601e24b481bSBarry Smith int ierr; 60243d6d2cbSBarry Smith #endif 603e24b481bSBarry Smith 604e24b481bSBarry Smith PetscFunctionBegin; 605e24b481bSBarry Smith 60643d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS) 607e24b481bSBarry Smith /* if it is already published then return */ 608e24b481bSBarry Smith if (v->amem >=0 ) PetscFunctionReturn(0); 609e24b481bSBarry Smith 610454a90a3SBarry Smith ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr); 611e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ, 612e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 613e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ, 614e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 615454a90a3SBarry Smith ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr); 616433994e6SBarry Smith #endif 617e24b481bSBarry Smith PetscFunctionReturn(0); 618e24b481bSBarry Smith } 619e24b481bSBarry Smith 6209b94acceSBarry Smith /* -----------------------------------------------------------*/ 6215615d1e5SSatish Balay #undef __FUNC__ 6225615d1e5SSatish Balay #define __FUNC__ "SNESCreate" 6239b94acceSBarry Smith /*@C 6249b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 6259b94acceSBarry Smith 626c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 627c7afd0dbSLois Curfman McInnes 628c7afd0dbSLois Curfman McInnes Input Parameters: 629c7afd0dbSLois Curfman McInnes + comm - MPI communicator 630c7afd0dbSLois Curfman McInnes - type - type of method, either 631c7afd0dbSLois Curfman McInnes SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations) 632c7afd0dbSLois Curfman McInnes or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization) 6339b94acceSBarry Smith 6349b94acceSBarry Smith Output Parameter: 6359b94acceSBarry Smith . outsnes - the new SNES context 6369b94acceSBarry Smith 637c7afd0dbSLois Curfman McInnes Options Database Keys: 638c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 639c7afd0dbSLois Curfman McInnes and no preconditioning matrix 640c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 641c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 642c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 643c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 644c1f60f51SBarry Smith 64536851e7fSLois Curfman McInnes Level: beginner 64636851e7fSLois Curfman McInnes 6479b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 6489b94acceSBarry Smith 64963a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy() 6509b94acceSBarry Smith @*/ 6514b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 6529b94acceSBarry Smith { 6539b94acceSBarry Smith int ierr; 6549b94acceSBarry Smith SNES snes; 6559b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 65637fcc0dbSBarry Smith 6573a40ed3dSBarry Smith PetscFunctionBegin; 6589b94acceSBarry Smith *outsnes = 0; 659d64ed03dSBarry Smith if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){ 660d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type"); 661d64ed03dSBarry Smith } 6623f1db9ecSBarry Smith PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView); 6639b94acceSBarry Smith PLogObjectCreate(snes); 664e24b481bSBarry Smith snes->bops->publish = SNESPublish_Petsc; 6659b94acceSBarry Smith snes->max_its = 50; 6669750a799SBarry Smith snes->max_funcs = 10000; 6679b94acceSBarry Smith snes->norm = 0.0; 6685a2d0531SBarry Smith if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 669b18e04deSLois Curfman McInnes snes->rtol = 1.e-8; 670b18e04deSLois Curfman McInnes snes->ttol = 0.0; 6719b94acceSBarry Smith snes->atol = 1.e-10; 672d64ed03dSBarry Smith } else { 673b4874afaSBarry Smith snes->rtol = 1.e-8; 674b4874afaSBarry Smith snes->ttol = 0.0; 675b4874afaSBarry Smith snes->atol = 1.e-50; 676b4874afaSBarry Smith } 6779b94acceSBarry Smith snes->xtol = 1.e-8; 678b18e04deSLois Curfman McInnes snes->trunctol = 1.e-12; /* no longer used */ 6799b94acceSBarry Smith snes->nfuncs = 0; 6809b94acceSBarry Smith snes->nfailures = 0; 6817a00f4a9SLois Curfman McInnes snes->linear_its = 0; 682639f9d9dSBarry Smith snes->numbermonitors = 0; 6839b94acceSBarry Smith snes->data = 0; 6849b94acceSBarry Smith snes->view = 0; 6859b94acceSBarry Smith snes->computeumfunction = 0; 6869b94acceSBarry Smith snes->umfunP = 0; 6879b94acceSBarry Smith snes->fc = 0; 6889b94acceSBarry Smith snes->deltatol = 1.e-12; 6899b94acceSBarry Smith snes->fmin = -1.e30; 6909b94acceSBarry Smith snes->method_class = type; 6919b94acceSBarry Smith snes->set_method_called = 0; 69282bf6240SBarry Smith snes->setupcalled = 0; 6939b94acceSBarry Smith snes->ksp_ewconv = 0; 6946f24a144SLois Curfman McInnes snes->vwork = 0; 6956f24a144SLois Curfman McInnes snes->nwork = 0; 696758f92a0SBarry Smith snes->conv_hist_len = 0; 697758f92a0SBarry Smith snes->conv_hist_max = 0; 698758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 699758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 700758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 701184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 7029b94acceSBarry Smith 7039b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 7040452661fSBarry Smith kctx = PetscNew(SNES_KSP_EW_ConvCtx);CHKPTRQ(kctx); 705eed86810SBarry Smith PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx)); 7069b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 7079b94acceSBarry Smith kctx->version = 2; 7089b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 7099b94acceSBarry Smith this was too large for some test cases */ 7109b94acceSBarry Smith kctx->rtol_last = 0; 7119b94acceSBarry Smith kctx->rtol_max = .9; 7129b94acceSBarry Smith kctx->gamma = 1.0; 7139b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 7149b94acceSBarry Smith kctx->alpha = kctx->alpha2; 7159b94acceSBarry Smith kctx->threshold = .1; 7169b94acceSBarry Smith kctx->lresid_last = 0; 7179b94acceSBarry Smith kctx->norm_last = 0; 7189b94acceSBarry Smith 7199b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr); 7209b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 7215334005bSBarry Smith 7229b94acceSBarry Smith *outsnes = snes; 723e24b481bSBarry Smith PetscPublishAll(snes); 7243a40ed3dSBarry Smith PetscFunctionReturn(0); 7259b94acceSBarry Smith } 7269b94acceSBarry Smith 7279b94acceSBarry Smith /* --------------------------------------------------------------- */ 7285615d1e5SSatish Balay #undef __FUNC__ 729d4bb536fSBarry Smith #define __FUNC__ "SNESSetFunction" 7309b94acceSBarry Smith /*@C 7319b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 7329b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 7339b94acceSBarry Smith equations. 7349b94acceSBarry Smith 735fee21e36SBarry Smith Collective on SNES 736fee21e36SBarry Smith 737c7afd0dbSLois Curfman McInnes Input Parameters: 738c7afd0dbSLois Curfman McInnes + snes - the SNES context 739c7afd0dbSLois Curfman McInnes . func - function evaluation routine 740c7afd0dbSLois Curfman McInnes . r - vector to store function value 741c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 742c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 7439b94acceSBarry Smith 744c7afd0dbSLois Curfman McInnes Calling sequence of func: 7458d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 746c7afd0dbSLois Curfman McInnes 747313e4042SLois Curfman McInnes . f - function vector 748c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 7499b94acceSBarry Smith 7509b94acceSBarry Smith Notes: 7519b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 7529b94acceSBarry Smith $ f'(x) x = -f(x), 753c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 7549b94acceSBarry Smith 7559b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7569b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7579b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 7589b94acceSBarry Smith 75936851e7fSLois Curfman McInnes Level: beginner 76036851e7fSLois Curfman McInnes 7619b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 7629b94acceSBarry Smith 763a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 7649b94acceSBarry Smith @*/ 7655334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx) 7669b94acceSBarry Smith { 7673a40ed3dSBarry Smith PetscFunctionBegin; 76877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 769184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 770184914b5SBarry Smith PetscCheckSameComm(snes,r); 771a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 772a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 773a8c6a408SBarry Smith } 774184914b5SBarry Smith 7759b94acceSBarry Smith snes->computefunction = func; 7769b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 7779b94acceSBarry Smith snes->funP = ctx; 7783a40ed3dSBarry Smith PetscFunctionReturn(0); 7799b94acceSBarry Smith } 7809b94acceSBarry Smith 7815615d1e5SSatish Balay #undef __FUNC__ 7825615d1e5SSatish Balay #define __FUNC__ "SNESComputeFunction" 7839b94acceSBarry Smith /*@ 78436851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 7859b94acceSBarry Smith SNESSetFunction(). 7869b94acceSBarry Smith 787c7afd0dbSLois Curfman McInnes Collective on SNES 788c7afd0dbSLois Curfman McInnes 7899b94acceSBarry Smith Input Parameters: 790c7afd0dbSLois Curfman McInnes + snes - the SNES context 791c7afd0dbSLois Curfman McInnes - x - input vector 7929b94acceSBarry Smith 7939b94acceSBarry Smith Output Parameter: 7943638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 7959b94acceSBarry Smith 7961bffabb2SLois Curfman McInnes Notes: 7979b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7989b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7999b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 8009b94acceSBarry Smith 80136851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 80236851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 80336851e7fSLois Curfman McInnes themselves. 80436851e7fSLois Curfman McInnes 80536851e7fSLois Curfman McInnes Level: developer 80636851e7fSLois Curfman McInnes 8079b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 8089b94acceSBarry Smith 809a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 8109b94acceSBarry Smith @*/ 8119b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y) 8129b94acceSBarry Smith { 8139b94acceSBarry Smith int ierr; 8149b94acceSBarry Smith 8153a40ed3dSBarry Smith PetscFunctionBegin; 816184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 817184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 818184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 819184914b5SBarry Smith PetscCheckSameComm(snes,x); 820184914b5SBarry Smith PetscCheckSameComm(snes,y); 821d4bb536fSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 822a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 823d4bb536fSBarry Smith } 824184914b5SBarry Smith 8259b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 826d64ed03dSBarry Smith PetscStackPush("SNES user function"); 8279b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 828d64ed03dSBarry Smith PetscStackPop; 829ae3c334cSLois Curfman McInnes snes->nfuncs++; 8309b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 8313a40ed3dSBarry Smith PetscFunctionReturn(0); 8329b94acceSBarry Smith } 8339b94acceSBarry Smith 8345615d1e5SSatish Balay #undef __FUNC__ 835d4bb536fSBarry Smith #define __FUNC__ "SNESSetMinimizationFunction" 8369b94acceSBarry Smith /*@C 8379b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 8389b94acceSBarry Smith unconstrained minimization. 8399b94acceSBarry Smith 840fee21e36SBarry Smith Collective on SNES 841fee21e36SBarry Smith 842c7afd0dbSLois Curfman McInnes Input Parameters: 843c7afd0dbSLois Curfman McInnes + snes - the SNES context 844c7afd0dbSLois Curfman McInnes . func - function evaluation routine 845c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 846c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8479b94acceSBarry Smith 848c7afd0dbSLois Curfman McInnes Calling sequence of func: 8498d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,double *f,void *ctx); 850c7afd0dbSLois Curfman McInnes 851c7afd0dbSLois Curfman McInnes + x - input vector 8529b94acceSBarry Smith . f - function 853c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined function context 8549b94acceSBarry Smith 85536851e7fSLois Curfman McInnes Level: beginner 85636851e7fSLois Curfman McInnes 8579b94acceSBarry Smith Notes: 8589b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 8599b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 8609b94acceSBarry Smith SNESSetFunction(). 8619b94acceSBarry Smith 8629b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 8639b94acceSBarry Smith 864a86d99e1SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 865a86d99e1SLois Curfman McInnes SNESSetHessian(), SNESSetGradient() 8669b94acceSBarry Smith @*/ 867454a90a3SBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),void *ctx) 8689b94acceSBarry Smith { 8693a40ed3dSBarry Smith PetscFunctionBegin; 87077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 871a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 872a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 873a8c6a408SBarry Smith } 8749b94acceSBarry Smith snes->computeumfunction = func; 8759b94acceSBarry Smith snes->umfunP = ctx; 8763a40ed3dSBarry Smith PetscFunctionReturn(0); 8779b94acceSBarry Smith } 8789b94acceSBarry Smith 8795615d1e5SSatish Balay #undef __FUNC__ 8805615d1e5SSatish Balay #define __FUNC__ "SNESComputeMinimizationFunction" 8819b94acceSBarry Smith /*@ 8829b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 8839b94acceSBarry Smith set with SNESSetMinimizationFunction(). 8849b94acceSBarry Smith 885c7afd0dbSLois Curfman McInnes Collective on SNES 886c7afd0dbSLois Curfman McInnes 8879b94acceSBarry Smith Input Parameters: 888c7afd0dbSLois Curfman McInnes + snes - the SNES context 889c7afd0dbSLois Curfman McInnes - x - input vector 8909b94acceSBarry Smith 8919b94acceSBarry Smith Output Parameter: 8929b94acceSBarry Smith . y - function value 8939b94acceSBarry Smith 8949b94acceSBarry Smith Notes: 8959b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 8969b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 8979b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 898a86d99e1SLois Curfman McInnes 89936851e7fSLois Curfman McInnes SNESComputeMinimizationFunction() is typically used within minimization 90036851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 90136851e7fSLois Curfman McInnes themselves. 90236851e7fSLois Curfman McInnes 90336851e7fSLois Curfman McInnes Level: developer 90436851e7fSLois Curfman McInnes 905a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function 906a86d99e1SLois Curfman McInnes 907a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 908a86d99e1SLois Curfman McInnes SNESComputeGradient(), SNESComputeHessian() 9099b94acceSBarry Smith @*/ 9109b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 9119b94acceSBarry Smith { 9129b94acceSBarry Smith int ierr; 9133a40ed3dSBarry Smith 9143a40ed3dSBarry Smith PetscFunctionBegin; 915184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 916184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 917184914b5SBarry Smith PetscCheckSameComm(snes,x); 918a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 919a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 920a8c6a408SBarry Smith } 921184914b5SBarry Smith 9229b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 923d64ed03dSBarry Smith PetscStackPush("SNES user minimzation function"); 9249b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr); 925d64ed03dSBarry Smith PetscStackPop; 926ae3c334cSLois Curfman McInnes snes->nfuncs++; 9279b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 9283a40ed3dSBarry Smith PetscFunctionReturn(0); 9299b94acceSBarry Smith } 9309b94acceSBarry Smith 9315615d1e5SSatish Balay #undef __FUNC__ 932d4bb536fSBarry Smith #define __FUNC__ "SNESSetGradient" 9339b94acceSBarry Smith /*@C 9349b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 9359b94acceSBarry Smith vector for use by the SNES routines. 9369b94acceSBarry Smith 937c7afd0dbSLois Curfman McInnes Collective on SNES 938c7afd0dbSLois Curfman McInnes 9399b94acceSBarry Smith Input Parameters: 940c7afd0dbSLois Curfman McInnes + snes - the SNES context 9419b94acceSBarry Smith . func - function evaluation routine 942044dda88SLois Curfman McInnes . ctx - optional user-defined context for private data for the 943044dda88SLois Curfman McInnes gradient evaluation routine (may be PETSC_NULL) 944c7afd0dbSLois Curfman McInnes - r - vector to store gradient value 945fee21e36SBarry Smith 9469b94acceSBarry Smith Calling sequence of func: 9478d76a1e5SLois Curfman McInnes $ func (SNES, Vec x, Vec g, void *ctx); 9489b94acceSBarry Smith 949c7afd0dbSLois Curfman McInnes + x - input vector 9509b94acceSBarry Smith . g - gradient vector 951c7afd0dbSLois Curfman McInnes - ctx - optional user-defined gradient context 9529b94acceSBarry Smith 9539b94acceSBarry Smith Notes: 9549b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 9559b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 9569b94acceSBarry Smith SNESSetFunction(). 9579b94acceSBarry Smith 95836851e7fSLois Curfman McInnes Level: beginner 95936851e7fSLois Curfman McInnes 9609b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9619b94acceSBarry Smith 962a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 963a86d99e1SLois Curfman McInnes SNESSetMinimizationFunction(), 9649b94acceSBarry Smith @*/ 96574679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 9669b94acceSBarry Smith { 9673a40ed3dSBarry Smith PetscFunctionBegin; 96877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 969184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 970184914b5SBarry Smith PetscCheckSameComm(snes,r); 971a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 972a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 973a8c6a408SBarry Smith } 9749b94acceSBarry Smith snes->computefunction = func; 9759b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 9769b94acceSBarry Smith snes->funP = ctx; 9773a40ed3dSBarry Smith PetscFunctionReturn(0); 9789b94acceSBarry Smith } 9799b94acceSBarry Smith 9805615d1e5SSatish Balay #undef __FUNC__ 9815615d1e5SSatish Balay #define __FUNC__ "SNESComputeGradient" 9829b94acceSBarry Smith /*@ 983a86d99e1SLois Curfman McInnes SNESComputeGradient - Computes the gradient that has been set with 984a86d99e1SLois Curfman McInnes SNESSetGradient(). 9859b94acceSBarry Smith 986c7afd0dbSLois Curfman McInnes Collective on SNES 987c7afd0dbSLois Curfman McInnes 9889b94acceSBarry Smith Input Parameters: 989c7afd0dbSLois Curfman McInnes + snes - the SNES context 990c7afd0dbSLois Curfman McInnes - x - input vector 9919b94acceSBarry Smith 9929b94acceSBarry Smith Output Parameter: 9939b94acceSBarry Smith . y - gradient vector 9949b94acceSBarry Smith 9959b94acceSBarry Smith Notes: 9969b94acceSBarry Smith SNESComputeGradient() is valid only for 9979b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 9989b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 999a86d99e1SLois Curfman McInnes 100036851e7fSLois Curfman McInnes SNESComputeGradient() is typically used within minimization 100136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 100236851e7fSLois Curfman McInnes themselves. 100336851e7fSLois Curfman McInnes 100436851e7fSLois Curfman McInnes Level: developer 100536851e7fSLois Curfman McInnes 1006a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient 1007a86d99e1SLois Curfman McInnes 1008a86d99e1SLois Curfman McInnes .seealso: SNESSetGradient(), SNESGetGradient(), 1009a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction(), SNESComputeHessian() 10109b94acceSBarry Smith @*/ 10119b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y) 10129b94acceSBarry Smith { 10139b94acceSBarry Smith int ierr; 10143a40ed3dSBarry Smith 10153a40ed3dSBarry Smith PetscFunctionBegin; 1016184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1017184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1018184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1019184914b5SBarry Smith PetscCheckSameComm(snes,x); 1020184914b5SBarry Smith PetscCheckSameComm(snes,y); 10213a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1022a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 10233a40ed3dSBarry Smith } 10249b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 1025d64ed03dSBarry Smith PetscStackPush("SNES user gradient function"); 10269b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1027d64ed03dSBarry Smith PetscStackPop; 10289b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 10293a40ed3dSBarry Smith PetscFunctionReturn(0); 10309b94acceSBarry Smith } 10319b94acceSBarry Smith 10325615d1e5SSatish Balay #undef __FUNC__ 10335615d1e5SSatish Balay #define __FUNC__ "SNESComputeJacobian" 103462fef451SLois Curfman McInnes /*@ 103562fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 103662fef451SLois Curfman McInnes set with SNESSetJacobian(). 103762fef451SLois Curfman McInnes 1038c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1039c7afd0dbSLois Curfman McInnes 104062fef451SLois Curfman McInnes Input Parameters: 1041c7afd0dbSLois Curfman McInnes + snes - the SNES context 1042c7afd0dbSLois Curfman McInnes - x - input vector 104362fef451SLois Curfman McInnes 104462fef451SLois Curfman McInnes Output Parameters: 1045c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 104662fef451SLois Curfman McInnes . B - optional preconditioning matrix 1047c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1048fee21e36SBarry Smith 104962fef451SLois Curfman McInnes Notes: 105062fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 105162fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 105262fef451SLois Curfman McInnes 1053dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1054dc5a77f8SLois Curfman McInnes flag parameter. 105562fef451SLois Curfman McInnes 105662fef451SLois Curfman McInnes SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 105762fef451SLois Curfman McInnes methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 1058005c665bSBarry Smith methods is SNESComputeHessian(). 105962fef451SLois Curfman McInnes 106036851e7fSLois Curfman McInnes SNESComputeJacobian() is typically used within nonlinear solver 106136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 106236851e7fSLois Curfman McInnes themselves. 106336851e7fSLois Curfman McInnes 106436851e7fSLois Curfman McInnes Level: developer 106536851e7fSLois Curfman McInnes 106662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 106762fef451SLois Curfman McInnes 106862fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 106962fef451SLois Curfman McInnes @*/ 10709b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10719b94acceSBarry Smith { 10729b94acceSBarry Smith int ierr; 10733a40ed3dSBarry Smith 10743a40ed3dSBarry Smith PetscFunctionBegin; 1075184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1076184914b5SBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE); 1077184914b5SBarry Smith PetscCheckSameComm(snes,X); 10783a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1079a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 10803a40ed3dSBarry Smith } 10813a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 10829b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 1083c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1084d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 10859b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1086d64ed03dSBarry Smith PetscStackPop; 10879b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 10886d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 108977c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 109077c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 10913a40ed3dSBarry Smith PetscFunctionReturn(0); 10929b94acceSBarry Smith } 10939b94acceSBarry Smith 10945615d1e5SSatish Balay #undef __FUNC__ 10955615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian" 109662fef451SLois Curfman McInnes /*@ 109762fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 109862fef451SLois Curfman McInnes set with SNESSetHessian(). 109962fef451SLois Curfman McInnes 1100c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1101c7afd0dbSLois Curfman McInnes 110262fef451SLois Curfman McInnes Input Parameters: 1103c7afd0dbSLois Curfman McInnes + snes - the SNES context 1104c7afd0dbSLois Curfman McInnes - x - input vector 110562fef451SLois Curfman McInnes 110662fef451SLois Curfman McInnes Output Parameters: 1107c7afd0dbSLois Curfman McInnes + A - Hessian matrix 110862fef451SLois Curfman McInnes . B - optional preconditioning matrix 1109c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1110fee21e36SBarry Smith 111162fef451SLois Curfman McInnes Notes: 111262fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 111362fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 111462fef451SLois Curfman McInnes 1115dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1116dc5a77f8SLois Curfman McInnes flag parameter. 111762fef451SLois Curfman McInnes 111862fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 111962fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 112062fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 112162fef451SLois Curfman McInnes 112236851e7fSLois Curfman McInnes SNESComputeHessian() is typically used within minimization 112336851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 112436851e7fSLois Curfman McInnes themselves. 112536851e7fSLois Curfman McInnes 112636851e7fSLois Curfman McInnes Level: developer 112736851e7fSLois Curfman McInnes 112862fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 112962fef451SLois Curfman McInnes 1130a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 1131a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 113262fef451SLois Curfman McInnes @*/ 113362fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 11349b94acceSBarry Smith { 11359b94acceSBarry Smith int ierr; 11363a40ed3dSBarry Smith 11373a40ed3dSBarry Smith PetscFunctionBegin; 1138184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1139184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1140184914b5SBarry Smith PetscCheckSameComm(snes,x); 11413a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1142a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 11433a40ed3dSBarry Smith } 11443a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 114562fef451SLois Curfman McInnes PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 11460f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 1147d64ed03dSBarry Smith PetscStackPush("SNES user Hessian function"); 114862fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr); 1149d64ed03dSBarry Smith PetscStackPop; 115062fef451SLois Curfman McInnes PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 11510f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 115277c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 115377c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 11543a40ed3dSBarry Smith PetscFunctionReturn(0); 11559b94acceSBarry Smith } 11569b94acceSBarry Smith 11575615d1e5SSatish Balay #undef __FUNC__ 1158d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian" 11599b94acceSBarry Smith /*@C 11609b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1161044dda88SLois Curfman McInnes location to store the matrix. 11629b94acceSBarry Smith 1163c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1164c7afd0dbSLois Curfman McInnes 11659b94acceSBarry Smith Input Parameters: 1166c7afd0dbSLois Curfman McInnes + snes - the SNES context 11679b94acceSBarry Smith . A - Jacobian matrix 11689b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11699b94acceSBarry Smith . func - Jacobian evaluation routine 1170c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11712cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11729b94acceSBarry Smith 11739b94acceSBarry Smith Calling sequence of func: 11748d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11759b94acceSBarry Smith 1176c7afd0dbSLois Curfman McInnes + x - input vector 11779b94acceSBarry Smith . A - Jacobian matrix 11789b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1179ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1180ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1181c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11829b94acceSBarry Smith 11839b94acceSBarry Smith Notes: 1184dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 11852cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1186ac21db08SLois Curfman McInnes 1187ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11889b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11899b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11909b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11919b94acceSBarry Smith throughout the global iterations. 11929b94acceSBarry Smith 119336851e7fSLois Curfman McInnes Level: beginner 119436851e7fSLois Curfman McInnes 11959b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11969b94acceSBarry Smith 1197ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 11989b94acceSBarry Smith @*/ 1199454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 12009b94acceSBarry Smith { 12013a40ed3dSBarry Smith PetscFunctionBegin; 120277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1203184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1204184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1205184914b5SBarry Smith PetscCheckSameComm(snes,A); 1206184914b5SBarry Smith PetscCheckSameComm(snes,B); 1207a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1208a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 1209a8c6a408SBarry Smith } 1210184914b5SBarry Smith 12119b94acceSBarry Smith snes->computejacobian = func; 12129b94acceSBarry Smith snes->jacP = ctx; 12139b94acceSBarry Smith snes->jacobian = A; 12149b94acceSBarry Smith snes->jacobian_pre = B; 12153a40ed3dSBarry Smith PetscFunctionReturn(0); 12169b94acceSBarry Smith } 121762fef451SLois Curfman McInnes 12185615d1e5SSatish Balay #undef __FUNC__ 1219d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian" 1220c2aafc4cSSatish Balay /*@C 1221b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1222b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1223b4fd4287SBarry Smith 1224c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1225c7afd0dbSLois Curfman McInnes 1226b4fd4287SBarry Smith Input Parameter: 1227b4fd4287SBarry Smith . snes - the nonlinear solver context 1228b4fd4287SBarry Smith 1229b4fd4287SBarry Smith Output Parameters: 1230c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1231b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 1232c7afd0dbSLois Curfman McInnes - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1233fee21e36SBarry Smith 123436851e7fSLois Curfman McInnes Level: advanced 123536851e7fSLois Curfman McInnes 1236b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1237b4fd4287SBarry Smith @*/ 1238b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx) 1239b4fd4287SBarry Smith { 12403a40ed3dSBarry Smith PetscFunctionBegin; 1241184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 12423a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1243a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 12443a40ed3dSBarry Smith } 1245b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1246b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1247b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 12483a40ed3dSBarry Smith PetscFunctionReturn(0); 1249b4fd4287SBarry Smith } 1250b4fd4287SBarry Smith 12515615d1e5SSatish Balay #undef __FUNC__ 1252d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian" 12539b94acceSBarry Smith /*@C 12549b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 1255044dda88SLois Curfman McInnes location to store the matrix. 12569b94acceSBarry Smith 1257c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1258c7afd0dbSLois Curfman McInnes 12599b94acceSBarry Smith Input Parameters: 1260c7afd0dbSLois Curfman McInnes + snes - the SNES context 12619b94acceSBarry Smith . A - Hessian matrix 12629b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 12639b94acceSBarry Smith . func - Jacobian evaluation routine 1264c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1265313e4042SLois Curfman McInnes Hessian evaluation routine (may be PETSC_NULL) 12669b94acceSBarry Smith 12679b94acceSBarry Smith Calling sequence of func: 12688d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 12699b94acceSBarry Smith 1270c7afd0dbSLois Curfman McInnes + x - input vector 12719b94acceSBarry Smith . A - Hessian matrix 12729b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1273ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1274ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1275c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Hessian context 12769b94acceSBarry Smith 12779b94acceSBarry Smith Notes: 1278dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 12792cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1280ac21db08SLois Curfman McInnes 12819b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 12829b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 12839b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 12849b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 12859b94acceSBarry Smith throughout the global iterations. 12869b94acceSBarry Smith 128736851e7fSLois Curfman McInnes Level: beginner 128836851e7fSLois Curfman McInnes 12899b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 12909b94acceSBarry Smith 1291ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 12929b94acceSBarry Smith @*/ 1293454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 12949b94acceSBarry Smith { 12953a40ed3dSBarry Smith PetscFunctionBegin; 129677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1297184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1298184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1299184914b5SBarry Smith PetscCheckSameComm(snes,A); 1300184914b5SBarry Smith PetscCheckSameComm(snes,B); 1301d4bb536fSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1302a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1303d4bb536fSBarry Smith } 13049b94acceSBarry Smith snes->computejacobian = func; 13059b94acceSBarry Smith snes->jacP = ctx; 13069b94acceSBarry Smith snes->jacobian = A; 13079b94acceSBarry Smith snes->jacobian_pre = B; 13083a40ed3dSBarry Smith PetscFunctionReturn(0); 13099b94acceSBarry Smith } 13109b94acceSBarry Smith 13115615d1e5SSatish Balay #undef __FUNC__ 1312d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian" 131362fef451SLois Curfman McInnes /*@ 131462fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 131562fef451SLois Curfman McInnes provided context for evaluating the Hessian. 131662fef451SLois Curfman McInnes 1317c7afd0dbSLois Curfman McInnes Not Collective, but Mat object is parallel if SNES object is parallel 1318c7afd0dbSLois Curfman McInnes 131962fef451SLois Curfman McInnes Input Parameter: 132062fef451SLois Curfman McInnes . snes - the nonlinear solver context 132162fef451SLois Curfman McInnes 132262fef451SLois Curfman McInnes Output Parameters: 1323c7afd0dbSLois Curfman McInnes + A - location to stash Hessian matrix (or PETSC_NULL) 132462fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 1325c7afd0dbSLois Curfman McInnes - ctx - location to stash Hessian ctx (or PETSC_NULL) 1326fee21e36SBarry Smith 132736851e7fSLois Curfman McInnes Level: advanced 132836851e7fSLois Curfman McInnes 132962fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 1330c7afd0dbSLois Curfman McInnes 1331c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian 133262fef451SLois Curfman McInnes @*/ 133362fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx) 133462fef451SLois Curfman McInnes { 13353a40ed3dSBarry Smith PetscFunctionBegin; 1336184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 13373a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){ 1338a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 13393a40ed3dSBarry Smith } 134062fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 134162fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 134262fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 13433a40ed3dSBarry Smith PetscFunctionReturn(0); 134462fef451SLois Curfman McInnes } 134562fef451SLois Curfman McInnes 13469b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 13479b94acceSBarry Smith 13485615d1e5SSatish Balay #undef __FUNC__ 13495615d1e5SSatish Balay #define __FUNC__ "SNESSetUp" 13509b94acceSBarry Smith /*@ 13519b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1352272ac6f2SLois Curfman McInnes of a nonlinear solver. 13539b94acceSBarry Smith 1354fee21e36SBarry Smith Collective on SNES 1355fee21e36SBarry Smith 1356c7afd0dbSLois Curfman McInnes Input Parameters: 1357c7afd0dbSLois Curfman McInnes + snes - the SNES context 1358c7afd0dbSLois Curfman McInnes - x - the solution vector 1359c7afd0dbSLois Curfman McInnes 1360272ac6f2SLois Curfman McInnes Notes: 1361272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1362272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1363272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1364272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1365272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1366272ac6f2SLois Curfman McInnes 136736851e7fSLois Curfman McInnes Level: advanced 136836851e7fSLois Curfman McInnes 13699b94acceSBarry Smith .keywords: SNES, nonlinear, setup 13709b94acceSBarry Smith 13719b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 13729b94acceSBarry Smith @*/ 13738ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 13749b94acceSBarry Smith { 1375272ac6f2SLois Curfman McInnes int ierr, flg; 13763a40ed3dSBarry Smith 13773a40ed3dSBarry Smith PetscFunctionBegin; 137877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 137977c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1380184914b5SBarry Smith PetscCheckSameComm(snes,x); 13818ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 13829b94acceSBarry Smith 1383c1f60f51SBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);CHKERRQ(ierr); 1384c1f60f51SBarry Smith /* 1385c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1386dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1387c1f60f51SBarry Smith */ 1388c1f60f51SBarry Smith if (flg) { 1389c1f60f51SBarry Smith Mat J; 13905a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1391c1f60f51SBarry Smith PLogObjectParent(snes,J); 1392c1f60f51SBarry Smith snes->mfshell = J; 1393c1f60f51SBarry Smith snes->jacobian = J; 1394c2aafc4cSSatish Balay if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1395c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n"); 1396d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1397c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n"); 1398d4bb536fSBarry Smith } else { 1399a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option"); 1400d4bb536fSBarry Smith } 14015a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1402c1f60f51SBarry Smith } 1403272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);CHKERRQ(ierr); 1404c1f60f51SBarry Smith /* 1405dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1406c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1407c1f60f51SBarry Smith */ 140831615d04SLois Curfman McInnes if (flg) { 1409272ac6f2SLois Curfman McInnes Mat J; 14105a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1411272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 1412272ac6f2SLois Curfman McInnes snes->mfshell = J; 141383e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 141483e56afdSLois Curfman McInnes ierr = SNESSetJacobian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 141594a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 1416d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 141783e56afdSLois Curfman McInnes ierr = SNESSetHessian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 141894a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 1419d4bb536fSBarry Smith } else { 1420a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option"); 1421d4bb536fSBarry Smith } 14225a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1423272ac6f2SLois Curfman McInnes } 14249b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 1425a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 1426a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 14275a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 1428a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 1429a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector"); 1430a8c6a408SBarry Smith } 1431a703fe33SLois Curfman McInnes 1432a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 1433454a90a3SBarry Smith if (snes->ksp_ewconv && !PetscTypeCompare(snes,SNES_EQ_TR)) { 1434a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 1435a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 1436a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 14375a655dc6SBarry Smith ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr); 1438a703fe33SLois Curfman McInnes } 14399b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 1440a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1441a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1442a8c6a408SBarry Smith if (!snes->computeumfunction) { 1443a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first"); 1444a8c6a408SBarry Smith } 14455a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()"); 1446d4bb536fSBarry Smith } else { 1447a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class"); 1448d4bb536fSBarry Smith } 1449a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);} 145082bf6240SBarry Smith snes->setupcalled = 1; 14513a40ed3dSBarry Smith PetscFunctionReturn(0); 14529b94acceSBarry Smith } 14539b94acceSBarry Smith 14545615d1e5SSatish Balay #undef __FUNC__ 1455d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy" 14569b94acceSBarry Smith /*@C 14579b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 14589b94acceSBarry Smith with SNESCreate(). 14599b94acceSBarry Smith 1460c7afd0dbSLois Curfman McInnes Collective on SNES 1461c7afd0dbSLois Curfman McInnes 14629b94acceSBarry Smith Input Parameter: 14639b94acceSBarry Smith . snes - the SNES context 14649b94acceSBarry Smith 146536851e7fSLois Curfman McInnes Level: beginner 146636851e7fSLois Curfman McInnes 14679b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 14689b94acceSBarry Smith 146963a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 14709b94acceSBarry Smith @*/ 14719b94acceSBarry Smith int SNESDestroy(SNES snes) 14729b94acceSBarry Smith { 1473b8a78c4aSBarry Smith int i,ierr; 14743a40ed3dSBarry Smith 14753a40ed3dSBarry Smith PetscFunctionBegin; 147677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14773a40ed3dSBarry Smith if (--snes->refct > 0) PetscFunctionReturn(0); 1478d4bb536fSBarry Smith 1479be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 1480*0f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1481be0abb6dSBarry Smith 1482e1311b90SBarry Smith if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);} 1483606d414cSSatish Balay if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);} 1484522c5e43SBarry Smith if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);} 14859b94acceSBarry Smith ierr = SLESDestroy(snes->sles);CHKERRQ(ierr); 1486522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1487b8a78c4aSBarry Smith for (i=0; i<snes->numbermonitors; i++ ) { 1488b8a78c4aSBarry Smith if (snes->monitordestroy[i]) { 1489b8a78c4aSBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1490b8a78c4aSBarry Smith } 1491b8a78c4aSBarry Smith } 14929b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 14930452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 14943a40ed3dSBarry Smith PetscFunctionReturn(0); 14959b94acceSBarry Smith } 14969b94acceSBarry Smith 14979b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 14989b94acceSBarry Smith 14995615d1e5SSatish Balay #undef __FUNC__ 15005615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances" 15019b94acceSBarry Smith /*@ 1502d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 15039b94acceSBarry Smith 1504c7afd0dbSLois Curfman McInnes Collective on SNES 1505c7afd0dbSLois Curfman McInnes 15069b94acceSBarry Smith Input Parameters: 1507c7afd0dbSLois Curfman McInnes + snes - the SNES context 150833174efeSLois Curfman McInnes . atol - absolute convergence tolerance 150933174efeSLois Curfman McInnes . rtol - relative convergence tolerance 151033174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 151133174efeSLois Curfman McInnes of the change in the solution between steps 151233174efeSLois Curfman McInnes . maxit - maximum number of iterations 1513c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1514fee21e36SBarry Smith 151533174efeSLois Curfman McInnes Options Database Keys: 1516c7afd0dbSLois Curfman McInnes + -snes_atol <atol> - Sets atol 1517c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1518c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1519c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1520c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15219b94acceSBarry Smith 1522d7a720efSLois Curfman McInnes Notes: 15239b94acceSBarry Smith The default maximum number of iterations is 50. 15249b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15259b94acceSBarry Smith 152636851e7fSLois Curfman McInnes Level: intermediate 152736851e7fSLois Curfman McInnes 152833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15299b94acceSBarry Smith 1530d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 15319b94acceSBarry Smith @*/ 1532d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf) 15339b94acceSBarry Smith { 15343a40ed3dSBarry Smith PetscFunctionBegin; 153577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1536d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1537d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1538d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1539d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1540d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15413a40ed3dSBarry Smith PetscFunctionReturn(0); 15429b94acceSBarry Smith } 15439b94acceSBarry Smith 15445615d1e5SSatish Balay #undef __FUNC__ 15455615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances" 15469b94acceSBarry Smith /*@ 154733174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 154833174efeSLois Curfman McInnes 1549c7afd0dbSLois Curfman McInnes Not Collective 1550c7afd0dbSLois Curfman McInnes 155133174efeSLois Curfman McInnes Input Parameters: 1552c7afd0dbSLois Curfman McInnes + snes - the SNES context 155333174efeSLois Curfman McInnes . atol - absolute convergence tolerance 155433174efeSLois Curfman McInnes . rtol - relative convergence tolerance 155533174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 155633174efeSLois Curfman McInnes of the change in the solution between steps 155733174efeSLois Curfman McInnes . maxit - maximum number of iterations 1558c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1559fee21e36SBarry Smith 156033174efeSLois Curfman McInnes Notes: 156133174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 156233174efeSLois Curfman McInnes 156336851e7fSLois Curfman McInnes Level: intermediate 156436851e7fSLois Curfman McInnes 156533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 156633174efeSLois Curfman McInnes 156733174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 156833174efeSLois Curfman McInnes @*/ 156933174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf) 157033174efeSLois Curfman McInnes { 15713a40ed3dSBarry Smith PetscFunctionBegin; 157233174efeSLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 157333174efeSLois Curfman McInnes if (atol) *atol = snes->atol; 157433174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 157533174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 157633174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 157733174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 15783a40ed3dSBarry Smith PetscFunctionReturn(0); 157933174efeSLois Curfman McInnes } 158033174efeSLois Curfman McInnes 15815615d1e5SSatish Balay #undef __FUNC__ 15825615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance" 158333174efeSLois Curfman McInnes /*@ 15849b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 15859b94acceSBarry Smith 1586fee21e36SBarry Smith Collective on SNES 1587fee21e36SBarry Smith 1588c7afd0dbSLois Curfman McInnes Input Parameters: 1589c7afd0dbSLois Curfman McInnes + snes - the SNES context 1590c7afd0dbSLois Curfman McInnes - tol - tolerance 1591c7afd0dbSLois Curfman McInnes 15929b94acceSBarry Smith Options Database Key: 1593c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 15949b94acceSBarry Smith 159536851e7fSLois Curfman McInnes Level: intermediate 159636851e7fSLois Curfman McInnes 15979b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 15989b94acceSBarry Smith 1599d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 16009b94acceSBarry Smith @*/ 16019b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol) 16029b94acceSBarry Smith { 16033a40ed3dSBarry Smith PetscFunctionBegin; 160477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16059b94acceSBarry Smith snes->deltatol = tol; 16063a40ed3dSBarry Smith PetscFunctionReturn(0); 16079b94acceSBarry Smith } 16089b94acceSBarry Smith 16095615d1e5SSatish Balay #undef __FUNC__ 16105615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance" 16119b94acceSBarry Smith /*@ 161277c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 16139b94acceSBarry Smith for unconstrained minimization solvers. 16149b94acceSBarry Smith 1615fee21e36SBarry Smith Collective on SNES 1616fee21e36SBarry Smith 1617c7afd0dbSLois Curfman McInnes Input Parameters: 1618c7afd0dbSLois Curfman McInnes + snes - the SNES context 1619c7afd0dbSLois Curfman McInnes - ftol - minimum function tolerance 1620c7afd0dbSLois Curfman McInnes 16219b94acceSBarry Smith Options Database Key: 1622c7afd0dbSLois Curfman McInnes . -snes_fmin <ftol> - Sets ftol 16239b94acceSBarry Smith 16249b94acceSBarry Smith Note: 162577c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 16269b94acceSBarry Smith methods only. 16279b94acceSBarry Smith 162836851e7fSLois Curfman McInnes Level: intermediate 162936851e7fSLois Curfman McInnes 16309b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 16319b94acceSBarry Smith 1632d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 16339b94acceSBarry Smith @*/ 163477c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol) 16359b94acceSBarry Smith { 16363a40ed3dSBarry Smith PetscFunctionBegin; 163777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16389b94acceSBarry Smith snes->fmin = ftol; 16393a40ed3dSBarry Smith PetscFunctionReturn(0); 16409b94acceSBarry Smith } 1641df9fa365SBarry Smith /* 1642df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1643df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1644df9fa365SBarry Smith macros instead of functions 1645df9fa365SBarry Smith */ 1646ce1608b8SBarry Smith #undef __FUNC__ 1647ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor" 1648ce1608b8SBarry Smith int SNESLGMonitor(SNES snes,int it,double norm,void *ctx) 1649ce1608b8SBarry Smith { 1650ce1608b8SBarry Smith int ierr; 1651ce1608b8SBarry Smith 1652ce1608b8SBarry Smith PetscFunctionBegin; 1653184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1654ce1608b8SBarry Smith ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1655ce1608b8SBarry Smith PetscFunctionReturn(0); 1656ce1608b8SBarry Smith } 1657ce1608b8SBarry Smith 1658df9fa365SBarry Smith #undef __FUNC__ 1659df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorCreate" 1660df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n, DrawLG *draw) 1661df9fa365SBarry Smith { 1662df9fa365SBarry Smith int ierr; 1663df9fa365SBarry Smith 1664df9fa365SBarry Smith PetscFunctionBegin; 1665df9fa365SBarry Smith ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1666df9fa365SBarry Smith PetscFunctionReturn(0); 1667df9fa365SBarry Smith } 1668df9fa365SBarry Smith 1669df9fa365SBarry Smith #undef __FUNC__ 1670df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorDestroy" 1671df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw) 1672df9fa365SBarry Smith { 1673df9fa365SBarry Smith int ierr; 1674df9fa365SBarry Smith 1675df9fa365SBarry Smith PetscFunctionBegin; 1676df9fa365SBarry Smith ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr); 1677df9fa365SBarry Smith PetscFunctionReturn(0); 1678df9fa365SBarry Smith } 1679df9fa365SBarry Smith 16809b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 16819b94acceSBarry Smith 16825615d1e5SSatish Balay #undef __FUNC__ 1683d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor" 16849b94acceSBarry Smith /*@C 16855cd90555SBarry Smith SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every 16869b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 16879b94acceSBarry Smith progress. 16889b94acceSBarry Smith 1689fee21e36SBarry Smith Collective on SNES 1690fee21e36SBarry Smith 1691c7afd0dbSLois Curfman McInnes Input Parameters: 1692c7afd0dbSLois Curfman McInnes + snes - the SNES context 1693c7afd0dbSLois Curfman McInnes . func - monitoring routine 1694b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1695c7afd0dbSLois Curfman McInnes monitor routine (may be PETSC_NULL) 1696b8a78c4aSBarry Smith - monitordestroy - options routine that frees monitor context 16979b94acceSBarry Smith 1698c7afd0dbSLois Curfman McInnes Calling sequence of func: 169940a0c1c6SLois Curfman McInnes $ int func(SNES snes,int its, double norm,void *mctx) 1700c7afd0dbSLois Curfman McInnes 1701c7afd0dbSLois Curfman McInnes + snes - the SNES context 1702c7afd0dbSLois Curfman McInnes . its - iteration number 1703c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 1704c7afd0dbSLois Curfman McInnes for SNES_NONLINEAR_EQUATIONS methods 170540a0c1c6SLois Curfman McInnes . norm - 2-norm gradient value (may be estimated) 1706c7afd0dbSLois Curfman McInnes for SNES_UNCONSTRAINED_MINIMIZATION methods 170740a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 17089b94acceSBarry Smith 17099665c990SLois Curfman McInnes Options Database Keys: 1710c7afd0dbSLois Curfman McInnes + -snes_monitor - sets SNESDefaultMonitor() 1711c7afd0dbSLois Curfman McInnes . -snes_xmonitor - sets line graph monitor, 1712c7afd0dbSLois Curfman McInnes uses SNESLGMonitorCreate() 1713c7afd0dbSLois Curfman McInnes _ -snes_cancelmonitors - cancels all monitors that have 1714c7afd0dbSLois Curfman McInnes been hardwired into a code by 1715c7afd0dbSLois Curfman McInnes calls to SNESSetMonitor(), but 1716c7afd0dbSLois Curfman McInnes does not cancel those set via 1717c7afd0dbSLois Curfman McInnes the options database. 17189665c990SLois Curfman McInnes 1719639f9d9dSBarry Smith Notes: 17206bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 17216bc08f3fSLois Curfman McInnes SNESSetMonitor() multiple times; all will be called in the 17226bc08f3fSLois Curfman McInnes order in which they were set. 1723639f9d9dSBarry Smith 172436851e7fSLois Curfman McInnes Level: intermediate 172536851e7fSLois Curfman McInnes 17269b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 17279b94acceSBarry Smith 17285cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor() 17299b94acceSBarry Smith @*/ 1730b8a78c4aSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx,int (*monitordestroy)(void *)) 17319b94acceSBarry Smith { 17323a40ed3dSBarry Smith PetscFunctionBegin; 1733184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1734639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 1735a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set"); 1736639f9d9dSBarry Smith } 1737639f9d9dSBarry Smith 1738639f9d9dSBarry Smith snes->monitor[snes->numbermonitors] = func; 1739b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1740639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 17413a40ed3dSBarry Smith PetscFunctionReturn(0); 17429b94acceSBarry Smith } 17439b94acceSBarry Smith 17445615d1e5SSatish Balay #undef __FUNC__ 17455cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor" 17465cd90555SBarry Smith /*@C 17475cd90555SBarry Smith SNESClearMonitor - Clears all the monitor functions for a SNES object. 17485cd90555SBarry Smith 1749c7afd0dbSLois Curfman McInnes Collective on SNES 1750c7afd0dbSLois Curfman McInnes 17515cd90555SBarry Smith Input Parameters: 17525cd90555SBarry Smith . snes - the SNES context 17535cd90555SBarry Smith 17545cd90555SBarry Smith Options Database: 1755c7afd0dbSLois Curfman McInnes . -snes_cancelmonitors - cancels all monitors that have been hardwired 1756c7afd0dbSLois Curfman McInnes into a code by calls to SNESSetMonitor(), but does not cancel those 1757c7afd0dbSLois Curfman McInnes set via the options database 17585cd90555SBarry Smith 17595cd90555SBarry Smith Notes: 17605cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 17615cd90555SBarry Smith 176236851e7fSLois Curfman McInnes Level: intermediate 176336851e7fSLois Curfman McInnes 17645cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 17655cd90555SBarry Smith 17665cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor() 17675cd90555SBarry Smith @*/ 17685cd90555SBarry Smith int SNESClearMonitor( SNES snes ) 17695cd90555SBarry Smith { 17705cd90555SBarry Smith PetscFunctionBegin; 1771184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 17725cd90555SBarry Smith snes->numbermonitors = 0; 17735cd90555SBarry Smith PetscFunctionReturn(0); 17745cd90555SBarry Smith } 17755cd90555SBarry Smith 17765cd90555SBarry Smith #undef __FUNC__ 1777d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest" 17789b94acceSBarry Smith /*@C 17799b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 17809b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 17819b94acceSBarry Smith 1782fee21e36SBarry Smith Collective on SNES 1783fee21e36SBarry Smith 1784c7afd0dbSLois Curfman McInnes Input Parameters: 1785c7afd0dbSLois Curfman McInnes + snes - the SNES context 1786c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1787c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1788c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 17899b94acceSBarry Smith 1790c7afd0dbSLois Curfman McInnes Calling sequence of func: 1791184914b5SBarry Smith $ int func (SNES snes,double xnorm,double gnorm,double f,SNESConvergedReason *reason,void *cctx) 1792c7afd0dbSLois Curfman McInnes 1793c7afd0dbSLois Curfman McInnes + snes - the SNES context 1794c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1795184914b5SBarry Smith . reason - reason for convergence/divergence 1796c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 1797c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods) 1798c7afd0dbSLois Curfman McInnes . f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods) 1799c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods) 1800c7afd0dbSLois Curfman McInnes - f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods) 18019b94acceSBarry Smith 180236851e7fSLois Curfman McInnes Level: advanced 180336851e7fSLois Curfman McInnes 18049b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 18059b94acceSBarry Smith 180640191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 180740191667SLois Curfman McInnes SNESConverged_UM_LS(), SNESConverged_UM_TR() 18089b94acceSBarry Smith @*/ 1809184914b5SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,SNESConvergedReason*,void*),void *cctx) 18109b94acceSBarry Smith { 18113a40ed3dSBarry Smith PetscFunctionBegin; 1812184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 18139b94acceSBarry Smith (snes)->converged = func; 18149b94acceSBarry Smith (snes)->cnvP = cctx; 18153a40ed3dSBarry Smith PetscFunctionReturn(0); 18169b94acceSBarry Smith } 18179b94acceSBarry Smith 18185615d1e5SSatish Balay #undef __FUNC__ 1819184914b5SBarry Smith #define __FUNC__ "SNESGetConvergedReason" 1820184914b5SBarry Smith /*@C 1821184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1822184914b5SBarry Smith 1823184914b5SBarry Smith Not Collective 1824184914b5SBarry Smith 1825184914b5SBarry Smith Input Parameter: 1826184914b5SBarry Smith . snes - the SNES context 1827184914b5SBarry Smith 1828184914b5SBarry Smith Output Parameter: 1829184914b5SBarry Smith . reason - negative value indicates diverged, positive value converged, see snes.h or the 1830184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1831184914b5SBarry Smith 1832184914b5SBarry Smith Level: intermediate 1833184914b5SBarry Smith 1834184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1835184914b5SBarry Smith 1836184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1837184914b5SBarry Smith 1838184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 1839184914b5SBarry Smith SNESConverged_UM_LS(), SNESConverged_UM_TR() 1840184914b5SBarry Smith @*/ 1841184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1842184914b5SBarry Smith { 1843184914b5SBarry Smith PetscFunctionBegin; 1844184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1845184914b5SBarry Smith *reason = snes->reason; 1846184914b5SBarry Smith PetscFunctionReturn(0); 1847184914b5SBarry Smith } 1848184914b5SBarry Smith 1849184914b5SBarry Smith #undef __FUNC__ 1850d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory" 1851c9005455SLois Curfman McInnes /*@ 1852c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1853c9005455SLois Curfman McInnes 1854fee21e36SBarry Smith Collective on SNES 1855fee21e36SBarry Smith 1856c7afd0dbSLois Curfman McInnes Input Parameters: 1857c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1858c7afd0dbSLois Curfman McInnes . a - array to hold history 1859758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1860758f92a0SBarry Smith negative if not converged) for each solve. 1861758f92a0SBarry Smith . na - size of a and its 1862758f92a0SBarry Smith - reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero, 1863758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1864c7afd0dbSLois Curfman McInnes 1865c9005455SLois Curfman McInnes Notes: 1866c9005455SLois Curfman McInnes If set, this array will contain the function norms (for 1867c9005455SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods) or gradient norms 1868c9005455SLois Curfman McInnes (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed 1869c9005455SLois Curfman McInnes at each step. 1870c9005455SLois Curfman McInnes 1871c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1872c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1873c9005455SLois Curfman McInnes during the section of code that is being timed. 1874c9005455SLois Curfman McInnes 187536851e7fSLois Curfman McInnes Level: intermediate 187636851e7fSLois Curfman McInnes 1877c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1878758f92a0SBarry Smith 187908405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1880758f92a0SBarry Smith 1881c9005455SLois Curfman McInnes @*/ 1882758f92a0SBarry Smith int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset) 1883c9005455SLois Curfman McInnes { 18843a40ed3dSBarry Smith PetscFunctionBegin; 1885c9005455SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 1886c9005455SLois Curfman McInnes if (na) PetscValidScalarPointer(a); 1887c9005455SLois Curfman McInnes snes->conv_hist = a; 1888758f92a0SBarry Smith snes->conv_hist_its = its; 1889758f92a0SBarry Smith snes->conv_hist_max = na; 1890758f92a0SBarry Smith snes->conv_hist_reset = reset; 1891758f92a0SBarry Smith PetscFunctionReturn(0); 1892758f92a0SBarry Smith } 1893758f92a0SBarry Smith 1894758f92a0SBarry Smith #undef __FUNC__ 1895758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory" 18960c4c9dddSBarry Smith /*@C 1897758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1898758f92a0SBarry Smith 1899758f92a0SBarry Smith Collective on SNES 1900758f92a0SBarry Smith 1901758f92a0SBarry Smith Input Parameter: 1902758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1903758f92a0SBarry Smith 1904758f92a0SBarry Smith Output Parameters: 1905758f92a0SBarry Smith . a - array to hold history 1906758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1907758f92a0SBarry Smith negative if not converged) for each solve. 1908758f92a0SBarry Smith - na - size of a and its 1909758f92a0SBarry Smith 1910758f92a0SBarry Smith Notes: 1911758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1912758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1913758f92a0SBarry Smith 1914758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1915758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1916758f92a0SBarry Smith during the section of code that is being timed. 1917758f92a0SBarry Smith 1918758f92a0SBarry Smith Level: intermediate 1919758f92a0SBarry Smith 1920758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1921758f92a0SBarry Smith 1922758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1923758f92a0SBarry Smith 1924758f92a0SBarry Smith @*/ 1925758f92a0SBarry Smith int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na) 1926758f92a0SBarry Smith { 1927758f92a0SBarry Smith PetscFunctionBegin; 1928758f92a0SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1929758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1930758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1931758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 19323a40ed3dSBarry Smith PetscFunctionReturn(0); 1933c9005455SLois Curfman McInnes } 1934c9005455SLois Curfman McInnes 1935c9005455SLois Curfman McInnes #undef __FUNC__ 19365615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private" 19379b94acceSBarry Smith /* 19389b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 19399b94acceSBarry Smith positive parameter delta. 19409b94acceSBarry Smith 19419b94acceSBarry Smith Input Parameters: 1942c7afd0dbSLois Curfman McInnes + snes - the SNES context 19439b94acceSBarry Smith . y - approximate solution of linear system 19449b94acceSBarry Smith . fnorm - 2-norm of current function 1945c7afd0dbSLois Curfman McInnes - delta - trust region size 19469b94acceSBarry Smith 19479b94acceSBarry Smith Output Parameters: 1948c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 19499b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 19509b94acceSBarry Smith region, and exceeds zero otherwise. 1951c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 19529b94acceSBarry Smith 19539b94acceSBarry Smith Note: 195440191667SLois Curfman McInnes For non-trust region methods such as SNES_EQ_LS, the parameter delta 19559b94acceSBarry Smith is set to be the maximum allowable step size. 19569b94acceSBarry Smith 19579b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 19589b94acceSBarry Smith */ 19599b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 19609b94acceSBarry Smith double *gpnorm,double *ynorm) 19619b94acceSBarry Smith { 19629b94acceSBarry Smith double norm; 19639b94acceSBarry Smith Scalar cnorm; 19643a40ed3dSBarry Smith int ierr; 19653a40ed3dSBarry Smith 19663a40ed3dSBarry Smith PetscFunctionBegin; 1967184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1968184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1969184914b5SBarry Smith PetscCheckSameComm(snes,y); 1970184914b5SBarry Smith 19713a40ed3dSBarry Smith ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr); 19729b94acceSBarry Smith if (norm > *delta) { 19739b94acceSBarry Smith norm = *delta/norm; 19749b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 19759b94acceSBarry Smith cnorm = norm; 19769b94acceSBarry Smith VecScale( &cnorm, y ); 19779b94acceSBarry Smith *ynorm = *delta; 19789b94acceSBarry Smith } else { 19799b94acceSBarry Smith *gpnorm = 0.0; 19809b94acceSBarry Smith *ynorm = norm; 19819b94acceSBarry Smith } 19823a40ed3dSBarry Smith PetscFunctionReturn(0); 19839b94acceSBarry Smith } 19849b94acceSBarry Smith 19855615d1e5SSatish Balay #undef __FUNC__ 19865615d1e5SSatish Balay #define __FUNC__ "SNESSolve" 19879b94acceSBarry Smith /*@ 19889b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 198963a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 19909b94acceSBarry Smith 1991c7afd0dbSLois Curfman McInnes Collective on SNES 1992c7afd0dbSLois Curfman McInnes 1993b2002411SLois Curfman McInnes Input Parameters: 1994c7afd0dbSLois Curfman McInnes + snes - the SNES context 1995c7afd0dbSLois Curfman McInnes - x - the solution vector 19969b94acceSBarry Smith 19979b94acceSBarry Smith Output Parameter: 1998b2002411SLois Curfman McInnes . its - number of iterations until termination 19999b94acceSBarry Smith 2000b2002411SLois Curfman McInnes Notes: 20018ddd3da0SLois Curfman McInnes The user should initialize the vector, x, with the initial guess 20028ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 20038ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 20048ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 20058ddd3da0SLois Curfman McInnes 200636851e7fSLois Curfman McInnes Level: beginner 200736851e7fSLois Curfman McInnes 20089b94acceSBarry Smith .keywords: SNES, nonlinear, solve 20099b94acceSBarry Smith 201063a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 20119b94acceSBarry Smith @*/ 20128ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 20139b94acceSBarry Smith { 20143c7409f5SSatish Balay int ierr, flg; 2015052efed2SBarry Smith 20163a40ed3dSBarry Smith PetscFunctionBegin; 201777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2018184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 2019184914b5SBarry Smith PetscCheckSameComm(snes,x); 202074679c65SBarry Smith PetscValidIntPointer(its); 202182bf6240SBarry Smith if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);} 2022c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 2023758f92a0SBarry Smith if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0; 20249b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 2025c96a6f78SLois Curfman McInnes snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0; 20269b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr); 20279b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 2028*0f5bd95cSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_view", &flg);CHKERRQ(ierr); 20296d4a8577SBarry Smith if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } 20303a40ed3dSBarry Smith PetscFunctionReturn(0); 20319b94acceSBarry Smith } 20329b94acceSBarry Smith 20339b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 20349b94acceSBarry Smith 20355615d1e5SSatish Balay #undef __FUNC__ 20365615d1e5SSatish Balay #define __FUNC__ "SNESSetType" 203782bf6240SBarry Smith /*@C 20384b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 20399b94acceSBarry Smith 2040fee21e36SBarry Smith Collective on SNES 2041fee21e36SBarry Smith 2042c7afd0dbSLois Curfman McInnes Input Parameters: 2043c7afd0dbSLois Curfman McInnes + snes - the SNES context 2044454a90a3SBarry Smith - type - a known method 2045c7afd0dbSLois Curfman McInnes 2046c7afd0dbSLois Curfman McInnes Options Database Key: 2047454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2048c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2049ae12b187SLois Curfman McInnes 20509b94acceSBarry Smith Notes: 20519b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 205236851e7fSLois Curfman McInnes + SNES_EQ_LS - Newton's method with line search 2053c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 2054c7afd0dbSLois Curfman McInnes . SNES_EQ_TR - Newton's method with trust region 2055c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 2056c7afd0dbSLois Curfman McInnes . SNES_UM_TR - Newton's method with trust region 2057c7afd0dbSLois Curfman McInnes (unconstrained minimization) 205836851e7fSLois Curfman McInnes - SNES_UM_LS - Newton's method with line search 2059c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20609b94acceSBarry Smith 2061ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2062ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2063ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2064ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2065ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2066ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2067ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2068ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2069ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 207036851e7fSLois Curfman McInnes appropriate method. In other words, this routine is not for beginners. 207136851e7fSLois Curfman McInnes 207236851e7fSLois Curfman McInnes Level: intermediate 2073a703fe33SLois Curfman McInnes 2074454a90a3SBarry Smith .keywords: SNES, set, type 20759b94acceSBarry Smith @*/ 2076454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type) 20779b94acceSBarry Smith { 2078*0f5bd95cSBarry Smith int ierr, (*r)(SNES); 2079*0f5bd95cSBarry Smith int match; 20803a40ed3dSBarry Smith 20813a40ed3dSBarry Smith PetscFunctionBegin; 208277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2083*0f5bd95cSBarry Smith PetscValidCharPointer(type); 208482bf6240SBarry Smith 2085*0f5bd95cSBarry Smith match = PetscTypeCompare(snes,type); 2086*0f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 208782bf6240SBarry Smith 208882bf6240SBarry Smith if (snes->setupcalled) { 2089e1311b90SBarry Smith ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr); 209082bf6240SBarry Smith snes->data = 0; 209182bf6240SBarry Smith } 20927d1a2b2bSBarry Smith 20939b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 209482bf6240SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 209582bf6240SBarry Smith 2096454a90a3SBarry Smith ierr = FListFind(snes->comm, SNESList, type,(int (**)(void *)) &r );CHKERRQ(ierr); 209782bf6240SBarry Smith 2098454a90a3SBarry Smith if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",type); 20991548b14aSBarry Smith 2100606d414cSSatish Balay if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);} 210182bf6240SBarry Smith snes->data = 0; 21023a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 210382bf6240SBarry Smith 2104454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 210582bf6240SBarry Smith snes->set_method_called = 1; 210682bf6240SBarry Smith 21073a40ed3dSBarry Smith PetscFunctionReturn(0); 21089b94acceSBarry Smith } 21099b94acceSBarry Smith 2110a847f771SSatish Balay 21119b94acceSBarry Smith /* --------------------------------------------------------------------- */ 21125615d1e5SSatish Balay #undef __FUNC__ 2113d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy" 21149b94acceSBarry Smith /*@C 21159b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 21169b94acceSBarry Smith registered by SNESRegister(). 21179b94acceSBarry Smith 2118fee21e36SBarry Smith Not Collective 2119fee21e36SBarry Smith 212036851e7fSLois Curfman McInnes Level: advanced 212136851e7fSLois Curfman McInnes 21229b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 21239b94acceSBarry Smith 21249b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 21259b94acceSBarry Smith @*/ 2126cf256101SBarry Smith int SNESRegisterDestroy(void) 21279b94acceSBarry Smith { 212882bf6240SBarry Smith int ierr; 212982bf6240SBarry Smith 21303a40ed3dSBarry Smith PetscFunctionBegin; 213182bf6240SBarry Smith if (SNESList) { 2132488ecbafSBarry Smith ierr = FListDestroy( SNESList );CHKERRQ(ierr); 213382bf6240SBarry Smith SNESList = 0; 21349b94acceSBarry Smith } 213584cb2905SBarry Smith SNESRegisterAllCalled = 0; 21363a40ed3dSBarry Smith PetscFunctionReturn(0); 21379b94acceSBarry Smith } 21389b94acceSBarry Smith 21395615d1e5SSatish Balay #undef __FUNC__ 2140d4bb536fSBarry Smith #define __FUNC__ "SNESGetType" 21419b94acceSBarry Smith /*@C 21429a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 21439b94acceSBarry Smith 2144c7afd0dbSLois Curfman McInnes Not Collective 2145c7afd0dbSLois Curfman McInnes 21469b94acceSBarry Smith Input Parameter: 21474b0e389bSBarry Smith . snes - nonlinear solver context 21489b94acceSBarry Smith 21499b94acceSBarry Smith Output Parameter: 2150454a90a3SBarry Smith . type - SNES method (a charactor string) 21519b94acceSBarry Smith 215236851e7fSLois Curfman McInnes Level: intermediate 215336851e7fSLois Curfman McInnes 2154454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 21559b94acceSBarry Smith @*/ 2156454a90a3SBarry Smith int SNESGetType(SNES snes, SNESType *type) 21579b94acceSBarry Smith { 21583a40ed3dSBarry Smith PetscFunctionBegin; 2159184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2160454a90a3SBarry Smith *type = snes->type_name; 21613a40ed3dSBarry Smith PetscFunctionReturn(0); 21629b94acceSBarry Smith } 21639b94acceSBarry Smith 21645615d1e5SSatish Balay #undef __FUNC__ 2165d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution" 21669b94acceSBarry Smith /*@C 21679b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 21689b94acceSBarry Smith stored. 21699b94acceSBarry Smith 2170c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2171c7afd0dbSLois Curfman McInnes 21729b94acceSBarry Smith Input Parameter: 21739b94acceSBarry Smith . snes - the SNES context 21749b94acceSBarry Smith 21759b94acceSBarry Smith Output Parameter: 21769b94acceSBarry Smith . x - the solution 21779b94acceSBarry Smith 217836851e7fSLois Curfman McInnes Level: advanced 217936851e7fSLois Curfman McInnes 21809b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 21819b94acceSBarry Smith 2182a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 21839b94acceSBarry Smith @*/ 21849b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 21859b94acceSBarry Smith { 21863a40ed3dSBarry Smith PetscFunctionBegin; 218777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 21889b94acceSBarry Smith *x = snes->vec_sol_always; 21893a40ed3dSBarry Smith PetscFunctionReturn(0); 21909b94acceSBarry Smith } 21919b94acceSBarry Smith 21925615d1e5SSatish Balay #undef __FUNC__ 2193d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate" 21949b94acceSBarry Smith /*@C 21959b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 21969b94acceSBarry Smith stored. 21979b94acceSBarry Smith 2198c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2199c7afd0dbSLois Curfman McInnes 22009b94acceSBarry Smith Input Parameter: 22019b94acceSBarry Smith . snes - the SNES context 22029b94acceSBarry Smith 22039b94acceSBarry Smith Output Parameter: 22049b94acceSBarry Smith . x - the solution update 22059b94acceSBarry Smith 220636851e7fSLois Curfman McInnes Level: advanced 220736851e7fSLois Curfman McInnes 22089b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 22099b94acceSBarry Smith 22109b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 22119b94acceSBarry Smith @*/ 22129b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 22139b94acceSBarry Smith { 22143a40ed3dSBarry Smith PetscFunctionBegin; 221577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22169b94acceSBarry Smith *x = snes->vec_sol_update_always; 22173a40ed3dSBarry Smith PetscFunctionReturn(0); 22189b94acceSBarry Smith } 22199b94acceSBarry Smith 22205615d1e5SSatish Balay #undef __FUNC__ 2221d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction" 22229b94acceSBarry Smith /*@C 22233638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 22249b94acceSBarry Smith 2225c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2226c7afd0dbSLois Curfman McInnes 22279b94acceSBarry Smith Input Parameter: 22289b94acceSBarry Smith . snes - the SNES context 22299b94acceSBarry Smith 22309b94acceSBarry Smith Output Parameter: 22317bf4e008SBarry Smith + r - the function (or PETSC_NULL) 22327bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 22339b94acceSBarry Smith 22349b94acceSBarry Smith Notes: 22359b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 22369b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 22379b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 22389b94acceSBarry Smith 223936851e7fSLois Curfman McInnes Level: advanced 224036851e7fSLois Curfman McInnes 2241a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 22429b94acceSBarry Smith 224331615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 224431615d04SLois Curfman McInnes SNESGetGradient() 22457bf4e008SBarry Smith 22469b94acceSBarry Smith @*/ 22477bf4e008SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx) 22489b94acceSBarry Smith { 22493a40ed3dSBarry Smith PetscFunctionBegin; 225077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2251a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 2252a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 2253a8c6a408SBarry Smith } 22547bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22557bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22563a40ed3dSBarry Smith PetscFunctionReturn(0); 22579b94acceSBarry Smith } 22589b94acceSBarry Smith 22595615d1e5SSatish Balay #undef __FUNC__ 2260d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient" 22619b94acceSBarry Smith /*@C 22623638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 22639b94acceSBarry Smith 2264c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2265c7afd0dbSLois Curfman McInnes 22669b94acceSBarry Smith Input Parameter: 22679b94acceSBarry Smith . snes - the SNES context 22689b94acceSBarry Smith 22699b94acceSBarry Smith Output Parameter: 22707bf4e008SBarry Smith + r - the gradient (or PETSC_NULL) 22717bf4e008SBarry Smith - ctx - the gradient context (or PETSC_NULL) 22729b94acceSBarry Smith 22739b94acceSBarry Smith Notes: 22749b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 22759b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 22769b94acceSBarry Smith SNESGetFunction(). 22779b94acceSBarry Smith 227836851e7fSLois Curfman McInnes Level: advanced 227936851e7fSLois Curfman McInnes 22809b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 22819b94acceSBarry Smith 22827bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(), 22837bf4e008SBarry Smith SNESSetGradient(), SNESSetFunction() 22847bf4e008SBarry Smith 22859b94acceSBarry Smith @*/ 22867bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx) 22879b94acceSBarry Smith { 22883a40ed3dSBarry Smith PetscFunctionBegin; 228977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22903a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2291a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 22923a40ed3dSBarry Smith } 22937bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22947bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22953a40ed3dSBarry Smith PetscFunctionReturn(0); 22969b94acceSBarry Smith } 22979b94acceSBarry Smith 22985615d1e5SSatish Balay #undef __FUNC__ 2299d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction" 23007bf4e008SBarry Smith /*@C 23019b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 23029b94acceSBarry Smith unconstrained minimization problems. 23039b94acceSBarry Smith 2304c7afd0dbSLois Curfman McInnes Not Collective 2305c7afd0dbSLois Curfman McInnes 23069b94acceSBarry Smith Input Parameter: 23079b94acceSBarry Smith . snes - the SNES context 23089b94acceSBarry Smith 23099b94acceSBarry Smith Output Parameter: 23107bf4e008SBarry Smith + r - the function (or PETSC_NULL) 23117bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 23129b94acceSBarry Smith 23139b94acceSBarry Smith Notes: 23149b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 23159b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 23169b94acceSBarry Smith SNESGetFunction(). 23179b94acceSBarry Smith 231836851e7fSLois Curfman McInnes Level: advanced 231936851e7fSLois Curfman McInnes 23209b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 23219b94acceSBarry Smith 23227bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction() 23237bf4e008SBarry Smith 23249b94acceSBarry Smith @*/ 23257bf4e008SBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r,void **ctx) 23269b94acceSBarry Smith { 23273a40ed3dSBarry Smith PetscFunctionBegin; 232877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 232974679c65SBarry Smith PetscValidScalarPointer(r); 23303a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2331a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 23323a40ed3dSBarry Smith } 23337bf4e008SBarry Smith if (r) *r = snes->fc; 23347bf4e008SBarry Smith if (ctx) *ctx = snes->umfunP; 23353a40ed3dSBarry Smith PetscFunctionReturn(0); 23369b94acceSBarry Smith } 23379b94acceSBarry Smith 23385615d1e5SSatish Balay #undef __FUNC__ 2339d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix" 23403c7409f5SSatish Balay /*@C 23413c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2342d850072dSLois Curfman McInnes SNES options in the database. 23433c7409f5SSatish Balay 2344fee21e36SBarry Smith Collective on SNES 2345fee21e36SBarry Smith 2346c7afd0dbSLois Curfman McInnes Input Parameter: 2347c7afd0dbSLois Curfman McInnes + snes - the SNES context 2348c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2349c7afd0dbSLois Curfman McInnes 2350d850072dSLois Curfman McInnes Notes: 2351a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2352c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2353d850072dSLois Curfman McInnes 235436851e7fSLois Curfman McInnes Level: advanced 235536851e7fSLois Curfman McInnes 23563c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2357a86d99e1SLois Curfman McInnes 2358a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 23593c7409f5SSatish Balay @*/ 23603c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 23613c7409f5SSatish Balay { 23623c7409f5SSatish Balay int ierr; 23633c7409f5SSatish Balay 23643a40ed3dSBarry Smith PetscFunctionBegin; 236577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2366639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 23673c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 23683a40ed3dSBarry Smith PetscFunctionReturn(0); 23693c7409f5SSatish Balay } 23703c7409f5SSatish Balay 23715615d1e5SSatish Balay #undef __FUNC__ 2372d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix" 23733c7409f5SSatish Balay /*@C 2374f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2375d850072dSLois Curfman McInnes SNES options in the database. 23763c7409f5SSatish Balay 2377fee21e36SBarry Smith Collective on SNES 2378fee21e36SBarry Smith 2379c7afd0dbSLois Curfman McInnes Input Parameters: 2380c7afd0dbSLois Curfman McInnes + snes - the SNES context 2381c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2382c7afd0dbSLois Curfman McInnes 2383d850072dSLois Curfman McInnes Notes: 2384a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2385c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2386d850072dSLois Curfman McInnes 238736851e7fSLois Curfman McInnes Level: advanced 238836851e7fSLois Curfman McInnes 23893c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2390a86d99e1SLois Curfman McInnes 2391a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 23923c7409f5SSatish Balay @*/ 23933c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 23943c7409f5SSatish Balay { 23953c7409f5SSatish Balay int ierr; 23963c7409f5SSatish Balay 23973a40ed3dSBarry Smith PetscFunctionBegin; 239877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2399639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 24003c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 24013a40ed3dSBarry Smith PetscFunctionReturn(0); 24023c7409f5SSatish Balay } 24033c7409f5SSatish Balay 24045615d1e5SSatish Balay #undef __FUNC__ 2405d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix" 24069ab63eb5SSatish Balay /*@C 24073c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 24083c7409f5SSatish Balay SNES options in the database. 24093c7409f5SSatish Balay 2410c7afd0dbSLois Curfman McInnes Not Collective 2411c7afd0dbSLois Curfman McInnes 24123c7409f5SSatish Balay Input Parameter: 24133c7409f5SSatish Balay . snes - the SNES context 24143c7409f5SSatish Balay 24153c7409f5SSatish Balay Output Parameter: 24163c7409f5SSatish Balay . prefix - pointer to the prefix string used 24173c7409f5SSatish Balay 24189ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 24199ab63eb5SSatish Balay sufficient length to hold the prefix. 24209ab63eb5SSatish Balay 242136851e7fSLois Curfman McInnes Level: advanced 242236851e7fSLois Curfman McInnes 24233c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2424a86d99e1SLois Curfman McInnes 2425a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 24263c7409f5SSatish Balay @*/ 24273c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 24283c7409f5SSatish Balay { 24293c7409f5SSatish Balay int ierr; 24303c7409f5SSatish Balay 24313a40ed3dSBarry Smith PetscFunctionBegin; 243277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2433639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 24343a40ed3dSBarry Smith PetscFunctionReturn(0); 24353c7409f5SSatish Balay } 24363c7409f5SSatish Balay 2437ca161407SBarry Smith #undef __FUNC__ 2438ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp" 2439ca161407SBarry Smith /*@ 2440ca161407SBarry Smith SNESPrintHelp - Prints all options for the SNES component. 2441ca161407SBarry Smith 2442c7afd0dbSLois Curfman McInnes Collective on SNES 2443c7afd0dbSLois Curfman McInnes 2444ca161407SBarry Smith Input Parameter: 2445ca161407SBarry Smith . snes - the SNES context 2446ca161407SBarry Smith 2447ca161407SBarry Smith Options Database Keys: 2448c7afd0dbSLois Curfman McInnes + -help - Prints SNES options 2449c7afd0dbSLois Curfman McInnes - -h - Prints SNES options 2450ca161407SBarry Smith 245136851e7fSLois Curfman McInnes Level: beginner 245236851e7fSLois Curfman McInnes 2453ca161407SBarry Smith .keywords: SNES, nonlinear, help 2454ca161407SBarry Smith 2455ca161407SBarry Smith .seealso: SNESSetFromOptions() 2456ca161407SBarry Smith @*/ 2457ca161407SBarry Smith int SNESPrintHelp(SNES snes) 2458ca161407SBarry Smith { 2459ca161407SBarry Smith char p[64]; 2460ca161407SBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2461ca161407SBarry Smith int ierr; 2462ca161407SBarry Smith 2463ca161407SBarry Smith PetscFunctionBegin; 2464ca161407SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2465ca161407SBarry Smith 2466549d3d68SSatish Balay ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 2467ca161407SBarry Smith if (snes->prefix) PetscStrcat(p, snes->prefix); 2468ca161407SBarry Smith 2469ca161407SBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2470ca161407SBarry Smith 2471ebb8b11fSBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2472d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr); 2473488ecbafSBarry Smith ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr); 2474d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr); 2475d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr); 2476d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr); 2477d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr); 2478d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr); 2479d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr); 2480d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr); 2481d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr); 2482d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr); 2483d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_monitor: use default SNES convergence monitor, prints\n\ 2484d132466eSBarry Smith residual norm at each iteration.\n",p);CHKERRQ(ierr); 2485d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_smonitor: same as the above, but prints fewer digits of the\n\ 2486ca161407SBarry Smith residual norm for small residual norms. This is useful to conceal\n\ 2487d132466eSBarry Smith meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr); 2488d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr); 2489d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr); 2490d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr); 2491ca161407SBarry Smith if (snes->type == SNES_NONLINEAR_EQUATIONS) { 2492d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2493d132466eSBarry Smith " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr); 2494d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr); 2495d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr); 2496d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr); 2497d132466eSBarry 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); 2498d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr); 2499d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr); 2500d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2501d132466eSBarry Smith " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr); 2502d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2503d132466eSBarry Smith " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr); 2504d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2505d132466eSBarry Smith " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr); 2506d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2507d132466eSBarry Smith " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr); 2508d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2509d132466eSBarry Smith " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr); 2510d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2511d132466eSBarry Smith " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr); 2512d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2513d132466eSBarry Smith " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr); 2514ca161407SBarry Smith } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 2515d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr); 2516d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr); 2517d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr); 2518d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr); 2519ca161407SBarry Smith } 2520454a90a3SBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <type> for help on ",p);CHKERRQ(ierr); 2521d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr); 2522ca161407SBarry Smith if (snes->printhelp) { 2523ca161407SBarry Smith ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr); 2524ca161407SBarry Smith } 2525ca161407SBarry Smith PetscFunctionReturn(0); 2526ca161407SBarry Smith } 25273c7409f5SSatish Balay 2528acb85ae6SSatish Balay /*MC 2529b2002411SLois Curfman McInnes SNESRegister - Adds a method to the nonlinear solver package. 25309b94acceSBarry Smith 2531b2002411SLois Curfman McInnes Synopsis: 2532b2002411SLois Curfman McInnes SNESRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES)) 25339b94acceSBarry Smith 25348d76a1e5SLois Curfman McInnes Not collective 25358d76a1e5SLois Curfman McInnes 2536b2002411SLois Curfman McInnes Input Parameters: 2537c7afd0dbSLois Curfman McInnes + name_solver - name of a new user-defined solver 2538b2002411SLois Curfman McInnes . path - path (either absolute or relative) the library containing this solver 2539b2002411SLois Curfman McInnes . name_create - name of routine to create method context 2540c7afd0dbSLois Curfman McInnes - routine_create - routine to create method context 25419b94acceSBarry Smith 2542b2002411SLois Curfman McInnes Notes: 2543b2002411SLois Curfman McInnes SNESRegister() may be called multiple times to add several user-defined solvers. 25443a40ed3dSBarry Smith 2545b2002411SLois Curfman McInnes If dynamic libraries are used, then the fourth input argument (routine_create) 2546b2002411SLois Curfman McInnes is ignored. 2547b2002411SLois Curfman McInnes 2548b2002411SLois Curfman McInnes Sample usage: 254969225d78SLois Curfman McInnes .vb 2550b2002411SLois Curfman McInnes SNESRegister("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a, 2551b2002411SLois Curfman McInnes "MySolverCreate",MySolverCreate); 255269225d78SLois Curfman McInnes .ve 2553b2002411SLois Curfman McInnes 2554b2002411SLois Curfman McInnes Then, your solver can be chosen with the procedural interface via 2555b2002411SLois Curfman McInnes $ SNESSetType(snes,"my_solver") 2556b2002411SLois Curfman McInnes or at runtime via the option 2557b2002411SLois Curfman McInnes $ -snes_type my_solver 2558b2002411SLois Curfman McInnes 255936851e7fSLois Curfman McInnes Level: advanced 256036851e7fSLois Curfman McInnes 256185614651SBarry Smith $PETSC_ARCH, $PETSC_DIR, $PETSC_LDIR, and $BOPT occuring in pathname will be replaced with appropriate values. 2562dd438238SBarry Smith 2563b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register 2564b2002411SLois Curfman McInnes 2565b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy() 2566b2002411SLois Curfman McInnes M*/ 2567b2002411SLois Curfman McInnes 2568b2002411SLois Curfman McInnes #undef __FUNC__ 2569b2002411SLois Curfman McInnes #define __FUNC__ "SNESRegister_Private" 2570b2002411SLois Curfman McInnes int SNESRegister_Private(char *sname,char *path,char *name,int (*function)(SNES)) 2571b2002411SLois Curfman McInnes { 2572b2002411SLois Curfman McInnes char fullname[256]; 2573b2002411SLois Curfman McInnes int ierr; 2574b2002411SLois Curfman McInnes 2575b2002411SLois Curfman McInnes PetscFunctionBegin; 2576250ffb4eSSatish Balay ierr = FListConcat_Private(path,name,fullname); CHKERRQ(ierr); 2577488ecbafSBarry Smith ierr = FListAdd_Private(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr); 2578b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2579b2002411SLois Curfman McInnes } 2580