Actual source code: plexegads.c

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

  4: #ifdef PETSC_HAVE_EGADS
  5:   #include <egads.h>
  6: #endif

  8: /* We need to understand how to natively parse STEP files. There seems to be only one open source implementation of
  9:    the STEP parser contained in the OpenCASCADE package. It is enough to make a strong man weep:

 11:      https://github.com/tpaviot/oce/tree/master/src/STEPControl

 13:    The STEP, and inner EXPRESS, formats are ISO standards, so they are documented

 15:      https://stackoverflow.com/questions/26774037/documentation-or-specification-for-step-and-stp-files
 16:      http://stepmod.sourceforge.net/express_model_spec/

 18:    but again it seems that there has been a deliberate effort at obfuscation, probably to raise the bar for entrants.
 19: */

 21: #ifdef PETSC_HAVE_EGADS
 22: PETSC_INTERN PetscErrorCode DMPlexSnapToGeomModel_EGADS_Internal(DM, PetscInt, ego, PetscInt, PetscInt, PetscInt, const PetscScalar[], PetscScalar[]);
 23: PETSC_INTERN PetscErrorCode DMPlexSnapToGeomModel_EGADSLite_Internal(DM, PetscInt, ego, PetscInt, PetscInt, PetscInt, const PetscScalar[], PetscScalar[]);

 25: PetscErrorCode DMPlexSnapToGeomModel_EGADS_Internal(DM dm, PetscInt p, ego model, PetscInt bodyID, PetscInt faceID, PetscInt edgeID, const PetscScalar mcoords[], PetscScalar gcoords[])
 26: {
 27:   DM   cdm;
 28:   ego *bodies;
 29:   ego  geom, body, obj;
 30:   /* result has to hold derivatives, along with the value */
 31:   double       params[3], result[18], paramsV[16 * 3], resultV[16 * 3], range[4];
 32:   int          Nb, oclass, mtype, *senses, peri;
 33:   Vec          coordinatesLocal;
 34:   PetscScalar *coords = NULL;
 35:   PetscInt     Nv, v, Np = 0, pm;
 36:   PetscInt     dE, d;

 39:   DMGetCoordinateDM(dm, &cdm);
 40:   DMGetCoordinateDim(dm, &dE);
 41:   DMGetCoordinatesLocal(dm, &coordinatesLocal);
 42:   EG_getTopology(model, &geom, &oclass, &mtype, NULL, &Nb, &bodies, &senses);
 44:   body = bodies[bodyID];

 46:   if (edgeID >= 0) {
 47:     EG_objectBodyTopo(body, EDGE, edgeID, &obj);
 48:     Np = 1;
 49:   } else if (faceID >= 0) {
 50:     EG_objectBodyTopo(body, FACE, faceID, &obj);
 51:     Np = 2;
 52:   } else {
 53:     for (d = 0; d < dE; ++d) gcoords[d] = mcoords[d];
 54:     return 0;
 55:   }

 57:   /* Calculate parameters (t or u,v) for vertices */
 58:   DMPlexVecGetClosure(cdm, NULL, coordinatesLocal, p, &Nv, &coords);
 59:   Nv /= dE;
 60:   if (Nv == 1) {
 61:     DMPlexVecRestoreClosure(cdm, NULL, coordinatesLocal, p, &Nv, &coords);
 62:     for (d = 0; d < dE; ++d) gcoords[d] = mcoords[d];
 63:     return 0;
 64:   }

 67:   /* Correct EGADSlite 2pi bug when calculating nearest point on Periodic Surfaces */
 68:   EG_getRange(obj, range, &peri);
 69:   for (v = 0; v < Nv; ++v) {
 70:     EG_invEvaluate(obj, &coords[v * dE], &paramsV[v * 3], &resultV[v * 3]);
 71:   #if 1
 72:     if (peri > 0) {
 73:       if (paramsV[v * 3 + 0] + 1.e-4 < range[0]) {
 74:         paramsV[v * 3 + 0] += 2. * PETSC_PI;
 75:       } else if (paramsV[v * 3 + 0] - 1.e-4 > range[1]) {
 76:         paramsV[v * 3 + 0] -= 2. * PETSC_PI;
 77:       }
 78:     }
 79:     if (peri > 1) {
 80:       if (paramsV[v * 3 + 1] + 1.e-4 < range[2]) {
 81:         paramsV[v * 3 + 1] += 2. * PETSC_PI;
 82:       } else if (paramsV[v * 3 + 1] - 1.e-4 > range[3]) {
 83:         paramsV[v * 3 + 1] -= 2. * PETSC_PI;
 84:       }
 85:     }
 86:   #endif
 87:   }
 88:   DMPlexVecRestoreClosure(cdm, NULL, coordinatesLocal, p, &Nv, &coords);
 89:   /* Calculate parameters (t or u,v) for new vertex at edge midpoint */
 90:   for (pm = 0; pm < Np; ++pm) {
 91:     params[pm] = 0.;
 92:     for (v = 0; v < Nv; ++v) params[pm] += paramsV[v * 3 + pm];
 93:     params[pm] /= Nv;
 94:   }
 97:   /* Put coordinates for new vertex in result[] */
 98:   EG_evaluate(obj, params, result);
 99:   for (d = 0; d < dE; ++d) gcoords[d] = result[d];
100:   return 0;
101: }
102: #endif

104: /*@
105:   DMPlexSnapToGeomModel - Given a coordinate point 'mcoords' on the mesh point 'p', return the closest coordinate point 'gcoords' on the geometry model associated with that point.

107:   Not collective

109:   Input Parameters:
110: + dm      - The `DMPLEX` object
111: . p       - The mesh point
112: . dE      - The coordinate dimension
113: - mcoords - A coordinate point lying on the mesh point

115:   Output Parameter:
116: . gcoords - The closest coordinate point on the geometry model associated with 'p' to the given point

118:   Level: intermediate

120:   Note:
121:   Returns the original coordinates if no geometry model is found. Right now the only supported geometry model is EGADS. The coordinate dimension may be different from the coordinate dimension of the dm, for example if the transformation is extrusion.

123: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMRefine()`, `DMPlexCreate()`, `DMPlexSetRefinementUniform()`
124: @*/
125: PetscErrorCode DMPlexSnapToGeomModel(DM dm, PetscInt p, PetscInt dE, const PetscScalar mcoords[], PetscScalar gcoords[])
126: {
127:   PetscInt d;

130: #ifdef PETSC_HAVE_EGADS
131:   {
132:     DM_Plex       *plex = (DM_Plex *)dm->data;
133:     DMLabel        bodyLabel, faceLabel, edgeLabel;
134:     PetscInt       bodyID, faceID, edgeID;
135:     PetscContainer modelObj;
136:     ego            model;
137:     PetscBool      islite = PETSC_FALSE;

139:     DMGetLabel(dm, "EGADS Body ID", &bodyLabel);
140:     DMGetLabel(dm, "EGADS Face ID", &faceLabel);
141:     DMGetLabel(dm, "EGADS Edge ID", &edgeLabel);
142:     if (!bodyLabel || !faceLabel || !edgeLabel || plex->ignoreModel) {
143:       for (d = 0; d < dE; ++d) gcoords[d] = mcoords[d];
144:       return 0;
145:     }
146:     PetscObjectQuery((PetscObject)dm, "EGADS Model", (PetscObject *)&modelObj);
147:     if (!modelObj) {
148:       PetscObjectQuery((PetscObject)dm, "EGADSLite Model", (PetscObject *)&modelObj);
149:       islite = PETSC_TRUE;
150:     }
151:     if (!modelObj) {
152:       for (d = 0; d < dE; ++d) gcoords[d] = mcoords[d];
153:       return 0;
154:     }
155:     PetscContainerGetPointer(modelObj, (void **)&model);
156:     DMLabelGetValue(bodyLabel, p, &bodyID);
157:     DMLabelGetValue(faceLabel, p, &faceID);
158:     DMLabelGetValue(edgeLabel, p, &edgeID);
159:     /* Allows for "Connective" Plex Edges present in models with multiple non-touching Entities */
160:     if (bodyID < 0) {
161:       for (d = 0; d < dE; ++d) gcoords[d] = mcoords[d];
162:       return 0;
163:     }
164:     if (islite) DMPlexSnapToGeomModel_EGADSLite_Internal(dm, p, model, bodyID, faceID, edgeID, mcoords, gcoords);
165:     else DMPlexSnapToGeomModel_EGADS_Internal(dm, p, model, bodyID, faceID, edgeID, mcoords, gcoords);
166:   }
167: #else
168:   for (d = 0; d < dE; ++d) gcoords[d] = mcoords[d];
169: #endif
170:   return 0;
171: }

