Actual source code: ms.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  1: #include <petsc/private/snesimpl.h>   /*I "petscsnes.h" I*/

  3: static SNESMSType SNESMSDefault = SNESMSM62;
  4: static PetscBool  SNESMSRegisterAllCalled;
  5: static PetscBool  SNESMSPackageInitialized;

  7: typedef struct _SNESMSTableau *SNESMSTableau;
  8: struct _SNESMSTableau {
  9:   char      *name;
 10:   PetscInt  nstages;            /* Number of stages */
 11:   PetscInt  nregisters;         /* Number of registers */
 12:   PetscReal stability;          /* Scaled stability region */
 13:   PetscReal *gamma;             /* Coefficients of 3S* method */
 14:   PetscReal *delta;             /* Coefficients of 3S* method */
 15:   PetscReal *betasub;           /* Subdiagonal of beta in Shu-Osher form */
 16: };

 18: typedef struct _SNESMSTableauLink *SNESMSTableauLink;
 19: struct _SNESMSTableauLink {
 20:   struct _SNESMSTableau tab;
 21:   SNESMSTableauLink     next;
 22: };
 23: static SNESMSTableauLink SNESMSTableauList;

 25: typedef struct {
 26:   SNESMSTableau tableau;        /* Tableau in low-storage form */
 27:   PetscReal     damping;        /* Damping parameter, like length of (pseudo) time step */
 28:   PetscBool     norms;          /* Compute norms, usually only for monitoring purposes */
 29: } SNES_MS;

 33: /*@C
 34:   SNESMSRegisterAll - Registers all of the multi-stage methods in SNESMS

 36:   Not Collective, but should be called by all processes which will need the schemes to be registered

 38:   Level: advanced

 40: .keywords: SNES, SNESMS, register, all

 42: .seealso:  SNESMSRegisterDestroy()
 43: @*/
 44: PetscErrorCode SNESMSRegisterAll(void)
 45: {

 49:   if (SNESMSRegisterAllCalled) return(0);
 50:   SNESMSRegisterAllCalled = PETSC_TRUE;

 52:   {
 53:     PetscReal gamma[3][1] = {{1.0},{0.0},{0.0}};
 54:     PetscReal delta[1]    = {0.0};
 55:     PetscReal betasub[1]  = {1.0};
 56:     SNESMSRegister(SNESMSEULER,1,3,1.0,&gamma[0][0],delta,betasub);
 57:   }

 59:   {
 60:     PetscReal gamma[3][6] = {
 61:       {0.0000000000000000E+00,  -7.0304722367110606E-01, -1.9836719667506464E-01, -1.6023843981863788E+00, 9.4483822882855284E-02,  -1.4204296130641869E-01},
 62:       {1.0000000000000000E+00,  1.1111025767083920E+00,  5.6150921583923230E-01,  7.4151723494934041E-01,  3.1714538168600587E-01,  4.6479276238548706E-01},
 63:       {0.0000000000000000E+00,  0.0000000000000000E+00,  0.0000000000000000E+00,  6.7968174970583317E-01,  -4.1755042846051737E-03, -1.9115668129923846E-01}
 64:     };
 65:     PetscReal delta[6]   = {1.0000000000000000E+00, 5.3275427433201750E-01, 6.0143544663985238E-01, 4.5874077053842177E-01, 2.7544386906104651E-01, 0.0000000000000000E+00};
 66:     PetscReal betasub[6] = {8.4753115429481929E-01, 7.4018896368655618E-01, 6.5963574086583309E-03, 4.6747795645517759E-01, 1.3314545813643919E-01, 5.3260800028018784E-01};
 67:     SNESMSRegister(SNESMSM62,6,3,1.0,&gamma[0][0],delta,betasub);
 68:   }
 69:   {
 70:     PetscReal gamma[3][4] = {{0,0,0,0},{0,0,0,0},{1,1,1,1}};
 71:     PetscReal delta[4]    = {0,0,0,0};
 72:     PetscReal betasub[4]  = {0.25, 0.5, 0.55, 1.0};
 73:     SNESMSRegister(SNESMSJAMESON83,4,3,1.0,&gamma[0][0],delta,betasub);
 74:   }
 75:   {                             /* Van Leer, Tai, and Powell (1989) 2 stage, order 1 */
 76:     PetscReal gamma[3][2] = {{0,0},{0,0},{1,1}};
 77:     PetscReal delta[2]    = {0,0};
 78:     PetscReal betasub[2]  = {0.3333,1.0};
 79:     SNESMSRegister(SNESMSVLTP21,2,3,1.0,&gamma[0][0],delta,betasub);
 80:   }
 81:   {                             /* Van Leer, Tai, and Powell (1989) 3 stage, order 1 */
 82:     PetscReal gamma[3][3] = {{0,0,0},{0,0,0},{1,1,1}};
 83:     PetscReal delta[3]    = {0,0,0};
 84:     PetscReal betasub[3]  = {0.1481,0.4000,1.0};
 85:     SNESMSRegister(SNESMSVLTP31,3,3,1.5,&gamma[0][0],delta,betasub);
 86:   }
 87:   {                             /* Van Leer, Tai, and Powell (1989) 4 stage, order 1 */
 88:     PetscReal gamma[3][4] = {{0,0,0,0},{0,0,0,0},{1,1,1,1}};
 89:     PetscReal delta[4]    = {0,0,0,0};
 90:     PetscReal betasub[4]  = {0.0833,0.2069,0.4265,1.0};
 91:     SNESMSRegister(SNESMSVLTP41,4,3,2.0,&gamma[0][0],delta,betasub);
 92:   }
 93:   {                             /* Van Leer, Tai, and Powell (1989) 5 stage, order 1 */
 94:     PetscReal gamma[3][5] = {{0,0,0,0,0},{0,0,0,0,0},{1,1,1,1,1}};
 95:     PetscReal delta[5]    = {0,0,0,0,0};
 96:     PetscReal betasub[5]  = {0.0533,0.1263,0.2375,0.4414,1.0};
 97:     SNESMSRegister(SNESMSVLTP51,5,3,2.5,&gamma[0][0],delta,betasub);
 98:   }
 99:   {                             /* Van Leer, Tai, and Powell (1989) 6 stage, order 1 */
100:     PetscReal gamma[3][6] = {{0,0,0,0,0,0},{0,0,0,0,0,0},{1,1,1,1,1,1}};
101:     PetscReal delta[6]    = {0,0,0,0,0,0};
102:     PetscReal betasub[6]  = {0.0370,0.0851,0.1521,0.2562,0.4512,1.0};
103:     SNESMSRegister(SNESMSVLTP61,6,3,3.0,&gamma[0][0],delta,betasub);
104:   }
105:   return(0);
106: }

