Actual source code: ex18.c
1: static const char help[] = "Test of PETSc/CAD Shape Modification Technology";
3: #include <petscdmplexegads.h>
4: #include <petsc/private/hashmapi.h>
6: static PetscErrorCode surfArea(DM dm)
7: {
8: DMLabel bodyLabel, faceLabel;
9: double surfaceArea = 0., volume = 0.;
10: PetscReal vol, centroid[3], normal[3];
11: PetscInt dim, cStart, cEnd, fStart, fEnd;
12: PetscInt bodyID, faceID;
13: MPI_Comm comm;
14: const char *name;
16: PetscFunctionBeginUser;
17: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
18: PetscCall(PetscObjectGetName((PetscObject)dm, &name));
19: PetscCall(DMGetDimension(dm, &dim));
20: PetscCall(PetscPrintf(comm, " dim = %" PetscInt_FMT "\n", dim));
21: PetscCall(DMGetLabel(dm, "EGADS Body ID", &bodyLabel));
22: PetscCall(DMGetLabel(dm, "EGADS Face ID", &faceLabel));
23: PetscCall(DMGetCoordinatesLocalSetUp(dm));
25: if (dim == 2) {
26: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
27: for (PetscInt ii = cStart; ii < cEnd; ++ii) {
28: PetscCall(DMLabelGetValue(faceLabel, ii, &faceID));
29: if (faceID >= 0) {
30: PetscCall(DMPlexComputeCellGeometryFVM(dm, ii, &vol, centroid, normal));
31: surfaceArea += vol;
32: }
33: }
34: }
36: if (dim == 3) {
37: PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
38: for (PetscInt ii = fStart; ii < fEnd; ++ii) {
39: PetscCall(DMLabelGetValue(faceLabel, ii, &faceID));
40: if (faceID >= 0) {
41: PetscCall(DMPlexComputeCellGeometryFVM(dm, ii, &vol, centroid, normal));
42: surfaceArea += vol;
43: }
44: }
46: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
47: for (PetscInt ii = cStart; ii < cEnd; ++ii) {
48: PetscCall(DMLabelGetValue(bodyLabel, ii, &bodyID));
49: if (bodyID >= 0) {
50: PetscCall(DMPlexComputeCellGeometryFVM(dm, ii, &vol, centroid, normal));
51: volume += vol;
52: }
53: }
54: }
56: if (dim == 2) {
57: PetscCall(PetscPrintf(comm, "%s Surface Area = %.6e \n\n", name, (double)surfaceArea));
58: } else if (dim == 3) {
59: PetscCall(PetscPrintf(comm, "%s Volume = %.6e \n", name, (double)volume));
60: PetscCall(PetscPrintf(comm, "%s Surface Area = %.6e \n\n", name, (double)surfaceArea));
61: }
62: PetscFunctionReturn(PETSC_SUCCESS);
63: }
65: int main(int argc, char *argv[])
66: {
67: /* EGADSlite variables */
68: PetscGeom model, *bodies, *fobjs;
69: int Nb, Nf, id;
70: /* PETSc variables */
71: DM dm;
72: MPI_Comm comm;
73: PetscContainer modelObj, cpHashTableObj, wHashTableObj, cpCoordDataLengthObj, wDataLengthObj;
74: Vec cntrlPtCoordsVec, cntrlPtWeightsVec;
75: PetscScalar *cpCoordData, *wData;
76: PetscInt cpCoordDataLength, wDataLength;
77: PetscInt *cpCoordDataLengthPtr, *wDataLengthPtr;
78: PetscHMapI cpHashTable, wHashTable;
79: PetscInt Nr = 2;
81: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
82: comm = PETSC_COMM_WORLD;
83: PetscCall(DMCreate(PETSC_COMM_WORLD, &dm));
84: PetscCall(DMSetType(dm, DMPLEX));
85: PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
86: PetscCall(DMSetFromOptions(dm));
88: PetscCall(PetscObjectSetName((PetscObject)dm, "Original Surface"));
89: PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
90: PetscCall(surfArea(dm));
92: // Expose Geometry Definition Data and Calculate Surface Gradients
93: PetscCall(DMPlexGeomDataAndGrads(dm, PETSC_FALSE));
95: // Get Attached EGADS model
96: PetscCall(PetscObjectQuery((PetscObject)dm, "EGADS Model", (PetscObject *)&modelObj));
97: if (!modelObj) PetscCall(PetscObjectQuery((PetscObject)dm, "EGADSlite Model", (PetscObject *)&modelObj));
99: // Get attached EGADS model (pointer)
100: PetscCall(PetscContainerGetPointer(modelObj, &model));
102: // Look to see if DM has Container for Geometry Control Point Data
103: PetscCall(PetscObjectQuery((PetscObject)dm, "Control Point Hash Table", (PetscObject *)&cpHashTableObj));
104: PetscCall(PetscObjectQuery((PetscObject)dm, "Control Point Coordinates", (PetscObject *)&cntrlPtCoordsVec));
105: PetscCall(PetscObjectQuery((PetscObject)dm, "Control Point Coordinate Data Length", (PetscObject *)&cpCoordDataLengthObj));
106: PetscCall(PetscObjectQuery((PetscObject)dm, "Control Point Weights Hash Table", (PetscObject *)&wHashTableObj));
107: PetscCall(PetscObjectQuery((PetscObject)dm, "Control Point Weight Data", (PetscObject *)&cntrlPtWeightsVec));
108: PetscCall(PetscObjectQuery((PetscObject)dm, "Control Point Weight Data Length", (PetscObject *)&wDataLengthObj));
110: // Get attached EGADS model Control Point and Weights Hash Tables and Data Arrays (pointer)
111: PetscCall(PetscContainerGetPointer(cpHashTableObj, &cpHashTable));
112: PetscCall(VecGetArrayWrite(cntrlPtCoordsVec, &cpCoordData));
113: PetscCall(PetscContainerGetPointer(cpCoordDataLengthObj, &cpCoordDataLengthPtr));
114: PetscCall(PetscContainerGetPointer(wHashTableObj, &wHashTable));
115: PetscCall(VecGetArrayWrite(cntrlPtWeightsVec, &wData));
116: PetscCall(PetscContainerGetPointer(wDataLengthObj, &wDataLengthPtr));
118: cpCoordDataLength = *cpCoordDataLengthPtr;
119: wDataLength = *wDataLengthPtr;
120: PetscCheck(cpCoordDataLength && wDataLength, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Data sizes must be positive");
122: // Get the number of bodies and body objects in the model
123: PetscCall(DMPlexGetGeomModelBodies(dm, &bodies, &Nb));
125: // Get all FACES of the Body
126: PetscGeom body = bodies[0];
127: PetscCall(DMPlexGetGeomModelBodyFaces(dm, body, &fobjs, &Nf));
129: // Update Control Point and Weight definitions for each surface
130: for (PetscInt jj = 0; jj < Nf; ++jj) {
131: PetscGeom face = fobjs[jj];
132: PetscInt numCntrlPnts = 0;
134: PetscCall(DMPlexGetGeomID(dm, body, face, &id));
135: PetscCall(DMPlexGetGeomFaceNumOfControlPoints(dm, face, &numCntrlPnts));
137: // Update Control Points
138: PetscHashIter CPiter, Witer;
139: PetscBool CPfound, Wfound;
140: PetscInt faceCPStartRow, faceWStartRow;
142: PetscCall(PetscHMapIFind(cpHashTable, id, &CPiter, &CPfound));
143: PetscCheck(CPfound, comm, PETSC_ERR_SUP, "FACE ID not found in Control Point Hash Table");
144: PetscCall(PetscHMapIGet(cpHashTable, id, &faceCPStartRow));
146: PetscCall(PetscHMapIFind(wHashTable, id, &Witer, &Wfound));
147: PetscCheck(Wfound, comm, PETSC_ERR_SUP, "FACE ID not found in Control Point Weights Hash Table");
148: PetscCall(PetscHMapIGet(wHashTable, id, &faceWStartRow));
150: for (PetscInt ii = 0; ii < numCntrlPnts; ++ii) {
151: if (ii == 4) {
152: // Update Control Points - Change the location of the center control point of the faces
153: // Note :: Modification geometry requires knowledge of how the geometry is defined.
154: cpCoordData[faceCPStartRow + (3 * ii) + 0] = 2.0 * cpCoordData[faceCPStartRow + (3 * ii) + 0];
155: cpCoordData[faceCPStartRow + (3 * ii) + 1] = 2.0 * cpCoordData[faceCPStartRow + (3 * ii) + 1];
156: cpCoordData[faceCPStartRow + (3 * ii) + 2] = 2.0 * cpCoordData[faceCPStartRow + (3 * ii) + 2];
157: } else {
158: // Do Nothing
159: // Note: Could use section the change location of other face control points.
160: }
161: }
162: }
163: PetscCall(DMPlexFreeGeomObject(dm, fobjs));
165: // Modify Control Points of Geometry
166: PetscCall(PetscObjectSetName((PetscObject)dm, "Modified Surface"));
167: // TODO Wrap EG_saveModel() in a Plex viewer to manage file access
168: PetscCall(DMPlexModifyGeomModel(dm, comm, cpCoordData, wData, PETSC_FALSE, PETSC_TRUE, "newModel.stp"));
169: PetscCall(VecRestoreArrayWrite(cntrlPtCoordsVec, &cpCoordData));
170: PetscCall(VecRestoreArrayWrite(cntrlPtWeightsVec, &wData));
172: // Inflate Mesh to Geometry
173: PetscCall(DMPlexInflateToGeomModel(dm, PETSC_FALSE));
174: PetscCall(surfArea(dm));
176: // Output .hdf5 file
177: PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
179: for (PetscInt r = 0; r < Nr; ++r) {
180: char name[PETSC_MAX_PATH_LEN];
182: // Perform refinement on the Mesh attached to the new geometry
183: PetscCall(DMSetFromOptions(dm));
184: PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "Modified Surface refinement %" PetscInt_FMT, r));
185: PetscCall(PetscObjectSetName((PetscObject)dm, name));
186: PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
187: PetscCall(surfArea(dm));
188: }
190: PetscCall(DMDestroy(&dm));
191: PetscCall(PetscFinalize());
192: return 0;
193: }
195: /*TEST
196: build:
197: requires: egads
199: # Use -dm_view hdf5:mesh_shapeMod_sphere.h5 to view the mesh
200: test:
201: suffix: sphere_shapeMod
202: requires: datafilespath
203: temporaries: newModel.stp
204: args: -dm_plex_filename ${DATAFILESPATH}/meshes/cad/sphere_example.stp \
205: -dm_refine -dm_plex_geom_print_model 1 -dm_plex_geom_shape_opt 1
207: TEST*/