Actual source code: anderson.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  1: #include <../src/snes/impls/ngmres/snesngmres.h> /*I "petscsnes.h" I*/

  3: extern PetscErrorCode SNESDestroy_NGMRES(SNES);
  4: extern PetscErrorCode SNESReset_NGMRES(SNES);
  5: extern PetscErrorCode SNESSetUp_NGMRES(SNES);
  6: extern PetscErrorCode SNESView_NGMRES(SNES,PetscViewer);

  8: PETSC_EXTERN const char *const SNESNGMRESRestartTypes[];

 12: PetscErrorCode SNESSetFromOptions_Anderson(SNES snes)
 13: {
 14:   SNES_NGMRES    *ngmres = (SNES_NGMRES*) snes->data;
 16:   PetscBool      debug;
 17:   SNESLineSearch linesearch;

 20:   PetscOptionsHead("SNES NGMRES options");
 21:   PetscOptionsInt("-snes_anderson_m",            "Number of directions","SNES",ngmres->msize,&ngmres->msize,NULL);
 22:   PetscOptionsReal("-snes_anderson_beta",        "Mixing parameter","SNES",ngmres->andersonBeta,&ngmres->andersonBeta,NULL);
 23:   PetscOptionsBool("-snes_anderson_monitor",     "Monitor steps of Anderson Mixing","SNES",ngmres->monitor ? PETSC_TRUE : PETSC_FALSE,&debug,NULL);
 24:   PetscOptionsInt("-snes_anderson_restart",      "Iterations before forced restart", "SNES",ngmres->restart_periodic,&ngmres->restart_periodic,NULL);
 25:   PetscOptionsInt("-snes_anderson_restart_it",   "Tolerance iterations before restart","SNES",ngmres->restart_it,&ngmres->restart_it,NULL);
 26:   PetscOptionsEnum("-snes_anderson_restart_type","Restart type","SNESNGMRESSetRestartType",SNESNGMRESRestartTypes,
 27:                           (PetscEnum)ngmres->restart_type,(PetscEnum*)&ngmres->restart_type,NULL);
 28:   if (debug) {
 29:     ngmres->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
 30:   }
 31:   PetscOptionsTail();
 32:   /* set the default type of the line search if the user hasn't already. */
 33:   if (!snes->linesearch) {
 34:     SNESGetLineSearch(snes,&linesearch);
 35:     SNESLineSearchSetType(linesearch,SNESLINESEARCHBASIC);
 36:   }
 37:   return(0);
 38: }

 42: PetscErrorCode SNESSolve_Anderson(SNES snes)
 43: {
 44:   SNES_NGMRES         *ngmres = (SNES_NGMRES*) snes->data;
 45:   /* present solution, residual, and preconditioned residual */
 46:   Vec                 X,F,B,D;
 47:   /* candidate linear combination answers */
 48:   Vec                 XA,FA,XM,FM;

 50:   /* coefficients and RHS to the minimization problem */
 51:   PetscReal           fnorm,fMnorm,fAnorm;
 52:   PetscReal           xnorm,ynorm;
 53:   PetscReal           dnorm,dminnorm=0.0,fminnorm;
 54:   PetscInt            restart_count=0;
 55:   PetscInt            k,k_restart,l,ivec;
 56:   PetscBool           selectRestart;
 57:   SNESConvergedReason reason;
 58:   PetscErrorCode      ierr;

 61:   PetscCitationsRegister(SNESCitation,&SNEScite);
 62:   /* variable initialization */
 63:   snes->reason = SNES_CONVERGED_ITERATING;
 64:   X            = snes->vec_sol;
 65:   F            = snes->vec_func;
 66:   B            = snes->vec_rhs;
 67:   XA           = snes->vec_sol_update;
 68:   FA           = snes->work[0];
 69:   D            = snes->work[1];

 71:   /* work for the line search */
 72:   XM = snes->work[3];
 73:   FM = snes->work[4];

 75:   PetscObjectSAWsTakeAccess((PetscObject)snes);
 76:   snes->iter = 0;
 77:   snes->norm = 0.;
 78:   PetscObjectSAWsGrantAccess((PetscObject)snes);

 80:   /* initialization */

 82:   /* r = F(x) */

 84:   if (snes->pc && snes->pcside == PC_LEFT) {
 85:     SNESApplyNPC(snes,X,NULL,F);
 86:     SNESGetConvergedReason(snes->pc,&reason);
 87:     if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
 88:       snes->reason = SNES_DIVERGED_INNER;
 89:       return(0);
 90:     }
 91:     VecNorm(F,NORM_2,&fnorm);
 92:   } else {
 93:     if (!snes->vec_func_init_set) {
 94:       SNESComputeFunction(snes,X,F);
 95:       if (snes->domainerror) {
 96:         snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
 97:         return(0);
 98:       }
 99:     } else snes->vec_func_init_set = PETSC_FALSE;

101:     VecNorm(F,NORM_2,&fnorm);
102:     if (PetscIsInfOrNanReal(fnorm)) {
103:       snes->reason = SNES_DIVERGED_FNORM_NAN;
104:       return(0);
105:     }
106:   }
107:   fminnorm = fnorm;