110: /*@C
111:    SNESMSRegisterDestroy - Frees the list of schemes that were registered by TSRosWRegister().

113:    Not Collective

115:    Level: advanced

117: .keywords: TSRosW, register, destroy
118: .seealso: TSRosWRegister(), TSRosWRegisterAll(), TSRosWRegister()
119: @*/
120: PetscErrorCode SNESMSRegisterDestroy(void)
121: {
122:   PetscErrorCode    ierr;
123:   SNESMSTableauLink link;

126:   while ((link = SNESMSTableauList)) {
127:     SNESMSTableau t = &link->tab;
128:     SNESMSTableauList = link->next;

130:     PetscFree3(t->gamma,t->delta,t->betasub);
131:     PetscFree(t->name);
132:     PetscFree(link);
133:   }
134:   SNESMSRegisterAllCalled = PETSC_FALSE;
135:   return(0);
136: }

140: /*@C
141:   SNESMSInitializePackage - This function initializes everything in the SNESMS package. It is called
142:   from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to SNESCreate_MS()
143:   when using static libraries.

145:   Level: developer

147: .keywords: SNES, SNESMS, initialize, package
148: .seealso: PetscInitialize()
149: @*/
150: PetscErrorCode SNESMSInitializePackage(void)
151: {

155:   if (SNESMSPackageInitialized) return(0);
156:   SNESMSPackageInitialized = PETSC_TRUE;

158:   SNESMSRegisterAll();
159:   PetscRegisterFinalize(SNESMSFinalizePackage);
160:   return(0);
161: }

165: /*@C
166:   SNESMSFinalizePackage - This function destroys everything in the SNESMS package. It is
167:   called from PetscFinalize().

169:   Level: developer

171: .keywords: Petsc, destroy, package
172: .seealso: PetscFinalize()
173: @*/
174: PetscErrorCode SNESMSFinalizePackage(void)
175: {

179:   SNESMSPackageInitialized = PETSC_FALSE;

181:   SNESMSRegisterDestroy();
182:   return(0);
183: }

