Skip to content

Commit

Permalink
Snapping points with optional coordinate
Browse files Browse the repository at this point in the history
  • Loading branch information
weiliangjin2021 committed Jan 2, 2025
1 parent 3bd312e commit fd671d5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
16 changes: 8 additions & 8 deletions tidy3d/components/grid/grid_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..geometry.base import Box
from ..source.utils import SourceType
from ..structure import Structure, StructureType
from ..types import TYPE_TAG_STR, Axis, Coordinate, Symmetry, annotate_type
from ..types import TYPE_TAG_STR, Axis, CoordinateOptional, Symmetry, annotate_type
from .grid import Coords, Coords1D, Grid
from .mesher import GradedMesher, MesherType

Expand All @@ -31,7 +31,7 @@ def make_coords(
periodic: bool,
wavelength: pd.PositiveFloat,
num_pml_layers: Tuple[pd.NonNegativeInt, pd.NonNegativeInt],
snapping_points: Tuple[Coordinate, ...],
snapping_points: Tuple[CoordinateOptional, ...],
) -> Coords1D:
"""Generate 1D coords to be used as grid boundaries, based on simulation parameters.
Symmetry, and PML layers will be treated here.
Expand All @@ -49,7 +49,7 @@ def make_coords(
Free-space wavelength.
num_pml_layers : Tuple[int, int]
number of layers in the absorber + and - direction along one dimension.
snapping_points : Tuple[Coordinate, ...]
snapping_points : Tuple[CoordinateOptional, ...]
A set of points that enforce grid boundaries to pass through them.
Returns
Expand Down Expand Up @@ -445,7 +445,7 @@ def _make_coords_initial(
wavelength: float,
symmetry: Symmetry,
is_periodic: bool,
snapping_points: Tuple[Coordinate, ...],
snapping_points: Tuple[CoordinateOptional, ...],
) -> Coords1D:
"""Customized 1D coords to be used as grid boundaries.
Expand All @@ -462,7 +462,7 @@ def _make_coords_initial(
normal to each of the three axes.
is_periodic : bool
Apply periodic boundary condition or not.
snapping_points : Tuple[Coordinate, ...]
snapping_points : Tuple[CoordinateOptional, ...]
A set of points that enforce grid boundaries to pass through them.
Returns
Expand Down Expand Up @@ -601,7 +601,7 @@ class GridSpec(Tidy3dBaseModel):
"uses :class:`.AutoGrid`.",
)

snapping_points: Tuple[Coordinate, ...] = pd.Field(
snapping_points: Tuple[CoordinateOptional, ...] = pd.Field(
(),
title="Grid specification snapping_points",
description="A set of points that enforce grid boundaries to pass through them. "
Expand Down Expand Up @@ -760,7 +760,7 @@ def auto(
min_steps_per_wvl: pd.PositiveFloat = 10.0,
max_scale: pd.PositiveFloat = 1.4,
override_structures: List[StructureType] = (),
snapping_points: Tuple[Coordinate, ...] = (),
snapping_points: Tuple[CoordinateOptional, ...] = (),
dl_min: pd.NonNegativeFloat = 0.0,
mesher: MesherType = GradedMesher(),
) -> GridSpec:
Expand All @@ -780,7 +780,7 @@ def auto(
A list of structures that is added on top of the simulation structures in
the process of generating the grid. This can be used to refine the grid or make it
coarser depending than the expected need for higher/lower resolution regions.
snapping_points : Tuple[Coordinate, ...]
snapping_points : Tuple[CoordinateOptional, ...]
A set of points that enforce grid boundaries to pass through them.
dl_min: pd.NonNegativeFloat
Lower bound of grid size.
Expand Down
10 changes: 6 additions & 4 deletions tidy3d/components/grid/mesher.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from ..base import Tidy3dBaseModel
from ..medium import AnisotropicMedium, LossyMetalMedium, Medium2D, PECMedium
from ..structure import MeshOverrideStructure, Structure, StructureType
from ..types import ArrayFloat1D, Axis, Bound, Coordinate
from ..types import ArrayFloat1D, Axis, Bound, CoordinateOptional

_ROOTS_TOL = 1e-10

Expand Down Expand Up @@ -50,7 +50,7 @@ def insert_snapping_points(
axis: Axis,
interval_coords: ArrayFloat1D,
max_dl_list: ArrayFloat1D,
snapping_points: List[Coordinate],
snapping_points: List[CoordinateOptional],
) -> Tuple[ArrayFloat1D, ArrayFloat1D]:
"""Insert snapping_points to the intervals."""

Expand Down Expand Up @@ -80,7 +80,7 @@ def insert_snapping_points(
axis: Axis,
interval_coords: ArrayFloat1D,
max_dl_list: ArrayFloat1D,
snapping_points: List[Coordinate],
snapping_points: List[CoordinateOptional],
) -> Tuple[ArrayFloat1D, ArrayFloat1D]:
"""Insert snapping_points to the intervals.
Expand All @@ -94,7 +94,7 @@ def insert_snapping_points(
Coordinate of interval boundaries.
max_dl_list : ArrayFloat1D
Maximal allowed step size of each interval generated from `parse_structures`.
snapping_points : List[Coordinate]
snapping_points : List[CoordinateOptional]
A set of points that enforce grid boundaries to pass through them.
Returns
Expand Down Expand Up @@ -123,6 +123,8 @@ def insert_snapping_points(

for point in snapping_points:
new_coord = point[axis]
if new_coord is None:
continue
# Skip if the point is outside the domain
if new_coord >= interval_coords[-1] or new_coord <= interval_coords[0]:
continue
Expand Down
20 changes: 20 additions & 0 deletions tidy3d/components/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,26 @@ def plot_grid(
# Plot snapping points
for point in self.grid_spec.snapping_points:
_, (x_point, y_point) = Geometry.pop_axis(point, axis=axis)
if x_point is None and y_point is None:
continue
if x_point is None:
ax.axhline(
y=self._evaluate_inf(y_point),
xmin=0.1,
xmax=0.9,
linewidth=2 * kwargs["linewidth"],
color=plot_params.edgecolor,
)
continue
if y_point is None:
ax.axvline(
x=self._evaluate_inf(x_point),
ymin=0.1,
ymax=0.9,
linewidth=2 * kwargs["linewidth"],
color=plot_params.edgecolor,
)
continue
x_point, y_point = (self._evaluate_inf(v) for v in (x_point, y_point))
ax.scatter(x_point, y_point, color=plot_params.edgecolor)

Expand Down
3 changes: 2 additions & 1 deletion tidy3d/components/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Defines 'types' that various fields can be"""

from typing import Tuple, Union
from typing import Optional, Tuple, Union

# Literal only available in python 3.8 + so try import otherwise use extensions
try:
Expand Down Expand Up @@ -185,6 +185,7 @@ def __modify_schema__(cls, field_schema):
Size1D = pydantic.NonNegativeFloat
Size = Tuple[Size1D, Size1D, Size1D]
Coordinate = Tuple[float, float, float]
CoordinateOptional = Tuple[Optional[float], Optional[float], Optional[float]]
Coordinate2D = Tuple[float, float]
Bound = Tuple[Coordinate, Coordinate]
GridSize = Union[pydantic.PositiveFloat, Tuple[pydantic.PositiveFloat, ...]]
Expand Down

0 comments on commit fd671d5

Please sign in to comment.