Actual source code: ex28.c
1: static char help[] = "Example application of the Bhatnagar-Gross-Krook (BGK) collision operator.\n\
2: This example is a 0D-1V setting for the kinetic equation\n\
3: https://en.wikipedia.org/wiki/Bhatnagar%E2%80%93Gross%E2%80%93Krook_operator\n";
5: #include <petscdmplex.h>
6: #include <petscdmswarm.h>
7: #include <petscts.h>
8: #include <petscdraw.h>
10: typedef struct {
11: PetscInt particlesPerCell; /* The number of partices per cell */
12: PetscReal momentTol; /* Tolerance for checking moment conservation */
13: PetscBool monitorhg; /* Flag for using the TS histogram monitor */
14: PetscBool monitorsp; /* Flag for using the TS scatter monitor */
15: PetscBool monitorks; /* Monitor to perform KS test to the maxwellian */
16: PetscBool error; /* Flag for printing the error */
17: PetscInt ostep; /* print the energy at each ostep time steps */
18: PetscDraw draw; /* The draw object for histogram monitoring */
19: PetscDrawHG drawhg; /* The histogram draw context for monitoring */
20: PetscDrawSP drawsp; /* The scatter plot draw context for the monitor */
21: PetscDrawSP drawks; /* Scatterplot draw context for KS test */
22: } AppCtx;
24: static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
25: {
26: PetscFunctionBeginUser;
27: options->monitorhg = PETSC_FALSE;
28: options->monitorsp = PETSC_FALSE;
29: options->monitorks = PETSC_FALSE;
30: options->particlesPerCell = 1;
31: options->momentTol = 100.0 * PETSC_MACHINE_EPSILON;
32: options->ostep = 100;
34: PetscOptionsBegin(comm, "", "Collision Options", "DMPLEX");
35: PetscCall(PetscOptionsBool("-monitorhg", "Flag to use the TS histogram monitor", "ex28.c", options->monitorhg, &options->monitorhg, NULL));
36: PetscCall(PetscOptionsBool("-monitorsp", "Flag to use the TS scatter plot monitor", "ex28.c", options->monitorsp, &options->monitorsp, NULL));
37: PetscCall(PetscOptionsBool("-monitorks", "Flag to plot KS test results", "ex28.c", options->monitorks, &options->monitorks, NULL));
38: PetscCall(PetscOptionsInt("-particles_per_cell", "Number of particles per cell", "ex28.c", options->particlesPerCell, &options->particlesPerCell, NULL));
39: PetscCall(PetscOptionsInt("-output_step", "Number of time steps between output", "ex28.c", options->ostep, &options->ostep, NULL));
40: PetscOptionsEnd();
41: PetscFunctionReturn(PETSC_SUCCESS);
42: }
44: /* Create the mesh for velocity space */
45: static PetscErrorCode CreateMesh(MPI_Comm comm, DM *dm, AppCtx *user)
46: {
47: PetscFunctionBeginUser;
48: PetscCall(DMCreate(comm, dm));
49: PetscCall(DMSetType(*dm, DMPLEX));
50: PetscCall(DMSetFromOptions(*dm));
51: PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
52: PetscFunctionReturn(PETSC_SUCCESS);
53: }
55: /* Since we are putting the same number of particles in each cell, this amounts to a uniform distribution of v */
56: static PetscErrorCode SetInitialCoordinates(DM sw)
57: {
58: AppCtx *user;
59: PetscRandom rnd;
60: DM dm;
61: DMPolytopeType ct;
62: PetscBool simplex;
63: PetscReal *centroid, *coords, *xi0, *v0, *J, *invJ, detJ, *vals;
64: PetscInt dim, d, cStart, cEnd, c, Np, p;
66: PetscFunctionBeginUser;
67: PetscCall(PetscRandomCreate(PetscObjectComm((PetscObject)sw), &rnd));
68: PetscCall(PetscRandomSetInterval(rnd, -1.0, 1.0));
69: PetscCall(PetscRandomSetFromOptions(rnd));
71: PetscCall(DMGetApplicationContext(sw, &user));
72: Np = user->particlesPerCell;
73: PetscCall(DMGetDimension(sw, &dim));
74: PetscCall(DMSwarmGetCellDM(sw, &dm));
75: PetscCall(DMGetCoordinatesLocalSetUp(dm));
76: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
77: PetscCall(DMPlexGetCellType(dm, cStart, &ct));
78: simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
79: PetscCall(PetscMalloc5(dim, ¢roid, dim, &xi0, dim, &v0, dim * dim, &J, dim * dim, &invJ));
80: for (d = 0; d < dim; ++d) xi0[d] = -1.0;
81: PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
82: PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&vals));
83: for (c = cStart; c < cEnd; ++c) {
84: if (Np == 1) {
85: PetscCall(DMPlexComputeCellGeometryFVM(dm, c, NULL, centroid, NULL));
86: for (d = 0; d < dim; ++d) {
87: coords[c * dim + d] = centroid[d];
88: if (coords[c * dim + d] >= -1 && coords[c * dim + d] <= 1) {
89: vals[c] = 1.0;
90: } else {
91: vals[c] = 0.;
92: }
93: }
94: } else {
95: PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); /* affine */
96: for (p = 0; p < Np; ++p) {
97: const PetscInt n = c * Np + p;
98: PetscReal sum = 0.0, refcoords[3];
100: for (d = 0; d < dim; ++d) {
101: PetscCall(PetscRandomGetValueReal(rnd, &refcoords[d]));
102: sum += refcoords[d];
103: }
104: if (simplex && sum > 0.0)
105: for (d = 0; d < dim; ++d) refcoords[d] -= PetscSqrtReal(dim) * sum;
106: vals[n] = 1.0;
107: PetscCall(DMPlexReferenceToCoordinates(dm, c, 1, refcoords, &coords[n * dim]));
108: }
109: }
110: }
111: PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
112: PetscCall(DMSwarmRestoreField(sw, "w_q", NULL, NULL, (void **)&vals));
113: PetscCall(PetscFree5(centroid, xi0, v0, J, invJ));
114: PetscCall(PetscRandomDestroy(&rnd));
115: PetscFunctionReturn(PETSC_SUCCESS);
116: }
118: /* The initial conditions are just the initial particle weights */
119: static PetscErrorCode SetInitialConditions(DM dmSw, Vec u)
120: {
121: DM dm;
122: AppCtx *user;
123: PetscReal *vals;
124: PetscScalar *initialConditions;
125: PetscInt dim, d, cStart, cEnd, c, Np, p, n;
127: PetscFunctionBeginUser;
128: PetscCall(VecGetLocalSize(u, &n));
129: PetscCall(DMGetApplicationContext(dmSw, &user));
130: Np = user->particlesPerCell;
131: PetscCall(DMSwarmGetCellDM(dmSw, &dm));
132: PetscCall(DMGetDimension(dm, &dim));
133: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
134: PetscCheck(n == (cEnd - cStart) * Np, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "TS solution local size %" PetscInt_FMT " != %" PetscInt_FMT " nm particles", n, (cEnd - cStart) * Np);
135: PetscCall(DMSwarmGetField(dmSw, "w_q", NULL, NULL, (void **)&vals));
136: PetscCall(VecGetArray(u, &initialConditions));
137: for (c = cStart; c < cEnd; ++c) {
138: for (p = 0; p < Np; ++p) {
139: const PetscInt n = c * Np + p;
140: for (d = 0; d < dim; d++) initialConditions[n] = vals[n];
141: }
142: }
143: PetscCall(VecRestoreArray(u, &initialConditions));
144: PetscCall(DMSwarmRestoreField(dmSw, "w_q", NULL, NULL, (void **)&vals));
145: PetscFunctionReturn(PETSC_SUCCESS);
146: }
148: static PetscErrorCode CreateParticles(DM dm, DM *sw, AppCtx *user)
149: {
150: DMSwarmCellDM celldm;
151: PetscInt *swarm_cellid;
152: PetscInt dim, cStart, cEnd, c, Np = user->particlesPerCell, p;
153: const char *cellid;
155: PetscFunctionBeginUser;
156: PetscCall(DMGetDimension(dm, &dim));
157: PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), sw));
158: PetscCall(DMSetType(*sw, DMSWARM));
159: PetscCall(DMSetDimension(*sw, dim));
160: PetscCall(DMSwarmSetType(*sw, DMSWARM_PIC));
161: PetscCall(DMSwarmSetCellDM(*sw, dm));
162: PetscCall(DMSwarmRegisterPetscDatatypeField(*sw, "kinematics", dim, PETSC_REAL));
163: PetscCall(DMSwarmRegisterPetscDatatypeField(*sw, "w_q", 1, PETSC_SCALAR));
164: PetscCall(DMSwarmFinalizeFieldRegister(*sw));
165: PetscCall(DMSwarmGetCellDMActive(*sw, &celldm));
166: PetscCall(DMSwarmCellDMGetCellID(celldm, &cellid));
167: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
168: PetscCall(DMSwarmSetLocalSizes(*sw, (cEnd - cStart) * Np, 0));
169: PetscCall(DMSetFromOptions(*sw));
170: PetscCall(DMSwarmGetField(*sw, cellid, NULL, NULL, (void **)&swarm_cellid));
171: for (c = cStart; c < cEnd; ++c) {
172: for (p = 0; p < Np; ++p) {
173: const PetscInt n = c * Np + p;
174: swarm_cellid[n] = c;
175: }
176: }
177: PetscCall(DMSwarmRestoreField(*sw, cellid, NULL, NULL, (void **)&swarm_cellid));
178: PetscCall(PetscObjectSetName((PetscObject)*sw, "Particles"));
179: PetscCall(DMViewFromOptions(*sw, NULL, "-sw_view"));
180: PetscFunctionReturn(PETSC_SUCCESS);
181: }
183: /*
184: f_t = 1/\tau \left( f_eq - f \right)
185: n_t = 1/\tau \left( \int f_eq - \int f \right) = 1/\tau (n - n) = 0
186: v_t = 1/\tau \left( \int v f_eq - \int v f \right) = 1/\tau (v - v) = 0
187: E_t = 1/\tau \left( \int v^2 f_eq - \int v^2 f \right) = 1/\tau (T - T) = 0
189: Let's look at a single cell:
191: \int_C f_t = 1/\tau \left( \int_C f_eq - \int_C f \right)
192: \sum_{x_i \in C} w^i_t = 1/\tau \left( F_eq(C) - \sum_{x_i \in C} w_i \right)
193: */
195: /* This computes the 1D Maxwellian distribution for given mass n, velocity v, and temperature T */
196: static PetscReal ComputePDF(PetscReal m, PetscReal n, PetscReal T, PetscReal v[])
197: {
198: return (n / PetscSqrtReal(2.0 * PETSC_PI * T / m)) * PetscExpReal(-0.5 * m * PetscSqr(v[0]) / T);
199: }
201: /*
202: erf z = \frac{2}{\sqrt\pi} \int^z_0 dt e^{-t^2} and erf \infty = 1
204: We begin with our distribution
206: \sqrt{\frac{m}{2 \pi T}} e^{-m v^2/2T}
208: Let t = \sqrt{m/2T} v, z = \sqrt{m/2T} w, so that we now have
210: \sqrt{\frac{m}{2 \pi T}} \int^w_0 dv e^{-m v^2/2T}
211: = \sqrt{\frac{m}{2 \pi T}} \int^{\sqrt{m/2T} w}_0 \sqrt{2T/m} dt e^{-t^2}
212: = 1/\sqrt{\pi} \int^{\sqrt{m/2T} w}_0 dt e^{-t^2}
213: = 1/2 erf(\sqrt{m/2T} w)
214: */
215: static PetscReal ComputeCDF(PetscReal m, PetscReal n, PetscReal T, PetscReal va, PetscReal vb)
216: {
217: PetscReal alpha = PetscSqrtReal(0.5 * m / T);
218: PetscReal za = alpha * va;
219: PetscReal zb = alpha * vb;
220: PetscReal sum = 0.0;
222: sum += zb >= 0 ? erf(zb) : -erf(-zb);
223: sum -= za >= 0 ? erf(za) : -erf(-za);
224: return 0.5 * n * sum;
225: }
227: static PetscErrorCode CheckDistribution(DM dm, PetscReal m, PetscReal n, PetscReal T, PetscReal v[])
228: {
229: PetscSection coordSection;
230: Vec coordsLocal;
231: PetscReal *xq, *wq;
232: PetscReal vmin, vmax, neq, veq, Teq;
233: PetscInt Nq = 100, q, cStart, cEnd, c;
235: PetscFunctionBeginUser;
236: PetscCall(DMGetBoundingBox(dm, &vmin, &vmax));
237: /* Check analytic over entire line */
238: neq = ComputeCDF(m, n, T, vmin, vmax);
239: PetscCheck(PetscAbsReal(neq - n) <= PETSC_SMALL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Int f %g != %g mass (%g)", (double)neq, (double)n, (double)(neq - n));
240: /* Check analytic over cells */
241: PetscCall(DMGetCoordinatesLocal(dm, &coordsLocal));
242: PetscCall(DMGetCoordinateSection(dm, &coordSection));
243: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
244: neq = 0.0;
245: for (c = cStart; c < cEnd; ++c) {
246: PetscScalar *vcoords = NULL;
248: PetscCall(DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, NULL, &vcoords));
249: neq += ComputeCDF(m, n, T, vcoords[0], vcoords[1]);
250: PetscCall(DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, NULL, &vcoords));
251: }
252: PetscCheck(PetscAbsReal(neq - n) <= PETSC_SMALL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell Int f %g != %g mass (%g)", (double)neq, (double)n, (double)(neq - n));
253: /* Check quadrature over entire line */
254: PetscCall(PetscMalloc2(Nq, &xq, Nq, &wq));
255: PetscCall(PetscDTGaussQuadrature(100, vmin, vmax, xq, wq));
256: neq = 0.0;
257: for (q = 0; q < Nq; ++q) neq += ComputePDF(m, n, T, &xq[q]) * wq[q];
258: PetscCheck(PetscAbsReal(neq - n) <= PETSC_SMALL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Int f %g != %g mass (%g)", (double)neq, (double)n, (double)(neq - n));
259: /* Check omemnts with quadrature */
260: veq = 0.0;
261: for (q = 0; q < Nq; ++q) veq += xq[q] * ComputePDF(m, n, T, &xq[q]) * wq[q];
262: veq /= neq;
263: PetscCheck(PetscAbsReal(veq - v[0]) <= PETSC_SMALL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Int v f %g != %g velocity (%g)", (double)veq, (double)v[0], (double)(veq - v[0]));
264: Teq = 0.0;
265: for (q = 0; q < Nq; ++q) Teq += PetscSqr(xq[q]) * ComputePDF(m, n, T, &xq[q]) * wq[q];
266: Teq = Teq * m / neq - PetscSqr(veq);
267: PetscCheck(PetscAbsReal(Teq - T) <= PETSC_SMALL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Int v^2 f %g != %g temperature (%g)", (double)Teq, (double)T, (double)(Teq - T));
268: PetscCall(PetscFree2(xq, wq));
269: PetscFunctionReturn(PETSC_SUCCESS);
270: }
272: static PetscErrorCode RHSFunctionParticles(TS ts, PetscReal t, Vec U, Vec R, PetscCtx ctx)
273: {
274: const PetscScalar *u;
275: PetscSection coordSection;
276: Vec coordsLocal;
277: PetscScalar *r, *coords;
278: PetscReal n = 0.0, v = 0.0, E = 0.0, T = 0.0, m = 1.0, cn = 0.0, cv = 0.0, cE = 0.0, pE = 0.0, eqE = 0.0;
279: PetscInt dim, d, Np, Ncp, p, cStart, cEnd, c;
280: DM dmSw, plex;
282: PetscFunctionBeginUser;
283: PetscCall(VecGetLocalSize(U, &Np));
284: PetscCall(VecGetArrayRead(U, &u));
285: PetscCall(VecGetArray(R, &r));
286: PetscCall(TSGetDM(ts, &dmSw));
287: PetscCall(DMSwarmGetCellDM(dmSw, &plex));
288: PetscCall(DMGetDimension(dmSw, &dim));
289: PetscCall(DMGetCoordinatesLocal(plex, &coordsLocal));
290: PetscCall(DMGetCoordinateSection(plex, &coordSection));
291: PetscCall(DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd));
292: Np /= dim;
293: Ncp = Np / (cEnd - cStart);
294: /* Calculate moments of particle distribution, note that velocity is in the coordinate */
295: PetscCall(DMSwarmGetField(dmSw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
296: for (p = 0; p < Np; ++p) {
297: PetscReal m1 = 0.0, m2 = 0.0;
299: for (d = 0; d < dim; ++d) {
300: m1 += PetscRealPart(coords[p * dim + d]);
301: m2 += PetscSqr(PetscRealPart(coords[p * dim + d]));
302: }
303: n += u[p];
304: v += u[p] * m1;
305: E += u[p] * m2;
306: }
307: v /= n;
308: T = E * m / n - v * v;
309: PetscCall(PetscInfo(ts, "Time %.2f: mass %.4f velocity: %+.4f temperature: %.4f\n", (double)t, (double)n, (double)v, (double)T));
310: PetscCall(CheckDistribution(plex, m, n, T, &v));
311: /*
312: Begin cellwise evaluation of the collision operator. Essentially, penalize the weights of the particles
313: in that cell against the maxwellian for the number of particles expected to be in that cell
314: */
315: for (c = cStart; c < cEnd; ++c) {
316: PetscScalar *vcoords = NULL;
317: PetscReal relaxation = 1.0, neq;
318: PetscInt sp = c * Ncp, q;
320: /* Calculate equilibrium occupation for this velocity cell */
321: PetscCall(DMPlexVecGetClosure(plex, coordSection, coordsLocal, c, NULL, &vcoords));
322: neq = ComputeCDF(m, n, T, vcoords[0], vcoords[1]);
323: PetscCall(DMPlexVecRestoreClosure(plex, coordSection, coordsLocal, c, NULL, &vcoords));
324: for (q = 0; q < Ncp; ++q) r[sp + q] = (1.0 / relaxation) * (neq - u[sp + q]);
325: }
326: /* Check update */
327: for (p = 0; p < Np; ++p) {
328: PetscReal m1 = 0.0, m2 = 0.0;
329: PetscScalar *vcoords = NULL;
331: for (d = 0; d < dim; ++d) {
332: m1 += PetscRealPart(coords[p * dim + d]);
333: m2 += PetscSqr(PetscRealPart(coords[p * dim + d]));
334: }
335: cn += r[p];
336: cv += r[p] * m1;
337: cE += r[p] * m2;
338: pE += u[p] * m2;
339: PetscCall(DMPlexVecGetClosure(plex, coordSection, coordsLocal, p, NULL, &vcoords));
340: eqE += ComputeCDF(m, n, T, vcoords[0], vcoords[1]) * m2;
341: PetscCall(DMPlexVecRestoreClosure(plex, coordSection, coordsLocal, p, NULL, &vcoords));
342: }
343: PetscCall(PetscInfo(ts, "Time %.2f: mass update %.8f velocity update: %+.8f energy update: %.8f (%.8f, %.8f)\n", (double)t, (double)cn, (double)cv, (double)cE, (double)pE, (double)eqE));
344: PetscCall(DMSwarmRestoreField(dmSw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
345: PetscCall(VecRestoreArrayRead(U, &u));
346: PetscCall(VecRestoreArray(R, &r));
347: PetscFunctionReturn(PETSC_SUCCESS);
348: }
350: static PetscErrorCode HGMonitor(TS ts, PetscInt step, PetscReal t, Vec U, PetscCtx ctx)
351: {
352: AppCtx *user = (AppCtx *)ctx;
353: const PetscScalar *u;
354: DM sw, dm;
355: PetscInt dim, Np, p;
357: PetscFunctionBeginUser;
358: if (step < 0) PetscFunctionReturn(PETSC_SUCCESS);
359: if ((user->ostep > 0) && (!(step % user->ostep))) {
360: PetscDrawAxis axis;
362: PetscCall(TSGetDM(ts, &sw));
363: PetscCall(DMSwarmGetCellDM(sw, &dm));
364: PetscCall(DMGetDimension(dm, &dim));
365: PetscCall(PetscDrawHGReset(user->drawhg));
366: PetscCall(PetscDrawHGGetAxis(user->drawhg, &axis));
367: PetscCall(PetscDrawAxisSetLabels(axis, "Particles", "V", "f(V)"));
368: PetscCall(PetscDrawAxisSetLimits(axis, -3, 3, 0, 100));
369: PetscCall(PetscDrawHGSetLimits(user->drawhg, -3.0, 3.0, 0, 10));
370: PetscCall(VecGetLocalSize(U, &Np));
371: Np /= dim;
372: PetscCall(VecGetArrayRead(U, &u));
373: /* get points from solution vector */
374: for (p = 0; p < Np; ++p) PetscCall(PetscDrawHGAddValue(user->drawhg, u[p]));
375: PetscCall(PetscDrawHGDraw(user->drawhg));
376: PetscCall(VecRestoreArrayRead(U, &u));
377: }
378: PetscFunctionReturn(PETSC_SUCCESS);
379: }
381: static PetscErrorCode SPMonitor(TS ts, PetscInt step, PetscReal t, Vec U, PetscCtx ctx)
382: {
383: AppCtx *user = (AppCtx *)ctx;
384: const PetscScalar *u;
385: PetscReal *v, *coords;
386: PetscInt Np, p;
387: DM dmSw;
389: PetscFunctionBeginUser;
390: if (step < 0) PetscFunctionReturn(PETSC_SUCCESS);
391: if ((user->ostep > 0) && (!(step % user->ostep))) {
392: PetscDrawAxis axis;
394: PetscCall(TSGetDM(ts, &dmSw));
395: PetscCall(PetscDrawSPReset(user->drawsp));
396: PetscCall(PetscDrawSPGetAxis(user->drawsp, &axis));
397: PetscCall(PetscDrawAxisSetLabels(axis, "Particles", "V", "w"));
398: PetscCall(VecGetLocalSize(U, &Np));
399: PetscCall(VecGetArrayRead(U, &u));
400: /* get points from solution vector */
401: PetscCall(PetscMalloc1(Np, &v));
402: for (p = 0; p < Np; ++p) v[p] = PetscRealPart(u[p]);
403: PetscCall(DMSwarmGetField(dmSw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
404: for (p = 0; p < Np - 1; ++p) PetscCall(PetscDrawSPAddPoint(user->drawsp, &coords[p], &v[p]));
405: PetscCall(PetscDrawSPDraw(user->drawsp, PETSC_TRUE));
406: PetscCall(VecRestoreArrayRead(U, &u));
407: PetscCall(DMSwarmRestoreField(dmSw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
408: PetscCall(PetscFree(v));
409: }
410: PetscFunctionReturn(PETSC_SUCCESS);
411: }
413: static PetscErrorCode KSConv(TS ts, PetscInt step, PetscReal t, Vec U, PetscCtx ctx)
414: {
415: AppCtx *user = (AppCtx *)ctx;
416: const PetscScalar *u;
417: PetscScalar sup;
418: PetscReal *v, *coords, T = 0., vel = 0., step_cast, w_sum;
419: PetscInt dim, Np, p, cStart, cEnd;
420: DM sw, plex;
422: PetscFunctionBeginUser;
423: if (step < 0) PetscFunctionReturn(PETSC_SUCCESS);
424: if ((user->ostep > 0) && (!(step % user->ostep))) {
425: PetscDrawAxis axis;
426: PetscCall(PetscDrawSPGetAxis(user->drawks, &axis));
427: PetscCall(PetscDrawAxisSetLabels(axis, "Particles", "t", "D_n"));
428: PetscCall(PetscDrawSPSetLimits(user->drawks, 0., 100, 0., 3.5));
429: PetscCall(TSGetDM(ts, &sw));
430: PetscCall(VecGetLocalSize(U, &Np));
431: PetscCall(VecGetArrayRead(U, &u));
432: /* get points from solution vector */
433: PetscCall(PetscMalloc1(Np, &v));
434: PetscCall(DMSwarmGetCellDM(sw, &plex));
435: PetscCall(DMGetDimension(sw, &dim));
436: PetscCall(DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd));
437: for (p = 0; p < Np; ++p) v[p] = PetscRealPart(u[p]);
438: PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
439: w_sum = 0.;
440: for (p = 0; p < Np; ++p) {
441: w_sum += u[p];
442: T += u[p] * coords[p] * coords[p];
443: vel += u[p] * coords[p];
444: }
445: vel /= w_sum;
446: T = T / w_sum - vel * vel;
447: sup = 0.0;
448: for (p = 0; p < Np; ++p) {
449: PetscReal tmp = 0.;
450: tmp = PetscAbs(u[p] - ComputePDF(1.0, w_sum, T, &coords[p * dim]));
451: if (tmp > sup) sup = tmp;
452: }
453: step_cast = (PetscReal)step;
454: PetscCall(PetscDrawSPAddPoint(user->drawks, &step_cast, &sup));
455: PetscCall(PetscDrawSPDraw(user->drawks, PETSC_TRUE));
456: PetscCall(VecRestoreArrayRead(U, &u));
457: PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
458: PetscCall(PetscFree(v));
459: }
460: PetscFunctionReturn(PETSC_SUCCESS);
461: }
463: static PetscErrorCode InitializeSolve(TS ts, Vec u)
464: {
465: DM dm;
466: AppCtx *user;
468: PetscFunctionBeginUser;
469: PetscCall(TSGetDM(ts, &dm));
470: PetscCall(DMGetApplicationContext(dm, &user));
471: PetscCall(SetInitialCoordinates(dm));
472: PetscCall(SetInitialConditions(dm, u));
473: PetscFunctionReturn(PETSC_SUCCESS);
474: }
475: /*
476: A single particle is generated for each velocity space cell using the dmswarmpicfield_coor and is used to evaluate collisions in that cell.
477: 0 weight ghost particles are initialized outside of a small velocity domain to ensure the tails of the amxwellian are resolved.
478: */
479: int main(int argc, char **argv)
480: {
481: TS ts; /* nonlinear solver */
482: DM dm, sw; /* Velocity space mesh and Particle Swarm */
483: Vec u, w; /* swarm vector */
484: MPI_Comm comm;
485: AppCtx user;
487: PetscFunctionBeginUser;
488: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
489: comm = PETSC_COMM_WORLD;
490: PetscCall(ProcessOptions(comm, &user));
491: PetscCall(CreateMesh(comm, &dm, &user));
492: PetscCall(CreateParticles(dm, &sw, &user));
493: PetscCall(DMSetApplicationContext(sw, &user));
494: PetscCall(TSCreate(comm, &ts));
495: PetscCall(TSSetDM(ts, sw));
496: PetscCall(TSSetMaxTime(ts, 10.0));
497: PetscCall(TSSetTimeStep(ts, 0.01));
498: PetscCall(TSSetMaxSteps(ts, 100000));
499: PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP));
500: if (user.monitorhg) {
501: PetscCall(PetscDrawCreate(comm, NULL, "monitor", 0, 0, 400, 300, &user.draw));
502: PetscCall(PetscDrawSetFromOptions(user.draw));
503: PetscCall(PetscDrawHGCreate(user.draw, 20, &user.drawhg));
504: PetscCall(PetscDrawHGSetColor(user.drawhg, 3));
505: PetscCall(TSMonitorSet(ts, HGMonitor, &user, NULL));
506: } else if (user.monitorsp) {
507: PetscCall(PetscDrawCreate(comm, NULL, "monitor", 0, 0, 400, 300, &user.draw));
508: PetscCall(PetscDrawSetFromOptions(user.draw));
509: PetscCall(PetscDrawSPCreate(user.draw, 1, &user.drawsp));
510: PetscCall(TSMonitorSet(ts, SPMonitor, &user, NULL));
511: } else if (user.monitorks) {
512: PetscCall(PetscDrawCreate(comm, NULL, "monitor", 0, 0, 400, 300, &user.draw));
513: PetscCall(PetscDrawSetFromOptions(user.draw));
514: PetscCall(PetscDrawSPCreate(user.draw, 1, &user.drawks));
515: PetscCall(TSMonitorSet(ts, KSConv, &user, NULL));
516: }
517: PetscCall(TSSetRHSFunction(ts, NULL, RHSFunctionParticles, &user));
518: PetscCall(TSSetFromOptions(ts));
519: PetscCall(TSSetComputeInitialCondition(ts, InitializeSolve));
520: PetscCall(DMSwarmCreateGlobalVectorFromField(sw, "w_q", &w));
521: PetscCall(VecDuplicate(w, &u));
522: PetscCall(VecCopy(w, u));
523: PetscCall(DMSwarmDestroyGlobalVectorFromField(sw, "w_q", &w));
524: PetscCall(TSComputeInitialCondition(ts, u));
525: PetscCall(TSSolve(ts, u));
526: if (user.monitorhg) {
527: PetscCall(PetscDrawSave(user.draw));
528: PetscCall(PetscDrawHGDestroy(&user.drawhg));
529: PetscCall(PetscDrawDestroy(&user.draw));
530: }
531: if (user.monitorsp) {
532: PetscCall(PetscDrawSave(user.draw));
533: PetscCall(PetscDrawSPDestroy(&user.drawsp));
534: PetscCall(PetscDrawDestroy(&user.draw));
535: }
536: if (user.monitorks) {
537: PetscCall(PetscDrawSave(user.draw));
538: PetscCall(PetscDrawSPDestroy(&user.drawks));
539: PetscCall(PetscDrawDestroy(&user.draw));
540: }
541: PetscCall(VecDestroy(&u));
542: PetscCall(TSDestroy(&ts));
543: PetscCall(DMDestroy(&sw));
544: PetscCall(DMDestroy(&dm));
545: PetscCall(PetscFinalize());
546: return 0;
547: }
549: /*TEST
550: build:
551: requires: double !complex
552: test:
553: suffix: 1
554: args: -particles_per_cell 1 -output_step 10 -ts_type euler -dm_plex_dim 1 -dm_plex_box_faces 200 -dm_plex_box_lower -10 -dm_plex_box_upper 10 -dm_view -monitorsp
555: test:
556: suffix: 2
557: args: -particles_per_cell 1 -output_step 50 -ts_type euler -dm_plex_dim 1 -dm_plex_box_faces 200 -dm_plex_box_lower -10 -dm_plex_box_upper 10 -dm_view -monitorks
558: TEST*/