1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*184914b5SBarry Smith static char vcid[] = "$Id: snes.c,v 1.195 1999/09/20 19:08:41 bsmith Exp bsmith $"; 39b94acceSBarry Smith #endif 49b94acceSBarry Smith 570f55243SBarry Smith #include "src/snes/snesimpl.h" /*I "snes.h" I*/ 69b94acceSBarry Smith 784cb2905SBarry Smith int SNESRegisterAllCalled = 0; 8488ecbafSBarry Smith FList SNESList = 0; 982bf6240SBarry Smith 105615d1e5SSatish Balay #undef __FUNC__ 11d4bb536fSBarry Smith #define __FUNC__ "SNESView" 129b94acceSBarry Smith /*@ 139b94acceSBarry Smith SNESView - Prints the SNES data structure. 149b94acceSBarry Smith 15fee21e36SBarry Smith Collective on SNES, unless Viewer is VIEWER_STDOUT_SELF 16fee21e36SBarry Smith 17c7afd0dbSLois Curfman McInnes Input Parameters: 18c7afd0dbSLois Curfman McInnes + SNES - the SNES context 19c7afd0dbSLois Curfman McInnes - viewer - visualization context 20c7afd0dbSLois Curfman McInnes 219b94acceSBarry Smith Options Database Key: 22c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 239b94acceSBarry Smith 249b94acceSBarry Smith Notes: 259b94acceSBarry Smith The available visualization contexts include 26c7afd0dbSLois Curfman McInnes + VIEWER_STDOUT_SELF - standard output (default) 27c7afd0dbSLois Curfman McInnes - VIEWER_STDOUT_WORLD - synchronized standard 28c8a8ba5cSLois Curfman McInnes output where only the first processor opens 29c8a8ba5cSLois Curfman McInnes the file. All other processors send their 30c8a8ba5cSLois Curfman McInnes data to the first processor to print. 319b94acceSBarry Smith 323e081fefSLois Curfman McInnes The user can open an alternative visualization context with 3377ed5343SBarry Smith ViewerASCIIOpen() - output to a specified file. 349b94acceSBarry Smith 3536851e7fSLois Curfman McInnes Level: beginner 3636851e7fSLois Curfman McInnes 379b94acceSBarry Smith .keywords: SNES, view 389b94acceSBarry Smith 3977ed5343SBarry Smith .seealso: ViewerASCIIOpen() 409b94acceSBarry Smith @*/ 419b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer) 429b94acceSBarry Smith { 439b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 449b94acceSBarry Smith int ierr; 459b94acceSBarry Smith SLES sles; 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); 58*184914b5SBarry Smith if (method) { 59d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," method: %s\n",method);CHKERRQ(ierr); 60*184914b5SBarry Smith } else { 61*184914b5SBarry Smith ierr = ViewerASCIIPrintf(viewer," method: not set yet\n");CHKERRQ(ierr); 62*184914b5SBarry Smith } 630ef38995SBarry Smith if (snes->view) { 640ef38995SBarry Smith ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 650ef38995SBarry Smith ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr); 660ef38995SBarry Smith ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 670ef38995SBarry Smith } 68d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 69d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 70d132466eSBarry Smith snes->rtol, snes->atol, snes->trunctol, snes->xtol);CHKERRQ(ierr); 71d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr); 72d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr); 730ef38995SBarry Smith if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 74d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr); 750ef38995SBarry Smith } 769b94acceSBarry Smith if (snes->ksp_ewconv) { 779b94acceSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 789b94acceSBarry Smith if (kctx) { 79d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr); 80d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 81d132466eSBarry Smith ierr = ViewerASCIIPrintf(viewer," gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 829b94acceSBarry Smith } 839b94acceSBarry Smith } 843f1db9ecSBarry Smith } else if (PetscTypeCompare(vtype,STRING_VIEWER)) { 850ef38995SBarry Smith ierr = SNESGetType(snes,&method);CHKERRQ(ierr); 860ef38995SBarry Smith ierr = ViewerStringSPrintf(viewer," %-3.3s",method);CHKERRQ(ierr); 8719bcc07fSBarry Smith } 8877ed5343SBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 890ef38995SBarry Smith ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 909b94acceSBarry Smith ierr = SLESView(sles,viewer);CHKERRQ(ierr); 910ef38995SBarry Smith ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 923a40ed3dSBarry Smith PetscFunctionReturn(0); 939b94acceSBarry Smith } 949b94acceSBarry Smith 95639f9d9dSBarry Smith /* 96639f9d9dSBarry Smith We retain a list of functions that also take SNES command 97639f9d9dSBarry Smith line options. These are called at the end SNESSetFromOptions() 98639f9d9dSBarry Smith */ 99639f9d9dSBarry Smith #define MAXSETFROMOPTIONS 5 100639f9d9dSBarry Smith static int numberofsetfromoptions; 101639f9d9dSBarry Smith static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 102639f9d9dSBarry Smith 1035615d1e5SSatish Balay #undef __FUNC__ 104d4bb536fSBarry Smith #define __FUNC__ "SNESAddOptionsChecker" 105639f9d9dSBarry Smith /*@ 106639f9d9dSBarry Smith SNESAddOptionsChecker - Adds an additional function to check for SNES options. 107639f9d9dSBarry Smith 108c7afd0dbSLois Curfman McInnes Not Collective 109c7afd0dbSLois Curfman McInnes 110639f9d9dSBarry Smith Input Parameter: 111639f9d9dSBarry Smith . snescheck - function that checks for options 112639f9d9dSBarry Smith 11336851e7fSLois Curfman McInnes Level: developer 11436851e7fSLois Curfman McInnes 115639f9d9dSBarry Smith .seealso: SNESSetFromOptions() 116639f9d9dSBarry Smith @*/ 117639f9d9dSBarry Smith int SNESAddOptionsChecker(int (*snescheck)(SNES) ) 118639f9d9dSBarry Smith { 1193a40ed3dSBarry Smith PetscFunctionBegin; 120639f9d9dSBarry Smith if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 121a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many options checkers, only 5 allowed"); 122639f9d9dSBarry Smith } 123639f9d9dSBarry Smith 124639f9d9dSBarry Smith othersetfromoptions[numberofsetfromoptions++] = snescheck; 1253a40ed3dSBarry Smith PetscFunctionReturn(0); 126639f9d9dSBarry Smith } 127639f9d9dSBarry Smith 1285615d1e5SSatish Balay #undef __FUNC__ 12915091d37SBarry Smith #define __FUNC__ "SNESSetTypeFromOptions" 13015091d37SBarry Smith /*@ 13115091d37SBarry Smith SNESSetTypeFromOptions - Sets the SNES solver type from the options database, 13215091d37SBarry Smith or sets a default if none is give. 13315091d37SBarry Smith 13415091d37SBarry Smith Collective on SNES 13515091d37SBarry Smith 13615091d37SBarry Smith Input Parameter: 13715091d37SBarry Smith . snes - the SNES context 13815091d37SBarry Smith 13915091d37SBarry Smith Options Database Keys: 14015091d37SBarry Smith . -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc 14115091d37SBarry Smith 14215091d37SBarry Smith Level: beginner 14315091d37SBarry Smith 14415091d37SBarry Smith .keywords: SNES, nonlinear, set, options, database 14515091d37SBarry Smith 14615091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetFromOptions() 14715091d37SBarry Smith @*/ 14815091d37SBarry Smith int SNESSetTypeFromOptions(SNES snes) 14915091d37SBarry Smith { 15015091d37SBarry Smith char method[256]; 15115091d37SBarry Smith int ierr, flg; 15215091d37SBarry Smith 15315091d37SBarry Smith PetscFunctionBegin; 15415091d37SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15515091d37SBarry Smith if (snes->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to SNESSetUp()"); 156888f2ed8SSatish Balay ierr = OptionsGetString(snes->prefix,"-snes_type",method,256,&flg);CHKERRQ(ierr); 15715091d37SBarry Smith if (flg) { 15815091d37SBarry Smith ierr = SNESSetType(snes,(SNESType) method);CHKERRQ(ierr); 15915091d37SBarry Smith } 16015091d37SBarry Smith /* 16115091d37SBarry Smith If SNES type has not yet been set, set it now 16215091d37SBarry Smith */ 16315091d37SBarry Smith if (!snes->type_name) { 16415091d37SBarry Smith if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 16515091d37SBarry Smith ierr = SNESSetType(snes,SNES_EQ_LS);CHKERRQ(ierr); 16615091d37SBarry Smith } else { 16715091d37SBarry Smith ierr = SNESSetType(snes,SNES_UM_TR);CHKERRQ(ierr); 16815091d37SBarry Smith } 16915091d37SBarry Smith } 17015091d37SBarry Smith PetscFunctionReturn(0); 17115091d37SBarry Smith } 17215091d37SBarry Smith 17315091d37SBarry Smith #undef __FUNC__ 1745615d1e5SSatish Balay #define __FUNC__ "SNESSetFromOptions" 1759b94acceSBarry Smith /*@ 176682d7d0cSBarry Smith SNESSetFromOptions - Sets various SNES and SLES parameters from user options. 1779b94acceSBarry Smith 178c7afd0dbSLois Curfman McInnes Collective on SNES 179c7afd0dbSLois Curfman McInnes 1809b94acceSBarry Smith Input Parameter: 1819b94acceSBarry Smith . snes - the SNES context 1829b94acceSBarry Smith 18336851e7fSLois Curfman McInnes Options Database Keys: 184b39c3a46SLois Curfman McInnes + -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc 18582738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 18682738288SBarry Smith of the change in the solution between steps 187b39c3a46SLois Curfman McInnes . -snes_atol <atol> - absolute tolerance of residual norm 188b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 189b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 190b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 191b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 19282738288SBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear or minimization 19382738288SBarry Smith solver; hence iterations will continue until max_it 1941fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 19582738288SBarry Smith of convergence test 19682738288SBarry Smith . -snes_monitor - prints residual norm at each iteration 197d132466eSBarry Smith . -snes_vecmonitor - plots solution at each iteration 198d132466eSBarry Smith . -snes_vecmonitor_update - plots update to solution at each iteration 19982738288SBarry Smith . -snes_xmonitor - plots residual norm at each iteration 200e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 20136851e7fSLois Curfman McInnes - -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 20282738288SBarry Smith 20382738288SBarry Smith Options Database for Eisenstat-Walker method: 20482738288SBarry Smith + -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence 20582738288SBarry Smith . -snes_ksp_eq_version ver - version of Eisenstat-Walker method 20636851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 20736851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 20836851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 20936851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 21036851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 21136851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 21282738288SBarry Smith 21311ca99fdSLois Curfman McInnes Notes: 21411ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 21511ca99fdSLois Curfman McInnes the users manual. 21683e2fdc7SBarry Smith 21736851e7fSLois Curfman McInnes Level: beginner 21836851e7fSLois Curfman McInnes 2199b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 2209b94acceSBarry Smith 22115091d37SBarry Smith .seealso: SNESPrintHelp(), SNESSetOptionsPrefix(), SNESSetTypeFromOptions() 2229b94acceSBarry Smith @*/ 2239b94acceSBarry Smith int SNESSetFromOptions(SNES snes) 2249b94acceSBarry Smith { 2259b94acceSBarry Smith double tmp; 2269b94acceSBarry Smith SLES sles; 22781f57ec7SBarry Smith int ierr, flg,i,loc[4],nmax = 4; 2283c7409f5SSatish Balay int version = PETSC_DEFAULT; 2299b94acceSBarry Smith double rtol_0 = PETSC_DEFAULT; 2309b94acceSBarry Smith double rtol_max = PETSC_DEFAULT; 2319b94acceSBarry Smith double gamma2 = PETSC_DEFAULT; 2329b94acceSBarry Smith double alpha = PETSC_DEFAULT; 2339b94acceSBarry Smith double alpha2 = PETSC_DEFAULT; 2349b94acceSBarry Smith double threshold = PETSC_DEFAULT; 2359b94acceSBarry Smith 2363a40ed3dSBarry Smith PetscFunctionBegin; 23777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 23815091d37SBarry Smith ierr = SNESSetTypeFromOptions(snes);CHKERRQ(ierr); 239ca161407SBarry Smith 24015091d37SBarry Smith loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300; 2413c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg);CHKERRQ(ierr); 242d64ed03dSBarry Smith if (flg) { 243d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 244d64ed03dSBarry Smith } 2453c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg);CHKERRQ(ierr); 246d64ed03dSBarry Smith if (flg) { 247d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 248d64ed03dSBarry Smith } 2493c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg);CHKERRQ(ierr); 250d64ed03dSBarry Smith if (flg) { 251d64ed03dSBarry Smith ierr = SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 252d64ed03dSBarry Smith } 2533c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg);CHKERRQ(ierr); 2543c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr); 255d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg);CHKERRQ(ierr); 256d64ed03dSBarry Smith if (flg) { ierr = SNESSetTrustRegionTolerance(snes,tmp);CHKERRQ(ierr); } 257d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg);CHKERRQ(ierr); 258d64ed03dSBarry Smith if (flg) { ierr = SNESSetMinimizationFunctionTolerance(snes,tmp);CHKERRQ(ierr);} 2593c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg);CHKERRQ(ierr); 2603c7409f5SSatish Balay if (flg) { snes->ksp_ewconv = 1; } 261b18e04deSLois Curfman McInnes ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg);CHKERRQ(ierr); 262b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg);CHKERRQ(ierr); 263b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr); 264b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg);CHKERRQ(ierr); 265b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg);CHKERRQ(ierr); 266b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg);CHKERRQ(ierr); 267b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr); 26893c39befSBarry Smith 26993c39befSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_no_convergence_test",&flg);CHKERRQ(ierr); 27093c39befSBarry Smith if (flg) {snes->converged = 0;} 27193c39befSBarry Smith 2729b94acceSBarry Smith ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 2739b94acceSBarry Smith alpha2,threshold);CHKERRQ(ierr); 2745bbad29bSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_cancelmonitors",&flg);CHKERRQ(ierr); 2755cd90555SBarry Smith if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);} 2763c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg);CHKERRQ(ierr); 277b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);} 2783c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg);CHKERRQ(ierr); 279b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);} 2803f1db9ecSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor",&flg);CHKERRQ(ierr); 281b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);} 282d132466eSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor_update",&flg);CHKERRQ(ierr); 283b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitorUpdate,0,0);CHKERRQ(ierr);} 28481f57ec7SBarry Smith ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr); 2853c7409f5SSatish Balay if (flg) { 28617699dbbSLois Curfman McInnes int rank = 0; 287d7e8b826SBarry Smith DrawLG lg; 28817699dbbSLois Curfman McInnes MPI_Initialized(&rank); 289d132466eSBarry Smith if (rank) {ierr = MPI_Comm_rank(snes->comm,&rank);CHKERRQ(ierr);} 29017699dbbSLois Curfman McInnes if (!rank) { 29181f57ec7SBarry Smith ierr = SNESLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg);CHKERRQ(ierr); 292b8a78c4aSBarry Smith ierr = SNESSetMonitor(snes,SNESLGMonitor,lg,( int (*)(void *))SNESLGMonitorDestroy);CHKERRQ(ierr); 2939b94acceSBarry Smith PLogObjectParent(snes,lg); 2949b94acceSBarry Smith } 2959b94acceSBarry Smith } 296e24b481bSBarry Smith 2973c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);CHKERRQ(ierr); 2983c7409f5SSatish Balay if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 2999b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 3009b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 301981c4779SBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 302981c4779SBarry Smith } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 30331615d04SLois Curfman McInnes ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 30431615d04SLois Curfman McInnes SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr); 305d64ed03dSBarry Smith PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 3069b94acceSBarry Smith } 307639f9d9dSBarry Smith 308639f9d9dSBarry Smith for ( i=0; i<numberofsetfromoptions; i++ ) { 309639f9d9dSBarry Smith ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr); 310639f9d9dSBarry Smith } 311639f9d9dSBarry Smith 3129b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 3139b94acceSBarry Smith ierr = SLESSetFromOptions(sles);CHKERRQ(ierr); 31493993e2dSLois Curfman McInnes 315e24b481bSBarry Smith /* set the special KSP monitor for matrix-free application */ 316e24b481bSBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg);CHKERRQ(ierr); 317e24b481bSBarry Smith if (flg) { 318e24b481bSBarry Smith KSP ksp; 319e24b481bSBarry Smith ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 320b8a78c4aSBarry Smith ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr); 321e24b481bSBarry Smith } 322e24b481bSBarry Smith 32382bf6240SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help", &flg);CHKERRQ(ierr); 32482bf6240SBarry Smith if (flg) { ierr = SNESPrintHelp(snes);CHKERRQ(ierr);} 32582bf6240SBarry Smith 3263a40ed3dSBarry Smith if (snes->setfromoptions) { 3273a40ed3dSBarry Smith ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr); 3283a40ed3dSBarry Smith } 3293a40ed3dSBarry Smith PetscFunctionReturn(0); 3309b94acceSBarry Smith } 3319b94acceSBarry Smith 332a847f771SSatish Balay 3335615d1e5SSatish Balay #undef __FUNC__ 334d4bb536fSBarry Smith #define __FUNC__ "SNESSetApplicationContext" 3359b94acceSBarry Smith /*@ 3369b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3379b94acceSBarry Smith the nonlinear solvers. 3389b94acceSBarry Smith 339fee21e36SBarry Smith Collective on SNES 340fee21e36SBarry Smith 341c7afd0dbSLois Curfman McInnes Input Parameters: 342c7afd0dbSLois Curfman McInnes + snes - the SNES context 343c7afd0dbSLois Curfman McInnes - usrP - optional user context 344c7afd0dbSLois Curfman McInnes 34536851e7fSLois Curfman McInnes Level: intermediate 34636851e7fSLois Curfman McInnes 3479b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3489b94acceSBarry Smith 3499b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3509b94acceSBarry Smith @*/ 3519b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 3529b94acceSBarry Smith { 3533a40ed3dSBarry Smith PetscFunctionBegin; 35477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3559b94acceSBarry Smith snes->user = usrP; 3563a40ed3dSBarry Smith PetscFunctionReturn(0); 3579b94acceSBarry Smith } 35874679c65SBarry Smith 3595615d1e5SSatish Balay #undef __FUNC__ 360d4bb536fSBarry Smith #define __FUNC__ "SNESGetApplicationContext" 3619b94acceSBarry Smith /*@C 3629b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3639b94acceSBarry Smith nonlinear solvers. 3649b94acceSBarry Smith 365c7afd0dbSLois Curfman McInnes Not Collective 366c7afd0dbSLois Curfman McInnes 3679b94acceSBarry Smith Input Parameter: 3689b94acceSBarry Smith . snes - SNES context 3699b94acceSBarry Smith 3709b94acceSBarry Smith Output Parameter: 3719b94acceSBarry Smith . usrP - user context 3729b94acceSBarry Smith 37336851e7fSLois Curfman McInnes Level: intermediate 37436851e7fSLois Curfman McInnes 3759b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3769b94acceSBarry Smith 3779b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3789b94acceSBarry Smith @*/ 3799b94acceSBarry Smith int SNESGetApplicationContext( SNES snes, void **usrP ) 3809b94acceSBarry Smith { 3813a40ed3dSBarry Smith PetscFunctionBegin; 38277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3839b94acceSBarry Smith *usrP = snes->user; 3843a40ed3dSBarry Smith PetscFunctionReturn(0); 3859b94acceSBarry Smith } 38674679c65SBarry Smith 3875615d1e5SSatish Balay #undef __FUNC__ 388d4bb536fSBarry Smith #define __FUNC__ "SNESGetIterationNumber" 3899b94acceSBarry Smith /*@ 390c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 391c8228a4eSBarry Smith at this time. 3929b94acceSBarry Smith 393c7afd0dbSLois Curfman McInnes Not Collective 394c7afd0dbSLois Curfman McInnes 3959b94acceSBarry Smith Input Parameter: 3969b94acceSBarry Smith . snes - SNES context 3979b94acceSBarry Smith 3989b94acceSBarry Smith Output Parameter: 3999b94acceSBarry Smith . iter - iteration number 4009b94acceSBarry Smith 401c8228a4eSBarry Smith Notes: 402c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 403c8228a4eSBarry Smith 404c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 40508405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 40608405cd6SLois Curfman McInnes .vb 40708405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 40808405cd6SLois Curfman McInnes if (!(it % 2)) { 40908405cd6SLois Curfman McInnes [compute Jacobian here] 41008405cd6SLois Curfman McInnes } 41108405cd6SLois Curfman McInnes .ve 412c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 41308405cd6SLois Curfman McInnes recomputed every second SNES iteration. 414c8228a4eSBarry Smith 41536851e7fSLois Curfman McInnes Level: intermediate 41636851e7fSLois Curfman McInnes 4179b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 4189b94acceSBarry Smith @*/ 4199b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 4209b94acceSBarry Smith { 4213a40ed3dSBarry Smith PetscFunctionBegin; 42277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 42374679c65SBarry Smith PetscValidIntPointer(iter); 4249b94acceSBarry Smith *iter = snes->iter; 4253a40ed3dSBarry Smith PetscFunctionReturn(0); 4269b94acceSBarry Smith } 42774679c65SBarry Smith 4285615d1e5SSatish Balay #undef __FUNC__ 4295615d1e5SSatish Balay #define __FUNC__ "SNESGetFunctionNorm" 4309b94acceSBarry Smith /*@ 4319b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4329b94acceSBarry Smith with SNESSSetFunction(). 4339b94acceSBarry Smith 434c7afd0dbSLois Curfman McInnes Collective on SNES 435c7afd0dbSLois Curfman McInnes 4369b94acceSBarry Smith Input Parameter: 4379b94acceSBarry Smith . snes - SNES context 4389b94acceSBarry Smith 4399b94acceSBarry Smith Output Parameter: 4409b94acceSBarry Smith . fnorm - 2-norm of function 4419b94acceSBarry Smith 4429b94acceSBarry Smith Note: 4439b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 444a86d99e1SLois Curfman McInnes A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 445a86d99e1SLois Curfman McInnes SNESGetGradientNorm(). 4469b94acceSBarry Smith 44736851e7fSLois Curfman McInnes Level: intermediate 44836851e7fSLois Curfman McInnes 4499b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 450a86d99e1SLois Curfman McInnes 45108405cd6SLois Curfman McInnes .seealso: SNESGetFunction() 4529b94acceSBarry Smith @*/ 4539b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 4549b94acceSBarry Smith { 4553a40ed3dSBarry Smith PetscFunctionBegin; 45677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 45774679c65SBarry Smith PetscValidScalarPointer(fnorm); 45874679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 459d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only"); 46074679c65SBarry Smith } 4619b94acceSBarry Smith *fnorm = snes->norm; 4623a40ed3dSBarry Smith PetscFunctionReturn(0); 4639b94acceSBarry Smith } 46474679c65SBarry Smith 4655615d1e5SSatish Balay #undef __FUNC__ 4665615d1e5SSatish Balay #define __FUNC__ "SNESGetGradientNorm" 4679b94acceSBarry Smith /*@ 4689b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 4699b94acceSBarry Smith with SNESSSetGradient(). 4709b94acceSBarry Smith 471c7afd0dbSLois Curfman McInnes Collective on SNES 472c7afd0dbSLois Curfman McInnes 4739b94acceSBarry Smith Input Parameter: 4749b94acceSBarry Smith . snes - SNES context 4759b94acceSBarry Smith 4769b94acceSBarry Smith Output Parameter: 4779b94acceSBarry Smith . fnorm - 2-norm of gradient 4789b94acceSBarry Smith 4799b94acceSBarry Smith Note: 4809b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 481a86d99e1SLois Curfman McInnes methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 482a86d99e1SLois Curfman McInnes is SNESGetFunctionNorm(). 4839b94acceSBarry Smith 48436851e7fSLois Curfman McInnes Level: intermediate 48536851e7fSLois Curfman McInnes 4869b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 487a86d99e1SLois Curfman McInnes 488a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient() 4899b94acceSBarry Smith @*/ 4909b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 4919b94acceSBarry Smith { 4923a40ed3dSBarry Smith PetscFunctionBegin; 49377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 49474679c65SBarry Smith PetscValidScalarPointer(gnorm); 49574679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 496d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 49774679c65SBarry Smith } 4989b94acceSBarry Smith *gnorm = snes->norm; 4993a40ed3dSBarry Smith PetscFunctionReturn(0); 5009b94acceSBarry Smith } 50174679c65SBarry Smith 5025615d1e5SSatish Balay #undef __FUNC__ 503d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberUnsuccessfulSteps" 5049b94acceSBarry Smith /*@ 5059b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 5069b94acceSBarry Smith attempted by the nonlinear solver. 5079b94acceSBarry Smith 508c7afd0dbSLois Curfman McInnes Not Collective 509c7afd0dbSLois Curfman McInnes 5109b94acceSBarry Smith Input Parameter: 5119b94acceSBarry Smith . snes - SNES context 5129b94acceSBarry Smith 5139b94acceSBarry Smith Output Parameter: 5149b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 5159b94acceSBarry Smith 516c96a6f78SLois Curfman McInnes Notes: 517c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 518c96a6f78SLois Curfman McInnes 51936851e7fSLois Curfman McInnes Level: intermediate 52036851e7fSLois Curfman McInnes 5219b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 5229b94acceSBarry Smith @*/ 5239b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 5249b94acceSBarry Smith { 5253a40ed3dSBarry Smith PetscFunctionBegin; 52677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 52774679c65SBarry Smith PetscValidIntPointer(nfails); 5289b94acceSBarry Smith *nfails = snes->nfailures; 5293a40ed3dSBarry Smith PetscFunctionReturn(0); 5309b94acceSBarry Smith } 531a847f771SSatish Balay 5325615d1e5SSatish Balay #undef __FUNC__ 533d4bb536fSBarry Smith #define __FUNC__ "SNESGetNumberLinearIterations" 534c96a6f78SLois Curfman McInnes /*@ 535c96a6f78SLois Curfman McInnes SNESGetNumberLinearIterations - Gets the total number of linear iterations 536c96a6f78SLois Curfman McInnes used by the nonlinear solver. 537c96a6f78SLois Curfman McInnes 538c7afd0dbSLois Curfman McInnes Not Collective 539c7afd0dbSLois Curfman McInnes 540c96a6f78SLois Curfman McInnes Input Parameter: 541c96a6f78SLois Curfman McInnes . snes - SNES context 542c96a6f78SLois Curfman McInnes 543c96a6f78SLois Curfman McInnes Output Parameter: 544c96a6f78SLois Curfman McInnes . lits - number of linear iterations 545c96a6f78SLois Curfman McInnes 546c96a6f78SLois Curfman McInnes Notes: 547c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 548c96a6f78SLois Curfman McInnes 54936851e7fSLois Curfman McInnes Level: intermediate 55036851e7fSLois Curfman McInnes 551c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 552c96a6f78SLois Curfman McInnes @*/ 55386bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits) 554c96a6f78SLois Curfman McInnes { 5553a40ed3dSBarry Smith PetscFunctionBegin; 556c96a6f78SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 557c96a6f78SLois Curfman McInnes PetscValidIntPointer(lits); 558c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 5593a40ed3dSBarry Smith PetscFunctionReturn(0); 560c96a6f78SLois Curfman McInnes } 561c96a6f78SLois Curfman McInnes 562c96a6f78SLois Curfman McInnes #undef __FUNC__ 563d4bb536fSBarry Smith #define __FUNC__ "SNESGetSLES" 5649b94acceSBarry Smith /*@C 5659b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 5669b94acceSBarry Smith 567c7afd0dbSLois Curfman McInnes Not Collective, but if SNES object is parallel, then SLES object is parallel 568c7afd0dbSLois Curfman McInnes 5699b94acceSBarry Smith Input Parameter: 5709b94acceSBarry Smith . snes - the SNES context 5719b94acceSBarry Smith 5729b94acceSBarry Smith Output Parameter: 5739b94acceSBarry Smith . sles - the SLES context 5749b94acceSBarry Smith 5759b94acceSBarry Smith Notes: 5769b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 5779b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 5789b94acceSBarry Smith KSP and PC contexts as well. 5799b94acceSBarry Smith 58036851e7fSLois Curfman McInnes Level: beginner 58136851e7fSLois Curfman McInnes 5829b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 5839b94acceSBarry Smith 5849b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 5859b94acceSBarry Smith @*/ 5869b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 5879b94acceSBarry Smith { 5883a40ed3dSBarry Smith PetscFunctionBegin; 58977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5909b94acceSBarry Smith *sles = snes->sles; 5913a40ed3dSBarry Smith PetscFunctionReturn(0); 5929b94acceSBarry Smith } 59382bf6240SBarry Smith 594e24b481bSBarry Smith #undef __FUNC__ 595e24b481bSBarry Smith #define __FUNC__ "SNESPublish_Petsc" 596e24b481bSBarry Smith static int SNESPublish_Petsc(PetscObject object) 597e24b481bSBarry Smith { 598aa482453SBarry Smith #if defined(PETSC_HAVE_AMS) 599e24b481bSBarry Smith SNES v = (SNES) object; 600e24b481bSBarry Smith int ierr; 60143d6d2cbSBarry Smith #endif 602e24b481bSBarry Smith 603e24b481bSBarry Smith PetscFunctionBegin; 604e24b481bSBarry Smith 60543d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS) 606e24b481bSBarry Smith /* if it is already published then return */ 607e24b481bSBarry Smith if (v->amem >=0 ) PetscFunctionReturn(0); 608e24b481bSBarry Smith 6093f1db9ecSBarry Smith ierr = PetscObjectPublishBaseBegin(object);CHKERRQ(ierr); 610e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ, 611e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 612e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ, 613e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 614e24b481bSBarry Smith ierr = PetscObjectPublishBaseEnd(object);CHKERRQ(ierr); 615433994e6SBarry Smith #endif 616e24b481bSBarry Smith PetscFunctionReturn(0); 617e24b481bSBarry Smith } 618e24b481bSBarry Smith 6199b94acceSBarry Smith /* -----------------------------------------------------------*/ 6205615d1e5SSatish Balay #undef __FUNC__ 6215615d1e5SSatish Balay #define __FUNC__ "SNESCreate" 6229b94acceSBarry Smith /*@C 6239b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 6249b94acceSBarry Smith 625c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 626c7afd0dbSLois Curfman McInnes 627c7afd0dbSLois Curfman McInnes Input Parameters: 628c7afd0dbSLois Curfman McInnes + comm - MPI communicator 629c7afd0dbSLois Curfman McInnes - type - type of method, either 630c7afd0dbSLois Curfman McInnes SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations) 631c7afd0dbSLois Curfman McInnes or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization) 6329b94acceSBarry Smith 6339b94acceSBarry Smith Output Parameter: 6349b94acceSBarry Smith . outsnes - the new SNES context 6359b94acceSBarry Smith 636c7afd0dbSLois Curfman McInnes Options Database Keys: 637c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 638c7afd0dbSLois Curfman McInnes and no preconditioning matrix 639c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 640c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 641c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 642c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 643c1f60f51SBarry Smith 64436851e7fSLois Curfman McInnes Level: beginner 64536851e7fSLois Curfman McInnes 6469b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 6479b94acceSBarry Smith 64863a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy() 6499b94acceSBarry Smith @*/ 6504b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 6519b94acceSBarry Smith { 6529b94acceSBarry Smith int ierr; 6539b94acceSBarry Smith SNES snes; 6549b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 65537fcc0dbSBarry Smith 6563a40ed3dSBarry Smith PetscFunctionBegin; 6579b94acceSBarry Smith *outsnes = 0; 658d64ed03dSBarry Smith if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){ 659d252947aSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type"); 660d64ed03dSBarry Smith } 6613f1db9ecSBarry Smith PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView); 6629b94acceSBarry Smith PLogObjectCreate(snes); 663e24b481bSBarry Smith snes->bops->publish = SNESPublish_Petsc; 6649b94acceSBarry Smith snes->max_its = 50; 6659750a799SBarry Smith snes->max_funcs = 10000; 6669b94acceSBarry Smith snes->norm = 0.0; 6675a2d0531SBarry Smith if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 668b18e04deSLois Curfman McInnes snes->rtol = 1.e-8; 669b18e04deSLois Curfman McInnes snes->ttol = 0.0; 6709b94acceSBarry Smith snes->atol = 1.e-10; 671d64ed03dSBarry Smith } else { 672b4874afaSBarry Smith snes->rtol = 1.e-8; 673b4874afaSBarry Smith snes->ttol = 0.0; 674b4874afaSBarry Smith snes->atol = 1.e-50; 675b4874afaSBarry Smith } 6769b94acceSBarry Smith snes->xtol = 1.e-8; 677b18e04deSLois Curfman McInnes snes->trunctol = 1.e-12; /* no longer used */ 6789b94acceSBarry Smith snes->nfuncs = 0; 6799b94acceSBarry Smith snes->nfailures = 0; 6807a00f4a9SLois Curfman McInnes snes->linear_its = 0; 681639f9d9dSBarry Smith snes->numbermonitors = 0; 6829b94acceSBarry Smith snes->data = 0; 6839b94acceSBarry Smith snes->view = 0; 6849b94acceSBarry Smith snes->computeumfunction = 0; 6859b94acceSBarry Smith snes->umfunP = 0; 6869b94acceSBarry Smith snes->fc = 0; 6879b94acceSBarry Smith snes->deltatol = 1.e-12; 6889b94acceSBarry Smith snes->fmin = -1.e30; 6899b94acceSBarry Smith snes->method_class = type; 6909b94acceSBarry Smith snes->set_method_called = 0; 69182bf6240SBarry Smith snes->setupcalled = 0; 6929b94acceSBarry Smith snes->ksp_ewconv = 0; 6936f24a144SLois Curfman McInnes snes->vwork = 0; 6946f24a144SLois Curfman McInnes snes->nwork = 0; 695758f92a0SBarry Smith snes->conv_hist_len = 0; 696758f92a0SBarry Smith snes->conv_hist_max = 0; 697758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 698758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 699758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 700*184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 7019b94acceSBarry Smith 7029b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 7030452661fSBarry Smith kctx = PetscNew(SNES_KSP_EW_ConvCtx);CHKPTRQ(kctx); 704eed86810SBarry Smith PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx)); 7059b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 7069b94acceSBarry Smith kctx->version = 2; 7079b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 7089b94acceSBarry Smith this was too large for some test cases */ 7099b94acceSBarry Smith kctx->rtol_last = 0; 7109b94acceSBarry Smith kctx->rtol_max = .9; 7119b94acceSBarry Smith kctx->gamma = 1.0; 7129b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 7139b94acceSBarry Smith kctx->alpha = kctx->alpha2; 7149b94acceSBarry Smith kctx->threshold = .1; 7159b94acceSBarry Smith kctx->lresid_last = 0; 7169b94acceSBarry Smith kctx->norm_last = 0; 7179b94acceSBarry Smith 7189b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr); 7199b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 7205334005bSBarry Smith 7219b94acceSBarry Smith *outsnes = snes; 722e24b481bSBarry Smith PetscPublishAll(snes); 7233a40ed3dSBarry Smith PetscFunctionReturn(0); 7249b94acceSBarry Smith } 7259b94acceSBarry Smith 7269b94acceSBarry Smith /* --------------------------------------------------------------- */ 7275615d1e5SSatish Balay #undef __FUNC__ 728d4bb536fSBarry Smith #define __FUNC__ "SNESSetFunction" 7299b94acceSBarry Smith /*@C 7309b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 7319b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 7329b94acceSBarry Smith equations. 7339b94acceSBarry Smith 734fee21e36SBarry Smith Collective on SNES 735fee21e36SBarry Smith 736c7afd0dbSLois Curfman McInnes Input Parameters: 737c7afd0dbSLois Curfman McInnes + snes - the SNES context 738c7afd0dbSLois Curfman McInnes . func - function evaluation routine 739c7afd0dbSLois Curfman McInnes . r - vector to store function value 740c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 741c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 7429b94acceSBarry Smith 743c7afd0dbSLois Curfman McInnes Calling sequence of func: 7448d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 745c7afd0dbSLois Curfman McInnes 746313e4042SLois Curfman McInnes . f - function vector 747c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 7489b94acceSBarry Smith 7499b94acceSBarry Smith Notes: 7509b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 7519b94acceSBarry Smith $ f'(x) x = -f(x), 752c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 7539b94acceSBarry Smith 7549b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7559b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7569b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 7579b94acceSBarry Smith 75836851e7fSLois Curfman McInnes Level: beginner 75936851e7fSLois Curfman McInnes 7609b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 7619b94acceSBarry Smith 762a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 7639b94acceSBarry Smith @*/ 7645334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx) 7659b94acceSBarry Smith { 7663a40ed3dSBarry Smith PetscFunctionBegin; 76777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 768*184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 769*184914b5SBarry Smith PetscCheckSameComm(snes,r); 770a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 771a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 772a8c6a408SBarry Smith } 773*184914b5SBarry Smith 7749b94acceSBarry Smith snes->computefunction = func; 7759b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 7769b94acceSBarry Smith snes->funP = ctx; 7773a40ed3dSBarry Smith PetscFunctionReturn(0); 7789b94acceSBarry Smith } 7799b94acceSBarry Smith 7805615d1e5SSatish Balay #undef __FUNC__ 7815615d1e5SSatish Balay #define __FUNC__ "SNESComputeFunction" 7829b94acceSBarry Smith /*@ 78336851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 7849b94acceSBarry Smith SNESSetFunction(). 7859b94acceSBarry Smith 786c7afd0dbSLois Curfman McInnes Collective on SNES 787c7afd0dbSLois Curfman McInnes 7889b94acceSBarry Smith Input Parameters: 789c7afd0dbSLois Curfman McInnes + snes - the SNES context 790c7afd0dbSLois Curfman McInnes - x - input vector 7919b94acceSBarry Smith 7929b94acceSBarry Smith Output Parameter: 7933638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 7949b94acceSBarry Smith 7951bffabb2SLois Curfman McInnes Notes: 7969b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7979b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7989b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 7999b94acceSBarry Smith 80036851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 80136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 80236851e7fSLois Curfman McInnes themselves. 80336851e7fSLois Curfman McInnes 80436851e7fSLois Curfman McInnes Level: developer 80536851e7fSLois Curfman McInnes 8069b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 8079b94acceSBarry Smith 808a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 8099b94acceSBarry Smith @*/ 8109b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y) 8119b94acceSBarry Smith { 8129b94acceSBarry Smith int ierr; 8139b94acceSBarry Smith 8143a40ed3dSBarry Smith PetscFunctionBegin; 815*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 816*184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 817*184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 818*184914b5SBarry Smith PetscCheckSameComm(snes,x); 819*184914b5SBarry Smith PetscCheckSameComm(snes,y); 820d4bb536fSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 821a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 822d4bb536fSBarry Smith } 823*184914b5SBarry Smith 8249b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 825d64ed03dSBarry Smith PetscStackPush("SNES user function"); 8269b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 827d64ed03dSBarry Smith PetscStackPop; 828ae3c334cSLois Curfman McInnes snes->nfuncs++; 8299b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 8303a40ed3dSBarry Smith PetscFunctionReturn(0); 8319b94acceSBarry Smith } 8329b94acceSBarry Smith 8335615d1e5SSatish Balay #undef __FUNC__ 834d4bb536fSBarry Smith #define __FUNC__ "SNESSetMinimizationFunction" 8359b94acceSBarry Smith /*@C 8369b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 8379b94acceSBarry Smith unconstrained minimization. 8389b94acceSBarry Smith 839fee21e36SBarry Smith Collective on SNES 840fee21e36SBarry Smith 841c7afd0dbSLois Curfman McInnes Input Parameters: 842c7afd0dbSLois Curfman McInnes + snes - the SNES context 843c7afd0dbSLois Curfman McInnes . func - function evaluation routine 844c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 845c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8469b94acceSBarry Smith 847c7afd0dbSLois Curfman McInnes Calling sequence of func: 8488d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,double *f,void *ctx); 849c7afd0dbSLois Curfman McInnes 850c7afd0dbSLois Curfman McInnes + x - input vector 8519b94acceSBarry Smith . f - function 852c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined function context 8539b94acceSBarry Smith 85436851e7fSLois Curfman McInnes Level: beginner 85536851e7fSLois Curfman McInnes 8569b94acceSBarry Smith Notes: 8579b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 8589b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 8599b94acceSBarry Smith SNESSetFunction(). 8609b94acceSBarry Smith 8619b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 8629b94acceSBarry Smith 863a86d99e1SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 864a86d99e1SLois Curfman McInnes SNESSetHessian(), SNESSetGradient() 8659b94acceSBarry Smith @*/ 8669b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*), 8679b94acceSBarry Smith void *ctx) 8689b94acceSBarry Smith { 8693a40ed3dSBarry Smith PetscFunctionBegin; 87077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 871a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 872a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 873a8c6a408SBarry Smith } 8749b94acceSBarry Smith snes->computeumfunction = func; 8759b94acceSBarry Smith snes->umfunP = ctx; 8763a40ed3dSBarry Smith PetscFunctionReturn(0); 8779b94acceSBarry Smith } 8789b94acceSBarry Smith 8795615d1e5SSatish Balay #undef __FUNC__ 8805615d1e5SSatish Balay #define __FUNC__ "SNESComputeMinimizationFunction" 8819b94acceSBarry Smith /*@ 8829b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 8839b94acceSBarry Smith set with SNESSetMinimizationFunction(). 8849b94acceSBarry Smith 885c7afd0dbSLois Curfman McInnes Collective on SNES 886c7afd0dbSLois Curfman McInnes 8879b94acceSBarry Smith Input Parameters: 888c7afd0dbSLois Curfman McInnes + snes - the SNES context 889c7afd0dbSLois Curfman McInnes - x - input vector 8909b94acceSBarry Smith 8919b94acceSBarry Smith Output Parameter: 8929b94acceSBarry Smith . y - function value 8939b94acceSBarry Smith 8949b94acceSBarry Smith Notes: 8959b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 8969b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 8979b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 898a86d99e1SLois Curfman McInnes 89936851e7fSLois Curfman McInnes SNESComputeMinimizationFunction() is typically used within minimization 90036851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 90136851e7fSLois Curfman McInnes themselves. 90236851e7fSLois Curfman McInnes 90336851e7fSLois Curfman McInnes Level: developer 90436851e7fSLois Curfman McInnes 905a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function 906a86d99e1SLois Curfman McInnes 907a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 908a86d99e1SLois Curfman McInnes SNESComputeGradient(), SNESComputeHessian() 9099b94acceSBarry Smith @*/ 9109b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 9119b94acceSBarry Smith { 9129b94acceSBarry Smith int ierr; 9133a40ed3dSBarry Smith 9143a40ed3dSBarry Smith PetscFunctionBegin; 915*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 916*184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 917*184914b5SBarry Smith PetscCheckSameComm(snes,x); 918a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 919a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 920a8c6a408SBarry Smith } 921*184914b5SBarry Smith 9229b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 923d64ed03dSBarry Smith PetscStackPush("SNES user minimzation function"); 9249b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr); 925d64ed03dSBarry Smith PetscStackPop; 926ae3c334cSLois Curfman McInnes snes->nfuncs++; 9279b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 9283a40ed3dSBarry Smith PetscFunctionReturn(0); 9299b94acceSBarry Smith } 9309b94acceSBarry Smith 9315615d1e5SSatish Balay #undef __FUNC__ 932d4bb536fSBarry Smith #define __FUNC__ "SNESSetGradient" 9339b94acceSBarry Smith /*@C 9349b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 9359b94acceSBarry Smith vector for use by the SNES routines. 9369b94acceSBarry Smith 937c7afd0dbSLois Curfman McInnes Collective on SNES 938c7afd0dbSLois Curfman McInnes 9399b94acceSBarry Smith Input Parameters: 940c7afd0dbSLois Curfman McInnes + snes - the SNES context 9419b94acceSBarry Smith . func - function evaluation routine 942044dda88SLois Curfman McInnes . ctx - optional user-defined context for private data for the 943044dda88SLois Curfman McInnes gradient evaluation routine (may be PETSC_NULL) 944c7afd0dbSLois Curfman McInnes - r - vector to store gradient value 945fee21e36SBarry Smith 9469b94acceSBarry Smith Calling sequence of func: 9478d76a1e5SLois Curfman McInnes $ func (SNES, Vec x, Vec g, void *ctx); 9489b94acceSBarry Smith 949c7afd0dbSLois Curfman McInnes + x - input vector 9509b94acceSBarry Smith . g - gradient vector 951c7afd0dbSLois Curfman McInnes - ctx - optional user-defined gradient context 9529b94acceSBarry Smith 9539b94acceSBarry Smith Notes: 9549b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 9559b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 9569b94acceSBarry Smith SNESSetFunction(). 9579b94acceSBarry Smith 95836851e7fSLois Curfman McInnes Level: beginner 95936851e7fSLois Curfman McInnes 9609b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9619b94acceSBarry Smith 962a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 963a86d99e1SLois Curfman McInnes SNESSetMinimizationFunction(), 9649b94acceSBarry Smith @*/ 96574679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 9669b94acceSBarry Smith { 9673a40ed3dSBarry Smith PetscFunctionBegin; 96877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 969*184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 970*184914b5SBarry Smith PetscCheckSameComm(snes,r); 971a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 972a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 973a8c6a408SBarry Smith } 9749b94acceSBarry Smith snes->computefunction = func; 9759b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 9769b94acceSBarry Smith snes->funP = ctx; 9773a40ed3dSBarry Smith PetscFunctionReturn(0); 9789b94acceSBarry Smith } 9799b94acceSBarry Smith 9805615d1e5SSatish Balay #undef __FUNC__ 9815615d1e5SSatish Balay #define __FUNC__ "SNESComputeGradient" 9829b94acceSBarry Smith /*@ 983a86d99e1SLois Curfman McInnes SNESComputeGradient - Computes the gradient that has been set with 984a86d99e1SLois Curfman McInnes SNESSetGradient(). 9859b94acceSBarry Smith 986c7afd0dbSLois Curfman McInnes Collective on SNES 987c7afd0dbSLois Curfman McInnes 9889b94acceSBarry Smith Input Parameters: 989c7afd0dbSLois Curfman McInnes + snes - the SNES context 990c7afd0dbSLois Curfman McInnes - x - input vector 9919b94acceSBarry Smith 9929b94acceSBarry Smith Output Parameter: 9939b94acceSBarry Smith . y - gradient vector 9949b94acceSBarry Smith 9959b94acceSBarry Smith Notes: 9969b94acceSBarry Smith SNESComputeGradient() is valid only for 9979b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 9989b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 999a86d99e1SLois Curfman McInnes 100036851e7fSLois Curfman McInnes SNESComputeGradient() is typically used within minimization 100136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 100236851e7fSLois Curfman McInnes themselves. 100336851e7fSLois Curfman McInnes 100436851e7fSLois Curfman McInnes Level: developer 100536851e7fSLois Curfman McInnes 1006a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient 1007a86d99e1SLois Curfman McInnes 1008a86d99e1SLois Curfman McInnes .seealso: SNESSetGradient(), SNESGetGradient(), 1009a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction(), SNESComputeHessian() 10109b94acceSBarry Smith @*/ 10119b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y) 10129b94acceSBarry Smith { 10139b94acceSBarry Smith int ierr; 10143a40ed3dSBarry Smith 10153a40ed3dSBarry Smith PetscFunctionBegin; 1016*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1017*184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1018*184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1019*184914b5SBarry Smith PetscCheckSameComm(snes,x); 1020*184914b5SBarry Smith PetscCheckSameComm(snes,y); 10213a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1022a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 10233a40ed3dSBarry Smith } 10249b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 1025d64ed03dSBarry Smith PetscStackPush("SNES user gradient function"); 10269b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1027d64ed03dSBarry Smith PetscStackPop; 10289b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 10293a40ed3dSBarry Smith PetscFunctionReturn(0); 10309b94acceSBarry Smith } 10319b94acceSBarry Smith 10325615d1e5SSatish Balay #undef __FUNC__ 10335615d1e5SSatish Balay #define __FUNC__ "SNESComputeJacobian" 103462fef451SLois Curfman McInnes /*@ 103562fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 103662fef451SLois Curfman McInnes set with SNESSetJacobian(). 103762fef451SLois Curfman McInnes 1038c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1039c7afd0dbSLois Curfman McInnes 104062fef451SLois Curfman McInnes Input Parameters: 1041c7afd0dbSLois Curfman McInnes + snes - the SNES context 1042c7afd0dbSLois Curfman McInnes - x - input vector 104362fef451SLois Curfman McInnes 104462fef451SLois Curfman McInnes Output Parameters: 1045c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 104662fef451SLois Curfman McInnes . B - optional preconditioning matrix 1047c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1048fee21e36SBarry Smith 104962fef451SLois Curfman McInnes Notes: 105062fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 105162fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 105262fef451SLois Curfman McInnes 1053dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1054dc5a77f8SLois Curfman McInnes flag parameter. 105562fef451SLois Curfman McInnes 105662fef451SLois Curfman McInnes SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 105762fef451SLois Curfman McInnes methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 1058005c665bSBarry Smith methods is SNESComputeHessian(). 105962fef451SLois Curfman McInnes 106036851e7fSLois Curfman McInnes SNESComputeJacobian() is typically used within nonlinear solver 106136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 106236851e7fSLois Curfman McInnes themselves. 106336851e7fSLois Curfman McInnes 106436851e7fSLois Curfman McInnes Level: developer 106536851e7fSLois Curfman McInnes 106662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 106762fef451SLois Curfman McInnes 106862fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 106962fef451SLois Curfman McInnes @*/ 10709b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10719b94acceSBarry Smith { 10729b94acceSBarry Smith int ierr; 10733a40ed3dSBarry Smith 10743a40ed3dSBarry Smith PetscFunctionBegin; 1075*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1076*184914b5SBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE); 1077*184914b5SBarry Smith PetscCheckSameComm(snes,X); 10783a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1079a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 10803a40ed3dSBarry Smith } 10813a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 10829b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 1083c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1084d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 10859b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1086d64ed03dSBarry Smith PetscStackPop; 10879b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 10886d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 108977c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 109077c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 10913a40ed3dSBarry Smith PetscFunctionReturn(0); 10929b94acceSBarry Smith } 10939b94acceSBarry Smith 10945615d1e5SSatish Balay #undef __FUNC__ 10955615d1e5SSatish Balay #define __FUNC__ "SNESComputeHessian" 109662fef451SLois Curfman McInnes /*@ 109762fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 109862fef451SLois Curfman McInnes set with SNESSetHessian(). 109962fef451SLois Curfman McInnes 1100c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1101c7afd0dbSLois Curfman McInnes 110262fef451SLois Curfman McInnes Input Parameters: 1103c7afd0dbSLois Curfman McInnes + snes - the SNES context 1104c7afd0dbSLois Curfman McInnes - x - input vector 110562fef451SLois Curfman McInnes 110662fef451SLois Curfman McInnes Output Parameters: 1107c7afd0dbSLois Curfman McInnes + A - Hessian matrix 110862fef451SLois Curfman McInnes . B - optional preconditioning matrix 1109c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1110fee21e36SBarry Smith 111162fef451SLois Curfman McInnes Notes: 111262fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 111362fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 111462fef451SLois Curfman McInnes 1115dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1116dc5a77f8SLois Curfman McInnes flag parameter. 111762fef451SLois Curfman McInnes 111862fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 111962fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 112062fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 112162fef451SLois Curfman McInnes 112236851e7fSLois Curfman McInnes SNESComputeHessian() is typically used within minimization 112336851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 112436851e7fSLois Curfman McInnes themselves. 112536851e7fSLois Curfman McInnes 112636851e7fSLois Curfman McInnes Level: developer 112736851e7fSLois Curfman McInnes 112862fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 112962fef451SLois Curfman McInnes 1130a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 1131a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 113262fef451SLois Curfman McInnes @*/ 113362fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 11349b94acceSBarry Smith { 11359b94acceSBarry Smith int ierr; 11363a40ed3dSBarry Smith 11373a40ed3dSBarry Smith PetscFunctionBegin; 1138*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1139*184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1140*184914b5SBarry Smith PetscCheckSameComm(snes,x); 11413a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1142a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 11433a40ed3dSBarry Smith } 11443a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 114562fef451SLois Curfman McInnes PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 11460f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 1147d64ed03dSBarry Smith PetscStackPush("SNES user Hessian function"); 114862fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr); 1149d64ed03dSBarry Smith PetscStackPop; 115062fef451SLois Curfman McInnes PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 11510f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 115277c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 115377c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 11543a40ed3dSBarry Smith PetscFunctionReturn(0); 11559b94acceSBarry Smith } 11569b94acceSBarry Smith 11575615d1e5SSatish Balay #undef __FUNC__ 1158d4bb536fSBarry Smith #define __FUNC__ "SNESSetJacobian" 11599b94acceSBarry Smith /*@C 11609b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1161044dda88SLois Curfman McInnes location to store the matrix. 11629b94acceSBarry Smith 1163c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1164c7afd0dbSLois Curfman McInnes 11659b94acceSBarry Smith Input Parameters: 1166c7afd0dbSLois Curfman McInnes + snes - the SNES context 11679b94acceSBarry Smith . A - Jacobian matrix 11689b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11699b94acceSBarry Smith . func - Jacobian evaluation routine 1170c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11712cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11729b94acceSBarry Smith 11739b94acceSBarry Smith Calling sequence of func: 11748d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 11759b94acceSBarry Smith 1176c7afd0dbSLois Curfman McInnes + x - input vector 11779b94acceSBarry Smith . A - Jacobian matrix 11789b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1179ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1180ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1181c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 11829b94acceSBarry Smith 11839b94acceSBarry Smith Notes: 1184dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 11852cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1186ac21db08SLois Curfman McInnes 1187ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 11889b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 11899b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 11909b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 11919b94acceSBarry Smith throughout the global iterations. 11929b94acceSBarry Smith 119336851e7fSLois Curfman McInnes Level: beginner 119436851e7fSLois Curfman McInnes 11959b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 11969b94acceSBarry Smith 1197ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 11989b94acceSBarry Smith @*/ 11999b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 12009b94acceSBarry Smith MatStructure*,void*),void *ctx) 12019b94acceSBarry Smith { 12023a40ed3dSBarry Smith PetscFunctionBegin; 120377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1204*184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1205*184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1206*184914b5SBarry Smith PetscCheckSameComm(snes,A); 1207*184914b5SBarry Smith PetscCheckSameComm(snes,B); 1208a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1209a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 1210a8c6a408SBarry Smith } 1211*184914b5SBarry Smith 12129b94acceSBarry Smith snes->computejacobian = func; 12139b94acceSBarry Smith snes->jacP = ctx; 12149b94acceSBarry Smith snes->jacobian = A; 12159b94acceSBarry Smith snes->jacobian_pre = B; 12163a40ed3dSBarry Smith PetscFunctionReturn(0); 12179b94acceSBarry Smith } 121862fef451SLois Curfman McInnes 12195615d1e5SSatish Balay #undef __FUNC__ 1220d4bb536fSBarry Smith #define __FUNC__ "SNESGetJacobian" 1221c2aafc4cSSatish Balay /*@C 1222b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1223b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1224b4fd4287SBarry Smith 1225c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1226c7afd0dbSLois Curfman McInnes 1227b4fd4287SBarry Smith Input Parameter: 1228b4fd4287SBarry Smith . snes - the nonlinear solver context 1229b4fd4287SBarry Smith 1230b4fd4287SBarry Smith Output Parameters: 1231c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1232b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 1233c7afd0dbSLois Curfman McInnes - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1234fee21e36SBarry Smith 123536851e7fSLois Curfman McInnes Level: advanced 123636851e7fSLois Curfman McInnes 1237b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1238b4fd4287SBarry Smith @*/ 1239b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx) 1240b4fd4287SBarry Smith { 12413a40ed3dSBarry Smith PetscFunctionBegin; 1242*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 12433a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1244a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 12453a40ed3dSBarry Smith } 1246b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1247b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1248b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 12493a40ed3dSBarry Smith PetscFunctionReturn(0); 1250b4fd4287SBarry Smith } 1251b4fd4287SBarry Smith 12525615d1e5SSatish Balay #undef __FUNC__ 1253d4bb536fSBarry Smith #define __FUNC__ "SNESSetHessian" 12549b94acceSBarry Smith /*@C 12559b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 1256044dda88SLois Curfman McInnes location to store the matrix. 12579b94acceSBarry Smith 1258c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1259c7afd0dbSLois Curfman McInnes 12609b94acceSBarry Smith Input Parameters: 1261c7afd0dbSLois Curfman McInnes + snes - the SNES context 12629b94acceSBarry Smith . A - Hessian matrix 12639b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 12649b94acceSBarry Smith . func - Jacobian evaluation routine 1265c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1266313e4042SLois Curfman McInnes Hessian evaluation routine (may be PETSC_NULL) 12679b94acceSBarry Smith 12689b94acceSBarry Smith Calling sequence of func: 12698d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 12709b94acceSBarry Smith 1271c7afd0dbSLois Curfman McInnes + x - input vector 12729b94acceSBarry Smith . A - Hessian matrix 12739b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1274ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1275ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1276c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Hessian context 12779b94acceSBarry Smith 12789b94acceSBarry Smith Notes: 1279dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 12802cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1281ac21db08SLois Curfman McInnes 12829b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 12839b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 12849b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 12859b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 12869b94acceSBarry Smith throughout the global iterations. 12879b94acceSBarry Smith 128836851e7fSLois Curfman McInnes Level: beginner 128936851e7fSLois Curfman McInnes 12909b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 12919b94acceSBarry Smith 1292ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 12939b94acceSBarry Smith @*/ 12949b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 12959b94acceSBarry Smith MatStructure*,void*),void *ctx) 12969b94acceSBarry Smith { 12973a40ed3dSBarry Smith PetscFunctionBegin; 129877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1299*184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1300*184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1301*184914b5SBarry Smith PetscCheckSameComm(snes,A); 1302*184914b5SBarry Smith PetscCheckSameComm(snes,B); 1303d4bb536fSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1304a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1305d4bb536fSBarry Smith } 13069b94acceSBarry Smith snes->computejacobian = func; 13079b94acceSBarry Smith snes->jacP = ctx; 13089b94acceSBarry Smith snes->jacobian = A; 13099b94acceSBarry Smith snes->jacobian_pre = B; 13103a40ed3dSBarry Smith PetscFunctionReturn(0); 13119b94acceSBarry Smith } 13129b94acceSBarry Smith 13135615d1e5SSatish Balay #undef __FUNC__ 1314d4bb536fSBarry Smith #define __FUNC__ "SNESGetHessian" 131562fef451SLois Curfman McInnes /*@ 131662fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 131762fef451SLois Curfman McInnes provided context for evaluating the Hessian. 131862fef451SLois Curfman McInnes 1319c7afd0dbSLois Curfman McInnes Not Collective, but Mat object is parallel if SNES object is parallel 1320c7afd0dbSLois Curfman McInnes 132162fef451SLois Curfman McInnes Input Parameter: 132262fef451SLois Curfman McInnes . snes - the nonlinear solver context 132362fef451SLois Curfman McInnes 132462fef451SLois Curfman McInnes Output Parameters: 1325c7afd0dbSLois Curfman McInnes + A - location to stash Hessian matrix (or PETSC_NULL) 132662fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 1327c7afd0dbSLois Curfman McInnes - ctx - location to stash Hessian ctx (or PETSC_NULL) 1328fee21e36SBarry Smith 132936851e7fSLois Curfman McInnes Level: advanced 133036851e7fSLois Curfman McInnes 133162fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 1332c7afd0dbSLois Curfman McInnes 1333c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian 133462fef451SLois Curfman McInnes @*/ 133562fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx) 133662fef451SLois Curfman McInnes { 13373a40ed3dSBarry Smith PetscFunctionBegin; 1338*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 13393a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){ 1340a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 13413a40ed3dSBarry Smith } 134262fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 134362fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 134462fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 13453a40ed3dSBarry Smith PetscFunctionReturn(0); 134662fef451SLois Curfman McInnes } 134762fef451SLois Curfman McInnes 13489b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 13499b94acceSBarry Smith 13505615d1e5SSatish Balay #undef __FUNC__ 13515615d1e5SSatish Balay #define __FUNC__ "SNESSetUp" 13529b94acceSBarry Smith /*@ 13539b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1354272ac6f2SLois Curfman McInnes of a nonlinear solver. 13559b94acceSBarry Smith 1356fee21e36SBarry Smith Collective on SNES 1357fee21e36SBarry Smith 1358c7afd0dbSLois Curfman McInnes Input Parameters: 1359c7afd0dbSLois Curfman McInnes + snes - the SNES context 1360c7afd0dbSLois Curfman McInnes - x - the solution vector 1361c7afd0dbSLois Curfman McInnes 1362272ac6f2SLois Curfman McInnes Notes: 1363272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1364272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1365272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1366272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1367272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1368272ac6f2SLois Curfman McInnes 136936851e7fSLois Curfman McInnes Level: advanced 137036851e7fSLois Curfman McInnes 13719b94acceSBarry Smith .keywords: SNES, nonlinear, setup 13729b94acceSBarry Smith 13739b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 13749b94acceSBarry Smith @*/ 13758ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 13769b94acceSBarry Smith { 1377272ac6f2SLois Curfman McInnes int ierr, flg; 13783a40ed3dSBarry Smith 13793a40ed3dSBarry Smith PetscFunctionBegin; 138077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 138177c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1382*184914b5SBarry Smith PetscCheckSameComm(snes,x); 13838ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 13849b94acceSBarry Smith 1385c1f60f51SBarry Smith ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);CHKERRQ(ierr); 1386c1f60f51SBarry Smith /* 1387c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1388dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1389c1f60f51SBarry Smith */ 1390c1f60f51SBarry Smith if (flg) { 1391c1f60f51SBarry Smith Mat J; 13925a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1393c1f60f51SBarry Smith PLogObjectParent(snes,J); 1394c1f60f51SBarry Smith snes->mfshell = J; 1395c1f60f51SBarry Smith snes->jacobian = J; 1396c2aafc4cSSatish Balay if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1397c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n"); 1398d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1399c1f60f51SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n"); 1400d4bb536fSBarry Smith } else { 1401a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option"); 1402d4bb536fSBarry Smith } 14035a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1404c1f60f51SBarry Smith } 1405272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);CHKERRQ(ierr); 1406c1f60f51SBarry Smith /* 1407dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1408c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1409c1f60f51SBarry Smith */ 141031615d04SLois Curfman McInnes if (flg) { 1411272ac6f2SLois Curfman McInnes Mat J; 14125a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1413272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 1414272ac6f2SLois Curfman McInnes snes->mfshell = J; 141583e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 141683e56afdSLois Curfman McInnes ierr = SNESSetJacobian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 141794a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 1418d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 141983e56afdSLois Curfman McInnes ierr = SNESSetHessian(snes,J,J,0,snes->funP);CHKERRQ(ierr); 142094a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 1421d4bb536fSBarry Smith } else { 1422a8c6a408SBarry Smith SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option"); 1423d4bb536fSBarry Smith } 14245a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 1425272ac6f2SLois Curfman McInnes } 14269b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 1427a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 1428a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 14295a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 1430a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 1431a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector"); 1432a8c6a408SBarry Smith } 1433a703fe33SLois Curfman McInnes 1434a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 143582bf6240SBarry Smith if (snes->ksp_ewconv && PetscStrcmp(snes->type_name,SNES_EQ_TR)) { 1436a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 1437a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 1438a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 14395a655dc6SBarry Smith ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,(void *)snes);CHKERRQ(ierr); 1440a703fe33SLois Curfman McInnes } 14419b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 1442a8c6a408SBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1443a8c6a408SBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1444a8c6a408SBarry Smith if (!snes->computeumfunction) { 1445a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first"); 1446a8c6a408SBarry Smith } 14475a655dc6SBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()"); 1448d4bb536fSBarry Smith } else { 1449a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class"); 1450d4bb536fSBarry Smith } 1451a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);} 145282bf6240SBarry Smith snes->setupcalled = 1; 14533a40ed3dSBarry Smith PetscFunctionReturn(0); 14549b94acceSBarry Smith } 14559b94acceSBarry Smith 14565615d1e5SSatish Balay #undef __FUNC__ 1457d4bb536fSBarry Smith #define __FUNC__ "SNESDestroy" 14589b94acceSBarry Smith /*@C 14599b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 14609b94acceSBarry Smith with SNESCreate(). 14619b94acceSBarry Smith 1462c7afd0dbSLois Curfman McInnes Collective on SNES 1463c7afd0dbSLois Curfman McInnes 14649b94acceSBarry Smith Input Parameter: 14659b94acceSBarry Smith . snes - the SNES context 14669b94acceSBarry Smith 146736851e7fSLois Curfman McInnes Level: beginner 146836851e7fSLois Curfman McInnes 14699b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 14709b94acceSBarry Smith 147163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 14729b94acceSBarry Smith @*/ 14739b94acceSBarry Smith int SNESDestroy(SNES snes) 14749b94acceSBarry Smith { 1475b8a78c4aSBarry Smith int i,ierr; 14763a40ed3dSBarry Smith 14773a40ed3dSBarry Smith PetscFunctionBegin; 147877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14793a40ed3dSBarry Smith if (--snes->refct > 0) PetscFunctionReturn(0); 1480d4bb536fSBarry Smith 1481be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 1482be0abb6dSBarry Smith ierr = PetscAMSDestroy(snes);CHKERRQ(ierr); 1483be0abb6dSBarry Smith 1484e1311b90SBarry Smith if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);} 1485606d414cSSatish Balay if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);} 1486522c5e43SBarry Smith if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);} 14879b94acceSBarry Smith ierr = SLESDestroy(snes->sles);CHKERRQ(ierr); 1488522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1489b8a78c4aSBarry Smith for (i=0; i<snes->numbermonitors; i++ ) { 1490b8a78c4aSBarry Smith if (snes->monitordestroy[i]) { 1491b8a78c4aSBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1492b8a78c4aSBarry Smith } 1493b8a78c4aSBarry Smith } 14949b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 14950452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 14963a40ed3dSBarry Smith PetscFunctionReturn(0); 14979b94acceSBarry Smith } 14989b94acceSBarry Smith 14999b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 15009b94acceSBarry Smith 15015615d1e5SSatish Balay #undef __FUNC__ 15025615d1e5SSatish Balay #define __FUNC__ "SNESSetTolerances" 15039b94acceSBarry Smith /*@ 1504d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 15059b94acceSBarry Smith 1506c7afd0dbSLois Curfman McInnes Collective on SNES 1507c7afd0dbSLois Curfman McInnes 15089b94acceSBarry Smith Input Parameters: 1509c7afd0dbSLois Curfman McInnes + snes - the SNES context 151033174efeSLois Curfman McInnes . atol - absolute convergence tolerance 151133174efeSLois Curfman McInnes . rtol - relative convergence tolerance 151233174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 151333174efeSLois Curfman McInnes of the change in the solution between steps 151433174efeSLois Curfman McInnes . maxit - maximum number of iterations 1515c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1516fee21e36SBarry Smith 151733174efeSLois Curfman McInnes Options Database Keys: 1518c7afd0dbSLois Curfman McInnes + -snes_atol <atol> - Sets atol 1519c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1520c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1521c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1522c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15239b94acceSBarry Smith 1524d7a720efSLois Curfman McInnes Notes: 15259b94acceSBarry Smith The default maximum number of iterations is 50. 15269b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15279b94acceSBarry Smith 152836851e7fSLois Curfman McInnes Level: intermediate 152936851e7fSLois Curfman McInnes 153033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15319b94acceSBarry Smith 1532d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 15339b94acceSBarry Smith @*/ 1534d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf) 15359b94acceSBarry Smith { 15363a40ed3dSBarry Smith PetscFunctionBegin; 153777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1538d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1539d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1540d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1541d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1542d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15433a40ed3dSBarry Smith PetscFunctionReturn(0); 15449b94acceSBarry Smith } 15459b94acceSBarry Smith 15465615d1e5SSatish Balay #undef __FUNC__ 15475615d1e5SSatish Balay #define __FUNC__ "SNESGetTolerances" 15489b94acceSBarry Smith /*@ 154933174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 155033174efeSLois Curfman McInnes 1551c7afd0dbSLois Curfman McInnes Not Collective 1552c7afd0dbSLois Curfman McInnes 155333174efeSLois Curfman McInnes Input Parameters: 1554c7afd0dbSLois Curfman McInnes + snes - the SNES context 155533174efeSLois Curfman McInnes . atol - absolute convergence tolerance 155633174efeSLois Curfman McInnes . rtol - relative convergence tolerance 155733174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 155833174efeSLois Curfman McInnes of the change in the solution between steps 155933174efeSLois Curfman McInnes . maxit - maximum number of iterations 1560c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1561fee21e36SBarry Smith 156233174efeSLois Curfman McInnes Notes: 156333174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 156433174efeSLois Curfman McInnes 156536851e7fSLois Curfman McInnes Level: intermediate 156636851e7fSLois Curfman McInnes 156733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 156833174efeSLois Curfman McInnes 156933174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 157033174efeSLois Curfman McInnes @*/ 157133174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf) 157233174efeSLois Curfman McInnes { 15733a40ed3dSBarry Smith PetscFunctionBegin; 157433174efeSLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 157533174efeSLois Curfman McInnes if (atol) *atol = snes->atol; 157633174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 157733174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 157833174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 157933174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 15803a40ed3dSBarry Smith PetscFunctionReturn(0); 158133174efeSLois Curfman McInnes } 158233174efeSLois Curfman McInnes 15835615d1e5SSatish Balay #undef __FUNC__ 15845615d1e5SSatish Balay #define __FUNC__ "SNESSetTrustRegionTolerance" 158533174efeSLois Curfman McInnes /*@ 15869b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 15879b94acceSBarry Smith 1588fee21e36SBarry Smith Collective on SNES 1589fee21e36SBarry Smith 1590c7afd0dbSLois Curfman McInnes Input Parameters: 1591c7afd0dbSLois Curfman McInnes + snes - the SNES context 1592c7afd0dbSLois Curfman McInnes - tol - tolerance 1593c7afd0dbSLois Curfman McInnes 15949b94acceSBarry Smith Options Database Key: 1595c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 15969b94acceSBarry Smith 159736851e7fSLois Curfman McInnes Level: intermediate 159836851e7fSLois Curfman McInnes 15999b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 16009b94acceSBarry Smith 1601d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 16029b94acceSBarry Smith @*/ 16039b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol) 16049b94acceSBarry Smith { 16053a40ed3dSBarry Smith PetscFunctionBegin; 160677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16079b94acceSBarry Smith snes->deltatol = tol; 16083a40ed3dSBarry Smith PetscFunctionReturn(0); 16099b94acceSBarry Smith } 16109b94acceSBarry Smith 16115615d1e5SSatish Balay #undef __FUNC__ 16125615d1e5SSatish Balay #define __FUNC__ "SNESSetMinimizationFunctionTolerance" 16139b94acceSBarry Smith /*@ 161477c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 16159b94acceSBarry Smith for unconstrained minimization solvers. 16169b94acceSBarry Smith 1617fee21e36SBarry Smith Collective on SNES 1618fee21e36SBarry Smith 1619c7afd0dbSLois Curfman McInnes Input Parameters: 1620c7afd0dbSLois Curfman McInnes + snes - the SNES context 1621c7afd0dbSLois Curfman McInnes - ftol - minimum function tolerance 1622c7afd0dbSLois Curfman McInnes 16239b94acceSBarry Smith Options Database Key: 1624c7afd0dbSLois Curfman McInnes . -snes_fmin <ftol> - Sets ftol 16259b94acceSBarry Smith 16269b94acceSBarry Smith Note: 162777c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 16289b94acceSBarry Smith methods only. 16299b94acceSBarry Smith 163036851e7fSLois Curfman McInnes Level: intermediate 163136851e7fSLois Curfman McInnes 16329b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 16339b94acceSBarry Smith 1634d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 16359b94acceSBarry Smith @*/ 163677c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol) 16379b94acceSBarry Smith { 16383a40ed3dSBarry Smith PetscFunctionBegin; 163977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16409b94acceSBarry Smith snes->fmin = ftol; 16413a40ed3dSBarry Smith PetscFunctionReturn(0); 16429b94acceSBarry Smith } 1643df9fa365SBarry Smith /* 1644df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1645df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1646df9fa365SBarry Smith macros instead of functions 1647df9fa365SBarry Smith */ 1648ce1608b8SBarry Smith #undef __FUNC__ 1649ce1608b8SBarry Smith #define __FUNC__ "SNESLGMonitor" 1650ce1608b8SBarry Smith int SNESLGMonitor(SNES snes,int it,double norm,void *ctx) 1651ce1608b8SBarry Smith { 1652ce1608b8SBarry Smith int ierr; 1653ce1608b8SBarry Smith 1654ce1608b8SBarry Smith PetscFunctionBegin; 1655*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1656ce1608b8SBarry Smith ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1657ce1608b8SBarry Smith PetscFunctionReturn(0); 1658ce1608b8SBarry Smith } 1659ce1608b8SBarry Smith 1660df9fa365SBarry Smith #undef __FUNC__ 1661df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorCreate" 1662df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n, DrawLG *draw) 1663df9fa365SBarry Smith { 1664df9fa365SBarry Smith int ierr; 1665df9fa365SBarry Smith 1666df9fa365SBarry Smith PetscFunctionBegin; 1667df9fa365SBarry Smith ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1668df9fa365SBarry Smith PetscFunctionReturn(0); 1669df9fa365SBarry Smith } 1670df9fa365SBarry Smith 1671df9fa365SBarry Smith #undef __FUNC__ 1672df9fa365SBarry Smith #define __FUNC__ "SNESLGMonitorDestroy" 1673df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw) 1674df9fa365SBarry Smith { 1675df9fa365SBarry Smith int ierr; 1676df9fa365SBarry Smith 1677df9fa365SBarry Smith PetscFunctionBegin; 1678df9fa365SBarry Smith ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr); 1679df9fa365SBarry Smith PetscFunctionReturn(0); 1680df9fa365SBarry Smith } 1681df9fa365SBarry Smith 16829b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 16839b94acceSBarry Smith 16845615d1e5SSatish Balay #undef __FUNC__ 1685d4bb536fSBarry Smith #define __FUNC__ "SNESSetMonitor" 16869b94acceSBarry Smith /*@C 16875cd90555SBarry Smith SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every 16889b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 16899b94acceSBarry Smith progress. 16909b94acceSBarry Smith 1691fee21e36SBarry Smith Collective on SNES 1692fee21e36SBarry Smith 1693c7afd0dbSLois Curfman McInnes Input Parameters: 1694c7afd0dbSLois Curfman McInnes + snes - the SNES context 1695c7afd0dbSLois Curfman McInnes . func - monitoring routine 1696b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1697c7afd0dbSLois Curfman McInnes monitor routine (may be PETSC_NULL) 1698b8a78c4aSBarry Smith - monitordestroy - options routine that frees monitor context 16999b94acceSBarry Smith 1700c7afd0dbSLois Curfman McInnes Calling sequence of func: 170140a0c1c6SLois Curfman McInnes $ int func(SNES snes,int its, double norm,void *mctx) 1702c7afd0dbSLois Curfman McInnes 1703c7afd0dbSLois Curfman McInnes + snes - the SNES context 1704c7afd0dbSLois Curfman McInnes . its - iteration number 1705c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 1706c7afd0dbSLois Curfman McInnes for SNES_NONLINEAR_EQUATIONS methods 170740a0c1c6SLois Curfman McInnes . norm - 2-norm gradient value (may be estimated) 1708c7afd0dbSLois Curfman McInnes for SNES_UNCONSTRAINED_MINIMIZATION methods 170940a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 17109b94acceSBarry Smith 17119665c990SLois Curfman McInnes Options Database Keys: 1712c7afd0dbSLois Curfman McInnes + -snes_monitor - sets SNESDefaultMonitor() 1713c7afd0dbSLois Curfman McInnes . -snes_xmonitor - sets line graph monitor, 1714c7afd0dbSLois Curfman McInnes uses SNESLGMonitorCreate() 1715c7afd0dbSLois Curfman McInnes _ -snes_cancelmonitors - cancels all monitors that have 1716c7afd0dbSLois Curfman McInnes been hardwired into a code by 1717c7afd0dbSLois Curfman McInnes calls to SNESSetMonitor(), but 1718c7afd0dbSLois Curfman McInnes does not cancel those set via 1719c7afd0dbSLois Curfman McInnes the options database. 17209665c990SLois Curfman McInnes 1721639f9d9dSBarry Smith Notes: 17226bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 17236bc08f3fSLois Curfman McInnes SNESSetMonitor() multiple times; all will be called in the 17246bc08f3fSLois Curfman McInnes order in which they were set. 1725639f9d9dSBarry Smith 172636851e7fSLois Curfman McInnes Level: intermediate 172736851e7fSLois Curfman McInnes 17289b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 17299b94acceSBarry Smith 17305cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor() 17319b94acceSBarry Smith @*/ 1732b8a78c4aSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx,int (*monitordestroy)(void *)) 17339b94acceSBarry Smith { 17343a40ed3dSBarry Smith PetscFunctionBegin; 1735*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1736639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 1737a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set"); 1738639f9d9dSBarry Smith } 1739639f9d9dSBarry Smith 1740639f9d9dSBarry Smith snes->monitor[snes->numbermonitors] = func; 1741b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1742639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 17433a40ed3dSBarry Smith PetscFunctionReturn(0); 17449b94acceSBarry Smith } 17459b94acceSBarry Smith 17465615d1e5SSatish Balay #undef __FUNC__ 17475cd90555SBarry Smith #define __FUNC__ "SNESClearMonitor" 17485cd90555SBarry Smith /*@C 17495cd90555SBarry Smith SNESClearMonitor - Clears all the monitor functions for a SNES object. 17505cd90555SBarry Smith 1751c7afd0dbSLois Curfman McInnes Collective on SNES 1752c7afd0dbSLois Curfman McInnes 17535cd90555SBarry Smith Input Parameters: 17545cd90555SBarry Smith . snes - the SNES context 17555cd90555SBarry Smith 17565cd90555SBarry Smith Options Database: 1757c7afd0dbSLois Curfman McInnes . -snes_cancelmonitors - cancels all monitors that have been hardwired 1758c7afd0dbSLois Curfman McInnes into a code by calls to SNESSetMonitor(), but does not cancel those 1759c7afd0dbSLois Curfman McInnes set via the options database 17605cd90555SBarry Smith 17615cd90555SBarry Smith Notes: 17625cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 17635cd90555SBarry Smith 176436851e7fSLois Curfman McInnes Level: intermediate 176536851e7fSLois Curfman McInnes 17665cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 17675cd90555SBarry Smith 17685cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor() 17695cd90555SBarry Smith @*/ 17705cd90555SBarry Smith int SNESClearMonitor( SNES snes ) 17715cd90555SBarry Smith { 17725cd90555SBarry Smith PetscFunctionBegin; 1773*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 17745cd90555SBarry Smith snes->numbermonitors = 0; 17755cd90555SBarry Smith PetscFunctionReturn(0); 17765cd90555SBarry Smith } 17775cd90555SBarry Smith 17785cd90555SBarry Smith #undef __FUNC__ 1779d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceTest" 17809b94acceSBarry Smith /*@C 17819b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 17829b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 17839b94acceSBarry Smith 1784fee21e36SBarry Smith Collective on SNES 1785fee21e36SBarry Smith 1786c7afd0dbSLois Curfman McInnes Input Parameters: 1787c7afd0dbSLois Curfman McInnes + snes - the SNES context 1788c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1789c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1790c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 17919b94acceSBarry Smith 1792c7afd0dbSLois Curfman McInnes Calling sequence of func: 1793*184914b5SBarry Smith $ int func (SNES snes,double xnorm,double gnorm,double f,SNESConvergedReason *reason,void *cctx) 1794c7afd0dbSLois Curfman McInnes 1795c7afd0dbSLois Curfman McInnes + snes - the SNES context 1796c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1797*184914b5SBarry Smith . reason - reason for convergence/divergence 1798c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 1799c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods) 1800c7afd0dbSLois Curfman McInnes . f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods) 1801c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods) 1802c7afd0dbSLois Curfman McInnes - f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods) 18039b94acceSBarry Smith 180436851e7fSLois Curfman McInnes Level: advanced 180536851e7fSLois Curfman McInnes 18069b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 18079b94acceSBarry Smith 180840191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 180940191667SLois Curfman McInnes SNESConverged_UM_LS(), SNESConverged_UM_TR() 18109b94acceSBarry Smith @*/ 1811*184914b5SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,SNESConvergedReason*,void*),void *cctx) 18129b94acceSBarry Smith { 18133a40ed3dSBarry Smith PetscFunctionBegin; 1814*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 18159b94acceSBarry Smith (snes)->converged = func; 18169b94acceSBarry Smith (snes)->cnvP = cctx; 18173a40ed3dSBarry Smith PetscFunctionReturn(0); 18189b94acceSBarry Smith } 18199b94acceSBarry Smith 18205615d1e5SSatish Balay #undef __FUNC__ 1821*184914b5SBarry Smith #define __FUNC__ "SNESGetConvergedReason" 1822*184914b5SBarry Smith /*@C 1823*184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1824*184914b5SBarry Smith 1825*184914b5SBarry Smith Not Collective 1826*184914b5SBarry Smith 1827*184914b5SBarry Smith Input Parameter: 1828*184914b5SBarry Smith . snes - the SNES context 1829*184914b5SBarry Smith 1830*184914b5SBarry Smith Output Parameter: 1831*184914b5SBarry Smith . reason - negative value indicates diverged, positive value converged, see snes.h or the 1832*184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1833*184914b5SBarry Smith 1834*184914b5SBarry Smith Level: intermediate 1835*184914b5SBarry Smith 1836*184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1837*184914b5SBarry Smith 1838*184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1839*184914b5SBarry Smith 1840*184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 1841*184914b5SBarry Smith SNESConverged_UM_LS(), SNESConverged_UM_TR() 1842*184914b5SBarry Smith @*/ 1843*184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1844*184914b5SBarry Smith { 1845*184914b5SBarry Smith PetscFunctionBegin; 1846*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1847*184914b5SBarry Smith *reason = snes->reason; 1848*184914b5SBarry Smith PetscFunctionReturn(0); 1849*184914b5SBarry Smith } 1850*184914b5SBarry Smith 1851*184914b5SBarry Smith #undef __FUNC__ 1852d4bb536fSBarry Smith #define __FUNC__ "SNESSetConvergenceHistory" 1853c9005455SLois Curfman McInnes /*@ 1854c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1855c9005455SLois Curfman McInnes 1856fee21e36SBarry Smith Collective on SNES 1857fee21e36SBarry Smith 1858c7afd0dbSLois Curfman McInnes Input Parameters: 1859c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1860c7afd0dbSLois Curfman McInnes . a - array to hold history 1861758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1862758f92a0SBarry Smith negative if not converged) for each solve. 1863758f92a0SBarry Smith . na - size of a and its 1864758f92a0SBarry Smith - reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero, 1865758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1866c7afd0dbSLois Curfman McInnes 1867c9005455SLois Curfman McInnes Notes: 1868c9005455SLois Curfman McInnes If set, this array will contain the function norms (for 1869c9005455SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods) or gradient norms 1870c9005455SLois Curfman McInnes (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed 1871c9005455SLois Curfman McInnes at each step. 1872c9005455SLois Curfman McInnes 1873c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1874c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1875c9005455SLois Curfman McInnes during the section of code that is being timed. 1876c9005455SLois Curfman McInnes 187736851e7fSLois Curfman McInnes Level: intermediate 187836851e7fSLois Curfman McInnes 1879c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1880758f92a0SBarry Smith 188108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1882758f92a0SBarry Smith 1883c9005455SLois Curfman McInnes @*/ 1884758f92a0SBarry Smith int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset) 1885c9005455SLois Curfman McInnes { 18863a40ed3dSBarry Smith PetscFunctionBegin; 1887c9005455SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 1888c9005455SLois Curfman McInnes if (na) PetscValidScalarPointer(a); 1889c9005455SLois Curfman McInnes snes->conv_hist = a; 1890758f92a0SBarry Smith snes->conv_hist_its = its; 1891758f92a0SBarry Smith snes->conv_hist_max = na; 1892758f92a0SBarry Smith snes->conv_hist_reset = reset; 1893758f92a0SBarry Smith PetscFunctionReturn(0); 1894758f92a0SBarry Smith } 1895758f92a0SBarry Smith 1896758f92a0SBarry Smith #undef __FUNC__ 1897758f92a0SBarry Smith #define __FUNC__ "SNESGetConvergenceHistory" 18980c4c9dddSBarry Smith /*@C 1899758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1900758f92a0SBarry Smith 1901758f92a0SBarry Smith Collective on SNES 1902758f92a0SBarry Smith 1903758f92a0SBarry Smith Input Parameter: 1904758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1905758f92a0SBarry Smith 1906758f92a0SBarry Smith Output Parameters: 1907758f92a0SBarry Smith . a - array to hold history 1908758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1909758f92a0SBarry Smith negative if not converged) for each solve. 1910758f92a0SBarry Smith - na - size of a and its 1911758f92a0SBarry Smith 1912758f92a0SBarry Smith Notes: 1913758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1914758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1915758f92a0SBarry Smith 1916758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1917758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1918758f92a0SBarry Smith during the section of code that is being timed. 1919758f92a0SBarry Smith 1920758f92a0SBarry Smith Level: intermediate 1921758f92a0SBarry Smith 1922758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1923758f92a0SBarry Smith 1924758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1925758f92a0SBarry Smith 1926758f92a0SBarry Smith @*/ 1927758f92a0SBarry Smith int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na) 1928758f92a0SBarry Smith { 1929758f92a0SBarry Smith PetscFunctionBegin; 1930758f92a0SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1931758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1932758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1933758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 19343a40ed3dSBarry Smith PetscFunctionReturn(0); 1935c9005455SLois Curfman McInnes } 1936c9005455SLois Curfman McInnes 1937c9005455SLois Curfman McInnes #undef __FUNC__ 19385615d1e5SSatish Balay #define __FUNC__ "SNESScaleStep_Private" 19399b94acceSBarry Smith /* 19409b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 19419b94acceSBarry Smith positive parameter delta. 19429b94acceSBarry Smith 19439b94acceSBarry Smith Input Parameters: 1944c7afd0dbSLois Curfman McInnes + snes - the SNES context 19459b94acceSBarry Smith . y - approximate solution of linear system 19469b94acceSBarry Smith . fnorm - 2-norm of current function 1947c7afd0dbSLois Curfman McInnes - delta - trust region size 19489b94acceSBarry Smith 19499b94acceSBarry Smith Output Parameters: 1950c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 19519b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 19529b94acceSBarry Smith region, and exceeds zero otherwise. 1953c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 19549b94acceSBarry Smith 19559b94acceSBarry Smith Note: 195640191667SLois Curfman McInnes For non-trust region methods such as SNES_EQ_LS, the parameter delta 19579b94acceSBarry Smith is set to be the maximum allowable step size. 19589b94acceSBarry Smith 19599b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 19609b94acceSBarry Smith */ 19619b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 19629b94acceSBarry Smith double *gpnorm,double *ynorm) 19639b94acceSBarry Smith { 19649b94acceSBarry Smith double norm; 19659b94acceSBarry Smith Scalar cnorm; 19663a40ed3dSBarry Smith int ierr; 19673a40ed3dSBarry Smith 19683a40ed3dSBarry Smith PetscFunctionBegin; 1969*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1970*184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1971*184914b5SBarry Smith PetscCheckSameComm(snes,y); 1972*184914b5SBarry Smith 19733a40ed3dSBarry Smith ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr); 19749b94acceSBarry Smith if (norm > *delta) { 19759b94acceSBarry Smith norm = *delta/norm; 19769b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 19779b94acceSBarry Smith cnorm = norm; 19789b94acceSBarry Smith VecScale( &cnorm, y ); 19799b94acceSBarry Smith *ynorm = *delta; 19809b94acceSBarry Smith } else { 19819b94acceSBarry Smith *gpnorm = 0.0; 19829b94acceSBarry Smith *ynorm = norm; 19839b94acceSBarry Smith } 19843a40ed3dSBarry Smith PetscFunctionReturn(0); 19859b94acceSBarry Smith } 19869b94acceSBarry Smith 19875615d1e5SSatish Balay #undef __FUNC__ 19885615d1e5SSatish Balay #define __FUNC__ "SNESSolve" 19899b94acceSBarry Smith /*@ 19909b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 199163a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 19929b94acceSBarry Smith 1993c7afd0dbSLois Curfman McInnes Collective on SNES 1994c7afd0dbSLois Curfman McInnes 1995b2002411SLois Curfman McInnes Input Parameters: 1996c7afd0dbSLois Curfman McInnes + snes - the SNES context 1997c7afd0dbSLois Curfman McInnes - x - the solution vector 19989b94acceSBarry Smith 19999b94acceSBarry Smith Output Parameter: 2000b2002411SLois Curfman McInnes . its - number of iterations until termination 20019b94acceSBarry Smith 2002b2002411SLois Curfman McInnes Notes: 20038ddd3da0SLois Curfman McInnes The user should initialize the vector, x, with the initial guess 20048ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 20058ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 20068ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 20078ddd3da0SLois Curfman McInnes 200836851e7fSLois Curfman McInnes Level: beginner 200936851e7fSLois Curfman McInnes 20109b94acceSBarry Smith .keywords: SNES, nonlinear, solve 20119b94acceSBarry Smith 201263a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 20139b94acceSBarry Smith @*/ 20148ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 20159b94acceSBarry Smith { 20163c7409f5SSatish Balay int ierr, flg; 2017052efed2SBarry Smith 20183a40ed3dSBarry Smith PetscFunctionBegin; 201977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2020*184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 2021*184914b5SBarry Smith PetscCheckSameComm(snes,x); 202274679c65SBarry Smith PetscValidIntPointer(its); 202382bf6240SBarry Smith if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);} 2024c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 2025758f92a0SBarry Smith if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0; 20269b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 2027c96a6f78SLois Curfman McInnes snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0; 20289b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr); 20299b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 20303c7409f5SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg);CHKERRQ(ierr); 20316d4a8577SBarry Smith if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } 20323a40ed3dSBarry Smith PetscFunctionReturn(0); 20339b94acceSBarry Smith } 20349b94acceSBarry Smith 20359b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 20369b94acceSBarry Smith 20375615d1e5SSatish Balay #undef __FUNC__ 20385615d1e5SSatish Balay #define __FUNC__ "SNESSetType" 203982bf6240SBarry Smith /*@C 20404b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 20419b94acceSBarry Smith 2042fee21e36SBarry Smith Collective on SNES 2043fee21e36SBarry Smith 2044c7afd0dbSLois Curfman McInnes Input Parameters: 2045c7afd0dbSLois Curfman McInnes + snes - the SNES context 2046c7afd0dbSLois Curfman McInnes - method - a known method 2047c7afd0dbSLois Curfman McInnes 2048c7afd0dbSLois Curfman McInnes Options Database Key: 2049c7afd0dbSLois Curfman McInnes . -snes_type <method> - Sets the method; use -help for a list 2050c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2051ae12b187SLois Curfman McInnes 20529b94acceSBarry Smith Notes: 20539b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 205436851e7fSLois Curfman McInnes + SNES_EQ_LS - Newton's method with line search 2055c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 2056c7afd0dbSLois Curfman McInnes . SNES_EQ_TR - Newton's method with trust region 2057c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 2058c7afd0dbSLois Curfman McInnes . SNES_UM_TR - Newton's method with trust region 2059c7afd0dbSLois Curfman McInnes (unconstrained minimization) 206036851e7fSLois Curfman McInnes - SNES_UM_LS - Newton's method with line search 2061c7afd0dbSLois Curfman McInnes (unconstrained minimization) 20629b94acceSBarry Smith 2063ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2064ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2065ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2066ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2067ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2068ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2069ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2070ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2071ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 207236851e7fSLois Curfman McInnes appropriate method. In other words, this routine is not for beginners. 207336851e7fSLois Curfman McInnes 207436851e7fSLois Curfman McInnes Level: intermediate 2075a703fe33SLois Curfman McInnes 2076f536c699SSatish Balay .keywords: SNES, set, method 20779b94acceSBarry Smith @*/ 20784b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method) 20799b94acceSBarry Smith { 208084cb2905SBarry Smith int ierr; 20819b94acceSBarry Smith int (*r)(SNES); 20823a40ed3dSBarry Smith 20833a40ed3dSBarry Smith PetscFunctionBegin; 208477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 208582bf6240SBarry Smith 20863f1db9ecSBarry Smith if (PetscTypeCompare(snes->type_name,method)) PetscFunctionReturn(0); 208782bf6240SBarry Smith 208882bf6240SBarry Smith if (snes->setupcalled) { 2089e1311b90SBarry Smith ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr); 209082bf6240SBarry Smith snes->data = 0; 209182bf6240SBarry Smith } 20927d1a2b2bSBarry Smith 20939b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 209482bf6240SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 209582bf6240SBarry Smith 2096488ecbafSBarry Smith ierr = FListFind(snes->comm, SNESList, method,(int (**)(void *)) &r );CHKERRQ(ierr); 209782bf6240SBarry Smith 2098596552b5SBarry Smith if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",method); 20991548b14aSBarry Smith 2100606d414cSSatish Balay if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);} 210182bf6240SBarry Smith snes->data = 0; 21023a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 210382bf6240SBarry Smith 2104be0abb6dSBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,method);CHKERRQ(ierr); 210582bf6240SBarry Smith snes->set_method_called = 1; 210682bf6240SBarry Smith 21073a40ed3dSBarry Smith PetscFunctionReturn(0); 21089b94acceSBarry Smith } 21099b94acceSBarry Smith 2110a847f771SSatish Balay 21119b94acceSBarry Smith /* --------------------------------------------------------------------- */ 21125615d1e5SSatish Balay #undef __FUNC__ 2113d4bb536fSBarry Smith #define __FUNC__ "SNESRegisterDestroy" 21149b94acceSBarry Smith /*@C 21159b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 21169b94acceSBarry Smith registered by SNESRegister(). 21179b94acceSBarry Smith 2118fee21e36SBarry Smith Not Collective 2119fee21e36SBarry Smith 212036851e7fSLois Curfman McInnes Level: advanced 212136851e7fSLois Curfman McInnes 21229b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 21239b94acceSBarry Smith 21249b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 21259b94acceSBarry Smith @*/ 2126cf256101SBarry Smith int SNESRegisterDestroy(void) 21279b94acceSBarry Smith { 212882bf6240SBarry Smith int ierr; 212982bf6240SBarry Smith 21303a40ed3dSBarry Smith PetscFunctionBegin; 213182bf6240SBarry Smith if (SNESList) { 2132488ecbafSBarry Smith ierr = FListDestroy( SNESList );CHKERRQ(ierr); 213382bf6240SBarry Smith SNESList = 0; 21349b94acceSBarry Smith } 213584cb2905SBarry Smith SNESRegisterAllCalled = 0; 21363a40ed3dSBarry Smith PetscFunctionReturn(0); 21379b94acceSBarry Smith } 21389b94acceSBarry Smith 21395615d1e5SSatish Balay #undef __FUNC__ 2140d4bb536fSBarry Smith #define __FUNC__ "SNESGetType" 21419b94acceSBarry Smith /*@C 21429a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 21439b94acceSBarry Smith 2144c7afd0dbSLois Curfman McInnes Not Collective 2145c7afd0dbSLois Curfman McInnes 21469b94acceSBarry Smith Input Parameter: 21474b0e389bSBarry Smith . snes - nonlinear solver context 21489b94acceSBarry Smith 21499b94acceSBarry Smith Output Parameter: 215082bf6240SBarry Smith . method - SNES method (a charactor string) 21519b94acceSBarry Smith 215236851e7fSLois Curfman McInnes Level: intermediate 215336851e7fSLois Curfman McInnes 21549b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name 21559b94acceSBarry Smith @*/ 215682bf6240SBarry Smith int SNESGetType(SNES snes, SNESType *method) 21579b94acceSBarry Smith { 21583a40ed3dSBarry Smith PetscFunctionBegin; 2159*184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 216082bf6240SBarry Smith *method = snes->type_name; 21613a40ed3dSBarry Smith PetscFunctionReturn(0); 21629b94acceSBarry Smith } 21639b94acceSBarry Smith 21645615d1e5SSatish Balay #undef __FUNC__ 2165d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolution" 21669b94acceSBarry Smith /*@C 21679b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 21689b94acceSBarry Smith stored. 21699b94acceSBarry Smith 2170c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2171c7afd0dbSLois Curfman McInnes 21729b94acceSBarry Smith Input Parameter: 21739b94acceSBarry Smith . snes - the SNES context 21749b94acceSBarry Smith 21759b94acceSBarry Smith Output Parameter: 21769b94acceSBarry Smith . x - the solution 21779b94acceSBarry Smith 217836851e7fSLois Curfman McInnes Level: advanced 217936851e7fSLois Curfman McInnes 21809b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 21819b94acceSBarry Smith 2182a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 21839b94acceSBarry Smith @*/ 21849b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 21859b94acceSBarry Smith { 21863a40ed3dSBarry Smith PetscFunctionBegin; 218777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 21889b94acceSBarry Smith *x = snes->vec_sol_always; 21893a40ed3dSBarry Smith PetscFunctionReturn(0); 21909b94acceSBarry Smith } 21919b94acceSBarry Smith 21925615d1e5SSatish Balay #undef __FUNC__ 2193d4bb536fSBarry Smith #define __FUNC__ "SNESGetSolutionUpdate" 21949b94acceSBarry Smith /*@C 21959b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 21969b94acceSBarry Smith stored. 21979b94acceSBarry Smith 2198c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2199c7afd0dbSLois Curfman McInnes 22009b94acceSBarry Smith Input Parameter: 22019b94acceSBarry Smith . snes - the SNES context 22029b94acceSBarry Smith 22039b94acceSBarry Smith Output Parameter: 22049b94acceSBarry Smith . x - the solution update 22059b94acceSBarry Smith 220636851e7fSLois Curfman McInnes Level: advanced 220736851e7fSLois Curfman McInnes 22089b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 22099b94acceSBarry Smith 22109b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 22119b94acceSBarry Smith @*/ 22129b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 22139b94acceSBarry Smith { 22143a40ed3dSBarry Smith PetscFunctionBegin; 221577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22169b94acceSBarry Smith *x = snes->vec_sol_update_always; 22173a40ed3dSBarry Smith PetscFunctionReturn(0); 22189b94acceSBarry Smith } 22199b94acceSBarry Smith 22205615d1e5SSatish Balay #undef __FUNC__ 2221d4bb536fSBarry Smith #define __FUNC__ "SNESGetFunction" 22229b94acceSBarry Smith /*@C 22233638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 22249b94acceSBarry Smith 2225c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2226c7afd0dbSLois Curfman McInnes 22279b94acceSBarry Smith Input Parameter: 22289b94acceSBarry Smith . snes - the SNES context 22299b94acceSBarry Smith 22309b94acceSBarry Smith Output Parameter: 22317bf4e008SBarry Smith + r - the function (or PETSC_NULL) 22327bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 22339b94acceSBarry Smith 22349b94acceSBarry Smith Notes: 22359b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 22369b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 22379b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 22389b94acceSBarry Smith 223936851e7fSLois Curfman McInnes Level: advanced 224036851e7fSLois Curfman McInnes 2241a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 22429b94acceSBarry Smith 224331615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 224431615d04SLois Curfman McInnes SNESGetGradient() 22457bf4e008SBarry Smith 22469b94acceSBarry Smith @*/ 22477bf4e008SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx) 22489b94acceSBarry Smith { 22493a40ed3dSBarry Smith PetscFunctionBegin; 225077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2251a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 2252a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 2253a8c6a408SBarry Smith } 22547bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22557bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22563a40ed3dSBarry Smith PetscFunctionReturn(0); 22579b94acceSBarry Smith } 22589b94acceSBarry Smith 22595615d1e5SSatish Balay #undef __FUNC__ 2260d4bb536fSBarry Smith #define __FUNC__ "SNESGetGradient" 22619b94acceSBarry Smith /*@C 22623638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 22639b94acceSBarry Smith 2264c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2265c7afd0dbSLois Curfman McInnes 22669b94acceSBarry Smith Input Parameter: 22679b94acceSBarry Smith . snes - the SNES context 22689b94acceSBarry Smith 22699b94acceSBarry Smith Output Parameter: 22707bf4e008SBarry Smith + r - the gradient (or PETSC_NULL) 22717bf4e008SBarry Smith - ctx - the gradient context (or PETSC_NULL) 22729b94acceSBarry Smith 22739b94acceSBarry Smith Notes: 22749b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 22759b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 22769b94acceSBarry Smith SNESGetFunction(). 22779b94acceSBarry Smith 227836851e7fSLois Curfman McInnes Level: advanced 227936851e7fSLois Curfman McInnes 22809b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 22819b94acceSBarry Smith 22827bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(), 22837bf4e008SBarry Smith SNESSetGradient(), SNESSetFunction() 22847bf4e008SBarry Smith 22859b94acceSBarry Smith @*/ 22867bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx) 22879b94acceSBarry Smith { 22883a40ed3dSBarry Smith PetscFunctionBegin; 228977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 22903a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2291a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 22923a40ed3dSBarry Smith } 22937bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 22947bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 22953a40ed3dSBarry Smith PetscFunctionReturn(0); 22969b94acceSBarry Smith } 22979b94acceSBarry Smith 22985615d1e5SSatish Balay #undef __FUNC__ 2299d4bb536fSBarry Smith #define __FUNC__ "SNESGetMinimizationFunction" 23007bf4e008SBarry Smith /*@C 23019b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 23029b94acceSBarry Smith unconstrained minimization problems. 23039b94acceSBarry Smith 2304c7afd0dbSLois Curfman McInnes Not Collective 2305c7afd0dbSLois Curfman McInnes 23069b94acceSBarry Smith Input Parameter: 23079b94acceSBarry Smith . snes - the SNES context 23089b94acceSBarry Smith 23099b94acceSBarry Smith Output Parameter: 23107bf4e008SBarry Smith + r - the function (or PETSC_NULL) 23117bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 23129b94acceSBarry Smith 23139b94acceSBarry Smith Notes: 23149b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 23159b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 23169b94acceSBarry Smith SNESGetFunction(). 23179b94acceSBarry Smith 231836851e7fSLois Curfman McInnes Level: advanced 231936851e7fSLois Curfman McInnes 23209b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 23219b94acceSBarry Smith 23227bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction() 23237bf4e008SBarry Smith 23249b94acceSBarry Smith @*/ 23257bf4e008SBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r,void **ctx) 23269b94acceSBarry Smith { 23273a40ed3dSBarry Smith PetscFunctionBegin; 232877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 232974679c65SBarry Smith PetscValidScalarPointer(r); 23303a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2331a8c6a408SBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 23323a40ed3dSBarry Smith } 23337bf4e008SBarry Smith if (r) *r = snes->fc; 23347bf4e008SBarry Smith if (ctx) *ctx = snes->umfunP; 23353a40ed3dSBarry Smith PetscFunctionReturn(0); 23369b94acceSBarry Smith } 23379b94acceSBarry Smith 23385615d1e5SSatish Balay #undef __FUNC__ 2339d4bb536fSBarry Smith #define __FUNC__ "SNESSetOptionsPrefix" 23403c7409f5SSatish Balay /*@C 23413c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2342d850072dSLois Curfman McInnes SNES options in the database. 23433c7409f5SSatish Balay 2344fee21e36SBarry Smith Collective on SNES 2345fee21e36SBarry Smith 2346c7afd0dbSLois Curfman McInnes Input Parameter: 2347c7afd0dbSLois Curfman McInnes + snes - the SNES context 2348c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2349c7afd0dbSLois Curfman McInnes 2350d850072dSLois Curfman McInnes Notes: 2351a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2352c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2353d850072dSLois Curfman McInnes 235436851e7fSLois Curfman McInnes Level: advanced 235536851e7fSLois Curfman McInnes 23563c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2357a86d99e1SLois Curfman McInnes 2358a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 23593c7409f5SSatish Balay @*/ 23603c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 23613c7409f5SSatish Balay { 23623c7409f5SSatish Balay int ierr; 23633c7409f5SSatish Balay 23643a40ed3dSBarry Smith PetscFunctionBegin; 236577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2366639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 23673c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 23683a40ed3dSBarry Smith PetscFunctionReturn(0); 23693c7409f5SSatish Balay } 23703c7409f5SSatish Balay 23715615d1e5SSatish Balay #undef __FUNC__ 2372d4bb536fSBarry Smith #define __FUNC__ "SNESAppendOptionsPrefix" 23733c7409f5SSatish Balay /*@C 2374f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2375d850072dSLois Curfman McInnes SNES options in the database. 23763c7409f5SSatish Balay 2377fee21e36SBarry Smith Collective on SNES 2378fee21e36SBarry Smith 2379c7afd0dbSLois Curfman McInnes Input Parameters: 2380c7afd0dbSLois Curfman McInnes + snes - the SNES context 2381c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2382c7afd0dbSLois Curfman McInnes 2383d850072dSLois Curfman McInnes Notes: 2384a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2385c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2386d850072dSLois Curfman McInnes 238736851e7fSLois Curfman McInnes Level: advanced 238836851e7fSLois Curfman McInnes 23893c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2390a86d99e1SLois Curfman McInnes 2391a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 23923c7409f5SSatish Balay @*/ 23933c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 23943c7409f5SSatish Balay { 23953c7409f5SSatish Balay int ierr; 23963c7409f5SSatish Balay 23973a40ed3dSBarry Smith PetscFunctionBegin; 239877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2399639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 24003c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 24013a40ed3dSBarry Smith PetscFunctionReturn(0); 24023c7409f5SSatish Balay } 24033c7409f5SSatish Balay 24045615d1e5SSatish Balay #undef __FUNC__ 2405d4bb536fSBarry Smith #define __FUNC__ "SNESGetOptionsPrefix" 24069ab63eb5SSatish Balay /*@C 24073c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 24083c7409f5SSatish Balay SNES options in the database. 24093c7409f5SSatish Balay 2410c7afd0dbSLois Curfman McInnes Not Collective 2411c7afd0dbSLois Curfman McInnes 24123c7409f5SSatish Balay Input Parameter: 24133c7409f5SSatish Balay . snes - the SNES context 24143c7409f5SSatish Balay 24153c7409f5SSatish Balay Output Parameter: 24163c7409f5SSatish Balay . prefix - pointer to the prefix string used 24173c7409f5SSatish Balay 24189ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 24199ab63eb5SSatish Balay sufficient length to hold the prefix. 24209ab63eb5SSatish Balay 242136851e7fSLois Curfman McInnes Level: advanced 242236851e7fSLois Curfman McInnes 24233c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2424a86d99e1SLois Curfman McInnes 2425a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 24263c7409f5SSatish Balay @*/ 24273c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 24283c7409f5SSatish Balay { 24293c7409f5SSatish Balay int ierr; 24303c7409f5SSatish Balay 24313a40ed3dSBarry Smith PetscFunctionBegin; 243277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2433639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix);CHKERRQ(ierr); 24343a40ed3dSBarry Smith PetscFunctionReturn(0); 24353c7409f5SSatish Balay } 24363c7409f5SSatish Balay 2437ca161407SBarry Smith #undef __FUNC__ 2438ca161407SBarry Smith #define __FUNC__ "SNESPrintHelp" 2439ca161407SBarry Smith /*@ 2440ca161407SBarry Smith SNESPrintHelp - Prints all options for the SNES component. 2441ca161407SBarry Smith 2442c7afd0dbSLois Curfman McInnes Collective on SNES 2443c7afd0dbSLois Curfman McInnes 2444ca161407SBarry Smith Input Parameter: 2445ca161407SBarry Smith . snes - the SNES context 2446ca161407SBarry Smith 2447ca161407SBarry Smith Options Database Keys: 2448c7afd0dbSLois Curfman McInnes + -help - Prints SNES options 2449c7afd0dbSLois Curfman McInnes - -h - Prints SNES options 2450ca161407SBarry Smith 245136851e7fSLois Curfman McInnes Level: beginner 245236851e7fSLois Curfman McInnes 2453ca161407SBarry Smith .keywords: SNES, nonlinear, help 2454ca161407SBarry Smith 2455ca161407SBarry Smith .seealso: SNESSetFromOptions() 2456ca161407SBarry Smith @*/ 2457ca161407SBarry Smith int SNESPrintHelp(SNES snes) 2458ca161407SBarry Smith { 2459ca161407SBarry Smith char p[64]; 2460ca161407SBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2461ca161407SBarry Smith int ierr; 2462ca161407SBarry Smith 2463ca161407SBarry Smith PetscFunctionBegin; 2464ca161407SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2465ca161407SBarry Smith 2466549d3d68SSatish Balay ierr = PetscStrcpy(p,"-");CHKERRQ(ierr); 2467ca161407SBarry Smith if (snes->prefix) PetscStrcat(p, snes->prefix); 2468ca161407SBarry Smith 2469ca161407SBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2470ca161407SBarry Smith 2471ebb8b11fSBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2472d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n");CHKERRQ(ierr); 2473488ecbafSBarry Smith ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr); 2474d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);CHKERRQ(ierr); 2475d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);CHKERRQ(ierr); 2476d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);CHKERRQ(ierr); 2477d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);CHKERRQ(ierr); 2478d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);CHKERRQ(ierr); 2479d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);CHKERRQ(ierr); 2480d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);CHKERRQ(ierr); 2481d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n");CHKERRQ(ierr); 2482d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p);CHKERRQ(ierr); 2483d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_monitor: use default SNES convergence monitor, prints\n\ 2484d132466eSBarry Smith residual norm at each iteration.\n",p);CHKERRQ(ierr); 2485d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_smonitor: same as the above, but prints fewer digits of the\n\ 2486ca161407SBarry Smith residual norm for small residual norms. This is useful to conceal\n\ 2487d132466eSBarry Smith meaningless digits that may be different on different machines.\n",p);CHKERRQ(ierr); 2488d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);CHKERRQ(ierr); 2489d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor: plots solution at each iteration \n",p);CHKERRQ(ierr); 2490d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_vecmonitor_update: plots update to solution at each iteration \n",p);CHKERRQ(ierr); 2491ca161407SBarry Smith if (snes->type == SNES_NONLINEAR_EQUATIONS) { 2492d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2493d132466eSBarry Smith " Options for solving systems of nonlinear equations only:\n");CHKERRQ(ierr); 2494d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p);CHKERRQ(ierr); 2495d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p);CHKERRQ(ierr); 2496d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);CHKERRQ(ierr); 2497d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf_ksp_monitor - if using matrix-free multiply then prints h at each KSP iteration\n",p);CHKERRQ(ierr); 2498d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p);CHKERRQ(ierr); 2499d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);CHKERRQ(ierr); 2500d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2501d132466eSBarry Smith " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);CHKERRQ(ierr); 2502d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2503d132466eSBarry Smith " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);CHKERRQ(ierr); 2504d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2505d132466eSBarry Smith " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);CHKERRQ(ierr); 2506d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2507d132466eSBarry Smith " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);CHKERRQ(ierr); 2508d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2509d132466eSBarry Smith " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);CHKERRQ(ierr); 2510d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2511d132466eSBarry Smith " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);CHKERRQ(ierr); 2512d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm, 2513d132466eSBarry Smith " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);CHKERRQ(ierr); 2514ca161407SBarry Smith } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 2515d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Options for solving unconstrained minimization problems only:\n");CHKERRQ(ierr); 2516d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);CHKERRQ(ierr); 2517d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p);CHKERRQ(ierr); 2518d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p);CHKERRQ(ierr); 2519ca161407SBarry Smith } 2520d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <method> for help on ",p);CHKERRQ(ierr); 2521d132466eSBarry Smith ierr = (*PetscHelpPrintf)(snes->comm,"a particular method\n");CHKERRQ(ierr); 2522ca161407SBarry Smith if (snes->printhelp) { 2523ca161407SBarry Smith ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr); 2524ca161407SBarry Smith } 2525ca161407SBarry Smith PetscFunctionReturn(0); 2526ca161407SBarry Smith } 25273c7409f5SSatish Balay 2528acb85ae6SSatish Balay /*MC 2529b2002411SLois Curfman McInnes SNESRegister - Adds a method to the nonlinear solver package. 25309b94acceSBarry Smith 2531b2002411SLois Curfman McInnes Synopsis: 2532b2002411SLois Curfman McInnes SNESRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES)) 25339b94acceSBarry Smith 25348d76a1e5SLois Curfman McInnes Not collective 25358d76a1e5SLois Curfman McInnes 2536b2002411SLois Curfman McInnes Input Parameters: 2537c7afd0dbSLois Curfman McInnes + name_solver - name of a new user-defined solver 2538b2002411SLois Curfman McInnes . path - path (either absolute or relative) the library containing this solver 2539b2002411SLois Curfman McInnes . name_create - name of routine to create method context 2540c7afd0dbSLois Curfman McInnes - routine_create - routine to create method context 25419b94acceSBarry Smith 2542b2002411SLois Curfman McInnes Notes: 2543b2002411SLois Curfman McInnes SNESRegister() may be called multiple times to add several user-defined solvers. 25443a40ed3dSBarry Smith 2545b2002411SLois Curfman McInnes If dynamic libraries are used, then the fourth input argument (routine_create) 2546b2002411SLois Curfman McInnes is ignored. 2547b2002411SLois Curfman McInnes 2548b2002411SLois Curfman McInnes Sample usage: 254969225d78SLois Curfman McInnes .vb 2550b2002411SLois Curfman McInnes SNESRegister("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a, 2551b2002411SLois Curfman McInnes "MySolverCreate",MySolverCreate); 255269225d78SLois Curfman McInnes .ve 2553b2002411SLois Curfman McInnes 2554b2002411SLois Curfman McInnes Then, your solver can be chosen with the procedural interface via 2555b2002411SLois Curfman McInnes $ SNESSetType(snes,"my_solver") 2556b2002411SLois Curfman McInnes or at runtime via the option 2557b2002411SLois Curfman McInnes $ -snes_type my_solver 2558b2002411SLois Curfman McInnes 255936851e7fSLois Curfman McInnes Level: advanced 256036851e7fSLois Curfman McInnes 256185614651SBarry Smith $PETSC_ARCH, $PETSC_DIR, $PETSC_LDIR, and $BOPT occuring in pathname will be replaced with appropriate values. 2562dd438238SBarry Smith 2563b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register 2564b2002411SLois Curfman McInnes 2565b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy() 2566b2002411SLois Curfman McInnes M*/ 2567b2002411SLois Curfman McInnes 2568b2002411SLois Curfman McInnes #undef __FUNC__ 2569b2002411SLois Curfman McInnes #define __FUNC__ "SNESRegister_Private" 2570b2002411SLois Curfman McInnes int SNESRegister_Private(char *sname,char *path,char *name,int (*function)(SNES)) 2571b2002411SLois Curfman McInnes { 2572b2002411SLois Curfman McInnes char fullname[256]; 2573b2002411SLois Curfman McInnes int ierr; 2574b2002411SLois Curfman McInnes 2575b2002411SLois Curfman McInnes PetscFunctionBegin; 2576549d3d68SSatish Balay ierr = PetscStrcpy(fullname,path);CHKERRQ(ierr); 2577*184914b5SBarry Smith ierr = PetscStrcat(fullname,":");CHKERRQ(ierr); 2578*184914b5SBarry Smith ierr = PetscStrcat(fullname,name);CHKERRQ(ierr); 2579488ecbafSBarry Smith ierr = FListAdd_Private(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr); 2580b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2581b2002411SLois Curfman McInnes } 2582