Actual source code: qn.c

petsc-3.11.1 2019-04-12
Report Typos and Errors
  1:  #include <petsc/private/snesimpl.h>
  2:  #include <petscdm.h>

  4: #define H(i,j)  qn->dXdFmat[i*qn->m + j]

  6: const char *const SNESQNScaleTypes[] =        {"DEFAULT","NONE","SHANNO","LINESEARCH","JACOBIAN","SNESQNScaleType","SNES_QN_SCALING_",0};
  7: const char *const SNESQNRestartTypes[] =      {"DEFAULT","NONE","POWELL","PERIODIC","SNESQNRestartType","SNES_QN_RESTART_",0};
  8: const char *const SNESQNTypes[] =             {"LBFGS","BROYDEN","BADBROYDEN","SNESQNType","SNES_QN_",0};

 10: typedef struct {
 11:   Vec               *U;                   /* Stored past states (vary from method to method) */
 12:   Vec               *V;                   /* Stored past states (vary from method to method) */
 13:   PetscInt          m;                    /* The number of kept previous steps */
 14:   PetscReal         *lambda;              /* The line search history of the method */
 15:   PetscReal         *norm;                /* norms of the steps */
 16:   PetscScalar       *alpha, *beta;
 17:   PetscScalar       *dXtdF, *dFtdX, *YtdX;
 18:   PetscBool         singlereduction;      /* Aggregated reduction implementation */
 19:   PetscScalar       *dXdFmat;             /* A matrix of values for dX_i dot dF_j */
 20:   PetscViewer       monitor;
 21:   PetscReal         powell_gamma;         /* Powell angle restart condition */
 22:   PetscReal         scaling;              /* scaling of H0 */
 23:   SNESQNType        type;                 /* the type of quasi-newton method used */
 24:   SNESQNScaleType   scale_type;           /* the type of scaling used */
 25:   SNESQNRestartType restart_type;         /* determine the frequency and type of restart conditions */
 26: } SNES_QN;

 28: PetscErrorCode SNESQNApply_Broyden(SNES snes,PetscInt it,Vec Y,Vec X,Vec Xold,Vec D)
 29: {
 30:   PetscErrorCode     ierr;
 31:   SNES_QN            *qn = (SNES_QN*)snes->data;
 32:   Vec                W   = snes->work[3];
 33:   Vec                *U  = qn->U;
 34:   PetscInt           m = qn->m;
 35:   PetscInt           k,i,j,l = m;
 36:   PetscReal          unorm,a,b;
 37:   PetscReal          *lambda=qn->lambda;
 38:   PetscScalar        gdot;
 39:   PetscReal          udot;

 42:   if (it < m) l = it;
 43:   if (it > 0) {
 44:     k = (it-1)%l;
 45:     SNESLineSearchGetLambda(snes->linesearch,&lambda[k]);
 46:     VecCopy(Xold,U[k]);
 47:     VecAXPY(U[k],-1.0,X);
 48:     if (qn->monitor) {
 49:       PetscViewerASCIIAddTab(qn->monitor,((PetscObject)snes)->tablevel+2);
 50:       PetscViewerASCIIPrintf(qn->monitor, "scaling vector %D of %D by lambda: %14.12e \n",k,l,(double)lambda[k]);
 51:       PetscViewerASCIISubtractTab(qn->monitor,((PetscObject)snes)->tablevel+2);
 52:     }
 53:   }
 54:   if (qn->scale_type == SNES_QN_SCALE_JACOBIAN) {
 55:     KSPSolve(snes->ksp,D,W);
 56:     SNESCheckKSPSolve(snes);
 57:     VecCopy(W,Y);
 58:   } else {
 59:     VecCopy(D,Y);
 60:     VecScale(Y,qn->scaling);
 61:   }

 63:   /* inward recursion starting at the first update and working forward */
 64:   for (i = 0; i < l-1; i++) {
 65:     j = (it+i-l)%l;
 66:     k = (it+i-l+1)%l;
 67:     VecNorm(U[j],NORM_2,&unorm);
 68:     VecDot(U[j],Y,&gdot);
 69:     unorm *= unorm;
 70:     udot = PetscRealPart(gdot);
 71:     a = (lambda[j]/lambda[k]);
 72:     b = -(1.-lambda[j]);
 73:     a *= udot/unorm;
 74:     b *= udot/unorm;
 75:     VecAXPBYPCZ(Y,a,b,1.,U[k],U[j]);

 77:     if (qn->monitor) {
 78:       PetscViewerASCIIAddTab(qn->monitor,((PetscObject)snes)->tablevel+2);
 79:       PetscViewerASCIIPrintf(qn->monitor, "using vector %D and %D, gdot: %14.12e\n",k,j,(double)PetscRealPart(gdot));
 80:       PetscViewerASCIISubtractTab(qn->monitor,((PetscObject)snes)->tablevel+2);
 81:     }
 82:   }
 83:   if (l > 0) {
 84:     k = (it-1)%l;
 85:     VecDot(U[k],Y,&gdot);
 86:     VecNorm(U[k],NORM_2,&unorm);
 87:     unorm *= unorm;
 88:     udot = PetscRealPart(gdot);
 89:     a = unorm/(unorm-lambda[k]*udot);
 90:     b = -(1.-lambda[k])*udot/(unorm-lambda[k]*udot);
 91:     if (qn->monitor) {
 92:       PetscViewerASCIIAddTab(qn->monitor,((PetscObject)snes)->tablevel+2);
 93:       PetscViewerASCIIPrintf(qn->monitor, "using vector %D: a: %14.12e b: %14.12e \n",k,(double)a,(double)b);
 94:       PetscViewerASCIISubtractTab(qn->monitor,((PetscObject)snes)->tablevel+2);
 95:     }
 96:     VecAXPBY(Y,b,a,U[k]);
 97:   }
 98:   l = m;
 99:   if (it+1<m)l=it+1;
100:   k = it%l;
101:   if (qn->monitor) {
102:     PetscViewerASCIIAddTab(qn->monitor,((PetscObject)snes)->tablevel+2);
103:     PetscViewerASCIIPrintf(qn->monitor, "setting vector %D of %D\n",k,l);
104:     PetscViewerASCIISubtractTab(qn->monitor,((PetscObject)snes)->tablevel+2);
105:   }
106:   return(0);
107: }