173: #if defined(PETSC_HAVE_EGADS)
174: static PetscErrorCode DMPlexEGADSPrintModel_Internal(ego model)
175: {
176:   ego geom, *bodies, *objs, *nobjs, *mobjs, *lobjs;
177:   int oclass, mtype, *senses;
178:   int Nb, b;

181:   /* test bodyTopo functions */
182:   EG_getTopology(model, &geom, &oclass, &mtype, NULL, &Nb, &bodies, &senses);
183:   PetscPrintf(PETSC_COMM_SELF, " Number of BODIES (nbodies): %d \n", Nb);

185:   for (b = 0; b < Nb; ++b) {
186:     ego body = bodies[b];
187:     int id, Nsh, Nf, Nl, l, Ne, e, Nv, v;

189:     /* Output Basic Model Topology */
190:     EG_getBodyTopos(body, NULL, SHELL, &Nsh, &objs);
191:     PetscPrintf(PETSC_COMM_SELF, "   Number of SHELLS: %d \n", Nsh);
192:     EG_free(objs);

194:     EG_getBodyTopos(body, NULL, FACE, &Nf, &objs);
195:     PetscPrintf(PETSC_COMM_SELF, "   Number of FACES: %d \n", Nf);
196:     EG_free(objs);

198:     EG_getBodyTopos(body, NULL, LOOP, &Nl, &lobjs);
199:     PetscPrintf(PETSC_COMM_SELF, "   Number of LOOPS: %d \n", Nl);

201:     EG_getBodyTopos(body, NULL, EDGE, &Ne, &objs);
202:     PetscPrintf(PETSC_COMM_SELF, "   Number of EDGES: %d \n", Ne);
203:     EG_free(objs);

205:     EG_getBodyTopos(body, NULL, NODE, &Nv, &objs);
206:     PetscPrintf(PETSC_COMM_SELF, "   Number of NODES: %d \n", Nv);
207:     EG_free(objs);

209:     for (l = 0; l < Nl; ++l) {
210:       ego loop = lobjs[l];

212:       id = EG_indexBodyTopo(body, loop);
213:       PetscPrintf(PETSC_COMM_SELF, "          LOOP ID: %d\n", id);

215:       /* Get EDGE info which associated with the current LOOP */
216:       EG_getTopology(loop, &geom, &oclass, &mtype, NULL, &Ne, &objs, &senses);

218:       for (e = 0; e < Ne; ++e) {
219:         ego    edge      = objs[e];
220:         double range[4]  = {0., 0., 0., 0.};
221:         double point[3]  = {0., 0., 0.};
222:         double params[3] = {0., 0., 0.};
223:         double result[18];
224:         int    peri;

226:         id = EG_indexBodyTopo(body, edge);
227:         PetscPrintf(PETSC_COMM_SELF, "            EDGE ID: %d (%d)\n", id, e);

229:         EG_getRange(edge, range, &peri);
230:         PetscPrintf(PETSC_COMM_SELF, "  Range = %lf, %lf, %lf, %lf \n", range[0], range[1], range[2], range[3]);

232:         /* Get NODE info which associated with the current EDGE */
233:         EG_getTopology(edge, &geom, &oclass, &mtype, NULL, &Nv, &nobjs, &senses);
234:         if (mtype == DEGENERATE) {
235:           PetscPrintf(PETSC_COMM_SELF, "  EDGE %d is DEGENERATE \n", id);
236:         } else {
237:           params[0] = range[0];
238:           EG_evaluate(edge, params, result);
239:           PetscPrintf(PETSC_COMM_SELF, "   between (%lf, %lf, %lf)", result[0], result[1], result[2]);
240:           params[0] = range[1];
241:           EG_evaluate(edge, params, result);
242:           PetscPrintf(PETSC_COMM_SELF, " and (%lf, %lf, %lf)\n", result[0], result[1], result[2]);
243:         }

245:         for (v = 0; v < Nv; ++v) {
246:           ego    vertex = nobjs[v];
247:           double limits[4];
248:           int    dummy;

250:           EG_getTopology(vertex, &geom, &oclass, &mtype, limits, &dummy, &mobjs, &senses);
251:           id = EG_indexBodyTopo(body, vertex);
252:           PetscPrintf(PETSC_COMM_SELF, "              NODE ID: %d \n", id);
253:           PetscPrintf(PETSC_COMM_SELF, "                 (x, y, z) = (%lf, %lf, %lf) \n", limits[0], limits[1], limits[2]);

255:           point[0] = point[0] + limits[0];
256:           point[1] = point[1] + limits[1];
257:           point[2] = point[2] + limits[2];
258:         }
259:       }
260:     }
261:     EG_free(lobjs);
262:   }
263:   return 0;
264: }

266: static PetscErrorCode DMPlexEGADSDestroy_Private(void *context)
267: {
268:   if (context) EG_close((ego)context);
269:   return 0;
270: }

