Actual source code: ex2.c
petsc-3.12.5 2020-03-29
1: static char help[] = "Read in a mesh and test whether it is valid\n\n";
3: #include <petscdmplex.h>
4: #if defined(PETSC_HAVE_CGNS)
5: #undef I /* Very old CGNS stupidly uses I as a variable, which fails when using complex. Curse you idiot package managers */
6: #include <cgnslib.h>
7: #endif
8: #if defined(PETSC_HAVE_EXODUSII)
9: #include <exodusII.h>
10: #endif
12: typedef struct {
13: PetscBool interpolate; /* Generate intermediate mesh elements */
14: char filename[PETSC_MAX_PATH_LEN]; /* Mesh filename */
15: PetscInt dim;
16: PetscErrorCode (**bcFuncs)(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx);
17: } AppCtx;
19: static PetscErrorCode zero(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx)
20: {
21: PetscInt i;
22: for (i = 0; i < dim; ++i) u[i] = 0.0;
23: return 0;
24: }
26: static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
27: {
31: options->interpolate = PETSC_TRUE;
32: options->filename[0] = '\0';
33: options->dim = 2;
34: options->bcFuncs = NULL;
36: PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMPLEX");
37: PetscOptionsBool("-interpolate", "Generate intermediate mesh elements", "ex2.c", options->interpolate, &options->interpolate, NULL);
38: PetscOptionsString("-filename", "The mesh file", "ex2.c", options->filename, options->filename, PETSC_MAX_PATH_LEN, NULL);
39: PetscOptionsRangeInt("-dim", "The dimension of problem used for non-file mesh", "ex2.c", options->dim, &options->dim, NULL,1,3);
40: PetscOptionsEnd();
41: return(0);
42: }
44: static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm)
45: {
46: size_t len;
50: PetscStrlen(user->filename, &len);
51: if (!len) {
52: DMLabel label;
53: PetscInt id = 1;
55: DMPlexCreateBoxMesh(comm, user->dim, PETSC_TRUE, NULL, NULL, NULL, NULL, user->interpolate, dm);
56: /* Mark boundary and set BC */
57: DMCreateLabel(*dm, "boundary");
58: DMGetLabel(*dm, "boundary", &label);
59: DMPlexMarkBoundaryFaces(*dm, 1, label);
60: DMPlexLabelComplete(*dm, label);
61: PetscMalloc1(1, &user->bcFuncs);
62: user->bcFuncs[0] = zero;
63: DMAddBoundary(*dm, DM_BC_ESSENTIAL, "wall", "boundary", 0, 0, NULL, (void (*)(void)) user->bcFuncs[0], 1, &id, user);
64: } else {
65: DMPlexCreateFromFile(comm, user->filename, user->interpolate, dm);
66: }
67: PetscObjectSetName((PetscObject) *dm, "Mesh");
68: DMSetFromOptions(*dm);
69: DMViewFromOptions(*dm, NULL, "-dm_view");
70: return(0);
71: }
73: int main(int argc, char **argv)
74: {
75: DM dm;
76: AppCtx user;
79: PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr;
80: ProcessOptions(PETSC_COMM_WORLD, &user);
81: CreateMesh(PETSC_COMM_WORLD, &user, &dm);
82: DMDestroy(&dm);
83: PetscFree(user.bcFuncs);
84: PetscFinalize();
85: return ierr;
86: }
88: /*TEST
90: testset:
91: args: -dm_plex_check_symmetry -dm_plex_check_skeleton -dm_plex_check_faces -dm_plex_check_geometry
92: # CGNS meshes 0-1
93: test:
94: suffix: 0
95: requires: cgns
96: TODO: broken
97: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/tut21.cgns
98: test:
99: suffix: 1
100: requires: cgns
101: TODO: broken
102: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/grid_c.cgns
103: # Gmsh meshes 2-4
104: test:
105: suffix: 2
106: requires: double
107: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/doublet-tet.msh
108: test:
109: suffix: 3
110: requires: double
111: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/square.msh
112: test:
113: suffix: 4
114: requires: double
115: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/square_bin.msh
116: # Exodus meshes 5-9
117: test:
118: suffix: 5
119: requires: exodusii
120: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/sevenside-quad.exo
121: test:
122: suffix: 6
123: requires: exodusii
124: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/sevenside-quad-15.exo
125: test:
126: suffix: 7
127: requires: exodusii
128: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/squaremotor-30.exo
129: test:
130: suffix: 8
131: requires: exodusii
132: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/blockcylinder-50.exo
133: test:
134: suffix: 9
135: requires: exodusii
136: args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/simpleblock-100.exo
138: TEST*/