1 #ifndef lint 2 static char vcid[] = "$Id: snesut.c,v 1.16 1996/03/23 23:15:25 curfman Exp curfman $"; 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 PetscPrintf(snes->comm, "iter = %d, SNES Function norm %g \n",its,fgnorm); 32 else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) 33 PetscPrintf(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 PetscPrintf(snes->comm, "iter = %d, Function norm %g \n",its,fgnorm); 44 } 45 else if (fgnorm > 1.e-11){ 46 PetscPrintf(snes->comm, "iter = %d, Function norm %5.3e \n",its,fgnorm); 47 } 48 else { 49 PetscPrintf(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 PetscPrintf(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 PetscPrintf(snes->comm, 59 "iter = %d, Function value %g, Gradient norm %5.3e \n", 60 its,snes->fc,fgnorm); 61 } 62 else { 63 PetscPrintf(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 SNESConverged_EQ_LS - 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 $ 4 if ( fnorm < rtol*fnorm0 ), 86 $ -2 if ( nfct > maxf ), 87 $ 0 otherwise, 88 89 where 90 $ maxf - maximum number of function evaluations, 91 $ set with SNESSetTolerances() 92 $ nfct - number of function evaluations, 93 $ atol - absolute function norm tolerance, 94 $ set with SNESSetTolerances() 95 $ rtol - relative function norm tolerance, 96 $ set with SNESSetTolerances() 97 98 .keywords: SNES, nonlinear, default, converged, convergence 99 100 .seealso: SNESSetConvergenceTest(), SNESEisenstatWalkerConverged() 101 @*/ 102 int SNESConverged_EQ_LS(SNES snes,double xnorm,double pnorm,double fnorm,void *dummy) 103 { 104 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 105 "SNESConverged_EQ_LS:For SNES_NONLINEAR_EQUATIONS only"); 106 /* Note: Reserve return code 1, -1 for compatibility with SNESConverged_EQTR */ 107 if (fnorm <= snes->ttol) { 108 PLogInfo(snes, 109 "SNES:Converged due to function norm %g < %g (relative tolerance)\n",fnorm,snes->ttol); 110 return 4; 111 } 112 113 if (fnorm < snes->atol) { 114 PLogInfo(snes, 115 "SNES: Converged due to function norm %g < %g\n",fnorm,snes->atol); 116 return 2; 117 } 118 if (pnorm < snes->xtol*(xnorm)) { 119 PLogInfo(snes, 120 "SNES: Converged due to small update length: %g < %g * %g\n", 121 pnorm,snes->xtol,xnorm); 122 return 3; 123 } 124 if (snes->nfuncs > snes->max_funcs) { 125 PLogInfo(snes, 126 "SNES: Exceeded maximum number of function evaluations: %d > %d\n", 127 snes->nfuncs, snes->max_funcs ); 128 return -2; 129 } 130 return 0; 131 } 132 /* ------------------------------------------------------------ */ 133 /*@ 134 SNES_KSP_SetConvergenceTestEW - Sets alternative convergence test for 135 for the linear solvers within an inexact Newton method. 136 137 Input Parameter: 138 . snes - SNES context 139 140 Notes: 141 Currently, the default is to use a constant relative tolerance for 142 the inner linear solvers. Alternatively, one can use the 143 Eisenstat-Walker method, where the relative convergence tolerance 144 is reset at each Newton iteration according progress of the nonlinear 145 solver. 146 147 Reference: 148 S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 149 inexact Newton method", Utah State University Math. Stat. Dept. Res. 150 Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 151 152 .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton 153 @*/ 154 int SNES_KSP_SetConvergenceTestEW(SNES snes) 155 { 156 snes->ksp_ewconv = 1; 157 return 0; 158 } 159 160 /*@ 161 SNES_KSP_SetParametersEW - Sets parameters for Eisenstat-Walker 162 convergence criteria for the linear solvers within an inexact 163 Newton method. 164 165 Input Parameters: 166 . snes - SNES context 167 . version - version 1 or 2 (default is 2) 168 . rtol_0 - initial relative tolerance 169 $ (0 <= rtol_0 < 1) 170 . rtol_max - maximum relative tolerance 171 $ (0 <= rtol_max < 1) 172 . alpha - power for version 2 rtol computation 173 $ (1 < alpha <= 2) 174 . alpha2 - power for safeguard 175 . gamma2 - multiplicative factor for version 2 rtol computation 176 $ (0 <= gamma2 <= 1) 177 . threshold - threshold for imposing safeguard 178 $ (0 < threshold < 1) 179 180 Note: 181 Use PETSC_DEFAULT to retain the default for any of the parameters. 182 183 Reference: 184 S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an 185 inexact Newton method", Utah State University Math. Stat. Dept. Res. 186 Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput. 187 188 .keywords: SNES, KSP, Eisenstat, Walker, set, parameters 189 190 .seealso: SNES_KSP_SetConvergenceTestEW() 191 @*/ 192 int SNES_KSP_SetParametersEW(SNES snes,int version,double rtol_0, 193 double rtol_max,double gamma2,double alpha, 194 double alpha2,double threshold) 195 { 196 SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx; 197 if (!kctx) SETERRQ(1,"SNES_KSP_SetParametersEW:No context"); 198 if (version != PETSC_DEFAULT) kctx->version = version; 199 if (rtol_0 != PETSC_DEFAULT) kctx->rtol_0 = rtol_0; 200 if (rtol_max != PETSC_DEFAULT) kctx->rtol_max = rtol_max; 201 if (gamma2 != PETSC_DEFAULT) kctx->gamma = gamma2; 202 if (alpha != PETSC_DEFAULT) kctx->alpha = alpha; 203 if (alpha2 != PETSC_DEFAULT) kctx->alpha2 = alpha2; 204 if (threshold != PETSC_DEFAULT) kctx->threshold = threshold; 205 if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) SETERRQ(1, 206 "SNES_KSP_SetParametersEW: 0.0 <= rtol_0 < 1.0\n"); 207 if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) SETERRQ(1, 208 "SNES_KSP_SetParametersEW: 0.0 <= rtol_max < 1.0\n"); 209 if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) SETERRQ(1, 210 "SNES_KSP_SetParametersEW: 0.0 < threshold < 1.0\n"); 211 if (kctx->gamma < 0.0 || kctx->gamma > 1.0) SETERRQ(1, 212 "SNES_KSP_SetParametersEW: 0.0 <= alpha <= 1.0\n"); 213 if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) SETERRQ(1, 214 "SNES_KSP_SetParametersEW: 1.0 < alpha <= 2.0\n"); 215 if (kctx->version != 1 && kctx->version !=2) SETERRQ(1, 216 "SNES_KSP_SetParametersEW: Only versions 1 and 2 are supported"); 217 return 0; 218 } 219 220 int SNES_KSP_EW_ComputeRelativeTolerance_Private(SNES snes,KSP ksp) 221 { 222 SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx; 223 double rtol, stol; 224 int ierr; 225 if (!kctx) 226 SETERRQ(1,"SNES_KSP_EW_ComputeRelativeTolerance_Private:No context"); 227 if (snes->iter == 1) { 228 rtol = kctx->rtol_0; 229 } else { 230 if (kctx->version == 1) { 231 rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last; 232 if (rtol < 0.0) rtol = -rtol; 233 stol = pow(kctx->rtol_last,kctx->alpha2); 234 if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 235 } else if (kctx->version == 2) { 236 rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha); 237 stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha); 238 if (stol > kctx->threshold) rtol = PetscMax(rtol,stol); 239 } else SETERRQ(1, 240 "SNES_KSP_EW_Converged_Private:Only versions 1 or 2 are supported"); 241 } 242 rtol = PetscMin(rtol,kctx->rtol_max); 243 kctx->rtol_last = rtol; 244 PLogInfo(snes, 245 "SNES: iter %d, Eisenstat-Walker (version %d) KSP rtol = %g\n", 246 snes->iter,kctx->version,rtol); 247 ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT); 248 CHKERRQ(ierr); 249 kctx->norm_last = snes->norm; 250 return 0; 251 } 252 253 int SNES_KSP_EW_Converged_Private(KSP ksp,int n,double rnorm,void *ctx) 254 { 255 SNES snes = (SNES)ctx; 256 SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx; 257 int convinfo; 258 259 if (!kctx) SETERRQ(1,"SNES_KSP_EW_Converged_Private:No convergence context"); 260 if (n == 0) SNES_KSP_EW_ComputeRelativeTolerance_Private(snes,ksp); 261 convinfo = KSPDefaultConverged(ksp,n,rnorm,ctx); 262 kctx->lresid_last = rnorm; 263 if (convinfo) 264 PLogInfo(snes,"SNES: KSP iterations=%d, rnorm=%g\n",n,rnorm); 265 return convinfo; 266 } 267 268 269