Skip to content

Commit faf6a94

Browse files
committed
Save backup
1 parent e5e56f8 commit faf6a94

7 files changed

Lines changed: 1640 additions & 308 deletions

File tree

samples/CSP/sco2_analysis_python_V2/G3P3_analysis_plotting/sco2_filenames.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ def get_filenames_baseline(split=False):
108108
r'C:\Users\tbrown2\OneDrive - NREL\sCO2-CSP 10302.41.01.40\Notes\G3P3\runs\baseline_FINAL\run_10_20250210_142612\mega_slim_pickled_merged\split_config\HTR_BP_w_o_LTR.pkl',
109109

110110
r'C:\Users\tbrown2\OneDrive - NREL\sCO2-CSP 10302.41.01.40\Notes\G3P3\runs\baseline_FINAL\run_10_20250210_142612\mega_slim_pickled_merged\split_config\Partial.pkl',
111-
r'C:\Users\tbrown2\OneDrive - NREL\sCO2-CSP 10302.41.01.40\Notes\G3P3\runs\baseline_FINAL\run_10_20250210_142612\mega_slim_pickled_merged\split_config\Partial_Intercooling_w_o_HTR.pkl',
112111
r'C:\Users\tbrown2\OneDrive - NREL\sCO2-CSP 10302.41.01.40\Notes\G3P3\runs\baseline_FINAL\run_10_20250210_142612\mega_slim_pickled_merged\split_config\Partial_w_o_HTR.pkl',
113112
r'C:\Users\tbrown2\OneDrive - NREL\sCO2-CSP 10302.41.01.40\Notes\G3P3\runs\baseline_FINAL\run_10_20250210_142612\mega_slim_pickled_merged\split_config\Partial_w_o_LTR.pkl',
113+
r'C:\Users\tbrown2\OneDrive - NREL\sCO2-CSP 10302.41.01.40\Notes\G3P3\runs\baseline_FINAL\run_10_20250210_142612\mega_slim_pickled_merged\split_config\Partial_Intercooling_w_o_HTR.pkl',
114114

115115
r'C:\Users\tbrown2\OneDrive - NREL\sCO2-CSP 10302.41.01.40\Notes\G3P3\runs\baseline_FINAL\run_10_20250210_142612\mega_slim_pickled_merged\split_config\Turbine_Split_Flow.pkl',
116116
r'C:\Users\tbrown2\OneDrive - NREL\sCO2-CSP 10302.41.01.40\Notes\G3P3\runs\baseline_FINAL\run_10_20250210_142612\mega_slim_pickled_merged\split_config\Turbine_Split_Flow_w_o_HTR.pkl',

samples/CSP/sco2_analysis_python_V2/G3P3_analysis_plotting/sco2_plot_compare_all.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ def plot_sco2_pareto(dict_list_w_kwarg, config_list, X_info, Y_info):
127127
_, new_kwarg_list = design_tools.plot_scatter_pts(
128128
local_dict_list_w_kwarg
129129
,
130-
X_info, Y_info, show_legend=True)
131-
130+
X_info, Y_info, show_legend=True, legend_loc="outside right")
131+
plt.tight_layout()
132132

133133
# Now plot pareto with correct colors and markers
134134
i = 0
@@ -142,18 +142,13 @@ def plot_sco2_pareto(dict_list_w_kwarg, config_list, X_info, Y_info):
142142

143143
pareto_list_w_kwarg.append([pareto_dict, pareto_kwarg])
144144

145-
i += 1
146-
147-
148-
149-
150-
151-
145+
i += 1
152146

153147
design_tools.plot_scatter_pts(
154148
pareto_list_w_kwarg
155149
,
156150
X_info, Y_info, show_legend=True)
151+
plt.tight_layout()
157152

158153
def plot_sco2_vars(dict_list_w_kwarg, sweep_label=''):
159154

@@ -233,13 +228,33 @@ def plot_barcharts(best_dict_list_w_kwarg, config_list):
233228
design_tools.plot_costs_barchart(dict_index_duo_list, type='sco2', plot_title="Cycle Cost Comparison", fontsize=fontsize_global, figsize=figsize_global)
234229
design_tools.plot_costs_barchart(dict_index_duo_list, type='system', plot_title="System Cost Comparison", fontsize=fontsize_global, figsize=figsize_global)
235230

