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