Coverage for mpcforces_extractor\reader\mpcforces_reader.py: 94%
48 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-06 21:34 +0100
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-06 21:34 +0100
1from typing import List
2from mpcforces_extractor.datastructure.subcases import Subcase
5class MPCForcesReader:
6 """
7 Class to read the MPC forces file and extract the forces for each node
8 """
10 file_path: str = None
11 file_content: str = None
13 def __init__(self, file_path):
14 self.file_path = file_path
15 self.file_content = self.__read_lines()
16 self.node_ids = []
18 def __read_lines(self) -> List[str]:
19 """
20 This method reads the lines of the MPC forces file
21 """
22 with open(self.file_path, "r", encoding="utf-8") as file:
23 return file.readlines()
24 return []
26 def build_subcases(self) -> None:
27 """
28 This method is used to extract the forces from the MPC forces file
29 and build the subcases
30 """
31 subcase_id = 0
32 time = 0
33 Subcase.reset()
34 subcase = None
35 for i, _ in enumerate(self.file_content):
36 line = self.file_content[i].strip()
38 if line.startswith("$SUBCASE"):
39 subcase_id = int(line.replace("$SUBCASE", "").strip())
40 if line.startswith("$TIME"):
41 time = float(line.replace("$TIME", "").strip())
42 subcase = Subcase(subcase_id, time)
44 if "X-FORCE" in line:
45 i += 2
46 line = self.file_content[i].strip()
47 while (
48 not self.file_content[i].startswith("---")
49 and not self.file_content[i].strip() == ""
50 ):
51 line = self.file_content[i]
53 # take the first 8 characters as the node id
54 node_id = int(line[:8].strip())
56 # take the next 13 characters as the force values
57 n = 13
58 line_content = [line[j : j + n] for j in range(8, len(line), n)]
59 line_content = line_content[:-1]
60 for j, _ in enumerate(line_content):
61 line_content[j] = line_content[j].strip()
63 if len(line_content) < 6:
64 line_content += [""] * (6 - len(line_content))
66 force_x = float(line_content[0]) if line_content[0] != "" else 0
67 force_y = float(line_content[1]) if line_content[1] != "" else 0
68 force_z = float(line_content[2]) if line_content[2] != "" else 0
69 moment_x = float(line_content[3]) if line_content[3] != "" else 0
70 moment_y = float(line_content[4]) if line_content[4] != "" else 0
71 moment_z = float(line_content[5]) if line_content[5] != "" else 0
73 force = [force_x, force_y, force_z, moment_x, moment_y, moment_z]
75 subcase.add_force(node_id, force)
76 self.node_ids.append(node_id)
78 i += 1