173f4d377SMatthew Knepley /*$Id: snes.c,v 1.235 2001/08/21 21:03:49 bsmith Exp $*/ 29b94acceSBarry Smith 3e090d566SSatish Balay #include "src/snes/snesimpl.h" /*I "petscsnes.h" I*/ 49b94acceSBarry Smith 54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE; 68ba1e511SMatthew Knepley PetscFList SNESList = PETSC_NULL; 78ba1e511SMatthew Knepley 88ba1e511SMatthew Knepley /* Logging support */ 98ba1e511SMatthew Knepley int SNES_COOKIE; 108ba1e511SMatthew Knepley int MATSNESMFCTX_COOKIE; 11d5ba7fb7SMatthew Knepley int SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval, SNES_MinimizationFunctionEval, SNES_GradientEval; 12d5ba7fb7SMatthew Knepley int SNES_HessianEval; 1382bf6240SBarry Smith 144a2ae208SSatish Balay #undef __FUNCT__ 15a09944afSBarry Smith #define __FUNCT__ "SNESGetProblemType" 16a09944afSBarry Smith /*@C 17a09944afSBarry Smith SNESGetProblemType -Indicates if SNES is solving a nonlinear system or a minimization 18a09944afSBarry Smith 19a09944afSBarry Smith Not Collective 20a09944afSBarry Smith 21a09944afSBarry Smith Input Parameter: 22a09944afSBarry Smith . SNES - the SNES context 23a09944afSBarry Smith 24a09944afSBarry Smith Output Parameter: 25a09944afSBarry Smith . type - SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations) 26a09944afSBarry Smith or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization) 27a09944afSBarry Smith 28a09944afSBarry Smith Level: intermediate 29a09944afSBarry Smith 30a09944afSBarry Smith .keywords: SNES, problem type 31a09944afSBarry Smith 32a09944afSBarry Smith .seealso: SNESCreate() 33a09944afSBarry Smith @*/ 34a09944afSBarry Smith int SNESGetProblemType(SNES snes,SNESProblemType *type) 35a09944afSBarry Smith { 36a09944afSBarry Smith PetscFunctionBegin; 37a09944afSBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 38a09944afSBarry Smith *type = snes->method_class; 39a09944afSBarry Smith PetscFunctionReturn(0); 40a09944afSBarry Smith } 41a09944afSBarry Smith 42a09944afSBarry Smith #undef __FUNCT__ 434a2ae208SSatish Balay #define __FUNCT__ "SNESView" 447e2c5f70SBarry Smith /*@C 459b94acceSBarry Smith SNESView - Prints the SNES data structure. 469b94acceSBarry Smith 474c49b128SBarry Smith Collective on SNES 48fee21e36SBarry Smith 49c7afd0dbSLois Curfman McInnes Input Parameters: 50c7afd0dbSLois Curfman McInnes + SNES - the SNES context 51c7afd0dbSLois Curfman McInnes - viewer - visualization context 52c7afd0dbSLois Curfman McInnes 539b94acceSBarry Smith Options Database Key: 54c8a8ba5cSLois Curfman McInnes . -snes_view - Calls SNESView() at end of SNESSolve() 559b94acceSBarry Smith 569b94acceSBarry Smith Notes: 579b94acceSBarry Smith The available visualization contexts include 58b0a32e0cSBarry Smith + PETSC_VIEWER_STDOUT_SELF - standard output (default) 59b0a32e0cSBarry Smith - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 60c8a8ba5cSLois Curfman McInnes output where only the first processor opens 61c8a8ba5cSLois Curfman McInnes the file. All other processors send their 62c8a8ba5cSLois Curfman McInnes data to the first processor to print. 639b94acceSBarry Smith 643e081fefSLois Curfman McInnes The user can open an alternative visualization context with 65b0a32e0cSBarry Smith PetscViewerASCIIOpen() - output to a specified file. 669b94acceSBarry Smith 6736851e7fSLois Curfman McInnes Level: beginner 6836851e7fSLois Curfman McInnes 699b94acceSBarry Smith .keywords: SNES, view 709b94acceSBarry Smith 71b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen() 729b94acceSBarry Smith @*/ 73b0a32e0cSBarry Smith int SNESView(SNES snes,PetscViewer viewer) 749b94acceSBarry Smith { 759b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 769b94acceSBarry Smith int ierr; 779b94acceSBarry Smith SLES sles; 78454a90a3SBarry Smith char *type; 796831982aSBarry Smith PetscTruth isascii,isstring; 809b94acceSBarry Smith 813a40ed3dSBarry Smith PetscFunctionBegin; 8274679c65SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 83b0a32e0cSBarry Smith if (!viewer) viewer = PETSC_VIEWER_STDOUT_(snes->comm); 84b0a32e0cSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE); 856831982aSBarry Smith PetscCheckSameComm(snes,viewer); 8674679c65SBarry Smith 87b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 88b0a32e0cSBarry Smith ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr); 890f5bd95cSBarry Smith if (isascii) { 903a7fca6bSBarry Smith if (snes->prefix) { 913a7fca6bSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr); 923a7fca6bSBarry Smith } else { 93b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr); 943a7fca6bSBarry Smith } 95454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 96454a90a3SBarry Smith if (type) { 97b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," type: %s\n",type);CHKERRQ(ierr); 98184914b5SBarry Smith } else { 99b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," type: not set yet\n");CHKERRQ(ierr); 100184914b5SBarry Smith } 1010ef38995SBarry Smith if (snes->view) { 102b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1030ef38995SBarry Smith ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr); 104b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1050ef38995SBarry Smith } 106b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr); 10777d8c4bbSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%g, absolute=%g, solution=%g\n", 10877d8c4bbSBarry Smith snes->rtol,snes->atol,snes->xtol);CHKERRQ(ierr); 109b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr); 110b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr); 1110ef38995SBarry Smith if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 112b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr); 1130ef38995SBarry Smith } 1149b94acceSBarry Smith if (snes->ksp_ewconv) { 1159b94acceSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 1169b94acceSBarry Smith if (kctx) { 117b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr); 118b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr); 119b0a32e0cSBarry Smith ierr = PetscViewerASCIIPrintf(viewer," gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr); 1209b94acceSBarry Smith } 1219b94acceSBarry Smith } 1220f5bd95cSBarry Smith } else if (isstring) { 123454a90a3SBarry Smith ierr = SNESGetType(snes,&type);CHKERRQ(ierr); 124b0a32e0cSBarry Smith ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr); 12519bcc07fSBarry Smith } 12677ed5343SBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 127b0a32e0cSBarry Smith ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 1289b94acceSBarry Smith ierr = SLESView(sles,viewer);CHKERRQ(ierr); 129b0a32e0cSBarry Smith ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 1303a40ed3dSBarry Smith PetscFunctionReturn(0); 1319b94acceSBarry Smith } 1329b94acceSBarry Smith 13376b2cf59SMatthew Knepley /* 13476b2cf59SMatthew Knepley We retain a list of functions that also take SNES command 13576b2cf59SMatthew Knepley line options. These are called at the end SNESSetFromOptions() 13676b2cf59SMatthew Knepley */ 13776b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5 13876b2cf59SMatthew Knepley static int numberofsetfromoptions; 13976b2cf59SMatthew Knepley static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 14076b2cf59SMatthew Knepley 141e74ef692SMatthew Knepley #undef __FUNCT__ 142e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker" 14376b2cf59SMatthew Knepley /*@ 14476b2cf59SMatthew Knepley SNESAddOptionsChecker - Adds an additional function to check for SNES options. 14576b2cf59SMatthew Knepley 14676b2cf59SMatthew Knepley Not Collective 14776b2cf59SMatthew Knepley 14876b2cf59SMatthew Knepley Input Parameter: 14976b2cf59SMatthew Knepley . snescheck - function that checks for options 15076b2cf59SMatthew Knepley 15176b2cf59SMatthew Knepley Level: developer 15276b2cf59SMatthew Knepley 15376b2cf59SMatthew Knepley .seealso: SNESSetFromOptions() 15476b2cf59SMatthew Knepley @*/ 15576b2cf59SMatthew Knepley int SNESAddOptionsChecker(int (*snescheck)(SNES)) 15676b2cf59SMatthew Knepley { 15776b2cf59SMatthew Knepley PetscFunctionBegin; 15876b2cf59SMatthew Knepley if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 15976b2cf59SMatthew Knepley SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %d allowed", MAXSETFROMOPTIONS); 16076b2cf59SMatthew Knepley } 16176b2cf59SMatthew Knepley 16276b2cf59SMatthew Knepley othersetfromoptions[numberofsetfromoptions++] = snescheck; 16376b2cf59SMatthew Knepley PetscFunctionReturn(0); 16476b2cf59SMatthew Knepley } 16576b2cf59SMatthew Knepley 1664a2ae208SSatish Balay #undef __FUNCT__ 1674a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions" 1689b94acceSBarry Smith /*@ 169682d7d0cSBarry Smith SNESSetFromOptions - Sets various SNES and SLES parameters from user options. 1709b94acceSBarry Smith 171c7afd0dbSLois Curfman McInnes Collective on SNES 172c7afd0dbSLois Curfman McInnes 1739b94acceSBarry Smith Input Parameter: 1749b94acceSBarry Smith . snes - the SNES context 1759b94acceSBarry Smith 17636851e7fSLois Curfman McInnes Options Database Keys: 1776831982aSBarry Smith + -snes_type <type> - ls, tr, umls, umtr, test 17882738288SBarry Smith . -snes_stol - convergence tolerance in terms of the norm 17982738288SBarry Smith of the change in the solution between steps 180b39c3a46SLois Curfman McInnes . -snes_atol <atol> - absolute tolerance of residual norm 181b39c3a46SLois Curfman McInnes . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 182b39c3a46SLois Curfman McInnes . -snes_max_it <max_it> - maximum number of iterations 183b39c3a46SLois Curfman McInnes . -snes_max_funcs <max_funcs> - maximum number of function evaluations 18450ffb88aSMatthew Knepley . -snes_max_fail <max_fail> - maximum number of failures 185b39c3a46SLois Curfman McInnes . -snes_trtol <trtol> - trust region tolerance 18682738288SBarry Smith . -snes_no_convergence_test - skip convergence test in nonlinear or minimization 18782738288SBarry Smith solver; hence iterations will continue until max_it 1881fbbfb26SLois Curfman McInnes or some other criterion is reached. Saves expense 18982738288SBarry Smith of convergence test 19082738288SBarry Smith . -snes_monitor - prints residual norm at each iteration 191d132466eSBarry Smith . -snes_vecmonitor - plots solution at each iteration 192d132466eSBarry Smith . -snes_vecmonitor_update - plots update to solution at each iteration 19382738288SBarry Smith . -snes_xmonitor - plots residual norm at each iteration 194e24b481bSBarry Smith . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 19536851e7fSLois Curfman McInnes - -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 19682738288SBarry Smith 19782738288SBarry Smith Options Database for Eisenstat-Walker method: 19882738288SBarry Smith + -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence 19982738288SBarry Smith . -snes_ksp_eq_version ver - version of Eisenstat-Walker method 20036851e7fSLois Curfman McInnes . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 20136851e7fSLois Curfman McInnes . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 20236851e7fSLois Curfman McInnes . -snes_ksp_ew_gamma <gamma> - Sets gamma 20336851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha <alpha> - Sets alpha 20436851e7fSLois Curfman McInnes . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 20536851e7fSLois Curfman McInnes - -snes_ksp_ew_threshold <threshold> - Sets threshold 20682738288SBarry Smith 20711ca99fdSLois Curfman McInnes Notes: 20811ca99fdSLois Curfman McInnes To see all options, run your program with the -help option or consult 20911ca99fdSLois Curfman McInnes the users manual. 21083e2fdc7SBarry Smith 21136851e7fSLois Curfman McInnes Level: beginner 21236851e7fSLois Curfman McInnes 2139b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 2149b94acceSBarry Smith 21569ed319cSSatish Balay .seealso: SNESSetOptionsPrefix() 2169b94acceSBarry Smith @*/ 2179b94acceSBarry Smith int SNESSetFromOptions(SNES snes) 2189b94acceSBarry Smith { 2199b94acceSBarry Smith SLES sles; 220186905e3SBarry Smith SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 221f1af5d2fSBarry Smith PetscTruth flg; 22276b2cf59SMatthew Knepley int ierr, i; 223186905e3SBarry Smith char *deft,type[256]; 2249b94acceSBarry Smith 2253a40ed3dSBarry Smith PetscFunctionBegin; 22677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 227ca161407SBarry Smith 228b0a32e0cSBarry Smith ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr); 229186905e3SBarry Smith if (snes->type_name) { 230186905e3SBarry Smith deft = snes->type_name; 231186905e3SBarry Smith } else { 232186905e3SBarry Smith if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 233186905e3SBarry Smith deft = SNESEQLS; 234186905e3SBarry Smith } else { 235186905e3SBarry Smith deft = SNESUMTR; 236d64ed03dSBarry Smith } 237d64ed03dSBarry Smith } 2384bbc92c1SBarry Smith 239186905e3SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 240b0a32e0cSBarry Smith ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr); 241d64ed03dSBarry Smith if (flg) { 242186905e3SBarry Smith ierr = SNESSetType(snes,type);CHKERRQ(ierr); 243186905e3SBarry Smith } else if (!snes->type_name) { 244186905e3SBarry Smith ierr = SNESSetType(snes,deft);CHKERRQ(ierr); 245d64ed03dSBarry Smith } 24693c39befSBarry Smith 24787828ca2SBarry Smith ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr); 24887828ca2SBarry Smith ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->atol,&snes->atol,0);CHKERRQ(ierr); 249186905e3SBarry Smith 25087828ca2SBarry Smith ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr); 251b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr); 252b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr); 25350ffb88aSMatthew Knepley ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr); 25487828ca2SBarry Smith ierr = PetscOptionsReal("-snes_fmin","Minimization function tolerance","SNESSetMinimizationFunctionTolerance",snes->fmin,&snes->fmin,0);CHKERRQ(ierr); 255186905e3SBarry Smith 256b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_ksp_ew_conv","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetParametersEW",&snes->ksp_ewconv);CHKERRQ(ierr); 257186905e3SBarry Smith 258b0a32e0cSBarry Smith ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1 or 2","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr); 25987828ca2SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr); 26087828ca2SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr); 26187828ca2SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr); 26287828ca2SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr); 26387828ca2SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr); 26487828ca2SBarry Smith ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr); 265186905e3SBarry Smith 266b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr); 26793c39befSBarry Smith if (flg) {snes->converged = 0;} 268b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_cancelmonitors","Remove all monitors","SNESClearMonitor",&flg);CHKERRQ(ierr); 2695cd90555SBarry Smith if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);} 270b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_monitor","Monitor norm of function","SNESDefaultMonitor",&flg);CHKERRQ(ierr); 271b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);} 2723a7fca6bSBarry Smith ierr = PetscOptionsName("-snes_ratiomonitor","Monitor norm of function","SNESSetRatioMonitor",&flg);CHKERRQ(ierr); 2733a7fca6bSBarry Smith if (flg) {ierr = SNESSetRatioMonitor(snes);CHKERRQ(ierr);} 274b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_smonitor","Monitor norm of function (fewer digits)","SNESDefaultSMonitor",&flg);CHKERRQ(ierr); 275b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);} 276b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_vecmonitor","Plot solution at each iteration","SNESVecViewMonitor",&flg);CHKERRQ(ierr); 277b8a78c4aSBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);} 278b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_vecmonitor_update","Plot correction at each iteration","SNESVecViewUpdateMonitor",&flg);CHKERRQ(ierr); 2797c922b88SBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewUpdateMonitor,0,0);CHKERRQ(ierr);} 280b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_xmonitor","Plot function norm at each iteration","SNESLGMonitor",&flg);CHKERRQ(ierr); 281186905e3SBarry Smith if (flg) {ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 282e24b481bSBarry Smith 283b0a32e0cSBarry Smith ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr); 2843c7409f5SSatish Balay if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 285186905e3SBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr); 286b0a32e0cSBarry Smith PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 287981c4779SBarry Smith } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 288186905e3SBarry Smith ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr); 289b0a32e0cSBarry Smith PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 2909b94acceSBarry Smith } 291639f9d9dSBarry Smith 29276b2cf59SMatthew Knepley for(i = 0; i < numberofsetfromoptions; i++) { 29376b2cf59SMatthew Knepley ierr = (*othersetfromoptions[i])(snes); CHKERRQ(ierr); 29476b2cf59SMatthew Knepley } 29576b2cf59SMatthew Knepley 296186905e3SBarry Smith if (snes->setfromoptions) { 297186905e3SBarry Smith ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr); 298639f9d9dSBarry Smith } 299639f9d9dSBarry Smith 300b0a32e0cSBarry Smith ierr = PetscOptionsEnd();CHKERRQ(ierr); 3014bbc92c1SBarry Smith 3029b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 3039b94acceSBarry Smith ierr = SLESSetFromOptions(sles);CHKERRQ(ierr); 30493993e2dSLois Curfman McInnes 3053a40ed3dSBarry Smith PetscFunctionReturn(0); 3069b94acceSBarry Smith } 3079b94acceSBarry Smith 308a847f771SSatish Balay 3094a2ae208SSatish Balay #undef __FUNCT__ 3104a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext" 3119b94acceSBarry Smith /*@ 3129b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 3139b94acceSBarry Smith the nonlinear solvers. 3149b94acceSBarry Smith 315fee21e36SBarry Smith Collective on SNES 316fee21e36SBarry Smith 317c7afd0dbSLois Curfman McInnes Input Parameters: 318c7afd0dbSLois Curfman McInnes + snes - the SNES context 319c7afd0dbSLois Curfman McInnes - usrP - optional user context 320c7afd0dbSLois Curfman McInnes 32136851e7fSLois Curfman McInnes Level: intermediate 32236851e7fSLois Curfman McInnes 3239b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 3249b94acceSBarry Smith 3259b94acceSBarry Smith .seealso: SNESGetApplicationContext() 3269b94acceSBarry Smith @*/ 3279b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 3289b94acceSBarry Smith { 3293a40ed3dSBarry Smith PetscFunctionBegin; 33077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3319b94acceSBarry Smith snes->user = usrP; 3323a40ed3dSBarry Smith PetscFunctionReturn(0); 3339b94acceSBarry Smith } 33474679c65SBarry Smith 3354a2ae208SSatish Balay #undef __FUNCT__ 3364a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext" 3379b94acceSBarry Smith /*@C 3389b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 3399b94acceSBarry Smith nonlinear solvers. 3409b94acceSBarry Smith 341c7afd0dbSLois Curfman McInnes Not Collective 342c7afd0dbSLois Curfman McInnes 3439b94acceSBarry Smith Input Parameter: 3449b94acceSBarry Smith . snes - SNES context 3459b94acceSBarry Smith 3469b94acceSBarry Smith Output Parameter: 3479b94acceSBarry Smith . usrP - user context 3489b94acceSBarry Smith 34936851e7fSLois Curfman McInnes Level: intermediate 35036851e7fSLois Curfman McInnes 3519b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 3529b94acceSBarry Smith 3539b94acceSBarry Smith .seealso: SNESSetApplicationContext() 3549b94acceSBarry Smith @*/ 3559b94acceSBarry Smith int SNESGetApplicationContext(SNES snes,void **usrP) 3569b94acceSBarry Smith { 3573a40ed3dSBarry Smith PetscFunctionBegin; 35877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3599b94acceSBarry Smith *usrP = snes->user; 3603a40ed3dSBarry Smith PetscFunctionReturn(0); 3619b94acceSBarry Smith } 36274679c65SBarry Smith 3634a2ae208SSatish Balay #undef __FUNCT__ 3644a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber" 3659b94acceSBarry Smith /*@ 366c8228a4eSBarry Smith SNESGetIterationNumber - Gets the number of nonlinear iterations completed 367c8228a4eSBarry Smith at this time. 3689b94acceSBarry Smith 369c7afd0dbSLois Curfman McInnes Not Collective 370c7afd0dbSLois Curfman McInnes 3719b94acceSBarry Smith Input Parameter: 3729b94acceSBarry Smith . snes - SNES context 3739b94acceSBarry Smith 3749b94acceSBarry Smith Output Parameter: 3759b94acceSBarry Smith . iter - iteration number 3769b94acceSBarry Smith 377c8228a4eSBarry Smith Notes: 378c8228a4eSBarry Smith For example, during the computation of iteration 2 this would return 1. 379c8228a4eSBarry Smith 380c8228a4eSBarry Smith This is useful for using lagged Jacobians (where one does not recompute the 38108405cd6SLois Curfman McInnes Jacobian at each SNES iteration). For example, the code 38208405cd6SLois Curfman McInnes .vb 38308405cd6SLois Curfman McInnes ierr = SNESGetIterationNumber(snes,&it); 38408405cd6SLois Curfman McInnes if (!(it % 2)) { 38508405cd6SLois Curfman McInnes [compute Jacobian here] 38608405cd6SLois Curfman McInnes } 38708405cd6SLois Curfman McInnes .ve 388c8228a4eSBarry Smith can be used in your ComputeJacobian() function to cause the Jacobian to be 38908405cd6SLois Curfman McInnes recomputed every second SNES iteration. 390c8228a4eSBarry Smith 39136851e7fSLois Curfman McInnes Level: intermediate 39236851e7fSLois Curfman McInnes 3939b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 3949b94acceSBarry Smith @*/ 3959b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 3969b94acceSBarry Smith { 3973a40ed3dSBarry Smith PetscFunctionBegin; 39877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 39974679c65SBarry Smith PetscValidIntPointer(iter); 4009b94acceSBarry Smith *iter = snes->iter; 4013a40ed3dSBarry Smith PetscFunctionReturn(0); 4029b94acceSBarry Smith } 40374679c65SBarry Smith 4044a2ae208SSatish Balay #undef __FUNCT__ 4054a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm" 4069b94acceSBarry Smith /*@ 4079b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 4089b94acceSBarry Smith with SNESSSetFunction(). 4099b94acceSBarry Smith 410c7afd0dbSLois Curfman McInnes Collective on SNES 411c7afd0dbSLois Curfman McInnes 4129b94acceSBarry Smith Input Parameter: 4139b94acceSBarry Smith . snes - SNES context 4149b94acceSBarry Smith 4159b94acceSBarry Smith Output Parameter: 4169b94acceSBarry Smith . fnorm - 2-norm of function 4179b94acceSBarry Smith 4189b94acceSBarry Smith Note: 4199b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 420a86d99e1SLois Curfman McInnes A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 421a86d99e1SLois Curfman McInnes SNESGetGradientNorm(). 4229b94acceSBarry Smith 42336851e7fSLois Curfman McInnes Level: intermediate 42436851e7fSLois Curfman McInnes 4259b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 426a86d99e1SLois Curfman McInnes 42708405cd6SLois Curfman McInnes .seealso: SNESGetFunction() 4289b94acceSBarry Smith @*/ 42987828ca2SBarry Smith int SNESGetFunctionNorm(SNES snes,PetscScalar *fnorm) 4309b94acceSBarry Smith { 4313a40ed3dSBarry Smith PetscFunctionBegin; 43277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 43374679c65SBarry Smith PetscValidScalarPointer(fnorm); 43474679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 43529bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"For SNES_NONLINEAR_EQUATIONS only"); 43674679c65SBarry Smith } 4379b94acceSBarry Smith *fnorm = snes->norm; 4383a40ed3dSBarry Smith PetscFunctionReturn(0); 4399b94acceSBarry Smith } 44074679c65SBarry Smith 4414a2ae208SSatish Balay #undef __FUNCT__ 4424a2ae208SSatish Balay #define __FUNCT__ "SNESGetGradientNorm" 4439b94acceSBarry Smith /*@ 4449b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 4459b94acceSBarry Smith with SNESSSetGradient(). 4469b94acceSBarry Smith 447c7afd0dbSLois Curfman McInnes Collective on SNES 448c7afd0dbSLois Curfman McInnes 4499b94acceSBarry Smith Input Parameter: 4509b94acceSBarry Smith . snes - SNES context 4519b94acceSBarry Smith 4529b94acceSBarry Smith Output Parameter: 4539b94acceSBarry Smith . fnorm - 2-norm of gradient 4549b94acceSBarry Smith 4559b94acceSBarry Smith Note: 4569b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 457a86d99e1SLois Curfman McInnes methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 458a86d99e1SLois Curfman McInnes is SNESGetFunctionNorm(). 4599b94acceSBarry Smith 46036851e7fSLois Curfman McInnes Level: intermediate 46136851e7fSLois Curfman McInnes 4629b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 463a86d99e1SLois Curfman McInnes 464a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient() 4659b94acceSBarry Smith @*/ 46687828ca2SBarry Smith int SNESGetGradientNorm(SNES snes,PetscScalar *gnorm) 4679b94acceSBarry Smith { 4683a40ed3dSBarry Smith PetscFunctionBegin; 46977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 47074679c65SBarry Smith PetscValidScalarPointer(gnorm); 47174679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 47229bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 47374679c65SBarry Smith } 4749b94acceSBarry Smith *gnorm = snes->norm; 4753a40ed3dSBarry Smith PetscFunctionReturn(0); 4769b94acceSBarry Smith } 47774679c65SBarry Smith 4784a2ae208SSatish Balay #undef __FUNCT__ 4794a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps" 4809b94acceSBarry Smith /*@ 4819b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 4829b94acceSBarry Smith attempted by the nonlinear solver. 4839b94acceSBarry Smith 484c7afd0dbSLois Curfman McInnes Not Collective 485c7afd0dbSLois Curfman McInnes 4869b94acceSBarry Smith Input Parameter: 4879b94acceSBarry Smith . snes - SNES context 4889b94acceSBarry Smith 4899b94acceSBarry Smith Output Parameter: 4909b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 4919b94acceSBarry Smith 492c96a6f78SLois Curfman McInnes Notes: 493c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 494c96a6f78SLois Curfman McInnes 49536851e7fSLois Curfman McInnes Level: intermediate 49636851e7fSLois Curfman McInnes 4979b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 4989b94acceSBarry Smith @*/ 4999b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 5009b94acceSBarry Smith { 5013a40ed3dSBarry Smith PetscFunctionBegin; 50277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 50374679c65SBarry Smith PetscValidIntPointer(nfails); 50450ffb88aSMatthew Knepley *nfails = snes->numFailures; 50550ffb88aSMatthew Knepley PetscFunctionReturn(0); 50650ffb88aSMatthew Knepley } 50750ffb88aSMatthew Knepley 50850ffb88aSMatthew Knepley #undef __FUNCT__ 50950ffb88aSMatthew Knepley #define __FUNCT__ "SNESSetMaximumUnsuccessfulSteps" 51050ffb88aSMatthew Knepley /*@ 51150ffb88aSMatthew Knepley SNESSetMaximumUnsuccessfulSteps - Sets the maximum number of unsuccessful steps 51250ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 51350ffb88aSMatthew Knepley 51450ffb88aSMatthew Knepley Not Collective 51550ffb88aSMatthew Knepley 51650ffb88aSMatthew Knepley Input Parameters: 51750ffb88aSMatthew Knepley + snes - SNES context 51850ffb88aSMatthew Knepley - maxFails - maximum of unsuccessful steps 51950ffb88aSMatthew Knepley 52050ffb88aSMatthew Knepley Level: intermediate 52150ffb88aSMatthew Knepley 52250ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps 52350ffb88aSMatthew Knepley @*/ 52450ffb88aSMatthew Knepley int SNESSetMaximumUnsuccessfulSteps(SNES snes, int maxFails) 52550ffb88aSMatthew Knepley { 52650ffb88aSMatthew Knepley PetscFunctionBegin; 52750ffb88aSMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE); 52850ffb88aSMatthew Knepley snes->maxFailures = maxFails; 52950ffb88aSMatthew Knepley PetscFunctionReturn(0); 53050ffb88aSMatthew Knepley } 53150ffb88aSMatthew Knepley 53250ffb88aSMatthew Knepley #undef __FUNCT__ 53350ffb88aSMatthew Knepley #define __FUNCT__ "SNESGetMaximumUnsuccessfulSteps" 53450ffb88aSMatthew Knepley /*@ 53550ffb88aSMatthew Knepley SNESGetMaximumUnsuccessfulSteps - Gets the maximum number of unsuccessful steps 53650ffb88aSMatthew Knepley attempted by the nonlinear solver before it gives up. 53750ffb88aSMatthew Knepley 53850ffb88aSMatthew Knepley Not Collective 53950ffb88aSMatthew Knepley 54050ffb88aSMatthew Knepley Input Parameter: 54150ffb88aSMatthew Knepley . snes - SNES context 54250ffb88aSMatthew Knepley 54350ffb88aSMatthew Knepley Output Parameter: 54450ffb88aSMatthew Knepley . maxFails - maximum of unsuccessful steps 54550ffb88aSMatthew Knepley 54650ffb88aSMatthew Knepley Level: intermediate 54750ffb88aSMatthew Knepley 54850ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps 54950ffb88aSMatthew Knepley @*/ 55050ffb88aSMatthew Knepley int SNESGetMaximumUnsuccessfulSteps(SNES snes, int *maxFails) 55150ffb88aSMatthew Knepley { 55250ffb88aSMatthew Knepley PetscFunctionBegin; 55350ffb88aSMatthew Knepley PetscValidHeaderSpecific(snes,SNES_COOKIE); 55450ffb88aSMatthew Knepley PetscValidIntPointer(maxFails); 55550ffb88aSMatthew Knepley *maxFails = snes->maxFailures; 5563a40ed3dSBarry Smith PetscFunctionReturn(0); 5579b94acceSBarry Smith } 558a847f771SSatish Balay 5594a2ae208SSatish Balay #undef __FUNCT__ 5604a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations" 561c96a6f78SLois Curfman McInnes /*@ 562c96a6f78SLois Curfman McInnes SNESGetNumberLinearIterations - Gets the total number of linear iterations 563c96a6f78SLois Curfman McInnes used by the nonlinear solver. 564c96a6f78SLois Curfman McInnes 565c7afd0dbSLois Curfman McInnes Not Collective 566c7afd0dbSLois Curfman McInnes 567c96a6f78SLois Curfman McInnes Input Parameter: 568c96a6f78SLois Curfman McInnes . snes - SNES context 569c96a6f78SLois Curfman McInnes 570c96a6f78SLois Curfman McInnes Output Parameter: 571c96a6f78SLois Curfman McInnes . lits - number of linear iterations 572c96a6f78SLois Curfman McInnes 573c96a6f78SLois Curfman McInnes Notes: 574c96a6f78SLois Curfman McInnes This counter is reset to zero for each successive call to SNESSolve(). 575c96a6f78SLois Curfman McInnes 57636851e7fSLois Curfman McInnes Level: intermediate 57736851e7fSLois Curfman McInnes 578c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations 579c96a6f78SLois Curfman McInnes @*/ 58086bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits) 581c96a6f78SLois Curfman McInnes { 5823a40ed3dSBarry Smith PetscFunctionBegin; 583c96a6f78SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 584c96a6f78SLois Curfman McInnes PetscValidIntPointer(lits); 585c96a6f78SLois Curfman McInnes *lits = snes->linear_its; 5863a40ed3dSBarry Smith PetscFunctionReturn(0); 587c96a6f78SLois Curfman McInnes } 588c96a6f78SLois Curfman McInnes 5894a2ae208SSatish Balay #undef __FUNCT__ 5904a2ae208SSatish Balay #define __FUNCT__ "SNESGetSLES" 5919b94acceSBarry Smith /*@C 5929b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 5939b94acceSBarry Smith 594c7afd0dbSLois Curfman McInnes Not Collective, but if SNES object is parallel, then SLES object is parallel 595c7afd0dbSLois Curfman McInnes 5969b94acceSBarry Smith Input Parameter: 5979b94acceSBarry Smith . snes - the SNES context 5989b94acceSBarry Smith 5999b94acceSBarry Smith Output Parameter: 6009b94acceSBarry Smith . sles - the SLES context 6019b94acceSBarry Smith 6029b94acceSBarry Smith Notes: 6039b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 6049b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 6059b94acceSBarry Smith KSP and PC contexts as well. 6069b94acceSBarry Smith 60736851e7fSLois Curfman McInnes Level: beginner 60836851e7fSLois Curfman McInnes 6099b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 6109b94acceSBarry Smith 6119b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 6129b94acceSBarry Smith @*/ 6139b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 6149b94acceSBarry Smith { 6153a40ed3dSBarry Smith PetscFunctionBegin; 61677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 6179b94acceSBarry Smith *sles = snes->sles; 6183a40ed3dSBarry Smith PetscFunctionReturn(0); 6199b94acceSBarry Smith } 62082bf6240SBarry Smith 6214a2ae208SSatish Balay #undef __FUNCT__ 6224a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc" 623454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj) 624e24b481bSBarry Smith { 625aa482453SBarry Smith #if defined(PETSC_HAVE_AMS) 626454a90a3SBarry Smith SNES v = (SNES) obj; 627e24b481bSBarry Smith int ierr; 62843d6d2cbSBarry Smith #endif 629e24b481bSBarry Smith 630e24b481bSBarry Smith PetscFunctionBegin; 631e24b481bSBarry Smith 63243d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS) 633e24b481bSBarry Smith /* if it is already published then return */ 634e24b481bSBarry Smith if (v->amem >=0) PetscFunctionReturn(0); 635e24b481bSBarry Smith 636454a90a3SBarry Smith ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr); 637e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ, 638e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 639e24b481bSBarry Smith ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ, 640e24b481bSBarry Smith AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 641454a90a3SBarry Smith ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr); 642433994e6SBarry Smith #endif 643e24b481bSBarry Smith PetscFunctionReturn(0); 644e24b481bSBarry Smith } 645e24b481bSBarry Smith 6469b94acceSBarry Smith /* -----------------------------------------------------------*/ 6474a2ae208SSatish Balay #undef __FUNCT__ 6484a2ae208SSatish Balay #define __FUNCT__ "SNESCreate" 6499b94acceSBarry Smith /*@C 6509b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 6519b94acceSBarry Smith 652c7afd0dbSLois Curfman McInnes Collective on MPI_Comm 653c7afd0dbSLois Curfman McInnes 654c7afd0dbSLois Curfman McInnes Input Parameters: 655c7afd0dbSLois Curfman McInnes + comm - MPI communicator 656c7afd0dbSLois Curfman McInnes - type - type of method, either 657c7afd0dbSLois Curfman McInnes SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations) 658c7afd0dbSLois Curfman McInnes or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization) 6599b94acceSBarry Smith 6609b94acceSBarry Smith Output Parameter: 6619b94acceSBarry Smith . outsnes - the new SNES context 6629b94acceSBarry Smith 663c7afd0dbSLois Curfman McInnes Options Database Keys: 664c7afd0dbSLois Curfman McInnes + -snes_mf - Activates default matrix-free Jacobian-vector products, 665c7afd0dbSLois Curfman McInnes and no preconditioning matrix 666c7afd0dbSLois Curfman McInnes . -snes_mf_operator - Activates default matrix-free Jacobian-vector 667c7afd0dbSLois Curfman McInnes products, and a user-provided preconditioning matrix 668c7afd0dbSLois Curfman McInnes as set by SNESSetJacobian() 669c7afd0dbSLois Curfman McInnes - -snes_fd - Uses (slow!) finite differences to compute Jacobian 670c1f60f51SBarry Smith 67136851e7fSLois Curfman McInnes Level: beginner 67236851e7fSLois Curfman McInnes 6739b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 6749b94acceSBarry Smith 675435da068SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNESProblemType, SNES 6769b94acceSBarry Smith @*/ 6774b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 6789b94acceSBarry Smith { 6799b94acceSBarry Smith int ierr; 6809b94acceSBarry Smith SNES snes; 6819b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 68237fcc0dbSBarry Smith 6833a40ed3dSBarry Smith PetscFunctionBegin; 6848ba1e511SMatthew Knepley PetscValidPointer(outsnes); 6858ba1e511SMatthew Knepley *outsnes = PETSC_NULL; 6868ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 6878ba1e511SMatthew Knepley ierr = SNESInitializePackage(PETSC_NULL); CHKERRQ(ierr); 6888ba1e511SMatthew Knepley #endif 6898ba1e511SMatthew Knepley 690d64ed03dSBarry Smith if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){ 69129bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"incorrect method type"); 692d64ed03dSBarry Smith } 6933f1db9ecSBarry Smith PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView); 694b0a32e0cSBarry Smith PetscLogObjectCreate(snes); 695e24b481bSBarry Smith snes->bops->publish = SNESPublish_Petsc; 6969b94acceSBarry Smith snes->max_its = 50; 6979750a799SBarry Smith snes->max_funcs = 10000; 6989b94acceSBarry Smith snes->norm = 0.0; 6995a2d0531SBarry Smith if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 700b18e04deSLois Curfman McInnes snes->rtol = 1.e-8; 701b18e04deSLois Curfman McInnes snes->ttol = 0.0; 7029b94acceSBarry Smith snes->atol = 1.e-10; 703d64ed03dSBarry Smith } else { 704b4874afaSBarry Smith snes->rtol = 1.e-8; 705b4874afaSBarry Smith snes->ttol = 0.0; 706b4874afaSBarry Smith snes->atol = 1.e-50; 707b4874afaSBarry Smith } 7089b94acceSBarry Smith snes->xtol = 1.e-8; 7099b94acceSBarry Smith snes->nfuncs = 0; 71050ffb88aSMatthew Knepley snes->numFailures = 0; 71150ffb88aSMatthew Knepley snes->maxFailures = 1; 7127a00f4a9SLois Curfman McInnes snes->linear_its = 0; 713639f9d9dSBarry Smith snes->numbermonitors = 0; 7149b94acceSBarry Smith snes->data = 0; 7159b94acceSBarry Smith snes->view = 0; 7169b94acceSBarry Smith snes->computeumfunction = 0; 7179b94acceSBarry Smith snes->umfunP = 0; 7189b94acceSBarry Smith snes->fc = 0; 7199b94acceSBarry Smith snes->deltatol = 1.e-12; 7209b94acceSBarry Smith snes->fmin = -1.e30; 7219b94acceSBarry Smith snes->method_class = type; 7229b94acceSBarry Smith snes->set_method_called = 0; 72382bf6240SBarry Smith snes->setupcalled = 0; 724186905e3SBarry Smith snes->ksp_ewconv = PETSC_FALSE; 7256f24a144SLois Curfman McInnes snes->vwork = 0; 7266f24a144SLois Curfman McInnes snes->nwork = 0; 727758f92a0SBarry Smith snes->conv_hist_len = 0; 728758f92a0SBarry Smith snes->conv_hist_max = 0; 729758f92a0SBarry Smith snes->conv_hist = PETSC_NULL; 730758f92a0SBarry Smith snes->conv_hist_its = PETSC_NULL; 731758f92a0SBarry Smith snes->conv_hist_reset = PETSC_TRUE; 732184914b5SBarry Smith snes->reason = SNES_CONVERGED_ITERATING; 7339b94acceSBarry Smith 7349b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 735b0a32e0cSBarry Smith ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr); 736b0a32e0cSBarry Smith PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx)); 7379b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 7389b94acceSBarry Smith kctx->version = 2; 7399b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 7409b94acceSBarry Smith this was too large for some test cases */ 7419b94acceSBarry Smith kctx->rtol_last = 0; 7429b94acceSBarry Smith kctx->rtol_max = .9; 7439b94acceSBarry Smith kctx->gamma = 1.0; 7449b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 7459b94acceSBarry Smith kctx->alpha = kctx->alpha2; 7469b94acceSBarry Smith kctx->threshold = .1; 7479b94acceSBarry Smith kctx->lresid_last = 0; 7489b94acceSBarry Smith kctx->norm_last = 0; 7499b94acceSBarry Smith 7509b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr); 751b0a32e0cSBarry Smith PetscLogObjectParent(snes,snes->sles) 7525334005bSBarry Smith 7539b94acceSBarry Smith *outsnes = snes; 75400036973SBarry Smith ierr = PetscPublishAll(snes);CHKERRQ(ierr); 7553a40ed3dSBarry Smith PetscFunctionReturn(0); 7569b94acceSBarry Smith } 7579b94acceSBarry Smith 7589b94acceSBarry Smith /* --------------------------------------------------------------- */ 7594a2ae208SSatish Balay #undef __FUNCT__ 7604a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction" 7619b94acceSBarry Smith /*@C 7629b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 7639b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 7649b94acceSBarry Smith equations. 7659b94acceSBarry Smith 766fee21e36SBarry Smith Collective on SNES 767fee21e36SBarry Smith 768c7afd0dbSLois Curfman McInnes Input Parameters: 769c7afd0dbSLois Curfman McInnes + snes - the SNES context 770c7afd0dbSLois Curfman McInnes . func - function evaluation routine 771c7afd0dbSLois Curfman McInnes . r - vector to store function value 772c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 773c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 7749b94acceSBarry Smith 775c7afd0dbSLois Curfman McInnes Calling sequence of func: 7768d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Vec f,void *ctx); 777c7afd0dbSLois Curfman McInnes 778313e4042SLois Curfman McInnes . f - function vector 779c7afd0dbSLois Curfman McInnes - ctx - optional user-defined function context 7809b94acceSBarry Smith 7819b94acceSBarry Smith Notes: 7829b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 7839b94acceSBarry Smith $ f'(x) x = -f(x), 784c7afd0dbSLois Curfman McInnes where f'(x) denotes the Jacobian matrix and f(x) is the function. 7859b94acceSBarry Smith 7869b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 7879b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 7889b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 7899b94acceSBarry Smith 79036851e7fSLois Curfman McInnes Level: beginner 79136851e7fSLois Curfman McInnes 7929b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 7939b94acceSBarry Smith 794a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 7959b94acceSBarry Smith @*/ 7965334005bSBarry Smith int SNESSetFunction(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 7979b94acceSBarry Smith { 7983a40ed3dSBarry Smith PetscFunctionBegin; 79977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 800184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 801184914b5SBarry Smith PetscCheckSameComm(snes,r); 802a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 80329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only"); 804a8c6a408SBarry Smith } 805184914b5SBarry Smith 8069b94acceSBarry Smith snes->computefunction = func; 8079b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 8089b94acceSBarry Smith snes->funP = ctx; 8093a40ed3dSBarry Smith PetscFunctionReturn(0); 8109b94acceSBarry Smith } 8119b94acceSBarry Smith 8124a2ae208SSatish Balay #undef __FUNCT__ 8134a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction" 8149b94acceSBarry Smith /*@ 81536851e7fSLois Curfman McInnes SNESComputeFunction - Calls the function that has been set with 8169b94acceSBarry Smith SNESSetFunction(). 8179b94acceSBarry Smith 818c7afd0dbSLois Curfman McInnes Collective on SNES 819c7afd0dbSLois Curfman McInnes 8209b94acceSBarry Smith Input Parameters: 821c7afd0dbSLois Curfman McInnes + snes - the SNES context 822c7afd0dbSLois Curfman McInnes - x - input vector 8239b94acceSBarry Smith 8249b94acceSBarry Smith Output Parameter: 8253638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 8269b94acceSBarry Smith 8271bffabb2SLois Curfman McInnes Notes: 8289b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 8299b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 8309b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 8319b94acceSBarry Smith 83236851e7fSLois Curfman McInnes SNESComputeFunction() is typically used within nonlinear solvers 83336851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 83436851e7fSLois Curfman McInnes themselves. 83536851e7fSLois Curfman McInnes 83636851e7fSLois Curfman McInnes Level: developer 83736851e7fSLois Curfman McInnes 8389b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 8399b94acceSBarry Smith 840a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 8419b94acceSBarry Smith @*/ 8429b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x,Vec y) 8439b94acceSBarry Smith { 8449b94acceSBarry Smith int ierr; 8459b94acceSBarry Smith 8463a40ed3dSBarry Smith PetscFunctionBegin; 847184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 848184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 849184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 850184914b5SBarry Smith PetscCheckSameComm(snes,x); 851184914b5SBarry Smith PetscCheckSameComm(snes,y); 852d4bb536fSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 85329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only"); 854d4bb536fSBarry Smith } 855184914b5SBarry Smith 856d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 857d64ed03dSBarry Smith PetscStackPush("SNES user function"); 8589b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 859d64ed03dSBarry Smith PetscStackPop; 860ae3c334cSLois Curfman McInnes snes->nfuncs++; 861d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr); 8623a40ed3dSBarry Smith PetscFunctionReturn(0); 8639b94acceSBarry Smith } 8649b94acceSBarry Smith 8654a2ae208SSatish Balay #undef __FUNCT__ 8664a2ae208SSatish Balay #define __FUNCT__ "SNESSetMinimizationFunction" 8679b94acceSBarry Smith /*@C 8689b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 8699b94acceSBarry Smith unconstrained minimization. 8709b94acceSBarry Smith 871fee21e36SBarry Smith Collective on SNES 872fee21e36SBarry Smith 873c7afd0dbSLois Curfman McInnes Input Parameters: 874c7afd0dbSLois Curfman McInnes + snes - the SNES context 875c7afd0dbSLois Curfman McInnes . func - function evaluation routine 876c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 877c7afd0dbSLois Curfman McInnes function evaluation routine (may be PETSC_NULL) 8789b94acceSBarry Smith 879c7afd0dbSLois Curfman McInnes Calling sequence of func: 880329f5518SBarry Smith $ func (SNES snes,Vec x,PetscReal *f,void *ctx); 881c7afd0dbSLois Curfman McInnes 882c7afd0dbSLois Curfman McInnes + x - input vector 8839b94acceSBarry Smith . f - function 884c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined function context 8859b94acceSBarry Smith 88636851e7fSLois Curfman McInnes Level: beginner 88736851e7fSLois Curfman McInnes 8889b94acceSBarry Smith Notes: 8899b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 8909b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 8919b94acceSBarry Smith SNESSetFunction(). 8929b94acceSBarry Smith 8939b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 8949b94acceSBarry Smith 895a86d99e1SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 896a86d99e1SLois Curfman McInnes SNESSetHessian(), SNESSetGradient() 8979b94acceSBarry Smith @*/ 898329f5518SBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,PetscReal*,void*),void *ctx) 8999b94acceSBarry Smith { 9003a40ed3dSBarry Smith PetscFunctionBegin; 90177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 902a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 90329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 904a8c6a408SBarry Smith } 9059b94acceSBarry Smith snes->computeumfunction = func; 9069b94acceSBarry Smith snes->umfunP = ctx; 9073a40ed3dSBarry Smith PetscFunctionReturn(0); 9089b94acceSBarry Smith } 9099b94acceSBarry Smith 9104a2ae208SSatish Balay #undef __FUNCT__ 9114a2ae208SSatish Balay #define __FUNCT__ "SNESComputeMinimizationFunction" 9129b94acceSBarry Smith /*@ 9139b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 9149b94acceSBarry Smith set with SNESSetMinimizationFunction(). 9159b94acceSBarry Smith 916c7afd0dbSLois Curfman McInnes Collective on SNES 917c7afd0dbSLois Curfman McInnes 9189b94acceSBarry Smith Input Parameters: 919c7afd0dbSLois Curfman McInnes + snes - the SNES context 920c7afd0dbSLois Curfman McInnes - x - input vector 9219b94acceSBarry Smith 9229b94acceSBarry Smith Output Parameter: 9239b94acceSBarry Smith . y - function value 9249b94acceSBarry Smith 9259b94acceSBarry Smith Notes: 9269b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 9279b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 9289b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 929a86d99e1SLois Curfman McInnes 93036851e7fSLois Curfman McInnes SNESComputeMinimizationFunction() is typically used within minimization 93136851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 93236851e7fSLois Curfman McInnes themselves. 93336851e7fSLois Curfman McInnes 93436851e7fSLois Curfman McInnes Level: developer 93536851e7fSLois Curfman McInnes 936a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function 937a86d99e1SLois Curfman McInnes 938a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 939a86d99e1SLois Curfman McInnes SNESComputeGradient(), SNESComputeHessian() 9409b94acceSBarry Smith @*/ 941329f5518SBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,PetscReal *y) 9429b94acceSBarry Smith { 9439b94acceSBarry Smith int ierr; 9443a40ed3dSBarry Smith 9453a40ed3dSBarry Smith PetscFunctionBegin; 946184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 947184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 948184914b5SBarry Smith PetscCheckSameComm(snes,x); 949a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 95029bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 951a8c6a408SBarry Smith } 952184914b5SBarry Smith 953d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);CHKERRQ(ierr); 954d64ed03dSBarry Smith PetscStackPush("SNES user minimzation function"); 9559b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr); 956d64ed03dSBarry Smith PetscStackPop; 957ae3c334cSLois Curfman McInnes snes->nfuncs++; 958d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);CHKERRQ(ierr); 9593a40ed3dSBarry Smith PetscFunctionReturn(0); 9609b94acceSBarry Smith } 9619b94acceSBarry Smith 9624a2ae208SSatish Balay #undef __FUNCT__ 9634a2ae208SSatish Balay #define __FUNCT__ "SNESSetGradient" 9649b94acceSBarry Smith /*@C 9659b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 9669b94acceSBarry Smith vector for use by the SNES routines. 9679b94acceSBarry Smith 968c7afd0dbSLois Curfman McInnes Collective on SNES 969c7afd0dbSLois Curfman McInnes 9709b94acceSBarry Smith Input Parameters: 971c7afd0dbSLois Curfman McInnes + snes - the SNES context 9729b94acceSBarry Smith . func - function evaluation routine 973044dda88SLois Curfman McInnes . ctx - optional user-defined context for private data for the 974044dda88SLois Curfman McInnes gradient evaluation routine (may be PETSC_NULL) 975c7afd0dbSLois Curfman McInnes - r - vector to store gradient value 976fee21e36SBarry Smith 9779b94acceSBarry Smith Calling sequence of func: 9788d76a1e5SLois Curfman McInnes $ func (SNES, Vec x, Vec g, void *ctx); 9799b94acceSBarry Smith 980c7afd0dbSLois Curfman McInnes + x - input vector 9819b94acceSBarry Smith . g - gradient vector 982c7afd0dbSLois Curfman McInnes - ctx - optional user-defined gradient context 9839b94acceSBarry Smith 9849b94acceSBarry Smith Notes: 9859b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 9869b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 9879b94acceSBarry Smith SNESSetFunction(). 9889b94acceSBarry Smith 98936851e7fSLois Curfman McInnes Level: beginner 99036851e7fSLois Curfman McInnes 9919b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 9929b94acceSBarry Smith 993a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 994a86d99e1SLois Curfman McInnes SNESSetMinimizationFunction(), 9959b94acceSBarry Smith @*/ 99674679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 9979b94acceSBarry Smith { 9983a40ed3dSBarry Smith PetscFunctionBegin; 99977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1000184914b5SBarry Smith PetscValidHeaderSpecific(r,VEC_COOKIE); 1001184914b5SBarry Smith PetscCheckSameComm(snes,r); 1002a8c6a408SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 100329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1004a8c6a408SBarry Smith } 10059b94acceSBarry Smith snes->computefunction = func; 10069b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 10079b94acceSBarry Smith snes->funP = ctx; 10083a40ed3dSBarry Smith PetscFunctionReturn(0); 10099b94acceSBarry Smith } 10109b94acceSBarry Smith 10114a2ae208SSatish Balay #undef __FUNCT__ 10124a2ae208SSatish Balay #define __FUNCT__ "SNESComputeGradient" 10139b94acceSBarry Smith /*@ 1014a86d99e1SLois Curfman McInnes SNESComputeGradient - Computes the gradient that has been set with 1015a86d99e1SLois Curfman McInnes SNESSetGradient(). 10169b94acceSBarry Smith 1017c7afd0dbSLois Curfman McInnes Collective on SNES 1018c7afd0dbSLois Curfman McInnes 10199b94acceSBarry Smith Input Parameters: 1020c7afd0dbSLois Curfman McInnes + snes - the SNES context 1021c7afd0dbSLois Curfman McInnes - x - input vector 10229b94acceSBarry Smith 10239b94acceSBarry Smith Output Parameter: 10249b94acceSBarry Smith . y - gradient vector 10259b94acceSBarry Smith 10269b94acceSBarry Smith Notes: 10279b94acceSBarry Smith SNESComputeGradient() is valid only for 10289b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 10299b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 1030a86d99e1SLois Curfman McInnes 103136851e7fSLois Curfman McInnes SNESComputeGradient() is typically used within minimization 103236851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 103336851e7fSLois Curfman McInnes themselves. 103436851e7fSLois Curfman McInnes 103536851e7fSLois Curfman McInnes Level: developer 103636851e7fSLois Curfman McInnes 1037a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient 1038a86d99e1SLois Curfman McInnes 1039a86d99e1SLois Curfman McInnes .seealso: SNESSetGradient(), SNESGetGradient(), 1040a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction(), SNESComputeHessian() 10419b94acceSBarry Smith @*/ 10429b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x,Vec y) 10439b94acceSBarry Smith { 10449b94acceSBarry Smith int ierr; 10453a40ed3dSBarry Smith 10463a40ed3dSBarry Smith PetscFunctionBegin; 1047184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1048184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1049184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 1050184914b5SBarry Smith PetscCheckSameComm(snes,x); 1051184914b5SBarry Smith PetscCheckSameComm(snes,y); 10523a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 105329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 10543a40ed3dSBarry Smith } 1055d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_GradientEval,snes,x,y,0);CHKERRQ(ierr); 1056d64ed03dSBarry Smith PetscStackPush("SNES user gradient function"); 10579b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr); 1058d64ed03dSBarry Smith PetscStackPop; 1059d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_GradientEval,snes,x,y,0);CHKERRQ(ierr); 10603a40ed3dSBarry Smith PetscFunctionReturn(0); 10619b94acceSBarry Smith } 10629b94acceSBarry Smith 10634a2ae208SSatish Balay #undef __FUNCT__ 10644a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian" 106562fef451SLois Curfman McInnes /*@ 106662fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 106762fef451SLois Curfman McInnes set with SNESSetJacobian(). 106862fef451SLois Curfman McInnes 1069c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1070c7afd0dbSLois Curfman McInnes 107162fef451SLois Curfman McInnes Input Parameters: 1072c7afd0dbSLois Curfman McInnes + snes - the SNES context 1073c7afd0dbSLois Curfman McInnes - x - input vector 107462fef451SLois Curfman McInnes 107562fef451SLois Curfman McInnes Output Parameters: 1076c7afd0dbSLois Curfman McInnes + A - Jacobian matrix 107762fef451SLois Curfman McInnes . B - optional preconditioning matrix 1078c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1079fee21e36SBarry Smith 108062fef451SLois Curfman McInnes Notes: 108162fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 108262fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 108362fef451SLois Curfman McInnes 1084dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1085dc5a77f8SLois Curfman McInnes flag parameter. 108662fef451SLois Curfman McInnes 108762fef451SLois Curfman McInnes SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 108862fef451SLois Curfman McInnes methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 1089005c665bSBarry Smith methods is SNESComputeHessian(). 109062fef451SLois Curfman McInnes 109136851e7fSLois Curfman McInnes Level: developer 109236851e7fSLois Curfman McInnes 109362fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 109462fef451SLois Curfman McInnes 109562fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 109662fef451SLois Curfman McInnes @*/ 10979b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 10989b94acceSBarry Smith { 10999b94acceSBarry Smith int ierr; 11003a40ed3dSBarry Smith 11013a40ed3dSBarry Smith PetscFunctionBegin; 1102184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1103184914b5SBarry Smith PetscValidHeaderSpecific(X,VEC_COOKIE); 1104184914b5SBarry Smith PetscCheckSameComm(snes,X); 11053a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 110629bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only"); 11073a40ed3dSBarry Smith } 11083a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 1109d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 1110c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 1111d64ed03dSBarry Smith PetscStackPush("SNES user Jacobian function"); 11129b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr); 1113d64ed03dSBarry Smith PetscStackPop; 1114d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr); 11156d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 111677c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 111777c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 11183a40ed3dSBarry Smith PetscFunctionReturn(0); 11199b94acceSBarry Smith } 11209b94acceSBarry Smith 11214a2ae208SSatish Balay #undef __FUNCT__ 11224a2ae208SSatish Balay #define __FUNCT__ "SNESComputeHessian" 112362fef451SLois Curfman McInnes /*@ 112462fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 112562fef451SLois Curfman McInnes set with SNESSetHessian(). 112662fef451SLois Curfman McInnes 1127c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1128c7afd0dbSLois Curfman McInnes 112962fef451SLois Curfman McInnes Input Parameters: 1130c7afd0dbSLois Curfman McInnes + snes - the SNES context 1131c7afd0dbSLois Curfman McInnes - x - input vector 113262fef451SLois Curfman McInnes 113362fef451SLois Curfman McInnes Output Parameters: 1134c7afd0dbSLois Curfman McInnes + A - Hessian matrix 113562fef451SLois Curfman McInnes . B - optional preconditioning matrix 1136c7afd0dbSLois Curfman McInnes - flag - flag indicating matrix structure 1137fee21e36SBarry Smith 113862fef451SLois Curfman McInnes Notes: 113962fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 114062fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 114162fef451SLois Curfman McInnes 1142dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the 1143dc5a77f8SLois Curfman McInnes flag parameter. 114462fef451SLois Curfman McInnes 114562fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 114662fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 114762fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 114862fef451SLois Curfman McInnes 114936851e7fSLois Curfman McInnes SNESComputeHessian() is typically used within minimization 115036851e7fSLois Curfman McInnes implementations, so most users would not generally call this routine 115136851e7fSLois Curfman McInnes themselves. 115236851e7fSLois Curfman McInnes 115336851e7fSLois Curfman McInnes Level: developer 115436851e7fSLois Curfman McInnes 115562fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 115662fef451SLois Curfman McInnes 1157a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 1158a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 115962fef451SLois Curfman McInnes @*/ 116062fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 11619b94acceSBarry Smith { 11629b94acceSBarry Smith int ierr; 11633a40ed3dSBarry Smith 11643a40ed3dSBarry Smith PetscFunctionBegin; 1165184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1166184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1167184914b5SBarry Smith PetscCheckSameComm(snes,x); 11683a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 116929bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 11703a40ed3dSBarry Smith } 11713a40ed3dSBarry Smith if (!snes->computejacobian) PetscFunctionReturn(0); 1172d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_HessianEval,snes,x,*A,*B);CHKERRQ(ierr); 11730f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 1174d64ed03dSBarry Smith PetscStackPush("SNES user Hessian function"); 117562fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr); 1176d64ed03dSBarry Smith PetscStackPop; 1177d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_HessianEval,snes,x,*A,*B);CHKERRQ(ierr); 11780f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 117977c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 118077c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 11813a40ed3dSBarry Smith PetscFunctionReturn(0); 11829b94acceSBarry Smith } 11839b94acceSBarry Smith 11844a2ae208SSatish Balay #undef __FUNCT__ 11854a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian" 11869b94acceSBarry Smith /*@C 11879b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 1188044dda88SLois Curfman McInnes location to store the matrix. 11899b94acceSBarry Smith 1190c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1191c7afd0dbSLois Curfman McInnes 11929b94acceSBarry Smith Input Parameters: 1193c7afd0dbSLois Curfman McInnes + snes - the SNES context 11949b94acceSBarry Smith . A - Jacobian matrix 11959b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 11969b94acceSBarry Smith . func - Jacobian evaluation routine 1197c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 11982cd2dfdcSLois Curfman McInnes Jacobian evaluation routine (may be PETSC_NULL) 11999b94acceSBarry Smith 12009b94acceSBarry Smith Calling sequence of func: 12018d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 12029b94acceSBarry Smith 1203c7afd0dbSLois Curfman McInnes + x - input vector 12049b94acceSBarry Smith . A - Jacobian matrix 12059b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1206ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1207ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1208c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Jacobian context 12099b94acceSBarry Smith 12109b94acceSBarry Smith Notes: 1211dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 12122cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1213ac21db08SLois Curfman McInnes 1214ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 12159b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 12169b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 12179b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 12189b94acceSBarry Smith throughout the global iterations. 12199b94acceSBarry Smith 122036851e7fSLois Curfman McInnes Level: beginner 122136851e7fSLois Curfman McInnes 12229b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 12239b94acceSBarry Smith 1224ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 12259b94acceSBarry Smith @*/ 1226454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 12279b94acceSBarry Smith { 12283a7fca6bSBarry Smith int ierr; 12293a7fca6bSBarry Smith 12303a40ed3dSBarry Smith PetscFunctionBegin; 123177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 123200036973SBarry Smith if (A) PetscValidHeaderSpecific(A,MAT_COOKIE); 123300036973SBarry Smith if (B) PetscValidHeaderSpecific(B,MAT_COOKIE); 123400036973SBarry Smith if (A) PetscCheckSameComm(snes,A); 123500036973SBarry Smith if (B) PetscCheckSameComm(snes,B); 1236a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 123729bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only"); 1238a8c6a408SBarry Smith } 1239184914b5SBarry Smith 12403a7fca6bSBarry Smith if (func) snes->computejacobian = func; 12413a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 12423a7fca6bSBarry Smith if (A) { 12433a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 12449b94acceSBarry Smith snes->jacobian = A; 12453a7fca6bSBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 12463a7fca6bSBarry Smith } 12473a7fca6bSBarry Smith if (B) { 12483a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 12499b94acceSBarry Smith snes->jacobian_pre = B; 12503a7fca6bSBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 12513a7fca6bSBarry Smith } 12523a40ed3dSBarry Smith PetscFunctionReturn(0); 12539b94acceSBarry Smith } 125462fef451SLois Curfman McInnes 12554a2ae208SSatish Balay #undef __FUNCT__ 12564a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian" 1257c2aafc4cSSatish Balay /*@C 1258b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1259b4fd4287SBarry Smith provided context for evaluating the Jacobian. 1260b4fd4287SBarry Smith 1261c7afd0dbSLois Curfman McInnes Not Collective, but Mat object will be parallel if SNES object is 1262c7afd0dbSLois Curfman McInnes 1263b4fd4287SBarry Smith Input Parameter: 1264b4fd4287SBarry Smith . snes - the nonlinear solver context 1265b4fd4287SBarry Smith 1266b4fd4287SBarry Smith Output Parameters: 1267c7afd0dbSLois Curfman McInnes + A - location to stash Jacobian matrix (or PETSC_NULL) 1268b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 126900036973SBarry Smith . ctx - location to stash Jacobian ctx (or PETSC_NULL) 127000036973SBarry Smith - func - location to put Jacobian function (or PETSC_NULL) 1271fee21e36SBarry Smith 127236851e7fSLois Curfman McInnes Level: advanced 127336851e7fSLois Curfman McInnes 1274b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 1275b4fd4287SBarry Smith @*/ 127600036973SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx,int (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*)) 1277b4fd4287SBarry Smith { 12783a40ed3dSBarry Smith PetscFunctionBegin; 1279184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 12803a40ed3dSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 128129bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only"); 12823a40ed3dSBarry Smith } 1283b4fd4287SBarry Smith if (A) *A = snes->jacobian; 1284b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 1285b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 128600036973SBarry Smith if (func) *func = snes->computejacobian; 12873a40ed3dSBarry Smith PetscFunctionReturn(0); 1288b4fd4287SBarry Smith } 1289b4fd4287SBarry Smith 12904a2ae208SSatish Balay #undef __FUNCT__ 12914a2ae208SSatish Balay #define __FUNCT__ "SNESSetHessian" 12929b94acceSBarry Smith /*@C 12939b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 1294044dda88SLois Curfman McInnes location to store the matrix. 12959b94acceSBarry Smith 1296c7afd0dbSLois Curfman McInnes Collective on SNES and Mat 1297c7afd0dbSLois Curfman McInnes 12989b94acceSBarry Smith Input Parameters: 1299c7afd0dbSLois Curfman McInnes + snes - the SNES context 13009b94acceSBarry Smith . A - Hessian matrix 13019b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 13029b94acceSBarry Smith . func - Jacobian evaluation routine 1303c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined context for private data for the 1304313e4042SLois Curfman McInnes Hessian evaluation routine (may be PETSC_NULL) 13059b94acceSBarry Smith 13069b94acceSBarry Smith Calling sequence of func: 13078d76a1e5SLois Curfman McInnes $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 13089b94acceSBarry Smith 1309c7afd0dbSLois Curfman McInnes + x - input vector 13109b94acceSBarry Smith . A - Hessian matrix 13119b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 1312ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 1313ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 1314c7afd0dbSLois Curfman McInnes - ctx - [optional] user-defined Hessian context 13159b94acceSBarry Smith 13169b94acceSBarry Smith Notes: 1317dc5a77f8SLois Curfman McInnes See SLESSetOperators() for important information about setting the flag 13182cd2dfdcSLois Curfman McInnes output parameter in the routine func(). Be sure to read this information! 1319ac21db08SLois Curfman McInnes 13209b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 13219b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 13229b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 13239b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 13249b94acceSBarry Smith throughout the global iterations. 13259b94acceSBarry Smith 132636851e7fSLois Curfman McInnes Level: beginner 132736851e7fSLois Curfman McInnes 13289b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 13299b94acceSBarry Smith 1330ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 13319b94acceSBarry Smith @*/ 1332454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx) 13339b94acceSBarry Smith { 13343a7fca6bSBarry Smith int ierr; 13353a7fca6bSBarry Smith 13363a40ed3dSBarry Smith PetscFunctionBegin; 133777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1338184914b5SBarry Smith PetscValidHeaderSpecific(A,MAT_COOKIE); 1339184914b5SBarry Smith PetscValidHeaderSpecific(B,MAT_COOKIE); 1340184914b5SBarry Smith PetscCheckSameComm(snes,A); 1341184914b5SBarry Smith PetscCheckSameComm(snes,B); 1342d4bb536fSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 134329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1344d4bb536fSBarry Smith } 13453a7fca6bSBarry Smith if (func) snes->computejacobian = func; 13463a7fca6bSBarry Smith if (ctx) snes->jacP = ctx; 13473a7fca6bSBarry Smith if (A) { 13483a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 13499b94acceSBarry Smith snes->jacobian = A; 13503a7fca6bSBarry Smith ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr); 13513a7fca6bSBarry Smith } 13523a7fca6bSBarry Smith if (B) { 13533a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 13549b94acceSBarry Smith snes->jacobian_pre = B; 13553a7fca6bSBarry Smith ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr); 13563a7fca6bSBarry Smith } 13573a40ed3dSBarry Smith PetscFunctionReturn(0); 13589b94acceSBarry Smith } 13599b94acceSBarry Smith 13604a2ae208SSatish Balay #undef __FUNCT__ 13614a2ae208SSatish Balay #define __FUNCT__ "SNESGetHessian" 136262fef451SLois Curfman McInnes /*@ 136362fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 136462fef451SLois Curfman McInnes provided context for evaluating the Hessian. 136562fef451SLois Curfman McInnes 1366c7afd0dbSLois Curfman McInnes Not Collective, but Mat object is parallel if SNES object is parallel 1367c7afd0dbSLois Curfman McInnes 136862fef451SLois Curfman McInnes Input Parameter: 136962fef451SLois Curfman McInnes . snes - the nonlinear solver context 137062fef451SLois Curfman McInnes 137162fef451SLois Curfman McInnes Output Parameters: 1372c7afd0dbSLois Curfman McInnes + A - location to stash Hessian matrix (or PETSC_NULL) 137362fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 1374c7afd0dbSLois Curfman McInnes - ctx - location to stash Hessian ctx (or PETSC_NULL) 1375fee21e36SBarry Smith 137636851e7fSLois Curfman McInnes Level: advanced 137736851e7fSLois Curfman McInnes 137862fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 1379c7afd0dbSLois Curfman McInnes 1380c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian 138162fef451SLois Curfman McInnes @*/ 138262fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B,void **ctx) 138362fef451SLois Curfman McInnes { 13843a40ed3dSBarry Smith PetscFunctionBegin; 1385184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 13863a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){ 138729bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 13883a40ed3dSBarry Smith } 138962fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 139062fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 139162fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 13923a40ed3dSBarry Smith PetscFunctionReturn(0); 139362fef451SLois Curfman McInnes } 139462fef451SLois Curfman McInnes 13959b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 13969b94acceSBarry Smith 13974a2ae208SSatish Balay #undef __FUNCT__ 13984a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp" 13999b94acceSBarry Smith /*@ 14009b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 1401272ac6f2SLois Curfman McInnes of a nonlinear solver. 14029b94acceSBarry Smith 1403fee21e36SBarry Smith Collective on SNES 1404fee21e36SBarry Smith 1405c7afd0dbSLois Curfman McInnes Input Parameters: 1406c7afd0dbSLois Curfman McInnes + snes - the SNES context 1407c7afd0dbSLois Curfman McInnes - x - the solution vector 1408c7afd0dbSLois Curfman McInnes 1409272ac6f2SLois Curfman McInnes Notes: 1410272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 1411272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 1412272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 1413272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 1414272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1415272ac6f2SLois Curfman McInnes 141636851e7fSLois Curfman McInnes Level: advanced 141736851e7fSLois Curfman McInnes 14189b94acceSBarry Smith .keywords: SNES, nonlinear, setup 14199b94acceSBarry Smith 14209b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 14219b94acceSBarry Smith @*/ 14228ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 14239b94acceSBarry Smith { 1424f1af5d2fSBarry Smith int ierr; 1425f1af5d2fSBarry Smith PetscTruth flg; 14263a40ed3dSBarry Smith 14273a40ed3dSBarry Smith PetscFunctionBegin; 142877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 142977c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 1430184914b5SBarry Smith PetscCheckSameComm(snes,x); 14318ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 14329b94acceSBarry Smith 1433b0a32e0cSBarry Smith ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr); 1434c1f60f51SBarry Smith /* 1435c1f60f51SBarry Smith This version replaces the user provided Jacobian matrix with a 1436dfa02198SLois Curfman McInnes matrix-free version but still employs the user-provided preconditioner matrix 1437c1f60f51SBarry Smith */ 1438c1f60f51SBarry Smith if (flg) { 1439c1f60f51SBarry Smith Mat J; 14405a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 14415a655dc6SBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 14423a7fca6bSBarry Smith PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator routines\n"); 14433a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr); 14443a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 1445c1f60f51SBarry Smith } 1446b0a32e0cSBarry Smith ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr); 1447c1f60f51SBarry Smith /* 1448dfa02198SLois Curfman McInnes This version replaces both the user-provided Jacobian and the user- 1449c1f60f51SBarry Smith provided preconditioner matrix with the default matrix free version. 1450c1f60f51SBarry Smith */ 145131615d04SLois Curfman McInnes if (flg) { 1452272ac6f2SLois Curfman McInnes Mat J; 1453f3ef73ceSBarry Smith SLES sles; 1454f3ef73ceSBarry Smith PC pc; 1455f3ef73ceSBarry Smith 14565a655dc6SBarry Smith ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 14573a7fca6bSBarry Smith ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr); 145893b98531SBarry Smith PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator and preconditioner routines\n"); 145983e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 14603a7fca6bSBarry Smith ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr); 1461d64ed03dSBarry Smith } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 14623a7fca6bSBarry Smith ierr = SNESSetHessian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr); 1463d4bb536fSBarry Smith } else { 146429bbc08cSBarry Smith SETERRQ(PETSC_ERR_SUP,"Method class doesn't support matrix-free option"); 1465d4bb536fSBarry Smith } 14663a7fca6bSBarry Smith ierr = MatDestroy(J);CHKERRQ(ierr); 14673a7fca6bSBarry Smith 1468f3ef73ceSBarry Smith /* force no preconditioner */ 1469f3ef73ceSBarry Smith ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 1470f3ef73ceSBarry Smith ierr = SLESGetPC(sles,&pc);CHKERRQ(ierr); 1471f3ef73ceSBarry Smith ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); 1472272ac6f2SLois Curfman McInnes } 1473f3ef73ceSBarry Smith 14749b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 14756831982aSBarry Smith PetscTruth iseqtr; 14766831982aSBarry Smith 147729bbc08cSBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 147829bbc08cSBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first"); 147929bbc08cSBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option"); 1480a8c6a408SBarry Smith if (snes->vec_func == snes->vec_sol) { 148129bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector"); 1482a8c6a408SBarry Smith } 1483a703fe33SLois Curfman McInnes 1484a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 14856831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr); 14866831982aSBarry Smith if (snes->ksp_ewconv && !iseqtr) { 1487a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 1488a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 1489a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 1490186905e3SBarry Smith ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr); 1491a703fe33SLois Curfman McInnes } 14929b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 149329bbc08cSBarry Smith if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetGradient() first"); 149429bbc08cSBarry Smith if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetGradient() first"); 1495a8c6a408SBarry Smith if (!snes->computeumfunction) { 149629bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetMinimizationFunction() first"); 1497a8c6a408SBarry Smith } 149829bbc08cSBarry Smith if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetHessian()"); 1499d4bb536fSBarry Smith } else { 150029bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Unknown method class"); 1501d4bb536fSBarry Smith } 1502a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);} 150382bf6240SBarry Smith snes->setupcalled = 1; 15043a40ed3dSBarry Smith PetscFunctionReturn(0); 15059b94acceSBarry Smith } 15069b94acceSBarry Smith 15074a2ae208SSatish Balay #undef __FUNCT__ 15084a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy" 15099b94acceSBarry Smith /*@C 15109b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 15119b94acceSBarry Smith with SNESCreate(). 15129b94acceSBarry Smith 1513c7afd0dbSLois Curfman McInnes Collective on SNES 1514c7afd0dbSLois Curfman McInnes 15159b94acceSBarry Smith Input Parameter: 15169b94acceSBarry Smith . snes - the SNES context 15179b94acceSBarry Smith 151836851e7fSLois Curfman McInnes Level: beginner 151936851e7fSLois Curfman McInnes 15209b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 15219b94acceSBarry Smith 152263a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 15239b94acceSBarry Smith @*/ 15249b94acceSBarry Smith int SNESDestroy(SNES snes) 15259b94acceSBarry Smith { 1526b8a78c4aSBarry Smith int i,ierr; 15273a40ed3dSBarry Smith 15283a40ed3dSBarry Smith PetscFunctionBegin; 152977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15303a40ed3dSBarry Smith if (--snes->refct > 0) PetscFunctionReturn(0); 1531d4bb536fSBarry Smith 1532be0abb6dSBarry Smith /* if memory was published with AMS then destroy it */ 15330f5bd95cSBarry Smith ierr = PetscObjectDepublish(snes);CHKERRQ(ierr); 1534be0abb6dSBarry Smith 1535e1311b90SBarry Smith if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);} 1536606d414cSSatish Balay if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);} 15373a7fca6bSBarry Smith if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);} 15383a7fca6bSBarry Smith if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);} 15399b94acceSBarry Smith ierr = SLESDestroy(snes->sles);CHKERRQ(ierr); 1540522c5e43SBarry Smith if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1541b8a78c4aSBarry Smith for (i=0; i<snes->numbermonitors; i++) { 1542b8a78c4aSBarry Smith if (snes->monitordestroy[i]) { 1543b8a78c4aSBarry Smith ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr); 1544b8a78c4aSBarry Smith } 1545b8a78c4aSBarry Smith } 1546b0a32e0cSBarry Smith PetscLogObjectDestroy((PetscObject)snes); 15470452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 15483a40ed3dSBarry Smith PetscFunctionReturn(0); 15499b94acceSBarry Smith } 15509b94acceSBarry Smith 15519b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 15529b94acceSBarry Smith 15534a2ae208SSatish Balay #undef __FUNCT__ 15544a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances" 15559b94acceSBarry Smith /*@ 1556d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 15579b94acceSBarry Smith 1558c7afd0dbSLois Curfman McInnes Collective on SNES 1559c7afd0dbSLois Curfman McInnes 15609b94acceSBarry Smith Input Parameters: 1561c7afd0dbSLois Curfman McInnes + snes - the SNES context 156233174efeSLois Curfman McInnes . atol - absolute convergence tolerance 156333174efeSLois Curfman McInnes . rtol - relative convergence tolerance 156433174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 156533174efeSLois Curfman McInnes of the change in the solution between steps 156633174efeSLois Curfman McInnes . maxit - maximum number of iterations 1567c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1568fee21e36SBarry Smith 156933174efeSLois Curfman McInnes Options Database Keys: 1570c7afd0dbSLois Curfman McInnes + -snes_atol <atol> - Sets atol 1571c7afd0dbSLois Curfman McInnes . -snes_rtol <rtol> - Sets rtol 1572c7afd0dbSLois Curfman McInnes . -snes_stol <stol> - Sets stol 1573c7afd0dbSLois Curfman McInnes . -snes_max_it <maxit> - Sets maxit 1574c7afd0dbSLois Curfman McInnes - -snes_max_funcs <maxf> - Sets maxf 15759b94acceSBarry Smith 1576d7a720efSLois Curfman McInnes Notes: 15779b94acceSBarry Smith The default maximum number of iterations is 50. 15789b94acceSBarry Smith The default maximum number of function evaluations is 1000. 15799b94acceSBarry Smith 158036851e7fSLois Curfman McInnes Level: intermediate 158136851e7fSLois Curfman McInnes 158233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances 15839b94acceSBarry Smith 1584d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 15859b94acceSBarry Smith @*/ 1586329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf) 15879b94acceSBarry Smith { 15883a40ed3dSBarry Smith PetscFunctionBegin; 158977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1590d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1591d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1592d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1593d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1594d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 15953a40ed3dSBarry Smith PetscFunctionReturn(0); 15969b94acceSBarry Smith } 15979b94acceSBarry Smith 15984a2ae208SSatish Balay #undef __FUNCT__ 15994a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances" 16009b94acceSBarry Smith /*@ 160133174efeSLois Curfman McInnes SNESGetTolerances - Gets various parameters used in convergence tests. 160233174efeSLois Curfman McInnes 1603c7afd0dbSLois Curfman McInnes Not Collective 1604c7afd0dbSLois Curfman McInnes 160533174efeSLois Curfman McInnes Input Parameters: 1606c7afd0dbSLois Curfman McInnes + snes - the SNES context 160733174efeSLois Curfman McInnes . atol - absolute convergence tolerance 160833174efeSLois Curfman McInnes . rtol - relative convergence tolerance 160933174efeSLois Curfman McInnes . stol - convergence tolerance in terms of the norm 161033174efeSLois Curfman McInnes of the change in the solution between steps 161133174efeSLois Curfman McInnes . maxit - maximum number of iterations 1612c7afd0dbSLois Curfman McInnes - maxf - maximum number of function evaluations 1613fee21e36SBarry Smith 161433174efeSLois Curfman McInnes Notes: 161533174efeSLois Curfman McInnes The user can specify PETSC_NULL for any parameter that is not needed. 161633174efeSLois Curfman McInnes 161736851e7fSLois Curfman McInnes Level: intermediate 161836851e7fSLois Curfman McInnes 161933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances 162033174efeSLois Curfman McInnes 162133174efeSLois Curfman McInnes .seealso: SNESSetTolerances() 162233174efeSLois Curfman McInnes @*/ 1623329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf) 162433174efeSLois Curfman McInnes { 16253a40ed3dSBarry Smith PetscFunctionBegin; 162633174efeSLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 162733174efeSLois Curfman McInnes if (atol) *atol = snes->atol; 162833174efeSLois Curfman McInnes if (rtol) *rtol = snes->rtol; 162933174efeSLois Curfman McInnes if (stol) *stol = snes->xtol; 163033174efeSLois Curfman McInnes if (maxit) *maxit = snes->max_its; 163133174efeSLois Curfman McInnes if (maxf) *maxf = snes->max_funcs; 16323a40ed3dSBarry Smith PetscFunctionReturn(0); 163333174efeSLois Curfman McInnes } 163433174efeSLois Curfman McInnes 16354a2ae208SSatish Balay #undef __FUNCT__ 16364a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance" 163733174efeSLois Curfman McInnes /*@ 16389b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 16399b94acceSBarry Smith 1640fee21e36SBarry Smith Collective on SNES 1641fee21e36SBarry Smith 1642c7afd0dbSLois Curfman McInnes Input Parameters: 1643c7afd0dbSLois Curfman McInnes + snes - the SNES context 1644c7afd0dbSLois Curfman McInnes - tol - tolerance 1645c7afd0dbSLois Curfman McInnes 16469b94acceSBarry Smith Options Database Key: 1647c7afd0dbSLois Curfman McInnes . -snes_trtol <tol> - Sets tol 16489b94acceSBarry Smith 164936851e7fSLois Curfman McInnes Level: intermediate 165036851e7fSLois Curfman McInnes 16519b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 16529b94acceSBarry Smith 1653d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 16549b94acceSBarry Smith @*/ 1655329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol) 16569b94acceSBarry Smith { 16573a40ed3dSBarry Smith PetscFunctionBegin; 165877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16599b94acceSBarry Smith snes->deltatol = tol; 16603a40ed3dSBarry Smith PetscFunctionReturn(0); 16619b94acceSBarry Smith } 16629b94acceSBarry Smith 16634a2ae208SSatish Balay #undef __FUNCT__ 16644a2ae208SSatish Balay #define __FUNCT__ "SNESSetMinimizationFunctionTolerance" 16659b94acceSBarry Smith /*@ 166677c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 16679b94acceSBarry Smith for unconstrained minimization solvers. 16689b94acceSBarry Smith 1669fee21e36SBarry Smith Collective on SNES 1670fee21e36SBarry Smith 1671c7afd0dbSLois Curfman McInnes Input Parameters: 1672c7afd0dbSLois Curfman McInnes + snes - the SNES context 1673c7afd0dbSLois Curfman McInnes - ftol - minimum function tolerance 1674c7afd0dbSLois Curfman McInnes 16759b94acceSBarry Smith Options Database Key: 1676c7afd0dbSLois Curfman McInnes . -snes_fmin <ftol> - Sets ftol 16779b94acceSBarry Smith 16789b94acceSBarry Smith Note: 167977c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 16809b94acceSBarry Smith methods only. 16819b94acceSBarry Smith 168236851e7fSLois Curfman McInnes Level: intermediate 168336851e7fSLois Curfman McInnes 16849b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 16859b94acceSBarry Smith 1686d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 16879b94acceSBarry Smith @*/ 1688329f5518SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,PetscReal ftol) 16899b94acceSBarry Smith { 16903a40ed3dSBarry Smith PetscFunctionBegin; 169177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16929b94acceSBarry Smith snes->fmin = ftol; 16933a40ed3dSBarry Smith PetscFunctionReturn(0); 16949b94acceSBarry Smith } 1695df9fa365SBarry Smith /* 1696df9fa365SBarry Smith Duplicate the lg monitors for SNES from KSP; for some reason with 1697df9fa365SBarry Smith dynamic libraries things don't work under Sun4 if we just use 1698df9fa365SBarry Smith macros instead of functions 1699df9fa365SBarry Smith */ 17004a2ae208SSatish Balay #undef __FUNCT__ 17014a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitor" 1702329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx) 1703ce1608b8SBarry Smith { 1704ce1608b8SBarry Smith int ierr; 1705ce1608b8SBarry Smith 1706ce1608b8SBarry Smith PetscFunctionBegin; 1707184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1708ce1608b8SBarry Smith ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1709ce1608b8SBarry Smith PetscFunctionReturn(0); 1710ce1608b8SBarry Smith } 1711ce1608b8SBarry Smith 17124a2ae208SSatish Balay #undef __FUNCT__ 17134a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorCreate" 1714b0a32e0cSBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,PetscDrawLG *draw) 1715df9fa365SBarry Smith { 1716df9fa365SBarry Smith int ierr; 1717df9fa365SBarry Smith 1718df9fa365SBarry Smith PetscFunctionBegin; 1719df9fa365SBarry Smith ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr); 1720df9fa365SBarry Smith PetscFunctionReturn(0); 1721df9fa365SBarry Smith } 1722df9fa365SBarry Smith 17234a2ae208SSatish Balay #undef __FUNCT__ 17244a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorDestroy" 1725b0a32e0cSBarry Smith int SNESLGMonitorDestroy(PetscDrawLG draw) 1726df9fa365SBarry Smith { 1727df9fa365SBarry Smith int ierr; 1728df9fa365SBarry Smith 1729df9fa365SBarry Smith PetscFunctionBegin; 1730df9fa365SBarry Smith ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr); 1731df9fa365SBarry Smith PetscFunctionReturn(0); 1732df9fa365SBarry Smith } 1733df9fa365SBarry Smith 17349b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 17359b94acceSBarry Smith 17364a2ae208SSatish Balay #undef __FUNCT__ 17374a2ae208SSatish Balay #define __FUNCT__ "SNESSetMonitor" 17389b94acceSBarry Smith /*@C 17395cd90555SBarry Smith SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every 17409b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 17419b94acceSBarry Smith progress. 17429b94acceSBarry Smith 1743fee21e36SBarry Smith Collective on SNES 1744fee21e36SBarry Smith 1745c7afd0dbSLois Curfman McInnes Input Parameters: 1746c7afd0dbSLois Curfman McInnes + snes - the SNES context 1747c7afd0dbSLois Curfman McInnes . func - monitoring routine 1748b8a78c4aSBarry Smith . mctx - [optional] user-defined context for private data for the 1749b3006f0bSLois Curfman McInnes monitor routine (use PETSC_NULL if no context is desitre) 1750b3006f0bSLois Curfman McInnes - monitordestroy - [optional] routine that frees monitor context 1751b3006f0bSLois Curfman McInnes (may be PETSC_NULL) 17529b94acceSBarry Smith 1753c7afd0dbSLois Curfman McInnes Calling sequence of func: 1754329f5518SBarry Smith $ int func(SNES snes,int its, PetscReal norm,void *mctx) 1755c7afd0dbSLois Curfman McInnes 1756c7afd0dbSLois Curfman McInnes + snes - the SNES context 1757c7afd0dbSLois Curfman McInnes . its - iteration number 1758c7afd0dbSLois Curfman McInnes . norm - 2-norm function value (may be estimated) 1759c7afd0dbSLois Curfman McInnes for SNES_NONLINEAR_EQUATIONS methods 176040a0c1c6SLois Curfman McInnes . norm - 2-norm gradient value (may be estimated) 1761c7afd0dbSLois Curfman McInnes for SNES_UNCONSTRAINED_MINIMIZATION methods 176240a0c1c6SLois Curfman McInnes - mctx - [optional] monitoring context 17639b94acceSBarry Smith 17649665c990SLois Curfman McInnes Options Database Keys: 1765c7afd0dbSLois Curfman McInnes + -snes_monitor - sets SNESDefaultMonitor() 1766c7afd0dbSLois Curfman McInnes . -snes_xmonitor - sets line graph monitor, 1767c7afd0dbSLois Curfman McInnes uses SNESLGMonitorCreate() 1768c7afd0dbSLois Curfman McInnes _ -snes_cancelmonitors - cancels all monitors that have 1769c7afd0dbSLois Curfman McInnes been hardwired into a code by 1770c7afd0dbSLois Curfman McInnes calls to SNESSetMonitor(), but 1771c7afd0dbSLois Curfman McInnes does not cancel those set via 1772c7afd0dbSLois Curfman McInnes the options database. 17739665c990SLois Curfman McInnes 1774639f9d9dSBarry Smith Notes: 17756bc08f3fSLois Curfman McInnes Several different monitoring routines may be set by calling 17766bc08f3fSLois Curfman McInnes SNESSetMonitor() multiple times; all will be called in the 17776bc08f3fSLois Curfman McInnes order in which they were set. 1778639f9d9dSBarry Smith 177936851e7fSLois Curfman McInnes Level: intermediate 178036851e7fSLois Curfman McInnes 17819b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 17829b94acceSBarry Smith 17835cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor() 17849b94acceSBarry Smith @*/ 1785329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *)) 17869b94acceSBarry Smith { 17873a40ed3dSBarry Smith PetscFunctionBegin; 1788184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1789639f9d9dSBarry Smith if (snes->numbermonitors >= MAXSNESMONITORS) { 179029bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set"); 1791639f9d9dSBarry Smith } 1792639f9d9dSBarry Smith 1793639f9d9dSBarry Smith snes->monitor[snes->numbermonitors] = func; 1794b8a78c4aSBarry Smith snes->monitordestroy[snes->numbermonitors] = monitordestroy; 1795639f9d9dSBarry Smith snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 17963a40ed3dSBarry Smith PetscFunctionReturn(0); 17979b94acceSBarry Smith } 17989b94acceSBarry Smith 17994a2ae208SSatish Balay #undef __FUNCT__ 18004a2ae208SSatish Balay #define __FUNCT__ "SNESClearMonitor" 18015cd90555SBarry Smith /*@C 18025cd90555SBarry Smith SNESClearMonitor - Clears all the monitor functions for a SNES object. 18035cd90555SBarry Smith 1804c7afd0dbSLois Curfman McInnes Collective on SNES 1805c7afd0dbSLois Curfman McInnes 18065cd90555SBarry Smith Input Parameters: 18075cd90555SBarry Smith . snes - the SNES context 18085cd90555SBarry Smith 18095cd90555SBarry Smith Options Database: 1810c7afd0dbSLois Curfman McInnes . -snes_cancelmonitors - cancels all monitors that have been hardwired 1811c7afd0dbSLois Curfman McInnes into a code by calls to SNESSetMonitor(), but does not cancel those 1812c7afd0dbSLois Curfman McInnes set via the options database 18135cd90555SBarry Smith 18145cd90555SBarry Smith Notes: 18155cd90555SBarry Smith There is no way to clear one specific monitor from a SNES object. 18165cd90555SBarry Smith 181736851e7fSLois Curfman McInnes Level: intermediate 181836851e7fSLois Curfman McInnes 18195cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor 18205cd90555SBarry Smith 18215cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor() 18225cd90555SBarry Smith @*/ 18235cd90555SBarry Smith int SNESClearMonitor(SNES snes) 18245cd90555SBarry Smith { 18255cd90555SBarry Smith PetscFunctionBegin; 1826184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 18275cd90555SBarry Smith snes->numbermonitors = 0; 18285cd90555SBarry Smith PetscFunctionReturn(0); 18295cd90555SBarry Smith } 18305cd90555SBarry Smith 18314a2ae208SSatish Balay #undef __FUNCT__ 18324a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest" 18339b94acceSBarry Smith /*@C 18349b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 18359b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 18369b94acceSBarry Smith 1837fee21e36SBarry Smith Collective on SNES 1838fee21e36SBarry Smith 1839c7afd0dbSLois Curfman McInnes Input Parameters: 1840c7afd0dbSLois Curfman McInnes + snes - the SNES context 1841c7afd0dbSLois Curfman McInnes . func - routine to test for convergence 1842c7afd0dbSLois Curfman McInnes - cctx - [optional] context for private data for the convergence routine 1843c7afd0dbSLois Curfman McInnes (may be PETSC_NULL) 18449b94acceSBarry Smith 1845c7afd0dbSLois Curfman McInnes Calling sequence of func: 1846329f5518SBarry Smith $ int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx) 1847c7afd0dbSLois Curfman McInnes 1848c7afd0dbSLois Curfman McInnes + snes - the SNES context 1849c7afd0dbSLois Curfman McInnes . cctx - [optional] convergence context 1850184914b5SBarry Smith . reason - reason for convergence/divergence 1851c7afd0dbSLois Curfman McInnes . xnorm - 2-norm of current iterate 1852c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods) 1853c7afd0dbSLois Curfman McInnes . f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods) 1854c7afd0dbSLois Curfman McInnes . gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods) 1855c7afd0dbSLois Curfman McInnes - f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods) 18569b94acceSBarry Smith 185736851e7fSLois Curfman McInnes Level: advanced 185836851e7fSLois Curfman McInnes 18599b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 18609b94acceSBarry Smith 186140191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 186240191667SLois Curfman McInnes SNESConverged_UM_LS(), SNESConverged_UM_TR() 18639b94acceSBarry Smith @*/ 1864329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx) 18659b94acceSBarry Smith { 18663a40ed3dSBarry Smith PetscFunctionBegin; 1867184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 18689b94acceSBarry Smith (snes)->converged = func; 18699b94acceSBarry Smith (snes)->cnvP = cctx; 18703a40ed3dSBarry Smith PetscFunctionReturn(0); 18719b94acceSBarry Smith } 18729b94acceSBarry Smith 18734a2ae208SSatish Balay #undef __FUNCT__ 18744a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason" 1875184914b5SBarry Smith /*@C 1876184914b5SBarry Smith SNESGetConvergedReason - Gets the reason the SNES iteration was stopped. 1877184914b5SBarry Smith 1878184914b5SBarry Smith Not Collective 1879184914b5SBarry Smith 1880184914b5SBarry Smith Input Parameter: 1881184914b5SBarry Smith . snes - the SNES context 1882184914b5SBarry Smith 1883184914b5SBarry Smith Output Parameter: 1884e090d566SSatish Balay . reason - negative value indicates diverged, positive value converged, see petscsnes.h or the 1885184914b5SBarry Smith manual pages for the individual convergence tests for complete lists 1886184914b5SBarry Smith 1887184914b5SBarry Smith Level: intermediate 1888184914b5SBarry Smith 1889184914b5SBarry Smith Notes: Can only be called after the call the SNESSolve() is complete. 1890184914b5SBarry Smith 1891184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1892184914b5SBarry Smith 1893184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 1894435da068SBarry Smith SNESConverged_UM_LS(), SNESConverged_UM_TR(), SNESConvergedReason 1895184914b5SBarry Smith @*/ 1896184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason) 1897184914b5SBarry Smith { 1898184914b5SBarry Smith PetscFunctionBegin; 1899184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1900184914b5SBarry Smith *reason = snes->reason; 1901184914b5SBarry Smith PetscFunctionReturn(0); 1902184914b5SBarry Smith } 1903184914b5SBarry Smith 19044a2ae208SSatish Balay #undef __FUNCT__ 19054a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory" 1906c9005455SLois Curfman McInnes /*@ 1907c9005455SLois Curfman McInnes SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1908c9005455SLois Curfman McInnes 1909fee21e36SBarry Smith Collective on SNES 1910fee21e36SBarry Smith 1911c7afd0dbSLois Curfman McInnes Input Parameters: 1912c7afd0dbSLois Curfman McInnes + snes - iterative context obtained from SNESCreate() 1913c7afd0dbSLois Curfman McInnes . a - array to hold history 1914cd5578b5SBarry Smith . its - integer array holds the number of linear iterations for each solve. 1915758f92a0SBarry Smith . na - size of a and its 191664731454SLois Curfman McInnes - reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero, 1917758f92a0SBarry Smith else it continues storing new values for new nonlinear solves after the old ones 1918c7afd0dbSLois Curfman McInnes 1919c9005455SLois Curfman McInnes Notes: 1920c9005455SLois Curfman McInnes If set, this array will contain the function norms (for 1921c9005455SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods) or gradient norms 1922c9005455SLois Curfman McInnes (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed 1923c9005455SLois Curfman McInnes at each step. 1924c9005455SLois Curfman McInnes 1925c9005455SLois Curfman McInnes This routine is useful, e.g., when running a code for purposes 1926c9005455SLois Curfman McInnes of accurate performance monitoring, when no I/O should be done 1927c9005455SLois Curfman McInnes during the section of code that is being timed. 1928c9005455SLois Curfman McInnes 192936851e7fSLois Curfman McInnes Level: intermediate 193036851e7fSLois Curfman McInnes 1931c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history 1932758f92a0SBarry Smith 193308405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory() 1934758f92a0SBarry Smith 1935c9005455SLois Curfman McInnes @*/ 1936329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset) 1937c9005455SLois Curfman McInnes { 19383a40ed3dSBarry Smith PetscFunctionBegin; 1939c9005455SLois Curfman McInnes PetscValidHeaderSpecific(snes,SNES_COOKIE); 1940c9005455SLois Curfman McInnes if (na) PetscValidScalarPointer(a); 1941c9005455SLois Curfman McInnes snes->conv_hist = a; 1942758f92a0SBarry Smith snes->conv_hist_its = its; 1943758f92a0SBarry Smith snes->conv_hist_max = na; 1944758f92a0SBarry Smith snes->conv_hist_reset = reset; 1945758f92a0SBarry Smith PetscFunctionReturn(0); 1946758f92a0SBarry Smith } 1947758f92a0SBarry Smith 19484a2ae208SSatish Balay #undef __FUNCT__ 19494a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory" 19500c4c9dddSBarry Smith /*@C 1951758f92a0SBarry Smith SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1952758f92a0SBarry Smith 1953758f92a0SBarry Smith Collective on SNES 1954758f92a0SBarry Smith 1955758f92a0SBarry Smith Input Parameter: 1956758f92a0SBarry Smith . snes - iterative context obtained from SNESCreate() 1957758f92a0SBarry Smith 1958758f92a0SBarry Smith Output Parameters: 1959758f92a0SBarry Smith . a - array to hold history 1960758f92a0SBarry Smith . its - integer array holds the number of linear iterations (or 1961758f92a0SBarry Smith negative if not converged) for each solve. 1962758f92a0SBarry Smith - na - size of a and its 1963758f92a0SBarry Smith 1964758f92a0SBarry Smith Notes: 1965758f92a0SBarry Smith The calling sequence for this routine in Fortran is 1966758f92a0SBarry Smith $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1967758f92a0SBarry Smith 1968758f92a0SBarry Smith This routine is useful, e.g., when running a code for purposes 1969758f92a0SBarry Smith of accurate performance monitoring, when no I/O should be done 1970758f92a0SBarry Smith during the section of code that is being timed. 1971758f92a0SBarry Smith 1972758f92a0SBarry Smith Level: intermediate 1973758f92a0SBarry Smith 1974758f92a0SBarry Smith .keywords: SNES, get, convergence, history 1975758f92a0SBarry Smith 1976758f92a0SBarry Smith .seealso: SNESSetConvergencHistory() 1977758f92a0SBarry Smith 1978758f92a0SBarry Smith @*/ 1979329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na) 1980758f92a0SBarry Smith { 1981758f92a0SBarry Smith PetscFunctionBegin; 1982758f92a0SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1983758f92a0SBarry Smith if (a) *a = snes->conv_hist; 1984758f92a0SBarry Smith if (its) *its = snes->conv_hist_its; 1985758f92a0SBarry Smith if (na) *na = snes->conv_hist_len; 19863a40ed3dSBarry Smith PetscFunctionReturn(0); 1987c9005455SLois Curfman McInnes } 1988c9005455SLois Curfman McInnes 1989e74ef692SMatthew Knepley #undef __FUNCT__ 1990e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetRhsBC" 199176b2cf59SMatthew Knepley /*@ 199276b2cf59SMatthew Knepley SNESSetRhsBC - Sets the function which applies boundary conditions 199376b2cf59SMatthew Knepley to the Rhs of each system. 199476b2cf59SMatthew Knepley 199576b2cf59SMatthew Knepley Collective on SNES 199676b2cf59SMatthew Knepley 199776b2cf59SMatthew Knepley Input Parameters: 199876b2cf59SMatthew Knepley . snes - The nonlinear solver context 199976b2cf59SMatthew Knepley . func - The function 200076b2cf59SMatthew Knepley 200176b2cf59SMatthew Knepley Calling sequence of func: 200276b2cf59SMatthew Knepley . func (SNES snes, Vec rhs, void *ctx); 200376b2cf59SMatthew Knepley 200476b2cf59SMatthew Knepley . rhs - The current rhs vector 200576b2cf59SMatthew Knepley . ctx - The user-context 200676b2cf59SMatthew Knepley 200776b2cf59SMatthew Knepley Level: intermediate 200876b2cf59SMatthew Knepley 200976b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions 201076b2cf59SMatthew Knepley .seealso SNESDefaultRhsBC(), SNESSetSolutionBC(), SNESSetUpdate() 201176b2cf59SMatthew Knepley @*/ 201276b2cf59SMatthew Knepley int SNESSetRhsBC(SNES snes, int (*func)(SNES, Vec, void *)) 201376b2cf59SMatthew Knepley { 201476b2cf59SMatthew Knepley PetscFunctionBegin; 201576b2cf59SMatthew Knepley PetscValidHeaderSpecific(snes, SNES_COOKIE); 201676b2cf59SMatthew Knepley snes->applyrhsbc = func; 201776b2cf59SMatthew Knepley PetscFunctionReturn(0); 201876b2cf59SMatthew Knepley } 201976b2cf59SMatthew Knepley 2020e74ef692SMatthew Knepley #undef __FUNCT__ 2021e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultRhsBC" 202276b2cf59SMatthew Knepley /*@ 202376b2cf59SMatthew Knepley SNESDefaultRhsBC - The default boundary condition function which does nothing. 202476b2cf59SMatthew Knepley 202576b2cf59SMatthew Knepley Not collective 202676b2cf59SMatthew Knepley 202776b2cf59SMatthew Knepley Input Parameters: 202876b2cf59SMatthew Knepley . snes - The nonlinear solver context 202976b2cf59SMatthew Knepley . rhs - The Rhs 203076b2cf59SMatthew Knepley . ctx - The user-context 203176b2cf59SMatthew Knepley 203276b2cf59SMatthew Knepley Level: beginner 203376b2cf59SMatthew Knepley 203476b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions 203576b2cf59SMatthew Knepley .seealso SNESSetRhsBC(), SNESDefaultSolutionBC(), SNESDefaultUpdate() 203676b2cf59SMatthew Knepley @*/ 203776b2cf59SMatthew Knepley int SNESDefaultRhsBC(SNES snes, Vec rhs, void *ctx) 203876b2cf59SMatthew Knepley { 203976b2cf59SMatthew Knepley PetscFunctionBegin; 204076b2cf59SMatthew Knepley PetscFunctionReturn(0); 204176b2cf59SMatthew Knepley } 204276b2cf59SMatthew Knepley 2043e74ef692SMatthew Knepley #undef __FUNCT__ 2044e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetSolutionBC" 204576b2cf59SMatthew Knepley /*@ 204676b2cf59SMatthew Knepley SNESSetSolutionBC - Sets the function which applies boundary conditions 204776b2cf59SMatthew Knepley to the solution of each system. 204876b2cf59SMatthew Knepley 204976b2cf59SMatthew Knepley Collective on SNES 205076b2cf59SMatthew Knepley 205176b2cf59SMatthew Knepley Input Parameters: 205276b2cf59SMatthew Knepley . snes - The nonlinear solver context 205376b2cf59SMatthew Knepley . func - The function 205476b2cf59SMatthew Knepley 205576b2cf59SMatthew Knepley Calling sequence of func: 205676b2cf59SMatthew Knepley . func (TS ts, Vec rsol, void *ctx); 205776b2cf59SMatthew Knepley 205876b2cf59SMatthew Knepley . sol - The current solution vector 205976b2cf59SMatthew Knepley . ctx - The user-context 206076b2cf59SMatthew Knepley 206176b2cf59SMatthew Knepley Level: intermediate 206276b2cf59SMatthew Knepley 206376b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions 206476b2cf59SMatthew Knepley .seealso SNESDefaultSolutionBC(), SNESSetRhsBC(), SNESSetUpdate() 206576b2cf59SMatthew Knepley @*/ 206676b2cf59SMatthew Knepley int SNESSetSolutionBC(SNES snes, int (*func)(SNES, Vec, void *)) 206776b2cf59SMatthew Knepley { 206876b2cf59SMatthew Knepley PetscFunctionBegin; 206976b2cf59SMatthew Knepley PetscValidHeaderSpecific(snes, SNES_COOKIE); 207076b2cf59SMatthew Knepley snes->applysolbc = func; 207176b2cf59SMatthew Knepley PetscFunctionReturn(0); 207276b2cf59SMatthew Knepley } 207376b2cf59SMatthew Knepley 2074e74ef692SMatthew Knepley #undef __FUNCT__ 2075e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultSolutionBC" 207676b2cf59SMatthew Knepley /*@ 207776b2cf59SMatthew Knepley SNESDefaultSolutionBC - The default boundary condition function which does nothing. 207876b2cf59SMatthew Knepley 207976b2cf59SMatthew Knepley Not collective 208076b2cf59SMatthew Knepley 208176b2cf59SMatthew Knepley Input Parameters: 208276b2cf59SMatthew Knepley . snes - The nonlinear solver context 208376b2cf59SMatthew Knepley . sol - The solution 208476b2cf59SMatthew Knepley . ctx - The user-context 208576b2cf59SMatthew Knepley 208676b2cf59SMatthew Knepley Level: beginner 208776b2cf59SMatthew Knepley 208876b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions 208976b2cf59SMatthew Knepley .seealso SNESSetSolutionBC(), SNESDefaultRhsBC(), SNESDefaultUpdate() 209076b2cf59SMatthew Knepley @*/ 209176b2cf59SMatthew Knepley int SNESDefaultSolutionBC(SNES snes, Vec sol, void *ctx) 209276b2cf59SMatthew Knepley { 209376b2cf59SMatthew Knepley PetscFunctionBegin; 209476b2cf59SMatthew Knepley PetscFunctionReturn(0); 209576b2cf59SMatthew Knepley } 209676b2cf59SMatthew Knepley 2097e74ef692SMatthew Knepley #undef __FUNCT__ 2098e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate" 209976b2cf59SMatthew Knepley /*@ 210076b2cf59SMatthew Knepley SNESSetUpdate - Sets the general-purpose update function called 210176b2cf59SMatthew Knepley at the beginning of every step of the iteration. 210276b2cf59SMatthew Knepley 210376b2cf59SMatthew Knepley Collective on SNES 210476b2cf59SMatthew Knepley 210576b2cf59SMatthew Knepley Input Parameters: 210676b2cf59SMatthew Knepley . snes - The nonlinear solver context 210776b2cf59SMatthew Knepley . func - The function 210876b2cf59SMatthew Knepley 210976b2cf59SMatthew Knepley Calling sequence of func: 211076b2cf59SMatthew Knepley . func (TS ts, int step); 211176b2cf59SMatthew Knepley 211276b2cf59SMatthew Knepley . step - The current step of the iteration 211376b2cf59SMatthew Knepley 211476b2cf59SMatthew Knepley Level: intermediate 211576b2cf59SMatthew Knepley 211676b2cf59SMatthew Knepley .keywords: SNES, update 211776b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC() 211876b2cf59SMatthew Knepley @*/ 211976b2cf59SMatthew Knepley int SNESSetUpdate(SNES snes, int (*func)(SNES, int)) 212076b2cf59SMatthew Knepley { 212176b2cf59SMatthew Knepley PetscFunctionBegin; 212276b2cf59SMatthew Knepley PetscValidHeaderSpecific(snes, SNES_COOKIE); 212376b2cf59SMatthew Knepley snes->update = func; 212476b2cf59SMatthew Knepley PetscFunctionReturn(0); 212576b2cf59SMatthew Knepley } 212676b2cf59SMatthew Knepley 2127e74ef692SMatthew Knepley #undef __FUNCT__ 2128e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate" 212976b2cf59SMatthew Knepley /*@ 213076b2cf59SMatthew Knepley SNESDefaultUpdate - The default update function which does nothing. 213176b2cf59SMatthew Knepley 213276b2cf59SMatthew Knepley Not collective 213376b2cf59SMatthew Knepley 213476b2cf59SMatthew Knepley Input Parameters: 213576b2cf59SMatthew Knepley . snes - The nonlinear solver context 213676b2cf59SMatthew Knepley . step - The current step of the iteration 213776b2cf59SMatthew Knepley 2138*205452f4SMatthew Knepley Level: intermediate 2139*205452f4SMatthew Knepley 214076b2cf59SMatthew Knepley .keywords: SNES, update 214176b2cf59SMatthew Knepley .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultSolutionBC() 214276b2cf59SMatthew Knepley @*/ 214376b2cf59SMatthew Knepley int SNESDefaultUpdate(SNES snes, int step) 214476b2cf59SMatthew Knepley { 214576b2cf59SMatthew Knepley PetscFunctionBegin; 214676b2cf59SMatthew Knepley PetscFunctionReturn(0); 214776b2cf59SMatthew Knepley } 214876b2cf59SMatthew Knepley 21494a2ae208SSatish Balay #undef __FUNCT__ 21504a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private" 21519b94acceSBarry Smith /* 21529b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 21539b94acceSBarry Smith positive parameter delta. 21549b94acceSBarry Smith 21559b94acceSBarry Smith Input Parameters: 2156c7afd0dbSLois Curfman McInnes + snes - the SNES context 21579b94acceSBarry Smith . y - approximate solution of linear system 21589b94acceSBarry Smith . fnorm - 2-norm of current function 2159c7afd0dbSLois Curfman McInnes - delta - trust region size 21609b94acceSBarry Smith 21619b94acceSBarry Smith Output Parameters: 2162c7afd0dbSLois Curfman McInnes + gpnorm - predicted function norm at the new point, assuming local 21639b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 21649b94acceSBarry Smith region, and exceeds zero otherwise. 2165c7afd0dbSLois Curfman McInnes - ynorm - 2-norm of the step 21669b94acceSBarry Smith 21679b94acceSBarry Smith Note: 21686831982aSBarry Smith For non-trust region methods such as SNESEQLS, the parameter delta 21699b94acceSBarry Smith is set to be the maximum allowable step size. 21709b94acceSBarry Smith 21719b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 21729b94acceSBarry Smith */ 2173329f5518SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta, 2174329f5518SBarry Smith PetscReal *gpnorm,PetscReal *ynorm) 21759b94acceSBarry Smith { 2176329f5518SBarry Smith PetscReal norm; 2177ea709b57SSatish Balay PetscScalar cnorm; 21783a40ed3dSBarry Smith int ierr; 21793a40ed3dSBarry Smith 21803a40ed3dSBarry Smith PetscFunctionBegin; 2181184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2182184914b5SBarry Smith PetscValidHeaderSpecific(y,VEC_COOKIE); 2183184914b5SBarry Smith PetscCheckSameComm(snes,y); 2184184914b5SBarry Smith 21853a40ed3dSBarry Smith ierr = VecNorm(y,NORM_2,&norm);CHKERRQ(ierr); 21869b94acceSBarry Smith if (norm > *delta) { 21879b94acceSBarry Smith norm = *delta/norm; 21889b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 21899b94acceSBarry Smith cnorm = norm; 2190329f5518SBarry Smith ierr = VecScale(&cnorm,y);CHKERRQ(ierr); 21919b94acceSBarry Smith *ynorm = *delta; 21929b94acceSBarry Smith } else { 21939b94acceSBarry Smith *gpnorm = 0.0; 21949b94acceSBarry Smith *ynorm = norm; 21959b94acceSBarry Smith } 21963a40ed3dSBarry Smith PetscFunctionReturn(0); 21979b94acceSBarry Smith } 21989b94acceSBarry Smith 21994a2ae208SSatish Balay #undef __FUNCT__ 22004a2ae208SSatish Balay #define __FUNCT__ "SNESSolve" 22019b94acceSBarry Smith /*@ 22029b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 220363a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 22049b94acceSBarry Smith 2205c7afd0dbSLois Curfman McInnes Collective on SNES 2206c7afd0dbSLois Curfman McInnes 2207b2002411SLois Curfman McInnes Input Parameters: 2208c7afd0dbSLois Curfman McInnes + snes - the SNES context 2209c7afd0dbSLois Curfman McInnes - x - the solution vector 22109b94acceSBarry Smith 22119b94acceSBarry Smith Output Parameter: 2212b2002411SLois Curfman McInnes . its - number of iterations until termination 22139b94acceSBarry Smith 2214b2002411SLois Curfman McInnes Notes: 22158ddd3da0SLois Curfman McInnes The user should initialize the vector,x, with the initial guess 22168ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 22178ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 22188ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 22198ddd3da0SLois Curfman McInnes 222036851e7fSLois Curfman McInnes Level: beginner 222136851e7fSLois Curfman McInnes 22229b94acceSBarry Smith .keywords: SNES, nonlinear, solve 22239b94acceSBarry Smith 222463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 22259b94acceSBarry Smith @*/ 22268ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 22279b94acceSBarry Smith { 2228f1af5d2fSBarry Smith int ierr; 2229f1af5d2fSBarry Smith PetscTruth flg; 2230052efed2SBarry Smith 22313a40ed3dSBarry Smith PetscFunctionBegin; 223277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2233184914b5SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 2234184914b5SBarry Smith PetscCheckSameComm(snes,x); 223574679c65SBarry Smith PetscValidIntPointer(its); 223629bbc08cSBarry Smith if (!snes->solve) SETERRQ(1,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()"); 223774637425SBarry Smith 223882bf6240SBarry Smith if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);} 2239c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 2240758f92a0SBarry Smith if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0; 2241d5ba7fb7SMatthew Knepley ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 224250ffb88aSMatthew Knepley snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0; 22439b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr); 2244d5ba7fb7SMatthew Knepley ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr); 2245b0a32e0cSBarry Smith ierr = PetscOptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr); 224693b98531SBarry Smith if (flg && !PetscPreLoadingOn) { ierr = SNESView(snes,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } 22473a40ed3dSBarry Smith PetscFunctionReturn(0); 22489b94acceSBarry Smith } 22499b94acceSBarry Smith 22509b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 22519b94acceSBarry Smith 22524a2ae208SSatish Balay #undef __FUNCT__ 22534a2ae208SSatish Balay #define __FUNCT__ "SNESSetType" 225482bf6240SBarry Smith /*@C 22554b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 22569b94acceSBarry Smith 2257fee21e36SBarry Smith Collective on SNES 2258fee21e36SBarry Smith 2259c7afd0dbSLois Curfman McInnes Input Parameters: 2260c7afd0dbSLois Curfman McInnes + snes - the SNES context 2261454a90a3SBarry Smith - type - a known method 2262c7afd0dbSLois Curfman McInnes 2263c7afd0dbSLois Curfman McInnes Options Database Key: 2264454a90a3SBarry Smith . -snes_type <type> - Sets the method; use -help for a list 2265c7afd0dbSLois Curfman McInnes of available methods (for instance, ls or tr) 2266ae12b187SLois Curfman McInnes 22679b94acceSBarry Smith Notes: 2268e090d566SSatish Balay See "petsc/include/petscsnes.h" for available methods (for instance) 22696831982aSBarry Smith + SNESEQLS - Newton's method with line search 2270c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 22716831982aSBarry Smith . SNESEQTR - Newton's method with trust region 2272c7afd0dbSLois Curfman McInnes (systems of nonlinear equations) 22736831982aSBarry Smith . SNESUMTR - Newton's method with trust region 2274c7afd0dbSLois Curfman McInnes (unconstrained minimization) 22756831982aSBarry Smith - SNESUMLS - Newton's method with line search 2276c7afd0dbSLois Curfman McInnes (unconstrained minimization) 22779b94acceSBarry Smith 2278ae12b187SLois Curfman McInnes Normally, it is best to use the SNESSetFromOptions() command and then 2279ae12b187SLois Curfman McInnes set the SNES solver type from the options database rather than by using 2280ae12b187SLois Curfman McInnes this routine. Using the options database provides the user with 2281ae12b187SLois Curfman McInnes maximum flexibility in evaluating the many nonlinear solvers. 2282ae12b187SLois Curfman McInnes The SNESSetType() routine is provided for those situations where it 2283ae12b187SLois Curfman McInnes is necessary to set the nonlinear solver independently of the command 2284ae12b187SLois Curfman McInnes line or options database. This might be the case, for example, when 2285ae12b187SLois Curfman McInnes the choice of solver changes during the execution of the program, 2286ae12b187SLois Curfman McInnes and the user's application is taking responsibility for choosing the 2287b0a32e0cSBarry Smith appropriate method. 228836851e7fSLois Curfman McInnes 228936851e7fSLois Curfman McInnes Level: intermediate 2290a703fe33SLois Curfman McInnes 2291454a90a3SBarry Smith .keywords: SNES, set, type 2292435da068SBarry Smith 2293435da068SBarry Smith .seealso: SNESType, SNESCreate() 2294435da068SBarry Smith 22959b94acceSBarry Smith @*/ 2296454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type) 22979b94acceSBarry Smith { 22980f5bd95cSBarry Smith int ierr,(*r)(SNES); 22996831982aSBarry Smith PetscTruth match; 23003a40ed3dSBarry Smith 23013a40ed3dSBarry Smith PetscFunctionBegin; 230277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 23030f5bd95cSBarry Smith PetscValidCharPointer(type); 230482bf6240SBarry Smith 23056831982aSBarry Smith ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr); 23060f5bd95cSBarry Smith if (match) PetscFunctionReturn(0); 230782bf6240SBarry Smith 230882bf6240SBarry Smith if (snes->setupcalled) { 2309e1311b90SBarry Smith ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr); 231082bf6240SBarry Smith snes->data = 0; 231182bf6240SBarry Smith } 23127d1a2b2bSBarry Smith 23139b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 231482bf6240SBarry Smith if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 231582bf6240SBarry Smith 2316b9617806SBarry Smith ierr = PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr); 231782bf6240SBarry Smith 231829bbc08cSBarry Smith if (!r) SETERRQ1(1,"Unable to find requested SNES type %s",type); 23191548b14aSBarry Smith 2320606d414cSSatish Balay if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);} 232182bf6240SBarry Smith snes->data = 0; 23223a40ed3dSBarry Smith ierr = (*r)(snes);CHKERRQ(ierr); 232382bf6240SBarry Smith 2324454a90a3SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr); 232582bf6240SBarry Smith snes->set_method_called = 1; 232682bf6240SBarry Smith 23273a40ed3dSBarry Smith PetscFunctionReturn(0); 23289b94acceSBarry Smith } 23299b94acceSBarry Smith 2330a847f771SSatish Balay 23319b94acceSBarry Smith /* --------------------------------------------------------------------- */ 23324a2ae208SSatish Balay #undef __FUNCT__ 23334a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy" 23349b94acceSBarry Smith /*@C 23359b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 2336f1af5d2fSBarry Smith registered by SNESRegisterDynamic(). 23379b94acceSBarry Smith 2338fee21e36SBarry Smith Not Collective 2339fee21e36SBarry Smith 234036851e7fSLois Curfman McInnes Level: advanced 234136851e7fSLois Curfman McInnes 23429b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 23439b94acceSBarry Smith 23449b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 23459b94acceSBarry Smith @*/ 2346cf256101SBarry Smith int SNESRegisterDestroy(void) 23479b94acceSBarry Smith { 234882bf6240SBarry Smith int ierr; 234982bf6240SBarry Smith 23503a40ed3dSBarry Smith PetscFunctionBegin; 235182bf6240SBarry Smith if (SNESList) { 2352b0a32e0cSBarry Smith ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr); 235382bf6240SBarry Smith SNESList = 0; 23549b94acceSBarry Smith } 23554c49b128SBarry Smith SNESRegisterAllCalled = PETSC_FALSE; 23563a40ed3dSBarry Smith PetscFunctionReturn(0); 23579b94acceSBarry Smith } 23589b94acceSBarry Smith 23594a2ae208SSatish Balay #undef __FUNCT__ 23604a2ae208SSatish Balay #define __FUNCT__ "SNESGetType" 23619b94acceSBarry Smith /*@C 23629a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 23639b94acceSBarry Smith 2364c7afd0dbSLois Curfman McInnes Not Collective 2365c7afd0dbSLois Curfman McInnes 23669b94acceSBarry Smith Input Parameter: 23674b0e389bSBarry Smith . snes - nonlinear solver context 23689b94acceSBarry Smith 23699b94acceSBarry Smith Output Parameter: 23703a7fca6bSBarry Smith . type - SNES method (a character string) 23719b94acceSBarry Smith 237236851e7fSLois Curfman McInnes Level: intermediate 237336851e7fSLois Curfman McInnes 2374454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name 23759b94acceSBarry Smith @*/ 2376454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type) 23779b94acceSBarry Smith { 23783a40ed3dSBarry Smith PetscFunctionBegin; 2379184914b5SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2380454a90a3SBarry Smith *type = snes->type_name; 23813a40ed3dSBarry Smith PetscFunctionReturn(0); 23829b94acceSBarry Smith } 23839b94acceSBarry Smith 23844a2ae208SSatish Balay #undef __FUNCT__ 23854a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution" 23869b94acceSBarry Smith /*@C 23879b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 23889b94acceSBarry Smith stored. 23899b94acceSBarry Smith 2390c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2391c7afd0dbSLois Curfman McInnes 23929b94acceSBarry Smith Input Parameter: 23939b94acceSBarry Smith . snes - the SNES context 23949b94acceSBarry Smith 23959b94acceSBarry Smith Output Parameter: 23969b94acceSBarry Smith . x - the solution 23979b94acceSBarry Smith 239836851e7fSLois Curfman McInnes Level: advanced 239936851e7fSLois Curfman McInnes 24009b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 24019b94acceSBarry Smith 2402a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 24039b94acceSBarry Smith @*/ 24049b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 24059b94acceSBarry Smith { 24063a40ed3dSBarry Smith PetscFunctionBegin; 240777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 24089b94acceSBarry Smith *x = snes->vec_sol_always; 24093a40ed3dSBarry Smith PetscFunctionReturn(0); 24109b94acceSBarry Smith } 24119b94acceSBarry Smith 24124a2ae208SSatish Balay #undef __FUNCT__ 24134a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate" 24149b94acceSBarry Smith /*@C 24159b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 24169b94acceSBarry Smith stored. 24179b94acceSBarry Smith 2418c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2419c7afd0dbSLois Curfman McInnes 24209b94acceSBarry Smith Input Parameter: 24219b94acceSBarry Smith . snes - the SNES context 24229b94acceSBarry Smith 24239b94acceSBarry Smith Output Parameter: 24249b94acceSBarry Smith . x - the solution update 24259b94acceSBarry Smith 242636851e7fSLois Curfman McInnes Level: advanced 242736851e7fSLois Curfman McInnes 24289b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 24299b94acceSBarry Smith 24309b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 24319b94acceSBarry Smith @*/ 24329b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 24339b94acceSBarry Smith { 24343a40ed3dSBarry Smith PetscFunctionBegin; 243577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 24369b94acceSBarry Smith *x = snes->vec_sol_update_always; 24373a40ed3dSBarry Smith PetscFunctionReturn(0); 24389b94acceSBarry Smith } 24399b94acceSBarry Smith 24404a2ae208SSatish Balay #undef __FUNCT__ 24414a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction" 24429b94acceSBarry Smith /*@C 24433638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 24449b94acceSBarry Smith 2445c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2446c7afd0dbSLois Curfman McInnes 24479b94acceSBarry Smith Input Parameter: 24489b94acceSBarry Smith . snes - the SNES context 24499b94acceSBarry Smith 24509b94acceSBarry Smith Output Parameter: 24517bf4e008SBarry Smith + r - the function (or PETSC_NULL) 245200036973SBarry Smith . ctx - the function context (or PETSC_NULL) 245300036973SBarry Smith - func - the function (or PETSC_NULL) 24549b94acceSBarry Smith 24559b94acceSBarry Smith Notes: 24569b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 24579b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 24589b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 24599b94acceSBarry Smith 246036851e7fSLois Curfman McInnes Level: advanced 246136851e7fSLois Curfman McInnes 2462a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 24639b94acceSBarry Smith 246431615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 246531615d04SLois Curfman McInnes SNESGetGradient() 24667bf4e008SBarry Smith 24679b94acceSBarry Smith @*/ 246800036973SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx,int (**func)(SNES,Vec,Vec,void*)) 24699b94acceSBarry Smith { 24703a40ed3dSBarry Smith PetscFunctionBegin; 247177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2472a8c6a408SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 247329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_NONLINEAR_EQUATIONS only"); 2474a8c6a408SBarry Smith } 24757bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 24767bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 247700036973SBarry Smith if (func) *func = snes->computefunction; 24783a40ed3dSBarry Smith PetscFunctionReturn(0); 24799b94acceSBarry Smith } 24809b94acceSBarry Smith 24814a2ae208SSatish Balay #undef __FUNCT__ 24824a2ae208SSatish Balay #define __FUNCT__ "SNESGetGradient" 24839b94acceSBarry Smith /*@C 24843638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 24859b94acceSBarry Smith 2486c7afd0dbSLois Curfman McInnes Not Collective, but Vec is parallel if SNES is parallel 2487c7afd0dbSLois Curfman McInnes 24889b94acceSBarry Smith Input Parameter: 24899b94acceSBarry Smith . snes - the SNES context 24909b94acceSBarry Smith 24919b94acceSBarry Smith Output Parameter: 24927bf4e008SBarry Smith + r - the gradient (or PETSC_NULL) 24937bf4e008SBarry Smith - ctx - the gradient context (or PETSC_NULL) 24949b94acceSBarry Smith 24959b94acceSBarry Smith Notes: 24969b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 24979b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 24989b94acceSBarry Smith SNESGetFunction(). 24999b94acceSBarry Smith 250036851e7fSLois Curfman McInnes Level: advanced 250136851e7fSLois Curfman McInnes 25029b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 25039b94acceSBarry Smith 25047bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(), 25057bf4e008SBarry Smith SNESSetGradient(), SNESSetFunction() 25067bf4e008SBarry Smith 25079b94acceSBarry Smith @*/ 25087bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx) 25099b94acceSBarry Smith { 25103a40ed3dSBarry Smith PetscFunctionBegin; 251177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 25123a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 251329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 25143a40ed3dSBarry Smith } 25157bf4e008SBarry Smith if (r) *r = snes->vec_func_always; 25167bf4e008SBarry Smith if (ctx) *ctx = snes->funP; 25173a40ed3dSBarry Smith PetscFunctionReturn(0); 25189b94acceSBarry Smith } 25199b94acceSBarry Smith 25204a2ae208SSatish Balay #undef __FUNCT__ 25214a2ae208SSatish Balay #define __FUNCT__ "SNESGetMinimizationFunction" 25227bf4e008SBarry Smith /*@C 25239b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 25249b94acceSBarry Smith unconstrained minimization problems. 25259b94acceSBarry Smith 2526c7afd0dbSLois Curfman McInnes Not Collective 2527c7afd0dbSLois Curfman McInnes 25289b94acceSBarry Smith Input Parameter: 25299b94acceSBarry Smith . snes - the SNES context 25309b94acceSBarry Smith 25319b94acceSBarry Smith Output Parameter: 25327bf4e008SBarry Smith + r - the function (or PETSC_NULL) 25337bf4e008SBarry Smith - ctx - the function context (or PETSC_NULL) 25349b94acceSBarry Smith 25359b94acceSBarry Smith Notes: 25369b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 25379b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 25389b94acceSBarry Smith SNESGetFunction(). 25399b94acceSBarry Smith 254036851e7fSLois Curfman McInnes Level: advanced 254136851e7fSLois Curfman McInnes 25429b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 25439b94acceSBarry Smith 25447bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction() 25457bf4e008SBarry Smith 25469b94acceSBarry Smith @*/ 2547329f5518SBarry Smith int SNESGetMinimizationFunction(SNES snes,PetscReal *r,void **ctx) 25489b94acceSBarry Smith { 25493a40ed3dSBarry Smith PetscFunctionBegin; 255077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 255174679c65SBarry Smith PetscValidScalarPointer(r); 25523a40ed3dSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 255329bbc08cSBarry Smith SETERRQ(PETSC_ERR_ARG_WRONG,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 25543a40ed3dSBarry Smith } 25557bf4e008SBarry Smith if (r) *r = snes->fc; 25567bf4e008SBarry Smith if (ctx) *ctx = snes->umfunP; 25573a40ed3dSBarry Smith PetscFunctionReturn(0); 25589b94acceSBarry Smith } 25599b94acceSBarry Smith 25604a2ae208SSatish Balay #undef __FUNCT__ 25614a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix" 25623c7409f5SSatish Balay /*@C 25633c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 2564d850072dSLois Curfman McInnes SNES options in the database. 25653c7409f5SSatish Balay 2566fee21e36SBarry Smith Collective on SNES 2567fee21e36SBarry Smith 2568c7afd0dbSLois Curfman McInnes Input Parameter: 2569c7afd0dbSLois Curfman McInnes + snes - the SNES context 2570c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2571c7afd0dbSLois Curfman McInnes 2572d850072dSLois Curfman McInnes Notes: 2573a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2574c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2575d850072dSLois Curfman McInnes 257636851e7fSLois Curfman McInnes Level: advanced 257736851e7fSLois Curfman McInnes 25783c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 2579a86d99e1SLois Curfman McInnes 2580a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 25813c7409f5SSatish Balay @*/ 25823c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 25833c7409f5SSatish Balay { 25843c7409f5SSatish Balay int ierr; 25853c7409f5SSatish Balay 25863a40ed3dSBarry Smith PetscFunctionBegin; 258777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2588639f9d9dSBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 25893c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 25903a40ed3dSBarry Smith PetscFunctionReturn(0); 25913c7409f5SSatish Balay } 25923c7409f5SSatish Balay 25934a2ae208SSatish Balay #undef __FUNCT__ 25944a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix" 25953c7409f5SSatish Balay /*@C 2596f525115eSLois Curfman McInnes SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2597d850072dSLois Curfman McInnes SNES options in the database. 25983c7409f5SSatish Balay 2599fee21e36SBarry Smith Collective on SNES 2600fee21e36SBarry Smith 2601c7afd0dbSLois Curfman McInnes Input Parameters: 2602c7afd0dbSLois Curfman McInnes + snes - the SNES context 2603c7afd0dbSLois Curfman McInnes - prefix - the prefix to prepend to all option names 2604c7afd0dbSLois Curfman McInnes 2605d850072dSLois Curfman McInnes Notes: 2606a83b1b31SSatish Balay A hyphen (-) must NOT be given at the beginning of the prefix name. 2607c7afd0dbSLois Curfman McInnes The first character of all runtime options is AUTOMATICALLY the hyphen. 2608d850072dSLois Curfman McInnes 260936851e7fSLois Curfman McInnes Level: advanced 261036851e7fSLois Curfman McInnes 26113c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 2612a86d99e1SLois Curfman McInnes 2613a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 26143c7409f5SSatish Balay @*/ 26153c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 26163c7409f5SSatish Balay { 26173c7409f5SSatish Balay int ierr; 26183c7409f5SSatish Balay 26193a40ed3dSBarry Smith PetscFunctionBegin; 262077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2621639f9d9dSBarry Smith ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 26223c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 26233a40ed3dSBarry Smith PetscFunctionReturn(0); 26243c7409f5SSatish Balay } 26253c7409f5SSatish Balay 26264a2ae208SSatish Balay #undef __FUNCT__ 26274a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix" 26289ab63eb5SSatish Balay /*@C 26293c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 26303c7409f5SSatish Balay SNES options in the database. 26313c7409f5SSatish Balay 2632c7afd0dbSLois Curfman McInnes Not Collective 2633c7afd0dbSLois Curfman McInnes 26343c7409f5SSatish Balay Input Parameter: 26353c7409f5SSatish Balay . snes - the SNES context 26363c7409f5SSatish Balay 26373c7409f5SSatish Balay Output Parameter: 26383c7409f5SSatish Balay . prefix - pointer to the prefix string used 26393c7409f5SSatish Balay 26409ab63eb5SSatish Balay Notes: On the fortran side, the user should pass in a string 'prifix' of 26419ab63eb5SSatish Balay sufficient length to hold the prefix. 26429ab63eb5SSatish Balay 264336851e7fSLois Curfman McInnes Level: advanced 264436851e7fSLois Curfman McInnes 26453c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 2646a86d99e1SLois Curfman McInnes 2647a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 26483c7409f5SSatish Balay @*/ 26493c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 26503c7409f5SSatish Balay { 26513c7409f5SSatish Balay int ierr; 26523c7409f5SSatish Balay 26533a40ed3dSBarry Smith PetscFunctionBegin; 265477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2655639f9d9dSBarry Smith ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr); 26563a40ed3dSBarry Smith PetscFunctionReturn(0); 26573c7409f5SSatish Balay } 26583c7409f5SSatish Balay 2659acb85ae6SSatish Balay /*MC 2660f1af5d2fSBarry Smith SNESRegisterDynamic - Adds a method to the nonlinear solver package. 26619b94acceSBarry Smith 2662b2002411SLois Curfman McInnes Synopsis: 26633a7fca6bSBarry Smith int SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES)) 26649b94acceSBarry Smith 26658d76a1e5SLois Curfman McInnes Not collective 26668d76a1e5SLois Curfman McInnes 2667b2002411SLois Curfman McInnes Input Parameters: 2668c7afd0dbSLois Curfman McInnes + name_solver - name of a new user-defined solver 2669b2002411SLois Curfman McInnes . path - path (either absolute or relative) the library containing this solver 2670b2002411SLois Curfman McInnes . name_create - name of routine to create method context 2671c7afd0dbSLois Curfman McInnes - routine_create - routine to create method context 26729b94acceSBarry Smith 2673b2002411SLois Curfman McInnes Notes: 2674f1af5d2fSBarry Smith SNESRegisterDynamic() may be called multiple times to add several user-defined solvers. 26753a40ed3dSBarry Smith 2676b2002411SLois Curfman McInnes If dynamic libraries are used, then the fourth input argument (routine_create) 2677b2002411SLois Curfman McInnes is ignored. 2678b2002411SLois Curfman McInnes 2679b9617806SBarry Smith Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT}, 26805ba88a07SLois Curfman McInnes and others of the form ${any_environmental_variable} occuring in pathname will be 26815ba88a07SLois Curfman McInnes replaced with appropriate values. 26825ba88a07SLois Curfman McInnes 2683b2002411SLois Curfman McInnes Sample usage: 268469225d78SLois Curfman McInnes .vb 2685f1af5d2fSBarry Smith SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a, 2686b2002411SLois Curfman McInnes "MySolverCreate",MySolverCreate); 268769225d78SLois Curfman McInnes .ve 2688b2002411SLois Curfman McInnes 2689b2002411SLois Curfman McInnes Then, your solver can be chosen with the procedural interface via 2690b2002411SLois Curfman McInnes $ SNESSetType(snes,"my_solver") 2691b2002411SLois Curfman McInnes or at runtime via the option 2692b2002411SLois Curfman McInnes $ -snes_type my_solver 2693b2002411SLois Curfman McInnes 269436851e7fSLois Curfman McInnes Level: advanced 269536851e7fSLois Curfman McInnes 2696b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register 2697b2002411SLois Curfman McInnes 2698b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy() 2699b2002411SLois Curfman McInnes M*/ 2700b2002411SLois Curfman McInnes 27014a2ae208SSatish Balay #undef __FUNCT__ 27024a2ae208SSatish Balay #define __FUNCT__ "SNESRegister" 2703f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES)) 2704b2002411SLois Curfman McInnes { 2705b2002411SLois Curfman McInnes char fullname[256]; 2706b2002411SLois Curfman McInnes int ierr; 2707b2002411SLois Curfman McInnes 2708b2002411SLois Curfman McInnes PetscFunctionBegin; 2709b0a32e0cSBarry Smith ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2710c134de8dSSatish Balay ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2711b2002411SLois Curfman McInnes PetscFunctionReturn(0); 2712b2002411SLois Curfman McInnes } 2713