Skip to content

Commit

Permalink
Ensuring mode solver fields are returned in requested precision
Browse files Browse the repository at this point in the history
  • Loading branch information
momchil-flex committed Nov 21, 2023
1 parent 1b5b46c commit 0029d47
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
14 changes: 13 additions & 1 deletion tests/test_plugins/test_mode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def compare_colocation(ms):
for key, field in data_col.field_components.items():

# Check the colocated data is the same
assert np.allclose(data_at_boundaries[key], field)
assert np.allclose(data_at_boundaries[key], field, atol=1e-7)

# Also check coordinates
for dim, coords1 in field.coords.items():
Expand Down Expand Up @@ -203,6 +203,15 @@ def verify_pol_fraction(ms):
)


def verify_dtype(ms):
"""Verify that the returned fields have the correct dtype w.r.t. the specified precision."""

dtype = np.complex64 if ms.mode_spec.precision == "single" else np.complex128
for field in ms.data.field_components.values():
print(dtype, field.dtype, type(field.dtype))
assert dtype == field.dtype


def test_mode_solver_validation():
"""Test invalidate mode solver setups."""

Expand Down Expand Up @@ -295,6 +304,7 @@ def test_mode_solver_simple(mock_remote_api, local):
if local:
compare_colocation(ms)
verify_pol_fraction(ms)
verify_dtype(ms)
dataframe = ms.data.to_dataframe()

else:
Expand Down Expand Up @@ -458,6 +468,7 @@ def test_mode_solver_angle_bend():
)
compare_colocation(ms)
verify_pol_fraction(ms)
verify_dtype(ms)
dataframe = ms.data.to_dataframe()

# Plot field
Expand Down Expand Up @@ -493,6 +504,7 @@ def test_mode_solver_2D():
)
compare_colocation(ms)
verify_pol_fraction(ms)
verify_dtype(ms)
dataframe = ms.data.to_dataframe()

mode_spec = td.ModeSpec(
Expand Down
3 changes: 2 additions & 1 deletion tidy3d/components/data/monitor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,8 @@ def _reorder_modes(
]

# Apply phase shift
field_sorted.data = field_sorted.data * np.exp(-1j * phase[None, None, None, :, :])
phase_fact = np.exp(-1j * phase[None, None, None, :, :]).astype(field_sorted.data.dtype)
field_sorted.data = field_sorted.data * phase_fact

update_dict[field_name] = field_sorted

Expand Down
5 changes: 4 additions & 1 deletion tidy3d/components/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,10 @@ class ModeSolverMonitor(AbstractModeMonitor):

def storage_size(self, num_cells: int, tmesh: int) -> int:
"""Size of monitor storage given the number of points after discretization."""
return 6 * BYTES_COMPLEX * num_cells * len(self.freqs) * self.mode_spec.num_modes
bytes_single = 6 * BYTES_COMPLEX * num_cells * len(self.freqs) * self.mode_spec.num_modes
if self.mode_spec.precision == "double":
return 2 * bytes_single
return bytes_single


class FieldProjectionSurface(Tidy3dBaseModel):
Expand Down
2 changes: 1 addition & 1 deletion tidy3d/plugins/mode/mode_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def _colocate_data(self, mode_solver_data: ModeSolverData) -> ModeSolverData:
# Colocate input data to new coordinates
data_dict_colocated = {}
for key, field in mode_solver_data.symmetry_expanded.field_components.items():
data_dict_colocated[key] = field.interp(**colocate_coords)
data_dict_colocated[key] = field.interp(**colocate_coords).astype(field.dtype)

# Update data
mode_solver_monitor = self.to_mode_solver_monitor(name=MODE_MONITOR_NAME)
Expand Down
4 changes: 4 additions & 0 deletions tidy3d/plugins/mode/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def compute_modes(

fields = np.stack((E, H), axis=0)

if mode_spec.precision == "single":
# Recast to single precision which may have changed due to earlier manipulations
fields = fields.astype(np.complex64)

return fields, neff + 1j * keff, eps_spec

@classmethod
Expand Down

0 comments on commit 0029d47

Please sign in to comment.