Peano
print_velocities.py
Go to the documentation of this file.
1 import matplotlib.pyplot as plt
2 import csv
3 import 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 """
9 parse arguments to get details about which data should be extracted
10 """
11 
12 parser = argparse.ArgumentParser(
13  description="Script to convert CSV generated by ExaHyPE2 executions into plots"
14 )
15 parser.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 )
22 parser.add_argument(
23  "--v", dest="v", type=int, default=0, help="Index of the variable to be plotted"
24 )
25 parser.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 )
32 parser.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 )
39 parser.add_argument(
40  "--x",
41  dest="x",
42  type=float,
43  default=None,
44  help="x position of tracer which should be considered",
45 )
46 parser.add_argument(
47  "--y",
48  dest="y",
49  type=float,
50  default=None,
51  help="y position of tracer which should be considered",
52 )
53 parser.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 
61 args = parser.parse_args()
62 
63 """
64 Extract some metadata about the positions of the various information in the csv file.
65 These should always be the same but it's safer to check.
66 """
67 try:
68  csvData = list(csv.reader(open(args.file_name, "r"), delimiter=","))
69 except 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 
79 timeIndex = csvData[0].index(" t")
80 vxIndex = csvData[0].index(" data ") + args.v
81 vyIndex = csvData[0].index(" data ") + args.v + 1
82 vzIndex = csvData[0].index(" data ") + args.v + 2
83 firstNumberIndex = csvData[0].index("number(0)")
84 secondNumberIndex = csvData[0].index(" number(1)")
85 dim = 2
86 if " 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  ]
93 else:
94  positionIndexes = [csvData[0].index(" x(0)"), csvData[0].index(" x(1)")]
95 
96 """
97 remove first row which should only contain metadata
98 """
99 csvData = csvData[1:-1]
100 
101 
102 """
103 Filter 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 
122 csvData = [data for data in csvData if position_check(data)]
123 
124 
125 """
126 Check how many tracers fit the filter by extracting the total numbers of number(0) and
127 number(1) tuples that exist with the filter.
128 The {number(0), number(1)} tuples should be unique for each tracer, therefore if there
129 exists more than one value for either of these after filtering, there are multiple
130 tracers that correspond to the filter.
131 In the same way if there are none, none of the tracers correspond to the filter.
132 """
133 checkFirstIndexLength = set([data[firstNumberIndex] for data in csvData])
134 checksecondIndexLength = set([data[secondNumberIndex] for data in csvData])
135 
136 if 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  )
140 elif 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 """
147 Extract only the data from time and from the required value
148 """
149 
150 dt = []
151 vx = []
152 vy = []
153 vz = []
154 
155 for 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 """
162 Sort this data in the direction of increasing time
163 """
164 
165 dt, vx, vy, vz = zip(*sorted(zip(dt, vx, vy, vz)))
166 
167 """
168 Plot the data
169 """
170 
171 fig, axs = plt.subplots(3)
172 
173 axs[0].plot(
174  dt, vx, lw=4
175 )
176 axs[0].set(xlabel="t", ylabel="vx")
177 axs[0].grid()
178 
179 axs[1].plot(
180  dt, vy, lw=4
181 )
182 axs[1].set(xlabel="t", ylabel="vy")
183 axs[1].grid()
184 
185 if 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 
192 fig.suptitle("velocities as a function of time", fontsize=20)
193 plt.legend()
194 plt.show()