-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_results.py
More file actions
370 lines (304 loc) · 12.6 KB
/
plot_results.py
File metadata and controls
370 lines (304 loc) · 12.6 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
Impeller Flow Simulator - Results Visualization Module
This module provides comprehensive visualization tools for analyzing impeller simulation results.
It generates:
- Efficiency vs flow rate plots
- Convergence analysis visualizations
- Performance heatmaps
- Interactive dashboards
The visualizations are designed to help understand the performance characteristics
and optimization results of the impeller simulations.
Author: John Benac
Date: January 2024
License: MIT
"""
import json
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from pathlib import Path
import plotly.graph_objects as go
from plotly.subplots import make_subplots
def load_results(results_file):
with open(results_file, 'r') as f:
return json.load(f)
def create_efficiency_flow_plot(results):
plt.figure(figsize=(12, 6))
flow_rates = []
efficiencies = []
categories = []
for case_name, case_data in results.items():
for flow_rate, result in case_data['results'].items():
if result['metrics'] is not None:
flow_rates.append(float(flow_rate))
efficiencies.append(result['metrics']['efficiency']['overall'] * 100)
categories.append(case_name)
sns.scatterplot(x=flow_rates, y=efficiencies, hue=categories, s=100)
plt.plot(flow_rates, efficiencies, '--', alpha=0.3)
plt.xlabel('Flow Rate (m³/s)')
plt.ylabel('Efficiency (%)')
plt.title('Efficiency vs Flow Rate')
plt.grid(True, alpha=0.3)
plt.savefig('results/efficiency_flow_plot.png', dpi=300, bbox_inches='tight')
plt.close()
def create_convergence_plot(results):
plt.figure(figsize=(12, 6))
for case_name, case_data in results.items():
if 'analysis' in case_data and 'convergence_rate' in case_data['analysis']:
flow_rates = []
improvements = []
for flow_rate, conv_data in case_data['analysis']['convergence_rate'].items():
flow_rates.append(float(flow_rate))
improvements.append(conv_data['improvement'])
plt.plot(flow_rates, improvements, 'o-', label=case_name)
plt.xlabel('Flow Rate (m³/s)')
plt.ylabel('Improvement (%)')
plt.title('Optimization Improvement by Flow Rate')
plt.legend()
plt.grid(True, alpha=0.3)
plt.savefig('results/convergence_plot.png', dpi=300, bbox_inches='tight')
plt.close()
def create_performance_heatmap(results):
flow_rates = []
efficiencies = []
errors = []
for case_data in results.values():
for flow_rate, result in case_data['results'].items():
if result['metrics'] is not None:
flow_rates.append(float(flow_rate))
efficiencies.append(result['metrics']['efficiency']['overall'] * 100)
errors.append(result['error'])
# Create 2D grid for heatmap
x = np.linspace(min(flow_rates), max(flow_rates), 50)
y = np.linspace(min(efficiencies), max(efficiencies), 50)
X, Y = np.meshgrid(x, y)
# Create heatmap data
Z = np.zeros_like(X)
for i in range(len(flow_rates)):
Z += np.exp(-((X - flow_rates[i])**2 + (Y - efficiencies[i])**2) / 100)
plt.figure(figsize=(12, 8))
plt.contourf(X, Y, Z, levels=20, cmap='viridis')
plt.colorbar(label='Performance Density')
plt.scatter(flow_rates, efficiencies, c='red', s=100, label='Test Points')
plt.xlabel('Flow Rate (m³/s)')
plt.ylabel('Efficiency (%)')
plt.title('Performance Heatmap')
plt.legend()
plt.savefig('results/performance_heatmap.png', dpi=300, bbox_inches='tight')
plt.close()
def create_performance_map(results):
plt.figure(figsize=(12, 8))
# Extract flow coefficients, head coefficients, and efficiencies
flow_coeffs = []
head_coeffs = []
efficiencies = []
for case_data in results.values():
for result in case_data['results'].values():
if result['metrics'] is not None:
flow_coeffs.append(result['metrics']['dimensionless']['flow_coefficient'])
head_coeffs.append(result['metrics']['dimensionless']['head_coefficient'])
efficiencies.append(result['metrics']['efficiency']['overall'] * 100)
# Add small random noise to prevent identical coordinates
flow_coeffs = np.array(flow_coeffs)
head_coeffs = np.array(head_coeffs)
flow_coeffs += np.random.normal(0, 0.0001, size=len(flow_coeffs))
head_coeffs += np.random.normal(0, 0.0001, size=len(head_coeffs))
# Create scatter plot of operating points
scatter = plt.scatter(flow_coeffs, head_coeffs, c=efficiencies,
cmap='viridis', s=100, label='Operating Points')
plt.colorbar(scatter, label='Efficiency (%)')
# Find and mark best efficiency point
best_idx = np.argmax(efficiencies)
plt.scatter(flow_coeffs[best_idx], head_coeffs[best_idx], c='white', s=200,
marker='*', label='Best Efficiency Point')
plt.xlabel('Flow Coefficient (φ)')
plt.ylabel('Head Coefficient (ψ)')
plt.title('Impeller Performance Map')
plt.legend()
plt.grid(True, alpha=0.3)
plt.savefig('results/performance_map.png', dpi=300, bbox_inches='tight')
plt.close()
def create_optimization_progress(results):
plt.figure(figsize=(12, 8))
# Create subplots for different metrics
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
for case_name, case_data in results.items():
# Plot error progression
flow_rates = []
errors = []
for flow_rate, result in case_data['results'].items():
flow_rates.append(float(flow_rate))
errors.append(result['error'])
ax1.plot(flow_rates, errors, '-o', label=case_name)
# Plot efficiency progression
efficiencies = [result['metrics']['efficiency']['overall'] * 100
for result in case_data['results'].values()]
ax2.plot(flow_rates, efficiencies, '-o', label=case_name)
# Customize subplots
ax1.set_title('Error vs Flow Rate')
ax1.set_xlabel('Flow Rate (m³/s)')
ax1.set_ylabel('Error (%)')
ax1.grid(True, alpha=0.3)
ax1.legend()
ax2.set_title('Efficiency vs Flow Rate')
ax2.set_xlabel('Flow Rate (m³/s)')
ax2.set_ylabel('Efficiency (%)')
ax2.grid(True, alpha=0.3)
ax2.legend()
plt.tight_layout()
plt.savefig('results/optimization_progress.png', dpi=300, bbox_inches='tight')
plt.close()
def create_blade_loading_plot(results):
plt.figure(figsize=(12, 8))
# Create subplots for pressure and velocity distributions
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))
has_blade_data = False
for case_name, case_data in results.items():
# Get the last result for each case
last_result = list(case_data['results'].values())[-1]
if 'blade_data' in last_result and last_result['blade_data'] is not None:
has_blade_data = True
# Plot pressure distribution
meridional_pos = last_result['blade_data']['meridional_pos']
ax1.plot(meridional_pos, last_result['blade_data']['pressure'],
label=f'{case_name} - Pressure Side')
ax1.plot(meridional_pos, last_result['blade_data']['suction'],
'--', label=f'{case_name} - Suction Side')
# Plot velocity distribution
ax2.plot(meridional_pos, last_result['blade_data']['velocity'],
label=case_name)
if not has_blade_data:
plt.close()
return
ax1.set_title('Blade Surface Pressure Distribution')
ax1.set_xlabel('Meridional Position')
ax1.set_ylabel('Pressure (Pa)')
ax1.grid(True, alpha=0.3)
ax1.legend()
ax2.set_title('Velocity Distribution Along Blade')
ax2.set_xlabel('Meridional Position')
ax2.set_ylabel('Velocity (m/s)')
ax2.grid(True, alpha=0.3)
ax2.legend()
plt.tight_layout()
plt.savefig('results/blade_loading.png', dpi=300, bbox_inches='tight')
plt.close()
def create_interactive_analysis(results):
fig = make_subplots(
rows=2, cols=2,
subplot_titles=('Performance Map', 'Error vs Flow Rate',
'Efficiency vs Flow Rate', 'Velocity Triangles')
)
# Add performance map
flow_coeffs = []
head_coeffs = []
efficiencies = []
for case_data in results.values():
for result in case_data['results'].values():
if result['metrics'] is not None:
flow_coeffs.append(result['metrics']['dimensionless']['flow_coefficient'])
head_coeffs.append(result['metrics']['dimensionless']['head_coefficient'])
efficiencies.append(result['metrics']['efficiency']['overall'] * 100)
fig.add_trace(
go.Scatter(
x=flow_coeffs,
y=head_coeffs,
mode='markers',
marker=dict(
size=10,
color=efficiencies,
colorscale='Viridis',
showscale=True,
colorbar=dict(title='Efficiency (%)')
),
text=[f'Efficiency: {e:.1f}%' for e in efficiencies],
name='Operating Points'
),
row=1, col=1
)
# Add error vs flow rate
for case_name, case_data in results.items():
flow_rates = []
errors = []
for flow_rate, result in case_data['results'].items():
flow_rates.append(float(flow_rate))
errors.append(result['error'])
fig.add_trace(
go.Scatter(
x=flow_rates,
y=errors,
mode='lines+markers',
name=f'{case_name} - Error'
),
row=1, col=2
)
# Add efficiency vs flow rate
for case_name, case_data in results.items():
flow_rates = []
efficiencies = []
for flow_rate, result in case_data['results'].items():
flow_rates.append(float(flow_rate))
efficiencies.append(result['metrics']['efficiency']['overall'] * 100)
fig.add_trace(
go.Scatter(
x=flow_rates,
y=efficiencies,
mode='lines+markers',
name=f'{case_name} - Efficiency'
),
row=2, col=1
)
# Add velocity triangles placeholder
# This would be implemented with actual velocity triangle calculations
# Update layout
fig.update_xaxes(title_text='Flow Coefficient (φ)', row=1, col=1)
fig.update_yaxes(title_text='Head Coefficient (ψ)', row=1, col=1)
fig.update_xaxes(title_text='Flow Rate (m³/s)', row=1, col=2)
fig.update_yaxes(title_text='Error (%)', row=1, col=2)
fig.update_xaxes(title_text='Flow Rate (m³/s)', row=2, col=1)
fig.update_yaxes(title_text='Efficiency (%)', row=2, col=1)
fig.update_layout(
height=800,
width=1200,
title_text='Comprehensive Impeller Analysis',
showlegend=True
)
fig.write_html('results/interactive_analysis.html')
def main():
# Ensure results directory exists
Path('results').mkdir(exist_ok=True)
# Find the most recent results file
results_dir = Path('results')
results_files = list(results_dir.glob('comprehensive_results_*.json'))
if not results_files:
print("No results files found!")
return
latest_file = max(results_files, key=lambda x: x.stat().st_mtime)
print(f"Loading results from: {latest_file}")
# Load results
results = load_results(latest_file)
# Generate all plots
print("Generating efficiency vs flow rate plot...")
create_efficiency_flow_plot(results)
print("Generating convergence plot...")
create_convergence_plot(results)
print("Generating performance heatmap...")
create_performance_heatmap(results)
print("Generating performance map...")
create_performance_map(results)
print("Generating optimization progress plots...")
create_optimization_progress(results)
print("Generating blade loading analysis...")
create_blade_loading_plot(results)
print("Generating interactive analysis...")
create_interactive_analysis(results)
print("\nAll visualizations have been generated in the 'results' directory:")
print("1. efficiency_flow_plot.png")
print("2. convergence_plot.png")
print("3. performance_heatmap.png")
print("4. performance_map.png")
print("5. optimization_progress.png")
print("6. blade_loading.png")
print("7. interactive_analysis.html")
if __name__ == "__main__":
main()