From b473e667ff76cef68334b5cce0e15b0810a56c65 Mon Sep 17 00:00:00 2001 From: Lucas <119979961+lucas-flexcompute@users.noreply.github.com> Date: Tue, 10 Oct 2023 12:58:32 -0300 Subject: [PATCH] Issue a warning when accessing unavailable group index data (#1169) (#1178) Signed-off-by: Lucas Heitzmann Gabrielli --- tests/test_plugins/test_mode_solver.py | 7 ++++++- tidy3d/components/data/dataset.py | 14 +++++++++++++- tidy3d/components/data/monitor_data.py | 18 +++++++++++++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/tests/test_plugins/test_mode_solver.py b/tests/test_plugins/test_mode_solver.py index 2cd9f6c66..c3b04091a 100644 --- a/tests/test_plugins/test_mode_solver.py +++ b/tests/test_plugins/test_mode_solver.py @@ -531,7 +531,7 @@ def test_mode_solver_2D(): @pytest.mark.parametrize("local", [True, False]) @responses.activate -def test_group_index(mock_remote_api, local): +def test_group_index(mock_remote_api, log_capture, local): """Test group index calculation""" simulation = td.Simulation( @@ -570,6 +570,11 @@ def test_group_index(mock_remote_api, local): modes = ms.solve() if local else msweb.run(ms) if local: assert modes.n_group is None + assert len(log_capture) == 1 + assert log_capture[0][0] == 30 + assert "ModeSpec" in log_capture[0][1] + _ = modes.n_group + assert len(log_capture) == 1 # Group index calculated ms = ModeSolver( diff --git a/tidy3d/components/data/dataset.py b/tidy3d/components/data/dataset.py index b5f4fa418..81a0ff5fa 100644 --- a/tidy3d/components/data/dataset.py +++ b/tidy3d/components/data/dataset.py @@ -329,8 +329,9 @@ class ModeSolverDataset(ElectromagneticFieldDataset): description="Complex-valued effective propagation constants associated with the mode.", ) - n_group: ModeIndexDataArray = pd.Field( + n_group_raw: ModeIndexDataArray = pd.Field( None, + alias="n_group", title="Group Index", description="Index associated with group velocity of the mode.", ) @@ -351,6 +352,17 @@ def k_eff(self): """Imaginary part of the propagation index.""" return self.n_complex.imag + @property + def n_group(self): + """Group index.""" + if self.n_group_raw is None: + log.warning( + "The group index was not computed. To calculate group index, pass " + "'group_index_step = True' in the 'ModeSpec'.", + log_once=True, + ) + return self.n_group_raw + def plot_field(self, *args, **kwargs): """Warn user to use the :class:`.ModeSolver` ``plot_field`` function now.""" raise DeprecationWarning( diff --git a/tidy3d/components/data/monitor_data.py b/tidy3d/components/data/monitor_data.py index b30cf4250..eb0b0a95a 100644 --- a/tidy3d/components/data/monitor_data.py +++ b/tidy3d/components/data/monitor_data.py @@ -1179,7 +1179,7 @@ def _group_index_post_process(self, frequency_step: float) -> ModeSolverData: ) # remove data corresponding to frequencies used only for group index calculation - update_dict = {"n_complex": self.n_complex.isel(f=center), "n_group": n_group} + update_dict = {"n_complex": self.n_complex.isel(f=center), "n_group_raw": n_group} for key, field in self.field_components.items(): update_dict[key] = field.isel(f=center) @@ -1313,7 +1313,7 @@ def modes_info(self) -> xr.Dataset: "wg TE fraction": self.pol_fraction_waveguide["te"], "wg TM fraction": self.pol_fraction_waveguide["tm"], "mode area": self.mode_area, - "group index": self.n_group, + "group index": self.n_group_raw, # Use raw field to avoid issuing a warning } return xr.Dataset(data_vars=info) @@ -1394,8 +1394,9 @@ class ModeData(MonitorData): description="Complex-valued effective propagation constants associated with the mode.", ) - n_group: ModeIndexDataArray = pd.Field( + n_group_raw: ModeIndexDataArray = pd.Field( None, + alias="n_group", title="Group Index", description="Index associated with group velocity of the mode.", ) @@ -1410,6 +1411,17 @@ def k_eff(self): """Imaginary part of the propagation index.""" return self.n_complex.imag + @property + def n_group(self): + """Group index.""" + if self.n_group_raw is None: + log.warning( + "The group index was not computed. To calculate group index, pass " + "'group_index_step = True' in the 'ModeSpec'.", + log_once=True, + ) + return self.n_group_raw + def normalize(self, source_spectrum_fn) -> ModeData: """Return copy of self after normalization is applied using source spectrum function.""" source_freq_amps = source_spectrum_fn(self.amps.f)[None, :, None]