|
| 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 | + |
0 commit comments