Skip to content

Commit 3ef0d66

Browse files
committed
test for sim results serialization
1 parent 79141cb commit 3ef0d66

2 files changed

Lines changed: 73 additions & 5 deletions

File tree

src/sparging/model.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
from sparging.inputs import SimulationInput
2323
import pint
2424
from collections.abc import Callable
25+
import logging
26+
27+
logger = logging.getLogger(__name__)
2528

2629
hours_to_seconds = 3600
2730
days_to_seconds = 24 * hours_to_seconds
@@ -103,25 +106,25 @@ def serialize_output(self):
103106
output["simulation parameters"][key] = str(value)
104107
output["results"] = self.__dict__.copy()
105108

106-
# remove c_T2_solutions and y_T2_solutions from results to avoid dumping large arrays in yaml, they can be saved separately if needed
109+
# remove objects incompatible with serialization
107110
for key in self.keys_to_ignore_results:
108111
output["results"].pop(key, None)
109112

110113
for key, value in output.items():
111114
if isinstance(value, np.ndarray):
112115
# convert numpy arrays to lists for JSON serialization
113116
output[key] = value.tolist()
114-
print(
117+
logger.verbose(
115118
"found list in results, converting to list for JSON serialization"
116119
)
117120
if isinstance(value, pint.Quantity):
118121
# convert pint.Quantity to string for JSON serialization
119122
output[key] = value.to_base_units().magnitude
120-
print(
123+
logger.verbose(
121124
"found pint.Quantity in results, converting to magnitude for JSON serialization"
122125
)
123126
else:
124-
print(
127+
logger.verbose(
125128
f"{key} is of type {type(value)}, no conversion needed for JSON serialization"
126129
)
127130

@@ -131,7 +134,7 @@ def serialize_output(self):
131134
output["results"][k] = {"value": v.magnitude, "units": units}
132135

133136
if isinstance(v.magnitude, np.ndarray):
134-
print(
137+
logger.verbose(
135138
f"found pint.Quantity with numpy array magnitude in results[{k}], converting to list for JSON serialization"
136139
)
137140
output["results"][k]["value"] = v.magnitude.tolist()

test/test_simulation_results.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from sparging.model import Simulation, SimulationResults
2+
from sparging.inputs import (
3+
LIBRA_PI_GEOM,
4+
LIBRA_PI_MAT,
5+
LIBRA_PI_OPERATING_PARAMS,
6+
LIBRA_PI_SPARGING_PARAMS,
7+
SimulationInput,
8+
ureg,
9+
)
10+
import numpy as np
11+
from pathlib import Path
12+
13+
14+
def test_simulation_results_serialization(tmp_path):
15+
# BUILD
16+
my_input = SimulationInput.from_parameters(
17+
LIBRA_PI_GEOM,
18+
LIBRA_PI_MAT,
19+
LIBRA_PI_OPERATING_PARAMS,
20+
LIBRA_PI_SPARGING_PARAMS,
21+
)
22+
23+
my_sim = Simulation(
24+
my_input,
25+
t_final=2 * ureg.hour,
26+
)
27+
28+
res = my_sim.solve(fast_solve=True)
29+
30+
# RUN
31+
# serialization
32+
path_json = Path(tmp_path).joinpath("results.json")
33+
path_pkl = Path(tmp_path).joinpath("results.pkl")
34+
res.to_json(path_json)
35+
res.to_pickle(path_pkl)
36+
37+
# deserialization
38+
new_res_json = SimulationResults.from_json(path_json)
39+
new_res_pickle = SimulationResults.from_pickle(path_pkl)
40+
41+
# TEST
42+
assert len(res.times) == len(new_res_json.times), (
43+
"JSON Times arrays have different lengths"
44+
)
45+
assert len(res.c_T2_solutions) == len(new_res_json.c_T2_solutions), (
46+
"JSON c_T2_solutions arrays have different lengths"
47+
)
48+
49+
assert np.allclose(res.times, new_res_json.times), "JSON Times arrays are not close"
50+
assert np.allclose(res.c_T2_solutions, new_res_json.c_T2_solutions), (
51+
"JSON c_T2_solutions arrays are not close"
52+
)
53+
54+
assert len(res.times) == len(new_res_pickle.times), (
55+
"Pickle Times arrays have different lengths"
56+
)
57+
assert len(res.c_T2_solutions) == len(new_res_pickle.c_T2_solutions), (
58+
"Pickle c_T2_solutions arrays have different lengths"
59+
)
60+
assert np.allclose(res.times, new_res_pickle.times), (
61+
"Pickle Times arrays are not close"
62+
)
63+
assert np.allclose(res.c_T2_solutions, new_res_pickle.c_T2_solutions), (
64+
"Pickle c_T2_solutions arrays are not close"
65+
)

0 commit comments

Comments
 (0)