Skip to content

Commit f9b7b79

Browse files
committed
Small fixes for modes in 2D simulations
1 parent 93ec431 commit f9b7b79

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

tidy3d/components/data/sim_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def apply_symmetry(self, monitor_data: MonitorDataType) -> MonitorDataType:
104104
def source_spectrum(self, source_index: int) -> Callable:
105105
"""Get a spectrum normalization function for a given source index."""
106106

107-
if source_index is None:
107+
if source_index is None or len(self.simulation.sources) == 0:
108108
return np.ones_like
109109

110110
source = self.simulation.sources[source_index]
@@ -132,11 +132,11 @@ def renormalize(self, normalize_index: int) -> SimulationData:
132132
"""Return a copy of the :class:`.SimulationData` with a different source used for the
133133
normalization."""
134134

135-
if normalize_index == self.simulation.normalize_index:
135+
num_sources = len(self.simulation.sources)
136+
if normalize_index == self.simulation.normalize_index or num_sources == 0:
136137
# already normalized to that index
137138
return self.copy()
138139

139-
num_sources = len(self.simulation.sources)
140140
if normalize_index and (normalize_index < 0 or normalize_index >= num_sources):
141141
# normalize index out of bounds for source list
142142
raise DataError(

tidy3d/plugins/mode/mode_solver.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def _solver_grid(self) -> Grid:
8888
will be reset to the exact plane position after the solve."""
8989
plane_sym = self.simulation.min_sym_box(self.plane)
9090
boundaries = self.simulation.discretize(plane_sym, extend=True).boundaries.to_list
91+
# Do not extend if simulation has a single pixel along a dimension
92+
for dim, num_cells in enumerate(self.simulation.grid.num_cells):
93+
if num_cells <= 1:
94+
boundaries[dim] = self.simulation.grid.boundaries.to_list[dim]
9195
# Remove extension on the min side if symmetry present
9296
bounds_norm, bounds_plane = plane_sym.pop_axis(boundaries, self.normal_axis)
9397
bounds_plane = list(bounds_plane)
@@ -140,7 +144,14 @@ def data_raw(self) -> ModeSolverData:
140144
for field_name in ("Ex", "Ey", "Ez", "Hx", "Hy", "Hz"):
141145

142146
xyz_coords = self._solver_grid[field_name].to_list
147+
# Snap to plane center along normal direction
143148
xyz_coords[self.normal_axis] = [self.plane.center[self.normal_axis]]
149+
# Snap to simulation center if simulation is 0D along a tangential dimension
150+
_, plane_axes = self.plane.pop_axis([0, 1, 2], axis=self.normal_axis)
151+
for plane_axis in plane_axes:
152+
if len(xyz_coords[plane_axis]) == 1:
153+
xyz_coords[plane_axis] = [self.simulation.center[plane_axis]]
154+
144155
scalar_field_data = ScalarModeFieldDataArray(
145156
np.stack([field_freq[field_name] for field_freq in fields], axis=-2),
146157
coords=dict(

0 commit comments

Comments
 (0)