Actual source code: ex2.c

      2: static char help[] = "Reaction Equation from Chemistry\n";

      4: /*

      6:      Page 6, An example from Atomospheric Chemistry

      8:                  u_1_t =
      9:                  u_2_t =
     10:                  u_3_t =
     11:                  u_4_t =

     13:   -ts_monitor_lg_error -ts_monitor_lg_solution  -ts_view -ts_max_time 2.e4

     15: */

     18: /*
     19:    Include "petscts.h" so that we can use TS solvers.  Note that this
     20:    file automatically includes:
     21:      petscsys.h       - base PETSc routines   petscvec.h - vectors
     22:      petscmat.h - matrices
     23:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
     24:      petscviewer.h - viewers               petscpc.h  - preconditioners
     25:      petscksp.h   - linear solvers
     26: */

     28: #include <petscts.h>

     30: typedef struct {
     31:   PetscScalar k1,k2,k3;
     32:   PetscScalar sigma2;
     33:   Vec         initialsolution;
     34: } AppCtx;

     36: PetscScalar k1(AppCtx *ctx,PetscReal t)
     37: {
     38:   PetscReal th    = t/3600.0;
     39:   PetscReal barth = th - 24.0*PetscFloorReal(th/24.0);
     40:   if (((((PetscInt)th) % 24) < 4) || ((((PetscInt)th) % 24) >= 20)) return(1.0e-40);
     41:   else return(ctx->k1*PetscExpReal(7.0*PetscPowReal(PetscSinReal(.0625*PETSC_PI*(barth - 4.0)),.2)));
     42: }

     44: static PetscErrorCode IFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,AppCtx *ctx)
     45: {
     46:   PetscErrorCode    ierr;
     47:   PetscScalar       *f;
     48:   const PetscScalar *u,*udot;

     51:   VecGetArrayRead(U,&u);
     52:   VecGetArrayRead(Udot,&udot);
     53:   VecGetArrayWrite(F,&f);
     54:   f[0] = udot[0] - k1(ctx,t)*u[2] + ctx->k2*u[0];
     55:   f[1] = udot[1] - k1(ctx,t)*u[2] + ctx->k3*u[1]*u[3] - ctx->sigma2;
     56:   f[2] = udot[2] - ctx->k3*u[1]*u[3] + k1(ctx,t)*u[2];
     57:   f[3] = udot[3] - ctx->k2*u[0] + ctx->k3*u[1]*u[3];
     58:   VecRestoreArrayRead(U,&u);
     59:   VecRestoreArrayRead(Udot,&udot);
     60:   VecRestoreArrayWrite(F,&f);
     61:   return(0);
     62: }

     64: static PetscErrorCode IJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal a,Mat A,Mat B,AppCtx *ctx)
     65: {
     66:   PetscErrorCode    ierr;
     67:   PetscInt          rowcol[] = {0,1,2,3};
     68:   PetscScalar       J[4][4];
     69:   const PetscScalar *u,*udot;

     72:   VecGetArrayRead(U,&u);
     73:   VecGetArrayRead(Udot,&udot);
     74:   J[0][0] = a + ctx->k2;   J[0][1] = 0.0;                J[0][2] = -k1(ctx,t);       J[0][3] = 0.0;
     75:   J[1][0] = 0.0;           J[1][1] = a + ctx->k3*u[3];   J[1][2] = -k1(ctx,t);       J[1][3] = ctx->k3*u[1];
     76:   J[2][0] = 0.0;           J[2][1] = -ctx->k3*u[3];      J[2][2] = a + k1(ctx,t);    J[2][3] =  -ctx->k3*u[1];
     77:   J[3][0] =  -ctx->k2;     J[3][1] = ctx->k3*u[3];       J[3][2] = 0.0;              J[3][3] = a + ctx->k3*u[1];
     78:   MatSetValues(B,4,rowcol,4,rowcol,&J[0][0],INSERT_VALUES);
     79:   VecRestoreArrayRead(U,&u);
     80:   VecRestoreArrayRead(Udot,&udot);

     82:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     83:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
     84:   if (A != B) {
     85:     MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
     86:     MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
     87:   }
     88:   return(0);
     89: }

     91: static PetscErrorCode Solution(TS ts,PetscReal t,Vec U,AppCtx *ctx)
     92: {

     96:   VecCopy(ctx->initialsolution,U);
     97:   if (t > 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Solution not given");
     98:   return(0);
     99: }

    101: int main(int argc,char **argv)
    102: {
    103:   TS             ts;            /* ODE integrator */
    104:   Vec            U;             /* solution */
    105:   Mat            A;             /* Jacobian matrix */
    107:   PetscMPIInt    size;
    108:   PetscInt       n = 4;
    109:   AppCtx         ctx;
    110:   PetscScalar    *u;

    112:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    113:      Initialize program
    114:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    115:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
    116:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
    117:   if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Only for sequential runs");

    119:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    120:     Create necessary matrix and vectors
    121:     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    122:   MatCreate(PETSC_COMM_WORLD,&A);
    123:   MatSetSizes(A,n,n,PETSC_DETERMINE,PETSC_DETERMINE);
    124:   MatSetFromOptions(A);
    125:   MatSetUp(A);

    127:   MatCreateVecs(A,&U,NULL);

    129:   ctx.k1     = 1.0e-5;
    130:   ctx.k2     = 1.0e5;
    131:   ctx.k3     = 1.0e-16;
    132:   ctx.sigma2 = 1.0e6;

    134:   VecDuplicate(U,&ctx.initialsolution);
    135:   VecGetArrayWrite(ctx.initialsolution,&u);
    136:   u[0] = 0.0;
    137:   u[1] = 1.3e8;
    138:   u[2] = 5.0e11;
    139:   u[3] = 8.0e11;
    140:   VecRestoreArrayWrite(ctx.initialsolution,&u);

    142:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    143:      Create timestepping solver context
    144:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    145:   TSCreate(PETSC_COMM_WORLD,&ts);
    146:   TSSetProblemType(ts,TS_NONLINEAR);
    147:   TSSetType(ts,TSROSW);
    148:   TSSetIFunction(ts,NULL,(TSIFunction) IFunction,&ctx);
    149:   TSSetIJacobian(ts,A,A,(TSIJacobian)IJacobian,&ctx);

    151:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    152:      Set initial conditions
    153:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    154:   Solution(ts,0,U,&ctx);
    155:   TSSetTime(ts,4.0*3600);
    156:   TSSetTimeStep(ts,1.0);
    157:   TSSetSolution(ts,U);

    159:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    160:      Set solver options
    161:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    162:   TSSetMaxTime(ts,518400.0);
    163:   TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);
    164:   TSSetMaxStepRejections(ts,100);
    165:   TSSetMaxSNESFailures(ts,-1); /* unlimited */
    166:   TSSetFromOptions(ts);

    168:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    169:      Solve nonlinear system
    170:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    171:   TSSolve(ts,U);

    173:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    174:      Free work space.  All PETSc objects should be destroyed when they
    175:      are no longer needed.
    176:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    177:   VecDestroy(&ctx.initialsolution);
    178:   MatDestroy(&A);
    179:   VecDestroy(&U);
    180:   TSDestroy(&ts);

    182:   PetscFinalize();
    183:   return ierr;
    184: }

    187: /*TEST

    189:    test:
    190:      args: -ts_view -ts_max_time 2.e4
    191:      timeoutfactor: 15
    192:      requires: !single

    194: TEST*/