Actual source code: tr.c
1: #define PETSCSNES_DLL
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: */
11: PetscErrorCode SNES_TR_KSPConverged_Private(KSP ksp,PetscInt n,PetscReal rnorm,KSPConvergedReason *reason,void *ctx)
12: {
13: SNES snes = (SNES) ctx;
14: SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx;
15: SNES_TR *neP = (SNES_TR*)snes->data;
16: Vec x;
17: PetscReal nrm;
18: PetscErrorCode ierr;
21: if (snes->ksp_ewconv) {
22: if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Eisenstat-Walker convergence context not created");
23: if (!n) {SNES_KSP_EW_ComputeRelativeTolerance_Private(snes,ksp);}
24: kctx->lresid_last = rnorm;
25: }
26: KSPDefaultConverged(ksp,n,rnorm,reason,ctx);
27: if (*reason) {
28: PetscInfo2(snes,"regular convergence test KSP iterations=%D, rnorm=%G\n",n,rnorm);
29: }
31: /* Determine norm of solution */
32: KSPBuildSolution(ksp,0,&x);
33: VecNorm(x,NORM_2,&nrm);
34: if (nrm >= neP->delta) {
35: PetscInfo2(snes,"KSP iterations=%D, rnorm=%G\n",n,rnorm);
36: PetscInfo2(snes,"Ending linear iteration early, delta=%G, length=%G\n",neP->delta,nrm);
37: *reason = KSP_CONVERGED_STEP_LENGTH;
38: }
39: return(0);
40: }
42: /*
43: SNESSolve_TR - Implements Newton's Method with a very simple trust
44: region approach for solving systems of nonlinear equations.
46:
47: */
50: static PetscErrorCode SNESSolve_TR(SNES snes)
51: {
52: SNES_TR *neP = (SNES_TR*)snes->data;
53: Vec X,F,Y,G,TMP,Ytmp;
54: PetscErrorCode ierr;
55: PetscInt maxits,i,lits;
56: MatStructure flg = DIFFERENT_NONZERO_PATTERN;
57: PetscReal rho,fnorm,gnorm,gpnorm,xnorm,delta,nrm,ynorm,norm1;
58: PetscScalar cnorm;
59: KSP ksp;
60: SNESConvergedReason reason;
61: PetscTruth conv,breakout = PETSC_FALSE;
64: maxits = snes->max_its; /* maximum number of iterations */
65: X = snes->vec_sol; /* solution vector */
66: F = snes->vec_func; /* residual vector */
67: Y = snes->work[0]; /* work vectors */
68: G = snes->work[1];
69: Ytmp = snes->work[2];
71: PetscObjectTakeAccess(snes);
72: snes->iter = 0;
73: PetscObjectGrantAccess(snes);
74: VecNorm(X,NORM_2,&xnorm); /* xnorm = || X || */
76: SNESComputeFunction(snes,X,F); /* F(X) */
77: VecNorm(F,NORM_2,&fnorm); /* fnorm <- || F || */
78: PetscObjectTakeAccess(snes);
79: snes->norm = fnorm;
80: PetscObjectGrantAccess(snes);
81: delta = neP->delta0*fnorm;
82: neP->delta = delta;
83: SNESLogConvHistory(snes,fnorm,0);
84: SNESMonitor(snes,0,fnorm);
85: SNESGetKSP(snes,&ksp);
87: if (fnorm < snes->abstol) {snes->reason = SNES_CONVERGED_FNORM_ABS; return(0);}
89: /* set parameter for default relative tolerance convergence test */
90: snes->ttol = fnorm*snes->rtol;
92: /* Set the stopping criteria to use the More' trick. */
93: PetscOptionsHasName(PETSC_NULL,"-snes_tr_ksp_regular_convergence_test",&conv);
94: if (!conv) {
95: KSPSetConvergenceTest(ksp,SNES_TR_KSPConverged_Private,(void*)snes);
96: PetscInfo(snes,"Using Krylov convergence test SNES_TR_KSPConverged_Private\n");
97: }
98:
99: for (i=0; i<maxits; i++) {
101: /* Call general purpose update function */
102: if (snes->ops->update) {
103: (*snes->ops->update)(snes, snes->iter);
104: }
106: SNESComputeJacobian(snes,X,&snes->jacobian,&snes->jacobian_pre,&flg);
107: KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,flg);
109: /* Solve J Y = F, where J is Jacobian matrix */
110: KSPSolve(snes->ksp,F,Ytmp);
111: KSPGetIterationNumber(ksp,&lits);
112: snes->linear_its += lits;
113: PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);
114: VecNorm(Ytmp,NORM_2,&nrm);
115: norm1 = nrm;
116: while(1) {
117: VecCopy(Ytmp,Y);
118: nrm = norm1;
120: /* Scale Y if need be and predict new value of F norm */
121: if (nrm >= delta) {
122: nrm = delta/nrm;
123: gpnorm = (1.0 - nrm)*fnorm;
124: cnorm = nrm;
125: PetscInfo1(snes,"Scaling direction by %G\n",nrm);
126: VecScale(Y,cnorm);
127: nrm = gpnorm;
128: ynorm = delta;
129: } else {
130: gpnorm = 0.0;
131: PetscInfo(snes,"Direction is in Trust Region\n");
132: ynorm = nrm;
133: }
134: VecAYPX(Y,-1.0,X); /* Y <- X - Y */
135: VecCopy(X,snes->vec_sol_update_always);
136: SNESComputeFunction(snes,Y,G); /* F(X) */
137: VecNorm(G,NORM_2,&gnorm); /* gnorm <- || g || */
138: if (fnorm == gpnorm) rho = 0.0;
139: else rho = (fnorm*fnorm - gnorm*gnorm)/(fnorm*fnorm - gpnorm*gpnorm);
141: /* Update size of trust region */
142: if (rho < neP->mu) delta *= neP->delta1;
143: else if (rho < neP->eta) delta *= neP->delta2;
144: else delta *= neP->delta3;
145: PetscInfo3(snes,"fnorm=%G, gnorm=%G, ynorm=%G\n",fnorm,gnorm,ynorm);
146: PetscInfo3(snes,"gpred=%G, rho=%G, delta=%G\n",gpnorm,rho,delta);
147: neP->delta = delta;
148: if (rho > neP->sigma) break;
149: PetscInfo(snes,"Trying again in smaller region\n");
150: /* check to see if progress is hopeless */
151: neP->itflag = PETSC_FALSE;
152: (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP);
153: if (reason) {
154: /* We're not progressing, so return with the current iterate */
155: SNESMonitor(snes,i+1,fnorm);
156: breakout = PETSC_TRUE;
157: break;
158: }
159: snes->numFailures++;
160: }
161: if (!breakout) {
162: fnorm = gnorm;
163: PetscObjectTakeAccess(snes);
164: snes->iter = i+1;
165: snes->norm = fnorm;
166: PetscObjectGrantAccess(snes);
167: TMP = F; F = G; snes->vec_func_always = F; G = TMP;
168: TMP = X; X = Y; snes->vec_sol_always = X; Y = TMP;
169: VecNorm(X,NORM_2,&xnorm); /* xnorm = || X || */
170: SNESLogConvHistory(snes,fnorm,lits);
171: SNESMonitor(snes,i+1,fnorm);
173: /* Test for convergence */
174: neP->itflag = PETSC_TRUE;
175: (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP);
176: if (reason) {
177: break;
178: }
179: } else {
180: break;
181: }
182: }
183: /* Verify solution is in corect location */
184: if (X != snes->vec_sol) {
185: VecCopy(X,snes->vec_sol);
186: }
187: if (F != snes->vec_func) {
188: VecCopy(F,snes->vec_func);
189: }
190: snes->vec_sol_always = snes->vec_sol;
191: snes->vec_func_always = snes->vec_func;
192: if (i == maxits) {
193: PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",maxits);
194: reason = SNES_DIVERGED_MAX_IT;
195: }
196: PetscObjectTakeAccess(snes);
197: snes->reason = reason;
198: PetscObjectGrantAccess(snes);
199: return(0);
200: }
201: /*------------------------------------------------------------*/
204: static PetscErrorCode SNESSetUp_TR(SNES snes)
205: {
209: if (!snes->work) {
210: snes->nwork = 4;
211: VecDuplicateVecs(snes->vec_sol,snes->nwork,&snes->work);
212: PetscLogObjectParents(snes,snes->nwork,snes->work);
213: }
214: snes->vec_sol_update_always = snes->work[3];
215: return(0);
216: }
217: /*------------------------------------------------------------*/
220: static PetscErrorCode SNESDestroy_TR(SNES snes)
221: {
225: if (snes->nwork) {
226: VecDestroyVecs(snes->work,snes->nwork);
227: }
228: PetscFree(snes->data);
229: return(0);
230: }
231: /*------------------------------------------------------------*/
235: static PetscErrorCode SNESSetFromOptions_TR(SNES snes)
236: {
237: SNES_TR *ctx = (SNES_TR *)snes->data;
241: PetscOptionsHead("SNES trust region options for nonlinear equations");
242: PetscOptionsReal("-snes_trtol","Trust region tolerance","SNESSetTrustRegionTolerance",snes->deltatol,&snes->deltatol,0);
243: PetscOptionsReal("-snes_tr_mu","mu","None",ctx->mu,&ctx->mu,0);
244: PetscOptionsReal("-snes_tr_eta","eta","None",ctx->eta,&ctx->eta,0);
245: PetscOptionsReal("-snes_tr_sigma","sigma","None",ctx->sigma,&ctx->sigma,0);
246: PetscOptionsReal("-snes_tr_delta0","delta0","None",ctx->delta0,&ctx->delta0,0);
247: PetscOptionsReal("-snes_tr_delta1","delta1","None",ctx->delta1,&ctx->delta1,0);
248: PetscOptionsReal("-snes_tr_delta2","delta2","None",ctx->delta2,&ctx->delta2,0);
249: PetscOptionsReal("-snes_tr_delta3","delta3","None",ctx->delta3,&ctx->delta3,0);
250: PetscOptionsTail();
251: return(0);
252: }
256: static PetscErrorCode SNESView_TR(SNES snes,PetscViewer viewer)
257: {
258: SNES_TR *tr = (SNES_TR *)snes->data;
260: PetscTruth iascii;
263: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
264: if (iascii) {
265: PetscViewerASCIIPrintf(viewer," mu=%G, eta=%G, sigma=%G\n",tr->mu,tr->eta,tr->sigma);
266: PetscViewerASCIIPrintf(viewer," delta0=%G, delta1=%G, delta2=%G, delta3=%G\n",tr->delta0,tr->delta1,tr->delta2,tr->delta3);
267: } else {
268: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for SNES EQ TR",((PetscObject)viewer)->type_name);
269: }
270: return(0);
271: }
273: /* ---------------------------------------------------------------- */
276: /*@C
277: SNESConverged_TR - Monitors the convergence of the trust region
278: method SNESTR for solving systems of nonlinear equations (default).
280: Collective on SNES
282: Input Parameters:
283: + snes - the SNES context
284: . xnorm - 2-norm of current iterate
285: . pnorm - 2-norm of current step
286: . fnorm - 2-norm of function
287: - dummy - unused context
289: Output Parameter:
290: . reason - one of
291: $ SNES_CONVERGED_FNORM_ABS - (fnorm < abstol),
292: $ SNES_CONVERGED_PNORM_RELATIVE - (pnorm < xtol*xnorm),
293: $ SNES_CONVERGED_FNORM_RELATIVE - (fnorm < rtol*fnorm0),
294: $ SNES_DIVERGED_FUNCTION_COUNT - (nfct > maxf),
295: $ SNES_DIVERGED_FNORM_NAN - (fnorm == NaN),
296: $ SNES_CONVERGED_TR_DELTA - (delta < xnorm*deltatol),
297: $ SNES_CONVERGED_ITERATING - (otherwise)
299: where
300: + delta - trust region paramenter
301: . deltatol - trust region size tolerance,
302: set with SNESSetTrustRegionTolerance()
303: . maxf - maximum number of function evaluations,
304: set with SNESSetTolerances()
305: . nfct - number of function evaluations,
306: . abstol - absolute function norm tolerance,
307: set with SNESSetTolerances()
308: - xtol - relative function norm tolerance,
309: set with SNESSetTolerances()
311: Level: intermediate
313: .keywords: SNES, nonlinear, default, converged, convergence
315: .seealso: SNESSetConvergenceTest(), SNESEisenstatWalkerConverged()
316: @*/
317: PetscErrorCode SNESConverged_TR(SNES snes,PetscInt it,PetscReal xnorm,PetscReal pnorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
318: {
319: SNES_TR *neP = (SNES_TR *)snes->data;
323: if (fnorm != fnorm) {
324: PetscInfo(snes,"Failed to converged, function norm is NaN\n");
325: *reason = SNES_DIVERGED_FNORM_NAN;
326: } else if (neP->delta < xnorm * snes->deltatol) {
327: PetscInfo3(snes,"Converged due to trust region param %G<%G*%G\n",neP->delta,xnorm,snes->deltatol);
328: *reason = SNES_CONVERGED_TR_DELTA;
329: } else if (neP->itflag) {
330: SNESConverged_LS(snes,it,xnorm,pnorm,fnorm,reason,dummy);
331: } else if (snes->nfuncs >= snes->max_funcs) {
332: PetscInfo2(snes,"Exceeded maximum number of function evaluations: %D > %D\n",snes->nfuncs,snes->max_funcs);
333: *reason = SNES_DIVERGED_FUNCTION_COUNT;
334: } else {
335: *reason = SNES_CONVERGED_ITERATING;
336: }
337: return(0);
338: }
339: /* ------------------------------------------------------------ */
340: /*MC
341: SNESTR - Newton based nonlinear solver that uses a trust region
343: Options Database:
344: + -snes_trtol <tol> Trust region tolerance
345: . -snes_tr_mu <mu>
346: . -snes_tr_eta <eta>
347: . -snes_tr_sigma <sigma>
348: . -snes_tr_delta0 <delta0>
349: . -snes_tr_delta1 <delta1>
350: . -snes_tr_delta2 <delta2>
351: - -snes_tr_delta3 <delta3>
353: The basic algorithm is taken from "The Minpack Project", by More',
354: Sorensen, Garbow, Hillstrom, pages 88-111 of "Sources and Development
355: of Mathematical Software", Wayne Cowell, editor.
357: This is intended as a model implementation, since it does not
358: necessarily have many of the bells and whistles of other
359: implementations.
361: Level: intermediate
363: .seealso: SNESCreate(), SNES, SNESSetType(), SNESLS, SNESSetTrustRegionTolerance()
365: M*/
369: PetscErrorCode SNESCreate_TR(SNES snes)
370: {
371: SNES_TR *neP;
375: snes->ops->setup = SNESSetUp_TR;
376: snes->ops->solve = SNESSolve_TR;
377: snes->ops->destroy = SNESDestroy_TR;
378: snes->ops->converged = SNESConverged_TR;
379: snes->ops->setfromoptions = SNESSetFromOptions_TR;
380: snes->ops->view = SNESView_TR;
381: snes->nwork = 0;
382:
383: ierr = PetscNew(SNES_TR,&neP);
384: PetscLogObjectMemory(snes,sizeof(SNES_TR));
385: snes->data = (void*)neP;
386: neP->mu = 0.25;
387: neP->eta = 0.75;
388: neP->delta = 0.0;
389: neP->delta0 = 0.2;
390: neP->delta1 = 0.3;
391: neP->delta2 = 0.75;
392: neP->delta3 = 2.0;
393: neP->sigma = 0.0001;
394: neP->itflag = PETSC_FALSE;
395: neP->rnorm0 = 0;
396: neP->ttol = 0;
397: return(0);
398: }