109: PetscErrorCode SNESQNApply_BadBroyden(SNES snes,PetscInt it,Vec Y,Vec X,Vec Xold,Vec D,Vec Dold)
110: {
112:   SNES_QN        *qn = (SNES_QN*)snes->data;
113:   Vec            W   = snes->work[3];
114:   Vec            *U  = qn->U;
115:   Vec            *T  = qn->V;

117:   /* ksp thing for Jacobian scaling */
118:   PetscInt           h,k,j,i;
119:   PetscInt           m = qn->m;
120:   PetscScalar        gdot,udot;
121:   PetscInt           l = m;

124:   if (it < m) l = it;
125:   VecCopy(D,Y);
126:   if (l > 0) {
127:     k    = (it-1)%l;
128:     SNESLineSearchGetLambda(snes->linesearch,&qn->lambda[k]);
129:     VecCopy(Dold,U[k]);
130:     VecAXPY(U[k],-1.0,D);
131:     VecCopy(Xold,T[k]);
132:     VecAXPY(T[k],-1.0,X);
133:   }

135:   if (qn->scale_type == SNES_QN_SCALE_JACOBIAN) {
136:     KSPSolve(snes->ksp,Y,W);
137:     SNESCheckKSPSolve(snes);
138:     VecCopy(W,Y);
139:   } else {
140:     VecScale(Y,qn->scaling);
141:   }

143:   /* inward recursion starting at the first update and working forward */
144:   if (l > 0) {
145:     for (i = 0; i < l-1; i++) {
146:       j    = (it+i-l)%l;
147:       k    = (it+i-l+1)%l;
148:       h    = (it-1)%l;
149:       VecDotBegin(U[j],U[h],&gdot);
150:       VecDotBegin(U[j],U[j],&udot);
151:       VecDotEnd(U[j],U[h],&gdot);
152:       VecDotEnd(U[j],U[j],&udot);
153:       VecAXPY(Y,PetscRealPart(gdot)/PetscRealPart(udot),T[k]);
154:       VecAXPY(Y,-(1.-qn->lambda[k])*PetscRealPart(gdot)/PetscRealPart(udot),T[j]);
155:       if (qn->monitor) {
156:         PetscViewerASCIIAddTab(qn->monitor,((PetscObject)snes)->tablevel+2);
157:         PetscViewerASCIIPrintf(qn->monitor, "it: %D k: %D gdot: %14.12e\n", it, k, (double)PetscRealPart(gdot));
158:         PetscViewerASCIISubtractTab(qn->monitor,((PetscObject)snes)->tablevel+2);
159:       }
160:     }
161:   }
162:   return(0);
163: }