272: static PetscErrorCode DMPlexCreateEGADS_Internal(MPI_Comm comm, ego context, ego model, DM *newdm)
273: {
274:   DMLabel  bodyLabel, faceLabel, edgeLabel, vertexLabel;
275:   PetscInt cStart, cEnd, c;
276:   /* EGADSLite variables */
277:   ego geom, *bodies, *objs, *nobjs, *mobjs, *lobjs;
278:   int oclass, mtype, nbodies, *senses;
279:   int b;
280:   /* PETSc variables */
281:   DM          dm;
282:   PetscHMapI  edgeMap = NULL;
283:   PetscInt    dim = -1, cdim = -1, numCorners = 0, maxCorners = 0, numVertices = 0, newVertices = 0, numEdges = 0, numCells = 0, newCells = 0, numQuads = 0, cOff = 0, fOff = 0;
284:   PetscInt   *cells = NULL, *cone = NULL;
285:   PetscReal  *coords = NULL;
286:   PetscMPIInt rank;

288:   MPI_Comm_rank(comm, &rank);
289:   if (rank == 0) {
290:     const PetscInt debug = 0;

292:     /* ---------------------------------------------------------------------------------------------------
293:     Generate Petsc Plex
294:       Get all Nodes in model, record coordinates in a correctly formatted array
295:       Cycle through bodies, cycle through loops, recorde NODE IDs in a correctly formatted array
296:       We need to uniformly refine the initial geometry to guarantee a valid mesh
297:     */

299:     /* Calculate cell and vertex sizes */
300:     EG_getTopology(model, &geom, &oclass, &mtype, NULL, &nbodies, &bodies, &senses);
301:     PetscHMapICreate(&edgeMap);
302:     numEdges = 0;
303:     for (b = 0; b < nbodies; ++b) {
304:       ego body = bodies[b];
305:       int id, Nl, l, Nv, v;

307:       EG_getBodyTopos(body, NULL, LOOP, &Nl, &lobjs);
308:       for (l = 0; l < Nl; ++l) {
309:         ego loop = lobjs[l];
310:         int Ner  = 0, Ne, e, Nc;

312:         EG_getTopology(loop, &geom, &oclass, &mtype, NULL, &Ne, &objs, &senses);
313:         for (e = 0; e < Ne; ++e) {
314:           ego           edge = objs[e];
315:           int           Nv, id;
316:           PetscHashIter iter;
317:           PetscBool     found;

319:           EG_getTopology(edge, &geom, &oclass, &mtype, NULL, &Nv, &nobjs, &senses);
320:           if (mtype == DEGENERATE) continue;
321:           id = EG_indexBodyTopo(body, edge);
322:           PetscHMapIFind(edgeMap, id - 1, &iter, &found);
323:           if (!found) PetscHMapISet(edgeMap, id - 1, numEdges++);
324:           ++Ner;
325:         }
326:         if (Ner == 2) {
327:           Nc = 2;
328:         } else if (Ner == 3) {
329:           Nc = 4;
330:         } else if (Ner == 4) {
331:           Nc = 8;
332:           ++numQuads;
333:         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot support loop with %d edges", Ner);
334:         numCells += Nc;
335:         newCells += Nc - 1;
336:         maxCorners = PetscMax(Ner * 2 + 1, maxCorners);
337:       }
338:       EG_getBodyTopos(body, NULL, NODE, &Nv, &nobjs);
339:       for (v = 0; v < Nv; ++v) {
340:         ego vertex = nobjs[v];

342:         id = EG_indexBodyTopo(body, vertex);
343:         /* TODO: Instead of assuming contiguous ids, we could use a hash table */
344:         numVertices = PetscMax(id, numVertices);
345:       }
346:       EG_free(lobjs);
347:       EG_free(nobjs);
348:     }
349:     PetscHMapIGetSize(edgeMap, &numEdges);
350:     newVertices = numEdges + numQuads;
351:     numVertices += newVertices;

353:     dim        = 2; /* Assume 3D Models :: Need to update to handle 2D Models in the future */
354:     cdim       = 3; /* Assume 3D Models :: Need to update to handle 2D Models in the future */
355:     numCorners = 3; /* Split cells into triangles */
356:     PetscMalloc3(numVertices * cdim, &coords, numCells * numCorners, &cells, maxCorners, &cone);

358:     /* Get vertex coordinates */
359:     for (b = 0; b < nbodies; ++b) {
360:       ego body = bodies[b];
361:       int id, Nv, v;

363:       EG_getBodyTopos(body, NULL, NODE, &Nv, &nobjs);
364:       for (v = 0; v < Nv; ++v) {
365:         ego    vertex = nobjs[v];
366:         double limits[4];
367:         int    dummy;

369:         EG_getTopology(vertex, &geom, &oclass, &mtype, limits, &dummy, &mobjs, &senses);
370:         id                          = EG_indexBodyTopo(body, vertex);
371:         coords[(id - 1) * cdim + 0] = limits[0];
372:         coords[(id - 1) * cdim + 1] = limits[1];
373:         coords[(id - 1) * cdim + 2] = limits[2];
374:       }
375:       EG_free(nobjs);
376:     }
377:     PetscHMapIClear(edgeMap);
378:     fOff     = numVertices - newVertices + numEdges;
379:     numEdges = 0;
380:     numQuads = 0;
381:     for (b = 0; b < nbodies; ++b) {
382:       ego body = bodies[b];
383:       int Nl, l;

385:       EG_getBodyTopos(body, NULL, LOOP, &Nl, &lobjs);
386:       for (l = 0; l < Nl; ++l) {
387:         ego loop = lobjs[l];
388:         int lid, Ner = 0, Ne, e;

390:         lid = EG_indexBodyTopo(body, loop);
391:         EG_getTopology(loop, &geom, &oclass, &mtype, NULL, &Ne, &objs, &senses);
392:         for (e = 0; e < Ne; ++e) {
393:           ego           edge = objs[e];
394:           int           eid, Nv;
395:           PetscHashIter iter;
396:           PetscBool     found;

398:           EG_getTopology(edge, &geom, &oclass, &mtype, NULL, &Nv, &nobjs, &senses);
399:           if (mtype == DEGENERATE) continue;
400:           ++Ner;
401:           eid = EG_indexBodyTopo(body, edge);
402:           PetscHMapIFind(edgeMap, eid - 1, &iter, &found);
403:           if (!found) {
404:             PetscInt v = numVertices - newVertices + numEdges;
405:             double   range[4], params[3] = {0., 0., 0.}, result[18];
406:             int      periodic[2];

408:             PetscHMapISet(edgeMap, eid - 1, numEdges++);
409:             EG_getRange(edge, range, periodic);
410:             params[0] = 0.5 * (range[0] + range[1]);
411:             EG_evaluate(edge, params, result);
412:             coords[v * cdim + 0] = result[0];
413:             coords[v * cdim + 1] = result[1];
414:             coords[v * cdim + 2] = result[2];
415:           }
416:         }
417:         if (Ner == 4) {
418:           PetscInt v = fOff + numQuads++;
419:           ego     *fobjs, face;
420:           double   range[4], params[3] = {0., 0., 0.}, result[18];
421:           int      Nf, fid, periodic[2];

423:           EG_getBodyTopos(body, loop, FACE, &Nf, &fobjs);
424:           face = fobjs[0];
425:           fid  = EG_indexBodyTopo(body, face);
427:           EG_getRange(face, range, periodic);
428:           params[0] = 0.5 * (range[0] + range[1]);
429:           params[1] = 0.5 * (range[2] + range[3]);
430:           EG_evaluate(face, params, result);
431:           coords[v * cdim + 0] = result[0];
432:           coords[v * cdim + 1] = result[1];
433:           coords[v * cdim + 2] = result[2];
434:         }
435:       }
436:     }

439:     /* Get cell vertices by traversing loops */
440:     numQuads = 0;
441:     cOff     = 0;
442:     for (b = 0; b < nbodies; ++b) {
443:       ego body = bodies[b];
444:       int id, Nl, l;

446:       EG_getBodyTopos(body, NULL, LOOP, &Nl, &lobjs);
447:       for (l = 0; l < Nl; ++l) {
448:         ego loop = lobjs[l];
449:         int lid, Ner = 0, Ne, e, nc = 0, c, Nt, t;

451:         lid = EG_indexBodyTopo(body, loop);
452:         EG_getTopology(loop, &geom, &oclass, &mtype, NULL, &Ne, &objs, &senses);

454:         for (e = 0; e < Ne; ++e) {
455:           ego edge = objs[e];
456:           int points[3];
457:           int eid, Nv, v, tmp;

459:           eid = EG_indexBodyTopo(body, edge);
460:           EG_getTopology(edge, &geom, &oclass, &mtype, NULL, &Nv, &nobjs, &senses);
461:           if (mtype == DEGENERATE) continue;
462:           else ++Ner;

465:           for (v = 0; v < Nv; ++v) {
466:             ego vertex = nobjs[v];

468:             id            = EG_indexBodyTopo(body, vertex);
469:             points[v * 2] = id - 1;
470:           }
471:           {
472:             PetscInt edgeNum;

474:             PetscHMapIGet(edgeMap, eid - 1, &edgeNum);
475:             points[1] = numVertices - newVertices + edgeNum;
476:           }
477:           /* EGADS loops are not oriented, but seem to be in order, so we must piece them together */
478:           if (!nc) {
479:             for (v = 0; v < Nv + 1; ++v) cone[nc++] = points[v];
480:           } else {
481:             if (cone[nc - 1] == points[0]) {
482:               cone[nc++] = points[1];
483:               if (cone[0] != points[2]) cone[nc++] = points[2];
484:             } else if (cone[nc - 1] == points[2]) {
485:               cone[nc++] = points[1];
486:               if (cone[0] != points[0]) cone[nc++] = points[0];
487:             } else if (cone[nc - 3] == points[0]) {
488:               tmp          = cone[nc - 3];
489:               cone[nc - 3] = cone[nc - 1];
490:               cone[nc - 1] = tmp;
491:               cone[nc++]   = points[1];
492:               if (cone[0] != points[2]) cone[nc++] = points[2];
493:             } else if (cone[nc - 3] == points[2]) {
494:               tmp          = cone[nc - 3];
495:               cone[nc - 3] = cone[nc - 1];
496:               cone[nc - 1] = tmp;
497:               cone[nc++]   = points[1];
498:               if (cone[0] != points[0]) cone[nc++] = points[0];
499:             } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Edge %d does not match its predecessor", eid);
500:           }
501:         }
503:         if (Ner == 4) cone[nc++] = numVertices - newVertices + numEdges + numQuads++;
505:         /* Triangulate the loop */
506:         switch (Ner) {
507:         case 2: /* Bi-Segment -> 2 triangles */
508:           Nt                           = 2;
509:           cells[cOff * numCorners + 0] = cone[0];
510:           cells[cOff * numCorners + 1] = cone[1];
511:           cells[cOff * numCorners + 2] = cone[2];
512:           ++cOff;
513:           cells[cOff * numCorners + 0] = cone[0];
514:           cells[cOff * numCorners + 1] = cone[2];
515:           cells[cOff * numCorners + 2] = cone[3];
516:           ++cOff;
517:           break;
518:         case 3: /* Triangle   -> 4 triangles */
519:           Nt                           = 4;
520:           cells[cOff * numCorners + 0] = cone[0];
521:           cells[cOff * numCorners + 1] = cone[1];
522:           cells[cOff * numCorners + 2] = cone[5];
523:           ++cOff;
524:           cells[cOff * numCorners + 0] = cone[1];
525:           cells[cOff * numCorners + 1] = cone[2];
526:           cells[cOff * numCorners + 2] = cone[3];
527:           ++cOff;
528:           cells[cOff * numCorners + 0] = cone[5];
529:           cells[cOff * numCorners + 1] = cone[3];
530:           cells[cOff * numCorners + 2] = cone[4];
531:           ++cOff;
532:           cells[cOff * numCorners + 0] = cone[1];
533:           cells[cOff * numCorners + 1] = cone[3];
534:           cells[cOff * numCorners + 2] = cone[5];
535:           ++cOff;
536:           break;
537:         case 4: /* Quad       -> 8 triangles */
538:           Nt                           = 8;
539:           cells[cOff * numCorners + 0] = cone[0];
540:           cells[cOff * numCorners + 1] = cone[1];
541:           cells[cOff * numCorners + 2] = cone[7];
542:           ++cOff;
543:           cells[cOff * numCorners + 0] = cone[1];
544:           cells[cOff * numCorners + 1] = cone[2];
545:           cells[cOff * numCorners + 2] = cone[3];
546:           ++cOff;
547:           cells[cOff * numCorners + 0] = cone[3];
548:           cells[cOff * numCorners + 1] = cone[4];
549:           cells[cOff * numCorners + 2] = cone[5];
550:           ++cOff;
551:           cells[cOff * numCorners + 0] = cone[5];
552:           cells[cOff * numCorners + 1] = cone[6];
553:           cells[cOff * numCorners + 2] = cone[7];
554:           ++cOff;
555:           cells[cOff * numCorners + 0] = cone[8];
556:           cells[cOff * numCorners + 1] = cone[1];
557:           cells[cOff * numCorners + 2] = cone[3];
558:           ++cOff;
559:           cells[cOff * numCorners + 0] = cone[8];
560:           cells[cOff * numCorners + 1] = cone[3];
561:           cells[cOff * numCorners + 2] = cone[5];
562:           ++cOff;
563:           cells[cOff * numCorners + 0] = cone[8];
564:           cells[cOff * numCorners + 1] = cone[5];
565:           cells[cOff * numCorners + 2] = cone[7];
566:           ++cOff;
567:           cells[cOff * numCorners + 0] = cone[8];
568:           cells[cOff * numCorners + 1] = cone[7];
569:           cells[cOff * numCorners + 2] = cone[1];
570:           ++cOff;
571:           break;
572:         default:
573:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Loop %d has %d edges, which we do not support", lid, Ner);
574:         }
575:         if (debug) {
576:           for (t = 0; t < Nt; ++t) {
577:             PetscPrintf(PETSC_COMM_SELF, "  LOOP Corner NODEs Triangle %" PetscInt_FMT " (", t);
578:             for (c = 0; c < numCorners; ++c) {
579:               if (c > 0) PetscPrintf(PETSC_COMM_SELF, ", ");
580:               PetscPrintf(PETSC_COMM_SELF, "%" PetscInt_FMT, cells[(cOff - Nt + t) * numCorners + c]);
581:             }
582:             PetscPrintf(PETSC_COMM_SELF, ")\n");
583:           }
584:         }
585:       }
586:       EG_free(lobjs);
587:     }
588:   }
590:   DMPlexCreateFromCellListPetsc(PETSC_COMM_WORLD, dim, numCells, numVertices, numCorners, PETSC_TRUE, cells, cdim, coords, &dm);
591:   PetscFree3(coords, cells, cone);
592:   PetscInfo(dm, " Total Number of Unique Cells    = %" PetscInt_FMT " (%" PetscInt_FMT ")\n", numCells, newCells);
593:   PetscInfo(dm, " Total Number of Unique Vertices = %" PetscInt_FMT " (%" PetscInt_FMT ")\n", numVertices, newVertices);
594:   /* Embed EGADS model in DM */
595:   {
596:     PetscContainer modelObj, contextObj;

598:     PetscContainerCreate(PETSC_COMM_SELF, &modelObj);
599:     PetscContainerSetPointer(modelObj, model);
600:     PetscObjectCompose((PetscObject)dm, "EGADS Model", (PetscObject)modelObj);
601:     PetscContainerDestroy(&modelObj);

603:     PetscContainerCreate(PETSC_COMM_SELF, &contextObj);
604:     PetscContainerSetPointer(contextObj, context);
605:     PetscContainerSetUserDestroy(contextObj, DMPlexEGADSDestroy_Private);
606:     PetscObjectCompose((PetscObject)dm, "EGADS Context", (PetscObject)contextObj);
607:     PetscContainerDestroy(&contextObj);
608:   }
609:   /* Label points */
610:   DMCreateLabel(dm, "EGADS Body ID");
611:   DMGetLabel(dm, "EGADS Body ID", &bodyLabel);
612:   DMCreateLabel(dm, "EGADS Face ID");
613:   DMGetLabel(dm, "EGADS Face ID", &faceLabel);
614:   DMCreateLabel(dm, "EGADS Edge ID");
615:   DMGetLabel(dm, "EGADS Edge ID", &edgeLabel);
616:   DMCreateLabel(dm, "EGADS Vertex ID");
617:   DMGetLabel(dm, "EGADS Vertex ID", &vertexLabel);
618:   cOff = 0;
619:   for (b = 0; b < nbodies; ++b) {
620:     ego body = bodies[b];
621:     int id, Nl, l;

623:     EG_getBodyTopos(body, NULL, LOOP, &Nl, &lobjs);
624:     for (l = 0; l < Nl; ++l) {
625:       ego  loop = lobjs[l];
626:       ego *fobjs;
627:       int  lid, Nf, fid, Ner = 0, Ne, e, Nt = 0, t;

629:       lid = EG_indexBodyTopo(body, loop);
630:       EG_getBodyTopos(body, loop, FACE, &Nf, &fobjs);
632:       fid = EG_indexBodyTopo(body, fobjs[0]);
633:       EG_free(fobjs);
634:       EG_getTopology(loop, &geom, &oclass, &mtype, NULL, &Ne, &objs, &senses);
635:       for (e = 0; e < Ne; ++e) {
636:         ego             edge = objs[e];
637:         int             eid, Nv, v;
638:         PetscInt        points[3], support[2], numEdges, edgeNum;
639:         const PetscInt *edges;

641:         eid = EG_indexBodyTopo(body, edge);
642:         EG_getTopology(edge, &geom, &oclass, &mtype, NULL, &Nv, &nobjs, &senses);
643:         if (mtype == DEGENERATE) continue;
644:         else ++Ner;
645:         for (v = 0; v < Nv; ++v) {
646:           ego vertex = nobjs[v];

648:           id = EG_indexBodyTopo(body, vertex);
649:           DMLabelSetValue(edgeLabel, numCells + id - 1, eid);
650:           points[v * 2] = numCells + id - 1;
651:         }
652:         PetscHMapIGet(edgeMap, eid - 1, &edgeNum);
653:         points[1] = numCells + numVertices - newVertices + edgeNum;

655:         DMLabelSetValue(edgeLabel, points[1], eid);
656:         support[0] = points[0];
657:         support[1] = points[1];
658:         DMPlexGetJoin(dm, 2, support, &numEdges, &edges);
660:         DMLabelSetValue(edgeLabel, edges[0], eid);
661:         DMPlexRestoreJoin(dm, 2, support, &numEdges, &edges);
662:         support[0] = points[1];
663:         support[1] = points[2];
664:         DMPlexGetJoin(dm, 2, support, &numEdges, &edges);
666:         DMLabelSetValue(edgeLabel, edges[0], eid);
667:         DMPlexRestoreJoin(dm, 2, support, &numEdges, &edges);
668:       }
669:       switch (Ner) {
670:       case 2:
671:         Nt = 2;
672:         break;
673:       case 3:
674:         Nt = 4;
675:         break;
676:       case 4:
677:         Nt = 8;
678:         break;
679:       default:
680:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Loop with %d edges is unsupported", Ner);
681:       }
682:       for (t = 0; t < Nt; ++t) {
683:         DMLabelSetValue(bodyLabel, cOff + t, b);
684:         DMLabelSetValue(faceLabel, cOff + t, fid);
685:       }
686:       cOff += Nt;
687:     }
688:     EG_free(lobjs);
689:   }
690:   PetscHMapIDestroy(&edgeMap);
691:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
692:   for (c = cStart; c < cEnd; ++c) {
693:     PetscInt *closure = NULL;
694:     PetscInt  clSize, cl, bval, fval;

696:     DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
697:     DMLabelGetValue(bodyLabel, c, &bval);
698:     DMLabelGetValue(faceLabel, c, &fval);
699:     for (cl = 0; cl < clSize * 2; cl += 2) {
700:       DMLabelSetValue(bodyLabel, closure[cl], bval);
701:       DMLabelSetValue(faceLabel, closure[cl], fval);
702:     }
703:     DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);
704:   }
705:   *newdm = dm;
706:   return 0;
707: }

