Skip to content

Commit aeca342

Browse files
Add priority to structure
1 parent 2b91331 commit aeca342

File tree

3 files changed

+54
-35
lines changed

3 files changed

+54
-35
lines changed

Diff for: tidy3d/components/scene.py

+19-32
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from tidy3d.components.tcad.doping import ConstantDoping, GaussianDoping
2525
from tidy3d.components.tcad.viz import HEAT_SOURCE_CMAP
2626

27-
from ..constants import CONDUCTIVITY, LARGE_NUMBER, THERMAL_CONDUCTIVITY, inf
27+
from ..constants import CONDUCTIVITY, THERMAL_CONDUCTIVITY, inf
2828
from ..exceptions import SetupError, Tidy3dError
2929
from ..log import log
3030
from .base import Tidy3dBaseModel, cached_property
@@ -43,7 +43,6 @@
4343
AbstractCustomMedium,
4444
AbstractMedium,
4545
AbstractPerturbationMedium,
46-
LossyMetalMedium,
4746
Medium,
4847
Medium2D,
4948
)
@@ -56,7 +55,7 @@
5655
InterpMethod,
5756
LengthUnit,
5857
PermittivityComponent,
59-
OverlappingPriority,
58+
PriorityMode,
6059
Shapely,
6160
Size,
6261
)
@@ -108,19 +107,20 @@ class Scene(Tidy3dBaseModel):
108107
(),
109108
title="Structures",
110109
description="Tuple of structures present in scene. "
111-
"Note: In regions of spatial overlapping between structures, "
112-
"material properties are dictated by structure of higher priority "
113-
"defined by ``overlapping_priority``.",
110+
"Note: In regions of spatial overlap between structures, "
111+
"material properties are dictated by structure of higher priority. "
112+
"The priority for structure of `priority=None` is set automatically "
113+
"from `structure_priority_mode`. For structures of equal priority, "
114+
"the structure added later to the structure list takes precedence.",
114115
)
115116

