Skip to content

Add validator to ensure that angle_phi is a multiple of pi when angle… #2346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/test_components/test_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ def test_group_index_step_validation():
assert not ms.group_index_step > 0


def test_angle_rotation_with_phi():
"""Test the `angle_rotation_with_phi` validator."""

td.ModeSpec(angle_phi=np.pi, angle_rotation=True)

# Case where angle_phi is not a multiple of np.pi and angle_rotation is True
with pytest.raises(pydantic.ValidationError):
td.ModeSpec(angle_phi=np.pi / 2, angle_rotation=True)


def get_mode_sim():
mode_spec = MODE_SPEC.updated_copy(filter_pol="tm")
sim = td.ModeSimulation(
Expand Down
13 changes: 12 additions & 1 deletion tidy3d/components/mode_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ class ModeSpec(Tidy3dBaseModel):
"a reference plane normal to the structure's azimuthal direction. Then, the fields are rotated "
"to align with the mode plane, using the 'n_eff' calculated at the reference plane. The second option can "
"produce more accurate results, but more care must be taken, for example, in ensuring that the "
"original mode plane intersects the correct geometries in the simulation with rotated structures.",
"original mode plane intersects the correct geometries in the simulation with rotated structures. "
"Note: currently only supported when 'angle_phi' is a multiple of 'np.pi'.",
)

track_freq: Union[TrackFreq, None] = pd.Field(
Expand Down Expand Up @@ -221,3 +222,13 @@ def check_precision(cls, values):
)

return values

@pd.validator("angle_rotation")
def angle_rotation_with_phi(cls, val, values):
"""Currently ``angle_rotation`` is only supported with ``angle_phi % np.pi == 0``."""
if val and not isclose(values["angle_phi"] % np.pi, 0):
raise ValidationError(
"Parameter 'angle_phi' must be a multiple of 'np.pi' when 'angle_rotation' is "
"enabled."
)
return val
Loading