165: PetscErrorCode SNESQNApply_LBFGS(SNES snes,PetscInt it,Vec Y,Vec X,Vec Xold,Vec D,Vec Dold)
166: {
168:   SNES_QN        *qn    = (SNES_QN*)snes->data;
169:   Vec            W      = snes->work[3];
170:   Vec            *dX    = qn->U;
171:   Vec            *dF    = qn->V;
172:   PetscScalar    *alpha = qn->alpha;
173:   PetscScalar    *beta  = qn->beta;
174:   PetscScalar    *dXtdF = qn->dXtdF;
175:   PetscScalar    *dFtdX = qn->dFtdX;
176:   PetscScalar    *YtdX  = qn->YtdX;

178:   /* ksp thing for Jacobian scaling */
179:   PetscInt           k,i,j,g;
180:   PetscInt           m = qn->m;
181:   PetscScalar        t;
182:   PetscInt           l = m;

185:   if (it < m) l = it;
186:   VecCopy(D,Y);
187:   if (it > 0) {
188:     k    = (it - 1) % l;
189:     VecCopy(D,dF[k]);
190:     VecAXPY(dF[k], -1.0, Dold);
191:     VecCopy(X, dX[k]);
192:     VecAXPY(dX[k], -1.0, Xold);
193:     if (qn->singlereduction) {
194:       VecMDotBegin(dF[k],l,dX,dXtdF);
195:       VecMDotBegin(dX[k],l,dF,dFtdX);
196:       VecMDotBegin(Y,l,dX,YtdX);
197:       VecMDotEnd(dF[k],l,dX,dXtdF);
198:       VecMDotEnd(dX[k],l,dF,dFtdX);
199:       VecMDotEnd(Y,l,dX,YtdX);
200:       for (j = 0; j < l; j++) {
201:         H(k, j) = dFtdX[j];
202:         H(j, k) = dXtdF[j];
203:       }
204:       /* copy back over to make the computation of alpha and beta easier */
205:       for (j = 0; j < l; j++) dXtdF[j] = H(j, j);
206:     } else {
207:       VecDot(dX[k], dF[k], &dXtdF[k]);
208:     }
209:     if (qn->scale_type == SNES_QN_SCALE_LINESEARCH) {
210:       SNESLineSearchGetLambda(snes->linesearch,&qn->scaling);
211:     }
212:   }

214:   /* outward recursion starting at iteration k's update and working back */
215:   for (i=0; i<l; i++) {
216:     k = (it-i-1)%l;
217:     if (qn->singlereduction) {
218:       /* construct t = dX[k] dot Y as Y_0 dot dX[k] + sum(-alpha[j]dX[k]dF[j]) */
219:       t = YtdX[k];
220:       for (j=0; j<i; j++) {
221:         g  = (it-j-1)%l;
222:         t -= alpha[g]*H(k, g);
223:       }
224:       alpha[k] = t/H(k,k);
225:     } else {
226:       VecDot(dX[k],Y,&t);
227:       alpha[k] = t/dXtdF[k];
228:     }
229:     if (qn->monitor) {
230:       PetscViewerASCIIAddTab(qn->monitor,((PetscObject)snes)->tablevel+2);
231:       PetscViewerASCIIPrintf(qn->monitor, "it: %D k: %D alpha:        %14.12e\n", it, k, (double)PetscRealPart(alpha[k]));
232:       PetscViewerASCIISubtractTab(qn->monitor,((PetscObject)snes)->tablevel+2);
233:     }
234:     VecAXPY(Y,-alpha[k],dF[k]);
235:   }

237:   if (qn->scale_type == SNES_QN_SCALE_JACOBIAN) {
238:     KSPSolve(snes->ksp,Y,W);
239:     SNESCheckKSPSolve(snes);
240:     VecCopy(W, Y);
241:   } else {
242:     VecScale(Y, qn->scaling);
243:   }
244:   if (qn->singlereduction) {
245:     VecMDot(Y,l,dF,YtdX);
246:   }
247:   /* inward recursion starting at the first update and working forward */
248:   for (i = 0; i < l; i++) {
249:     k = (it + i - l) % l;
250:     if (qn->singlereduction) {
251:       t = YtdX[k];
252:       for (j = 0; j < i; j++) {
253:         g  = (it + j - l) % l;
254:         t += (alpha[g] - beta[g])*H(g, k);
255:       }
256:       beta[k] = t / H(k, k);
257:     } else {
258:       VecDot(dF[k], Y, &t);
259:       beta[k] = t / dXtdF[k];
260:     }
261:     VecAXPY(Y, (alpha[k] - beta[k]), dX[k]);
262:     if (qn->monitor) {
263:       PetscViewerASCIIAddTab(qn->monitor,((PetscObject)snes)->tablevel+2);
264:       PetscViewerASCIIPrintf(qn->monitor, "it: %D k: %D alpha - beta: %14.12e\n", it, k, (double)PetscRealPart(alpha[k] - beta[k]));
265:       PetscViewerASCIISubtractTab(qn->monitor,((PetscObject)snes)->tablevel+2);
266:     }
267:   }
268:   return(0);
269: }

271: static PetscErrorCode SNESSolve_QN(SNES snes)
272: {
273:   PetscErrorCode       ierr;
274:   SNES_QN              *qn = (SNES_QN*) snes->data;
275:   Vec                  X,Xold;
276:   Vec                  F,W;
277:   Vec                  Y,D,Dold;
278:   PetscInt             i, i_r;
279:   PetscReal            fnorm,xnorm,ynorm,gnorm;
280:   SNESLineSearchReason lssucceed;
281:   PetscBool            powell,periodic;
282:   PetscScalar          DolddotD,DolddotDold;
283:   SNESConvergedReason  reason;

285:   /* basically just a regular newton's method except for the application of the Jacobian */

288:   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);

290:   PetscCitationsRegister(SNESCitation,&SNEScite);
291:   F    = snes->vec_func;                /* residual vector */
292:   Y    = snes->vec_sol_update;          /* search direction generated by J^-1D*/
293:   W    = snes->work[3];
294:   X    = snes->vec_sol;                 /* solution vector */
295:   Xold = snes->work[0];

