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

Actual source code: eimex.c
petsc-3.7.3 2016-07-24
Report Typos and Errors

      1: /*
      2:  * eimex.c
      3:  *
      4:  *  Created on: Jun 21, 2012
      5:  *      Written by Hong Zhang (zhang@vt.edu), Virginia Tech
      6:  *                 Emil Constantinescu (emconsta@mcs.anl.gov), Argonne National Laboratory
      7:  */
      8: /*MC
      9:       EIMEX - Time stepping with Extrapolated IMEX methods.

     11:   Notes:
     12:   The general system is written as

     14:   G(t,X,Xdot) = F(t,X)

     16:   where G represents the stiff part and F represents the non-stiff part. The user should provide the stiff part
     17:   of the equation using TSSetIFunction() and the non-stiff part with TSSetRHSFunction().
     18:   This method is designed to be linearly implicit on G and can use an approximate and lagged Jacobian.

     20:   Another common form for the system is

     22:   y'=f(x)+g(x)

     24:   The relationship between F,G and f,g is

     26:   G = y'-g(x), F = f(x)

     28:  References
     29:   E. Constantinescu and A. Sandu, Extrapolated implicit-explicit time stepping, SIAM Journal on Scientific
     30: Computing, 31 (2010), pp. 4452-4477.

     32:       Level: beginner

     34: .seealso:  TSCreate(), TS, TSSetType(), TSEIMEXSetMaxRows(), TSEIMEXSetRowCol(), TSEIMEXSetOrdAdapt()

     36:  M*/
     37: #include <petsc/private/tsimpl.h>                /*I   "petscts.h"   I*/
     38: #include <petscdm.h>

     40: static const PetscInt TSEIMEXDefault = 3;

     42: typedef struct {
     43:   PetscInt     row_ind;         /* Return the term T[row_ind][col_ind] */
     44:   PetscInt     col_ind;         /* Return the term T[row_ind][col_ind] */
     45:   PetscInt     nstages;         /* Numbers of stages in current scheme */
     46:   PetscInt     max_rows;        /* Maximum number of rows */
     47:   PetscInt     *N;              /* Harmonic sequence N[max_rows] */
     48:   Vec          Y;               /* States computed during the step, used to complete the step */
     49:   Vec          Z;               /* For shift*(Y-Z) */
     50:   Vec          *T;              /* Working table, size determined by nstages */
     51:   Vec          YdotRHS;         /* f(x) Work vector holding YdotRHS during residual evaluation */
     52:   Vec          YdotI;           /* xdot-g(x) Work vector holding YdotI = G(t,x,xdot) when xdot =0 */
     53:   Vec          Ydot;            /* f(x)+g(x) Work vector */
     54:   Vec          VecSolPrev;      /* Work vector holding the solution from the previous step (used for interpolation) */
     55:   PetscReal    shift;
     56:   PetscReal    ctime;
     57:   PetscBool    recompute_jacobian; /* Recompute the Jacobian at each stage, default is to freeze the Jacobian at the start of each step */
     58:   PetscBool    ord_adapt;       /* order adapativity */
     59:   TSStepStatus status;
     60: } TS_EIMEX;

     62: /* This function is pure */
     63: static PetscInt Map(PetscInt i, PetscInt j, PetscInt s)
     64: {
     65:   return ((2*s-j+1)*j/2+i-j);
     66: }

     71: static PetscErrorCode TSEvaluateStep_EIMEX(TS ts,PetscInt order,Vec X,PetscBool *done)
     72: {
     73:   TS_EIMEX        *ext = (TS_EIMEX*)ts->data;
     74:   const PetscInt  ns = ext->nstages;
     77:   VecCopy(ext->T[Map(ext->row_ind,ext->col_ind,ns)],X);
     78:   return(0);
     79: }

     84: static PetscErrorCode TSStage_EIMEX(TS ts,PetscInt istage)
     85: {
     86:   TS_EIMEX        *ext = (TS_EIMEX*)ts->data;
     87:   PetscReal       h;
     88:   Vec             Y=ext->Y, Z=ext->Z;
     89:   SNES            snes;
     90:   TSAdapt         adapt;
     91:   PetscInt        i,its,lits;
     92:   PetscBool       accept;
     93:   PetscErrorCode  ierr;

     96:   TSGetSNES(ts,&snes);
     97:   h = ts->time_step/ext->N[istage];/* step size for the istage-th stage */
     98:   ext->shift = 1./h;
     99:   SNESSetLagJacobian(snes,-2); /* Recompute the Jacobian on this solve, but not again */
    100:   VecCopy(ext->VecSolPrev,Y); /* Take the previous solution as intial step */

    102:   for(i=0; i<ext->N[istage]; i++){
    103:     ext->ctime = ts->ptime + h*i;
    104:     VecCopy(Y,Z);/* Save the solution of the previous substep */
    105:     SNESSolve(snes,NULL,Y);
    106:     SNESGetIterationNumber(snes,&its);
    107:     SNESGetLinearSolveIterations(snes,&lits);
    108:     ts->snes_its += its; ts->ksp_its += lits;
    109:     TSGetAdapt(ts,&adapt);
    110:     TSAdaptCheckStage(adapt,ts,ext->ctime,Y,&accept);
    111:   }

    113:   return(0);
    114: }

    119: static PetscErrorCode TSStep_EIMEX(TS ts)
    120: {
    121:   TS_EIMEX        *ext = (TS_EIMEX*)ts->data;
    122:   const PetscInt  ns = ext->nstages;
    123:   Vec             *T=ext->T, Y=ext->Y;

    125:   SNES            snes;
    126:   PetscInt        i,j;
    127:   PetscBool       accept = PETSC_FALSE;
    128:   PetscErrorCode  ierr;
    129:   PetscReal       alpha,local_error;

    132:   TSGetSNES(ts,&snes);
    133:   SNESSetType(snes,"ksponly");
    134:   ext->status = TS_STEP_INCOMPLETE;

    136:   VecCopy(ts->vec_sol,ext->VecSolPrev);

    138:   /* Apply n_j steps of the base method to obtain solutions of T(j,1),1<=j<=s */
    139:   for(j=0; j<ns; j++){
    140:         TSStage_EIMEX(ts,j);
    141:         VecCopy(Y,T[j]);
    142:   }

    144:   for(i=1;i<ns;i++){
    145:     for(j=i;j<ns;j++){
    146:       alpha = -(PetscReal)ext->N[j]/ext->N[j-i];
    147:       VecAXPBYPCZ(T[Map(j,i,ns)],alpha,1.0,0,T[Map(j,i-1,ns)],T[Map(j-1,i-1,ns)]);/* T[j][i]=alpha*T[j][i-1]+T[j-1][i-1] */
    148:       alpha = 1.0/(1.0 + alpha);
    149:       VecScale(T[Map(j,i,ns)],alpha);
    150:     }
    151:   }

    153:   TSEvaluateStep(ts,ns,ts->vec_sol,NULL);/*update ts solution */

    155:   if(ext->ord_adapt && ext->nstages < ext->max_rows){
    156:         accept = PETSC_FALSE;
    157:         while(!accept && ext->nstages < ext->max_rows){
    158:           TSErrorWeightedNorm(ts,ts->vec_sol,T[Map(ext->nstages-1,ext->nstages-2,ext->nstages)],ts->adapt->wnormtype,&local_error);
    159:           accept = (local_error < 1.0)? PETSC_TRUE : PETSC_FALSE;

    161:           if(!accept){/* add one more stage*/
    162:             TSStage_EIMEX(ts,ext->nstages);
    163:             ext->nstages++; ext->row_ind++; ext->col_ind++;
    164:             /*T table need to be recycled*/
    165:             VecDuplicateVecs(ts->vec_sol,(1+ext->nstages)*ext->nstages/2,&ext->T);
    166:             for(i=0; i<ext->nstages-1; i++){
    167:               for(j=0; j<=i; j++){
    168:                 VecCopy(T[Map(i,j,ext->nstages-1)],ext->T[Map(i,j,ext->nstages)]);
    169:               }
    170:             }
    171:             VecDestroyVecs(ext->nstages*(ext->nstages-1)/2,&T);
    172:             T = ext->T; /*reset the pointer*/
    173:             /*recycling finished, store the new solution*/
    174:             VecCopy(Y,T[ext->nstages-1]);
    175:             /*extrapolation for the newly added stage*/
    176:             for(i=1;i<ext->nstages;i++){
    177:               alpha = -(PetscReal)ext->N[ext->nstages-1]/ext->N[ext->nstages-1-i];
    178:               VecAXPBYPCZ(T[Map(ext->nstages-1,i,ext->nstages)],alpha,1.0,0,T[Map(ext->nstages-1,i-1,ext->nstages)],T[Map(ext->nstages-1-1,i-1,ext->nstages)]);/*T[ext->nstages-1][i]=alpha*T[ext->nstages-1][i-1]+T[ext->nstages-1-1][i-1]*/
    179:               alpha = 1.0/(1.0 + alpha);
    180:               VecScale(T[Map(ext->nstages-1,i,ext->nstages)],alpha);
    181:             }
    182:             /*update ts solution */
    183:             TSEvaluateStep(ts,ext->nstages,ts->vec_sol,NULL);
    184:           }/*end if !accept*/
    185:         }/*end while*/

    187:         if(ext->nstages == ext->max_rows){
    188:                 PetscInfo(ts,"Max number of rows has been used\n");
    189:         }
    190:   }/*end if ext->ord_adapt*/
    191:   ts->ptime += ts->time_step;
    192:   ext->status = TS_STEP_COMPLETE;

    194:   if (ext->status != TS_STEP_COMPLETE && !ts->reason) ts->reason = TS_DIVERGED_STEP_REJECTED;
    195:   return(0);
    196: }

    198: /* cubic Hermit spline */
    201: static PetscErrorCode TSInterpolate_EIMEX(TS ts,PetscReal itime,Vec X)
    202: {
    203:   TS_EIMEX       *ext = (TS_EIMEX*)ts->data;
    204:   PetscReal      t,a,b;
    205:   Vec            Y0=ext->VecSolPrev,Y1=ext->Y,Ydot=ext->Ydot,YdotI=ext->YdotI;
    206:   const PetscReal h = ts->ptime - ts->ptime_prev;
    209:   t = (itime -ts->ptime + h)/h;
    210:   /* YdotI = -f(x)-g(x) */

    212:   VecZeroEntries(Ydot);
    213:   TSComputeIFunction(ts,ts->ptime-h,Y0,Ydot,YdotI,PETSC_FALSE);

    215:   a    = 2.0*t*t*t - 3.0*t*t + 1.0;
    216:   b    = -(t*t*t - 2.0*t*t + t)*h;
    217:   VecAXPBYPCZ(X,a,b,0.0,Y0,YdotI);

    219:   TSComputeIFunction(ts,ts->ptime,Y1,Ydot,YdotI,PETSC_FALSE);
    220:   a    = -2.0*t*t*t+3.0*t*t;
    221:   b    = -(t*t*t - t*t)*h;
    222:   VecAXPBYPCZ(X,a,b,1.0,Y1,YdotI);

    224:   return(0);
    225: }

    230: static PetscErrorCode TSReset_EIMEX(TS ts)
    231: {
    232:   TS_EIMEX        *ext = (TS_EIMEX*)ts->data;
    233:   PetscInt        ns;
    234:   PetscErrorCode  ierr;

    237:   ns = ext->nstages;
    238:   VecDestroyVecs((1+ns)*ns/2,&ext->T);
    239:   VecDestroy(&ext->Y);
    240:   VecDestroy(&ext->Z);
    241:   VecDestroy(&ext->YdotRHS);
    242:   VecDestroy(&ext->YdotI);
    243:   VecDestroy(&ext->Ydot);
    244:   VecDestroy(&ext->VecSolPrev);
    245:   PetscFree(ext->N);
    246:   return(0);
    247: }

    251: static PetscErrorCode TSDestroy_EIMEX(TS ts)
    252: {
    253:   PetscErrorCode  ierr;

    256:   TSReset_EIMEX(ts);
    257:   PetscFree(ts->data);
    258:   PetscObjectComposeFunction((PetscObject)ts,"TSEIMEXSetMaxRows_C",NULL);
    259:   PetscObjectComposeFunction((PetscObject)ts,"TSEIMEXSetRowCol_C",NULL);
    260:   PetscObjectComposeFunction((PetscObject)ts,"TSEIMEXSetOrdAdapt_C",NULL);

    262:   return(0);
    263: }

    268: static PetscErrorCode TSEIMEXGetVecs(TS ts,DM dm,Vec *Z,Vec *Ydot,Vec *YdotI, Vec *YdotRHS)
    269: {
    270:   TS_EIMEX       *ext = (TS_EIMEX*)ts->data;

    274:   if (Z) {
    275:     if (dm && dm != ts->dm) {
    276:       DMGetNamedGlobalVector(dm,"TSEIMEX_Z",Z);
    277:     } else *Z = ext->Z;
    278:   }
    279:   if (Ydot) {
    280:     if (dm && dm != ts->dm) {
    281:       DMGetNamedGlobalVector(dm,"TSEIMEX_Ydot",Ydot);
    282:     } else *Ydot = ext->Ydot;
    283:   }
    284:   if (YdotI) {
    285:     if (dm && dm != ts->dm) {
    286:       DMGetNamedGlobalVector(dm,"TSEIMEX_YdotI",YdotI);
    287:     } else *YdotI = ext->YdotI;
    288:   }
    289:   if (YdotRHS) {
    290:     if (dm && dm != ts->dm) {
    291:       DMGetNamedGlobalVector(dm,"TSEIMEX_YdotRHS",YdotRHS);
    292:     } else *YdotRHS = ext->YdotRHS;
    293:   }
    294:   return(0);
    295: }

    300: static PetscErrorCode TSEIMEXRestoreVecs(TS ts,DM dm,Vec *Z,Vec *Ydot,Vec *YdotI,Vec *YdotRHS)
    301: {

    305:   if (Z) {
    306:     if (dm && dm != ts->dm) {
    307:       DMRestoreNamedGlobalVector(dm,"TSEIMEX_Z",Z);
    308:     }
    309:   }
    310:   if (Ydot) {
    311:     if (dm && dm != ts->dm) {
    312:       DMRestoreNamedGlobalVector(dm,"TSEIMEX_Ydot",Ydot);
    313:     }
    314:   }
    315:   if (YdotI) {
    316:     if (dm && dm != ts->dm) {
    317:       DMRestoreNamedGlobalVector(dm,"TSEIMEX_YdotI",YdotI);
    318:     }
    319:   }
    320:   if (YdotRHS) {
    321:     if (dm && dm != ts->dm) {
    322:       DMRestoreNamedGlobalVector(dm,"TSEIMEX_YdotRHS",YdotRHS);
    323:     }
    324:   }
    325:   return(0);
    326: }

    329: /*
    330:   This defines the nonlinear equation that is to be solved with SNES
    331:   Fn[t0+Theta*dt, U, (U-U0)*shift] = 0
    332:   In the case of Backward Euler, Fn = (U-U0)/h-g(t1,U))
    333:   Since FormIFunction calculates G = ydot - g(t,y), ydot will be set to (U-U0)/h
    334: */
    337: static PetscErrorCode SNESTSFormFunction_EIMEX(SNES snes,Vec X,Vec G,TS ts)
    338: {
    339:   TS_EIMEX        *ext = (TS_EIMEX*)ts->data;
    340:   PetscErrorCode  ierr;
    341:   Vec             Ydot,Z;
    342:   DM              dm,dmsave;

    345:   VecZeroEntries(G);

    347:   SNESGetDM(snes,&dm);
    348:   TSEIMEXGetVecs(ts,dm,&Z,&Ydot,NULL,NULL);
    349:   VecZeroEntries(Ydot);
    350:   dmsave = ts->dm;
    351:   ts->dm = dm;
    352:   TSComputeIFunction(ts,ext->ctime,X,Ydot,G,PETSC_FALSE);
    353:   /* PETSC_FALSE indicates non-imex, adding explicit RHS to the implicit I function.  */
    354:   VecCopy(G,Ydot);
    355:   ts->dm = dmsave;
    356:   TSEIMEXRestoreVecs(ts,dm,&Z,&Ydot,NULL,NULL);

    358:   return(0);
    359: }

    361: /*
    362:  This defined the Jacobian matrix for SNES. Jn = (I/h-g'(t,y))
    363:  */
    366: static PetscErrorCode SNESTSFormJacobian_EIMEX(SNES snes,Vec X,Mat A,Mat B,TS ts)
    367: {
    368:   TS_EIMEX        *ext = (TS_EIMEX*)ts->data;
    369:   Vec             Ydot;
    370:   PetscErrorCode  ierr;
    371:   DM              dm,dmsave;
    373:   SNESGetDM(snes,&dm);
    374:   TSEIMEXGetVecs(ts,dm,NULL,&Ydot,NULL,NULL);
    375:   /*  VecZeroEntries(Ydot); */
    376:   /* ext->Ydot have already been computed in SNESTSFormFunction_EIMEX (SNES guarantees this) */
    377:   dmsave = ts->dm;
    378:   ts->dm = dm;
    379:   TSComputeIJacobian(ts,ts->ptime,X,Ydot,ext->shift,A,B,PETSC_TRUE);
    380:   ts->dm = dmsave;
    381:   TSEIMEXRestoreVecs(ts,dm,NULL,&Ydot,NULL,NULL);
    382:   return(0);
    383: }

    387: static PetscErrorCode DMCoarsenHook_TSEIMEX(DM fine,DM coarse,void *ctx)
    388: {

    391:   return(0);
    392: }

    396: static PetscErrorCode DMRestrictHook_TSEIMEX(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
    397: {
    398:   TS ts = (TS)ctx;
    400:   Vec Z,Z_c;

    403:   TSEIMEXGetVecs(ts,fine,&Z,NULL,NULL,NULL);
    404:   TSEIMEXGetVecs(ts,coarse,&Z_c,NULL,NULL,NULL);
    405:   MatRestrict(restrct,Z,Z_c);
    406:   VecPointwiseMult(Z_c,rscale,Z_c);
    407:   TSEIMEXRestoreVecs(ts,fine,&Z,NULL,NULL,NULL);
    408:   TSEIMEXRestoreVecs(ts,coarse,&Z_c,NULL,NULL,NULL);
    409:   return(0);
    410: }

    415: static PetscErrorCode TSSetUp_EIMEX(TS ts)
    416: {
    417:   TS_EIMEX       *ext = (TS_EIMEX*)ts->data;
    419:   DM             dm;

    422:   if (!ext->N){ /* ext->max_rows not set */
    423:     TSEIMEXSetMaxRows(ts,TSEIMEXDefault);
    424:   }
    425:   if(-1 == ext->row_ind && -1 == ext->col_ind){
    426:         TSEIMEXSetRowCol(ts,ext->max_rows,ext->max_rows);
    427:   } else{/* ext->row_ind and col_ind already set */
    428:     if (ext->ord_adapt){
    429:       PetscInfo(ts,"Order adaptivity is enabled and TSEIMEXSetRowCol or -ts_eimex_row_col option will take no effect\n");
    430:     }
    431:   }

    433:   if(ext->ord_adapt){
    434:     ext->nstages = 2; /* Start with the 2-stage scheme */
    435:     TSEIMEXSetRowCol(ts,ext->nstages,ext->nstages);
    436:   } else{
    437:     ext->nstages = ext->max_rows; /* by default nstages is the same as max_rows, this can be changed by setting order adaptivity */
    438:   }

    440:   VecDuplicateVecs(ts->vec_sol,(1+ext->nstages)*ext->nstages/2,&ext->T);/* full T table */
    441:   VecDuplicate(ts->vec_sol,&ext->YdotI);
    442:   VecDuplicate(ts->vec_sol,&ext->YdotRHS);
    443:   VecDuplicate(ts->vec_sol,&ext->Ydot);
    444:   VecDuplicate(ts->vec_sol,&ext->VecSolPrev);
    445:   VecDuplicate(ts->vec_sol,&ext->Y);
    446:   VecDuplicate(ts->vec_sol,&ext->Z);
    447:   TSGetDM(ts,&dm);
    448:   if (dm) {
    449:     DMCoarsenHookAdd(dm,DMCoarsenHook_TSEIMEX,DMRestrictHook_TSEIMEX,ts);
    450:   }
    451:   return(0);
    452: }

    456: static PetscErrorCode TSSetFromOptions_EIMEX(PetscOptionItems *PetscOptionsObject,TS ts)
    457: {
    458:   TS_EIMEX       *ext = (TS_EIMEX*)ts->data;
    460:   PetscInt       tindex[2];
    461:   PetscInt       np = 2, nrows=TSEIMEXDefault;

    464:   tindex[0] = TSEIMEXDefault;
    465:   tindex[1] = TSEIMEXDefault;
    466:   PetscOptionsHead(PetscOptionsObject,"EIMEX ODE solver options");
    467:   {
    468:     PetscBool flg;
    469:     PetscOptionsInt("-ts_eimex_max_rows","Define the maximum number of rows used","TSEIMEXSetMaxRows",nrows,&nrows,&flg); /* default value 3 */
    470:     if(flg){
    471:       TSEIMEXSetMaxRows(ts,nrows);
    472:     }
    473:     PetscOptionsIntArray("-ts_eimex_row_col","Return the specific term in the T table","TSEIMEXSetRowCol",tindex,&np,&flg);
    474:     if(flg){
    475:       TSEIMEXSetRowCol(ts,tindex[0],tindex[1]);
    476:     }
    477:     PetscOptionsBool("-ts_eimex_order_adapt","Solve the problem with adaptive order","TSEIMEXSetOrdAdapt",ext->ord_adapt,&ext->ord_adapt,NULL);
    478:   }
    479:   PetscOptionsTail();
    480:   return(0);
    481: }

    485: static PetscErrorCode TSView_EIMEX(TS ts,PetscViewer viewer)
    486: {
    487:   /*  TS_EIMEX         *ext = (TS_EIMEX*)ts->data; */
    488:   PetscBool        iascii;
    489:   PetscErrorCode   ierr;

    492:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    493:   if (iascii) {
    494:     PetscViewerASCIIPrintf(viewer,"  EIMEX\n");
    495:   }
    496:   SNESView(ts->snes,viewer);
    497:   return(0);
    498: }

    503: /*@C
    504:   TSEIMEXSetMaxRows - Set the maximum number of rows for EIMEX schemes

    506:   Logically collective

    508:   Input Parameter:
    509: +  ts - timestepping context
    510: -  nrows - maximum number of rows

    512:   Level: intermediate

    514: .seealso: TSEIMEXSetRowCol(), TSEIMEXSetOrdAdapt(), TSEIMEX
    515: @*/
    516: PetscErrorCode TSEIMEXSetMaxRows(TS ts, PetscInt nrows)
    517: {
    521:   PetscTryMethod(ts,"TSEIMEXSetMaxRows_C",(TS,PetscInt),(ts,nrows));
    522:   return(0);
    523: }

    528: /*@C
    529:   TSEIMEXSetRowCol - Set the type index in the T table for the return value

    531:   Logically collective

    533:   Input Parameter:
    534: +  ts - timestepping context
    535: -  tindex - index in the T table

    537:   Level: intermediate

    539: .seealso: TSEIMEXSetMaxRows(), TSEIMEXSetOrdAdapt(), TSEIMEX
    540: @*/
    541: PetscErrorCode TSEIMEXSetRowCol(TS ts, PetscInt row, PetscInt col)
    542: {
    546:   PetscTryMethod(ts,"TSEIMEXSetRowCol_C",(TS,PetscInt, PetscInt),(ts,row,col));
    547:   return(0);
    548: }

    553: /*@C
    554:   TSEIMEXSetOrdAdapt - Set the order adaptativity

    556:   Logically collective

    558:   Input Parameter:
    559: +  ts - timestepping context
    560: -  tindex - index in the T table

    562:   Level: intermediate

    564: .seealso: TSEIMEXSetRowCol(), TSEIMEXSetOrdAdapt(), TSEIMEX
    565: @*/
    566: PetscErrorCode TSEIMEXSetOrdAdapt(TS ts, PetscBool flg)
    567: {
    571:   PetscTryMethod(ts,"TSEIMEXSetOrdAdapt_C",(TS,PetscBool),(ts,flg));
    572:   return(0);
    573: }

    578: static PetscErrorCode TSEIMEXSetMaxRows_EIMEX(TS ts,PetscInt nrows)
    579: {
    580:   TS_EIMEX *ext = (TS_EIMEX*)ts->data;
    582:   PetscInt       i;

    585:   if (nrows < 0 || nrows > 100) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Max number of rows (current value %D) should be an integer number between 1 and 100\n",nrows);
    586:   PetscFree(ext->N);
    587:   ext->max_rows = nrows;
    588:   PetscMalloc1(nrows,&ext->N);
    589:   for(i=0;i<nrows;i++) ext->N[i]=i+1;
    590:   return(0);
    591: }

    595: static PetscErrorCode TSEIMEXSetRowCol_EIMEX(TS ts,PetscInt row,PetscInt col)
    596: {
    597:   TS_EIMEX *ext = (TS_EIMEX*)ts->data;

    600:   if (row < 1 || col < 1) SETERRQ2(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"The row or column index (current value %d,%d) should not be less than 1 \n",row,col);
    601:   if (row > ext->max_rows || col > ext->max_rows) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"The row or column index (current value %d,%d) exceeds the maximum number of rows %d\n",row,col,ext->max_rows);
    602:   if (col > row) SETERRQ2(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"The column index (%d) exceeds the row index (%d)\n",col,row);

    604:   ext->row_ind = row - 1;
    605:   ext->col_ind = col - 1; /* Array index in C starts from 0 */
    606:   return(0);
    607: }

    611: static PetscErrorCode TSEIMEXSetOrdAdapt_EIMEX(TS ts,PetscBool flg)
    612: {
    613:   TS_EIMEX *ext = (TS_EIMEX*)ts->data;
    615:   ext->ord_adapt = flg;
    616:   return(0);
    617: }

    619: /* ------------------------------------------------------------ */
    620: /*MC
    621:       TSEIMEX - ODE solver using extrapolated IMEX schemes
    622:   These methods are intended for problems with well-separated time scales, especially when a slow scale is strongly nonlinear such that it is expensive to solve with a fully implicit method. The user should provide the stiff part of the equation using TSSetIFunction() and the non-stiff part with TSSetRHSFunction().

    624:    Notes:
    625:   The default is a 3-stage scheme, it can be changed with TSEIMEXSetMaxRows() or -ts_eimex_max_rows

    627:   This method currently only works with ODE, for which the stiff part G(t,X,Xdot) has the form Xdot + Ghat(t,X).

    629:   Level: beginner

    631: .seealso:  TSCreate(), TS
    632: M*/
    635: PETSC_EXTERN PetscErrorCode TSCreate_EIMEX(TS ts)
    636: {
    637:   TS_EIMEX       *ext;

    642:   ts->ops->reset          = TSReset_EIMEX;
    643:   ts->ops->destroy        = TSDestroy_EIMEX;
    644:   ts->ops->view           = TSView_EIMEX;
    645:   ts->ops->setup          = TSSetUp_EIMEX;
    646:   ts->ops->step           = TSStep_EIMEX;
    647:   ts->ops->interpolate    = TSInterpolate_EIMEX;
    648:   ts->ops->evaluatestep   = TSEvaluateStep_EIMEX;
    649:   ts->ops->setfromoptions = TSSetFromOptions_EIMEX;
    650:   ts->ops->snesfunction   = SNESTSFormFunction_EIMEX;
    651:   ts->ops->snesjacobian   = SNESTSFormJacobian_EIMEX;

    653:   PetscNewLog(ts,&ext);
    654:   ts->data = (void*)ext;

    656:   ext->ord_adapt = PETSC_FALSE; /* By default, no order adapativity */
    657:   ext->row_ind   = -1;
    658:   ext->col_ind   = -1;
    659:   ext->max_rows  = TSEIMEXDefault;
    660:   ext->nstages   = TSEIMEXDefault;

    662:   PetscObjectComposeFunction((PetscObject)ts,"TSEIMEXSetMaxRows_C", TSEIMEXSetMaxRows_EIMEX);
    663:   PetscObjectComposeFunction((PetscObject)ts,"TSEIMEXSetRowCol_C",  TSEIMEXSetRowCol_EIMEX);
    664:   PetscObjectComposeFunction((PetscObject)ts,"TSEIMEXSetOrdAdapt_C",TSEIMEXSetOrdAdapt_EIMEX);
    665:   return(0);
    666: }