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