297:   /* directions generated by the preconditioned problem with F_pre = F or x - M(x, b) */
298:   D    = snes->work[1];
299:   Dold = snes->work[2];

301:   snes->reason = SNES_CONVERGED_ITERATING;

303:   PetscObjectSAWsTakeAccess((PetscObject)snes);
304:   snes->iter = 0;
305:   snes->norm = 0.;
306:   PetscObjectSAWsGrantAccess((PetscObject)snes);

308:   if (snes->npc && snes->npcside== PC_LEFT && snes->functype == SNES_FUNCTION_PRECONDITIONED) {
309:     SNESApplyNPC(snes,X,NULL,F);
310:     SNESGetConvergedReason(snes->npc,&reason);
311:     if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
312:       snes->reason = SNES_DIVERGED_INNER;
313:       return(0);
314:     }
315:     VecNorm(F,NORM_2,&fnorm);
316:   } else {
317:     if (!snes->vec_func_init_set) {
318:       SNESComputeFunction(snes,X,F);
319:     } else snes->vec_func_init_set = PETSC_FALSE;

321:     VecNorm(F,NORM_2,&fnorm);
322:     SNESCheckFunctionNorm(snes,fnorm);
323:   }
324:   if (snes->npc && snes->npcside== PC_LEFT && snes->functype == SNES_FUNCTION_UNPRECONDITIONED) {
325:       SNESApplyNPC(snes,X,F,D);
326:       SNESGetConvergedReason(snes->npc,&reason);
327:       if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
328:         snes->reason = SNES_DIVERGED_INNER;
329:         return(0);
330:       }
331:   } else {
332:     VecCopy(F,D);
333:   }

335:   PetscObjectSAWsTakeAccess((PetscObject)snes);
336:   snes->norm = fnorm;
337:   PetscObjectSAWsGrantAccess((PetscObject)snes);
338:   SNESLogConvergenceHistory(snes,fnorm,0);
339:   SNESMonitor(snes,0,fnorm);

341:   /* test convergence */
342:   (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
343:   if (snes->reason) return(0);

345:   if (snes->npc && snes->npcside== PC_RIGHT) {
346:     PetscLogEventBegin(SNES_NPCSolve,snes->npc,X,0,0);
347:     SNESSolve(snes->npc,snes->vec_rhs,X);
348:     PetscLogEventEnd(SNES_NPCSolve,snes->npc,X,0,0);
349:     SNESGetConvergedReason(snes->npc,&reason);
350:     if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
351:       snes->reason = SNES_DIVERGED_INNER;
352:       return(0);
353:     }
354:     SNESGetNPCFunction(snes,F,&fnorm);
355:     VecCopy(F,D);
356:   }

358:   /* scale the initial update */
359:   if (qn->scale_type == SNES_QN_SCALE_JACOBIAN) {
360:     SNESComputeJacobian(snes,X,snes->jacobian,snes->jacobian_pre);
361:     SNESCheckJacobianDomainerror(snes);
362:     KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre);
363:   }

365:   for (i = 0, i_r = 0; i < snes->max_its; i++, i_r++) {
366:     if (qn->scale_type == SNES_QN_SCALE_SHANNO && i_r > 0) {
367:       PetscScalar ff,xf;
368:       VecCopy(Dold,Y);
369:       VecCopy(Xold,W);
370:       VecAXPY(Y,-1.0,D);
371:       VecAXPY(W,-1.0,X);
372:       VecDotBegin(Y,Y,&ff);
373:       VecDotBegin(W,Y,&xf);
374:       VecDotEnd(Y,Y,&ff);
375:       VecDotEnd(W,Y,&xf);
376:       qn->scaling = PetscRealPart(xf)/PetscRealPart(ff);
377:       if (qn->monitor) {
378:         PetscViewerASCIIAddTab(qn->monitor,((PetscObject)snes)->tablevel+2);
379:         PetscViewerASCIIPrintf(qn->monitor, "Shanno scaling %D %g\n", i,(double)qn->scaling);
380:         PetscViewerASCIISubtractTab(qn->monitor,((PetscObject)snes)->tablevel+2);
381:       }
382:     }
383:     switch (qn->type) {
384:     case SNES_QN_BADBROYDEN:
385:       SNESQNApply_BadBroyden(snes,i_r,Y,X,Xold,D,Dold);
386:       break;
387:     case SNES_QN_BROYDEN:
388:       SNESQNApply_Broyden(snes,i_r,Y,X,Xold,D);
389:       break;
390:     case SNES_QN_LBFGS:
391:       SNESQNApply_LBFGS(snes,i_r,Y,X,Xold,D,Dold);
392:       break;
393:     }
394:     /* line search for lambda */
395:     ynorm = 1; gnorm = fnorm;
396:     VecCopy(D, Dold);
397:     VecCopy(X, Xold);
398:     SNESLineSearchApply(snes->linesearch, X, F, &fnorm, Y);
399:     if (snes->reason == SNES_DIVERGED_FUNCTION_COUNT) break;
400:     SNESLineSearchGetReason(snes->linesearch, &lssucceed);
401:     SNESLineSearchGetNorms(snes->linesearch, &xnorm, &fnorm, &ynorm);
402:     if (lssucceed) {
403:       if (++snes->numFailures >= snes->maxFailures) {
404:         snes->reason = SNES_DIVERGED_LINE_SEARCH;
405:         break;
406:       }
407:     }
408:     if (qn->scale_type == SNES_QN_SCALE_LINESEARCH) {
409:       SNESLineSearchGetLambda(snes->linesearch, &qn->scaling);
410:     }

412:     /* convergence monitoring */
413:     PetscInfo4(snes,"fnorm=%18.16e, gnorm=%18.16e, ynorm=%18.16e, lssucceed=%d\n",(double)fnorm,(double)gnorm,(double)ynorm,(int)lssucceed);

415:     if (snes->npc && snes->npcside== PC_RIGHT) {
416:       PetscLogEventBegin(SNES_NPCSolve,snes->npc,X,0,0);
417:       SNESSolve(snes->npc,snes->vec_rhs,X);
418:       PetscLogEventEnd(SNES_NPCSolve,snes->npc,X,0,0);
419:       SNESGetConvergedReason(snes->npc,&reason);
420:       if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
421:         snes->reason = SNES_DIVERGED_INNER;
422:         return(0);
423:       }
424:       SNESGetNPCFunction(snes,F,&fnorm);
425:     }

