Actual source code: anderson.c

  1: #include <../src/snes/impls/ngmres/snesngmres.h>

  3: static PetscErrorCode SNESSetFromOptions_Anderson(PetscOptionItems *PetscOptionsObject,SNES snes)
  4: {
  5:   SNES_NGMRES    *ngmres = (SNES_NGMRES*) snes->data;
  6:   PetscBool      monitor = PETSC_FALSE;

  8:   PetscOptionsHead(PetscOptionsObject,"SNES NGMRES options");
  9:   PetscOptionsInt("-snes_anderson_m",            "Number of directions","SNES",ngmres->msize,&ngmres->msize,NULL);
 10:   PetscOptionsReal("-snes_anderson_beta",        "Mixing parameter","SNES",ngmres->andersonBeta,&ngmres->andersonBeta,NULL);
 11:   PetscOptionsInt("-snes_anderson_restart",      "Iterations before forced restart", "SNES",ngmres->restart_periodic,&ngmres->restart_periodic,NULL);
 12:   PetscOptionsInt("-snes_anderson_restart_it",   "Tolerance iterations before restart","SNES",ngmres->restart_it,&ngmres->restart_it,NULL);
 13:   PetscOptionsEnum("-snes_anderson_restart_type","Restart type","SNESNGMRESSetRestartType",SNESNGMRESRestartTypes,(PetscEnum)ngmres->restart_type,(PetscEnum*)&ngmres->restart_type,NULL);
 14:   PetscOptionsBool("-snes_anderson_monitor",     "Monitor steps of Anderson Mixing","SNES",ngmres->monitor ? PETSC_TRUE : PETSC_FALSE,&monitor,NULL);
 15:   if (monitor) {
 16:     ngmres->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
 17:   }
 18:   PetscOptionsTail();
 19:   return 0;
 20: }

 22: static PetscErrorCode SNESSolve_Anderson(SNES snes)
 23: {
 24:   SNES_NGMRES         *ngmres = (SNES_NGMRES*) snes->data;
 25:   /* present solution, residual, and preconditioned residual */
 26:   Vec                 X,F,B,D;
 27:   /* candidate linear combination answers */
 28:   Vec                 XA,FA,XM,FM;

 30:   /* coefficients and RHS to the minimization problem */
 31:   PetscReal           fnorm,fMnorm,fAnorm;
 32:   PetscReal           xnorm,ynorm;
 33:   PetscReal           dnorm,dminnorm=0.0,fminnorm;
 34:   PetscInt            restart_count=0;
 35:   PetscInt            k,k_restart,l,ivec;
 36:   PetscBool           selectRestart;
 37:   SNESConvergedReason reason;


 41:   PetscCitationsRegister(SNESCitation,&SNEScite);
 42:   /* variable initialization */
 43:   snes->reason = SNES_CONVERGED_ITERATING;
 44:   X            = snes->vec_sol;
 45:   F            = snes->vec_func;
 46:   B            = snes->vec_rhs;
 47:   XA           = snes->vec_sol_update;
 48:   FA           = snes->work[0];
 49:   D            = snes->work[1];

 51:   /* work for the line search */
 52:   XM = snes->work[3];
 53:   FM = snes->work[4];

 55:   PetscObjectSAWsTakeAccess((PetscObject)snes);
 56:   snes->iter = 0;
 57:   snes->norm = 0.;
 58:   PetscObjectSAWsGrantAccess((PetscObject)snes);

 60:   /* initialization */

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

 64:   if (snes->npc && snes->npcside== PC_LEFT) {
 65:     SNESApplyNPC(snes,X,NULL,F);
 66:     SNESGetConvergedReason(snes->npc,&reason);
 67:     if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
 68:       snes->reason = SNES_DIVERGED_INNER;
 69:       return 0;
 70:     }
 71:     VecNorm(F,NORM_2,&fnorm);
 72:   } else {
 73:     if (!snes->vec_func_init_set) {
 74:       SNESComputeFunction(snes,X,F);
 75:     } else snes->vec_func_init_set = PETSC_FALSE;

 77:     VecNorm(F,NORM_2,&fnorm);
 78:     SNESCheckFunctionNorm(snes,fnorm);
 79:   }
 80:   fminnorm = fnorm;

 82:   PetscObjectSAWsTakeAccess((PetscObject)snes);
 83:   snes->norm = fnorm;
 84:   PetscObjectSAWsGrantAccess((PetscObject)snes);
 85:   SNESLogConvergenceHistory(snes,fnorm,0);
 86:   SNESMonitor(snes,0,fnorm);
 87:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
 88:   if (snes->reason) return 0;

 90:   k_restart = 0;
 91:   l         = 0;
 92:   ivec      = 0;
 93:   for (k=1; k < snes->max_its+1; k++) {
 94:     /* select which vector of the stored subspace will be updated */
 95:     if (snes->npc && snes->npcside== PC_RIGHT) {
 96:       VecCopy(X,XM);
 97:       SNESSetInitialFunction(snes->npc,F);

 99:       PetscLogEventBegin(SNES_NPCSolve,snes->npc,XM,B,0);
100:       SNESSolve(snes->npc,B,XM);
101:       PetscLogEventEnd(SNES_NPCSolve,snes->npc,XM,B,0);

103:       SNESGetConvergedReason(snes->npc,&reason);
104:       if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
105:         snes->reason = SNES_DIVERGED_INNER;
106:         return 0;
107:       }
108:       SNESGetNPCFunction(snes,FM,&fMnorm);
109:       if (ngmres->andersonBeta != 1.0) {
110:         VecAXPBY(XM,(1.0 - ngmres->andersonBeta),ngmres->andersonBeta,X);
111:       }
112:     } else {
113:       VecCopy(F,FM);
114:       VecCopy(X,XM);
115:       VecAXPY(XM,-ngmres->andersonBeta,FM);
116:       fMnorm = fnorm;
117:     }

