Skip to content

Commit 5f954ff

Browse files
committed
Added "swap_axes" argument to mode_solver.py. Updated unit tests for test_geometry.py and test_mode_solver.py
1 parent e491d6e commit 5f954ff

File tree

5 files changed

+157
-88
lines changed

5 files changed

+157
-88
lines changed

tests/test_components/test_geometry.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@
8585
_, AX = plt.subplots()
8686

8787

88-
@pytest.mark.parametrize("component", GEO_TYPES)
89-
def test_plot(component):
90-
_ = component.plot(z=0, ax=AX)
88+
@pytest.mark.parametrize("component, swap_axes", zip(GEO_TYPES, [True, False]))
89+
def test_plot(component, swap_axes):
90+
_ = component.plot(z=0, ax=AX, swap_axes=swap_axes)
9191
plt.close()
9292

93-
94-
def test_plot_with_units():
95-
_ = BOX.plot(z=0, ax=AX, plot_length_units="nm")
93+
@pytest.mark.parametrize("swap_axes", [True, False])
94+
def test_plot_with_units(swap_axes):
95+
_ = BOX.plot(z=0, ax=AX, plot_length_units="nm", swap_axes=swap_axes)
9696
plt.close()
9797

9898

@@ -204,11 +204,11 @@ def test_array_to_vertices():
204204
assert np.all(np.array(vertices) == np.array(vertices2))
205205

206206

207-
@pytest.mark.parametrize("component", GEO_TYPES)
208-
def test_intersections_plane(component):
209-
assert len(component.intersections_plane(z=0.2)) > 0
210-
assert len(component.intersections_plane(x=0.2)) > 0
211-
assert len(component.intersections_plane(x=10000)) == 0
207+
@pytest.mark.parametrize("component, swap_axes", zip(GEO_TYPES, [True, False]))
208+
def test_intersections_plane(component, swap_axes):
209+
assert len(component.intersections_plane(z=0.2, swap_axes=swap_axes)) > 0
210+
assert len(component.intersections_plane(x=0.2, swap_axes=swap_axes)) > 0
211+
assert len(component.intersections_plane(x=10000, swap_axes=swap_axes)) == 0
212212

213213

214214
def test_intersections_plane_inf():
@@ -778,38 +778,39 @@ def test_pop_axis():
778778
assert Ly == _Ly
779779

780780

781-
def test_pop_axis_and_swap():
781+
@pytest.mark.parametrize("swap_axes", [True, False])
782+
def test_pop_axis_and_swap(swap_axes):
782783
b = td.Box(size=(1, 1, 1))
783-
for swap_axes in (False, True):
784-
for axis in range(3):
785-
coords = (1, 2, 3)
786-
Lz, (Lx, Ly) = b.pop_axis_and_swap(coords, axis=axis, swap_axes=swap_axes)
787-
_coords = b.unpop_axis_and_swap(Lz, (Lx, Ly), axis=axis, swap_axes=swap_axes)
788-
assert all(c == _c for (c, _c) in zip(coords, _coords))
789-
_Lz, (_Lx, _Ly) = b.pop_axis_and_swap(_coords, axis=axis, swap_axes=swap_axes)
790-
assert Lz == _Lz
791-
assert Lx == _Lx
792-
assert Ly == _Ly
793-
794-
795-
def test_2b_box_intersections():
784+
for axis in range(3):
785+
coords = (1, 2, 3)
786+
Lz, (Lx, Ly) = b.pop_axis_and_swap(coords, axis=axis, swap_axes=swap_axes)
787+
_coords = b.unpop_axis_and_swap(Lz, (Lx, Ly), axis=axis, swap_axes=swap_axes)
788+
assert all(c == _c for (c, _c) in zip(coords, _coords))
789+
_Lz, (_Lx, _Ly) = b.pop_axis_and_swap(_coords, axis=axis, swap_axes=swap_axes)
790+
assert Lz == _Lz
791+
assert Lx == _Lx
792+
assert Ly == _Ly
793+
794+
795+
@pytest.mark.parametrize("swap_axes", [True, False])
796+
def test_2b_box_intersections(swap_axes):
796797
plane = td.Box(size=(1, 4, 0))
797798
box1 = td.Box(size=(1, 1, 1))
798799
box2 = td.Box(size=(1, 1, 1), center=(3, 0, 0))
799800

800-
result = plane.intersections_with(box1)
801+
result = plane.intersections_with(box1, swap_axes=swap_axes)
801802
assert len(result) == 1
802803
assert result[0].geom_type == "Polygon"
803-
assert len(plane.intersections_with(box2)) == 0
804+
assert len(plane.intersections_with(box2, swap_axes=swap_axes)) == 0
804805

805806
with pytest.raises(ValidationError):
806-
_ = box1.intersections_with(box2)
807+
_ = box1.intersections_with(box2, swap_axes=swap_axes)
807808

808-
assert len(box1.intersections_2dbox(plane)) == 1
809-
assert len(box2.intersections_2dbox(plane)) == 0
809+
assert len(box1.intersections_2dbox(plane, swap_axes=swap_axes)) == 1
810+
assert len(box2.intersections_2dbox(plane, swap_axes=swap_axes)) == 0
810811

811812
with pytest.raises(ValidationError):
812-
_ = box2.intersections_2dbox(box1)
813+
_ = box2.intersections_2dbox(box1, swap_axes=swap_axes)
813814

814815

815816
def test_polyslab_merge():

tests/test_components/test_simulation.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -708,22 +708,25 @@ def test_wvl_mat_min_error():
708708
SIM.wvl_mat_min()
709709

710710

