Skip to content

Commit

Permalink
Mode sorting tool
Browse files Browse the repository at this point in the history
  • Loading branch information
dbochkov-flexcompute committed Nov 17, 2022
1 parent 79fe521 commit a460c29
Show file tree
Hide file tree
Showing 14 changed files with 318 additions and 24 deletions.
1 change: 1 addition & 0 deletions test_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e
black .
python lint.py

pytest -ra tests/test_components/test_apodization.py
pytest -ra tests/test_components/test_base.py
pytest -ra tests/test_components/test_boundaries.py
pytest -ra tests/test_components/test_custom.py
Expand Down
2 changes: 1 addition & 1 deletion tests/sims/simulation_1_8_0.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions tests/test_components/test_mode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests mode objects."""
import pytest
import pydantic
import numpy as np
import tidy3d as td
from tidy3d.log import SetupError
Expand All @@ -10,6 +11,15 @@ def test_modes():
m = td.ModeSpec(num_modes=2)
m = td.ModeSpec(num_modes=1, target_neff=1.0)

options = [None, "lowest", "highest", "central"]
for opt in options:
m = td.ModeSpec(num_modes=3, track_freq=opt)

with pytest.raises(pydantic.ValidationError) as e_info:
m = td.ModeSpec(num_modes=3, track_freq="middle")
with pytest.raises(pydantic.ValidationError) as e_info:
m = td.ModeSpec(num_modes=3, track_freq=4)


def test_bend_axis_not_given():
with pytest.raises(SetupError):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_components/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
import numpy as np
import tidy3d as td
from tidy3d.log import SetupError, DataError, ValidationError
from tidy3d.log import SetupError, ValidationError


def test_stop_start():
Expand Down
19 changes: 9 additions & 10 deletions tests/test_data/test_data_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@
ORDERS_X = list(range(-1, 2))
ORDERS_Y = list(range(-2, 3))

FS = np.linspace(1e14, 2e14, 11)
TS = np.linspace(0, 1e-12, 11)
MODE_INDICES = np.arange(0, 3)
FS11 = np.linspace(1e14, 2e14, 11)
# unused
# TS = np.linspace(0, 1e-12, 11)
# MODE_INDICES = np.arange(0, 3)
# DIRECTIONS = ["+", "-"]
FS = np.linspace(1e14, 2e14, 5)
TS = np.linspace(0, 1e-12, 4)
MODE_INDICES = np.arange(0, 4)
DIRECTIONS = ["+", "-"]

FIELD_MONITOR = FieldMonitor(size=SIZE_3D, fields=FIELDS, name="field", freqs=FREQS)
Expand All @@ -59,7 +64,7 @@
size=SIZE_2D, fields=FIELDS, name="field_time_2d", interval=INTERVAL
)
MODE_SOLVE_MONITOR = ModeSolverMonitor(
size=SIZE_2D, name="mode_solver", mode_spec=MODE_SPEC, freqs=FREQS
size=SIZE_2D, name="mode_solver", mode_spec=MODE_SPEC, freqs=FS
)
PERMITTIVITY_MONITOR = PermittivityMonitor(size=SIZE_3D, name="permittivity", freqs=FREQS)
MODE_MONITOR = ModeMonitor(size=SIZE_2D, name="mode", mode_spec=MODE_SPEC, freqs=FREQS)
Expand Down Expand Up @@ -108,11 +113,6 @@
structures=STRUCTURES,
)

FS = np.linspace(1e14, 2e14, 5)
TS = np.linspace(0, 1e-12, 4)
MODE_INDICES = np.arange(0, 3)
DIRECTIONS = ["+", "-"]

""" Generate the data arrays (used in other test files) """


Expand Down Expand Up @@ -160,7 +160,6 @@ def make_mode_amps_data_array():


def make_mode_index_data_array():
f = np.linspace(2e14, 3e14, 1001)
values = (1 + 1j) * np.random.random((len(FS), len(MODE_INDICES)))
return ModeIndexDataArray(values, coords=dict(f=FS, mode_index=MODE_INDICES))

Expand Down
18 changes: 18 additions & 0 deletions tests/test_data/test_monitor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,21 @@ def test_diffraction_data_use_medium():
data = make_diffraction_data()
data = data.copy(update=dict(medium=td.Medium(permittivity=4)))
assert np.allclose(data.eta, np.real(td.ETA_0 / 2.0))


def test_mode_solver_data_sort():
# test basic matching algorithm
arr = np.array([[1, 2, 3], [6, 5, 4], [7, 9, 8]])
pairs, values = ModeSolverData._find_closest_pairs(arr)
assert np.all(pairs == [2, 0, 1])
assert np.all(values == [3, 6, 9])

# test sorting function
data = make_mode_solver_data()
data_first = data.overlap_sort(track_freq="lowest")
data_last = data.overlap_sort(track_freq="highest")
data_center = data.overlap_sort(track_freq="central")
# these are probably not the best tests
assert data_first.field_components != data.field_components
assert data_first.field_components != data_last.field_components
assert data_first.field_components != data_center.field_components
16 changes: 14 additions & 2 deletions tests/test_plugins/test_mode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ def test_mode_solver_simple():
target_neff=2.0,
filter_pol="tm",
precision="double",
track_freq="lowest",
)
ms = ModeSolver(
simulation=simulation, plane=PLANE, mode_spec=mode_spec, freqs=[td.constants.C_0 / 1.0]
simulation=simulation,
plane=PLANE,
mode_spec=mode_spec,
freqs=[td.constants.C_0 / 0.9, td.constants.C_0 / 1.0, td.constants.C_0 / 1.1],
)
modes = ms.solve()

Expand All @@ -56,6 +60,7 @@ def test_mode_solver_angle_bend():
bend_axis=0,
angle_theta=np.pi / 3,
angle_phi=np.pi,
track_freq="highest",
)
# put plane entirely in the symmetry quadrant rather than sitting on its center
plane = td.Box(center=(0, 0.5, 0), size=(1, 0, 1))
Expand All @@ -73,9 +78,16 @@ def test_mode_solver_angle_bend():
mmonitor = ms.to_monitor(freqs=[1.0, 2.0], name="mode_mnt")


# TODO: FieldData.dot needs to be fixed for 1D monitors
def test_mode_solver_2D():
"""Run mode solver in 2D simulations."""
mode_spec = td.ModeSpec(num_modes=3, filter_pol="te", precision="double", num_pml=(0, 10))
mode_spec = td.ModeSpec(
num_modes=3,
filter_pol="te",
precision="double",
num_pml=(0, 10),
track_freq="central",
)
simulation = td.Simulation(
size=(0, 2, 2),
grid_spec=td.GridSpec(wavelength=1.0),
Expand Down
Loading

0 comments on commit a460c29

Please sign in to comment.