119:     SNESNGMRESFormCombinedSolution_Private(snes,ivec,l,XM,FM,fMnorm,X,XA,FA);
120:     ivec = k_restart % ngmres->msize;
121:     if (ngmres->restart_type == SNES_NGMRES_RESTART_DIFFERENCE) {
122:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,&dnorm,&dminnorm,NULL,NULL,NULL,&xnorm,&fAnorm,&ynorm);
123:       SNESNGMRESSelectRestart_Private(snes,l,fMnorm,fnorm,dnorm,fminnorm,dminnorm,&selectRestart);
124:       /* if the restart conditions persist for more than restart_it iterations, restart. */
125:       if (selectRestart) restart_count++;
126:       else restart_count = 0;
127:     } else if (ngmres->restart_type == SNES_NGMRES_RESTART_PERIODIC) {
128:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,NULL,NULL,NULL,NULL,NULL,&xnorm,&fAnorm,&ynorm);
129:       if (k_restart > ngmres->restart_periodic) {
130:         if (ngmres->monitor) PetscViewerASCIIPrintf(ngmres->monitor,"periodic restart after %D iterations\n",k_restart);
131:         restart_count = ngmres->restart_it;
132:       }
133:     } else {
134:       SNESNGMRESNorms_Private(snes,l,X,F,XM,FM,XA,FA,D,NULL,NULL,NULL,NULL,NULL,&xnorm,&fAnorm,&ynorm);
135:     }
136:     /* restart after restart conditions have persisted for a fixed number of iterations */
137:     if (restart_count >= ngmres->restart_it) {
138:       if (ngmres->monitor) {
139:         PetscViewerASCIIPrintf(ngmres->monitor,"Restarted at iteration %d\n",k_restart);
140:       }
141:       restart_count = 0;
142:       k_restart     = 0;
143:       l             = 0;
144:       ivec          = 0;
145:     } else {
146:       if (l < ngmres->msize) l++;
147:       k_restart++;
148:       SNESNGMRESUpdateSubspace_Private(snes,ivec,l,FM,fnorm,XM);
149:     }