709: static PetscErrorCode DMPlexCreateEGADS(MPI_Comm comm, ego context, ego model, DM *newdm)
710: {
711:   DMLabel bodyLabel, faceLabel, edgeLabel, vertexLabel;
712:   // EGADS/EGADSLite variables
713:   ego geom, *bodies, *mobjs, *fobjs, *lobjs, *eobjs, *nobjs;
714:   ego topRef, prev, next;
715:   int oclass, mtype, nbodies, *senses, *lSenses, *eSenses;
716:   int b;
717:   // PETSc variables
718:   DM              dm;
719:   PetscHMapI      edgeMap = NULL, bodyIndexMap = NULL, bodyVertexMap = NULL, bodyEdgeMap = NULL, bodyFaceMap = NULL, bodyEdgeGlobalMap = NULL;
720:   PetscInt        dim = -1, cdim = -1, numCorners = 0, numVertices = 0, numEdges = 0, numFaces = 0, numCells = 0, edgeCntr = 0;
721:   PetscInt        cellCntr = 0, numPoints = 0;
722:   PetscInt       *cells  = NULL;
723:   const PetscInt *cone   = NULL;
724:   PetscReal      *coords = NULL;
725:   PetscMPIInt     rank;

728:   MPI_Comm_rank(comm, &rank);
729:   if (rank == 0) {
730:     // ---------------------------------------------------------------------------------------------------
731:     // Generate Petsc Plex
732:     //  Get all Nodes in model, record coordinates in a correctly formatted array
733:     //  Cycle through bodies, cycle through loops, recorde NODE IDs in a correctly formatted array
734:     //  We need to uniformly refine the initial geometry to guarantee a valid mesh

736:     // Calculate cell and vertex sizes
737:     EG_getTopology(model, &geom, &oclass, &mtype, NULL, &nbodies, &bodies, &senses);

739:     PetscHMapICreate(&edgeMap);
740:     PetscHMapICreate(&bodyIndexMap);
741:     PetscHMapICreate(&bodyVertexMap);
742:     PetscHMapICreate(&bodyEdgeMap);
743:     PetscHMapICreate(&bodyEdgeGlobalMap);
744:     PetscHMapICreate(&bodyFaceMap);

746:     for (b = 0; b < nbodies; ++b) {
747:       ego           body = bodies[b];
748:       int           Nf, Ne, Nv;
749:       PetscHashIter BIiter, BViter, BEiter, BEGiter, BFiter, EMiter;
750:       PetscBool     BIfound, BVfound, BEfound, BEGfound, BFfound, EMfound;

752:       PetscHMapIFind(bodyIndexMap, b, &BIiter, &BIfound);
753:       PetscHMapIFind(bodyVertexMap, b, &BViter, &BVfound);
754:       PetscHMapIFind(bodyEdgeMap, b, &BEiter, &BEfound);
755:       PetscHMapIFind(bodyEdgeGlobalMap, b, &BEGiter, &BEGfound);
756:       PetscHMapIFind(bodyFaceMap, b, &BFiter, &BFfound);

758:       if (!BIfound) PetscHMapISet(bodyIndexMap, b, numFaces + numEdges + numVertices);
759:       if (!BVfound) PetscHMapISet(bodyVertexMap, b, numVertices);
760:       if (!BEfound) PetscHMapISet(bodyEdgeMap, b, numEdges);
761:       if (!BEGfound) PetscHMapISet(bodyEdgeGlobalMap, b, edgeCntr);
762:       if (!BFfound) PetscHMapISet(bodyFaceMap, b, numFaces);

764:       EG_getBodyTopos(body, NULL, FACE, &Nf, &fobjs);
765:       EG_getBodyTopos(body, NULL, EDGE, &Ne, &eobjs);
766:       EG_getBodyTopos(body, NULL, NODE, &Nv, &nobjs);
767:       EG_free(fobjs);
768:       EG_free(eobjs);
769:       EG_free(nobjs);

771:       // Remove DEGENERATE EDGES from Edge count
772:       EG_getBodyTopos(body, NULL, EDGE, &Ne, &eobjs);
773:       int Netemp = 0;
774:       for (int e = 0; e < Ne; ++e) {
775:         ego edge = eobjs[e];
776:         int eid;

778:         EG_getInfo(edge, &oclass, &mtype, &topRef, &prev, &next);
779:         eid = EG_indexBodyTopo(body, edge);

781:         PetscHMapIFind(edgeMap, edgeCntr + eid - 1, &EMiter, &EMfound);
782:         if (mtype == DEGENERATE) {
783:           if (!EMfound) PetscHMapISet(edgeMap, edgeCntr + eid - 1, -1);
784:         } else {
785:           ++Netemp;
786:           if (!EMfound) PetscHMapISet(edgeMap, edgeCntr + eid - 1, Netemp);
787:         }
788:       }
789:       EG_free(eobjs);

791:       // Determine Number of Cells
792:       EG_getBodyTopos(body, NULL, FACE, &Nf, &fobjs);
793:       for (int f = 0; f < Nf; ++f) {
794:         ego face     = fobjs[f];
795:         int edgeTemp = 0;

797:         EG_getBodyTopos(body, face, EDGE, &Ne, &eobjs);
798:         for (int e = 0; e < Ne; ++e) {
799:           ego edge = eobjs[e];

801:           EG_getInfo(edge, &oclass, &mtype, &topRef, &prev, &next);
802:           if (mtype != DEGENERATE) ++edgeTemp;
803:         }
804:         numCells += (2 * edgeTemp);
805:         EG_free(eobjs);
806:       }
807:       EG_free(fobjs);

809:       numFaces += Nf;
810:       numEdges += Netemp;
811:       numVertices += Nv;
812:       edgeCntr += Ne;
813:     }

815:     // Set up basic DMPlex parameters
816:     dim        = 2;                                 // Assumes 3D Models :: Need to handle 2D models in the future
817:     cdim       = 3;                                 // Assumes 3D Models :: Need to update to handle 2D models in future
818:     numCorners = 3;                                 // Split Faces into triangles
819:     numPoints  = numVertices + numEdges + numFaces; // total number of coordinate points

821:     PetscMalloc2(numPoints * cdim, &coords, numCells * numCorners, &cells);

823:     // Get Vertex Coordinates and Set up Cells
824:     for (b = 0; b < nbodies; ++b) {
825:       ego           body = bodies[b];
826:       int           Nf, Ne, Nv;
827:       PetscInt      bodyVertexIndexStart, bodyEdgeIndexStart, bodyEdgeGlobalIndexStart, bodyFaceIndexStart;
828:       PetscHashIter BViter, BEiter, BEGiter, BFiter, EMiter;
829:       PetscBool     BVfound, BEfound, BEGfound, BFfound, EMfound;

831:       // Vertices on Current Body
832:       EG_getBodyTopos(body, NULL, NODE, &Nv, &nobjs);

834:       PetscHMapIFind(bodyVertexMap, b, &BViter, &BVfound);
836:       PetscHMapIGet(bodyVertexMap, b, &bodyVertexIndexStart);

838:       for (int v = 0; v < Nv; ++v) {
839:         ego    vertex = nobjs[v];
840:         double limits[4];
841:         int    id, dummy;

843:         EG_getTopology(vertex, &geom, &oclass, &mtype, limits, &dummy, &mobjs, &senses);
844:         id = EG_indexBodyTopo(body, vertex);

846:         coords[(bodyVertexIndexStart + id - 1) * cdim + 0] = limits[0];
847:         coords[(bodyVertexIndexStart + id - 1) * cdim + 1] = limits[1];
848:         coords[(bodyVertexIndexStart + id - 1) * cdim + 2] = limits[2];
849:       }
850:       EG_free(nobjs);

852:       // Edge Midpoint Vertices on Current Body
853:       EG_getBodyTopos(body, NULL, EDGE, &Ne, &eobjs);

855:       PetscHMapIFind(bodyEdgeMap, b, &BEiter, &BEfound);
857:       PetscHMapIGet(bodyEdgeMap, b, &bodyEdgeIndexStart);

859:       PetscHMapIFind(bodyEdgeGlobalMap, b, &BEGiter, &BEGfound);
861:       PetscHMapIGet(bodyEdgeGlobalMap, b, &bodyEdgeGlobalIndexStart);

863:       for (int e = 0; e < Ne; ++e) {
864:         ego    edge = eobjs[e];
865:         double range[2], avgt[1], cntrPnt[9];
866:         int    eid, eOffset;
867:         int    periodic;

869:         EG_getInfo(edge, &oclass, &mtype, &topRef, &prev, &next);
870:         if (mtype == DEGENERATE) continue;

872:         eid = EG_indexBodyTopo(body, edge);

874:         // get relative offset from globalEdgeID Vector
875:         PetscHMapIFind(edgeMap, bodyEdgeGlobalIndexStart + eid - 1, &EMiter, &EMfound);
877:         PetscHMapIGet(edgeMap, bodyEdgeGlobalIndexStart + eid - 1, &eOffset);

879:         EG_getRange(edge, range, &periodic);
880:         avgt[0] = (range[0] + range[1]) / 2.;

882:         EG_evaluate(edge, avgt, cntrPnt);
883:         coords[(numVertices + bodyEdgeIndexStart + eOffset - 1) * cdim + 0] = cntrPnt[0];
884:         coords[(numVertices + bodyEdgeIndexStart + eOffset - 1) * cdim + 1] = cntrPnt[1];
885:         coords[(numVertices + bodyEdgeIndexStart + eOffset - 1) * cdim + 2] = cntrPnt[2];
886:       }
887:       EG_free(eobjs);

889:       // Face Midpoint Vertices on Current Body
890:       EG_getBodyTopos(body, NULL, FACE, &Nf, &fobjs);

892:       PetscHMapIFind(bodyFaceMap, b, &BFiter, &BFfound);
894:       PetscHMapIGet(bodyFaceMap, b, &bodyFaceIndexStart);

896:       for (int f = 0; f < Nf; ++f) {
897:         ego    face = fobjs[f];
898:         double range[4], avgUV[2], cntrPnt[18];
899:         int    peri, id;

901:         id = EG_indexBodyTopo(body, face);
902:         EG_getRange(face, range, &peri);

904:         avgUV[0] = (range[0] + range[1]) / 2.;
905:         avgUV[1] = (range[2] + range[3]) / 2.;
906:         EG_evaluate(face, avgUV, cntrPnt);

908:         coords[(numVertices + numEdges + bodyFaceIndexStart + id - 1) * cdim + 0] = cntrPnt[0];
909:         coords[(numVertices + numEdges + bodyFaceIndexStart + id - 1) * cdim + 1] = cntrPnt[1];
910:         coords[(numVertices + numEdges + bodyFaceIndexStart + id - 1) * cdim + 2] = cntrPnt[2];
911:       }
912:       EG_free(fobjs);

914:       // Define Cells :: Note - This could be incorporated in the Face Midpoint Vertices Loop but was kept separate for clarity
915:       EG_getBodyTopos(body, NULL, FACE, &Nf, &fobjs);
916:       for (int f = 0; f < Nf; ++f) {
917:         ego face = fobjs[f];
918:         int fID, midFaceID, midPntID, startID, endID, Nl;

920:         fID       = EG_indexBodyTopo(body, face);
921:         midFaceID = numVertices + numEdges + bodyFaceIndexStart + fID - 1;
922:         // Must Traverse Loop to ensure we have all necessary information like the sense (+/- 1) of the edges.
923:         // TODO :: Only handles single loop faces (No holes). The choices for handling multiloop faces are:
924:         //            1) Use the DMPlexCreateEGADSFromFile() with the -dm_plex_egads_with_tess = 1 option.
925:         //               This will use a default EGADS tessellation as an initial surface mesh.
926:         //            2) Create the initial surface mesh via a 2D mesher :: Currently not available (?future?)
927:         //               May I suggest the XXXX as a starting point?

929:         EG_getTopology(face, &geom, &oclass, &mtype, NULL, &Nl, &lobjs, &lSenses);

932:         for (int l = 0; l < Nl; ++l) {
933:           ego loop = lobjs[l];

935:           EG_getTopology(loop, &geom, &oclass, &mtype, NULL, &Ne, &eobjs, &eSenses);
936:           for (int e = 0; e < Ne; ++e) {
937:             ego edge = eobjs[e];
938:             int eid, eOffset;

940:             EG_getInfo(edge, &oclass, &mtype, &topRef, &prev, &next);
941:             eid = EG_indexBodyTopo(body, edge);
942:             if (mtype == DEGENERATE) continue;

944:             // get relative offset from globalEdgeID Vector
945:             PetscHMapIFind(edgeMap, bodyEdgeGlobalIndexStart + eid - 1, &EMiter, &EMfound);
947:             PetscHMapIGet(edgeMap, bodyEdgeGlobalIndexStart + eid - 1, &eOffset);

949:             midPntID = numVertices + bodyEdgeIndexStart + eOffset - 1;

951:             EG_getTopology(edge, &geom, &oclass, &mtype, NULL, &Nv, &nobjs, &senses);

953:             if (eSenses[e] > 0) {
954:               startID = EG_indexBodyTopo(body, nobjs[0]);
955:               endID   = EG_indexBodyTopo(body, nobjs[1]);
956:             } else {
957:               startID = EG_indexBodyTopo(body, nobjs[1]);
958:               endID   = EG_indexBodyTopo(body, nobjs[0]);
959:             }

961:             // Define 2 Cells per Edge with correct orientation
962:             cells[cellCntr * numCorners + 0] = midFaceID;
963:             cells[cellCntr * numCorners + 1] = bodyVertexIndexStart + startID - 1;
964:             cells[cellCntr * numCorners + 2] = midPntID;

966:             cells[cellCntr * numCorners + 3] = midFaceID;
967:             cells[cellCntr * numCorners + 4] = midPntID;
968:             cells[cellCntr * numCorners + 5] = bodyVertexIndexStart + endID - 1;

970:             cellCntr = cellCntr + 2;
971:           }
972:         }
973:       }
974:       EG_free(fobjs);
975:     }
976:   }

978:   // Generate DMPlex
979:   DMPlexCreateFromCellListPetsc(PETSC_COMM_WORLD, dim, numCells, numPoints, numCorners, PETSC_TRUE, cells, cdim, coords, &dm);
980:   PetscFree2(coords, cells);
981:   PetscInfo(dm, " Total Number of Unique Cells    = %" PetscInt_FMT " \n", numCells);
982:   PetscInfo(dm, " Total Number of Unique Vertices = %" PetscInt_FMT " \n", numVertices);

984:   // Embed EGADS model in DM
985:   {
986:     PetscContainer modelObj, contextObj;

988:     PetscContainerCreate(PETSC_COMM_SELF, &modelObj);
989:     PetscContainerSetPointer(modelObj, model);
990:     PetscObjectCompose((PetscObject)dm, "EGADS Model", (PetscObject)modelObj);
991:     PetscContainerDestroy(&modelObj);

993:     PetscContainerCreate(PETSC_COMM_SELF, &contextObj);
994:     PetscContainerSetPointer(contextObj, context);
995:     PetscContainerSetUserDestroy(contextObj, DMPlexEGADSDestroy_Private);
996:     PetscObjectCompose((PetscObject)dm, "EGADS Context", (PetscObject)contextObj);
997:     PetscContainerDestroy(&contextObj);
998:   }
999:   // Label points
1000:   PetscInt nStart, nEnd;

1002:   DMCreateLabel(dm, "EGADS Body ID");
1003:   DMGetLabel(dm, "EGADS Body ID", &bodyLabel);
1004:   DMCreateLabel(dm, "EGADS Face ID");
1005:   DMGetLabel(dm, "EGADS Face ID", &faceLabel);
1006:   DMCreateLabel(dm, "EGADS Edge ID");
1007:   DMGetLabel(dm, "EGADS Edge ID", &edgeLabel);
1008:   DMCreateLabel(dm, "EGADS Vertex ID");
1009:   DMGetLabel(dm, "EGADS Vertex ID", &vertexLabel);

1011:   DMPlexGetHeightStratum(dm, 2, &nStart, &nEnd);

1013:   cellCntr = 0;
1014:   for (b = 0; b < nbodies; ++b) {
1015:     ego           body = bodies[b];
1016:     int           Nv, Ne, Nf;
1017:     PetscInt      bodyVertexIndexStart, bodyEdgeIndexStart, bodyEdgeGlobalIndexStart, bodyFaceIndexStart;
1018:     PetscHashIter BViter, BEiter, BEGiter, BFiter, EMiter;
1019:     PetscBool     BVfound, BEfound, BEGfound, BFfound, EMfound;

1021:     PetscHMapIFind(bodyVertexMap, b, &BViter, &BVfound);
1023:     PetscHMapIGet(bodyVertexMap, b, &bodyVertexIndexStart);

1025:     PetscHMapIFind(bodyEdgeMap, b, &BEiter, &BEfound);
1027:     PetscHMapIGet(bodyEdgeMap, b, &bodyEdgeIndexStart);

1029:     PetscHMapIFind(bodyFaceMap, b, &BFiter, &BFfound);
1031:     PetscHMapIGet(bodyFaceMap, b, &bodyFaceIndexStart);

1033:     PetscHMapIFind(bodyEdgeGlobalMap, b, &BEGiter, &BEGfound);
1035:     PetscHMapIGet(bodyEdgeGlobalMap, b, &bodyEdgeGlobalIndexStart);

1037:     EG_getBodyTopos(body, NULL, FACE, &Nf, &fobjs);
1038:     for (int f = 0; f < Nf; ++f) {
1039:       ego face = fobjs[f];
1040:       int fID, Nl;

1042:       fID = EG_indexBodyTopo(body, face);

1044:       EG_getBodyTopos(body, face, LOOP, &Nl, &lobjs);
1045:       for (int l = 0; l < Nl; ++l) {
1046:         ego loop = lobjs[l];
1047:         int lid;

1049:         lid = EG_indexBodyTopo(body, loop);

1052:         EG_getTopology(loop, &geom, &oclass, &mtype, NULL, &Ne, &eobjs, &eSenses);
1053:         for (int e = 0; e < Ne; ++e) {
1054:           ego edge = eobjs[e];
1055:           int eid, eOffset;

1057:           // Skip DEGENERATE Edges
1058:           EG_getInfo(edge, &oclass, &mtype, &topRef, &prev, &next);
1059:           if (mtype == DEGENERATE) continue;
1060:           eid = EG_indexBodyTopo(body, edge);

1062:           // get relative offset from globalEdgeID Vector
1063:           PetscHMapIFind(edgeMap, bodyEdgeGlobalIndexStart + eid - 1, &EMiter, &EMfound);
1065:           PetscHMapIGet(edgeMap, bodyEdgeGlobalIndexStart + eid - 1, &eOffset);

1067:           EG_getBodyTopos(body, edge, NODE, &Nv, &nobjs);
1068:           for (int v = 0; v < Nv; ++v) {
1069:             ego vertex = nobjs[v];
1070:             int vID;

1072:             vID = EG_indexBodyTopo(body, vertex);
1073:             DMLabelSetValue(bodyLabel, nStart + bodyVertexIndexStart + vID - 1, b);
1074:             DMLabelSetValue(vertexLabel, nStart + bodyVertexIndexStart + vID - 1, vID);
1075:           }
1076:           EG_free(nobjs);

1078:           DMLabelSetValue(bodyLabel, nStart + numVertices + bodyEdgeIndexStart + eOffset - 1, b);
1079:           DMLabelSetValue(edgeLabel, nStart + numVertices + bodyEdgeIndexStart + eOffset - 1, eid);

1081:           // Define Cell faces
1082:           for (int jj = 0; jj < 2; ++jj) {
1083:             DMLabelSetValue(bodyLabel, cellCntr, b);
1084:             DMLabelSetValue(faceLabel, cellCntr, fID);
1085:             DMPlexGetCone(dm, cellCntr, &cone);

1087:             DMLabelSetValue(bodyLabel, cone[0], b);
1088:             DMLabelSetValue(faceLabel, cone[0], fID);

1090:             DMLabelSetValue(bodyLabel, cone[1], b);
1091:             DMLabelSetValue(edgeLabel, cone[1], eid);

1093:             DMLabelSetValue(bodyLabel, cone[2], b);
1094:             DMLabelSetValue(faceLabel, cone[2], fID);

1096:             cellCntr = cellCntr + 1;
1097:           }
1098:         }
1099:       }
1100:       EG_free(lobjs);

1102:       DMLabelSetValue(bodyLabel, nStart + numVertices + numEdges + bodyFaceIndexStart + fID - 1, b);
1103:       DMLabelSetValue(faceLabel, nStart + numVertices + numEdges + bodyFaceIndexStart + fID - 1, fID);
1104:     }
1105:     EG_free(fobjs);
1106:   }

1108:   PetscHMapIDestroy(&edgeMap);
1109:   PetscHMapIDestroy(&bodyIndexMap);
1110:   PetscHMapIDestroy(&bodyVertexMap);
1111:   PetscHMapIDestroy(&bodyEdgeMap);
1112:   PetscHMapIDestroy(&bodyEdgeGlobalMap);
1113:   PetscHMapIDestroy(&bodyFaceMap);

1115:   *newdm = dm;
1116:   return 0;
1117: }