427:     SNESSetIterationNumber(snes, i+1);
428:     snes->norm = fnorm;
429:     snes->xnorm = xnorm;
430:     snes->ynorm = ynorm;

432:     SNESLogConvergenceHistory(snes,snes->norm,snes->iter);
433:     SNESMonitor(snes,snes->iter,snes->norm);
434:     /* set parameter for default relative tolerance convergence test */
435:     (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);
436:     if (snes->reason) return(0);
437:     if (snes->npc && snes->npcside== PC_LEFT && snes->functype == SNES_FUNCTION_UNPRECONDITIONED) {
438:       SNESApplyNPC(snes,X,F,D);
439:       SNESGetConvergedReason(snes->npc,&reason);
440:       if (reason < 0  && reason != SNES_DIVERGED_MAX_IT) {
441:         snes->reason = SNES_DIVERGED_INNER;
442:         return(0);
443:       }
444:     } else {
445:       VecCopy(F, D);
446:     }
447:     powell = PETSC_FALSE;
448:     if (qn->restart_type == SNES_QN_RESTART_POWELL && i_r > 1) {
449:       /* check restart by Powell's Criterion: |F^T H_0 Fold| > powell_gamma * |Fold^T H_0 Fold| */
450:       if (qn->scale_type == SNES_QN_SCALE_JACOBIAN) {
451:         MatMult(snes->jacobian_pre,Dold,W);
452:       } else {
453:         VecCopy(Dold,W);
454:       }
455:       VecDotBegin(W, Dold, &DolddotDold);
456:       VecDotBegin(W, D, &DolddotD);
457:       VecDotEnd(W, Dold, &DolddotDold);
458:       VecDotEnd(W, D, &DolddotD);
459:       if (PetscAbs(PetscRealPart(DolddotD)) > qn->powell_gamma*PetscAbs(PetscRealPart(DolddotDold))) powell = PETSC_TRUE;
460:     }
461:     periodic = PETSC_FALSE;
462:     if (qn->restart_type == SNES_QN_RESTART_PERIODIC) {
463:       if (i_r>qn->m-1) periodic = PETSC_TRUE;
464:     }
465:     /* restart if either powell or periodic restart is satisfied. */
466:     if (powell || periodic) {
467:       if (qn->monitor) {
468:         PetscViewerASCIIAddTab(qn->monitor,((PetscObject)snes)->tablevel+2);
469:         if (powell) {
470:           PetscViewerASCIIPrintf(qn->monitor, "Powell restart! |%14.12e| > %6.4f*|%14.12e| i_r = %D\n", (double)PetscRealPart(DolddotD), (double)qn->powell_gamma, (double)PetscRealPart(DolddotDold),i_r);
471:         } else {
472:           PetscViewerASCIIPrintf(qn->monitor, "Periodic restart! i_r = %D\n", i_r);
473:         }
474:         PetscViewerASCIISubtractTab(qn->monitor,((PetscObject)snes)->tablevel+2);
475:       }
476:       i_r = -1;
477:       /* general purpose update */
478:       if (snes->ops->update) {
479:         (*snes->ops->update)(snes, snes->iter);
480:       }
481:       if (qn->scale_type == SNES_QN_SCALE_JACOBIAN) {
482:         SNESComputeJacobian(snes,X,snes->jacobian,snes->jacobian_pre);
483:         SNESCheckJacobianDomainerror(snes);
484:       }
485:     }
486:     /* general purpose update */
487:     if (snes->ops->update) {
488:       (*snes->ops->update)(snes, snes->iter);
489:     }
490:   }
491:   if (i == snes->max_its) {
492:     PetscInfo1(snes, "Maximum number of iterations has been reached: %D\n", snes->max_its);
493:     if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
494:   }
495:   return(0);
496: }

