Skip to content

Commit

Permalink
Fixes for group_index_step validator (#1143)
Browse files Browse the repository at this point in the history
Signed-off-by: Lucas Heitzmann Gabrielli <lucas@flexcompute.com>
  • Loading branch information
lucas-flexcompute committed Sep 11, 2023
1 parent faac469 commit 42ea215
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
### Changed
### Fixed
- Validation for `ModeSpec.group_index_step` working incorrectly if the value was set to 1.

## [2.4.0] - 2023-9-11

Expand Down
11 changes: 11 additions & 0 deletions tests/test_components/test_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ def test_bend_axis_not_given():
def test_glancing_incidence():
with pytest.raises(pydantic.ValidationError):
_ = td.ModeSpec(angle_theta=np.pi / 2)

def test_group_index_step_validation():
with pytest.raises(pydantic.ValidationError):
_ = td.ModeSpec(group_index_step=1.0)

ms = td.ModeSpec(group_index_step=True)
assert ms.group_index_step == td.components.mode.GROUP_INDEX_STEP

ms = td.ModeSpec(group_index_step=False)
assert ms.group_index_step is False
assert not ms.group_index_step > 0
18 changes: 13 additions & 5 deletions tidy3d/components/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ModeSpec(Tidy3dBaseModel):
"If ``None`` no mode tracking is performed.",
)

group_index_step: Union[bool, pd.PositiveFloat] = pd.Field(
group_index_step: Union[pd.PositiveFloat, bool] = pd.Field(
False,
title="Frequency step for group index computation",
description="Control the computation of the group index alongside the effective index. If "
Expand All @@ -130,16 +130,24 @@ def glancing_incidence(cls, val):
)
return val

@pd.validator("group_index_step")
# Must be executed before type validation by pydantic, otherwise True is converted to 1.0
@pd.validator("group_index_step", pre=True)
def assign_default_on_true(cls, val):
"""Assing the default fractional frequency step value if not provided."""
"""Assign the default fractional frequency step value if not provided."""
if val is True:
return GROUP_INDEX_STEP
return val

@pd.validator("group_index_step")
def check_group_step_size(cls, val):
"""Ensure a reasonable group index step is used."""
if val >= 1:
raise ValidationError("Parameter 'group_index_step' must be less than 1.")
raise ValidationError(
"Parameter 'group_index_step' is a fractional value. It must be less than 1."
)
return val

@pd.root_validator()
@pd.root_validator(skip_on_failure=True)
def check_precision(cls, values):
"""Verify critical ModeSpec settings for group index calculation."""
if values["group_index_step"] > 0:
Expand Down

0 comments on commit 42ea215

Please sign in to comment.