Coverage for fiqus/parsers/ParserMSH.py: 96%
26 statements
« prev ^ index » next coverage.py v7.4.4, created at 2025-01-14 02:37 +0100
« prev ^ index » next coverage.py v7.4.4, created at 2025-01-14 02:37 +0100
1import gmsh
2import statistics
4class ParserMSH:
5 def __init__(self, mesh_file_path):
6 """
7 Read msh file and returns mesh format and physical names as class attributes.
8 :param mesh_file_path: Full path to .msh file, including file name and extension.
9 """
10 self.mesh_file_path = mesh_file_path
12 self._mesh_format_markers = {'s': '$MeshFormat', 'e': '$EndMeshFormat'}
13 self._physical_name_markers = {'s': 'PhysicalNames', 'e': '$EndPhysicalNames'}
15 with open(mesh_file_path) as f:
16 self._contents = f.read()
18 def __get_content(self, markers_dict):
19 """
20 Gets text string between two markers specified in markers_dict
21 """
22 return self._contents[self._contents.find(markers_dict['s']) + len(markers_dict['s']):self._contents.find(markers_dict['e'])]
24 def get_average_mesh_quality(self):
25 """
26 Gets the lowest mesh quality from the mesh file
27 """
28 gmsh.initialize()
29 gmsh.open(self.mesh_file_path)
31 # SICN not implemented in 1D!
32 allElementsDim2 = gmsh.model.mesh.getElements(dim=2)[1]
33 allElementsDim3 = gmsh.model.mesh.getElements(dim=3)[1]
34 allElements = list(allElementsDim2[0]) + (list(allElementsDim3[0]) if allElementsDim3 else [])
35 lowestQuality = statistics.fmean(gmsh.model.mesh.getElementQualities(allElements))
37 gmsh.finalize()
39 return lowestQuality
41 @property
42 def mesh_format(self):
43 """
44 Parse mesh_generators field and assign it to the class attribute
45 """
46 return self.__get_content(self._mesh_format_markers)
48 @property
49 def physical_names(self):
50 """
51 Parse physical_names field and assign it to the class attribute
52 """
53 return self.__get_content(self._physical_name_markers)