Skip to content

Commit

Permalink
Issue a warning when accessing unavailable group index data (#1169) (#…
Browse files Browse the repository at this point in the history
…1178)

Signed-off-by: Lucas Heitzmann Gabrielli <[email protected]>
  • Loading branch information
lucas-flexcompute authored Oct 10, 2023
1 parent e7ec44e commit b473e66
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
7 changes: 6 additions & 1 deletion tests/test_plugins/test_mode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
14 changes: 13 additions & 1 deletion tidy3d/components/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)
Expand All @@ -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(
Expand Down
18 changes: 15 additions & 3 deletions tidy3d/components/data/monitor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.",
)
Expand All @@ -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]
Expand Down

0 comments on commit b473e66

Please sign in to comment.