Actual source code: ex2.c

petsc-main 2021-04-20
Report Typos and Errors
  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, sizeof(options->filename), 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:     PetscMalloc1(1, &user->bcFuncs);
 61:     user->bcFuncs[0] = zero;
 62:     DMAddBoundary(*dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 0, 0, NULL, (void (*)(void)) user->bcFuncs[0], NULL, user, NULL);
 63:   } else {
 64:     DMPlexCreateFromFile(comm, user->filename, user->interpolate, dm);
 65:   }
 66:   PetscObjectSetName((PetscObject) *dm, "Mesh");
 67:   DMSetFromOptions(*dm);
 68:   DMViewFromOptions(*dm, NULL, "-dm_view");
 69:   return(0);
 70: }

 72: int main(int argc, char **argv)
 73: {
 74:   DM             dm;
 75:   AppCtx         user;

 78:   PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr;
 79:   ProcessOptions(PETSC_COMM_WORLD, &user);
 80:   CreateMesh(PETSC_COMM_WORLD, &user, &dm);
 81:   DMDestroy(&dm);
 82:   PetscFree(user.bcFuncs);
 83:   PetscFinalize();
 84:   return ierr;
 85: }

 87: /*TEST

 89:   testset:
 90:     args: -dm_plex_check_symmetry -dm_plex_check_skeleton -dm_plex_check_faces -dm_plex_check_geometry
 91:     # CGNS meshes 0-1
 92:     test:
 93:       suffix: 0
 94:       requires: cgns
 95:       args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/tut21.cgns
 96:     test:
 97:       suffix: 1
 98:       requires: cgns
 99:       TODO: broken
100:       args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/grid_c.cgns
101:     # Gmsh meshes 2-4
102:     test:
103:       suffix: 2
104:       requires: double
105:       args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/doublet-tet.msh
106:     test:
107:       suffix: 3
108:       requires: double
109:       args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/square.msh
110:     test:
111:       suffix: 4
112:       requires: double
113:       args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/square_bin.msh
114:     # Exodus meshes 5-9
115:     test:
116:       suffix: 5
117:       requires: exodusii
118:       args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/sevenside-quad.exo
119:     test:
120:       suffix: 6
121:       requires: exodusii
122:       args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/sevenside-quad-15.exo
123:     test:
124:       suffix: 7
125:       requires: exodusii
126:       args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/squaremotor-30.exo
127:     test:
128:       suffix: 8
129:       requires: exodusii
130:       args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/blockcylinder-50.exo
131:     test:
132:       suffix: 9
133:       requires: exodusii
134:      args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/simpleblock-100.exo

136: TEST*/