1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*606d414cSSatish Balay static char vcid[] = "$Id: snes.c,v 1.190 1999/06/30 22:51:49 bsmith Exp balay $"; 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; 469b94acceSBarry Smith char *method; 4719bcc07fSBarry Smith ViewerType vtype; 489b94acceSBarry Smith 493a40ed3dSBarry Smith PetscFunctionBegin; 5074679c65SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5174679c65SBarry Smith if (viewer) {PetscValidHeader(viewer);} 5274679c65SBarry Smith else { viewer = VIEWER_STDOUT_SELF; } 5374679c65SBarry Smith 5419bcc07fSBarry Smith ierr = ViewerGetType(viewer,&vtype);CHKERRQ(ierr); 553f1db9ecSBarry Smith if (PetscTypeCompare(vtype,ASCII_VIEWER)) { 56d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr); 57d132466eSBarry Smith ierr = SNESGetType(snes,&method);CHKERRQ(ierr); 58d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," method: %s\n",method);CHKERRQ(ierr); 590ef38995SBarry Smith if (snes->view) { 600ef38995SBarry Smith ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 610ef38995SBarry Smith ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr); 620ef38995SBarry Smith ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 630ef38995SBarry Smith } 64d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 65d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 66d132466eSBarry Smith snes->rtol, snes->atol, snes->trunctol, snes->xtol);CHKERRQ(ierr); 67d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr); 68d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr); 690ef38995SBarry Smith if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 70d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr); 710ef38995SBarry Smith } 729b94acceSBarry Smith if (snes->ksp_ewconv) { 739b94acceSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 749b94acceSBarry Smith if (kctx) { 75d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr); 76d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 77d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 789b94acceSBarry Smith } 799b94acceSBarry Smith } 803f1db9ecSBarry Smith } else if (PetscTypeCompare(vtype,STRING_VIEWER)) { 810ef38995SBarry Smith ierr = SNESGetType(snes,&method);CHKERRQ(ierr); 820ef38995SBarry Smith ierr = ViewerStringSPrintf(viewer," %-3.3s",method);CHKERRQ(ierr); 8319bcc07fSBarry Smith } 8477ed5343SBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 850ef38995SBarry Smith ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 869b94acceSBarry Smith ierr = SLESView(sles,viewer);CHKERRQ(ierr); 870ef38995SBarry Smith ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 883a40ed3dSBarry Smith PetscFunctionReturn(0); 899b94acceSBarry Smith } 909b94acceSBarry Smith 91639f9d9dSBarry Smith /* 92639f9d9dSBarry Smith We retain a list of functions that also take SNES command 93639f9d9dSBarry Smith line options. These are called at the end SNESSetFromOptions() 94639f9d9dSBarry Smith */ 95639f9d9dSBarry Smith #define MAXSETFROMOPTIONS 5 96639f9d9dSBarry Smith static int numberofsetfromoptions; 97639f9d9dSBarry Smith static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 98639f9d9dSBarry Smith 995615d1e5SSatish Balay #undef __FUNC__ 100d4bb536fSBarry Smith #define __FUNC__ "SNESAddOptionsChecker" 101639f9d9dSBarry Smith /*@ 102639f9d9dSBarry Smith SNESAddOptionsChecker - Adds an additional function to check for SNES options. 103639f9d9dSBarry Smith 104c7afd0dbSLois Curfman McInnes Not Collective 105c7afd0dbSLois Curfman McInnes 106639f9d9dSBarry Smith Input Parameter: 107639f9d9dSBarry Smith . snescheck - function that checks for options 108639f9d9dSBarry Smith 10936851e7fSLois Curfman McInnes Level: developer 11036851e7fSLois Curfman McInnes 111639f9d9dSBarry Smith .seealso: SNESSetFromOptions() 112639f9d9dSBarry Smith @*/ 113639f9d9dSBarry Smith int SNESAddOptionsChecker(int (*snescheck)(SNES) ) 114639f9d9dSBarry Smith { 1153a40ed3dSBarry Smith PetscFunctionBegin; 116639f9d9dSBarry Smith if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 117a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many options checkers, only 5 allowed"); 118639f9d9dSBarry Smith } 119639f9d9dSBarry Smith 120639f9d9dSBarry Smith othersetfromoptions[numberofsetfromoptions++] = snescheck; 1213a40ed3dSBarry Smith PetscFunctionReturn(0); 122639f9d9dSBarry Smith } 123639f9d9dSBarry Smith 1245615d1e5SSatish Balay #undef __FUNC__ 12515091d37SBarry Smith #define __FUNC__ "SNESSetTypeFromOptions" 12615091d37SBarry Smith /*@ 12715091d37SBarry Smith SNESSetTypeFromOptions - Sets the SNES solver type from the options database, 12815091d37SBarry Smith or sets a default if none is give. 12915091d37SBarry Smith 13015091d37SBarry Smith Collective on SNES 13115091d37SBarry Smith 13215091d37SBarry Smith Input Parameter: 13315091d37SBarry Smith . snes - the SNES context 13415091d37SBarry Smith 13515091d37SBarry Smith Options Database Keys: 13615091d37SBarry Smith . -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc 13715091d37SBarry Smith 13815091d37SBarry Smith Level: beginner 13915091d37SBarry Smith 14015091d37SBarry Smith .keywords: SNES, nonlinear, set, options, database 14115091d37SBarry Smith 14215091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetFromOptions() 14315091d37SBarry Smith @*/ 14415091d37SBarry Smith int SNESSetTypeFromOptions(SNES snes) 14515091d37SBarry Smith { 14615091d37SBarry Smith char method[256]; 14715091d37SBarry Smith int ierr, flg; 14815091d37SBarry Smith 14915091d37SBarry Smith PetscFunctionBegin; 15015091d37SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15115091d37SBarry Smith if (snes->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to SNESSetUp()"); 152888f2ed8SSatish Balay ierr = OptionsGetString(snes->prefix,"-snes_type",method,256,&flg);CHKERRQ(ierr); 15315091d37SBarry Smith if (flg) { 15415091d37SBarry Smith ierr = SNESSetType(snes,(SNESType) method);CHKERRQ(ierr); 15515091d37SBarry Smith } 15615091d37SBarry Smith /* 15715091d37SBarry Smith If SNES type has not yet been set, set it now 15815091d37SBarry Smith */ 15915091d37SBarry Smith if (!snes->type_name) { 16015091d37SBarry Smith if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 16115091d37SBarry Smith ierr = SNESSetType(snes,SNES_EQ_LS);CHKERRQ(ierr); 16215091d37SBarry Smith } else { 16315091d37SBarry Smith ierr = SNESSetType(snes,SNES_UM_TR);CHKERRQ(ierr); 16415091d37SBarry Smith } 16515091d37SBarry Smith } 16615091d37SBarry Smith PetscFunctionReturn(0); 16715091d37SBarry Smith } 16815091d37SBarry Smith 16915091d37SBarry Smith #undef __FUNC__ 1705615d1e5SSatish Balay #define __FUNC__ "SNESSetFromOptions" 1719b94acceSBarry Smith /*@ 172682d7d0cSBarry Smith SNESSetFromOptions - Sets various SNES and SLES parameters from user options. 1739b94acceSBarry Smith 174c7afd0dbSLois Curfman McInnes Collective on SNES 175c7afd0dbSLois Curfman McInnes 1769b94acceSBarry Smith Input Parameter: 1779b94acceSBarry Smith . snes - the SNES context 1789b94acceSBarry Smith 17936851e7fSLois Curfman McInnes Options Database Keys: 180b39c3a46SLois Curfman McInnes + -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc 18182738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 18282738288SBarry Smith of the change in the solution between steps 183b39c3a46SLois Curfman McInnes . -snes_atol <atol> - absolute tolerance of residual norm 184b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 185b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 186b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 187b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 18882738288SBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear or minimization 18982738288SBarry Smith solver; hence iterations will continue until max_it 1901fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 19182738288SBarry Smith of convergence test 19282738288SBarry Smith . -snes_monitor - prints residual norm at each iteration 193d132466eSBarry Smith . -snes_vecmonitor - plots solution at each iteration 194d132466eSBarry Smith . -snes_vecmonitor_update - plots update to solution at each iteration 19582738288SBarry Smith . -snes_xmonitor - plots residual norm at each iteration 196e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 19736851e7fSLois Curfman McInnes - -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 19882738288SBarry Smith 19982738288SBarry Smith Options Database for Eisenstat-Walker method: 20082738288SBarry Smith + -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence 20182738288SBarry Smith . -snes_ksp_eq_version ver - version of Eisenstat-Walker method 20236851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 20336851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 20436851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 20536851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 20636851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 20736851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 20882738288SBarry Smith 20911ca99fdSLois Curfman McInnes Notes: 21011ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 21111ca99fdSLois Curfman McInnes the users manual. 21283e2fdc7SBarry Smith 21336851e7fSLois Curfman McInnes Level: beginner 21436851e7fSLois Curfman McInnes 2159b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 2169b94acceSBarry Smith 21715091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetTypeFromOptions() 2189b94acceSBarry Smith @*/ 2199b94acceSBarry Smith int SNESSetFromOptions(SNES snes) 2209b94acceSBarry Smith { 2219b94acceSBarry Smith double tmp; 2229b94acceSBarry Smith SLES sles; 22381f57ec7SBarry Smith int ierr, flg,i,loc[4],nmax = 4; 2243c7409f5SSatish Balay int version = PETSC_DEFAULT; 2259b94acceSBarry Smith double rtol_0 = PETSC_DEFAULT; 2269b94acceSBarry Smith double rtol_max = PETSC_DEFAULT; 2279b94acceSBarry Smith double gamma2 = PETSC_DEFAULT; 2289b94acceSBarry Smith double alpha = PETSC_DEFAULT; 2299b94acceSBarry Smith double alpha2 = PETSC_DEFAULT; 2309b94acceSBarry Smith double threshold = PETSC_DEFAULT; 2319b94acceSBarry Smith 2323a40ed3dSBarry Smith PetscFunctionBegin; 23377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 23415091d37SBarry Smith ierr = SNESSetTypeFromOptions(snes);CHKERRQ(ierr); 235ca161407SBarry Smith 23615091d37SBarry Smith loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300; 2373c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg);CHKERRQ(ierr); 238d64ed03dSBarry Smith if (flg) { 239d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 240d64ed03dSBarry Smith } 2413c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg);CHKERRQ(ierr); 242d64ed03dSBarry Smith if (flg) { 243d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 244d64ed03dSBarry Smith } 2453c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg);CHKERRQ(ierr); 246d64ed03dSBarry Smith if (flg) { 247d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 248d64ed03dSBarry Smith } 2493c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg);CHKERRQ(ierr); 2503c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr); 251d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg);CHKERRQ(ierr); 252d64ed03dSBarry Smith if (flg) { ierr = SNESSetTrustRegionTolerance(snes,tmp);CHKERRQ(ierr); } 253d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg);CHKERRQ(ierr); 254d64ed03dSBarry Smith if (flg) { ierr = SNESSetMinimizationFunctionTolerance(snes,tmp);CHKERRQ(ierr);} 2553c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg);CHKERRQ(ierr); 2563c7409f5SSatish Balay if (flg) { snes->ksp_ewconv = 1; } 257b18e04deSLois Curfman McInnes ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg);CHKERRQ(ierr); 258b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg);CHKERRQ(ierr); 259b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr); 260b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg);CHKERRQ(ierr); 261b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg);CHKERRQ(ierr); 262b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg);CHKERRQ(ierr); 263b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr); 26493c39befSBarry Smith 26593c39befSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_no_convergence_test",&flg);CHKERRQ(ierr); 26693c39befSBarry Smith if (flg) {snes->converged = 0;} 26793c39befSBarry Smith 2689b94acceSBarry Smith ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 2699b94acceSBarry Smith alpha2,threshold);CHKERRQ(ierr); 2705bbad29bSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_cancelmonitors",&flg);CHKERRQ(ierr); 2715cd90555SBarry Smith if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);} 2723c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg);CHKERRQ(ierr); 273b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);} 2743c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg);CHKERRQ(ierr); 275b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);} 2763f1db9ecSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor",&flg);CHKERRQ(ierr); 277b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);} 278d132466eSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor_update",&flg);CHKERRQ(ierr); 279b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitorUpdate,0,0);CHKERRQ(ierr);} 28081f57ec7SBarry Smith ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr); 2813c7409f5SSatish Balay if (flg) { 28217699dbbSLois Curfman McInnes int rank = 0; 283d7e8b826SBarry Smith DrawLG lg; 28417699dbbSLois Curfman McInnes MPI_Initialized(&rank); 285d132466eSBarry Smith if (rank) {ierr = MPI_Comm_rank(snes->comm,&rank);CHKERRQ(ierr);} 28617699dbbSLois Curfman McInnes if (!rank) { 28781f57ec7SBarry Smith ierr = SNESLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg);CHKERRQ(ierr); 288b8a78c4aSBarry Smith ierr = SNESSetMonitor(snes,SNESLGMonitor,lg,( int (*)(void *))SNESLGMonitorDestroy);CHKERRQ(ierr); 2899b94acceSBarry Smith PLogObjectParent(snes,lg); 2909b94acceSBarry Smith } 2919b94acceSBarry Smith } 292e24b481bSBarry Smith 2933c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);CHKERRQ(ierr); 2943c7409f5SSatish Balay if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 2959b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 2969b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 297981c4779SBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 298981c4779SBarry Smith } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 29931615d04SLois Curfman McInnes ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 30031615d04SLois Curfman McInnes SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr); 301d64ed03dSBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 3029b94acceSBarry Smith } 303639f9d9dSBarry Smith 304639f9d9dSBarry Smith for ( i=0; i<numberofsetfromoptions; i++ ) { 305639f9d9dSBarry Smith ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 306639f9d9dSBarry Smith } 307639f9d9dSBarry Smith 3089b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 3099b94acceSBarry Smith ierr = SLESSetFromOptions(sles);CHKERRQ(ierr); 31093993e2dSLois Curfman McInnes 311e24b481bSBarry Smith /* set the special KSP monitor for matrix-free application */ 312e24b481bSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg);CHKERRQ(ierr); 313e24b481bSBarry Smith if (flg) { 314e24b481bSBarry Smith KSP ksp; 315e24b481bSBarry Smith ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 316b8a78c4aSBarry Smith ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr); 317e24b481bSBarry Smith } 318e24b481bSBarry Smith 31982bf6240SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help", &flg);CHKERRQ(ierr); 32082bf6240SBarry Smith if (flg) { ierr = SNESPrintHelp(snes);CHKERRQ(ierr);} 32182bf6240SBarry Smith 3223a40ed3dSBarry Smith if (snes->setfromoptions) { 3233a40ed3dSBarry Smith ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr); 3243a40ed3dSBarry Smith } 3253a40ed3dSBarry Smith PetscFunctionReturn(0); 3269b94acceSBarry Smith } 3279b94acceSBarry Smith 328a847f771SSatish Balay 3295615d1e5SSatish Balay #undef __FUNC__ 330d4bb536fSBarry Smith #define __FUNC__ "SNESSetApplicationContext" 3319b94acceSBarry Smith /*@ 3329b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3339b94acceSBarry Smith the nonlinear solvers. 3349b94acceSBarry Smith 335fee21e36SBarry Smith Collective on SNES 336fee21e36SBarry Smith 337c7afd0dbSLois Curfman McInnes Input Parameters: 338c7afd0dbSLois Curfman McInnes + snes - the SNES context 339c7afd0dbSLois Curfman McInnes - usrP - optional user context 340c7afd0dbSLois Curfman McInnes 34136851e7fSLois Curfman McInnes Level: intermediate 34236851e7fSLois Curfman McInnes 3439b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3449b94acceSBarry Smith 3459b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3469b94acceSBarry Smith @*/ 3479b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 3489b94acceSBarry Smith { 3493a40ed3dSBarry Smith PetscFunctionBegin; 35077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3519b94acceSBarry Smith snes->user = usrP; 3523a40ed3dSBarry Smith PetscFunctionReturn(0); 3539b94acceSBarry Smith } 35474679c65SBarry Smith 3555615d1e5SSatish Balay #undef __FUNC__ 356d4bb536fSBarry Smith #define __FUNC__ "SNESGetApplicationContext" 3579b94acceSBarry Smith /*@C 3589b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3599b94acceSBarry Smith nonlinear solvers. 3609b94acceSBarry Smith 361c7afd0dbSLois Curfman McInnes Not Collective 362c7afd0dbSLois Curfman McInnes 3639b94acceSBarry Smith Input Parameter: 3649b94acceSBarry Smith . snes - SNES context 3659b94acceSBarry Smith 3669b94acceSBarry Smith Output Parameter: 3679b94acceSBarry Smith . usrP - user context 3689b94acceSBarry Smith 36936851e7fSLois Curfman McInnes Level: intermediate 37036851e7fSLois Curfman McInnes 3719b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3729b94acceSBarry Smith 3739b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3749b94acceSBarry Smith @*/ 3759b94acceSBarry Smith int SNESGetApplicationContext( SNES snes, void **usrP ) 3769b94acceSBarry Smith { 3773a40ed3dSBarry Smith PetscFunctionBegin; 37877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3799b94acceSBarry Smith *usrP = snes->user; 3803a40ed3dSBarry Smith PetscFunctionReturn(0); 3819b94acceSBarry Smith } 38274679c65SBarry Smith 3835615d1e5SSatish Balay #undef __FUNC__ 384d4bb536fSBarry Smith #define __FUNC__ "SNESGetIterationNumber" 3859b94acceSBarry Smith /*@ 386c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 387c8228a4eSBarry Smith at this time. 3889b94acceSBarry Smith 389c7afd0dbSLois Curfman McInnes Not Collective 390c7afd0dbSLois Curfman McInnes 3919b94acceSBarry Smith Input Parameter: 3929b94acceSBarry Smith . snes - SNES context 3939b94acceSBarry Smith 3949b94acceSBarry Smith Output Parameter: 3959b94acceSBarry Smith . iter - iteration number 3969b94acceSBarry Smith 397c8228a4eSBarry Smith Notes: 398c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 399c8228a4eSBarry Smith 400c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 40108405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 40208405cd6SLois Curfman McInnes .vb 40308405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 40408405cd6SLois Curfman McInnes if (!(it % 2)) { 40508405cd6SLois Curfman McInnes [compute Jacobian here] 40608405cd6SLois Curfman McInnes } 40708405cd6SLois Curfman McInnes .ve 408c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 40908405cd6SLois Curfman McInnes recomputed every second SNES iteration. 410c8228a4eSBarry Smith 41136851e7fSLois Curfman McInnes Level: intermediate 41236851e7fSLois Curfman McInnes 4139b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 4149b94acceSBarry Smith @*/ 4159b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 4169b94acceSBarry Smith { 4173a40ed3dSBarry Smith PetscFunctionBegin; 41877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 41974679c65SBarry Smith PetscValidIntPointer(iter); 4209b94acceSBarry Smith *iter = snes->iter; 4213a40ed3dSBarry Smith PetscFunctionReturn(0); 4229b94acceSBarry Smith } 42374679c65SBarry Smith 4245615d1e5SSatish Balay #undef __FUNC__ 4255615d1e5SSatish Balay #define __FUNC__ "SNESGetFunctionNorm" 4269b94acceSBarry Smith /*@ 4279b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4289b94acceSBarry Smith with SNESSSetFunction(). 4299b94acceSBarry Smith 430c7afd0dbSLois Curfman McInnes Collective on SNES 431c7afd0dbSLois Curfman McInnes 4329b94acceSBarry Smith Input Parameter: 4339b94acceSBarry Smith . snes - SNES context 4349b94acceSBarry Smith 4359b94acceSBarry Smith Output Parameter: 4369b94acceSBarry Smith . fnorm - 2-norm of function 4379b94acceSBarry Smith 4389b94acceSBarry Smith Note: 4399b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 440a86d99e1SLois Curfman McInnes A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 441a86d99e1SLois Curfman McInnes SNESGetGradientNorm(). 4429b94acceSBarry Smith 44336851e7fSLois Curfman McInnes Level: intermediate 44436851e7fSLois Curfman McInnes 4459b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 446a86d99e1SLois Curfman McInnes 44708405cd6SLois Curfman McInnes .seealso: SNESGetFunction() 4489b94acceSBarry Smith @*/ 4499b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 4509b94acceSBarry Smith { 4513a40ed3dSBarry Smith PetscFunctionBegin; 45277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 45374679c65SBarry Smith PetscValidScalarPointer(fnorm); 45474679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 455d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only"); 45674679c65SBarry Smith } 4579b94acceSBarry Smith *fnorm = snes->norm; 4583a40ed3dSBarry Smith PetscFunctionReturn(0); 4599b94acceSBarry Smith } 46074679c65SBarry Smith 4615615d1e5SSatish Balay #undef __FUNC__ 4625615d1e5SSatish Balay #define __FUNC__ "SNESGetGradientNorm" 4639b94acceSBarry Smith /*@ 4649b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 4659b94acceSBarry Smith with SNESSSetGradient(). 4669b94acceSBarry Smith 467c7afd0dbSLois Curfman McInnes Collective on SNES 468c7afd0dbSLois Curfman McInnes 4699b94acceSBarry Smith Input Parameter: 4709b94acceSBarry Smith . snes - SNES context 4719b94acceSBarry Smith 4729b94acceSBarry Smith Output Parameter: 4739b94acceSBarry Smith . fnorm - 2-norm of gradient 4749b94acceSBarry Smith 4759b94acceSBarry Smith Note: 4769b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 477a86d99e1SLois Curfman McInnes methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 478a86d99e1SLois Curfman McInnes is SNESGetFunctionNorm(). 4799b94acceSBarry Smith 48036851e7fSLois Curfman McInnes Level: intermediate 48136851e7fSLois Curfman McInnes 4829b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 483a86d99e1SLois Curfman McInnes 484a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient() 4859b94acceSBarry Smith @*/ 4869b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 4879b94acceSBarry Smith { 4883a40ed3dSBarry Smith PetscFunctionBegin; 48977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 49074679c65SBarry Smith PetscValidScalarPointer(gnorm); 49174679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 492d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 49374679c65SBarry Smith } 4949b94acceSBarry Smith *gnorm = snes->norm; 4953a40ed3dSBarry Smith PetscFunctionReturn(0); 4969b94acceSBarry Smith } 49774679c65SBarry Smith 4985615d1e5SSatish Balay #undef __FUNC__ 499d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberUnsuccessfulSteps" 5009b94acceSBarry Smith /*@ 5019b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 5029b94acceSBarry Smith attempted by the nonlinear solver. 5039b94acceSBarry Smith 504c7afd0dbSLois Curfman McInnes Not Collective 505c7afd0dbSLois Curfman McInnes 5069b94acceSBarry Smith Input Parameter: 5079b94acceSBarry Smith . snes - SNES context 5089b94acceSBarry Smith 5099b94acceSBarry Smith Output Parameter: 5109b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 5119b94acceSBarry Smith 512c96a6f78SLois Curfman McInnes Notes: 513c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 514c96a6f78SLois Curfman McInnes 51536851e7fSLois Curfman McInnes Level: intermediate 51636851e7fSLois Curfman McInnes 5179b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 5189b94acceSBarry Smith @*/ 5199b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 5209b94acceSBarry Smith { 5213a40ed3dSBarry Smith PetscFunctionBegin; 52277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 52374679c65SBarry Smith PetscValidIntPointer(nfails); 5249b94acceSBarry Smith *nfails = snes->nfailures; 5253a40ed3dSBarry Smith PetscFunctionReturn(0); 5269b94acceSBarry Smith } 527a847f771SSatish Balay 5285615d1e5SSatish Balay #undef __FUNC__ 529d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberLinearIterations" 530c96a6f78SLois Curfman McInnes /*@ 531c96a6f78SLois Curfman McInnes SNESGetNumberLinearIterations - Gets the total number of linear iterations 532c96a6f78SLois Curfman McInnes used by the nonlinear solver. 533c96a6f78SLois Curfman McInnes 534c7afd0dbSLois Curfman McInnes Not Collective 535c7afd0dbSLois Curfman McInnes 536c96a6f78SLois Curfman McInnes Input Parameter: 537c96a6f78SLois Curfman McInnes . snes - SNES context 538c96a6f78SLois Curfman McInnes 539c96a6f78SLois Curfman McInnes Output Parameter: 540c96a6f78SLois Curfman McInnes . lits - number of linear iterations 541c96a6f78SLois Curfman McInnes 542c96a6f78SLois Curfman McInnes Notes: 543c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 544c96a6f78SLois Curfman McInnes 54536851e7fSLois Curfman McInnes Level: intermediate 54636851e7fSLois Curfman McInnes 547c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 548c96a6f78SLois Curfman McInnes @*/ 54986bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits) 550c96a6f78SLois Curfman McInnes { 5513a40ed3dSBarry Smith PetscFunctionBegin; 552c96a6f78SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 553c96a6f78SLois Curfman McInnes PetscValidIntPointer(lits); 554c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 5553a40ed3dSBarry Smith PetscFunctionReturn(0); 556c96a6f78SLois Curfman McInnes } 557c96a6f78SLois Curfman McInnes 558c96a6f78SLois Curfman McInnes #undef __FUNC__ 559d4bb536fSBarry Smith #define __FUNC__ "SNESGetSLES" 5609b94acceSBarry Smith /*@C 5619b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 5629b94acceSBarry Smith 563c7afd0dbSLois Curfman McInnes Not Collective, but if SNES object is parallel, then SLES object is parallel 564c7afd0dbSLois Curfman McInnes 5659b94acceSBarry Smith Input Parameter: 5669b94acceSBarry Smith . snes - the SNES context 5679b94acceSBarry Smith 5689b94acceSBarry Smith Output Parameter: 5699b94acceSBarry Smith . sles - the SLES context 5709b94acceSBarry Smith 5719b94acceSBarry Smith Notes: 5729b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 5739b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 5749b94acceSBarry Smith KSP and PC contexts as well. 5759b94acceSBarry Smith 57636851e7fSLois Curfman McInnes Level: beginner 57736851e7fSLois Curfman McInnes 5789b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 5799b94acceSBarry Smith 5809b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 5819b94acceSBarry Smith @*/ 5829b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 5839b94acceSBarry Smith { 5843a40ed3dSBarry Smith PetscFunctionBegin; 58577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5869b94acceSBarry Smith *sles = snes->sles; 5873a40ed3dSBarry Smith PetscFunctionReturn(0); 5889b94acceSBarry Smith } 58982bf6240SBarry Smith 590e24b481bSBarry Smith #undef __FUNC__ 591e24b481bSBarry Smith #define __FUNC__ "SNESPublish_Petsc" 592e24b481bSBarry Smith static int SNESPublish_Petsc(PetscObject object) 593e24b481bSBarry Smith { 594aa482453SBarry Smith #if defined(PETSC_HAVE_AMS) 595e24b481bSBarry Smith SNES v = (SNES) object; 596e24b481bSBarry Smith int ierr; 597e24b481bSBarry Smith 598e24b481bSBarry Smith PetscFunctionBegin; 599e24b481bSBarry Smith 600e24b481bSBarry Smith /* if it is already published then return */ 601e24b481bSBarry Smith if (v->amem >=0 ) PetscFunctionReturn(0); 602e24b481bSBarry Smith 6033f1db9ecSBarry Smith ierr = PetscObjectPublishBaseBegin(object);CHKERRQ(ierr); 604e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ, 605e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 606e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ, 607e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 608e24b481bSBarry Smith ierr = PetscObjectPublishBaseEnd(object);CHKERRQ(ierr); 609e24b481bSBarry Smith #else 610e24b481bSBarry Smith PetscFunctionBegin; 611e24b481bSBarry Smith #endif 612e24b481bSBarry Smith PetscFunctionReturn(0); 613e24b481bSBarry Smith } 614e24b481bSBarry Smith 6159b94acceSBarry Smith /* -----------------------------------------------------------*/ 6165615d1e5SSatish Balay #undef __FUNC__ 6175615d1e5SSatish Balay #define __FUNC__ "SNESCreate" 6189b94acceSBarry Smith /*@C 6199b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 6209b94acceSBarry Smith 621c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 622c7afd0dbSLois Curfman McInnes 623c7afd0dbSLois Curfman McInnes Input Parameters: 624c7afd0dbSLois Curfman McInnes + comm - MPI communicator 625c7afd0dbSLois Curfman McInnes - type - type of method, either 626c7afd0dbSLois Curfman McInnes SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations) 627c7afd0dbSLois Curfman McInnes or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization) 6289b94acceSBarry Smith 6299b94acceSBarry Smith Output Parameter: 6309b94acceSBarry Smith . outsnes - the new SNES context 6319b94acceSBarry Smith 632c7afd0dbSLois Curfman McInnes Options Database Keys: 633c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 634c7afd0dbSLois Curfman McInnes and no preconditioning matrix 635c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 636c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 637c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 638c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 639c1f60f51SBarry Smith 64036851e7fSLois Curfman McInnes Level: beginner 64136851e7fSLois Curfman McInnes 6429b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 6439b94acceSBarry Smith 64463a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy() 6459b94acceSBarry Smith @*/ 6464b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 6479b94acceSBarry Smith { 6489b94acceSBarry Smith int ierr; 6499b94acceSBarry Smith SNES snes; 6509b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 65137fcc0dbSBarry Smith 6523a40ed3dSBarry Smith PetscFunctionBegin; 6539b94acceSBarry Smith *outsnes = 0; 654d64ed03dSBarry Smith if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){ 655d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type"); 656d64ed03dSBarry Smith } 6573f1db9ecSBarry Smith PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView); 6589b94acceSBarry Smith PLogObjectCreate(snes); 659e24b481bSBarry Smith snes->bops->publish = SNESPublish_Petsc; 6609b94acceSBarry Smith snes->max_its = 50; 6619750a799SBarry Smith snes->max_funcs = 10000; 6629b94acceSBarry Smith snes->norm = 0.0; 6635a2d0531SBarry Smith if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 664b18e04deSLois Curfman McInnes snes->rtol = 1.e-8; 665b18e04deSLois Curfman McInnes snes->ttol = 0.0; 6669b94acceSBarry Smith snes->atol = 1.e-10; 667d64ed03dSBarry Smith } else { 668b4874afaSBarry Smith snes->rtol = 1.e-8; 669b4874afaSBarry Smith snes->ttol = 0.0; 670b4874afaSBarry Smith snes->atol = 1.e-50; 671b4874afaSBarry Smith } 6729b94acceSBarry Smith snes->xtol = 1.e-8; 673b18e04deSLois Curfman McInnes snes->trunctol = 1.e-12; /* no longer used */ 6749b94acceSBarry Smith snes->nfuncs = 0; 6759b94acceSBarry Smith snes->nfailures = 0; 6767a00f4a9SLois Curfman McInnes snes->linear_its = 0; 677639f9d9dSBarry Smith snes->numbermonitors = 0; 6789b94acceSBarry Smith snes->data = 0; 6799b94acceSBarry Smith snes->view = 0; 6809b94acceSBarry Smith snes->computeumfunction = 0; 6819b94acceSBarry Smith snes->umfunP = 0; 6829b94acceSBarry Smith snes->fc = 0; 6839b94acceSBarry Smith snes->deltatol = 1.e-12; 6849b94acceSBarry Smith snes->fmin = -1.e30; 6859b94acceSBarry Smith snes->method_class = type; 6869b94acceSBarry Smith snes->set_method_called = 0; 68782bf6240SBarry Smith snes->setupcalled = 0; 6889b94acceSBarry Smith snes->ksp_ewconv = 0; 6896f24a144SLois Curfman McInnes snes->vwork = 0; 6906f24a144SLois Curfman McInnes snes->nwork = 0; 691758f92a0SBarry Smith snes->conv_hist_len = 0; 692758f92a0SBarry Smith snes->conv_hist_max = 0; 693758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 694758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 695758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 6969b94acceSBarry Smith 6979b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 6980452661fSBarry Smith kctx = PetscNew(SNES_KSP_EW_ConvCtx);CHKPTRQ(kctx); 699eed86810SBarry Smith PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx)); 7009b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 7019b94acceSBarry Smith kctx->version = 2; 7029b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 7039b94acceSBarry Smith this was too large for some test cases */ 7049b94acceSBarry Smith kctx->rtol_last = 0; 7059b94acceSBarry Smith kctx->rtol_max = .9; 7069b94acceSBarry Smith kctx->gamma = 1.0; 7079b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 7089b94acceSBarry Smith kctx->alpha = kctx->alpha2; 7099b94acceSBarry Smith kctx->threshold = .1; 7109b94acceSBarry Smith kctx->lresid_last = 0; 7119b94acceSBarry Smith kctx->norm_last = 0; 7129b94acceSBarry Smith 7139b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr); 7149b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 7155334005bSBarry Smith 7169b94acceSBarry Smith *outsnes = snes; 717e24b481bSBarry Smith PetscPublishAll(snes); 7183a40ed3dSBarry Smith PetscFunctionReturn(0); 7199b94acceSBarry Smith } 7209b94acceSBarry Smith 7219b94acceSBarry Smith /* --------------------------------------------------------------- */ 7225615d1e5SSatish Balay #undef __FUNC__ 723d4bb536fSBarry Smith #define __FUNC__ "SNESSetFunction" 7249b94acceSBarry Smith /*@C 7259b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 7269b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 7279b94acceSBarry Smith equations. 7289b94acceSBarry Smith 729fee21e36SBarry Smith Collective on SNES 730fee21e36SBarry Smith 731c7afd0dbSLois Curfman McInnes Input Parameters: 732c7afd0dbSLois Curfman McInnes + snes - the SNES context 733c7afd0dbSLois Curfman McInnes . func - function evaluation routine 734c7afd0dbSLois Curfman McInnes . r - vector to store function value 735c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 736c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 7379b94acceSBarry Smith 738c7afd0dbSLois Curfman McInnes Calling sequence of func: 7398d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 740c7afd0dbSLois Curfman McInnes 741313e4042SLois Curfman McInnes . f - function vector 742c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 7439b94acceSBarry Smith 7449b94acceSBarry Smith Notes: 7459b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 7469b94acceSBarry Smith $ f'(x) x = -f(x), 747c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 7489b94acceSBarry Smith 7499b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7509b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7519b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 7529b94acceSBarry Smith 75336851e7fSLois Curfman McInnes Level: beginner 75436851e7fSLois Curfman McInnes 7559b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 7569b94acceSBarry Smith 757a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 7589b94acceSBarry Smith @*/ 7595334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx) 7609b94acceSBarry Smith { 7613a40ed3dSBarry Smith PetscFunctionBegin; 76277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 763a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 764a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 765a8c6a408SBarry Smith } 7669b94acceSBarry Smith snes->computefunction = func; 7679b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 7689b94acceSBarry Smith snes->funP = ctx; 7693a40ed3dSBarry Smith PetscFunctionReturn(0); 7709b94acceSBarry Smith } 7719b94acceSBarry Smith 7725615d1e5SSatish Balay #undef __FUNC__ 7735615d1e5SSatish Balay #define __FUNC__ "SNESComputeFunction" 7749b94acceSBarry Smith /*@ 77536851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 7769b94acceSBarry Smith SNESSetFunction(). 7779b94acceSBarry Smith 778c7afd0dbSLois Curfman McInnes Collective on SNES 779c7afd0dbSLois Curfman McInnes 7809b94acceSBarry Smith Input Parameters: 781c7afd0dbSLois Curfman McInnes + snes - the SNES context 782c7afd0dbSLois Curfman McInnes - x - input vector 7839b94acceSBarry Smith 7849b94acceSBarry Smith Output Parameter: 7853638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 7869b94acceSBarry Smith 7871bffabb2SLois Curfman McInnes Notes: 7889b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7899b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7909b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 7919b94acceSBarry Smith 79236851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 79336851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 79436851e7fSLois Curfman McInnes themselves. 79536851e7fSLois Curfman McInnes 79636851e7fSLois Curfman McInnes Level: developer 79736851e7fSLois Curfman McInnes 7989b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 7999b94acceSBarry Smith 800a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 8019b94acceSBarry Smith @*/ 8029b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y) 8039b94acceSBarry Smith { 8049b94acceSBarry Smith int ierr; 8059b94acceSBarry Smith 8063a40ed3dSBarry Smith PetscFunctionBegin; 807d4bb536fSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 808a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 809d4bb536fSBarry Smith } 8109b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 811d64ed03dSBarry Smith PetscStackPush("SNES user function"); 8129b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 813d64ed03dSBarry Smith PetscStackPop; 814ae3c334cSLois Curfman McInnes snes->nfuncs++; 8159b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 8163a40ed3dSBarry Smith PetscFunctionReturn(0); 8179b94acceSBarry Smith } 8189b94acceSBarry Smith 8195615d1e5SSatish Balay #undef __FUNC__ 820d4bb536fSBarry Smith #define __FUNC__ "SNESSetMinimizationFunction" 8219b94acceSBarry Smith /*@C 8229b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 8239b94acceSBarry Smith unconstrained minimization. 8249b94acceSBarry Smith 825fee21e36SBarry Smith Collective on SNES 826fee21e36SBarry Smith 827c7afd0dbSLois Curfman McInnes Input Parameters: 828c7afd0dbSLois Curfman McInnes + snes - the SNES context 829c7afd0dbSLois Curfman McInnes . func - function evaluation routine 830c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 831c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8329b94acceSBarry Smith 833c7afd0dbSLois Curfman McInnes Calling sequence of func: 8348d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,double *f,void *ctx); 835c7afd0dbSLois Curfman McInnes 836c7afd0dbSLois Curfman McInnes + x - input vector 8379b94acceSBarry Smith . f - function 838c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined function context 8399b94acceSBarry Smith 84036851e7fSLois Curfman McInnes Level: beginner 84136851e7fSLois Curfman McInnes 8429b94acceSBarry Smith Notes: 8439b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 8449b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 8459b94acceSBarry Smith SNESSetFunction(). 8469b94acceSBarry Smith 8479b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 8489b94acceSBarry Smith 849a86d99e1SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 850a86d99e1SLois Curfman McInnes SNESSetHessian(), SNESSetGradient() 8519b94acceSBarry Smith @*/ 8529b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*), 8539b94acceSBarry Smith void *ctx) 8549b94acceSBarry Smith { 8553a40ed3dSBarry Smith PetscFunctionBegin; 85677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 857a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 858a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 859a8c6a408SBarry Smith } 8609b94acceSBarry Smith snes->computeumfunction = func; 8619b94acceSBarry Smith snes->umfunP = ctx; 8623a40ed3dSBarry Smith PetscFunctionReturn(0); 8639b94acceSBarry Smith } 8649b94acceSBarry Smith 8655615d1e5SSatish Balay #undef __FUNC__ 8665615d1e5SSatish Balay #define __FUNC__ "SNESComputeMinimizationFunction" 8679b94acceSBarry Smith /*@ 8689b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 8699b94acceSBarry Smith set with SNESSetMinimizationFunction(). 8709b94acceSBarry Smith 871c7afd0dbSLois Curfman McInnes Collective on SNES 872c7afd0dbSLois Curfman McInnes 8739b94acceSBarry Smith Input Parameters: 874c7afd0dbSLois Curfman McInnes + snes - the SNES context 875c7afd0dbSLois Curfman McInnes - x - input vector 8769b94acceSBarry Smith 8779b94acceSBarry Smith Output Parameter: 8789b94acceSBarry Smith . y - function value 8799b94acceSBarry Smith 8809b94acceSBarry Smith Notes: 8819b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 8829b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 8839b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 884a86d99e1SLois Curfman McInnes 88536851e7fSLois Curfman McInnes SNESComputeMinimizationFunction() is typically used within minimization 88636851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 88736851e7fSLois Curfman McInnes themselves. 88836851e7fSLois Curfman McInnes 88936851e7fSLois Curfman McInnes Level: developer 89036851e7fSLois Curfman McInnes 891a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function 892a86d99e1SLois Curfman McInnes 893a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 894a86d99e1SLois Curfman McInnes SNESComputeGradient(), SNESComputeHessian() 8959b94acceSBarry Smith @*/ 8969b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 8979b94acceSBarry Smith { 8989b94acceSBarry Smith int ierr; 8993a40ed3dSBarry Smith 9003a40ed3dSBarry Smith PetscFunctionBegin; 901a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 902a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 903a8c6a408SBarry Smith } 9049b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 905d64ed03dSBarry Smith PetscStackPush("SNES user minimzation function"); 9069b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr); 907d64ed03dSBarry Smith PetscStackPop; 908ae3c334cSLois Curfman McInnes snes->nfuncs++; 9099b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 9103a40ed3dSBarry Smith PetscFunctionReturn(0); 9119b94acceSBarry Smith } 9129b94acceSBarry Smith 9135615d1e5SSatish Balay #undef __FUNC__ 914d4bb536fSBarry Smith #define __FUNC__ "SNESSetGradient" 9159b94acceSBarry Smith /*@C 9169b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 9179b94acceSBarry Smith vector for use by the SNES routines. 9189b94acceSBarry Smith 919c7afd0dbSLois Curfman McInnes Collective on SNES 920c7afd0dbSLois Curfman McInnes 9219b94acceSBarry Smith Input Parameters: 922c7afd0dbSLois Curfman McInnes + snes - the SNES context 9239b94acceSBarry Smith . func - function evaluation routine 924044dda88SLois Curfman McInnes . ctx - optional user-defined context for private data for the 925044dda88SLois Curfman McInnes gradient evaluation routine (may be PETSC_NULL) 926c7afd0dbSLois Curfman McInnes - r - vector to store gradient value 927fee21e36SBarry Smith 9289b94acceSBarry Smith Calling sequence of func: 9298d76a1e5SLois Curfman McInnes $ func (SNES, Vec x, Vec g, void *ctx); 9309b94acceSBarry Smith 931c7afd0dbSLois Curfman McInnes + x - input vector 9329b94acceSBarry Smith . g - gradient vector 933c7afd0dbSLois Curfman McInnes - ctx - optional user-defined gradient context 9349b94acceSBarry Smith 9359b94acceSBarry Smith Notes: 9369b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 9379b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 9389b94acceSBarry Smith SNESSetFunction(). 9399b94acceSBarry Smith 94036851e7fSLois Curfman McInnes Level: beginner 94136851e7fSLois Curfman McInnes 9429b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9439b94acceSBarry Smith 944a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 945a86d99e1SLois Curfman McInnes SNESSetMinimizationFunction(), 9469b94acceSBarry Smith @*/ 94774679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 9489b94acceSBarry Smith { 9493a40ed3dSBarry Smith PetscFunctionBegin; 95077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 951a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 952a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 953a8c6a408SBarry Smith } 9549b94acceSBarry Smith snes->computefunction = func; 9559b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 9569b94acceSBarry Smith snes->funP = ctx; 9573a40ed3dSBarry Smith PetscFunctionReturn(0); 9589b94acceSBarry Smith } 9599b94acceSBarry Smith 9605615d1e5SSatish Balay #undef __FUNC__ 9615615d1e5SSatish Balay #define __FUNC__ "SNESComputeGradient" 9629b94acceSBarry Smith /*@ 963a86d99e1SLois Curfman McInnes SNESComputeGradient - Computes the gradient that has been set with 964a86d99e1SLois Curfman McInnes SNESSetGradient(). 9659b94acceSBarry Smith 966c7afd0dbSLois Curfman McInnes Collective on SNES 967c7afd0dbSLois Curfman McInnes 9689b94acceSBarry Smith Input Parameters: 969c7afd0dbSLois Curfman McInnes + snes - the SNES context 970c7afd0dbSLois Curfman McInnes - x - input vector 9719b94acceSBarry Smith 9729b94acceSBarry Smith Output Parameter: 9739b94acceSBarry Smith . y - gradient vector 9749b94acceSBarry Smith 9759b94acceSBarry Smith Notes: 9769b94acceSBarry Smith SNESComputeGradient() is valid only for 9779b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 9789b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 979a86d99e1SLois Curfman McInnes 98036851e7fSLois Curfman McInnes SNESComputeGradient() is typically used within minimization 98136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 98236851e7fSLois Curfman McInnes themselves. 98336851e7fSLois Curfman McInnes 98436851e7fSLois Curfman McInnes Level: developer 98536851e7fSLois Curfman McInnes 986a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient 987a86d99e1SLois Curfman McInnes 988a86d99e1SLois Curfman McInnes .seealso: SNESSetGradient(), SNESGetGradient(), 989a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction(), SNESComputeHessian() 9909b94acceSBarry Smith @*/ 9919b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y) 9929b94acceSBarry Smith { 9939b94acceSBarry Smith int ierr; 9943a40ed3dSBarry Smith 9953a40ed3dSBarry Smith PetscFunctionBegin; 9963a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 997a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 9983a40ed3dSBarry Smith } 9999b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 1000d64ed03dSBarry Smith PetscStackPush("SNES user gradient function"); 10019b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1002d64ed03dSBarry Smith PetscStackPop; 10039b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 10043a40ed3dSBarry Smith PetscFunctionReturn(0); 10059b94acceSBarry Smith } 10069b94acceSBarry Smith 10075615d1e5SSatish Balay #undef __FUNC__ 10085615d1e5SSatish Balay #define __FUNC__ "SNESComputeJacobian" 100962fef451SLois Curfman McInnes /*@ 101062fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 101162fef451SLois Curfman McInnes set with SNESSetJacobian(). 101262fef451SLois Curfman McInnes 1013c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1014c7afd0dbSLois Curfman McInnes 101562fef451SLois Curfman McInnes Input Parameters: 1016c7afd0dbSLois Curfman McInnes + snes - the SNES context 1017c7afd0dbSLois Curfman McInnes - x - input vector 101862fef451SLois Curfman McInnes 101962fef451SLois Curfman McInnes Output Parameters: 1020c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 102162fef451SLois Curfman McInnes . B - optional preconditioning matrix 1022c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1023fee21e36SBarry Smith 102462fef451SLois Curfman McInnes Notes: 102562fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 102662fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 102762fef451SLois Curfman McInnes 1028dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1029dc5a77f8SLois Curfman McInnes flag parameter. 103062fef451SLois Curfman McInnes 103162fef451SLois Curfman McInnes SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 103262fef451SLois Curfman McInnes methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 1033005c665bSBarry Smith methods is SNESComputeHessian(). 103462fef451SLois Curfman McInnes 103536851e7fSLois Curfman McInnes SNESComputeJacobian() is typically used within nonlinear solver 103636851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 103736851e7fSLois Curfman McInnes themselves. 103836851e7fSLois Curfman McInnes 103936851e7fSLois Curfman McInnes Level: developer 104036851e7fSLois Curfman McInnes 104162fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 104262fef451SLois Curfman McInnes 104362fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 104462fef451SLois Curfman McInnes @*/ 10459b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10469b94acceSBarry Smith { 10479b94acceSBarry Smith int ierr; 10483a40ed3dSBarry Smith 10493a40ed3dSBarry Smith PetscFunctionBegin; 10503a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1051a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 10523a40ed3dSBarry Smith } 10533a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 10549b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 1055c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1056d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 10579b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1058d64ed03dSBarry Smith PetscStackPop; 10599b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 10606d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 106177c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 106277c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 10633a40ed3dSBarry Smith PetscFunctionReturn(0); 10649b94acceSBarry Smith } 10659b94acceSBarry Smith 10665615d1e5SSatish Balay #undef __FUNC__ 10675615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian" 106862fef451SLois Curfman McInnes /*@ 106962fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 107062fef451SLois Curfman McInnes set with SNESSetHessian(). 107162fef451SLois Curfman McInnes 1072c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1073c7afd0dbSLois Curfman McInnes 107462fef451SLois Curfman McInnes Input Parameters: 1075c7afd0dbSLois Curfman McInnes + snes - the SNES context 1076c7afd0dbSLois Curfman McInnes - x - input vector 107762fef451SLois Curfman McInnes 107862fef451SLois Curfman McInnes Output Parameters: 1079c7afd0dbSLois Curfman McInnes + A - Hessian matrix 108062fef451SLois Curfman McInnes . B - optional preconditioning matrix 1081c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1082fee21e36SBarry Smith 108362fef451SLois Curfman McInnes Notes: 108462fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 108562fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 108662fef451SLois Curfman McInnes 1087dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1088dc5a77f8SLois Curfman McInnes flag parameter. 108962fef451SLois Curfman McInnes 109062fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 109162fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 109262fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 109362fef451SLois Curfman McInnes 109436851e7fSLois Curfman McInnes SNESComputeHessian() is typically used within minimization 109536851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 109636851e7fSLois Curfman McInnes themselves. 109736851e7fSLois Curfman McInnes 109836851e7fSLois Curfman McInnes Level: developer 109936851e7fSLois Curfman McInnes 110062fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 110162fef451SLois Curfman McInnes 1102a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 1103a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 110462fef451SLois Curfman McInnes @*/ 110562fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 11069b94acceSBarry Smith { 11079b94acceSBarry Smith int ierr; 11083a40ed3dSBarry Smith 11093a40ed3dSBarry Smith PetscFunctionBegin; 11103a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1111a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 11123a40ed3dSBarry Smith } 11133a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 111462fef451SLois Curfman McInnes PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 11150f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 1116d64ed03dSBarry Smith PetscStackPush("SNES user Hessian function"); 111762fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr); 1118d64ed03dSBarry Smith PetscStackPop; 111962fef451SLois Curfman McInnes PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 11200f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 112177c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 112277c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 11233a40ed3dSBarry Smith PetscFunctionReturn(0); 11249b94acceSBarry Smith } 11259b94acceSBarry Smith 11265615d1e5SSatish Balay #undef __FUNC__ 1127d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian" 11289b94acceSBarry Smith /*@C 11299b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1130044dda88SLois Curfman McInnes location to store the matrix. 11319b94acceSBarry Smith 1132c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1133c7afd0dbSLois Curfman McInnes 11349b94acceSBarry Smith Input Parameters: 1135c7afd0dbSLois Curfman McInnes + snes - the SNES context 11369b94acceSBarry Smith . A - Jacobian matrix 11379b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11389b94acceSBarry Smith . func - Jacobian evaluation routine 1139c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11402cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11419b94acceSBarry Smith 11429b94acceSBarry Smith Calling sequence of func: 11438d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11449b94acceSBarry Smith 1145c7afd0dbSLois Curfman McInnes + x - input vector 11469b94acceSBarry Smith . A - Jacobian matrix 11479b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1148ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1149ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1150c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11519b94acceSBarry Smith 11529b94acceSBarry Smith Notes: 1153dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 11542cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1155ac21db08SLois Curfman McInnes 1156ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11579b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11589b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11599b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11609b94acceSBarry Smith throughout the global iterations. 11619b94acceSBarry Smith 116236851e7fSLois Curfman McInnes Level: beginner 116336851e7fSLois Curfman McInnes 11649b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11659b94acceSBarry Smith 1166ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 11679b94acceSBarry Smith @*/ 11689b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 11699b94acceSBarry Smith MatStructure*,void*),void *ctx) 11709b94acceSBarry Smith { 11713a40ed3dSBarry Smith PetscFunctionBegin; 117277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1173a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1174a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 1175a8c6a408SBarry Smith } 11769b94acceSBarry Smith snes->computejacobian = func; 11779b94acceSBarry Smith snes->jacP = ctx; 11789b94acceSBarry Smith snes->jacobian = A; 11799b94acceSBarry Smith snes->jacobian_pre = B; 11803a40ed3dSBarry Smith PetscFunctionReturn(0); 11819b94acceSBarry Smith } 118262fef451SLois Curfman McInnes 11835615d1e5SSatish Balay #undef __FUNC__ 1184d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian" 1185c2aafc4cSSatish Balay /*@C 1186b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1187b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1188b4fd4287SBarry Smith 1189c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1190c7afd0dbSLois Curfman McInnes 1191b4fd4287SBarry Smith Input Parameter: 1192b4fd4287SBarry Smith . snes - the nonlinear solver context 1193b4fd4287SBarry Smith 1194b4fd4287SBarry Smith Output Parameters: 1195c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1196b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 1197c7afd0dbSLois Curfman McInnes - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1198fee21e36SBarry Smith 119936851e7fSLois Curfman McInnes Level: advanced 120036851e7fSLois Curfman McInnes 1201b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1202b4fd4287SBarry Smith @*/ 1203b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx) 1204b4fd4287SBarry Smith { 12053a40ed3dSBarry Smith PetscFunctionBegin; 12063a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1207a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 12083a40ed3dSBarry Smith } 1209b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1210b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1211b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 12123a40ed3dSBarry Smith PetscFunctionReturn(0); 1213b4fd4287SBarry Smith } 1214b4fd4287SBarry Smith 12155615d1e5SSatish Balay #undef __FUNC__ 1216d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian" 12179b94acceSBarry Smith /*@C 12189b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 1219044dda88SLois Curfman McInnes location to store the matrix. 12209b94acceSBarry Smith 1221c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1222c7afd0dbSLois Curfman McInnes 12239b94acceSBarry Smith Input Parameters: 1224c7afd0dbSLois Curfman McInnes + snes - the SNES context 12259b94acceSBarry Smith . A - Hessian matrix 12269b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 12279b94acceSBarry Smith . func - Jacobian evaluation routine 1228c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1229313e4042SLois Curfman McInnes Hessian evaluation routine (may be PETSC_NULL) 12309b94acceSBarry Smith 12319b94acceSBarry Smith Calling sequence of func: 12328d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 12339b94acceSBarry Smith 1234c7afd0dbSLois Curfman McInnes + x - input vector 12359b94acceSBarry Smith . A - Hessian matrix 12369b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1237ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1238ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1239c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Hessian context 12409b94acceSBarry Smith 12419b94acceSBarry Smith Notes: 1242dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 12432cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1244ac21db08SLois Curfman McInnes 12459b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 12469b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 12479b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 12489b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 12499b94acceSBarry Smith throughout the global iterations. 12509b94acceSBarry Smith 125136851e7fSLois Curfman McInnes Level: beginner 125236851e7fSLois Curfman McInnes 12539b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 12549b94acceSBarry Smith 1255ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 12569b94acceSBarry Smith @*/ 12579b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 12589b94acceSBarry Smith MatStructure*,void*),void *ctx) 12599b94acceSBarry Smith { 12603a40ed3dSBarry Smith PetscFunctionBegin; 126177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1262d4bb536fSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1263a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1264d4bb536fSBarry Smith } 12659b94acceSBarry Smith snes->computejacobian = func; 12669b94acceSBarry Smith snes->jacP = ctx; 12679b94acceSBarry Smith snes->jacobian = A; 12689b94acceSBarry Smith snes->jacobian_pre = B; 12693a40ed3dSBarry Smith PetscFunctionReturn(0); 12709b94acceSBarry Smith } 12719b94acceSBarry Smith 12725615d1e5SSatish Balay #undef __FUNC__ 1273d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian" 127462fef451SLois Curfman McInnes /*@ 127562fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 127662fef451SLois Curfman McInnes provided context for evaluating the Hessian. 127762fef451SLois Curfman McInnes 1278c7afd0dbSLois Curfman McInnes Not Collective, but Mat object is parallel if SNES object is parallel 1279c7afd0dbSLois Curfman McInnes 128062fef451SLois Curfman McInnes Input Parameter: 128162fef451SLois Curfman McInnes . snes - the nonlinear solver context 128262fef451SLois Curfman McInnes 128362fef451SLois Curfman McInnes Output Parameters: 1284c7afd0dbSLois Curfman McInnes + A - location to stash Hessian matrix (or PETSC_NULL) 128562fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 1286c7afd0dbSLois Curfman McInnes - ctx - location to stash Hessian ctx (or PETSC_NULL) 1287fee21e36SBarry Smith 128836851e7fSLois Curfman McInnes Level: advanced 128936851e7fSLois Curfman McInnes 129062fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 1291c7afd0dbSLois Curfman McInnes 1292c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian 129362fef451SLois Curfman McInnes @*/ 129462fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx) 129562fef451SLois Curfman McInnes { 12963a40ed3dSBarry Smith PetscFunctionBegin; 12973a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){ 1298a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 12993a40ed3dSBarry Smith } 130062fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 130162fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 130262fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 13033a40ed3dSBarry Smith PetscFunctionReturn(0); 130462fef451SLois Curfman McInnes } 130562fef451SLois Curfman McInnes 13069b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 13079b94acceSBarry Smith 13085615d1e5SSatish Balay #undef __FUNC__ 13095615d1e5SSatish Balay #define __FUNC__ "SNESSetUp" 13109b94acceSBarry Smith /*@ 13119b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1312272ac6f2SLois Curfman McInnes of a nonlinear solver. 13139b94acceSBarry Smith 1314fee21e36SBarry Smith Collective on SNES 1315fee21e36SBarry Smith 1316c7afd0dbSLois Curfman McInnes Input Parameters: 1317c7afd0dbSLois Curfman McInnes + snes - the SNES context 1318c7afd0dbSLois Curfman McInnes - x - the solution vector 1319c7afd0dbSLois Curfman McInnes 1320272ac6f2SLois Curfman McInnes Notes: 1321272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1322272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1323272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1324272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1325272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1326272ac6f2SLois Curfman McInnes 132736851e7fSLois Curfman McInnes Level: advanced 132836851e7fSLois Curfman McInnes 13299b94acceSBarry Smith .keywords: SNES, nonlinear, setup 13309b94acceSBarry Smith 13319b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 13329b94acceSBarry Smith @*/ 13338ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 13349b94acceSBarry Smith { 1335272ac6f2SLois Curfman McInnes int ierr, flg; 13363a40ed3dSBarry Smith 13373a40ed3dSBarry Smith PetscFunctionBegin; 133877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 133977c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 13408ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 13419b94acceSBarry Smith 1342c1f60f51SBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);CHKERRQ(ierr); 1343c1f60f51SBarry Smith /* 1344c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1345dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1346c1f60f51SBarry Smith */ 1347c1f60f51SBarry Smith if (flg) { 1348c1f60f51SBarry Smith Mat J; 13495a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1350c1f60f51SBarry Smith PLogObjectParent(snes,J); 1351c1f60f51SBarry Smith snes->mfshell = J; 1352c1f60f51SBarry Smith snes->jacobian = J; 1353c2aafc4cSSatish Balay if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1354c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n"); 1355d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1356c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n"); 1357d4bb536fSBarry Smith } else { 1358a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option"); 1359d4bb536fSBarry Smith } 13605a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1361c1f60f51SBarry Smith } 1362272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);CHKERRQ(ierr); 1363c1f60f51SBarry Smith /* 1364dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1365c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1366c1f60f51SBarry Smith */ 136731615d04SLois Curfman McInnes if (flg) { 1368272ac6f2SLois Curfman McInnes Mat J; 13695a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1370272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 1371272ac6f2SLois Curfman McInnes snes->mfshell = J; 137283e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 137383e56afdSLois Curfman McInnes ierr = SNESSetJacobian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 137494a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 1375d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 137683e56afdSLois Curfman McInnes ierr = SNESSetHessian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 137794a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 1378d4bb536fSBarry Smith } else { 1379a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option"); 1380d4bb536fSBarry Smith } 13815a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1382272ac6f2SLois Curfman McInnes } 13839b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 1384a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 1385a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 13865a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 1387a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 1388a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector"); 1389a8c6a408SBarry Smith } 1390a703fe33SLois Curfman McInnes 1391a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 139282bf6240SBarry Smith if (snes->ksp_ewconv && PetscStrcmp(snes->type_name,SNES_EQ_TR)) { 1393a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 1394a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 1395a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 13965a655dc6SBarry Smith ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr); 1397a703fe33SLois Curfman McInnes } 13989b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 1399a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1400a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1401a8c6a408SBarry Smith if (!snes->computeumfunction) { 1402a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first"); 1403a8c6a408SBarry Smith } 14045a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()"); 1405d4bb536fSBarry Smith } else { 1406a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class"); 1407d4bb536fSBarry Smith } 1408a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);} 140982bf6240SBarry Smith snes->setupcalled = 1; 14103a40ed3dSBarry Smith PetscFunctionReturn(0); 14119b94acceSBarry Smith } 14129b94acceSBarry Smith 14135615d1e5SSatish Balay #undef __FUNC__ 1414d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy" 14159b94acceSBarry Smith /*@C 14169b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 14179b94acceSBarry Smith with SNESCreate(). 14189b94acceSBarry Smith 1419c7afd0dbSLois Curfman McInnes Collective on SNES 1420c7afd0dbSLois Curfman McInnes 14219b94acceSBarry Smith Input Parameter: 14229b94acceSBarry Smith . snes - the SNES context 14239b94acceSBarry Smith 142436851e7fSLois Curfman McInnes Level: beginner 142536851e7fSLois Curfman McInnes 14269b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 14279b94acceSBarry Smith 142863a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 14299b94acceSBarry Smith @*/ 14309b94acceSBarry Smith int SNESDestroy(SNES snes) 14319b94acceSBarry Smith { 1432b8a78c4aSBarry Smith int i,ierr; 14333a40ed3dSBarry Smith 14343a40ed3dSBarry Smith PetscFunctionBegin; 143577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14363a40ed3dSBarry Smith if (--snes->refct > 0) PetscFunctionReturn(0); 1437d4bb536fSBarry Smith 1438e1311b90SBarry Smith if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);} 1439*606d414cSSatish Balay if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);} 1440522c5e43SBarry Smith if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);} 14419b94acceSBarry Smith ierr = SLESDestroy(snes->sles);CHKERRQ(ierr); 1442522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1443b8a78c4aSBarry Smith for (i=0; i<snes->numbermonitors; i++ ) { 1444b8a78c4aSBarry Smith if (snes->monitordestroy[i]) { 1445b8a78c4aSBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1446b8a78c4aSBarry Smith } 1447b8a78c4aSBarry Smith } 14489b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 14490452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 14503a40ed3dSBarry Smith PetscFunctionReturn(0); 14519b94acceSBarry Smith } 14529b94acceSBarry Smith 14539b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 14549b94acceSBarry Smith 14555615d1e5SSatish Balay #undef __FUNC__ 14565615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances" 14579b94acceSBarry Smith /*@ 1458d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 14599b94acceSBarry Smith 1460c7afd0dbSLois Curfman McInnes Collective on SNES 1461c7afd0dbSLois Curfman McInnes 14629b94acceSBarry Smith Input Parameters: 1463c7afd0dbSLois Curfman McInnes + snes - the SNES context 146433174efeSLois Curfman McInnes . atol - absolute convergence tolerance 146533174efeSLois Curfman McInnes . rtol - relative convergence tolerance 146633174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 146733174efeSLois Curfman McInnes of the change in the solution between steps 146833174efeSLois Curfman McInnes . maxit - maximum number of iterations 1469c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1470fee21e36SBarry Smith 147133174efeSLois Curfman McInnes Options Database Keys: 1472c7afd0dbSLois Curfman McInnes + -snes_atol <atol> - Sets atol 1473c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1474c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1475c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1476c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 14779b94acceSBarry Smith 1478d7a720efSLois Curfman McInnes Notes: 14799b94acceSBarry Smith The default maximum number of iterations is 50. 14809b94acceSBarry Smith The default maximum number of function evaluations is 1000. 14819b94acceSBarry Smith 148236851e7fSLois Curfman McInnes Level: intermediate 148336851e7fSLois Curfman McInnes 148433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 14859b94acceSBarry Smith 1486d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 14879b94acceSBarry Smith @*/ 1488d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf) 14899b94acceSBarry Smith { 14903a40ed3dSBarry Smith PetscFunctionBegin; 149177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1492d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1493d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1494d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1495d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1496d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 14973a40ed3dSBarry Smith PetscFunctionReturn(0); 14989b94acceSBarry Smith } 14999b94acceSBarry Smith 15005615d1e5SSatish Balay #undef __FUNC__ 15015615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances" 15029b94acceSBarry Smith /*@ 150333174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 150433174efeSLois Curfman McInnes 1505c7afd0dbSLois Curfman McInnes Not Collective 1506c7afd0dbSLois Curfman McInnes 150733174efeSLois Curfman McInnes Input Parameters: 1508c7afd0dbSLois Curfman McInnes + snes - the SNES context 150933174efeSLois Curfman McInnes . atol - absolute convergence tolerance 151033174efeSLois Curfman McInnes . rtol - relative convergence tolerance 151133174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 151233174efeSLois Curfman McInnes of the change in the solution between steps 151333174efeSLois Curfman McInnes . maxit - maximum number of iterations 1514c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1515fee21e36SBarry Smith 151633174efeSLois Curfman McInnes Notes: 151733174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 151833174efeSLois Curfman McInnes 151936851e7fSLois Curfman McInnes Level: intermediate 152036851e7fSLois Curfman McInnes 152133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 152233174efeSLois Curfman McInnes 152333174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 152433174efeSLois Curfman McInnes @*/ 152533174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf) 152633174efeSLois Curfman McInnes { 15273a40ed3dSBarry Smith PetscFunctionBegin; 152833174efeSLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 152933174efeSLois Curfman McInnes if (atol) *atol = snes->atol; 153033174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 153133174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 153233174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 153333174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 15343a40ed3dSBarry Smith PetscFunctionReturn(0); 153533174efeSLois Curfman McInnes } 153633174efeSLois Curfman McInnes 15375615d1e5SSatish Balay #undef __FUNC__ 15385615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance" 153933174efeSLois Curfman McInnes /*@ 15409b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 15419b94acceSBarry Smith 1542fee21e36SBarry Smith Collective on SNES 1543fee21e36SBarry Smith 1544c7afd0dbSLois Curfman McInnes Input Parameters: 1545c7afd0dbSLois Curfman McInnes + snes - the SNES context 1546c7afd0dbSLois Curfman McInnes - tol - tolerance 1547c7afd0dbSLois Curfman McInnes 15489b94acceSBarry Smith Options Database Key: 1549c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 15509b94acceSBarry Smith 155136851e7fSLois Curfman McInnes Level: intermediate 155236851e7fSLois Curfman McInnes 15539b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 15549b94acceSBarry Smith 1555d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 15569b94acceSBarry Smith @*/ 15579b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol) 15589b94acceSBarry Smith { 15593a40ed3dSBarry Smith PetscFunctionBegin; 156077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15619b94acceSBarry Smith snes->deltatol = tol; 15623a40ed3dSBarry Smith PetscFunctionReturn(0); 15639b94acceSBarry Smith } 15649b94acceSBarry Smith 15655615d1e5SSatish Balay #undef __FUNC__ 15665615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance" 15679b94acceSBarry Smith /*@ 156877c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 15699b94acceSBarry Smith for unconstrained minimization solvers. 15709b94acceSBarry Smith 1571fee21e36SBarry Smith Collective on SNES 1572fee21e36SBarry Smith 1573c7afd0dbSLois Curfman McInnes Input Parameters: 1574c7afd0dbSLois Curfman McInnes + snes - the SNES context 1575c7afd0dbSLois Curfman McInnes - ftol - minimum function tolerance 1576c7afd0dbSLois Curfman McInnes 15779b94acceSBarry Smith Options Database Key: 1578c7afd0dbSLois Curfman McInnes . -snes_fmin <ftol> - Sets ftol 15799b94acceSBarry Smith 15809b94acceSBarry Smith Note: 158177c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 15829b94acceSBarry Smith methods only. 15839b94acceSBarry Smith 158436851e7fSLois Curfman McInnes Level: intermediate 158536851e7fSLois Curfman McInnes 15869b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 15879b94acceSBarry Smith 1588d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 15899b94acceSBarry Smith @*/ 159077c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol) 15919b94acceSBarry Smith { 15923a40ed3dSBarry Smith PetscFunctionBegin; 159377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15949b94acceSBarry Smith snes->fmin = ftol; 15953a40ed3dSBarry Smith PetscFunctionReturn(0); 15969b94acceSBarry Smith } 1597df9fa365SBarry Smith /* 1598df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1599df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1600df9fa365SBarry Smith macros instead of functions 1601df9fa365SBarry Smith */ 1602ce1608b8SBarry Smith #undef __FUNC__ 1603ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor" 1604ce1608b8SBarry Smith int SNESLGMonitor(SNES snes,int it,double norm,void *ctx) 1605ce1608b8SBarry Smith { 1606ce1608b8SBarry Smith int ierr; 1607ce1608b8SBarry Smith 1608ce1608b8SBarry Smith PetscFunctionBegin; 1609ce1608b8SBarry Smith ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1610ce1608b8SBarry Smith PetscFunctionReturn(0); 1611ce1608b8SBarry Smith } 1612ce1608b8SBarry Smith 1613df9fa365SBarry Smith #undef __FUNC__ 1614df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorCreate" 1615df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n, DrawLG *draw) 1616df9fa365SBarry Smith { 1617df9fa365SBarry Smith int ierr; 1618df9fa365SBarry Smith 1619df9fa365SBarry Smith PetscFunctionBegin; 1620df9fa365SBarry Smith ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1621df9fa365SBarry Smith PetscFunctionReturn(0); 1622df9fa365SBarry Smith } 1623df9fa365SBarry Smith 1624df9fa365SBarry Smith #undef __FUNC__ 1625df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorDestroy" 1626df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw) 1627df9fa365SBarry Smith { 1628df9fa365SBarry Smith int ierr; 1629df9fa365SBarry Smith 1630df9fa365SBarry Smith PetscFunctionBegin; 1631df9fa365SBarry Smith ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr); 1632df9fa365SBarry Smith PetscFunctionReturn(0); 1633df9fa365SBarry Smith } 1634df9fa365SBarry Smith 16359b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 16369b94acceSBarry Smith 16375615d1e5SSatish Balay #undef __FUNC__ 1638d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor" 16399b94acceSBarry Smith /*@C 16405cd90555SBarry Smith SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every 16419b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 16429b94acceSBarry Smith progress. 16439b94acceSBarry Smith 1644fee21e36SBarry Smith Collective on SNES 1645fee21e36SBarry Smith 1646c7afd0dbSLois Curfman McInnes Input Parameters: 1647c7afd0dbSLois Curfman McInnes + snes - the SNES context 1648c7afd0dbSLois Curfman McInnes . func - monitoring routine 1649b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1650c7afd0dbSLois Curfman McInnes monitor routine (may be PETSC_NULL) 1651b8a78c4aSBarry Smith - monitordestroy - options routine that frees monitor context 16529b94acceSBarry Smith 1653c7afd0dbSLois Curfman McInnes Calling sequence of func: 165440a0c1c6SLois Curfman McInnes $ int func(SNES snes,int its, double norm,void *mctx) 1655c7afd0dbSLois Curfman McInnes 1656c7afd0dbSLois Curfman McInnes + snes - the SNES context 1657c7afd0dbSLois Curfman McInnes . its - iteration number 1658c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 1659c7afd0dbSLois Curfman McInnes for SNES_NONLINEAR_EQUATIONS methods 166040a0c1c6SLois Curfman McInnes . norm - 2-norm gradient value (may be estimated) 1661c7afd0dbSLois Curfman McInnes for SNES_UNCONSTRAINED_MINIMIZATION methods 166240a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 16639b94acceSBarry Smith 16649665c990SLois Curfman McInnes Options Database Keys: 1665c7afd0dbSLois Curfman McInnes + -snes_monitor - sets SNESDefaultMonitor() 1666c7afd0dbSLois Curfman McInnes . -snes_xmonitor - sets line graph monitor, 1667c7afd0dbSLois Curfman McInnes uses SNESLGMonitorCreate() 1668c7afd0dbSLois Curfman McInnes _ -snes_cancelmonitors - cancels all monitors that have 1669c7afd0dbSLois Curfman McInnes been hardwired into a code by 1670c7afd0dbSLois Curfman McInnes calls to SNESSetMonitor(), but 1671c7afd0dbSLois Curfman McInnes does not cancel those set via 1672c7afd0dbSLois Curfman McInnes the options database. 16739665c990SLois Curfman McInnes 1674639f9d9dSBarry Smith Notes: 16756bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 16766bc08f3fSLois Curfman McInnes SNESSetMonitor() multiple times; all will be called in the 16776bc08f3fSLois Curfman McInnes order in which they were set. 1678639f9d9dSBarry Smith 167936851e7fSLois Curfman McInnes Level: intermediate 168036851e7fSLois Curfman McInnes 16819b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 16829b94acceSBarry Smith 16835cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor() 16849b94acceSBarry Smith @*/ 1685b8a78c4aSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx,int (*monitordestroy)(void *)) 16869b94acceSBarry Smith { 16873a40ed3dSBarry Smith PetscFunctionBegin; 1688639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 1689a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set"); 1690639f9d9dSBarry Smith } 1691639f9d9dSBarry Smith 1692639f9d9dSBarry Smith snes->monitor[snes->numbermonitors] = func; 1693b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1694639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 16953a40ed3dSBarry Smith PetscFunctionReturn(0); 16969b94acceSBarry Smith } 16979b94acceSBarry Smith 16985615d1e5SSatish Balay #undef __FUNC__ 16995cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor" 17005cd90555SBarry Smith /*@C 17015cd90555SBarry Smith SNESClearMonitor - Clears all the monitor functions for a SNES object. 17025cd90555SBarry Smith 1703c7afd0dbSLois Curfman McInnes Collective on SNES 1704c7afd0dbSLois Curfman McInnes 17055cd90555SBarry Smith Input Parameters: 17065cd90555SBarry Smith . snes - the SNES context 17075cd90555SBarry Smith 17085cd90555SBarry Smith Options Database: 1709c7afd0dbSLois Curfman McInnes . -snes_cancelmonitors - cancels all monitors that have been hardwired 1710c7afd0dbSLois Curfman McInnes into a code by calls to SNESSetMonitor(), but does not cancel those 1711c7afd0dbSLois Curfman McInnes set via the options database 17125cd90555SBarry Smith 17135cd90555SBarry Smith Notes: 17145cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 17155cd90555SBarry Smith 171636851e7fSLois Curfman McInnes Level: intermediate 171736851e7fSLois Curfman McInnes 17185cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 17195cd90555SBarry Smith 17205cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor() 17215cd90555SBarry Smith @*/ 17225cd90555SBarry Smith int SNESClearMonitor( SNES snes ) 17235cd90555SBarry Smith { 17245cd90555SBarry Smith PetscFunctionBegin; 17255cd90555SBarry Smith snes->numbermonitors = 0; 17265cd90555SBarry Smith PetscFunctionReturn(0); 17275cd90555SBarry Smith } 17285cd90555SBarry Smith 17295cd90555SBarry Smith #undef __FUNC__ 1730d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest" 17319b94acceSBarry Smith /*@C 17329b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 17339b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 17349b94acceSBarry Smith 1735fee21e36SBarry Smith Collective on SNES 1736fee21e36SBarry Smith 1737c7afd0dbSLois Curfman McInnes Input Parameters: 1738c7afd0dbSLois Curfman McInnes + snes - the SNES context 1739c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1740c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1741c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 17429b94acceSBarry Smith 1743c7afd0dbSLois Curfman McInnes Calling sequence of func: 17448d76a1e5SLois Curfman McInnes $ int func (SNES snes,double xnorm,double gnorm,double f,void *cctx) 1745c7afd0dbSLois Curfman McInnes 1746c7afd0dbSLois Curfman McInnes + snes - the SNES context 1747c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1748c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 1749c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods) 1750c7afd0dbSLois Curfman McInnes . f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods) 1751c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods) 1752c7afd0dbSLois Curfman McInnes - f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods) 17539b94acceSBarry Smith 175436851e7fSLois Curfman McInnes Level: advanced 175536851e7fSLois Curfman McInnes 17569b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 17579b94acceSBarry Smith 175840191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 175940191667SLois Curfman McInnes SNESConverged_UM_LS(), SNESConverged_UM_TR() 17609b94acceSBarry Smith @*/ 176174679c65SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,void*),void *cctx) 17629b94acceSBarry Smith { 17633a40ed3dSBarry Smith PetscFunctionBegin; 17649b94acceSBarry Smith (snes)->converged = func; 17659b94acceSBarry Smith (snes)->cnvP = cctx; 17663a40ed3dSBarry Smith PetscFunctionReturn(0); 17679b94acceSBarry Smith } 17689b94acceSBarry Smith 17695615d1e5SSatish Balay #undef __FUNC__ 1770d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory" 1771c9005455SLois Curfman McInnes /*@ 1772c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1773c9005455SLois Curfman McInnes 1774fee21e36SBarry Smith Collective on SNES 1775fee21e36SBarry Smith 1776c7afd0dbSLois Curfman McInnes Input Parameters: 1777c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1778c7afd0dbSLois Curfman McInnes . a - array to hold history 1779758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1780758f92a0SBarry Smith negative if not converged) for each solve. 1781758f92a0SBarry Smith . na - size of a and its 1782758f92a0SBarry Smith - reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero, 1783758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1784c7afd0dbSLois Curfman McInnes 1785c9005455SLois Curfman McInnes Notes: 1786c9005455SLois Curfman McInnes If set, this array will contain the function norms (for 1787c9005455SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods) or gradient norms 1788c9005455SLois Curfman McInnes (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed 1789c9005455SLois Curfman McInnes at each step. 1790c9005455SLois Curfman McInnes 1791c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1792c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1793c9005455SLois Curfman McInnes during the section of code that is being timed. 1794c9005455SLois Curfman McInnes 179536851e7fSLois Curfman McInnes Level: intermediate 179636851e7fSLois Curfman McInnes 1797c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1798758f92a0SBarry Smith 179908405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1800758f92a0SBarry Smith 1801c9005455SLois Curfman McInnes @*/ 1802758f92a0SBarry Smith int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset) 1803c9005455SLois Curfman McInnes { 18043a40ed3dSBarry Smith PetscFunctionBegin; 1805c9005455SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 1806c9005455SLois Curfman McInnes if (na) PetscValidScalarPointer(a); 1807c9005455SLois Curfman McInnes snes->conv_hist = a; 1808758f92a0SBarry Smith snes->conv_hist_its = its; 1809758f92a0SBarry Smith snes->conv_hist_max = na; 1810758f92a0SBarry Smith snes->conv_hist_reset = reset; 1811758f92a0SBarry Smith PetscFunctionReturn(0); 1812758f92a0SBarry Smith } 1813758f92a0SBarry Smith 1814758f92a0SBarry Smith #undef __FUNC__ 1815758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory" 18160c4c9dddSBarry Smith /*@C 1817758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1818758f92a0SBarry Smith 1819758f92a0SBarry Smith Collective on SNES 1820758f92a0SBarry Smith 1821758f92a0SBarry Smith Input Parameter: 1822758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1823758f92a0SBarry Smith 1824758f92a0SBarry Smith Output Parameters: 1825758f92a0SBarry Smith . a - array to hold history 1826758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1827758f92a0SBarry Smith negative if not converged) for each solve. 1828758f92a0SBarry Smith - na - size of a and its 1829758f92a0SBarry Smith 1830758f92a0SBarry Smith Notes: 1831758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1832758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1833758f92a0SBarry Smith 1834758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1835758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1836758f92a0SBarry Smith during the section of code that is being timed. 1837758f92a0SBarry Smith 1838758f92a0SBarry Smith Level: intermediate 1839758f92a0SBarry Smith 1840758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1841758f92a0SBarry Smith 1842758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1843758f92a0SBarry Smith 1844758f92a0SBarry Smith @*/ 1845758f92a0SBarry Smith int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na) 1846758f92a0SBarry Smith { 1847758f92a0SBarry Smith PetscFunctionBegin; 1848758f92a0SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1849758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1850758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1851758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 18523a40ed3dSBarry Smith PetscFunctionReturn(0); 1853c9005455SLois Curfman McInnes } 1854c9005455SLois Curfman McInnes 1855c9005455SLois Curfman McInnes #undef __FUNC__ 18565615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private" 18579b94acceSBarry Smith /* 18589b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 18599b94acceSBarry Smith positive parameter delta. 18609b94acceSBarry Smith 18619b94acceSBarry Smith Input Parameters: 1862c7afd0dbSLois Curfman McInnes + snes - the SNES context 18639b94acceSBarry Smith . y - approximate solution of linear system 18649b94acceSBarry Smith . fnorm - 2-norm of current function 1865c7afd0dbSLois Curfman McInnes - delta - trust region size 18669b94acceSBarry Smith 18679b94acceSBarry Smith Output Parameters: 1868c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 18699b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 18709b94acceSBarry Smith region, and exceeds zero otherwise. 1871c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 18729b94acceSBarry Smith 18739b94acceSBarry Smith Note: 187440191667SLois Curfman McInnes For non-trust region methods such as SNES_EQ_LS, the parameter delta 18759b94acceSBarry Smith is set to be the maximum allowable step size. 18769b94acceSBarry Smith 18779b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 18789b94acceSBarry Smith */ 18799b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 18809b94acceSBarry Smith double *gpnorm,double *ynorm) 18819b94acceSBarry Smith { 18829b94acceSBarry Smith double norm; 18839b94acceSBarry Smith Scalar cnorm; 18843a40ed3dSBarry Smith int ierr; 18853a40ed3dSBarry Smith 18863a40ed3dSBarry Smith PetscFunctionBegin; 18873a40ed3dSBarry Smith ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr); 18889b94acceSBarry Smith if (norm > *delta) { 18899b94acceSBarry Smith norm = *delta/norm; 18909b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 18919b94acceSBarry Smith cnorm = norm; 18929b94acceSBarry Smith VecScale( &cnorm, y ); 18939b94acceSBarry Smith *ynorm = *delta; 18949b94acceSBarry Smith } else { 18959b94acceSBarry Smith *gpnorm = 0.0; 18969b94acceSBarry Smith *ynorm = norm; 18979b94acceSBarry Smith } 18983a40ed3dSBarry Smith PetscFunctionReturn(0); 18999b94acceSBarry Smith } 19009b94acceSBarry Smith 19015615d1e5SSatish Balay #undef __FUNC__ 19025615d1e5SSatish Balay #define __FUNC__ "SNESSolve" 19039b94acceSBarry Smith /*@ 19049b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 190563a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 19069b94acceSBarry Smith 1907c7afd0dbSLois Curfman McInnes Collective on SNES 1908c7afd0dbSLois Curfman McInnes 1909b2002411SLois Curfman McInnes Input Parameters: 1910c7afd0dbSLois Curfman McInnes + snes - the SNES context 1911c7afd0dbSLois Curfman McInnes - x - the solution vector 19129b94acceSBarry Smith 19139b94acceSBarry Smith Output Parameter: 1914b2002411SLois Curfman McInnes . its - number of iterations until termination 19159b94acceSBarry Smith 1916b2002411SLois Curfman McInnes Notes: 19178ddd3da0SLois Curfman McInnes The user should initialize the vector, x, with the initial guess 19188ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 19198ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 19208ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 19218ddd3da0SLois Curfman McInnes 192236851e7fSLois Curfman McInnes Level: beginner 192336851e7fSLois Curfman McInnes 19249b94acceSBarry Smith .keywords: SNES, nonlinear, solve 19259b94acceSBarry Smith 192663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 19279b94acceSBarry Smith @*/ 19288ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 19299b94acceSBarry Smith { 19303c7409f5SSatish Balay int ierr, flg; 1931052efed2SBarry Smith 19323a40ed3dSBarry Smith PetscFunctionBegin; 193377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 193474679c65SBarry Smith PetscValidIntPointer(its); 193582bf6240SBarry Smith if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);} 1936c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 1937758f92a0SBarry Smith if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0; 19389b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 1939c96a6f78SLois Curfman McInnes snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0; 19409b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr); 19419b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 19423c7409f5SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg);CHKERRQ(ierr); 19436d4a8577SBarry Smith if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } 19443a40ed3dSBarry Smith PetscFunctionReturn(0); 19459b94acceSBarry Smith } 19469b94acceSBarry Smith 19479b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 19489b94acceSBarry Smith 19495615d1e5SSatish Balay #undef __FUNC__ 19505615d1e5SSatish Balay #define __FUNC__ "SNESSetType" 195182bf6240SBarry Smith /*@C 19524b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 19539b94acceSBarry Smith 1954fee21e36SBarry Smith Collective on SNES 1955fee21e36SBarry Smith 1956c7afd0dbSLois Curfman McInnes Input Parameters: 1957c7afd0dbSLois Curfman McInnes + snes - the SNES context 1958c7afd0dbSLois Curfman McInnes - method - a known method 1959c7afd0dbSLois Curfman McInnes 1960c7afd0dbSLois Curfman McInnes Options Database Key: 1961c7afd0dbSLois Curfman McInnes . -snes_type <method> - Sets the method; use -help for a list 1962c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 1963ae12b187SLois Curfman McInnes 19649b94acceSBarry Smith Notes: 19659b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 196636851e7fSLois Curfman McInnes + SNES_EQ_LS - Newton's method with line search 1967c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 1968c7afd0dbSLois Curfman McInnes . SNES_EQ_TR - Newton's method with trust region 1969c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 1970c7afd0dbSLois Curfman McInnes . SNES_UM_TR - Newton's method with trust region 1971c7afd0dbSLois Curfman McInnes (unconstrained minimization) 197236851e7fSLois Curfman McInnes - SNES_UM_LS - Newton's method with line search 1973c7afd0dbSLois Curfman McInnes (unconstrained minimization) 19749b94acceSBarry Smith 1975ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 1976ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 1977ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 1978ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 1979ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 1980ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 1981ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 1982ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 1983ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 198436851e7fSLois Curfman McInnes appropriate method. In other words, this routine is not for beginners. 198536851e7fSLois Curfman McInnes 198636851e7fSLois Curfman McInnes Level: intermediate 1987a703fe33SLois Curfman McInnes 1988f536c699SSatish Balay .keywords: SNES, set, method 19899b94acceSBarry Smith @*/ 19904b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method) 19919b94acceSBarry Smith { 199284cb2905SBarry Smith int ierr; 19939b94acceSBarry Smith int (*r)(SNES); 19943a40ed3dSBarry Smith 19953a40ed3dSBarry Smith PetscFunctionBegin; 199677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 199782bf6240SBarry Smith 19983f1db9ecSBarry Smith if (PetscTypeCompare(snes->type_name,method)) PetscFunctionReturn(0); 199982bf6240SBarry Smith 200082bf6240SBarry Smith if (snes->setupcalled) { 2001e1311b90SBarry Smith ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr); 200282bf6240SBarry Smith snes->data = 0; 200382bf6240SBarry Smith } 20047d1a2b2bSBarry Smith 20059b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 200682bf6240SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 200782bf6240SBarry Smith 2008488ecbafSBarry Smith ierr = FListFind(snes->comm, SNESList, method,(int (**)(void *)) &r );CHKERRQ(ierr); 200982bf6240SBarry Smith 2010596552b5SBarry Smith if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",method); 20111548b14aSBarry Smith 2012*606d414cSSatish Balay if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);} 201382bf6240SBarry Smith snes->data = 0; 20143a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 201582bf6240SBarry Smith 2016*606d414cSSatish Balay if (snes->type_name) {ierr = PetscFree(snes->type_name);CHKERRQ(ierr);} 201782bf6240SBarry Smith snes->type_name = (char *) PetscMalloc((PetscStrlen(method)+1)*sizeof(char));CHKPTRQ(snes->type_name); 2018549d3d68SSatish Balay ierr = PetscStrcpy(snes->type_name,method);CHKERRQ(ierr); 201982bf6240SBarry Smith snes->set_method_called = 1; 202082bf6240SBarry Smith 20213a40ed3dSBarry Smith PetscFunctionReturn(0); 20229b94acceSBarry Smith } 20239b94acceSBarry Smith 2024a847f771SSatish Balay 20259b94acceSBarry Smith /* --------------------------------------------------------------------- */ 20265615d1e5SSatish Balay #undef __FUNC__ 2027d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy" 20289b94acceSBarry Smith /*@C 20299b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 20309b94acceSBarry Smith registered by SNESRegister(). 20319b94acceSBarry Smith 2032fee21e36SBarry Smith Not Collective 2033fee21e36SBarry Smith 203436851e7fSLois Curfman McInnes Level: advanced 203536851e7fSLois Curfman McInnes 20369b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 20379b94acceSBarry Smith 20389b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 20399b94acceSBarry Smith @*/ 2040cf256101SBarry Smith int SNESRegisterDestroy(void) 20419b94acceSBarry Smith { 204282bf6240SBarry Smith int ierr; 204382bf6240SBarry Smith 20443a40ed3dSBarry Smith PetscFunctionBegin; 204582bf6240SBarry Smith if (SNESList) { 2046488ecbafSBarry Smith ierr = FListDestroy( SNESList );CHKERRQ(ierr); 204782bf6240SBarry Smith SNESList = 0; 20489b94acceSBarry Smith } 204984cb2905SBarry Smith SNESRegisterAllCalled = 0; 20503a40ed3dSBarry Smith PetscFunctionReturn(0); 20519b94acceSBarry Smith } 20529b94acceSBarry Smith 20535615d1e5SSatish Balay #undef __FUNC__ 2054d4bb536fSBarry Smith #define __FUNC__ "SNESGetType" 20559b94acceSBarry Smith /*@C 20569a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 20579b94acceSBarry Smith 2058c7afd0dbSLois Curfman McInnes Not Collective 2059c7afd0dbSLois Curfman McInnes 20609b94acceSBarry Smith Input Parameter: 20614b0e389bSBarry Smith . snes - nonlinear solver context 20629b94acceSBarry Smith 20639b94acceSBarry Smith Output Parameter: 206482bf6240SBarry Smith . method - SNES method (a charactor string) 20659b94acceSBarry Smith 206636851e7fSLois Curfman McInnes Level: intermediate 206736851e7fSLois Curfman McInnes 20689b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name 20699b94acceSBarry Smith @*/ 207082bf6240SBarry Smith int SNESGetType(SNES snes, SNESType *method) 20719b94acceSBarry Smith { 20723a40ed3dSBarry Smith PetscFunctionBegin; 207382bf6240SBarry Smith *method = snes->type_name; 20743a40ed3dSBarry Smith PetscFunctionReturn(0); 20759b94acceSBarry Smith } 20769b94acceSBarry Smith 20775615d1e5SSatish Balay #undef __FUNC__ 2078d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution" 20799b94acceSBarry Smith /*@C 20809b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 20819b94acceSBarry Smith stored. 20829b94acceSBarry Smith 2083c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2084c7afd0dbSLois Curfman McInnes 20859b94acceSBarry Smith Input Parameter: 20869b94acceSBarry Smith . snes - the SNES context 20879b94acceSBarry Smith 20889b94acceSBarry Smith Output Parameter: 20899b94acceSBarry Smith . x - the solution 20909b94acceSBarry Smith 209136851e7fSLois Curfman McInnes Level: advanced 209236851e7fSLois Curfman McInnes 20939b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 20949b94acceSBarry Smith 2095a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 20969b94acceSBarry Smith @*/ 20979b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 20989b94acceSBarry Smith { 20993a40ed3dSBarry Smith PetscFunctionBegin; 210077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 21019b94acceSBarry Smith *x = snes->vec_sol_always; 21023a40ed3dSBarry Smith PetscFunctionReturn(0); 21039b94acceSBarry Smith } 21049b94acceSBarry Smith 21055615d1e5SSatish Balay #undef __FUNC__ 2106d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate" 21079b94acceSBarry Smith /*@C 21089b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 21099b94acceSBarry Smith stored. 21109b94acceSBarry Smith 2111c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2112c7afd0dbSLois Curfman McInnes 21139b94acceSBarry Smith Input Parameter: 21149b94acceSBarry Smith . snes - the SNES context 21159b94acceSBarry Smith 21169b94acceSBarry Smith Output Parameter: 21179b94acceSBarry Smith . x - the solution update 21189b94acceSBarry Smith 211936851e7fSLois Curfman McInnes Level: advanced 212036851e7fSLois Curfman McInnes 21219b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 21229b94acceSBarry Smith 21239b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 21249b94acceSBarry Smith @*/ 21259b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 21269b94acceSBarry Smith { 21273a40ed3dSBarry Smith PetscFunctionBegin; 212877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 21299b94acceSBarry Smith *x = snes->vec_sol_update_always; 21303a40ed3dSBarry Smith PetscFunctionReturn(0); 21319b94acceSBarry Smith } 21329b94acceSBarry Smith 21335615d1e5SSatish Balay #undef __FUNC__ 2134d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction" 21359b94acceSBarry Smith /*@C 21363638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 21379b94acceSBarry Smith 2138c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2139c7afd0dbSLois Curfman McInnes 21409b94acceSBarry Smith Input Parameter: 21419b94acceSBarry Smith . snes - the SNES context 21429b94acceSBarry Smith 21439b94acceSBarry Smith Output Parameter: 21443638b69dSLois Curfman McInnes . r - the function 21459b94acceSBarry Smith 21469b94acceSBarry Smith Notes: 21479b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 21489b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 21499b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 21509b94acceSBarry Smith 215136851e7fSLois Curfman McInnes Level: advanced 215236851e7fSLois Curfman McInnes 2153a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 21549b94acceSBarry Smith 215531615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 215631615d04SLois Curfman McInnes SNESGetGradient() 21579b94acceSBarry Smith @*/ 21589b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r) 21599b94acceSBarry Smith { 21603a40ed3dSBarry Smith PetscFunctionBegin; 216177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2162a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 2163a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 2164a8c6a408SBarry Smith } 21659b94acceSBarry Smith *r = snes->vec_func_always; 21663a40ed3dSBarry Smith PetscFunctionReturn(0); 21679b94acceSBarry Smith } 21689b94acceSBarry Smith 21695615d1e5SSatish Balay #undef __FUNC__ 2170d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient" 21719b94acceSBarry Smith /*@C 21723638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 21739b94acceSBarry Smith 2174c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2175c7afd0dbSLois Curfman McInnes 21769b94acceSBarry Smith Input Parameter: 21779b94acceSBarry Smith . snes - the SNES context 21789b94acceSBarry Smith 21799b94acceSBarry Smith Output Parameter: 21809b94acceSBarry Smith . r - the gradient 21819b94acceSBarry Smith 21829b94acceSBarry Smith Notes: 21839b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 21849b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 21859b94acceSBarry Smith SNESGetFunction(). 21869b94acceSBarry Smith 218736851e7fSLois Curfman McInnes Level: advanced 218836851e7fSLois Curfman McInnes 21899b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 21909b94acceSBarry Smith 219131615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction() 21929b94acceSBarry Smith @*/ 21939b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r) 21949b94acceSBarry Smith { 21953a40ed3dSBarry Smith PetscFunctionBegin; 219677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 21973a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2198a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 21993a40ed3dSBarry Smith } 22009b94acceSBarry Smith *r = snes->vec_func_always; 22013a40ed3dSBarry Smith PetscFunctionReturn(0); 22029b94acceSBarry Smith } 22039b94acceSBarry Smith 22045615d1e5SSatish Balay #undef __FUNC__ 2205d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction" 22069b94acceSBarry Smith /*@ 22079b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 22089b94acceSBarry Smith unconstrained minimization problems. 22099b94acceSBarry Smith 2210c7afd0dbSLois Curfman McInnes Not Collective 2211c7afd0dbSLois Curfman McInnes 22129b94acceSBarry Smith Input Parameter: 22139b94acceSBarry Smith . snes - the SNES context 22149b94acceSBarry Smith 22159b94acceSBarry Smith Output Parameter: 22169b94acceSBarry Smith . r - the function 22179b94acceSBarry Smith 22189b94acceSBarry Smith Notes: 22199b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 22209b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 22219b94acceSBarry Smith SNESGetFunction(). 22229b94acceSBarry Smith 222336851e7fSLois Curfman McInnes Level: advanced 222436851e7fSLois Curfman McInnes 22259b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 22269b94acceSBarry Smith 222731615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction() 22289b94acceSBarry Smith @*/ 22299b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r) 22309b94acceSBarry Smith { 22313a40ed3dSBarry Smith PetscFunctionBegin; 223277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 223374679c65SBarry Smith PetscValidScalarPointer(r); 22343a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2235a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 22363a40ed3dSBarry Smith } 22379b94acceSBarry Smith *r = snes->fc; 22383a40ed3dSBarry Smith PetscFunctionReturn(0); 22399b94acceSBarry Smith } 22409b94acceSBarry Smith 22415615d1e5SSatish Balay #undef __FUNC__ 2242d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix" 22433c7409f5SSatish Balay /*@C 22443c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2245d850072dSLois Curfman McInnes SNES options in the database. 22463c7409f5SSatish Balay 2247fee21e36SBarry Smith Collective on SNES 2248fee21e36SBarry Smith 2249c7afd0dbSLois Curfman McInnes Input Parameter: 2250c7afd0dbSLois Curfman McInnes + snes - the SNES context 2251c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2252c7afd0dbSLois Curfman McInnes 2253d850072dSLois Curfman McInnes Notes: 2254a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2255c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2256d850072dSLois Curfman McInnes 225736851e7fSLois Curfman McInnes Level: advanced 225836851e7fSLois Curfman McInnes 22593c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2260a86d99e1SLois Curfman McInnes 2261a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 22623c7409f5SSatish Balay @*/ 22633c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 22643c7409f5SSatish Balay { 22653c7409f5SSatish Balay int ierr; 22663c7409f5SSatish Balay 22673a40ed3dSBarry Smith PetscFunctionBegin; 226877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2269639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 22703c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 22713a40ed3dSBarry Smith PetscFunctionReturn(0); 22723c7409f5SSatish Balay } 22733c7409f5SSatish Balay 22745615d1e5SSatish Balay #undef __FUNC__ 2275d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix" 22763c7409f5SSatish Balay /*@C 2277f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2278d850072dSLois Curfman McInnes SNES options in the database. 22793c7409f5SSatish Balay 2280fee21e36SBarry Smith Collective on SNES 2281fee21e36SBarry Smith 2282c7afd0dbSLois Curfman McInnes Input Parameters: 2283c7afd0dbSLois Curfman McInnes + snes - the SNES context 2284c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2285c7afd0dbSLois Curfman McInnes 2286d850072dSLois Curfman McInnes Notes: 2287a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2288c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2289d850072dSLois Curfman McInnes 229036851e7fSLois Curfman McInnes Level: advanced 229136851e7fSLois Curfman McInnes 22923c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2293a86d99e1SLois Curfman McInnes 2294a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 22953c7409f5SSatish Balay @*/ 22963c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 22973c7409f5SSatish Balay { 22983c7409f5SSatish Balay int ierr; 22993c7409f5SSatish Balay 23003a40ed3dSBarry Smith PetscFunctionBegin; 230177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2302639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 23033c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 23043a40ed3dSBarry Smith PetscFunctionReturn(0); 23053c7409f5SSatish Balay } 23063c7409f5SSatish Balay 23075615d1e5SSatish Balay #undef __FUNC__ 2308d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix" 23099ab63eb5SSatish Balay /*@C 23103c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 23113c7409f5SSatish Balay SNES options in the database. 23123c7409f5SSatish Balay 2313c7afd0dbSLois Curfman McInnes Not Collective 2314c7afd0dbSLois Curfman McInnes 23153c7409f5SSatish Balay Input Parameter: 23163c7409f5SSatish Balay . snes - the SNES context 23173c7409f5SSatish Balay 23183c7409f5SSatish Balay Output Parameter: 23193c7409f5SSatish Balay . prefix - pointer to the prefix string used 23203c7409f5SSatish Balay 23219ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 23229ab63eb5SSatish Balay sufficient length to hold the prefix. 23239ab63eb5SSatish Balay 232436851e7fSLois Curfman McInnes Level: advanced 232536851e7fSLois Curfman McInnes 23263c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2327a86d99e1SLois Curfman McInnes 2328a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 23293c7409f5SSatish Balay @*/ 23303c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 23313c7409f5SSatish Balay { 23323c7409f5SSatish Balay int ierr; 23333c7409f5SSatish Balay 23343a40ed3dSBarry Smith PetscFunctionBegin; 233577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2336639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 23373a40ed3dSBarry Smith PetscFunctionReturn(0); 23383c7409f5SSatish Balay } 23393c7409f5SSatish Balay 2340ca161407SBarry Smith #undef __FUNC__ 2341ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp" 2342ca161407SBarry Smith /*@ 2343ca161407SBarry Smith SNESPrintHelp - Prints all options for the SNES component. 2344ca161407SBarry Smith 2345c7afd0dbSLois Curfman McInnes Collective on SNES 2346c7afd0dbSLois Curfman McInnes 2347ca161407SBarry Smith Input Parameter: 2348ca161407SBarry Smith . snes - the SNES context 2349ca161407SBarry Smith 2350ca161407SBarry Smith Options Database Keys: 2351c7afd0dbSLois Curfman McInnes + -help - Prints SNES options 2352c7afd0dbSLois Curfman McInnes - -h - Prints SNES options 2353ca161407SBarry Smith 235436851e7fSLois Curfman McInnes Level: beginner 235536851e7fSLois Curfman McInnes 2356ca161407SBarry Smith .keywords: SNES, nonlinear, help 2357ca161407SBarry Smith 2358ca161407SBarry Smith .seealso: SNESSetFromOptions() 2359ca161407SBarry Smith @*/ 2360ca161407SBarry Smith int SNESPrintHelp(SNES snes) 2361ca161407SBarry Smith { 2362ca161407SBarry Smith char p[64]; 2363ca161407SBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2364ca161407SBarry Smith int ierr; 2365ca161407SBarry Smith 2366ca161407SBarry Smith PetscFunctionBegin; 2367ca161407SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2368ca161407SBarry Smith 2369549d3d68SSatish Balay ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 2370ca161407SBarry Smith if (snes->prefix) PetscStrcat(p, snes->prefix); 2371ca161407SBarry Smith 2372ca161407SBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2373ca161407SBarry Smith 2374ebb8b11fSBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2375d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr); 2376488ecbafSBarry Smith ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr); 2377d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr); 2378d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr); 2379d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr); 2380d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr); 2381d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr); 2382d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr); 2383d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr); 2384d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr); 2385d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr); 2386d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_monitor: use default SNES convergence monitor, prints\n\ 2387d132466eSBarry Smith residual norm at each iteration.\n",p);CHKERRQ(ierr); 2388d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_smonitor: same as the above, but prints fewer digits of the\n\ 2389ca161407SBarry Smith residual norm for small residual norms. This is useful to conceal\n\ 2390d132466eSBarry Smith meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr); 2391d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr); 2392d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr); 2393d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr); 2394ca161407SBarry Smith if (snes->type == SNES_NONLINEAR_EQUATIONS) { 2395d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2396d132466eSBarry Smith " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr); 2397d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr); 2398d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr); 2399d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr); 2400d132466eSBarry 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); 2401d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr); 2402d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr); 2403d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2404d132466eSBarry Smith " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr); 2405d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2406d132466eSBarry Smith " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr); 2407d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2408d132466eSBarry Smith " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr); 2409d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2410d132466eSBarry Smith " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr); 2411d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2412d132466eSBarry Smith " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr); 2413d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2414d132466eSBarry Smith " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr); 2415d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2416d132466eSBarry Smith " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr); 2417ca161407SBarry Smith } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 2418d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr); 2419d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr); 2420d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr); 2421d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr); 2422ca161407SBarry Smith } 2423d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <method> for help on ",p);CHKERRQ(ierr); 2424d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr); 2425ca161407SBarry Smith if (snes->printhelp) { 2426ca161407SBarry Smith ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr); 2427ca161407SBarry Smith } 2428ca161407SBarry Smith PetscFunctionReturn(0); 2429ca161407SBarry Smith } 24303c7409f5SSatish Balay 2431acb85ae6SSatish Balay /*MC 2432b2002411SLois Curfman McInnes SNESRegister - Adds a method to the nonlinear solver package. 24339b94acceSBarry Smith 2434b2002411SLois Curfman McInnes Synopsis: 2435b2002411SLois Curfman McInnes SNESRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES)) 24369b94acceSBarry Smith 24378d76a1e5SLois Curfman McInnes Not collective 24388d76a1e5SLois Curfman McInnes 2439b2002411SLois Curfman McInnes Input Parameters: 2440c7afd0dbSLois Curfman McInnes + name_solver - name of a new user-defined solver 2441b2002411SLois Curfman McInnes . path - path (either absolute or relative) the library containing this solver 2442b2002411SLois Curfman McInnes . name_create - name of routine to create method context 2443c7afd0dbSLois Curfman McInnes - routine_create - routine to create method context 24449b94acceSBarry Smith 2445b2002411SLois Curfman McInnes Notes: 2446b2002411SLois Curfman McInnes SNESRegister() may be called multiple times to add several user-defined solvers. 24473a40ed3dSBarry Smith 2448b2002411SLois Curfman McInnes If dynamic libraries are used, then the fourth input argument (routine_create) 2449b2002411SLois Curfman McInnes is ignored. 2450b2002411SLois Curfman McInnes 2451b2002411SLois Curfman McInnes Sample usage: 245269225d78SLois Curfman McInnes .vb 2453b2002411SLois Curfman McInnes SNESRegister("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a, 2454b2002411SLois Curfman McInnes "MySolverCreate",MySolverCreate); 245569225d78SLois Curfman McInnes .ve 2456b2002411SLois Curfman McInnes 2457b2002411SLois Curfman McInnes Then, your solver can be chosen with the procedural interface via 2458b2002411SLois Curfman McInnes $ SNESSetType(snes,"my_solver") 2459b2002411SLois Curfman McInnes or at runtime via the option 2460b2002411SLois Curfman McInnes $ -snes_type my_solver 2461b2002411SLois Curfman McInnes 246236851e7fSLois Curfman McInnes Level: advanced 246336851e7fSLois Curfman McInnes 246485614651SBarry Smith $PETSC_ARCH, $PETSC_DIR, $PETSC_LDIR, and $BOPT occuring in pathname will be replaced with appropriate values. 2465dd438238SBarry Smith 2466b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register 2467b2002411SLois Curfman McInnes 2468b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy() 2469b2002411SLois Curfman McInnes M*/ 2470b2002411SLois Curfman McInnes 2471b2002411SLois Curfman McInnes #undef __FUNC__ 2472b2002411SLois Curfman McInnes #define __FUNC__ "SNESRegister_Private" 2473b2002411SLois Curfman McInnes int SNESRegister_Private(char *sname,char *path,char *name,int (*function)(SNES)) 2474b2002411SLois Curfman McInnes { 2475b2002411SLois Curfman McInnes char fullname[256]; 2476b2002411SLois Curfman McInnes int ierr; 2477b2002411SLois Curfman McInnes 2478b2002411SLois Curfman McInnes PetscFunctionBegin; 2479549d3d68SSatish Balay ierr = PetscStrcpy(fullname,path);CHKERRQ(ierr); 2480549d3d68SSatish Balay PetscStrcat(fullname,":");PetscStrcat(fullname,name); 2481488ecbafSBarry Smith ierr = FListAdd_Private(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr); 2482b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2483b2002411SLois Curfman McInnes } 2484