1 static char help[] = "Tests DMPlex Gmsh reader.\n\n"; 2 3 #include <petscdmplex.h> 4 5 #if !defined(PETSC_GMSH_EXE) 6 #define PETSC_GMSH_EXE "gmsh" 7 #endif 8 9 #include <petscds.h> 10 11 static void one(PetscInt dim, PetscInt Nf, PetscInt NfAux, 12 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 13 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 14 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar value[]) 15 { 16 value[0] = (PetscReal)1; 17 } 18 19 static PetscErrorCode CreateFE(DM dm) 20 { 21 DM cdm; 22 PetscSpace P; 23 PetscDualSpace Q; 24 DM K; 25 PetscFE fe; 26 DMPolytopeType ptype; 27 28 PetscInt dim,k; 29 PetscBool isSimplex; 30 31 PetscDS ds; 32 33 PetscFunctionBeginUser; 34 PetscCall(DMGetCoordinateDM(dm, &cdm)); 35 PetscCall(DMGetField(cdm, 0, NULL, (PetscObject*) &fe)); 36 PetscCall(PetscFEGetBasisSpace(fe, &P)); 37 PetscCall(PetscFEGetDualSpace(fe, &Q)); 38 PetscCall(PetscDualSpaceGetDM(Q,&K)); 39 PetscCall(DMGetDimension(K,&dim)); 40 PetscCall(PetscSpaceGetDegree(P, &k, NULL)); 41 PetscCall(DMPlexGetCellType(K, 0, &ptype)); 42 switch (ptype) { 43 case DM_POLYTOPE_QUADRILATERAL: 44 case DM_POLYTOPE_HEXAHEDRON: 45 isSimplex = PETSC_FALSE; break; 46 default: 47 isSimplex = PETSC_TRUE; break; 48 } 49 50 PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, 1, isSimplex, k, PETSC_DETERMINE, &fe)); 51 PetscCall(PetscFESetName(fe, "scalar")); 52 PetscCall(DMAddField(dm, NULL, (PetscObject) fe)); 53 PetscCall(PetscFEDestroy(&fe)); 54 PetscCall(DMCreateDS(dm)); 55 56 PetscCall(DMGetDS(dm, &ds)); 57 PetscCall(PetscDSSetObjective(ds, 0, one)); 58 PetscFunctionReturn(0); 59 } 60 61 static PetscErrorCode CheckIntegral(DM dm, PetscReal integral, PetscReal tol) 62 { 63 Vec u; 64 PetscReal rval; 65 PetscScalar result; 66 67 PetscFunctionBeginUser; 68 PetscCall(DMGetGlobalVector(dm, &u)); 69 PetscCall(DMPlexComputeIntegralFEM(dm, u, &result, NULL)); 70 PetscCall(DMRestoreGlobalVector(dm, &u)); 71 rval = PetscRealPart(result); 72 if (integral > 0 && PetscAbsReal(integral - rval) > tol) { 73 PetscCall(PetscPrintf(PetscObjectComm((PetscObject) dm), "Calculated value %g != %g actual value (error %g > %g tol)\n", 74 (double) rval, (double) integral, (double) PetscAbsReal(integral - rval), (double) tol)); 75 } 76 PetscFunctionReturn(0); 77 } 78 79 int main(int argc, char **argv) 80 { 81 DM dm; 82 const char *const mshlist[] = {"seg", "tri", "qua", "tet", "wed", "hex", 83 "vtx", "B2tri", "B2qua", "B3tet", "B3hex"}; 84 const char *const fmtlist[] = {"msh22", "msh40", "msh41"}; 85 PetscInt msh = 5; 86 PetscInt fmt = 2; 87 PetscBool bin = PETSC_TRUE; 88 PetscInt dim = 3; 89 PetscInt order = 2; 90 91 const char cmdtemplate[] = "%s -format %s %s -%d -order %d %s -o %s"; 92 char gmsh[PETSC_MAX_PATH_LEN] = PETSC_GMSH_EXE; 93 char tag[PETSC_MAX_PATH_LEN], path[PETSC_MAX_PATH_LEN]; 94 char geo[PETSC_MAX_PATH_LEN], geodir[PETSC_MAX_PATH_LEN] = "."; 95 char out[PETSC_MAX_PATH_LEN], outdir[PETSC_MAX_PATH_LEN] = "."; 96 char cmd[PETSC_MAX_PATH_LEN*4]; 97 PetscBool set,flg; 98 FILE *fp; 99 100 PetscFunctionBeginUser; 101 PetscCall(PetscInitialize(&argc, &argv, NULL, help)); 102 103 PetscCall(PetscStrncpy(geodir, "${PETSC_DIR}/share/petsc/datafiles/meshes", sizeof(geodir))); 104 PetscCall(PetscOptionsGetenv(PETSC_COMM_SELF, "GMSH", path, sizeof(path), &set)); 105 if (set) PetscCall(PetscStrncpy(gmsh, path, sizeof(gmsh))); 106 PetscCall(PetscOptionsGetString(NULL, NULL, "-gmsh", gmsh, sizeof(gmsh), NULL)); 107 PetscCall(PetscOptionsGetString(NULL, NULL, "-dir", geodir, sizeof(geodir), NULL)); 108 PetscCall(PetscOptionsGetString(NULL, NULL, "-out", outdir, sizeof(outdir), NULL)); 109 PetscCall(PetscOptionsGetEList(NULL, NULL, "-msh", mshlist, PETSC_STATIC_ARRAY_LENGTH(mshlist), &msh, NULL)); 110 PetscCall(PetscOptionsGetEList(NULL, NULL, "-fmt", fmtlist, PETSC_STATIC_ARRAY_LENGTH(fmtlist), &fmt, NULL)); 111 PetscCall(PetscOptionsGetBool(NULL, NULL, "-bin", &bin, NULL)); 112 PetscCall(PetscOptionsGetInt(NULL, NULL, "-dim", &dim, NULL)); 113 PetscCall(PetscOptionsGetInt(NULL, NULL, "-order", &order, NULL)); 114 if (fmt == 1) bin = PETSC_FALSE; /* Recent Gmsh releases cannot generate msh40+binary format*/ 115 116 { /* This test requires Gmsh >= 4.2.0 */ 117 char space[PETSC_MAX_PATH_LEN]; 118 int inum = 0, major = 0, minor = 0, micro = 0; 119 PetscCall(PetscSNPrintf(cmd, sizeof(cmd), "%s -info", gmsh)); 120 PetscCall(PetscPOpen(PETSC_COMM_SELF, NULL, cmd, "r", &fp)); 121 if (fp) {inum = fscanf(fp, "Version %s %d.%d.%d", space, &major, &minor, µ);} 122 PetscCall(PetscPClose(PETSC_COMM_SELF, fp)); 123 if (inum != 4 || major < 4 || (major == 4 && minor < 2)) { 124 PetscCall(PetscPrintf(PETSC_COMM_SELF, "Gmsh>=4.2.0 not available\n")); goto finish; 125 } 126 } 127 128 PetscCall(PetscSNPrintf(tag, sizeof(tag), "%s-%d-%d-%s%s", mshlist[msh], (int)dim, (int)order, fmtlist[fmt], bin?"-bin":"")); 129 PetscCall(PetscSNPrintf(geo, sizeof(geo), "%s/gmsh-%s.geo", geodir, mshlist[msh])); 130 PetscCall(PetscSNPrintf(out, sizeof(out), "%s/mesh-%s.msh", outdir, tag)); 131 PetscCall(PetscStrreplace(PETSC_COMM_SELF, geo, path, sizeof(path))); 132 PetscCall(PetscFixFilename(path, geo)); 133 PetscCall(PetscStrreplace(PETSC_COMM_SELF, out, path, sizeof(path))); 134 PetscCall(PetscFixFilename(path, out)); 135 PetscCall(PetscTestFile(geo, 'r', &flg)); 136 PetscCheck(flg,PETSC_COMM_SELF, PETSC_ERR_USER_INPUT, "File not found: %s", geo); 137 138 PetscCall(PetscSNPrintf(cmd, sizeof(cmd), cmdtemplate, gmsh, fmtlist[fmt], bin?"-bin":"", (int)dim, (int)order, geo, out)); 139 PetscCall(PetscPOpen(PETSC_COMM_SELF, NULL, cmd, "r", &fp)); 140 PetscCall(PetscPClose(PETSC_COMM_SELF, fp)); 141 142 PetscCall(DMPlexCreateFromFile(PETSC_COMM_SELF, out, "ex99_plex", PETSC_TRUE, &dm)); 143 PetscCall(PetscSNPrintf(tag, sizeof(tag), "mesh-%s", mshlist[msh])); 144 PetscCall(PetscObjectSetName((PetscObject)dm, tag)); 145 PetscCall(DMSetFromOptions(dm)); 146 PetscCall(DMViewFromOptions(dm, NULL, "-dm_view")); 147 { 148 PetscBool check; 149 PetscReal integral = 0, tol = (PetscReal)1.0e-4; 150 PetscCall(PetscOptionsGetReal(NULL, NULL, "-integral", &integral, &check)); 151 PetscCall(PetscOptionsGetReal(NULL, NULL, "-tol", &tol, NULL)); 152 if (check) { 153 PetscCall(CreateFE(dm)); 154 PetscCall(CheckIntegral(dm, integral, tol)); 155 } 156 } 157 PetscCall(DMDestroy(&dm)); 158 159 finish: 160 PetscCall(PetscFinalize()); 161 return 0; 162 } 163 164 /*TEST 165 166 build: 167 requires: defined(PETSC_HAVE_POPEN) 168 169 test: 170 requires: defined(PETSC_GMSH_EXE) 171 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 172 args: -msh {{vtx}separate_output} 173 args: -order 1 174 args: -fmt {{msh22 msh40 msh41}} -bin {{0 1}} 175 args: -dm_view ::ascii_info_detail 176 args: -dm_plex_check_all 177 args: -dm_plex_gmsh_highorder false 178 args: -dm_plex_boundary_label marker 179 args: -dm_plex_gmsh_spacedim 3 180 181 test: 182 requires: defined(PETSC_GMSH_EXE) 183 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 184 args: -msh {{seg tri qua tet wed hex}separate_output} 185 args: -order {{1 2 3 7}} 186 args: -fmt {{msh22 msh40 msh41}} -bin {{0 1}} 187 args: -dm_view ::ascii_info_detail 188 args: -dm_plex_check_all 189 args: -dm_plex_gmsh_highorder false 190 args: -dm_plex_boundary_label marker 191 192 testset: 193 suffix: B2 # 2D ball 194 requires: defined(PETSC_GMSH_EXE) 195 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 196 args: -msh {{B2tri B2qua}} 197 args: -dim 2 -integral 3.141592653589793 # pi 198 args: -order {{2 3 4 5 6 7 8 9}} -tol 0.05 199 200 testset: 201 suffix: B2_bnd # 2D ball boundary 202 requires: defined(PETSC_GMSH_EXE) 203 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 204 args: -dm_plex_gmsh_spacedim 2 205 args: -msh {{B2tri B2qua}} 206 args: -dim 1 -integral 6.283185307179586 # 2*pi 207 args: -order {{2 3 4 5 6 7 8 9}} -tol 0.05 208 209 testset: 210 suffix: B3 # 3D ball 211 requires: defined(PETSC_GMSH_EXE) 212 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 213 args: -msh {{B3tet B3hex}} 214 args: -dim 3 -integral 4.1887902047863905 # 4/3*pi 215 args: -order {{2 3 4 5}} -tol 0.20 216 217 testset: 218 suffix: B3_bnd # 3D ball boundary 219 requires: defined(PETSC_GMSH_EXE) 220 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 221 args: -dm_plex_gmsh_spacedim 3 222 args: -msh {{B3tet B3hex}} 223 args: -dim 2 -integral 12.566370614359172 # 4*pi 224 args: -order {{2 3 4 5 6 7 8 9}} -tol 0.25 225 226 testset: 227 suffix: B_lin # linear discretizations 228 requires: defined(PETSC_GMSH_EXE) 229 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 230 args: -dm_plex_gmsh_highorder true 231 args: -dm_plex_gmsh_project true 232 args: -dm_plex_gmsh_project_petscspace_degree {{1 2 3}separate_output} 233 args: -dm_plex_gmsh_fe_view 234 args: -dm_plex_gmsh_project_fe_view 235 args: -order 1 -tol 1e-4 236 test: 237 suffix: dim-1 238 args: -dm_plex_gmsh_spacedim 2 239 args: -msh {{B2tri B2qua}separate_output} 240 args: -dim 1 -integral 5.656854249492381 # 4*sqrt(2) 241 test: 242 suffix: dim-2 243 args: -dm_plex_gmsh_spacedim 2 244 args: -msh {{B2tri B2qua}separate_output} 245 args: -dim 2 -integral 2.000000000000000 # 2 246 test: 247 suffix: dim-2_msh-B3tet 248 args: -dm_plex_gmsh_spacedim 3 249 args: -msh B3tet -dim 2 -integral 9.914478 250 test: 251 suffix: dim-2_msh-B3hex 252 args: -dm_plex_gmsh_spacedim 3 253 args: -msh B3hex -dim 2 -integral 8.000000 254 test: 255 suffix: dim-3_msh-B3tet 256 args: -dm_plex_gmsh_spacedim 3 257 args: -msh B3tet -dim 3 -integral 2.666649 258 test: 259 suffix: dim-3_msh-B3hex 260 args: -dm_plex_gmsh_spacedim 3 261 args: -msh B3hex -dim 3 -integral 1.539600 262 263 TEST*/ 264