Skip to content

Commit 75af965

Browse files
committed
Adding ModeSimulation.plot and more arguments to mode plotting
1 parent f4141cd commit 75af965

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- `EMEFieldMonitor` now supports `interval_space`.
1717
- `Simulation.precision` option allows to select `"double"` precision for very high-accuracy results. Note that this is very rarely needed, and doubles the simulation computational weight and correpsondingly FlexCredit cost.
1818
- Added material type `PMCMedium` for perfect magnetic conductor.
19+
- `ModeSimulation.plot()` method that plots the mode simulation plane by default, or the containing FDTD simulation if any of ``x``, ``y``, or ``z`` is passed.
1920

2021
### Changed
2122
- Switched to an analytical gradient calculation for spatially-varying pole-residue models (`CustomPoleResidue`).

tests/test_components/test_mode.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ def get_mode_sim():
176176
def test_mode_sim():
177177
with AssertLogLevel(None):
178178
sim = get_mode_sim()
179+
_ = sim.plot(ax=AX)
180+
_ = sim.plot(ax=AX, fill_structures=False, hlim=(-1, 1), vlim=(-1, 1))
179181
_ = sim.plot(y=0, ax=AX)
180182
_ = sim.plot_mode_plane(ax=AX)
181183
_ = sim.plot_eps_mode_plane(ax=AX)

tidy3d/components/mode/mode_solver.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,9 @@ def plot_field(
21172117
def plot(
21182118
self,
21192119
ax: Ax = None,
2120+
hlim: Optional[tuple[float, float]] = None,
2121+
vlim: Optional[tuple[float, float]] = None,
2122+
fill_structures: bool = True,
21202123
**patch_kwargs,
21212124
) -> Ax:
21222125
"""Plot the mode plane simulation's components.
@@ -2125,6 +2128,12 @@ def plot(
21252128
----------
21262129
ax : matplotlib.axes._subplots.Axes = None
21272130
Matplotlib axes to plot on, if not specified, one is created.
2131+
hlim : Tuple[float, float] = None
2132+
The x range if plotting on xy or xz planes, y range if plotting on yz plane.
2133+
vlim : Tuple[float, float] = None
2134+
The z range if plotting on xz or yz planes, y plane if plotting on xy plane.
2135+
fill_structures : bool = True
2136+
Whether to fill structures with color or just draw outlines.
21282137
21292138
Returns
21302139
-------
@@ -2139,20 +2148,26 @@ def plot(
21392148
21402149
"""
21412150
# Get the mode plane normal axis, center, and limits.
2142-
a_center, h_lim, v_lim, _ = self._center_and_lims(
2151+
a_center, hlim_plane, vlim_plane, _ = self._center_and_lims(
21432152
simulation=self.simulation, plane=self.plane
21442153
)
21452154

2155+
if hlim is None:
2156+
hlim = hlim_plane
2157+
if vlim is None:
2158+
vlim = vlim_plane
2159+
21462160
ax = self.simulation.plot(
21472161
x=a_center[0],
21482162
y=a_center[1],
21492163
z=a_center[2],
2150-
hlim=h_lim,
2151-
vlim=v_lim,
2164+
hlim=hlim,
2165+
vlim=vlim,
21522166
source_alpha=0,
21532167
monitor_alpha=0,
21542168
lumped_element_alpha=0,
21552169
ax=ax,
2170+
fill_structures=fill_structures,
21562171
**patch_kwargs,
21572172
)
21582173

tidy3d/components/mode/simulation.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,75 @@ def from_mode_solver(
361361
)
362362
return mode_sim
363363

364+
def plot(
365+
self,
366+
x: Optional[float] = None,
367+
y: Optional[float] = None,
368+
z: Optional[float] = None,
369+
ax: Ax = None,
370+
source_alpha: Optional[float] = 0,
371+
monitor_alpha: Optional[float] = 0,
372+
lumped_element_alpha: Optional[float] = 0,
373+
hlim: Optional[tuple[float, float]] = None,
374+
vlim: Optional[tuple[float, float]] = None,
375+
fill_structures: bool = True,
376+
**patch_kwargs,
377+
) -> Ax:
378+
"""Plot the mode simulation. If any of ``x``, ``y``, or ``z`` is provided, the potentially
379+
larger FDTD simulation containing the mode plane is plotted at the desired location.
380+
Otherwise, the mode plane is plotted by default.
381+
382+
Parameters
383+
----------
384+
fill_structures : bool = True
385+
Whether to fill structures with color or just draw outlines.
386+
x : float = None
387+
position of plane in x direction, only one of x, y, z must be specified to define plane.
388+
y : float = None
389+
position of plane in y direction, only one of x, y, z must be specified to define plane.
390+
z : float = None
391+
position of plane in z direction, only one of x, y, z must be specified to define plane.
392+
source_alpha : float = 0
393+
Opacity of the sources. If ``None``, uses Tidy3d default.
394+
monitor_alpha : float = 0
395+
Opacity of the monitors. If ``None``, uses Tidy3d default.
396+
lumped_element_alpha : float = 0
397+
Opacity of the lumped elements. If ``None``, uses Tidy3d default.
398+
ax : matplotlib.axes._subplots.Axes = None
399+
Matplotlib axes to plot on, if not specified, one is created.
400+
hlim : Tuple[float, float] = None
401+
The x range if plotting on xy or xz planes, y range if plotting on yz plane.
402+
vlim : Tuple[float, float] = None
403+
The z range if plotting on xz or yz planes, y plane if plotting on xy plane.
404+
405+
Returns
406+
-------
407+
matplotlib.axes._subplots.Axes
408+
The supplied or created matplotlib axes.
409+
"""
410+
411+
if x is not None or y is not None or z is not None:
412+
return super().plot(
413+
x=x,
414+
y=y,
415+
z=z,
416+
ax=ax,
417+
source_alpha=source_alpha,
418+
monitor_alpha=monitor_alpha,
419+
lumped_element_alpha=lumped_element_alpha,
420+
hlim=hlim,
421+
vlim=vlim,
422+
fill_structures=fill_structures,
423+
**patch_kwargs,
424+
)
425+
return self._mode_solver.plot(
426+
ax=ax,
427+
hlim=hlim,
428+
vlim=vlim,
429+
fill_structures=fill_structures,
430+
**patch_kwargs,
431+
)
432+
364433
def plot_mode_plane(
365434
self,
366435
ax: Ax = None,

0 commit comments

Comments
 (0)