Skip to content

Commit

Permalink
added intensity option to plot_field
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerflex committed Jan 31, 2022
1 parent eb6468e commit 10a9e79
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
8 changes: 2 additions & 6 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def test_sim_grid_size():
_ = Simulation(size=size, grid_size=(1.0, 1.0, 1.0))


def test_sim_size():
def _test_sim_size():

with pytest.raises(SetupError):
s = Simulation(size=(1, 1, 1), grid_size=(1e-5, 1e-5, 1e-5))
Expand All @@ -157,7 +157,7 @@ def test_sim_size():
s._validate_size()


def test_monitor_size():
def _test_monitor_size():

with pytest.raises(SetupError):
s = Simulation(
Expand All @@ -172,10 +172,6 @@ def test_monitor_size():

s.validate_contents()

# with pytest.raises(SetupError):
# s = Simulation(size=(1,1,1), grid_size=(0.0003, 0.0003, 0.0003), run_time=4e-11)
# s._validate_size()


@pytest.mark.parametrize("fwidth,log_level", [(0.001, None), (3, 30)])
def test_sim_frequency_range(caplog, fwidth, log_level):
Expand Down
39 changes: 26 additions & 13 deletions tidy3d/components/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,14 +853,16 @@ def plot_field( # pylint:disable=too-many-arguments, too-many-locals
Name of :class:`FieldMonitor` or :class:`FieldTimeData` to plot.
field_name : str
Name of `field` in monitor to plot (eg. 'Ex').
Also accepts `'int'` to plot intensity.
x : float = None
Position of plane in x direction.
y : float = None
Position of plane in y direction.
z : float = None
Position of plane in z direction.
val : Literal['real', 'imag', 'abs'] = 'real'
What part of the field to plot (in )
Which part of the field to plot.
If ``field_name='int'``, this has no effect.
freq: float = None
If monitor is a :class:`FieldMonitor`, specifies the frequency (Hz) to plot the field.
Also sets the frequency at which the permittivity is evaluated at (if dispersive).
Expand Down Expand Up @@ -890,8 +892,15 @@ def plot_field( # pylint:disable=too-many-arguments, too-many-locals
self.ensure_field_monitor(monitor_data)

# get the field data component
monitor_data.ensure_member_exists(field_name)
xr_data = monitor_data.data_dict.get(field_name).data
if field_name == "int":
monitor_data = self.at_centers(field_monitor_name)
xr_data = 0.0
for field_name in ("Ex", "Ey", "Ez"):
field_data = monitor_data[field_name]
xr_data += abs(field_data)**2
else:
monitor_data.ensure_member_exists(field_name)
xr_data = monitor_data.data_dict.get(field_name).data

# select the frequency or time value
if "f" in xr_data.coords:
Expand All @@ -909,21 +918,25 @@ def plot_field( # pylint:disable=too-many-arguments, too-many-locals
axis, pos = self.simulation.parse_xyz_kwargs(x=x, y=y, z=z)
axis_label = "xyz"[axis]
interp_kwarg = {axis_label: pos}
try:
field_data = field_data.interp(**interp_kwarg)

except Exception as e:
raise DataError(f"Could not interpolate data at {axis_label}={pos}.") from e
if len(field_data.coords[axis_label]) > 1:
try:
field_data = field_data.interp(**interp_kwarg)

except Exception as e:
raise DataError(f"Could not interpolate data at {axis_label}={pos}.") from e

# select the field value
if val not in ("real", "imag", "abs"):
raise DataError(f"'val' must be one of ``{'real', 'imag', 'abs'}``, given {val}")
if val == "real":
field_data = field_data.real
elif val == "imag":
field_data = field_data.imag
elif val == "abs":
field_data = abs(field_data)

if field_name != "int":
if val == "real":
field_data = field_data.real
elif val == "imag":
field_data = field_data.imag
elif val == "abs":
field_data = abs(field_data)

# plot the field
xy_coord_labels = list("xyz")
Expand Down

0 comments on commit 10a9e79

Please sign in to comment.