Skip to content

Commit

Permalink
make frequency and resolution parameters of plot2D dictionary keys of…
Browse files Browse the repository at this point in the history
… eps_parameters
  • Loading branch information
oskooi committed Nov 24, 2021
1 parent b47d272 commit 00f7b48
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 58 deletions.
21 changes: 7 additions & 14 deletions doc/docs/Python_User_Interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -2632,8 +2632,6 @@ def plot2D(self,
source_parameters=None,
monitor_parameters=None,
field_parameters=None,
frequency=None,
resolution=None,
plot_eps_flag=True,
plot_sources_flag=True,
plot_monitors_flag=True,
Expand All @@ -2660,18 +2658,13 @@ plt.show()
plt.savefig('sim_domain.png')
```
If you just want to quickly visualize the simulation domain without the fields (i.e., when
debugging your simulation), there is no need to invoke the `run` function prior to calling
setting up your simulation), there is no need to invoke the `run` function prior to calling
`plot2D`. Just define the `Simulation` object followed by any DFT monitors and then
invoke `plot2D`.

Note: When running a [parallel simulation](Parallel_Meep.md), the `plot2D` function expects
to be called on all processes, but only generates a plot on the master process.

Note: The geometry function is evaluated at infinite frequency (i.e., the instantaneous response).
Dispersive materials containing complex, wavelength-dependent $\varepsilon$ are ignored. To
visualize dispersive materials, you can use the `get_array` or `output_epsilon` functions and
specify a `frequency` parameter for evaluating the complex $\varepsilon$.

**Parameters:**

* `ax`: a `matplotlib` axis object. `plot2D()` will add plot objects, like lines,
Expand All @@ -2692,8 +2685,12 @@ specify a `frequency` parameter for evaluating the complex $\varepsilon$.
- `alpha=1.0`: transparency of geometry
- `contour=False`: if `True`, plot a contour of the geometry rather than its image
- `contour_linewidth=1`: line width of the contour lines if `contour=True`
- `resolution=None`: the resolution to sample the $\varepsilon$ grid. Defaults to the
`resolution` of the `Simulation` object.
- `frequency=None`: for materials with a [frequency-dependent
permittivity](Materials.md#material-dispersion) $\varepsilon(f)$, specifies the
frequency $f$ (in Meep units) of the real part of the permittivity to use in the
plot. Defaults to the `frequency` parameter of the [Source](#source) object.
- `resolution=None`: the resolution of the $\varepsilon$ grid. Defaults to the
`resolution` of the `Simulation` object.
* `boundary_parameters`: a `dict` of optional plotting parameters that override
the default parameters for the boundary layers.
- `alpha=1.0`: transparency of boundary layers
Expand Down Expand Up @@ -2730,10 +2727,6 @@ specify a `frequency` parameter for evaluating the complex $\varepsilon$.
- `alpha=0.6`: transparency of fields
- `post_process=np.real`: post processing function to apply to fields (must be
a function object)
* `frequency`: for materials with a [frequency-dependent
permittivity](Materials.md#material-dispersion) $\varepsilon(f)$, specifies the
frequency $f$ (in Meep units) of the real part of the permittivity to use in the
plot. Defaults to the `frequency` parameter of the [Source](#source) object.

</div>

Expand Down
28 changes: 10 additions & 18 deletions python/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4070,10 +4070,9 @@ def get_sfield_p(self,snap=False):
def plot2D(self, ax=None, output_plane=None, fields=None, labels=False,
eps_parameters=None, boundary_parameters=None,
source_parameters=None, monitor_parameters=None,
field_parameters=None, frequency=None, resolution=None,
plot_eps_flag=True, plot_sources_flag=True,
plot_monitors_flag=True, plot_boundaries_flag=True,
**kwargs):
field_parameters=None, plot_eps_flag=True,
plot_sources_flag=True, plot_monitors_flag=True,
plot_boundaries_flag=True, **kwargs):
"""
Plots a 2D cross section of the simulation domain using `matplotlib`. The plot
includes the geometry, boundary layers, sources, and monitors. Fields can also be
Expand All @@ -4092,18 +4091,13 @@ def plot2D(self, ax=None, output_plane=None, fields=None, labels=False,
plt.savefig('sim_domain.png')
```
If you just want to quickly visualize the simulation domain without the fields (i.e., when
debugging your simulation), there is no need to invoke the `run` function prior to calling
setting up your simulation), there is no need to invoke the `run` function prior to calling
`plot2D`. Just define the `Simulation` object followed by any DFT monitors and then
invoke `plot2D`.
Note: When running a [parallel simulation](Parallel_Meep.md), the `plot2D` function expects
to be called on all processes, but only generates a plot on the master process.
Note: The geometry function is evaluated at infinite frequency (i.e., the instantaneous response).
Dispersive materials containing complex, wavelength-dependent $\\varepsilon$ are ignored. To
visualize dispersive materials, you can use the `get_array` or `output_epsilon` functions and
specify a `frequency` parameter for evaluating the complex $\\varepsilon$.
**Parameters:**
* `ax`: a `matplotlib` axis object. `plot2D()` will add plot objects, like lines,
Expand All @@ -4124,8 +4118,12 @@ def plot2D(self, ax=None, output_plane=None, fields=None, labels=False,
- `alpha=1.0`: transparency of geometry
- `contour=False`: if `True`, plot a contour of the geometry rather than its image
- `contour_linewidth=1`: line width of the contour lines if `contour=True`
- `resolution=None`: the resolution to sample the $\\varepsilon$ grid. Defaults to the
`resolution` of the `Simulation` object.
- `frequency=None`: for materials with a [frequency-dependent
permittivity](Materials.md#material-dispersion) $\\varepsilon(f)$, specifies the
frequency $f$ (in Meep units) of the real part of the permittivity to use in the
plot. Defaults to the `frequency` parameter of the [Source](#source) object.
- `resolution=None`: the resolution of the $\\varepsilon$ grid. Defaults to the
`resolution` of the `Simulation` object.
* `boundary_parameters`: a `dict` of optional plotting parameters that override
the default parameters for the boundary layers.
- `alpha=1.0`: transparency of boundary layers
Expand Down Expand Up @@ -4162,10 +4160,6 @@ def plot2D(self, ax=None, output_plane=None, fields=None, labels=False,
- `alpha=0.6`: transparency of fields
- `post_process=np.real`: post processing function to apply to fields (must be
a function object)
* `frequency`: for materials with a [frequency-dependent
permittivity](Materials.md#material-dispersion) $\\varepsilon(f)$, specifies the
frequency $f$ (in Meep units) of the real part of the permittivity to use in the
plot. Defaults to the `frequency` parameter of the [Source](#source) object.
"""
return vis.plot2D(self,
ax=ax,
Expand All @@ -4177,8 +4171,6 @@ def plot2D(self, ax=None, output_plane=None, fields=None, labels=False,
source_parameters=source_parameters,
monitor_parameters=monitor_parameters,
field_parameters=field_parameters,
frequency=frequency,
resolution=resolution,
plot_eps_flag=plot_eps_flag,
plot_sources_flag=plot_sources_flag,
plot_monitors_flag=plot_monitors_flag,
Expand Down
53 changes: 27 additions & 26 deletions python/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
'cmap':'binary',
'alpha':1.0,
'contour':False,
'contour_linewidth':1
'contour_linewidth':1,
'frequency':None,
'resolution':None
}

default_boundary_parameters = {
Expand Down Expand Up @@ -327,10 +329,27 @@ def sort_points(xy):
return ax
return ax

def plot_eps(sim, ax, output_plane=None, eps_parameters=None, resolution=None, frequency=0):
def plot_eps(sim, ax, output_plane=None, eps_parameters=None):
# consolidate plotting parameters
eps_parameters = default_eps_parameters if eps_parameters is None else dict(default_eps_parameters, **eps_parameters)

# Determine a frequency to plot all epsilon
if eps_parameters['frequency'] is None:
try:
# Continuous sources
eps_parameters['frequency'] = sim.sources[0].frequency
except:
try:
# Gaussian sources
eps_parameters['frequency'] = sim.sources[0].src.frequency
except:
try:
# Custom sources
eps_parameters['frequency'] = sim.sources[0].src.center_frequency
except:
# No sources
eps_parameters['frequency'] = 0

# Get domain measurements
sim_center, sim_size = get_2D_dimensions(sim, output_plane)

Expand All @@ -344,7 +363,7 @@ def plot_eps(sim, ax, output_plane=None, eps_parameters=None, resolution=None, f
center = Vector3(sim_center.x, sim_center.y, sim_center.z)
cell_size = Vector3(sim_size.x, sim_size.y, sim_size.z)

grid_resolution = resolution if resolution else sim.resolution
grid_resolution = eps_parameters['resolution'] if eps_parameters['resolution'] else sim.resolution
Nx = int((xmax - xmin) * grid_resolution + 1)
Ny = int((ymax - ymin) * grid_resolution + 1)
Nz = int((zmax - zmin) * grid_resolution + 1)
Expand Down Expand Up @@ -376,7 +395,7 @@ def plot_eps(sim, ax, output_plane=None, eps_parameters=None, resolution=None, f
else:
raise ValueError("A 2D plane has not been specified...")

eps_data = np.rot90(np.real(sim.get_epsilon_grid(xtics, ytics, ztics, frequency)))
eps_data = np.rot90(np.real(sim.get_epsilon_grid(xtics, ytics, ztics, eps_parameters['frequency'])))

if mp.am_master():
if eps_parameters['contour']:
Expand Down Expand Up @@ -543,32 +562,15 @@ def plot_fields(sim, ax=None, fields=None, output_plane=None, field_parameters=N
def plot2D(sim, ax=None, output_plane=None, fields=None, labels=False,
eps_parameters=None, boundary_parameters=None,
source_parameters=None, monitor_parameters=None,
field_parameters=None, frequency=None, resolution=None,
plot_eps_flag=True, plot_sources_flag=True,
plot_monitors_flag=True, plot_boundaries_flag=True):
field_parameters=None, plot_eps_flag=True,
plot_sources_flag=True, plot_monitors_flag=True,
plot_boundaries_flag=True):

# Ensure a figure axis exists
if ax is None and mp.am_master():
from matplotlib import pyplot as plt
ax = plt.gca()

# Determine a frequency to plot all epsilon
if frequency is None:
try:
# Continuous sources
frequency = sim.sources[0].frequency
except:
try:
# Gaussian sources
frequency = sim.sources[0].src.frequency
except:
try:
# Custom sources
frequency = sim.sources[0].src.center_frequency
except:
# No sources
frequency = 0

# validate the output plane to ensure proper 2D coordinates
from meep.simulation import Volume
sim_center, sim_size = get_2D_dimensions(sim, output_plane)
Expand All @@ -577,8 +579,7 @@ def plot2D(sim, ax=None, output_plane=None, fields=None, labels=False,
# Plot geometry
if plot_eps_flag:
ax = plot_eps(sim, ax, output_plane=output_plane,
eps_parameters=eps_parameters, resolution=resolution,
frequency=frequency)
eps_parameters=eps_parameters)

# Plot boundaries
if plot_boundaries_flag:
Expand Down

0 comments on commit 00f7b48

Please sign in to comment.