Coverage for fiqus/parsers/ParserMSH.py: 96%

26 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2024-05-20 03:24 +0200

1import gmsh 

2import statistics 

3 

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 

11 

12 self._mesh_format_markers = {'s': '$MeshFormat', 'e': '$EndMeshFormat'} 

13 self._physical_name_markers = {'s': 'PhysicalNames', 'e': '$EndPhysicalNames'} 

14 

15 with open(mesh_file_path) as f: 

16 self._contents = f.read() 

17 

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'])] 

23 

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) 

30 

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)) 

36 

37 gmsh.finalize() 

38 

39 return lowestQuality 

40 

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) 

47 

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)