From 53193a8788be0edbd94e3f9e2e390a29e0310205 Mon Sep 17 00:00:00 2001 From: dbochkov-flexcompute Date: Mon, 8 Jul 2024 22:48:17 -0500 Subject: [PATCH] dummy version updater for base simulation --- tests/test_components/test_eme.py | 11 +++++++++++ tests/test_components/test_heat.py | 11 +++++++++++ tidy3d/components/base_sim/simulation.py | 12 ++++++++++++ 3 files changed, 34 insertions(+) diff --git a/tests/test_components/test_eme.py b/tests/test_components/test_eme.py index d0d5f1964..4ca218965 100644 --- a/tests/test_components/test_eme.py +++ b/tests/test_components/test_eme.py @@ -86,6 +86,17 @@ def make_eme_sim(): return sim +def test_sim_version_update(log_capture): + sim = make_eme_sim() + sim_dict = sim.dict() + sim_dict["version"] = "ancient_version" + + with AssertLogLevel(log_capture, "WARNING"): + sim_new = td.EMESimulation.parse_obj(sim_dict) + + assert sim_new.version == td.__version__ + + def test_eme_grid(): sim_geom = td.Box(size=(4, 4, 4), center=(0, 0, 0)) axis = 2 diff --git a/tests/test_components/test_heat.py b/tests/test_components/test_heat.py index 15edf5a1f..20e34168b 100644 --- a/tests/test_components/test_heat.py +++ b/tests/test_components/test_heat.py @@ -552,6 +552,17 @@ def test_relative_min_dl_warning(log_capture): ) +def test_sim_version_update(log_capture): + heat_sim = make_heat_sim() + heat_sim_dict = heat_sim.dict() + heat_sim_dict["version"] = "ancient_version" + + with AssertLogLevel(log_capture, "WARNING"): + heat_sim_new = td.HeatSimulation.parse_obj(heat_sim_dict) + + assert heat_sim_new.version == td.__version__ + + @pytest.mark.parametrize("zero_dim_axis", [None, 0, 2]) def test_symmetry_expanded(zero_dim_axis): symmetry_center = [2, 0.5, 0] diff --git a/tidy3d/components/base_sim/simulation.py b/tidy3d/components/base_sim/simulation.py index 79e7f626e..6f1b19bbe 100644 --- a/tidy3d/components/base_sim/simulation.py +++ b/tidy3d/components/base_sim/simulation.py @@ -104,6 +104,18 @@ class AbstractSimulation(Box, ABC): """ Validating setup """ + @pd.root_validator(pre=True) + def _update_simulation(cls, values): + """Update the simulation if it is an earlier version.""" + + # dummy upgrade of version number + # this should be overriden by each simulation class if needed + current_version = values.get("version") + if current_version != __version__ and current_version is not None: + log.warning(f"updating {cls.__name__} from {current_version} to {__version__}") + values["version"] = __version__ + return values + # make sure all names are unique _unique_monitor_names = assert_unique_names("monitors") _unique_structure_names = assert_unique_names("structures")