Skip to content

Commit ccddac6

Browse files
committed
started adding "swap_axes" to "plot_field()", and all of the functions it depends on. WARNING: This effort is not complete. The code is now in a completely broken state. But I had to backup my changes before I changed branches.
1 parent f0995ca commit ccddac6

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

tests/test_components/test_geometry.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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():

tidy3d/components/data/sim_data.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from tidy3d.components.autograd.utils import split_list
1717
from tidy3d.components.base import JSON_TAG, Tidy3dBaseModel
1818
from tidy3d.components.base_sim.data.sim_data import AbstractSimulationData
19+
from tidy3d.components.geometry.base import Geometry
1920
from tidy3d.components.file_util import replace_values
2021
from tidy3d.components.monitor import Monitor
2122
from tidy3d.components.simulation import Simulation
@@ -452,6 +453,7 @@ def plot_field_monitor_data(
452453
vmax: Optional[float] = None,
453454
ax: Ax = None,
454455
shading: str = "flat",
456+
swap_axes: bool = False,
455457
**sel_kwargs,
456458
) -> Ax:
457459
"""Plot the field data for a monitor with simulation plot overlaid.
@@ -651,6 +653,7 @@ def plot_field_monitor_data(
651653
ax=ax,
652654
shading=shading,
653655
infer_intervals=True if shading == "flat" else False,
656+
swap_axes=swap_axes,
654657
)
655658

656659
def plot_field(
@@ -666,6 +669,7 @@ def plot_field(
666669
vmax: Optional[float] = None,
667670
ax: Ax = None,
668671
shading: str = "flat",
672+
swap_axes: bool = False,
669673
**sel_kwargs,
670674
) -> Ax:
671675
"""Plot the field data for a monitor with simulation plot overlaid.
@@ -730,6 +734,7 @@ def plot_field(
730734
vmax=vmax,
731735
ax=ax,
732736
shading=shading,
737+
swap_axes=swap_axes,
733738
**sel_kwargs,
734739
)
735740

@@ -747,6 +752,7 @@ def plot_scalar_array(
747752
vmax: Optional[float] = None,
748753
cmap_type: ColormapType = "divergent",
749754
ax: Ax = None,
755+
swap_axes: bool = False,
750756
**kwargs,
751757
) -> Ax:
752758
"""Plot the field data for a monitor with simulation plot overlaid.
@@ -807,10 +813,16 @@ def plot_scalar_array(
807813
eps_reverse = False
808814

809815
# plot the field
810-
xy_coord_labels = list("xyz")
811-
xy_coord_labels.pop(axis)
816+
xy_coord_labels = Geometry.pop_axis_and_swap("xyz", axis=axis, swap_axes=swap_axes)
812817
x_coord_label, y_coord_label = xy_coord_labels[0], xy_coord_labels[1]
813-
field_data.plot(
818+
819+
field_data_tmp = field_data.copy()
820+
if swap_axes:
821+
# field_data.coords is a 2-column matrix of 2D coordinate data
822+
assert len(field_data.coords.shape) == 2 and field_data.coords.shape[1] == 2
823+
# swap column 0 (x coords) with column 1 (y coords)
824+
field_data_tmp.coords = field_data_tmp.coords[:, ::-1]
825+
field_data_tmp.plot(
814826
ax=ax,
815827
x=x_coord_label,
816828
y=y_coord_label,
@@ -819,7 +831,7 @@ def plot_scalar_array(
819831
vmax=vmax,
820832
robust=robust,
821833
center=center,
822-
cbar_kwargs={"label": field_data.name},
834+
cbar_kwargs={"label": field_data_tmp.name},
823835
**kwargs,
824836
)
825837

@@ -830,12 +842,13 @@ def plot_scalar_array(
830842
alpha=eps_alpha,
831843
reverse=eps_reverse,
832844
ax=ax,
845+
swap_axes=swap_axes,
833846
**interp_kwarg,
834847
)
835848

836849
# set the limits based on the xarray coordinates min and max
837-
x_coord_values = field_data.coords[x_coord_label]
838-
y_coord_values = field_data.coords[y_coord_label]
850+
x_coord_values = field_data_tmp.coords[x_coord_label]
851+
y_coord_values = field_data_tmp.coords[y_coord_label]
839852
ax.set_xlim(min(x_coord_values), max(x_coord_values))
840853
ax.set_ylim(min(y_coord_values), max(y_coord_values))
841854

tidy3d/components/mode/mode_solver.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,7 @@ def plot_field(
20292029
vmin: Optional[float] = None,
20302030
vmax: Optional[float] = None,
20312031
ax: Ax = None,
2032+
swap_axes: bool = False,
20322033
**sel_kwargs,
20332034
) -> Ax:
20342035
"""Plot the field for a :class:`.ModeSolverData` with :class:`.Simulation` plot overlaid.
@@ -2080,12 +2081,14 @@ def plot_field(
20802081
vmin=vmin,
20812082
vmax=vmax,
20822083
ax=ax,
2084+
swap_axes=swap_axes,
20832085
**sel_kwargs,
20842086
)
20852087