711-
def test_plot_structure():
712-
_ = SIM_FULL.structures[0].plot(x=0)
711+
@pytest.mark.parametrize("swap_axes", [True, False])
712+
def test_plot_structure(swap_axes):
713+
_ = SIM_FULL.structures[0].plot(x=0, swap_axes=swap_axes)
713714
plt.close()
714715

715716

716-
def test_plot_eps():
717-
_ = SIM_FULL.plot_eps(x=0)
717+
@pytest.mark.parametrize("swap_axes", [True, False])
718+
def test_plot_eps(swap_axes):
719+
_ = SIM_FULL.plot_eps(x=0, swap_axes=swap_axes)
718720
plt.close()
719721

720722

721-
def test_plot_eps_bounds():
722-
_ = SIM_FULL.plot_eps(x=0, hlim=[-0.45, 0.45])
723+
@pytest.mark.parametrize("swap_axes", [True, False])
724+
def test_plot_eps_bounds(swap_axes):
725+
_ = SIM_FULL.plot_eps(x=0, hlim=[-0.45, 0.45], swap_axes=swap_axes)
723726
plt.close()
724-
_ = SIM_FULL.plot_eps(x=0, vlim=[-0.45, 0.45])
727+
_ = SIM_FULL.plot_eps(x=0, vlim=[-0.45, 0.45], swap_axes=swap_axes)
725728
plt.close()
726-
_ = SIM_FULL.plot_eps(x=0, hlim=[-0.45, 0.45], vlim=[-0.45, 0.45])
729+
_ = SIM_FULL.plot_eps(x=0, hlim=[-0.45, 0.45], vlim=[-0.45, 0.45], swap_axes=swap_axes)
727730
plt.close()
728731

729732

@@ -949,22 +952,23 @@ def test_plot_3d():
949952
plt.close()
950953

951954

952-
def test_structure_alpha():
953-
_ = SIM_FULL.plot_structures_eps(x=0, alpha=None)
955+
@pytest.mark.parametrize("swap_axes", [True, False])
956+
def test_structure_alpha(swap_axes):
957+
_ = SIM_FULL.plot_structures_eps(x=0, alpha=None, swap_axes=swap_axes)
954958
plt.close()
955-
_ = SIM_FULL.plot_structures_eps(x=0, alpha=-1)
959+
_ = SIM_FULL.plot_structures_eps(x=0, alpha=-1, swap_axes=swap_axes)
956960
plt.close()
957-
_ = SIM_FULL.plot_structures_eps(x=0, alpha=1)
961+
_ = SIM_FULL.plot_structures_eps(x=0, alpha=1, swap_axes=swap_axes)
958962
plt.close()
959-
_ = SIM_FULL.plot_structures_eps(x=0, alpha=0.5)
963+
_ = SIM_FULL.plot_structures_eps(x=0, alpha=0.5, swap_axes=swap_axes)
960964
plt.close()
961-
_ = SIM_FULL.plot_structures_eps(x=0, alpha=0.5, cbar=True)
965+
_ = SIM_FULL.plot_structures_eps(x=0, alpha=0.5, cbar=True, swap_axes=swap_axes)
962966
plt.close()
963967
new_structs = [
964968
td.Structure(geometry=s.geometry, medium=SIM_FULL.medium) for s in SIM_FULL.structures
965969
]
966970
S2 = SIM_FULL.copy(update={"structures": new_structs})
967-
_ = S2.plot_structures_eps(x=0, alpha=0.5)
971+
_ = S2.plot_structures_eps(x=0, alpha=0.5, swap_axes=swap_axes)
968972
plt.close()
969973

970974

tests/test_plugins/test_mode_solver.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,8 @@ def test_mode_solver_relative():
10671067
_ = ms._data_on_yee_grid_relative(basis=basis)
10681068

10691069

1070-
def test_mode_solver_plot():
1070+
@pytest.mark.parametrize("swap_axes", [True, False])
1071+
def test_mode_solver_plot(swap_axes):
10711072
"""Test mode plane plotting functions"""
10721073

10731074
simulation = td.Simulation(
@@ -1094,13 +1095,13 @@ def test_mode_solver_plot():
10941095
colocate=False,
10951096
)
10961097
_, ax = plt.subplots(2, 2, figsize=(12, 8), tight_layout=True)
1097-
ms.plot(ax=ax[0, 0])
1098-
ms.plot_eps(freq=200e14, alpha=0.7, ax=ax[0, 1])
1099-
ms.plot_structures_eps(freq=200e14, alpha=0.8, cbar=True, reverse=False, ax=ax[1, 0])
1100-
ms.plot_grid(linewidth=0.3, ax=ax[1, 0])
1101-
ms.plot(ax=ax[1, 1])
1102-
ms.plot_pml(ax=ax[1, 1])
1103-
ms.plot_grid(linewidth=0.3, ax=ax[1, 1])
1098+
ms.plot(ax=ax[0, 0], swap_axes=swap_axes)
1099+
ms.plot_eps(freq=200e14, alpha=0.7, ax=ax[0, 1], swap_axes=swap_axes)
1100+
ms.plot_structures_eps(freq=200e14, alpha=0.8, cbar=True, reverse=False, ax=ax[1, 0], swap_axes=swap_axes)
1101+
ms.plot_grid(linewidth=0.3, ax=ax[1, 0], swap_axes=swap_axes)
1102+
ms.plot(ax=ax[1, 1], swap_axes=swap_axes)
1103+
ms.plot_pml(ax=ax[1, 1], swap_axes=swap_axes)
1104+
ms.plot_grid(linewidth=0.3, ax=ax[1, 1], swap_axes=swap_axes)
11041105
plt.close()
11051106

11061107

0 commit comments

Comments
 (0)