Actual source code: ex2.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  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 =
 12: */


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

 26: typedef struct {
 27:   PetscScalar k1,k2,k3;
 28:   PetscScalar sigma2;
 29:   Vec         initialsolution;
 30: } AppCtx;

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

 42: static PetscErrorCode IFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,AppCtx *ctx)
 43: {
 45:   PetscScalar    *u,*udot,*f;

 48:   VecGetArray(U,&u);
 49:   VecGetArray(Udot,&udot);
 50:   VecGetArray(F,&f);
 51:   f[0] = udot[0] - k1(ctx,t)*u[2] + ctx->k2*u[0];
 52:   f[1] = udot[1] - k1(ctx,t)*u[2] + ctx->k3*u[1]*u[3] - ctx->sigma2;
 53:   f[2] = udot[2] - ctx->k3*u[1]*u[3] + k1(ctx,t)*u[2];
 54:   f[3] = udot[3] - ctx->k2*u[0] + ctx->k3*u[1]*u[3];
 55:   VecRestoreArray(U,&u);
 56:   VecRestoreArray(Udot,&udot);
 57:   VecRestoreArray(F,&f);
 58:   return(0);
 59: }

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

 70:   VecGetArray(U,&u);
 71:   VecGetArray(Udot,&udot);
 72:   J[0][0] = a + ctx->k2;   J[0][1] = 0.0;                J[0][2] = -k1(ctx,t);       J[0][3] = 0.0;
 73:   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];
 74:   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];
 75:   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];
 76:   MatSetValues(B,4,rowcol,4,rowcol,&J[0][0],INSERT_VALUES);
 77:   VecRestoreArray(U,&u);
 78:   VecRestoreArray(Udot,&udot);

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

 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: }

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

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

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

129:   MatGetVecs(A,&U,NULL);

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

136:   VecDuplicate(U,&ctx.initialsolution);
137:   VecGetArray(ctx.initialsolution,&u);
138:   u[0] = 0.0;
139:   u[1] = 1.3e8;
140:   u[2] = 5.0e11;
141:   u[3] = 8.0e11;
142:   VecRestoreArray(ctx.initialsolution,&u);

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

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

160:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
161:      Set solver options
162:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
163:   TSSetDuration(ts,1000000,518400.0);
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(0);
184: }