Skip to content

Commit

Permalink
Added bend_angle_rotation in mode_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
QimingFlex committed Nov 1, 2024
1 parent e76d6cc commit 4e6dbc7
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Users can manually specify the background medium for a structure to be used for geometry gradient calculations by supplying `Structure.background_permittivity`. This is useful when there are overlapping structures or structures embedded in other mediums.
- Autograd functions can now be called directly on `DataArray` (e.g., `np.sum(data_array)`) in objective functions.
- Automatic differentiation support for local field projections with `FieldProjectionAngleMonitor` and `FieldProjectionCartesianMonitor` using `FieldProjector.project_fields(far_field_monitor)`.
- `bend_angle_rotation` in `mode_spec` to improve accuracy when both bend and angle are defined."

### Changed
- Improved autograd tracer handling in `DataArray`, resulting in significant speedups for differentiation involving large monitors.
Expand Down
2 changes: 2 additions & 0 deletions tidy3d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
PointDataArray,
ScalarFieldDataArray,
ScalarFieldTimeDataArray,
ScalarModeFieldCylindricalDataArray,
ScalarModeFieldDataArray,
SpatialDataArray,
)
Expand Down Expand Up @@ -371,6 +372,7 @@ def set_logging_level(level: str) -> None:
"FieldProjector",
"ScalarFieldDataArray",
"ScalarModeFieldDataArray",
"ScalarModeFieldCylindricalDataArray",
"ScalarFieldTimeDataArray",
"SpatialDataArray",
"ModeAmpsDataArray",
Expand Down
18 changes: 18 additions & 0 deletions tidy3d/components/data/data_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,24 @@ class ScalarModeFieldDataArray(AbstractSpatialDataArray):
_dims = ("x", "y", "z", "f", "mode_index")


class ScalarModeFieldCylindricalDataArray(AbstractSpatialDataArray):
"""Spatial distribution of a mode in frequency-domain as a function of mode index.
Example
-------
>>> rho = [1,2]
>>> theta = [2,3,4]
>>> axial = [3,4,5,6]
>>> f = [2e14, 3e14]
>>> mode_index = np.arange(5)
>>> coords = dict(rho=rho, theta=theta, axial=axial, f=f, mode_index=mode_index)
>>> fd = ScalarModeFieldCylindricalDataArray((1+1j) * np.random.random((2,3,4,2,5)), coords=coords)
"""

__slots__ = ()
_dims = ("rho", "theta", "axial", "f", "mode_index")


class FluxDataArray(DataArray):
"""Flux through a surface in the frequency-domain.
Expand Down
2 changes: 2 additions & 0 deletions tidy3d/components/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
PointDataArray,
ScalarFieldDataArray,
ScalarFieldTimeDataArray,
ScalarModeFieldCylindricalDataArray,
ScalarModeFieldDataArray,
SpatialDataArray,
TimeDataArray,
Expand Down Expand Up @@ -149,6 +150,7 @@ def colocate(self, x=None, y=None, z=None) -> xr.Dataset:
ScalarFieldDataArray,
ScalarFieldTimeDataArray,
ScalarModeFieldDataArray,
ScalarModeFieldCylindricalDataArray,
EMEScalarModeFieldDataArray,
EMEScalarFieldDataArray,
]
Expand Down
23 changes: 23 additions & 0 deletions tidy3d/components/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ class ModeSpec(Tidy3dBaseModel):
"yz plane, the ``bend_axis`` is always 1 (the global z axis).",
)

bend_angle_rotation: bool = pd.Field(
False,
title="Use fields rotation when both bend and angle are defined",
description="Defines how modes are computed when both a bend and an angle are defined. "
" If `False`, the two coordinate transformations are directly composed. If `True`, the "
"structures in the simulation are first rotated, to compute a mode solution at a reference"
"plane normal to the bend's azimuthal direction. Then, the fields are rotated to align with"
"the mode plane, using the `n_eff` calculated at the reference plane. The second option can"
"produce more accurate results, but more care must be taken for example in ensuring that the"
"original mode plane intersects the right geometries in the simulation with rotated structures.",
)

track_freq: Union[TrackFreq, None] = pd.Field(
"central",
title="Mode Tracking Frequency",
Expand Down Expand Up @@ -160,6 +172,17 @@ def bend_radius_not_zero(cls, val, values):
raise SetupError("The magnitude of 'bend_radius' must be larger than 0.")
return val

@pd.validator("bend_angle_rotation", always=True)
@skip_if_fields_missing(["bend_radius", "bend_axis"])
def validate_bend_correction_requirements(cls, val, values):
"""Ensure that both ``bend_axis`` and ``bend_radius`` are provided if ``bend_correction`` is enabled."""
if val is True:
if values.get("bend_axis") is None or values.get("bend_radius") is None:
raise SetupError(
"'bend_correction' can only be enabled when both 'bend_axis' and 'bend_radius' are provided."
)
return val

@pd.validator("angle_theta", allow_reuse=True, always=True)
def glancing_incidence(cls, val):
"""Warn if close to glancing incidence."""
Expand Down
Loading

0 comments on commit 4e6dbc7

Please sign in to comment.