Actual source code: tr.c
1: /*$Id: tr.c,v 1.128 2001/08/07 03:04:11 balay Exp $*/
2:
3: #include src/snes/impls/tr/tr.h
5: /*
6: This convergence test determines if the two norm of the
7: solution lies outside the trust region, if so it halts.
8: */
9: int SNES_TR_KSPConverged_Private(KSP ksp,int n,PetscReal rnorm,KSPConvergedReason *reason,void *ctx)
10: {
11: SNES snes = (SNES) ctx;
12: SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx;
13: SNES_TR *neP = (SNES_TR*)snes->data;
14: Vec x;
15: PetscReal nrm;
16: int ierr;
19: if (snes->ksp_ewconv) {
20: if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Eisenstat-Walker onvergence context not created");
21: if (!n) {SNES_KSP_EW_ComputeRelativeTolerance_Private(snes,ksp);}
22: kctx->lresid_last = rnorm;
23: }
24: KSPDefaultConverged(ksp,n,rnorm,reason,ctx);
25: if (*reason) {
26: PetscLogInfo(snes,"SNES_TR_KSPConverged_Private: regular convergence test KSP iterations=%d, rnorm=%gn",n,rnorm);
27: }
29: /* Determine norm of solution */
30: KSPBuildSolution(ksp,0,&x);
31: VecNorm(x,NORM_2,&nrm);
32: if (nrm >= neP->delta) {
33: PetscLogInfo(snes,"SNES_TR_KSPConverged_Private: KSP iterations=%d, rnorm=%gn",n,rnorm);
34: PetscLogInfo(snes,"SNES_TR_KSPConverged_Private: Ending linear iteration early, delta=%g, length=%gn",neP->delta,nrm);
35: *reason = KSP_CONVERGED_STEP_LENGTH;
36: }
37: return(0);
38: }
40: /*
41: SNESSolve_TR - Implements Newton's Method with a very simple trust
42: region approach for solving systems of nonlinear equations.
44: The basic algorithm is taken from "The Minpack Project", by More',
45: Sorensen, Garbow, Hillstrom, pages 88-111 of "Sources and Development
46: of Mathematical Software", Wayne Cowell, editor.
48: This is intended as a model implementation, since it does not
49: necessarily have many of the bells and whistles of other
50: implementations.
51: */
52: static int SNESSolve_TR(SNES snes,int *its)
53: {
54: SNES_TR *neP = (SNES_TR*)snes->data;
55: Vec X,F,Y,G,TMP,Ytmp;
56: int maxits,i,ierr,lits,breakout = 0;
57: MatStructure flg = DIFFERENT_NONZERO_PATTERN;
58: PetscReal rho,fnorm,gnorm,gpnorm,xnorm,delta,nrm,ynorm,norm1;
59: PetscScalar mone = -1.0,cnorm;
60: KSP ksp;
61: SLES sles;
62: SNESConvergedReason reason;
63: PetscTruth conv;
66: maxits = snes->max_its; /* maximum number of iterations */
67: X = snes->vec_sol; /* solution vector */
68: F = snes->vec_func; /* residual vector */
69: Y = snes->work[0]; /* work vectors */
70: G = snes->work[1];
71: Ytmp = snes->work[2];
73: PetscObjectTakeAccess(snes);
74: snes->iter = 0;
75: PetscObjectGrantAccess(snes);
76: VecNorm(X,NORM_2,&xnorm); /* xnorm = || X || */
78: SNESComputeFunction(snes,X,F); /* F(X) */
79: VecNorm(F,NORM_2,&fnorm); /* fnorm <- || F || */
80: PetscObjectTakeAccess(snes);
81: snes->norm = fnorm;
82: PetscObjectGrantAccess(snes);
83: delta = neP->delta0*fnorm;
84: neP->delta = delta;
85: SNESLogConvHistory(snes,fnorm,0);
86: SNESMonitor(snes,0,fnorm);
88: if (fnorm < snes->atol) {*its = 0; snes->reason = SNES_CONVERGED_FNORM_ABS; return(0);}
90: /* set parameter for default relative tolerance convergence test */
91: snes->ttol = fnorm*snes->rtol;
93: /* Set the stopping criteria to use the More' trick. */
94: PetscOptionsHasName(PETSC_NULL,"-snes_tr_ksp_regular_convergence_test",&conv);
95: if (!conv) {
96: SNESGetSLES(snes,&sles);
97: SLESGetKSP(sles,&ksp);
98: KSPSetConvergenceTest(ksp,SNES_TR_KSPConverged_Private,(void *)snes);
99: PetscLogInfo(snes,"SNESSolve_TR: Using Krylov convergence test SNES_TR_KSPConverged_Privaten");
100: }
101:
102: for (i=0; i<maxits; i++) {
103: SNESComputeJacobian(snes,X,&snes->jacobian,&snes->jacobian_pre,&flg);
104: SLESSetOperators(snes->sles,snes->jacobian,snes->jacobian_pre,flg);
106: /* Solve J Y = F, where J is Jacobian matrix */
107: SLESSolve(snes->sles,F,Ytmp,&lits);
108: snes->linear_its += lits;
109: PetscLogInfo(snes,"SNESSolve_TR: iter=%d, linear solve iterations=%dn",snes->iter,lits);
110: VecNorm(Ytmp,NORM_2,&nrm);
111: norm1 = nrm;
112: while(1) {
113: VecCopy(Ytmp,Y);
114: nrm = norm1;
116: /* Scale Y if need be and predict new value of F norm */
117: if (nrm >= delta) {
118: nrm = delta/nrm;
119: gpnorm = (1.0 - nrm)*fnorm;
120: cnorm = nrm;
121: PetscLogInfo(snes,"SNESSolve_TR: Scaling direction by %gn",nrm);
122: VecScale(&cnorm,Y);
123: nrm = gpnorm;
124: ynorm = delta;
125: } else {
126: gpnorm = 0.0;
127: PetscLogInfo(snes,"SNESSolve_TR: Direction is in Trust Regionn");
128: ynorm = nrm;
129: }
130: VecAYPX(&mone,X,Y); /* Y <- X - Y */
131: VecCopy(X,snes->vec_sol_update_always);
132: SNESComputeFunction(snes,Y,G); /* F(X) */
133: VecNorm(G,NORM_2,&gnorm); /* gnorm <- || g || */
134: if (fnorm == gpnorm) rho = 0.0;
135: else rho = (fnorm*fnorm - gnorm*gnorm)/(fnorm*fnorm - gpnorm*gpnorm);
137: /* Update size of trust region */
138: if (rho < neP->mu) delta *= neP->delta1;
139: else if (rho < neP->eta) delta *= neP->delta2;
140: else delta *= neP->delta3;
141: PetscLogInfo(snes,"SNESSolve_TR: fnorm=%g, gnorm=%g, ynorm=%gn",fnorm,gnorm,ynorm);
142: PetscLogInfo(snes,"SNESSolve_TR: gpred=%g, rho=%g, delta=%gn",gpnorm,rho,delta);
143: neP->delta = delta;
144: if (rho > neP->sigma) break;
145: PetscLogInfo(snes,"SNESSolve_TR: Trying again in smaller regionn");
146: /* check to see if progress is hopeless */
147: neP->itflag = 0;
148: (*snes->converged)(snes,xnorm,ynorm,fnorm,&reason,snes->cnvP);
149: if (reason) {
150: /* We're not progressing, so return with the current iterate */
151: SNESMonitor(snes,i+1,fnorm);
152: breakout = 1;
153: break;
154: }
155: snes->numFailures++;
156: }
157: if (!breakout) {
158: fnorm = gnorm;
159: PetscObjectTakeAccess(snes);
160: snes->iter = i+1;
161: snes->norm = fnorm;
162: PetscObjectGrantAccess(snes);
163: TMP = F; F = G; snes->vec_func_always = F; G = TMP;
164: TMP = X; X = Y; snes->vec_sol_always = X; Y = TMP;
165: VecNorm(X,NORM_2,&xnorm); /* xnorm = || X || */
166: SNESLogConvHistory(snes,fnorm,lits);
167: SNESMonitor(snes,i+1,fnorm);
169: /* Test for convergence */
170: neP->itflag = 1;
171: (*snes->converged)(snes,xnorm,ynorm,fnorm,&reason,snes->cnvP);
172: if (reason) {
173: break;
174: }
175: } else {
176: break;
177: }
178: }
179: /* Verify solution is in corect location */
180: if (X != snes->vec_sol) {
181: VecCopy(X,snes->vec_sol);
182: }
183: if (F != snes->vec_func) {
184: VecCopy(F,snes->vec_func);
185: }
186: snes->vec_sol_always = snes->vec_sol;
187: snes->vec_func_always = snes->vec_func;
188: if (i == maxits) {
189: PetscLogInfo(snes,"SNESSolve_TR: Maximum number of iterations has been reached: %dn",maxits);
190: i--;
191: reason = SNES_DIVERGED_MAX_IT;
192: }
193: *its = i+1;
194: PetscObjectTakeAccess(snes);
195: snes->reason = reason;
196: PetscObjectGrantAccess(snes);
197: return(0);
198: }
199: /*------------------------------------------------------------*/
200: static int SNESSetUp_TR(SNES snes)
201: {
205: snes->nwork = 4;
206: VecDuplicateVecs(snes->vec_sol,snes->nwork,&snes->work);
207: PetscLogObjectParents(snes,snes->nwork,snes->work);
208: snes->vec_sol_update_always = snes->work[3];
209: return(0);
210: }
211: /*------------------------------------------------------------*/
212: static int SNESDestroy_TR(SNES snes)
213: {
214: int ierr;
217: if (snes->nwork) {
218: VecDestroyVecs(snes->work,snes->nwork);
219: }
220: PetscFree(snes->data);
221: return(0);
222: }
223: /*------------------------------------------------------------*/
225: static int SNESSetFromOptions_TR(SNES snes)
226: {
227: SNES_TR *ctx = (SNES_TR *)snes->data;
228: int ierr;
231: PetscOptionsHead("SNES trust region options for nonlinear equations");
232: PetscOptionsReal("-snes_trtol","Trust region tolerance","SNESSetTrustRegionTolerance",snes->deltatol,&snes->deltatol,0);
233: PetscOptionsReal("-snes_tr_mu","mu","None",ctx->mu,&ctx->mu,0);
234: PetscOptionsReal("-snes_tr_eta","eta","None",ctx->eta,&ctx->eta,0);
235: PetscOptionsReal("-snes_tr_sigma","sigma","None",ctx->sigma,&ctx->sigma,0);
236: PetscOptionsReal("-snes_tr_delta0","delta0","None",ctx->delta0,&ctx->delta0,0);
237: PetscOptionsReal("-snes_tr_delta1","delta1","None",ctx->delta1,&ctx->delta1,0);
238: PetscOptionsReal("-snes_tr_delta2","delta2","None",ctx->delta2,&ctx->delta2,0);
239: PetscOptionsReal("-snes_tr_delta3","delta3","None",ctx->delta3,&ctx->delta3,0);
240: PetscOptionsTail();
241: return(0);
242: }
244: static int SNESView_TR(SNES snes,PetscViewer viewer)
245: {
246: SNES_TR *tr = (SNES_TR *)snes->data;
247: int ierr;
248: PetscTruth isascii;
251: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
252: if (isascii) {
253: PetscViewerASCIIPrintf(viewer," mu=%g, eta=%g, sigma=%gn",tr->mu,tr->eta,tr->sigma);
254: PetscViewerASCIIPrintf(viewer," delta0=%g, delta1=%g, delta2=%g, delta3=%gn",tr->delta0,tr->delta1,tr->delta2,tr->delta3);
255: } else {
256: SETERRQ1(1,"Viewer type %s not supported for SNES EQ TR",((PetscObject)viewer)->type_name);
257: }
258: return(0);
259: }
261: /* ---------------------------------------------------------------- */
262: /*@C
263: SNESConverged_TR - Monitors the convergence of the trust region
264: method SNESTR for solving systems of nonlinear equations (default).
266: Collective on SNES
268: Input Parameters:
269: + snes - the SNES context
270: . xnorm - 2-norm of current iterate
271: . pnorm - 2-norm of current step
272: . fnorm - 2-norm of function
273: - dummy - unused context
275: Output Parameter:
276: . reason - one of
277: $ SNES_CONVERGED_FNORM_ABS - (fnorm < atol),
278: $ SNES_CONVERGED_PNORM_RELATIVE - (pnorm < xtol*xnorm),
279: $ SNES_CONVERGED_FNORM_RELATIVE - (fnorm < rtol*fnorm0),
280: $ SNES_DIVERGED_FUNCTION_COUNT - (nfct > maxf),
281: $ SNES_DIVERGED_FNORM_NAN - (fnorm == NaN),
282: $ SNES_CONVERGED_TR_DELTA - (delta < xnorm*deltatol),
283: $ SNES_CONVERGED_ITERATING - (otherwise)
285: where
286: + delta - trust region paramenter
287: . deltatol - trust region size tolerance,
288: set with SNESSetTrustRegionTolerance()
289: . maxf - maximum number of function evaluations,
290: set with SNESSetTolerances()
291: . nfct - number of function evaluations,
292: . atol - absolute function norm tolerance,
293: set with SNESSetTolerances()
294: - xtol - relative function norm tolerance,
295: set with SNESSetTolerances()
297: Level: intermediate
299: .keywords: SNES, nonlinear, default, converged, convergence
301: .seealso: SNESSetConvergenceTest(), SNESEisenstatWalkerConverged()
302: @*/
303: int SNESConverged_TR(SNES snes,PetscReal xnorm,PetscReal pnorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
304: {
305: SNES_TR *neP = (SNES_TR *)snes->data;
306: int ierr;
309: if (fnorm != fnorm) {
310: PetscLogInfo(snes,"SNESConverged_TR:Failed to converged, function norm is NaNn");
311: *reason = SNES_DIVERGED_FNORM_NAN;
312: } else if (neP->delta < xnorm * snes->deltatol) {
313: PetscLogInfo(snes,"SNESConverged_TR: Converged due to trust region param %g<%g*%gn",neP->delta,xnorm,snes->deltatol);
314: *reason = SNES_CONVERGED_TR_DELTA;
315: } else if (neP->itflag) {
316: SNESConverged_LS(snes,xnorm,pnorm,fnorm,reason,dummy);
317: } else if (snes->nfuncs > snes->max_funcs) {
318: PetscLogInfo(snes,"SNESConverged_TR: Exceeded maximum number of function evaluations: %d > %dn",snes->nfuncs,snes->max_funcs);
319: *reason = SNES_DIVERGED_FUNCTION_COUNT;
320: } else {
321: *reason = SNES_CONVERGED_ITERATING;
322: }
323: return(0);
324: }
325: /* ------------------------------------------------------------ */
326: EXTERN_C_BEGIN
327: int SNESCreate_TR(SNES snes)
328: {
329: SNES_TR *neP;
330: int ierr;
333: snes->setup = SNESSetUp_TR;
334: snes->solve = SNESSolve_TR;
335: snes->destroy = SNESDestroy_TR;
336: snes->converged = SNESConverged_TR;
337: snes->setfromoptions = SNESSetFromOptions_TR;
338: snes->view = SNESView_TR;
339: snes->nwork = 0;
340:
341: ierr = PetscNew(SNES_TR,&neP);
342: PetscLogObjectMemory(snes,sizeof(SNES_TR));
343: snes->data = (void*)neP;
344: neP->mu = 0.25;
345: neP->eta = 0.75;
346: neP->delta = 0.0;
347: neP->delta0 = 0.2;
348: neP->delta1 = 0.3;
349: neP->delta2 = 0.75;
350: neP->delta3 = 2.0;
351: neP->sigma = 0.0001;
352: neP->itflag = 0;
353: neP->rnorm0 = 0;
354: neP->ttol = 0;
355: return(0);
356: }
357: EXTERN_C_END