Coverage for mpcforces_extractor\force_extractor.py: 79%

42 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-01 11:08 +0100

1import os 

2import time 

3from typing import Optional 

4from mpcforces_extractor.reader.modelreaders import FemFileReader 

5from mpcforces_extractor.reader.mpcforces_reader import MPCForcesReader 

6from mpcforces_extractor.datastructure.entities import Element 

7from mpcforces_extractor.datastructure.rigids import MPC 

8from mpcforces_extractor.datastructure.subcases import Subcase 

9 

10 

11class MPCForceExtractor: 

12 """ 

13 This class is used to extract the forces from the MPC forces file 

14 and calculate the forces for each rigid element by property 

15 """ 

16 

17 def __init__( 

18 self, fem_file_path, mpc_file_path, output_folder: Optional[str] = None 

19 ): 

20 self.fem_file_path: str = fem_file_path 

21 self.mpc_file_path: str = mpc_file_path 

22 self.output_folder: str = output_folder 

23 self.reader: FemFileReader = None 

24 self.mpc_forces_reader = None 

25 self.subcases = [] 

26 # reset the graph (very important) and the MPCs 

27 Element.reset_graph() 

28 MPC.reset() 

29 

30 if output_folder: 

31 # create output folder if it does not exist, otherwise delete the content 

32 if os.path.exists(output_folder): 

33 for file in os.listdir(output_folder): 

34 file_path = os.path.join(output_folder, file) 

35 try: 

36 if os.path.isfile(file_path): 

37 os.unlink(file_path) 

38 except Exception as e: 

39 print(e) 

40 else: 

41 os.makedirs(output_folder, exist_ok=True) 

42 

43 def build_fem_and_subcase_data(self, block_size: int) -> None: 

44 """ 

45 This method reads the FEM File and the MPCF file and extracts the forces 

46 in a dictory with the rigid element as the key and the property2forces dict as the value 

47 """ 

48 self.mpc_forces_reader = MPCForcesReader(self.mpc_file_path) 

49 self.mpc_forces_reader.build_subcases() 

50 self.subcases = Subcase.subcases 

51 

52 self.reader = FemFileReader(self.fem_file_path, block_size) 

53 print("Reading the FEM file") 

54 start_time = time.time() 

55 self.reader.create_entities() 

56 

57 print("..took ", round(time.time() - start_time, 2), "seconds") 

58 print("Building the mpcs") 

59 start_time = time.time() 

60 self.reader.get_rigid_elements() 

61 print("..took ", round(time.time() - start_time, 2), "seconds") 

62 

63 self.reader.get_loads()