-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.c
319 lines (295 loc) · 13 KB
/
pattern.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
static char help[] =
"Coupled reaction-diffusion equations (Pearson 1993). Option prefix -ptn_.\n"
"Demonstrates form F(t,Y,dot Y) = G(t,Y) where F() is IFunction and G() is\n"
"RHSFunction(). Implements IJacobian() and RHSJacobian(). Defaults to\n"
"ARKIMEX (= adaptive Runge-Kutta implicit-explicit) TS type.\n\n";
#include <petsc.h>
typedef struct {
PetscReal u, v;
} Field;
typedef struct {
PetscReal L, // domain side length
Du, // diffusion coefficient: u equation
Dv, // v equation
phi, // "dimensionless feed rate" (F in Pearson 1993)
kappa; // "dimensionless rate constant" (k in Pearson 1993)
PetscBool IFcn_called, IJac_called, RHSFcn_called, RHSJac_called;
} PatternCtx;
extern PetscErrorCode InitialState(DM, Vec, PetscReal, PatternCtx*);
extern PetscErrorCode FormRHSFunctionLocal(DMDALocalInfo*, PetscReal, Field**,
Field**, PatternCtx*);
extern PetscErrorCode FormRHSJacobianLocal(DMDALocalInfo*, PetscReal, Field**,
Mat, Mat, PatternCtx*);
extern PetscErrorCode FormIFunctionLocal(DMDALocalInfo*, PetscReal, Field**, Field**,
Field **, PatternCtx*);
extern PetscErrorCode FormIJacobianLocal(DMDALocalInfo*, PetscReal, Field**, Field**,
PetscReal, Mat, Mat, PatternCtx*);
int main(int argc,char **argv)
{
PatternCtx user;
TS ts;
Vec x;
DM da;
DMDALocalInfo info;
PetscReal noiselevel = -1.0; // negative value means no initial noise
PetscBool no_rhsjacobian = PETSC_FALSE,
no_ijacobian = PETSC_FALSE,
call_back_report = PETSC_FALSE;
TSType type;
PetscCall(PetscInitialize(&argc,&argv,NULL,help));
// parameter values from pages 21-22 in Hundsdorfer & Verwer (2003)
user.L = 2.5;
user.Du = 8.0e-5;
user.Dv = 4.0e-5;
user.phi = 0.024;
user.kappa = 0.06;
user.IFcn_called = PETSC_FALSE;
user.IJac_called = PETSC_FALSE;
user.RHSFcn_called = PETSC_FALSE;
user.RHSJac_called = PETSC_FALSE;
PetscOptionsBegin(PETSC_COMM_WORLD, "ptn_", "options for patterns", "");
PetscCall(PetscOptionsBool("-call_back_report","report on which user-supplied call-backs were actually called",
"pattern.c",call_back_report,&(call_back_report),NULL));
PetscCall(PetscOptionsReal("-Du","diffusion coefficient of first equation",
"pattern.c",user.Du,&user.Du,NULL));
PetscCall(PetscOptionsReal("-Dv","diffusion coefficient of second equation",
"pattern.c",user.Dv,&user.Dv,NULL));
PetscCall(PetscOptionsReal("-kappa","dimensionless rate constant (=k in (Pearson, 1993))",
"pattern.c",user.kappa,&user.kappa,NULL));
PetscCall(PetscOptionsReal("-L","square domain side length; recommend L >= 0.5",
"pattern.c",user.L,&user.L,NULL));
PetscCall(PetscOptionsBool("-no_ijacobian","do not set call-back DMDATSSetIJacobian()",
"pattern.c",no_ijacobian,&(no_ijacobian),NULL));
PetscCall(PetscOptionsBool("-no_rhsjacobian","do not set call-back DMDATSSetRHSJacobian()",
"pattern.c",no_rhsjacobian,&(no_rhsjacobian),NULL));
PetscCall(PetscOptionsReal("-noisy_init",
"initialize u,v with this much random noise (e.g. 0.2) on top of usual initial values",
"pattern.c",noiselevel,&noiselevel,NULL));
PetscCall(PetscOptionsReal("-phi","dimensionless feed rate (=F in (Pearson, 1993))",
"pattern.c",user.phi,&user.phi,NULL));
PetscOptionsEnd();
PetscCall(DMDACreate2d(PETSC_COMM_WORLD,
DM_BOUNDARY_PERIODIC, DM_BOUNDARY_PERIODIC,
DMDA_STENCIL_BOX, // for 9-point stencil
3,3,PETSC_DECIDE,PETSC_DECIDE,
2, 1, // degrees of freedom, stencil width
NULL,NULL,&da));
PetscCall(DMSetFromOptions(da));
PetscCall(DMSetUp(da));
PetscCall(DMDASetFieldName(da,0,"u"));
PetscCall(DMDASetFieldName(da,1,"v"));
PetscCall(DMDAGetLocalInfo(da,&info));
if (info.mx != info.my) {
SETERRQ(PETSC_COMM_SELF,1,"pattern.c requires mx == my");
}
PetscCall(DMDASetUniformCoordinates(da, 0.0, user.L, 0.0, user.L, -1.0, -1.0));
PetscCall(PetscPrintf(PETSC_COMM_WORLD,
"running on %d x %d grid with square cells of side h = %.6f ...\n",
info.mx,info.my,user.L/(PetscReal)(info.mx)));
//STARTTSSETUP
PetscCall(TSCreate(PETSC_COMM_WORLD,&ts));
PetscCall(TSSetProblemType(ts,TS_NONLINEAR));
PetscCall(TSSetDM(ts,da));
PetscCall(TSSetApplicationContext(ts,&user));
PetscCall(DMDATSSetRHSFunctionLocal(da,INSERT_VALUES,
(DMDATSRHSFunctionLocal)FormRHSFunctionLocal,&user));
if (!no_rhsjacobian) {
PetscCall(DMDATSSetRHSJacobianLocal(da,
(DMDATSRHSJacobianLocal)FormRHSJacobianLocal,&user));
}
PetscCall(DMDATSSetIFunctionLocal(da,INSERT_VALUES,
(DMDATSIFunctionLocal)FormIFunctionLocal,&user));
if (!no_ijacobian) {
PetscCall(DMDATSSetIJacobianLocal(da,
(DMDATSIJacobianLocal)FormIJacobianLocal,&user));
}
PetscCall(TSSetType(ts,TSARKIMEX));
PetscCall(TSSetTime(ts,0.0));
PetscCall(TSSetMaxTime(ts,200.0));
PetscCall(TSSetTimeStep(ts,5.0));
PetscCall(TSSetExactFinalTime(ts,TS_EXACTFINALTIME_MATCHSTEP));
PetscCall(TSSetFromOptions(ts));
//ENDTSSETUP
PetscCall(DMCreateGlobalVector(da,&x));
PetscCall(InitialState(da,x,noiselevel,&user));
PetscCall(TSSolve(ts,x));
// optionally report on call-backs
if (call_back_report) {
PetscCall(TSGetType(ts,&type));
PetscCall(PetscPrintf(PETSC_COMM_WORLD,"CALL-BACK REPORT\n solver type: %s\n",type));
PetscCall(PetscPrintf(PETSC_COMM_WORLD," IFunction: %d | IJacobian: %d\n",
(int)user.IFcn_called,(int)user.IJac_called));
PetscCall(PetscPrintf(PETSC_COMM_WORLD," RHSFunction: %d | RHSJacobian: %d\n",
(int)user.RHSFcn_called,(int)user.RHSJac_called));
}
PetscCall(VecDestroy(&x));
PetscCall(TSDestroy(&ts));
PetscCall(DMDestroy(&da));
PetscCall(PetscFinalize());
return 0;
}
// Formulas from page 22 of Hundsdorfer & Verwer (2003). Interpretation here is
// to always generate 0.5 x 0.5 non-trivial patch in (0,L) x (0,L) domain.
PetscErrorCode InitialState(DM da, Vec Y, PetscReal noiselevel, PatternCtx* user) {
DMDALocalInfo info;
PetscInt i,j;
PetscReal sx,sy;
const PetscReal ledge = (user->L - 0.5) / 2.0, // nontrivial initial values on
redge = user->L - ledge; // ledge < x,y < redge
DMDACoor2d **aC;
Field **aY;
PetscCall(VecSet(Y,0.0));
if (noiselevel > 0.0) {
// noise added to usual initial condition is uniform on [0,noiselevel],
// independently for each location and component
PetscCall(VecSetRandom(Y,NULL));
PetscCall(VecScale(Y,noiselevel));
}
PetscCall(DMDAGetLocalInfo(da,&info));
PetscCall(DMDAGetCoordinateArray(da,&aC));
PetscCall(DMDAVecGetArray(da,Y,&aY));
for (j = info.ys; j < info.ys+info.ym; j++) {
for (i = info.xs; i < info.xs+info.xm; i++) {
if ((aC[j][i].x >= ledge) && (aC[j][i].x <= redge)
&& (aC[j][i].y >= ledge) && (aC[j][i].y <= redge)) {
sx = PetscSinReal(4.0 * PETSC_PI * aC[j][i].x);
sy = PetscSinReal(4.0 * PETSC_PI * aC[j][i].y);
aY[j][i].v += 0.5 * sx * sx * sy * sy;
}
aY[j][i].u += 1.0 - 2.0 * aY[j][i].v;
}
}
PetscCall(DMDAVecRestoreArray(da,Y,&aY));
PetscCall(DMDARestoreCoordinateArray(da,&aC));
return 0;
}
// in system form F(t,Y,dot Y) = G(t,Y), compute G():
// G^u(t,u,v) = - u v^2 + phi (1 - u)
// G^v(t,u,v) = + u v^2 - (phi + kappa) v
//STARTRHSFUNCTION
PetscErrorCode FormRHSFunctionLocal(DMDALocalInfo *info,
PetscReal t, Field **aY, Field **aG, PatternCtx *user) {
PetscInt i, j;
PetscReal uv2;
user->RHSFcn_called = PETSC_TRUE;
for (j = info->ys; j < info->ys + info->ym; j++) {
for (i = info->xs; i < info->xs + info->xm; i++) {
uv2 = aY[j][i].u * aY[j][i].v * aY[j][i].v;
aG[j][i].u = - uv2 + user->phi * (1.0 - aY[j][i].u);
aG[j][i].v = + uv2 - (user->phi + user->kappa) * aY[j][i].v;
}
}
return 0;
}
//ENDRHSFUNCTION
PetscErrorCode FormRHSJacobianLocal(DMDALocalInfo *info,
PetscReal t, Field **aY,
Mat J, Mat P, PatternCtx *user) {
PetscInt i, j;
PetscReal v[2], uv, v2;
MatStencil col[2],row;
user->RHSJac_called = PETSC_TRUE;
for (j = info->ys; j < info->ys+info->ym; j++) {
row.j = j; col[0].j = j; col[1].j = j;
for (i = info->xs; i < info->xs+info->xm; i++) {
row.i = i; col[0].i = i; col[1].i = i;
uv = aY[j][i].u * aY[j][i].v;
v2 = aY[j][i].v * aY[j][i].v;
// u equation
row.c = 0; col[0].c = 0; col[1].c = 1;
v[0] = - v2 - user->phi;
v[1] = - 2.0 * uv;
PetscCall(MatSetValuesStencil(P,1,&row,2,col,v,INSERT_VALUES));
// v equation
row.c = 1; col[0].c = 0; col[1].c = 1;
v[0] = v2;
v[1] = 2.0 * uv - (user->phi + user->kappa);
PetscCall(MatSetValuesStencil(P,1,&row,2,col,v,INSERT_VALUES));
}
}
PetscCall(MatAssemblyBegin(P,MAT_FINAL_ASSEMBLY));
PetscCall(MatAssemblyEnd(P,MAT_FINAL_ASSEMBLY));
if (J != P) {
PetscCall(MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY));
PetscCall(MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY));
}
return 0;
}
// in system form F(t,Y,dot Y) = G(t,Y), compute F():
// F^u(t,u,v,u_t,v_t) = u_t - D_u Laplacian u
// F^v(t,u,v,u_t,v_t) = v_t - D_v Laplacian v
//STARTIFUNCTION
PetscErrorCode FormIFunctionLocal(DMDALocalInfo *info, PetscReal t,
Field **aY, Field **aYdot, Field **aF,
PatternCtx *user) {
PetscInt i, j;
const PetscReal h = user->L / (PetscReal)(info->mx),
Cu = user->Du / (6.0 * h * h),
Cv = user->Dv / (6.0 * h * h);
PetscReal u, v, lapu, lapv;
user->IFcn_called = PETSC_TRUE;
for (j = info->ys; j < info->ys + info->ym; j++) {
for (i = info->xs; i < info->xs + info->xm; i++) {
u = aY[j][i].u;
v = aY[j][i].v;
lapu = aY[j+1][i-1].u + 4.0*aY[j+1][i].u + aY[j+1][i+1].u
+ 4.0*aY[j][i-1].u - 20.0*u + 4.0*aY[j][i+1].u
+ aY[j-1][i-1].u + 4.0*aY[j-1][i].u + aY[j-1][i+1].u;
lapv = aY[j+1][i-1].v + 4.0*aY[j+1][i].v + aY[j+1][i+1].v
+ 4.0*aY[j][i-1].v - 20.0*v + 4.0*aY[j][i+1].v
+ aY[j-1][i-1].v + 4.0*aY[j-1][i].v + aY[j-1][i+1].v;
aF[j][i].u = aYdot[j][i].u - Cu * lapu;
aF[j][i].v = aYdot[j][i].v - Cv * lapv;
}
}
return 0;
}
//ENDIFUNCTION
// in system form F(t,Y,dot Y) = G(t,Y), compute combined/shifted
// Jacobian of F():
// J = (shift) dF/d(dot Y) + dF/dY
//STARTIJACOBIAN
PetscErrorCode FormIJacobianLocal(DMDALocalInfo *info,
PetscReal t, Field **aY, Field **aYdot,
PetscReal shift, Mat J, Mat P,
PatternCtx *user) {
PetscInt i, j, s, c;
const PetscReal h = user->L / (PetscReal)(info->mx),
Cu = user->Du / (6.0 * h * h),
Cv = user->Dv / (6.0 * h * h);
PetscReal val[9], CC;
MatStencil col[9], row;
PetscCall(MatZeroEntries(P)); // workaround to address PETSc issue #734
user->IJac_called = PETSC_TRUE;
for (j = info->ys; j < info->ys + info->ym; j++) {
row.j = j;
for (i = info->xs; i < info->xs + info->xm; i++) {
row.i = i;
for (c = 0; c < 2; c++) { // u,v equations are c=0,1
row.c = c;
CC = (c == 0) ? Cu : Cv;
for (s = 0; s < 9; s++)
col[s].c = c;
col[0].i = i; col[0].j = j;
val[0] = shift + 20.0 * CC;
col[1].i = i-1; col[1].j = j; val[1] = - 4.0 * CC;
col[2].i = i+1; col[2].j = j; val[2] = - 4.0 * CC;
col[3].i = i; col[3].j = j-1; val[3] = - 4.0 * CC;
col[4].i = i; col[4].j = j+1; val[4] = - 4.0 * CC;
col[5].i = i-1; col[5].j = j-1; val[5] = - CC;
col[6].i = i-1; col[6].j = j+1; val[6] = - CC;
col[7].i = i+1; col[7].j = j-1; val[7] = - CC;
col[8].i = i+1; col[8].j = j+1; val[8] = - CC;
PetscCall(MatSetValuesStencil(P,1,&row,9,col,val,INSERT_VALUES));
}
}
}
PetscCall(MatAssemblyBegin(P,MAT_FINAL_ASSEMBLY));
PetscCall(MatAssemblyEnd(P,MAT_FINAL_ASSEMBLY));
if (J != P) {
PetscCall(MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY));
PetscCall(MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY));
}
return 0;
}
//ENDIJACOBIAN