xref: /petsc/src/dm/impls/swarm/tests/ex2.c (revision 84467f862f2de26368b63ea79dccd665bcda30cd)
1 static char help[] = "Tests L2 projection with DMSwarm using delta function particles and deposition of linear shape.\n";
2 
3 #include <petscdmplex.h>
4 #include <petscfe.h>
5 #include <petscdmswarm.h>
6 #include <petscds.h>
7 #include <petscksp.h>
8 #include <petsc/private/petscfeimpl.h>
9 typedef struct {
10   PetscReal L[3];             /* Dimensions of the mesh bounding box */
11   PetscInt  particlesPerCell; /* The number of partices per cell */
12   PetscReal particleRelDx;    /* Relative particle position perturbation compared to average cell diameter h */
13   PetscReal meshRelDx;        /* Relative vertex position perturbation compared to average cell diameter h */
14   PetscInt  k;                /* Mode number for test function */
15   PetscReal momentTol;        /* Tolerance for checking moment conservation */
16   PetscBool shape;
17   PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *);
18 } AppCtx;
19 
20 /* const char *const ex2FunctionTypes[] = {"linear","x2_x4","sin","ex2FunctionTypes","EX2_FUNCTION_",0}; */
21 
22 static PetscErrorCode linear(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *a_ctx)
23 {
24   AppCtx  *ctx = (AppCtx *)a_ctx;
25   PetscInt d;
26 
27   u[0] = 0.0;
28   for (d = 0; d < dim; ++d) u[0] += x[d] / (ctx->L[d]);
29   return PETSC_SUCCESS;
30 }
31 
32 static PetscErrorCode x2_x4(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *a_ctx)
33 {
34   AppCtx  *ctx = (AppCtx *)a_ctx;
35   PetscInt d;
36 
37   u[0] = 1;
38   for (d = 0; d < dim; ++d) u[0] *= PetscSqr(x[d]) * PetscSqr(ctx->L[d]) - PetscPowRealInt(x[d], 4);
39   return PETSC_SUCCESS;
40 }
41 
42 static PetscErrorCode sinx(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *a_ctx)
43 {
44   AppCtx *ctx = (AppCtx *)a_ctx;
45 
46   u[0] = PetscSinScalar(2 * PETSC_PI * ctx->k * x[0] / (ctx->L[0]));
47   return PETSC_SUCCESS;
48 }
49 
50 static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
51 {
52   char      fstring[PETSC_MAX_PATH_LEN] = "linear";
53   PetscBool flag;
54 
55   PetscFunctionBeginUser;
56   options->particlesPerCell = 1;
57   options->k                = 1;
58   options->particleRelDx    = 1.e-20;
59   options->meshRelDx        = 1.e-20;
60   options->momentTol        = 100. * PETSC_MACHINE_EPSILON;
61   options->shape            = PETSC_FALSE;
62 
63   PetscOptionsBegin(comm, "", "L2 Projection Options", "DMPLEX");
64   PetscCall(PetscOptionsBool("-shape", "Test linear function projection", "ex2.c", options->shape, &options->shape, NULL));
65   PetscCall(PetscOptionsInt("-k", "Mode number of test", "ex2.c", options->k, &options->k, NULL));
66   PetscCall(PetscOptionsInt("-particlesPerCell", "Number of particles per cell", "ex2.c", options->particlesPerCell, &options->particlesPerCell, NULL));
67   PetscCall(PetscOptionsReal("-particle_perturbation", "Relative perturbation of particles (0,1)", "ex2.c", options->particleRelDx, &options->particleRelDx, NULL));
68   PetscCall(PetscOptionsReal("-mesh_perturbation", "Relative perturbation of mesh points (0,1)", "ex2.c", options->meshRelDx, &options->meshRelDx, NULL));
69   PetscCall(PetscOptionsString("-function", "Name of test function", "ex2.c", fstring, fstring, sizeof(fstring), NULL));
70   PetscCall(PetscStrcmp(fstring, "linear", &flag));
71   if (flag) {
72     options->func = linear;
73   } else {
74     PetscCall(PetscStrcmp(fstring, "sin", &flag));
75     if (flag) {
76       options->func = sinx;
77     } else {
78       PetscCall(PetscStrcmp(fstring, "x2_x4", &flag));
79       options->func = x2_x4;
80       PetscCheck(flag, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown function %s", fstring);
81     }
82   }
83   PetscCall(PetscOptionsReal("-moment_tol", "Tolerance for moment checks", "ex2.c", options->momentTol, &options->momentTol, NULL));
84   PetscOptionsEnd();
85   PetscFunctionReturn(PETSC_SUCCESS);
86 }
87 
88 static PetscErrorCode PerturbVertices(DM dm, AppCtx *user)
89 {
90   PetscRandom  rnd;
91   PetscReal    interval = user->meshRelDx;
92   Vec          coordinates;
93   PetscScalar *coords;
94   PetscReal   *hh, low[3], high[3];
95   PetscInt     d, cdim, cEnd, N, p, bs;
96 
97   PetscFunctionBeginUser;
98   PetscCall(DMGetBoundingBox(dm, low, high));
99   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
100   PetscCall(PetscRandomCreate(PetscObjectComm((PetscObject)dm), &rnd));
101   PetscCall(PetscRandomSetInterval(rnd, -interval, interval));
102   PetscCall(PetscRandomSetFromOptions(rnd));
103   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
104   PetscCall(DMGetCoordinateDim(dm, &cdim));
105   PetscCall(PetscCalloc1(cdim, &hh));
106   for (d = 0; d < cdim; ++d) hh[d] = (user->L[d]) / PetscPowReal(cEnd, 1. / cdim);
107   PetscCall(VecGetLocalSize(coordinates, &N));
108   PetscCall(VecGetBlockSize(coordinates, &bs));
109   PetscCheck(bs == cdim, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Coordinate vector has wrong block size %" PetscInt_FMT " != %" PetscInt_FMT, bs, cdim);
110   PetscCall(VecGetArray(coordinates, &coords));
111   for (p = 0; p < N; p += cdim) {
112     PetscScalar *coord = &coords[p], value;
113 
114     for (d = 0; d < cdim; ++d) {
115       PetscCall(PetscRandomGetValue(rnd, &value));
116       coord[d] = PetscMax(low[d], PetscMin(high[d], PetscRealPart(coord[d] + value * hh[d])));
117     }
118   }
119   PetscCall(VecRestoreArray(coordinates, &coords));
120   PetscCall(PetscRandomDestroy(&rnd));
121   PetscCall(PetscFree(hh));
122   PetscFunctionReturn(PETSC_SUCCESS);
123 }
124 
125 static PetscErrorCode CreateMesh(MPI_Comm comm, DM *dm, AppCtx *user)
126 {
127   PetscReal low[3], high[3];
128   PetscInt  cdim, d;
129 
130   PetscFunctionBeginUser;
131   PetscCall(DMCreate(comm, dm));
132   PetscCall(DMSetType(*dm, DMPLEX));
133   PetscCall(DMSetFromOptions(*dm));
134   PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
135 
136   PetscCall(DMGetCoordinateDim(*dm, &cdim));
137   PetscCall(DMGetBoundingBox(*dm, low, high));
138   for (d = 0; d < cdim; ++d) user->L[d] = high[d] - low[d];
139   PetscCall(PerturbVertices(*dm, user));
140   PetscFunctionReturn(PETSC_SUCCESS);
141 }
142 
143 static void identity(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
144 {
145   g0[0] = 1.0;
146 }
147 
148 static PetscErrorCode CreateFEM(DM dm, AppCtx *user)
149 {
150   PetscFE        fe;
151   PetscDS        ds;
152   DMPolytopeType ct;
153   PetscInt       dim, cStart;
154 
155   PetscFunctionBeginUser;
156   PetscCall(DMGetDimension(dm, &dim));
157   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
158   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
159   PetscCall(PetscFECreateByCell(PetscObjectComm((PetscObject)dm), dim, 1, ct, NULL, -1, &fe));
160   PetscCall(PetscObjectSetName((PetscObject)fe, "fe"));
161   PetscCall(DMSetField(dm, 0, NULL, (PetscObject)fe));
162   PetscCall(DMCreateDS(dm));
163   PetscCall(PetscFEDestroy(&fe));
164   /* Setup to form mass matrix */
165   PetscCall(DMGetDS(dm, &ds));
166   PetscCall(PetscDSSetJacobian(ds, 0, 0, identity, NULL, NULL, NULL));
167   PetscFunctionReturn(PETSC_SUCCESS);
168 }
169 
170 static PetscErrorCode CreateParticles(DM dm, DM *sw, AppCtx *user)
171 {
172   PetscRandom    rnd, rndp;
173   PetscReal      interval = user->particleRelDx;
174   DMPolytopeType ct;
175   PetscBool      simplex;
176   PetscScalar    value, *vals;
177   PetscReal     *centroid, *coords, *xi0, *v0, *J, *invJ, detJ;
178   PetscInt      *cellid;
179   PetscInt       Ncell, Np = user->particlesPerCell, p, cStart, c, dim, d;
180 
181   PetscFunctionBeginUser;
182   PetscCall(DMGetDimension(dm, &dim));
183   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
184   PetscCall(DMPlexGetCellType(dm, cStart, &ct));
185   simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct) + 1 ? PETSC_TRUE : PETSC_FALSE;
186   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), sw));
187   PetscCall(DMSetType(*sw, DMSWARM));
188   PetscCall(DMSetDimension(*sw, dim));
189 
190   PetscCall(PetscRandomCreate(PetscObjectComm((PetscObject)dm), &rnd));
191   PetscCall(PetscRandomSetInterval(rnd, -1.0, 1.0));
192   PetscCall(PetscRandomSetFromOptions(rnd));
193   PetscCall(PetscRandomCreate(PetscObjectComm((PetscObject)dm), &rndp));
194   PetscCall(PetscRandomSetInterval(rndp, -interval, interval));
195   PetscCall(PetscRandomSetFromOptions(rndp));
196 
197   PetscCall(DMSwarmSetType(*sw, DMSWARM_PIC));
198   PetscCall(DMSwarmSetCellDM(*sw, dm));
199   PetscCall(DMSwarmRegisterPetscDatatypeField(*sw, "w_q", 1, PETSC_SCALAR));
200   PetscCall(DMSwarmFinalizeFieldRegister(*sw));
201   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &Ncell));
202   PetscCall(DMSwarmSetLocalSizes(*sw, Ncell * Np, 0));
203   PetscCall(DMSetFromOptions(*sw));
204   PetscCall(DMSwarmGetField(*sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
205   PetscCall(DMSwarmGetField(*sw, DMSwarmPICField_cellid, NULL, NULL, (void **)&cellid));
206   PetscCall(DMSwarmGetField(*sw, "w_q", NULL, NULL, (void **)&vals));
207 
208   PetscCall(PetscMalloc5(dim, &centroid, dim, &xi0, dim, &v0, dim * dim, &J, dim * dim, &invJ));
209   for (c = 0; c < Ncell; ++c) {
210     if (Np == 1) {
211       PetscCall(DMPlexComputeCellGeometryFVM(dm, c, NULL, centroid, NULL));
212       cellid[c] = c;
213       for (d = 0; d < dim; ++d) coords[c * dim + d] = centroid[d];
214     } else {
215       PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ)); /* affine */
216       for (d = 0; d < dim; ++d) xi0[d] = -1.0;
217       for (p = 0; p < Np; ++p) {
218         const PetscInt n   = c * Np + p;
219         PetscReal      sum = 0.0, refcoords[3];
220 
221         cellid[n] = c;
222         for (d = 0; d < dim; ++d) {
223           PetscCall(PetscRandomGetValue(rnd, &value));
224           refcoords[d] = PetscRealPart(value);
225           sum += refcoords[d];
226         }
227         if (simplex && sum > 0.0)
228           for (d = 0; d < dim; ++d) refcoords[d] -= PetscSqrtReal(dim) * sum;
229         CoordinatesRefToReal(dim, dim, xi0, v0, J, refcoords, &coords[n * dim]);
230       }
231     }
232   }
233   PetscCall(PetscFree5(centroid, xi0, v0, J, invJ));
234   for (c = 0; c < Ncell; ++c) {
235     for (p = 0; p < Np; ++p) {
236       const PetscInt n = c * Np + p;
237 
238       for (d = 0; d < dim; ++d) {
239         PetscCall(PetscRandomGetValue(rndp, &value));
240         coords[n * dim + d] += PetscRealPart(value);
241       }
242       PetscCall(user->func(dim, 0.0, &coords[n * dim], 1, &vals[c], user));
243     }
244   }
245   PetscCall(DMSwarmRestoreField(*sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
246   PetscCall(DMSwarmRestoreField(*sw, DMSwarmPICField_cellid, NULL, NULL, (void **)&cellid));
247   PetscCall(DMSwarmRestoreField(*sw, "w_q", NULL, NULL, (void **)&vals));
248   PetscCall(PetscRandomDestroy(&rnd));
249   PetscCall(PetscRandomDestroy(&rndp));
250   PetscCall(PetscObjectSetName((PetscObject)*sw, "Particles"));
251   PetscCall(DMSwarmVectorDefineField(*sw, "w_q"));
252   PetscCall(DMViewFromOptions(*sw, NULL, "-sw_view"));
253   PetscFunctionReturn(PETSC_SUCCESS);
254 }
255 
256 static PetscErrorCode CreateParticles_Shape(DM dm, DM *sw, AppCtx *user)
257 {
258   PetscDS          prob;
259   PetscFE          fe;
260   PetscQuadrature  quad;
261   PetscScalar     *vals;
262   PetscReal       *v0, *J, *invJ, detJ, *coords, *xi0;
263   PetscInt        *cellid;
264   const PetscReal *qpoints, *qweights;
265   PetscInt         cStart, cEnd, c, Nq, q, dim;
266 
267   PetscFunctionBeginUser;
268   PetscCall(DMGetDimension(dm, &dim));
269   PetscCall(DMGetDS(dm, &prob));
270   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
271   PetscCall(PetscDSGetDiscretization(prob, 0, (PetscObject *)&fe));
272   PetscCall(PetscFEGetQuadrature(fe, &quad));
273   PetscCall(PetscQuadratureGetData(quad, NULL, NULL, &Nq, &qpoints, &qweights));
274   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), sw));
275   PetscCall(DMSetType(*sw, DMSWARM));
276   PetscCall(DMSetDimension(*sw, dim));
277   PetscCall(DMSwarmSetType(*sw, DMSWARM_PIC));
278   PetscCall(DMSwarmSetCellDM(*sw, dm));
279   PetscCall(DMSwarmRegisterPetscDatatypeField(*sw, "w_q", 1, PETSC_REAL));
280   PetscCall(DMSwarmFinalizeFieldRegister(*sw));
281   PetscCall(DMSwarmSetLocalSizes(*sw, (cEnd - cStart) * Nq, 0));
282   PetscCall(DMSetFromOptions(*sw));
283   PetscCall(PetscMalloc4(dim, &xi0, dim, &v0, dim * dim, &J, dim * dim, &invJ));
284   for (c = 0; c < dim; c++) xi0[c] = -1.;
285   PetscCall(DMSwarmGetField(*sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
286   PetscCall(DMSwarmGetField(*sw, DMSwarmPICField_cellid, NULL, NULL, (void **)&cellid));
287   PetscCall(DMSwarmGetField(*sw, "w_q", NULL, NULL, (void **)&vals));
288   for (c = cStart; c < cEnd; ++c) {
289     for (q = 0; q < Nq; ++q) {
290       PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ));
291       cellid[c * Nq + q] = c;
292       CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q * dim], &coords[(c * Nq + q) * dim]);
293       PetscCall(linear(dim, 0.0, &coords[(c * Nq + q) * dim], 1, &vals[c * Nq + q], (void *)user));
294       vals[c * Nq + q] *= qweights[q] * detJ;
295     }
296   }
297   PetscCall(DMSwarmRestoreField(*sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
298   PetscCall(DMSwarmRestoreField(*sw, DMSwarmPICField_cellid, NULL, NULL, (void **)&cellid));
299   PetscCall(DMSwarmRestoreField(*sw, "w_q", NULL, NULL, (void **)&vals));
300   PetscCall(PetscFree4(xi0, v0, J, invJ));
301   PetscCall(DMSwarmMigrate(*sw, PETSC_FALSE));
302   PetscCall(PetscObjectSetName((PetscObject)*sw, "Particles"));
303   PetscCall(DMSwarmVectorDefineField(*sw, "w_q"));
304   PetscCall(DMViewFromOptions(*sw, NULL, "-sw_view"));
305   PetscFunctionReturn(PETSC_SUCCESS);
306 }
307 
308 static PetscErrorCode computeParticleMoments(DM sw, PetscReal moments[3], AppCtx *user)
309 {
310   DM                 dm;
311   const PetscReal   *coords;
312   const PetscScalar *w;
313   PetscReal          mom[3] = {0.0, 0.0, 0.0};
314   PetscInt           cell, cStart, cEnd, dim;
315 
316   PetscFunctionBeginUser;
317   PetscCall(DMGetDimension(sw, &dim));
318   PetscCall(DMSwarmGetCellDM(sw, &dm));
319   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
320   PetscCall(DMSwarmSortGetAccess(sw));
321   PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
322   PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&w));
323   for (cell = cStart; cell < cEnd; ++cell) {
324     PetscInt *pidx;
325     PetscInt  Np, p, d;
326 
327     PetscCall(DMSwarmSortGetPointsPerCell(sw, cell, &Np, &pidx));
328     for (p = 0; p < Np; ++p) {
329       const PetscInt   idx = pidx[p];
330       const PetscReal *c   = &coords[idx * dim];
331 
332       mom[0] += PetscRealPart(w[idx]);
333       mom[1] += PetscRealPart(w[idx]) * c[0];
334       for (d = 0; d < dim; ++d) mom[2] += PetscRealPart(w[idx]) * c[d] * c[d];
335     }
336     PetscCall(DMSwarmSortRestorePointsPerCell(sw, cell, &Np, &pidx));
337   }
338   PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
339   PetscCall(DMSwarmRestoreField(sw, "w_q", NULL, NULL, (void **)&w));
340   PetscCall(DMSwarmSortRestoreAccess(sw));
341   PetscCallMPI(MPIU_Allreduce(mom, moments, 3, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)sw)));
342   PetscFunctionReturn(PETSC_SUCCESS);
343 }
344 
345 static void f0_1(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
346 {
347   f0[0] = u[0];
348 }
349 
350 static void f0_x(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
351 {
352   f0[0] = x[0] * u[0];
353 }
354 
355 static void f0_r2(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
356 {
357   PetscInt d;
358 
359   f0[0] = 0.0;
360   for (d = 0; d < dim; ++d) f0[0] += PetscSqr(x[d]) * u[0];
361 }
362 
363 static PetscErrorCode computeFEMMoments(DM dm, Vec u, PetscReal moments[3], AppCtx *user)
364 {
365   PetscDS     prob;
366   PetscScalar mom;
367 
368   PetscFunctionBeginUser;
369   PetscCall(DMGetDS(dm, &prob));
370   PetscCall(PetscDSSetObjective(prob, 0, &f0_1));
371   PetscCall(DMPlexComputeIntegralFEM(dm, u, &mom, user));
372   moments[0] = PetscRealPart(mom);
373   PetscCall(PetscDSSetObjective(prob, 0, &f0_x));
374   PetscCall(DMPlexComputeIntegralFEM(dm, u, &mom, user));
375   moments[1] = PetscRealPart(mom);
376   PetscCall(PetscDSSetObjective(prob, 0, &f0_r2));
377   PetscCall(DMPlexComputeIntegralFEM(dm, u, &mom, user));
378   moments[2] = PetscRealPart(mom);
379   PetscFunctionReturn(PETSC_SUCCESS);
380 }
381 
382 static PetscErrorCode TestL2ProjectionParticlesToField(DM dm, DM sw, Vec fhat, AppCtx *user)
383 {
384   const char *fieldnames[1] = {"w_q"};
385   Vec         fields[1]     = {fhat};
386   PetscReal   pmoments[3]; // \int f, \int x f, \int r^2 f
387   PetscReal   fmoments[3]; // \int \hat f, \int x \hat f, \int r^2 \hat f
388 
389   PetscFunctionBeginUser;
390   PetscCall(DMSwarmProjectFields(sw, dm, 1, fieldnames, fields, SCATTER_FORWARD));
391 
392   /* Check moments of field */
393   PetscCall(computeParticleMoments(sw, pmoments, user));
394   PetscCall(computeFEMMoments(dm, fhat, fmoments, user));
395   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "L2 projection mass: %20.10e, x-momentum: %20.10e, energy: %20.10e.\n", fmoments[0], fmoments[1], fmoments[2]));
396   for (PetscInt m = 0; m < 3; ++m) {
397     PetscCheck(PetscAbsReal((fmoments[m] - pmoments[m]) / fmoments[m]) <= user->momentTol, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Moment %" PetscInt_FMT " error too large %g > %g", m, PetscAbsReal((fmoments[m] - pmoments[m]) / fmoments[m]),
398                user->momentTol);
399   }
400   PetscFunctionReturn(PETSC_SUCCESS);
401 }
402 
403 static PetscErrorCode TestL2ProjectionFieldToParticles(DM dm, DM sw, Vec fhat, AppCtx *user)
404 {
405   const char *fieldnames[1] = {"w_q"};
406   Vec         fields[1]     = {fhat};
407   PetscReal   pmoments[3]; // \int f, \int x f, \int r^2 f
408   PetscReal   fmoments[3]; // \int \hat f, \int x \hat f, \int r^2 \hat f
409 
410   PetscFunctionBeginUser;
411   PetscCall(DMSwarmProjectFields(sw, dm, 1, fieldnames, fields, SCATTER_REVERSE));
412 
413   /* Check moments */
414   PetscCall(computeParticleMoments(sw, pmoments, user));
415   PetscCall(computeFEMMoments(dm, fhat, fmoments, user));
416   PetscCall(PetscPrintf(PetscObjectComm((PetscObject)dm), "L2 projection mass: %20.10e, x-momentum: %20.10e, energy: %20.10e.\n", fmoments[0], fmoments[1], fmoments[2]));
417   for (PetscInt m = 0; m < 3; ++m) {
418     PetscCheck(PetscAbsReal((fmoments[m] - pmoments[m]) / fmoments[m]) <= user->momentTol, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Moment %" PetscInt_FMT " error too large %g > %g", m, PetscAbsReal((fmoments[m] - pmoments[m]) / fmoments[m]),
419                user->momentTol);
420   }
421   PetscFunctionReturn(PETSC_SUCCESS);
422 }
423 
424 /* Interpolate the gradient of an FEM (FVM) field. Code repurposed from DMPlexComputeGradientClementInterpolant */
425 static PetscErrorCode InterpolateGradient(DM dm, Vec locX, Vec locC)
426 {
427   DM_Plex         *mesh  = (DM_Plex *)dm->data;
428   PetscInt         debug = mesh->printFEM;
429   DM               dmC;
430   PetscSection     section;
431   PetscQuadrature  quad = NULL;
432   PetscScalar     *interpolant, *gradsum;
433   PetscFEGeom      fegeom;
434   PetscReal       *coords;
435   const PetscReal *quadPoints, *quadWeights;
436   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, vStart, vEnd, v, field, fieldOffset;
437 
438   PetscFunctionBegin;
439   PetscCall(VecGetDM(locC, &dmC));
440   PetscCall(VecSet(locC, 0.0));
441   PetscCall(DMGetDimension(dm, &dim));
442   PetscCall(DMGetCoordinateDim(dm, &coordDim));
443   fegeom.dimEmbed = coordDim;
444   PetscCall(DMGetLocalSection(dm, &section));
445   PetscCall(PetscSectionGetNumFields(section, &numFields));
446   for (field = 0; field < numFields; ++field) {
447     PetscObject  obj;
448     PetscClassId id;
449     PetscInt     Nc;
450 
451     PetscCall(DMGetField(dm, field, NULL, &obj));
452     PetscCall(PetscObjectGetClassId(obj, &id));
453     if (id == PETSCFE_CLASSID) {
454       PetscFE fe = (PetscFE)obj;
455 
456       PetscCall(PetscFEGetQuadrature(fe, &quad));
457       PetscCall(PetscFEGetNumComponents(fe, &Nc));
458     } else if (id == PETSCFV_CLASSID) {
459       PetscFV fv = (PetscFV)obj;
460 
461       PetscCall(PetscFVGetQuadrature(fv, &quad));
462       PetscCall(PetscFVGetNumComponents(fv, &Nc));
463     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %" PetscInt_FMT, field);
464     numComponents += Nc;
465   }
466   PetscCall(PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights));
467   PetscCheck(!(qNc != 1) || !(qNc != numComponents), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Quadrature components %" PetscInt_FMT " != %" PetscInt_FMT " field components", qNc, numComponents);
468   PetscCall(PetscMalloc6(coordDim * numComponents * 2, &gradsum, coordDim * numComponents, &interpolant, coordDim * Nq, &coords, Nq, &fegeom.detJ, coordDim * coordDim * Nq, &fegeom.J, coordDim * coordDim * Nq, &fegeom.invJ));
469   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
470   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
471   for (v = vStart; v < vEnd; ++v) {
472     PetscInt *star = NULL;
473     PetscInt  starSize, st, d, fc;
474 
475     PetscCall(PetscArrayzero(gradsum, coordDim * numComponents));
476     PetscCall(DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star));
477     for (st = 0; st < starSize * 2; st += 2) {
478       const PetscInt cell = star[st];
479       PetscScalar   *grad = &gradsum[coordDim * numComponents];
480       PetscScalar   *x    = NULL;
481 
482       if ((cell < cStart) || (cell >= cEnd)) continue;
483       PetscCall(DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ));
484       PetscCall(DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x));
485       for (field = 0, fieldOffset = 0; field < numFields; ++field) {
486         PetscObject  obj;
487         PetscClassId id;
488         PetscInt     Nb, Nc, q, qc = 0;
489 
490         PetscCall(PetscArrayzero(grad, coordDim * numComponents));
491         PetscCall(DMGetField(dm, field, NULL, &obj));
492         PetscCall(PetscObjectGetClassId(obj, &id));
493         if (id == PETSCFE_CLASSID) {
494           PetscCall(PetscFEGetNumComponents((PetscFE)obj, &Nc));
495           PetscCall(PetscFEGetDimension((PetscFE)obj, &Nb));
496         } else if (id == PETSCFV_CLASSID) {
497           PetscCall(PetscFVGetNumComponents((PetscFV)obj, &Nc));
498           Nb = 1;
499         } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %" PetscInt_FMT, field);
500         for (q = 0; q < Nq; ++q) {
501           PetscCheck(fegeom.detJ[q] > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %" PetscInt_FMT ", quadrature points %" PetscInt_FMT, (double)fegeom.detJ[q], cell, q);
502           if (id == PETSCFE_CLASSID) PetscCall(PetscFEInterpolateGradient_Static((PetscFE)obj, 1, &x[fieldOffset], &fegeom, q, interpolant));
503           else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %" PetscInt_FMT, field);
504           for (fc = 0; fc < Nc; ++fc) {
505             const PetscReal wt = quadWeights[q * qNc + qc + fc];
506 
507             for (d = 0; d < coordDim; ++d) grad[fc * coordDim + d] += interpolant[fc * dim + d] * wt * fegeom.detJ[q];
508           }
509         }
510         fieldOffset += Nb;
511       }
512       PetscCall(DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x));
513       for (fc = 0; fc < numComponents; ++fc) {
514         for (d = 0; d < coordDim; ++d) gradsum[fc * coordDim + d] += grad[fc * coordDim + d];
515       }
516       if (debug) {
517         PetscCall(PetscPrintf(PETSC_COMM_SELF, "Cell %" PetscInt_FMT " gradient: [", cell));
518         for (fc = 0; fc < numComponents; ++fc) {
519           for (d = 0; d < coordDim; ++d) {
520             if (fc || d > 0) PetscCall(PetscPrintf(PETSC_COMM_SELF, ", "));
521             PetscCall(PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc * coordDim + d])));
522           }
523         }
524         PetscCall(PetscPrintf(PETSC_COMM_SELF, "]\n"));
525       }
526     }
527     PetscCall(DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star));
528     PetscCall(DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES));
529   }
530   PetscCall(PetscFree6(gradsum, interpolant, coords, fegeom.detJ, fegeom.J, fegeom.invJ));
531   PetscFunctionReturn(PETSC_SUCCESS);
532 }
533 
534 static PetscErrorCode TestFieldGradientProjection(DM dm, DM sw, AppCtx *user)
535 {
536   MPI_Comm  comm;
537   KSP       ksp;
538   Mat       M;                  /* FEM mass matrix */
539   Mat       M_p;                /* Particle mass matrix */
540   Vec       f, rhs, fhat, grad; /* Particle field f, \int phi_i f, FEM field */
541   PetscReal pmoments[3];        /* \int f, \int x f, \int r^2 f */
542   PetscReal fmoments[3];        /* \int \hat f, \int x \hat f, \int r^2 \hat f */
543   PetscInt  m;
544 
545   PetscFunctionBeginUser;
546   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
547   PetscCall(KSPCreate(comm, &ksp));
548   PetscCall(KSPSetOptionsPrefix(ksp, "ptof_"));
549   PetscCall(DMGetGlobalVector(dm, &fhat));
550   PetscCall(DMGetGlobalVector(dm, &rhs));
551   PetscCall(DMGetGlobalVector(dm, &grad));
552 
553   PetscCall(DMCreateMassMatrix(sw, dm, &M_p));
554   PetscCall(MatViewFromOptions(M_p, NULL, "-M_p_view"));
555 
556   /* make particle weight vector */
557   PetscCall(DMSwarmCreateGlobalVectorFromField(sw, "w_q", &f));
558 
559   /* create matrix RHS vector */
560   PetscCall(MatMultTranspose(M_p, f, rhs));
561   PetscCall(DMSwarmDestroyGlobalVectorFromField(sw, "w_q", &f));
562   PetscCall(PetscObjectSetName((PetscObject)rhs, "rhs"));
563   PetscCall(VecViewFromOptions(rhs, NULL, "-rhs_view"));
564 
565   PetscCall(DMCreateMatrix(dm, &M));
566   PetscCall(DMPlexSNESComputeJacobianFEM(dm, fhat, M, M, user));
567 
568   PetscCall(InterpolateGradient(dm, fhat, grad));
569 
570   PetscCall(MatViewFromOptions(M, NULL, "-M_view"));
571   PetscCall(KSPSetOperators(ksp, M, M));
572   PetscCall(KSPSetFromOptions(ksp));
573   PetscCall(KSPSolve(ksp, rhs, grad));
574   PetscCall(PetscObjectSetName((PetscObject)fhat, "fhat"));
575   PetscCall(VecViewFromOptions(fhat, NULL, "-fhat_view"));
576 
577   /* Check moments of field */
578   PetscCall(computeParticleMoments(sw, pmoments, user));
579   PetscCall(computeFEMMoments(dm, grad, fmoments, user));
580   PetscCall(PetscPrintf(comm, "L2 projection mass: %20.10e, x-momentum: %20.10e, energy: %20.10e.\n", fmoments[0], fmoments[1], fmoments[2]));
581   for (m = 0; m < 3; ++m) {
582     PetscCheck(PetscAbsReal((fmoments[m] - pmoments[m]) / fmoments[m]) <= user->momentTol, comm, PETSC_ERR_ARG_WRONG, "Moment %" PetscInt_FMT " error too large %g > %g", m, PetscAbsReal((fmoments[m] - pmoments[m]) / fmoments[m]), user->momentTol);
583   }
584 
585   PetscCall(KSPDestroy(&ksp));
586   PetscCall(MatDestroy(&M));
587   PetscCall(MatDestroy(&M_p));
588   PetscCall(DMRestoreGlobalVector(dm, &fhat));
589   PetscCall(DMRestoreGlobalVector(dm, &rhs));
590   PetscCall(DMRestoreGlobalVector(dm, &grad));
591   PetscFunctionReturn(PETSC_SUCCESS);
592 }
593 
594 static PetscErrorCode TestL2Projection_Shape(DM dm, DM sw, AppCtx *user)
595 {
596   PetscErrorCode (*funcs[1])(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *);
597   KSP       ksp;
598   Mat       mass;
599   Vec       u, rhs, uproj;
600   void     *ctxs[1];
601   PetscReal error;
602 
603   PetscFunctionBeginUser;
604   ctxs[0]  = (void *)user;
605   funcs[0] = linear;
606 
607   PetscCall(DMSwarmCreateGlobalVectorFromField(sw, "w_q", &u));
608   PetscCall(VecViewFromOptions(u, NULL, "-f_view"));
609   PetscCall(DMGetGlobalVector(dm, &rhs));
610   PetscCall(DMCreateMassMatrix(sw, dm, &mass));
611   PetscCall(MatMultTranspose(mass, u, rhs));
612   PetscCall(VecViewFromOptions(rhs, NULL, "-rhs_view"));
613   PetscCall(MatDestroy(&mass));
614   PetscCall(DMSwarmDestroyGlobalVectorFromField(sw, "w_q", &u));
615   PetscCall(DMGetGlobalVector(dm, &uproj));
616   PetscCall(DMCreateMatrix(dm, &mass));
617   PetscCall(DMPlexSNESComputeJacobianFEM(dm, uproj, mass, mass, user));
618   PetscCall(MatViewFromOptions(mass, NULL, "-mass_mat_view"));
619   PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
620   PetscCall(KSPSetOperators(ksp, mass, mass));
621   PetscCall(KSPSetFromOptions(ksp));
622   PetscCall(KSPSolve(ksp, rhs, uproj));
623   PetscCall(KSPDestroy(&ksp));
624   PetscCall(PetscObjectSetName((PetscObject)uproj, "Full Projection"));
625   PetscCall(VecViewFromOptions(uproj, NULL, "-proj_vec_view"));
626   PetscCall(DMComputeL2Diff(dm, 0.0, funcs, ctxs, uproj, &error));
627   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Projected L2 Error: %g\n", (double)error));
628   PetscCall(MatDestroy(&mass));
629   PetscCall(DMRestoreGlobalVector(dm, &rhs));
630   PetscCall(DMRestoreGlobalVector(dm, &uproj));
631   PetscFunctionReturn(PETSC_SUCCESS);
632 }
633 
634 int main(int argc, char *argv[])
635 {
636   MPI_Comm comm;
637   DM       dm, sw;
638   AppCtx   user;
639 
640   PetscFunctionBeginUser;
641   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
642   comm = PETSC_COMM_WORLD;
643   PetscCall(ProcessOptions(comm, &user));
644   PetscCall(CreateMesh(comm, &dm, &user));
645   PetscCall(CreateFEM(dm, &user));
646   if (!user.shape) {
647     Vec fhat;
648 
649     PetscCall(CreateParticles(dm, &sw, &user));
650     PetscCall(DMGetGlobalVector(dm, &fhat));
651     PetscCall(TestL2ProjectionParticlesToField(dm, sw, fhat, &user));
652     PetscCall(TestL2ProjectionFieldToParticles(dm, sw, fhat, &user));
653     PetscCall(TestFieldGradientProjection(dm, sw, &user));
654     PetscCall(DMRestoreGlobalVector(dm, &fhat));
655   } else {
656     PetscCall(CreateParticles_Shape(dm, &sw, &user));
657     PetscCall(TestL2Projection_Shape(dm, sw, &user));
658   }
659   PetscCall(DMDestroy(&dm));
660   PetscCall(DMDestroy(&sw));
661   PetscCall(PetscFinalize());
662   return 0;
663 }
664 
665 /*TEST
666 
667   # Swarm does not handle complex or quad
668   build:
669     requires: !complex double
670 
671   test:
672     suffix: proj_tri_0
673     requires: triangle
674     args: -dm_plex_box_faces 1,1 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
675     filter: grep -v marker | grep -v atomic | grep -v usage
676 
677   test:
678     suffix: proj_tri_2_faces
679     requires: triangle
680     args: -dm_plex_box_faces 2,2 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
681     filter: grep -v marker | grep -v atomic | grep -v usage
682 
683   test:
684     suffix: proj_quad_0
685     requires: triangle
686     args: -dm_plex_simplex 0 -dm_plex_box_faces 1,1 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
687     filter: grep -v marker | grep -v atomic | grep -v usage
688 
689   test:
690     suffix: proj_quad_2_faces
691     requires: triangle
692     args: -dm_plex_simplex 0 -dm_plex_box_faces 2,2 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
693     filter: grep -v marker | grep -v atomic | grep -v usage
694 
695   test:
696     suffix: proj_tri_5P
697     requires: triangle
698     args: -dm_plex_box_faces 1,1 -particlesPerCell 5 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
699     filter: grep -v marker | grep -v atomic | grep -v usage
700 
701   test:
702     suffix: proj_quad_5P
703     requires: triangle
704     args: -dm_plex_simplex 0 -dm_plex_box_faces 1,1 -particlesPerCell 5 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
705     filter: grep -v marker | grep -v atomic | grep -v usage
706 
707   test:
708     suffix: proj_tri_mdx
709     requires: triangle
710     args: -dm_plex_box_faces 1,1 -mesh_perturbation 1.0e-1 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
711     filter: grep -v marker | grep -v atomic | grep -v usage
712 
713   test:
714     suffix: proj_tri_mdx_5P
715     requires: triangle
716     args: -dm_plex_box_faces 1,1 -particlesPerCell 5 -mesh_perturbation 1.0e-1 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
717     filter: grep -v marker | grep -v atomic | grep -v usage
718 
719   test:
720     suffix: proj_tri_3d
721     requires: ctetgen
722     args: -dm_plex_dim 3 -dm_plex_box_faces 1,1,1 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
723     filter: grep -v marker | grep -v atomic | grep -v usage
724 
725   test:
726     suffix: proj_tri_3d_2_faces
727     requires: ctetgen
728     args: -dm_plex_dim 3 -dm_plex_box_faces 2,2,2 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
729     filter: grep -v marker | grep -v atomic | grep -v usage
730 
731   test:
732     suffix: proj_tri_3d_5P
733     requires: ctetgen
734     args: -dm_plex_dim 3 -dm_plex_box_faces 1,1,1 -particlesPerCell 5 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
735     filter: grep -v marker | grep -v atomic | grep -v usage
736 
737   test:
738     suffix: proj_tri_3d_mdx
739     requires: ctetgen
740     args: -dm_plex_dim 3 -dm_plex_box_faces 1,1,1 -mesh_perturbation 1.0e-1 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
741     filter: grep -v marker | grep -v atomic | grep -v usage
742 
743   test:
744     suffix: proj_tri_3d_mdx_5P
745     requires: ctetgen
746     args: -dm_plex_dim 3 -dm_plex_box_faces 1,1,1 -particlesPerCell 5 -mesh_perturbation 1.0e-1 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
747     filter: grep -v marker | grep -v atomic | grep -v usage
748 
749   test:
750     suffix: proj_tri_3d_mdx_2_faces
751     requires: ctetgen
752     args: -dm_plex_dim 3 -dm_plex_box_faces 2,2,2 -mesh_perturbation 1.0e-1 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
753     filter: grep -v marker | grep -v atomic | grep -v usage
754 
755   test:
756     suffix: proj_tri_3d_mdx_5P_2_faces
757     requires: ctetgen
758     args: -dm_plex_dim 3 -dm_plex_box_faces 2,2,2 -particlesPerCell 5 -mesh_perturbation 1.0e-1 -dm_view -sw_view -petscspace_degree 2 -petscfe_default_quadrature_order {{2 3}} -ptof_pc_type lu -ftop_ksp_rtol 1e-15 -ftop_ksp_type lsqr -ftop_pc_type none
759     filter: grep -v marker | grep -v atomic | grep -v usage
760 
761   test:
762     suffix: proj_quad_lsqr_scale
763     requires: !complex
764     args: -dm_plex_simplex 0 -dm_plex_box_faces 4,4 \
765       -petscspace_degree 2 -petscfe_default_quadrature_order 3 \
766       -particlesPerCell 200 \
767       -ptof_pc_type lu \
768       -ftop_ksp_rtol 1e-17 -ftop_ksp_type lsqr -ftop_pc_type none
769 
770   test:
771     suffix: proj_quad_lsqr_prec_scale
772     requires: !complex
773     args: -dm_plex_simplex 0 -dm_plex_box_faces 4,4 \
774       -petscspace_degree 2 -petscfe_default_quadrature_order 3 \
775       -particlesPerCell 200 \
776       -ptof_pc_type lu \
777       -ftop_ksp_rtol 1e-17 -ftop_ksp_type lsqr -ftop_pc_type bjacobi -ftop_sub_pc_type lu -ftop_sub_pc_factor_shift_type nonzero
778   test:
779     suffix: proj_shape_linear_tri_2d
780     requires: ctetgen triangle
781     args: -shape -dm_plex_box_faces 2,2\
782           -dm_view -sw_view\
783           -petscspace_degree 1 -petscfe_default_quadrature_order 1\
784           -pc_type lu
785   test:
786     suffix: proj_shape_linear_quad_2d
787     requires: ctetgen triangle
788     args: -shape -dm_plex_simplex 0 -dm_plex_box_faces 2,2\
789           -dm_view -sw_view\
790           -petscspace_degree 1 -petscfe_default_quadrature_order 1\
791           -pc_type lu
792   test:
793     suffix: proj_shape_linear_tri_3d
794     requires: ctetgen triangle
795     args: -shape -dm_plex_dim 3 -dm_plex_box_faces 2,2,2\
796           -dm_view -sw_view\
797           -petscspace_degree 1 -petscfe_default_quadrature_order 1\
798           -pc_type lu
799   test:
800     suffix: proj_shape_linear_quad_3d
801     requires: ctetgen triangle
802     args: -shape -dm_plex_dim 3 -dm_plex_simplex 0\
803           -dm_plex_box_faces 2,2,2\
804           -dm_view -sw_view\
805           -petscspace_degree 1 -petscfe_default_quadrature_order 1\
806           -pc_type lu
807 TEST*/
808