Coverage for bim2sim/plugins/PluginOpenFOAM/bim2sim_openfoam/task/run_meshing.py: 0%

44 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-01 10:24 +0000

1from bim2sim.plugins.PluginOpenFOAM.bim2sim_openfoam.utils.openfoam_utils import \ 

2 OpenFOAMUtils 

3from bim2sim.tasks.base import ITask 

4import sys 

5import os 

6import logging 

7from butterfly import decomposeParDict as decPD 

8import pathlib 

9 

10logger = logging.getLogger(__name__) 

11 

12 

13class RunOpenFOAMMeshing(ITask): 

14 """This ITask runs the openfoam meshing on linux systems. 

15 

16 In case of error messages, please check the following hints, especially 

17 when working with Anaconda environments and PyCharm. 

18 - Check which version of OpenFOAM you have installed and if you can 

19 access it from the terminal (e.g. 'blockMesh'/'which blockMesh'). If the 

20 specified path is not the one you'd like to use, please add the OpenFOAM 

21 installation path to the PATH system variable. 

22 - Check which version of OpenMPI you have installed and if you can 

23 access it from the terminal (e.g. 'mpirun'/'which mpirun'). Make sure to 

24 not use the MPI included in Anaconda and adjust the path in the .bashrc 

25 accordingly or set a symbolic link. 

26 - For error messages like: 'error loading shared libraries', please make 

27 sure that PyCharm includes all system path variables, especially 

28 LD_LIBRARY_PATH by checking Run Configurations -> Environment Variables. 

29 If it is not included automatically, add it manually. The value must be 

30 the same as the output you get from 'echo $LD_LIBRARY_PATH'. 

31 """ 

32 

33 reads = ('openfoam_case', ) 

34 touches = () 

35 

36 def __init__(self, playground): 

37 super().__init__(playground) 

38 

39 def run(self, openfoam_case): 

40 if not self.playground.sim_settings.run_meshing: 

41 return 

42 

43 if not sys.platform == 'linux': 

44 logger.warning('Execution on non-Linux systems is not ' 

45 'recommended. Meshing aborted.') 

46 return 

47 

48 of_path = openfoam_case.openfoam_dir 

49 logger.warning("Meshing in progress. This will take several minutes.") 

50 # Use half of the available processes 

51 procs = os.cpu_count() 

52 # procs = round(procs / 4) * 2 

53 distrib = OpenFOAMUtils.split_into_three_factors(procs) 

54 

55 # Write updated distribution to decomposeParDict 

56 dPpath = of_path / 'system' / 'decomposeParDict' 

57 decomposeParDict = decPD.DecomposeParDict() 

58 decomposeParDict = decomposeParDict.from_file(dPpath) 

59 decomposeParDict.set_value_by_parameter('numberOfSubdomains', 

60 str(procs)) 

61 hc1 = decomposeParDict.get_value_by_parameter('hierarchicalCoeffs') 

62 distrib = '(' + str(distrib[0]) + ' ' + str(distrib[1]) + ' ' + \ 

63 str(distrib[2]) + ')' 

64 hc1['n'] = distrib 

65 decomposeParDict.set_value_by_parameter('hierarchicalCoeffs', hc1) 

66 decomposeParDict.save(project_folder=of_path) 

67 

68 # execution 

69 cwd = os.getcwd() 

70 os.chdir(of_path) 

71 os.system('pwd') 

72 # os.system('conda deactivate') 

73 os.system('blockMesh') 

74 os.system('decomposePar -force') 

75 logger.info('Writing snappyHexMesh output to file \'logMeshing\'.') 

76 os.system('mpiexec --oversubscribe -np ' + str(procs) + ' snappyHexMesh -parallel ' 

77 '-overwrite > logMeshing') 

78 os.system('reconstructParMesh -mergeTol 1e-10 -constant') 

79 os.system('topoSet') 

80 os.system('setsToZones') 

81 os.system('checkMesh') 

82 os.chdir(cwd) 

83