Coverage for dragonfly_sim/utils/data_handler.py: 52%
89 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-21 00:13 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-21 00:13 +0000
1import os
2import numpy as np
3from matplotlib import pyplot as plt
5def split_list_of_tuples_into_separate_lists(inp_list, iter_acceptance_list=None):
6 list_of_lists = []
7 for i in range(len(inp_list[0])):
8 single_list_i = [element[i] for element in inp_list]
10 if i == 0 and iter_acceptance_list is not None:
11 elements_to_include = np.isin(single_list_i, iter_acceptance_list)
13 if iter_acceptance_list is not None:
14 single_list_i = list(np.array(single_list_i)[elements_to_include])
16 list_of_lists += [single_list_i]
17 return list_of_lists
20class DataStore():
21 def __init__(self):
22 # Initialize arrays that store outputs
24 # Relative residual norms eta = ||r||/||u||. These, not absolute norms,
25 # are what to plot or threshold on: eta is free of the sqrt(n_dofs) and
26 # state-magnitude factors that make absolute norms incomparable
27 # between meshes.
28 self.fom_relative_residuals = []
30 # objective function histories
31 self.FOM_force_coefficients = []
32 # Dimensional (unnormalized) FOM lift/drag/moment forces, same
33 # (eval_idx, D, L, M_1, ...) tuple layout and eval_idx indexing as
34 # FOM_force_coefficients' (eval_idx, c_d, c_l, c_m_1, ...), with one
35 # moment entry per moment of the postprocessor, in its order.
36 self.FOM_forces = []
38 # Parameter vector histories
39 self.parameter_vectors_per_iteration = []
41 # Wall times of the full-order model
42 self.FOM_walltime = []
44 # POD reduced-order model (core/reduced_order_model.py), per ROM solve:
45 # wall time, eta, (eval_idx, c_d, c_l, c_m_1, ...) like
46 # FOM_force_coefficients, whether eta met the acceptance threshold,
47 # whether the LSPG solver itself is considered converged (ROM_lspg_converged),
48 # and (eval_idx, dc_d, dc_l, dc_m_1, ...) ROM - FOM when the FOM also ran;
49 # the snapshot singular values after every snapshot added
50 self.ROM_walltime = []
51 self.rom_relative_residuals = []
52 self.ROM_force_coefficients = []
53 self.ROM_meets_threshold = []
54 self.ROM_lspg_converged = []
55 self.rom_coefficient_errors = []
56 self.global_snapshot_sing_vals_per_iteration = []
58 # Per adjoint solve (one per output row of a derivative evaluation):
59 # (eval_idx, 'rom' or 'fom', wall time). 'rom' is the reduced adjoint
60 # of an accepted ROM solution, 'fom' the full-order adjoint.
61 self.adjoint_solves = []
63 # Per ROM adjoint, when DG_windtunnel_model's compute_all_adjoint_errors
64 # is on: (eval_idx, output row, ||psi_rom - psi_fom||_2, relative to
65 # ||psi_fom||_2, ||psi_fom||_2), psi_fom being the full-order adjoint at the
66 # same state and right-hand side. The row numbers the adjoint solves of
67 # one evaluation in CSDL's order (objective and constraint rows).
68 self.adjoint_errors = []
70 # ROM - FOM state errors at the evaluations where the FOM also ran:
71 # (eval_idx, value), the L2 norm over the domain and over the wall of
72 # u_FOM - u_ROM, absolute and relative to the L2 norm of u_FOM
73 self.absolute_l2_errors_per_iteration = []
74 self.relative_l2_errors_per_iteration = []
75 self.absolute_l2_bdry_errors_per_iteration = []
76 self.relative_l2_bdry_errors_per_iteration = []
78 # Every output quantity of the postprocessor by name ("L", "D", "c_l",
79 # "c_d", "M_<name>", "c_m_<name>"): {name: [(eval_idx, value), ...]}
80 # for the full-order and the reduced-order solution of an evaluation.
81 # ROM - FOM errors follow from output_errors().
82 self.FOM_outputs = {}
83 self.ROM_outputs = {}
85 # Number of DoFs in simulation
86 self.fe_dofs = 0
87 # Number of design parameters in optimization
88 self.num_var = 0
90 def record_outputs(self, kind, eval_idx, values):
91 """Append the named output values of one evaluation; kind is 'FOM' or 'ROM'."""
92 store = {"FOM": self.FOM_outputs, "ROM": self.ROM_outputs}[kind]
93 for name, value in values.items():
94 store.setdefault(name, []).append((eval_idx, float(value)))
96 @property
97 def output_names(self):
98 return list(self.ROM_outputs) if self.ROM_outputs else list(self.FOM_outputs)
100 def output_errors(self, name, relative=True):
101 """(eval_idx, error) arrays of ROM - FOM (divided by |FOM| if relative)
102 of the output `name`, for the evaluations that have both. A repeated
103 eval_idx in the FOM record (the baseline solve) keeps its last entry."""
104 fom = {int(i): v for i, v in self.FOM_outputs.get(name, [])}
105 pairs = [(int(i), v, fom[int(i)]) for i, v in self.ROM_outputs.get(name, []) if int(i) in fom]
106 if not pairs:
107 return np.zeros(0, dtype=int), np.zeros(0)
108 idx, rom, fom_vals = (np.array(c) for c in zip(*pairs))
109 err = rom - fom_vals
110 return idx.astype(int), (err / np.abs(fom_vals) if relative else err)
112 def compile_store_in_dict(self):
113 data_dict = {}
114 for data in vars(self):
115 # print(data)
116 # print(self.__dict__[str(data)])
117 data_dict[str(data)] = self.__dict__[str(data)]
118 return data_dict
120 def write_store_to_numpy_file(self, save_folder="Result_dicts", save_filename="POD_deployment.npy"):
121 save_location = save_folder + "/" + save_filename
122 os.makedirs(save_folder, exist_ok=True)
123 # create dictionary with all data
124 data_dict = self.compile_store_in_dict()
125 # save data_dict to location
126 np.save(save_location, data_dict)
127 print("Saved data dictionary to {}".format(save_location))
129 def open_dict_and_import_data(self, file_folder="Result_dicts", filename="POD_deployment.npy"):
130 file_location = file_folder + "/" + filename
131 data_dict = np.load(file_location, allow_pickle=True)
132 data_dict = data_dict.item()
133 for key, value in data_dict.items():
134 setattr(self, key, value)
136 # TODO: Add routine to unpack input list(s) for plotting, move actual plotting routines to separate utility file
139def plot_walltime_boxplots(data_stores, data_legend, yrange=(0, 3), show_yaxis_label=True):
140 # One boxplot of the full-order-model wall times per data store, with a
141 # horizontal line at the mean wall time pooled over all data stores
142 fom_walltime_list = []
143 for data_store in data_stores:
144 _, FOM_walltime = zip(*data_store.FOM_walltime)
145 fom_walltime_list += [np.array(FOM_walltime)]
147 fig = plt.figure(figsize=(2.2,3.))
148 ax = fig.add_subplot(111)
150 fom_walltime_mean = np.mean(np.concatenate(fom_walltime_list))
151 ax.plot([0.5, len(data_stores)+0.5], [fom_walltime_mean, fom_walltime_mean], 'k', linewidth=3)
153 ax.boxplot(fom_walltime_list, widths=0.8)
155 # adding horizontal grid lines
156 ax.yaxis.grid(True)
157 # ax.set_yscale("log")
158 ax.set_xticks([y + 1 for y in range(len(fom_walltime_list))],
159 labels=data_legend)
160 # ax.set_xlabel(r'Solver')
161 if show_yaxis_label:
162 ax.set_ylabel(r'Total wall time [s]')
163 # else:
164 # # frame = plt.gca()
165 # ax.axes.yaxis.set_ticklabels([])
167 ax.tick_params(axis='x', labelrotation=45)
169 for tick in ax.xaxis.get_major_ticks():
170 tick.label1.set_horizontalalignment('center')
172 ax.set_ylim(yrange[0], yrange[1])
174 plt.subplots_adjust(left=0.23, right=0.955, bottom=0.19, top=0.95)
175 # plt.rcParams.update({'font.size': 22})
176 plt.show()
179# def plot_force_coefficients(data_stores)