116-
overlapping_priority: OverlappingPriority = pd.Field(
117-
"latter",
118-
title="Resolution Strategy In Structure Overlapping Region",
119-
description="In regions of spatial overlapping between structures, "
120-
"if ``overlapping_priority=latter``, structures defined later in the list haver higher "
121-
"priority. If ``overlapping_priority=metal``, metallic structures of higher conductivity "
122-
"have higher priority; when the structures are of the same priority from material perspecive, "
123-
"the one defined later in the list has higher priority.",
117+
structure_priority_mode: PriorityMode = pd.Field(
118+
"equal",
119+
title="Automatic Structure Priority Setup",
120+
description="This field only affects structures of `priority=None`. "
121+
"If `equal`, the priority of those structures is set to 0; if `conductor`, "
122+
"the priority of structures made of `LossyMetalMedium` is set to 90, "
123+
"`PECMedium` to 100, and others to 0.",
124124
)
125125

126126
plot_length_units: Optional[LengthUnit] = pd.Field(
@@ -260,31 +260,18 @@ def medium_map(self) -> Dict[StructureMediumType, pd.NonNegativeInt]:
260260

261261
@cached_property
262262
def sorted_structures(self) -> List[Structure]:
263-
"""Returns a list of sorted structures based on the priority strategy when structures
264-
overlap. In the sorted list, latter added structures take higher priority.
263+
"""Returns a list of sorted structures based on their priority.In the sorted list,
264+
latter added structures take higher priority.
265265
266266
Returns
267267
-------
268268
List[:class:`.Structure`]
269269
"""
270270

271-
if self.overlapping_priority == "latter":
272-
return self.structures
273-
274-
# The other strategy is "metal":
275-
# Prioritize metallic structures including LossyMetal and PEC. When two metallic structures overlap,
276-
# prioritize the structure of higher conductivity.
277271
def structure_comparator(struct1, struct2):
278-
mat_list = [struct1.medium, struct2.medium]
279-
# For implementation convenience, let's denote conductivity of regular medium as -1,
280-
# so that it's lower than that of any metal.
281-
sigmas = [-1, -1]
282-
for ind, mat in enumerate(mat_list):
283-
if mat.is_pec:
284-
sigmas[ind] = LARGE_NUMBER
285-
elif isinstance(mat, LossyMetalMedium):
286-
sigmas[ind] = mat.conductivity
287-
return sigmas[0] - sigmas[1]
272+
return struct1._priority(self.structure_priority_mode) - struct2._priority(
273+
self.structure_priority_mode
274+
)
288275

289276
return sorted(self.structures, key=cmp_to_key(structure_comparator))
290277

Diff for: tidy3d/components/structure.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
from .geometry.utils import GeometryType, validate_no_transformed_polyslabs
2525
from .grid.grid import Coords
2626
from .material.types import StructureMediumType
27-
from .medium import AbstractCustomMedium, CustomMedium, Medium, Medium2D
27+
from .medium import AbstractCustomMedium, CustomMedium, LossyMetalMedium, Medium, Medium2D
2828
from .monitor import FieldMonitor, PermittivityMonitor
29-
from .types import TYPE_TAG_STR, Ax, Axis
29+
from .types import TYPE_TAG_STR, Ax, Axis, PriorityMode
3030
from .validators import validate_name_str
3131
from .viz import add_ax_if_none, equal_aspect
3232

@@ -75,6 +75,16 @@ class AbstractStructure(Tidy3dBaseModel):
7575
"``Simulation`` by default to compute the shape derivatives.",
7676
)
7777

78+
priority: int = pydantic.Field(
79+
None,
80+
title="Priority",
81+
description="Priority of the structure in structure overlapping region. "
82+
"The material property in the overlapping region is dictated by the structure "
83+
"of higher priority. For structures of equal priority, "
84+
"the structure added later to the structure list takes precedence. When `priority` is None, "
85+
"the value is automatically assigned based on `structure_priority_mode` in the `Simulation`.",
86+
)
87+
7888
@pydantic.root_validator(skip_on_failure=True)
7989
def _handle_background_mediums(cls, values):
8090
"""Handle background medium combinations, including deprecation."""
@@ -110,6 +120,14 @@ def _transformed_slanted_polyslabs_not_allowed(cls, val):
110120
validate_no_transformed_polyslabs(val)
111121
return val
112122

123+
def _priority(self, priority_mode: PriorityMode) -> int:
124+
"""Priority of this structure. The priority value is set automatically based on `priority_modes,
125+
if its original value is `None`.
126+
"""
127+
if self.priority is not None:
128+
return self.priority
129+
return 0
130+
113131
@property
114132
def viz_spec(self):
115133
return None
@@ -189,6 +207,20 @@ class Structure(AbstractStructure):
189207
discriminator=TYPE_TAG_STR,
190208
)
191209

210+
def _priority(self, priority_mode: PriorityMode) -> int:
211+
"""Priority of this structure. The priority value is set automatically based on `priority_modes,
212+
if its original value is `None`.
213+
"""
214+
if self.priority is not None:
215+
return self.priority
216+
217+
if priority_mode == "conductor":
218+
if self.medium.is_pec:
219+
return 100
220+
if isinstance(self.medium, LossyMetalMedium):
221+
return 90
222+
return 0
223+
192224
@property
193225
def viz_spec(self):
194226
return self.medium.viz_spec

Diff for: tidy3d/components/types.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def __modify_schema__(cls, field_schema):
200200
ClipOperationType = Literal["union", "intersection", "difference", "symmetric_difference"]
201201
BoxSurface = Literal["x-", "x+", "y-", "y+", "z-", "z+"]
202202
LengthUnit = Literal["nm", "μm", "um", "mm", "cm", "m"]
203-
OverlappingPriority = Literal["metal", "latter"]
203+
PriorityMode = Literal["equal", "conductor"]
204204

205205
""" medium """
206206

0 commit comments

Comments
 (0)