Actual source code: ex25.c
petsc-3.4.5 2014-06-29
1: static const char help[] = "Call PetscInitialize multiple times.\n";
2: /*
3: This example is based on the Brusselator tutorial of the same name, but tests multiple calls to PetscInitialize().
4: This is a bad "convergence study" because it only compares min and max values of the solution rather than comparing
5: norms of the errors. For convergence studies, we recommend invoking PetscInitialize() only once and comparing norms
6: of errors (perhaps estimated using an accurate reference solution).
8: Time-dependent Brusselator reaction-diffusion PDE in 1d. Demonstrates IMEX methods and multiple solves.
10: u_t - alpha u_xx = A + u^2 v - (B+1) u
11: v_t - alpha v_xx = B u - u^2 v
12: 0 < x < 1;
13: A = 1, B = 3, alpha = 1/10
15: Initial conditions:
16: u(x,0) = 1 + sin(2 pi x)
17: v(x,0) = 3
19: Boundary conditions:
20: u(0,t) = u(1,t) = 1
21: v(0,t) = v(1,t) = 3
22: */
24: #include <petscdmda.h>
25: #include <petscts.h>
27: typedef struct {
28: PetscScalar u,v;
29: } Field;
31: typedef struct _User *User;
32: struct _User {
33: PetscReal A,B; /* Reaction coefficients */
34: PetscReal alpha; /* Diffusion coefficient */
35: PetscReal uleft,uright; /* Dirichlet boundary conditions */
36: PetscReal vleft,vright; /* Dirichlet boundary conditions */
37: };
39: static PetscErrorCode FormRHSFunction(TS,PetscReal,Vec,Vec,void*);
40: static PetscErrorCode FormIFunction(TS,PetscReal,Vec,Vec,Vec,void*);
41: static PetscErrorCode FormIJacobian(TS,PetscReal,Vec,Vec,PetscReal,Mat*,Mat*,MatStructure*,void*);
42: static PetscErrorCode FormInitialSolution(TS,Vec,void*);
43: static int Brusselator(int,char**,PetscInt);
47: int main(int argc,char **argv)
48: {
49: PetscInt cycle;
50: MPI_Init(&argc,&argv);
51: for (cycle=0; cycle<4; cycle++) {
52: Brusselator(argc,argv,cycle);
53: }
54: MPI_Finalize();
55: return 0;
56: }
60: int Brusselator(int argc,char **argv,PetscInt cycle)
61: {
62: TS ts; /* nonlinear solver */
63: Vec X; /* solution, residual vectors */
64: Mat J; /* Jacobian matrix */
65: PetscInt steps,maxsteps,mx;
66: PetscErrorCode ierr;
67: DM da;
68: PetscReal ftime,hx,dt,xmax,xmin;
69: struct _User user; /* user-defined work context */
70: TSConvergedReason reason;
72: PetscInitialize(&argc,&argv,(char*)0,help);
74: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75: Create distributed array (DMDA) to manage parallel grid and vectors
76: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
77: DMDACreate1d(PETSC_COMM_WORLD,DMDA_BOUNDARY_NONE,-11,2,2,NULL,&da);
79: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80: Extract global vectors from DMDA;
81: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
82: DMCreateGlobalVector(da,&X);
84: /* Initialize user application context */
85: PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Advection-reaction options","");
86: {
87: user.A = 1;
88: user.B = 3;
89: user.alpha = 0.1;
90: user.uleft = 1;
91: user.uright = 1;
92: user.vleft = 3;
93: user.vright = 3;
94: PetscOptionsReal("-A","Reaction rate","",user.A,&user.A,NULL);
95: PetscOptionsReal("-B","Reaction rate","",user.B,&user.B,NULL);
96: PetscOptionsReal("-alpha","Diffusion coefficient","",user.alpha,&user.alpha,NULL);
97: PetscOptionsReal("-uleft","Dirichlet boundary condition","",user.uleft,&user.uleft,NULL);
98: PetscOptionsReal("-uright","Dirichlet boundary condition","",user.uright,&user.uright,NULL);
99: PetscOptionsReal("-vleft","Dirichlet boundary condition","",user.vleft,&user.vleft,NULL);
100: PetscOptionsReal("-vright","Dirichlet boundary condition","",user.vright,&user.vright,NULL);
101: }
102: PetscOptionsEnd();
104: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105: Create timestepping solver context
106: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
107: TSCreate(PETSC_COMM_WORLD,&ts);
108: TSSetDM(ts,da);
109: TSSetType(ts,TSARKIMEX);
110: TSSetRHSFunction(ts,NULL,FormRHSFunction,&user);
111: TSSetIFunction(ts,NULL,FormIFunction,&user);
112: DMCreateMatrix(da,MATAIJ,&J);
113: TSSetIJacobian(ts,J,J,FormIJacobian,&user);
115: ftime = 1.0;
116: maxsteps = 10000;
117: TSSetDuration(ts,maxsteps,ftime);
119: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120: Set initial conditions
121: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
122: FormInitialSolution(ts,X,&user);
123: TSSetSolution(ts,X);
124: VecGetSize(X,&mx);
125: hx = 1.0/(PetscReal)(mx/2-1);
126: dt = 0.4 * PetscSqr(hx) / user.alpha; /* Diffusive stability limit */
127: dt *= PetscPowRealInt(0.2,cycle); /* Shrink the time step in convergence study. */
128: TSSetInitialTimeStep(ts,0.0,dt);
129: TSSetTolerances(ts,1e-3*PetscPowRealInt(0.5,cycle),NULL,1e-3*PetscPowRealInt(0.5,cycle),NULL);
131: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
132: Set runtime options
133: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
134: TSSetFromOptions(ts);
136: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137: Solve nonlinear system
138: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
139: TSSolve(ts,X);
140: TSGetSolveTime(ts,&ftime);
141: TSGetTimeStepNumber(ts,&steps);
142: TSGetConvergedReason(ts,&reason);
143: VecMin(X,NULL,&xmin);
144: VecMax(X,NULL,&xmax);
145: PetscPrintf(PETSC_COMM_WORLD,"%s at time %G after % 3D steps. Range [%6.4f,%6.4f]\n",TSConvergedReasons[reason],ftime,steps,(double)xmin,(double)xmax);
147: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
148: Free work space.
149: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
150: MatDestroy(&J);
151: VecDestroy(&X);
152: TSDestroy(&ts);
153: DMDestroy(&da);
154: PetscFinalize();
155: return 0;
156: }
160: static PetscErrorCode FormIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ptr)
161: {
162: User user = (User)ptr;
163: DM da;
164: DMDALocalInfo info;
165: PetscInt i;
166: Field *x,*xdot,*f;
167: PetscReal hx;
168: Vec Xloc;
172: TSGetDM(ts,&da);
173: DMDAGetLocalInfo(da,&info);
174: hx = 1.0/(PetscReal)(info.mx-1);
176: /*
177: Scatter ghost points to local vector,using the 2-step process
178: DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
179: By placing code between these two statements, computations can be
180: done while messages are in transition.
181: */
182: DMGetLocalVector(da,&Xloc);
183: DMGlobalToLocalBegin(da,X,INSERT_VALUES,Xloc);
184: DMGlobalToLocalEnd(da,X,INSERT_VALUES,Xloc);
186: /* Get pointers to vector data */
187: DMDAVecGetArray(da,Xloc,&x);
188: DMDAVecGetArray(da,Xdot,&xdot);
189: DMDAVecGetArray(da,F,&f);
191: /* Compute function over the locally owned part of the grid */
192: for (i=info.xs; i<info.xs+info.xm; i++) {
193: if (i == 0) {
194: f[i].u = hx * (x[i].u - user->uleft);
195: f[i].v = hx * (x[i].v - user->vleft);
196: } else if (i == info.mx-1) {
197: f[i].u = hx * (x[i].u - user->uright);
198: f[i].v = hx * (x[i].v - user->vright);
199: } else {
200: f[i].u = hx * xdot[i].u - user->alpha * (x[i-1].u - 2.*x[i].u + x[i+1].u) / hx;
201: f[i].v = hx * xdot[i].v - user->alpha * (x[i-1].v - 2.*x[i].v + x[i+1].v) / hx;
202: }
203: }
205: /* Restore vectors */
206: DMDAVecRestoreArray(da,Xloc,&x);
207: DMDAVecRestoreArray(da,Xdot,&xdot);
208: DMDAVecRestoreArray(da,F,&f);
209: DMRestoreLocalVector(da,&Xloc);
210: return(0);
211: }
215: static PetscErrorCode FormRHSFunction(TS ts,PetscReal t,Vec X,Vec F,void *ptr)
216: {
217: User user = (User)ptr;
218: DM da;
219: DMDALocalInfo info;
220: PetscInt i;
221: PetscReal hx;
222: Field *x,*f;
226: TSGetDM(ts,&da);
227: DMDAGetLocalInfo(da,&info);
228: hx = 1.0/(PetscReal)(info.mx-1);
230: /* Get pointers to vector data */
231: DMDAVecGetArray(da,X,&x);
232: DMDAVecGetArray(da,F,&f);
234: /* Compute function over the locally owned part of the grid */
235: for (i=info.xs; i<info.xs+info.xm; i++) {
236: PetscScalar u = x[i].u,v = x[i].v;
237: f[i].u = hx*(user->A + u*u*v - (user->B+1)*u);
238: f[i].v = hx*(user->B*u - u*u*v);
239: }
241: /* Restore vectors */
242: DMDAVecRestoreArray(da,X,&x);
243: DMDAVecRestoreArray(da,F,&f);
244: return(0);
245: }
247: /* --------------------------------------------------------------------- */
248: /*
249: IJacobian - Compute IJacobian = dF/dU + a dF/dUdot
250: */
253: PetscErrorCode FormIJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal a,Mat *J,Mat *Jpre,MatStructure *str,void *ptr)
254: {
255: User user = (User)ptr;
257: DMDALocalInfo info;
258: PetscInt i;
259: PetscReal hx;
260: DM da;
261: Field *x,*xdot;
264: TSGetDM(ts,&da);
265: DMDAGetLocalInfo(da,&info);
266: hx = 1.0/(PetscReal)(info.mx-1);
268: /* Get pointers to vector data */
269: DMDAVecGetArray(da,X,&x);
270: DMDAVecGetArray(da,Xdot,&xdot);
272: /* Compute function over the locally owned part of the grid */
273: for (i=info.xs; i<info.xs+info.xm; i++) {
274: if (i == 0 || i == info.mx-1) {
275: const PetscInt row = i,col = i;
276: const PetscScalar vals[2][2] = {{hx,0},{0,hx}};
277: MatSetValuesBlocked(*Jpre,1,&row,1,&col,&vals[0][0],INSERT_VALUES);
278: } else {
279: const PetscInt row = i,col[] = {i-1,i,i+1};
280: const PetscScalar dxxL = -user->alpha/hx,dxx0 = 2.*user->alpha/hx,dxxR = -user->alpha/hx;
281: const PetscScalar vals[2][3][2] = {{{dxxL,0},{a *hx+dxx0,0},{dxxR,0}},
282: {{0,dxxL},{0,a*hx+dxx0},{0,dxxR}}};
283: MatSetValuesBlocked(*Jpre,1,&row,3,col,&vals[0][0][0],INSERT_VALUES);
284: }
285: }
287: /* Restore vectors */
288: DMDAVecRestoreArray(da,X,&x);
289: DMDAVecRestoreArray(da,Xdot,&xdot);
291: MatAssemblyBegin(*Jpre,MAT_FINAL_ASSEMBLY);
292: MatAssemblyEnd(*Jpre,MAT_FINAL_ASSEMBLY);
293: if (*J != *Jpre) {
294: MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
295: MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
296: }
297: return(0);
298: }
302: PetscErrorCode FormInitialSolution(TS ts,Vec X,void *ctx)
303: {
304: User user = (User)ctx;
305: DM da;
306: PetscInt i;
307: DMDALocalInfo info;
308: Field *x;
309: PetscReal hx;
313: TSGetDM(ts,&da);
314: DMDAGetLocalInfo(da,&info);
315: hx = 1.0/(PetscReal)(info.mx-1);
317: /* Get pointers to vector data */
318: DMDAVecGetArray(da,X,&x);
320: /* Compute function over the locally owned part of the grid */
321: for (i=info.xs; i<info.xs+info.xm; i++) {
322: PetscReal xi = i*hx;
323: x[i].u = user->uleft*(1.-xi) + user->uright*xi + sin(2.*PETSC_PI*xi);
324: x[i].v = user->vleft*(1.-xi) + user->vright*xi;
325: }
326: DMDAVecRestoreArray(da,X,&x);
327: return(0);
328: }