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 PetscCall(PetscInitialize(&argc, &argv, NULL, help)); 101 102 PetscCall(PetscStrncpy(geodir, "${PETSC_DIR}/share/petsc/datafiles/meshes", sizeof(geodir))); 103 PetscCall(PetscOptionsGetenv(PETSC_COMM_SELF, "GMSH", path, sizeof(path), &set)); 104 if (set) PetscCall(PetscStrncpy(gmsh, path, sizeof(gmsh))); 105 PetscCall(PetscOptionsGetString(NULL, NULL, "-gmsh", gmsh, sizeof(gmsh), NULL)); 106 PetscCall(PetscOptionsGetString(NULL, NULL, "-dir", geodir, sizeof(geodir), NULL)); 107 PetscCall(PetscOptionsGetString(NULL, NULL, "-out", outdir, sizeof(outdir), NULL)); 108 PetscCall(PetscOptionsGetEList(NULL, NULL, "-msh", mshlist, PETSC_STATIC_ARRAY_LENGTH(mshlist), &msh, NULL)); 109 PetscCall(PetscOptionsGetEList(NULL, NULL, "-fmt", fmtlist, PETSC_STATIC_ARRAY_LENGTH(fmtlist), &fmt, NULL)); 110 PetscCall(PetscOptionsGetBool(NULL, NULL, "-bin", &bin, NULL)); 111 PetscCall(PetscOptionsGetInt(NULL, NULL, "-dim", &dim, NULL)); 112 PetscCall(PetscOptionsGetInt(NULL, NULL, "-order", &order, NULL)); 113 if (fmt == 1) bin = PETSC_FALSE; /* Recent Gmsh releases cannot generate msh40+binary format*/ 114 115 { /* This test requires Gmsh >= 4.2.0 */ 116 int inum = 0, major = 0, minor = 0, micro = 0; 117 PetscCall(PetscSNPrintf(cmd, sizeof(cmd), "%s -info", gmsh)); 118 PetscCall(PetscPOpen(PETSC_COMM_SELF, NULL, cmd, "r", &fp)); 119 if (fp) {inum = fscanf(fp, "Version : %d.%d.%d", &major, &minor, µ);} 120 PetscCall(PetscPClose(PETSC_COMM_SELF, fp)); 121 if (inum != 3 || major < 4 || (major == 4 && minor < 2)) { 122 PetscCall(PetscPrintf(PETSC_COMM_SELF, "Gmsh>=4.2.0 not available\n")); goto finish; 123 } 124 } 125 126 PetscCall(PetscSNPrintf(tag, sizeof(tag), "%s-%d-%d-%s%s", mshlist[msh], (int)dim, (int)order, fmtlist[fmt], bin?"-bin":"")); 127 PetscCall(PetscSNPrintf(geo, sizeof(geo), "%s/gmsh-%s.geo", geodir, mshlist[msh])); 128 PetscCall(PetscSNPrintf(out, sizeof(out), "%s/mesh-%s.msh", outdir, tag)); 129 PetscCall(PetscStrreplace(PETSC_COMM_SELF, geo, path, sizeof(path))); 130 PetscCall(PetscFixFilename(path, geo)); 131 PetscCall(PetscStrreplace(PETSC_COMM_SELF, out, path, sizeof(path))); 132 PetscCall(PetscFixFilename(path, out)); 133 PetscCall(PetscTestFile(geo, 'r', &flg)); 134 PetscCheck(flg,PETSC_COMM_SELF, PETSC_ERR_USER_INPUT, "File not found: %s", geo); 135 136 PetscCall(PetscSNPrintf(cmd, sizeof(cmd), cmdtemplate, gmsh, fmtlist[fmt], bin?"-bin":"", (int)dim, (int)order, geo, out)); 137 PetscCall(PetscPOpen(PETSC_COMM_SELF, NULL, cmd, "r", &fp)); 138 PetscCall(PetscPClose(PETSC_COMM_SELF, fp)); 139 140 PetscCall(DMPlexCreateFromFile(PETSC_COMM_SELF, out, "ex99_plex", PETSC_TRUE, &dm)); 141 PetscCall(PetscSNPrintf(tag, sizeof(tag), "mesh-%s", mshlist[msh])); 142 PetscCall(PetscObjectSetName((PetscObject)dm, tag)); 143 PetscCall(DMViewFromOptions(dm, NULL, "-dm_view")); 144 PetscCall(DMSetFromOptions(dm)); 145 { 146 PetscBool check; 147 PetscReal integral = 0, tol = (PetscReal)1.0e-4; 148 PetscCall(PetscOptionsGetReal(NULL, NULL, "-integral", &integral, &check)); 149 PetscCall(PetscOptionsGetReal(NULL, NULL, "-tol", &tol, NULL)); 150 if (check) { 151 PetscCall(CreateFE(dm)); 152 PetscCall(CheckIntegral(dm, integral, tol)); 153 } 154 } 155 PetscCall(DMDestroy(&dm)); 156 157 finish: 158 PetscCall(PetscFinalize()); 159 return 0; 160 } 161 162 /*TEST 163 164 build: 165 requires: defined(PETSC_HAVE_POPEN) 166 167 test: 168 requires: defined(PETSC_GMSH_EXE) 169 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 170 args: -msh {{vtx}separate_output} 171 args: -order 1 172 args: -fmt {{msh22 msh40 msh41}} -bin {{0 1}} 173 args: -dm_view ::ascii_info_detail 174 args: -dm_plex_check_all 175 args: -dm_plex_gmsh_highorder false 176 args: -dm_plex_gmsh_use_marker true 177 args: -dm_plex_gmsh_spacedim 3 178 179 test: 180 requires: defined(PETSC_GMSH_EXE) 181 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 182 args: -msh {{seg tri qua tet wed hex}separate_output} 183 args: -order {{1 2 3 7}} 184 args: -fmt {{msh22 msh40 msh41}} -bin {{0 1}} 185 args: -dm_view ::ascii_info_detail 186 args: -dm_plex_check_all 187 args: -dm_plex_gmsh_highorder false 188 args: -dm_plex_gmsh_use_marker true 189 190 testset: 191 suffix: B2 # 2D ball 192 requires: defined(PETSC_GMSH_EXE) 193 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 194 args: -msh {{B2tri B2qua}} 195 args: -dim 2 -integral 3.141592653589793 # pi 196 args: -order {{2 3 4 5 6 7 8 9}} -tol 0.05 197 198 testset: 199 suffix: B2_bnd # 2D ball boundary 200 requires: defined(PETSC_GMSH_EXE) 201 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 202 args: -dm_plex_gmsh_spacedim 2 203 args: -msh {{B2tri B2qua}} 204 args: -dim 1 -integral 6.283185307179586 # 2*pi 205 args: -order {{2 3 4 5 6 7 8 9}} -tol 0.05 206 207 testset: 208 suffix: B3 # 3D ball 209 requires: defined(PETSC_GMSH_EXE) 210 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 211 args: -msh {{B3tet B3hex}} 212 args: -dim 3 -integral 4.1887902047863905 # 4/3*pi 213 args: -order {{2 3 4 5}} -tol 0.20 214 215 testset: 216 suffix: B3_bnd # 3D ball boundary 217 requires: defined(PETSC_GMSH_EXE) 218 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 219 args: -dm_plex_gmsh_spacedim 3 220 args: -msh {{B3tet B3hex}} 221 args: -dim 2 -integral 12.566370614359172 # 4*pi 222 args: -order {{2 3 4 5 6 7 8 9}} -tol 0.25 223 224 testset: 225 suffix: B_lin # linear discretizations 226 requires: defined(PETSC_GMSH_EXE) 227 args: -dir ${wPETSC_DIR}/share/petsc/datafiles/meshes 228 args: -dm_plex_gmsh_highorder true 229 args: -dm_plex_gmsh_project true 230 args: -dm_plex_gmsh_project_petscspace_degree {{1 2 3}separate_output} 231 args: -dm_plex_gmsh_fe_view 232 args: -dm_plex_gmsh_project_fe_view 233 args: -order 1 -tol 1e-4 234 test: 235 suffix: dim-1 236 args: -dm_plex_gmsh_spacedim 2 237 args: -msh {{B2tri B2qua}separate_output} 238 args: -dim 1 -integral 5.656854249492381 # 4*sqrt(2) 239 test: 240 suffix: dim-2 241 args: -dm_plex_gmsh_spacedim 2 242 args: -msh {{B2tri B2qua}separate_output} 243 args: -dim 2 -integral 2.000000000000000 # 2 244 test: 245 suffix: dim-2_msh-B3tet 246 args: -dm_plex_gmsh_spacedim 3 247 args: -msh B3tet -dim 2 -integral 9.914478 248 test: 249 suffix: dim-2_msh-B3hex 250 args: -dm_plex_gmsh_spacedim 3 251 args: -msh B3hex -dim 2 -integral 8.000000 252 test: 253 suffix: dim-3_msh-B3tet 254 args: -dm_plex_gmsh_spacedim 3 255 args: -msh B3tet -dim 3 -integral 2.666649 256 test: 257 suffix: dim-3_msh-B3hex 258 args: -dm_plex_gmsh_spacedim 3 259 args: -msh B3hex -dim 3 -integral 1.539600 260 261 TEST*/ 262