-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_testing.py
299 lines (243 loc) · 11.6 KB
/
error_testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
# line above specifies which program should be called to run this script - called shebang
# the way it is called above (and not #!/user/bin/python) ensures portability amongst Unix distros
######################
## Library Imports ##
######################
import numpy as np
import matplotlib.pyplot as plt
import h5py
import sys
import os
import re
import glob
from matplotlib.gridspec import GridSpec
from subprocess import Popen, PIPE
from Plotting.functions import read_input_file
## For colour printing to terminal
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
######################
## MAIN ##
######################
if __name__ == '__main__':
# executable
exe = "./Solver/bin/main_TG_test"
# output dirs
out_dir_n = "./Data/ErrorTesting/Error_N/"
out_dir_dt = "./Data/ErrorTesting/Error_dt/"
## Get command line arguments -> determine which error testing to do
if len(sys.argv) != 2:
print(bcolors.FAIL + "[ERROR]: " + bcolors.ENDC + "User must specify " + bcolors.OKCYAN + "N " + bcolors.ENDC + "or " + bcolors.OKCYAN + "dt " + bcolors.ENDC + "to choose which error testing to perform!")
sys.exit()
else:
test_type = str(sys.argv[1])
###------------------------
### Check Executable
###------------------------
## If executable doesn't exist than make it:
print("\nChecking if executable exists...")
if not os.path.isfile(exe):
print(bcolors.WARNING + "Executable does not exist!")
print(bcolors.WARNING + "Now making executable:" + bcolors.OKCYAN + exe)
## Command to execute
cmd = "cd ./Solver; make test; cd .."
## Open subprocess to execute make command
process = Popen(cmd, shell = True, stdout = PIPE, stdin = PIPE, universal_newlines = True)
## Communicate with subprocess - print to screen errors and output
[runCodeOutput, runCodeErr] = process.communicate()
print(runCodeOutput)
print(runCodeErr)
## Wait unitl it is finished
process.wait()
else:
print(bcolors.OKGREEN + "[SUCCESS]: " + bcolors.ENDC + "Executable Exists!")
print()
###------------------------
### Check for Output Dirs
###------------------------
## If output folder does not exist create it
print("Checking if output error directory exists...")
if not os.path.isdir(out_dir_n):
print(bcolors.WARNING + "Output folder does not exist...")
print(bcolors.WARNING + "Now creating output folder:" + bcolors.OKCYAN + out_dir_n)
os.mkdir(out_dir_n)
print(bcolors.OKGREEN + "[SUCCESS]: " + bcolors.ENDC + "Output directory for errror vs N now created!")
else:
print(bcolors.OKGREEN + "[SUCCESS]: " + bcolors.ENDC + "Output directory for errror vs N already exists!")
if not os.path.isdir(out_dir_dt):
print(bcolors.WARNING + "Output folder does not exist...")
print(bcolors.WARNING + "Now creating output folder:" + bcolors.OKCYAN + out_dir_dt)
os.mkdir(out_dir_dt)
print(bcolors.OKGREEN + "[SUCCESS]: " + bcolors.ENDC + "Output directory for errror vs dt now created!")
else:
print(bcolors.OKGREEN + "[SUCCESS]: " + bcolors.ENDC + "Output directory for errror vs dt already exists!")
print()
#####################################################################################
###### #####
###### ERROR AS A FUNCTION OF dt #####
###### #####
#####################################################################################
if test_type == "dt":
N = 128
cfl = [2.0, 1.75, 1.5, 1.25, 1.0, 0.75]
cfl_run = cfl.copy()
##------------------------
## Check for Data
##------------------------
## Loop files in output directory to see which datafiles exist
for file in os.listdir(out_dir_dt):
if file.endswith('.h5'):
for dt in cfl:
if "CFL[" + "{:0.2f}".format(dt) + "]" in file:
print(bcolors.OKGREEN + "[SUCCESS]:" + bcolors.ENDC + " Data file for CFL = " + bcolors.OKCYAN + "{}".format(dt) + bcolors.ENDC + " exists!")
## Remove from running list if data file exists already
cfl_run.remove(dt)
break
## Loop through and generate files that dont exist
for dt in cfl_run:
## Print update
print(bcolors.WARNING + "\nExecuting CFL = " + bcolors.OKCYAN + "{}".format(dt) + bcolors.ENDC)
## Create command
cmd = 'mpirun -n 4 ' + exe + ' -o ' + out_dir_dt + ' -n ' + str(N) + ' -n ' + str(N) + ' -s 0.0 -e 1.0 -h 0.0001 -i \"TG_VORT\" -v 1.0 -c ' + str(dt)
print(bcolors.WARNING + "Command: " + bcolors.ENDC + cmd)
# Open subprocess to execute command
process = Popen(cmd, shell = True, stdout = PIPE, stdin = PIPE, universal_newlines = True)
## Communicate with subprocess - print to screen errors and output
[runCodeOutput, runCodeErr] = process.communicate()
print("Output: \n{}".format(runCodeOutput))
print("Errors: {}".format(runCodeErr))
## Wait unitl it is finished
process.wait()
print()
#####################################################################################
###### #####
###### ERROR AS A FUNCTION OF N #####
###### #####
#####################################################################################
if test_type == "N":
# Create dataspace
N = [2 ** i for i in range(6, 11)]
N_run = N.copy()
printing = {2**6: 100, 2**7: 250, 2**8: 500, 2**9: 2500, 2**10: 5000}
##------------------------
## Check for Data
##------------------------
## Loop files in output directory to see which datafiles exist
for file in os.listdir(out_dir_n):
if file.endswith('.h5'):
for n in N:
if "N[" + str(n) + "," + str(n) + "]" in file:
print(bcolors.OKGREEN + "[SUCCESS]:" + bcolors.ENDC + " Data file for N = " + bcolors.OKCYAN + "{}".format(n) + bcolors.ENDC + " exists!")
## Remove from running list if data file exists already
N_run.remove(n)
break
## Loop through and generate files that dont exist
for n in N_run:
## Print update
print(bcolors.WARNING + "\nExecuting N = " + bcolors.OKCYAN + "{}".format(n) + bcolors.ENDC)
## Create command
cmd = 'mpirun -n 4 ' + exe + ' -o ' + out_dir_n + ' -n ' + str(n) + ' -n ' + str(n) + ' -s 0.0 -e 1.0 -h 0.0001 -i \"TG_VORT\" -v 1.0 -p ' + str(printing[n])
print(bcolors.WARNING + "Command: " + bcolors.ENDC + cmd)
# Open subprocess to execute command
process = Popen(cmd, shell = True, stdout = PIPE, stdin = PIPE, universal_newlines = True)
## Communicate with subprocess - print to screen errors and output
[runCodeOutput, runCodeErr] = process.communicate()
print("Output: \n{}".format(runCodeOutput))
print("Errors: {}".format(runCodeErr))
## Wait unitl it is finished
process.wait()
print()
###------------------------
### Compute Errors
###------------------------
data_linf = {}
data_l2 = {}
if test_type == "dt":
output_dir = out_dir_dt
elif test_type == "N":
output_dir = out_dir_n
for file in os.listdir(output_dir):
if file.endswith('.h5'):
## Retirve the n from the file name
if test_type == "dt":
n = int(re.split('\[', file)[1].split(',')[0])
c = float(re.split('\]', re.split('\[', file)[3])[0])
data_linf[c] = []
data_l2[c] = []
elif test_type == "N":
n = int(re.split('\[', file)[1].split(',')[0])
data_linf[n] = []
data_l2[n] = []
## Read in data
print(bcolors.WARNING + "Reading in file: " + bcolors.OKCYAN+ "{}".format(file) + bcolors.ENDC)
w = read_input_file(output_dir + file, n, n)[0]
tg_soln = read_input_file(output_dir + file, n, n)[2]
## Compute error norms
if test_type == "dt":
## Collect the error at the final iteration
data_linf[c].append(np.amax(np.absolute(w[-1, :, :] - tg_soln[-1, :, :])))
data_l2[c].append(np.linalg.norm(np.absolute(w[-1, :, :] - tg_soln[-1, :, :]), ord = 2))
elif test_type == "N":
l2 = np.zeros(w.shape[0])
linf = np.zeros(w.shape[0])
## Collect error norms for this n
for i in range(w.shape[0]):
linf[i] = np.amax(np.absolute(w[i, :, :] - tg_soln[i, :, :]))
# linf[i] = np.linalg.norm(np.absolute(w[i, :, :] - tg_soln[i, :, :]), ord = np.inf)
l2[i] = np.linalg.norm(np.absolute(w[i, :, :] - tg_soln[i, :, :]), ord = 2)
data_linf[n].append(np.amax(linf))
data_l2[n].append(np.linalg.norm(l2, ord = 2))
print(data_l2)
print(sorted(data_l2))
print()
print(data_linf)
print(sorted(data_linf))
## Order the data
l2norm = []
linfnorm = []
if test_type == "dt":
datasapce = cfl.copy()
elif test_type == "N":
datasapce = N.copy()
for d in datasapce:
l2norm.append(data_l2[d])
linfnorm.append(data_linf[d])
print(data_linf[d])
print(bcolors.OKGREEN + "[SUCCESS]" + bcolors.ENDC + "All errors computed\n")
###------------------------
### Plot Results
###------------------------
print(bcolors.WARNING + "Now plotting data.." + bcolors.ENDC)
fig = plt.figure(figsize = (16, 8))
gs = GridSpec(1, 2, hspace = 0.4, wspace = 0.4)
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot(datasapce, l2norm, '.-')
ax1.set_yscale('log')
ax1.set_title("L2 Norm")
ax1.grid(which = 'both')
ax2 = fig.add_subplot(gs[0, 1])
ax2.plot(datasapce, linfnorm, '.-')
ax2.set_yscale('log')
ax2.set_title("Max Absolute Error")
ax2.grid(which = 'both')
if test_type == "dt":
ax1.set_xlabel(r"CFL")
ax1.set_ylabel(r"Error(CFL)")
ax2.set_ylabel(r"Error(CFL)")
ax2.set_xlabel(r"CFL")
elif test_type == "N":
ax1.set_xlabel(r"N")
ax1.set_ylabel(r"Error(N)")
ax2.set_ylabel(r"Error(N)")
ax2.set_xlabel(r"N")
plt.savefig(output_dir + './TG_Error_as_function_of_{}.png'.format(test_type))
plt.close()