151:     fnorm = fAnorm;
152:     if (fminnorm > fnorm) fminnorm = fnorm;

154:     VecCopy(XA,X);
155:     VecCopy(FA,F);

157:     PetscObjectSAWsTakeAccess((PetscObject)snes);
158:     snes->iter = k;
159:     snes->norm = fnorm;
160:     snes->xnorm = xnorm;
161:     snes->ynorm = ynorm;
162:     PetscObjectSAWsGrantAccess((PetscObject)snes);
163:     SNESLogConvergenceHistory(snes,snes->norm,snes->iter);
164:     SNESMonitor(snes,snes->iter,snes->norm);
165:     (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);
166:     if (snes->reason) return 0;
167:   }
168:   snes->reason = SNES_DIVERGED_MAX_IT;
169:   return 0;
170: }

172: /*MC
173:   SNESANDERSON - Anderson Mixing method.

175:    Level: beginner

177:    Options Database:
178: +  -snes_anderson_m                - Number of stored previous solutions and residuals
179: .  -snes_anderson_beta             - Anderson mixing parameter
180: .  -snes_anderson_restart_type     - Type of restart (see SNESNGMRES)
181: .  -snes_anderson_restart_it       - Number of iterations of restart conditions before restart
182: .  -snes_anderson_restart          - Number of iterations before periodic restart
183: -  -snes_anderson_monitor          - Prints relevant information about the ngmres iteration

185:    Notes:

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

190:    Very similar to the SNESNGMRES algorithm.

192:    References:
193: +  * -  D. G. Anderson. Iterative procedures for nonlinear integral equations.
194:     J. Assoc. Comput. Mach., 12, 1965."
195: -  * - Peter R. Brune, Matthew G. Knepley, Barry F. Smith, and Xuemin Tu,"Composing Scalable Nonlinear Algebraic Solvers",
196:    SIAM Review, 57(4), 2015

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

201: PETSC_EXTERN PetscErrorCode SNESCreate_Anderson(SNES snes)
202: {
203:   SNES_NGMRES    *ngmres;
204:   SNESLineSearch linesearch;

206:   snes->ops->destroy        = SNESDestroy_NGMRES;
207:   snes->ops->setup          = SNESSetUp_NGMRES;
208:   snes->ops->setfromoptions = SNESSetFromOptions_Anderson;
209:   snes->ops->view           = SNESView_NGMRES;
210:   snes->ops->solve          = SNESSolve_Anderson;
211:   snes->ops->reset          = SNESReset_NGMRES;

213:   snes->usesnpc = PETSC_TRUE;
214:   snes->usesksp = PETSC_FALSE;
215:   snes->npcside = PC_RIGHT;

217:   snes->alwayscomputesfinalresidual = PETSC_TRUE;

219:   PetscNewLog(snes,&ngmres);
220:   snes->data    = (void*) ngmres;
221:   ngmres->msize = 30;

223:   if (!snes->tolerancesset) {
224:     snes->max_funcs = 30000;
225:     snes->max_its   = 10000;
226:   }

228:   SNESGetLineSearch(snes,&linesearch);
229:   if (!((PetscObject)linesearch)->type_name) {
230:     SNESLineSearchSetType(linesearch,SNESLINESEARCHBASIC);
231:   }

233:   ngmres->additive_linesearch = NULL;
234:   ngmres->approxfunc       = PETSC_FALSE;
235:   ngmres->restart_type     = SNES_NGMRES_RESTART_NONE;
236:   ngmres->restart_it       = 2;
237:   ngmres->restart_periodic = 30;
238:   ngmres->gammaA           = 2.0;
239:   ngmres->gammaC           = 2.0;
240:   ngmres->deltaB           = 0.9;
241:   ngmres->epsilonB         = 0.1;

243:   ngmres->andersonBeta = 1.0;
244:   return 0;
245: }