Actual source code: trigenerate.c

  1: #include <petsc/private/dmpleximpl.h>

  3: #if !defined(ANSI_DECLARATORS)
  4:   #define ANSI_DECLARATORS
  5: #endif
  6: #include <triangle.h>

  8: static PetscErrorCode InitInput_Triangle(struct triangulateio *inputCtx)
  9: {
 10:   PetscFunctionBegin;
 11:   inputCtx->numberofpoints             = 0;
 12:   inputCtx->numberofpointattributes    = 0;
 13:   inputCtx->pointlist                  = NULL;
 14:   inputCtx->pointattributelist         = NULL;
 15:   inputCtx->pointmarkerlist            = NULL;
 16:   inputCtx->numberofsegments           = 0;
 17:   inputCtx->segmentlist                = NULL;
 18:   inputCtx->segmentmarkerlist          = NULL;
 19:   inputCtx->numberoftriangleattributes = 0;
 20:   inputCtx->trianglelist               = NULL;
 21:   inputCtx->numberofholes              = 0;
 22:   inputCtx->holelist                   = NULL;
 23:   inputCtx->numberofregions            = 0;
 24:   inputCtx->regionlist                 = NULL;
 25:   PetscFunctionReturn(PETSC_SUCCESS);
 26: }

 28: static PetscErrorCode InitOutput_Triangle(struct triangulateio *outputCtx)
 29: {
 30:   PetscFunctionBegin;
 31:   outputCtx->numberofpoints        = 0;
 32:   outputCtx->pointlist             = NULL;
 33:   outputCtx->pointattributelist    = NULL;
 34:   outputCtx->pointmarkerlist       = NULL;
 35:   outputCtx->numberoftriangles     = 0;
 36:   outputCtx->trianglelist          = NULL;
 37:   outputCtx->triangleattributelist = NULL;
 38:   outputCtx->neighborlist          = NULL;
 39:   outputCtx->segmentlist           = NULL;
 40:   outputCtx->segmentmarkerlist     = NULL;
 41:   outputCtx->numberofedges         = 0;
 42:   outputCtx->edgelist              = NULL;
 43:   outputCtx->edgemarkerlist        = NULL;
 44:   PetscFunctionReturn(PETSC_SUCCESS);
 45: }

 47: static PetscErrorCode FiniOutput_Triangle(struct triangulateio *outputCtx)
 48: {
 49:   PetscFunctionBegin;
 50:   free(outputCtx->pointlist);
 51:   free(outputCtx->pointmarkerlist);
 52:   free(outputCtx->segmentlist);
 53:   free(outputCtx->segmentmarkerlist);
 54:   free(outputCtx->edgelist);
 55:   free(outputCtx->edgemarkerlist);
 56:   free(outputCtx->trianglelist);
 57:   free(outputCtx->neighborlist);
 58:   PetscFunctionReturn(PETSC_SUCCESS);
 59: }

 61: PETSC_EXTERN PetscErrorCode DMPlexGenerate_Triangle(DM boundary, PetscBool interpolate, DM *dm)
 62: {
 63:   MPI_Comm             comm;
 64:   DM_Plex             *mesh             = (DM_Plex *)boundary->data;
 65:   PetscInt             dim              = 2;
 66:   const PetscBool      createConvexHull = PETSC_FALSE;
 67:   const PetscBool      constrained      = PETSC_FALSE;
 68:   const char          *labelName        = "marker";
 69:   const char          *labelName2       = "Face Sets";
 70:   struct triangulateio in;
 71:   struct triangulateio out;
 72:   DMLabel              label, label2;
 73:   PetscInt             vStart, vEnd, v, eStart, eEnd, e;
 74:   PetscMPIInt          rank;
 75:   PetscBool            flg;
 76:   char                 opts[64];

 78:   PetscFunctionBegin;
 79:   PetscCall(PetscObjectGetComm((PetscObject)boundary, &comm));
 80:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
 81:   PetscCall(InitInput_Triangle(&in));
 82:   PetscCall(InitOutput_Triangle(&out));
 83:   PetscCall(DMPlexGetDepthStratum(boundary, 0, &vStart, &vEnd));
 84:   PetscCall(DMGetLabel(boundary, labelName, &label));
 85:   PetscCall(DMGetLabel(boundary, labelName2, &label2));
 86:   PetscCall(PetscOptionsGetString(((PetscObject)boundary)->options, ((PetscObject)boundary)->prefix, "-dm_plex_generate_triangle_opts", opts, sizeof(opts), &flg));
 87:   if (flg) PetscCall(DMPlexTriangleSetOptions(boundary, opts));

 89:   PetscCall(PetscCIntCast(vEnd - vStart, &in.numberofpoints));
 90:   if (in.numberofpoints > 0) {
 91:     PetscSection coordSection;
 92:     Vec          coordinates;
 93:     PetscScalar *array;

 95:     PetscCall(PetscMalloc1(in.numberofpoints * dim, &in.pointlist));
 96:     PetscCall(PetscMalloc1(in.numberofpoints, &in.pointmarkerlist));
 97:     PetscCall(DMGetCoordinatesLocal(boundary, &coordinates));
 98:     PetscCall(DMGetCoordinateSection(boundary, &coordSection));
 99:     PetscCall(VecGetArray(coordinates, &array));
100:     for (v = vStart; v < vEnd; ++v) {
101:       const PetscInt idx = v - vStart;
102:       PetscInt       val, off, d;

104:       PetscCall(PetscSectionGetOffset(coordSection, v, &off));
105:       for (d = 0; d < dim; ++d) in.pointlist[idx * dim + d] = PetscRealPart(array[off + d]);
106:       if (label) {
107:         PetscCall(DMLabelGetValue(label, v, &val));
108:         PetscCall(PetscCIntCast(val, &in.pointmarkerlist[idx]));
109:       }
110:     }
111:     PetscCall(VecRestoreArray(coordinates, &array));
112:   }
113:   PetscCall(DMPlexGetHeightStratum(boundary, 0, &eStart, &eEnd));
114:   PetscCall(PetscCIntCast(eEnd - eStart, &in.numberofsegments));
115:   if (in.numberofsegments > 0) {
116:     PetscCall(PetscMalloc1(in.numberofsegments * 2, &in.segmentlist));
117:     PetscCall(PetscMalloc1(in.numberofsegments, &in.segmentmarkerlist));
118:     for (e = eStart; e < eEnd; ++e) {
119:       const PetscInt  idx = e - eStart;
120:       const PetscInt *cone;
121:       PetscInt        val;

123:       PetscCall(DMPlexGetCone(boundary, e, &cone));

125:       PetscCall(PetscCIntCast(cone[0] - vStart, &in.segmentlist[idx * 2 + 0]));
126:       PetscCall(PetscCIntCast(cone[1] - vStart, &in.segmentlist[idx * 2 + 1]));

128:       if (label) {
129:         PetscCall(DMLabelGetValue(label, e, &val));
130:         PetscCall(PetscCIntCast(val, &in.segmentmarkerlist[idx]));
131:       }
132:     }
133:   }
134:   if (rank == 0) {
135:     char args[32];

137:     /* Take away 'Q' for verbose output */
138:     if (mesh->triangleAngBound > 0.) {
139:       PetscCall(PetscSNPrintf(args, sizeof(args), "pq%.0fezQ", (double)mesh->triangleAngBound));
140:     } else {
141:       PetscCall(PetscStrncpy(args, "pqezQ", sizeof(args)));
142:     }
143:     if (createConvexHull) PetscCall(PetscStrlcat(args, "c", sizeof(args)));
144:     if (constrained) PetscCall(PetscStrncpy(args, "zepDQ", sizeof(args)));
145:     if (mesh->triangleOpts) {
146:       triangulate(mesh->triangleOpts, &in, &out, NULL);
147:     } else {
148:       triangulate(args, &in, &out, NULL);
149:     }
150:   }
151:   PetscCall(PetscFree(in.pointlist));
152:   PetscCall(PetscFree(in.pointmarkerlist));
153:   PetscCall(PetscFree(in.segmentlist));
154:   PetscCall(PetscFree(in.segmentmarkerlist));
155:   PetscCall(PetscFree(in.holelist));

157:   {
158:     DMLabel        glabel      = NULL;
159:     DMLabel        glabel2     = NULL;
160:     const PetscInt numCorners  = 3;
161:     const PetscInt numCells    = out.numberoftriangles;
162:     const PetscInt numVertices = out.numberofpoints;
163:     PetscInt      *cells;
164:     PetscReal     *meshCoords;

166:     if (sizeof(PetscReal) == sizeof(out.pointlist[0])) {
167:       meshCoords = (PetscReal *)out.pointlist;
168:     } else {
169:       PetscInt i;

171:       PetscCall(PetscMalloc1(dim * numVertices, &meshCoords));
172:       for (i = 0; i < dim * numVertices; i++) meshCoords[i] = (PetscReal)out.pointlist[i];
173:     }
174:     if (sizeof(PetscInt) == sizeof(out.trianglelist[0])) {
175:       cells = (PetscInt *)out.trianglelist;
176:     } else {
177:       PetscCall(PetscMalloc1(numCells * numCorners, &cells));
178:       for (PetscInt i = 0; i < numCells * numCorners; i++) cells[i] = (PetscInt)out.trianglelist[i];
179:     }
180:     PetscCall(DMPlexCreateFromCellListPetsc(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dm));
181:     if (sizeof(PetscReal) != sizeof(out.pointlist[0])) PetscCall(PetscFree(meshCoords));
182:     if (sizeof(PetscInt) != sizeof(out.trianglelist[0])) PetscCall(PetscFree(cells));
183:     if (label) {
184:       PetscCall(DMCreateLabel(*dm, labelName));
185:       PetscCall(DMGetLabel(*dm, labelName, &glabel));
186:     }
187:     if (label2) {
188:       PetscCall(DMCreateLabel(*dm, labelName2));
189:       PetscCall(DMGetLabel(*dm, labelName2, &glabel2));
190:     }
191:     /* Set labels */
192:     for (v = 0; v < numVertices; ++v) {
193:       if (out.pointmarkerlist[v] && glabel) PetscCall(DMLabelSetValue(glabel, v + numCells, out.pointmarkerlist[v]));
194:     }
195:     if (interpolate) {
196:       for (e = 0; e < out.numberofedges; e++) {
197:         if (out.edgemarkerlist[e]) {
198:           const PetscInt  vertices[2] = {out.edgelist[e * 2 + 0] + numCells, out.edgelist[e * 2 + 1] + numCells};
199:           const PetscInt *edges;
200:           PetscInt        numEdges;

202:           PetscCall(DMPlexGetJoin(*dm, 2, vertices, &numEdges, &edges));
203:           PetscCheck(numEdges == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %" PetscInt_FMT, numEdges);
204:           if (glabel) PetscCall(DMLabelSetValue(glabel, edges[0], out.edgemarkerlist[e]));
205:           if (glabel2) PetscCall(DMLabelSetValue(glabel2, edges[0], out.edgemarkerlist[e]));
206:           PetscCall(DMPlexRestoreJoin(*dm, 2, vertices, &numEdges, &edges));
207:         }
208:       }
209:     }
210:     PetscCall(DMPlexSetRefinementUniform(*dm, PETSC_FALSE));
211:   }
212: #if 0 /* Do not currently support holes */
213:   PetscCall(DMPlexCopyHoles(*dm, boundary));
214: #endif
215:   PetscCall(FiniOutput_Triangle(&out));
216:   PetscFunctionReturn(PETSC_SUCCESS);
217: }

219: PETSC_EXTERN PetscErrorCode DMPlexRefine_Triangle(DM dm, PetscReal *inmaxVolumes, DM *dmRefined)
220: {
221:   MPI_Comm             comm;
222:   PetscInt             dim       = 2;
223:   const char          *labelName = "marker";
224:   struct triangulateio in;
225:   struct triangulateio out;
226:   DMLabel              label;
227:   PetscInt             vStart, vEnd, v, gcStart, cStart, cEnd, c, depth, depthGlobal;
228:   PetscMPIInt          rank;
229:   double              *maxVolumes;

231:   PetscFunctionBegin;
232:   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
233:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
234:   PetscCall(InitInput_Triangle(&in));
235:   PetscCall(InitOutput_Triangle(&out));
236:   PetscCall(DMPlexGetDepth(dm, &depth));
237:   PetscCallMPI(MPIU_Allreduce(&depth, &depthGlobal, 1, MPIU_INT, MPI_MAX, comm));
238:   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
239:   PetscCall(DMGetLabel(dm, labelName, &label));

241:   PetscCall(PetscCIntCast(vEnd - vStart, &in.numberofpoints));
242:   if (in.numberofpoints > 0) {
243:     PetscSection coordSection;
244:     Vec          coordinates;
245:     PetscScalar *array;

247:     PetscCall(PetscMalloc1(in.numberofpoints * dim, &in.pointlist));
248:     PetscCall(PetscMalloc1(in.numberofpoints, &in.pointmarkerlist));
249:     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
250:     PetscCall(DMGetCoordinateSection(dm, &coordSection));
251:     PetscCall(VecGetArray(coordinates, &array));
252:     for (v = vStart; v < vEnd; ++v) {
253:       const PetscInt idx = v - vStart;
254:       PetscInt       off, d, val;

256:       PetscCall(PetscSectionGetOffset(coordSection, v, &off));
257:       for (d = 0; d < dim; ++d) in.pointlist[idx * dim + d] = PetscRealPart(array[off + d]);
258:       if (label) {
259:         PetscCall(DMLabelGetValue(label, v, &val));
260:         PetscCall(PetscCIntCast(val, &in.pointmarkerlist[idx]));
261:       }
262:     }
263:     PetscCall(VecRestoreArray(coordinates, &array));
264:   }
265:   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
266:   PetscCall(DMPlexGetCellTypeStratum(dm, DM_POLYTOPE_FV_GHOST, &gcStart, NULL));
267:   if (gcStart >= 0) cEnd = gcStart;

269:   in.numberofcorners = 3;
270:   PetscCall(PetscCIntCast(cEnd - cStart, &in.numberoftriangles));

272: #if !defined(PETSC_USE_REAL_DOUBLE)
273:   PetscCall(PetscMalloc1(cEnd - cStart, &maxVolumes));
274:   for (c = 0; c < cEnd - cStart; ++c) maxVolumes[c] = (double)inmaxVolumes[c];
275: #else
276:   maxVolumes = inmaxVolumes;
277: #endif

279:   in.trianglearealist = (double *)maxVolumes;
280:   if (in.numberoftriangles > 0) {
281:     PetscCall(PetscMalloc1(in.numberoftriangles * in.numberofcorners, &in.trianglelist));
282:     for (c = cStart; c < cEnd; ++c) {
283:       const PetscInt idx     = c - cStart;
284:       PetscInt      *closure = NULL;
285:       PetscInt       closureSize;

287:       PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
288:       PetscCheck(!(closureSize != 4) || !(closureSize != 7), comm, PETSC_ERR_ARG_WRONG, "Mesh has cell which is not a triangle, %" PetscInt_FMT " vertices in closure", closureSize);
289:       for (v = 0; v < 3; ++v) PetscCall(PetscCIntCast(closure[(v + closureSize - 3) * 2] - vStart, &in.trianglelist[idx * in.numberofcorners + v]));
290:       PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
291:     }
292:   }
293:   /* TODO: Segment markers are missing on input */
294:   if (rank == 0) {
295:     char args[32];

297:     /* Take away 'Q' for verbose output */
298:     PetscCall(PetscStrncpy(args, "pqezQra", sizeof(args)));
299:     triangulate(args, &in, &out, NULL);
300:   }
301:   PetscCall(PetscFree(in.pointlist));
302:   PetscCall(PetscFree(in.pointmarkerlist));
303:   PetscCall(PetscFree(in.segmentlist));
304:   PetscCall(PetscFree(in.segmentmarkerlist));
305:   PetscCall(PetscFree(in.trianglelist));

307:   {
308:     DMLabel        rlabel      = NULL;
309:     const PetscInt numCorners  = 3;
310:     const PetscInt numCells    = out.numberoftriangles;
311:     const PetscInt numVertices = out.numberofpoints;
312:     PetscInt      *cells;
313:     PetscReal     *meshCoords;
314:     PetscBool      interpolate = depthGlobal > 1 ? PETSC_TRUE : PETSC_FALSE;

316:     if (sizeof(PetscReal) == sizeof(out.pointlist[0])) {
317:       meshCoords = (PetscReal *)out.pointlist;
318:     } else {
319:       PetscCall(PetscMalloc1(dim * numVertices, &meshCoords));
320:       for (PetscInt i = 0; i < dim * numVertices; i++) meshCoords[i] = (PetscReal)out.pointlist[i];
321:     }
322:     if (sizeof(PetscInt) == sizeof(out.trianglelist[0])) {
323:       cells = (PetscInt *)out.trianglelist;
324:     } else {
325:       PetscCall(PetscMalloc1(numCells * numCorners, &cells));
326:       for (PetscInt i = 0; i < numCells * numCorners; i++) cells[i] = (PetscInt)out.trianglelist[i];
327:     }

329:     PetscCall(DMPlexCreateFromCellListPetsc(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dmRefined));
330:     if (label) {
331:       PetscCall(DMCreateLabel(*dmRefined, labelName));
332:       PetscCall(DMGetLabel(*dmRefined, labelName, &rlabel));
333:     }
334:     if (sizeof(PetscReal) != sizeof(out.pointlist[0])) PetscCall(PetscFree(meshCoords));
335:     if (sizeof(PetscInt) != sizeof(out.trianglelist[0])) PetscCall(PetscFree(cells));
336:     /* Set labels */
337:     for (v = 0; v < numVertices; ++v) {
338:       if (out.pointmarkerlist[v] && rlabel) PetscCall(DMLabelSetValue(rlabel, v + numCells, out.pointmarkerlist[v]));
339:     }
340:     if (interpolate) {
341:       for (PetscInt e = 0; e < out.numberofedges; e++) {
342:         if (out.edgemarkerlist[e]) {
343:           const PetscInt  vertices[2] = {out.edgelist[e * 2 + 0] + numCells, out.edgelist[e * 2 + 1] + numCells};
344:           const PetscInt *edges;
345:           PetscInt        numEdges;

347:           PetscCall(DMPlexGetJoin(*dmRefined, 2, vertices, &numEdges, &edges));
348:           PetscCheck(numEdges == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %" PetscInt_FMT, numEdges);
349:           if (rlabel) PetscCall(DMLabelSetValue(rlabel, edges[0], out.edgemarkerlist[e]));
350:           PetscCall(DMPlexRestoreJoin(*dmRefined, 2, vertices, &numEdges, &edges));
351:         }
352:       }
353:     }
354:     PetscCall(DMPlexSetRefinementUniform(*dmRefined, PETSC_FALSE));
355:   }
356: #if 0 /* Do not currently support holes */
357:   PetscCall(DMPlexCopyHoles(*dm, boundary));
358: #endif
359:   PetscCall(FiniOutput_Triangle(&out));
360: #if !defined(PETSC_USE_REAL_DOUBLE)
361:   PetscCall(PetscFree(maxVolumes));
362: #endif
363:   PetscFunctionReturn(PETSC_SUCCESS);
364: }