1119: static PetscErrorCode DMPlexCreateEGADS_Tess_Internal(MPI_Comm comm, ego context, ego model, DM *newdm)
1120: {
1121:   DMLabel bodyLabel, faceLabel, edgeLabel, vertexLabel;
1122:   /* EGADSLite variables */
1123:   ego    geom, *bodies, *fobjs;
1124:   int    b, oclass, mtype, nbodies, *senses;
1125:   int    totalNumTris = 0, totalNumPoints = 0;
1126:   double boundBox[6] = {0., 0., 0., 0., 0., 0.}, tessSize;
1127:   /* PETSc variables */
1128:   DM              dm;
1129:   PetscHMapI      pointIndexStartMap = NULL, triIndexStartMap = NULL, pTypeLabelMap = NULL, pIndexLabelMap = NULL;
1130:   PetscHMapI      pBodyIndexLabelMap = NULL, triFaceIDLabelMap = NULL, triBodyIDLabelMap = NULL;
1131:   PetscInt        dim = -1, cdim = -1, numCorners = 0, counter = 0;
1132:   PetscInt       *cells  = NULL;
1133:   const PetscInt *cone   = NULL;
1134:   PetscReal      *coords = NULL;
1135:   PetscMPIInt     rank;

1138:   MPI_Comm_rank(comm, &rank);
1139:   if (rank == 0) {
1140:     // ---------------------------------------------------------------------------------------------------
1141:     // Generate Petsc Plex from EGADSlite created Tessellation of geometry
1142:     // ---------------------------------------------------------------------------------------------------

1144:     // Calculate cell and vertex sizes
1145:     EG_getTopology(model, &geom, &oclass, &mtype, NULL, &nbodies, &bodies, &senses);

1147:     PetscHMapICreate(&pointIndexStartMap);
1148:     PetscHMapICreate(&triIndexStartMap);
1149:     PetscHMapICreate(&pTypeLabelMap);
1150:     PetscHMapICreate(&pIndexLabelMap);
1151:     PetscHMapICreate(&pBodyIndexLabelMap);
1152:     PetscHMapICreate(&triFaceIDLabelMap);
1153:     PetscHMapICreate(&triBodyIDLabelMap);

1155:     /* Create Tessellation of Bodies */
1156:     ego tessArray[nbodies];

1158:     for (b = 0; b < nbodies; ++b) {
1159:       ego           body      = bodies[b];
1160:       double        params[3] = {0.0, 0.0, 0.0}; // Parameters for Tessellation
1161:       int           Nf, bodyNumPoints = 0, bodyNumTris = 0;
1162:       PetscHashIter PISiter, TISiter;
1163:       PetscBool     PISfound, TISfound;

1165:       /* Store Start Index for each Body's Point and Tris */
1166:       PetscHMapIFind(pointIndexStartMap, b, &PISiter, &PISfound);
1167:       PetscHMapIFind(triIndexStartMap, b, &TISiter, &TISfound);

1169:       if (!PISfound) PetscHMapISet(pointIndexStartMap, b, totalNumPoints);
1170:       if (!TISfound) PetscHMapISet(triIndexStartMap, b, totalNumTris);

1172:       /* Calculate Tessellation parameters based on Bounding Box */
1173:       /* Get Bounding Box Dimensions of the BODY */
1174:       EG_getBoundingBox(body, boundBox);
1175:       tessSize = boundBox[3] - boundBox[0];
1176:       if (tessSize < boundBox[4] - boundBox[1]) tessSize = boundBox[4] - boundBox[1];
1177:       if (tessSize < boundBox[5] - boundBox[2]) tessSize = boundBox[5] - boundBox[2];

1179:       // TODO :: May want to give users tessellation parameter options //
1180:       params[0] = 0.0250 * tessSize;
1181:       params[1] = 0.0075 * tessSize;
1182:       params[2] = 15.0;

1184:       EG_makeTessBody(body, params, &tessArray[b]);

1186:       EG_getBodyTopos(body, NULL, FACE, &Nf, &fobjs);

1188:       for (int f = 0; f < Nf; ++f) {
1189:         ego           face = fobjs[f];
1190:         int           len, fID, ntris;
1191:         const int    *ptype, *pindex, *ptris, *ptric;
1192:         const double *pxyz, *puv;

1194:         // Get Face ID //
1195:         fID = EG_indexBodyTopo(body, face);

1197:         // Checkout the Surface Tessellation //
1198:         EG_getTessFace(tessArray[b], fID, &len, &pxyz, &puv, &ptype, &pindex, &ntris, &ptris, &ptric);

1200:         // Determine total number of triangle cells in the tessellation //
1201:         bodyNumTris += (int)ntris;

1203:         // Check out the point index and coordinate //
1204:         for (int p = 0; p < len; ++p) {
1205:           int global;

1207:           EG_localToGlobal(tessArray[b], fID, p + 1, &global);

1209:           // Determine the total number of points in the tessellation //
1210:           bodyNumPoints = PetscMax(bodyNumPoints, global);
1211:         }
1212:       }
1213:       EG_free(fobjs);

1215:       totalNumPoints += bodyNumPoints;
1216:       totalNumTris += bodyNumTris;
1217:     }
1218:     //}  - Original End of (rank == 0)

1220:     dim        = 2;
1221:     cdim       = 3;
1222:     numCorners = 3;
1223:     //PetscInt counter = 0;

1225:     /* NEED TO DEFINE MATRICES/VECTORS TO STORE GEOM REFERENCE DATA   */
1226:     /* Fill in below and use to define DMLabels after DMPlex creation */
1227:     PetscMalloc2(totalNumPoints * cdim, &coords, totalNumTris * numCorners, &cells);

1229:     for (b = 0; b < nbodies; ++b) {
1230:       ego           body = bodies[b];
1231:       int           Nf;
1232:       PetscInt      pointIndexStart;
1233:       PetscHashIter PISiter;
1234:       PetscBool     PISfound;

1236:       PetscHMapIFind(pointIndexStartMap, b, &PISiter, &PISfound);
1238:       PetscHMapIGet(pointIndexStartMap, b, &pointIndexStart);

1240:       EG_getBodyTopos(body, NULL, FACE, &Nf, &fobjs);

1242:       for (int f = 0; f < Nf; ++f) {
1243:         /* Get Face Object */
1244:         ego           face = fobjs[f];
1245:         int           len, fID, ntris;
1246:         const int    *ptype, *pindex, *ptris, *ptric;
1247:         const double *pxyz, *puv;

1249:         /* Get Face ID */
1250:         fID = EG_indexBodyTopo(body, face);

1252:         /* Checkout the Surface Tessellation */
1253:         EG_getTessFace(tessArray[b], fID, &len, &pxyz, &puv, &ptype, &pindex, &ntris, &ptris, &ptric);

1255:         /* Check out the point index and coordinate */
1256:         for (int p = 0; p < len; ++p) {
1257:           int           global;
1258:           PetscHashIter PTLiter, PILiter, PBLiter;
1259:           PetscBool     PTLfound, PILfound, PBLfound;

1261:           EG_localToGlobal(tessArray[b], fID, p + 1, &global);

1263:           /* Set the coordinates array for DAG */
1264:           coords[((global - 1 + pointIndexStart) * 3) + 0] = pxyz[(p * 3) + 0];
1265:           coords[((global - 1 + pointIndexStart) * 3) + 1] = pxyz[(p * 3) + 1];
1266:           coords[((global - 1 + pointIndexStart) * 3) + 2] = pxyz[(p * 3) + 2];

1268:           /* Store Geometry Label Information for DMLabel assignment later */
1269:           PetscHMapIFind(pTypeLabelMap, global - 1 + pointIndexStart, &PTLiter, &PTLfound);
1270:           PetscHMapIFind(pIndexLabelMap, global - 1 + pointIndexStart, &PILiter, &PILfound);
1271:           PetscHMapIFind(pBodyIndexLabelMap, global - 1 + pointIndexStart, &PBLiter, &PBLfound);

1273:           if (!PTLfound) PetscHMapISet(pTypeLabelMap, global - 1 + pointIndexStart, ptype[p]);
1274:           if (!PILfound) PetscHMapISet(pIndexLabelMap, global - 1 + pointIndexStart, pindex[p]);
1275:           if (!PBLfound) PetscHMapISet(pBodyIndexLabelMap, global - 1 + pointIndexStart, b);

1277:           if (ptype[p] < 0) PetscHMapISet(pIndexLabelMap, global - 1 + pointIndexStart, fID);
1278:         }

1280:         for (int t = 0; t < (int)ntris; ++t) {
1281:           int           global, globalA, globalB;
1282:           PetscHashIter TFLiter, TBLiter;
1283:           PetscBool     TFLfound, TBLfound;

1285:           EG_localToGlobal(tessArray[b], fID, ptris[(t * 3) + 0], &global);
1286:           cells[(counter * 3) + 0] = global - 1 + pointIndexStart;

1288:           EG_localToGlobal(tessArray[b], fID, ptris[(t * 3) + 1], &globalA);
1289:           cells[(counter * 3) + 1] = globalA - 1 + pointIndexStart;

1291:           EG_localToGlobal(tessArray[b], fID, ptris[(t * 3) + 2], &globalB);
1292:           cells[(counter * 3) + 2] = globalB - 1 + pointIndexStart;

1294:           PetscHMapIFind(triFaceIDLabelMap, counter, &TFLiter, &TFLfound);
1295:           PetscHMapIFind(triBodyIDLabelMap, counter, &TBLiter, &TBLfound);

1297:           if (!TFLfound) PetscHMapISet(triFaceIDLabelMap, counter, fID);
1298:           if (!TBLfound) PetscHMapISet(triBodyIDLabelMap, counter, b);

1300:           counter += 1;
1301:         }
1302:       }
1303:       EG_free(fobjs);
1304:     }
1305:   }

1307:   //Build DMPlex
1308:   DMPlexCreateFromCellListPetsc(PETSC_COMM_WORLD, dim, totalNumTris, totalNumPoints, numCorners, PETSC_TRUE, cells, cdim, coords, &dm);
1309:   PetscFree2(coords, cells);

1311:   // Embed EGADS model in DM
1312:   {
1313:     PetscContainer modelObj, contextObj;

1315:     PetscContainerCreate(PETSC_COMM_SELF, &modelObj);
1316:     PetscContainerSetPointer(modelObj, model);
1317:     PetscObjectCompose((PetscObject)dm, "EGADS Model", (PetscObject)modelObj);
1318:     PetscContainerDestroy(&modelObj);

1320:     PetscContainerCreate(PETSC_COMM_SELF, &contextObj);
1321:     PetscContainerSetPointer(contextObj, context);
1322:     PetscContainerSetUserDestroy(contextObj, DMPlexEGADSDestroy_Private);
1323:     PetscObjectCompose((PetscObject)dm, "EGADS Context", (PetscObject)contextObj);
1324:     PetscContainerDestroy(&contextObj);
1325:   }

1327:   // Label Points
1328:   DMCreateLabel(dm, "EGADS Body ID");
1329:   DMGetLabel(dm, "EGADS Body ID", &bodyLabel);
1330:   DMCreateLabel(dm, "EGADS Face ID");
1331:   DMGetLabel(dm, "EGADS Face ID", &faceLabel);
1332:   DMCreateLabel(dm, "EGADS Edge ID");
1333:   DMGetLabel(dm, "EGADS Edge ID", &edgeLabel);
1334:   DMCreateLabel(dm, "EGADS Vertex ID");
1335:   DMGetLabel(dm, "EGADS Vertex ID", &vertexLabel);

1337:   /* Get Number of DAG Nodes at each level */
1338:   int fStart, fEnd, eStart, eEnd, nStart, nEnd;

1340:   DMPlexGetHeightStratum(dm, 0, &fStart, &fEnd);
1341:   DMPlexGetHeightStratum(dm, 1, &eStart, &eEnd);
1342:   DMPlexGetHeightStratum(dm, 2, &nStart, &nEnd);

1344:   /* Set DMLabels for NODES */
1345:   for (int n = nStart; n < nEnd; ++n) {
1346:     int           pTypeVal, pIndexVal, pBodyVal;
1347:     PetscHashIter PTLiter, PILiter, PBLiter;
1348:     PetscBool     PTLfound, PILfound, PBLfound;

1350:     //Converted to Hash Tables
1351:     PetscHMapIFind(pTypeLabelMap, n - nStart, &PTLiter, &PTLfound);
1353:     PetscHMapIGet(pTypeLabelMap, n - nStart, &pTypeVal);

1355:     PetscHMapIFind(pIndexLabelMap, n - nStart, &PILiter, &PILfound);
1357:     PetscHMapIGet(pIndexLabelMap, n - nStart, &pIndexVal);

1359:     PetscHMapIFind(pBodyIndexLabelMap, n - nStart, &PBLiter, &PBLfound);
1361:     PetscHMapIGet(pBodyIndexLabelMap, n - nStart, &pBodyVal);

1363:     DMLabelSetValue(bodyLabel, n, pBodyVal);
1364:     if (pTypeVal == 0) DMLabelSetValue(vertexLabel, n, pIndexVal);
1365:     if (pTypeVal > 0) DMLabelSetValue(edgeLabel, n, pIndexVal);
1366:     if (pTypeVal < 0) DMLabelSetValue(faceLabel, n, pIndexVal);
1367:   }

1369:   /* Set DMLabels for Edges - Based on the DMLabels of the EDGE's NODES */
1370:   for (int e = eStart; e < eEnd; ++e) {
1371:     int bodyID_0, vertexID_0, vertexID_1, edgeID_0, edgeID_1, faceID_0, faceID_1;

1373:     DMPlexGetCone(dm, e, &cone);
1374:     DMLabelGetValue(bodyLabel, cone[0], &bodyID_0); // Do I need to check the other end?
1375:     DMLabelGetValue(vertexLabel, cone[0], &vertexID_0);
1376:     DMLabelGetValue(vertexLabel, cone[1], &vertexID_1);
1377:     DMLabelGetValue(edgeLabel, cone[0], &edgeID_0);
1378:     DMLabelGetValue(edgeLabel, cone[1], &edgeID_1);
1379:     DMLabelGetValue(faceLabel, cone[0], &faceID_0);
1380:     DMLabelGetValue(faceLabel, cone[1], &faceID_1);

1382:     DMLabelSetValue(bodyLabel, e, bodyID_0);

1384:     if (edgeID_0 == edgeID_1) DMLabelSetValue(edgeLabel, e, edgeID_0);
1385:     else if (vertexID_0 > 0 && edgeID_1 > 0) DMLabelSetValue(edgeLabel, e, edgeID_1);
1386:     else if (vertexID_1 > 0 && edgeID_0 > 0) DMLabelSetValue(edgeLabel, e, edgeID_0);
1387:     else { /* Do Nothing */ }
1388:   }

1390:   /* Set DMLabels for Cells */
1391:   for (int f = fStart; f < fEnd; ++f) {
1392:     int           edgeID_0;
1393:     PetscInt      triBodyVal, triFaceVal;
1394:     PetscHashIter TFLiter, TBLiter;
1395:     PetscBool     TFLfound, TBLfound;

1397:     // Convert to Hash Table
1398:     PetscHMapIFind(triFaceIDLabelMap, f - fStart, &TFLiter, &TFLfound);
1400:     PetscHMapIGet(triFaceIDLabelMap, f - fStart, &triFaceVal);

1402:     PetscHMapIFind(triBodyIDLabelMap, f - fStart, &TBLiter, &TBLfound);
1404:     PetscHMapIGet(triBodyIDLabelMap, f - fStart, &triBodyVal);

1406:     DMLabelSetValue(bodyLabel, f, triBodyVal);
1407:     DMLabelSetValue(faceLabel, f, triFaceVal);

1409:     /* Finish Labeling previously unlabeled DMPlex Edges - Assumes Triangular Cell (3 Edges Max) */
1410:     DMPlexGetCone(dm, f, &cone);

1412:     for (int jj = 0; jj < 3; ++jj) {
1413:       DMLabelGetValue(edgeLabel, cone[jj], &edgeID_0);

1415:       if (edgeID_0 < 0) {
1416:         DMLabelSetValue(bodyLabel, cone[jj], triBodyVal);
1417:         DMLabelSetValue(faceLabel, cone[jj], triFaceVal);
1418:       }
1419:     }
1420:   }

1422:   *newdm = dm;
1423:   return 0;
1424: }
1425: #endif

