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: */
17: /*
18: Include "petscts.h" so that we can use TS solvers. Note that this
19: file automatically includes:
20: petscsys.h - base PETSc routines petscvec.h - vectors
21: petscmat.h - matrices
22: petscis.h - index sets petscksp.h - Krylov subspace methods
23: petscviewer.h - viewers petscpc.h - preconditioners
24: petscksp.h - linear solvers
25: */
27: #include <petscts.h>
29: typedef struct {
30: PetscScalar k1,k2,k3;
31: PetscScalar sigma2;
32: Vec initialsolution;
33: } AppCtx;
35: PetscScalar k1(AppCtx *ctx,PetscReal t)
36: {
37: PetscReal th = t/3600.0;
38: PetscReal barth = th - 24.0*PetscFloorReal(th/24.0);
39: if (((((PetscInt)th) % 24) < 4) || ((((PetscInt)th) % 24) >= 20)) return(1.0e-40);
40: else return(ctx->k1*PetscExpReal(7.0*PetscPowReal(PetscSinReal(.0625*PETSC_PI*(barth - 4.0)),.2)));
41: }
43: static PetscErrorCode IFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,AppCtx *ctx)
44: {
45: PetscErrorCode ierr;
46: PetscScalar *f;
47: const PetscScalar *u,*udot;
50: VecGetArrayRead(U,&u);
51: VecGetArrayRead(Udot,&udot);
52: VecGetArrayWrite(F,&f);
53: f[0] = udot[0] - k1(ctx,t)*u[2] + ctx->k2*u[0];
54: f[1] = udot[1] - k1(ctx,t)*u[2] + ctx->k3*u[1]*u[3] - ctx->sigma2;
55: f[2] = udot[2] - ctx->k3*u[1]*u[3] + k1(ctx,t)*u[2];
56: f[3] = udot[3] - ctx->k2*u[0] + ctx->k3*u[1]*u[3];
57: VecRestoreArrayRead(U,&u);
58: VecRestoreArrayRead(Udot,&udot);
59: VecRestoreArrayWrite(F,&f);
60: return(0);
61: }
63: static PetscErrorCode IJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal a,Mat A,Mat B,AppCtx *ctx)
64: {
65: PetscErrorCode ierr;
66: PetscInt rowcol[] = {0,1,2,3};
67: PetscScalar J[4][4];
68: const PetscScalar *u,*udot;
71: VecGetArrayRead(U,&u);
72: VecGetArrayRead(Udot,&udot);
73: J[0][0] = a + ctx->k2; J[0][1] = 0.0; J[0][2] = -k1(ctx,t); J[0][3] = 0.0;
74: 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];
75: 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];
76: 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];
77: MatSetValues(B,4,rowcol,4,rowcol,&J[0][0],INSERT_VALUES);
78: VecRestoreArrayRead(U,&u);
79: VecRestoreArrayRead(Udot,&udot);
81: MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
82: MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
83: if (A != B) {
84: MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
85: MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
86: }
87: return(0);
88: }
90: static PetscErrorCode Solution(TS ts,PetscReal t,Vec U,AppCtx *ctx)
91: {
95: VecCopy(ctx->initialsolution,U);
96: if (t > 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Solution not given");
97: return(0);
98: }
100: int main(int argc,char **argv)
101: {
102: TS ts; /* ODE integrator */
103: Vec U; /* solution */
104: Mat A; /* Jacobian matrix */
106: PetscMPIInt size;
107: PetscInt n = 4;
108: AppCtx ctx;
109: PetscScalar *u;
111: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112: Initialize program
113: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
114: PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
115: MPI_Comm_size(PETSC_COMM_WORLD,&size);
116: if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Only for sequential runs");
118: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
119: Create necessary matrix and vectors
120: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
121: MatCreate(PETSC_COMM_WORLD,&A);
122: MatSetSizes(A,n,n,PETSC_DETERMINE,PETSC_DETERMINE);
123: MatSetFromOptions(A);
124: MatSetUp(A);
126: MatCreateVecs(A,&U,NULL);
128: ctx.k1 = 1.0e-5;
129: ctx.k2 = 1.0e5;
130: ctx.k3 = 1.0e-16;
131: ctx.sigma2 = 1.0e6;
133: VecDuplicate(U,&ctx.initialsolution);
134: VecGetArrayWrite(ctx.initialsolution,&u);
135: u[0] = 0.0;
136: u[1] = 1.3e8;
137: u[2] = 5.0e11;
138: u[3] = 8.0e11;
139: VecRestoreArrayWrite(ctx.initialsolution,&u);
141: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
142: Create timestepping solver context
143: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
144: TSCreate(PETSC_COMM_WORLD,&ts);
145: TSSetProblemType(ts,TS_NONLINEAR);
146: TSSetType(ts,TSROSW);
147: TSSetIFunction(ts,NULL,(TSIFunction) IFunction,&ctx);
148: TSSetIJacobian(ts,A,A,(TSIJacobian)IJacobian,&ctx);
150: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151: Set initial conditions
152: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
153: Solution(ts,0,U,&ctx);
154: TSSetTime(ts,4.0*3600);
155: TSSetTimeStep(ts,1.0);
156: TSSetSolution(ts,U);
158: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159: Set solver options
160: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161: TSSetMaxTime(ts,518400.0);
162: TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);
163: TSSetMaxStepRejections(ts,100);
164: TSSetMaxSNESFailures(ts,-1); /* unlimited */
165: TSSetFromOptions(ts);
167: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
168: Solve nonlinear system
169: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
170: TSSolve(ts,U);
172: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
173: Free work space. All PETSc objects should be destroyed when they
174: are no longer needed.
175: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
176: VecDestroy(&ctx.initialsolution);
177: MatDestroy(&A);
178: VecDestroy(&U);
179: TSDestroy(&ts);
181: PetscFinalize();
182: return ierr;
183: }
185: /*TEST
187: test:
188: args: -ts_view -ts_max_time 2.e4
189: timeoutfactor: 15
190: requires: !single
192: TEST*/