Actual source code: anderson.c

petsc-3.11.4 2019-09-28
Report Typos and Errors
  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;
  7:   PetscBool      monitor = PETSC_FALSE;
  8:   SNESLineSearch linesearch;

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

 30: static PetscErrorCode SNESSolve_Anderson(SNES snes)
 31: {
 32:   SNES_NGMRES         *ngmres = (SNES_NGMRES*) snes->data;
 33:   /* present solution, residual, and preconditioned residual */
 34:   Vec                 X,F,B,D;
 35:   /* candidate linear combination answers */
 36:   Vec                 XA,FA,XM,FM;

 38:   /* coefficients and RHS to the minimization problem */
 39:   PetscReal           fnorm,fMnorm,fAnorm;
 40:   PetscReal           xnorm,ynorm;
 41:   PetscReal           dnorm,dminnorm=0.0,fminnorm;
 42:   PetscInt            restart_count=0;
 43:   PetscInt            k,k_restart,l,ivec;
 44:   PetscBool           selectRestart;
 45:   SNESConvergedReason reason;
 46:   PetscErrorCode      ierr;

 49:   if (snes->xl || snes->xu || snes->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "SNES solver %s does not support bounds", ((PetscObject)snes)->type_name);

 51:   PetscCitationsRegister(SNESCitation,&SNEScite);
 52:   /* variable initialization */
 53:   snes->reason = SNES_CONVERGED_ITERATING;
 54:   X            = snes->vec_sol;
 55:   F            = snes->vec_func;
 56:   B            = snes->vec_rhs;
 57:   XA           = snes->vec_sol_update;
 58:   FA           = snes->work[0];
 59:   D            = snes->work[1];

 61:   /* work for the line search */
 62:   XM = snes->work[3];
 63:   FM = snes->work[4];

 65:   PetscObjectSAWsTakeAccess((PetscObject)snes);
 66:   snes->iter = 0;
 67:   snes->norm = 0.;
 68:   PetscObjectSAWsGrantAccess((PetscObject)snes);

 70:   /* initialization */

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

 74:   if (snes->npc && snes->npcside== PC_LEFT) {
 75:     SNESApplyNPC(snes,X,NULL,F);
 76:     SNESGetConvergedReason(snes->npc,&reason);
 77:     if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
 78:       snes->reason = SNES_DIVERGED_INNER;
 79:       return(0);
 80:     }
 81:     VecNorm(F,NORM_2,&fnorm);
 82:   } else {
 83:     if (!snes->vec_func_init_set) {
 84:       SNESComputeFunction(snes,X,F);
 85:     } else snes->vec_func_init_set = PETSC_FALSE;

 87:     VecNorm(F,NORM_2,&fnorm);
 88:     SNESCheckFunctionNorm(snes,fnorm);
 89:   }
 90:   fminnorm = fnorm;

 92:   PetscObjectSAWsTakeAccess((PetscObject)snes);
 93:   snes->norm = fnorm;
 94:   PetscObjectSAWsGrantAccess((PetscObject)snes);
 95:   SNESLogConvergenceHistory(snes,fnorm,0);
 96:   SNESMonitor(snes,0,fnorm);
 97:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
 98:   if (snes->reason) return(0);

100:   k_restart = 0;
101:   l         = 0;
102:   ivec      = 0;
103:   for (k=1; k < snes->max_its+1; k++) {
104:     /* select which vector of the stored subspace will be updated */
105:     if (snes->npc && snes->npcside== PC_RIGHT) {
106:       VecCopy(X,XM);
107:       SNESSetInitialFunction(snes->npc,F);

109:       PetscLogEventBegin(SNES_NPCSolve,snes->npc,XM,B,0);
110:       SNESSolve(snes->npc,B,XM);
111:       PetscLogEventEnd(SNES_NPCSolve,snes->npc,XM,B,0);

113:       SNESGetConvergedReason(snes->npc,&reason);
114:       if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
115:         snes->reason = SNES_DIVERGED_INNER;
116:         return(0);
117:       }
118:       SNESGetNPCFunction(snes,FM,&fMnorm);
119:       if (ngmres->andersonBeta != 1.0) {
120:         VecAXPBY(XM,(1.0 - ngmres->andersonBeta),ngmres->andersonBeta,X);
121:       }
122:     } else {
123:       VecCopy(F,FM);
124:       VecCopy(X,XM);
125:       VecAXPY(XM,-ngmres->andersonBeta,FM);
126:       fMnorm = fnorm;
127:     }

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