187: /*@C
188:    SNESMSRegister - register a multistage scheme

190:    Not Collective, but the same schemes should be registered on all processes on which they will be used

192:    Input Parameters:
193: +  name - identifier for method
194: .  nstages - number of stages
195: .  nregisters - number of registers used by low-storage implementation
196: .  gamma - coefficients, see Ketcheson's paper
197: .  delta - coefficients, see Ketcheson's paper
198: -  betasub - subdiagonal of Shu-Osher form

200:    Notes:
201:    The notation is described in Ketcheson (2010) Runge-Kutta methods with minimum storage implementations.

203:    Level: advanced

205: .keywords: SNES, register

207: .seealso: SNESMS
208: @*/
209: PetscErrorCode SNESMSRegister(SNESMSType name,PetscInt nstages,PetscInt nregisters,PetscReal stability,const PetscReal gamma[],const PetscReal delta[],const PetscReal betasub[])
210: {
211:   PetscErrorCode    ierr;
212:   SNESMSTableauLink link;
213:   SNESMSTableau     t;

217:   if (nstages < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have at least one stage");
218:   if (nregisters != 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only support for methods written in 3-register form");

223:   PetscNew(&link);
224:   t             = &link->tab;
225:   PetscStrallocpy(name,&t->name);
226:   t->nstages    = nstages;
227:   t->nregisters = nregisters;
228:   t->stability  = stability;

230:   PetscMalloc3(nstages*nregisters,&t->gamma,nstages,&t->delta,nstages,&t->betasub);
231:   PetscMemcpy(t->gamma,gamma,nstages*nregisters*sizeof(PetscReal));
232:   PetscMemcpy(t->delta,delta,nstages*sizeof(PetscReal));
233:   PetscMemcpy(t->betasub,betasub,nstages*sizeof(PetscReal));

235:   link->next        = SNESMSTableauList;
236:   SNESMSTableauList = link;
237:   return(0);
238: }

242: /*
243:   X - initial state, updated in-place.
244:   F - residual, computed at the initial X on input
245: */
246: static PetscErrorCode SNESMSStep_3Sstar(SNES snes,Vec X,Vec F)
247: {
248:   PetscErrorCode  ierr;
249:   SNES_MS         *ms    = (SNES_MS*)snes->data;
250:   SNESMSTableau   t      = ms->tableau;
251:   const PetscReal *gamma = t->gamma,*delta = t->delta,*betasub = t->betasub;
252:   Vec             S1,S2,S3,Y;
253:   PetscInt        i,nstages = t->nstages;;


257:   Y    = snes->work[0];
258:   S1   = X;
259:   S2   = snes->work[1];
260:   S3   = snes->work[2];
261:   VecZeroEntries(S2);
262:   VecCopy(X,S3);
263:   for (i=0; i<nstages; i++) {
264:     Vec         Ss[4];
265:     PetscScalar scoeff[4];

267:     Ss[0] = S1; Ss[1] = S2; Ss[2] = S3; Ss[3] = Y;

269:     scoeff[0] = gamma[0*nstages+i]-(PetscReal)1.0;
270:     scoeff[1] = gamma[1*nstages+i];
271:     scoeff[2] = gamma[2*nstages+i];
272:     scoeff[3] = -betasub[i]*ms->damping;

274:     VecAXPY(S2,delta[i],S1);
275:     if (i>0) {
276:       SNESComputeFunction(snes,S1,F);
277:     }
278:     KSPSolve(snes->ksp,F,Y);
279:     VecMAXPY(S1,4,scoeff,Ss);
280:   }
281:   return(0);
282: }

286: static PetscErrorCode SNESSolve_MS(SNES snes)
287: {
288:   SNES_MS        *ms = (SNES_MS*)snes->data;
289:   Vec            X   = snes->vec_sol,F = snes->vec_func;
290:   PetscReal      fnorm;
291:   PetscInt       i;


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

300:   PetscCitationsRegister(SNESCitation,&SNEScite);
301:   snes->reason = SNES_CONVERGED_ITERATING;
302:   PetscObjectSAWsTakeAccess((PetscObject)snes);
303:   snes->iter   = 0;
304:   snes->norm   = 0.;
305:   PetscObjectSAWsGrantAccess((PetscObject)snes);
306:   if (!snes->vec_func_init_set) {
307:     SNESComputeFunction(snes,X,F);
308:   } else snes->vec_func_init_set = PETSC_FALSE;

310:   if (snes->jacobian) {         /* This method does not require a Jacobian, but it is usually preconditioned by PBJacobi */
311:     SNESComputeJacobian(snes,snes->vec_sol,snes->jacobian,snes->jacobian_pre);
312:   }
313:   if (ms->norms) {
314:     VecNorm(F,NORM_2,&fnorm); /* fnorm <- ||F||  */
315:     SNESCheckFunctionNorm(snes,fnorm);
316:     /* Monitor convergence */
317:     PetscObjectSAWsTakeAccess((PetscObject)snes);
318:     snes->iter = 0;
319:     snes->norm = fnorm;
320:     PetscObjectSAWsGrantAccess((PetscObject)snes);
321:     SNESLogConvergenceHistory(snes,snes->norm,0);
322:     SNESMonitor(snes,snes->iter,snes->norm);

324:     /* Test for convergence */
325:     (*snes->ops->converged)(snes,snes->iter,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
326:     if (snes->reason) return(0);
327:   }

329:   /* Call general purpose update function */
330:   if (snes->ops->update) {
331:     (*snes->ops->update)(snes,snes->iter);
332:   }
333:   for (i = 0; i < snes->max_its; i++) {
334:     SNESMSStep_3Sstar(snes,X,F);

336:     if (i+1 < snes->max_its || ms->norms) {
337:       SNESComputeFunction(snes,X,F);
338:     }

340:     if (ms->norms) {
341:       VecNorm(F,NORM_2,&fnorm); /* fnorm <- ||F||  */
342:       SNESCheckFunctionNorm(snes,fnorm);

344:       /* Monitor convergence */
345:       PetscObjectSAWsTakeAccess((PetscObject)snes);
346:       snes->iter = i+1;
347:       snes->norm = fnorm;
348:       PetscObjectSAWsGrantAccess((PetscObject)snes);
349:       SNESLogConvergenceHistory(snes,snes->norm,0);
350:       SNESMonitor(snes,snes->iter,snes->norm);

352:       /* Test for convergence */
353:       (*snes->ops->converged)(snes,snes->iter,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
354:       if (snes->reason) return(0);
355:     }

357:     /* Call general purpose update function */
358:     if (snes->ops->update) {
359:       (*snes->ops->update)(snes, snes->iter);
360:     }
361:   }
362:   if (!snes->reason) snes->reason = SNES_CONVERGED_ITS;
363:   return(0);
364: }

368: static PetscErrorCode SNESSetUp_MS(SNES snes)
369: {
370:   SNES_MS        *ms = (SNES_MS*)snes->data;

374:   if (!ms->tableau) {SNESMSSetType(snes,SNESMSDefault);}
375:   SNESSetWorkVecs(snes,3);
376:   SNESSetUpMatrices(snes);
377:   return(0);
378: }

382: static PetscErrorCode SNESReset_MS(SNES snes)
383: {

386:   return(0);
387: }

391: static PetscErrorCode SNESDestroy_MS(SNES snes)
392: {

396:   PetscFree(snes->data);
397:   PetscObjectComposeFunction((PetscObject)snes,"",NULL);
398:   return(0);
399: }

403: static PetscErrorCode SNESView_MS(SNES snes,PetscViewer viewer)
404: {
405:   PetscBool      iascii;
407:   SNES_MS        *ms = (SNES_MS*)snes->data;

410:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);
411:   if (iascii) {
412:     SNESMSTableau tab = ms->tableau;
413:     PetscViewerASCIIPrintf(viewer,"  multi-stage method type: %s\n",tab ? tab->name : "not yet set");
414:   }
415:   return(0);
416: }

420: static PetscErrorCode SNESSetFromOptions_MS(PetscOptions *PetscOptionsObject,SNES snes)
421: {
422:   SNES_MS        *ms = (SNES_MS*)snes->data;

426:   PetscOptionsHead(PetscOptionsObject,"SNES MS options");
427:   {
428:     SNESMSTableauLink link;
429:     PetscInt          count,choice;
430:     PetscBool         flg;
431:     const char        **namelist;
432:     char              mstype[256];

434:     PetscStrncpy(mstype,SNESMSDefault,sizeof(mstype));
435:     for (link=SNESMSTableauList,count=0; link; link=link->next,count++) ;
436:     PetscMalloc1(count,&namelist);
437:     for (link=SNESMSTableauList,count=0; link; link=link->next,count++) namelist[count] = link->tab.name;
438:     PetscOptionsEList("-snes_ms_type","Multistage smoother type","SNESMSSetType",(const char*const*)namelist,count,mstype,&choice,&flg);
439:     SNESMSSetType(snes,flg ? namelist[choice] : mstype);
440:     PetscFree(namelist);
441:     PetscOptionsReal("-snes_ms_damping","Damping for multistage method","SNESMSSetDamping",ms->damping,&ms->damping,NULL);
442:     PetscOptionsBool("-snes_ms_norms","Compute norms for monitoring","none",ms->norms,&ms->norms,NULL);
443:   }
444:   PetscOptionsTail();
445:   return(0);
446: }

450: PetscErrorCode  SNESMSSetType_MS(SNES snes,SNESMSType mstype)
451: {
452:   PetscErrorCode    ierr;
453:   SNES_MS           *ms = (SNES_MS*)snes->data;
454:   SNESMSTableauLink link;
455:   PetscBool         match;

458:   if (ms->tableau) {
459:     PetscStrcmp(ms->tableau->name,mstype,&match);
460:     if (match) return(0);
461:   }
462:   for (link = SNESMSTableauList; link; link=link->next) {
463:     PetscStrcmp(link->tab.name,mstype,&match);
464:     if (match) {
465:       SNESReset_MS(snes);
466:       ms->tableau = &link->tab;
467:       return(0);
468:     }
469:   }
470:   SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_UNKNOWN_TYPE,"Could not find '%s'",mstype);
471:   return(0);
472: }

476: /*@C
477:   SNESMSSetType - Set the type of multistage smoother

479:   Logically collective

481:   Input Parameter:
482: +  snes - nonlinear solver context
483: -  mstype - type of multistage method

485:   Level: beginner

487: .seealso: SNESMSGetType(), SNESMS
488: @*/
489: PetscErrorCode SNESMSSetType(SNES snes,SNESMSType rostype)
490: {

495:   PetscTryMethod(snes,"SNESMSSetType_C",(SNES,SNESMSType),(snes,rostype));
496:   return(0);
497: }

499: /* -------------------------------------------------------------------------- */
500: /*MC
501:       SNESMS - multi-stage smoothers

503:       Options Database:

505: +     -snes_ms_type - type of multi-stage smoother
506: -     -snes_ms_damping - damping for multi-stage method

508:       Notes:
509:       These multistage methods are explicit Runge-Kutta methods that are often used as smoothers for
510:       FAS multigrid for transport problems. In the linear case, these are equivalent to polynomial smoothers (such as Chebyshev).

512:       Multi-stage smoothers should usually be preconditioned by point-block Jacobi to ensure proper scaling and to normalize the wave speeds.

514:       The methods are specified in low storage form (Ketcheson 2010). New methods can be registered with SNESMSRegister().

516:       References:

518:       Ketcheson (2010) Runge-Kutta methods with minimum storage implementations.

520:       Jameson (1983) Solution of the Euler equations for two dimensional transonic flow by a multigrid method.

522:       Pierce and Giles (1997) Preconditioned multigrid methods for compressible flow calculations on stretched meshes.

524:       Level: beginner

526: .seealso:  SNESCreate(), SNES, SNESSetType(), SNESMS, SNESFAS, KSPCHEBYSHEV

528: M*/
531: PETSC_EXTERN PetscErrorCode SNESCreate_MS(SNES snes)
532: {
534:   SNES_MS        *ms;

537:   SNESMSInitializePackage();

539:   snes->ops->setup          = SNESSetUp_MS;
540:   snes->ops->solve          = SNESSolve_MS;
541:   snes->ops->destroy        = SNESDestroy_MS;
542:   snes->ops->setfromoptions = SNESSetFromOptions_MS;
543:   snes->ops->view           = SNESView_MS;
544:   snes->ops->reset          = SNESReset_MS;

546:   snes->usespc  = PETSC_FALSE;
547:   snes->usesksp = PETSC_TRUE;

549:   PetscNewLog(snes,&ms);
550:   snes->data  = (void*)ms;
551:   ms->damping = 0.9;
552:   ms->norms   = PETSC_FALSE;

554:   PetscObjectComposeFunction((PetscObject)snes,"SNESMSSetType_C",SNESMSSetType_MS);
555:   return(0);
556: }