tidy3d.SimulationData#

class SimulationData[source]#

Bases: AbstractSimulationData

Stores data from a collection of Monitor objects in a Simulation.

Parameters:

Notes

The SimulationData objects store a copy of the original Simulation:, so it can be recovered if the SimulationData is loaded in a new session and the Simulation is no longer in memory.

More importantly, the SimulationData contains a reference to the data for each of the monitors within the original Simulation. This data can be accessed directly using the name given to the monitors initially.

Examples

Standalone example:

>>> import tidy3d as td
>>> num_modes = 5
>>> x = [-1,1,3]
>>> y = [-2,0,2,4]
>>> z = [-3,-1,1,3,5]
>>> f = [2e14, 3e14]
>>> coords = dict(x=x[:-1], y=y[:-1], z=z[:-1], f=f)
>>> grid = td.Grid(boundaries=td.Coords(x=x, y=y, z=z))
>>> scalar_field = td.ScalarFieldDataArray((1+1j) * np.random.random((2,3,4,2)), coords=coords)
>>> field_monitor = td.FieldMonitor(
...     size=(2,4,6),
...     freqs=[2e14, 3e14],
...     name='field',
...     fields=['Ex'],
...     colocate=True,
... )
>>> sim = Simulation(
...     size=(2, 4, 6),
...     grid_spec=td.GridSpec(wavelength=1.0),
...     monitors=[field_monitor],
...     run_time=2e-12,
...     sources=[
...         td.UniformCurrentSource(
...             size=(0, 0, 0),
...             center=(0, 0.5, 0),
...             polarization="Hx",
...             source_time=td.GaussianPulse(
...                 freq0=2e14,
...                 fwidth=4e13,
...             ),
...         )
...     ],
... )
>>> field_data = td.FieldData(monitor=field_monitor, Ex=scalar_field, grid_expanded=grid)
>>> sim_data = td.SimulationData(simulation=sim, data=(field_data,))

To save and load the SimulationData object.

sim_data.to_file(fname='path/to/file.hdf5') # Save a SimulationData object to a HDF5 file
sim_data = SimulationData.from_file(fname='path/to/file.hdf5') # Load a SimulationData object from a HDF5 file.

Attributes

final_decay_value

Returns value of the field decay at the final time step.

data

log

Custom logger to avoid the complexities of the logging module

Methods

apply_phase(data[, phase])

Apply a phase to xarray data.

at_boundaries(field_monitor_name)

Return xarray.Dataset representation of field monitor data colocated at Yee cell boundaries.

at_centers(field_monitor_name)

Return xarray.Dataset representation of field monitor data colocated at Yee cell centers.

get_intensity(field_monitor_name)

return xarray.DataArray of the intensity of a field monitor at Yee cell centers.

get_poynting_vector(field_monitor_name)

return xarray.Dataset of the Poynting vector at Yee cell centers.

load_field_monitor(monitor_name)

Load monitor and raise exception if not a field monitor.

mnt_data_from_file(fname, mnt_name, ...)

Loads data for a specific monitor from a .hdf5 file with data for a SimulationData.

plot_field(field_monitor_name, field_name[, ...])

Plot the field data for a monitor with simulation plot overlaid.

plot_scalar_array(field_data, axis, position)

Plot the field data for a monitor with simulation plot overlaid.

renormalize(normalize_index)

Return a copy of the SimulationData with a different source used for the normalization.

source_spectrum(source_index)

Get a spectrum normalization function for a given source index.

simulation#
data#
diverged#
property final_decay_value#

Returns value of the field decay at the final time step.

source_spectrum(source_index)[source]#

Get a spectrum normalization function for a given source index.

renormalize(normalize_index)[source]#

Return a copy of the SimulationData with a different source used for the normalization.

load_field_monitor(monitor_name)[source]#

Load monitor and raise exception if not a field monitor.

at_centers(field_monitor_name)[source]#

Return xarray.Dataset representation of field monitor data colocated at Yee cell centers.

Parameters:

field_monitor_name (str) – Name of field monitor used in the original Simulation.

Returns:

Dataset containing all of the fields in the data interpolated to center locations on the Yee grid.

Return type:

xarray.Dataset

at_boundaries(field_monitor_name)[source]#

Return xarray.Dataset representation of field monitor data colocated at Yee cell boundaries.

Parameters:

field_monitor_name (str) – Name of field monitor used in the original Simulation.

Returns:

Dataset containing all of the fields in the data interpolated to boundary locations on the Yee grid.

Return type:

xarray.Dataset

get_poynting_vector(field_monitor_name)[source]#

return xarray.Dataset of the Poynting vector at Yee cell centers.

Calculated values represent the instantaneous Poynting vector for time-domain fields and the complex vector for frequency-domain: S = 1/2 E × conj(H).

Only the available components are returned, e.g., if the indicated monitor doesn’t include field component “Ex”, then “Sy” and “Sz” will not be calculated.

