Skip to content

Commit 4e9b7f6

Browse files
committed
clean up outliers from data, adding uniformity measurement script
1 parent fa237d8 commit 4e9b7f6

File tree

5 files changed

+240
-7
lines changed

5 files changed

+240
-7
lines changed

hotwater-parallel.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import glob
33
import os
44
import multiprocessing
5-
import sys
65

76
import tecplot as tp
87

hotwater-uniformity-parallel.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import atexit
2+
import glob
3+
import os
4+
import multiprocessing
5+
import re
6+
7+
import numpy as np
8+
from scipy import optimize, stats
9+
10+
import tecplot as tp
11+
12+
def init():
13+
# !!! IMPORTANT !!!
14+
# Must register stop at exit to ensure Tecplot cleans
15+
# up all temporary files and does not create a core dump
16+
atexit.register(tp.session.stop)
17+
18+
def work(datafile):
19+
match = re.search(r'Z(\d+)ZT(\d+)', datafile)
20+
Z, ZT = int(match.group(1)), int(match.group(2))
21+
tp.new_layout()
22+
tp.data.load_tecplot(datafile)
23+
24+
# create a slice at the exit point of the pipe
25+
pipe_exit = tp.data.extract.extract_slice((0, 0.3, 0), (0, 1, 0))
26+
27+
# get y-velocity on this slice
28+
x = pipe_exit.values('X')[:]
29+
z = pipe_exit.values('Z')[:]
30+
vy = pipe_exit.values('Y Velocity')[:]
31+
t = pipe_exit.values('Temperature')[:]
32+
33+
def velocity_model(xy, a, b):
34+
x, y = xy
35+
return a * (x**2 + y**2) + b
36+
37+
# fit data to our model
38+
pfit, pcov = optimize.curve_fit(velocity_model, (x, z), vy, p0=[1, 1])
39+
perr = np.sqrt(np.abs(pcov.diagonal()))
40+
41+
# chi-sq test for the fit
42+
ndf = len(vy) - len(pfit)
43+
vy_fit = velocity_model((x, z), *pfit)
44+
chisq, pval = stats.chisquare(vy, vy_fit, len(pfit))
45+
46+
def velocity_model(xy, x0, y0, a, b):
47+
x, y = xy
48+
return a * ((x - x0)**2 + (y - y0)**2) + b
49+
50+
# fit data to our model
51+
pfit, pcov = optimize.curve_fit(velocity_model, (x, z), vy, p0=[0, 0, 1, 1])
52+
y0 = pfit[1]
53+
54+
return Z, ZT, chisq/ndf, np.std(vy), np.std(t), y0
55+
56+
if __name__ == '__main__':
57+
# !!! IMPORTANT !!!
58+
# On Linux systems, Python's multiprocessing start method
59+
# defaults to "fork" which is incompatible with PyTecplot
60+
# and must be set to "spawn"
61+
multiprocessing.set_start_method('spawn')
62+
63+
# Get the datafiles
64+
files = glob.glob('hotwatermixing/HotWaterMixing_Y05YT10*.plt')
65+
66+
# Set up the pool with initializing function and associated arguments
67+
num_workers = min(multiprocessing.cpu_count(), len(files))
68+
pool = multiprocessing.Pool(num_workers, initializer=init)
69+
70+
try:
71+
if not os.path.exists('images'):
72+
os.makedirs('images')
73+
74+
# Map the work function to each of the job arguments
75+
results = pool.map(work, files)
76+
finally:
77+
# !!! IMPORTANT !!!
78+
# Must join the process pool before parent script exits
79+
# to ensure Tecplot cleans up all temporary files
80+
# and does not create a core dump
81+
pool.close()
82+
pool.join()
83+
84+
Z, ZT, chisq_per_ndf, stddev_vy, stddev_t, y0 = zip(*results)
85+
86+
from scipy import interpolate
87+
from matplotlib import pyplot
88+
89+
x = np.linspace(min(Z), max(Z), 300)
90+
y = np.linspace(min(ZT), max(ZT), 300)
91+
XX, YY = np.meshgrid(x, y)
92+
93+
fig, ax = pyplot.subplots(2, 2)
94+
for ax, data in zip(ax.ravel(), (chisq_per_ndf, stddev_vy, stddev_t, y0)):
95+
ZZ = interpolate.griddata((Z, ZT), data, (XX, YY))
96+
plt = ax.pcolormesh(XX, YY, ZZ)
97+
ax.scatter(Z, ZT)
98+
fig.colorbar(plt, ax=ax)
99+
pyplot.show()
100+

