Skip to content

Commit c0276e7

Browse files
QimingFlexmomchil-flex
authored andcommitted
Raise an error if far_field_approx=False enabled for 2D simulations.
1 parent 6102643 commit c0276e7

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

tests/test_components/test_field_projection.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test near field to far field transformations."""
22

33
import numpy as np
4+
import pydantic.v1 as pydantic
45
import pytest
56
import tidy3d as td
67
from tidy3d.components.field_projection import FieldProjector
@@ -597,6 +598,50 @@ def test_2d_proj_clientside():
597598
make_2d_proj(plane)
598599

599600

601+
def test_2d_sim_with_proj_monitors_near():
602+
"""Creates near-field projection monitors by modifying proj_distance and far_field_approx."""
603+
center = [0, 0, 0]
604+
freqs = 1e13
605+
monitor_size = (0, 2, td.inf)
606+
plane = "xy"
607+
f0 = 1e13
608+
sim_size = (5, 5, 0)
609+
# boundary conditions
610+
boundary_conds = td.BoundarySpec(
611+
x=td.Boundary.pml(),
612+
y=td.Boundary.pml(),
613+
z=td.Boundary.periodic(),
614+
)
615+
616+
monitors = make_2d_proj_monitors(center, monitor_size, freqs, plane)
617+
618+
# Modify only proj_distance and far_field_approx
619+
proj_monitors_near = [
620+
monitor.__class__(
621+
proj_distance=R_FAR / 50, # Adjust projection distance
622+
far_field_approx=False, # Disable far-field approximation
623+
**{
624+
k: v
625+
for k, v in monitor.__dict__.items()
626+
if k not in ["proj_distance", "far_field_approx"]
627+
},
628+
)
629+
for monitor in monitors
630+
]
631+
632+
with pytest.raises(
633+
pydantic.ValidationError,
634+
match="Exact far-field projection for 2D simulations is not yet available",
635+
):
636+
_ = td.Simulation(
637+
size=sim_size,
638+
grid_spec=td.GridSpec.auto(wavelength=td.C_0 / f0),
639+
boundary_spec=boundary_conds,
640+
monitors=proj_monitors_near,
641+
run_time=1e-12,
642+
)
643+
644+
600645
@pytest.mark.parametrize(
601646
"array, pts, axes, expected",
602647
[

tidy3d/components/simulation.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3272,6 +3272,12 @@ def _projection_mnts_2d(cls, val, values):
32723272
f"Monitor '{monitor.name}' is not supported in 1D simulations."
32733273
)
32743274

3275+
if not monitor.far_field_approx:
3276+
raise SetupError(
3277+
f"Exact far-field projection for 2D simulations is not yet available for Monitor '{monitor.name}'. "
3278+
"Currently, only 'far_field_approx = True' is supported."
3279+
)
3280+
32753281
if isinstance(monitor, FieldProjectionAngleMonitor):
32763282
config = {
32773283
"y-z": {"valid_value": [np.pi / 2, 3 * np.pi / 2], "coord": "phi"},

0 commit comments

Comments
 (0)