Skip to content

Commit

Permalink
fix bug in 2D material subdivision where invalid shapely geometry was…
Browse files Browse the repository at this point in the history
… produced
  • Loading branch information
dmarek-flex committed Oct 4, 2024
1 parent 9f02a97 commit e0a168c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix numerical precision issue in `FieldProjectionCartesianMonitor`.
- Bug where lumped elements in the `Simulation` were being overwritten by the `TerminalComponentModeler`.
- Bug in `Simulation.subsection` where lumped elements were not being correctly removed.
- Bug when adding 2D structures to the `Simulation` that are composed of multiple overlapping polygons.

## [2.7.2] - 2024-08-07

Expand Down
18 changes: 18 additions & 0 deletions tests/test_components/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from tidy3d.components.geometry.base import Planar
from tidy3d.components.geometry.mesh import AREA_SIZE_THRESHOLD
from tidy3d.components.geometry.utils import flatten_groups, traverse_geometries
from tidy3d.components.geometry.utils_2d import subdivide
from tidy3d.constants import LARGE_NUMBER
from tidy3d.exceptions import SetupError, Tidy3dKeyError, ValidationError

Expand Down Expand Up @@ -956,3 +957,20 @@ def test_update_from_bounds():
for geom2d in geometries:
with pytest.raises(NotImplementedError):
geom_update = geom2d._update_from_bounds(bounds=new_bounds, axis=axis)


def test_subdivide():
# Test the functionality that subdivides structures with Medium2D into partitions,
# where each partition is paired with homogeneous medium above and below
box = td.Box(size=(1, 1, 0))
overlap_box = td.Box(size=(2, 0.5, 0), center=(0.5, 0, 0))
# These overlapping boxes have their left edge coincident, and the second box has a smaller left edge.
# This results in an invalid geometry for MultiPolygon which must be fixed by applying a union
# operation in ``subdivide``. This should fix other types of invalid geometries as well.
overlapping_boxes = td.GeometryGroup(geometries=(box, overlap_box))

background_structure = td.Structure(medium=td.Medium(), geometry=td.Box(size=(10, 10, 10)))
subdivisions = subdivide(
geom=overlapping_boxes, axis=2, axis_dl=1e-5, structures=[background_structure]
)
assert len(subdivisions) == 1
8 changes: 3 additions & 5 deletions tidy3d/components/geometry/utils_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,13 @@ def to_multipolygon(shapely_geometry) -> shapely.MultiPolygon:
plane = {coord: center}

# Convert input geometry into MultiPolygon shapely geometry and track the original structure that references the media properties
geom_shapely = Geometry.evaluate_inf_shape(
shapely.MultiPolygon(geom.intersections_plane(**plane))
)
geom_shapely = Geometry.evaluate_inf_shape(shapely.union_all(geom.intersections_plane(**plane)))

plane[coord] = center + check_delta[1]
above_shapely = [
(
Geometry.evaluate_inf_shape(
shapely.MultiPolygon(structure.geometry.intersections_plane(**plane))
shapely.union_all(structure.geometry.intersections_plane(**plane))
),
structure,
)
Expand All @@ -141,7 +139,7 @@ def to_multipolygon(shapely_geometry) -> shapely.MultiPolygon:
below_shapely = [
[
Geometry.evaluate_inf_shape(
shapely.MultiPolygon(structure.geometry.intersections_plane(**plane))
shapely.union_all(structure.geometry.intersections_plane(**plane))
),
structure,
]
Expand Down

0 comments on commit e0a168c

Please sign in to comment.