Actual source code: tr.c

  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;

100:   maxits        = snes->max_its;        /* maximum number of iterations */
101:   X                = snes->vec_sol;        /* solution vector */
102:   F                = snes->vec_func;        /* residual vector */
103:   Y                = snes->work[0];        /* work vectors */
104:   G                = snes->work[1];
105:   Ytmp          = snes->work[2];

107:   PetscObjectTakeAccess(snes);
108:   snes->iter = 0;
109:   PetscObjectGrantAccess(snes);

111:   SNESComputeFunction(snes,X,F);          /* F(X) */
112:   VecNorm(F,NORM_2,&fnorm);             /* fnorm <- || F || */
113:   PetscObjectTakeAccess(snes);
114:   snes->norm = fnorm;
115:   PetscObjectGrantAccess(snes);
116:   delta = neP->delta0*fnorm;
117:   neP->delta = delta;
118:   SNESLogConvHistory(snes,fnorm,0);
119:   SNESMonitor(snes,0,fnorm);

121:   /* set parameter for default relative tolerance convergence test */
122:   snes->ttol = fnorm*snes->rtol;
123:   /* test convergence */
124:   (*snes->ops->converged)(snes,snes->iter,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
125:   if (snes->reason) return(0);

127:   /* Set the stopping criteria to use the More' trick. */
128:   PetscOptionsGetBool(PETSC_NULL,"-snes_tr_ksp_regular_convergence_test",&conv,PETSC_NULL);
129:   if (!conv) {
130:     SNES_TR_KSPConverged_Ctx *ctx;
131:     SNESGetKSP(snes,&ksp);
132:     PetscNew(SNES_TR_KSPConverged_Ctx,&ctx);
133:     ctx->snes = snes;
134:     KSPDefaultConvergedCreate(&ctx->ctx);
135:     KSPSetConvergenceTest(ksp,SNES_TR_KSPConverged_Private,ctx,SNES_TR_KSPConverged_Destroy);
136:     PetscInfo(snes,"Using Krylov convergence test SNES_TR_KSPConverged_Private\n");
137:   }
138: 
139:   for (i=0; i<maxits; i++) {

141:     /* Call general purpose update function */
142:     if (snes->ops->update) {
143:       (*snes->ops->update)(snes, snes->iter);
144:     }

146:     /* Solve J Y = F, where J is Jacobian matrix */
147:     SNESComputeJacobian(snes,X,&snes->jacobian,&snes->jacobian_pre,&flg);
148:     KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,flg);
149:     SNES_KSPSolve(snes,snes->ksp,F,Ytmp);
150:     KSPGetIterationNumber(snes->ksp,&lits);
151:     snes->linear_its += lits;
152:     PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);
153:     VecNorm(Ytmp,NORM_2,&nrm);
154:     norm1 = nrm;
155:     while(1) {
156:       VecCopy(Ytmp,Y);
157:       nrm = norm1;

159:       /* Scale Y if need be and predict new value of F norm */
160:       if (nrm >= delta) {
161:         nrm = delta/nrm;
162:         gpnorm = (1.0 - nrm)*fnorm;
163:         cnorm = nrm;
164:         PetscInfo1(snes,"Scaling direction by %G\n",nrm);
165:         VecScale(Y,cnorm);
166:         nrm = gpnorm;
167:         ynorm = delta;
168:       } else {
169:         gpnorm = 0.0;
170:         PetscInfo(snes,"Direction is in Trust Region\n");
171:         ynorm = nrm;
172:       }
173:       VecAYPX(Y,-1.0,X);            /* Y <- X - Y */
174:       VecCopy(X,snes->vec_sol_update);
175:       SNESComputeFunction(snes,Y,G); /*  F(X) */
176:       VecNorm(G,NORM_2,&gnorm);      /* gnorm <- || g || */
177:       if (fnorm == gpnorm) rho = 0.0;
178:       else rho = (fnorm*fnorm - gnorm*gnorm)/(fnorm*fnorm - gpnorm*gpnorm);

180:       /* Update size of trust region */
181:       if      (rho < neP->mu)  delta *= neP->delta1;
182:       else if (rho < neP->eta) delta *= neP->delta2;
183:       else                     delta *= neP->delta3;
184:       PetscInfo3(snes,"fnorm=%G, gnorm=%G, ynorm=%G\n",fnorm,gnorm,ynorm);
185:       PetscInfo3(snes,"gpred=%G, rho=%G, delta=%G\n",gpnorm,rho,delta);
186:       neP->delta = delta;
187:       if (rho > neP->sigma) break;
188:       PetscInfo(snes,"Trying again in smaller region\n");
189:       /* check to see if progress is hopeless */
190:       neP->itflag = PETSC_FALSE;
191:       SNES_TR_Converged_Private(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP);
192:       if (!reason) { (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP); }
193:       if (reason) {
194:         /* We're not progressing, so return with the current iterate */
195:         SNESMonitor(snes,i+1,fnorm);
196:         breakout = PETSC_TRUE;
197:         break;
198:       }
199:       snes->numFailures++;
200:     }
201:     if (!breakout) {
202:       /* Update function and solution vectors */
203:       fnorm = gnorm;
204:       VecCopy(G,F);
205:       VecCopy(Y,X);
206:       /* Monitor convergence */
207:       PetscObjectTakeAccess(snes);
208:       snes->iter = i+1;
209:       snes->norm = fnorm;
210:       PetscObjectGrantAccess(snes);
211:       SNESLogConvHistory(snes,snes->norm,lits);
212:       SNESMonitor(snes,snes->iter,snes->norm);
213:       /* Test for convergence, xnorm = || X || */
214:       neP->itflag = PETSC_TRUE;
215:       if (snes->ops->converged != SNESSkipConverged) { VecNorm(X,NORM_2,&xnorm); }
216:       (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&reason,snes->cnvP);
217:       if (reason) break;
218:     } else {
219:       break;
220:     }
221:   }
222:   if (i == maxits) {
223:     PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",maxits);
224:     if (!reason) reason = SNES_DIVERGED_MAX_IT;
225:   }
226:   PetscObjectTakeAccess(snes);
227:   snes->reason = reason;
228:   PetscObjectGrantAccess(snes);
229:   return(0);
230: }
231: /*------------------------------------------------------------*/
234: static PetscErrorCode SNESSetUp_TR(SNES snes)
235: {

239:   SNESDefaultGetWork(snes,3);
240:   return(0);
241: }

