Peano
Loading...
Searching...
No Matches
print_velocities.py
Go to the documentation of this file.
1import matplotlib.pyplot as plt
2import csv
3import argparse
4
5"Written by Marc Marot-Lassauzaie, I use this file to convert data from the .csv files generated by the parsers to somewhat legible plots"
6
7
8"""
9parse arguments to get details about which data should be extracted
10"""
11
12parser = argparse.ArgumentParser(
13 description="Script to convert CSV generated by ExaHyPE2 executions into plots"
14)
15parser.add_argument(
16 "--f",
17 dest="file_name",
18 type=str,
19 required=True,
20 help="Name of the file to be checked, does not need to contain '.csv'",
21)
22parser.add_argument(
23 "--v", dest="v", type=int, default=0, help="Index of the variable to be plotted"
24)
25parser.add_argument(
26 "--tmax",
27 dest="t_max",
28 type=float,
29 default=None,
30 help="Minimal time for which data should be plotted",
31)
32parser.add_argument(
33 "--tmin",
34 dest="t_min",
35 type=float,
36 default=0.01,
37 help="Maximal time for which data should be plotted",
38)
39parser.add_argument(
40 "--x",
41 dest="x",
42 type=float,
43 default=None,
44 help="x position of tracer which should be considered",
45)
46parser.add_argument(
47 "--y",
48 dest="y",
49 type=float,
50 default=None,
51 help="y position of tracer which should be considered",
52)
53parser.add_argument(
54 "--z",
55 dest="z",
56 type=float,
57 default=None,
58 help="z position of tracer which should be considered",
59)
60
61args = parser.parse_args()
62
63"""
64Extract some metadata about the positions of the various information in the csv file.
65These should always be the same but it's safer to check.
66"""
67try:
68 csvData = list(csv.reader(open(args.file_name, "r"), delimiter=","))
69except FileNotFoundError:
70 try:
71 csvData = list(csv.reader(open(args.file_name + ".csv", "r"), delimiter=","))
72 except:
73 print(
74 "the file you have specified does not exist, please make sure that it does in the specified location"
75 )
76 exit()
77
78
79timeIndex = csvData[0].index(" t")
80vxIndex = csvData[0].index(" data ") + args.v
81vyIndex = csvData[0].index(" data ") + args.v + 1
82vzIndex = csvData[0].index(" data ") + args.v + 2
83firstNumberIndex = csvData[0].index("number(0)")
84secondNumberIndex = csvData[0].index(" number(1)")
85dim = 2
86if " x(2)" in csvData[0]:
87 dim = 3
88 positionIndexes = [
89 csvData[0].index(" x(0)"),
90 csvData[0].index(" x(1)"),
91 csvData[0].index(" x(2)"),
92 ]
93else:
94 positionIndexes = [csvData[0].index(" x(0)"), csvData[0].index(" x(1)")]
95
96"""
97remove first row which should only contain metadata
98"""
99csvData = csvData[1:-1]
100
101
102"""
103Filter out only the data from tracers at the specified positions
104"""
105
106
108 value = True
109 if args.t_min != None:
110 value = value and float(a[timeIndex]) >= args.t_min
111 if args.t_max != None:
112 value = value and float(a[timeIndex]) <= args.t_max
113 if args.x != None:
114 value = value and float(a[positionIndexes[0]]) == args.x
115 if args.y != None:
116 value = value and float(a[positionIndexes[1]]) == args.y
117 if dim==3 and args.z != None:
118 value = value and float(a[positionIndexes[2]]) == args.z
119 return value
120
121
122csvData = [data for data in csvData if position_check(data)]
123
124
125"""
126Check how many tracers fit the filter by extracting the total numbers of number(0) and
127number(1) tuples that exist with the filter.
128The {number(0), number(1)} tuples should be unique for each tracer, therefore if there
129exists more than one value for either of these after filtering, there are multiple
130tracers that correspond to the filter.
131In the same way if there are none, none of the tracers correspond to the filter.
132"""
133checkFirstIndexLength = set([data[firstNumberIndex] for data in csvData])
134checksecondIndexLength = set([data[secondNumberIndex] for data in csvData])
135
136if len(checkFirstIndexLength) == 0:
137 raise Exception(
138 "Specification was valid for none of the tracers, please check that a tracer exists at the specified position"
139 )
140elif len(checkFirstIndexLength) > 1 or len(checksecondIndexLength) > 1:
141 raise Exception(
142 "Specification was valid for more than one tracer, please clarify which you want through use of positions x, y and z"
143 )
144
145
146"""
147Extract only the data from time and from the required value
148"""
149
150dt = []
151vx = []
152vy = []
153vz = []
154
155for row in csvData:
156 dt.append(float(row[timeIndex]))
157 vx.append(float(row[vxIndex]))
158 vy.append(float(row[vyIndex]))
159 vz.append(float(row[vzIndex]))
160
161"""
162Sort this data in the direction of increasing time
163"""
164
165dt, vx, vy, vz = zip(*sorted(zip(dt, vx, vy, vz)))
166
167"""
168Plot the data
169"""
170
171fig, axs = plt.subplots(3)
172
173axs[0].plot(
174 dt, vx, lw=4
175)
176axs[0].set(xlabel="t", ylabel="vx")
177axs[0].grid()
178
179axs[1].plot(
180 dt, vy, lw=4
181)
182axs[1].set(xlabel="t", ylabel="vy")
183axs[1].grid()
184
185if dim==3:
186 axs[2].plot(
187 dt,vz, lw=4
188 )
189 axs[2].set(xlabel="t", ylabel="vz")
190 axs[2].grid()
191
192fig.suptitle("velocities as a function of time", fontsize=20)
193plt.legend()
194plt.show()