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_TR *neP = (SNES_TR*)snes->data;
15: Vec x;
16: PetscReal nrm;
17: PetscErrorCode ierr;
20: KSPDefaultConverged(ksp,n,rnorm,reason,ctx);
21: if (*reason) {
22: PetscInfo2(snes,"default convergence test KSP iterations=%D, rnorm=%G\n",n,rnorm);
23: }
24: /* Determine norm of solution */
25: KSPBuildSolution(ksp,0,&x);
26: VecNorm(x,NORM_2,&nrm);
27: if (nrm >= neP->delta) {
28: PetscInfo2(snes,"Ending linear iteration early, delta=%G, length=%G\n",neP->delta,nrm);
29: *reason = KSP_CONVERGED_STEP_LENGTH;
30: }
31: return(0);
32: }
34: /*
35: SNESSolve_TR - Implements Newton's Method with a very simple trust
36: region approach for solving systems of nonlinear equations.
38:
39: */
42: static PetscErrorCode SNESSolve_TR(SNES snes)
43: {
44: SNES_TR *neP = (SNES_TR*)snes->data;
45: Vec X,F,Y,G,TMP,Ytmp;
46: PetscErrorCode ierr;
47: PetscInt maxits,i,lits;
48: MatStructure flg = DIFFERENT_NONZERO_PATTERN;
49: PetscReal rho,fnorm,gnorm,gpnorm,xnorm,delta,nrm,ynorm,norm1;
50: PetscScalar cnorm;
51: KSP ksp;
52: SNESConvergedReason reason = SNES_CONVERGED_ITERATING;
53: PetscTruth conv,breakout = PETSC_FALSE;
56: maxits = snes->max_its; /* maximum number of iterations */
57: X = snes->vec_sol; /* solution vector */
58: F = snes->vec_func; /* residual vector */
59: Y = snes->work[0]; /* work vectors */
60: G = snes->work[1];
61: Ytmp = snes->work[2];
63: PetscObjectTakeAccess(snes);
64: snes->iter = 0;
65: PetscObjectGrantAccess(snes);
67: SNESComputeFunction(snes,X,F); /* F(X) */
68: VecNorm(F,NORM_2,&fnorm); /* fnorm <- || F || */
69: PetscObjectTakeAccess(snes);
70: snes->norm = fnorm;
71: PetscObjectGrantAccess(snes);
72: delta = neP->delta0*fnorm;
73: neP->delta = delta;
74: SNESLogConvHistory(snes,fnorm,0);
75: SNESMonitor(snes,0,fnorm);
77: /* set parameter for default relative tolerance convergence test */
78: snes->ttol = fnorm*snes->rtol;
79:
80: /* XXX Sould we use snes->ops->converged like in SNESLS ?*/
81: if (fnorm < snes->abstol) {snes->reason = SNES_CONVERGED_FNORM_ABS; return(0);}
82: if (snes->ops->converged) {
83: VecNorm(X,NORM_2,&xnorm); /* xnorm = || X || */
84: }
86: /* Set the stopping criteria to use the More' trick. */
87: PetscOptionsHasName(PETSC_NULL,"-snes_tr_ksp_regular_convergence_test",&conv);
88: if (!conv) {
89: SNESGetKSP(snes,&ksp);
90: KSPSetConvergenceTest(ksp,SNES_TR_KSPConverged_Private,(void*)snes);
91: PetscInfo(snes,"Using Krylov convergence test SNES_TR_KSPConverged_Private\n");
92: }
93:
94: for (i=0; i<maxits; i++) {
96: /* Call general purpose update function */
97: if (snes->ops->update) {
98: (*snes->ops->update)(snes, snes->iter);
99: }
101: SNESComputeJacobian(snes,X,&snes->jacobian,&snes->jacobian_pre,&flg);
102: KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,flg);
104: /* Solve J Y = F, where J is Jacobian matrix */
105: SNES_KSPSolve(snes,snes->ksp,F,Ytmp);
106: KSPGetIterationNumber(snes->ksp,&lits);
107: snes->linear_its += lits;
108: PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);
109: VecNorm(Ytmp,NORM_2,&nrm);
110: norm1 = nrm;
111: while(1) {
112: VecCopy(Ytmp,Y);
113: nrm = norm1;
115: /* Scale Y if need be and predict new value of F norm */
116: if (nrm >= delta) {
117: nrm = delta/nrm;
118: gpnorm = (1.0 - nrm)*fnorm;
119: cnorm = nrm;
120: PetscInfo1(snes,"Scaling direction by %G\n",nrm);
121: VecScale(Y,cnorm);
122: nrm = gpnorm;
123: ynorm = delta;
124: } else {
125: gpnorm = 0.0;
126: PetscInfo(snes,"Direction is in Trust Region\n");
127: ynorm = nrm;
128: }
129: VecAYPX(Y,-1.0,X); /* Y <- X - Y */
130: VecCopy(X,snes->vec_sol_update_always);
131: SNESComputeFunction(snes,Y,G); /* F(X) */
132: VecNorm(G,NORM_2,&gnorm); /* gnorm <- || g || */
133: if (fnorm == gpnorm) rho = 0.0;
134: else rho = (fnorm*fnorm - gnorm*gnorm)/(fnorm*fnorm - gpnorm*gpnorm);
136: /* Update size of trust region */
137: if (rho < neP->mu) delta *= neP->delta1;
138: else if (rho < neP->eta) delta *= neP->delta2;
139: else delta *= neP->delta3;
140: PetscInfo3(snes,"fnorm=%G, gnorm=%G, ynorm=%G\n",fnorm,gnorm,ynorm);
141: PetscInfo3(snes,"gpred=%G, rho=%G, delta=%G\n",gpnorm,rho,delta);
142: neP->delta = delta;
143: if (rho > neP->sigma) break;
144: PetscInfo(snes,"Trying again in smaller region\n");
145: /* check to see if progress is hopeless */
146: neP->itflag = PETSC_FALSE;
147: if (snes->ops->converged) {
148: (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP);
149: }
150: if (reason) {
151: /* We're not progressing, so return with the current iterate */
152: SNESMonitor(snes,i+1,fnorm);
153: breakout = PETSC_TRUE;
154: break;
155: }
156: snes->numFailures++;
157: }
158: if (!breakout) {
159: fnorm = gnorm;
160: PetscObjectTakeAccess(snes);
161: snes->iter = i+1;
162: snes->norm = fnorm;
163: PetscObjectGrantAccess(snes);
164: TMP = F; F = G; snes->vec_func_always = F; G = TMP;
165: TMP = X; X = Y; snes->vec_sol_always = X; Y = TMP;
166: SNESLogConvHistory(snes,fnorm,lits);
167: SNESMonitor(snes,i+1,fnorm);
168: /* Test for convergence */
169: neP->itflag = PETSC_TRUE;
170: if (snes->ops->converged) {
171: VecNorm(X,NORM_2,&xnorm); /* xnorm = || X || */
172: (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP);
173: }
174: if (reason) break;
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: PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",maxits);
190: reason = SNES_DIVERGED_MAX_IT;
191: }
192: PetscObjectTakeAccess(snes);
193: snes->reason = reason;
194: PetscObjectGrantAccess(snes);
195: return(0);
196: }
197: /*------------------------------------------------------------*/
200: static PetscErrorCode SNESSetUp_TR(SNES snes)
201: {
205: if (!snes->work) {
206: snes->nwork = 4;
207: VecDuplicateVecs(snes->vec_sol,snes->nwork,&snes->work);
208: PetscLogObjectParents(snes,snes->nwork,snes->work);
209: }
210: snes->vec_sol_update_always = snes->work[3];
211: return(0);
212: }
213: /*------------------------------------------------------------*/
216: static PetscErrorCode SNESDestroy_TR(SNES snes)
217: {
221: if (snes->nwork) {
222: VecDestroyVecs(snes->work,snes->nwork);
223: }
224: PetscFree(snes->data);
225: return(0);
226: }
227: /*------------------------------------------------------------*/
231: static PetscErrorCode SNESSetFromOptions_TR(SNES snes)
232: {
233: SNES_TR *ctx = (SNES_TR *)snes->data;
237: PetscOptionsHead("SNES trust region options for nonlinear equations");
238: PetscOptionsReal("-snes_trtol","Trust region tolerance","SNESSetTrustRegionTolerance",snes->deltatol,&snes->deltatol,0);
239: PetscOptionsReal("-snes_tr_mu","mu","None",ctx->mu,&ctx->mu,0);
240: PetscOptionsReal("-snes_tr_eta","eta","None",ctx->eta,&ctx->eta,0);
241: PetscOptionsReal("-snes_tr_sigma","sigma","None",ctx->sigma,&ctx->sigma,0);
242: PetscOptionsReal("-snes_tr_delta0","delta0","None",ctx->delta0,&ctx->delta0,0);
243: PetscOptionsReal("-snes_tr_delta1","delta1","None",ctx->delta1,&ctx->delta1,0);
244: PetscOptionsReal("-snes_tr_delta2","delta2","None",ctx->delta2,&ctx->delta2,0);
245: PetscOptionsReal("-snes_tr_delta3","delta3","None",ctx->delta3,&ctx->delta3,0);
246: PetscOptionsTail();
247: return(0);
248: }
252: static PetscErrorCode SNESView_TR(SNES snes,PetscViewer viewer)
253: {
254: SNES_TR *tr = (SNES_TR *)snes->data;
256: PetscTruth iascii;
259: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
260: if (iascii) {
261: PetscViewerASCIIPrintf(viewer," mu=%G, eta=%G, sigma=%G\n",tr->mu,tr->eta,tr->sigma);
262: PetscViewerASCIIPrintf(viewer," delta0=%G, delta1=%G, delta2=%G, delta3=%G\n",tr->delta0,tr->delta1,tr->delta2,tr->delta3);
263: } else {
264: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for SNES EQ TR",((PetscObject)viewer)->type_name);
265: }
266: return(0);
267: }
269: /* ---------------------------------------------------------------- */
272: /*@C
273: SNESConverged_TR - Monitors the convergence of the trust region
274: method SNESTR for solving systems of nonlinear equations (default).
276: Collective on SNES
278: Input Parameters:
279: + snes - the SNES context
280: . xnorm - 2-norm of current iterate
281: . pnorm - 2-norm of current step
282: . fnorm - 2-norm of function
283: - dummy - unused context
285: Output Parameter:
286: . reason - one of
287: $ SNES_CONVERGED_FNORM_ABS - (fnorm < abstol),
288: $ SNES_CONVERGED_PNORM_RELATIVE - (pnorm < xtol*xnorm),
289: $ SNES_CONVERGED_FNORM_RELATIVE - (fnorm < rtol*fnorm0),
290: $ SNES_DIVERGED_FUNCTION_COUNT - (nfct > maxf),
291: $ SNES_DIVERGED_FNORM_NAN - (fnorm == NaN),
292: $ SNES_CONVERGED_TR_DELTA - (delta < xnorm*deltatol),
293: $ SNES_CONVERGED_ITERATING - (otherwise)
295: where
296: + delta - trust region paramenter
297: . deltatol - trust region size tolerance,
298: set with SNESSetTrustRegionTolerance()
299: . maxf - maximum number of function evaluations,
300: set with SNESSetTolerances()
301: . nfct - number of function evaluations,
302: . abstol - absolute function norm tolerance,
303: set with SNESSetTolerances()
304: - xtol - relative function norm tolerance,
305: set with SNESSetTolerances()
307: Level: intermediate
309: .keywords: SNES, nonlinear, default, converged, convergence
311: .seealso: SNESSetConvergenceTest(), SNESEisenstatWalkerConverged()
312: @*/
313: PetscErrorCode SNESConverged_TR(SNES snes,PetscInt it,PetscReal xnorm,PetscReal pnorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
314: {
315: SNES_TR *neP;
322: 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: SNESDefaultConverged(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: }