xref: /petsc/src/dm/impls/plex/tests/ex37.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
1 static const char help[] = "Test of CAD functionality";
2 
3 #include <petscdmplex.h>
4 
5 /* TODO
6   - Fix IGES
7   - Test tessellation using -dm_plex_egads_with_tess
8 */
9 
10 typedef struct {
11   char      filename[PETSC_MAX_PATH_LEN];
12   PetscBool volumeMesh;
13 } AppCtx;
14 
15 static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) {
16   PetscFunctionBeginUser;
17   options->filename[0] = '\0';
18   options->volumeMesh  = PETSC_TRUE;
19 
20   PetscOptionsBegin(comm, "", "EGADSPlex Problem Options", "EGADSLite");
21   PetscCall(PetscOptionsString("-filename", "The CAD file", "ex37.c", options->filename, options->filename, sizeof(options->filename), NULL));
22   PetscCall(PetscOptionsBool("-volume_mesh", "Create a volume mesh", "ex37.c", options->volumeMesh, &options->volumeMesh, NULL));
23   PetscOptionsEnd();
24   PetscFunctionReturn(0);
25 }
26 
27 static PetscErrorCode ComputeVolume(DM dm) {
28   PetscObject obj = (PetscObject)dm;
29   DMLabel     bodyLabel, faceLabel, edgeLabel;
30   double      surface = 0., volume = 0., vol;
31   PetscInt    dim, pStart, pEnd, p, pid;
32   const char *name;
33 
34   PetscFunctionBeginUser;
35   PetscCall(DMGetDimension(dm, &dim));
36   if (dim < 2) PetscFunctionReturn(0);
37   PetscCall(DMGetLabel(dm, "EGADS Body ID", &bodyLabel));
38   PetscCall(DMGetLabel(dm, "EGADS Face ID", &faceLabel));
39   PetscCall(DMGetLabel(dm, "EGADS Edge ID", &edgeLabel));
40 
41   PetscCall(DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd));
42   for (p = pStart; p < pEnd; ++p) {
43     PetscCall(DMLabelGetValue(dim == 2 ? faceLabel : bodyLabel, p, &pid));
44     if (pid >= 0) {
45       PetscCall(DMPlexComputeCellGeometryFVM(dm, p, &vol, NULL, NULL));
46       volume += vol;
47     }
48   }
49   PetscCall(DMPlexGetHeightStratum(dm, 1, &pStart, &pEnd));
50   for (p = pStart; p < pEnd; ++p) {
51     PetscCall(DMLabelGetValue(dim == 2 ? edgeLabel : faceLabel, p, &pid));
52     if (pid >= 0) {
53       PetscCall(DMPlexComputeCellGeometryFVM(dm, p, &vol, NULL, NULL));
54       surface += vol;
55     }
56   }
57 
58   PetscCall(PetscObjectGetName(obj, &name));
59   PetscCall(PetscPrintf(PetscObjectComm(obj), "DM %s: Surface Area = %.6e Volume = %.6e\n", name ? name : "", surface, volume));
60   PetscFunctionReturn(0);
61 }
62 
63 int main(int argc, char *argv[]) {
64   DM     surface, dm;
65   AppCtx ctx;
66 
67   PetscFunctionBeginUser;
68   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
69   PetscCall(ProcessOptions(PETSC_COMM_WORLD, &ctx));
70   PetscCall(DMPlexCreateFromFile(PETSC_COMM_WORLD, ctx.filename, "ex37_plex", PETSC_TRUE, &surface));
71   PetscCall(PetscObjectSetName((PetscObject)surface, "CAD Surface"));
72   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)surface, "sur_"));
73   PetscCall(DMSetFromOptions(surface));
74   PetscCall(DMViewFromOptions(surface, NULL, "-dm_view"));
75   PetscCall(ComputeVolume(surface));
76 
77   if (ctx.volumeMesh) {
78     PetscCall(DMPlexGenerate(surface, "tetgen", PETSC_TRUE, &dm));
79     PetscCall(PetscObjectSetName((PetscObject)dm, "CAD Mesh"));
80     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
81     PetscCall(DMViewFromOptions(dm, NULL, "-pre_dm_view"));
82 
83     PetscCall(DMPlexInflateToGeomModel(dm));
84     PetscCall(DMViewFromOptions(dm, NULL, "-inf_dm_view"));
85 
86     PetscCall(DMSetFromOptions(dm));
87     PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
88     PetscCall(ComputeVolume(dm));
89   }
90 
91   PetscCall(DMDestroy(&dm));
92   PetscCall(DMDestroy(&surface));
93   PetscCall(PetscFinalize());
94   return 0;
95 }
96 
97 /*TEST
98 
99   build:
100     requires: egads tetgen
101 
102   test:
103     suffix: sphere_0
104     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/unit_sphere.egadslite -dm_refine 1 -sur_dm_view -dm_plex_check_all -dm_plex_egads_print_model -sur_dm_plex_view_labels "EGADS Body ID","EGADS Face ID","EGADS Edge ID"
105 
106   test:
107     suffix: sphere_egads
108     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/unit_sphere.egads -dm_refine 1 -sur_dm_view -dm_plex_check_all -dm_plex_egads_print_model -sur_dm_plex_view_labels "EGADS Body ID","EGADS Face ID","EGADS Edge ID"
109 
110   test:
111     suffix: sphere_iges
112     requires: broken
113     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/unit_sphere.igs -dm_refine 1 -sur_dm_view -dm_plex_check_all -dm_plex_egads_print_model -sur_dm_plex_view_labels "EGADS Body ID","EGADS Face ID","EGADS Edge ID"
114 
115   test:
116     suffix: sphere_step
117     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/unit_sphere.stp -dm_refine 1 -sur_dm_view -dm_plex_check_all -dm_plex_egads_print_model -sur_dm_plex_view_labels "EGADS Body ID","EGADS Face ID","EGADS Edge ID"
118 
119   test:
120     suffix: nozzle_0
121     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/nozzle.egadslite -sur_dm_refine 1 -sur_dm_view -dm_plex_check_all
122 
123   test:
124     suffix: nozzle_egads
125     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/nozzle.egads -sur_dm_refine 1 -sur_dm_view -dm_plex_check_all
126 
127   test:
128     suffix: nozzle_iges
129     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/nozzle.igs -sur_dm_refine 1 -sur_dm_view -dm_plex_check_all
130 
131   test:
132     suffix: nozzle_step
133     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/nozzle.stp -sur_dm_refine 1 -sur_dm_view -dm_plex_check_all
134 
135 TEST*/
136