Skip to content

Commit 6c86b63

Browse files
committed
feat: warning for upcoming change in waist_distance convention with direction='-'
1 parent 603c8a8 commit 6c86b63

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed

tests/test_components/test_beam.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
)
1414
from tidy3d.components.data.monitor_data import FieldData
1515

16+
from ..utils import AssertLogLevel
17+
1618
FREQS = np.linspace(1e14, 2e14, 10).tolist()
1719

1820

@@ -90,6 +92,63 @@ def test_astigmatic_gaussian_beam():
9092
assert field_data.flux == -field_data_bwd.flux
9193

9294

95+
def test_gaussian_beam_profile_backward_waist_distance_warning():
96+
center = (0, 0, 0)
97+
size = (10, 10, 0)
98+
resolution = 100
99+
100+
with AssertLogLevel(
101+
"WARNING",
102+
contains_str="GaussianBeamProfile with direction '-' and non-zero 'waist_distance'",
103+
):
104+
GaussianBeamProfile(
105+
center=center,
106+
size=size,
107+
resolution=resolution,
108+
freqs=FREQS,
109+
direction="-",
110+
waist_distance=1.0,
111+
)
112+
113+
with AssertLogLevel(None):
114+
GaussianBeamProfile(
115+
center=center,
116+
size=size,
117+
resolution=resolution,
118+
freqs=FREQS,
119+
direction="-",
120+
waist_distance=0.0,
121+
)
122+
123+
124+
def test_astigmatic_gaussian_beam_profile_backward_waist_distance_warning():
125+
center = (0, 0, 0)
126+
size = (0, 20, 20)
127+
128+
with AssertLogLevel(
129+
"WARNING",
130+
contains_str="AstigmaticGaussianBeamProfile with direction '-' and non-zero 'waist_distances'",
131+
):
132+
AstigmaticGaussianBeamProfile(
133+
center=center,
134+
size=size,
135+
freqs=FREQS,
136+
direction="-",
137+
waist_sizes=(4.0, 2.0),
138+
waist_distances=(1.0, 0.0),
139+
)
140+
141+
with AssertLogLevel(None):
142+
AstigmaticGaussianBeamProfile(
143+
center=center,
144+
size=size,
145+
freqs=FREQS,
146+
direction="-",
147+
waist_sizes=(4.0, 2.0),
148+
waist_distances=(0.0, 0.0),
149+
)
150+
151+
93152
def test_invalid_beam_size():
94153
"""
95154
Test that a beam with three nonzero size values raises a validation error.

tests/test_components/test_source.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,58 @@ def test_FieldSource():
338338
# plt.close()
339339

340340

341+
def test_gaussian_beam_backward_waist_distance_warning():
342+
g = td.GaussianPulse(freq0=1e12, fwidth=0.1e12)
343+
344+
with AssertLogLevel(
345+
"WARNING",
346+
contains_str="GaussianBeam with direction '-' and non-zero 'waist_distance'",
347+
):
348+
td.GaussianBeam(
349+
size=(0, 1, 1),
350+
source_time=g,
351+
pol_angle=np.pi / 2,
352+
direction="-",
353+
waist_distance=1.0,
354+
)
355+
356+
with AssertLogLevel(None):
357+
td.GaussianBeam(
358+
size=(0, 1, 1),
359+
source_time=g,
360+
pol_angle=np.pi / 2,
361+
direction="-",
362+
waist_distance=0.0,
363+
)
364+
365+
366+
def test_astigmatic_gaussian_beam_backward_waist_distance_warning():
367+
g = td.GaussianPulse(freq0=1e12, fwidth=0.1e12)
368+
369+
with AssertLogLevel(
370+
"WARNING",
371+
contains_str="AstigmaticGaussianBeam with direction '-' and non-zero 'waist_distances'",
372+
):
373+
td.AstigmaticGaussianBeam(
374+
size=(0, 1, 1),
375+
source_time=g,
376+
pol_angle=np.pi / 2,
377+
direction="-",
378+
waist_sizes=(0.2, 0.4),
379+
waist_distances=(0.1, 0.0),
380+
)
381+
382+
with AssertLogLevel(None):
383+
td.AstigmaticGaussianBeam(
384+
size=(0, 1, 1),
385+
source_time=g,
386+
pol_angle=np.pi / 2,
387+
direction="-",
388+
waist_sizes=(0.2, 0.4),
389+
waist_distances=(0.0, 0.0),
390+
)
391+
392+
341393
def test_pol_arrow():
342394
g = td.GaussianPulse(freq0=1e12, fwidth=0.1e12)
343395

tidy3d/components/beam.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .monitor import FieldMonitor
2121
from .source.field import FixedAngleSpec, FixedInPlaneKSpec
2222
from .types import TYPE_TAG_STR, Direction, FreqArray, Numpy
23-
from .validators import assert_plane
23+
from .validators import assert_plane, warn_backward_waist_distance
2424

2525
DEFAULT_RESOLUTION = 200
2626

@@ -374,6 +374,7 @@ class GaussianBeamProfile(BeamProfile):
374374
"For an angled beam, the distance is defined along the rotated propagation direction.",
375375
units=MICROMETER,
376376
)
377+
_backward_waist_warning = warn_backward_waist_distance("waist_distance")
377378

378379
def beam_params(self, z: Numpy, k0: Numpy) -> tuple[Numpy, Numpy, Numpy]:
379380
"""Compute the parameters needed to evaluate a Gaussian beam at z.
@@ -440,6 +441,7 @@ class AstigmaticGaussianBeamProfile(BeamProfile):
440441
"the beam plane.",
441442
units=MICROMETER,
442443
)
444+
_backward_waist_warning = warn_backward_waist_distance("waist_distances")
443445