hotwater-uniformity.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import sys
2+
3+
import numpy as np
4+
from scipy import interpolate, optimize, stats
5+
6+
7+
import tecplot as tp
8+
from tecplot.exception import *
9+
from tecplot.constant import *
10+
11+
# if "-c" is passed as an argument from the
12+
# console conntect to Tecplot 360 on the
13+
# default port (7600) and clear the layout
14+
if '-c' in sys.argv:
15+
tp.session.connect()
16+
tp.new_layout()
17+
18+
# load a single data file
19+
infile = 'hotwatermixing/HotWaterMixing.plt'
20+
dataset = tp.data.load_tecplot(infile)
21+
22+
# create a slice at the exit point of the pipe
23+
pipe_exit = tp.data.extract.extract_slice((0, 0.34, 0), (0, 1, 0))
24+
25+
# get y-velocity on this slice
26+
x = pipe_exit.values('X')[:]
27+
z = pipe_exit.values('Z')[:]
28+
vy = pipe_exit.values('Y Velocity')[:]
29+
30+
def velocity_model(xy, a, b):
31+
x, y = xy
32+
return a * (x**2 + y**2) + b
33+
34+
# fit data to our model
35+
pfit, pcov = optimize.curve_fit(velocity_model, (x, z), vy, p0=[1, 1])
36+
perr = np.sqrt(np.abs(pcov.diagonal()))
37+
38+
# chi-sq test for the fit
39+
ndf = len(vy) - len(pfit)
40+
vy_fit = velocity_model((x, z), *pfit)
41+
chisq, pval = stats.chisquare(vy, vy_fit, len(pfit))
42+
43+
print(f'''\
44+
y-velocity equation: a * (x**2 + y**2) + b
45+
fitted parameters:
46+
a: {pfit[0]:.3f} +/- {perr[0]:.3f}
47+
b: {pfit[1]:.3f} +/- {perr[1]:.3f}
48+
chi-sq / ndf: {chisq/ndf:.3f}''')
49+
50+
51+
t = pipe_exit.values('Temperature')[:]
52+
print(f'''\
53+
temperature:
54+
average: {np.average(t)}
55+
stddev: {np.std(t)}''')
56+
57+
def velocity_model(xy, x0, y0, a, b):
58+
x, y = xy
59+
return a * ((x - x0)**2 + (y - y0)**2) + b
60+
61+
# fit data to our model
62+
pfit, pcov = optimize.curve_fit(velocity_model, (x, z), vy, p0=[0, 0, 1, 1])
63+
perr = np.sqrt(np.abs(pcov.diagonal()))
64+
65+
# chi-sq test for the fit
66+
ndf = len(vy) - len(pfit)
67+
vy_fit = velocity_model((x, z), *pfit)
68+
chisq, pval = stats.chisquare(vy, vy_fit, len(pfit))
69+
print(f'''\
70+
y-velocity equation: a * ((x - x0)**2 + (y - y0)**2) + b
71+
fitted parameters:
72+
x0: {pfit[0]:.3f} +/- {perr[0]:.3f}
73+
y0: {pfit[1]:.3f} +/- {perr[1]:.3f}
74+
a: {pfit[2]:.3f} +/- {perr[2]:.3f}
75+
b: {pfit[3]:.3f} +/- {perr[3]:.3f}
76+
chi-sq / ndf: {chisq/ndf:.3f}''')
77+
78+
79+
"""
80+
81+
82+
83+
xx = np.linspace(x.min(), x.max(), 300)
84+
zz = np.linspace(z.min(), z.max(), 300)
85+
X, Z = np.meshgrid(xx, zz)
86+
Vdata = interpolate.griddata((x, z), vy, (X, Z))
87+
Vfit = velocity_model((X, Z), *pfit)
88+
89+
from matplotlib import pyplot
90+
fig, ax = pyplot.subplots(2,2)
91+
for ax, data in zip(ax.ravel(), (Vfit, Vdata, Vdata - Vfit)):
92+
plt = ax.pcolormesh(X, Z, data)
93+
fig.colorbar(plt, ax=ax)
94+
pyplot.show()
95+
96+
97+
# Calculate cell area (using cell volume calculation from CFDA)
98+
tp.macro.execute_extended_command('CFDAnalyzer4', r'''
99+
Calculate Function='CELLVOLUME'
100+
Normalization='None'
101+
ValueLocation='CellCentered'
102+
CalculateOnDemand='F'
103+
UseMorePointsForFEGradientCalculations='F'
104+
''')
105+
106+
# calculate cell-centered temperature and velocity
107+
tp.data.operate.execute_equation(
108+
'{Temperature CC}={Temperature}',
109+
value_location=ValueLocation.CellCentered)
110+
tp.data.operate.execute_equation(
111+
'{Y Velocity CC}={Y Velocity}',
112+
value_location=ValueLocation.CellCentered)
113+
114+
# fetch data from Tecplot into Python
115+
cellvol = pipe_exit.values('Cell Volume')[:]
116+
temp = pipe_exit.values('Temperature CC')[:]
117+
yvel = pipe_exit.values('Y Velocity CC')[:]
118+
119+
# normalize cell volume so the sum is equal to one
120+
cellvol /= np.sum(cellvol)
121+
122+
# calculate average velocity and temperature
123+
avgyvel = np.sum(yvel * cellvol)
124+
avgtemp = np.sum(temp * cellvol)
125+
print(f'average velocity: {avgyvel}')
126+
print(f'average temperature: {avgtemp}')
127+
128+
# calculate the standard deviate of velocity and temperature
129+
stddev_yvel = np.std(yvel)
130+
stddev_temp = np.std(temp)
131+
print(f'stddev velocity: {stddev_yvel}')
132+
print(f'stddev temperature: {stddev_temp}')
133+
134+
stddev_yvel = np.std(yvel * cellvol / np.sum(cellvol))
135+
stddev_temp = np.std(temp * cellvol / np.sum(cellvol))
136+
print(f'stddev velocity: {stddev_yvel}')
137+
print(f'stddev temperature: {stddev_temp}')
138+
139+
"""
140+

hotwatermixing/HotWaterMixing_Y05YT10Z05ZT075.plt

Lines changed: 0 additions & 3 deletions
This file was deleted.

hotwatermixing/HotWaterMixing_Y05YT10Z05ZT099.plt

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)