236-
def plot_pareto_spec_config(list_of_dict_list_w_kwarg, final_sweep_labels, config_list):
231+
def plot_pareto_spec_config(list_of_dict_list_w_kwarg, final_sweep_labels, config_list, Y_label=[]):
237232

238233
# Define X and Y Labels
239234
PC_ETA_label = ["eta_thermal_calc", "", "PC Efficiency"]
240235
COST_PER_kW_NET_label = ["cost_per_kWe_net_ish", "$/kWe", "Cost per kWe Net"]
241236
UA_TOTAL_label = ["recup_total_UA_calculated", "MW/K", "Total UA Calculated"]
242237

238+
if Y_label == "recup_over_cycle":
239+
recup_var = "recup_total_cost_equipment"
240+
cycle_var = "cycle_cost"
241+
recup_over_cycle = "recup_over_cycle"
242+
243+
for result_dict_list_w_kwarg in list_of_dict_list_w_kwarg:
244+
for result_dict, kwarg in result_dict_list_w_kwarg:
245+
NVal = len(result_dict[recup_var])
246+
result_dict[recup_over_cycle] = []
247+
for i in range(NVal):
248+
recup = result_dict[recup_var][i]
249+
cycle = result_dict[cycle_var][i]
250+
r_frac = recup/cycle
251+
result_dict[recup_over_cycle].append(r_frac)
252+
253+
Y_label = [recup_over_cycle, "", "Recup Cost over Cycle"]
254+
255+
if len(Y_label) == 0:
256+
Y_label = COST_PER_kW_NET_label
257+
243258
for config_name in config_list:
244259

245260
# Get cycle config from each data set
@@ -286,12 +301,12 @@ def plot_pareto_spec_config(list_of_dict_list_w_kwarg, final_sweep_labels, confi
286301
i += 1
287302

288303
design_tools.plot_scatter_pts(pareto_dict_w_kwarg_list,
289-
PC_ETA_label, COST_PER_kW_NET_label,
304+
PC_ETA_label, Y_label,
290305
show_legend=True, legend_loc='upper right',show_line=True,
291306
title=config_name, disk_load=True, is_norm=False, ax=ax1)
292307

293308
design_tools.plot_scatter_pts(pareto_dict_w_kwarg_list,
294-
PC_ETA_label, COST_PER_kW_NET_label,
309+
PC_ETA_label, Y_label,
295310
show_legend=True, legend_loc='upper right',show_line=True,
296311
title=config_name + " Normalized", disk_load=True, is_norm=True, ax=ax2)
297312

@@ -556,12 +571,20 @@ def test_compare():
556571
filenames_recup50_w_label, filenames_recup150_w_label,
557572
filenames_tes50_w_label, filenames_tes150_w_label,
558573
filenames_phxbucklow_w_label, filenames_phxbuckhigh_w_label,
559-
filenames_helio_phxbuckhigh_w_label]
574+
]
575+
576+
#filenames_list_w_label = [filenames_baseline_w_label,
577+
# filenames_heliocost_w_label,
578+
# filenames_recup50_w_label, filenames_recup150_w_label,
579+
# filenames_tes50_w_label, filenames_tes150_w_label,
580+
# ]
560581

561582
#filenames_list_w_label = [filenames_baseline_w_label, filenames_heliocost_w_label]
562583

563-
584+
filenames_list_w_label = [filenames_baseline_w_label]
564585

586+
#filenames_list_w_label = [filenames_baseline_w_label,
587+
# filenames_phxbucklow_w_label, filenames_phxbuckhigh_w_label]
565588
#filenames_list_w_label = [filenames_baseline_w_label]
566589

567590
#filenames_list_w_label = [filenames_TIT550_w_label]
@@ -578,8 +601,8 @@ def test_compare():
578601
#filenames_list_w_label = [filenames_baseline_w_label,
579602
# filenames_eta8085_w_label, filenames_eta8090_w_label]
580603
#
581-
#filenames_list_w_label = [filenames_baseline_w_label,
582-
# filenames_TIT550_w_label, filenames_TIT625_w_label,]
604+
filenames_list_w_label = [filenames_baseline_w_label,
605+
filenames_TIT550_w_label, filenames_TIT625_w_label]
583606

