Date: 2016-07-24T17:09:31+00:00

Actual source code: ms.c
petsc-3.7.3 2016-07-24
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;

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

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

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

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

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

    333:     if (i+1 < snes->max_its || ms->norms) {
    334:       SNESComputeFunction(snes,X,F);
    335:     }

    337:     if (ms->norms) {
    338:       VecNorm(F,NORM_2,&fnorm); /* fnorm <- ||F||  */
    339:       SNESCheckFunctionNorm(snes,fnorm);

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

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

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

    365: static PetscErrorCode SNESSetUp_MS(SNES snes)
    366: {
    367:   SNES_MS        *ms = (SNES_MS*)snes->data;

    371:   if (!ms->tableau) {SNESMSSetType(snes,SNESMSDefault);}
    372:   SNESSetWorkVecs(snes,3);
    373:   SNESSetUpMatrices(snes);
    374:   return(0);
    375: }

    379: static PetscErrorCode SNESReset_MS(SNES snes)
    380: {

    383:   return(0);
    384: }

    388: static PetscErrorCode SNESDestroy_MS(SNES snes)
    389: {

    393:   PetscFree(snes->data);
    394:   PetscObjectComposeFunction((PetscObject)snes,"",NULL);
    395:   return(0);
    396: }

    400: static PetscErrorCode SNESView_MS(SNES snes,PetscViewer viewer)
    401: {
    402:   PetscBool      iascii;
    404:   SNES_MS        *ms = (SNES_MS*)snes->data;

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

    417: static PetscErrorCode SNESSetFromOptions_MS(PetscOptionItems *PetscOptionsObject,SNES snes)
    418: {
    419:   SNES_MS        *ms = (SNES_MS*)snes->data;

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

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

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

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

    473: /*@C
    474:   SNESMSSetType - Set the type of multistage smoother

    476:   Logically collective

    478:   Input Parameter:
    479: +  snes - nonlinear solver context
    480: -  mstype - type of multistage method

    482:   Level: beginner

    484: .seealso: SNESMSGetType(), SNESMS
    485: @*/
    486: PetscErrorCode SNESMSSetType(SNES snes,SNESMSType rostype)
    487: {

    492:   PetscTryMethod(snes,"SNESMSSetType_C",(SNES,SNESMSType),(snes,rostype));
    493:   return(0);
    494: }

    496: /* -------------------------------------------------------------------------- */
    497: /*MC
    498:       SNESMS - multi-stage smoothers

    500:       Options Database:

    502: +     -snes_ms_type - type of multi-stage smoother
    503: -     -snes_ms_damping - damping for multi-stage method

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

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

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

    513:       References:
    514: +   1. -   Ketcheson (2010) Runge Kutta methods with minimum storage implementations.
    515: .   2. -   Jameson (1983) Solution of the Euler equations for two dimensional transonic flow by a multigrid method.
    516: -   3. -   Pierce and Giles (1997) Preconditioned multigrid methods for compressible flow calculations on stretched meshes.

    518:       Level: beginner

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

    522: M*/
    525: PETSC_EXTERN PetscErrorCode SNESCreate_MS(SNES snes)
    526: {
    528:   SNES_MS        *ms;

    531:   SNESMSInitializePackage();

    533:   snes->ops->setup          = SNESSetUp_MS;
    534:   snes->ops->solve          = SNESSolve_MS;
    535:   snes->ops->destroy        = SNESDestroy_MS;
    536:   snes->ops->setfromoptions = SNESSetFromOptions_MS;
    537:   snes->ops->view           = SNESView_MS;
    538:   snes->ops->reset          = SNESReset_MS;

    540:   snes->usespc  = PETSC_FALSE;
    541:   snes->usesksp = PETSC_TRUE;

    543:   PetscNewLog(snes,&ms);
    544:   snes->data  = (void*)ms;
    545:   ms->damping = 0.9;
    546:   ms->norms   = PETSC_FALSE;

    548:   PetscObjectComposeFunction((PetscObject)snes,"SNESMSSetType_C",SNESMSSetType_MS);
    549:   return(0);
    550: }