1427: /*@
1428:   DMPlexInflateToGeomModel - Snaps the vertex coordinates of a `DMPLEX` object representing the mesh to its geometry if some vertices depart from the model. This usually happens with non-conforming refinement.

1430:   Collective on dm

1432:   Input Parameter:
1433: . dm - The uninflated `DM` object representing the mesh

1435:   Output Parameter:
1436: . dm - The inflated `DM` object representing the mesh

1438:   Level: intermediate

1440: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexCreateEGADS()`
1441: @*/
1442: PetscErrorCode DMPlexInflateToGeomModel(DM dm)
1443: {
1444: #if defined(PETSC_HAVE_EGADS)
1445:   /* EGADS Variables */
1446:   ego    model, geom, body, face, edge;
1447:   ego   *bodies;
1448:   int    Nb, oclass, mtype, *senses;
1449:   double result[3];
1450:   /* PETSc Variables */
1451:   DM             cdm;
1452:   PetscContainer modelObj;
1453:   DMLabel        bodyLabel, faceLabel, edgeLabel, vertexLabel;
1454:   Vec            coordinates;
1455:   PetscScalar   *coords;
1456:   PetscInt       bodyID, faceID, edgeID, vertexID;
1457:   PetscInt       cdim, d, vStart, vEnd, v;
1458: #endif

1460: #if defined(PETSC_HAVE_EGADS)
1461:   PetscObjectQuery((PetscObject)dm, "EGADS Model", (PetscObject *)&modelObj);
1462:   if (!modelObj) return 0;
1463:   DMGetCoordinateDim(dm, &cdim);
1464:   DMGetCoordinateDM(dm, &cdm);
1465:   DMGetCoordinatesLocal(dm, &coordinates);
1466:   DMGetLabel(dm, "EGADS Body ID", &bodyLabel);
1467:   DMGetLabel(dm, "EGADS Face ID", &faceLabel);
1468:   DMGetLabel(dm, "EGADS Edge ID", &edgeLabel);
1469:   DMGetLabel(dm, "EGADS Vertex ID", &vertexLabel);

1471:   PetscContainerGetPointer(modelObj, (void **)&model);
1472:   EG_getTopology(model, &geom, &oclass, &mtype, NULL, &Nb, &bodies, &senses);

1474:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
1475:   VecGetArrayWrite(coordinates, &coords);
1476:   for (v = vStart; v < vEnd; ++v) {
1477:     PetscScalar *vcoords;

1479:     DMLabelGetValue(bodyLabel, v, &bodyID);
1480:     DMLabelGetValue(faceLabel, v, &faceID);
1481:     DMLabelGetValue(edgeLabel, v, &edgeID);
1482:     DMLabelGetValue(vertexLabel, v, &vertexID);

1485:     body = bodies[bodyID];

1487:     DMPlexPointLocalRef(cdm, v, coords, (void *)&vcoords);
1488:     if (edgeID > 0) {
1489:       /* Snap to EDGE at nearest location */
1490:       double params[1];
1491:       EG_objectBodyTopo(body, EDGE, edgeID, &edge);
1492:       EG_invEvaluate(edge, vcoords, params, result); // Get (x,y,z) of nearest point on EDGE
1493:       for (d = 0; d < cdim; ++d) vcoords[d] = result[d];
1494:     } else if (faceID > 0) {
1495:       /* Snap to FACE at nearest location */
1496:       double params[2];
1497:       EG_objectBodyTopo(body, FACE, faceID, &face);
1498:       EG_invEvaluate(face, vcoords, params, result); // Get (x,y,z) of nearest point on FACE
1499:       for (d = 0; d < cdim; ++d) vcoords[d] = result[d];
1500:     }
1501:   }
1502:   VecRestoreArrayWrite(coordinates, &coords);
1503:   /* Clear out global coordinates */
1504:   VecDestroy(&dm->coordinates[0].x);
1505: #endif
1506:   return 0;
1507: }

