Actual source code: ex7.c
1: static const char help[] = "1D periodic Finite Volume solver by a particular slope limiter with semidiscrete time stepping.\n"
2: " advection - Constant coefficient scalar advection\n"
3: " u_t + (a*u)_x = 0\n"
4: " for this toy problem, we choose different meshsizes for different sub-domains, say\n"
5: " hxs = (xmax - xmin)/2.0*(hratio+1.0)/Mx, \n"
6: " hxf = (xmax - xmin)/2.0*(1.0+1.0/hratio)/Mx, \n"
7: " with x belongs to (xmin,xmax), the number of total mesh points is Mx and the ratio between the meshsize of coarse\n\n"
8: " grids and fine grids is hratio.\n"
9: " exact - Exact Riemann solver which usually needs to perform a Newton iteration to connect\n"
10: " the states across shocks and rarefactions\n"
11: " simulation - use reference solution which is generated by smaller time step size to be true solution,\n"
12: " also the reference solution should be generated by user and stored in a binary file.\n"
13: " characteristic - Limit the characteristic variables, this is usually preferred (default)\n"
14: "Several initial conditions can be chosen with -initial N\n\n"
15: "The problem size should be set with -da_grid_x M\n\n"
16: "This script choose the slope limiter by biased second-order upwind procedure which is proposed by Van Leer in 1994\n"
17: " u(x_(k+1/2),t) = u(x_k,t) + phi(x_(k+1/2),t)*(u(x_k,t)-u(x_(k-1),t)) \n"
18: " limiter phi(x_(k+1/2),t) = max(0,min(r(k+1/2),min(2,gamma(k+1/2)*r(k+1/2)+alpha(k+1/2)))) \n"
19: " r(k+1/2) = (u(x_(k+1))-u(x_k))/(u(x_k)-u(x_(k-1))) \n"
20: " alpha(k+1/2) = (h_k*h_(k+1))/(h_(k-1)+h_k)/(h_(k-1)+h_k+h_(k+1)) \n"
21: " gamma(k+1/2) = h_k*(h_(k-1)+h_k)/(h_k+h_(k+1))/(h_(k-1)+h_k+h_(k+1)) \n";
23: #include <petscts.h>
24: #include <petscdm.h>
25: #include <petscdmda.h>
26: #include <petscdraw.h>
27: #include <petscmath.h>
29: static inline PetscReal RangeMod(PetscReal a, PetscReal xmin, PetscReal xmax)
30: {
31: PetscReal range = xmax - xmin;
32: return xmin + PetscFmodReal(range + PetscFmodReal(a, range), range);
33: }
35: /* --------------------------------- Finite Volume data structures ----------------------------------- */
37: typedef enum {
38: FVBC_PERIODIC,
39: FVBC_OUTFLOW
40: } FVBCType;
41: static const char *FVBCTypes[] = {"PERIODIC", "OUTFLOW", "FVBCType", "FVBC_", 0};
43: typedef struct {
44: PetscErrorCode (*sample)(void *, PetscInt, FVBCType, PetscReal, PetscReal, PetscReal, PetscReal, PetscReal *);
45: PetscErrorCode (*flux)(void *, const PetscScalar *, PetscScalar *, PetscReal *);
46: PetscErrorCode (*destroy)(void *);
47: void *user;
48: PetscInt dof;
49: char *fieldname[16];
50: } PhysicsCtx;
52: typedef struct {
53: PhysicsCtx physics;
54: MPI_Comm comm;
55: char prefix[256];
57: /* Local work arrays */
58: PetscScalar *flux; /* Flux across interface */
59: PetscReal *speeds; /* Speeds of each wave */
60: PetscScalar *u; /* value at face */
62: PetscReal cfl_idt; /* Max allowable value of 1/Delta t */
63: PetscReal cfl;
64: PetscReal xmin, xmax;
65: PetscInt initial;
66: PetscBool exact;
67: PetscBool simulation;
68: FVBCType bctype;
69: PetscInt hratio; /* hratio = hslow/hfast */
70: IS isf, iss;
71: PetscInt sf, fs; /* slow-fast and fast-slow interfaces */
72: } FVCtx;
74: /* --------------------------------- Physics ----------------------------------- */
75: static PetscErrorCode PhysicsDestroy_SimpleFree(void *vctx)
76: {
78: PetscFree(vctx);
79: return 0;
80: }
82: /* --------------------------------- Advection ----------------------------------- */
83: typedef struct {
84: PetscReal a; /* advective velocity */
85: } AdvectCtx;
87: static PetscErrorCode PhysicsFlux_Advect(void *vctx, const PetscScalar *u, PetscScalar *flux, PetscReal *maxspeed)
88: {
89: AdvectCtx *ctx = (AdvectCtx *)vctx;
90: PetscReal speed;
93: speed = ctx->a;
94: flux[0] = speed * u[0];
95: *maxspeed = speed;
96: return 0;
97: }
99: static PetscErrorCode PhysicsSample_Advect(void *vctx, PetscInt initial, FVBCType bctype, PetscReal xmin, PetscReal xmax, PetscReal t, PetscReal x, PetscReal *u)
100: {
101: AdvectCtx *ctx = (AdvectCtx *)vctx;
102: PetscReal a = ctx->a, x0;
105: switch (bctype) {
106: case FVBC_OUTFLOW:
107: x0 = x - a * t;
108: break;
109: case FVBC_PERIODIC:
110: x0 = RangeMod(x - a * t, xmin, xmax);
111: break;
112: default:
113: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "unknown BCType");
114: }
115: switch (initial) {
116: case 0:
117: u[0] = (x0 < 0) ? 1 : -1;
118: break;
119: case 1:
120: u[0] = (x0 < 0) ? -1 : 1;
121: break;
122: case 2:
123: u[0] = (0 < x0 && x0 < 1) ? 1 : 0;
124: break;
125: case 3:
126: u[0] = PetscSinReal(2 * PETSC_PI * x0);
127: break;
128: case 4:
129: u[0] = PetscAbs(x0);
130: break;
131: case 5:
132: u[0] = (x0 < 0 || x0 > 0.5) ? 0 : PetscSqr(PetscSinReal(2 * PETSC_PI * x0));
133: break;
134: case 6:
135: u[0] = (x0 < 0) ? 0 : ((x0 < 1) ? x0 : ((x0 < 2) ? 2 - x0 : 0));
136: break;
137: case 7:
138: u[0] = PetscPowReal(PetscSinReal(PETSC_PI * x0), 10.0);
139: break;
140: default:
141: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "unknown initial condition");
142: }
143: return 0;
144: }
146: static PetscErrorCode PhysicsCreate_Advect(FVCtx *ctx)
147: {
148: AdvectCtx *user;
151: PetscNew(&user);
152: ctx->physics.sample = PhysicsSample_Advect;
153: ctx->physics.flux = PhysicsFlux_Advect;
154: ctx->physics.destroy = PhysicsDestroy_SimpleFree;
155: ctx->physics.user = user;
156: ctx->physics.dof = 1;
157: PetscStrallocpy("u", &ctx->physics.fieldname[0]);
158: user->a = 1;
159: PetscOptionsBegin(ctx->comm, ctx->prefix, "Options for advection", "");
160: {
161: PetscOptionsReal("-physics_advect_a", "Speed", "", user->a, &user->a, NULL);
162: }
163: PetscOptionsEnd();
164: return 0;
165: }
167: /* --------------------------------- Finite Volume Solver ----------------------------------- */
169: static PetscErrorCode FVRHSFunction(TS ts, PetscReal time, Vec X, Vec F, void *vctx)
170: {
171: FVCtx *ctx = (FVCtx *)vctx;
172: PetscInt i, j, Mx, dof, xs, xm, sf = ctx->sf, fs = ctx->fs;
173: PetscReal hf, hs, cfl_idt = 0;
174: PetscScalar *x, *f, *r, *min, *alpha, *gamma;
175: Vec Xloc;
176: DM da;
179: TSGetDM(ts, &da);
180: DMGetLocalVector(da, &Xloc); /* Xloc contains ghost points */
181: DMDAGetInfo(da, 0, &Mx, 0, 0, 0, 0, 0, &dof, 0, 0, 0, 0, 0); /* Mx is the number of center points */
182: hs = (ctx->xmax - ctx->xmin) / 2.0 * (ctx->hratio + 1.0) / Mx;
183: hf = (ctx->xmax - ctx->xmin) / 2.0 * (1.0 + 1.0 / ctx->hratio) / Mx;
184: DMGlobalToLocalBegin(da, X, INSERT_VALUES, Xloc); /* X is solution vector which does not contain ghost points */
185: DMGlobalToLocalEnd(da, X, INSERT_VALUES, Xloc);
186: VecZeroEntries(F); /* F is the right hand side function corresponds to center points */
187: DMDAVecGetArray(da, Xloc, &x);
188: DMDAVecGetArray(da, F, &f);
189: DMDAGetCorners(da, &xs, 0, 0, &xm, 0, 0);
190: PetscMalloc4(dof, &r, dof, &min, dof, &alpha, dof, &gamma);
192: if (ctx->bctype == FVBC_OUTFLOW) {
193: for (i = xs - 2; i < 0; i++) {
194: for (j = 0; j < dof; j++) x[i * dof + j] = x[j];
195: }
196: for (i = Mx; i < xs + xm + 2; i++) {
197: for (j = 0; j < dof; j++) x[i * dof + j] = x[(xs + xm - 1) * dof + j];
198: }
199: }
201: for (i = xs; i < xs + xm + 1; i++) {
202: PetscReal maxspeed;
203: PetscScalar *u;
204: if (i < sf || i > fs + 1) {
205: u = &ctx->u[0];
206: alpha[0] = 1.0 / 6.0;
207: gamma[0] = 1.0 / 3.0;
208: for (j = 0; j < dof; j++) {
209: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
210: min[j] = PetscMin(r[j], 2.0);
211: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
212: }
213: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
214: cfl_idt = PetscMax(cfl_idt, PetscAbsScalar(maxspeed / hs));
215: if (i > xs) {
216: for (j = 0; j < dof; j++) f[(i - 1) * dof + j] -= ctx->flux[j] / hs;
217: }
218: if (i < xs + xm) {
219: for (j = 0; j < dof; j++) f[i * dof + j] += ctx->flux[j] / hs;
220: }
221: } else if (i == sf) {
222: u = &ctx->u[0];
223: alpha[0] = hs * hf / (hs + hs) / (hs + hs + hf);
224: gamma[0] = hs * (hs + hs) / (hs + hf) / (hs + hs + hf);
225: for (j = 0; j < dof; j++) {
226: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
227: min[j] = PetscMin(r[j], 2.0);
228: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
229: }
230: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
231: if (i > xs) {
232: for (j = 0; j < dof; j++) f[(i - 1) * dof + j] -= ctx->flux[j] / hs;
233: }
234: if (i < xs + xm) {
235: for (j = 0; j < dof; j++) f[i * dof + j] += ctx->flux[j] / hf;
236: }
237: } else if (i == sf + 1) {
238: u = &ctx->u[0];
239: alpha[0] = hf * hf / (hs + hf) / (hs + hf + hf);
240: gamma[0] = hf * (hs + hf) / (hf + hf) / (hs + hf + hf);
241: for (j = 0; j < dof; j++) {
242: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
243: min[j] = PetscMin(r[j], 2.0);
244: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
245: }
246: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
247: if (i > xs) {
248: for (j = 0; j < dof; j++) f[(i - 1) * dof + j] -= ctx->flux[j] / hf;
249: }
250: if (i < xs + xm) {
251: for (j = 0; j < dof; j++) f[i * dof + j] += ctx->flux[j] / hf;
252: }
253: } else if (i > sf + 1 && i < fs) {
254: u = &ctx->u[0];
255: alpha[0] = 1.0 / 6.0;
256: gamma[0] = 1.0 / 3.0;
257: for (j = 0; j < dof; j++) {
258: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
259: min[j] = PetscMin(r[j], 2.0);
260: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
261: }
262: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
263: if (i > xs) {
264: for (j = 0; j < dof; j++) f[(i - 1) * dof + j] -= ctx->flux[j] / hf;
265: }
266: if (i < xs + xm) {
267: for (j = 0; j < dof; j++) f[i * dof + j] += ctx->flux[j] / hf;
268: }
269: } else if (i == fs) {
270: u = &ctx->u[0];
271: alpha[0] = hf * hs / (hf + hf) / (hf + hf + hs);
272: gamma[0] = hf * (hf + hf) / (hf + hs) / (hf + hf + hs);
273: for (j = 0; j < dof; j++) {
274: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
275: min[j] = PetscMin(r[j], 2.0);
276: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
277: }
278: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
279: if (i > xs) {
280: for (j = 0; j < dof; j++) f[(i - 1) * dof + j] -= ctx->flux[j] / hf;
281: }
282: if (i < xs + xm) {
283: for (j = 0; j < dof; j++) f[i * dof + j] += ctx->flux[j] / hs;
284: }
285: } else if (i == fs + 1) {
286: u = &ctx->u[0];
287: alpha[0] = hs * hs / (hf + hs) / (hf + hs + hs);
288: gamma[0] = hs * (hf + hs) / (hs + hs) / (hf + hs + hs);
289: for (j = 0; j < dof; j++) {
290: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
291: min[j] = PetscMin(r[j], 2.0);
292: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
293: }
294: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
295: if (i > xs) {
296: for (j = 0; j < dof; j++) f[(i - 1) * dof + j] -= ctx->flux[j] / hs;
297: }
298: if (i < xs + xm) {
299: for (j = 0; j < dof; j++) f[i * dof + j] += ctx->flux[j] / hs;
300: }
301: }
302: }
303: DMDAVecRestoreArray(da, Xloc, &x);
304: DMDAVecRestoreArray(da, F, &f);
305: DMRestoreLocalVector(da, &Xloc);
306: MPI_Allreduce(&cfl_idt, &ctx->cfl_idt, 1, MPIU_SCALAR, MPIU_MAX, PetscObjectComm((PetscObject)da));
307: if (0) {
308: /* We need a way to inform the TS of a CFL constraint, this is a debugging fragment */
309: PetscReal dt, tnow;
310: TSGetTimeStep(ts, &dt);
311: TSGetTime(ts, &tnow);
312: if (dt > 0.5 / ctx->cfl_idt) PetscPrintf(ctx->comm, "Stability constraint exceeded at t=%g, dt %g > %g\n", (double)tnow, (double)dt, (double)(0.5 / ctx->cfl_idt));
313: }
314: PetscFree4(r, min, alpha, gamma);
315: return 0;
316: }
318: static PetscErrorCode FVRHSFunctionslow(TS ts, PetscReal time, Vec X, Vec F, void *vctx)
319: {
320: FVCtx *ctx = (FVCtx *)vctx;
321: PetscInt i, j, Mx, dof, xs, xm, islow = 0, sf = ctx->sf, fs = ctx->fs;
322: PetscReal hf, hs;
323: PetscScalar *x, *f, *r, *min, *alpha, *gamma;
324: Vec Xloc;
325: DM da;
328: TSGetDM(ts, &da);
329: DMGetLocalVector(da, &Xloc); /* Xloc contains ghost points */
330: DMDAGetInfo(da, 0, &Mx, 0, 0, 0, 0, 0, &dof, 0, 0, 0, 0, 0); /* Mx is the number of center points */
331: hs = (ctx->xmax - ctx->xmin) / 2.0 * (ctx->hratio + 1.0) / Mx;
332: hf = (ctx->xmax - ctx->xmin) / 2.0 * (1.0 + 1.0 / ctx->hratio) / Mx;
333: DMGlobalToLocalBegin(da, X, INSERT_VALUES, Xloc); /* X is solution vector which does not contain ghost points */
334: DMGlobalToLocalEnd(da, X, INSERT_VALUES, Xloc);
335: VecZeroEntries(F); /* F is the right hand side function corresponds to center points */
336: DMDAVecGetArray(da, Xloc, &x);
337: VecGetArray(F, &f);
338: DMDAGetCorners(da, &xs, 0, 0, &xm, 0, 0);
339: PetscMalloc4(dof, &r, dof, &min, dof, &alpha, dof, &gamma);
341: if (ctx->bctype == FVBC_OUTFLOW) {
342: for (i = xs - 2; i < 0; i++) {
343: for (j = 0; j < dof; j++) x[i * dof + j] = x[j];
344: }
345: for (i = Mx; i < xs + xm + 2; i++) {
346: for (j = 0; j < dof; j++) x[i * dof + j] = x[(xs + xm - 1) * dof + j];
347: }
348: }
350: for (i = xs; i < xs + xm + 1; i++) {
351: PetscReal maxspeed;
352: PetscScalar *u;
353: if (i < sf) {
354: u = &ctx->u[0];
355: alpha[0] = 1.0 / 6.0;
356: gamma[0] = 1.0 / 3.0;
357: for (j = 0; j < dof; j++) {
358: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
359: min[j] = PetscMin(r[j], 2.0);
360: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
361: }
362: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
363: if (i > xs) {
364: for (j = 0; j < dof; j++) f[(islow - 1) * dof + j] -= ctx->flux[j] / hs;
365: }
366: if (i < xs + xm) {
367: for (j = 0; j < dof; j++) f[islow * dof + j] += ctx->flux[j] / hs;
368: islow++;
369: }
370: } else if (i == sf) {
371: u = &ctx->u[0];
372: alpha[0] = hs * hf / (hs + hs) / (hs + hs + hf);
373: gamma[0] = hs * (hs + hs) / (hs + hf) / (hs + hs + hf);
374: for (j = 0; j < dof; j++) {
375: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
376: min[j] = PetscMin(r[j], 2.0);
377: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
378: }
379: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
380: if (i > xs) {
381: for (j = 0; j < dof; j++) f[(islow - 1) * dof + j] -= ctx->flux[j] / hs;
382: }
383: } else if (i == fs) {
384: u = &ctx->u[0];
385: alpha[0] = hf * hs / (hf + hf) / (hf + hf + hs);
386: gamma[0] = hf * (hf + hf) / (hf + hs) / (hf + hf + hs);
387: for (j = 0; j < dof; j++) {
388: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
389: min[j] = PetscMin(r[j], 2.0);
390: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
391: }
392: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
393: if (i < xs + xm) {
394: for (j = 0; j < dof; j++) f[islow * dof + j] += ctx->flux[j] / hs;
395: islow++;
396: }
397: } else if (i == fs + 1) {
398: u = &ctx->u[0];
399: alpha[0] = hs * hs / (hf + hs) / (hf + hs + hs);
400: gamma[0] = hs * (hf + hs) / (hs + hs) / (hf + hs + hs);
401: for (j = 0; j < dof; j++) {
402: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
403: min[j] = PetscMin(r[j], 2.0);
404: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
405: }
406: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
407: if (i > xs) {
408: for (j = 0; j < dof; j++) f[(islow - 1) * dof + j] -= ctx->flux[j] / hs;
409: }
410: if (i < xs + xm) {
411: for (j = 0; j < dof; j++) f[islow * dof + j] += ctx->flux[j] / hs;
412: islow++;
413: }
414: } else if (i > fs + 1) {
415: u = &ctx->u[0];
416: alpha[0] = 1.0 / 6.0;
417: gamma[0] = 1.0 / 3.0;
418: for (j = 0; j < dof; j++) {
419: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
420: min[j] = PetscMin(r[j], 2.0);
421: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
422: }
423: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
424: if (i > xs) {
425: for (j = 0; j < dof; j++) f[(islow - 1) * dof + j] -= ctx->flux[j] / hs;
426: }
427: if (i < xs + xm) {
428: for (j = 0; j < dof; j++) f[islow * dof + j] += ctx->flux[j] / hs;
429: islow++;
430: }
431: }
432: }
433: DMDAVecRestoreArray(da, Xloc, &x);
434: VecRestoreArray(F, &f);
435: DMRestoreLocalVector(da, &Xloc);
436: PetscFree4(r, min, alpha, gamma);
437: return 0;
438: }
440: static PetscErrorCode FVRHSFunctionfast(TS ts, PetscReal time, Vec X, Vec F, void *vctx)
441: {
442: FVCtx *ctx = (FVCtx *)vctx;
443: PetscInt i, j, Mx, dof, xs, xm, ifast = 0, sf = ctx->sf, fs = ctx->fs;
444: PetscReal hf, hs;
445: PetscScalar *x, *f, *r, *min, *alpha, *gamma;
446: Vec Xloc;
447: DM da;
450: TSGetDM(ts, &da);
451: DMGetLocalVector(da, &Xloc); /* Xloc contains ghost points */
452: DMDAGetInfo(da, 0, &Mx, 0, 0, 0, 0, 0, &dof, 0, 0, 0, 0, 0); /* Mx is the number of center points */
453: hs = (ctx->xmax - ctx->xmin) / 2.0 * (ctx->hratio + 1.0) / Mx;
454: hf = (ctx->xmax - ctx->xmin) / 2.0 * (1.0 + 1.0 / ctx->hratio) / Mx;
455: DMGlobalToLocalBegin(da, X, INSERT_VALUES, Xloc); /* X is solution vector which does not contain ghost points */
456: DMGlobalToLocalEnd(da, X, INSERT_VALUES, Xloc);
457: VecZeroEntries(F); /* F is the right hand side function corresponds to center points */
458: DMDAVecGetArray(da, Xloc, &x);
459: VecGetArray(F, &f);
460: DMDAGetCorners(da, &xs, 0, 0, &xm, 0, 0);
461: PetscMalloc4(dof, &r, dof, &min, dof, &alpha, dof, &gamma);
463: if (ctx->bctype == FVBC_OUTFLOW) {
464: for (i = xs - 2; i < 0; i++) {
465: for (j = 0; j < dof; j++) x[i * dof + j] = x[j];
466: }
467: for (i = Mx; i < xs + xm + 2; i++) {
468: for (j = 0; j < dof; j++) x[i * dof + j] = x[(xs + xm - 1) * dof + j];
469: }
470: }
472: for (i = xs; i < xs + xm + 1; i++) {
473: PetscReal maxspeed;
474: PetscScalar *u;
475: if (i == sf) {
476: u = &ctx->u[0];
477: alpha[0] = hs * hf / (hs + hs) / (hs + hs + hf);
478: gamma[0] = hs * (hs + hs) / (hs + hf) / (hs + hs + hf);
479: for (j = 0; j < dof; j++) {
480: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
481: min[j] = PetscMin(r[j], 2.0);
482: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
483: }
484: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
485: if (i < xs + xm) {
486: for (j = 0; j < dof; j++) f[ifast * dof + j] += ctx->flux[j] / hf;
487: ifast++;
488: }
489: } else if (i == sf + 1) {
490: u = &ctx->u[0];
491: alpha[0] = hf * hf / (hs + hf) / (hs + hf + hf);
492: gamma[0] = hf * (hs + hf) / (hf + hf) / (hs + hf + hf);
493: for (j = 0; j < dof; j++) {
494: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
495: min[j] = PetscMin(r[j], 2.0);
496: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
497: }
498: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
499: if (i > xs) {
500: for (j = 0; j < dof; j++) f[(ifast - 1) * dof + j] -= ctx->flux[j] / hf;
501: }
502: if (i < xs + xm) {
503: for (j = 0; j < dof; j++) f[ifast * dof + j] += ctx->flux[j] / hf;
504: ifast++;
505: }
506: } else if (i > sf + 1 && i < fs) {
507: u = &ctx->u[0];
508: alpha[0] = 1.0 / 6.0;
509: gamma[0] = 1.0 / 3.0;
510: for (j = 0; j < dof; j++) {
511: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
512: min[j] = PetscMin(r[j], 2.0);
513: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
514: }
515: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
516: if (i > xs) {
517: for (j = 0; j < dof; j++) f[(ifast - 1) * dof + j] -= ctx->flux[j] / hf;
518: }
519: if (i < xs + xm) {
520: for (j = 0; j < dof; j++) f[ifast * dof + j] += ctx->flux[j] / hf;
521: ifast++;
522: }
523: } else if (i == fs) {
524: u = &ctx->u[0];
525: alpha[0] = hf * hs / (hf + hf) / (hf + hf + hs);
526: gamma[0] = hf * (hf + hf) / (hf + hs) / (hf + hf + hs);
527: for (j = 0; j < dof; j++) {
528: r[j] = (x[i * dof + j] - x[(i - 1) * dof + j]) / (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
529: min[j] = PetscMin(r[j], 2.0);
530: u[j] = x[(i - 1) * dof + j] + PetscMax(0, PetscMin(min[j], alpha[0] + gamma[0] * r[j])) * (x[(i - 1) * dof + j] - x[(i - 2) * dof + j]);
531: }
532: (*ctx->physics.flux)(ctx->physics.user, u, ctx->flux, &maxspeed);
533: if (i > xs) {
534: for (j = 0; j < dof; j++) f[(ifast - 1) * dof + j] -= ctx->flux[j] / hf;
535: }
536: }
537: }
538: DMDAVecRestoreArray(da, Xloc, &x);
539: VecRestoreArray(F, &f);
540: DMRestoreLocalVector(da, &Xloc);
541: PetscFree4(r, min, alpha, gamma);
542: return 0;
543: }
545: /* --------------------------------- Finite Volume Solver for slow components ----------------------------------- */
547: PetscErrorCode FVSample(FVCtx *ctx, DM da, PetscReal time, Vec U)
548: {
549: PetscScalar *u, *uj, xj, xi;
550: PetscInt i, j, k, dof, xs, xm, Mx, count_slow, count_fast;
551: const PetscInt N = 200;
555: DMDAGetInfo(da, 0, &Mx, 0, 0, 0, 0, 0, &dof, 0, 0, 0, 0, 0);
556: DMDAGetCorners(da, &xs, 0, 0, &xm, 0, 0);
557: DMDAVecGetArray(da, U, &u);
558: PetscMalloc1(dof, &uj);
559: const PetscReal hs = (ctx->xmax - ctx->xmin) / 2.0 * (ctx->hratio + 1.0) / Mx;
560: const PetscReal hf = (ctx->xmax - ctx->xmin) / 2.0 * (1.0 + 1.0 / ctx->hratio) / Mx;
561: count_slow = Mx / (1 + ctx->hratio);
562: count_fast = Mx - count_slow;
563: for (i = xs; i < xs + xm; i++) {
564: if (i * hs + 0.5 * hs < (ctx->xmax - ctx->xmin) * 0.25) {
565: xi = ctx->xmin + 0.5 * hs + i * hs;
566: /* Integrate over cell i using trapezoid rule with N points. */
567: for (k = 0; k < dof; k++) u[i * dof + k] = 0;
568: for (j = 0; j < N + 1; j++) {
569: xj = xi + hs * (j - N / 2) / (PetscReal)N;
570: (*ctx->physics.sample)(ctx->physics.user, ctx->initial, ctx->bctype, ctx->xmin, ctx->xmax, time, xj, uj);
571: for (k = 0; k < dof; k++) u[i * dof + k] += ((j == 0 || j == N) ? 0.5 : 1.0) * uj[k] / N;
572: }
573: } else if ((ctx->xmax - ctx->xmin) * 0.25 + (i - count_slow / 2) * hf + 0.5 * hf < (ctx->xmax - ctx->xmin) * 0.75) {
574: xi = ctx->xmin + (ctx->xmax - ctx->xmin) * 0.25 + 0.5 * hf + (i - count_slow / 2) * hf;
575: /* Integrate over cell i using trapezoid rule with N points. */
576: for (k = 0; k < dof; k++) u[i * dof + k] = 0;
577: for (j = 0; j < N + 1; j++) {
578: xj = xi + hf * (j - N / 2) / (PetscReal)N;
579: (*ctx->physics.sample)(ctx->physics.user, ctx->initial, ctx->bctype, ctx->xmin, ctx->xmax, time, xj, uj);
580: for (k = 0; k < dof; k++) u[i * dof + k] += ((j == 0 || j == N) ? 0.5 : 1.0) * uj[k] / N;
581: }
582: } else {
583: xi = ctx->xmin + (ctx->xmax - ctx->xmin) * 0.75 + 0.5 * hs + (i - count_slow / 2 - count_fast) * hs;
584: /* Integrate over cell i using trapezoid rule with N points. */
585: for (k = 0; k < dof; k++) u[i * dof + k] = 0;
586: for (j = 0; j < N + 1; j++) {
587: xj = xi + hs * (j - N / 2) / (PetscReal)N;
588: (*ctx->physics.sample)(ctx->physics.user, ctx->initial, ctx->bctype, ctx->xmin, ctx->xmax, time, xj, uj);
589: for (k = 0; k < dof; k++) u[i * dof + k] += ((j == 0 || j == N) ? 0.5 : 1.0) * uj[k] / N;
590: }
591: }
592: }
593: DMDAVecRestoreArray(da, U, &u);
594: PetscFree(uj);
595: return 0;
596: }
598: static PetscErrorCode SolutionStatsView(DM da, Vec X, PetscViewer viewer)
599: {
600: PetscReal xmin, xmax;
601: PetscScalar sum, tvsum, tvgsum;
602: const PetscScalar *x;
603: PetscInt imin, imax, Mx, i, j, xs, xm, dof;
604: Vec Xloc;
605: PetscBool iascii;
608: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii);
609: if (iascii) {
610: /* PETSc lacks a function to compute total variation norm (difficult in multiple dimensions), we do it here */
611: DMGetLocalVector(da, &Xloc);
612: DMGlobalToLocalBegin(da, X, INSERT_VALUES, Xloc);
613: DMGlobalToLocalEnd(da, X, INSERT_VALUES, Xloc);
614: DMDAVecGetArrayRead(da, Xloc, (void *)&x);
615: DMDAGetCorners(da, &xs, 0, 0, &xm, 0, 0);
616: DMDAGetInfo(da, 0, &Mx, 0, 0, 0, 0, 0, &dof, 0, 0, 0, 0, 0);
617: tvsum = 0;
618: for (i = xs; i < xs + xm; i++) {
619: for (j = 0; j < dof; j++) tvsum += PetscAbsScalar(x[i * dof + j] - x[(i - 1) * dof + j]);
620: }
621: MPI_Allreduce(&tvsum, &tvgsum, 1, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject)da));
622: DMDAVecRestoreArrayRead(da, Xloc, (void *)&x);
623: DMRestoreLocalVector(da, &Xloc);
625: VecMin(X, &imin, &xmin);
626: VecMax(X, &imax, &xmax);
627: VecSum(X, &sum);
628: PetscViewerASCIIPrintf(viewer, "Solution range [%g,%g] with minimum at %" PetscInt_FMT ", mean %g, ||x||_TV %g\n", (double)xmin, (double)xmax, imin, (double)(sum / Mx), (double)(tvgsum / Mx));
629: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Viewer type not supported");
630: return 0;
631: }
633: static PetscErrorCode SolutionErrorNorms(FVCtx *ctx, DM da, PetscReal t, Vec X, PetscReal *nrm1)
634: {
635: Vec Y;
636: PetscInt i, Mx, count_slow = 0, count_fast = 0;
637: const PetscScalar *ptr_X, *ptr_Y;
640: VecGetSize(X, &Mx);
641: VecDuplicate(X, &Y);
642: FVSample(ctx, da, t, Y);
643: const PetscReal hs = (ctx->xmax - ctx->xmin) / 2.0 * (ctx->hratio + 1.0) / Mx;
644: const PetscReal hf = (ctx->xmax - ctx->xmin) / 2.0 * (1.0 + 1.0 / ctx->hratio) / Mx;
645: count_slow = (PetscReal)Mx / (1.0 + ctx->hratio);
646: count_fast = Mx - count_slow;
647: VecGetArrayRead(X, &ptr_X);
648: VecGetArrayRead(Y, &ptr_Y);
649: for (i = 0; i < Mx; i++) {
650: if (i < count_slow / 2 || i > count_slow / 2 + count_fast - 1) *nrm1 += hs * PetscAbs(ptr_X[i] - ptr_Y[i]);
651: else *nrm1 += hf * PetscAbs(ptr_X[i] - ptr_Y[i]);
652: }
653: VecRestoreArrayRead(X, &ptr_X);
654: VecRestoreArrayRead(Y, &ptr_Y);
655: VecDestroy(&Y);
656: return 0;
657: }
659: int main(int argc, char *argv[])
660: {
661: char physname[256] = "advect", final_fname[256] = "solution.m";
662: PetscFunctionList physics = 0;
663: MPI_Comm comm;
664: TS ts;
665: DM da;
666: Vec X, X0, R;
667: FVCtx ctx;
668: PetscInt i, k, dof, xs, xm, Mx, draw = 0, count_slow, count_fast, islow = 0, ifast = 0, *index_slow, *index_fast;
669: PetscBool view_final = PETSC_FALSE;
670: PetscReal ptime;
673: PetscInitialize(&argc, &argv, 0, help);
674: comm = PETSC_COMM_WORLD;
675: PetscMemzero(&ctx, sizeof(ctx));
677: /* Register physical models to be available on the command line */
678: PetscFunctionListAdd(&physics, "advect", PhysicsCreate_Advect);
680: ctx.comm = comm;
681: ctx.cfl = 0.9;
682: ctx.bctype = FVBC_PERIODIC;
683: ctx.xmin = -1.0;
684: ctx.xmax = 1.0;
685: PetscOptionsBegin(comm, NULL, "Finite Volume solver options", "");
686: PetscOptionsReal("-xmin", "X min", "", ctx.xmin, &ctx.xmin, NULL);
687: PetscOptionsReal("-xmax", "X max", "", ctx.xmax, &ctx.xmax, NULL);
688: PetscOptionsInt("-draw", "Draw solution vector, bitwise OR of (1=initial,2=final,4=final error)", "", draw, &draw, NULL);
689: PetscOptionsString("-view_final", "Write final solution in ASCII MATLAB format to given file name", "", final_fname, final_fname, sizeof(final_fname), &view_final);
690: PetscOptionsInt("-initial", "Initial condition (depends on the physics)", "", ctx.initial, &ctx.initial, NULL);
691: PetscOptionsBool("-exact", "Compare errors with exact solution", "", ctx.exact, &ctx.exact, NULL);
692: PetscOptionsBool("-simulation", "Compare errors with reference solution", "", ctx.simulation, &ctx.simulation, NULL);
693: PetscOptionsReal("-cfl", "CFL number to time step at", "", ctx.cfl, &ctx.cfl, NULL);
694: PetscOptionsEnum("-bc_type", "Boundary condition", "", FVBCTypes, (PetscEnum)ctx.bctype, (PetscEnum *)&ctx.bctype, NULL);
695: PetscOptionsInt("-hratio", "Spacing ratio", "", ctx.hratio, &ctx.hratio, NULL);
696: PetscOptionsEnd();
698: /* Choose the physics from the list of registered models */
699: {
700: PetscErrorCode (*r)(FVCtx *);
701: PetscFunctionListFind(physics, physname, &r);
703: /* Create the physics, will set the number of fields and their names */
704: (*r)(&ctx);
705: }
707: /* Create a DMDA to manage the parallel grid */
708: DMDACreate1d(comm, DM_BOUNDARY_PERIODIC, 50, ctx.physics.dof, 2, NULL, &da);
709: DMSetFromOptions(da);
710: DMSetUp(da);
711: /* Inform the DMDA of the field names provided by the physics. */
712: /* The names will be shown in the title bars when run with -ts_monitor_draw_solution */
713: for (i = 0; i < ctx.physics.dof; i++) DMDASetFieldName(da, i, ctx.physics.fieldname[i]);
714: DMDAGetInfo(da, 0, &Mx, 0, 0, 0, 0, 0, &dof, 0, 0, 0, 0, 0);
715: DMDAGetCorners(da, &xs, 0, 0, &xm, 0, 0);
717: /* Set coordinates of cell centers */
718: DMDASetUniformCoordinates(da, ctx.xmin + 0.5 * (ctx.xmax - ctx.xmin) / Mx, ctx.xmax + 0.5 * (ctx.xmax - ctx.xmin) / Mx, 0, 0, 0, 0);
720: /* Allocate work space for the Finite Volume solver (so it doesn't have to be reallocated on each function evaluation) */
721: PetscMalloc3(dof, &ctx.u, dof, &ctx.flux, dof, &ctx.speeds);
723: /* Create a vector to store the solution and to save the initial state */
724: DMCreateGlobalVector(da, &X);
725: VecDuplicate(X, &X0);
726: VecDuplicate(X, &R);
728: /* create index for slow parts and fast parts*/
729: count_slow = Mx / (1 + ctx.hratio);
731: count_fast = Mx - count_slow;
732: ctx.sf = count_slow / 2;
733: ctx.fs = ctx.sf + count_fast;
734: PetscMalloc1(xm * dof, &index_slow);
735: PetscMalloc1(xm * dof, &index_fast);
736: for (i = xs; i < xs + xm; i++) {
737: if (i < count_slow / 2 || i > count_slow / 2 + count_fast - 1)
738: for (k = 0; k < dof; k++) index_slow[islow++] = i * dof + k;
739: else
740: for (k = 0; k < dof; k++) index_fast[ifast++] = i * dof + k;
741: }
742: ISCreateGeneral(PETSC_COMM_WORLD, islow, index_slow, PETSC_COPY_VALUES, &ctx.iss);
743: ISCreateGeneral(PETSC_COMM_WORLD, ifast, index_fast, PETSC_COPY_VALUES, &ctx.isf);
745: /* Create a time-stepping object */
746: TSCreate(comm, &ts);
747: TSSetDM(ts, da);
748: TSSetRHSFunction(ts, R, FVRHSFunction, &ctx);
749: TSRHSSplitSetIS(ts, "slow", ctx.iss);
750: TSRHSSplitSetIS(ts, "fast", ctx.isf);
751: TSRHSSplitSetRHSFunction(ts, "slow", NULL, FVRHSFunctionslow, &ctx);
752: TSRHSSplitSetRHSFunction(ts, "fast", NULL, FVRHSFunctionfast, &ctx);
754: TSSetType(ts, TSMPRK);
755: TSSetMaxTime(ts, 10);
756: TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER);
758: /* Compute initial conditions and starting time step */
759: FVSample(&ctx, da, 0, X0);
760: FVRHSFunction(ts, 0, X0, X, (void *)&ctx); /* Initial function evaluation, only used to determine max speed */
761: VecCopy(X0, X); /* The function value was not used so we set X=X0 again */
762: TSSetTimeStep(ts, ctx.cfl / ctx.cfl_idt);
763: TSSetFromOptions(ts); /* Take runtime options */
764: SolutionStatsView(da, X, PETSC_VIEWER_STDOUT_WORLD);
765: {
766: PetscInt steps;
767: PetscScalar mass_initial, mass_final, mass_difference, mass_differenceg;
768: const PetscScalar *ptr_X, *ptr_X0;
769: const PetscReal hs = (ctx.xmax - ctx.xmin) / 2.0 / count_slow;
770: const PetscReal hf = (ctx.xmax - ctx.xmin) / 2.0 / count_fast;
771: TSSolve(ts, X);
772: TSGetSolveTime(ts, &ptime);
773: TSGetStepNumber(ts, &steps);
774: /* calculate the total mass at initial time and final time */
775: mass_initial = 0.0;
776: mass_final = 0.0;
777: DMDAVecGetArrayRead(da, X0, (void *)&ptr_X0);
778: DMDAVecGetArrayRead(da, X, (void *)&ptr_X);
779: for (i = xs; i < xs + xm; i++) {
780: if (i < ctx.sf || i > ctx.fs - 1) {
781: for (k = 0; k < dof; k++) {
782: mass_initial = mass_initial + hs * ptr_X0[i * dof + k];
783: mass_final = mass_final + hs * ptr_X[i * dof + k];
784: }
785: } else {
786: for (k = 0; k < dof; k++) {
787: mass_initial = mass_initial + hf * ptr_X0[i * dof + k];
788: mass_final = mass_final + hf * ptr_X[i * dof + k];
789: }
790: }
791: }
792: DMDAVecRestoreArrayRead(da, X0, (void *)&ptr_X0);
793: DMDAVecRestoreArrayRead(da, X, (void *)&ptr_X);
794: mass_difference = mass_final - mass_initial;
795: MPI_Allreduce(&mass_difference, &mass_differenceg, 1, MPIU_SCALAR, MPIU_SUM, comm);
796: PetscPrintf(comm, "Mass difference %g\n", (double)mass_differenceg);
797: PetscPrintf(comm, "Final time %g, steps %" PetscInt_FMT "\n", (double)ptime, steps);
798: if (ctx.exact) {
799: PetscReal nrm1 = 0;
800: SolutionErrorNorms(&ctx, da, ptime, X, &nrm1);
801: PetscPrintf(comm, "Error ||x-x_e||_1 %g\n", (double)nrm1);
802: }
803: if (ctx.simulation) {
804: PetscReal nrm1 = 0;
805: PetscViewer fd;
806: char filename[PETSC_MAX_PATH_LEN] = "binaryoutput";
807: Vec XR;
808: PetscBool flg;
809: const PetscScalar *ptr_XR;
810: PetscOptionsGetString(NULL, NULL, "-f", filename, sizeof(filename), &flg);
812: PetscViewerBinaryOpen(PETSC_COMM_WORLD, filename, FILE_MODE_READ, &fd);
813: VecDuplicate(X0, &XR);
814: VecLoad(XR, fd);
815: PetscViewerDestroy(&fd);
816: VecGetArrayRead(X, &ptr_X);
817: VecGetArrayRead(XR, &ptr_XR);
818: for (i = 0; i < Mx; i++) {
819: if (i < count_slow / 2 || i > count_slow / 2 + count_fast - 1) nrm1 = nrm1 + hs * PetscAbs(ptr_X[i] - ptr_XR[i]);
820: else nrm1 = nrm1 + hf * PetscAbs(ptr_X[i] - ptr_XR[i]);
821: }
822: VecRestoreArrayRead(X, &ptr_X);
823: VecRestoreArrayRead(XR, &ptr_XR);
824: PetscPrintf(comm, "Error ||x-x_e||_1 %g\n", (double)nrm1);
825: VecDestroy(&XR);
826: }
827: }
829: SolutionStatsView(da, X, PETSC_VIEWER_STDOUT_WORLD);
830: if (draw & 0x1) VecView(X0, PETSC_VIEWER_DRAW_WORLD);
831: if (draw & 0x2) VecView(X, PETSC_VIEWER_DRAW_WORLD);
832: if (draw & 0x4) {
833: Vec Y;
834: VecDuplicate(X, &Y);
835: FVSample(&ctx, da, ptime, Y);
836: VecAYPX(Y, -1, X);
837: VecView(Y, PETSC_VIEWER_DRAW_WORLD);
838: VecDestroy(&Y);
839: }
841: if (view_final) {
842: PetscViewer viewer;
843: PetscViewerASCIIOpen(PETSC_COMM_WORLD, final_fname, &viewer);
844: PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
845: VecView(X, viewer);
846: PetscViewerPopFormat(viewer);
847: PetscViewerDestroy(&viewer);
848: }
850: /* Clean up */
851: (*ctx.physics.destroy)(ctx.physics.user);
852: for (i = 0; i < ctx.physics.dof; i++) PetscFree(ctx.physics.fieldname[i]);
853: PetscFree3(ctx.u, ctx.flux, ctx.speeds);
854: ISDestroy(&ctx.iss);
855: ISDestroy(&ctx.isf);
856: VecDestroy(&X);
857: VecDestroy(&X0);
858: VecDestroy(&R);
859: DMDestroy(&da);
860: TSDestroy(&ts);
861: PetscFree(index_slow);
862: PetscFree(index_fast);
863: PetscFunctionListDestroy(&physics);
864: PetscFinalize();
865: return 0;
866: }
868: /*TEST
870: build:
871: requires: !complex
873: test:
874: args: -da_grid_x 60 -initial 7 -xmin -1 -xmax 1 -hratio 2 -ts_dt 0.025 -ts_max_steps 24 -ts_type rk -ts_rk_type 2a -ts_rk_dtratio 2 -ts_rk_multirate -ts_use_splitrhsfunction 0
876: test:
877: suffix: 2
878: args: -da_grid_x 60 -initial 7 -xmin -1 -xmax 1 -hratio 2 -ts_dt 0.025 -ts_max_steps 24 -ts_type rk -ts_rk_type 2a -ts_rk_dtratio 2 -ts_rk_multirate -ts_use_splitrhsfunction 1
879: output_file: output/ex7_1.out
881: test:
882: suffix: 3
883: args: -da_grid_x 60 -initial 7 -xmin -1 -xmax 1 -hratio 2 -ts_dt 0.025 -ts_max_steps 24 -ts_type mprk -ts_mprk_type 2a22 -ts_use_splitrhsfunction 0
885: test:
886: suffix: 4
887: args: -da_grid_x 60 -initial 7 -xmin -1 -xmax 1 -hratio 2 -ts_dt 0.025 -ts_max_steps 24 -ts_type mprk -ts_mprk_type 2a22 -ts_use_splitrhsfunction 1
888: output_file: output/ex7_3.out
890: test:
891: suffix: 5
892: nsize: 2
893: args: -da_grid_x 60 -initial 7 -xmin -1 -xmax 1 -hratio 2 -ts_dt 0.025 -ts_max_steps 24 -ts_type mprk -ts_mprk_type 2a22 -ts_use_splitrhsfunction 1
894: output_file: output/ex7_3.out
895: TEST*/