Coverage for fiqus/parsers/ParserGetDPTimeTable.py: 9%
54 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-12-27 02:39 +0100
« prev ^ index » next coverage.py v7.4.4, created at 2024-12-27 02:39 +0100
1import re
2import math
5class ParserGetDPTimeTable:
6 """
7 This class parses GetDP's TimeTable format output files.
8 """
10 def __init__(self, filePath):
11 self.time_values = []
12 self.values = []
13 # Parse data:
14 with open(filePath) as file:
15 # If the first line starts with #, we skip it.
16 first_line = file.readline()
17 if not first_line.startswith("#"):
18 number_of_entries = len(
19 re.findall(r"(-?\d+\.?\d*e?[-+]*\d*)", first_line)
20 )
21 # readline() moves the cursor to the next line, so we need to go back to
22 # the beginning of the file.
23 file.seek(0)
24 else:
25 second_line = file.readline()
26 number_of_entries = len(
27 re.findall(r"(-?\d+\.?\d*e?[-+]*\d*)", second_line)
28 )
29 # Seek to the second line
30 file.seek(len(first_line) + 1)
32 data = file.read()
34 entries = re.findall(r"(-?\d+\.?\d*e?[-+]*\d*)", data)
35 if number_of_entries == 2:
36 # Global scalar value:
37 time_index = 0
38 value_index = 1
39 self.data_type = "scalar"
40 elif number_of_entries == 6:
41 # Local scalar value probed at a point:
42 time_index = 1
43 value_index = 5
44 self.data_type = "scalar"
45 elif number_of_entries == 8:
46 # Local vector value probed at a point:
47 time_index = 1
48 value_index = [5, 6, 7]
49 self.data_type = "vector"
50 elif number_of_entries == 14:
51 # Local tensor value probed at a point:
52 time_index = 1
53 value_index = [[5, 6, 7], [8, 9, 10], [11, 12, 13]]
54 self.data_type = "tensor"
55 else:
56 raise ValueError(f"{filePath} contains an unexpected type of data.")
58 # Pack entries for each line:
59 entries = [
60 entries[i : i + number_of_entries]
61 for i in range(0, len(entries), number_of_entries)
62 ]
64 for entry in entries:
65 if self.data_type == "scalar":
66 self.time_values.append(float(entry[time_index]))
67 self.values.append(float(entry[value_index]))
68 elif self.data_type == "vector":
69 self.time_values.append(float(entry[time_index]))
70 self.values.append(
71 (
72 float(entry[value_index[0]]),
73 float(entry[value_index[1]]),
74 float(entry[value_index[2]]),
75 )
76 )
77 elif self.data_type == "tensor":
78 self.time_values.append(float(entry[time_index]))
79 self.values.append(
80 [
81 [
82 float(entry[value_index[0][0]]),
83 float(entry[value_index[0][1]]),
84 float(entry[value_index[0][2]]),
85 ],
86 [
87 float(entry[value_index[1][0]]),
88 float(entry[value_index[1][1]]),
89 float(entry[value_index[1][2]]),
90 ],
91 [
92 float(entry[value_index[2][0]]),
93 float(entry[value_index[2][1]]),
94 float(entry[value_index[2][2]]),
95 ],
96 ]
97 )
99 def get_equivalent_scalar_values(self):
100 """
101 Returns the same scalar if self.data_type is scalar.
102 Returns the magnitude of the vectors if self.data_type is vector.
103 Returns the von misses equivalents of the tensors if self.data_type is tensor.
104 """
106 if self.data_type == "scalar":
107 return self.values
108 elif self.data_type == "vector":
109 magnitudes = [
110 math.sqrt(v[0] ** 2 + v[1] ** 2 + v[2] ** 2)
111 for v in self.values
112 ]
113 return magnitudes
114 elif self.data_type == "tensor":
115 von_misses_equivalents = [
116 math.sqrt(
117 0.5
118 * (
119 (v[0][0] - v[1][1]) ** 2
120 + (v[1][1] - v[2][2]) ** 2
121 + (v[2][2] - v[0][0]) ** 2
122 + 6
123 * (
124 ((v[0][1] + v[1][0]) / 2) ** 2
125 + ((v[1][2] + v[2][1]) / 2) ** 2
126 + ((v[0][2] + v[2][0]) / 2) ** 2
127 )
128 )
129 )
130 for v in self.values
131 ]
132 return von_misses_equivalents
133 else:
134 raise RuntimeError("Data type not recognized.")