584607
#filenames_list_w_label = [filenames_baseline_w_label,
585608
# filenames_coldapproach40_w_label, filenames_coldapproach60_w_label,
@@ -618,7 +641,9 @@ def test_compare():
618641
"ui_direct_subtotal",
619642
"UA_recup_tot_des", "LTR_UA_des_in", "HTR_UA_des_in",
620643
"is_PR_fixed", "is_IP_fixed", "is_recomp_ok",
621-
"is_turbine_split_ok", "is_bypass_ok"]
644+
"is_turbine_split_ok", "is_bypass_ok",
645+
"HTR_cost_equipment", "LTR_cost_equipment",
646+
"recup_total_cost_equipment"]
622647

623648
#key_list = sco2_varnames.get_core_var_list()
624649

@@ -705,7 +730,11 @@ def test_compare():
705730

706731
# Pareto by config
707732
if True:
708-
plot_pareto_spec_config(list_of_dict_list_w_kwargs, final_sweep_labels, show_config_list)
733+
Y_label = ["recup_total_cost_equipment", "M$", "Recup Cost"]
734+
Y_label = "recup_over_cycle"
735+
Y_label = ["cycle_cost", "M$", "Cycle Cost"]
736+
Y_label = ["UA_recup_tot_des", "MW/K", "Recup Total Conductance"]
737+
plot_pareto_spec_config(list_of_dict_list_w_kwargs, final_sweep_labels, show_config_list, Y_label=COST_PER_kW_info)
709738

710739
# Sweep Comparison
711740
if True:

