tidy3d.SimulationData#

class SimulationData[source]#

Bases: AbstractSimulationData

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

Parameters:
  • simulation (Attribute: simulation) โ€“

    Type

    Simulation

    Default

    Description

    Original Simulation associated with the data.

  • data (Attribute: data) โ€“

    Type

    Tuple[Annotated[Union[tidy3d.components.data.monitor_data.FieldData, tidy3d.components.data.monitor_data.FieldTimeData, tidy3d.components.data.monitor_data.PermittivityData, tidy3d.components.data.monitor_data.ModeSolverData, tidy3d.components.data.monitor_data.ModeData, tidy3d.components.data.monitor_data.FluxData, tidy3d.components.data.monitor_data.FluxTimeData, tidy3d.components.data.monitor_data.FieldProjectionKSpaceData, tidy3d.components.data.monitor_data.FieldProjectionCartesianData, tidy3d.components.data.monitor_data.FieldProjectionAngleData, tidy3d.components.data.monitor_data.DiffractionData], FieldInfo(default=PydanticUndefined, discriminator=โ€™typeโ€™, extra={})], โ€ฆ]

    Default

    Description

    List of MonitorData instances associated with the monitors of the original Simulation.

  • log (Attribute: log) โ€“

    Type

    Optional[str]

    Default

    = None

    Description

    A string containing the log information from the simulation run.

  • diverged (Attribute: diverged) โ€“

    Type

    bool

    Default

    = False

    Description

    A boolean flag denoting whether the simulation run diverged.

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.