Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatic mode rotation for bent waveguide #2028

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Differentiable `smooth_min`, `smooth_max`, and `least_squares` functions in `tidy3d.plugins.autograd`.
- Differential operators `grad` and `value_and_grad` in `tidy3d.plugins.autograd` that behave similarly to the autograd operators but support auxiliary data via `aux_data=True` as well as differentiation w.r.t. `DataArray`.
- `@scalar_objective` decorator in `tidy3d.plugins.autograd` that wraps objective functions to ensure they return a scalar value and performs additional checks to ensure compatibility of objective functions with autograd. Used by default in `tidy3d.plugins.autograd.value_and_grad` as well as `tidy3d.plugins.autograd.grad`.

- `bend_angle_rotation` in `mode_spec` to improve accuracy in some cases when both `bend_radius` and `angle_theta` are defined."

### Changed
- `CustomMedium` design regions require far less data when performing inverse design by reducing adjoint field monitor size for dims with one pixel.
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 @@ -813,6 +813,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
55 changes: 54 additions & 1 deletion tidy3d/components/geometry/polyslab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from copy import copy
from math import isclose
from typing import List, Tuple
from typing import List, Tuple, Union

import autograd.numpy as np
import pydantic.v1 as pydantic
Expand All @@ -19,6 +19,7 @@
from ..autograd import AutogradFieldMap, TracedVertices, get_static
from ..autograd.derivative_utils import DerivativeInfo
from ..base import cached_property, skip_if_fields_missing
from ..transformation import RotationAroundAxis
from ..types import (
ArrayFloat2D,
ArrayLike,
Expand Down Expand Up @@ -1518,6 +1519,58 @@ def normalize_vect(arr: np.ndarray) -> np.ndarray:
norm = np.where(norm == 0, 1, norm)
return arr / norm

def translated(self, x: float, y: float, z: float) -> PolySlab:
"""Return a translated copy of this geometry.

Parameters
----------
x : float
Translation along x.
y : float
Translation along y.
z : float
Translation along z.

Returns
-------
:class:`PolySlab`
Translated copy of this ``PolySlab``.
"""

t_normal, t_plane = self.pop_axis((x, y, z), axis=self.axis)
translated_vertices = np.array(self.vertices) + np.array(t_plane)[None, :]
translated_slab_bounds = (self.slab_bounds[0] + t_normal, self.slab_bounds[1] + t_normal)
return self.updated_copy(vertices=translated_vertices, slab_bounds=translated_slab_bounds)

def rotated(self, angle: float, axis: Union[Axis, Coordinate]) -> PolySlab:
"""Return a rotated copy of this geometry.

Parameters
----------
angle : float
Rotation angle (in radians).
axis : Union[int, Tuple[float, float, float]]
Axis of rotation: 0, 1, or 2 for x, y, and z, respectively, or a 3D vector.

Returns
-------
:class:`PolySlab`
Rotated copy of this ``PolySlab``.
"""
_, plane_axs = self.pop_axis([0, 1, 2], self.axis)
if (isinstance(axis, int) and axis == self.axis) or (
isinstance(axis, tuple) and all(axis[ax] == 0 for ax in plane_axs)
):
verts_3d = np.zeros((3, self.vertices.shape[0]))
verts_3d[plane_axs[0], :] = self.vertices[:, 0]
verts_3d[plane_axs[1], :] = self.vertices[:, 1]
rotation = RotationAroundAxis(angle=angle, axis=axis)
rotated_vertices = rotation.rotate_vector(verts_3d)
rotated_vertices = np.take(rotated_vertices, plane_axs, axis=0).T
return self.updated_copy(vertices=rotated_vertices)

return super().rotated(angle=angle, axis=axis)


class ComplexPolySlabBase(PolySlab):
"""Interface for dividing a complex polyslab where self-intersecting polygon can
Expand Down
12 changes: 12 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 correct geometries in the simulation with rotated structures.",
)
QimingFlex marked this conversation as resolved.
Show resolved Hide resolved

track_freq: Union[TrackFreq, None] = pd.Field(
"central",
title="Mode Tracking Frequency",
Expand Down
Loading