|
19 | 19 | import inspect |
20 | 20 | import sparging.correlations as c |
21 | 21 |
|
22 | | -from sparging.config import * |
23 | | - |
24 | | - |
25 | | -@dataclass |
26 | | -class SimulationResults: |
27 | | - times: list |
28 | | - c_T2_solutions: list |
29 | | - y_T2_solutions: list |
30 | | - x_ct: np.ndarray |
31 | | - x_y: np.ndarray |
32 | | - inventories_T2_salt: np.ndarray |
33 | | - source_T2: list |
34 | | - fluxes_T2: list |
35 | | - sim_input: SimulationInput |
36 | | - |
37 | | - keys_to_ignore_results = [ # TODO do it the other way: keys_to_include_results |
38 | | - "c_T2_solutions", |
39 | | - "y_T2_solutions", |
40 | | - "x_ct", |
41 | | - "x_y", |
42 | | - "inventories_T2_salt", |
43 | | - "times", |
44 | | - "source_T2", |
45 | | - "fluxes_T2", |
46 | | - "sim_input", |
47 | | - ] |
48 | | - |
49 | | - namespace = { |
50 | | - "ramp": lambda s, e: helpers.string_to_ramp(times, s, e), |
51 | | - "step": lambda s: helpers.string_to_step(times, s), |
52 | | - } |
53 | | - |
54 | | - def to_yaml(self, output_path: str): |
55 | | - sim_dict = self.sim_input.__dict__.copy() |
56 | | - helpers.setup_yaml() |
57 | | - |
58 | | - # structure the output |
59 | | - output = { |
60 | | - "metadata": { |
61 | | - "git_commit": helpers.get_git_hash(), |
62 | | - "date": datetime.now().isoformat(), |
63 | | - }, |
64 | | - } |
65 | | - sim_dict.pop( |
66 | | - "quantities_dict" |
67 | | - ) # remove quantities_dict from input for cleaner output |
68 | | - |
69 | | - output["input"] = {} |
70 | | - for key, value in sim_dict.items(): |
71 | | - output["input"][key] = str(value) |
72 | | - |
73 | | - output["results"] = self.__dict__.copy() |
74 | | - # remove c_T2_solutions and y_T2_solutions from results to avoid dumping large arrays in yaml, they can be saved separately if needed |
75 | | - for key in self.keys_to_ignore_results: |
76 | | - output["results"].pop(key, None) |
77 | | - |
78 | | - with open(output_path, "w") as f: |
79 | | - yaml.dump(output, f, sort_keys=False) |
80 | | - |
81 | | - def to_json(self, output_path: str): |
82 | | - sim_dict = self.sim_input.quantities_dict.copy() |
83 | | - |
84 | | - # structure the output |
85 | | - output = { |
86 | | - "metadata": { |
87 | | - "git_commit": helpers.get_git_hash(), |
88 | | - "date": datetime.now().isoformat(), |
89 | | - }, |
90 | | - } |
91 | | - if sim_dict.get("inputed"): |
92 | | - output["input parameters"] = sim_dict["inputed"] |
93 | | - if sim_dict.get("computed"): |
94 | | - output["calculated properties"] = sim_dict["computed"] |
95 | | - output["results"] = self.__dict__.copy() |
96 | | - |
97 | | - # remove c_T2_solutions and y_T2_solutions from results to avoid dumping large arrays in yaml, they can be saved separately if needed |
98 | | - for key in self.keys_to_ignore_results: |
99 | | - output["results"].pop(key, None) |
100 | | - |
101 | | - for key, value in output.items(): |
102 | | - if isinstance(value, np.ndarray): |
103 | | - # convert numpy arrays to lists for JSON serialization |
104 | | - output[key] = value.tolist() |
105 | | - print( |
106 | | - "found list in results, converting to list for JSON serialization" |
107 | | - ) |
108 | | - |
109 | | - with open(output_path, "w") as f: |
110 | | - json.dump(output, f, indent=3) |
111 | | - |
112 | | - def profiles_to_csv(self, output_path: str): |
113 | | - """save c_T2 and y_T2 profiles at all time steps to csv files, one for c_T2 and one for y_T2, with columns for each time step""" |
114 | | - import pandas as pd |
115 | | - |
116 | | - df_c_T2 = pd.DataFrame({"x": self.x_ct}) |
117 | | - df_y_T2 = pd.DataFrame({"x": self.x_y}) |
118 | | - |
119 | | - # add one column for each profile |
120 | | - for i, (c_T2_profile, y_T2_profile) in enumerate( |
121 | | - zip(self.c_T2_solutions, self.y_T2_solutions) |
122 | | - ): |
123 | | - df_c_T2[f"c_T2_t{i}"] = c_T2_profile |
124 | | - df_y_T2[f"y_T2_t{i}"] = y_T2_profile |
125 | | - |
126 | | - df_c_T2.to_csv(output_path + "_c_T2.csv", index=False) |
127 | | - df_y_T2.to_csv(output_path + "_y_T2.csv", index=False) |
128 | | - |
| 22 | +from sparging.config import ureg, const_R, const_g, VERBOSE |
129 | 23 |
|
130 | 24 | hours_to_seconds = 3600 |
131 | 25 | days_to_seconds = 24 * hours_to_seconds |
@@ -315,6 +209,112 @@ def __str__(self): |
315 | 209 | ) |
316 | 210 |
|
317 | 211 |
|
| 212 | +@dataclass |
| 213 | +class SimulationResults: |
| 214 | + times: list |
| 215 | + c_T2_solutions: list |
| 216 | + y_T2_solutions: list |
| 217 | + x_ct: np.ndarray |
| 218 | + x_y: np.ndarray |
| 219 | + inventories_T2_salt: np.ndarray |
| 220 | + source_T2: list |
| 221 | + fluxes_T2: list |
| 222 | + sim_input: SimulationInput |
| 223 | + |
| 224 | + keys_to_ignore_results = [ # TODO do it the other way: keys_to_include_results |
| 225 | + "c_T2_solutions", |
| 226 | + "y_T2_solutions", |
| 227 | + "x_ct", |
| 228 | + "x_y", |
| 229 | + "inventories_T2_salt", |
| 230 | + "times", |
| 231 | + "source_T2", |
| 232 | + "fluxes_T2", |
| 233 | + "sim_input", |
| 234 | + ] |
| 235 | + |
| 236 | + # FIXME |
| 237 | + namespace = { |
| 238 | + "ramp": lambda s, e: helpers.string_to_ramp(times, s, e), |
| 239 | + "step": lambda s: helpers.string_to_step(times, s), |
| 240 | + } |
| 241 | + |
| 242 | + def to_yaml(self, output_path: str): |
| 243 | + sim_dict = self.sim_input.__dict__.copy() |
| 244 | + helpers.setup_yaml() |
| 245 | + |
| 246 | + # structure the output |
| 247 | + output = { |
| 248 | + "metadata": { |
| 249 | + "git_commit": helpers.get_git_hash(), |
| 250 | + "date": datetime.now().isoformat(), |
| 251 | + }, |
| 252 | + } |
| 253 | + sim_dict.pop( |
| 254 | + "quantities_dict" |
| 255 | + ) # remove quantities_dict from input for cleaner output |
| 256 | + |
| 257 | + output["input"] = {} |
| 258 | + for key, value in sim_dict.items(): |
| 259 | + output["input"][key] = str(value) |
| 260 | + |
| 261 | + output["results"] = self.__dict__.copy() |
| 262 | + # remove c_T2_solutions and y_T2_solutions from results to avoid dumping large arrays in yaml, they can be saved separately if needed |
| 263 | + for key in self.keys_to_ignore_results: |
| 264 | + output["results"].pop(key, None) |
| 265 | + |
| 266 | + with open(output_path, "w") as f: |
| 267 | + yaml.dump(output, f, sort_keys=False) |
| 268 | + |
| 269 | + def to_json(self, output_path: str): |
| 270 | + sim_dict = self.sim_input.quantities_dict.copy() |
| 271 | + |
| 272 | + # structure the output |
| 273 | + output = { |
| 274 | + "metadata": { |
| 275 | + "git_commit": helpers.get_git_hash(), |
| 276 | + "date": datetime.now().isoformat(), |
| 277 | + }, |
| 278 | + } |
| 279 | + if sim_dict.get("inputed"): |
| 280 | + output["input parameters"] = sim_dict["inputed"] |
| 281 | + if sim_dict.get("computed"): |
| 282 | + output["calculated properties"] = sim_dict["computed"] |
| 283 | + output["results"] = self.__dict__.copy() |
| 284 | + |
| 285 | + # remove c_T2_solutions and y_T2_solutions from results to avoid dumping large arrays in yaml, they can be saved separately if needed |
| 286 | + for key in self.keys_to_ignore_results: |
| 287 | + output["results"].pop(key, None) |
| 288 | + |
| 289 | + for key, value in output.items(): |
| 290 | + if isinstance(value, np.ndarray): |
| 291 | + # convert numpy arrays to lists for JSON serialization |
| 292 | + output[key] = value.tolist() |
| 293 | + print( |
| 294 | + "found list in results, converting to list for JSON serialization" |
| 295 | + ) |
| 296 | + |
| 297 | + with open(output_path, "w") as f: |
| 298 | + json.dump(output, f, indent=3) |
| 299 | + |
| 300 | + def profiles_to_csv(self, output_path: str): |
| 301 | + """save c_T2 and y_T2 profiles at all time steps to csv files, one for c_T2 and one for y_T2, with columns for each time step""" |
| 302 | + import pandas as pd |
| 303 | + |
| 304 | + df_c_T2 = pd.DataFrame({"x": self.x_ct}) |
| 305 | + df_y_T2 = pd.DataFrame({"x": self.x_y}) |
| 306 | + |
| 307 | + # add one column for each profile |
| 308 | + for i, (c_T2_profile, y_T2_profile) in enumerate( |
| 309 | + zip(self.c_T2_solutions, self.y_T2_solutions) |
| 310 | + ): |
| 311 | + df_c_T2[f"c_T2_t{i}"] = c_T2_profile |
| 312 | + df_y_T2[f"y_T2_t{i}"] = y_T2_profile |
| 313 | + |
| 314 | + df_c_T2.to_csv(output_path + "_c_T2.csv", index=False) |
| 315 | + df_y_T2.to_csv(output_path + "_y_T2.csv", index=False) |
| 316 | + |
| 317 | + |
318 | 318 | def solve( |
319 | 319 | input: SimulationInput, t_final: float, t_irr: float | list, t_sparging: list = None |
320 | 320 | ): |
|
0 commit comments