Actual source code: plexcreate.c
1: #define PETSCDM_DLL
2: #include <petsc/private/dmpleximpl.h>
3: #include <petsc/private/hashseti.h>
4: #include <petscsf.h>
5: #include <petscdmplextransform.h>
6: #include <petsc/private/kernels/blockmatmult.h>
7: #include <petsc/private/kernels/blockinvert.h>
9: PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
11: /* External function declarations here */
12: static PetscErrorCode DMInitialize_Plex(DM dm);
14: /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
15: PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout)
16: {
17: const PetscReal *maxCell, *Lstart, *L;
18: PetscBool dist;
19: DMPlexReorderDefaultFlag reorder;
21: if (copyPeriodicity) {
22: DMGetPeriodicity(dmin, &maxCell, &Lstart, &L);
23: DMSetPeriodicity(dmout, maxCell, Lstart, L);
24: }
25: DMPlexDistributeGetDefault(dmin, &dist);
26: DMPlexDistributeSetDefault(dmout, dist);
27: DMPlexReorderGetDefault(dmin, &reorder);
28: DMPlexReorderSetDefault(dmout, reorder);
29: ((DM_Plex *)dmout->data)->useHashLocation = ((DM_Plex *)dmin->data)->useHashLocation;
30: if (copyOverlap) DMPlexSetOverlap_Plex(dmout, dmin, 0);
31: return 0;
32: }
34: /* Replace dm with the contents of ndm, and then destroy ndm
35: - Share the DM_Plex structure
36: - Share the coordinates
37: - Share the SF
38: */
39: PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm)
40: {
41: PetscSF sf;
42: DM dmNew = *ndm, coordDM, coarseDM;
43: Vec coords;
44: const PetscReal *maxCell, *Lstart, *L;
45: PetscInt dim, cdim;
47: if (dm == dmNew) {
48: DMDestroy(ndm);
49: return 0;
50: }
51: dm->setupcalled = dmNew->setupcalled;
52: DMGetDimension(dmNew, &dim);
53: DMSetDimension(dm, dim);
54: DMGetCoordinateDim(dmNew, &cdim);
55: DMSetCoordinateDim(dm, cdim);
56: DMGetPointSF(dmNew, &sf);
57: DMSetPointSF(dm, sf);
58: DMGetCoordinateDM(dmNew, &coordDM);
59: DMGetCoordinatesLocal(dmNew, &coords);
60: DMSetCoordinateDM(dm, coordDM);
61: DMSetCoordinatesLocal(dm, coords);
62: DMGetCellCoordinateDM(dmNew, &coordDM);
63: DMGetCellCoordinatesLocal(dmNew, &coords);
64: DMSetCellCoordinateDM(dm, coordDM);
65: DMSetCellCoordinatesLocal(dm, coords);
66: /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
67: DMFieldDestroy(&dm->coordinates[0].field);
68: dm->coordinates[0].field = dmNew->coordinates[0].field;
69: ((DM_Plex *)dmNew->data)->coordFunc = ((DM_Plex *)dm->data)->coordFunc;
70: DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L);
71: DMSetPeriodicity(dm, maxCell, Lstart, L);
72: DMDestroy_Plex(dm);
73: DMInitialize_Plex(dm);
74: dm->data = dmNew->data;
75: ((DM_Plex *)dmNew->data)->refct++;
76: DMDestroyLabelLinkList_Internal(dm);
77: DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL);
78: DMGetCoarseDM(dmNew, &coarseDM);
79: DMSetCoarseDM(dm, coarseDM);
80: DMDestroy(ndm);
81: return 0;
82: }
84: /* Swap dm with the contents of dmNew
85: - Swap the DM_Plex structure
86: - Swap the coordinates
87: - Swap the point PetscSF
88: */
89: static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
90: {
91: DM coordDMA, coordDMB;
92: Vec coordsA, coordsB;
93: PetscSF sfA, sfB;
94: DMField fieldTmp;
95: void *tmp;
96: DMLabelLink listTmp;
97: DMLabel depthTmp;
98: PetscInt tmpI;
100: if (dmA == dmB) return 0;
101: DMGetPointSF(dmA, &sfA);
102: DMGetPointSF(dmB, &sfB);
103: PetscObjectReference((PetscObject)sfA);
104: DMSetPointSF(dmA, sfB);
105: DMSetPointSF(dmB, sfA);
106: PetscObjectDereference((PetscObject)sfA);
108: DMGetCoordinateDM(dmA, &coordDMA);
109: DMGetCoordinateDM(dmB, &coordDMB);
110: PetscObjectReference((PetscObject)coordDMA);
111: DMSetCoordinateDM(dmA, coordDMB);
112: DMSetCoordinateDM(dmB, coordDMA);
113: PetscObjectDereference((PetscObject)coordDMA);
115: DMGetCoordinatesLocal(dmA, &coordsA);
116: DMGetCoordinatesLocal(dmB, &coordsB);
117: PetscObjectReference((PetscObject)coordsA);
118: DMSetCoordinatesLocal(dmA, coordsB);
119: DMSetCoordinatesLocal(dmB, coordsA);
120: PetscObjectDereference((PetscObject)coordsA);
122: DMGetCellCoordinateDM(dmA, &coordDMA);
123: DMGetCellCoordinateDM(dmB, &coordDMB);
124: PetscObjectReference((PetscObject)coordDMA);
125: DMSetCellCoordinateDM(dmA, coordDMB);
126: DMSetCellCoordinateDM(dmB, coordDMA);
127: PetscObjectDereference((PetscObject)coordDMA);
129: DMGetCellCoordinatesLocal(dmA, &coordsA);
130: DMGetCellCoordinatesLocal(dmB, &coordsB);
131: PetscObjectReference((PetscObject)coordsA);
132: DMSetCellCoordinatesLocal(dmA, coordsB);
133: DMSetCellCoordinatesLocal(dmB, coordsA);
134: PetscObjectDereference((PetscObject)coordsA);
136: fieldTmp = dmA->coordinates[0].field;
137: dmA->coordinates[0].field = dmB->coordinates[0].field;
138: dmB->coordinates[0].field = fieldTmp;
139: fieldTmp = dmA->coordinates[1].field;
140: dmA->coordinates[1].field = dmB->coordinates[1].field;
141: dmB->coordinates[1].field = fieldTmp;
142: tmp = dmA->data;
143: dmA->data = dmB->data;
144: dmB->data = tmp;
145: listTmp = dmA->labels;
146: dmA->labels = dmB->labels;
147: dmB->labels = listTmp;
148: depthTmp = dmA->depthLabel;
149: dmA->depthLabel = dmB->depthLabel;
150: dmB->depthLabel = depthTmp;
151: depthTmp = dmA->celltypeLabel;
152: dmA->celltypeLabel = dmB->celltypeLabel;
153: dmB->celltypeLabel = depthTmp;
154: tmpI = dmA->levelup;
155: dmA->levelup = dmB->levelup;
156: dmB->levelup = tmpI;
157: return 0;
158: }
160: static PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
161: {
162: DM idm;
164: DMPlexInterpolate(dm, &idm);
165: DMPlexCopyCoordinates(dm, idm);
166: DMPlexReplace_Internal(dm, &idm);
167: return 0;
168: }
170: /*@C
171: DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
173: Collective on dm
175: Input Parameters:
176: + DM - The DM
177: . degree - The degree of the finite element or PETSC_DECIDE
178: - coordFunc - An optional function to map new points from refinement to the surface
180: Level: advanced
182: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `PetscPointFunc`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
183: @*/
184: PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
185: {
186: DM_Plex *mesh = (DM_Plex *)dm->data;
187: DM cdm;
188: PetscDS cds;
189: PetscFE fe;
190: PetscClassId id;
192: DMGetCoordinateDM(dm, &cdm);
193: DMGetDS(cdm, &cds);
194: PetscDSGetDiscretization(cds, 0, (PetscObject *)&fe);
195: PetscObjectGetClassId((PetscObject)fe, &id);
196: if (id != PETSCFE_CLASSID) {
197: PetscBool simplex;
198: PetscInt dim, dE, qorder;
200: DMGetDimension(dm, &dim);
201: DMGetCoordinateDim(dm, &dE);
202: qorder = degree;
203: PetscObjectOptionsBegin((PetscObject)cdm);
204: PetscOptionsBoundedInt("-coord_dm_default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0);
205: PetscOptionsEnd();
206: if (degree == PETSC_DECIDE) fe = NULL;
207: else {
208: DMPlexIsSimplex(dm, &simplex);
209: PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, degree, qorder, &fe);
210: }
211: DMProjectCoordinates(dm, fe);
212: PetscFEDestroy(&fe);
213: }
214: mesh->coordFunc = coordFunc;
215: return 0;
216: }
218: /*@
219: DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
221: Collective
223: Input Parameters:
224: + comm - The communicator for the `DM` object
225: . dim - The spatial dimension
226: . simplex - Flag for simplicial cells, otherwise they are tensor product cells
227: . interpolate - Flag to create intermediate mesh pieces (edges, faces)
228: - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
230: Output Parameter:
231: . dm - The `DM` object
233: Level: beginner
235: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()`
236: @*/
237: PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
238: {
239: DM dm;
240: PetscMPIInt rank;
242: DMCreate(comm, &dm);
243: DMSetType(dm, DMPLEX);
244: DMSetDimension(dm, dim);
245: MPI_Comm_rank(comm, &rank);
246: switch (dim) {
247: case 2:
248: if (simplex) PetscObjectSetName((PetscObject)dm, "triangular");
249: else PetscObjectSetName((PetscObject)dm, "quadrilateral");
250: break;
251: case 3:
252: if (simplex) PetscObjectSetName((PetscObject)dm, "tetrahedral");
253: else PetscObjectSetName((PetscObject)dm, "hexahedral");
254: break;
255: default:
256: SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
257: }
258: if (rank) {
259: PetscInt numPoints[2] = {0, 0};
260: DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL);
261: } else {
262: switch (dim) {
263: case 2:
264: if (simplex) {
265: PetscInt numPoints[2] = {4, 2};
266: PetscInt coneSize[6] = {3, 3, 0, 0, 0, 0};
267: PetscInt cones[6] = {2, 3, 4, 5, 4, 3};
268: PetscInt coneOrientations[6] = {0, 0, 0, 0, 0, 0};
269: PetscScalar vertexCoords[8] = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
271: DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
272: } else {
273: PetscInt numPoints[2] = {6, 2};
274: PetscInt coneSize[8] = {4, 4, 0, 0, 0, 0, 0, 0};
275: PetscInt cones[8] = {2, 3, 4, 5, 3, 6, 7, 4};
276: PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
277: PetscScalar vertexCoords[12] = {-1.0, -0.5, 0.0, -0.5, 0.0, 0.5, -1.0, 0.5, 1.0, -0.5, 1.0, 0.5};
279: DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
280: }
281: break;
282: case 3:
283: if (simplex) {
284: PetscInt numPoints[2] = {5, 2};
285: PetscInt coneSize[7] = {4, 4, 0, 0, 0, 0, 0};
286: PetscInt cones[8] = {4, 3, 5, 2, 5, 3, 4, 6};
287: PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
288: PetscScalar vertexCoords[15] = {-1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0};
290: DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
291: } else {
292: PetscInt numPoints[2] = {12, 2};
293: PetscInt coneSize[14] = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
294: PetscInt cones[16] = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8};
295: PetscInt coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
296: PetscScalar vertexCoords[36] = {-1.0, -0.5, -0.5, -1.0, 0.5, -0.5, 0.0, 0.5, -0.5, 0.0, -0.5, -0.5, -1.0, -0.5, 0.5, 0.0, -0.5, 0.5, 0.0, 0.5, 0.5, -1.0, 0.5, 0.5, 1.0, 0.5, -0.5, 1.0, -0.5, -0.5, 1.0, -0.5, 0.5, 1.0, 0.5, 0.5};
298: DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
299: }
300: break;
301: default:
302: SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
303: }
304: }
305: *newdm = dm;
306: if (refinementLimit > 0.0) {
307: DM rdm;
308: const char *name;
310: DMPlexSetRefinementUniform(*newdm, PETSC_FALSE);
311: DMPlexSetRefinementLimit(*newdm, refinementLimit);
312: DMRefine(*newdm, comm, &rdm);
313: PetscObjectGetName((PetscObject)*newdm, &name);
314: PetscObjectSetName((PetscObject)rdm, name);
315: DMDestroy(newdm);
316: *newdm = rdm;
317: }
318: if (interpolate) {
319: DM idm;
321: DMPlexInterpolate(*newdm, &idm);
322: DMDestroy(newdm);
323: *newdm = idm;
324: }
325: return 0;
326: }
328: static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
329: {
330: const PetscInt numVertices = 2;
331: PetscInt markerRight = 1;
332: PetscInt markerLeft = 1;
333: PetscBool markerSeparate = PETSC_FALSE;
334: Vec coordinates;
335: PetscSection coordSection;
336: PetscScalar *coords;
337: PetscInt coordSize;
338: PetscMPIInt rank;
339: PetscInt cdim = 1, v;
341: PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
342: if (markerSeparate) {
343: markerRight = 2;
344: markerLeft = 1;
345: }
346: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
347: if (rank == 0) {
348: DMPlexSetChart(dm, 0, numVertices);
349: DMSetUp(dm); /* Allocate space for cones */
350: DMSetLabelValue(dm, "marker", 0, markerLeft);
351: DMSetLabelValue(dm, "marker", 1, markerRight);
352: }
353: DMPlexSymmetrize(dm);
354: DMPlexStratify(dm);
355: /* Build coordinates */
356: DMSetCoordinateDim(dm, cdim);
357: DMGetCoordinateSection(dm, &coordSection);
358: PetscSectionSetNumFields(coordSection, 1);
359: PetscSectionSetChart(coordSection, 0, numVertices);
360: PetscSectionSetFieldComponents(coordSection, 0, cdim);
361: for (v = 0; v < numVertices; ++v) {
362: PetscSectionSetDof(coordSection, v, cdim);
363: PetscSectionSetFieldDof(coordSection, v, 0, cdim);
364: }
365: PetscSectionSetUp(coordSection);
366: PetscSectionGetStorageSize(coordSection, &coordSize);
367: VecCreate(PETSC_COMM_SELF, &coordinates);
368: PetscObjectSetName((PetscObject)coordinates, "coordinates");
369: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
370: VecSetBlockSize(coordinates, cdim);
371: VecSetType(coordinates, VECSTANDARD);
372: VecGetArray(coordinates, &coords);
373: coords[0] = lower[0];
374: coords[1] = upper[0];
375: VecRestoreArray(coordinates, &coords);
376: DMSetCoordinatesLocal(dm, coordinates);
377: VecDestroy(&coordinates);
378: return 0;
379: }
381: static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
382: {
383: const PetscInt numVertices = (edges[0] + 1) * (edges[1] + 1);
384: const PetscInt numEdges = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1];
385: PetscInt markerTop = 1;
386: PetscInt markerBottom = 1;
387: PetscInt markerRight = 1;
388: PetscInt markerLeft = 1;
389: PetscBool markerSeparate = PETSC_FALSE;
390: Vec coordinates;
391: PetscSection coordSection;
392: PetscScalar *coords;
393: PetscInt coordSize;
394: PetscMPIInt rank;
395: PetscInt v, vx, vy;
397: PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
398: if (markerSeparate) {
399: markerTop = 3;
400: markerBottom = 1;
401: markerRight = 2;
402: markerLeft = 4;
403: }
404: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
405: if (rank == 0) {
406: PetscInt e, ex, ey;
408: DMPlexSetChart(dm, 0, numEdges + numVertices);
409: for (e = 0; e < numEdges; ++e) DMPlexSetConeSize(dm, e, 2);
410: DMSetUp(dm); /* Allocate space for cones */
411: for (vx = 0; vx <= edges[0]; vx++) {
412: for (ey = 0; ey < edges[1]; ey++) {
413: PetscInt edge = vx * edges[1] + ey + edges[0] * (edges[1] + 1);
414: PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges;
415: PetscInt cone[2];
417: cone[0] = vertex;
418: cone[1] = vertex + edges[0] + 1;
419: DMPlexSetCone(dm, edge, cone);
420: if (vx == edges[0]) {
421: DMSetLabelValue(dm, "marker", edge, markerRight);
422: DMSetLabelValue(dm, "marker", cone[0], markerRight);
423: if (ey == edges[1] - 1) {
424: DMSetLabelValue(dm, "marker", cone[1], markerRight);
425: DMSetLabelValue(dm, "Face Sets", cone[1], markerRight);
426: }
427: } else if (vx == 0) {
428: DMSetLabelValue(dm, "marker", edge, markerLeft);
429: DMSetLabelValue(dm, "marker", cone[0], markerLeft);
430: if (ey == edges[1] - 1) {
431: DMSetLabelValue(dm, "marker", cone[1], markerLeft);
432: DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft);
433: }
434: }
435: }
436: }
437: for (vy = 0; vy <= edges[1]; vy++) {
438: for (ex = 0; ex < edges[0]; ex++) {
439: PetscInt edge = vy * edges[0] + ex;
440: PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges;
441: PetscInt cone[2];
443: cone[0] = vertex;
444: cone[1] = vertex + 1;
445: DMPlexSetCone(dm, edge, cone);
446: if (vy == edges[1]) {
447: DMSetLabelValue(dm, "marker", edge, markerTop);
448: DMSetLabelValue(dm, "marker", cone[0], markerTop);
449: if (ex == edges[0] - 1) {
450: DMSetLabelValue(dm, "marker", cone[1], markerTop);
451: DMSetLabelValue(dm, "Face Sets", cone[1], markerTop);
452: }
453: } else if (vy == 0) {
454: DMSetLabelValue(dm, "marker", edge, markerBottom);
455: DMSetLabelValue(dm, "marker", cone[0], markerBottom);
456: if (ex == edges[0] - 1) {
457: DMSetLabelValue(dm, "marker", cone[1], markerBottom);
458: DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom);
459: }
460: }
461: }
462: }
463: }
464: DMPlexSymmetrize(dm);
465: DMPlexStratify(dm);
466: /* Build coordinates */
467: DMSetCoordinateDim(dm, 2);
468: DMGetCoordinateSection(dm, &coordSection);
469: PetscSectionSetNumFields(coordSection, 1);
470: PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);
471: PetscSectionSetFieldComponents(coordSection, 0, 2);
472: for (v = numEdges; v < numEdges + numVertices; ++v) {
473: PetscSectionSetDof(coordSection, v, 2);
474: PetscSectionSetFieldDof(coordSection, v, 0, 2);
475: }
476: PetscSectionSetUp(coordSection);
477: PetscSectionGetStorageSize(coordSection, &coordSize);
478: VecCreate(PETSC_COMM_SELF, &coordinates);
479: PetscObjectSetName((PetscObject)coordinates, "coordinates");
480: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
481: VecSetBlockSize(coordinates, 2);
482: VecSetType(coordinates, VECSTANDARD);
483: VecGetArray(coordinates, &coords);
484: for (vy = 0; vy <= edges[1]; ++vy) {
485: for (vx = 0; vx <= edges[0]; ++vx) {
486: coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx;
487: coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy;
488: }
489: }
490: VecRestoreArray(coordinates, &coords);
491: DMSetCoordinatesLocal(dm, coordinates);
492: VecDestroy(&coordinates);
493: return 0;
494: }
496: static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
497: {
498: PetscInt vertices[3], numVertices;
499: PetscInt numFaces = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2];
500: PetscInt markerTop = 1;
501: PetscInt markerBottom = 1;
502: PetscInt markerFront = 1;
503: PetscInt markerBack = 1;
504: PetscInt markerRight = 1;
505: PetscInt markerLeft = 1;
506: PetscBool markerSeparate = PETSC_FALSE;
507: Vec coordinates;
508: PetscSection coordSection;
509: PetscScalar *coords;
510: PetscInt coordSize;
511: PetscMPIInt rank;
512: PetscInt v, vx, vy, vz;
513: PetscInt voffset, iface = 0, cone[4];
516: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
517: PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
518: if (markerSeparate) {
519: markerBottom = 1;
520: markerTop = 2;
521: markerFront = 3;
522: markerBack = 4;
523: markerRight = 5;
524: markerLeft = 6;
525: }
526: vertices[0] = faces[0] + 1;
527: vertices[1] = faces[1] + 1;
528: vertices[2] = faces[2] + 1;
529: numVertices = vertices[0] * vertices[1] * vertices[2];
530: if (rank == 0) {
531: PetscInt f;
533: DMPlexSetChart(dm, 0, numFaces + numVertices);
534: for (f = 0; f < numFaces; ++f) DMPlexSetConeSize(dm, f, 4);
535: DMSetUp(dm); /* Allocate space for cones */
537: /* Side 0 (Top) */
538: for (vy = 0; vy < faces[1]; vy++) {
539: for (vx = 0; vx < faces[0]; vx++) {
540: voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx;
541: cone[0] = voffset;
542: cone[1] = voffset + 1;
543: cone[2] = voffset + vertices[0] + 1;
544: cone[3] = voffset + vertices[0];
545: DMPlexSetCone(dm, iface, cone);
546: DMSetLabelValue(dm, "marker", iface, markerTop);
547: DMSetLabelValue(dm, "marker", voffset + 0, markerTop);
548: DMSetLabelValue(dm, "marker", voffset + 1, markerTop);
549: DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop);
550: DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop);
551: iface++;
552: }
553: }
555: /* Side 1 (Bottom) */
556: for (vy = 0; vy < faces[1]; vy++) {
557: for (vx = 0; vx < faces[0]; vx++) {
558: voffset = numFaces + vy * (faces[0] + 1) + vx;
559: cone[0] = voffset + 1;
560: cone[1] = voffset;
561: cone[2] = voffset + vertices[0];
562: cone[3] = voffset + vertices[0] + 1;
563: DMPlexSetCone(dm, iface, cone);
564: DMSetLabelValue(dm, "marker", iface, markerBottom);
565: DMSetLabelValue(dm, "marker", voffset + 0, markerBottom);
566: DMSetLabelValue(dm, "marker", voffset + 1, markerBottom);
567: DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom);
568: DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom);
569: iface++;
570: }
571: }
573: /* Side 2 (Front) */
574: for (vz = 0; vz < faces[2]; vz++) {
575: for (vx = 0; vx < faces[0]; vx++) {
576: voffset = numFaces + vz * vertices[0] * vertices[1] + vx;
577: cone[0] = voffset;
578: cone[1] = voffset + 1;
579: cone[2] = voffset + vertices[0] * vertices[1] + 1;
580: cone[3] = voffset + vertices[0] * vertices[1];
581: DMPlexSetCone(dm, iface, cone);
582: DMSetLabelValue(dm, "marker", iface, markerFront);
583: DMSetLabelValue(dm, "marker", voffset + 0, markerFront);
584: DMSetLabelValue(dm, "marker", voffset + 1, markerFront);
585: DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront);
586: DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront);
587: iface++;
588: }
589: }
591: /* Side 3 (Back) */
592: for (vz = 0; vz < faces[2]; vz++) {
593: for (vx = 0; vx < faces[0]; vx++) {
594: voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx;
595: cone[0] = voffset + vertices[0] * vertices[1];
596: cone[1] = voffset + vertices[0] * vertices[1] + 1;
597: cone[2] = voffset + 1;
598: cone[3] = voffset;
599: DMPlexSetCone(dm, iface, cone);
600: DMSetLabelValue(dm, "marker", iface, markerBack);
601: DMSetLabelValue(dm, "marker", voffset + 0, markerBack);
602: DMSetLabelValue(dm, "marker", voffset + 1, markerBack);
603: DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack);
604: DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack);
605: iface++;
606: }
607: }
609: /* Side 4 (Left) */
610: for (vz = 0; vz < faces[2]; vz++) {
611: for (vy = 0; vy < faces[1]; vy++) {
612: voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0];
613: cone[0] = voffset;
614: cone[1] = voffset + vertices[0] * vertices[1];
615: cone[2] = voffset + vertices[0] * vertices[1] + vertices[0];
616: cone[3] = voffset + vertices[0];
617: DMPlexSetCone(dm, iface, cone);
618: DMSetLabelValue(dm, "marker", iface, markerLeft);
619: DMSetLabelValue(dm, "marker", voffset + 0, markerLeft);
620: DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft);
621: DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft);
622: DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft);
623: iface++;
624: }
625: }
627: /* Side 5 (Right) */
628: for (vz = 0; vz < faces[2]; vz++) {
629: for (vy = 0; vy < faces[1]; vy++) {
630: voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0];
631: cone[0] = voffset + vertices[0] * vertices[1];
632: cone[1] = voffset;
633: cone[2] = voffset + vertices[0];
634: cone[3] = voffset + vertices[0] * vertices[1] + vertices[0];
635: DMPlexSetCone(dm, iface, cone);
636: DMSetLabelValue(dm, "marker", iface, markerRight);
637: DMSetLabelValue(dm, "marker", voffset + 0, markerRight);
638: DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight);
639: DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight);
640: DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight);
641: iface++;
642: }
643: }
644: }
645: DMPlexSymmetrize(dm);
646: DMPlexStratify(dm);
647: /* Build coordinates */
648: DMSetCoordinateDim(dm, 3);
649: DMGetCoordinateSection(dm, &coordSection);
650: PetscSectionSetNumFields(coordSection, 1);
651: PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);
652: PetscSectionSetFieldComponents(coordSection, 0, 3);
653: for (v = numFaces; v < numFaces + numVertices; ++v) {
654: PetscSectionSetDof(coordSection, v, 3);
655: PetscSectionSetFieldDof(coordSection, v, 0, 3);
656: }
657: PetscSectionSetUp(coordSection);
658: PetscSectionGetStorageSize(coordSection, &coordSize);
659: VecCreate(PETSC_COMM_SELF, &coordinates);
660: PetscObjectSetName((PetscObject)coordinates, "coordinates");
661: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
662: VecSetBlockSize(coordinates, 3);
663: VecSetType(coordinates, VECSTANDARD);
664: VecGetArray(coordinates, &coords);
665: for (vz = 0; vz <= faces[2]; ++vz) {
666: for (vy = 0; vy <= faces[1]; ++vy) {
667: for (vx = 0; vx <= faces[0]; ++vx) {
668: coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx;
669: coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy;
670: coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz;
671: }
672: }
673: }
674: VecRestoreArray(coordinates, &coords);
675: DMSetCoordinatesLocal(dm, coordinates);
676: VecDestroy(&coordinates);
677: return 0;
678: }
680: static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
681: {
683: DMSetDimension(dm, dim - 1);
684: DMSetCoordinateDim(dm, dim);
685: switch (dim) {
686: case 1:
687: DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces);
688: break;
689: case 2:
690: DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces);
691: break;
692: case 3:
693: DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces);
694: break;
695: default:
696: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
697: }
698: if (interpolate) DMPlexInterpolateInPlace_Internal(dm);
699: return 0;
700: }
702: /*@C
703: DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
705: Collective
707: Input Parameters:
708: + comm - The communicator for the `DM` object
709: . dim - The spatial dimension of the box, so the resulting mesh is has dimension dim-1
710: . faces - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
711: . lower - The lower left corner, or NULL for (0, 0, 0)
712: . upper - The upper right corner, or NULL for (1, 1, 1)
713: - interpolate - Flag to create intermediate mesh pieces (edges, faces)
715: Output Parameter:
716: . dm - The `DM` object
718: Level: beginner
720: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
721: @*/
722: PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
723: {
724: PetscInt fac[3] = {1, 1, 1};
725: PetscReal low[3] = {0, 0, 0};
726: PetscReal upp[3] = {1, 1, 1};
728: DMCreate(comm, dm);
729: DMSetType(*dm, DMPLEX);
730: DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate);
731: return 0;
732: }
734: static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd)
735: {
736: PetscInt i, fStart, fEnd, numCells = 0, numVerts = 0;
737: PetscInt numPoints[2], *coneSize, *cones, *coneOrientations;
738: PetscScalar *vertexCoords;
739: PetscReal L, maxCell;
740: PetscBool markerSeparate = PETSC_FALSE;
741: PetscInt markerLeft = 1, faceMarkerLeft = 1;
742: PetscInt markerRight = 1, faceMarkerRight = 2;
743: PetscBool wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
744: PetscMPIInt rank;
748: DMSetDimension(dm, 1);
749: DMCreateLabel(dm, "marker");
750: DMCreateLabel(dm, "Face Sets");
752: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
753: if (rank == 0) numCells = segments;
754: if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
756: numPoints[0] = numVerts;
757: numPoints[1] = numCells;
758: PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords);
759: PetscArrayzero(coneOrientations, numCells + numVerts);
760: for (i = 0; i < numCells; ++i) coneSize[i] = 2;
761: for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0;
762: for (i = 0; i < numCells; ++i) {
763: cones[2 * i] = numCells + i % numVerts;
764: cones[2 * i + 1] = numCells + (i + 1) % numVerts;
765: }
766: for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells);
767: DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
768: PetscFree4(coneSize, cones, coneOrientations, vertexCoords);
770: PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
771: if (markerSeparate) {
772: markerLeft = faceMarkerLeft;
773: markerRight = faceMarkerRight;
774: }
775: if (!wrap && rank == 0) {
776: DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);
777: DMSetLabelValue(dm, "marker", fStart, markerLeft);
778: DMSetLabelValue(dm, "marker", fEnd - 1, markerRight);
779: DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft);
780: DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight);
781: }
782: if (wrap) {
783: L = upper - lower;
784: maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments));
785: DMSetPeriodicity(dm, &maxCell, &lower, &L);
786: }
787: DMPlexSetRefinementUniform(dm, PETSC_TRUE);
788: return 0;
789: }
791: static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
792: {
793: DM boundary, vol;
794: DMLabel bdlabel;
798: DMCreate(PetscObjectComm((PetscObject)dm), &boundary);
799: DMSetType(boundary, DMPLEX);
800: DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE);
801: DMPlexGenerate(boundary, NULL, interpolate, &vol);
802: DMGetLabel(vol, "marker", &bdlabel);
803: if (bdlabel) DMPlexLabelComplete(vol, bdlabel);
804: DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol);
805: DMPlexReplace_Internal(dm, &vol);
806: DMDestroy(&boundary);
807: return 0;
808: }
810: static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
811: {
812: DMLabel cutLabel = NULL;
813: PetscInt markerTop = 1, faceMarkerTop = 1;
814: PetscInt markerBottom = 1, faceMarkerBottom = 1;
815: PetscInt markerFront = 1, faceMarkerFront = 1;
816: PetscInt markerBack = 1, faceMarkerBack = 1;
817: PetscInt markerRight = 1, faceMarkerRight = 1;
818: PetscInt markerLeft = 1, faceMarkerLeft = 1;
819: PetscInt dim;
820: PetscBool markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
821: PetscMPIInt rank;
823: DMGetDimension(dm, &dim);
824: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
825: DMCreateLabel(dm, "marker");
826: DMCreateLabel(dm, "Face Sets");
827: PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL);
828: if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST || bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST || bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {
829: if (cutMarker) {
830: DMCreateLabel(dm, "periodic_cut");
831: DMGetLabel(dm, "periodic_cut", &cutLabel);
832: }
833: }
834: switch (dim) {
835: case 2:
836: faceMarkerTop = 3;
837: faceMarkerBottom = 1;
838: faceMarkerRight = 2;
839: faceMarkerLeft = 4;
840: break;
841: case 3:
842: faceMarkerBottom = 1;
843: faceMarkerTop = 2;
844: faceMarkerFront = 3;
845: faceMarkerBack = 4;
846: faceMarkerRight = 5;
847: faceMarkerLeft = 6;
848: break;
849: default:
850: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
851: }
852: PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);
853: if (markerSeparate) {
854: markerBottom = faceMarkerBottom;
855: markerTop = faceMarkerTop;
856: markerFront = faceMarkerFront;
857: markerBack = faceMarkerBack;
858: markerRight = faceMarkerRight;
859: markerLeft = faceMarkerLeft;
860: }
861: {
862: const PetscInt numXEdges = rank == 0 ? edges[0] : 0;
863: const PetscInt numYEdges = rank == 0 ? edges[1] : 0;
864: const PetscInt numZEdges = rank == 0 ? edges[2] : 0;
865: const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
866: const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
867: const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
868: const PetscInt numCells = numXEdges * numYEdges * numZEdges;
869: const PetscInt numXFaces = numYEdges * numZEdges;
870: const PetscInt numYFaces = numXEdges * numZEdges;
871: const PetscInt numZFaces = numXEdges * numYEdges;
872: const PetscInt numTotXFaces = numXVertices * numXFaces;
873: const PetscInt numTotYFaces = numYVertices * numYFaces;
874: const PetscInt numTotZFaces = numZVertices * numZFaces;
875: const PetscInt numFaces = numTotXFaces + numTotYFaces + numTotZFaces;
876: const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
877: const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
878: const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
879: const PetscInt numVertices = numXVertices * numYVertices * numZVertices;
880: const PetscInt numEdges = numTotXEdges + numTotYEdges + numTotZEdges;
881: const PetscInt firstVertex = (dim == 2) ? numFaces : numCells;
882: const PetscInt firstXFace = (dim == 2) ? 0 : numCells + numVertices;
883: const PetscInt firstYFace = firstXFace + numTotXFaces;
884: const PetscInt firstZFace = firstYFace + numTotYFaces;
885: const PetscInt firstXEdge = numCells + numFaces + numVertices;
886: const PetscInt firstYEdge = firstXEdge + numTotXEdges;
887: const PetscInt firstZEdge = firstYEdge + numTotYEdges;
888: Vec coordinates;
889: PetscSection coordSection;
890: PetscScalar *coords;
891: PetscInt coordSize;
892: PetscInt v, vx, vy, vz;
893: PetscInt c, f, fx, fy, fz, e, ex, ey, ez;
895: DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices);
896: for (c = 0; c < numCells; c++) DMPlexSetConeSize(dm, c, 6);
897: for (f = firstXFace; f < firstXFace + numFaces; ++f) DMPlexSetConeSize(dm, f, 4);
898: for (e = firstXEdge; e < firstXEdge + numEdges; ++e) DMPlexSetConeSize(dm, e, 2);
899: DMSetUp(dm); /* Allocate space for cones */
900: /* Build cells */
901: for (fz = 0; fz < numZEdges; ++fz) {
902: for (fy = 0; fy < numYEdges; ++fy) {
903: for (fx = 0; fx < numXEdges; ++fx) {
904: PetscInt cell = (fz * numYEdges + fy) * numXEdges + fx;
905: PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
906: PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
907: PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
908: PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
909: PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
910: PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
911: /* B, T, F, K, R, L */
912: PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
913: PetscInt cone[6];
915: /* no boundary twisting in 3D */
916: cone[0] = faceB;
917: cone[1] = faceT;
918: cone[2] = faceF;
919: cone[3] = faceK;
920: cone[4] = faceR;
921: cone[5] = faceL;
922: DMPlexSetCone(dm, cell, cone);
923: DMPlexSetConeOrientation(dm, cell, ornt);
924: if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) DMLabelSetValue(cutLabel, cell, 2);
925: if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) DMLabelSetValue(cutLabel, cell, 2);
926: if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) DMLabelSetValue(cutLabel, cell, 2);
927: }
928: }
929: }
930: /* Build x faces */
931: for (fz = 0; fz < numZEdges; ++fz) {
932: for (fy = 0; fy < numYEdges; ++fy) {
933: for (fx = 0; fx < numXVertices; ++fx) {
934: PetscInt face = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
935: PetscInt edgeL = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
936: PetscInt edgeR = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
937: PetscInt edgeB = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
938: PetscInt edgeT = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
939: PetscInt ornt[4] = {0, 0, -1, -1};
940: PetscInt cone[4];
942: if (dim == 3) {
943: /* markers */
944: if (bdX != DM_BOUNDARY_PERIODIC) {
945: if (fx == numXVertices - 1) {
946: DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);
947: DMSetLabelValue(dm, "marker", face, markerRight);
948: } else if (fx == 0) {
949: DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);
950: DMSetLabelValue(dm, "marker", face, markerLeft);
951: }
952: }
953: }
954: cone[0] = edgeB;
955: cone[1] = edgeR;
956: cone[2] = edgeT;
957: cone[3] = edgeL;
958: DMPlexSetCone(dm, face, cone);
959: DMPlexSetConeOrientation(dm, face, ornt);
960: }
961: }
962: }
963: /* Build y faces */
964: for (fz = 0; fz < numZEdges; ++fz) {
965: for (fx = 0; fx < numXEdges; ++fx) {
966: for (fy = 0; fy < numYVertices; ++fy) {
967: PetscInt face = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
968: PetscInt edgeL = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
969: PetscInt edgeR = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
970: PetscInt edgeB = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
971: PetscInt edgeT = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
972: PetscInt ornt[4] = {0, 0, -1, -1};
973: PetscInt cone[4];
975: if (dim == 3) {
976: /* markers */
977: if (bdY != DM_BOUNDARY_PERIODIC) {
978: if (fy == numYVertices - 1) {
979: DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);
980: DMSetLabelValue(dm, "marker", face, markerBack);
981: } else if (fy == 0) {
982: DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);
983: DMSetLabelValue(dm, "marker", face, markerFront);
984: }
985: }
986: }
987: cone[0] = edgeB;
988: cone[1] = edgeR;
989: cone[2] = edgeT;
990: cone[3] = edgeL;
991: DMPlexSetCone(dm, face, cone);
992: DMPlexSetConeOrientation(dm, face, ornt);
993: }
994: }
995: }
996: /* Build z faces */
997: for (fy = 0; fy < numYEdges; ++fy) {
998: for (fx = 0; fx < numXEdges; ++fx) {
999: for (fz = 0; fz < numZVertices; fz++) {
1000: PetscInt face = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
1001: PetscInt edgeL = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
1002: PetscInt edgeR = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
1003: PetscInt edgeB = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
1004: PetscInt edgeT = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1005: PetscInt ornt[4] = {0, 0, -1, -1};
1006: PetscInt cone[4];
1008: if (dim == 2) {
1009: if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
1010: edgeR += numYEdges - 1 - 2 * fy;
1011: ornt[1] = -1;
1012: }
1013: if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
1014: edgeT += numXEdges - 1 - 2 * fx;
1015: ornt[2] = 0;
1016: }
1017: if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) DMLabelSetValue(cutLabel, face, 2);
1018: if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) DMLabelSetValue(cutLabel, face, 2);
1019: } else {
1020: /* markers */
1021: if (bdZ != DM_BOUNDARY_PERIODIC) {
1022: if (fz == numZVertices - 1) {
1023: DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);
1024: DMSetLabelValue(dm, "marker", face, markerTop);
1025: } else if (fz == 0) {
1026: DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);
1027: DMSetLabelValue(dm, "marker", face, markerBottom);
1028: }
1029: }
1030: }
1031: cone[0] = edgeB;
1032: cone[1] = edgeR;
1033: cone[2] = edgeT;
1034: cone[3] = edgeL;
1035: DMPlexSetCone(dm, face, cone);
1036: DMPlexSetConeOrientation(dm, face, ornt);
1037: }
1038: }
1039: }
1040: /* Build Z edges*/
1041: for (vy = 0; vy < numYVertices; vy++) {
1042: for (vx = 0; vx < numXVertices; vx++) {
1043: for (ez = 0; ez < numZEdges; ez++) {
1044: const PetscInt edge = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
1045: const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
1046: const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
1047: PetscInt cone[2];
1049: cone[0] = vertexB;
1050: cone[1] = vertexT;
1051: DMPlexSetCone(dm, edge, cone);
1052: if (dim == 3) {
1053: if (bdX != DM_BOUNDARY_PERIODIC) {
1054: if (vx == numXVertices - 1) {
1055: DMSetLabelValue(dm, "marker", edge, markerRight);
1056: DMSetLabelValue(dm, "marker", cone[0], markerRight);
1057: if (ez == numZEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerRight);
1058: } else if (vx == 0) {
1059: DMSetLabelValue(dm, "marker", edge, markerLeft);
1060: DMSetLabelValue(dm, "marker", cone[0], markerLeft);
1061: if (ez == numZEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerLeft);
1062: }
1063: }
1064: if (bdY != DM_BOUNDARY_PERIODIC) {
1065: if (vy == numYVertices - 1) {
1066: DMSetLabelValue(dm, "marker", edge, markerBack);
1067: DMSetLabelValue(dm, "marker", cone[0], markerBack);
1068: if (ez == numZEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerBack);
1069: } else if (vy == 0) {
1070: DMSetLabelValue(dm, "marker", edge, markerFront);
1071: DMSetLabelValue(dm, "marker", cone[0], markerFront);
1072: if (ez == numZEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerFront);
1073: }
1074: }
1075: }
1076: }
1077: }
1078: }
1079: /* Build Y edges*/
1080: for (vz = 0; vz < numZVertices; vz++) {
1081: for (vx = 0; vx < numXVertices; vx++) {
1082: for (ey = 0; ey < numYEdges; ey++) {
1083: const PetscInt nextv = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
1084: const PetscInt edge = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
1085: const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
1086: const PetscInt vertexK = firstVertex + nextv;
1087: PetscInt cone[2];
1089: cone[0] = vertexF;
1090: cone[1] = vertexK;
1091: DMPlexSetCone(dm, edge, cone);
1092: if (dim == 2) {
1093: if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
1094: if (vx == numXVertices - 1) {
1095: DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);
1096: DMSetLabelValue(dm, "marker", edge, markerRight);
1097: DMSetLabelValue(dm, "marker", cone[0], markerRight);
1098: if (ey == numYEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerRight);
1099: } else if (vx == 0) {
1100: DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);
1101: DMSetLabelValue(dm, "marker", edge, markerLeft);
1102: DMSetLabelValue(dm, "marker", cone[0], markerLeft);
1103: if (ey == numYEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerLeft);
1104: }
1105: } else {
1106: if (vx == 0 && cutLabel) {
1107: DMLabelSetValue(cutLabel, edge, 1);
1108: DMLabelSetValue(cutLabel, cone[0], 1);
1109: if (ey == numYEdges - 1) DMLabelSetValue(cutLabel, cone[1], 1);
1110: }
1111: }
1112: } else {
1113: if (bdX != DM_BOUNDARY_PERIODIC) {
1114: if (vx == numXVertices - 1) {
1115: DMSetLabelValue(dm, "marker", edge, markerRight);
1116: DMSetLabelValue(dm, "marker", cone[0], markerRight);
1117: if (ey == numYEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerRight);
1118: } else if (vx == 0) {
1119: DMSetLabelValue(dm, "marker", edge, markerLeft);
1120: DMSetLabelValue(dm, "marker", cone[0], markerLeft);
1121: if (ey == numYEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerLeft);
1122: }
1123: }
1124: if (bdZ != DM_BOUNDARY_PERIODIC) {
1125: if (vz == numZVertices - 1) {
1126: DMSetLabelValue(dm, "marker", edge, markerTop);
1127: DMSetLabelValue(dm, "marker", cone[0], markerTop);
1128: if (ey == numYEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerTop);
1129: } else if (vz == 0) {
1130: DMSetLabelValue(dm, "marker", edge, markerBottom);
1131: DMSetLabelValue(dm, "marker", cone[0], markerBottom);
1132: if (ey == numYEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerBottom);
1133: }
1134: }
1135: }
1136: }
1137: }
1138: }
1139: /* Build X edges*/
1140: for (vz = 0; vz < numZVertices; vz++) {
1141: for (vy = 0; vy < numYVertices; vy++) {
1142: for (ex = 0; ex < numXEdges; ex++) {
1143: const PetscInt nextv = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
1144: const PetscInt edge = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
1145: const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
1146: const PetscInt vertexR = firstVertex + nextv;
1147: PetscInt cone[2];
1149: cone[0] = vertexL;
1150: cone[1] = vertexR;
1151: DMPlexSetCone(dm, edge, cone);
1152: if (dim == 2) {
1153: if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
1154: if (vy == numYVertices - 1) {
1155: DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);
1156: DMSetLabelValue(dm, "marker", edge, markerTop);
1157: DMSetLabelValue(dm, "marker", cone[0], markerTop);
1158: if (ex == numXEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerTop);
1159: } else if (vy == 0) {
1160: DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);
1161: DMSetLabelValue(dm, "marker", edge, markerBottom);
1162: DMSetLabelValue(dm, "marker", cone[0], markerBottom);
1163: if (ex == numXEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerBottom);
1164: }
1165: } else {
1166: if (vy == 0 && cutLabel) {
1167: DMLabelSetValue(cutLabel, edge, 1);
1168: DMLabelSetValue(cutLabel, cone[0], 1);
1169: if (ex == numXEdges - 1) DMLabelSetValue(cutLabel, cone[1], 1);
1170: }
1171: }
1172: } else {
1173: if (bdY != DM_BOUNDARY_PERIODIC) {
1174: if (vy == numYVertices - 1) {
1175: DMSetLabelValue(dm, "marker", edge, markerBack);
1176: DMSetLabelValue(dm, "marker", cone[0], markerBack);
1177: if (ex == numXEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerBack);
1178: } else if (vy == 0) {
1179: DMSetLabelValue(dm, "marker", edge, markerFront);
1180: DMSetLabelValue(dm, "marker", cone[0], markerFront);
1181: if (ex == numXEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerFront);
1182: }
1183: }
1184: if (bdZ != DM_BOUNDARY_PERIODIC) {
1185: if (vz == numZVertices - 1) {
1186: DMSetLabelValue(dm, "marker", edge, markerTop);
1187: DMSetLabelValue(dm, "marker", cone[0], markerTop);
1188: if (ex == numXEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerTop);
1189: } else if (vz == 0) {
1190: DMSetLabelValue(dm, "marker", edge, markerBottom);
1191: DMSetLabelValue(dm, "marker", cone[0], markerBottom);
1192: if (ex == numXEdges - 1) DMSetLabelValue(dm, "marker", cone[1], markerBottom);
1193: }
1194: }
1195: }
1196: }
1197: }
1198: }
1199: DMPlexSymmetrize(dm);
1200: DMPlexStratify(dm);
1201: /* Build coordinates */
1202: DMGetCoordinateSection(dm, &coordSection);
1203: PetscSectionSetNumFields(coordSection, 1);
1204: PetscSectionSetFieldComponents(coordSection, 0, dim);
1205: PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices);
1206: for (v = firstVertex; v < firstVertex + numVertices; ++v) {
1207: PetscSectionSetDof(coordSection, v, dim);
1208: PetscSectionSetFieldDof(coordSection, v, 0, dim);
1209: }
1210: PetscSectionSetUp(coordSection);
1211: PetscSectionGetStorageSize(coordSection, &coordSize);
1212: VecCreate(PETSC_COMM_SELF, &coordinates);
1213: PetscObjectSetName((PetscObject)coordinates, "coordinates");
1214: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1215: VecSetBlockSize(coordinates, dim);
1216: VecSetType(coordinates, VECSTANDARD);
1217: VecGetArray(coordinates, &coords);
1218: for (vz = 0; vz < numZVertices; ++vz) {
1219: for (vy = 0; vy < numYVertices; ++vy) {
1220: for (vx = 0; vx < numXVertices; ++vx) {
1221: coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
1222: coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1223: if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
1224: }
1225: }
1226: }
1227: VecRestoreArray(coordinates, &coords);
1228: DMSetCoordinatesLocal(dm, coordinates);
1229: VecDestroy(&coordinates);
1230: }
1231: return 0;
1232: }
1234: static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1235: {
1236: DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1237: PetscInt fac[3] = {0, 0, 0}, d;
1241: DMSetDimension(dm, dim);
1242: for (d = 0; d < dim; ++d) {
1243: fac[d] = faces[d];
1244: bdt[d] = periodicity[d];
1245: }
1246: DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]);
1247: if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST || periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST || (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
1248: PetscReal L[3] = {-1., -1., 0.};
1249: PetscReal maxCell[3] = {-1., -1., 0.};
1251: for (d = 0; d < dim; ++d) {
1252: if (periodicity[d] != DM_BOUNDARY_NONE) {
1253: L[d] = upper[d] - lower[d];
1254: maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1255: }
1256: }
1257: DMSetPeriodicity(dm, maxCell, lower, L);
1258: }
1259: DMPlexSetRefinementUniform(dm, PETSC_TRUE);
1260: return 0;
1261: }
1263: static PetscErrorCode DMPlexCreateBoxMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
1264: {
1265: if (dim == 1) DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]);
1266: else if (simplex) DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate);
1267: else DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity);
1268: if (!interpolate && dim > 1 && !simplex) {
1269: DM udm;
1271: DMPlexUninterpolate(dm, &udm);
1272: DMPlexCopyCoordinates(dm, udm);
1273: DMPlexReplace_Internal(dm, &udm);
1274: }
1275: return 0;
1276: }
1278: /*@C
1279: DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1281: Collective
1283: Input Parameters:
1284: + comm - The communicator for the `DM` object
1285: . dim - The spatial dimension
1286: . simplex - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
1287: . faces - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1288: . lower - The lower left corner, or NULL for (0, 0, 0)
1289: . upper - The upper right corner, or NULL for (1, 1, 1)
1290: . periodicity - The boundary type for the X,Y,Z direction, or NULL for `DM_BOUNDARY_NONE`
1291: - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1293: Output Parameter:
1294: . dm - The `DM` object
1296: Level: beginner
1298: Note:
1299: To customize this mesh using options, use
1300: .vb
1301: DMCreate(comm, &dm);
1302: DMSetType(dm, DMPLEX);
1303: DMSetFromOptions(dm);
1304: .ve
1305: and use the options in `DMSetFromOptions()`.
1307: Here is the numbering returned for 2 faces in each direction for tensor cells:
1308: .vb
1309: 10---17---11---18----12
1310: | | |
1311: | | |
1312: 20 2 22 3 24
1313: | | |
1314: | | |
1315: 7---15----8---16----9
1316: | | |
1317: | | |
1318: 19 0 21 1 23
1319: | | |
1320: | | |
1321: 4---13----5---14----6
1322: .ve
1323: and for simplicial cells
1324: .vb
1325: 14----8---15----9----16
1326: |\ 5 |\ 7 |
1327: | \ | \ |
1328: 13 2 14 3 15
1329: | 4 \ | 6 \ |
1330: | \ | \ |
1331: 11----6---12----7----13
1332: |\ |\ |
1333: | \ 1 | \ 3 |
1334: 10 0 11 1 12
1335: | 0 \ | 2 \ |
1336: | \ | \ |
1337: 8----4----9----5----10
1338: .ve
1340: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1341: @*/
1342: PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, DM *dm)
1343: {
1344: PetscInt fac[3] = {1, 1, 1};
1345: PetscReal low[3] = {0, 0, 0};
1346: PetscReal upp[3] = {1, 1, 1};
1347: DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1349: DMCreate(comm, dm);
1350: DMSetType(*dm, DMPLEX);
1351: DMPlexCreateBoxMesh_Internal(*dm, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate);
1352: if (periodicity) DMLocalizeCoordinates(*dm);
1353: return 0;
1354: }
1356: static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1357: {
1358: DM bdm, vol;
1359: PetscInt i;
1362: DMCreate(PetscObjectComm((PetscObject)dm), &bdm);
1363: DMSetType(bdm, DMPLEX);
1364: DMSetDimension(bdm, 2);
1365: DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE);
1366: DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, NULL, NULL, &vol);
1367: DMDestroy(&bdm);
1368: DMPlexReplace_Internal(dm, &vol);
1369: if (lower[2] != 0.0) {
1370: Vec v;
1371: PetscScalar *x;
1372: PetscInt cDim, n;
1374: DMGetCoordinatesLocal(dm, &v);
1375: VecGetBlockSize(v, &cDim);
1376: VecGetLocalSize(v, &n);
1377: VecGetArray(v, &x);
1378: x += cDim;
1379: for (i = 0; i < n; i += cDim) x[i] += lower[2];
1380: VecRestoreArray(v, &x);
1381: DMSetCoordinatesLocal(dm, v);
1382: }
1383: return 0;
1384: }
1386: /*@
1387: DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
1389: Collective
1391: Input Parameters:
1392: + comm - The communicator for the `DM` object
1393: . faces - Number of faces per dimension, or NULL for (1, 1, 1)
1394: . lower - The lower left corner, or NULL for (0, 0, 0)
1395: . upper - The upper right corner, or NULL for (1, 1, 1)
1396: . periodicity - The boundary type for the X,Y,Z direction, or NULL for `DM_BOUNDARY_NONE`
1397: . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
1398: - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1400: Output Parameter:
1401: . dm - The `DM` object
1403: Level: beginner
1405: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
1406: @*/
1407: PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1408: {
1409: PetscInt fac[3] = {1, 1, 1};
1410: PetscReal low[3] = {0, 0, 0};
1411: PetscReal upp[3] = {1, 1, 1};
1412: DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1414: DMCreate(comm, dm);
1415: DMSetType(*dm, DMPLEX);
1416: DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt);
1417: if (!interpolate) {
1418: DM udm;
1420: DMPlexUninterpolate(*dm, &udm);
1421: DMPlexReplace_Internal(*dm, &udm);
1422: }
1423: if (periodicity) DMLocalizeCoordinates(*dm);
1424: return 0;
1425: }
1427: /*@C
1428: DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
1430: Logically Collective on dm
1432: Input Parameters:
1433: + dm - the DM context
1434: - prefix - the prefix to prepend to all option names
1436: Level: advanced
1438: Note:
1439: A hyphen (-) must NOT be given at the beginning of the prefix name.
1440: The first character of all runtime options is AUTOMATICALLY the hyphen.
1442: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
1443: @*/
1444: PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1445: {
1446: DM_Plex *mesh = (DM_Plex *)dm->data;
1449: PetscObjectSetOptionsPrefix((PetscObject)dm, prefix);
1450: PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix);
1451: return 0;
1452: }
1454: /* Remap geometry to cylinder
1455: TODO: This only works for a single refinement, then it is broken
1457: Interior square: Linear interpolation is correct
1458: The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
1459: such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
1461: phi = arctan(y/x)
1462: d_close = sqrt(1/8 + 1/4 sin^2(phi))
1463: d_far = sqrt(1/2 + sin^2(phi))
1465: so we remap them using
1467: x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
1468: y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
1470: If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
1471: */
1472: static void snapToCylinder(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[])
1473: {
1474: const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
1475: const PetscReal ds2 = 0.5 * dis;
1477: if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
1478: f0[0] = u[0];
1479: f0[1] = u[1];
1480: } else {
1481: PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
1483: x = PetscRealPart(u[0]);
1484: y = PetscRealPart(u[1]);
1485: phi = PetscAtan2Real(y, x);
1486: sinp = PetscSinReal(phi);
1487: cosp = PetscCosReal(phi);
1488: if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
1489: dc = PetscAbsReal(ds2 / sinp);
1490: df = PetscAbsReal(dis / sinp);
1491: xc = ds2 * x / PetscAbsReal(y);
1492: yc = ds2 * PetscSignReal(y);
1493: } else {
1494: dc = PetscAbsReal(ds2 / cosp);
1495: df = PetscAbsReal(dis / cosp);
1496: xc = ds2 * PetscSignReal(x);
1497: yc = ds2 * y / PetscAbsReal(x);
1498: }
1499: f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
1500: f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
1501: }
1502: f0[2] = u[2];
1503: }
1505: static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
1506: {
1507: const PetscInt dim = 3;
1508: PetscInt numCells, numVertices;
1509: PetscMPIInt rank;
1511: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
1512: DMSetDimension(dm, dim);
1513: /* Create topology */
1514: {
1515: PetscInt cone[8], c;
1517: numCells = rank == 0 ? 5 : 0;
1518: numVertices = rank == 0 ? 16 : 0;
1519: if (periodicZ == DM_BOUNDARY_PERIODIC) {
1520: numCells *= 3;
1521: numVertices = rank == 0 ? 24 : 0;
1522: }
1523: DMPlexSetChart(dm, 0, numCells + numVertices);
1524: for (c = 0; c < numCells; c++) DMPlexSetConeSize(dm, c, 8);
1525: DMSetUp(dm);
1526: if (rank == 0) {
1527: if (periodicZ == DM_BOUNDARY_PERIODIC) {
1528: cone[0] = 15;
1529: cone[1] = 18;
1530: cone[2] = 17;
1531: cone[3] = 16;
1532: cone[4] = 31;
1533: cone[5] = 32;
1534: cone[6] = 33;
1535: cone[7] = 34;
1536: DMPlexSetCone(dm, 0, cone);
1537: cone[0] = 16;
1538: cone[1] = 17;
1539: cone[2] = 24;
1540: cone[3] = 23;
1541: cone[4] = 32;
1542: cone[5] = 36;
1543: cone[6] = 37;
1544: cone[7] = 33; /* 22 25 26 21 */
1545: DMPlexSetCone(dm, 1, cone);
1546: cone[0] = 18;
1547: cone[1] = 27;
1548: cone[2] = 24;
1549: cone[3] = 17;
1550: cone[4] = 34;
1551: cone[5] = 33;
1552: cone[6] = 37;
1553: cone[7] = 38;
1554: DMPlexSetCone(dm, 2, cone);
1555: cone[0] = 29;
1556: cone[1] = 27;
1557: cone[2] = 18;
1558: cone[3] = 15;
1559: cone[4] = 35;
1560: cone[5] = 31;
1561: cone[6] = 34;
1562: cone[7] = 38;
1563: DMPlexSetCone(dm, 3, cone);
1564: cone[0] = 29;
1565: cone[1] = 15;
1566: cone[2] = 16;
1567: cone[3] = 23;
1568: cone[4] = 35;
1569: cone[5] = 36;
1570: cone[6] = 32;
1571: cone[7] = 31;
1572: DMPlexSetCone(dm, 4, cone);
1574: cone[0] = 31;
1575: cone[1] = 34;
1576: cone[2] = 33;
1577: cone[3] = 32;
1578: cone[4] = 19;
1579: cone[5] = 22;
1580: cone[6] = 21;
1581: cone[7] = 20;
1582: DMPlexSetCone(dm, 5, cone);
1583: cone[0] = 32;
1584: cone[1] = 33;
1585: cone[2] = 37;
1586: cone[3] = 36;
1587: cone[4] = 22;
1588: cone[5] = 25;
1589: cone[6] = 26;
1590: cone[7] = 21;
1591: DMPlexSetCone(dm, 6, cone);
1592: cone[0] = 34;
1593: cone[1] = 38;
1594: cone[2] = 37;
1595: cone[3] = 33;
1596: cone[4] = 20;
1597: cone[5] = 21;
1598: cone[6] = 26;
1599: cone[7] = 28;
1600: DMPlexSetCone(dm, 7, cone);
1601: cone[0] = 35;
1602: cone[1] = 38;
1603: cone[2] = 34;
1604: cone[3] = 31;
1605: cone[4] = 30;
1606: cone[5] = 19;
1607: cone[6] = 20;
1608: cone[7] = 28;
1609: DMPlexSetCone(dm, 8, cone);
1610: cone[0] = 35;
1611: cone[1] = 31;
1612: cone[2] = 32;
1613: cone[3] = 36;
1614: cone[4] = 30;
1615: cone[5] = 25;
1616: cone[6] = 22;
1617: cone[7] = 19;
1618: DMPlexSetCone(dm, 9, cone);
1620: cone[0] = 19;
1621: cone[1] = 20;
1622: cone[2] = 21;
1623: cone[3] = 22;
1624: cone[4] = 15;
1625: cone[5] = 16;
1626: cone[6] = 17;
1627: cone[7] = 18;
1628: DMPlexSetCone(dm, 10, cone);
1629: cone[0] = 22;
1630: cone[1] = 21;
1631: cone[2] = 26;
1632: cone[3] = 25;
1633: cone[4] = 16;
1634: cone[5] = 23;
1635: cone[6] = 24;
1636: cone[7] = 17;
1637: DMPlexSetCone(dm, 11, cone);
1638: cone[0] = 20;
1639: cone[1] = 28;
1640: cone[2] = 26;
1641: cone[3] = 21;
1642: cone[4] = 18;
1643: cone[5] = 17;
1644: cone[6] = 24;
1645: cone[7] = 27;
1646: DMPlexSetCone(dm, 12, cone);
1647: cone[0] = 30;
1648: cone[1] = 28;
1649: cone[2] = 20;
1650: cone[3] = 19;
1651: cone[4] = 29;
1652: cone[5] = 15;
1653: cone[6] = 18;
1654: cone[7] = 27;
1655: DMPlexSetCone(dm, 13, cone);
1656: cone[0] = 30;
1657: cone[1] = 19;
1658: cone[2] = 22;
1659: cone[3] = 25;
1660: cone[4] = 29;
1661: cone[5] = 23;
1662: cone[6] = 16;
1663: cone[7] = 15;
1664: DMPlexSetCone(dm, 14, cone);
1665: } else {
1666: cone[0] = 5;
1667: cone[1] = 8;
1668: cone[2] = 7;
1669: cone[3] = 6;
1670: cone[4] = 9;
1671: cone[5] = 12;
1672: cone[6] = 11;
1673: cone[7] = 10;
1674: DMPlexSetCone(dm, 0, cone);
1675: cone[0] = 6;
1676: cone[1] = 7;
1677: cone[2] = 14;
1678: cone[3] = 13;
1679: cone[4] = 12;
1680: cone[5] = 15;
1681: cone[6] = 16;
1682: cone[7] = 11;
1683: DMPlexSetCone(dm, 1, cone);
1684: cone[0] = 8;
1685: cone[1] = 17;
1686: cone[2] = 14;
1687: cone[3] = 7;
1688: cone[4] = 10;
1689: cone[5] = 11;
1690: cone[6] = 16;
1691: cone[7] = 18;
1692: DMPlexSetCone(dm, 2, cone);
1693: cone[0] = 19;
1694: cone[1] = 17;
1695: cone[2] = 8;
1696: cone[3] = 5;
1697: cone[4] = 20;
1698: cone[5] = 9;
1699: cone[6] = 10;
1700: cone[7] = 18;
1701: DMPlexSetCone(dm, 3, cone);
1702: cone[0] = 19;
1703: cone[1] = 5;
1704: cone[2] = 6;
1705: cone[3] = 13;
1706: cone[4] = 20;
1707: cone[5] = 15;
1708: cone[6] = 12;
1709: cone[7] = 9;
1710: DMPlexSetCone(dm, 4, cone);
1711: }
1712: }
1713: DMPlexSymmetrize(dm);
1714: DMPlexStratify(dm);
1715: }
1716: /* Create cube geometry */
1717: {
1718: Vec coordinates;
1719: PetscSection coordSection;
1720: PetscScalar *coords;
1721: PetscInt coordSize, v;
1722: const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
1723: const PetscReal ds2 = dis / 2.0;
1725: /* Build coordinates */
1726: DMGetCoordinateSection(dm, &coordSection);
1727: PetscSectionSetNumFields(coordSection, 1);
1728: PetscSectionSetFieldComponents(coordSection, 0, dim);
1729: PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
1730: for (v = numCells; v < numCells + numVertices; ++v) {
1731: PetscSectionSetDof(coordSection, v, dim);
1732: PetscSectionSetFieldDof(coordSection, v, 0, dim);
1733: }
1734: PetscSectionSetUp(coordSection);
1735: PetscSectionGetStorageSize(coordSection, &coordSize);
1736: VecCreate(PETSC_COMM_SELF, &coordinates);
1737: PetscObjectSetName((PetscObject)coordinates, "coordinates");
1738: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1739: VecSetBlockSize(coordinates, dim);
1740: VecSetType(coordinates, VECSTANDARD);
1741: VecGetArray(coordinates, &coords);
1742: if (rank == 0) {
1743: coords[0 * dim + 0] = -ds2;
1744: coords[0 * dim + 1] = -ds2;
1745: coords[0 * dim + 2] = 0.0;
1746: coords[1 * dim + 0] = ds2;
1747: coords[1 * dim + 1] = -ds2;
1748: coords[1 * dim + 2] = 0.0;
1749: coords[2 * dim + 0] = ds2;
1750: coords[2 * dim + 1] = ds2;
1751: coords[2 * dim + 2] = 0.0;
1752: coords[3 * dim + 0] = -ds2;
1753: coords[3 * dim + 1] = ds2;
1754: coords[3 * dim + 2] = 0.0;
1755: coords[4 * dim + 0] = -ds2;
1756: coords[4 * dim + 1] = -ds2;
1757: coords[4 * dim + 2] = 1.0;
1758: coords[5 * dim + 0] = -ds2;
1759: coords[5 * dim + 1] = ds2;
1760: coords[5 * dim + 2] = 1.0;
1761: coords[6 * dim + 0] = ds2;
1762: coords[6 * dim + 1] = ds2;
1763: coords[6 * dim + 2] = 1.0;
1764: coords[7 * dim + 0] = ds2;
1765: coords[7 * dim + 1] = -ds2;
1766: coords[7 * dim + 2] = 1.0;
1767: coords[8 * dim + 0] = dis;
1768: coords[8 * dim + 1] = -dis;
1769: coords[8 * dim + 2] = 0.0;
1770: coords[9 * dim + 0] = dis;
1771: coords[9 * dim + 1] = dis;
1772: coords[9 * dim + 2] = 0.0;
1773: coords[10 * dim + 0] = dis;
1774: coords[10 * dim + 1] = -dis;
1775: coords[10 * dim + 2] = 1.0;
1776: coords[11 * dim + 0] = dis;
1777: coords[11 * dim + 1] = dis;
1778: coords[11 * dim + 2] = 1.0;
1779: coords[12 * dim + 0] = -dis;
1780: coords[12 * dim + 1] = dis;
1781: coords[12 * dim + 2] = 0.0;
1782: coords[13 * dim + 0] = -dis;
1783: coords[13 * dim + 1] = dis;
1784: coords[13 * dim + 2] = 1.0;
1785: coords[14 * dim + 0] = -dis;
1786: coords[14 * dim + 1] = -dis;
1787: coords[14 * dim + 2] = 0.0;
1788: coords[15 * dim + 0] = -dis;
1789: coords[15 * dim + 1] = -dis;
1790: coords[15 * dim + 2] = 1.0;
1791: if (periodicZ == DM_BOUNDARY_PERIODIC) {
1792: /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
1793: coords[16 * dim + 1] = -ds2;
1794: coords[16 * dim + 2] = 0.5;
1795: /* 16 32 22 */ coords[17 * dim + 0] = ds2;
1796: coords[17 * dim + 1] = -ds2;
1797: coords[17 * dim + 2] = 0.5;
1798: /* 17 33 21 */ coords[18 * dim + 0] = ds2;
1799: coords[18 * dim + 1] = ds2;
1800: coords[18 * dim + 2] = 0.5;
1801: /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
1802: coords[19 * dim + 1] = ds2;
1803: coords[19 * dim + 2] = 0.5;
1804: /* 29 35 30 */ coords[20 * dim + 0] = -dis;
1805: coords[20 * dim + 1] = -dis;
1806: coords[20 * dim + 2] = 0.5;
1807: /* 23 36 25 */ coords[21 * dim + 0] = dis;
1808: coords[21 * dim + 1] = -dis;
1809: coords[21 * dim + 2] = 0.5;
1810: /* 24 37 26 */ coords[22 * dim + 0] = dis;
1811: coords[22 * dim + 1] = dis;
1812: coords[22 * dim + 2] = 0.5;
1813: /* 27 38 28 */ coords[23 * dim + 0] = -dis;
1814: coords[23 * dim + 1] = dis;
1815: coords[23 * dim + 2] = 0.5;
1816: }
1817: }
1818: VecRestoreArray(coordinates, &coords);
1819: DMSetCoordinatesLocal(dm, coordinates);
1820: VecDestroy(&coordinates);
1821: }
1822: /* Create periodicity */
1823: if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1824: PetscReal L[3] = {-1., -1., 0.};
1825: PetscReal maxCell[3] = {-1., -1., 0.};
1826: PetscReal lower[3] = {0.0, 0.0, 0.0};
1827: PetscReal upper[3] = {1.0, 1.0, 1.5};
1828: PetscInt numZCells = 3;
1830: L[2] = upper[2] - lower[2];
1831: maxCell[2] = 1.1 * (L[2] / numZCells);
1832: DMSetPeriodicity(dm, maxCell, lower, L);
1833: }
1834: {
1835: DM cdm;
1836: PetscDS cds;
1837: PetscScalar c[2] = {1.0, 1.0};
1839: DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder);
1840: DMGetCoordinateDM(dm, &cdm);
1841: DMGetDS(cdm, &cds);
1842: PetscDSSetConstants(cds, 2, c);
1843: }
1844: /* Wait for coordinate creation before doing in-place modification */
1845: DMPlexInterpolateInPlace_Internal(dm);
1846: return 0;
1847: }
1849: /*@
1850: DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
1852: Collective
1854: Input Parameters:
1855: + comm - The communicator for the `DM` object
1856: - periodicZ - The boundary type for the Z direction
1858: Output Parameter:
1859: . dm - The DM object
1861: Level: beginner
1863: Note:
1864: Here is the output numbering looking from the bottom of the cylinder:
1865: .vb
1866: 17-----14
1867: | |
1868: | 2 |
1869: | |
1870: 17-----8-----7-----14
1871: | | | |
1872: | 3 | 0 | 1 |
1873: | | | |
1874: 19-----5-----6-----13
1875: | |
1876: | 4 |
1877: | |
1878: 19-----13
1880: and up through the top
1882: 18-----16
1883: | |
1884: | 2 |
1885: | |
1886: 18----10----11-----16
1887: | | | |
1888: | 3 | 0 | 1 |
1889: | | | |
1890: 20-----9----12-----15
1891: | |
1892: | 4 |
1893: | |
1894: 20-----15
1895: .ve
1897: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
1898: @*/
1899: PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
1900: {
1902: DMCreate(comm, dm);
1903: DMSetType(*dm, DMPLEX);
1904: DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ);
1905: return 0;
1906: }
1908: static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
1909: {
1910: const PetscInt dim = 3;
1911: PetscInt numCells, numVertices, v;
1912: PetscMPIInt rank;
1915: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
1916: DMSetDimension(dm, dim);
1917: /* Must create the celltype label here so that we do not automatically try to compute the types */
1918: DMCreateLabel(dm, "celltype");
1919: /* Create topology */
1920: {
1921: PetscInt cone[6], c;
1923: numCells = rank == 0 ? n : 0;
1924: numVertices = rank == 0 ? 2 * (n + 1) : 0;
1925: DMPlexSetChart(dm, 0, numCells + numVertices);
1926: for (c = 0; c < numCells; c++) DMPlexSetConeSize(dm, c, 6);
1927: DMSetUp(dm);
1928: for (c = 0; c < numCells; c++) {
1929: cone[0] = c + n * 1;
1930: cone[1] = (c + 1) % n + n * 1;
1931: cone[2] = 0 + 3 * n;
1932: cone[3] = c + n * 2;
1933: cone[4] = (c + 1) % n + n * 2;
1934: cone[5] = 1 + 3 * n;
1935: DMPlexSetCone(dm, c, cone);
1936: DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR);
1937: }
1938: DMPlexSymmetrize(dm);
1939: DMPlexStratify(dm);
1940: }
1941: for (v = numCells; v < numCells + numVertices; ++v) DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT);
1942: /* Create cylinder geometry */
1943: {
1944: Vec coordinates;
1945: PetscSection coordSection;
1946: PetscScalar *coords;
1947: PetscInt coordSize, c;
1949: /* Build coordinates */
1950: DMGetCoordinateSection(dm, &coordSection);
1951: PetscSectionSetNumFields(coordSection, 1);
1952: PetscSectionSetFieldComponents(coordSection, 0, dim);
1953: PetscSectionSetChart(coordSection, numCells, numCells + numVertices);
1954: for (v = numCells; v < numCells + numVertices; ++v) {
1955: PetscSectionSetDof(coordSection, v, dim);
1956: PetscSectionSetFieldDof(coordSection, v, 0, dim);
1957: }
1958: PetscSectionSetUp(coordSection);
1959: PetscSectionGetStorageSize(coordSection, &coordSize);
1960: VecCreate(PETSC_COMM_SELF, &coordinates);
1961: PetscObjectSetName((PetscObject)coordinates, "coordinates");
1962: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
1963: VecSetBlockSize(coordinates, dim);
1964: VecSetType(coordinates, VECSTANDARD);
1965: VecGetArray(coordinates, &coords);
1966: for (c = 0; c < numCells; c++) {
1967: coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
1968: coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
1969: coords[(c + 0 * n) * dim + 2] = 1.0;
1970: coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
1971: coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
1972: coords[(c + 1 * n) * dim + 2] = 0.0;
1973: }
1974: if (rank == 0) {
1975: coords[(2 * n + 0) * dim + 0] = 0.0;
1976: coords[(2 * n + 0) * dim + 1] = 0.0;
1977: coords[(2 * n + 0) * dim + 2] = 1.0;
1978: coords[(2 * n + 1) * dim + 0] = 0.0;
1979: coords[(2 * n + 1) * dim + 1] = 0.0;
1980: coords[(2 * n + 1) * dim + 2] = 0.0;
1981: }
1982: VecRestoreArray(coordinates, &coords);
1983: DMSetCoordinatesLocal(dm, coordinates);
1984: VecDestroy(&coordinates);
1985: }
1986: /* Interpolate */
1987: if (interpolate) DMPlexInterpolateInPlace_Internal(dm);
1988: return 0;
1989: }
1991: /*@
1992: DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
1994: Collective
1996: Input Parameters:
1997: + comm - The communicator for the `DM` object
1998: . n - The number of wedges around the origin
1999: - interpolate - Create edges and faces
2001: Output Parameter:
2002: . dm - The `DM` object
2004: Level: beginner
2006: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
2007: @*/
2008: PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2009: {
2011: DMCreate(comm, dm);
2012: DMSetType(*dm, DMPLEX);
2013: DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate);
2014: return 0;
2015: }
2017: static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2018: {
2019: PetscReal prod = 0.0;
2020: PetscInt i;
2021: for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
2022: return PetscSqrtReal(prod);
2023: }
2024: static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2025: {
2026: PetscReal prod = 0.0;
2027: PetscInt i;
2028: for (i = 0; i < dim; ++i) prod += x[i] * y[i];
2029: return prod;
2030: }
2032: /* The first constant is the sphere radius */
2033: static void snapToSphere(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[])
2034: {
2035: PetscReal r = PetscRealPart(constants[0]);
2036: PetscReal norm2 = 0.0, fac;
2037: PetscInt n = uOff[1] - uOff[0], d;
2039: for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
2040: fac = r / PetscSqrtReal(norm2);
2041: for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
2042: }
2044: static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2045: {
2046: const PetscInt embedDim = dim + 1;
2047: PetscSection coordSection;
2048: Vec coordinates;
2049: PetscScalar *coords;
2050: PetscReal *coordsIn;
2051: PetscInt numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, c, e;
2052: PetscMPIInt rank;
2055: DMSetDimension(dm, dim);
2056: DMSetCoordinateDim(dm, dim + 1);
2057: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
2058: switch (dim) {
2059: case 2:
2060: if (simplex) {
2061: const PetscReal radius = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
2062: const PetscReal edgeLen = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
2063: const PetscInt degree = 5;
2064: PetscReal vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
2065: PetscInt s[3] = {1, 1, 1};
2066: PetscInt cone[3];
2067: PetscInt *graph, p, i, j, k;
2069: vertex[0] *= R / radius;
2070: vertex[1] *= R / radius;
2071: vertex[2] *= R / radius;
2072: numCells = rank == 0 ? 20 : 0;
2073: numVerts = rank == 0 ? 12 : 0;
2074: firstVertex = numCells;
2075: /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
2077: (0, \pm 1/\phi+1, \pm \phi/\phi+1)
2079: where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
2080: length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
2081: */
2082: /* Construct vertices */
2083: PetscCalloc1(numVerts * embedDim, &coordsIn);
2084: if (rank == 0) {
2085: for (p = 0, i = 0; p < embedDim; ++p) {
2086: for (s[1] = -1; s[1] < 2; s[1] += 2) {
2087: for (s[2] = -1; s[2] < 2; s[2] += 2) {
2088: for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
2089: ++i;
2090: }
2091: }
2092: }
2093: }
2094: /* Construct graph */
2095: PetscCalloc1(numVerts * numVerts, &graph);
2096: for (i = 0; i < numVerts; ++i) {
2097: for (j = 0, k = 0; j < numVerts; ++j) {
2098: if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
2099: graph[i * numVerts + j] = 1;
2100: ++k;
2101: }
2102: }
2104: }
2105: /* Build Topology */
2106: DMPlexSetChart(dm, 0, numCells + numVerts);
2107: for (c = 0; c < numCells; c++) DMPlexSetConeSize(dm, c, embedDim);
2108: DMSetUp(dm); /* Allocate space for cones */
2109: /* Cells */
2110: for (i = 0, c = 0; i < numVerts; ++i) {
2111: for (j = 0; j < i; ++j) {
2112: for (k = 0; k < j; ++k) {
2113: if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
2114: cone[0] = firstVertex + i;
2115: cone[1] = firstVertex + j;
2116: cone[2] = firstVertex + k;
2117: /* Check orientation */
2118: {
2119: const PetscInt epsilon[3][3][3] = {
2120: {{0, 0, 0}, {0, 0, 1}, {0, -1, 0}},
2121: {{0, 0, -1}, {0, 0, 0}, {1, 0, 0} },
2122: {{0, 1, 0}, {-1, 0, 0}, {0, 0, 0} }
2123: };
2124: PetscReal normal[3];
2125: PetscInt e, f;
2127: for (d = 0; d < embedDim; ++d) {
2128: normal[d] = 0.0;
2129: for (e = 0; e < embedDim; ++e) {
2130: for (f = 0; f < embedDim; ++f) normal[d] += epsilon[d][e][f] * (coordsIn[j * embedDim + e] - coordsIn[i * embedDim + e]) * (coordsIn[k * embedDim + f] - coordsIn[i * embedDim + f]);
2131: }
2132: }
2133: if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
2134: PetscInt tmp = cone[1];
2135: cone[1] = cone[2];
2136: cone[2] = tmp;
2137: }
2138: }
2139: DMPlexSetCone(dm, c++, cone);
2140: }
2141: }
2142: }
2143: }
2144: DMPlexSymmetrize(dm);
2145: DMPlexStratify(dm);
2146: PetscFree(graph);
2147: } else {
2148: /*
2149: 12-21--13
2150: | |
2151: 25 4 24
2152: | |
2153: 12-25--9-16--8-24--13
2154: | | | |
2155: 23 5 17 0 15 3 22
2156: | | | |
2157: 10-20--6-14--7-19--11
2158: | |
2159: 20 1 19
2160: | |
2161: 10-18--11
2162: | |
2163: 23 2 22
2164: | |
2165: 12-21--13
2166: */
2167: PetscInt cone[4], ornt[4];
2169: numCells = rank == 0 ? 6 : 0;
2170: numEdges = rank == 0 ? 12 : 0;
2171: numVerts = rank == 0 ? 8 : 0;
2172: firstVertex = numCells;
2173: firstEdge = numCells + numVerts;
2174: /* Build Topology */
2175: DMPlexSetChart(dm, 0, numCells + numEdges + numVerts);
2176: for (c = 0; c < numCells; c++) DMPlexSetConeSize(dm, c, 4);
2177: for (e = firstEdge; e < firstEdge + numEdges; ++e) DMPlexSetConeSize(dm, e, 2);
2178: DMSetUp(dm); /* Allocate space for cones */
2179: if (rank == 0) {
2180: /* Cell 0 */
2181: cone[0] = 14;
2182: cone[1] = 15;
2183: cone[2] = 16;
2184: cone[3] = 17;
2185: DMPlexSetCone(dm, 0, cone);
2186: ornt[0] = 0;
2187: ornt[1] = 0;
2188: ornt[2] = 0;
2189: ornt[3] = 0;
2190: DMPlexSetConeOrientation(dm, 0, ornt);
2191: /* Cell 1 */
2192: cone[0] = 18;
2193: cone[1] = 19;
2194: cone[2] = 14;
2195: cone[3] = 20;
2196: DMPlexSetCone(dm, 1, cone);
2197: ornt[0] = 0;
2198: ornt[1] = 0;
2199: ornt[2] = -1;
2200: ornt[3] = 0;
2201: DMPlexSetConeOrientation(dm, 1, ornt);
2202: /* Cell 2 */
2203: cone[0] = 21;
2204: cone[1] = 22;
2205: cone[2] = 18;
2206: cone[3] = 23;
2207: DMPlexSetCone(dm, 2, cone);
2208: ornt[0] = 0;
2209: ornt[1] = 0;
2210: ornt[2] = -1;
2211: ornt[3] = 0;
2212: DMPlexSetConeOrientation(dm, 2, ornt);
2213: /* Cell 3 */
2214: cone[0] = 19;
2215: cone[1] = 22;
2216: cone[2] = 24;
2217: cone[3] = 15;
2218: DMPlexSetCone(dm, 3, cone);
2219: ornt[0] = -1;
2220: ornt[1] = -1;
2221: ornt[2] = 0;
2222: ornt[3] = -1;
2223: DMPlexSetConeOrientation(dm, 3, ornt);
2224: /* Cell 4 */
2225: cone[0] = 16;
2226: cone[1] = 24;
2227: cone[2] = 21;
2228: cone[3] = 25;
2229: DMPlexSetCone(dm, 4, cone);
2230: ornt[0] = -1;
2231: ornt[1] = -1;
2232: ornt[2] = -1;
2233: ornt[3] = 0;
2234: DMPlexSetConeOrientation(dm, 4, ornt);
2235: /* Cell 5 */
2236: cone[0] = 20;
2237: cone[1] = 17;
2238: cone[2] = 25;
2239: cone[3] = 23;
2240: DMPlexSetCone(dm, 5, cone);
2241: ornt[0] = -1;
2242: ornt[1] = -1;
2243: ornt[2] = -1;
2244: ornt[3] = -1;
2245: DMPlexSetConeOrientation(dm, 5, ornt);
2246: /* Edges */
2247: cone[0] = 6;
2248: cone[1] = 7;
2249: DMPlexSetCone(dm, 14, cone);
2250: cone[0] = 7;
2251: cone[1] = 8;
2252: DMPlexSetCone(dm, 15, cone);
2253: cone[0] = 8;
2254: cone[1] = 9;
2255: DMPlexSetCone(dm, 16, cone);
2256: cone[0] = 9;
2257: cone[1] = 6;
2258: DMPlexSetCone(dm, 17, cone);
2259: cone[0] = 10;
2260: cone[1] = 11;
2261: DMPlexSetCone(dm, 18, cone);
2262: cone[0] = 11;
2263: cone[1] = 7;
2264: DMPlexSetCone(dm, 19, cone);
2265: cone[0] = 6;
2266: cone[1] = 10;
2267: DMPlexSetCone(dm, 20, cone);
2268: cone[0] = 12;
2269: cone[1] = 13;
2270: DMPlexSetCone(dm, 21, cone);
2271: cone[0] = 13;
2272: cone[1] = 11;
2273: DMPlexSetCone(dm, 22, cone);
2274: cone[0] = 10;
2275: cone[1] = 12;
2276: DMPlexSetCone(dm, 23, cone);
2277: cone[0] = 13;
2278: cone[1] = 8;
2279: DMPlexSetCone(dm, 24, cone);
2280: cone[0] = 12;
2281: cone[1] = 9;
2282: DMPlexSetCone(dm, 25, cone);
2283: }
2284: DMPlexSymmetrize(dm);
2285: DMPlexStratify(dm);
2286: /* Build coordinates */
2287: PetscCalloc1(numVerts * embedDim, &coordsIn);
2288: if (rank == 0) {
2289: coordsIn[0 * embedDim + 0] = -R;
2290: coordsIn[0 * embedDim + 1] = R;
2291: coordsIn[0 * embedDim + 2] = -R;
2292: coordsIn[1 * embedDim + 0] = R;
2293: coordsIn[1 * embedDim + 1] = R;
2294: coordsIn[1 * embedDim + 2] = -R;
2295: coordsIn[2 * embedDim + 0] = R;
2296: coordsIn[2 * embedDim + 1] = -R;
2297: coordsIn[2 * embedDim + 2] = -R;
2298: coordsIn[3 * embedDim + 0] = -R;
2299: coordsIn[3 * embedDim + 1] = -R;
2300: coordsIn[3 * embedDim + 2] = -R;
2301: coordsIn[4 * embedDim + 0] = -R;
2302: coordsIn[4 * embedDim + 1] = R;
2303: coordsIn[4 * embedDim + 2] = R;
2304: coordsIn[5 * embedDim + 0] = R;
2305: coordsIn[5 * embedDim + 1] = R;
2306: coordsIn[5 * embedDim + 2] = R;
2307: coordsIn[6 * embedDim + 0] = -R;
2308: coordsIn[6 * embedDim + 1] = -R;
2309: coordsIn[6 * embedDim + 2] = R;
2310: coordsIn[7 * embedDim + 0] = R;
2311: coordsIn[7 * embedDim + 1] = -R;
2312: coordsIn[7 * embedDim + 2] = R;
2313: }
2314: }
2315: break;
2316: case 3:
2317: if (simplex) {
2318: const PetscReal edgeLen = 1.0 / PETSC_PHI;
2319: PetscReal vertexA[4] = {0.5, 0.5, 0.5, 0.5};
2320: PetscReal vertexB[4] = {1.0, 0.0, 0.0, 0.0};
2321: PetscReal vertexC[4] = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
2322: const PetscInt degree = 12;
2323: PetscInt s[4] = {1, 1, 1};
2324: PetscInt evenPerm[12][4] = {
2325: {0, 1, 2, 3},
2326: {0, 2, 3, 1},
2327: {0, 3, 1, 2},
2328: {1, 0, 3, 2},
2329: {1, 2, 0, 3},
2330: {1, 3, 2, 0},
2331: {2, 0, 1, 3},
2332: {2, 1, 3, 0},
2333: {2, 3, 0, 1},
2334: {3, 0, 2, 1},
2335: {3, 1, 0, 2},
2336: {3, 2, 1, 0}
2337: };
2338: PetscInt cone[4];
2339: PetscInt *graph, p, i, j, k, l;
2341: vertexA[0] *= R;
2342: vertexA[1] *= R;
2343: vertexA[2] *= R;
2344: vertexA[3] *= R;
2345: vertexB[0] *= R;
2346: vertexB[1] *= R;
2347: vertexB[2] *= R;
2348: vertexB[3] *= R;
2349: vertexC[0] *= R;
2350: vertexC[1] *= R;
2351: vertexC[2] *= R;
2352: vertexC[3] *= R;
2353: numCells = rank == 0 ? 600 : 0;
2354: numVerts = rank == 0 ? 120 : 0;
2355: firstVertex = numCells;
2356: /* Use the 600-cell, which for a unit sphere has coordinates which are
2358: 1/2 (\pm 1, \pm 1, \pm 1, \pm 1) 16
2359: (\pm 1, 0, 0, 0) all cyclic permutations 8
2360: 1/2 (\pm 1, \pm phi, \pm 1/phi, 0) all even permutations 96
2362: where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
2363: length is then given by 1/\phi = 0.61803.
2365: http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2366: http://mathworld.wolfram.com/600-Cell.html
2367: */
2368: /* Construct vertices */
2369: PetscCalloc1(numVerts * embedDim, &coordsIn);
2370: i = 0;
2371: if (rank == 0) {
2372: for (s[0] = -1; s[0] < 2; s[0] += 2) {
2373: for (s[1] = -1; s[1] < 2; s[1] += 2) {
2374: for (s[2] = -1; s[2] < 2; s[2] += 2) {
2375: for (s[3] = -1; s[3] < 2; s[3] += 2) {
2376: for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
2377: ++i;
2378: }
2379: }
2380: }
2381: }
2382: for (p = 0; p < embedDim; ++p) {
2383: s[1] = s[2] = s[3] = 1;
2384: for (s[0] = -1; s[0] < 2; s[0] += 2) {
2385: for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
2386: ++i;
2387: }
2388: }
2389: for (p = 0; p < 12; ++p) {
2390: s[3] = 1;
2391: for (s[0] = -1; s[0] < 2; s[0] += 2) {
2392: for (s[1] = -1; s[1] < 2; s[1] += 2) {
2393: for (s[2] = -1; s[2] < 2; s[2] += 2) {
2394: for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
2395: ++i;
2396: }
2397: }
2398: }
2399: }
2400: }
2402: /* Construct graph */
2403: PetscCalloc1(numVerts * numVerts, &graph);
2404: for (i = 0; i < numVerts; ++i) {
2405: for (j = 0, k = 0; j < numVerts; ++j) {
2406: if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
2407: graph[i * numVerts + j] = 1;
2408: ++k;
2409: }
2410: }
2412: }
2413: /* Build Topology */
2414: DMPlexSetChart(dm, 0, numCells + numVerts);
2415: for (c = 0; c < numCells; c++) DMPlexSetConeSize(dm, c, embedDim);
2416: DMSetUp(dm); /* Allocate space for cones */
2417: /* Cells */
2418: if (rank == 0) {
2419: for (i = 0, c = 0; i < numVerts; ++i) {
2420: for (j = 0; j < i; ++j) {
2421: for (k = 0; k < j; ++k) {
2422: for (l = 0; l < k; ++l) {
2423: if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i] && graph[l * numVerts + i] && graph[l * numVerts + j] && graph[l * numVerts + k]) {
2424: cone[0] = firstVertex + i;
2425: cone[1] = firstVertex + j;
2426: cone[2] = firstVertex + k;
2427: cone[3] = firstVertex + l;
2428: /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2429: {
2430: const PetscInt epsilon[4][4][4][4] = {
2431: {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, -1, 0}}, {{0, 0, 0, 0}, {0, 0, 0, -1}, {0, 0, 0, 0}, {0, 1, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 1, 0}, {0, -1, 0, 0}, {0, 0, 0, 0}}},
2433: {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, -1}, {0, 0, 1, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {-1, 0, 0, 0}}, {{0, 0, -1, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}}},
2435: {{{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 0}, {0, -1, 0, 0}}, {{0, 0, 0, -1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 1, 0, 0}, {-1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}},
2437: {{{0, 0, 0, 0}, {0, 0, -1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 1, 0}, {0, 0, 0, 0}, {-1, 0, 0, 0}, {0, 0, 0, 0}}, {{0, -1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} }
2438: };
2439: PetscReal normal[4];
2440: PetscInt e, f, g;
2442: for (d = 0; d < embedDim; ++d) {
2443: normal[d] = 0.0;
2444: for (e = 0; e < embedDim; ++e) {
2445: for (f = 0; f < embedDim; ++f) {
2446: for (g = 0; g < embedDim; ++g) {
2447: normal[d] += epsilon[d][e][f][g] * (coordsIn[j * embedDim + e] - coordsIn[i * embedDim + e]) * (coordsIn[k * embedDim + f] - coordsIn[i * embedDim + f]) * (coordsIn[l * embedDim + f] - coordsIn[i * embedDim + f]);
2448: }
2449: }
2450: }
2451: }
2452: if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
2453: PetscInt tmp = cone[1];
2454: cone[1] = cone[2];
2455: cone[2] = tmp;
2456: }
2457: }
2458: DMPlexSetCone(dm, c++, cone);
2459: }
2460: }
2461: }
2462: }
2463: }
2464: }
2465: DMPlexSymmetrize(dm);
2466: DMPlexStratify(dm);
2467: PetscFree(graph);
2468: }
2469: break;
2470: default:
2471: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
2472: }
2473: /* Create coordinates */
2474: DMGetCoordinateSection(dm, &coordSection);
2475: PetscSectionSetNumFields(coordSection, 1);
2476: PetscSectionSetFieldComponents(coordSection, 0, embedDim);
2477: PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts);
2478: for (v = firstVertex; v < firstVertex + numVerts; ++v) {
2479: PetscSectionSetDof(coordSection, v, embedDim);
2480: PetscSectionSetFieldDof(coordSection, v, 0, embedDim);
2481: }
2482: PetscSectionSetUp(coordSection);
2483: PetscSectionGetStorageSize(coordSection, &coordSize);
2484: VecCreate(PETSC_COMM_SELF, &coordinates);
2485: VecSetBlockSize(coordinates, embedDim);
2486: PetscObjectSetName((PetscObject)coordinates, "coordinates");
2487: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
2488: VecSetType(coordinates, VECSTANDARD);
2489: VecGetArray(coordinates, &coords);
2490: for (v = 0; v < numVerts; ++v)
2491: for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
2492: VecRestoreArray(coordinates, &coords);
2493: DMSetCoordinatesLocal(dm, coordinates);
2494: VecDestroy(&coordinates);
2495: PetscFree(coordsIn);
2496: {
2497: DM cdm;
2498: PetscDS cds;
2499: PetscScalar c = R;
2501: DMPlexCreateCoordinateSpace(dm, 1, snapToSphere);
2502: DMGetCoordinateDM(dm, &cdm);
2503: DMGetDS(cdm, &cds);
2504: PetscDSSetConstants(cds, 1, &c);
2505: }
2506: /* Wait for coordinate creation before doing in-place modification */
2507: if (simplex) DMPlexInterpolateInPlace_Internal(dm);
2508: return 0;
2509: }
2511: typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
2513: /*
2514: The Schwarz P implicit surface is
2516: f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2517: */
2518: static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2519: {
2520: PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2521: PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2522: f[0] = c[0] + c[1] + c[2];
2523: for (PetscInt i = 0; i < 3; i++) {
2524: grad[i] = PETSC_PI * g[i];
2525: for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2526: }
2527: }
2529: // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2530: static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2531: {
2532: for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
2533: return 0;
2534: }
2536: /*
2537: The Gyroid implicit surface is
2539: f(x,y,z) = sin(pi * x) * cos (pi * (y + 1/2)) + sin(pi * (y + 1/2)) * cos(pi * (z + 1/4)) + sin(pi * (z + 1/4)) * cos(pi * x)
2541: */
2542: static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2543: {
2544: PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2545: PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2546: f[0] = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2547: grad[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2548: grad[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2549: grad[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2550: hess[0][0] = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2551: hess[0][1] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2552: hess[0][2] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2553: hess[1][0] = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2554: hess[1][1] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2555: hess[2][2] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2556: hess[2][0] = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2557: hess[2][1] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2558: hess[2][2] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2559: }
2561: // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2562: static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2563: {
2564: PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
2565: PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
2566: u[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2567: u[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2568: u[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2569: return 0;
2570: }
2572: /*
2573: We wish to solve
2575: min_y || y - x ||^2 subject to f(y) = 0
2577: Let g(y) = grad(f). The minimization problem is equivalent to asking to satisfy
2578: f(y) = 0 and (y-x) is parallel to g(y). We do this by using Householder QR to obtain a basis for the
2579: tangent space and ask for both components in the tangent space to be zero.
2581: Take g to be a column vector and compute the "full QR" factorization Q R = g,
2582: where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2583: The first column of Q is parallel to g so the remaining two columns span the null space.
2584: Let Qn = Q[:,1:] be those remaining columns. Then Qn Qn^T is an orthogonal projector into the tangent space.
2585: Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
2586: In total, we have a system of 3 equations in 3 unknowns:
2588: f(y) = 0 1 equation
2589: Qn^T (y - x) = 0 2 equations
2591: Here, we compute the residual and Jacobian of this system.
2592: */
2593: static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2594: {
2595: PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2596: PetscReal d[3] = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
2597: PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
2598: PetscReal n_y[3][3] = {
2599: {0, 0, 0},
2600: {0, 0, 0},
2601: {0, 0, 0}
2602: };
2604: feval(yreal, &f, grad, n_y);
2606: for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
2607: norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2608: for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2610: // Define the Householder reflector
2611: sign = n[0] >= 0 ? 1. : -1.;
2612: n[0] += norm * sign;
2613: for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
2615: norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2616: norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
2617: norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
2618: norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
2620: for (PetscInt i = 0; i < 3; i++) {
2621: n[i] /= norm;
2622: for (PetscInt j = 0; j < 3; j++) {
2623: // note that n[i] is n_old[i]/norm when executing the code below
2624: n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
2625: }
2626: }
2628: nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
2629: for (PetscInt i = 0; i < 3; i++) nd_y[i] = n[i] + n_y[0][i] * d[0] + n_y[1][i] * d[1] + n_y[2][i] * d[2];
2631: res[0] = f;
2632: res[1] = d[1] - 2 * n[1] * nd;
2633: res[2] = d[2] - 2 * n[2] * nd;
2634: // J[j][i] is J_{ij} (column major)
2635: for (PetscInt j = 0; j < 3; j++) {
2636: J[0 + j * 3] = grad[j];
2637: J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
2638: J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
2639: }
2640: }
2642: /*
2643: Project x to the nearest point on the implicit surface using Newton's method.
2644: */
2645: static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
2646: {
2647: PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
2649: for (PetscInt iter = 0; iter < 10; iter++) {
2650: PetscScalar res[3], J[9];
2651: PetscReal resnorm;
2652: TPSNearestPointResJac(feval, x, y, res, J);
2653: resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
2654: if (0) { // Turn on this monitor if you need to confirm quadratic convergence
2655: PetscPrintf(PETSC_COMM_SELF, "[%" PetscInt_FMT "] res [%g %g %g]\n", iter, (double)PetscRealPart(res[0]), (double)PetscRealPart(res[1]), (double)PetscRealPart(res[2]));
2656: }
2657: if (resnorm < PETSC_SMALL) break;
2659: // Take the Newton step
2660: PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL);
2661: PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
2662: }
2663: for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
2664: return 0;
2665: }
2667: const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
2669: static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
2670: {
2671: PetscMPIInt rank;
2672: PetscInt topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
2673: PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
2674: PetscInt *cells_flat = NULL;
2675: PetscReal *vtxCoords = NULL;
2676: TPSEvaluateFunc evalFunc = NULL;
2677: PetscSimplePointFunc normalFunc = NULL;
2678: DMLabel label;
2680: MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
2682: switch (tpstype) {
2683: case DMPLEX_TPS_SCHWARZ_P:
2685: if (rank == 0) {
2686: PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
2687: PetscInt Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
2688: PetscReal L = 1;
2690: Npipes[0] = (extent[0] + 1) * extent[1] * extent[2];
2691: Npipes[1] = extent[0] * (extent[1] + 1) * extent[2];
2692: Npipes[2] = extent[0] * extent[1] * (extent[2] + 1);
2693: Njunctions = extent[0] * extent[1] * extent[2];
2694: Ncuts = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
2695: numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
2696: PetscMalloc1(3 * numVertices, &vtxCoords);
2697: PetscMalloc1(Njunctions, &cells);
2698: PetscMalloc1(Ncuts * 4, &edges);
2699: PetscMalloc1(Ncuts * 4, &edgeSets);
2700: // x-normal pipes
2701: vcount = 0;
2702: for (PetscInt i = 0; i < extent[0] + 1; i++) {
2703: for (PetscInt j = 0; j < extent[1]; j++) {
2704: for (PetscInt k = 0; k < extent[2]; k++) {
2705: for (PetscInt l = 0; l < 4; l++) {
2706: vtxCoords[vcount++] = (2 * i - 1) * L;
2707: vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2708: vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2709: }
2710: }
2711: }
2712: }
2713: // y-normal pipes
2714: for (PetscInt i = 0; i < extent[0]; i++) {
2715: for (PetscInt j = 0; j < extent[1] + 1; j++) {
2716: for (PetscInt k = 0; k < extent[2]; k++) {
2717: for (PetscInt l = 0; l < 4; l++) {
2718: vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2719: vtxCoords[vcount++] = (2 * j - 1) * L;
2720: vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2721: }
2722: }
2723: }
2724: }
2725: // z-normal pipes
2726: for (PetscInt i = 0; i < extent[0]; i++) {
2727: for (PetscInt j = 0; j < extent[1]; j++) {
2728: for (PetscInt k = 0; k < extent[2] + 1; k++) {
2729: for (PetscInt l = 0; l < 4; l++) {
2730: vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2731: vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2732: vtxCoords[vcount++] = (2 * k - 1) * L;
2733: }
2734: }
2735: }
2736: }
2737: // junctions
2738: for (PetscInt i = 0; i < extent[0]; i++) {
2739: for (PetscInt j = 0; j < extent[1]; j++) {
2740: for (PetscInt k = 0; k < extent[2]; k++) {
2741: const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
2743: for (PetscInt ii = 0; ii < 2; ii++) {
2744: for (PetscInt jj = 0; jj < 2; jj++) {
2745: for (PetscInt kk = 0; kk < 2; kk++) {
2746: double Ls = (1 - sqrt(2) / 4) * L;
2747: vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
2748: vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
2749: vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
2750: }
2751: }
2752: }
2753: const PetscInt jfaces[3][2][4] = {
2754: {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
2755: {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
2756: {{6, 2, 0, 4}, {7, 3, 1, 5}} // z-aligned
2757: };
2758: const PetscInt pipe_lo[3] = {// vertex numbers of pipes
2759: ((i * extent[1] + j) * extent[2] + k) * 4, ((i * (extent[1] + 1) + j) * extent[2] + k + Npipes[0]) * 4, ((i * extent[1] + j) * (extent[2] + 1) + k + Npipes[0] + Npipes[1]) * 4};
2760: const PetscInt pipe_hi[3] = {// vertex numbers of pipes
2761: (((i + 1) * extent[1] + j) * extent[2] + k) * 4, ((i * (extent[1] + 1) + j + 1) * extent[2] + k + Npipes[0]) * 4, ((i * extent[1] + j) * (extent[2] + 1) + k + 1 + Npipes[0] + Npipes[1]) * 4};
2762: for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
2763: const PetscInt ijk[3] = {i, j, k};
2764: for (PetscInt l = 0; l < 4; l++) { // rotations
2765: cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
2766: cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
2767: cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
2768: cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
2769: cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
2770: cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
2771: cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
2772: cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
2773: if (ijk[dir] == 0) {
2774: edges[numEdges][0] = pipe_lo[dir] + l;
2775: edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
2776: edgeSets[numEdges] = dir * 2 + 1;
2777: numEdges++;
2778: }
2779: if (ijk[dir] + 1 == extent[dir]) {
2780: edges[numEdges][0] = pipe_hi[dir] + l;
2781: edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
2782: edgeSets[numEdges] = dir * 2 + 2;
2783: numEdges++;
2784: }
2785: }
2786: }
2787: }
2788: }
2789: }
2791: numFaces = 24 * Njunctions;
2792: cells_flat = cells[0][0][0];
2793: }
2794: evalFunc = TPSEvaluate_SchwarzP;
2795: normalFunc = TPSExtrudeNormalFunc_SchwarzP;
2796: break;
2797: case DMPLEX_TPS_GYROID:
2798: if (rank == 0) {
2799: // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
2800: //
2801: // sin(pi*x)*cos(pi*(y+1/2)) + sin(pi*(y+1/2))*cos(pi*(z+1/4)) + sin(pi*(z+1/4))*cos(x)
2802: //
2803: // on the cell [0,2]^3.
2804: //
2805: // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
2806: // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
2807: // like a boomerang:
2808: //
2809: // z = 0 z = 1/4 z = 1/2 z = 3/4 //
2810: // ----- ------- ------- ------- //
2811: // //
2812: // + + + + + + + \ + //
2813: // \ / \ //
2814: // \ `-_ _-' / } //
2815: // *-_ `-' _-' / //
2816: // + `-+ + + +-' + + / + //
2817: // //
2818: // //
2819: // z = 1 z = 5/4 z = 3/2 z = 7/4 //
2820: // ----- ------- ------- ------- //
2821: // //
2822: // +-_ + + + + _-+ + / + //
2823: // `-_ _-_ _-` / //
2824: // \ _-' `-_ / { //
2825: // \ / \ //
2826: // + + + + + + + \ + //
2827: //
2828: //
2829: // This course mesh approximates each of these slices by two line segments,
2830: // and then connects the segments in consecutive layers with quadrilateral faces.
2831: // All of the end points of the segments are multiples of 1/4 except for the
2832: // point * in the picture for z = 0 above and the similar points in other layers.
2833: // That point is at (gamma, gamma, 0), where gamma is calculated below.
2834: //
2835: // The column [1,2]x[1,2]x[0,2] looks the same as this column;
2836: // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
2837: //
2838: // As for how this method turned into the names given to the vertices:
2839: // that was not systematic, it was just the way it worked out in my handwritten notes.
2841: PetscInt facesPerBlock = 64;
2842: PetscInt vertsPerBlock = 56;
2843: PetscInt extentPlus[3];
2844: PetscInt numBlocks, numBlocksPlus;
2845: const PetscInt A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, II = 8, J = 9, K = 10, L = 11, M = 12, N = 13, O = 14, P = 15, Q = 16, R = 17, S = 18, T = 19, U = 20, V = 21, W = 22, X = 23, Y = 24, Z = 25, Ap = 26, Bp = 27, Cp = 28, Dp = 29, Ep = 30, Fp = 31, Gp = 32, Hp = 33, Ip = 34, Jp = 35, Kp = 36, Lp = 37, Mp = 38, Np = 39, Op = 40, Pp = 41, Qp = 42, Rp = 43, Sp = 44, Tp = 45, Up = 46, Vp = 47, Wp = 48, Xp = 49, Yp = 50, Zp = 51, Aq = 52, Bq = 53, Cq = 54, Dq = 55;
2846: const PetscInt pattern[64][4] = {
2847: /* face to vertex within the coarse discretization of a single gyroid block */
2848: /* layer 0 */
2849: {A, C, K, G },
2850: {C, B, II, K },
2851: {D, A, H, L },
2852: {B + 56 * 1, D, L, J },
2853: {E, B + 56 * 1, J, N },
2854: {A + 56 * 2, E, N, H + 56 * 2 },
2855: {F, A + 56 * 2, G + 56 * 2, M },
2856: {B, F, M, II },
2857: /* layer 1 */
2858: {G, K, Q, O },
2859: {K, II, P, Q },
2860: {L, H, O + 56 * 1, R },
2861: {J, L, R, P },
2862: {N, J, P, S },
2863: {H + 56 * 2, N, S, O + 56 * 3 },
2864: {M, G + 56 * 2, O + 56 * 2, T },
2865: {II, M, T, P },
2866: /* layer 2 */
2867: {O, Q, Y, U },
2868: {Q, P, W, Y },
2869: {R, O + 56 * 1, U + 56 * 1, Ap },
2870: {P, R, Ap, W },
2871: {S, P, X, Bp },
2872: {O + 56 * 3, S, Bp, V + 56 * 1 },
2873: {T, O + 56 * 2, V, Z },
2874: {P, T, Z, X },
2875: /* layer 3 */
2876: {U, Y, Ep, Dp },
2877: {Y, W, Cp, Ep },
2878: {Ap, U + 56 * 1, Dp + 56 * 1, Gp },
2879: {W, Ap, Gp, Cp },
2880: {Bp, X, Cp + 56 * 2, Fp },
2881: {V + 56 * 1, Bp, Fp, Dp + 56 * 1},
2882: {Z, V, Dp, Hp },
2883: {X, Z, Hp, Cp + 56 * 2},
2884: /* layer 4 */
2885: {Dp, Ep, Mp, Kp },
2886: {Ep, Cp, Ip, Mp },
2887: {Gp, Dp + 56 * 1, Lp, Np },
2888: {Cp, Gp, Np, Jp },
2889: {Fp, Cp + 56 * 2, Jp + 56 * 2, Pp },
2890: {Dp + 56 * 1, Fp, Pp, Lp },
2891: {Hp, Dp, Kp, Op },
2892: {Cp + 56 * 2, Hp, Op, Ip + 56 * 2},
2893: /* layer 5 */
2894: {Kp, Mp, Sp, Rp },
2895: {Mp, Ip, Qp, Sp },
2896: {Np, Lp, Rp, Tp },
2897: {Jp, Np, Tp, Qp + 56 * 1},
2898: {Pp, Jp + 56 * 2, Qp + 56 * 3, Up },
2899: {Lp, Pp, Up, Rp },
2900: {Op, Kp, Rp, Vp },
2901: {Ip + 56 * 2, Op, Vp, Qp + 56 * 2},
2902: /* layer 6 */
2903: {Rp, Sp, Aq, Yp },
2904: {Sp, Qp, Wp, Aq },
2905: {Tp, Rp, Yp, Cq },
2906: {Qp + 56 * 1, Tp, Cq, Wp + 56 * 1},
2907: {Up, Qp + 56 * 3, Xp + 56 * 1, Dq },
2908: {Rp, Up, Dq, Zp },
2909: {Vp, Rp, Zp, Bq },
2910: {Qp + 56 * 2, Vp, Bq, Xp },
2911: /* layer 7 (the top is the periodic image of the bottom of layer 0) */
2912: {Yp, Aq, C + 56 * 4, A + 56 * 4 },
2913: {Aq, Wp, B + 56 * 4, C + 56 * 4 },
2914: {Cq, Yp, A + 56 * 4, D + 56 * 4 },
2915: {Wp + 56 * 1, Cq, D + 56 * 4, B + 56 * 5 },
2916: {Dq, Xp + 56 * 1, B + 56 * 5, E + 56 * 4 },
2917: {Zp, Dq, E + 56 * 4, A + 56 * 6 },
2918: {Bq, Zp, A + 56 * 6, F + 56 * 4 },
2919: {Xp, Bq, F + 56 * 4, B + 56 * 4 }
2920: };
2921: const PetscReal gamma = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
2922: const PetscReal patternCoords[56][3] = {
2923: {1., 0., 0. }, /* A */
2924: {0., 1., 0. }, /* B */
2925: {gamma, gamma, 0. }, /* C */
2926: {1 + gamma, 1 - gamma, 0. }, /* D */
2927: {2 - gamma, 2 - gamma, 0. }, /* E */
2928: {1 - gamma, 1 + gamma, 0. }, /* F */
2930: {.5, 0, .25 }, /* G */
2931: {1.5, 0., .25 }, /* H */
2932: {.5, 1., .25 }, /* II */
2933: {1.5, 1., .25 }, /* J */
2934: {.25, .5, .25 }, /* K */
2935: {1.25, .5, .25 }, /* L */
2936: {.75, 1.5, .25 }, /* M */
2937: {1.75, 1.5, .25 }, /* N */
2939: {0., 0., .5 }, /* O */
2940: {1., 1., .5 }, /* P */
2941: {gamma, 1 - gamma, .5 }, /* Q */
2942: {1 + gamma, gamma, .5 }, /* R */
2943: {2 - gamma, 1 + gamma, .5 }, /* S */
2944: {1 - gamma, 2 - gamma, .5 }, /* T */
2946: {0., .5, .75 }, /* U */
2947: {0., 1.5, .75 }, /* V */
2948: {1., .5, .75 }, /* W */
2949: {1., 1.5, .75 }, /* X */
2950: {.5, .75, .75 }, /* Y */
2951: {.5, 1.75, .75 }, /* Z */
2952: {1.5, .25, .75 }, /* Ap */
2953: {1.5, 1.25, .75 }, /* Bp */
2955: {1., 0., 1. }, /* Cp */
2956: {0., 1., 1. }, /* Dp */
2957: {1 - gamma, 1 - gamma, 1. }, /* Ep */
2958: {1 + gamma, 1 + gamma, 1. }, /* Fp */
2959: {2 - gamma, gamma, 1. }, /* Gp */
2960: {gamma, 2 - gamma, 1. }, /* Hp */
2962: {.5, 0., 1.25}, /* Ip */
2963: {1.5, 0., 1.25}, /* Jp */
2964: {.5, 1., 1.25}, /* Kp */
2965: {1.5, 1., 1.25}, /* Lp */
2966: {.75, .5, 1.25}, /* Mp */
2967: {1.75, .5, 1.25}, /* Np */
2968: {.25, 1.5, 1.25}, /* Op */
2969: {1.25, 1.5, 1.25}, /* Pp */
2971: {0., 0., 1.5 }, /* Qp */
2972: {1., 1., 1.5 }, /* Rp */
2973: {1 - gamma, gamma, 1.5 }, /* Sp */
2974: {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
2975: {1 + gamma, 2 - gamma, 1.5 }, /* Up */
2976: {gamma, 1 + gamma, 1.5 }, /* Vp */
2978: {0., .5, 1.75}, /* Wp */
2979: {0., 1.5, 1.75}, /* Xp */
2980: {1., .5, 1.75}, /* Yp */
2981: {1., 1.5, 1.75}, /* Zp */
2982: {.5, .25, 1.75}, /* Aq */
2983: {.5, 1.25, 1.75}, /* Bq */
2984: {1.5, .75, 1.75}, /* Cq */
2985: {1.5, 1.75, 1.75}, /* Dq */
2986: };
2987: PetscInt(*cells)[64][4] = NULL;
2988: PetscBool *seen;
2989: PetscInt *vertToTrueVert;
2990: PetscInt count;
2992: for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
2993: numBlocks = 1;
2994: for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
2995: numBlocksPlus = 1;
2996: for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
2997: numFaces = numBlocks * facesPerBlock;
2998: PetscMalloc1(numBlocks, &cells);
2999: PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen);
3000: for (PetscInt k = 0; k < extent[2]; k++) {
3001: for (PetscInt j = 0; j < extent[1]; j++) {
3002: for (PetscInt i = 0; i < extent[0]; i++) {
3003: for (PetscInt f = 0; f < facesPerBlock; f++) {
3004: for (PetscInt v = 0; v < 4; v++) {
3005: PetscInt vertRaw = pattern[f][v];
3006: PetscInt blockidx = vertRaw / 56;
3007: PetscInt patternvert = vertRaw % 56;
3008: PetscInt xplus = (blockidx & 1);
3009: PetscInt yplus = (blockidx & 2) >> 1;
3010: PetscInt zplus = (blockidx & 4) >> 2;
3011: PetscInt zcoord = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3012: PetscInt ycoord = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3013: PetscInt xcoord = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3014: PetscInt vert = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3016: cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3017: seen[vert] = PETSC_TRUE;
3018: }
3019: }
3020: }
3021: }
3022: }
3023: for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
3024: if (seen[i]) numVertices++;
3025: count = 0;
3026: PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert);
3027: PetscMalloc1(numVertices * 3, &vtxCoords);
3028: for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3029: for (PetscInt k = 0; k < extentPlus[2]; k++) {
3030: for (PetscInt j = 0; j < extentPlus[1]; j++) {
3031: for (PetscInt i = 0; i < extentPlus[0]; i++) {
3032: for (PetscInt v = 0; v < vertsPerBlock; v++) {
3033: PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3035: if (seen[vIdx]) {
3036: PetscInt thisVert;
3038: vertToTrueVert[vIdx] = thisVert = count++;
3040: for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3041: vtxCoords[3 * thisVert + 0] += i * 2;
3042: vtxCoords[3 * thisVert + 1] += j * 2;
3043: vtxCoords[3 * thisVert + 2] += k * 2;
3044: }
3045: }
3046: }
3047: }
3048: }
3049: for (PetscInt i = 0; i < numBlocks; i++) {
3050: for (PetscInt f = 0; f < facesPerBlock; f++) {
3051: for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3052: }
3053: }
3054: PetscFree(vertToTrueVert);
3055: PetscFree(seen);
3056: cells_flat = cells[0][0];
3057: numEdges = 0;
3058: for (PetscInt i = 0; i < numFaces; i++) {
3059: for (PetscInt e = 0; e < 4; e++) {
3060: PetscInt ev[] = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3061: const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3063: for (PetscInt d = 0; d < 3; d++) {
3064: if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3065: if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3066: if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3067: }
3068: }
3069: }
3070: }
3071: PetscMalloc1(numEdges, &edges);
3072: PetscMalloc1(numEdges, &edgeSets);
3073: for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3074: for (PetscInt e = 0; e < 4; e++) {
3075: PetscInt ev[] = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3076: const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3078: for (PetscInt d = 0; d < 3; d++) {
3079: if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3080: if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3081: edges[edge][0] = ev[0];
3082: edges[edge][1] = ev[1];
3083: edgeSets[edge++] = 2 * d;
3084: }
3085: if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3086: edges[edge][0] = ev[0];
3087: edges[edge][1] = ev[1];
3088: edgeSets[edge++] = 2 * d + 1;
3089: }
3090: }
3091: }
3092: }
3093: }
3094: }
3095: evalFunc = TPSEvaluate_Gyroid;
3096: normalFunc = TPSExtrudeNormalFunc_Gyroid;
3097: break;
3098: }
3100: DMSetDimension(dm, topoDim);
3101: if (rank == 0) DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat);
3102: else DMPlexBuildFromCellList(dm, 0, 0, 0, NULL);
3103: PetscFree(cells_flat);
3104: {
3105: DM idm;
3106: DMPlexInterpolate(dm, &idm);
3107: DMPlexReplace_Internal(dm, &idm);
3108: }
3109: if (rank == 0) DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords);
3110: else DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL);
3111: PetscFree(vtxCoords);
3113: DMCreateLabel(dm, "Face Sets");
3114: DMGetLabel(dm, "Face Sets", &label);
3115: for (PetscInt e = 0; e < numEdges; e++) {
3116: PetscInt njoin;
3117: const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
3118: DMPlexGetJoin(dm, 2, verts, &njoin, &join);
3120: DMLabelSetValue(label, join[0], edgeSets[e]);
3121: DMPlexRestoreJoin(dm, 2, verts, &njoin, &join);
3122: }
3123: PetscFree(edges);
3124: PetscFree(edgeSets);
3125: if (tps_distribute) {
3126: DM pdm = NULL;
3127: PetscPartitioner part;
3129: DMPlexGetPartitioner(dm, &part);
3130: PetscPartitionerSetFromOptions(part);
3131: DMPlexDistribute(dm, 0, NULL, &pdm);
3132: if (pdm) DMPlexReplace_Internal(dm, &pdm);
3133: // Do not auto-distribute again
3134: DMPlexDistributeSetDefault(dm, PETSC_FALSE);
3135: }
3137: DMPlexSetRefinementUniform(dm, PETSC_TRUE);
3138: for (PetscInt refine = 0; refine < refinements; refine++) {
3139: PetscInt m;
3140: DM dmf;
3141: Vec X;
3142: PetscScalar *x;
3143: DMRefine(dm, MPI_COMM_NULL, &dmf);
3144: DMPlexReplace_Internal(dm, &dmf);
3146: DMGetCoordinatesLocal(dm, &X);
3147: VecGetLocalSize(X, &m);
3148: VecGetArray(X, &x);
3149: for (PetscInt i = 0; i < m; i += 3) TPSNearestPoint(evalFunc, &x[i]);
3150: VecRestoreArray(X, &x);
3151: }
3153: // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
3154: DMGetLabel(dm, "Face Sets", &label);
3155: DMPlexLabelComplete(dm, label);
3157: if (thickness > 0) {
3158: DM edm, cdm, ecdm;
3159: DMPlexTransform tr;
3160: const char *prefix;
3161: PetscOptions options;
3162: // Code from DMPlexExtrude
3163: DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr);
3164: DMPlexTransformSetDM(tr, dm);
3165: DMPlexTransformSetType(tr, DMPLEXEXTRUDE);
3166: PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix);
3167: PetscObjectSetOptionsPrefix((PetscObject)tr, prefix);
3168: PetscObjectGetOptions((PetscObject)dm, &options);
3169: PetscObjectSetOptions((PetscObject)tr, options);
3170: DMPlexTransformExtrudeSetLayers(tr, layers);
3171: DMPlexTransformExtrudeSetThickness(tr, thickness);
3172: DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE);
3173: DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE);
3174: DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc);
3175: DMPlexTransformSetFromOptions(tr);
3176: PetscObjectSetOptions((PetscObject)tr, NULL);
3177: DMPlexTransformSetUp(tr);
3178: PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view");
3179: DMPlexTransformApply(tr, dm, &edm);
3180: DMCopyDisc(dm, edm);
3181: DMGetCoordinateDM(dm, &cdm);
3182: DMGetCoordinateDM(edm, &ecdm);
3183: DMCopyDisc(cdm, ecdm);
3184: DMPlexTransformCreateDiscLabels(tr, edm);
3185: DMPlexTransformDestroy(&tr);
3186: if (edm) {
3187: ((DM_Plex *)edm->data)->printFEM = ((DM_Plex *)dm->data)->printFEM;
3188: ((DM_Plex *)edm->data)->printL2 = ((DM_Plex *)dm->data)->printL2;
3189: ((DM_Plex *)edm->data)->printLocate = ((DM_Plex *)dm->data)->printLocate;
3190: }
3191: DMPlexReplace_Internal(dm, &edm);
3192: }
3193: return 0;
3194: }
3196: /*@
3197: DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
3199: Collective
3201: Input Parameters:
3202: + comm - The communicator for the `DM` object
3203: . tpstype - Type of triply-periodic surface
3204: . extent - Array of length 3 containing number of periods in each direction
3205: . periodic - array of length 3 with periodicity, or NULL for non-periodic
3206: . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
3207: . refinements - Number of factor-of-2 refinements of 2D manifold mesh
3208: . layers - Number of cell layers extruded in normal direction
3209: - thickness - Thickness in normal direction
3211: Output Parameter:
3212: . dm - The `DM` object
3214: Level: beginner
3216: Notes:
3217: This meshes the surface of the Schwarz P or Gyroid surfaces. Schwarz P is is the simplest member of the triply-periodic minimal surfaces.
3218: https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22) and can be cut with "clean" boundaries.
3219: The Gyroid (https://en.wikipedia.org/wiki/Gyroid) is another triply-periodic minimal surface with applications in additive manufacturing; it is much more difficult to "cut" since there are no planes of symmetry.
3220: Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
3221: On each refinement, all vertices are projected to their nearest point on the surface.
3222: This projection could readily be extended to related surfaces.
3224: The face (edge) sets for the Schwarz P surface are numbered 1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z).
3225: When the mesh is refined, "Face Sets" contain the new vertices (created during refinement). Use DMPlexLabelComplete() to propagate to coarse-level vertices.
3227: Developer Note:
3228: The Gyroid mesh does not currently mark boundary sets.
3230: References:
3231: . * - Maskery et al, Insights into the mechanical properties of several triply periodic minimal surface lattice structures made by polymer additive manufacturing, 2017.
3232: https://doi.org/10.1016/j.polymer.2017.11.049
3234: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
3235: @*/
3236: PetscErrorCode DMPlexCreateTPSMesh(MPI_Comm comm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness, DM *dm)
3237: {
3238: DMCreate(comm, dm);
3239: DMSetType(*dm, DMPLEX);
3240: DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness);
3241: return 0;
3242: }
3244: /*@
3245: DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
3247: Collective
3249: Input Parameters:
3250: + comm - The communicator for the `DM` object
3251: . dim - The dimension
3252: . simplex - Use simplices, or tensor product cells
3253: - R - The radius
3255: Output Parameter:
3256: . dm - The `DM` object
3258: Level: beginner
3260: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
3261: @*/
3262: PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
3263: {
3265: DMCreate(comm, dm);
3266: DMSetType(*dm, DMPLEX);
3267: DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R);
3268: return 0;
3269: }
3271: static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
3272: {
3273: DM sdm, vol;
3274: DMLabel bdlabel;
3276: DMCreate(PetscObjectComm((PetscObject)dm), &sdm);
3277: DMSetType(sdm, DMPLEX);
3278: PetscObjectSetOptionsPrefix((PetscObject)sdm, "bd_");
3279: DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R);
3280: DMSetFromOptions(sdm);
3281: DMViewFromOptions(sdm, NULL, "-dm_view");
3282: DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol);
3283: DMDestroy(&sdm);
3284: DMPlexReplace_Internal(dm, &vol);
3285: DMCreateLabel(dm, "marker");
3286: DMGetLabel(dm, "marker", &bdlabel);
3287: DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel);
3288: DMPlexLabelComplete(dm, bdlabel);
3289: return 0;
3290: }
3292: /*@
3293: DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
3295: Collective
3297: Input Parameters:
3298: + comm - The communicator for the `DM` object
3299: . dim - The dimension
3300: - R - The radius
3302: Output Parameter:
3303: . dm - The `DM` object
3305: Options Database Key:
3306: - bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
3308: Level: beginner
3310: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
3311: @*/
3312: PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
3313: {
3314: DMCreate(comm, dm);
3315: DMSetType(*dm, DMPLEX);
3316: DMPlexCreateBallMesh_Internal(*dm, dim, R);
3317: return 0;
3318: }
3320: static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
3321: {
3322: switch (ct) {
3323: case DM_POLYTOPE_POINT: {
3324: PetscInt numPoints[1] = {1};
3325: PetscInt coneSize[1] = {0};
3326: PetscInt cones[1] = {0};
3327: PetscInt coneOrientations[1] = {0};
3328: PetscScalar vertexCoords[1] = {0.0};
3330: DMSetDimension(rdm, 0);
3331: DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3332: } break;
3333: case DM_POLYTOPE_SEGMENT: {
3334: PetscInt numPoints[2] = {2, 1};
3335: PetscInt coneSize[3] = {2, 0, 0};
3336: PetscInt cones[2] = {1, 2};
3337: PetscInt coneOrientations[2] = {0, 0};
3338: PetscScalar vertexCoords[2] = {-1.0, 1.0};
3340: DMSetDimension(rdm, 1);
3341: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3342: } break;
3343: case DM_POLYTOPE_POINT_PRISM_TENSOR: {
3344: PetscInt numPoints[2] = {2, 1};
3345: PetscInt coneSize[3] = {2, 0, 0};
3346: PetscInt cones[2] = {1, 2};
3347: PetscInt coneOrientations[2] = {0, 0};
3348: PetscScalar vertexCoords[2] = {-1.0, 1.0};
3350: DMSetDimension(rdm, 1);
3351: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3352: } break;
3353: case DM_POLYTOPE_TRIANGLE: {
3354: PetscInt numPoints[2] = {3, 1};
3355: PetscInt coneSize[4] = {3, 0, 0, 0};
3356: PetscInt cones[3] = {1, 2, 3};
3357: PetscInt coneOrientations[3] = {0, 0, 0};
3358: PetscScalar vertexCoords[6] = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
3360: DMSetDimension(rdm, 2);
3361: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3362: } break;
3363: case DM_POLYTOPE_QUADRILATERAL: {
3364: PetscInt numPoints[2] = {4, 1};
3365: PetscInt coneSize[5] = {4, 0, 0, 0, 0};
3366: PetscInt cones[4] = {1, 2, 3, 4};
3367: PetscInt coneOrientations[4] = {0, 0, 0, 0};
3368: PetscScalar vertexCoords[8] = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
3370: DMSetDimension(rdm, 2);
3371: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3372: } break;
3373: case DM_POLYTOPE_SEG_PRISM_TENSOR: {
3374: PetscInt numPoints[2] = {4, 1};
3375: PetscInt coneSize[5] = {4, 0, 0, 0, 0};
3376: PetscInt cones[4] = {1, 2, 3, 4};
3377: PetscInt coneOrientations[4] = {0, 0, 0, 0};
3378: PetscScalar vertexCoords[8] = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
3380: DMSetDimension(rdm, 2);
3381: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3382: } break;
3383: case DM_POLYTOPE_TETRAHEDRON: {
3384: PetscInt numPoints[2] = {4, 1};
3385: PetscInt coneSize[5] = {4, 0, 0, 0, 0};
3386: PetscInt cones[4] = {1, 2, 3, 4};
3387: PetscInt coneOrientations[4] = {0, 0, 0, 0};
3388: PetscScalar vertexCoords[12] = {-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0};
3390: DMSetDimension(rdm, 3);
3391: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3392: } break;
3393: case DM_POLYTOPE_HEXAHEDRON: {
3394: PetscInt numPoints[2] = {8, 1};
3395: PetscInt coneSize[9] = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3396: PetscInt cones[8] = {1, 2, 3, 4, 5, 6, 7, 8};
3397: PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3398: PetscScalar vertexCoords[24] = {-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0};
3400: DMSetDimension(rdm, 3);
3401: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3402: } break;
3403: case DM_POLYTOPE_TRI_PRISM: {
3404: PetscInt numPoints[2] = {6, 1};
3405: PetscInt coneSize[7] = {6, 0, 0, 0, 0, 0, 0};
3406: PetscInt cones[6] = {1, 2, 3, 4, 5, 6};
3407: PetscInt coneOrientations[6] = {0, 0, 0, 0, 0, 0};
3408: PetscScalar vertexCoords[18] = {-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0};
3410: DMSetDimension(rdm, 3);
3411: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3412: } break;
3413: case DM_POLYTOPE_TRI_PRISM_TENSOR: {
3414: PetscInt numPoints[2] = {6, 1};
3415: PetscInt coneSize[7] = {6, 0, 0, 0, 0, 0, 0};
3416: PetscInt cones[6] = {1, 2, 3, 4, 5, 6};
3417: PetscInt coneOrientations[6] = {0, 0, 0, 0, 0, 0};
3418: PetscScalar vertexCoords[18] = {-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0};
3420: DMSetDimension(rdm, 3);
3421: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3422: } break;
3423: case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
3424: PetscInt numPoints[2] = {8, 1};
3425: PetscInt coneSize[9] = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3426: PetscInt cones[8] = {1, 2, 3, 4, 5, 6, 7, 8};
3427: PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3428: PetscScalar vertexCoords[24] = {-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0};
3430: DMSetDimension(rdm, 3);
3431: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3432: } break;
3433: case DM_POLYTOPE_PYRAMID: {
3434: PetscInt numPoints[2] = {5, 1};
3435: PetscInt coneSize[6] = {5, 0, 0, 0, 0, 0};
3436: PetscInt cones[5] = {1, 2, 3, 4, 5};
3437: PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3438: PetscScalar vertexCoords[24] = {-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, 1.0};
3440: DMSetDimension(rdm, 3);
3441: DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);
3442: } break;
3443: default:
3444: SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
3445: }
3446: {
3447: PetscInt Nv, v;
3449: /* Must create the celltype label here so that we do not automatically try to compute the types */
3450: DMCreateLabel(rdm, "celltype");
3451: DMPlexSetCellType(rdm, 0, ct);
3452: DMPlexGetChart(rdm, NULL, &Nv);
3453: for (v = 1; v < Nv; ++v) DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT);
3454: }
3455: DMPlexInterpolateInPlace_Internal(rdm);
3456: PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]);
3457: return 0;
3458: }
3460: /*@
3461: DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
3463: Collective
3465: Input Parameters:
3466: + comm - The communicator
3467: - ct - The cell type of the reference cell
3469: Output Parameter:
3470: . refdm - The reference cell
3472: Level: intermediate
3474: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateReferenceCell()`, `DMPlexCreateBoxMesh()`
3475: @*/
3476: PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
3477: {
3478: DMCreate(comm, refdm);
3479: DMSetType(*refdm, DMPLEX);
3480: DMPlexCreateReferenceCell_Internal(*refdm, ct);
3481: return 0;
3482: }
3484: static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
3485: {
3486: DM plex;
3487: DMLabel label;
3488: PetscBool hasLabel;
3490: DMHasLabel(dm, name, &hasLabel);
3491: if (hasLabel) return 0;
3492: DMCreateLabel(dm, name);
3493: DMGetLabel(dm, name, &label);
3494: DMConvert(dm, DMPLEX, &plex);
3495: DMPlexMarkBoundaryFaces(plex, 1, label);
3496: DMPlexLabelComplete(plex, label);
3497: DMDestroy(&plex);
3498: return 0;
3499: }
3501: /*
3502: We use the last coordinate as the radius, the inner radius is lower[dim-1] and the outer radius is upper[dim-1]. Then we map the first coordinate around the circle.
3504: (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
3505: */
3506: static void boxToAnnulus(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[])
3507: {
3508: const PetscReal low = PetscRealPart(constants[0]);
3509: const PetscReal upp = PetscRealPart(constants[1]);
3510: const PetscReal r = PetscRealPart(u[1]);
3511: const PetscReal th = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
3513: f0[0] = r * PetscCosReal(th);
3514: f0[1] = r * PetscSinReal(th);
3515: }
3517: const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
3519: static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
3520: {
3521: DMPlexShape shape = DM_SHAPE_BOX;
3522: DMPolytopeType cell = DM_POLYTOPE_TRIANGLE;
3523: PetscInt dim = 2;
3524: PetscBool simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3525: PetscBool flg, flg2, fflg, bdfflg, nameflg;
3526: MPI_Comm comm;
3527: char filename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3528: char bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3529: char plexname[PETSC_MAX_PATH_LEN] = "";
3531: PetscObjectGetComm((PetscObject)dm, &comm);
3532: /* TODO Turn this into a registration interface */
3533: PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg);
3534: PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg);
3535: PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg);
3536: PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL);
3537: PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL);
3538: PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg);
3539: PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0);
3541: PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg);
3542: PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg);
3543: PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg);
3544: PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2);
3545: if (flg || flg2) DMSetBasicAdjacency(dm, adjCone, adjClosure);
3547: switch (cell) {
3548: case DM_POLYTOPE_POINT:
3549: case DM_POLYTOPE_SEGMENT:
3550: case DM_POLYTOPE_POINT_PRISM_TENSOR:
3551: case DM_POLYTOPE_TRIANGLE:
3552: case DM_POLYTOPE_QUADRILATERAL:
3553: case DM_POLYTOPE_TETRAHEDRON:
3554: case DM_POLYTOPE_HEXAHEDRON:
3555: *useCoordSpace = PETSC_TRUE;
3556: break;
3557: default:
3558: *useCoordSpace = PETSC_FALSE;
3559: break;
3560: }
3562: if (fflg) {
3563: DM dmnew;
3565: DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, plexname, interpolate, &dmnew);
3566: DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew);
3567: DMPlexReplace_Internal(dm, &dmnew);
3568: } else if (refDomain) {
3569: DMPlexCreateReferenceCell_Internal(dm, cell);
3570: } else if (bdfflg) {
3571: DM bdm, dmnew;
3573: DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, plexname, interpolate, &bdm);
3574: PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_");
3575: DMSetFromOptions(bdm);
3576: DMPlexGenerate(bdm, NULL, interpolate, &dmnew);
3577: DMDestroy(&bdm);
3578: DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew);
3579: DMPlexReplace_Internal(dm, &dmnew);
3580: } else {
3581: PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]);
3582: switch (shape) {
3583: case DM_SHAPE_BOX:
3584: case DM_SHAPE_ANNULUS: {
3585: PetscInt faces[3] = {0, 0, 0};
3586: PetscReal lower[3] = {0, 0, 0};
3587: PetscReal upper[3] = {1, 1, 1};
3588: DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3589: PetscBool isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
3590: PetscInt i, n;
3592: n = dim;
3593: for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
3594: PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);
3595: n = 3;
3596: PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);
3598: n = 3;
3599: PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);
3601: n = 3;
3602: PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg);
3606: if (isAnnular)
3607: for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
3609: switch (cell) {
3610: case DM_POLYTOPE_TRI_PRISM_TENSOR:
3611: DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt);
3612: if (!interpolate) {
3613: DM udm;
3615: DMPlexUninterpolate(dm, &udm);
3616: DMPlexReplace_Internal(dm, &udm);
3617: }
3618: break;
3619: default:
3620: DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate);
3621: break;
3622: }
3623: if (isAnnular) {
3624: DM cdm;
3625: PetscDS cds;
3626: PetscScalar bounds[2] = {lower[0], upper[0]};
3628: // Fix coordinates for annular region
3629: DMSetPeriodicity(dm, NULL, NULL, NULL);
3630: DMSetCellCoordinatesLocal(dm, NULL);
3631: DMSetCellCoordinates(dm, NULL);
3632: DMPlexCreateCoordinateSpace(dm, 1, NULL);
3633: DMGetCoordinateDM(dm, &cdm);
3634: DMGetDS(cdm, &cds);
3635: PetscDSSetConstants(cds, 2, bounds);
3636: DMPlexRemapGeometry(dm, 0.0, boxToAnnulus);
3637: }
3638: } break;
3639: case DM_SHAPE_BOX_SURFACE: {
3640: PetscInt faces[3] = {0, 0, 0};
3641: PetscReal lower[3] = {0, 0, 0};
3642: PetscReal upper[3] = {1, 1, 1};
3643: PetscInt i, n;
3645: n = dim + 1;
3646: for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
3647: PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);
3648: n = 3;
3649: PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);
3651: n = 3;
3652: PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);
3654: DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate);
3655: } break;
3656: case DM_SHAPE_SPHERE: {
3657: PetscReal R = 1.0;
3659: PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg);
3660: DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R);
3661: } break;
3662: case DM_SHAPE_BALL: {
3663: PetscReal R = 1.0;
3665: PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg);
3666: DMPlexCreateBallMesh_Internal(dm, dim, R);
3667: } break;
3668: case DM_SHAPE_CYLINDER: {
3669: DMBoundaryType bdt = DM_BOUNDARY_NONE;
3670: PetscInt Nw = 6;
3672: PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL);
3673: PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL);
3674: switch (cell) {
3675: case DM_POLYTOPE_TRI_PRISM_TENSOR:
3676: DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate);
3677: break;
3678: default:
3679: DMPlexCreateHexCylinderMesh_Internal(dm, bdt);
3680: break;
3681: }
3682: } break;
3683: case DM_SHAPE_SCHWARZ_P: // fallthrough
3684: case DM_SHAPE_GYROID: {
3685: PetscInt extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
3686: PetscReal thickness = 0.;
3687: DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3688: DMPlexTPSType tps_type = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
3689: PetscBool tps_distribute;
3690: PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL);
3691: PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL);
3692: PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL);
3693: PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL);
3694: PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL);
3695: DMPlexDistributeGetDefault(dm, &tps_distribute);
3696: PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL);
3697: DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness);
3698: } break;
3699: case DM_SHAPE_DOUBLET: {
3700: DM dmnew;
3701: PetscReal rl = 0.0;
3703: PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL);
3704: DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew);
3705: DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew);
3706: DMPlexReplace_Internal(dm, &dmnew);
3707: } break;
3708: default:
3709: SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
3710: }
3711: }
3712: DMPlexSetRefinementUniform(dm, PETSC_TRUE);
3713: if (!((PetscObject)dm)->name && nameflg) PetscObjectSetName((PetscObject)dm, plexname);
3714: return 0;
3715: }
3717: PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
3718: {
3719: DM_Plex *mesh = (DM_Plex *)dm->data;
3720: PetscBool flg, flg2;
3721: char bdLabel[PETSC_MAX_PATH_LEN];
3723: /* Handle viewing */
3724: PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL);
3725: PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0);
3726: PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL);
3727: PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0);
3728: PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0);
3729: DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg);
3730: if (flg) PetscLogDefaultBegin();
3731: /* Labeling */
3732: PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg);
3733: if (flg) DMPlexCreateBoundaryLabel_Private(dm, bdLabel);
3734: /* Point Location */
3735: PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL);
3736: /* Partitioning and distribution */
3737: PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL);
3738: /* Generation and remeshing */
3739: PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL);
3740: /* Projection behavior */
3741: PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0);
3742: PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);
3743: /* Checking structure */
3744: {
3745: PetscBool all = PETSC_FALSE;
3747: PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL);
3748: if (all) {
3749: DMPlexCheck(dm);
3750: } else {
3751: PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2);
3752: if (flg && flg2) DMPlexCheckSymmetry(dm);
3753: PetscOptionsBool("-dm_plex_check_skeleton", "Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes)", "DMPlexCheckSkeleton", PETSC_FALSE, &flg, &flg2);
3754: if (flg && flg2) DMPlexCheckSkeleton(dm, 0);
3755: PetscOptionsBool("-dm_plex_check_faces", "Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type", "DMPlexCheckFaces", PETSC_FALSE, &flg, &flg2);
3756: if (flg && flg2) DMPlexCheckFaces(dm, 0);
3757: PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2);
3758: if (flg && flg2) DMPlexCheckGeometry(dm);
3759: PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2);
3760: if (flg && flg2) DMPlexCheckPointSF(dm, NULL, PETSC_FALSE);
3761: PetscOptionsBool("-dm_plex_check_interface_cones", "Check points on inter-partition interfaces have conforming order of cone points", "DMPlexCheckInterfaceCones", PETSC_FALSE, &flg, &flg2);
3762: if (flg && flg2) DMPlexCheckInterfaceCones(dm);
3763: }
3764: PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2);
3765: if (flg && flg2) DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE);
3766: }
3767: {
3768: PetscReal scale = 1.0;
3770: PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg);
3771: if (flg) {
3772: Vec coordinates, coordinatesLocal;
3774: DMGetCoordinates(dm, &coordinates);
3775: DMGetCoordinatesLocal(dm, &coordinatesLocal);
3776: VecScale(coordinates, scale);
3777: VecScale(coordinatesLocal, scale);
3778: }
3779: }
3780: PetscPartitionerSetFromOptions(mesh->partitioner);
3781: return 0;
3782: }
3784: PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
3785: {
3786: PetscInt numOvLabels = 16, numOvExLabels = 16;
3787: char *ovLabelNames[16], *ovExLabelNames[16];
3788: PetscInt numOvValues = 16, numOvExValues = 16, l;
3789: PetscBool flg;
3791: PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0);
3792: PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg);
3793: if (!flg) numOvLabels = 0;
3794: if (numOvLabels) {
3795: ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
3796: for (l = 0; l < numOvLabels; ++l) {
3797: DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]);
3799: PetscFree(ovLabelNames[l]);
3800: }
3801: PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg);
3802: if (!flg) numOvValues = 0;
3805: PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg);
3806: if (!flg) numOvExLabels = 0;
3807: ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
3808: for (l = 0; l < numOvExLabels; ++l) {
3809: DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]);
3811: PetscFree(ovExLabelNames[l]);
3812: }
3813: PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg);
3814: if (!flg) numOvExValues = 0;
3816: }
3817: return 0;
3818: }
3820: static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
3821: {
3822: PetscFunctionList ordlist;
3823: char oname[256];
3824: PetscReal volume = -1.0;
3825: PetscInt prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
3826: PetscBool uniformOrig, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute, interpolate = PETSC_TRUE, coordSpace = PETSC_TRUE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, ignoreModel = PETSC_FALSE, flg;
3827: DMPlexReorderDefaultFlag reorder;
3829: PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
3830: /* Handle automatic creation */
3831: DMGetDimension(dm, &dim);
3832: if (dim < 0) {
3833: DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm);
3834: created = PETSC_TRUE;
3835: }
3836: DMGetDimension(dm, &dim);
3837: /* Handle interpolation before distribution */
3838: PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg);
3839: if (flg) {
3840: DMPlexInterpolatedFlag interpolated;
3842: DMPlexIsInterpolated(dm, &interpolated);
3843: if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
3844: DM udm;
3846: DMPlexUninterpolate(dm, &udm);
3847: DMPlexReplace_Internal(dm, &udm);
3848: } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
3849: DM idm;
3851: DMPlexInterpolate(dm, &idm);
3852: DMPlexReplace_Internal(dm, &idm);
3853: }
3854: }
3855: /* Handle DMPlex refinement before distribution */
3856: PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg);
3857: if (flg) ((DM_Plex *)dm->data)->ignoreModel = ignoreModel;
3858: DMPlexGetRefinementUniform(dm, &uniformOrig);
3859: PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0);
3860: PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);
3861: PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg);
3862: if (flg) DMPlexSetRefinementUniform(dm, uniform);
3863: PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg);
3864: if (flg) {
3865: DMPlexSetRefinementUniform(dm, PETSC_FALSE);
3866: DMPlexSetRefinementLimit(dm, volume);
3867: prerefine = PetscMax(prerefine, 1);
3868: }
3869: for (r = 0; r < prerefine; ++r) {
3870: DM rdm;
3871: PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
3873: DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject);
3874: DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm);
3875: DMPlexReplace_Internal(dm, &rdm);
3876: DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject);
3877: if (coordFunc && remap) {
3878: DMPlexRemapGeometry(dm, 0.0, coordFunc);
3879: ((DM_Plex *)dm->data)->coordFunc = coordFunc;
3880: }
3881: }
3882: DMPlexSetRefinementUniform(dm, uniformOrig);
3883: /* Handle DMPlex extrusion before distribution */
3884: PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0);
3885: if (extLayers) {
3886: DM edm;
3888: DMExtrude(dm, extLayers, &edm);
3889: DMPlexReplace_Internal(dm, &edm);
3890: ((DM_Plex *)dm->data)->coordFunc = NULL;
3891: DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject);
3892: extLayers = 0;
3893: }
3894: /* Handle DMPlex reordering before distribution */
3895: DMPlexReorderGetDefault(dm, &reorder);
3896: MatGetOrderingList(&ordlist);
3897: PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname));
3898: PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg);
3899: if (reorder == DMPLEX_REORDER_DEFAULT_TRUE || flg) {
3900: DM pdm;
3901: IS perm;
3903: DMPlexGetOrdering(dm, oname, NULL, &perm);
3904: DMPlexPermute(dm, perm, &pdm);
3905: ISDestroy(&perm);
3906: DMPlexReplace_Internal(dm, &pdm);
3907: DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject);
3908: }
3909: /* Handle DMPlex distribution */
3910: DMPlexDistributeGetDefault(dm, &distribute);
3911: PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL);
3912: DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap);
3913: if (distribute) {
3914: DM pdm = NULL;
3915: PetscPartitioner part;
3917: DMPlexGetPartitioner(dm, &part);
3918: PetscPartitionerSetFromOptions(part);
3919: DMPlexDistribute(dm, overlap, NULL, &pdm);
3920: if (pdm) DMPlexReplace_Internal(dm, &pdm);
3921: }
3922: /* Create coordinate space */
3923: if (created) {
3924: DM_Plex *mesh = (DM_Plex *)dm->data;
3925: PetscInt degree = 1;
3926: PetscBool flg;
3928: PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg);
3929: PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, °ree, NULL);
3930: if (coordSpace) DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc);
3931: if (flg && !coordSpace) {
3932: DM cdm;
3933: PetscDS cds;
3934: PetscObject obj;
3935: PetscClassId id;
3937: DMGetCoordinateDM(dm, &cdm);
3938: DMGetDS(cdm, &cds);
3939: PetscDSGetDiscretization(cds, 0, &obj);
3940: PetscObjectGetClassId(obj, &id);
3941: if (id == PETSCFE_CLASSID) {
3942: PetscContainer dummy;
3944: PetscContainerCreate(PETSC_COMM_SELF, &dummy);
3945: PetscObjectSetName((PetscObject)dummy, "coordinates");
3946: DMSetField(cdm, 0, NULL, (PetscObject)dummy);
3947: PetscContainerDestroy(&dummy);
3948: DMClearDS(cdm);
3949: }
3950: mesh->coordFunc = NULL;
3951: }
3952: PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "", dm->sparseLocalize, &dm->sparseLocalize, &flg);
3953: DMLocalizeCoordinates(dm);
3954: }
3955: /* Handle DMPlex refinement */
3956: remap = PETSC_TRUE;
3957: PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0);
3958: PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);
3959: PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0);
3960: if (refine) DMPlexSetRefinementUniform(dm, PETSC_TRUE);
3961: if (refine && isHierarchy) {
3962: DM *dms, coarseDM;
3964: DMGetCoarseDM(dm, &coarseDM);
3965: PetscObjectReference((PetscObject)coarseDM);
3966: PetscMalloc1(refine, &dms);
3967: DMRefineHierarchy(dm, refine, dms);
3968: /* Total hack since we do not pass in a pointer */
3969: DMPlexSwap_Static(dm, dms[refine - 1]);
3970: if (refine == 1) {
3971: DMSetCoarseDM(dm, dms[0]);
3972: DMPlexSetRegularRefinement(dm, PETSC_TRUE);
3973: } else {
3974: DMSetCoarseDM(dm, dms[refine - 2]);
3975: DMPlexSetRegularRefinement(dm, PETSC_TRUE);
3976: DMSetCoarseDM(dms[0], dms[refine - 1]);
3977: DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);
3978: }
3979: DMSetCoarseDM(dms[refine - 1], coarseDM);
3980: PetscObjectDereference((PetscObject)coarseDM);
3981: /* Free DMs */
3982: for (r = 0; r < refine; ++r) {
3983: DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject);
3984: DMDestroy(&dms[r]);
3985: }
3986: PetscFree(dms);
3987: } else {
3988: for (r = 0; r < refine; ++r) {
3989: DM rdm;
3990: PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
3992: DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject);
3993: DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm);
3994: /* Total hack since we do not pass in a pointer */
3995: DMPlexReplace_Internal(dm, &rdm);
3996: DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject);
3997: if (coordFunc && remap) {
3998: DMPlexRemapGeometry(dm, 0.0, coordFunc);
3999: ((DM_Plex *)dm->data)->coordFunc = coordFunc;
4000: }
4001: }
4002: }
4003: /* Handle DMPlex coarsening */
4004: PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0);
4005: PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0);
4006: if (coarsen && isHierarchy) {
4007: DM *dms;
4009: PetscMalloc1(coarsen, &dms);
4010: DMCoarsenHierarchy(dm, coarsen, dms);
4011: /* Free DMs */
4012: for (r = 0; r < coarsen; ++r) {
4013: DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject);
4014: DMDestroy(&dms[r]);
4015: }
4016: PetscFree(dms);
4017: } else {
4018: for (r = 0; r < coarsen; ++r) {
4019: DM cdm;
4020: PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
4022: DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject);
4023: DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm);
4024: /* Total hack since we do not pass in a pointer */
4025: DMPlexReplace_Internal(dm, &cdm);
4026: DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject);
4027: if (coordFunc) {
4028: DMPlexRemapGeometry(dm, 0.0, coordFunc);
4029: ((DM_Plex *)dm->data)->coordFunc = coordFunc;
4030: }
4031: }
4032: }
4033: /* Handle ghost cells */
4034: PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL);
4035: if (ghostCells) {
4036: DM gdm;
4037: char lname[PETSC_MAX_PATH_LEN];
4039: lname[0] = '\0';
4040: PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg);
4041: DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm);
4042: DMPlexReplace_Internal(dm, &gdm);
4043: }
4044: /* Handle 1D order */
4045: if (reorder != DMPLEX_REORDER_DEFAULT_FALSE && dim == 1) {
4046: DM cdm, rdm;
4047: PetscDS cds;
4048: PetscObject obj;
4049: PetscClassId id = PETSC_OBJECT_CLASSID;
4050: IS perm;
4051: PetscInt Nf;
4052: PetscBool distributed;
4054: DMPlexIsDistributed(dm, &distributed);
4055: DMGetCoordinateDM(dm, &cdm);
4056: DMGetDS(cdm, &cds);
4057: PetscDSGetNumFields(cds, &Nf);
4058: if (Nf) {
4059: PetscDSGetDiscretization(cds, 0, &obj);
4060: PetscObjectGetClassId(obj, &id);
4061: }
4062: if (!distributed && id != PETSCFE_CLASSID) {
4063: DMPlexGetOrdering1D(dm, &perm);
4064: DMPlexPermute(dm, perm, &rdm);
4065: DMPlexReplace_Internal(dm, &rdm);
4066: ISDestroy(&perm);
4067: }
4068: }
4069: /* Handle */
4070: DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject);
4071: PetscOptionsHeadEnd();
4072: return 0;
4073: }
4075: static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
4076: {
4077: DMCreateGlobalVector_Section_Private(dm, vec);
4078: /* VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM); */
4079: VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex);
4080: VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native);
4081: VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex);
4082: VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native);
4083: return 0;
4084: }
4086: static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
4087: {
4088: DMCreateLocalVector_Section_Private(dm, vec);
4089: VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local);
4090: VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local);
4091: return 0;
4092: }
4094: static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4095: {
4096: PetscInt depth, d;
4098: DMPlexGetDepth(dm, &depth);
4099: if (depth == 1) {
4100: DMGetDimension(dm, &d);
4101: if (dim == 0) DMPlexGetDepthStratum(dm, dim, pStart, pEnd);
4102: else if (dim == d) DMPlexGetDepthStratum(dm, 1, pStart, pEnd);
4103: else {
4104: *pStart = 0;
4105: *pEnd = 0;
4106: }
4107: } else {
4108: DMPlexGetDepthStratum(dm, dim, pStart, pEnd);
4109: }
4110: return 0;
4111: }
4113: static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
4114: {
4115: PetscSF sf;
4116: PetscInt niranks, njranks, n;
4117: const PetscMPIInt *iranks, *jranks;
4118: DM_Plex *data = (DM_Plex *)dm->data;
4120: DMGetPointSF(dm, &sf);
4121: if (!data->neighbors) {
4122: PetscSFSetUp(sf);
4123: PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL);
4124: PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL);
4125: PetscMalloc1(njranks + niranks + 1, &data->neighbors);
4126: PetscArraycpy(data->neighbors + 1, jranks, njranks);
4127: PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks);
4128: n = njranks + niranks;
4129: PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1);
4130: /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
4131: PetscMPIIntCast(n, data->neighbors);
4132: }
4133: if (nranks) *nranks = data->neighbors[0];
4134: if (ranks) {
4135: if (data->neighbors[0]) *ranks = data->neighbors + 1;
4136: else *ranks = NULL;
4137: }
4138: return 0;
4139: }
4141: PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
4143: static PetscErrorCode DMInitialize_Plex(DM dm)
4144: {
4145: dm->ops->view = DMView_Plex;
4146: dm->ops->load = DMLoad_Plex;
4147: dm->ops->setfromoptions = DMSetFromOptions_Plex;
4148: dm->ops->clone = DMClone_Plex;
4149: dm->ops->setup = DMSetUp_Plex;
4150: dm->ops->createlocalsection = DMCreateLocalSection_Plex;
4151: dm->ops->createdefaultconstraints = DMCreateDefaultConstraints_Plex;
4152: dm->ops->createglobalvector = DMCreateGlobalVector_Plex;
4153: dm->ops->createlocalvector = DMCreateLocalVector_Plex;
4154: dm->ops->getlocaltoglobalmapping = NULL;
4155: dm->ops->createfieldis = NULL;
4156: dm->ops->createcoordinatedm = DMCreateCoordinateDM_Plex;
4157: dm->ops->createcoordinatefield = DMCreateCoordinateField_Plex;
4158: dm->ops->getcoloring = NULL;
4159: dm->ops->creatematrix = DMCreateMatrix_Plex;
4160: dm->ops->createinterpolation = DMCreateInterpolation_Plex;
4161: dm->ops->createmassmatrix = DMCreateMassMatrix_Plex;
4162: dm->ops->createmassmatrixlumped = DMCreateMassMatrixLumped_Plex;
4163: dm->ops->createinjection = DMCreateInjection_Plex;
4164: dm->ops->refine = DMRefine_Plex;
4165: dm->ops->coarsen = DMCoarsen_Plex;
4166: dm->ops->refinehierarchy = DMRefineHierarchy_Plex;
4167: dm->ops->coarsenhierarchy = DMCoarsenHierarchy_Plex;
4168: dm->ops->extrude = DMExtrude_Plex;
4169: dm->ops->globaltolocalbegin = NULL;
4170: dm->ops->globaltolocalend = NULL;
4171: dm->ops->localtoglobalbegin = NULL;
4172: dm->ops->localtoglobalend = NULL;
4173: dm->ops->destroy = DMDestroy_Plex;
4174: dm->ops->createsubdm = DMCreateSubDM_Plex;
4175: dm->ops->createsuperdm = DMCreateSuperDM_Plex;
4176: dm->ops->getdimpoints = DMGetDimPoints_Plex;
4177: dm->ops->locatepoints = DMLocatePoints_Plex;
4178: dm->ops->projectfunctionlocal = DMProjectFunctionLocal_Plex;
4179: dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
4180: dm->ops->projectfieldlocal = DMProjectFieldLocal_Plex;
4181: dm->ops->projectfieldlabellocal = DMProjectFieldLabelLocal_Plex;
4182: dm->ops->projectbdfieldlabellocal = DMProjectBdFieldLabelLocal_Plex;
4183: dm->ops->computel2diff = DMComputeL2Diff_Plex;
4184: dm->ops->computel2gradientdiff = DMComputeL2GradientDiff_Plex;
4185: dm->ops->computel2fielddiff = DMComputeL2FieldDiff_Plex;
4186: dm->ops->getneighbors = DMGetNeighbors_Plex;
4187: PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex);
4188: PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerviativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex);
4189: PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex);
4190: PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex);
4191: PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex);
4192: PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex);
4193: PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex);
4194: PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex);
4195: PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex);
4196: PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex);
4197: PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex);
4198: PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex);
4199: return 0;
4200: }
4202: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
4203: {
4204: DM_Plex *mesh = (DM_Plex *)dm->data;
4206: mesh->refct++;
4207: (*newdm)->data = mesh;
4208: PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX);
4209: DMInitialize_Plex(*newdm);
4210: return 0;
4211: }
4213: /*MC
4214: DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
4215: In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
4216: specified by a PetscSection object. Ownership in the global representation is determined by
4217: ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
4219: Options Database Keys:
4220: + -dm_refine_pre - Refine mesh before distribution
4221: + -dm_refine_uniform_pre - Choose uniform or generator-based refinement
4222: + -dm_refine_volume_limit_pre - Cell volume limit after pre-refinement using generator
4223: . -dm_distribute - Distribute mesh across processes
4224: . -dm_distribute_overlap - Number of cells to overlap for distribution
4225: . -dm_refine - Refine mesh after distribution
4226: . -dm_plex_hash_location - Use grid hashing for point location
4227: . -dm_plex_hash_box_faces <n,m,p> - The number of divisions in each direction of the grid hash
4228: . -dm_plex_partition_balance - Attempt to evenly divide points on partition boundary between processes
4229: . -dm_plex_remesh_bd - Allow changes to the boundary on remeshing
4230: . -dm_plex_max_projection_height - Maximum mesh point height used to project locally
4231: . -dm_plex_regular_refinement - Use special nested projection algorithm for regular refinement
4232: . -dm_plex_check_all - Perform all shecks below
4233: . -dm_plex_check_symmetry - Check that the adjacency information in the mesh is symmetric
4234: . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
4235: . -dm_plex_check_faces <celltype> - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type
4236: . -dm_plex_check_geometry - Check that cells have positive volume
4237: . -dm_view :mesh.tex:ascii_latex - View the mesh in LaTeX/TikZ
4238: . -dm_plex_view_scale <num> - Scale the TikZ
4239: - -dm_plex_print_fem <num> - View FEM assembly information, such as element vectors and matrices
4241: Level: intermediate
4243: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
4244: M*/
4246: PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
4247: {
4248: DM_Plex *mesh;
4249: PetscInt unit;
4252: PetscNew(&mesh);
4253: dm->data = mesh;
4255: mesh->refct = 1;
4256: PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);
4257: PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);
4258: mesh->refinementUniform = PETSC_TRUE;
4259: mesh->refinementLimit = -1.0;
4260: mesh->distDefault = PETSC_TRUE;
4261: mesh->reorderDefault = DMPLEX_REORDER_DEFAULT_NOTSET;
4262: mesh->distributionName = NULL;
4263: mesh->interpolated = DMPLEX_INTERPOLATED_INVALID;
4264: mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
4266: PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);
4267: mesh->remeshBd = PETSC_FALSE;
4269: for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
4271: mesh->depthState = -1;
4272: mesh->celltypeState = -1;
4273: mesh->printTol = 1.0e-10;
4275: DMInitialize_Plex(dm);
4276: return 0;
4277: }
4279: /*@
4280: DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
4282: Collective
4284: Input Parameter:
4285: . comm - The communicator for the `DMPLEX` object
4287: Output Parameter:
4288: . mesh - The `DMPLEX` object
4290: Level: beginner
4292: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`
4293: @*/
4294: PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
4295: {
4297: DMCreate(comm, mesh);
4298: DMSetType(*mesh, DMPLEX);
4299: return 0;
4300: }
4302: /*@C
4303: DMPlexBuildFromCellListParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
4305: Collective on dm
4307: Input Parameters:
4308: + dm - The `DM`
4309: . numCells - The number of cells owned by this process
4310: . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
4311: . NVertices - The global number of vertices, or `PETSC_DETERMINE`
4312: . numCorners - The number of vertices for each cell
4313: - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4315: Output Parameters:
4316: + vertexSF - (Optional) `PetscSF` describing complete vertex ownership
4317: - verticesAdjSaved - (Optional) vertex adjacency array
4319: Level: advanced
4321: Notes:
4322: Two triangles sharing a face
4323: .vb
4325: 2
4326: / | \
4327: / | \
4328: / | \
4329: 0 0 | 1 3
4330: \ | /
4331: \ | /
4332: \ | /
4333: 1
4334: .ve
4335: would have input
4336: .vb
4337: numCells = 2, numVertices = 4
4338: cells = [0 1 2 1 3 2]
4339: .ve
4340: which would result in the `DMPLEX`
4341: .vb
4343: 4
4344: / | \
4345: / | \
4346: / | \
4347: 2 0 | 1 5
4348: \ | /
4349: \ | /
4350: \ | /
4351: 3
4352: .ve
4354: Vertices are implicitly numbered consecutively 0,...,NVertices.
4355: Each rank owns a chunk of numVertices consecutive vertices.
4356: If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
4357: If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
4358: If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
4360: The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
4362: Fortran Note:
4363: Not currently supported in Fortran.
4365: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
4366: `PetscSF`
4367: @*/
4368: PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
4369: {
4370: PetscSF sfPoint;
4371: PetscLayout layout;
4372: PetscInt numVerticesAdj, *verticesAdj, *cones, c, p;
4375: PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0);
4376: /* Get/check global number of vertices */
4377: {
4378: PetscInt NVerticesInCells, i;
4379: const PetscInt len = numCells * numCorners;
4381: /* NVerticesInCells = max(cells) + 1 */
4382: NVerticesInCells = PETSC_MIN_INT;
4383: for (i = 0; i < len; i++)
4384: if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
4385: ++NVerticesInCells;
4386: MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));
4388: if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
4389: else
4391: }
4392: /* Count locally unique vertices */
4393: {
4394: PetscHSetI vhash;
4395: PetscInt off = 0;
4397: PetscHSetICreate(&vhash);
4398: for (c = 0; c < numCells; ++c) {
4399: for (p = 0; p < numCorners; ++p) PetscHSetIAdd(vhash, cells[c * numCorners + p]);
4400: }
4401: PetscHSetIGetSize(vhash, &numVerticesAdj);
4402: if (!verticesAdjSaved) PetscMalloc1(numVerticesAdj, &verticesAdj);
4403: else verticesAdj = *verticesAdjSaved;
4404: PetscHSetIGetElems(vhash, &off, verticesAdj);
4405: PetscHSetIDestroy(&vhash);
4407: }
4408: PetscSortInt(numVerticesAdj, verticesAdj);
4409: /* Create cones */
4410: DMPlexSetChart(dm, 0, numCells + numVerticesAdj);
4411: for (c = 0; c < numCells; ++c) DMPlexSetConeSize(dm, c, numCorners);
4412: DMSetUp(dm);
4413: DMPlexGetCones(dm, &cones);
4414: for (c = 0; c < numCells; ++c) {
4415: for (p = 0; p < numCorners; ++p) {
4416: const PetscInt gv = cells[c * numCorners + p];
4417: PetscInt lv;
4419: /* Positions within verticesAdj form 0-based local vertex numbering;
4420: we need to shift it by numCells to get correct DAG points (cells go first) */
4421: PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);
4423: cones[c * numCorners + p] = lv + numCells;
4424: }
4425: }
4426: /* Build point sf */
4427: PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout);
4428: PetscLayoutSetSize(layout, NVertices);
4429: PetscLayoutSetLocalSize(layout, numVertices);
4430: PetscLayoutSetBlockSize(layout, 1);
4431: PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint);
4432: PetscLayoutDestroy(&layout);
4433: if (!verticesAdjSaved) PetscFree(verticesAdj);
4434: PetscObjectSetName((PetscObject)sfPoint, "point SF");
4435: if (dm->sf) {
4436: const char *prefix;
4438: PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix);
4439: PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix);
4440: }
4441: DMSetPointSF(dm, sfPoint);
4442: PetscSFDestroy(&sfPoint);
4443: if (vertexSF) PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF");
4444: /* Fill in the rest of the topology structure */
4445: DMPlexSymmetrize(dm);
4446: DMPlexStratify(dm);
4447: PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0);
4448: return 0;
4449: }
4451: /*@C
4452: DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4454: Collective on dm
4456: Input Parameters:
4457: + dm - The `DM`
4458: . spaceDim - The spatial dimension used for coordinates
4459: . sfVert - `PetscSF` describing complete vertex ownership
4460: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4462: Level: advanced
4464: Fortran Note:
4465: Not currently supported in Fortran.
4467: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
4468: @*/
4469: PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
4470: {
4471: PetscSection coordSection;
4472: Vec coordinates;
4473: PetscScalar *coords;
4474: PetscInt numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
4476: PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0);
4477: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
4479: DMSetCoordinateDim(dm, spaceDim);
4480: PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);
4482: DMGetCoordinateSection(dm, &coordSection);
4483: PetscSectionSetNumFields(coordSection, 1);
4484: PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
4485: PetscSectionSetChart(coordSection, vStart, vEnd);
4486: for (v = vStart; v < vEnd; ++v) {
4487: PetscSectionSetDof(coordSection, v, spaceDim);
4488: PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
4489: }
4490: PetscSectionSetUp(coordSection);
4491: PetscSectionGetStorageSize(coordSection, &coordSize);
4492: VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);
4493: VecSetBlockSize(coordinates, spaceDim);
4494: PetscObjectSetName((PetscObject)coordinates, "coordinates");
4495: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
4496: VecSetType(coordinates, VECSTANDARD);
4497: VecGetArray(coordinates, &coords);
4498: {
4499: MPI_Datatype coordtype;
4501: /* Need a temp buffer for coords if we have complex/single */
4502: MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);
4503: MPI_Type_commit(&coordtype);
4504: #if defined(PETSC_USE_COMPLEX)
4505: {
4506: PetscScalar *svertexCoords;
4507: PetscInt i;
4508: PetscMalloc1(numVertices * spaceDim, &svertexCoords);
4509: for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
4510: PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE);
4511: PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE);
4512: PetscFree(svertexCoords);
4513: }
4514: #else
4515: PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE);
4516: PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE);
4517: #endif
4518: MPI_Type_free(&coordtype);
4519: }
4520: VecRestoreArray(coordinates, &coords);
4521: DMSetCoordinatesLocal(dm, coordinates);
4522: VecDestroy(&coordinates);
4523: PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0);
4524: return 0;
4525: }
4527: /*@
4528: DMPlexCreateFromCellListParallelPetsc - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output)
4530: Collective
4532: Input Parameters:
4533: + comm - The communicator
4534: . dim - The topological dimension of the mesh
4535: . numCells - The number of cells owned by this process
4536: . numVertices - The number of vertices owned by this process, or `PETSC_DECIDE`
4537: . NVertices - The global number of vertices, or `PETSC_DECIDE`
4538: . numCorners - The number of vertices for each cell
4539: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
4540: . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4541: . spaceDim - The spatial dimension used for coordinates
4542: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4544: Output Parameters:
4545: + dm - The `DM`
4546: . vertexSF - (Optional) `PetscSF` describing complete vertex ownership
4547: - verticesAdjSaved - (Optional) vertex adjacency array
4549: Level: intermediate
4551: Notes:
4552: This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
4553: `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
4555: See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
4557: See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
4559: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
4560: @*/
4561: PetscErrorCode DMPlexCreateFromCellListParallelPetsc(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, PetscBool interpolate, const PetscInt cells[], PetscInt spaceDim, const PetscReal vertexCoords[], PetscSF *vertexSF, PetscInt **verticesAdj, DM *dm)
4562: {
4563: PetscSF sfVert;
4565: DMCreate(comm, dm);
4566: DMSetType(*dm, DMPLEX);
4569: DMSetDimension(*dm, dim);
4570: DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj);
4571: if (interpolate) {
4572: DM idm;
4574: DMPlexInterpolate(*dm, &idm);
4575: DMDestroy(dm);
4576: *dm = idm;
4577: }
4578: DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords);
4579: if (vertexSF) *vertexSF = sfVert;
4580: else PetscSFDestroy(&sfVert);
4581: return 0;
4582: }
4584: /*@C
4585: DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
4587: Collective on dm
4589: Input Parameters:
4590: + dm - The `DM`
4591: . numCells - The number of cells owned by this process
4592: . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
4593: . numCorners - The number of vertices for each cell
4594: - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4596: Level: advanced
4598: Notes:
4599: Two triangles sharing a face
4600: .vb
4602: 2
4603: / | \
4604: / | \
4605: / | \
4606: 0 0 | 1 3
4607: \ | /
4608: \ | /
4609: \ | /
4610: 1
4611: .ve
4612: would have input
4613: .vb
4614: numCells = 2, numVertices = 4
4615: cells = [0 1 2 1 3 2]
4616: .ve
4617: which would result in the `DMPLEX`
4618: .vb
4620: 4
4621: / | \
4622: / | \
4623: / | \
4624: 2 0 | 1 5
4625: \ | /
4626: \ | /
4627: \ | /
4628: 3
4629: .ve
4631: If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
4633: Not currently supported in Fortran.
4635: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
4636: @*/
4637: PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
4638: {
4639: PetscInt *cones, c, p, dim;
4641: PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0);
4642: DMGetDimension(dm, &dim);
4643: /* Get/check global number of vertices */
4644: {
4645: PetscInt NVerticesInCells, i;
4646: const PetscInt len = numCells * numCorners;
4648: /* NVerticesInCells = max(cells) + 1 */
4649: NVerticesInCells = PETSC_MIN_INT;
4650: for (i = 0; i < len; i++)
4651: if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
4652: ++NVerticesInCells;
4654: if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
4655: else
4657: }
4658: DMPlexSetChart(dm, 0, numCells + numVertices);
4659: for (c = 0; c < numCells; ++c) DMPlexSetConeSize(dm, c, numCorners);
4660: DMSetUp(dm);
4661: DMPlexGetCones(dm, &cones);
4662: for (c = 0; c < numCells; ++c) {
4663: for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
4664: }
4665: DMPlexSymmetrize(dm);
4666: DMPlexStratify(dm);
4667: PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0);
4668: return 0;
4669: }
4671: /*@C
4672: DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4674: Collective on dm
4676: Input Parameters:
4677: + dm - The `DM`
4678: . spaceDim - The spatial dimension used for coordinates
4679: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4681: Level: advanced
4683: Fortran Note:
4684: Not currently supported in Fortran.
4686: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
4687: @*/
4688: PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
4689: {
4690: PetscSection coordSection;
4691: Vec coordinates;
4692: DM cdm;
4693: PetscScalar *coords;
4694: PetscInt v, vStart, vEnd, d;
4696: PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0);
4697: DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
4699: DMSetCoordinateDim(dm, spaceDim);
4700: DMGetCoordinateSection(dm, &coordSection);
4701: PetscSectionSetNumFields(coordSection, 1);
4702: PetscSectionSetFieldComponents(coordSection, 0, spaceDim);
4703: PetscSectionSetChart(coordSection, vStart, vEnd);
4704: for (v = vStart; v < vEnd; ++v) {
4705: PetscSectionSetDof(coordSection, v, spaceDim);
4706: PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);
4707: }
4708: PetscSectionSetUp(coordSection);
4710: DMGetCoordinateDM(dm, &cdm);
4711: DMCreateLocalVector(cdm, &coordinates);
4712: VecSetBlockSize(coordinates, spaceDim);
4713: PetscObjectSetName((PetscObject)coordinates, "coordinates");
4714: VecGetArrayWrite(coordinates, &coords);
4715: for (v = 0; v < vEnd - vStart; ++v) {
4716: for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
4717: }
4718: VecRestoreArrayWrite(coordinates, &coords);
4719: DMSetCoordinatesLocal(dm, coordinates);
4720: VecDestroy(&coordinates);
4721: PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0);
4722: return 0;
4723: }
4725: /*@
4726: DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
4728: Collective
4730: Input Parameters:
4731: + comm - The communicator
4732: . dim - The topological dimension of the mesh
4733: . numCells - The number of cells, only on process 0
4734: . numVertices - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
4735: . numCorners - The number of vertices for each cell, only on process 0
4736: . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
4737: . cells - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
4738: . spaceDim - The spatial dimension used for coordinates
4739: - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
4741: Output Parameter:
4742: . dm - The `DM`, which only has points on process 0
4744: Level: intermediate
4746: Notes:
4747: This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
4748: `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
4750: See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
4751: See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
4752: See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
4754: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
4755: @*/
4756: PetscErrorCode DMPlexCreateFromCellListPetsc(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, PetscBool interpolate, const PetscInt cells[], PetscInt spaceDim, const PetscReal vertexCoords[], DM *dm)
4757: {
4758: PetscMPIInt rank;
4761: MPI_Comm_rank(comm, &rank);
4762: DMCreate(comm, dm);
4763: DMSetType(*dm, DMPLEX);
4764: DMSetDimension(*dm, dim);
4765: if (rank == 0) DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells);
4766: else DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL);
4767: if (interpolate) {
4768: DM idm;
4770: DMPlexInterpolate(*dm, &idm);
4771: DMDestroy(dm);
4772: *dm = idm;
4773: }
4774: if (rank == 0) DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords);
4775: else DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL);
4776: return 0;
4777: }
4779: /*@
4780: DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a DM
4782: Input Parameters:
4783: + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
4784: . depth - The depth of the DAG
4785: . numPoints - Array of size depth + 1 containing the number of points at each depth
4786: . coneSize - The cone size of each point
4787: . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
4788: . coneOrientations - The orientation of each cone point
4789: - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()
4791: Output Parameter:
4792: . dm - The DM
4794: Note:
4795: Two triangles sharing a face would have input
4796: .vb
4797: depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
4798: cones = [2 3 4 3 5 4], coneOrientations = [0 0 0 0 0 0]
4799: vertexCoords = [-1.0 0.0 0.0 -1.0 0.0 1.0 1.0 0.0]
4800: .ve
4801: which would result in the DMPlex
4802: .vb
4803: 4
4804: / | \
4805: / | \
4806: / | \
4807: 2 0 | 1 5
4808: \ | /
4809: \ | /
4810: \ | /
4811: 3
4812: .ve
4813: Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
4815: Level: advanced
4817: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
4818: @*/
4819: PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
4820: {
4821: Vec coordinates;
4822: PetscSection coordSection;
4823: PetscScalar *coords;
4824: PetscInt coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
4826: DMGetDimension(dm, &dim);
4827: DMGetCoordinateDim(dm, &dimEmbed);
4829: for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
4830: DMPlexSetChart(dm, pStart, pEnd);
4831: for (p = pStart; p < pEnd; ++p) {
4832: DMPlexSetConeSize(dm, p, coneSize[p - pStart]);
4833: if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
4834: }
4836: DMSetUp(dm); /* Allocate space for cones */
4837: for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
4838: DMPlexSetCone(dm, p, &cones[off]);
4839: DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);
4840: }
4841: DMPlexSymmetrize(dm);
4842: DMPlexStratify(dm);
4843: /* Build coordinates */
4844: DMGetCoordinateSection(dm, &coordSection);
4845: PetscSectionSetNumFields(coordSection, 1);
4846: PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);
4847: PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]);
4848: for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
4849: PetscSectionSetDof(coordSection, v, dimEmbed);
4850: PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);
4851: }
4852: PetscSectionSetUp(coordSection);
4853: PetscSectionGetStorageSize(coordSection, &coordSize);
4854: VecCreate(PETSC_COMM_SELF, &coordinates);
4855: PetscObjectSetName((PetscObject)coordinates, "coordinates");
4856: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
4857: VecSetBlockSize(coordinates, dimEmbed);
4858: VecSetType(coordinates, VECSTANDARD);
4859: if (vertexCoords) {
4860: VecGetArray(coordinates, &coords);
4861: for (v = 0; v < numPoints[0]; ++v) {
4862: PetscInt off;
4864: PetscSectionGetOffset(coordSection, v + firstVertex, &off);
4865: for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
4866: }
4867: }
4868: VecRestoreArray(coordinates, &coords);
4869: DMSetCoordinatesLocal(dm, coordinates);
4870: VecDestroy(&coordinates);
4871: return 0;
4872: }
4874: /*@C
4875: DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
4877: Collective
4879: + comm - The MPI communicator
4880: . filename - Name of the .dat file
4881: - interpolate - Create faces and edges in the mesh
4883: Output Parameter:
4884: . dm - The `DM` object representing the mesh
4886: Level: beginner
4888: Note:
4889: The format is the simplest possible:
4890: .vb
4891: Ne
4892: v0 v1 ... vk
4893: Nv
4894: x y z marker
4895: .ve
4897: Developer Note:
4898: Should use a `PetscViewer` not a filename
4900: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateMedFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
4901: @*/
4902: PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
4903: {
4904: DMLabel marker;
4905: PetscViewer viewer;
4906: Vec coordinates;
4907: PetscSection coordSection;
4908: PetscScalar *coords;
4909: char line[PETSC_MAX_PATH_LEN];
4910: PetscInt dim = 3, cdim = 3, coordSize, v, c, d;
4911: PetscMPIInt rank;
4912: int snum, Nv, Nc, Ncn, Nl;
4914: MPI_Comm_rank(comm, &rank);
4915: PetscViewerCreate(comm, &viewer);
4916: PetscViewerSetType(viewer, PETSCVIEWERASCII);
4917: PetscViewerFileSetMode(viewer, FILE_MODE_READ);
4918: PetscViewerFileSetName(viewer, filename);
4919: if (rank == 0) {
4920: PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING);
4921: snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
4923: } else {
4924: Nc = Nv = Ncn = Nl = 0;
4925: }
4926: DMCreate(comm, dm);
4927: DMSetType(*dm, DMPLEX);
4928: DMPlexSetChart(*dm, 0, Nc + Nv);
4929: DMSetDimension(*dm, dim);
4930: DMSetCoordinateDim(*dm, cdim);
4931: /* Read topology */
4932: if (rank == 0) {
4933: char format[PETSC_MAX_PATH_LEN];
4934: PetscInt cone[8];
4935: int vbuf[8], v;
4937: for (c = 0; c < Ncn; ++c) {
4938: format[c * 3 + 0] = '%';
4939: format[c * 3 + 1] = 'd';
4940: format[c * 3 + 2] = ' ';
4941: }
4942: format[Ncn * 3 - 1] = '\0';
4943: for (c = 0; c < Nc; ++c) DMPlexSetConeSize(*dm, c, Ncn);
4944: DMSetUp(*dm);
4945: for (c = 0; c < Nc; ++c) {
4946: PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING);
4947: switch (Ncn) {
4948: case 2:
4949: snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
4950: break;
4951: case 3:
4952: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
4953: break;
4954: case 4:
4955: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
4956: break;
4957: case 6:
4958: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
4959: break;
4960: case 8:
4961: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
4962: break;
4963: default:
4964: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
4965: }
4967: for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
4968: /* Hexahedra are inverted */
4969: if (Ncn == 8) {
4970: PetscInt tmp = cone[1];
4971: cone[1] = cone[3];
4972: cone[3] = tmp;
4973: }
4974: DMPlexSetCone(*dm, c, cone);
4975: }
4976: }
4977: DMPlexSymmetrize(*dm);
4978: DMPlexStratify(*dm);
4979: /* Read coordinates */
4980: DMGetCoordinateSection(*dm, &coordSection);
4981: PetscSectionSetNumFields(coordSection, 1);
4982: PetscSectionSetFieldComponents(coordSection, 0, cdim);
4983: PetscSectionSetChart(coordSection, Nc, Nc + Nv);
4984: for (v = Nc; v < Nc + Nv; ++v) {
4985: PetscSectionSetDof(coordSection, v, cdim);
4986: PetscSectionSetFieldDof(coordSection, v, 0, cdim);
4987: }
4988: PetscSectionSetUp(coordSection);
4989: PetscSectionGetStorageSize(coordSection, &coordSize);
4990: VecCreate(PETSC_COMM_SELF, &coordinates);
4991: PetscObjectSetName((PetscObject)coordinates, "coordinates");
4992: VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);
4993: VecSetBlockSize(coordinates, cdim);
4994: VecSetType(coordinates, VECSTANDARD);
4995: VecGetArray(coordinates, &coords);
4996: if (rank == 0) {
4997: char format[PETSC_MAX_PATH_LEN];
4998: double x[3];
4999: int l, val[3];
5001: if (Nl) {
5002: for (l = 0; l < Nl; ++l) {
5003: format[l * 3 + 0] = '%';
5004: format[l * 3 + 1] = 'd';
5005: format[l * 3 + 2] = ' ';
5006: }
5007: format[Nl * 3 - 1] = '\0';
5008: DMCreateLabel(*dm, "marker");
5009: DMGetLabel(*dm, "marker", &marker);
5010: }
5011: for (v = 0; v < Nv; ++v) {
5012: PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING);
5013: snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
5015: switch (Nl) {
5016: case 0:
5017: snum = 0;
5018: break;
5019: case 1:
5020: snum = sscanf(line, format, &val[0]);
5021: break;
5022: case 2:
5023: snum = sscanf(line, format, &val[0], &val[1]);
5024: break;
5025: case 3:
5026: snum = sscanf(line, format, &val[0], &val[1], &val[2]);
5027: break;
5028: default:
5029: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
5030: }
5032: for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
5033: for (l = 0; l < Nl; ++l) DMLabelSetValue(marker, v + Nc, val[l]);
5034: }
5035: }
5036: VecRestoreArray(coordinates, &coords);
5037: DMSetCoordinatesLocal(*dm, coordinates);
5038: VecDestroy(&coordinates);
5039: PetscViewerDestroy(&viewer);
5040: if (interpolate) {
5041: DM idm;
5042: DMLabel bdlabel;
5044: DMPlexInterpolate(*dm, &idm);
5045: DMDestroy(dm);
5046: *dm = idm;
5048: if (!Nl) {
5049: DMCreateLabel(*dm, "marker");
5050: DMGetLabel(*dm, "marker", &bdlabel);
5051: DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel);
5052: DMPlexLabelComplete(*dm, bdlabel);
5053: }
5054: }
5055: return 0;
5056: }
5058: /*@C
5059: DMPlexCreateFromFile - This takes a filename and produces a `DM`
5061: Collective
5063: Input Parameters:
5064: + comm - The communicator
5065: . filename - A file name
5066: . plexname - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
5067: - interpolate - Flag to create intermediate mesh pieces (edges, faces)
5069: Output Parameter:
5070: . dm - The `DM`
5072: Options Database Key:
5073: . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
5075: Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
5076: $ -dm_plex_create_viewer_hdf5_collective
5078: Level: beginner
5080: Notes:
5081: Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
5082: meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
5083: before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
5084: The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
5085: calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
5087: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
5088: @*/
5089: PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
5090: {
5091: const char extGmsh[] = ".msh";
5092: const char extGmsh2[] = ".msh2";
5093: const char extGmsh4[] = ".msh4";
5094: const char extCGNS[] = ".cgns";
5095: const char extExodus[] = ".exo";
5096: const char extExodus_e[] = ".e";
5097: const char extGenesis[] = ".gen";
5098: const char extFluent[] = ".cas";
5099: const char extHDF5[] = ".h5";
5100: const char extMed[] = ".med";
5101: const char extPLY[] = ".ply";
5102: const char extEGADSLite[] = ".egadslite";
5103: const char extEGADS[] = ".egads";
5104: const char extIGES[] = ".igs";
5105: const char extSTEP[] = ".stp";
5106: const char extCV[] = ".dat";
5107: size_t len;
5108: PetscBool isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV;
5109: PetscMPIInt rank;
5114: DMInitializePackage();
5115: PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0);
5116: MPI_Comm_rank(comm, &rank);
5117: PetscStrlen(filename, &len);
5120: #define CheckExtension(extension__, is_extension__) \
5121: do { \
5122: PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
5123: /* don't count the null-terminator at the end */ \
5124: const size_t ext_len = sizeof(extension__) - 1; \
5125: if (len < ext_len) { \
5126: is_extension__ = PETSC_FALSE; \
5127: } else { \
5128: PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__); \
5129: } \
5130: } while (0)
5132: CheckExtension(extGmsh, isGmsh);
5133: CheckExtension(extGmsh2, isGmsh2);
5134: CheckExtension(extGmsh4, isGmsh4);
5135: CheckExtension(extCGNS, isCGNS);
5136: CheckExtension(extExodus, isExodus);
5137: if (!isExodus) CheckExtension(extExodus_e, isExodus);
5138: CheckExtension(extGenesis, isGenesis);
5139: CheckExtension(extFluent, isFluent);
5140: CheckExtension(extHDF5, isHDF5);
5141: CheckExtension(extMed, isMed);
5142: CheckExtension(extPLY, isPLY);
5143: CheckExtension(extEGADSLite, isEGADSLite);
5144: CheckExtension(extEGADS, isEGADS);
5145: CheckExtension(extIGES, isIGES);
5146: CheckExtension(extSTEP, isSTEP);
5147: CheckExtension(extCV, isCV);
5149: #undef CheckExtension
5151: if (isGmsh || isGmsh2 || isGmsh4) {
5152: DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);
5153: } else if (isCGNS) {
5154: DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);
5155: } else if (isExodus || isGenesis) {
5156: DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);
5157: } else if (isFluent) {
5158: DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);
5159: } else if (isHDF5) {
5160: PetscBool load_hdf5_xdmf = PETSC_FALSE;
5161: PetscViewer viewer;
5163: /* PETSC_VIEWER_HDF5_XDMF is used if the filename ends with .xdmf.h5, or if -dm_plex_create_from_hdf5_xdmf option is present */
5164: PetscStrncmp(&filename[PetscMax(0, len - 8)], ".xdmf", 5, &load_hdf5_xdmf);
5165: PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL);
5166: PetscViewerCreate(comm, &viewer);
5167: PetscViewerSetType(viewer, PETSCVIEWERHDF5);
5168: PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_");
5169: PetscViewerSetFromOptions(viewer);
5170: PetscViewerFileSetMode(viewer, FILE_MODE_READ);
5171: PetscViewerFileSetName(viewer, filename);
5173: DMCreate(comm, dm);
5174: PetscObjectSetName((PetscObject)(*dm), plexname);
5175: DMSetType(*dm, DMPLEX);
5176: if (load_hdf5_xdmf) PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF);
5177: DMLoad(*dm, viewer);
5178: if (load_hdf5_xdmf) PetscViewerPopFormat(viewer);
5179: PetscViewerDestroy(&viewer);
5181: if (interpolate) {
5182: DM idm;
5184: DMPlexInterpolate(*dm, &idm);
5185: DMDestroy(dm);
5186: *dm = idm;
5187: }
5188: } else if (isMed) {
5189: DMPlexCreateMedFromFile(comm, filename, interpolate, dm);
5190: } else if (isPLY) {
5191: DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);
5192: } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
5193: if (isEGADSLite) DMPlexCreateEGADSLiteFromFile(comm, filename, dm);
5194: else DMPlexCreateEGADSFromFile(comm, filename, dm);
5195: if (!interpolate) {
5196: DM udm;
5198: DMPlexUninterpolate(*dm, &udm);
5199: DMDestroy(dm);
5200: *dm = udm;
5201: }
5202: } else if (isCV) {
5203: DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm);
5204: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
5205: PetscStrlen(plexname, &len);
5206: if (len) PetscObjectSetName((PetscObject)(*dm), plexname);
5207: PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0);
5208: return 0;
5209: }