498: static PetscErrorCode SNESSetUp_QN(SNES snes)
499: {
500:   SNES_QN        *qn = (SNES_QN*)snes->data;
502:   DM             dm;


506:   if (!snes->vec_sol) {
507:     SNESGetDM(snes,&dm);
508:     DMCreateGlobalVector(dm,&snes->vec_sol);
509:   }

511:   VecDuplicateVecs(snes->vec_sol, qn->m, &qn->U);
512:   if (qn->type != SNES_QN_BROYDEN) VecDuplicateVecs(snes->vec_sol, qn->m, &qn->V);
513:   PetscMalloc4(qn->m,&qn->alpha,qn->m,&qn->beta,qn->m,&qn->dXtdF,qn->m,&qn->lambda);

515:   if (qn->singlereduction) {
516:     PetscMalloc3(qn->m*qn->m,&qn->dXdFmat,qn->m,&qn->dFtdX,qn->m,&qn->YtdX);
517:   }
518:   SNESSetWorkVecs(snes,4);
519:   /* set method defaults */
520:   if (qn->scale_type == SNES_QN_SCALE_DEFAULT) {
521:     if (qn->type == SNES_QN_BADBROYDEN) {
522:       qn->scale_type = SNES_QN_SCALE_NONE;
523:     } else {
524:       qn->scale_type = SNES_QN_SCALE_SHANNO;
525:     }
526:   }
527:   if (qn->restart_type == SNES_QN_RESTART_DEFAULT) {
528:     if (qn->type == SNES_QN_LBFGS) {
529:       qn->restart_type = SNES_QN_RESTART_POWELL;
530:     } else {
531:       qn->restart_type = SNES_QN_RESTART_PERIODIC;
532:     }
533:   }

535:   if (qn->scale_type == SNES_QN_SCALE_JACOBIAN) {
536:     SNESSetUpMatrices(snes);
537:   }
538:   if (snes->npcside== PC_LEFT && snes->functype == SNES_FUNCTION_DEFAULT) {snes->functype = SNES_FUNCTION_UNPRECONDITIONED;}
539:   return(0);
540: }

542: static PetscErrorCode SNESReset_QN(SNES snes)
543: {
545:   SNES_QN        *qn;

548:   if (snes->data) {
549:     qn = (SNES_QN*)snes->data;
550:     if (qn->U) {
551:       VecDestroyVecs(qn->m, &qn->U);
552:     }
553:     if (qn->V) {
554:       VecDestroyVecs(qn->m, &qn->V);
555:     }
556:     if (qn->singlereduction) {
557:       PetscFree3(qn->dXdFmat, qn->dFtdX, qn->YtdX);
558:     }
559:     PetscFree4(qn->alpha,qn->beta,qn->dXtdF,qn->lambda);
560:   }
561:   return(0);
562: }

564: static PetscErrorCode SNESDestroy_QN(SNES snes)
565: {

569:   SNESReset_QN(snes);
570:   PetscFree(snes->data);
571:   PetscObjectComposeFunction((PetscObject)snes,"",NULL);
572:   return(0);
573: }

575: static PetscErrorCode SNESSetFromOptions_QN(PetscOptionItems *PetscOptionsObject,SNES snes)
576: {

578:   PetscErrorCode    ierr;
579:   SNES_QN           *qn    = (SNES_QN*)snes->data;
580:   PetscBool         monflg = PETSC_FALSE,flg;
581:   SNESLineSearch    linesearch;
582:   SNESQNRestartType rtype = qn->restart_type;
583:   SNESQNScaleType   stype = qn->scale_type;
584:   SNESQNType        qtype = qn->type;

587:   PetscOptionsHead(PetscOptionsObject,"SNES QN options");
588:   PetscOptionsInt("-snes_qn_m","Number of past states saved for L-BFGS methods","SNESQN",qn->m,&qn->m,NULL);
589:   PetscOptionsReal("-snes_qn_powell_gamma","Powell angle tolerance",          "SNESQN", qn->powell_gamma, &qn->powell_gamma, NULL);
590:   PetscOptionsBool("-snes_qn_monitor",         "Monitor for the QN methods",      "SNESQN", monflg, &monflg, NULL);
591:   PetscOptionsBool("-snes_qn_single_reduction", "Aggregate reductions",           "SNESQN", qn->singlereduction, &qn->singlereduction, NULL);
592:   PetscOptionsEnum("-snes_qn_scale_type","Scaling type","SNESQNSetScaleType",SNESQNScaleTypes,(PetscEnum)stype,(PetscEnum*)&stype,&flg);
593:   if (flg) SNESQNSetScaleType(snes,stype);

595:   PetscOptionsEnum("-snes_qn_restart_type","Restart type","SNESQNSetRestartType",SNESQNRestartTypes,(PetscEnum)rtype,(PetscEnum*)&rtype,&flg);
596:   if (flg) SNESQNSetRestartType(snes,rtype);

598:   PetscOptionsEnum("-snes_qn_type","Quasi-Newton update type","",SNESQNTypes,(PetscEnum)qtype,(PetscEnum*)&qtype,&flg);
599:   if (flg) {SNESQNSetType(snes,qtype);}
600:   PetscOptionsTail();
601:   if (!snes->linesearch) {
602:     SNESGetLineSearch(snes, &linesearch);
603:     if (qn->type == SNES_QN_LBFGS) {
604:       SNESLineSearchSetType(linesearch, SNESLINESEARCHCP);
605:     } else if (qn->type == SNES_QN_BROYDEN) {
606:       SNESLineSearchSetType(linesearch, SNESLINESEARCHBASIC);
607:     } else {
608:       SNESLineSearchSetType(linesearch, SNESLINESEARCHL2);
609:     }
610:   }
611:   if (monflg) {
612:     qn->monitor = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes));
613:   }
614:   return(0);
615: }