samples/CSP/sco2_analysis_python_V2/G3P3_analysis_plotting/sco2_varnames.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,33 @@ def get_sco2_design_vars_info_list():
8585
["is_recomp_ok", "", "Recompression Fraction"],
8686
["is_turbine_split_ok", "", "Turbine Split Fraction"],
8787
["is_bypass_ok", "", "Bypass Fraction"]]
88-
return sco2_vars
88+
return sco2_vars
89+
90+
def get_baseline_table_info_list():
91+
general_vars = [["cost_per_kWe_net_ish", "$/kWe", "System Cost per Net Power"],
92+
["eta_thermal_calc", "", "PC Thermal Efficiency"],
93+
["T_htf_cold_des", "C", "HTF Cold Temperature"],
94+
['total_installed_cost', '$', "System Total Installed Cost"],
95+
['cycle_cost', 'M$', "Cycle Cost"]
96+
]
97+
98+
sco2_cost_vars = [["mc_cost_bare_erected", "M$", "Main Compressor Cost"],
99+
["rc_cost_bare_erected", "M$", "Recompressor Cost"],
100+
["pc_cost_bare_erected", "M$", "Precompressor Cost"],
101+
["LTR_cost_bare_erected", "M$", "LTR Cost"],
102+
["HTR_cost_bare_erected", "M$", "HTR Cost"],
103+
["PHX_cost_bare_erected", "M$", "Primary Heat Exchanger Cost"],
104+
["BPX_cost_bare_erected", "M$", "Bypass Heat Exchanger Cost"],
105+
["t_cost_bare_erected", "M$", "Turbine Cost"],
106+
["t2_cost_bare_erected", "M$", "Second Turbine Cost"],
107+
["mc_cooler_cost_bare_erected", "M$", "Main Air Cooler Cost"],
108+
["pc_cooler_cost_bare_erected", "M$", "Precompressor Air Cooler Cost"]
109+
]
110+
111+
system_cost_vars = [["csp.pt.cost.heliostats", "$", "Heliostats Cost"],
112+
["csp.pt.cost.tower", "$", "Tower Cost"],
113+
["csp.pt.cost.receiver", "$", "Receiver Cost"],
114+
["csp.pt.cost.storage", "$", "TES Cost"]
115+
]
116+
117+
return [*general_vars, *sco2_cost_vars, *system_cost_vars]
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import os
2+
import sys
3+
import psutil
4+
from multiprocessing import Manager
5+
import concurrent.futures
6+
import mmap
7+
import psutil
8+
import pickle
9+
import copy
10+
import gc
11+
12+
parentDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
13+
sys.path.append(parentDir)
14+
exampleFolder = os.path.join(parentDir, 'example')
15+
coreFolder = os.path.join(parentDir, 'core')
16+
sys.path.append(exampleFolder)
17+
sys.path.append(coreFolder)
18+
19+
import sco2_cycle_ssc as sco2_solve
20+
import design_point_tools as design_tools
21+
import sco2_plot_g3p3_baseline_FINAL
22+
23+
def open_pickle_mmap(filename, keys=[]):
24+
with open(filename, 'rb') as f:
25+
# Memory map the file
26+
mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
27+
full_dict = pickle.load(mm)
28+
return_dict = full_dict
29+
30+
if len(keys) > 0:
31+
#partial_dict = {k: full_dict[k] for k in keys if k in full_dict}
32+
partial_dict = {}
33+
for dict_key in full_dict:
34+
for key in keys:
35+
if key in dict_key:
36+
partial_dict[dict_key] = full_dict[dict_key]
37+
38+
return_dict = partial_dict
39+
40+
NVal = len(return_dict["config_name"])
41+
return_dict["run_id"] = []
42+
return_dict["run_filename"] = []
43+
for col_id in range(NVal):
44+
return_dict["run_id"].append(col_id)
45+
return_dict["run_filename"].append(filename)
46+
47+
mm.close()
48+
return return_dict
49+
50+
def process_sweep_fileset_optimized(filenames, sweep_label, color, result_queue, key_list):
51+
# Open files, split by config name
52+
# Return dict list with kwarg, best cases?
53+
54+
# Open Pickles
55+
print('Opening pickles...')
56+
input_dict_list = []
57+
58+
# Track memory before and after operations
59+
print(f"Memory before loading: {psutil.Process().memory_info().rss / 1024 / 1024:.2f} MB")
60+
61+
for filename in filenames:
62+
#input_dict_list.append(open_pickle(filename))
63+
input_dict_list.append(open_pickle_mmap(filename, key_list))
64+
65+
# Rename config names
66+
print('Naming cycles...')
67+
is_new_config_names = False
68+
presplit = True
69+
for result_dict in input_dict_list:
70+
NVal = len(result_dict[list(result_dict.keys())[0]])
71+
if('config_name' in result_dict == False):
72+
result_dict['config_name'] = []
73+
for i in range(NVal):
74+
result_dict['config_name'].append('')
75+
76+
prev_config_name = result_dict['config_name'][0]
77+
78+
for i in range(NVal):
79+
cycle_config = result_dict['cycle_config'][i]
80+
recomp_frac = result_dict['recomp_frac'][i]
81+
bypass_frac = 0 if (('bypass_frac' in result_dict) == False) else result_dict['bypass_frac'][i]
82+
LTR_UA = result_dict['LTR_UA_calculated'][i]
83+
HTR_UA = result_dict['HTR_UA_calculated'][i]
84+
is_LTR = LTR_UA > 0
85+
is_HTR = HTR_UA > 0
86+
if(is_LTR == False) and (is_HTR == False):
87+
x = 0
88+
89+
if bypass_frac == '':
90+
bypass_frac = 0
91+
92+
new_config_name = sco2_solve.get_config_name(cycle_config, recomp_frac, bypass_frac, is_LTR, is_HTR)
93+
94+
if new_config_name != result_dict['config_name'][i]:
95+
result_dict['config_name'][i] = new_config_name
96+
is_new_config_names = True
97+
98+
if result_dict['config_name'][i] != prev_config_name:
99+
presplit = False
100+
101+
# Split by config name
102+
if presplit == True:
103+
print("Skipping dictionary splitting by config name...")
104+
result_dict_list = input_dict_list
105+
else:
106+
print("Splitting dictionaries by config_name...")
107+
result_dict_list = design_tools.split_by_config_name_optimized(input_dict_list)
108+
109+
# Clear original dict (for memory)
110+
for input_dict in input_dict_list:
111+
input_dict.clear()
112+
113+
114+
# Make dict_list_with_kwarg
115+
dict_list_with_kwargs = []
116+
marker_list = design_tools.get_marker_list()
117+
for i, result_dict in enumerate(result_dict_list):
118+
config_name = result_dict['config_name'][0]
119+
dict_w_kwarg = [result_dict, {'label': config_name,
120+
'marker': marker_list[i],
121+
'c':color}]
122+
dict_list_with_kwargs.append(dict_w_kwarg)
123+
124+
# Get Best Cases
125+
print("Finding best $/kWe cases...")
126+
best_dict_list_with_kwarg = []
127+
for dict_kwarg in dict_list_with_kwargs:
128+
diction = dict_kwarg[0]
129+
kwarg = copy.deepcopy(dict_kwarg[1])
130+
kwarg.setdefault('marker', 'X')
131+
kwarg['label'] = 'Best ' + kwarg['label']
132+
kwarg['c'] = color
133+
best_dict = sco2_plot_g3p3_baseline_FINAL.get_best_dict_optimized(diction, "cost_per_kWe_net_ish", False)
134+
best_dict_list_with_kwarg.append([best_dict, kwarg])
135+
136+
# Put the result in the queue
137+
result_queue.put([dict_list_with_kwargs, best_dict_list_with_kwarg, sweep_label])
138+
#result_queue.put([result_dict_list])
139+
140+
# Clear memory
141+
del input_dict_list
142+
del result_dict_list
143+
input_dict_list = None
144+
gc.collect()
145+
146+
print(f"Memory after cleanup: {psutil.Process().memory_info().rss / 1024 / 1024:.2f} MB")
147+
#del dict_list_with_kwargs
148+
#del best_dict_list_with_kwarg
149+
#
150+
151+
return
152+
153+
def open_file_set_w_label(filenames_list_w_label, key_list=[]):
154+
155+
list_of_dict_list_w_kwargs = []
156+
list_of_best_dict_list_with_kwarg = []
157+
final_sweep_labels = []
158+
159+
# Create a queue to hold the results
160+
with Manager() as manager:
161+
result_queue = manager.Queue()
162+
163+
with concurrent.futures.ProcessPoolExecutor(max_workers=8) as executor:
164+
futures = [executor.submit(process_sweep_fileset_optimized, filenames_w_label[0], filenames_w_label[1], filenames_w_label[2],
165+
result_queue, key_list)
166+
for filenames_w_label in filenames_list_w_label]
167+
168+
# Wait for all futures to complete
169+
concurrent.futures.wait(futures)
170+
171+
# Collect all results from the queue
172+
while not result_queue.empty():
173+
dict_list_w_kwargs, best_dict_list_with_kwarg, sweep_label = result_queue.get()
174+
list_of_dict_list_w_kwargs.append(dict_list_w_kwargs)
175+
list_of_best_dict_list_with_kwarg.append(best_dict_list_with_kwarg)
176+
final_sweep_labels.append(sweep_label)
177+
178+
# Create mapping based on the original order in filenames_list_w_label
179+
label_order = [label for _, label, _ in filenames_list_w_label]
180+
expected_order = {label: idx for idx, label in enumerate(label_order)}
181+
182+
# Sort the collected results to match the original order
183+
sorted_indices = []
184+
for i, label in enumerate(final_sweep_labels):
185+
sorted_indices.append(expected_order[label])
186+
187+
# Apply sorting to all three lists
188+
list_of_dict_list_w_kwargs = [x for _, x in sorted(zip(sorted_indices, list_of_dict_list_w_kwargs))]
189+
list_of_best_dict_list_with_kwarg = [x for _, x in sorted(zip(sorted_indices, list_of_best_dict_list_with_kwarg))]
190+
final_sweep_labels = [x for _, x in sorted(zip(sorted_indices, final_sweep_labels))]
191+
192+
return list_of_dict_list_w_kwargs, list_of_best_dict_list_with_kwarg, final_sweep_labels

0 commit comments

Comments
 (0)