Skip to content

Commit 16607e9

Browse files
authored
Avoid interpolation errors with some xarray versions (#2538)
1 parent e902f77 commit 16607e9

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626
- Bug when generating a grid with snapping points near the simulation boundaries.
2727
- Fixed field colocation in `EMEModeSolverMonitor`.
2828
- Solver error for EME simulations with bends, introduced when support for 2D EME simulations was added.
29+
- Internal interpolation errors with some versions of `xarray` and `numpy`.
2930

3031
### Changed
3132
- Relaxed bounds checking of path integrals during `WavePort` validation.

tidy3d/components/data/monitor_data.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,13 @@ def to_zbf(
11461146
"'freq' was not specified for 'FieldData.to_zbf()'. Defaulting to the mean frequency of the dataset."
11471147
)
11481148
freq = np.mean(e_x.coords["f"].values)
1149+
else:
1150+
freq = np.array(freq)
1151+
1152+
if freq.size > 1:
1153+
raise ValueError("'freq' must be a single value, not an array.")
1154+
else:
1155+
freq = freq.item()
11491156

11501157
mode_area = mode_area.interp(f=freq)
11511158
e_x = e_x.interp(f=freq)

tidy3d/components/data/sim_data.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,17 +583,20 @@ def plot_field_monitor_data(
583583

584584
# select the extra coordinates out of the data from user-specified kwargs
585585
for coord_name, coord_val in sel_kwargs.items():
586+
interp_val = np.array(coord_val)
587+
if interp_val.size == 1:
588+
interp_val = interp_val.item()
586589
if (
587590
field_data.coords[coord_name].size <= 1
588591
or coord_name == "eme_port_index"
589592
or coord_name == "eme_cell_index"
590593
or coord_name == "sweep_index"
591594
or coord_name == "mode_index"
592595
):
593-
field_data = field_data.sel(**{coord_name: coord_val}, method=None)
596+
field_data = field_data.sel(**{coord_name: interp_val}, method=None)
594597
else:
595598
field_data = field_data.interp(
596-
**{coord_name: coord_val}, kwargs={"bounds_error": True}
599+
**{coord_name: interp_val}, kwargs={"bounds_error": True}
597600
)
598601

599602
# before dropping coordinates, check if a frequency can be derived from the data that can

tidy3d/components/mode/mode_solver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,7 @@ def _grid_correction(
16991699
Copy of the data with renormalized fields.
17001700
"""
17011701
normal_axis = plane.size.index(0.0)
1702-
normal_pos = plane.center[normal_axis]
1702+
normal_pos = float(plane.center[normal_axis])
17031703
normal_dim = "xyz"[normal_axis]
17041704

17051705
# Primal and dual grid along the normal direction,

tidy3d/components/parameter_perturbation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,10 @@ def sample(
942942
if isinstance(h_vals, UnstructuredGridDataset):
943943
h_vals = h_vals.values
944944

945+
# Needed to avoid error in some xarray / numpy versions
946+
e_vals = e_vals.item() if e_vals.size == 1 else e_vals
947+
h_vals = h_vals.item() if h_vals.size == 1 else h_vals
948+
945949
# note that the dimensionality of this operation differs depending on whether xarrays
946950
# or simple unlabeled arrays are provided:
947951
# - for unlabeled arrays, values are broadcasted

0 commit comments

Comments
 (0)