Coverage for mpcforces_extractor\datastructure\subcases.py: 86%
29 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-07 22:34 +0100
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-07 22:34 +0100
1from typing import List
4class Subcase:
5 """
6 This class is used to store the subcase information
7 The purpose of this class is to make multiple subcases available
8 in the mpcforces_extractor
9 """
11 subcases = []
13 def __init__(self, subcase_id: int, time: float):
14 """
15 Constructor
16 """
17 self.subcase_id = subcase_id
18 self.time = time
19 self.node_id2forces = {}
20 Subcase.subcases.append(self)
22 def add_force(self, node_id: int, forces: List) -> None:
23 """
24 This method is used to add the forces for a node
25 """
26 self.node_id2forces[node_id] = forces
28 def get_sum_forces(self, node_ids: List) -> None:
29 """
30 This method is used to sum the forces for all nodes
31 """
32 sum_forces = [0, 0, 0, 0, 0, 0]
33 for node_id in node_ids:
34 if node_id not in self.node_id2forces:
35 print(f"Node {node_id} not found in mpcf, setting to 0.")
36 continue
37 forces = self.node_id2forces[node_id]
38 sum_forces = [sf + f for sf, f in zip(sum_forces, forces)]
39 return sum_forces
41 @staticmethod
42 def get_subcase_by_id(subcase_id: int):
43 """
44 This method is used to get a subcase by its id
45 """
46 for subcase in Subcase.subcases:
47 if subcase.subcase_id == subcase_id:
48 return subcase
49 print(f"No Subcase with id {id} was found.")
50 return None
52 @staticmethod
53 def reset():
54 """
55 This method is used to reset the subcases list
56 """
57 Subcase.subcases = []