Actual source code: glle.c

      2: #include <../src/ts/impls/implicit/glle/glle.h>
      3: #include <petscdm.h>
      4: #include <petscblaslapack.h>

      6: static const char        *TSGLLEErrorDirections[] = {"FORWARD","BACKWARD","TSGLLEErrorDirection","TSGLLEERROR_",NULL};
      7: static PetscFunctionList TSGLLEList;
      8: static PetscFunctionList TSGLLEAcceptList;
      9: static PetscBool         TSGLLEPackageInitialized;
     10: static PetscBool         TSGLLERegisterAllCalled;

     12: /* This function is pure */
     13: static PetscScalar Factorial(PetscInt n)
     14: {
     15:   PetscInt i;
     16:   if (n < 12) {                 /* Can compute with 32-bit integers */
     17:     PetscInt f = 1;
     18:     for (i=2; i<=n; i++) f *= i;
     19:     return (PetscScalar)f;
     20:   } else {
     21:     PetscScalar f = 1.;
     22:     for (i=2; i<=n; i++) f *= (PetscScalar)i;
     23:     return f;
     24:   }
     25: }

     27: /* This function is pure */
     28: static PetscScalar CPowF(PetscScalar c,PetscInt p)
     29: {
     30:   return PetscPowRealInt(PetscRealPart(c),p)/Factorial(p);
     31: }

     33: static PetscErrorCode TSGLLEGetVecs(TS ts,DM dm,Vec *Z,Vec *Ydotstage)
     34: {
     35:   TS_GLLE        *gl = (TS_GLLE*)ts->data;

     39:   if (Z) {
     40:     if (dm && dm != ts->dm) {
     41:       DMGetNamedGlobalVector(dm,"TSGLLE_Z",Z);
     42:     } else *Z = gl->Z;
     43:   }
     44:   if (Ydotstage) {
     45:     if (dm && dm != ts->dm) {
     46:       DMGetNamedGlobalVector(dm,"TSGLLE_Ydot",Ydotstage);
     47:     } else *Ydotstage = gl->Ydot[gl->stage];
     48:   }
     49:   return(0);
     50: }

     53: static PetscErrorCode TSGLLERestoreVecs(TS ts,DM dm,Vec *Z,Vec *Ydotstage)
     54: {

     58:   if (Z) {
     59:     if (dm && dm != ts->dm) {
     60:       DMRestoreNamedGlobalVector(dm,"TSGLLE_Z",Z);
     61:     }
     62:   }
     63:   if (Ydotstage) {

     65:     if (dm && dm != ts->dm) {
     66:       DMRestoreNamedGlobalVector(dm,"TSGLLE_Ydot",Ydotstage);
     67:     }
     68:   }
     69:   return(0);
     70: }

     72: static PetscErrorCode DMCoarsenHook_TSGLLE(DM fine,DM coarse,void *ctx)
     73: {
     75:   return(0);
     76: }

     78: static PetscErrorCode DMRestrictHook_TSGLLE(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
     79: {
     80:   TS             ts = (TS)ctx;
     82:   Vec            Ydot,Ydot_c;

     85:   TSGLLEGetVecs(ts,fine,NULL,&Ydot);
     86:   TSGLLEGetVecs(ts,coarse,NULL,&Ydot_c);
     87:   MatRestrict(restrct,Ydot,Ydot_c);
     88:   VecPointwiseMult(Ydot_c,rscale,Ydot_c);
     89:   TSGLLERestoreVecs(ts,fine,NULL,&Ydot);
     90:   TSGLLERestoreVecs(ts,coarse,NULL,&Ydot_c);
     91:   return(0);
     92: }

     94: static PetscErrorCode DMSubDomainHook_TSGLLE(DM dm,DM subdm,void *ctx)
     95: {
     97:   return(0);
     98: }

    100: static PetscErrorCode DMSubDomainRestrictHook_TSGLLE(DM dm,VecScatter gscat, VecScatter lscat,DM subdm,void *ctx)
    101: {
    102:   TS             ts = (TS)ctx;
    104:   Vec            Ydot,Ydot_s;

    107:   TSGLLEGetVecs(ts,dm,NULL,&Ydot);
    108:   TSGLLEGetVecs(ts,subdm,NULL,&Ydot_s);

    110:   VecScatterBegin(gscat,Ydot,Ydot_s,INSERT_VALUES,SCATTER_FORWARD);
    111:   VecScatterEnd(gscat,Ydot,Ydot_s,INSERT_VALUES,SCATTER_FORWARD);

    113:   TSGLLERestoreVecs(ts,dm,NULL,&Ydot);
    114:   TSGLLERestoreVecs(ts,subdm,NULL,&Ydot_s);
    115:   return(0);
    116: }

    118: static PetscErrorCode TSGLLESchemeCreate(PetscInt p,PetscInt q,PetscInt r,PetscInt s,const PetscScalar *c,
    119:                                        const PetscScalar *a,const PetscScalar *b,const PetscScalar *u,const PetscScalar *v,TSGLLEScheme *inscheme)
    120: {
    121:   TSGLLEScheme     scheme;
    122:   PetscInt       j;

    126:   if (p < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Scheme order must be positive");
    127:   if (r < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"At least one item must be carried between steps");
    128:   if (s < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"At least one stage is required");
    130:   *inscheme = NULL;
    131:   PetscNew(&scheme);
    132:   scheme->p = p;
    133:   scheme->q = q;
    134:   scheme->r = r;
    135:   scheme->s = s;

    137:   PetscMalloc5(s,&scheme->c,s*s,&scheme->a,r*s,&scheme->b,r*s,&scheme->u,r*r,&scheme->v);
    138:   PetscArraycpy(scheme->c,c,s);
    139:   for (j=0; j<s*s; j++) scheme->a[j] = (PetscAbsScalar(a[j]) < 1e-12) ? 0 : a[j];
    140:   for (j=0; j<r*s; j++) scheme->b[j] = (PetscAbsScalar(b[j]) < 1e-12) ? 0 : b[j];
    141:   for (j=0; j<s*r; j++) scheme->u[j] = (PetscAbsScalar(u[j]) < 1e-12) ? 0 : u[j];
    142:   for (j=0; j<r*r; j++) scheme->v[j] = (PetscAbsScalar(v[j]) < 1e-12) ? 0 : v[j];

    144:   PetscMalloc6(r,&scheme->alpha,r,&scheme->beta,r,&scheme->gamma,3*s,&scheme->phi,3*r,&scheme->psi,r,&scheme->stage_error);
    145:   {
    146:     PetscInt     i,j,k,ss=s+2;
    147:     PetscBLASInt m,n,one=1,*ipiv,lwork=4*((s+3)*3+3),info,ldb;
    148:     PetscReal    rcond,*sing,*workreal;
    149:     PetscScalar  *ImV,*H,*bmat,*workscalar,*c=scheme->c,*a=scheme->a,*b=scheme->b,*u=scheme->u,*v=scheme->v;
    150:     PetscBLASInt rank;
    151:     PetscMalloc7(PetscSqr(r),&ImV,3*s,&H,3*ss,&bmat,lwork,&workscalar,5*(3+r),&workreal,r+s,&sing,r+s,&ipiv);

    153:     /* column-major input */
    154:     for (i=0; i<r-1; i++) {
    155:       for (j=0; j<r-1; j++) ImV[i+j*r] = 1.0*(i==j) - v[(i+1)*r+j+1];
    156:     }
    157:     /* Build right hand side for alpha (tp - glm.B(2:end,:)*(glm.c.^(p)./factorial(p))) */
    158:     for (i=1; i<r; i++) {
    159:       scheme->alpha[i] = 1./Factorial(p+1-i);
    160:       for (j=0; j<s; j++) scheme->alpha[i] -= b[i*s+j]*CPowF(c[j],p);
    161:     }
    162:     PetscBLASIntCast(r-1,&m);
    163:     PetscBLASIntCast(r,&n);
    164:     PetscStackCallBLAS("LAPACKgesv",LAPACKgesv_(&m,&one,ImV,&n,ipiv,scheme->alpha+1,&n,&info));
    165:     if (info < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GESV");
    166:     if (info > 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Bad LU factorization");

    168:     /* Build right hand side for beta (tp1 - glm.B(2:end,:)*(glm.c.^(p+1)./factorial(p+1)) - e.alpha) */
    169:     for (i=1; i<r; i++) {
    170:       scheme->beta[i] = 1./Factorial(p+2-i) - scheme->alpha[i];
    171:       for (j=0; j<s; j++) scheme->beta[i] -= b[i*s+j]*CPowF(c[j],p+1);
    172:     }
    173:     PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("No transpose",&m,&one,ImV,&n,ipiv,scheme->beta+1,&n,&info));
    174:     if (info < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GETRS");
    175:     if (info > 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Should not happen");

    177:     /* Build stage_error vector
    178:            xi = glm.c.^(p+1)/factorial(p+1) - glm.A*glm.c.^p/factorial(p) + glm.U(:,2:end)*e.alpha;
    179:     */
    180:     for (i=0; i<s; i++) {
    181:       scheme->stage_error[i] = CPowF(c[i],p+1);
    182:       for (j=0; j<s; j++) scheme->stage_error[i] -= a[i*s+j]*CPowF(c[j],p);
    183:       for (j=1; j<r; j++) scheme->stage_error[i] += u[i*r+j]*scheme->alpha[j];
    184:     }

    186:     /* alpha[0] (epsilon in B,J,W 2007)
    187:            epsilon = 1/factorial(p+1) - B(1,:)*c.^p/factorial(p) + V(1,2:end)*e.alpha;
    188:     */
    189:     scheme->alpha[0] = 1./Factorial(p+1);
    190:     for (j=0; j<s; j++) scheme->alpha[0] -= b[0*s+j]*CPowF(c[j],p);
    191:     for (j=1; j<r; j++) scheme->alpha[0] += v[0*r+j]*scheme->alpha[j];

    193:     /* right hand side for gamma (glm.B(2:end,:)*e.xi - e.epsilon*eye(s-1,1)) */
    194:     for (i=1; i<r; i++) {
    195:       scheme->gamma[i] = (i==1 ? -1. : 0)*scheme->alpha[0];
    196:       for (j=0; j<s; j++) scheme->gamma[i] += b[i*s+j]*scheme->stage_error[j];
    197:     }
    198:     PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("No transpose",&m,&one,ImV,&n,ipiv,scheme->gamma+1,&n,&info));
    199:     if (info < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GETRS");
    200:     if (info > 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Should not happen");

    202:     /* beta[0] (rho in B,J,W 2007)
    203:         e.rho = 1/factorial(p+2) - glm.B(1,:)*glm.c.^(p+1)/factorial(p+1) ...
    204:             + glm.V(1,2:end)*e.beta;% - e.epsilon;
    205:     % Note: The paper (B,J,W 2007) includes the last term in their definition
    206:     * */
    207:     scheme->beta[0] = 1./Factorial(p+2);
    208:     for (j=0; j<s; j++) scheme->beta[0] -= b[0*s+j]*CPowF(c[j],p+1);
    209:     for (j=1; j<r; j++) scheme->beta[0] += v[0*r+j]*scheme->beta[j];

    211:     /* gamma[0] (sigma in B,J,W 2007)
    212:     *   e.sigma = glm.B(1,:)*e.xi + glm.V(1,2:end)*e.gamma;
    213:     * */
    214:     scheme->gamma[0] = 0.0;
    215:     for (j=0; j<s; j++) scheme->gamma[0] += b[0*s+j]*scheme->stage_error[j];
    216:     for (j=1; j<r; j++) scheme->gamma[0] += v[0*s+j]*scheme->gamma[j];

    218:     /* Assemble H
    219:     *    % Determine the error estimators phi
    220:        H = [[cpow(glm.c,p) + C*e.alpha] [cpow(glm.c,p+1) + C*e.beta] ...
    221:                [e.xi - C*(e.gamma + 0*e.epsilon*eye(s-1,1))]]';
    222:     % Paper has formula above without the 0, but that term must be left
    223:     % out to satisfy the conditions they propose and to make the
    224:     % example schemes work
    225:     e.H = H;
    226:     e.phi = (H \ [1 0 0;1 1 0;0 0 -1])';
    227:     e.psi = -e.phi*C;
    228:     * */
    229:     for (j=0; j<s; j++) {
    230:       H[0+j*3] = CPowF(c[j],p);
    231:       H[1+j*3] = CPowF(c[j],p+1);
    232:       H[2+j*3] = scheme->stage_error[j];
    233:       for (k=1; k<r; k++) {
    234:         H[0+j*3] += CPowF(c[j],k-1)*scheme->alpha[k];
    235:         H[1+j*3] += CPowF(c[j],k-1)*scheme->beta[k];
    236:         H[2+j*3] -= CPowF(c[j],k-1)*scheme->gamma[k];
    237:       }
    238:     }
    239:     bmat[0+0*ss] = 1.;  bmat[0+1*ss] = 0.;  bmat[0+2*ss] = 0.;
    240:     bmat[1+0*ss] = 1.;  bmat[1+1*ss] = 1.;  bmat[1+2*ss] = 0.;
    241:     bmat[2+0*ss] = 0.;  bmat[2+1*ss] = 0.;  bmat[2+2*ss] = -1.;
    242:     m     = 3;
    243:     PetscBLASIntCast(s,&n);
    244:     PetscBLASIntCast(ss,&ldb);
    245:     rcond = 1e-12;
    246: #if defined(PETSC_USE_COMPLEX)
    247:     /* ZGELSS( M, N, NRHS, A, LDA, B, LDB, S, RCOND, RANK, WORK, LWORK, RWORK, INFO) */
    248:     PetscStackCallBLAS("LAPACKgelss",LAPACKgelss_(&m,&n,&m,H,&m,bmat,&ldb,sing,&rcond,&rank,workscalar,&lwork,workreal,&info));
    249: #else
    250:     /* DGELSS( M, N, NRHS, A, LDA, B, LDB, S, RCOND, RANK, WORK, LWORK, INFO) */
    251:     PetscStackCallBLAS("LAPACKgelss",LAPACKgelss_(&m,&n,&m,H,&m,bmat,&ldb,sing,&rcond,&rank,workscalar,&lwork,&info));
    252: #endif
    253:     if (info < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Bad argument to GELSS");
    254:     if (info > 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"SVD failed to converge");

    256:     for (j=0; j<3; j++) {
    257:       for (k=0; k<s; k++) scheme->phi[k+j*s] = bmat[k+j*ss];
    258:     }

    260:     /* the other part of the error estimator, psi in B,J,W 2007 */
    261:     scheme->psi[0*r+0] = 0.;
    262:     scheme->psi[1*r+0] = 0.;
    263:     scheme->psi[2*r+0] = 0.;
    264:     for (j=1; j<r; j++) {
    265:       scheme->psi[0*r+j] = 0.;
    266:       scheme->psi[1*r+j] = 0.;
    267:       scheme->psi[2*r+j] = 0.;
    268:       for (k=0; k<s; k++) {
    269:         scheme->psi[0*r+j] -= CPowF(c[k],j-1)*scheme->phi[0*s+k];
    270:         scheme->psi[1*r+j] -= CPowF(c[k],j-1)*scheme->phi[1*s+k];
    271:         scheme->psi[2*r+j] -= CPowF(c[k],j-1)*scheme->phi[2*s+k];
    272:       }
    273:     }
    274:     PetscFree7(ImV,H,bmat,workscalar,workreal,sing,ipiv);
    275:   }
    276:   /* Check which properties are satisfied */
    277:   scheme->stiffly_accurate = PETSC_TRUE;
    278:   if (scheme->c[s-1] != 1.) scheme->stiffly_accurate = PETSC_FALSE;
    279:   for (j=0; j<s; j++) if (a[(s-1)*s+j] != b[j]) scheme->stiffly_accurate = PETSC_FALSE;
    280:   for (j=0; j<r; j++) if (u[(s-1)*r+j] != v[j]) scheme->stiffly_accurate = PETSC_FALSE;
    281:   scheme->fsal = scheme->stiffly_accurate; /* FSAL is stronger */
    282:   for (j=0; j<s-1; j++) if (r>1 && b[1*s+j] != 0.) scheme->fsal = PETSC_FALSE;
    283:   if (b[1*s+r-1] != 1.) scheme->fsal = PETSC_FALSE;
    284:   for (j=0; j<r; j++) if (r>1 && v[1*r+j] != 0.) scheme->fsal = PETSC_FALSE;

    286:   *inscheme = scheme;
    287:   return(0);
    288: }

    290: static PetscErrorCode TSGLLESchemeDestroy(TSGLLEScheme sc)
    291: {

    295:   PetscFree5(sc->c,sc->a,sc->b,sc->u,sc->v);
    296:   PetscFree6(sc->alpha,sc->beta,sc->gamma,sc->phi,sc->psi,sc->stage_error);
    297:   PetscFree(sc);
    298:   return(0);
    299: }

    301: static PetscErrorCode TSGLLEDestroy_Default(TS_GLLE *gl)
    302: {
    304:   PetscInt       i;

    307:   for (i=0; i<gl->nschemes; i++) {
    308:     if (gl->schemes[i]) {TSGLLESchemeDestroy(gl->schemes[i]);}
    309:   }
    310:   PetscFree(gl->schemes);
    311:   gl->nschemes = 0;
    312:   PetscMemzero(gl->type_name,sizeof(gl->type_name));
    313:   return(0);
    314: }

    316: static PetscErrorCode TSGLLEViewTable_Private(PetscViewer viewer,PetscInt m,PetscInt n,const PetscScalar a[],const char name[])
    317: {
    319:   PetscBool      iascii;
    320:   PetscInt       i,j;

    323:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    324:   if (iascii) {
    325:     PetscViewerASCIIPrintf(viewer,"%30s = [",name);
    326:     for (i=0; i<m; i++) {
    327:       if (i) {PetscViewerASCIIPrintf(viewer,"%30s   [","");}
    328:       PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
    329:       for (j=0; j<n; j++) {
    330:         PetscViewerASCIIPrintf(viewer," %12.8g",PetscRealPart(a[i*n+j]));
    331:       }
    332:       PetscViewerASCIIPrintf(viewer,"]\n");
    333:       PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
    334:     }
    335:   }
    336:   return(0);
    337: }

    340: static PetscErrorCode TSGLLESchemeView(TSGLLEScheme sc,PetscBool view_details,PetscViewer viewer)
    341: {
    343:   PetscBool      iascii;

    346:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    347:   if (iascii) {
    348:     PetscViewerASCIIPrintf(viewer,"GL scheme p,q,r,s = %d,%d,%d,%d\n",sc->p,sc->q,sc->r,sc->s);
    349:     PetscViewerASCIIPushTab(viewer);
    350:     PetscViewerASCIIPrintf(viewer,"Stiffly accurate: %s,  FSAL: %s\n",sc->stiffly_accurate ? "yes" : "no",sc->fsal ? "yes" : "no");
    351:     PetscViewerASCIIPrintf(viewer,"Leading error constants: %10.3e  %10.3e  %10.3e\n",
    352:                                   PetscRealPart(sc->alpha[0]),PetscRealPart(sc->beta[0]),PetscRealPart(sc->gamma[0]));
    353:     TSGLLEViewTable_Private(viewer,1,sc->s,sc->c,"Abscissas c");
    354:     if (view_details) {
    355:       TSGLLEViewTable_Private(viewer,sc->s,sc->s,sc->a,"A");
    356:       TSGLLEViewTable_Private(viewer,sc->r,sc->s,sc->b,"B");
    357:       TSGLLEViewTable_Private(viewer,sc->s,sc->r,sc->u,"U");
    358:       TSGLLEViewTable_Private(viewer,sc->r,sc->r,sc->v,"V");

    360:       TSGLLEViewTable_Private(viewer,3,sc->s,sc->phi,"Error estimate phi");
    361:       TSGLLEViewTable_Private(viewer,3,sc->r,sc->psi,"Error estimate psi");
    362:       TSGLLEViewTable_Private(viewer,1,sc->r,sc->alpha,"Modify alpha");
    363:       TSGLLEViewTable_Private(viewer,1,sc->r,sc->beta,"Modify beta");
    364:       TSGLLEViewTable_Private(viewer,1,sc->r,sc->gamma,"Modify gamma");
    365:       TSGLLEViewTable_Private(viewer,1,sc->s,sc->stage_error,"Stage error xi");
    366:     }
    367:     PetscViewerASCIIPopTab(viewer);
    368:   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported",((PetscObject)viewer)->type_name);
    369:   return(0);
    370: }

    372: static PetscErrorCode TSGLLEEstimateHigherMoments_Default(TSGLLEScheme sc,PetscReal h,Vec Ydot[],Vec Xold[],Vec hm[])
    373: {
    375:   PetscInt       i;

    378:   if (sc->r > 64 || sc->s > 64) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Ridiculous number of stages or items passed between stages");
    379:   /* build error vectors*/
    380:   for (i=0; i<3; i++) {
    381:     PetscScalar phih[64];
    382:     PetscInt    j;
    383:     for (j=0; j<sc->s; j++) phih[j] = sc->phi[i*sc->s+j]*h;
    384:     VecZeroEntries(hm[i]);
    385:     VecMAXPY(hm[i],sc->s,phih,Ydot);
    386:     VecMAXPY(hm[i],sc->r,&sc->psi[i*sc->r],Xold);
    387:   }
    388:   return(0);
    389: }

    391: static PetscErrorCode TSGLLECompleteStep_Rescale(TSGLLEScheme sc,PetscReal h,TSGLLEScheme next_sc,PetscReal next_h,Vec Ydot[],Vec Xold[],Vec X[])
    392: {
    394:   PetscScalar    brow[32],vrow[32];
    395:   PetscInt       i,j,r,s;

    398:   /* Build the new solution from (X,Ydot) */
    399:   r = sc->r;
    400:   s = sc->s;
    401:   for (i=0; i<r; i++) {
    402:     VecZeroEntries(X[i]);
    403:     for (j=0; j<s; j++) brow[j] = h*sc->b[i*s+j];
    404:     VecMAXPY(X[i],s,brow,Ydot);
    405:     for (j=0; j<r; j++) vrow[j] = sc->v[i*r+j];
    406:     VecMAXPY(X[i],r,vrow,Xold);
    407:   }
    408:   return(0);
    409: }

    411: static PetscErrorCode TSGLLECompleteStep_RescaleAndModify(TSGLLEScheme sc,PetscReal h,TSGLLEScheme next_sc,PetscReal next_h,Vec Ydot[],Vec Xold[],Vec X[])
    412: {
    414:   PetscScalar    brow[32],vrow[32];
    415:   PetscReal      ratio;
    416:   PetscInt       i,j,p,r,s;

    419:   /* Build the new solution from (X,Ydot) */
    420:   p     = sc->p;
    421:   r     = sc->r;
    422:   s     = sc->s;
    423:   ratio = next_h/h;
    424:   for (i=0; i<r; i++) {
    425:     VecZeroEntries(X[i]);
    426:     for (j=0; j<s; j++) {
    427:       brow[j] = h*(PetscPowRealInt(ratio,i)*sc->b[i*s+j]
    428:                    + (PetscPowRealInt(ratio,i) - PetscPowRealInt(ratio,p+1))*(+ sc->alpha[i]*sc->phi[0*s+j])
    429:                    + (PetscPowRealInt(ratio,i) - PetscPowRealInt(ratio,p+2))*(+ sc->beta [i]*sc->phi[1*s+j]
    430:                                                                               + sc->gamma[i]*sc->phi[2*s+j]));
    431:     }
    432:     VecMAXPY(X[i],s,brow,Ydot);
    433:     for (j=0; j<r; j++) {
    434:       vrow[j] = (PetscPowRealInt(ratio,i)*sc->v[i*r+j]
    435:                  + (PetscPowRealInt(ratio,i) - PetscPowRealInt(ratio,p+1))*(+ sc->alpha[i]*sc->psi[0*r+j])
    436:                  + (PetscPowRealInt(ratio,i) - PetscPowRealInt(ratio,p+2))*(+ sc->beta [i]*sc->psi[1*r+j]
    437:                                                                             + sc->gamma[i]*sc->psi[2*r+j]));
    438:     }
    439:     VecMAXPY(X[i],r,vrow,Xold);
    440:   }
    441:   if (r < next_sc->r) {
    442:     if (r+1 != next_sc->r) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cannot accommodate jump in r greater than 1");
    443:     VecZeroEntries(X[r]);
    444:     for (j=0; j<s; j++) brow[j] = h*PetscPowRealInt(ratio,p+1)*sc->phi[0*s+j];
    445:     VecMAXPY(X[r],s,brow,Ydot);
    446:     for (j=0; j<r; j++) vrow[j] = PetscPowRealInt(ratio,p+1)*sc->psi[0*r+j];
    447:     VecMAXPY(X[r],r,vrow,Xold);
    448:   }
    449:   return(0);
    450: }

    452: static PetscErrorCode TSGLLECreate_IRKS(TS ts)
    453: {
    454:   TS_GLLE        *gl = (TS_GLLE*)ts->data;

    458:   gl->Destroy               = TSGLLEDestroy_Default;
    459:   gl->EstimateHigherMoments = TSGLLEEstimateHigherMoments_Default;
    460:   gl->CompleteStep          = TSGLLECompleteStep_RescaleAndModify;
    461:   PetscMalloc1(10,&gl->schemes);
    462:   gl->nschemes = 0;

    464:   {
    465:     /* p=1,q=1, r=s=2, A- and L-stable with error estimates of order 2 and 3
    466:     * Listed in Butcher & Podhaisky 2006. On error estimation in general linear methods for stiff ODE.
    467:     * irks(0.3,0,[.3,1],[1],1)
    468:     * Note: can be made to have classical order (not stage order) 2 by replacing 0.3 with 1-sqrt(1/2)
    469:     * but doing so would sacrifice the error estimator.
    470:     */
    471:     const PetscScalar c[2]    = {3./10., 1.};
    472:     const PetscScalar a[2][2] = {{3./10., 0}, {7./10., 3./10.}};
    473:     const PetscScalar b[2][2] = {{7./10., 3./10.}, {0,1}};
    474:     const PetscScalar u[2][2] = {{1,0},{1,0}};
    475:     const PetscScalar v[2][2] = {{1,0},{0,0}};
    476:     TSGLLESchemeCreate(1,1,2,2,c,*a,*b,*u,*v,&gl->schemes[gl->nschemes++]);
    477:   }

    479:   {
    480:     /* p=q=2, r=s=3: irks(4/9,0,[1:3]/3,[0.33852],1) */
    481:     /* http://www.math.auckland.ac.nz/~hpod/atlas/i2a.html */
    482:     const PetscScalar c[3] = {1./3., 2./3., 1}
    483:     ,a[3][3] = {{4./9.                ,0                      ,       0},
    484:                 {1.03750643704090e+00 ,                  4./9.,       0},
    485:                 {7.67024779410304e-01 ,  -3.81140216918943e-01,   4./9.}}
    486:     ,b[3][3] = {{0.767024779410304,  -0.381140216918943,   4./9.},
    487:                 {0.000000000000000,  0.000000000000000,   1.000000000000000},
    488:                 {-2.075048385225385,   0.621728385225383,   1.277197204924873}}
    489:     ,u[3][3] = {{1.0000000000000000,  -0.1111111111111109,  -0.0925925925925922},
    490:                 {1.0000000000000000,  -0.8152842148186744,  -0.4199095530877056},
    491:                 {1.0000000000000000,   0.1696709930641948,   0.0539741070314165}}
    492:     ,v[3][3] = {{1.0000000000000000,  0.1696709930641948,   0.0539741070314165},
    493:                 {0.000000000000000,   0.000000000000000,   0.000000000000000},
    494:                 {0.000000000000000,   0.176122795075129,   0.000000000000000}};
    495:     TSGLLESchemeCreate(2,2,3,3,c,*a,*b,*u,*v,&gl->schemes[gl->nschemes++]);
    496:   }
    497:   {
    498:     /* p=q=3, r=s=4: irks(9/40,0,[1:4]/4,[0.3312 1.0050],[0.49541 1;1 0]) */
    499:     const PetscScalar c[4] = {0.25,0.5,0.75,1.0}
    500:     ,a[4][4] = {{9./40.               ,                      0,                      0,                      0},
    501:                 {2.11286958887701e-01 ,    9./40.             ,                      0,                      0},
    502:                 {9.46338294287584e-01 ,  -3.42942861246094e-01,   9./40.              ,                      0},
    503:                 {0.521490453970721    ,  -0.662474225622980,   0.490476425459734,   9./40.           }}
    504:     ,b[4][4] = {{0.521490453970721    ,  -0.662474225622980,   0.490476425459734,   9./40.           },
    505:                 {0.000000000000000    ,   0.000000000000000,   0.000000000000000,   1.000000000000000},
    506:                 {-0.084677029310348   ,   1.390757514776085,  -1.568157386206001,   2.023192696767826},
    507:                 {0.465383797936408    ,   1.478273530625148,  -1.930836081010182,   1.644872111193354}}
    508:     ,u[4][4] = {{1.00000000000000000  ,   0.02500000000001035,  -0.02499999999999053,  -0.00442708333332865},
    509:                 {1.00000000000000000  ,   0.06371304111232945,  -0.04032173972189845,  -0.01389438413189452},
    510:                 {1.00000000000000000  ,  -0.07839543304147778,   0.04738685705116663,   0.02032603595928376},
    511:                 {1.00000000000000000  ,   0.42550734619251651,   0.10800718022400080,  -0.01726712647760034}}
    512:     ,v[4][4] = {{1.00000000000000000  ,   0.42550734619251651,   0.10800718022400080,  -0.01726712647760034},
    513:                 {0.000000000000000    ,   0.000000000000000,   0.000000000000000,   0.000000000000000},
    514:                 {0.000000000000000    ,  -1.761115796027561,  -0.521284157173780,   0.258249384305463},
    515:                 {0.000000000000000    ,  -1.657693358744728,  -1.052227765232394,   0.521284157173780}};
    516:     TSGLLESchemeCreate(3,3,4,4,c,*a,*b,*u,*v,&gl->schemes[gl->nschemes++]);
    517:   }
    518:   {
    519:     /* p=q=4, r=s=5:
    520:           irks(3/11,0,[1:5]/5, [0.1715   -0.1238    0.6617],...
    521:           [ -0.0812    0.4079    1.0000
    522:              1.0000         0         0
    523:              0.8270    1.0000         0])
    524:     */
    525:     const PetscScalar c[5] = {0.2,0.4,0.6,0.8,1.0}
    526:     ,a[5][5] = {{2.72727272727352e-01 ,   0.00000000000000e+00,  0.00000000000000e+00 ,  0.00000000000000e+00  ,  0.00000000000000e+00},
    527:                 {-1.03980153733431e-01,   2.72727272727405e-01,   0.00000000000000e+00,  0.00000000000000e+00  ,  0.00000000000000e+00},
    528:                 {-1.58615400341492e+00,   7.44168951881122e-01,   2.72727272727309e-01,  0.00000000000000e+00  ,  0.00000000000000e+00},
    529:                 {-8.73658042865628e-01,   5.37884671894595e-01,  -1.63298538799523e-01,   2.72727272726996e-01 ,  0.00000000000000e+00},
    530:                 {2.95489397443992e-01 , -1.18481693910097e+00 , -6.68029812659953e-01 ,  1.00716687860943e+00  , 2.72727272727288e-01}}
    531:     ,b[5][5] = {{2.95489397443992e-01 , -1.18481693910097e+00 , -6.68029812659953e-01 ,  1.00716687860943e+00  , 2.72727272727288e-01},
    532:                 {0.00000000000000e+00 ,  1.11022302462516e-16 , -2.22044604925031e-16 ,  0.00000000000000e+00  , 1.00000000000000e+00},
    533:                 {-4.05882503986005e+00,  -4.00924006567769e+00,  -1.38930610972481e+00,   4.45223930308488e+00 ,  6.32331093108427e-01},
    534:                 {8.35690179937017e+00 , -2.26640927349732e+00 ,  6.86647884973826e+00 , -5.22595158025740e+00  , 4.50893068837431e+00},
    535:                 {1.27656267027479e+01 ,  2.80882153840821e+00 ,  8.91173096522890e+00 , -1.07936444078906e+01  , 4.82534148988854e+00}}
    536:     ,u[5][5] = {{1.00000000000000e+00 , -7.27272727273551e-02 , -3.45454545454419e-02 , -4.12121212119565e-03  ,-2.96969696964014e-04},
    537:                 {1.00000000000000e+00 ,  2.31252881006154e-01 , -8.29487834416481e-03 , -9.07191207681020e-03  ,-1.70378403743473e-03},
    538:                 {1.00000000000000e+00 ,  1.16925777880663e+00 ,  3.59268562942635e-02 , -4.09013451730615e-02  ,-1.02411119670164e-02},
    539:                 {1.00000000000000e+00 ,  1.02634463704356e+00 ,  1.59375044913405e-01 ,  1.89673015035370e-03  ,-4.89987231897569e-03},
    540:                 {1.00000000000000e+00 ,  1.27746320298021e+00 ,  2.37186008132728e-01 , -8.28694373940065e-02  ,-5.34396510196430e-02}}
    541:     ,v[5][5] = {{1.00000000000000e+00 ,  1.27746320298021e+00 ,  2.37186008132728e-01 , -8.28694373940065e-02  ,-5.34396510196430e-02},
    542:                 {0.00000000000000e+00 , -1.77635683940025e-15 , -1.99840144432528e-15 , -9.99200722162641e-16  ,-3.33066907387547e-16},
    543:                 {0.00000000000000e+00 ,  4.37280081906924e+00 ,  5.49221645016377e-02 , -8.88913177394943e-02  , 1.12879077989154e-01},
    544:                 {0.00000000000000e+00 , -1.22399504837280e+01 , -5.21287338448645e+00 , -8.03952325565291e-01  , 4.60298678047147e-01},
    545:                 {0.00000000000000e+00 , -1.85178762883829e+01 , -5.21411849862624e+00 , -1.04283436528809e+00  , 7.49030161063651e-01}};
    546:     TSGLLESchemeCreate(4,4,5,5,c,*a,*b,*u,*v,&gl->schemes[gl->nschemes++]);
    547:   }
    548:   {
    549:     /* p=q=5, r=s=6;
    550:        irks(1/3,0,[1:6]/6,...
    551:           [-0.0489    0.4228   -0.8814    0.9021],...
    552:           [-0.3474   -0.6617    0.6294    0.2129
    553:             0.0044   -0.4256   -0.1427   -0.8936
    554:            -0.8267    0.4821    0.1371   -0.2557
    555:            -0.4426   -0.3855   -0.7514    0.3014])
    556:     */
    557:     const PetscScalar c[6] = {1./6, 2./6, 3./6, 4./6, 5./6, 1.}
    558:     ,a[6][6] = {{  3.33333333333940e-01,  0                   ,  0                   ,  0                   ,  0                   ,  0                   },
    559:                 { -8.64423857333350e-02,  3.33333333332888e-01,  0                   ,  0                   ,  0                   ,  0                   },
    560:                 { -2.16850174258252e+00, -2.23619072028839e+00,  3.33333333335204e-01,  0                   ,  0                   ,  0                   },
    561:                 { -4.73160970138997e+00, -3.89265344629268e+00, -2.76318716520933e-01,  3.33333333335759e-01,  0                   ,  0                   },
    562:                 { -6.75187540297338e+00, -7.90756533769377e+00,  7.90245051802259e-01, -4.48352364517632e-01,  3.33333333328483e-01,  0                   },
    563:                 { -4.26488287921548e+00, -1.19320395589302e+01,  3.38924509887755e+00, -2.23969848002481e+00,  6.62807710124007e-01,  3.33333333335440e-01}}
    564:     ,b[6][6] = {{ -4.26488287921548e+00, -1.19320395589302e+01,  3.38924509887755e+00, -2.23969848002481e+00,  6.62807710124007e-01,  3.33333333335440e-01},
    565:                 { -8.88178419700125e-16,  4.44089209850063e-16, -1.54737334057131e-15, -8.88178419700125e-16,  0.00000000000000e+00,  1.00000000000001e+00},
    566:                 { -2.87780425770651e+01, -1.13520448264971e+01,  2.62002318943161e+01,  2.56943874812797e+01, -3.06702268304488e+01,  6.68067773510103e+00},
    567:                 {  5.47971245256474e+01,  6.80366875868284e+01, -6.50952588861999e+01, -8.28643975339097e+01,  8.17416943896414e+01, -1.17819043489036e+01},
    568:                 { -2.33332114788869e+02,  6.12942539462634e+01, -4.91850135865944e+01,  1.82716844135480e+02, -1.29788173979395e+02,  3.09968095651099e+01},
    569:                 { -1.72049132343751e+02,  8.60194713593999e+00,  7.98154219170200e-01,  1.50371386053218e+02, -1.18515423962066e+02,  2.50898277784663e+01}}
    570:     ,u[6][6] = {{  1.00000000000000e+00, -1.66666666666870e-01, -4.16666666664335e-02, -3.85802469124815e-03, -2.25051440302250e-04, -9.64506172339142e-06},
    571:                 {  1.00000000000000e+00,  8.64423857327162e-02, -4.11484912671353e-02, -1.11450903217645e-02, -1.47651050487126e-03, -1.34395070766826e-04},
    572:                 {  1.00000000000000e+00,  4.57135912953434e+00,  1.06514719719137e+00,  1.33517564218007e-01,  1.11365952968659e-02,  6.12382756769504e-04},
    573:                 {  1.00000000000000e+00,  9.23391519753404e+00,  2.22431212392095e+00,  2.91823807741891e-01,  2.52058456411084e-02,  1.22800542949647e-03},
    574:                 {  1.00000000000000e+00,  1.48175480533865e+01,  3.73439117461835e+00,  5.14648336541804e-01,  4.76430038853402e-02,  2.56798515502156e-03},
    575:                 {  1.00000000000000e+00,  1.50512347758335e+01,  4.10099701165164e+00,  5.66039141003603e-01,  3.91213893800891e-02, -2.99136269067853e-03}}
    576:     ,v[6][6] = {{  1.00000000000000e+00,  1.50512347758335e+01,  4.10099701165164e+00,  5.66039141003603e-01,  3.91213893800891e-02, -2.99136269067853e-03},
    577:                 {  0.00000000000000e+00, -4.88498130835069e-15, -6.43929354282591e-15, -3.55271367880050e-15, -1.22124532708767e-15, -3.12250225675825e-16},
    578:                 {  0.00000000000000e+00,  1.22250171233141e+01, -1.77150760606169e+00,  3.54516769879390e-01,  6.22298845883398e-01,  2.31647447450276e-01},
    579:                 {  0.00000000000000e+00, -4.48339457331040e+01, -3.57363126641880e-01,  5.18750173123425e-01,  6.55727990241799e-02,  1.63175368287079e-01},
    580:                 {  0.00000000000000e+00,  1.37297394708005e+02, -1.60145272991317e+00, -5.05319555199441e+00,  1.55328940390990e-01,  9.16629423682464e-01},
    581:                 {  0.00000000000000e+00,  1.05703241119022e+02, -1.16610260983038e+00, -2.99767252773859e+00, -1.13472315553890e-01,  1.09742849254729e+00}};
    582:     TSGLLESchemeCreate(5,5,6,6,c,*a,*b,*u,*v,&gl->schemes[gl->nschemes++]);
    583:   }
    584:   return(0);
    585: }

    587: /*@C
    588:    TSGLLESetType - sets the class of general linear method to use for time-stepping

    590:    Collective on TS

    592:    Input Parameters:
    593: +  ts - the TS context
    594: -  type - a method

    596:    Options Database Key:
    597: .  -ts_gl_type <type> - sets the method, use -help for a list of available method (e.g. irks)

    599:    Notes:
    600:    See "petsc/include/petscts.h" for available methods (for instance)
    601: .    TSGLLE_IRKS - Diagonally implicit methods with inherent Runge-Kutta stability (for stiff problems)

    603:    Normally, it is best to use the TSSetFromOptions() command and
    604:    then set the TSGLLE type from the options database rather than by using
    605:    this routine.  Using the options database provides the user with
    606:    maximum flexibility in evaluating the many different solvers.
    607:    The TSGLLESetType() routine is provided for those situations where it
    608:    is necessary to set the timestepping solver independently of the
    609:    command line or options database.  This might be the case, for example,
    610:    when the choice of solver changes during the execution of the
    611:    program, and the user's application is taking responsibility for
    612:    choosing the appropriate method.

    614:    Level: intermediate

    616: @*/
    617: PetscErrorCode  TSGLLESetType(TS ts,TSGLLEType type)
    618: {

    624:   PetscTryMethod(ts,"TSGLLESetType_C",(TS,TSGLLEType),(ts,type));
    625:   return(0);
    626: }

    628: /*@C
    629:    TSGLLESetAcceptType - sets the acceptance test

    631:    Time integrators that need to control error must have the option to reject a time step based on local error
    632:    estimates.  This function allows different schemes to be set.

    634:    Logically Collective on TS

    636:    Input Parameters:
    637: +  ts - the TS context
    638: -  type - the type

    640:    Options Database Key:
    641: .  -ts_gl_accept_type <type> - sets the method used to determine whether to accept or reject a step

    643:    Level: intermediate

    645: .seealso: TS, TSGLLE, TSGLLEAcceptRegister(), TSGLLEAdapt, set type
    646: @*/
    647: PetscErrorCode  TSGLLESetAcceptType(TS ts,TSGLLEAcceptType type)
    648: {

    654:   PetscTryMethod(ts,"TSGLLESetAcceptType_C",(TS,TSGLLEAcceptType),(ts,type));
    655:   return(0);
    656: }

    658: /*@C
    659:    TSGLLEGetAdapt - gets the TSGLLEAdapt object from the TS

    661:    Not Collective

    663:    Input Parameter:
    664: .  ts - the TS context

    666:    Output Parameter:
    667: .  adapt - the TSGLLEAdapt context

    669:    Notes:
    670:    This allows the user set options on the TSGLLEAdapt object.  Usually it is better to do this using the options
    671:    database, so this function is rarely needed.

    673:    Level: advanced

    675: .seealso: TSGLLEAdapt, TSGLLEAdaptRegister()
    676: @*/
    677: PetscErrorCode  TSGLLEGetAdapt(TS ts,TSGLLEAdapt *adapt)
    678: {

    684:   PetscUseMethod(ts,"TSGLLEGetAdapt_C",(TS,TSGLLEAdapt*),(ts,adapt));
    685:   return(0);
    686: }

    688: static PetscErrorCode TSGLLEAccept_Always(TS ts,PetscReal tleft,PetscReal h,const PetscReal enorms[],PetscBool  *accept)
    689: {
    691:   *accept = PETSC_TRUE;
    692:   return(0);
    693: }

    695: static PetscErrorCode TSGLLEUpdateWRMS(TS ts)
    696: {
    697:   TS_GLLE        *gl = (TS_GLLE*)ts->data;
    699:   PetscScalar    *x,*w;
    700:   PetscInt       n,i;

    703:   VecGetArray(gl->X[0],&x);
    704:   VecGetArray(gl->W,&w);
    705:   VecGetLocalSize(gl->W,&n);
    706:   for (i=0; i<n; i++) w[i] = 1./(gl->wrms_atol + gl->wrms_rtol*PetscAbsScalar(x[i]));
    707:   VecRestoreArray(gl->X[0],&x);
    708:   VecRestoreArray(gl->W,&w);
    709:   return(0);
    710: }

    712: static PetscErrorCode TSGLLEVecNormWRMS(TS ts,Vec X,PetscReal *nrm)
    713: {
    714:   TS_GLLE        *gl = (TS_GLLE*)ts->data;
    716:   PetscScalar    *x,*w;
    717:   PetscReal      sum = 0.0,gsum;
    718:   PetscInt       n,N,i;

    721:   VecGetArray(X,&x);
    722:   VecGetArray(gl->W,&w);
    723:   VecGetLocalSize(gl->W,&n);
    724:   for (i=0; i<n; i++) sum += PetscAbsScalar(PetscSqr(x[i]*w[i]));
    725:   VecRestoreArray(X,&x);
    726:   VecRestoreArray(gl->W,&w);
    727:   MPIU_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));
    728:   VecGetSize(gl->W,&N);
    729:   *nrm = PetscSqrtReal(gsum/(1.*N));
    730:   return(0);
    731: }

    733: static PetscErrorCode TSGLLESetType_GLLE(TS ts,TSGLLEType type)
    734: {
    735:   PetscErrorCode ierr,(*r)(TS);
    736:   PetscBool      same;
    737:   TS_GLLE        *gl = (TS_GLLE*)ts->data;

    740:   if (gl->type_name[0]) {
    741:     PetscStrcmp(gl->type_name,type,&same);
    742:     if (same) return(0);
    743:     (*gl->Destroy)(gl);
    744:   }

    746:   PetscFunctionListFind(TSGLLEList,type,&r);
    747:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown TSGLLE type \"%s\" given",type);
    748:   (*r)(ts);
    749:   PetscStrcpy(gl->type_name,type);
    750:   return(0);
    751: }

    753: static PetscErrorCode TSGLLESetAcceptType_GLLE(TS ts,TSGLLEAcceptType type)
    754: {
    755:   PetscErrorCode       ierr;
    756:   TSGLLEAcceptFunction r;
    757:   TS_GLLE              *gl = (TS_GLLE*)ts->data;

    760:   PetscFunctionListFind(TSGLLEAcceptList,type,&r);
    761:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown TSGLLEAccept type \"%s\" given",type);
    762:   gl->Accept = r;
    763:   PetscStrncpy(gl->accept_name,type,sizeof(gl->accept_name));
    764:   return(0);
    765: }

    767: static PetscErrorCode TSGLLEGetAdapt_GLLE(TS ts,TSGLLEAdapt *adapt)
    768: {
    770:   TS_GLLE        *gl = (TS_GLLE*)ts->data;

    773:   if (!gl->adapt) {
    774:     TSGLLEAdaptCreate(PetscObjectComm((PetscObject)ts),&gl->adapt);
    775:     PetscObjectIncrementTabLevel((PetscObject)gl->adapt,(PetscObject)ts,1);
    776:     PetscLogObjectParent((PetscObject)ts,(PetscObject)gl->adapt);
    777:   }
    778:   *adapt = gl->adapt;
    779:   return(0);
    780: }

    782: static PetscErrorCode TSGLLEChooseNextScheme(TS ts,PetscReal h,const PetscReal hmnorm[],PetscInt *next_scheme,PetscReal *next_h,PetscBool  *finish)
    783: {
    785:   TS_GLLE        *gl = (TS_GLLE*)ts->data;
    786:   PetscInt       i,n,cur_p,cur,next_sc,candidates[64],orders[64];
    787:   PetscReal      errors[64],costs[64],tleft;

    790:   cur   = -1;
    791:   cur_p = gl->schemes[gl->current_scheme]->p;
    792:   tleft = ts->max_time - (ts->ptime + ts->time_step);
    793:   for (i=0,n=0; i<gl->nschemes; i++) {
    794:     TSGLLEScheme sc = gl->schemes[i];
    795:     if (sc->p < gl->min_order || gl->max_order < sc->p) continue;
    796:     if (sc->p == cur_p - 1)    errors[n] = PetscAbsScalar(sc->alpha[0])*hmnorm[0];
    797:     else if (sc->p == cur_p)   errors[n] = PetscAbsScalar(sc->alpha[0])*hmnorm[1];
    798:     else if (sc->p == cur_p+1) errors[n] = PetscAbsScalar(sc->alpha[0])*(hmnorm[2]+hmnorm[3]);
    799:     else continue;
    800:     candidates[n] = i;
    801:     orders[n]     = PetscMin(sc->p,sc->q); /* order of global truncation error */
    802:     costs[n]      = sc->s;                 /* estimate the cost as the number of stages */
    803:     if (i == gl->current_scheme) cur = n;
    804:     n++;
    805:   }
    806:   if (cur < 0 || gl->nschemes <= cur) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Current scheme not found in scheme list");
    807:   TSGLLEAdaptChoose(gl->adapt,n,orders,errors,costs,cur,h,tleft,&next_sc,next_h,finish);
    808:   *next_scheme = candidates[next_sc];
    809:   PetscInfo7(ts,"Adapt chose scheme %d (%d,%d,%d,%d) with step size %6.2e, finish=%d\n",*next_scheme,gl->schemes[*next_scheme]->p,gl->schemes[*next_scheme]->q,gl->schemes[*next_scheme]->r,gl->schemes[*next_scheme]->s,*next_h,*finish);
    810:   return(0);
    811: }

    813: static PetscErrorCode TSGLLEGetMaxSizes(TS ts,PetscInt *max_r,PetscInt *max_s)
    814: {
    815:   TS_GLLE *gl = (TS_GLLE*)ts->data;

    818:   *max_r = gl->schemes[gl->nschemes-1]->r;
    819:   *max_s = gl->schemes[gl->nschemes-1]->s;
    820:   return(0);
    821: }

    823: static PetscErrorCode TSSolve_GLLE(TS ts)
    824: {
    825:   TS_GLLE             *gl = (TS_GLLE*)ts->data;
    826:   PetscInt            i,k,its,lits,max_r,max_s;
    827:   PetscBool           final_step,finish;
    828:   SNESConvergedReason snesreason;
    829:   PetscErrorCode      ierr;

    832:   TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);

    834:   TSGLLEGetMaxSizes(ts,&max_r,&max_s);
    835:   VecCopy(ts->vec_sol,gl->X[0]);
    836:   for (i=1; i<max_r; i++) {
    837:     VecZeroEntries(gl->X[i]);
    838:   }
    839:   TSGLLEUpdateWRMS(ts);

    841:   if (0) {
    842:     /* Find consistent initial data for DAE */
    843:     gl->stage_time = ts->ptime + ts->time_step;
    844:     gl->scoeff = 1.;
    845:     gl->stage  = 0;

    847:     VecCopy(ts->vec_sol,gl->Z);
    848:     VecCopy(ts->vec_sol,gl->Y);
    849:     SNESSolve(ts->snes,NULL,gl->Y);
    850:     SNESGetIterationNumber(ts->snes,&its);
    851:     SNESGetLinearSolveIterations(ts->snes,&lits);
    852:     SNESGetConvergedReason(ts->snes,&snesreason);

    854:     ts->snes_its += its; ts->ksp_its += lits;
    855:     if (snesreason < 0 && ts->max_snes_failures > 0 && ++ts->num_snes_failures >= ts->max_snes_failures) {
    856:       ts->reason = TS_DIVERGED_NONLINEAR_SOLVE;
    857:       PetscInfo2(ts,"Step=%D, nonlinear solve solve failures %D greater than current TS allowed, stopping solve\n",ts->steps,ts->num_snes_failures);
    858:       return(0);
    859:     }
    860:   }

    862:   if (gl->current_scheme < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"A starting scheme has not been provided");

    864:   for (k=0,final_step=PETSC_FALSE,finish=PETSC_FALSE; k<ts->max_steps && !finish; k++) {
    865:     PetscInt          j,r,s,next_scheme = 0,rejections;
    866:     PetscReal         h,hmnorm[4],enorm[3],next_h;
    867:     PetscBool         accept;
    868:     const PetscScalar *c,*a,*u;
    869:     Vec               *X,*Ydot,Y;
    870:     TSGLLEScheme        scheme = gl->schemes[gl->current_scheme];

    872:     r = scheme->r; s = scheme->s;
    873:     c = scheme->c;
    874:     a = scheme->a; u = scheme->u;
    875:     h = ts->time_step;
    876:     X = gl->X; Ydot = gl->Ydot; Y = gl->Y;

    878:     if (ts->ptime > ts->max_time) break;

    880:     /*
    881:       We only call PreStep at the start of each STEP, not each STAGE.  This is because it is
    882:       possible to fail (have to restart a step) after multiple stages.
    883:     */
    884:     TSPreStep(ts);

    886:     rejections = 0;
    887:     while (1) {
    888:       for (i=0; i<s; i++) {
    889:         PetscScalar shift;
    890:         gl->scoeff     = 1./PetscRealPart(a[i*s+i]);
    891:         shift          = gl->scoeff/ts->time_step;
    892:         gl->stage      = i;
    893:         gl->stage_time = ts->ptime + PetscRealPart(c[i])*h;

    895:         /*
    896:         * Stage equation: Y = h A Y' + U X
    897:         * We assume that A is lower-triangular so that we can solve the stages (Y,Y') sequentially
    898:         * Build the affine vector z_i = -[1/(h a_ii)](h sum_j a_ij y'_j + sum_j u_ij x_j)
    899:         * Then y'_i = z + 1/(h a_ii) y_i
    900:         */
    901:         VecZeroEntries(gl->Z);
    902:         for (j=0; j<r; j++) {
    903:           VecAXPY(gl->Z,-shift*u[i*r+j],X[j]);
    904:         }
    905:         for (j=0; j<i; j++) {
    906:           VecAXPY(gl->Z,-shift*h*a[i*s+j],Ydot[j]);
    907:         }
    908:         /* Note: Z is used within function evaluation, Ydot = Z + shift*Y */

    910:         /* Compute an estimate of Y to start Newton iteration */
    911:         if (gl->extrapolate) {
    912:           if (i==0) {
    913:             /* Linear extrapolation on the first stage */
    914:             VecWAXPY(Y,c[i]*h,X[1],X[0]);
    915:           } else {
    916:             /* Linear extrapolation from the last stage */
    917:             VecAXPY(Y,(c[i]-c[i-1])*h,Ydot[i-1]);
    918:           }
    919:         } else if (i==0) {        /* Directly use solution from the last step, otherwise reuse the last stage (do nothing) */
    920:           VecCopy(X[0],Y);
    921:         }

    923:         /* Solve this stage (Ydot[i] is computed during function evaluation) */
    924:         SNESSolve(ts->snes,NULL,Y);
    925:         SNESGetIterationNumber(ts->snes,&its);
    926:         SNESGetLinearSolveIterations(ts->snes,&lits);
    927:         SNESGetConvergedReason(ts->snes,&snesreason);
    928:         ts->snes_its += its; ts->ksp_its += lits;
    929:         if (snesreason < 0 && ts->max_snes_failures > 0 && ++ts->num_snes_failures >= ts->max_snes_failures) {
    930:           ts->reason = TS_DIVERGED_NONLINEAR_SOLVE;
    931:           PetscInfo2(ts,"Step=%D, nonlinear solve solve failures %D greater than current TS allowed, stopping solve\n",ts->steps,ts->num_snes_failures);
    932:           return(0);
    933:         }
    934:       }

    936:       gl->stage_time = ts->ptime + ts->time_step;

    938:       (*gl->EstimateHigherMoments)(scheme,h,Ydot,gl->X,gl->himom);
    939:       /* hmnorm[i] = h^{p+i}x^{(p+i)} with i=0,1,2; hmnorm[3] = h^{p+2}(dx'/dx) x^{(p+1)} */
    940:       for (i=0; i<3; i++) {
    941:         TSGLLEVecNormWRMS(ts,gl->himom[i],&hmnorm[i+1]);
    942:       }
    943:       enorm[0] = PetscRealPart(scheme->alpha[0])*hmnorm[1];
    944:       enorm[1] = PetscRealPart(scheme->beta[0]) *hmnorm[2];
    945:       enorm[2] = PetscRealPart(scheme->gamma[0])*hmnorm[3];
    946:       (*gl->Accept)(ts,ts->max_time-gl->stage_time,h,enorm,&accept);
    947:       if (accept) goto accepted;
    948:       rejections++;
    949:       PetscInfo3(ts,"Step %D (t=%g) not accepted, rejections=%D\n",k,gl->stage_time,rejections);
    950:       if (rejections > gl->max_step_rejections) break;
    951:       /*
    952:         There are lots of reasons why a step might be rejected, including solvers not converging and other factors that
    953:         TSGLLEChooseNextScheme does not support.  Additionally, the error estimates may be very screwed up, so I'm not
    954:         convinced that it's safe to just compute a new error estimate using the same interface as the current adaptor
    955:         (the adaptor interface probably has to change).  Here we make an arbitrary and naive choice.  This assumes that
    956:         steps were written in Nordsieck form.  The "correct" method would be to re-complete the previous time step with
    957:         the correct "next" step size.  It is unclear to me whether the present ad-hoc method of rescaling X is stable.
    958:       */
    959:       h *= 0.5;
    960:       for (i=1; i<scheme->r; i++) {
    961:         VecScale(X[i],PetscPowRealInt(0.5,i));
    962:       }
    963:     }
    964:     SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_CONV_FAILED,"Time step %D (t=%g) not accepted after %D failures\n",k,gl->stage_time,rejections);

    966: accepted:
    967:     /* This term is not error, but it *would* be the leading term for a lower order method */
    968:     TSGLLEVecNormWRMS(ts,gl->X[scheme->r-1],&hmnorm[0]);
    969:     /* Correct scaling so that these are equivalent to norms of the Nordsieck vectors */

    971:     PetscInfo4(ts,"Last moment norm %10.2e, estimated error norms %10.2e %10.2e %10.2e\n",hmnorm[0],enorm[0],enorm[1],enorm[2]);
    972:     if (!final_step) {
    973:       TSGLLEChooseNextScheme(ts,h,hmnorm,&next_scheme,&next_h,&final_step);
    974:     } else {
    975:       /* Dummy values to complete the current step in a consistent manner */
    976:       next_scheme = gl->current_scheme;
    977:       next_h      = h;
    978:       finish      = PETSC_TRUE;
    979:     }

    981:     X        = gl->Xold;
    982:     gl->Xold = gl->X;
    983:     gl->X    = X;
    984:     (*gl->CompleteStep)(scheme,h,gl->schemes[next_scheme],next_h,Ydot,gl->Xold,gl->X);

    986:     TSGLLEUpdateWRMS(ts);

    988:     /* Post the solution for the user, we could avoid this copy with a small bit of cleverness */
    989:     VecCopy(gl->X[0],ts->vec_sol);
    990:     ts->ptime += h;
    991:     ts->steps++;

    993:     TSPostEvaluate(ts);
    994:     TSPostStep(ts);
    995:     TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);

    997:     gl->current_scheme = next_scheme;
    998:     ts->time_step      = next_h;
    999:   }
    1000:   return(0);
    1001: }

    1003: /*------------------------------------------------------------*/

    1005: static PetscErrorCode TSReset_GLLE(TS ts)
    1006: {
    1007:   TS_GLLE        *gl = (TS_GLLE*)ts->data;
    1008:   PetscInt       max_r,max_s;

    1012:   if (gl->setupcalled) {
    1013:     TSGLLEGetMaxSizes(ts,&max_r,&max_s);
    1014:     VecDestroyVecs(max_r,&gl->Xold);
    1015:     VecDestroyVecs(max_r,&gl->X);
    1016:     VecDestroyVecs(max_s,&gl->Ydot);
    1017:     VecDestroyVecs(3,&gl->himom);
    1018:     VecDestroy(&gl->W);
    1019:     VecDestroy(&gl->Y);
    1020:     VecDestroy(&gl->Z);
    1021:   }
    1022:   gl->setupcalled = PETSC_FALSE;
    1023:   return(0);
    1024: }

    1026: static PetscErrorCode TSDestroy_GLLE(TS ts)
    1027: {
    1028:   TS_GLLE        *gl = (TS_GLLE*)ts->data;

    1032:   TSReset_GLLE(ts);
    1033:   if (ts->dm) {
    1034:     DMCoarsenHookRemove(ts->dm,DMCoarsenHook_TSGLLE,DMRestrictHook_TSGLLE,ts);
    1035:     DMSubDomainHookRemove(ts->dm,DMSubDomainHook_TSGLLE,DMSubDomainRestrictHook_TSGLLE,ts);
    1036:   }
    1037:   if (gl->adapt) {TSGLLEAdaptDestroy(&gl->adapt);}
    1038:   if (gl->Destroy) {(*gl->Destroy)(gl);}
    1039:   PetscFree(ts->data);
    1040:   PetscObjectComposeFunction((PetscObject)ts,"TSGLLESetType_C",NULL);
    1041:   PetscObjectComposeFunction((PetscObject)ts,"TSGLLESetAcceptType_C",NULL);
    1042:   PetscObjectComposeFunction((PetscObject)ts,"TSGLLEGetAdapt_C",NULL);
    1043:   return(0);
    1044: }

    1046: /*
    1047:     This defines the nonlinear equation that is to be solved with SNES
    1048:     g(x) = f(t,x,z+shift*x) = 0
    1049: */
    1050: static PetscErrorCode SNESTSFormFunction_GLLE(SNES snes,Vec x,Vec f,TS ts)
    1051: {
    1052:   TS_GLLE        *gl = (TS_GLLE*)ts->data;
    1054:   Vec            Z,Ydot;
    1055:   DM             dm,dmsave;

    1058:   SNESGetDM(snes,&dm);
    1059:   TSGLLEGetVecs(ts,dm,&Z,&Ydot);
    1060:   VecWAXPY(Ydot,gl->scoeff/ts->time_step,x,Z);
    1061:   dmsave = ts->dm;
    1062:   ts->dm = dm;
    1063:   TSComputeIFunction(ts,gl->stage_time,x,Ydot,f,PETSC_FALSE);
    1064:   ts->dm = dmsave;
    1065:   TSGLLERestoreVecs(ts,dm,&Z,&Ydot);
    1066:   return(0);
    1067: }

    1069: static PetscErrorCode SNESTSFormJacobian_GLLE(SNES snes,Vec x,Mat A,Mat B,TS ts)
    1070: {
    1071:   TS_GLLE        *gl = (TS_GLLE*)ts->data;
    1073:   Vec            Z,Ydot;
    1074:   DM             dm,dmsave;

    1077:   SNESGetDM(snes,&dm);
    1078:   TSGLLEGetVecs(ts,dm,&Z,&Ydot);
    1079:   dmsave = ts->dm;
    1080:   ts->dm = dm;
    1081:   /* gl->Xdot will have already been computed in SNESTSFormFunction_GLLE */
    1082:   TSComputeIJacobian(ts,gl->stage_time,x,gl->Ydot[gl->stage],gl->scoeff/ts->time_step,A,B,PETSC_FALSE);
    1083:   ts->dm = dmsave;
    1084:   TSGLLERestoreVecs(ts,dm,&Z,&Ydot);
    1085:   return(0);
    1086: }

    1089: static PetscErrorCode TSSetUp_GLLE(TS ts)
    1090: {
    1091:   TS_GLLE        *gl = (TS_GLLE*)ts->data;
    1092:   PetscInt       max_r,max_s;
    1094:   DM             dm;

    1097:   gl->setupcalled = PETSC_TRUE;
    1098:   TSGLLEGetMaxSizes(ts,&max_r,&max_s);
    1099:   VecDuplicateVecs(ts->vec_sol,max_r,&gl->X);
    1100:   VecDuplicateVecs(ts->vec_sol,max_r,&gl->Xold);
    1101:   VecDuplicateVecs(ts->vec_sol,max_s,&gl->Ydot);
    1102:   VecDuplicateVecs(ts->vec_sol,3,&gl->himom);
    1103:   VecDuplicate(ts->vec_sol,&gl->W);
    1104:   VecDuplicate(ts->vec_sol,&gl->Y);
    1105:   VecDuplicate(ts->vec_sol,&gl->Z);

    1107:   /* Default acceptance tests and adaptivity */
    1108:   if (!gl->Accept) {TSGLLESetAcceptType(ts,TSGLLEACCEPT_ALWAYS);}
    1109:   if (!gl->adapt)  {TSGLLEGetAdapt(ts,&gl->adapt);}

    1111:   if (gl->current_scheme < 0) {
    1112:     PetscInt i;
    1113:     for (i=0;; i++) {
    1114:       if (gl->schemes[i]->p == gl->start_order) break;
    1115:       if (i+1 == gl->nschemes) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"No schemes available with requested start order %d",i);
    1116:     }
    1117:     gl->current_scheme = i;
    1118:   }
    1119:   TSGetDM(ts,&dm);
    1120:   DMCoarsenHookAdd(dm,DMCoarsenHook_TSGLLE,DMRestrictHook_TSGLLE,ts);
    1121:   DMSubDomainHookAdd(dm,DMSubDomainHook_TSGLLE,DMSubDomainRestrictHook_TSGLLE,ts);
    1122:   return(0);
    1123: }
    1124: /*------------------------------------------------------------*/

    1126: static PetscErrorCode TSSetFromOptions_GLLE(PetscOptionItems *PetscOptionsObject,TS ts)
    1127: {
    1128:   TS_GLLE        *gl        = (TS_GLLE*)ts->data;
    1129:   char           tname[256] = TSGLLE_IRKS,completef[256] = "rescale-and-modify";

    1133:   PetscOptionsHead(PetscOptionsObject,"General Linear ODE solver options");
    1134:   {
    1135:     PetscBool flg;
    1136:     PetscOptionsFList("-ts_gl_type","Type of GL method","TSGLLESetType",TSGLLEList,gl->type_name[0] ? gl->type_name : tname,tname,sizeof(tname),&flg);
    1137:     if (flg || !gl->type_name[0]) {
    1138:       TSGLLESetType(ts,tname);
    1139:     }
    1140:     PetscOptionsInt("-ts_gl_max_step_rejections","Maximum number of times to attempt a step","None",gl->max_step_rejections,&gl->max_step_rejections,NULL);
    1141:     PetscOptionsInt("-ts_gl_max_order","Maximum order to try","TSGLLESetMaxOrder",gl->max_order,&gl->max_order,NULL);
    1142:     PetscOptionsInt("-ts_gl_min_order","Minimum order to try","TSGLLESetMinOrder",gl->min_order,&gl->min_order,NULL);
    1143:     PetscOptionsInt("-ts_gl_start_order","Initial order to try","TSGLLESetMinOrder",gl->start_order,&gl->start_order,NULL);
    1144:     PetscOptionsEnum("-ts_gl_error_direction","Which direction to look when estimating error","TSGLLESetErrorDirection",TSGLLEErrorDirections,(PetscEnum)gl->error_direction,(PetscEnum*)&gl->error_direction,NULL);
    1145:     PetscOptionsBool("-ts_gl_extrapolate","Extrapolate stage solution from previous solution (sometimes unstable)","TSGLLESetExtrapolate",gl->extrapolate,&gl->extrapolate,NULL);
    1146:     PetscOptionsReal("-ts_gl_atol","Absolute tolerance","TSGLLESetTolerances",gl->wrms_atol,&gl->wrms_atol,NULL);
    1147:     PetscOptionsReal("-ts_gl_rtol","Relative tolerance","TSGLLESetTolerances",gl->wrms_rtol,&gl->wrms_rtol,NULL);
    1148:     PetscOptionsString("-ts_gl_complete","Method to use for completing the step","none",completef,completef,sizeof(completef),&flg);
    1149:     if (flg) {
    1150:       PetscBool match1,match2;
    1151:       PetscStrcmp(completef,"rescale",&match1);
    1152:       PetscStrcmp(completef,"rescale-and-modify",&match2);
    1153:       if (match1)      gl->CompleteStep = TSGLLECompleteStep_Rescale;
    1154:       else if (match2) gl->CompleteStep = TSGLLECompleteStep_RescaleAndModify;
    1155:       else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"%s",completef);
    1156:     }
    1157:     {
    1158:       char type[256] = TSGLLEACCEPT_ALWAYS;
    1159:       PetscOptionsFList("-ts_gl_accept_type","Method to use for determining whether to accept a step","TSGLLESetAcceptType",TSGLLEAcceptList,gl->accept_name[0] ? gl->accept_name : type,type,sizeof(type),&flg);
    1160:       if (flg || !gl->accept_name[0]) {
    1161:         TSGLLESetAcceptType(ts,type);
    1162:       }
    1163:     }
    1164:     {
    1165:       TSGLLEAdapt adapt;
    1166:       TSGLLEGetAdapt(ts,&adapt);
    1167:       TSGLLEAdaptSetFromOptions(PetscOptionsObject,adapt);
    1168:     }
    1169:   }
    1170:   PetscOptionsTail();
    1171:   return(0);
    1172: }

    1174: static PetscErrorCode TSView_GLLE(TS ts,PetscViewer viewer)
    1175: {
    1176:   TS_GLLE        *gl = (TS_GLLE*)ts->data;
    1177:   PetscInt       i;
    1178:   PetscBool      iascii,details;

    1182:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    1183:   if (iascii) {
    1184:     PetscViewerASCIIPrintf(viewer,"  min order %D, max order %D, current order %D\n",gl->min_order,gl->max_order,gl->schemes[gl->current_scheme]->p);
    1185:     PetscViewerASCIIPrintf(viewer,"  Error estimation: %s\n",TSGLLEErrorDirections[gl->error_direction]);
    1186:     PetscViewerASCIIPrintf(viewer,"  Extrapolation: %s\n",gl->extrapolate ? "yes" : "no");
    1187:     PetscViewerASCIIPrintf(viewer,"  Acceptance test: %s\n",gl->accept_name[0] ? gl->accept_name : "(not yet set)");
    1188:     PetscViewerASCIIPushTab(viewer);
    1189:     TSGLLEAdaptView(gl->adapt,viewer);
    1190:     PetscViewerASCIIPopTab(viewer);
    1191:     PetscViewerASCIIPrintf(viewer,"  type: %s\n",gl->type_name[0] ? gl->type_name : "(not yet set)");
    1192:     PetscViewerASCIIPrintf(viewer,"Schemes within family (%d):\n",gl->nschemes);
    1193:     details = PETSC_FALSE;
    1194:     PetscOptionsGetBool(((PetscObject)ts)->options,((PetscObject)ts)->prefix,"-ts_gl_view_detailed",&details,NULL);
    1195:     PetscViewerASCIIPushTab(viewer);
    1196:     for (i=0; i<gl->nschemes; i++) {
    1197:       TSGLLESchemeView(gl->schemes[i],details,viewer);
    1198:     }
    1199:     if (gl->View) {
    1200:       (*gl->View)(gl,viewer);
    1201:     }
    1202:     PetscViewerASCIIPopTab(viewer);
    1203:   }
    1204:   return(0);
    1205: }

    1207: /*@C
    1208:    TSGLLERegister -  adds a TSGLLE implementation

    1210:    Not Collective

    1212:    Input Parameters:
    1213: +  name_scheme - name of user-defined general linear scheme
    1214: -  routine_create - routine to create method context

    1216:    Notes:
    1217:    TSGLLERegister() may be called multiple times to add several user-defined families.

    1219:    Sample usage:
    1220: .vb
    1221:    TSGLLERegister("my_scheme",MySchemeCreate);
    1222: .ve

    1224:    Then, your scheme can be chosen with the procedural interface via
    1225: $     TSGLLESetType(ts,"my_scheme")
    1226:    or at runtime via the option
    1227: $     -ts_gl_type my_scheme

    1229:    Level: advanced

    1231: .seealso: TSGLLERegisterAll()
    1232: @*/
    1233: PetscErrorCode  TSGLLERegister(const char sname[],PetscErrorCode (*function)(TS))
    1234: {

    1238:   TSGLLEInitializePackage();
    1239:   PetscFunctionListAdd(&TSGLLEList,sname,function);
    1240:   return(0);
    1241: }

    1243: /*@C
    1244:    TSGLLEAcceptRegister -  adds a TSGLLE acceptance scheme

    1246:    Not Collective

    1248:    Input Parameters:
    1249: +  name_scheme - name of user-defined acceptance scheme
    1250: -  routine_create - routine to create method context

    1252:    Notes:
    1253:    TSGLLEAcceptRegister() may be called multiple times to add several user-defined families.

    1255:    Sample usage:
    1256: .vb
    1257:    TSGLLEAcceptRegister("my_scheme",MySchemeCreate);
    1258: .ve

    1260:    Then, your scheme can be chosen with the procedural interface via
    1261: $     TSGLLESetAcceptType(ts,"my_scheme")
    1262:    or at runtime via the option
    1263: $     -ts_gl_accept_type my_scheme

    1265:    Level: advanced

    1267: .seealso: TSGLLERegisterAll()
    1268: @*/
    1269: PetscErrorCode  TSGLLEAcceptRegister(const char sname[],TSGLLEAcceptFunction function)
    1270: {

    1274:   PetscFunctionListAdd(&TSGLLEAcceptList,sname,function);
    1275:   return(0);
    1276: }

    1278: /*@C
    1279:   TSGLLERegisterAll - Registers all of the general linear methods in TSGLLE

    1281:   Not Collective

    1283:   Level: advanced

    1285: .seealso:  TSGLLERegisterDestroy()
    1286: @*/
    1287: PetscErrorCode  TSGLLERegisterAll(void)
    1288: {

    1292:   if (TSGLLERegisterAllCalled) return(0);
    1293:   TSGLLERegisterAllCalled = PETSC_TRUE;

    1295:   TSGLLERegister(TSGLLE_IRKS,              TSGLLECreate_IRKS);
    1296:   TSGLLEAcceptRegister(TSGLLEACCEPT_ALWAYS,TSGLLEAccept_Always);
    1297:   return(0);
    1298: }

    1300: /*@C
    1301:   TSGLLEInitializePackage - This function initializes everything in the TSGLLE package. It is called
    1302:   from TSInitializePackage().

    1304:   Level: developer

    1306: .seealso: PetscInitialize()
    1307: @*/
    1308: PetscErrorCode  TSGLLEInitializePackage(void)
    1309: {

    1313:   if (TSGLLEPackageInitialized) return(0);
    1314:   TSGLLEPackageInitialized = PETSC_TRUE;
    1315:   TSGLLERegisterAll();
    1316:   PetscRegisterFinalize(TSGLLEFinalizePackage);
    1317:   return(0);
    1318: }

    1320: /*@C
    1321:   TSGLLEFinalizePackage - This function destroys everything in the TSGLLE package. It is
    1322:   called from PetscFinalize().

    1324:   Level: developer

    1326: .seealso: PetscFinalize()
    1327: @*/
    1328: PetscErrorCode  TSGLLEFinalizePackage(void)
    1329: {

    1333:   PetscFunctionListDestroy(&TSGLLEList);
    1334:   PetscFunctionListDestroy(&TSGLLEAcceptList);
    1335:   TSGLLEPackageInitialized = PETSC_FALSE;
    1336:   TSGLLERegisterAllCalled  = PETSC_FALSE;
    1337:   return(0);
    1338: }

    1340: /* ------------------------------------------------------------ */
    1341: /*MC
    1342:       TSGLLE - DAE solver using implicit General Linear methods

    1344:   These methods contain Runge-Kutta and multistep schemes as special cases.  These special cases have some fundamental
    1345:   limitations.  For example, diagonally implicit Runge-Kutta cannot have stage order greater than 1 which limits their
    1346:   applicability to very stiff systems.  Meanwhile, multistep methods cannot be A-stable for order greater than 2 and BDF
    1347:   are not 0-stable for order greater than 6.  GL methods can be A- and L-stable with arbitrarily high stage order and
    1348:   reliable error estimates for both 1 and 2 orders higher to facilitate adaptive step sizes and adaptive order schemes.
    1349:   All this is possible while preserving a singly diagonally implicit structure.

    1351:   Options database keys:
    1352: +  -ts_gl_type <type> - the class of general linear method (irks)
    1353: .  -ts_gl_rtol <tol>  - relative error
    1354: .  -ts_gl_atol <tol>  - absolute error
    1355: .  -ts_gl_min_order <p> - minimum order method to consider (default=1)
    1356: .  -ts_gl_max_order <p> - maximum order method to consider (default=3)
    1357: .  -ts_gl_start_order <p> - order of starting method (default=1)
    1358: .  -ts_gl_complete <method> - method to use for completing the step (rescale-and-modify or rescale)
    1359: -  -ts_adapt_type <method> - adaptive controller to use (none step both)

    1361:   Notes:
    1362:   This integrator can be applied to DAE.

    1364:   Diagonally implicit general linear (DIGL) methods are a generalization of diagonally implicit Runge-Kutta (DIRK).
    1365:   They are represented by the tableau

    1367: .vb
    1368:   A  |  U
    1369:   -------
    1370:   B  |  V
    1371: .ve

    1373:   combined with a vector c of abscissa.  "Diagonally implicit" means that A is lower triangular.
    1374:   A step of the general method reads

    1376: .vb
    1377:   [ Y ] = [A  U] [  Y'   ]
    1378:   [X^k] = [B  V] [X^{k-1}]
    1379: .ve

    1381:   where Y is the multivector of stage values, Y' is the multivector of stage derivatives, X^k is the Nordsieck vector of
    1382:   the solution at step k.  The Nordsieck vector consists of the first r moments of the solution, given by

    1384: .vb
    1385:   X = [x_0,x_1,...,x_{r-1}] = [x, h x', h^2 x'', ..., h^{r-1} x^{(r-1)} ]
    1386: .ve

    1388:   If A is lower triangular, we can solve the stages (Y,Y') sequentially

    1390: .vb
    1391:   y_i = h sum_{j=0}^{s-1} (a_ij y'_j) + sum_{j=0}^{r-1} u_ij x_j,    i=0,...,{s-1}
    1392: .ve

    1394:   and then construct the pieces to carry to the next step

    1396: .vb
    1397:   xx_i = h sum_{j=0}^{s-1} b_ij y'_j  + sum_{j=0}^{r-1} v_ij x_j,    i=0,...,{r-1}
    1398: .ve

    1400:   Note that when the equations are cast in implicit form, we are using the stage equation to define y'_i
    1401:   in terms of y_i and known stuff (y_j for j<i and x_j for all j).

    1404:   Error estimation

    1406:   At present, the most attractive GL methods for stiff problems are singly diagonally implicit schemes which posses
    1407:   Inherent Runge-Kutta Stability (IRKS).  These methods have r=s, the number of items passed between steps is equal to
    1408:   the number of stages.  The order and stage-order are one less than the number of stages.  We use the error estimates
    1409:   in the 2007 paper which provide the following estimates

    1411: .vb
    1412:   h^{p+1} X^{(p+1)}          = phi_0^T Y' + [0 psi_0^T] Xold
    1413:   h^{p+2} X^{(p+2)}          = phi_1^T Y' + [0 psi_1^T] Xold
    1414:   h^{p+2} (dx'/dx) X^{(p+1)} = phi_2^T Y' + [0 psi_2^T] Xold
    1415: .ve

    1417:   These estimates are accurate to O(h^{p+3}).

    1419:   Changing the step size

    1421:   We use the generalized "rescale and modify" scheme, see equation (4.5) of the 2007 paper.

    1423:   Level: beginner

    1425:   References:
    1426: +  1. - John Butcher and Z. Jackieweicz and W. Wright, On error propagation in general linear methods for
    1427:   ordinary differential equations, Journal of Complexity, Vol 23, 2007.
    1428: -  2. - John Butcher, Numerical methods for ordinary differential equations, second edition, Wiley, 2009.

    1430: .seealso:  TSCreate(), TS, TSSetType()

    1432: M*/
    1433: PETSC_EXTERN PetscErrorCode TSCreate_GLLE(TS ts)
    1434: {
    1435:   TS_GLLE        *gl;

    1439:   TSGLLEInitializePackage();

    1441:   PetscNewLog(ts,&gl);
    1442:   ts->data = (void*)gl;

    1444:   ts->ops->reset          = TSReset_GLLE;
    1445:   ts->ops->destroy        = TSDestroy_GLLE;
    1446:   ts->ops->view           = TSView_GLLE;
    1447:   ts->ops->setup          = TSSetUp_GLLE;
    1448:   ts->ops->solve          = TSSolve_GLLE;
    1449:   ts->ops->setfromoptions = TSSetFromOptions_GLLE;
    1450:   ts->ops->snesfunction   = SNESTSFormFunction_GLLE;
    1451:   ts->ops->snesjacobian   = SNESTSFormJacobian_GLLE;

    1453:   ts->usessnes = PETSC_TRUE;

    1456:   gl->max_step_rejections = 1;
    1457:   gl->min_order           = 1;
    1458:   gl->max_order           = 3;
    1459:   gl->start_order         = 1;
    1460:   gl->current_scheme      = -1;
    1461:   gl->extrapolate         = PETSC_FALSE;

    1463:   gl->wrms_atol = 1e-8;
    1464:   gl->wrms_rtol = 1e-5;

    1466:   PetscObjectComposeFunction((PetscObject)ts,"TSGLLESetType_C",      &TSGLLESetType_GLLE);
    1467:   PetscObjectComposeFunction((PetscObject)ts,"TSGLLESetAcceptType_C",&TSGLLESetAcceptType_GLLE);
    1468:   PetscObjectComposeFunction((PetscObject)ts,"TSGLLEGetAdapt_C",     &TSGLLEGetAdapt_GLLE);
    1469:   return(0);
    1470: }