161:     fnorm = fAnorm;
162:     if (fminnorm > fnorm) fminnorm = fnorm;

164:     VecCopy(XA,X);
165:     VecCopy(FA,F);

167:     PetscObjectSAWsTakeAccess((PetscObject)snes);
168:     snes->iter = k;
169:     snes->norm = fnorm;
170:     snes->xnorm = xnorm;
171:     snes->ynorm = ynorm;
172:     PetscObjectSAWsGrantAccess((PetscObject)snes);
173:     SNESLogConvergenceHistory(snes,snes->norm,snes->iter);
174:     SNESMonitor(snes,snes->iter,snes->norm);
175:     (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);
176:     if (snes->reason) return(0);
177:   }
178:   snes->reason = SNES_DIVERGED_MAX_IT;
179:   return(0);
180: }

182: /*MC
183:   SNESANDERSON - Anderson Mixing method.

185:    Level: beginner

187:    Options Database:
188: +  -snes_anderson_m                - Number of stored previous solutions and residuals
189: .  -snes_anderson_beta             - Anderson mixing parameter
190: .  -snes_anderson_restart_type     - Type of restart (see SNESNGMRES)
191: .  -snes_anderson_restart_it       - Number of iterations of restart conditions before restart
192: .  -snes_anderson_restart          - Number of iterations before periodic restart
193: -  -snes_anderson_monitor          - Prints relevant information about the ngmres iteration

195:    Notes:

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

200:    Very similar to the SNESNGMRES algorithm.

202:    References:
203: +  1. -  D. G. Anderson. Iterative procedures for nonlinear integral equations.
204:     J. Assoc. Comput. Mach., 12, 1965."
205: -  2. - Peter R. Brune, Matthew G. Knepley, Barry F. Smith, and Xuemin Tu,"Composing Scalable Nonlinear Algebraic Solvers", 
206:    SIAM Review, 57(4), 2015


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

212: PETSC_EXTERN PetscErrorCode SNESCreate_Anderson(SNES snes)
213: {
214:   SNES_NGMRES    *ngmres;

218:   snes->ops->destroy        = SNESDestroy_NGMRES;
219:   snes->ops->setup          = SNESSetUp_NGMRES;
220:   snes->ops->setfromoptions = SNESSetFromOptions_Anderson;
221:   snes->ops->view           = SNESView_NGMRES;
222:   snes->ops->solve          = SNESSolve_Anderson;
223:   snes->ops->reset          = SNESReset_NGMRES;

225:   snes->usesnpc = PETSC_TRUE;
226:   snes->usesksp = PETSC_FALSE;
227:   snes->npcside = PC_RIGHT;

229:   snes->alwayscomputesfinalresidual = PETSC_TRUE;

231:   PetscNewLog(snes,&ngmres);
232:   snes->data    = (void*) ngmres;
233:   ngmres->msize = 30;

235:   if (!snes->tolerancesset) {
236:     snes->max_funcs = 30000;
237:     snes->max_its   = 10000;
238:   }

240:   ngmres->additive_linesearch = NULL;
241:   ngmres->approxfunc       = PETSC_FALSE;
242:   ngmres->restart_type     = SNES_NGMRES_RESTART_NONE;
243:   ngmres->restart_it       = 2;
244:   ngmres->restart_periodic = 30;
245:   ngmres->gammaA           = 2.0;
246:   ngmres->gammaC           = 2.0;
247:   ngmres->deltaB           = 0.9;
248:   ngmres->epsilonB         = 0.1;

250:   ngmres->andersonBeta = 1.0;
251:   return(0);
252: }