617: static PetscErrorCode SNESView_QN(SNES snes, PetscViewer viewer)
618: {
619:   SNES_QN        *qn    = (SNES_QN*)snes->data;
620:   PetscBool      iascii;

624:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
625:   if (iascii) {
626:     PetscViewerASCIIPrintf(viewer,"  type is %s, restart type is %s, scale type is %s\n",SNESQNTypes[qn->type],SNESQNRestartTypes[qn->restart_type],SNESQNScaleTypes[qn->scale_type]);
627:     PetscViewerASCIIPrintf(viewer,"  Stored subspace size: %D\n", qn->m);
628:     if (qn->singlereduction) {
629:       PetscViewerASCIIPrintf(viewer,"  Using the single reduction variant.\n");
630:     }
631:   }
632:   return(0);
633: }

635: /*@
636:     SNESQNSetRestartType - Sets the restart type for SNESQN.

638:     Logically Collective on SNES

640:     Input Parameters:
641: +   snes - the iterative context
642: -   rtype - restart type

644:     Options Database:
645: +   -snes_qn_restart_type <powell,periodic,none> - set the restart type
646: -   -snes_qn_m <m> - sets the number of stored updates and the restart period for periodic

648:     Level: intermediate

650:     SNESQNRestartTypes:
651: +   SNES_QN_RESTART_NONE - never restart
652: .   SNES_QN_RESTART_POWELL - restart based upon descent criteria
653: -   SNES_QN_RESTART_PERIODIC - restart after a fixed number of iterations

655: .keywords: SNES, SNESQN, restart, type, set SNESLineSearch, SNESQNRestartType
656: @*/
657: PetscErrorCode SNESQNSetRestartType(SNES snes, SNESQNRestartType rtype)
658: {

663:   PetscTryMethod(snes,"SNESQNSetRestartType_C",(SNES,SNESQNRestartType),(snes,rtype));
664:   return(0);
665: }

667: /*@
668:     SNESQNSetScaleType - Sets the scaling type for the inner inverse Jacobian in SNESQN.

670:     Logically Collective on SNES

672:     Input Parameters:
673: +   snes - the iterative context
674: -   stype - scale type

676:     Options Database:
677: .   -snes_qn_scale_type <shanno,none,linesearch,jacobian>

679:     Level: intermediate

681:     SNESQNScaleTypes:
682: +   SNES_QN_SCALE_NONE - don't scale the problem
683: .   SNES_QN_SCALE_SHANNO - use shanno scaling
684: .   SNES_QN_SCALE_LINESEARCH - scale based upon line search lambda
685: -   SNES_QN_SCALE_JACOBIAN - scale by solving a linear system coming from the Jacobian you provided with SNESSetJacobian() computed at the first iteration 
686:                              of QN and at ever restart.

688: .keywords: scaling type

690: .seealso: SNES, SNESQN, SNESLineSearch, SNESQNScaleType, SNESetJacobian()
691: @*/

693: PetscErrorCode SNESQNSetScaleType(SNES snes, SNESQNScaleType stype)
694: {

699:   PetscTryMethod(snes,"SNESQNSetScaleType_C",(SNES,SNESQNScaleType),(snes,stype));
700:   return(0);
701: }

703: PetscErrorCode SNESQNSetScaleType_QN(SNES snes, SNESQNScaleType stype)
704: {
705:   SNES_QN *qn = (SNES_QN*)snes->data;

708:   qn->scale_type = stype;
709:   return(0);
710: }

712: PetscErrorCode SNESQNSetRestartType_QN(SNES snes, SNESQNRestartType rtype)
713: {
714:   SNES_QN *qn = (SNES_QN*)snes->data;

717:   qn->restart_type = rtype;
718:   return(0);
719: }

721: /*@
722:     SNESQNSetType - Sets the quasi-Newton variant to be used in SNESQN.

724:     Logically Collective on SNES

726:     Input Parameters:
727: +   snes - the iterative context
728: -   qtype - variant type

730:     Options Database:
731: .   -snes_qn_type <lbfgs,broyden,badbroyden>

733:     Level: beginner

735:     SNESQNTypes:
736: +   SNES_QN_LBFGS - LBFGS variant
737: .   SNES_QN_BROYDEN - Broyden variant
738: -   SNES_QN_BADBROYDEN - Bad Broyden variant

740: .keywords: SNES, SNESQN, type, set, SNESQNType
741: @*/