245: PetscErrorCode SNESReset_TR(SNES snes)
246: {

250:   if (snes->work) {VecDestroyVecs(snes->nwork,&snes->work);}
251:   return(0);
252: }

256: static PetscErrorCode SNESDestroy_TR(SNES snes)
257: {

261:   SNESReset_TR(snes);
262:   PetscFree(snes->data);
263:   return(0);
264: }
265: /*------------------------------------------------------------*/

269: static PetscErrorCode SNESSetFromOptions_TR(SNES snes)
270: {
271:   SNES_TR *ctx = (SNES_TR *)snes->data;

275:   PetscOptionsHead("SNES trust region options for nonlinear equations");
276:     PetscOptionsReal("-snes_trtol","Trust region tolerance","SNESSetTrustRegionTolerance",snes->deltatol,&snes->deltatol,0);
277:     PetscOptionsReal("-snes_tr_mu","mu","None",ctx->mu,&ctx->mu,0);
278:     PetscOptionsReal("-snes_tr_eta","eta","None",ctx->eta,&ctx->eta,0);
279:     PetscOptionsReal("-snes_tr_sigma","sigma","None",ctx->sigma,&ctx->sigma,0);
280:     PetscOptionsReal("-snes_tr_delta0","delta0","None",ctx->delta0,&ctx->delta0,0);
281:     PetscOptionsReal("-snes_tr_delta1","delta1","None",ctx->delta1,&ctx->delta1,0);
282:     PetscOptionsReal("-snes_tr_delta2","delta2","None",ctx->delta2,&ctx->delta2,0);
283:     PetscOptionsReal("-snes_tr_delta3","delta3","None",ctx->delta3,&ctx->delta3,0);
284:   PetscOptionsTail();
285:   return(0);
286: }

290: static PetscErrorCode SNESView_TR(SNES snes,PetscViewer viewer)
291: {
292:   SNES_TR *tr = (SNES_TR *)snes->data;
294:   PetscBool  iascii;

297:   PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
298:   if (iascii) {
299:     PetscViewerASCIIPrintf(viewer,"  mu=%G, eta=%G, sigma=%G\n",tr->mu,tr->eta,tr->sigma);
300:     PetscViewerASCIIPrintf(viewer,"  delta0=%G, delta1=%G, delta2=%G, delta3=%G\n",tr->delta0,tr->delta1,tr->delta2,tr->delta3);
301:   }
302:   return(0);
303: }
304: /* ------------------------------------------------------------ */
305: /*MC
306:       SNESTR - Newton based nonlinear solver that uses a trust region

308:    Options Database:
309: +    -snes_trtol <tol> Trust region tolerance
310: .    -snes_tr_mu <mu>
311: .    -snes_tr_eta <eta>
312: .    -snes_tr_sigma <sigma>
313: .    -snes_tr_delta0 <delta0>
314: .    -snes_tr_delta1 <delta1>
315: .    -snes_tr_delta2 <delta2>
316: -    -snes_tr_delta3 <delta3>

318:    The basic algorithm is taken from "The Minpack Project", by More', 
319:    Sorensen, Garbow, Hillstrom, pages 88-111 of "Sources and Development 
320:    of Mathematical Software", Wayne Cowell, editor.

322:    This is intended as a model implementation, since it does not 
323:    necessarily have many of the bells and whistles of other 
324:    implementations.  

326:    Level: intermediate

328: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESLS, SNESSetTrustRegionTolerance()

330: M*/
334: PetscErrorCode  SNESCreate_TR(SNES snes)
335: {
336:   SNES_TR        *neP;

340:   snes->ops->setup             = SNESSetUp_TR;
341:   snes->ops->solve             = SNESSolve_TR;
342:   snes->ops->destroy             = SNESDestroy_TR;
343:   snes->ops->setfromoptions  = SNESSetFromOptions_TR;
344:   snes->ops->view            = SNESView_TR;
345:   snes->ops->reset           = SNESReset_TR;

347:   ierr                        = PetscNewLog(snes,SNES_TR,&neP);
348:   snes->data                = (void*)neP;
349:   neP->mu                = 0.25;
350:   neP->eta                = 0.75;
351:   neP->delta                = 0.0;
352:   neP->delta0                = 0.2;
353:   neP->delta1                = 0.3;
354:   neP->delta2                = 0.75;
355:   neP->delta3                = 2.0;
356:   neP->sigma                = 0.0001;
357:   neP->itflag                = PETSC_FALSE;
358:   neP->rnorm0                = 0.0;
359:   neP->ttol                = 0.0;
360:   return(0);
361: }