xref: /petsc/src/dm/impls/plex/tests/ex42.c (revision daa037dfd3c3bec8dc8659548d2b20b07c1dc6de)
1 static const char help[] = "Simple libCEED test to calculate surface area using 1^T M 1";
2 
3 /*
4   This is a recreation of libCeed Example 2: https://libceed.readthedocs.io/en/latest/examples/ceed/
5 */
6 
7 #include <petscdmceed.h>
8 #include <petscdmplexceed.h>
9 #include <petscfeceed.h>
10 #include <petscdmplex.h>
11 #include <petscds.h>
12 
13 typedef struct {
14   PetscReal         areaExact;
15   CeedQFunctionUser setupgeo,       apply;
16   const char       *setupgeofname, *applyfname;
17 } AppCtx;
18 
19 typedef struct {
20   CeedQFunction qf_apply;
21   CeedOperator  op_apply;
22   CeedVector    qdata, uceed, vceed;
23 } CeedData;
24 
25 static PetscErrorCode CeedDataDestroy(CeedData *data)
26 {
27   PetscFunctionBeginUser;
28   PetscCall(CeedVectorDestroy(&data->qdata));
29   PetscCall(CeedVectorDestroy(&data->uceed));
30   PetscCall(CeedVectorDestroy(&data->vceed));
31   PetscCall(CeedQFunctionDestroy(&data->qf_apply));
32   PetscCall(CeedOperatorDestroy(&data->op_apply));
33   PetscFunctionReturn(0);
34 }
35 
36 CEED_QFUNCTION(Mass)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out)
37 {
38   const CeedScalar *u = in[0], *qdata = in[1];
39   CeedScalar       *v = out[0];
40 
41   CeedPragmaSIMD
42   for (CeedInt i = 0; i < Q; ++i)
43     v[i] = qdata[i] * u[i];
44 
45   return 0;
46 }
47 
48 /*
49 // Reference (parent) 2D coordinates: X \in [-1, 1]^2
50 //
51 // Global physical coordinates given by the mesh (3D): xx \in [-l, l]^3
52 //
53 // Local physical coordinates on the manifold (2D): x \in [-l, l]^2
54 //
55 // Change of coordinates matrix computed by the library:
56 //   (physical 3D coords relative to reference 2D coords)
57 //   dxx_j/dX_i (indicial notation) [3 * 2]
58 //
59 // Change of coordinates x (physical 2D) relative to xx (phyisical 3D):
60 //   dx_i/dxx_j (indicial notation) [2 * 3]
61 //
62 // Change of coordinates x (physical 2D) relative to X (reference 2D):
63 //   (by chain rule)
64 //   dx_i/dX_j = dx_i/dxx_k * dxx_k/dX_j
65 //
66 // The quadrature data is stored in the array qdata.
67 //
68 // We require the determinant of the Jacobian to properly compute integrals of the form: int(u v)
69 //
70 // Qdata: w * det(dx_i/dX_j)
71 */
72 CEED_QFUNCTION(SetupMassGeoCube)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out)
73 {
74   const CeedScalar *J = in[1], *w = in[2];
75   CeedScalar       *qdata = out[0];
76 
77   CeedPragmaSIMD
78   for (CeedInt i = 0; i < Q; ++i) {
79     // Read dxxdX Jacobian entries, stored as [[0 3], [1 4], [2 5]]
80     const CeedScalar dxxdX[3][2] = {{J[i+Q*0], J[i+Q*3]},
81                                     {J[i+Q*1], J[i+Q*4]},
82                                     {J[i+Q*2], J[i+Q*5]}};
83     // Modulus of dxxdX column vectors
84     const CeedScalar modg1 = PetscSqrtReal(dxxdX[0][0]*dxxdX[0][0] + dxxdX[1][0]*dxxdX[1][0] + dxxdX[2][0]*dxxdX[2][0]);
85     const CeedScalar modg2 = PetscSqrtReal(dxxdX[0][1]*dxxdX[0][1] + dxxdX[1][1]*dxxdX[1][1] + dxxdX[2][1]*dxxdX[2][1]);
86     // Use normalized column vectors of dxxdX as rows of dxdxx
87     const CeedScalar dxdxx[2][3] = {{dxxdX[0][0] / modg1, dxxdX[1][0] / modg1, dxxdX[2][0] / modg1},
88                                     {dxxdX[0][1] / modg2, dxxdX[1][1] / modg2, dxxdX[2][1] / modg2}};
89 
90     CeedScalar dxdX[2][2];
91     for (int j = 0; j < 2; ++j)
92       for (int k = 0; k < 2; ++k) {
93         dxdX[j][k] = 0;
94         for (int l = 0; l < 3; ++l)
95           dxdX[j][k] += dxdxx[j][l]*dxxdX[l][k];
96       }
97     qdata[i+Q*0] = (dxdX[0][0]*dxdX[1][1] - dxdX[1][0]*dxdX[0][1]) * w[i]; /* det J * weight */
98   }
99   return 0;
100 }
101 
102 /*
103 // Reference (parent) 2D coordinates: X \in [-1, 1]^2
104 //
105 // Global 3D physical coordinates given by the mesh: xx \in [-R, R]^3
106 //   with R radius of the sphere
107 //
108 // Local 3D physical coordinates on the 2D manifold: x \in [-l, l]^3
109 //   with l half edge of the cube inscribed in the sphere
110 //
111 // Change of coordinates matrix computed by the library:
112 //   (physical 3D coords relative to reference 2D coords)
113 //   dxx_j/dX_i (indicial notation) [3 * 2]
114 //
115 // Change of coordinates x (on the 2D manifold) relative to xx (phyisical 3D):
116 //   dx_i/dxx_j (indicial notation) [3 * 3]
117 //
118 // Change of coordinates x (on the 2D manifold) relative to X (reference 2D):
119 //   (by chain rule)
120 //   dx_i/dX_j = dx_i/dxx_k * dxx_k/dX_j [3 * 2]
121 //
122 // modJ is given by the magnitude of the cross product of the columns of dx_i/dX_j
123 //
124 // The quadrature data is stored in the array qdata.
125 //
126 // We require the determinant of the Jacobian to properly compute integrals of
127 //   the form: int(u v)
128 //
129 // Qdata: modJ * w
130 */
131 CEED_QFUNCTION(SetupMassGeoSphere)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
132   const CeedScalar *X = in[0], *J = in[1], *w = in[2];
133   CeedScalar       *qdata = out[0];
134 
135   CeedPragmaSIMD
136   for (CeedInt i = 0; i < Q; ++i) {
137     const CeedScalar xx[3][1] = {{X[i+0*Q]}, {X[i+1*Q]}, {X[i+2*Q]}};
138     // Read dxxdX Jacobian entries, stored as [[0 3], [1 4], [2 5]]
139     const CeedScalar dxxdX[3][2] = {{J[i+Q*0], J[i+Q*3]},
140                                     {J[i+Q*1], J[i+Q*4]},
141                                     {J[i+Q*2], J[i+Q*5]}};
142     // Setup
143     const CeedScalar modxxsq = xx[0][0]*xx[0][0]+xx[1][0]*xx[1][0]+xx[2][0]*xx[2][0];
144     CeedScalar xxsq[3][3];
145     for (int j = 0; j < 3; ++j)
146       for (int k = 0; k < 3; ++k) {
147         xxsq[j][k] = 0.;
148         for (int l = 0; l < 1; ++l)
149           xxsq[j][k] += xx[j][l]*xx[k][l] / (sqrt(modxxsq) * modxxsq);
150       }
151 
152     const CeedScalar dxdxx[3][3] = {{1./sqrt(modxxsq) - xxsq[0][0], -xxsq[0][1], -xxsq[0][2]},
153                                     {-xxsq[1][0], 1./sqrt(modxxsq) - xxsq[1][1], -xxsq[1][2]},
154                                     {-xxsq[2][0], -xxsq[2][1], 1./sqrt(modxxsq) - xxsq[2][2]}};
155 
156     CeedScalar dxdX[3][2];
157     for (int j = 0; j < 3; ++j)
158       for (int k = 0; k < 2; ++k) {
159         dxdX[j][k] = 0.;
160         for (int l = 0; l < 3; ++l)
161           dxdX[j][k] += dxdxx[j][l]*dxxdX[l][k];
162       }
163     // J is given by the cross product of the columns of dxdX
164     const CeedScalar J[3][1] = {{dxdX[1][0]*dxdX[2][1] - dxdX[2][0]*dxdX[1][1]},
165                                 {dxdX[2][0]*dxdX[0][1] - dxdX[0][0]*dxdX[2][1]},
166                                 {dxdX[0][0]*dxdX[1][1] - dxdX[1][0]*dxdX[0][1]}};
167     // Use the magnitude of J as our detJ (volume scaling factor)
168     const CeedScalar modJ = sqrt(J[0][0]*J[0][0]+J[1][0]*J[1][0]+J[2][0]*J[2][0]);
169     qdata[i+Q*0] = modJ * w[i];
170   }
171   return 0;
172 }
173 
174 static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *ctx)
175 {
176   DMPlexShape    shape = DM_SHAPE_UNKNOWN;
177   PetscErrorCode ierr;
178 
179   PetscFunctionBeginUser;
180 
181   ierr = PetscOptionsBegin(comm, "", "libCEED Test Options", "DMPLEX");PetscCall(ierr);
182   ierr = PetscOptionsEnd();PetscCall(ierr);
183   PetscCall(PetscOptionsGetEnum(NULL, NULL, "-dm_plex_shape", DMPlexShapes, (PetscEnum *) &shape, NULL));
184   ctx->setupgeo      = NULL;
185   ctx->setupgeofname = NULL;
186   ctx->apply         = Mass;
187   ctx->applyfname    = Mass_loc;
188   ctx->areaExact     = 0.0;
189   switch (shape) {
190     case DM_SHAPE_BOX_SURFACE:
191       ctx->setupgeo      = SetupMassGeoCube;
192       ctx->setupgeofname = SetupMassGeoCube_loc;
193       ctx->areaExact     = 6.0;
194       break;
195     case DM_SHAPE_SPHERE:
196       ctx->setupgeo      = SetupMassGeoSphere;
197       ctx->setupgeofname = SetupMassGeoSphere_loc;
198       ctx->areaExact     = 4.0*M_PI;
199       break;
200     default: break;
201   }
202   PetscFunctionReturn(0);
203 }
204 
205 static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *ctx, DM *dm)
206 {
207   PetscFunctionBegin;
208   PetscCall(DMCreate(comm, dm));
209   PetscCall(DMSetType(*dm, DMPLEX));
210   PetscCall(DMSetFromOptions(*dm));
211   PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
212 #ifdef PETSC_HAVE_LIBCEED
213   {
214     Ceed        ceed;
215     const char *usedresource;
216 
217     PetscCall(DMGetCeed(*dm, &ceed));
218     PetscCall(CeedGetResource(ceed, &usedresource));
219     PetscCall(PetscPrintf(PetscObjectComm((PetscObject) *dm), "libCEED Backend: %s\n", usedresource));
220   }
221 #endif
222   PetscFunctionReturn(0);
223 }
224 
225 static PetscErrorCode SetupDiscretization(DM dm)
226 {
227   DM        cdm;
228   PetscFE   fe, cfe;
229   PetscInt  dim, cnc;
230   PetscBool simplex;
231 
232   PetscFunctionBeginUser;
233   PetscCall(DMGetDimension(dm, &dim));
234   PetscCall(DMPlexIsSimplex(dm, &simplex));
235   PetscCall(PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, NULL, PETSC_DETERMINE, &fe));
236   PetscCall(PetscFESetName(fe, "indicator"));
237   PetscCall(DMAddField(dm, NULL, (PetscObject) fe));
238   PetscCall(PetscFEDestroy(&fe));
239   PetscCall(DMCreateDS(dm));
240   PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL));
241   PetscCall(DMGetCoordinateDim(dm, &cnc));
242   PetscCall(PetscFECreateDefault(PETSC_COMM_SELF, dim, cnc, simplex, NULL, PETSC_DETERMINE, &cfe));
243   PetscCall(DMProjectCoordinates(dm, cfe));
244   PetscCall(PetscFEDestroy(&cfe));
245   PetscCall(DMGetCoordinateDM(dm, &cdm));
246   PetscCall(DMPlexSetClosurePermutationTensor(cdm, PETSC_DETERMINE, NULL));
247   PetscFunctionReturn(0);
248 }
249 
250 static PetscErrorCode LibCeedSetupByDegree(DM dm, AppCtx *ctx, CeedData *data)
251 {
252   PetscDS             ds;
253   PetscFE             fe, cfe;
254   Ceed                ceed;
255   CeedElemRestriction Erestrictx, Erestrictu, Erestrictq;
256   CeedQFunction       qf_setupgeo;
257   CeedOperator        op_setupgeo;
258   CeedVector          xcoord;
259   CeedBasis           basisu, basisx;
260   CeedInt             Nqdata = 1;
261   CeedInt             nqpts, nqptsx;
262   DM                  cdm;
263   Vec                 coords;
264   const PetscScalar  *coordArray;
265   PetscInt            dim, cdim, cStart, cEnd, Ncell;
266 
267   PetscFunctionBeginUser;
268   PetscCall(DMGetCeed(dm, &ceed));
269   PetscCall(DMGetDimension(dm, &dim));
270   PetscCall(DMGetCoordinateDim(dm, &cdim));
271   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
272   Ncell = cEnd - cStart;
273   // CEED bases
274   PetscCall(DMGetDS(dm, &ds));
275   PetscCall(PetscDSGetDiscretization(ds, 0, (PetscObject *) &fe));
276   PetscCall(PetscFEGetCeedBasis(fe, &basisu));
277   PetscCall(DMGetCoordinateDM(dm, &cdm));
278   PetscCall(DMGetDS(cdm, &ds));
279   PetscCall(PetscDSGetDiscretization(ds, 0, (PetscObject *) &cfe));
280   PetscCall(PetscFEGetCeedBasis(cfe, &basisx));
281 
282   PetscCall(DMPlexGetCeedRestriction(cdm, NULL, 0, 0, 0, &Erestrictx));
283   PetscCall(DMPlexGetCeedRestriction(dm,  NULL, 0, 0, 0, &Erestrictu));
284   PetscCall(CeedBasisGetNumQuadraturePoints(basisu, &nqpts));
285   PetscCall(CeedBasisGetNumQuadraturePoints(basisx, &nqptsx));
286   PetscCheckFalse(nqptsx != nqpts,PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Number of qpoints for u %D != %D Number of qpoints for x", nqpts, nqptsx);
287   PetscCall(CeedElemRestrictionCreateStrided(ceed, Ncell, nqpts, Nqdata, Nqdata*Ncell*nqpts, CEED_STRIDES_BACKEND, &Erestrictq));
288 
289   PetscCall(DMGetCoordinatesLocal(dm, &coords));
290   PetscCall(VecGetArrayRead(coords, &coordArray));
291   PetscCall(CeedElemRestrictionCreateVector(Erestrictx, &xcoord, NULL));
292   PetscCall(CeedVectorSetArray(xcoord, CEED_MEM_HOST, CEED_COPY_VALUES, (PetscScalar *) coordArray));
293   PetscCall(VecRestoreArrayRead(coords, &coordArray));
294 
295   // Create the vectors that will be needed in setup and apply
296   PetscCall(CeedElemRestrictionCreateVector(Erestrictu, &data->uceed, NULL));
297   PetscCall(CeedElemRestrictionCreateVector(Erestrictu, &data->vceed, NULL));
298   PetscCall(CeedElemRestrictionCreateVector(Erestrictq, &data->qdata, NULL));
299 
300   // Create the Q-function that builds the operator (i.e. computes its quadrature data) and set its context data
301   PetscCall(CeedQFunctionCreateInterior(ceed, 1, ctx->setupgeo, ctx->setupgeofname, &qf_setupgeo));
302   PetscCall(CeedQFunctionAddInput(qf_setupgeo,  "x",      cdim,     CEED_EVAL_INTERP));
303   PetscCall(CeedQFunctionAddInput(qf_setupgeo,  "dx",     cdim*dim, CEED_EVAL_GRAD));
304   PetscCall(CeedQFunctionAddInput(qf_setupgeo,  "weight", 1,        CEED_EVAL_WEIGHT));
305   PetscCall(CeedQFunctionAddOutput(qf_setupgeo, "qdata",  Nqdata,   CEED_EVAL_NONE));
306 
307   // Set up the mass operator
308   PetscCall(CeedQFunctionCreateInterior(ceed, 1, ctx->apply, ctx->applyfname, &data->qf_apply));
309   PetscCall(CeedQFunctionAddInput(data->qf_apply,  "u",     1,      CEED_EVAL_INTERP));
310   PetscCall(CeedQFunctionAddInput(data->qf_apply,  "qdata", Nqdata, CEED_EVAL_NONE));
311   PetscCall(CeedQFunctionAddOutput(data->qf_apply, "v",     1,      CEED_EVAL_INTERP));
312 
313   // Create the operator that builds the quadrature data for the operator
314   PetscCall(CeedOperatorCreate(ceed, qf_setupgeo, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setupgeo));
315   PetscCall(CeedOperatorSetField(op_setupgeo, "x",      Erestrictx, basisx, CEED_VECTOR_ACTIVE));
316   PetscCall(CeedOperatorSetField(op_setupgeo, "dx",     Erestrictx, basisx, CEED_VECTOR_ACTIVE));
317   PetscCall(CeedOperatorSetField(op_setupgeo, "weight", CEED_ELEMRESTRICTION_NONE, basisx, CEED_VECTOR_NONE));
318   PetscCall(CeedOperatorSetField(op_setupgeo, "qdata",  Erestrictq, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE));
319 
320   // Create the mass operator
321   PetscCall(CeedOperatorCreate(ceed, data->qf_apply, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &data->op_apply));
322   PetscCall(CeedOperatorSetField(data->op_apply, "u",     Erestrictu, basisu, CEED_VECTOR_ACTIVE));
323   PetscCall(CeedOperatorSetField(data->op_apply, "qdata", Erestrictq, CEED_BASIS_COLLOCATED, data->qdata));
324   PetscCall(CeedOperatorSetField(data->op_apply, "v",     Erestrictu, basisu, CEED_VECTOR_ACTIVE));
325 
326   // Setup qdata
327   PetscCall(CeedOperatorApply(op_setupgeo, xcoord, data->qdata, CEED_REQUEST_IMMEDIATE));
328 
329   PetscCall(CeedElemRestrictionDestroy(&Erestrictq));
330   PetscCall(CeedQFunctionDestroy(&qf_setupgeo));
331   PetscCall(CeedOperatorDestroy(&op_setupgeo));
332   PetscCall(CeedVectorDestroy(&xcoord));
333   PetscFunctionReturn(0);
334 }
335 
336 int main(int argc, char **argv)
337 {
338   MPI_Comm       comm;
339   DM             dm;
340   AppCtx         ctx;
341   Vec            U, Uloc, V, Vloc;
342   PetscScalar   *v;
343   PetscScalar    area;
344   CeedData       ceeddata;
345 
346   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
347   comm = PETSC_COMM_WORLD;
348   PetscCall(ProcessOptions(comm, &ctx));
349   PetscCall(CreateMesh(comm, &ctx, &dm));
350   PetscCall(SetupDiscretization(dm));
351 
352   PetscCall(LibCeedSetupByDegree(dm, &ctx, &ceeddata));
353 
354   PetscCall(DMCreateGlobalVector(dm, &U));
355   PetscCall(DMCreateLocalVector(dm, &Uloc));
356   PetscCall(VecDuplicate(U, &V));
357   PetscCall(VecDuplicate(Uloc, &Vloc));
358 
359   /**/
360   PetscCall(VecSet(Uloc, 1.));
361   PetscCall(VecZeroEntries(V));
362   PetscCall(VecZeroEntries(Vloc));
363   PetscCall(VecGetArray(Vloc, &v));
364   PetscCall(CeedVectorSetArray(ceeddata.vceed, CEED_MEM_HOST, CEED_USE_POINTER, v));
365   PetscCall(CeedVectorSetValue(ceeddata.uceed, 1.0));
366   PetscCall(CeedOperatorApply(ceeddata.op_apply, ceeddata.uceed, ceeddata.vceed, CEED_REQUEST_IMMEDIATE));
367   PetscCall(CeedVectorTakeArray(ceeddata.vceed, CEED_MEM_HOST, NULL));
368   PetscCall(VecRestoreArray(Vloc, &v));
369   PetscCall(DMLocalToGlobalBegin(dm, Vloc, ADD_VALUES, V));
370   PetscCall(DMLocalToGlobalEnd(dm, Vloc, ADD_VALUES, V));
371 
372   PetscCall(VecSum(V, &area));
373   if (ctx.areaExact > 0.) {
374     PetscReal error = PetscAbsReal(area - ctx.areaExact);
375     PetscReal tol   = PETSC_SMALL;
376 
377     PetscCall(PetscPrintf(comm,   "Exact mesh surface area    : % .*f\n", fabs(ctx.areaExact - round(ctx.areaExact)) > 1E-15 ? 14 : 1, (double) ctx.areaExact));
378     PetscCall(PetscPrintf(comm,   "Computed mesh surface area : % .*f\n", fabs(area          - round(area))          > 1E-15 ? 14 : 1, (double) area));
379     if (error > tol) {
380       PetscCall(PetscPrintf(comm, "Area error                 : % .14f\n", (double) error));
381     } else {
382       PetscCall(PetscPrintf(comm, "Area verifies!\n", (double) error));
383     }
384   }
385 
386   PetscCall(CeedDataDestroy(&ceeddata));
387   PetscCall(VecDestroy(&U));
388   PetscCall(VecDestroy(&Uloc));
389   PetscCall(VecDestroy(&V));
390   PetscCall(VecDestroy(&Vloc));
391   PetscCall(DMDestroy(&dm));
392   return PetscFinalize();
393 }
394 
395 /*TEST
396 
397   build:
398     requires: libceed
399 
400   testset:
401     args: -dm_plex_simplex 0 -petscspace_degree 3 -dm_view -dm_petscds_view \
402           -petscfe_default_quadrature_order 4 -coord_dm_default_quadrature_order 4
403 
404     test:
405       suffix: cube_3
406       args: -dm_plex_shape box_surface -dm_refine 2
407 
408     test:
409       suffix: cube_3_p4
410       nsize: 4
411       args: -petscpartitioner_type simple -dm_refine_pre 1 -dm_plex_shape box_surface -dm_refine 1
412 
413     test:
414       suffix: sphere_3
415       args: -dm_plex_shape sphere -dm_refine 3
416 
417     test:
418       suffix: sphere_3_p4
419       nsize: 4
420       args: -petscpartitioner_type simple -dm_refine_pre 1 -dm_plex_shape sphere -dm_refine 2
421 
422 TEST*/
423