1509: /*@C
1510:   DMPlexCreateEGADSFromFile - Create a `DMPLEX` mesh from an EGADS, IGES, or STEP file.

1512:   Collective

1514:   Input Parameters:
1515: + comm     - The MPI communicator
1516: - filename - The name of the EGADS, IGES, or STEP file

1518:   Output Parameter:
1519: . dm       - The `DM` object representing the mesh

1521:   Level: beginner

1523: .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMCreate()`, `DMPlexCreateEGADS()`, `DMPlexCreateEGADSLiteFromFile()`
1524: @*/
1525: PetscErrorCode DMPlexCreateEGADSFromFile(MPI_Comm comm, const char filename[], DM *dm)
1526: {
1527:   PetscMPIInt rank;
1528: #if defined(PETSC_HAVE_EGADS)
1529:   ego context = NULL, model = NULL;
1530: #endif
1531:   PetscBool printModel = PETSC_FALSE, tessModel = PETSC_FALSE, newModel = PETSC_FALSE;

1534:   PetscOptionsGetBool(NULL, NULL, "-dm_plex_egads_print_model", &printModel, NULL);
1535:   PetscOptionsGetBool(NULL, NULL, "-dm_plex_egads_tess_model", &tessModel, NULL);
1536:   PetscOptionsGetBool(NULL, NULL, "-dm_plex_egads_new_model", &newModel, NULL);
1537:   MPI_Comm_rank(comm, &rank);
1538: #if defined(PETSC_HAVE_EGADS)
1539:   if (rank == 0) {
1540:     EG_open(&context);
1541:     EG_loadModel(context, 0, filename, &model);
1542:     if (printModel) DMPlexEGADSPrintModel_Internal(model);
1543:   }
1544:   if (tessModel) DMPlexCreateEGADS_Tess_Internal(comm, context, model, dm);
1545:   else if (newModel) DMPlexCreateEGADS(comm, context, model, dm);
1546:   else DMPlexCreateEGADS_Internal(comm, context, model, dm);
1547:   return 0;
1548: #else
1549:   SETERRQ(comm, PETSC_ERR_SUP, "This method requires EGADS support. Reconfigure using --download-egads");
1550: #endif
1551: }