Actual source code: ex22.c
petsc-dev 2014-02-02
1: static const char help[] = "Time-dependent advection-reaction PDE in 1d, demonstrates IMEX methods.\n";
2: /*
3: u_t + a1*u_x = -k1*u + k2*v + s1
4: v_t + a2*v_x = k1*u - k2*v + s2
5: 0 < x < 1;
6: a1 = 1, k1 = 10^6, s1 = 0,
7: a2 = 0, k2 = 2*k1, s2 = 1
9: Initial conditions:
10: u(x,0) = 1 + s2*x
11: v(x,0) = k0/k1*u(x,0) + s1/k1
13: Upstream boundary conditions:
14: u(0,t) = 1-sin(12*t)^4
16: Useful command-line parameters:
17: -ts_arkimex_fully_implicit - treats advection implicitly, only relevant with -ts_type arkimex (default)
18: -ts_type rosw - use Rosenbrock-W method (linearly implicit IMEX, amortizes assembly and preconditioner setup)
19: */
21: #include <petscdmda.h>
22: #include <petscts.h>
24: typedef PetscScalar Field[2];
26: typedef struct _User *User;
27: struct _User {
28: PetscReal a[2]; /* Advection speeds */
29: PetscReal k[2]; /* Reaction coefficients */
30: PetscReal s[2]; /* Source terms */
31: };
33: static PetscErrorCode FormRHSFunction(TS,PetscReal,Vec,Vec,void*);
34: static PetscErrorCode FormIFunction(TS,PetscReal,Vec,Vec,Vec,void*);
35: static PetscErrorCode FormIJacobian(TS,PetscReal,Vec,Vec,PetscReal,Mat*,Mat*,MatStructure*,void*);
36: static PetscErrorCode FormInitialSolution(TS,Vec,void*);
40: int main(int argc,char **argv)
41: {
42: TS ts; /* time integrator */
43: SNES snes; /* nonlinear solver */
44: SNESLineSearch linesearch; /* line search */
45: Vec X; /* solution, residual vectors */
46: Mat J; /* Jacobian matrix */
47: PetscInt steps,maxsteps,mx;
48: PetscErrorCode ierr;
49: DM da;
50: PetscReal ftime,dt;
51: struct _User user; /* user-defined work context */
52: TSConvergedReason reason;
54: PetscInitialize(&argc,&argv,(char*)0,help);
56: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57: Create distributed array (DMDA) to manage parallel grid and vectors
58: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
59: DMDACreate1d(PETSC_COMM_WORLD,DMDA_BOUNDARY_NONE,-11,2,2,NULL,&da);
61: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62: Extract global vectors from DMDA;
63: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
64: DMCreateGlobalVector(da,&X);
66: /* Initialize user application context */
67: PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Advection-reaction options","");
68: {
69: user.a[0] = 1; PetscOptionsReal("-a0","Advection rate 0","",user.a[0],&user.a[0],NULL);
70: user.a[1] = 0; PetscOptionsReal("-a1","Advection rate 1","",user.a[1],&user.a[1],NULL);
71: user.k[0] = 1e6; PetscOptionsReal("-k0","Reaction rate 0","",user.k[0],&user.k[0],NULL);
72: user.k[1] = 2*user.k[0]; PetscOptionsReal("-k1","Reaction rate 1","",user.k[1],&user.k[1],NULL);
73: user.s[0] = 0; PetscOptionsReal("-s0","Source 0","",user.s[0],&user.s[0],NULL);
74: user.s[1] = 1; PetscOptionsReal("-s1","Source 1","",user.s[1],&user.s[1],NULL);
75: }
76: PetscOptionsEnd();
78: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79: Create timestepping solver context
80: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
81: TSCreate(PETSC_COMM_WORLD,&ts);
82: TSSetDM(ts,da);
83: TSSetType(ts,TSARKIMEX);
84: TSSetRHSFunction(ts,NULL,FormRHSFunction,&user);
85: TSSetIFunction(ts,NULL,FormIFunction,&user);
86: DMSetMatType(da,MATAIJ);
87: DMCreateMatrix(da,&J);
88: TSSetIJacobian(ts,J,J,FormIJacobian,&user);
90: /* A line search in the nonlinear solve can fail due to ill-conditioning unless an absolute tolerance is set. Since
91: * this problem is linear, we deactivate the line search. For a linear problem, it is usually recommended to also use
92: * SNESSetType(snes,SNESKSPONLY). */
93: TSGetSNES(ts,&snes);
94: SNESGetLineSearch(snes,&linesearch);
95: SNESLineSearchSetType(linesearch,SNESLINESEARCHBASIC);
97: ftime = 1.0;
98: maxsteps = 10000;
99: TSSetDuration(ts,maxsteps,ftime);
101: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102: Set initial conditions
103: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
104: FormInitialSolution(ts,X,&user);
105: TSSetSolution(ts,X);
106: VecGetSize(X,&mx);
107: dt = .1 * PetscMax(user.a[0],user.a[1]) / mx; /* Advective CFL, I don't know why it needs so much safety factor. */
108: TSSetInitialTimeStep(ts,0.0,dt);
110: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111: Set runtime options
112: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
113: TSSetFromOptions(ts);
115: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116: Solve nonlinear system
117: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
118: TSSolve(ts,X);
119: TSGetSolveTime(ts,&ftime);
120: TSGetTimeStepNumber(ts,&steps);
121: TSGetConvergedReason(ts,&reason);
122: PetscPrintf(PETSC_COMM_WORLD,"%s at time %g after %D steps\n",TSConvergedReasons[reason],(double)ftime,steps);
124: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125: Free work space.
126: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
127: MatDestroy(&J);
128: VecDestroy(&X);
129: TSDestroy(&ts);
130: DMDestroy(&da);
131: PetscFinalize();
132: return 0;
133: }
137: static PetscErrorCode FormIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ptr)
138: {
139: User user = (User)ptr;
140: DM da;
141: DMDALocalInfo info;
142: PetscInt i;
143: Field *x,*xdot,*f;
147: TSGetDM(ts,&da);
148: DMDAGetLocalInfo(da,&info);
150: /* Get pointers to vector data */
151: DMDAVecGetArray(da,X,&x);
152: DMDAVecGetArray(da,Xdot,&xdot);
153: DMDAVecGetArray(da,F,&f);
155: /* Compute function over the locally owned part of the grid */
156: for (i=info.xs; i<info.xs+info.xm; i++) {
157: f[i][0] = xdot[i][0] + user->k[0]*x[i][0] - user->k[1]*x[i][1] - user->s[0];
158: f[i][1] = xdot[i][1] - user->k[0]*x[i][0] + user->k[1]*x[i][1] - user->s[1];
159: }
161: /* Restore vectors */
162: DMDAVecRestoreArray(da,X,&x);
163: DMDAVecRestoreArray(da,Xdot,&xdot);
164: DMDAVecRestoreArray(da,F,&f);
165: return(0);
166: }
170: static PetscErrorCode FormRHSFunction(TS ts,PetscReal t,Vec X,Vec F,void *ptr)
171: {
172: User user = (User)ptr;
173: DM da;
174: Vec Xloc;
175: DMDALocalInfo info;
176: PetscInt i,j;
177: PetscReal hx;
178: Field *x,*f;
182: TSGetDM(ts,&da);
183: DMDAGetLocalInfo(da,&info);
184: hx = 1.0/(PetscReal)info.mx;
186: /*
187: Scatter ghost points to local vector,using the 2-step process
188: DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
189: By placing code between these two statements, computations can be
190: done while messages are in transition.
191: */
192: DMGetLocalVector(da,&Xloc);
193: DMGlobalToLocalBegin(da,X,INSERT_VALUES,Xloc);
194: DMGlobalToLocalEnd(da,X,INSERT_VALUES,Xloc);
196: /* Get pointers to vector data */
197: DMDAVecGetArray(da,Xloc,&x);
198: DMDAVecGetArray(da,F,&f);
200: /* Compute function over the locally owned part of the grid */
201: for (i=info.xs; i<info.xs+info.xm; i++) {
202: const PetscReal *a = user->a;
203: PetscReal u0t[2] = {1. - PetscPowRealInt(PetscSinReal(12*t),4),0};
204: for (j=0; j<2; j++) {
205: if (i == 0) f[i][j] = a[j]/hx*(1./3*u0t[j] + 0.5*x[i][j] - x[i+1][j] + 1./6*x[i+2][j]);
206: else if (i == 1) f[i][j] = a[j]/hx*(-1./12*u0t[j] + 2./3*x[i-1][j] - 2./3*x[i+1][j] + 1./12*x[i+2][j]);
207: else if (i == info.mx-2) f[i][j] = a[j]/hx*(-1./6*x[i-2][j] + x[i-1][j] - 0.5*x[i][j] - 1./3*x[i+1][j]);
208: else if (i == info.mx-1) f[i][j] = a[j]/hx*(-x[i][j] + x[i-1][j]);
209: else f[i][j] = a[j]/hx*(-1./12*x[i-2][j] + 2./3*x[i-1][j] - 2./3*x[i+1][j] + 1./12*x[i+2][j]);
210: }
211: }
213: /* Restore vectors */
214: DMDAVecRestoreArray(da,Xloc,&x);
215: DMDAVecRestoreArray(da,F,&f);
216: DMRestoreLocalVector(da,&Xloc);
217: return(0);
218: }
220: /* --------------------------------------------------------------------- */
221: /*
222: IJacobian - Compute IJacobian = dF/dU + a dF/dUdot
223: */
226: PetscErrorCode FormIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal a,Mat *J,Mat *Jpre,MatStructure *str,void *ptr)
227: {
228: User user = (User)ptr;
230: DMDALocalInfo info;
231: PetscInt i;
232: DM da;
233: Field *x,*xdot;
236: TSGetDM(ts,&da);
237: DMDAGetLocalInfo(da,&info);
239: /* Get pointers to vector data */
240: DMDAVecGetArray(da,X,&x);
241: DMDAVecGetArray(da,Xdot,&xdot);
243: /* Compute function over the locally owned part of the grid */
244: for (i=info.xs; i<info.xs+info.xm; i++) {
245: const PetscReal *k = user->k;
246: PetscScalar v[2][2];
248: v[0][0] = a + k[0]; v[0][1] = -k[1];
249: v[1][0] = -k[0]; v[1][1] = a+k[1];
250: MatSetValuesBlocked(*Jpre,1,&i,1,&i,&v[0][0],INSERT_VALUES);
251: }
253: /* Restore vectors */
254: DMDAVecRestoreArray(da,X,&x);
255: DMDAVecRestoreArray(da,Xdot,&xdot);
257: MatAssemblyBegin(*Jpre,MAT_FINAL_ASSEMBLY);
258: MatAssemblyEnd(*Jpre,MAT_FINAL_ASSEMBLY);
259: if (*J != *Jpre) {
260: MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
261: MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
262: }
263: return(0);
264: }
268: PetscErrorCode FormInitialSolution(TS ts,Vec X,void *ctx)
269: {
270: User user = (User)ctx;
271: DM da;
272: PetscInt i;
273: DMDALocalInfo info;
274: Field *x;
275: PetscReal hx;
279: TSGetDM(ts,&da);
280: DMDAGetLocalInfo(da,&info);
281: hx = 1.0/(PetscReal)info.mx;
283: /* Get pointers to vector data */
284: DMDAVecGetArray(da,X,&x);
286: /* Compute function over the locally owned part of the grid */
287: for (i=info.xs; i<info.xs+info.xm; i++) {
288: PetscReal r = (i+1)*hx,ik = user->k[1] != 0.0 ? 1.0/user->k[1] : 1.0;
289: x[i][0] = 1 + user->s[1]*r;
290: x[i][1] = user->k[0]*ik*x[i][0] + user->s[1]*ik;
291: }
292: DMDAVecRestoreArray(da,X,&x);
293: return(0);
294: }