444446
def beam_params(self, z: Numpy, k0: Numpy) -> tuple[Numpy, Numpy, Numpy, Numpy]:
445447
"""Compute the parameters needed to evaluate an astigmatic Gaussian beam at z.

tidy3d/components/source/field.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
assert_plane,
2020
assert_single_freq_in_range,
2121
assert_volumetric,
22+
warn_backward_waist_distance,
2223
warn_if_dataset_none,
2324
)
2425
from tidy3d.constants import GLANCING_CUTOFF, MICROMETER, RADIAN, inf
@@ -602,6 +603,7 @@ class GaussianBeam(AngledFieldSource, PlanarSource, BroadbandSource):
602603
ge=1,
603604
le=20,
604605
)
606+
_backward_waist_warning = warn_backward_waist_distance("waist_distance")
605607

606608

607609
class AstigmaticGaussianBeam(AngledFieldSource, PlanarSource, BroadbandSource):
@@ -662,6 +664,7 @@ class AstigmaticGaussianBeam(AngledFieldSource, PlanarSource, BroadbandSource):
662664
ge=1,
663665
le=20,
664666
)
667+
_backward_waist_warning = warn_backward_waist_distance("waist_distances")
665668

666669

667670
class TFSF(AngledFieldSource, VolumeSource, BroadbandSource):

tidy3d/components/validators.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,31 @@ def _warn_if_none(cls, val: Dataset) -> Dataset:
306306
return _warn_if_none
307307

308308

309+
def warn_backward_waist_distance(field_name: str):
310+
"""Warn if a backward-propagating beam uses a non-zero waist distance."""
311+
312+
@pydantic.root_validator(allow_reuse=True)
313+
def _warn_backward_nonzero(cls, values):
314+
"""Emit deprecation warning for backward propagation with non-zero waist."""
315+
direction = values.get("direction")
316+
if direction != "-":
317+
return values
318+
waist_value = values.get(field_name)
319+
waist_array = np.atleast_1d(waist_value)
320+
if not np.all(np.isclose(waist_array, 0.0)):
321+
log.warning(
322+
f"Behavior of {cls.__name__} with direction '-' and non-zero '{field_name}' will "
323+
"change in version 2.11 to be consistent with upcoming beam overlap monitors and "
324+
"ports. Currently, the waist distance is interpreted w.r.t. the directed "
325+
"propagation axis, so switching 'direction' also switches the position of the "
326+
"waist in the global reference frame. In the future, the waist position will be "
327+
"defined such that it is the same for backward- and forward-propagating beams.",
328+
)
329+
return values
330+
331+
return _warn_backward_nonzero
332+
333+
309334
def assert_single_freq_in_range(field_name: str):
310335
"""Assert only one frequency supplied in source and it's in source time range."""
311336

0 commit comments

Comments
 (0)