Actual source code: tr.c
petsc-3.3-p7 2013-05-11
1:
2: #include <../src/snes/impls/tr/trimpl.h> /*I "petscsnes.h" I*/
4: typedef struct {
5: void *ctx;
6: SNES snes;
7: } SNES_TR_KSPConverged_Ctx;
9: /*
10: This convergence test determines if the two norm of the
11: solution lies outside the trust region, if so it halts.
12: */
15: PetscErrorCode SNES_TR_KSPConverged_Private(KSP ksp,PetscInt n,PetscReal rnorm,KSPConvergedReason *reason,void *cctx)
16: {
17: SNES_TR_KSPConverged_Ctx *ctx = (SNES_TR_KSPConverged_Ctx *)cctx;
18: SNES snes = ctx->snes;
19: SNES_TR *neP = (SNES_TR*)snes->data;
20: Vec x;
21: PetscReal nrm;
22: PetscErrorCode ierr;
25: KSPDefaultConverged(ksp,n,rnorm,reason,ctx->ctx);
26: if (*reason) {
27: PetscInfo2(snes,"default convergence test KSP iterations=%D, rnorm=%G\n",n,rnorm);
28: }
29: /* Determine norm of solution */
30: KSPBuildSolution(ksp,0,&x);
31: VecNorm(x,NORM_2,&nrm);
32: if (nrm >= neP->delta) {
33: PetscInfo2(snes,"Ending linear iteration early, delta=%G, length=%G\n",neP->delta,nrm);
34: *reason = KSP_CONVERGED_STEP_LENGTH;
35: }
36: return(0);
37: }
41: PetscErrorCode SNES_TR_KSPConverged_Destroy(void *cctx)
42: {
43: SNES_TR_KSPConverged_Ctx *ctx = (SNES_TR_KSPConverged_Ctx *)cctx;
44: PetscErrorCode ierr;
47: KSPDefaultConvergedDestroy(ctx->ctx);
48: PetscFree(ctx);
49: return(0);
50: }
52: /* ---------------------------------------------------------------- */
55: /*
56: SNES_TR_Converged_Private -test convergence JUST for
57: the trust region tolerance.
59: */
60: static PetscErrorCode SNES_TR_Converged_Private(SNES snes,PetscInt it,PetscReal xnorm,PetscReal pnorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
61: {
62: SNES_TR *neP = (SNES_TR *)snes->data;
66: *reason = SNES_CONVERGED_ITERATING;
67: if (neP->delta < xnorm * snes->deltatol) {
68: PetscInfo3(snes,"Converged due to trust region param %G<%G*%G\n",neP->delta,xnorm,snes->deltatol);
69: *reason = SNES_CONVERGED_TR_DELTA;
70: } else if (snes->nfuncs >= snes->max_funcs) {
71: PetscInfo1(snes,"Exceeded maximum number of function evaluations: %D\n",snes->max_funcs);
72: *reason = SNES_DIVERGED_FUNCTION_COUNT;
73: }
74: return(0);
75: }
78: /*
79: SNESSolve_TR - Implements Newton's Method with a very simple trust
80: region approach for solving systems of nonlinear equations.
82:
83: */
86: static PetscErrorCode SNESSolve_TR(SNES snes)
87: {
88: SNES_TR *neP = (SNES_TR*)snes->data;
89: Vec X,F,Y,G,Ytmp;
90: PetscErrorCode ierr;
91: PetscInt maxits,i,lits;
92: MatStructure flg = DIFFERENT_NONZERO_PATTERN;
93: PetscReal rho,fnorm,gnorm,gpnorm,xnorm=0,delta,nrm,ynorm,norm1;
94: PetscScalar cnorm;
95: KSP ksp;
96: SNESConvergedReason reason = SNES_CONVERGED_ITERATING;
97: PetscBool conv = PETSC_FALSE,breakout = PETSC_FALSE;
98: PetscBool domainerror;
101: maxits = snes->max_its; /* maximum number of iterations */
102: X = snes->vec_sol; /* solution vector */
103: F = snes->vec_func; /* residual vector */
104: Y = snes->work[0]; /* work vectors */
105: G = snes->work[1];
106: Ytmp = snes->work[2];
108: PetscObjectTakeAccess(snes);
109: snes->iter = 0;
110: PetscObjectGrantAccess(snes);
112: if (!snes->vec_func_init_set) {
113: SNESComputeFunction(snes,X,F); /* F(X) */
114: SNESGetFunctionDomainError(snes, &domainerror);
115: if (domainerror) {
116: snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
117: return(0);
118: }
119: } else {
120: snes->vec_func_init_set = PETSC_FALSE;
121: }
123: if (!snes->norm_init_set) {
124: VecNorm(F,NORM_2,&fnorm); /* fnorm <- || F || */
125: if (PetscIsInfOrNanReal(fnorm)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided compute function generated a Not-a-Number");
126: } else {
127: fnorm = snes->norm_init;
128: snes->norm_init_set = PETSC_FALSE;
129: }
131: PetscObjectTakeAccess(snes);
132: snes->norm = fnorm;
133: PetscObjectGrantAccess(snes);
134: delta = neP->delta0*fnorm;
135: neP->delta = delta;
136: SNESLogConvHistory(snes,fnorm,0);
137: SNESMonitor(snes,0,fnorm);
139: /* set parameter for default relative tolerance convergence test */
140: snes->ttol = fnorm*snes->rtol;
141: /* test convergence */
142: (*snes->ops->converged)(snes,snes->iter,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
143: if (snes->reason) return(0);
145: /* Set the stopping criteria to use the More' trick. */
146: PetscOptionsGetBool(PETSC_NULL,"-snes_tr_ksp_regular_convergence_test",&conv,PETSC_NULL);
147: if (!conv) {
148: SNES_TR_KSPConverged_Ctx *ctx;
149: SNESGetKSP(snes,&ksp);
150: PetscNew(SNES_TR_KSPConverged_Ctx,&ctx);
151: ctx->snes = snes;
152: KSPDefaultConvergedCreate(&ctx->ctx);
153: KSPSetConvergenceTest(ksp,SNES_TR_KSPConverged_Private,ctx,SNES_TR_KSPConverged_Destroy);
154: PetscInfo(snes,"Using Krylov convergence test SNES_TR_KSPConverged_Private\n");
155: }
156:
157: for (i=0; i<maxits; i++) {
159: /* Call general purpose update function */
160: if (snes->ops->update) {
161: (*snes->ops->update)(snes, snes->iter);
162: }
164: /* Solve J Y = F, where J is Jacobian matrix */
165: SNESComputeJacobian(snes,X,&snes->jacobian,&snes->jacobian_pre,&flg);
166: KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,flg);
167: SNES_KSPSolve(snes,snes->ksp,F,Ytmp);
168: KSPGetIterationNumber(snes->ksp,&lits);
169: snes->linear_its += lits;
170: PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);
171: VecNorm(Ytmp,NORM_2,&nrm);
172: norm1 = nrm;
173: while(1) {
174: VecCopy(Ytmp,Y);
175: nrm = norm1;
177: /* Scale Y if need be and predict new value of F norm */
178: if (nrm >= delta) {
179: nrm = delta/nrm;
180: gpnorm = (1.0 - nrm)*fnorm;
181: cnorm = nrm;
182: PetscInfo1(snes,"Scaling direction by %G\n",nrm);
183: VecScale(Y,cnorm);
184: nrm = gpnorm;
185: ynorm = delta;
186: } else {
187: gpnorm = 0.0;
188: PetscInfo(snes,"Direction is in Trust Region\n");
189: ynorm = nrm;
190: }
191: VecAYPX(Y,-1.0,X); /* Y <- X - Y */
192: VecCopy(X,snes->vec_sol_update);
193: SNESComputeFunction(snes,Y,G); /* F(X) */
194: VecNorm(G,NORM_2,&gnorm); /* gnorm <- || g || */
195: if (fnorm == gpnorm) rho = 0.0;
196: else rho = (fnorm*fnorm - gnorm*gnorm)/(fnorm*fnorm - gpnorm*gpnorm);
198: /* Update size of trust region */
199: if (rho < neP->mu) delta *= neP->delta1;
200: else if (rho < neP->eta) delta *= neP->delta2;
201: else delta *= neP->delta3;
202: PetscInfo3(snes,"fnorm=%G, gnorm=%G, ynorm=%G\n",fnorm,gnorm,ynorm);
203: PetscInfo3(snes,"gpred=%G, rho=%G, delta=%G\n",gpnorm,rho,delta);
204: neP->delta = delta;
205: if (rho > neP->sigma) break;
206: PetscInfo(snes,"Trying again in smaller region\n");
207: /* check to see if progress is hopeless */
208: neP->itflag = PETSC_FALSE;
209: SNES_TR_Converged_Private(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP);
210: if (!reason) { (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP); }
211: if (reason) {
212: /* We're not progressing, so return with the current iterate */
213: SNESMonitor(snes,i+1,fnorm);
214: breakout = PETSC_TRUE;
215: break;
216: }
217: snes->numFailures++;
218: }
219: if (!breakout) {
220: /* Update function and solution vectors */
221: fnorm = gnorm;
222: VecCopy(G,F);
223: VecCopy(Y,X);
224: /* Monitor convergence */
225: PetscObjectTakeAccess(snes);
226: snes->iter = i+1;
227: snes->norm = fnorm;
228: PetscObjectGrantAccess(snes);
229: SNESLogConvHistory(snes,snes->norm,lits);
230: SNESMonitor(snes,snes->iter,snes->norm);
231: /* Test for convergence, xnorm = || X || */
232: neP->itflag = PETSC_TRUE;
233: if (snes->ops->converged != SNESSkipConverged) { VecNorm(X,NORM_2,&xnorm); }
234: (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP);
235: if (reason) break;
236: } else {
237: break;
238: }
239: }
240: if (i == maxits) {
241: PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",maxits);
242: if (!reason) reason = SNES_DIVERGED_MAX_IT;
243: }
244: PetscObjectTakeAccess(snes);
245: snes->reason = reason;
246: PetscObjectGrantAccess(snes);
247: return(0);
248: }
249: /*------------------------------------------------------------*/
252: static PetscErrorCode SNESSetUp_TR(SNES snes)
253: {
257: SNESDefaultGetWork(snes,3);
258: SNESSetUpMatrices(snes);
259: return(0);
260: }
264: PetscErrorCode SNESReset_TR(SNES snes)
265: {
268: return(0);
269: }
273: static PetscErrorCode SNESDestroy_TR(SNES snes)
274: {
278: SNESReset_TR(snes);
279: PetscFree(snes->data);
280: return(0);
281: }
282: /*------------------------------------------------------------*/
286: static PetscErrorCode SNESSetFromOptions_TR(SNES snes)
287: {
288: SNES_TR *ctx = (SNES_TR *)snes->data;
292: PetscOptionsHead("SNES trust region options for nonlinear equations");
293: PetscOptionsReal("-snes_trtol","Trust region tolerance","SNESSetTrustRegionTolerance",snes->deltatol,&snes->deltatol,0);
294: PetscOptionsReal("-snes_tr_mu","mu","None",ctx->mu,&ctx->mu,0);
295: PetscOptionsReal("-snes_tr_eta","eta","None",ctx->eta,&ctx->eta,0);
296: PetscOptionsReal("-snes_tr_sigma","sigma","None",ctx->sigma,&ctx->sigma,0);
297: PetscOptionsReal("-snes_tr_delta0","delta0","None",ctx->delta0,&ctx->delta0,0);
298: PetscOptionsReal("-snes_tr_delta1","delta1","None",ctx->delta1,&ctx->delta1,0);
299: PetscOptionsReal("-snes_tr_delta2","delta2","None",ctx->delta2,&ctx->delta2,0);
300: PetscOptionsReal("-snes_tr_delta3","delta3","None",ctx->delta3,&ctx->delta3,0);
301: PetscOptionsTail();
302: return(0);
303: }
307: static PetscErrorCode SNESView_TR(SNES snes,PetscViewer viewer)
308: {
309: SNES_TR *tr = (SNES_TR *)snes->data;
311: PetscBool iascii;
314: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
315: if (iascii) {
316: PetscViewerASCIIPrintf(viewer," mu=%G, eta=%G, sigma=%G\n",tr->mu,tr->eta,tr->sigma);
317: PetscViewerASCIIPrintf(viewer," delta0=%G, delta1=%G, delta2=%G, delta3=%G\n",tr->delta0,tr->delta1,tr->delta2,tr->delta3);
318: }
319: return(0);
320: }
321: /* ------------------------------------------------------------ */
322: /*MC
323: SNESTR - Newton based nonlinear solver that uses a trust region
325: Options Database:
326: + -snes_trtol <tol> Trust region tolerance
327: . -snes_tr_mu <mu>
328: . -snes_tr_eta <eta>
329: . -snes_tr_sigma <sigma>
330: . -snes_tr_delta0 <delta0>
331: . -snes_tr_delta1 <delta1>
332: . -snes_tr_delta2 <delta2>
333: - -snes_tr_delta3 <delta3>
335: The basic algorithm is taken from "The Minpack Project", by More',
336: Sorensen, Garbow, Hillstrom, pages 88-111 of "Sources and Development
337: of Mathematical Software", Wayne Cowell, editor.
339: This is intended as a model implementation, since it does not
340: necessarily have many of the bells and whistles of other
341: implementations.
343: Level: intermediate
345: .seealso: SNESCreate(), SNES, SNESSetType(), SNESLS, SNESSetTrustRegionTolerance()
347: M*/
348: EXTERN_C_BEGIN
351: PetscErrorCode SNESCreate_TR(SNES snes)
352: {
353: SNES_TR *neP;
357: snes->ops->setup = SNESSetUp_TR;
358: snes->ops->solve = SNESSolve_TR;
359: snes->ops->destroy = SNESDestroy_TR;
360: snes->ops->setfromoptions = SNESSetFromOptions_TR;
361: snes->ops->view = SNESView_TR;
362: snes->ops->reset = SNESReset_TR;
364: snes->usesksp = PETSC_TRUE;
365: snes->usespc = PETSC_FALSE;
367: ierr = PetscNewLog(snes,SNES_TR,&neP);
368: snes->data = (void*)neP;
369: neP->mu = 0.25;
370: neP->eta = 0.75;
371: neP->delta = 0.0;
372: neP->delta0 = 0.2;
373: neP->delta1 = 0.3;
374: neP->delta2 = 0.75;
375: neP->delta3 = 2.0;
376: neP->sigma = 0.0001;
377: neP->itflag = PETSC_FALSE;
378: neP->rnorm0 = 0.0;
379: neP->ttol = 0.0;
380: return(0);
381: }
382: EXTERN_C_END