Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dbochkov-flexcompute authored and momchil-flex committed Oct 30, 2023
1 parent 76a4ac1 commit 11b59e7
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 67 deletions.
8 changes: 4 additions & 4 deletions tests/test_components/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def test_scene_init():
"""make sure a scene can be initialized"""

sim = td.Scene(
scene = td.Scene(
structures=[
td.Structure(
geometry=td.Box(size=(1, 1, 1), center=(-1, 0, 0)),
Expand All @@ -37,9 +37,9 @@ def test_scene_init():
medium=td.Medium(permittivity=3.0),
)

_ = sim.mediums
_ = sim.medium_map
_ = sim.background_structure
_ = scene.mediums
_ = scene.medium_map
_ = scene.background_structure


def test_validate_components_none():
Expand Down
14 changes: 13 additions & 1 deletion tests/test_components/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ def test_sim_init():
_ = sim.dt
_ = sim.tmesh
sim.validate_pre_upload()
m = sim.get_monitor_by_name("point")
# will not work in 3.0
_ = sim.mediums
_ = sim.medium_map
m = sim.get_monitor_by_name("point")
_ = sim.background_structure
# will continue working in 3.0
_ = sim.scene.mediums
_ = sim.scene.medium_map
_ = sim.scene.background_structure
# sim.plot(x=0)
# plt.close()
# sim.plot_eps(x=0)
Expand Down Expand Up @@ -915,11 +920,18 @@ def test_sim_monitor_homogeneous():
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

# will be removed in 3.0
mediums = td.Simulation.intersecting_media(monitor_n2f_vol, [box])
assert len(mediums) == 1
mediums = td.Simulation.intersecting_media(monitor_n2f_vol, [box_transparent])
assert len(mediums) == 1

# continue in 3.0
mediums = td.Scene.intersecting_media(monitor_n2f_vol, [box])
assert len(mediums) == 1
mediums = td.Scene.intersecting_media(monitor_n2f_vol, [box_transparent])
assert len(mediums) == 1

# when another medium intersects an excluded surface, no errors should be raised
monitor_n2f_vol_exclude = td.FieldProjectionAngleMonitor(
center=(0.2, 0, 0.2),
Expand Down
174 changes: 173 additions & 1 deletion tidy3d/components/base_sim/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def simulation_geometry(self) -> Box:
@cached_property
def simulation_structure(self) -> Structure:
"""Returns structure representing the domain of the simulation. This differs from
``Simulation.background_structure`` in that it has finite extent."""
``Simulation.scene.background_structure`` in that it has finite extent."""
return Structure(geometry=self.simulation_geometry, medium=self.medium)

@equal_aspect
Expand Down Expand Up @@ -416,6 +416,178 @@ def plot_boundaries(
The supplied or created matplotlib axes.
"""

@equal_aspect
@add_ax_if_none
def plot_structures(
self,
x: float = None,
y: float = None,
z: float = None,
ax: Ax = None,
hlim: Tuple[float, float] = None,
vlim: Tuple[float, float] = None,
) -> Ax:
"""Plot each of simulation's structures on a plane defined by one nonzero x,y,z coordinate.
Parameters
----------
x : float = None
position of plane in x direction, only one of x, y, z must be specified to define plane.
y : float = None
position of plane in y direction, only one of x, y, z must be specified to define plane.
z : float = None
position of plane in z direction, only one of x, y, z must be specified to define plane.
ax : matplotlib.axes._subplots.Axes = None
Matplotlib axes to plot on, if not specified, one is created.
hlim : Tuple[float, float] = None
The x range if plotting on xy or xz planes, y range if plotting on yz plane.
vlim : Tuple[float, float] = None
The z range if plotting on xz or yz planes, y plane if plotting on xy plane.
Returns
-------
matplotlib.axes._subplots.Axes
The supplied or created matplotlib axes.
"""

hlim_new, vlim_new = Scene._get_plot_lims(
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim
)

return self.scene.plot_structures(x=x, y=y, z=z, ax=ax, hlim=hlim_new, vlim=vlim_new)

@equal_aspect
@add_ax_if_none
def plot_structures_eps(
self,
x: float = None,
y: float = None,
z: float = None,
freq: float = None,
alpha: float = None,
cbar: bool = True,
reverse: bool = False,
ax: Ax = None,
hlim: Tuple[float, float] = None,
vlim: Tuple[float, float] = None,
) -> Ax:
"""Plot each of simulation's structures on a plane defined by one nonzero x,y,z coordinate.
The permittivity is plotted in grayscale based on its value at the specified frequency.
Parameters
----------
x : float = None
position of plane in x direction, only one of x, y, z must be specified to define plane.
y : float = None
position of plane in y direction, only one of x, y, z must be specified to define plane.
z : float = None
position of plane in z direction, only one of x, y, z must be specified to define plane.
freq : float = None
Frequency to evaluate the relative permittivity of all mediums.
If not specified, evaluates at infinite frequency.
reverse : bool = False
If ``False``, the highest permittivity is plotted in black.
If ``True``, it is plotteed in white (suitable for black backgrounds).
cbar : bool = True
Whether to plot a colorbar for the relative permittivity.
alpha : float = None
Opacity of the structures being plotted.
Defaults to the structure default alpha.
ax : matplotlib.axes._subplots.Axes = None
Matplotlib axes to plot on, if not specified, one is created.
hlim : Tuple[float, float] = None
The x range if plotting on xy or xz planes, y range if plotting on yz plane.
vlim : Tuple[float, float] = None
The z range if plotting on xz or yz planes, y plane if plotting on xy plane.
Returns
-------
matplotlib.axes._subplots.Axes
The supplied or created matplotlib axes.
"""

hlim, vlim = Scene._get_plot_lims(
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim
)

return self.scene.plot_structures_eps(
freq=freq,
cbar=cbar,
alpha=alpha,
ax=ax,
x=x,
y=y,
z=z,
hlim=hlim,
vlim=vlim,
reverse=reverse,
)

@equal_aspect
@add_ax_if_none
def plot_structures_heat_conductivity(
self,
x: float = None,
y: float = None,
z: float = None,
alpha: float = None,
cbar: bool = True,
reverse: bool = False,
ax: Ax = None,
hlim: Tuple[float, float] = None,
vlim: Tuple[float, float] = None,
) -> Ax:
"""Plot each of simulation's structures on a plane defined by one nonzero x,y,z coordinate.
The permittivity is plotted in grayscale based on its value at the specified frequency.
Parameters
----------
x : float = None
position of plane in x direction, only one of x, y, z must be specified to define plane.
y : float = None
position of plane in y direction, only one of x, y, z must be specified to define plane.
z : float = None
position of plane in z direction, only one of x, y, z must be specified to define plane.
freq : float = None
Frequency to evaluate the relative permittivity of all mediums.
If not specified, evaluates at infinite frequency.
reverse : bool = False
If ``False``, the highest permittivity is plotted in black.
If ``True``, it is plotteed in white (suitable for black backgrounds).
cbar : bool = True
Whether to plot a colorbar for the relative permittivity.
alpha : float = None
Opacity of the structures being plotted.
Defaults to the structure default alpha.
ax : matplotlib.axes._subplots.Axes = None
Matplotlib axes to plot on, if not specified, one is created.
hlim : Tuple[float, float] = None
The x range if plotting on xy or xz planes, y range if plotting on yz plane.
vlim : Tuple[float, float] = None
The z range if plotting on xz or yz planes, y plane if plotting on xy plane.
Returns
-------
matplotlib.axes._subplots.Axes
The supplied or created matplotlib axes.
"""

hlim, vlim = Scene._get_plot_lims(
bounds=self.simulation_bounds, x=x, y=y, z=z, hlim=hlim, vlim=vlim
)

return self.scene.plot_structures_heat_conductivity(
cbar=cbar,
alpha=alpha,
ax=ax,
x=x,
y=y,
z=z,
hlim=hlim,
vlim=vlim,
reverse=reverse,
)

@classmethod
def from_scene(cls, scene: Scene, **kwargs) -> AbstractSimulation:
"""Create a simulation from a :class:.`Scene` instance. Must provide additional parameters
Expand Down
Loading

0 comments on commit 11b59e7

Please sign in to comment.