|
| 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