743: PetscErrorCode SNESQNSetType(SNES snes, SNESQNType qtype)
744: {

749:   PetscTryMethod(snes,"SNESQNSetType_C",(SNES,SNESQNType),(snes,qtype));
750:   return(0);
751: }

753: PetscErrorCode SNESQNSetType_QN(SNES snes, SNESQNType qtype)
754: {
755:   SNES_QN *qn = (SNES_QN*)snes->data;

758:   qn->type = qtype;
759:   return(0);
760: }

762: /* -------------------------------------------------------------------------- */
763: /*MC
764:       SNESQN - Limited-Memory Quasi-Newton methods for the solution of nonlinear systems.

766:       Options Database:

768: +     -snes_qn_m <m> - Number of past states saved for the L-Broyden methods.
769: +     -snes_qn_restart_type <powell,periodic,none> - set the restart type
770: .     -snes_qn_powell_gamma - Angle condition for restart.
771: .     -snes_qn_powell_descent - Descent condition for restart.
772: .     -snes_qn_type <lbfgs,broyden,badbroyden> - QN type
773: .     -snes_qn_scale_type <shanno,none,linesearch,jacobian> - scaling performed on inner Jacobian
774: .     -snes_linesearch_type <cp, l2, basic> - Type of line search.
775: -     -snes_qn_monitor - Monitors the quasi-newton Jacobian.

777:       Notes:
778:     This implements the L-BFGS, Broyden, and "Bad" Broyden algorithms for the solution of F(x) = b using
779:       previous change in F(x) and x to form the approximate inverse Jacobian using a series of multiplicative rank-one
780:       updates.

782:       When using a nonlinear preconditioner, one has two options as to how the preconditioner is applied.  The first of
783:       these options, sequential, uses the preconditioner to generate a new solution and function and uses those at this
784:       iteration as the current iteration's values when constructing the approximate Jacobian.  The second, composed,
785:       perturbs the problem the Jacobian represents to be P(x, b) - x = 0, where P(x, b) is the preconditioner.

787:       Uses left nonlinear preconditioning by default.

789:       References:
790: +   1. -   Kelley, C.T., Iterative Methods for Linear and Nonlinear Equations, Chapter 8, SIAM, 1995.
791: .   2. -   R. Byrd, J. Nocedal, R. Schnabel, Representations of Quasi Newton Matrices and their use in Limited Memory Methods,
792:       Technical Report, Northwestern University, June 1992.
793: .   3. -   Peter N. Brown, Alan C. Hindmarsh, Homer F. Walker, Experiments with Quasi-Newton Methods in Solving Stiff ODE
794:       Systems, SIAM J. Sci. Stat. Comput. Vol 6(2), April 1985.
795: -   4. -   Peter R. Brune, Matthew G. Knepley, Barry F. Smith, and Xuemin Tu, "Composing Scalable Nonlinear Algebraic Solvers", 
796:        SIAM Review, 57(4), 2015

798:       Level: beginner

800: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR

802: M*/
803: PETSC_EXTERN PetscErrorCode SNESCreate_QN(SNES snes)
804: {
806:   SNES_QN        *qn;

809:   snes->ops->setup          = SNESSetUp_QN;
810:   snes->ops->solve          = SNESSolve_QN;
811:   snes->ops->destroy        = SNESDestroy_QN;
812:   snes->ops->setfromoptions = SNESSetFromOptions_QN;
813:   snes->ops->view           = SNESView_QN;
814:   snes->ops->reset          = SNESReset_QN;

816:   snes->npcside= PC_LEFT;

818:   snes->usesnpc = PETSC_TRUE;
819:   snes->usesksp = PETSC_FALSE;

821:   snes->alwayscomputesfinalresidual = PETSC_TRUE;

823:   if (!snes->tolerancesset) {
824:     snes->max_funcs = 30000;
825:     snes->max_its   = 10000;
826:   }

828:   PetscNewLog(snes,&qn);
829:   snes->data          = (void*) qn;
830:   qn->m               = 10;
831:   qn->scaling         = 1.0;
832:   qn->U               = NULL;
833:   qn->V               = NULL;
834:   qn->dXtdF           = NULL;
835:   qn->dFtdX           = NULL;
836:   qn->dXdFmat         = NULL;
837:   qn->monitor         = NULL;
838:   qn->singlereduction = PETSC_TRUE;
839:   qn->powell_gamma    = 0.9999;
840:   qn->scale_type      = SNES_QN_SCALE_DEFAULT;
841:   qn->restart_type    = SNES_QN_RESTART_DEFAULT;
842:   qn->type            = SNES_QN_LBFGS;

844:   PetscObjectComposeFunction((PetscObject)snes,"SNESQNSetScaleType_C",SNESQNSetScaleType_QN);
845:   PetscObjectComposeFunction((PetscObject)snes,"SNESQNSetRestartType_C",SNESQNSetRestartType_QN);
846:   PetscObjectComposeFunction((PetscObject)snes,"SNESQNSetType_C",SNESQNSetType_QN);
847:   return(0);
848: }