-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpostprocessing.py
More file actions
181 lines (134 loc) · 4.89 KB
/
Copy pathpostprocessing.py
File metadata and controls
181 lines (134 loc) · 4.89 KB
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
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 31 23:07:48 2019
@author: RickFu
"""
from mpl_toolkits.mplot3d import Axes3D
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import pandas as pd
import os
def evolutionField(results, outputDir=None):
""" Generate 3D temperature fields
For better understanding of the results
Inputs:
1. parameter, a pandas series
2. results, a numpy array
"""
X = results.index
Y = results.columns
X, Y = np.meshgrid(X, Y)
fig = plt.figure(figsize=(8,6))
ax = fig.add_subplot(111, projection='3d')
ax.set_xlabel('x, m')
ax.set_ylabel('Time, s')
ax.set_zlabel('Temperature, K')
Z = results.T.values
ax.plot_surface(X, Y, Z,
cmap=cm.seismic,
linewidth=0,
antialiased=False)
if outputDir:
fig.savefig(os.path.join(outputDir, 'evolutionField.png'), dpi=150)
plt.close(fig)
def thermalCouplePlot(results, positions, outputDir=None):
""" Generate x-y plots as thermo-couple data
Inputs:
1. results, a pandas DataFrame
2. Positions, a list of positions of the generated
grids.
"""
df = results.loc[positions,:]
df = df.T
df = df.add_prefix('x = ')
df = df.add_suffix(' m')
ax = df.plot(grid=True)
ax.set_xlabel("Time, s")
ax.set_ylabel("Temperature, K")
if outputDir:
ax.get_figure().savefig(os.path.join(outputDir, 'thermalCouple.png'), dpi=150)
plt.close(ax.get_figure())
def temperatureDistribution(results, times, outputDir=None):
""" Generate temperature distribution at different times
Inputs:
1. results, a pandas DataFrame
2. times, a list of timings on the calculated
time steps
"""
# Use nearest-match lookup (adaptive dt may not hit exact requested times)
col_idx = [results.columns[np.argmin(np.abs(results.columns - t))] for t in times]
df = results.loc[:, col_idx]
df = df.add_prefix('t = ')
df = df.add_suffix(' s')
ax = df.plot(grid=True)
ax.set_xlabel("x, m")
ax.set_ylabel("Temperature, K")
if outputDir:
ax.get_figure().savefig(os.path.join(outputDir, 'temperatureDistribution.png'), dpi=150)
plt.close(ax.get_figure())
def probePositions(parameter, probes_per_layer=5):
""" Generate thermocouple probe positions from layer configuration.
Places probes evenly within each layer, snapped to actual grid nodes.
"""
# Build actual grid positions
dx_arr = parameter['dx_array']
N = parameter['numberOfNode']
x = np.zeros(N)
for i in range(N - 1):
x[i + 1] = x[i] + dx_arr[i]
def snap(target):
"""Snap target position to nearest grid node."""
idx = np.argmin(np.abs(x - target))
return round(x[idx], 5)
if parameter.get('material function') == 'layered':
t_layers = parameter['layerThicknesses']
boundaries = np.cumsum(t_layers)
starts = np.concatenate([[0], boundaries[:-1]])
positions = set()
for s, e in zip(starts, boundaries):
for p in np.linspace(s, e, probes_per_layer):
positions.add(snap(p))
return sorted(positions)
else:
length = x[-1]
positions = set()
for p in np.linspace(0, length, probes_per_layer * 2):
positions.add(snap(p))
return sorted(positions)
def preprocess(parameter, results):
""" Pre-Process results
To convert numpy array into pandas DataFrame for easier
data processing.
Input:
1. Generated parameter serie
2. results as a numpy array
Return:
A pandas DataFrame with index as times and
columns as grid positions
"""
numberOfNode = parameter['numberOfNode']
numOfTimeStep = parameter['numberOfTimeStep']
deltaTime = parameter['deltaTime']
time = deltaTime * numOfTimeStep
# Build x-positions from dx_array (supports non-uniform layered grid)
dx_arr = parameter['dx_array']
grids = np.zeros(numberOfNode)
for i in range(numberOfNode - 1):
grids[i + 1] = grids[i] + dx_arr[i]
grids = grids.round(5)
times = np.linspace(0, time, numOfTimeStep+1).round(5)
df = pd.DataFrame(results,
index = grids,
columns = times)
return df
if __name__ == "__main__":
global para, results
test = preprocess(para, results)
evolutionField(test)
positions = [0, 0.002, 0.004, 0.006, 0.008, 0.01]
thermalCouplePlot(test, positions)
times = [0, 2, 4, 6, 8, 10]
temperatureDistribution(test, times)