Actual source code: ex27.c
1: static char help[] = "Particle Basis Landau Example using nonlinear solve + Implicit Midpoint-like time stepping.";
3: /* TODO
5: 1) SNES is sensitive to epsilon (but not to h). Should we do continuation in it?
7: 2) Put this timestepper in library, maybe by changing DG
9: 3) Add monitor to visualize distributions
11: */
13: /* References
14: [1] https://arxiv.org/abs/1910.03080v2
15: */
17: #include <petscdmplex.h>
18: #include <petscdmswarm.h>
19: #include <petscts.h>
21: typedef struct {
22: /* Velocity space grid and functions */
23: PetscInt N; /* The number of partices per spatial cell */
24: PetscReal L; /* Velocity space is [-L, L]^d */
25: PetscReal h; /* Spacing for grid 2L / N^{1/d} */
26: PetscReal epsilon; /* gaussian regularization parameter */
27: PetscReal momentTol; /* Tolerance for checking moment conservation */
28: } AppCtx;
30: static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
31: {
32: PetscFunctionBeginUser;
33: options->N = 1;
34: options->momentTol = 100.0 * PETSC_MACHINE_EPSILON;
35: options->L = 1.0;
36: options->h = -1.0;
37: options->epsilon = -1.0;
39: PetscOptionsBegin(comm, "", "Collision Options", "DMPLEX");
40: PetscCall(PetscOptionsInt("-N", "Number of particles per spatial cell", "ex27.c", options->N, &options->N, NULL));
41: PetscCall(PetscOptionsReal("-L", "Velocity-space extent", "ex27.c", options->L, &options->L, NULL));
42: PetscCall(PetscOptionsReal("-h", "Velocity-space resolution", "ex27.c", options->h, &options->h, NULL));
43: PetscCall(PetscOptionsReal("-epsilon", "Mollifier regularization parameter", "ex27.c", options->epsilon, &options->epsilon, NULL));
44: PetscOptionsEnd();
45: PetscFunctionReturn(PETSC_SUCCESS);
46: }
48: static PetscErrorCode CreateMesh(MPI_Comm comm, DM *dm, AppCtx *user)
49: {
50: PetscFunctionBeginUser;
51: PetscCall(DMCreate(comm, dm));
52: PetscCall(DMSetType(*dm, DMPLEX));
53: PetscCall(DMSetFromOptions(*dm));
54: PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
55: PetscFunctionReturn(PETSC_SUCCESS);
56: }
58: static PetscErrorCode SetInitialCoordinates(DM sw)
59: {
60: AppCtx *user;
61: PetscRandom rnd, rndv;
62: DM dm;
63: DMPolytopeType ct;
64: PetscBool simplex;
65: PetscReal *centroid, *coords, *velocity, *xi0, *v0, *J, *invJ, detJ, *vals;
66: PetscInt dim, d, cStart, cEnd, c, Np, p;
68: PetscFunctionBeginUser;
69: /* Randomization for coordinates */
70: PetscCall(PetscRandomCreate(PetscObjectComm((PetscObject)sw), &rnd));
71: PetscCall(PetscRandomSetInterval(rnd, -1.0, 1.0));
72: PetscCall(PetscRandomSetFromOptions(rnd));
73: PetscCall(PetscRandomCreate(PetscObjectComm((PetscObject)sw), &rndv));
74: PetscCall(PetscRandomSetInterval(rndv, -1., 1.));
75: PetscCall(PetscRandomSetFromOptions(rndv));
76: PetscCall(DMGetApplicationContext(sw, &user));
77: Np = user->N;
78: PetscCall(DMGetDimension(sw, &dim));
79: PetscCall(DMSwarmGetCellDM(sw, &dm));
80: PetscCall(DMGetCoordinatesLocalSetUp(dm));
81: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
82: PetscCall(DMPlexGetCellType(dm, cStart, &ct));
83: simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
84: PetscCall(PetscMalloc5(dim, ¢roid, dim, &xi0, dim, &v0, dim * dim, &J, dim * dim, &invJ));
85: for (d = 0; d < dim; ++d) xi0[d] = -1.0;
86: PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
87: PetscCall(DMSwarmGetField(sw, "velocity", NULL, NULL, (void **)&velocity));
88: PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&vals));
89: for (c = cStart; c < cEnd; ++c) {
90: if (Np == 1) {
91: PetscCall(DMPlexComputeCellGeometryFVM(dm, c, NULL, centroid, NULL));
92: for (d = 0; d < dim; ++d) coords[c * dim + d] = centroid[d];
93: vals[c] = 1.0;
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: /* Random velocity IC */
112: for (c = cStart; c < cEnd; ++c) {
113: for (p = 0; p < Np; ++p) {
114: for (d = 0; d < dim; ++d) {
115: PetscReal v_val;
117: PetscCall(PetscRandomGetValueReal(rndv, &v_val));
118: velocity[p * dim + d] = v_val;
119: }
120: }
121: }
122: PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
123: PetscCall(DMSwarmRestoreField(sw, "velocity", NULL, NULL, (void **)&velocity));
124: PetscCall(DMSwarmRestoreField(sw, "w_q", NULL, NULL, (void **)&vals));
125: PetscCall(PetscFree5(centroid, xi0, v0, J, invJ));
126: PetscCall(PetscRandomDestroy(&rnd));
127: PetscCall(PetscRandomDestroy(&rndv));
128: PetscFunctionReturn(PETSC_SUCCESS);
129: }
131: /* Get velocities from swarm and place in solution vector */
132: static PetscErrorCode SetInitialConditions(DM dmSw, Vec u)
133: {
134: DM dm;
135: AppCtx *user;
136: PetscReal *velocity;
137: PetscScalar *initialConditions;
138: PetscInt dim, d, cStart, cEnd, c, Np, p, n;
140: PetscFunctionBeginUser;
141: PetscCall(VecGetLocalSize(u, &n));
142: PetscCall(DMGetApplicationContext(dmSw, &user));
143: Np = user->N;
144: PetscCall(DMSwarmGetCellDM(dmSw, &dm));
145: PetscCall(DMGetDimension(dm, &dim));
146: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
147: PetscCall(DMSwarmGetField(dmSw, "velocity", NULL, NULL, (void **)&velocity));
148: PetscCall(VecGetArray(u, &initialConditions));
149: for (c = cStart; c < cEnd; ++c) {
150: for (p = 0; p < Np; ++p) {
151: const PetscInt n = c * Np + p;
152: for (d = 0; d < dim; d++) initialConditions[n * dim + d] = velocity[n * dim + d];
153: }
154: }
155: PetscCall(VecRestoreArray(u, &initialConditions));
156: PetscCall(DMSwarmRestoreField(dmSw, "velocity", NULL, NULL, (void **)&velocity));
157: PetscFunctionReturn(PETSC_SUCCESS);
158: }
160: static PetscErrorCode CreateParticles(DM dm, DM *sw, AppCtx *user)
161: {
162: DMSwarmCellDM celldm;
163: PetscInt *swarm_cellid;
164: PetscInt dim, cStart, cEnd, c, Np = user->N, p;
165: PetscBool view = PETSC_FALSE;
166: const char *cellid;
168: PetscFunctionBeginUser;
169: PetscCall(DMGetDimension(dm, &dim));
170: PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), sw));
171: PetscCall(DMSetType(*sw, DMSWARM));
172: PetscCall(DMSetDimension(*sw, dim));
173: /* h = 2L/n and N = n^d */
174: if (user->h < 0.) user->h = 2. * user->L / PetscPowReal(user->N, 1. / dim);
175: /* From Section 4 in [1], \epsilon = 0.64 h^.98 */
176: if (user->epsilon < 0.) user->epsilon = 0.64 * pow(user->h, 1.98);
177: PetscCall(PetscOptionsGetBool(NULL, NULL, "-param_view", &view, NULL));
178: if (view) PetscCall(PetscPrintf(PETSC_COMM_SELF, "N: %" PetscInt_FMT " L: %g h: %g eps: %g\n", user->N, (double)user->L, (double)user->h, (double)user->epsilon));
179: PetscCall(DMSwarmSetType(*sw, DMSWARM_PIC));
180: PetscCall(DMSwarmSetCellDM(*sw, dm));
181: PetscCall(DMSwarmRegisterPetscDatatypeField(*sw, "velocity", dim, PETSC_REAL));
182: PetscCall(DMSwarmRegisterPetscDatatypeField(*sw, "w_q", 1, PETSC_SCALAR));
183: PetscCall(DMSwarmFinalizeFieldRegister(*sw));
184: PetscCall(DMSwarmGetCellDMActive(*sw, &celldm));
185: PetscCall(DMSwarmCellDMGetCellID(celldm, &cellid));
186: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
187: PetscCall(DMSwarmSetLocalSizes(*sw, (cEnd - cStart) * Np, 0));
188: PetscCall(DMSetFromOptions(*sw));
189: PetscCall(DMSwarmGetField(*sw, cellid, NULL, NULL, (void **)&swarm_cellid));
190: for (c = cStart; c < cEnd; ++c) {
191: for (p = 0; p < Np; ++p) {
192: const PetscInt n = c * Np + p;
193: swarm_cellid[n] = c;
194: }
195: }
196: PetscCall(DMSwarmRestoreField(*sw, cellid, NULL, NULL, (void **)&swarm_cellid));
197: PetscCall(PetscObjectSetName((PetscObject)*sw, "Particles"));
198: PetscCall(DMViewFromOptions(*sw, NULL, "-sw_view"));
199: PetscFunctionReturn(PETSC_SUCCESS);
200: }
202: /* Internal dmplex function, same as found in dmpleximpl.h */
203: static void DMPlex_WaxpyD_Internal(PetscInt dim, PetscReal a, const PetscReal *x, const PetscReal *y, PetscReal *w)
204: {
205: PetscInt d;
207: for (d = 0; d < dim; ++d) w[d] = a * x[d] + y[d];
208: }
210: /* Internal dmplex function, same as found in dmpleximpl.h */
211: static PetscReal DMPlex_DotD_Internal(PetscInt dim, const PetscScalar *x, const PetscReal *y)
212: {
213: PetscReal sum = 0.0;
214: PetscInt d;
216: for (d = 0; d < dim; ++d) sum += PetscRealPart(x[d]) * y[d];
217: return sum;
218: }
220: /* Internal dmplex function, same as found in dmpleximpl.h */
221: static void DMPlex_MultAdd2DReal_Internal(const PetscReal A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
222: {
223: PetscScalar z[2];
224: z[0] = x[0];
225: z[1] = x[ldx];
226: y[0] += A[0] * z[0] + A[1] * z[1];
227: y[ldx] += A[2] * z[0] + A[3] * z[1];
228: (void)PetscLogFlops(6.0);
229: }
231: /* Internal dmplex function, same as found in dmpleximpl.h to avoid private includes. */
232: static void DMPlex_MultAdd3DReal_Internal(const PetscReal A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
233: {
234: PetscScalar z[3];
235: z[0] = x[0];
236: z[1] = x[ldx];
237: z[2] = x[ldx * 2];
238: y[0] += A[0] * z[0] + A[1] * z[1] + A[2] * z[2];
239: y[ldx] += A[3] * z[0] + A[4] * z[1] + A[5] * z[2];
240: y[ldx * 2] += A[6] * z[0] + A[7] * z[1] + A[8] * z[2];
241: (void)PetscLogFlops(15.0);
242: }
244: /*
245: Gaussian - The Gaussian function G(x)
247: Input Parameters:
248: + dim - The number of dimensions, or size of x
249: . mu - The mean, or center
250: . sigma - The standard deviation, or width
251: - x - The evaluation point of the function
253: Output Parameter:
254: . ret - The value G(x)
255: */
256: static PetscReal Gaussian(PetscInt dim, const PetscReal mu[], PetscReal sigma, const PetscReal x[])
257: {
258: PetscReal arg = 0.0;
259: PetscInt d;
261: for (d = 0; d < dim; ++d) arg += PetscSqr(x[d] - mu[d]);
262: return PetscPowReal(2.0 * PETSC_PI * sigma, -dim / 2.0) * PetscExpReal(-arg / (2.0 * sigma));
263: }
265: /*
266: ComputeGradS - Compute grad_v dS_eps/df
268: Input Parameters:
269: + dim - The dimension
270: . Np - The number of particles
271: . vp - The velocity v_p of the particle at which we evaluate
272: . velocity - The velocity field for all particles
273: . epsilon - The regularization strength
275: Output Parameter:
276: . integral - The output grad_v dS_eps/df (v_p)
278: Note:
279: This comes from (3.6) in [1], and we are computing
280: $ \nabla_v S_p = \grad \psi_\epsilon(v_p - v) log \sum_q \psi_\epsilon(v - v_q)
281: which is discretized by using a one-point quadrature in each box l at its center v^c_l
282: $ \sum_l h^d \nabla\psi_\epsilon(v_p - v^c_l) \log\left( \sum_q w_q \psi_\epsilon(v^c_l - v_q) \right)
283: where h^d is the volume of each box.
284: */
285: static PetscErrorCode ComputeGradS(PetscInt dim, PetscInt Np, const PetscReal vp[], const PetscReal velocity[], PetscReal integral[], AppCtx *ctx)
286: {
287: PetscReal vc_l[3], L = ctx->L, h = ctx->h, epsilon = ctx->epsilon, init = 0.5 * h - L;
288: PetscInt nx = roundf(2. * L / h);
289: PetscInt ny = dim > 1 ? nx : 1;
290: PetscInt nz = dim > 2 ? nx : 1;
291: PetscInt i, j, k, d, q, dbg = 0;
293: PetscFunctionBeginHot;
294: for (d = 0; d < dim; ++d) integral[d] = 0.0;
295: for (k = 0, vc_l[2] = init; k < nz; ++k, vc_l[2] += h) {
296: for (j = 0, vc_l[1] = init; j < ny; ++j, vc_l[1] += h) {
297: for (i = 0, vc_l[0] = init; i < nx; ++i, vc_l[0] += h) {
298: PetscReal sum = 0.0;
300: if (dbg) PetscCall(PetscPrintf(PETSC_COMM_SELF, "(%" PetscInt_FMT " %" PetscInt_FMT ") vc_l: %g %g\n", i, j, (double)vc_l[0], (double)vc_l[1]));
301: /* \log \sum_k \psi(v - v_k) */
302: for (q = 0; q < Np; ++q) sum += Gaussian(dim, &velocity[q * dim], epsilon, vc_l);
303: sum = PetscLogReal(sum);
304: for (d = 0; d < dim; ++d) integral[d] += (-1. / epsilon) * PetscAbsReal(vp[d] - vc_l[d]) * Gaussian(dim, vp, epsilon, vc_l) * sum;
305: }
306: }
307: }
308: PetscFunctionReturn(PETSC_SUCCESS);
309: }
311: /* Q = 1/|xi| (I - xi xi^T / |xi|^2), xi = vp - vq */
312: static PetscErrorCode QCompute(PetscInt dim, const PetscReal vp[], const PetscReal vq[], PetscReal Q[])
313: {
314: PetscReal xi[3], xi2, xi3, mag;
316: PetscFunctionBeginHot;
317: DMPlex_WaxpyD_Internal(dim, -1.0, vq, vp, xi);
318: xi2 = DMPlex_DotD_Internal(dim, xi, xi);
319: mag = PetscSqrtReal(xi2);
320: xi3 = xi2 * mag;
321: for (PetscInt d = 0; d < dim; ++d) {
322: for (PetscInt e = 0; e < dim; ++e) Q[d * dim + e] = -xi[d] * xi[e] / xi3;
323: Q[d * dim + d] += 1. / mag;
324: }
325: PetscFunctionReturn(PETSC_SUCCESS);
326: }
328: static PetscErrorCode RHSFunctionParticles(TS ts, PetscReal t, Vec U, Vec R, PetscCtx ctx)
329: {
330: AppCtx *user = (AppCtx *)ctx;
331: PetscInt dbg = 0;
332: DM sw; /* Particles */
333: Vec sol; /* Solution vector at current time */
334: const PetscScalar *u; /* input solution vector */
335: PetscScalar *r;
336: PetscReal *velocity;
337: PetscInt dim, Np, p, q;
339: PetscFunctionBeginUser;
340: PetscCall(VecZeroEntries(R));
341: PetscCall(TSGetDM(ts, &sw));
342: PetscCall(DMGetDimension(sw, &dim));
343: PetscCall(VecGetLocalSize(U, &Np));
344: PetscCall(TSGetSolution(ts, &sol));
345: PetscCall(VecGetArray(sol, &velocity));
346: PetscCall(VecGetArray(R, &r));
347: PetscCall(VecGetArrayRead(U, &u));
348: Np /= dim;
349: if (dbg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Part ppr x y\n"));
350: for (p = 0; p < Np; ++p) {
351: PetscReal gradS_p[3] = {0., 0., 0.};
353: PetscCall(ComputeGradS(dim, Np, &velocity[p * dim], velocity, gradS_p, user));
354: for (q = 0; q < Np; ++q) {
355: PetscReal gradS_q[3] = {0., 0., 0.}, GammaS[3] = {0., 0., 0.}, Q[9];
357: if (q == p) continue;
358: PetscCall(ComputeGradS(dim, Np, &velocity[q * dim], velocity, gradS_q, user));
359: DMPlex_WaxpyD_Internal(dim, -1.0, gradS_q, gradS_p, GammaS);
360: PetscCall(QCompute(dim, &u[p * dim], &u[q * dim], Q));
361: switch (dim) {
362: case 2:
363: DMPlex_MultAdd2DReal_Internal(Q, 1, GammaS, &r[p * dim]);
364: break;
365: case 3:
366: DMPlex_MultAdd3DReal_Internal(Q, 1, GammaS, &r[p * dim]);
367: break;
368: default:
369: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Do not support dimension %" PetscInt_FMT, dim);
370: }
371: }
372: if (dbg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Final %4" PetscInt_FMT " %10.8lf %10.8lf\n", p, r[p * dim + 0], r[p * dim + 1]));
373: }
374: PetscCall(VecRestoreArrayRead(U, &u));
375: PetscCall(VecRestoreArray(R, &r));
376: PetscCall(VecRestoreArray(sol, &velocity));
377: PetscCall(VecViewFromOptions(R, NULL, "-residual_view"));
378: PetscFunctionReturn(PETSC_SUCCESS);
379: }
381: /*
382: TS Post Step Function. Copy the solution back into the swarm for migration. We may also need to reform
383: the solution vector in cases of particle migration, but we forgo that here since there is no velocity space grid
384: to migrate between.
385: */
386: static PetscErrorCode UpdateSwarm(TS ts)
387: {
388: PetscInt n;
389: const PetscScalar *u;
390: PetscScalar *velocity;
391: DM sw;
392: Vec sol;
394: PetscFunctionBeginUser;
395: PetscCall(TSGetDM(ts, &sw));
396: PetscCall(DMSwarmGetField(sw, "velocity", NULL, NULL, (void **)&velocity));
397: PetscCall(TSGetSolution(ts, &sol));
398: PetscCall(VecGetArrayRead(sol, &u));
399: PetscCall(VecGetLocalSize(sol, &n));
400: for (PetscInt idx = 0; idx < n; ++idx) velocity[idx] = u[idx];
401: PetscCall(VecRestoreArrayRead(sol, &u));
402: PetscCall(DMSwarmRestoreField(sw, "velocity", NULL, NULL, (void **)&velocity));
403: PetscFunctionReturn(PETSC_SUCCESS);
404: }
406: static PetscErrorCode InitializeSolve(TS ts, Vec u)
407: {
408: DM dm;
409: AppCtx *user;
411: PetscFunctionBeginUser;
412: PetscCall(TSGetDM(ts, &dm));
413: PetscCall(DMGetApplicationContext(dm, &user));
414: PetscCall(SetInitialCoordinates(dm));
415: PetscCall(SetInitialConditions(dm, u));
416: PetscFunctionReturn(PETSC_SUCCESS);
417: }
419: int main(int argc, char **argv)
420: {
421: TS ts; /* nonlinear solver */
422: DM dm, sw; /* Velocity space mesh and Particle Swarm */
423: Vec u, v; /* problem vector */
424: MPI_Comm comm;
425: AppCtx user;
427: PetscFunctionBeginUser;
428: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
429: comm = PETSC_COMM_WORLD;
430: PetscCall(ProcessOptions(comm, &user));
431: /* Initialize objects and set initial conditions */
432: PetscCall(CreateMesh(comm, &dm, &user));
433: PetscCall(CreateParticles(dm, &sw, &user));
434: PetscCall(DMSetApplicationContext(sw, &user));
435: PetscCall(DMSwarmVectorDefineField(sw, "velocity"));
436: PetscCall(TSCreate(comm, &ts));
437: PetscCall(TSSetDM(ts, sw));
438: PetscCall(TSSetMaxTime(ts, 10.0));
439: PetscCall(TSSetTimeStep(ts, 0.1));
440: PetscCall(TSSetMaxSteps(ts, 1));
441: PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP));
442: PetscCall(TSSetRHSFunction(ts, NULL, RHSFunctionParticles, &user));
443: PetscCall(TSSetFromOptions(ts));
444: PetscCall(TSSetComputeInitialCondition(ts, InitializeSolve));
445: PetscCall(DMSwarmCreateGlobalVectorFromField(sw, "velocity", &v));
446: PetscCall(VecDuplicate(v, &u));
447: PetscCall(DMSwarmDestroyGlobalVectorFromField(sw, "velocity", &v));
448: PetscCall(TSComputeInitialCondition(ts, u));
449: PetscCall(TSSetPostStep(ts, UpdateSwarm));
450: PetscCall(TSSolve(ts, u));
451: PetscCall(VecDestroy(&u));
452: PetscCall(TSDestroy(&ts));
453: PetscCall(DMDestroy(&sw));
454: PetscCall(DMDestroy(&dm));
455: PetscCall(PetscFinalize());
456: return 0;
457: }
459: /*TEST
460: build:
461: requires: triangle !single !complex
462: test:
463: suffix: midpoint
464: args: -N 3 -dm_plex_dim 2 -dm_plex_simplex 0 -dm_plex_box_faces 1,1 -dm_plex_box_lower -1,-1 -dm_plex_box_upper 1,1 -dm_view \
465: -ts_type theta -ts_theta_theta 0.5 -ts_dmswarm_monitor_moments -ts_dmswarm_monitor_moments_interval 1 -snes_fd
466: TEST*/