20862088
def plot(
20872089
self,
20882090
ax: Ax = None,
2091+
swap_axes: bool = False,
20892092
**patch_kwargs,
20902093
) -> Ax:
20912094
"""Plot the mode plane simulation's components.
@@ -2122,6 +2125,7 @@ def plot(
21222125
monitor_alpha=0,
21232126
lumped_element_alpha=0,
21242127
ax=ax,
2128+
swap_axes=swap_axes,
21252129
**patch_kwargs,
21262130
)
21272131

@@ -2132,6 +2136,7 @@ def plot_eps(
21322136
freq: Optional[float] = None,
21332137
alpha: Optional[float] = None,
21342138
ax: Ax = None,
2139+
swap_axes: bool = False,
21352140
) -> Ax:
21362141
"""Plot the mode plane simulation's components.
21372142
The permittivity is plotted in grayscale based on its value at the specified frequency.
@@ -2179,6 +2184,7 @@ def plot_eps(
21792184
monitor_alpha=0,
21802185
lumped_element_alpha=0,
21812186
ax=ax,
2187+
swap_axes=swap_axes,
21822188
)
21832189

21842190
def plot_structures_eps(
@@ -2188,6 +2194,7 @@ def plot_structures_eps(
21882194
cbar: bool = True,
21892195
reverse: bool = False,
21902196
ax: Ax = None,
2197+
swap_axes: bool = False,
21912198
) -> Ax:
21922199
"""Plot the mode plane simulation's components.
21932200
The permittivity is plotted in grayscale based on its value at the specified frequency.
@@ -2239,11 +2246,13 @@ def plot_structures_eps(
22392246
hlim=h_lim,
22402247
vlim=v_lim,
22412248
ax=ax,
2249+
swap_axes=swap_axes,
22422250
)
22432251

22442252
def plot_grid(
22452253
self,
22462254
ax: Ax = None,
2255+
swap_axes: bool = False,
22472256
**kwargs,
22482257
) -> Ax:
22492258
"""Plot the mode plane cell boundaries as lines.
@@ -2269,7 +2278,7 @@ def plot_grid(
22692278
)
22702279

22712280
return self.simulation.plot_grid(
2272-
x=a_center[0], y=a_center[1], z=a_center[2], hlim=h_lim, vlim=v_lim, ax=ax, **kwargs
2281+
x=a_center[0], y=a_center[1], z=a_center[2], hlim=h_lim, vlim=v_lim, ax=ax, swap_axes=swap_axes, **kwargs
22732282
)
22742283

22752284
@classmethod
@@ -2363,6 +2372,7 @@ def _mode_plane_size_no_pml(
23632372
def plot_pml(
23642373
self,
23652374
ax: Ax = None,
2375+
swap_axes: bool = False,
23662376
) -> Ax:
23672377
"""Plot the mode plane absorbing boundaries.
23682378
@@ -2377,16 +2387,16 @@ def plot_pml(
23772387
The supplied or created matplotlib axes.
23782388
"""
23792389
return self._plot_pml(
2380-
simulation=self.simulation, plane=self.plane, mode_spec=self.mode_spec, ax=ax
2390+
simulation=self.simulation, plane=self.plane, mode_spec=self.mode_spec, ax=ax, swap_axes=swap_axes
23812391
)
23822392

23832393
@classmethod
23842394
def _plot_pml(
2385-
cls, simulation: Simulation, plane: Box, mode_spec: ModeSpec, ax: Ax = None
2395+
cls, simulation: Simulation, plane: Box, mode_spec: ModeSpec, ax: Ax = None, swap_axes: bool = False,
23862396
) -> Ax:
23872397
"""Plot the mode plane absorbing boundaries."""
23882398
# Get the mode plane normal axis, center, and limits.
2389-
_, h_lim, v_lim, _ = cls._center_and_lims(simulation=simulation, plane=plane)
2399+
_, h_lim, v_lim, _ = cls._center_and_lims(simulation=simulation, plane=plane, swap_axes=swap_axes)
23902400

23912401
# Create ax if ax=None.
23922402
if not ax:
@@ -2434,16 +2444,20 @@ def _plot_pml(
24342444
return ax
24352445

24362446
@staticmethod
2437-
def _center_and_lims(simulation: Simulation, plane: Box) -> tuple[list, list, list, list]:
2447+
def _center_and_lims(
2448+
simulation: Simulation,
2449+
plane: Box,
2450+
swap_axes: bool = False
2451+
) -> tuple[list, list, list, list]:
24382452
"""Get the mode plane center and limits."""
24392453
normal_axis = plane.size.index(0.0)
24402454

2441-
n_axis, t_axes = plane.pop_axis([0, 1, 2], normal_axis)
2455+
n_axis, t_axes = plane.pop_axis_and_swap([0, 1, 2], normal_axis, swap_axes=swap_axes)
24422456
a_center = [None, None, None]
24432457
a_center[n_axis] = plane.center[n_axis]
24442458

2445-
_, (h_min_s, v_min_s) = Box.pop_axis(simulation.bounds[0], axis=n_axis)
2446-
_, (h_max_s, v_max_s) = Box.pop_axis(simulation.bounds[1], axis=n_axis)
2459+
_, (h_min_s, v_min_s) = Box.pop_axis_and_swap(simulation.bounds[0], axis=n_axis, swap_axes=swap_axes)
2460+
_, (h_max_s, v_max_s) = Box.pop_axis_and_swap(simulation.bounds[1], axis=n_axis, swap_axes=swap_axes)
24472461

24482462
h_min = plane.center[t_axes[0]] - plane.size[t_axes[0]] / 2
24492463
h_max = plane.center[t_axes[0]] + plane.size[t_axes[0]] / 2

0 commit comments

Comments
 (0)