Skip to content

Commit

Permalink
automatic generation of diffraction orders
Browse files Browse the repository at this point in the history
  • Loading branch information
shashwat-sh committed Nov 29, 2022
1 parent c22271d commit e934dfe
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 41 deletions.
2 changes: 1 addition & 1 deletion tests/sims/simulation_1_8_0.json

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions tests/test_components/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,6 @@ def test_diffraction_validators():
with pytest.raises(SetupError) as e_info:
monitor = td.DiffractionMonitor(size=[td.inf, 4, 0], freqs=[1e12], name="de")

# ensure error if orders are non-unique
with pytest.raises(SetupError) as e_info:
monitor = td.DiffractionMonitor(
size=[td.inf, td.inf, 0], freqs=[1e12], name="de", orders_x=[0, 1, 1]
)
with pytest.raises(SetupError) as e_info:
monitor = td.DiffractionMonitor(
size=[td.inf, td.inf, 0], freqs=[1e12], name="de", orders_y=[0, 1, 1]
)


def test_monitor():

Expand Down
2 changes: 0 additions & 2 deletions tests/test_data/test_data_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@
size=(inf, inf, 0),
freqs=FS,
name="diffraction",
orders_x=ORDERS_X,
orders_y=ORDERS_Y,
)

MONITORS = [
Expand Down
5 changes: 2 additions & 3 deletions tidy3d/components/data/monitor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,8 +1361,7 @@ class DiffractionData(AbstractFieldProjectionData):
>>> values = (1+1j) * np.random.random((len(orders_x), len(orders_y), len(f)))
>>> field = DiffractionDataArray(values, coords=coords)
>>> monitor = DiffractionMonitor(
... center=(1,2,3), size=(np.inf,np.inf,0), freqs=f, name='diffraction',
... orders_x=orders_x, orders_y=orders_y
... center=(1,2,3), size=(np.inf,np.inf,0), freqs=f, name='diffraction'
... )
>>> data = DiffractionData(
... monitor=monitor, sim_size=[1,1], bloch_vecs=[1,2],
Expand Down Expand Up @@ -1419,7 +1418,7 @@ class DiffractionData(AbstractFieldProjectionData):
)

@staticmethod
def shifted_orders(orders: Tuple[int], bloch_vec: float) -> np.ndarray:
def shifted_orders(orders: Tuple[int, ...], bloch_vec: float) -> np.ndarray:
"""Diffraction orders shifted by the Bloch vector."""
return bloch_vec + np.atleast_1d(orders)

Expand Down
29 changes: 4 additions & 25 deletions tidy3d/components/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .types import Ax, EMField, ArrayLike, Bound, FreqArray
from .types import Literal, Direction, Coordinate, Axis, ObsGridArray
from .geometry import Box
from .validators import assert_plane, validate_unique
from .validators import assert_plane
from .base import cached_property
from .mode import ModeSpec
from .apodization import ApodizationSpec
Expand Down Expand Up @@ -684,7 +684,7 @@ def storage_size(self, num_cells: int, tmesh: ArrayLike[float, 1]) -> int:

class DiffractionMonitor(PlanarMonitor, FreqMonitor):
""":class:`Monitor` that uses a 2D Fourier transform to compute the
diffraction amplitudes and efficiency for given (or all) diffraction orders.
diffraction amplitudes and efficiency for allowed diffraction orders.
Example
-------
Expand All @@ -694,8 +694,6 @@ class DiffractionMonitor(PlanarMonitor, FreqMonitor):
... freqs=[250e12, 300e12],
... name='diffraction_monitor',
... normal_dir='+',
... orders_x=[0, 1, 2],
... orders_y=[0],
... )
"""

Expand All @@ -707,25 +705,6 @@ class DiffractionMonitor(PlanarMonitor, FreqMonitor):
"Defaults to ``'+'`` if not provided.",
)

orders_x: Tuple[int, ...] = pydantic.Field(
[0],
title="Diffraction orders along x",
description="Diffraction orders along x for which efficiency should be computed. "
"Orders corresponding to wave numbers outside of the light cone will be ignored. "
"By default, only order 0 will be considered.",
)

orders_y: Tuple[int, ...] = pydantic.Field(
[0],
title="Diffraction orders along y",
description="Diffraction orders along y for which efficiency should be computed. "
"Orders corresponding to wave numbers outside of the light cone will be ignored. "
"By default, only order 0 will be considered.",
)

_unique_x = validate_unique("orders_x")
_unique_y = validate_unique("orders_y")

@pydantic.validator("size", always=True)
def diffraction_monitor_size(cls, val):
"""Ensure that the monitor is infinite in the transverse direction."""
Expand All @@ -743,8 +722,8 @@ def normal_axis(self) -> Axis:

def storage_size(self, num_cells: int, tmesh: ArrayLike[float, 1]) -> int:
"""Size of monitor storage given the number of points after discretization."""
# stores 1 complex number per pair of diffraction orders, per frequency
return BYTES_COMPLEX * len(self.orders_x) * len(self.orders_y) * len(self.freqs)
# assumes 1 diffraction order per frequency; actual size will be larger
return BYTES_COMPLEX * len(self.freqs)


# types of monitors that are accepted by simulation
Expand Down
2 changes: 2 additions & 0 deletions tidy3d/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ def fix_kspace_info(mnt_dict: dict) -> dict:

def fix_diffraction_info(mnt_dict: dict) -> dict:
mnt_dict.pop("medium", None)
mnt_dict.pop("orders_x", None)
mnt_dict.pop("orders_y", None)
return mnt_dict

iterate_update_dict(
Expand Down

0 comments on commit e934dfe

Please sign in to comment.