Parameters:

field_monitor_name (str) – Name of field monitor used in the original Simulation.

Returns:

DataArray containing the Poynting vector calculated based on the field components colocated at the center locations of the Yee grid.

Return type:

xarray.DataArray

get_intensity(field_monitor_name)[source]#

return xarray.DataArray of the intensity of a field monitor at Yee cell centers.

Parameters:

field_monitor_name (str) – Name of field monitor used in the original Simulation.

Returns:

DataArray containing the electric intensity of the field-like monitor. Data is interpolated to the center locations on Yee grid.

Return type:

xarray.DataArray

classmethod mnt_data_from_file(fname, mnt_name, **parse_obj_kwargs)[source]#

Loads data for a specific monitor from a .hdf5 file with data for a SimulationData.

Parameters:
  • fname (str) – Full path to an hdf5 file containing SimulationData data.

  • mnt_name (str, optional) – .name of the monitor to load the data from.

  • **parse_obj_kwargs – Keyword arguments passed to either pydantic’s parse_obj function when loading model.

Returns:

Monitor data corresponding to the mnt_name type.

Return type:

MonitorData

Example

>>> field_data = SimulationData.from_file(fname='folder/data.hdf5', mnt_name="field") 
static apply_phase(data, phase=0.0)[source]#

Apply a phase to xarray data.

plot_field(field_monitor_name, field_name, val='real', scale='lin', eps_alpha=0.2, phase=0.0, robust=True, vmin=None, vmax=None, ax=None, **sel_kwargs)[source]#

Plot the field data for a monitor with simulation plot overlaid.

Parameters:
  • field_monitor_name (str) – Name of FieldMonitor, FieldTimeData, or ModeSolverData to plot.

  • field_name (str) – Name of field component to plot (eg. ‘Ex’). Also accepts 'E' and 'H' to plot the vector magnitudes of the electric and magnetic fields, and 'S' for the Poynting vector.

  • val (Literal['real', 'imag', 'abs', 'abs^2', 'phase'] = 'real') – Which part of the field to plot.

  • scale (Literal['lin', 'dB']) – Plot in linear or logarithmic (dB) scale.

  • eps_alpha (float = 0.2) – Opacity of the structure permittivity. Must be between 0 and 1 (inclusive).

  • phase (float = 0.0) – Optional phase (radians) to apply to the fields. Only has an effect on frequency-domain fields.

  • robust (bool = True) – If True and vmin or vmax are absent, uses the 2nd and 98th percentiles of the data to compute the color limits. This helps in visualizing the field patterns especially in the presence of a source.

  • vmin (float = None) – The lower bound of data range that the colormap covers. If None, they are inferred from the data and other keyword arguments.

  • vmax (float = None) – The upper bound of data range that the colormap covers. If None, they are inferred from the data and other keyword arguments.

  • ax (matplotlib.axes._subplots.Axes = None) – matplotlib axes to plot on, if not specified, one is created.

  • sel_kwargs (keyword arguments used to perform .sel() selection in the monitor data.) – These kwargs can select over the spatial dimensions (x, y, z), frequency or time dimensions (f, t) or mode_index, if applicable. For the plotting to work appropriately, the resulting data after selection must contain only two coordinates with len > 1. Furthermore, these should be spatial coordinates (x, y, or z).

Returns:

The supplied or created matplotlib axes.

Return type:

matplotlib.axes._subplots.Axes

plot_scalar_array(field_data, axis, position, freq=None, eps_alpha=0.2, robust=True, vmin=None, vmax=None, cmap_type='divergent', ax=None)[source]#

Plot the field data for a monitor with simulation plot overlaid.

Parameters:
  • field_data (xr.DataArray) – DataArray with the field data to plot. Must be a scalar field.

  • axis (Axis) – Axis normal to the plotting plane.

  • position (float) – Position along the axis.

  • freq (float = None) – Frequency at which the permittivity is evaluated at (if dispersive). By default, chooses permittivity as frequency goes to infinity.

  • eps_alpha (float = 0.2) – Opacity of the structure permittivity. Must be between 0 and 1 (inclusive).

  • robust (bool = True) – If True and vmin or vmax are absent, uses the 2nd and 98th percentiles of the data to compute the color limits. This helps in visualizing the field patterns especially in the presence of a source.

  • vmin (float = None) – The lower bound of data range that the colormap covers. If None, they are inferred from the data and other keyword arguments.

  • vmax (float = None) – The upper bound of data range that the colormap covers. If None, they are inferred from the data and other keyword arguments.

  • cmap_type (Literal["divergent", "sequential", "cyclic"] = "divergent") – Type of color map to use for plotting.

  • ax (matplotlib.axes._subplots.Axes = None) – matplotlib axes to plot on, if not specified, one is created.

Returns:

The supplied or created matplotlib axes.

Return type:

matplotlib.axes._subplots.Axes

__hash__()#

Hash method.