109:   PetscObjectSAWsTakeAccess((PetscObject)snes);
110:   snes->norm = fnorm;
111:   PetscObjectSAWsGrantAccess((PetscObject)snes);
112:   SNESLogConvergenceHistory(snes,fnorm,0);
113:   SNESMonitor(snes,0,fnorm);
114:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
115:   if (snes->reason) return(0);

117:   k_restart = 0;
118:   l         = 0;
119:   ivec      = 0;
120:   for (k=1; k < snes->max_its+1; k++) {
121:     /* select which vector of the stored subspace will be updated */
122:     if (snes->pc && snes->pcside == PC_RIGHT) {
123:       VecCopy(X,XM);
124:       SNESSetInitialFunction(snes->pc,F);

126:       PetscLogEventBegin(SNES_NPCSolve,snes->pc,XM,B,0);
127:       SNESSolve(snes->pc,B,XM);
128:       PetscLogEventEnd(SNES_NPCSolve,snes->pc,XM,B,0);

130:       SNESGetConvergedReason(snes->pc,&reason);
131:       if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
132:         snes->reason = SNES_DIVERGED_INNER;
133:         return(0);
134:       }
135:       SNESGetNPCFunction(snes,FM,&fMnorm);
136:       if (ngmres->andersonBeta != 1.0) {
137:         VecAXPBY(XM,(1.0 - ngmres->andersonBeta),ngmres->andersonBeta,X);
138:       }
139:     } else {
140:       VecCopy(F,FM);
141:       VecCopy(X,XM);
142:       VecAXPY(XM,-ngmres->andersonBeta,FM);
143:       fMnorm = fnorm;
144:     }

146:     SNESNGMRESFormCombinedSolution_Private(snes,ivec,l,XM,FM,fMnorm,X,XA,FA);
147:     ivec = k_restart % ngmres->msize;
148:     if (ngmres->restart_type == SNES_NGMRES_RESTART_DIFFERENCE) {
149:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,&dnorm,&dminnorm,NULL,NULL,NULL,&xnorm,&fAnorm,&ynorm);
150:       SNESNGMRESSelectRestart_Private(snes,l,fnorm,dnorm,fminnorm,dminnorm,&selectRestart);
151:       /* if the restart conditions persist for more than restart_it iterations, restart. */
152:       if (selectRestart) restart_count++;
153:       else restart_count = 0;
154:     } else if (ngmres->restart_type == SNES_NGMRES_RESTART_PERIODIC) {
155:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,NULL,NULL,NULL,NULL,NULL,&xnorm,&fAnorm,&ynorm);
156:       if (k_restart > ngmres->restart_periodic) {
157:         if (ngmres->monitor) PetscViewerASCIIPrintf(ngmres->monitor,"periodic restart after %D iterations\n",k_restart);
158:         restart_count = ngmres->restart_it;
159:       }
160:     } else {
161:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,NULL,NULL,NULL,NULL,NULL,&xnorm,&fAnorm,&ynorm);
162:     }
163:     /* restart after restart conditions have persisted for a fixed number of iterations */
164:     if (restart_count >= ngmres->restart_it) {
165:       if (ngmres->monitor) {
166:         PetscViewerASCIIPrintf(ngmres->monitor,"Restarted at iteration %d\n",k_restart);
167:       }
168:       restart_count = 0;
169:       k_restart     = 0;
170:       l             = 0;
171:       ivec          = 0;
172:     } else {
173:       if (l < ngmres->msize) l++;
174:       k_restart++;
175:       SNESNGMRESUpdateSubspace_Private(snes,ivec,l,FM,fnorm,XM);
176:     }

