Skip to content

Commit 275b709

Browse files
committed
Add validator to ensure that angle_phi is a multiple of pi when angle_rotation is used
1 parent 954a43a commit 275b709

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

tests/test_components/test_mode.py

+10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ def test_group_index_step_validation():
6565
assert not ms.group_index_step > 0
6666

6767

68+
def test_angle_rotation_with_phi():
69+
"""Test the `angle_rotation_with_phi` validator."""
70+
71+
td.ModeSpec(angle_phi=np.pi, angle_rotation=True)
72+
73+
# Case where angle_phi is not a multiple of np.pi and angle_rotation is True
74+
with pytest.raises(pydantic.ValidationError):
75+
td.ModeSpec(angle_phi=np.pi / 2, angle_rotation=True)
76+
77+
6878
def get_mode_sim():
6979
mode_spec = MODE_SPEC.updated_copy(filter_pol="tm")
7080
sim = td.ModeSimulation(

tidy3d/components/mode_spec.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ class ModeSpec(Tidy3dBaseModel):
138138
"a reference plane normal to the structure's azimuthal direction. Then, the fields are rotated "
139139
"to align with the mode plane, using the 'n_eff' calculated at the reference plane. The second option can "
140140
"produce more accurate results, but more care must be taken, for example, in ensuring that the "
141-
"original mode plane intersects the correct geometries in the simulation with rotated structures.",
141+
"original mode plane intersects the correct geometries in the simulation with rotated structures. "
142+
"Note: currently only supported when 'angle_phi' is a multiple of 'np.pi'.",
142143
)
143144

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

223224
return values
225+
226+
@pd.validator("angle_rotation")
227+
def angle_rotation_with_phi(cls, val, values):
228+
"""Currently ``angle_rotation`` is only supported with ``angle_phi % np.pi == 0``."""
229+
if val and not isclose(values["angle_phi"] % np.pi, 0):
230+
raise ValidationError(
231+
"Parameter 'angle_phi' must be a multiple of 'np.pi' when 'angle_rotation' is "
232+
"enabled."
233+
)
234+
return val

0 commit comments

Comments
 (0)