1 #ifndef lint 2 static char vcid[] = "$Id: snesut.c,v 1.4 1995/09/30 19:31:04 bsmith Exp bsmith $"; 3 #endif 4 5 #include <math.h> 6 #include "snesimpl.h" /*I "snes.h" I*/ 7 8 /*@C 9 SNESDefaultMonitor - Default SNES monitoring routine. 10 11 Input Parameters: 12 . snes - the SNES context 13 . its - iteration number 14 . fgnorm - 2-norm of residual (or gradient) 15 . dummy - unused context 16 17 Notes: 18 For SNES_NONLINEAR_EQUATIONS methods the routine prints the 19 residual norm at each iteration. 20 21 For SNES_UNCONSTRAINED_MINIMIZATION methods the routine prints the 22 function value and gradient norm at each iteration. 23 24 .keywords: SNES, nonlinear, default, monitor, norm 25 26 .seealso: SNESSetMonitor() 27 @*/ 28 int SNESDefaultMonitor(SNES snes,int its,double fgnorm,void *dummy) 29 { 30 if (snes->method_class == SNES_NONLINEAR_EQUATIONS) 31 MPIU_printf(snes->comm, "iter = %d, Function norm %g \n",its,fgnorm); 32 else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) 33 MPIU_printf(snes->comm, 34 "iter = %d, Function value %g, Gradient norm %g \n",its,snes->fc,fgnorm); 35 else SETERRQ(1,"SNESDefaultMonitor:Unknown method class"); 36 return 0; 37 } 38 /* ---------------------------------------------------------------- */ 39 int SNESDefaultSMonitor(SNES snes,int its, double fgnorm,void *dummy) 40 { 41 if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 42 if (fgnorm > 1.e-9 || fgnorm == 0.0) { 43 MPIU_printf(snes->comm, "iter = %d, Function norm %g \n",its,fgnorm); 44 } 45 else if (fgnorm > 1.e-11){ 46 MPIU_printf(snes->comm, "iter = %d, Function norm %5.3e \n",its,fgnorm); 47 } 48 else { 49 MPIU_printf(snes->comm, "iter = %d, Function norm < 1.e-11\n",its); 50 } 51 } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 52 if (fgnorm > 1.e-9 || fgnorm == 0.0) { 53 MPIU_printf(snes->comm, 54 "iter = %d, Function value %g, Gradient norm %g \n", 55 its,snes->fc,fgnorm); 56 } 57 else if (fgnorm > 1.e-11) { 58 MPIU_printf(snes->comm, 59 "iter = %d, Function value %g, Gradient norm %5.3e \n", 60 its,snes->fc,fgnorm); 61 } 62 else { 63 MPIU_printf(snes->comm, 64 "iter = %d, Function value %g, Gradient norm < 1.e-11\n", 65 its,snes->fc); 66 } 67 } else SETERRQ(1,"SNESDefaultSMonitor:Unknown method class"); 68 return 0; 69 } 70 /* ---------------------------------------------------------------- */ 71 /*@C 72 SNESDefaultConverged - Default test for monitoring the convergence 73 of the solvers for systems of nonlinear equations. 74 75 Input Parameters: 76 . snes - the SNES context 77 . xnorm - 2-norm of current iterate 78 . pnorm - 2-norm of current step 79 . fnorm - 2-norm of function 80 . dummy - unused context 81 82 Returns: 83 $ 2 if ( fnorm < atol ), 84 $ 3 if ( pnorm < xtol*xnorm ), 85 $ -2 if ( nfct > maxf ), 86 $ 0 otherwise, 87 88 where 89 $ maxf - maximum number of function evaluations, 90 $ set with SNESSetMaxFunctionEvaluations() 91 $ nfct - number of function evaluations, 92 $ atol - absolute function norm tolerance, 93 $ set with SNESSetAbsoluteTolerance() 94 $ xtol - relative function norm tolerance, 95 $ set with SNESSetRelativeTolerance() 96 97 .keywords: SNES, nonlinear, default, converged, convergence 98 99 .seealso: SNESSetConvergenceTest(), SNESEisenstatWalkerConverged() 100 @*/ 101 int SNESDefaultConverged(SNES snes,double xnorm,double pnorm,double fnorm, 102 void *dummy) 103 { 104 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 105 "SNESDefaultConverged:For SNES_NONLINEAR_EQUATIONS only"); 106 /* Note: Reserve return code 1, -1 for compatibility with 107 SNESTrustRegionDefaultConverged */ 108 if (fnorm < snes->atol) { 109 PLogInfo((PetscObject)snes, 110 "SNES: Converged due to function norm %g < %g\n",fnorm,snes->atol); 111 return 2; 112 } 113 if (pnorm < snes->xtol*(xnorm)) { 114 PLogInfo((PetscObject)snes, 115 "SNES: Converged due to small update length: %g < %g * %g\n", 116 pnorm,snes->xtol,xnorm); 117 return 3; 118 } 119 if (snes->nfuncs > snes->max_funcs) { 120 PLogInfo((PetscObject)snes, 121 "SNES: Exceeded maximum number of function evaluations: %d > %d\n", 122 snes->nfuncs, snes->max_funcs ); 123 return -2; 124 } 125 return 0; 126 } 127 /* ------------------------------------------------------------ */ 128 /*@ 129 SNES_KSP_SetConvergenceTestEW - Sets alternative convergence test for 130 for the linear solvers within an inexact Newton method. 131 132 Input Parameter: 133 . snes - SNES context 134 135 Notes: 136 Currently, the default is to use a constant relative tolerance for 137 the inner linear solvers. Alternatively, one can use the 138 Eisenstat-Walker method, where the relative convergence tolerance 139 is reset at each Newton iteration according progress of the nonlinear 140 solver. 141 142 Reference: 143 S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 144 inexact Newton method", Utah State University Math. Stat. Dept. Res. 145 Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 146 147 .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 148 @*/ 149 int SNES_KSP_SetConvergenceTestEW(SNES snes) 150 { 151 snes->ksp_ewconv = 1; 152 return 0; 153 } 154 155 /*@ 156 SNES_KSP_SetParametersEW - Sets parameters for Eisenstat-Walker 157 convergence criteria for the linear solvers within an inexact 158 Newton method. 159 160 Input Parameters: 161 . snes - SNES context 162 . version - version 1 or 2 (default is 2) 163 . rtol_0 - initial relative tolerance 164 $ (0 <= rtol_0 < 1) 165 . rtol_max - maximum relative tolerance 166 $ (0 <= rtol_max < 1) 167 . alpha - power for version 2 rtol computation 168 $ (1 < alpha <= 2) 169 . alpha2 - power for safeguard 170 . gamma2 - multiplicative factor for version 2 rtol computation 171 $ (0 <= gamma2 <= 1) 172 . threshold - threshold for imposing safeguard 173 $ (0 < threshold < 1) 174 175 Note: 176 Use PETSC_DEFAULT to retain the default for any of the parameters. 177 178 Reference: 179 S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 180 inexact Newton method", Utah State University Math. Stat. Dept. Res. 181 Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 182 183 .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 184 185 .seealso: SNES_KSP_SetConvergenceTestEW() 186 @*/ 187 int SNES_KSP_SetParametersEW(SNES snes,int version,double rtol_0, 188 double rtol_max,double gamma2,double alpha, 189 double alpha2,double threshold) 190 { 191 SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx; 192 if (!kctx) SETERRQ(1,"SNES_KSP_SetParametersEW:No context"); 193 if (version != PETSC_DEFAULT) kctx->version = version; 194 if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 195 if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 196 if (gamma2 != PETSC_DEFAULT) kctx->gamma = gamma2; 197 if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 198 if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 199 if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 200 if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) SETERRQ(1, 201 "SNES_KSP_SetParametersEW: 0.0 <= rtol_0 < 1.0\n"); 202 if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) SETERRQ(1, 203 "SNES_KSP_SetParametersEW: 0.0 <= rtol_max < 1.0\n"); 204 if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) SETERRQ(1, 205 "SNES_KSP_SetParametersEW: 0.0 < threshold < 1.0\n"); 206 if (kctx->gamma < 0.0 || kctx->gamma > 1.0) SETERRQ(1, 207 "SNES_KSP_SetParametersEW: 0.0 <= alpha <= 1.0\n"); 208 if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) SETERRQ(1, 209 "SNES_KSP_SetParametersEW: 1.0 < alpha <= 2.0\n"); 210 if (kctx->version != 1 && kctx->version !=2) SETERRQ(1, 211 "SNES_KSP_SetParametersEW: Only versions 1 and 2 are supported"); 212 return 0; 213 } 214 215 int SNES_KSP_EW_ComputeRelativeTolerance_Private(SNES snes,KSP ksp) 216 { 217 SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx; 218 double rtol, stol; 219 int ierr; 220 if (!kctx) 221 SETERRQ(1,"SNES_KSP_EW_ComputeRelativeTolerance_Private:No context"); 222 if (snes->iter == 1) { 223 rtol = kctx->rtol_0; 224 } else { 225 if (kctx->version == 1) { 226 rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 227 if (rtol < 0.0) rtol = -rtol; 228 stol = pow(kctx->rtol_last,kctx->alpha2); 229 if (stol > kctx->threshold) rtol = PETSCMAX(rtol,stol); 230 } else if (kctx->version == 2) { 231 rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 232 stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 233 if (stol > kctx->threshold) rtol = PETSCMAX(rtol,stol); 234 } else SETERRQ(1, 235 "SNES_KSP_EW_Converged_Private:Only versions 1 or 2 are supported"); 236 } 237 rtol = PETSCMIN(rtol,kctx->rtol_max); 238 kctx->rtol_last = rtol; 239 PLogInfo((PetscObject)snes, 240 "SNES: iter %d, Eisenstat-Walker (version %d) KSP rtol = %g\n", 241 snes->iter,kctx->version,rtol); 242 ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT); 243 CHKERRQ(ierr); 244 kctx->norm_last = snes->norm; 245 return 0; 246 } 247 248 int SNES_KSP_EW_Converged_Private(KSP ksp,int n,double rnorm,void *ctx) 249 { 250 SNES snes = (SNES)ctx; 251 SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx; 252 int convinfo; 253 254 if (!kctx) SETERRQ(1,"SNES_KSP_EW_Converged_Private:No convergence context"); 255 if (n == 0) SNES_KSP_EW_ComputeRelativeTolerance_Private(snes,ksp); 256 convinfo = KSPDefaultConverged(ksp,n,rnorm,ctx); 257 kctx->lresid_last = rnorm; 258 if (convinfo) 259 PLogInfo((PetscObject)snes,"SNES: KSP iterations=%d, rnorm=%g\n",n,rnorm); 260 return convinfo; 261 } 262 263 264