178:     fnorm = fAnorm;
179:     if (fminnorm > fnorm) fminnorm = fnorm;

181:     VecCopy(XA,X);
182:     VecCopy(FA,F);

184:     PetscObjectSAWsTakeAccess((PetscObject)snes);
185:     snes->iter = k;
186:     snes->norm = fnorm;
187:     PetscObjectSAWsGrantAccess((PetscObject)snes);
188:     SNESLogConvergenceHistory(snes,snes->norm,snes->iter);
189:     SNESMonitor(snes,snes->iter,snes->norm);
190:     (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);
191:     if (snes->reason) return(0);
192:   }
193:   snes->reason = SNES_DIVERGED_MAX_IT;
194:   return(0);
195: }

197: /*MC
198:   SNESAnderson - Anderson Mixing method.

200:    Level: beginner

202:    Options Database:
203: +  -snes_anderson_m                - Number of stored previous solutions and residuals
204: .  -snes_anderson_beta             - Relaxation parameter; X_{update} = X + \beta F
205: .  -snes_anderson_restart_type     - Type of restart (see SNESNGMRES)
206: .  -snes_anderson_restart_it       - Number of iterations of restart conditions before restart
207: .  -snes_anderson_restart          - Number of iterations before periodic restart
208: -  -snes_anderson_monitor          - Prints relevant information about the ngmres iteration

210:    Notes:

212:    The Anderson Mixing method combines m previous solutions into a minimum-residual solution by solving a small linearized
213:    optimization problem at each iteration.

215:    References:

217:     "D. G. Anderson. Iterative procedures for nonlinear integral equations.
218:     J. Assoc. Comput. Mach., 12:547-560, 1965."

220: .seealso: SNESNGMRES, SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
221: M*/

225: PETSC_EXTERN PetscErrorCode SNESCreate_Anderson(SNES snes)
226: {
227:   SNES_NGMRES    *ngmres;

231:   snes->ops->destroy        = SNESDestroy_NGMRES;
232:   snes->ops->setup          = SNESSetUp_NGMRES;
233:   snes->ops->setfromoptions = SNESSetFromOptions_Anderson;
234:   snes->ops->view           = SNESView_NGMRES;
235:   snes->ops->solve          = SNESSolve_Anderson;
236:   snes->ops->reset          = SNESReset_NGMRES;

238:   snes->usespc  = PETSC_TRUE;
239:   snes->usesksp = PETSC_FALSE;
240:   snes->pcside  = PC_RIGHT;

242:   PetscNewLog(snes,&ngmres);
243:   snes->data    = (void*) ngmres;
244:   ngmres->msize = 30;

246:   if (!snes->tolerancesset) {
247:     snes->max_funcs = 30000;
248:     snes->max_its   = 10000;
249:   }

251:   ngmres->additive_linesearch = NULL;
252:   ngmres->approxfunc       = PETSC_FALSE;
253:   ngmres->restart_type     = SNES_NGMRES_RESTART_NONE;
254:   ngmres->restart_it       = 2;
255:   ngmres->restart_periodic = 30;
256:   ngmres->gammaA           = 2.0;
257:   ngmres->gammaC           = 2.0;
258:   ngmres->deltaB           = 0.9;
259:   ngmres->epsilonB         = 0.1;

261:   ngmres->andersonBeta = 1.0;
262:   return(0);
263: }