|
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import pydantic.v1 as pd |
| 9 | +from shapely import Polygon |
9 | 10 |
|
10 | 11 | from tidy3d.components.base import cached_property |
11 | 12 | from tidy3d.components.geometry.base import Box |
|
17 | 18 | ) |
18 | 19 | from tidy3d.components.mode_spec import AbstractModeSpec |
19 | 20 | from tidy3d.components.types import Coordinate2D, annotate_type |
| 21 | +from tidy3d.components.types.base import ArrayFloat2D |
20 | 22 | from tidy3d.constants import fp_eps |
21 | 23 | from tidy3d.exceptions import SetupError |
22 | 24 |
|
| 25 | +ConductorIdentifierType = Union[Coordinate2D, str, ArrayFloat2D] |
| 26 | + |
23 | 27 |
|
24 | 28 | class TerminalSpec(MicrowaveBaseModel): |
25 | | - plus_terminals: tuple[Coordinate2D, ...] = pd.Field( |
| 29 | + """Specifies the desired voltage pattern across conductors for a transmission line mode. |
| 30 | +
|
| 31 | + Identifies which conductors should be at positive versus negative voltage for mode selection |
| 32 | + and ordering in coupled transmission line systems. Conductors can be identified using point |
| 33 | + coordinates, structure names, or geometric regions for maximum flexibility. |
| 34 | +
|
| 35 | + Notes |
| 36 | + ----- |
| 37 | + For most use cases, identifying conductors using a single (x, y) coordinate point or a |
| 38 | + structure name string is recommended. If desired, users may specify geometric regions using |
| 39 | + arrays of vertices, which are then converted to points, lines, or polygons to identify |
| 40 | + conductors via intersections. |
| 41 | +
|
| 42 | + Example |
| 43 | + ------- |
| 44 | + >>> # Recommended: Identify conductors using point coordinates |
| 45 | + >>> # Differential mode: conductor 1 positive, conductor 2 negative |
| 46 | + >>> diff_mode = TerminalSpec( |
| 47 | + ... plus_terminals=((1.0, 0.5),), # Point inside positive conductor |
| 48 | + ... minus_terminals=((-1.0, 0.5),) # Point inside negative conductor |
| 49 | + ... ) |
| 50 | + >>> |
| 51 | + >>> # Common mode: both conductors positive relative to ground |
| 52 | + >>> common_mode = TerminalSpec( |
| 53 | + ... plus_terminals=((1.0, 0.5), (-1.0, 0.5)), # Both conductors positive |
| 54 | + ... minus_terminals=() # Ground plane is reference |
| 55 | + ... ) |
| 56 | + >>> |
| 57 | + >>> # Alternative: Identify conductors by structure name |
| 58 | + >>> named_mode = TerminalSpec( |
| 59 | + ... plus_terminals=("trace1",), |
| 60 | + ... minus_terminals=("trace2",) |
| 61 | + ... ) |
| 62 | + >>> |
| 63 | + >>> # Advanced: Identify conductor using polygon region |
| 64 | + >>> import numpy as np |
| 65 | + >>> polygon_mode = TerminalSpec( |
| 66 | + ... plus_terminals=(np.array([[0, 0], [1, 0], [1, 1], [0, 1]]),), # Square region |
| 67 | + ... minus_terminals=() |
| 68 | + ... ) |
| 69 | + """ |
| 70 | + |
| 71 | + plus_terminals: tuple[ConductorIdentifierType, ...] = pd.Field( |
26 | 72 | ..., |
27 | | - title="Impedance Specifications", |
28 | | - description="Field controls how the impedan", |
| 73 | + title="Positive Terminals", |
| 74 | + description="Identifies conductors that should be at positive voltage for this mode. " |
| 75 | + "Each conductor can be specified as: (1) a (u, v) coordinate tuple locating a point inside " |
| 76 | + "the conductor (recommended), (2) a string matching a structure name, or (3) an Nx2 array " |
| 77 | + "of vertices defining a geometric region (point for N=1, line for N=2, polygon for N>2). " |
| 78 | + "The coordinate or region should lie within the desired conductor in the mode plane.", |
29 | 79 | ) |
30 | | - minus_terminals: tuple[Coordinate2D, ...] = pd.Field( |
| 80 | + minus_terminals: tuple[ConductorIdentifierType, ...] = pd.Field( |
31 | 81 | ..., |
32 | | - title="Impedance Specifications", |
33 | | - description="Field controls how the impedan", |
| 82 | + title="Negative Terminals", |
| 83 | + description="Identifies conductors that should be at negative voltage for this mode. " |
| 84 | + "Each conductor can be specified as: (1) a (u, v) coordinate tuple locating a point inside " |
| 85 | + "the conductor (recommended), (2) a string matching a structure name, or (3) an Nx2 array " |
| 86 | + "of vertices defining a geometric region (point for N=1, line for N=2, polygon for N>2). " |
| 87 | + "The coordinate or region should lie within the desired conductor in the mode plane.", |
34 | 88 | ) |
35 | 89 |
|
| 90 | + @pd.validator("plus_terminals", "minus_terminals", each_item=True) |
| 91 | + def _validate_conductor_identifiers(cls, val): |
| 92 | + """Validate conductor identification inputs.""" |
| 93 | + |
| 94 | + # If it's a string or tuple, pass through |
| 95 | + if isinstance(val, (str, tuple)): |
| 96 | + return val |
| 97 | + |
| 98 | + # If it's a numpy array, validate shape and geometry |
| 99 | + if isinstance(val, np.ndarray): |
| 100 | + # Check that 2D arrays have exactly 2 columns (u, v coordinates) |
| 101 | + if val.shape[1] != 2: |
| 102 | + raise ValueError( |
| 103 | + f"Arrays must have exactly 2 columns for (u, v) coordinates, got shape {val.shape}" |
| 104 | + ) |
| 105 | + # For 2D arrays, check number of points and polygon validity |
| 106 | + num_points = val.shape[0] |
| 107 | + if num_points <= 2: |
| 108 | + return val |
| 109 | + elif num_points > 2: |
| 110 | + polygon = Polygon(val) |
| 111 | + if not polygon.is_valid: |
| 112 | + raise ValueError( |
| 113 | + f"A supplied set of vertices {val} did not result in a valid " |
| 114 | + "polygon, make sure there are no self-intersections." |
| 115 | + ) |
| 116 | + return val |
| 117 | + |
36 | 118 |
|
37 | 119 | class MicrowaveModeSpec(AbstractModeSpec, MicrowaveBaseModel): |
38 | 120 | """ |
@@ -80,11 +162,11 @@ class MicrowaveModeSpec(AbstractModeSpec, MicrowaveBaseModel): |
80 | 162 | terminal_specs: Optional[tuple[TerminalSpec, ...]] = pd.Field( |
81 | 163 | None, |
82 | 164 | title="Terminal Specifications", |
83 | | - description="Field controls how the impedance is calculated for each mode calculated by the mode solver. " |
84 | | - "Can be a single impedance specification (which will be applied to all modes) or a tuple of specifications " |
85 | | - "(one per mode). The number of impedance specifications should match the number of modes field. " |
86 | | - "When an impedance specification of ``None`` is used, the impedance calculation will be " |
87 | | - "ignored for the associated mode.", |
| 165 | + description="Optional tuple of terminal specifications for mode selection and ordering in" |
| 166 | + "transmission line systems. Each 'TerminalSpec' defines the desired voltage pattern (which conductors " |
| 167 | + "should be positive vs. negative) for a mode. When provided, the mode solver automatically reorders " |
| 168 | + "computed modes to match the terminal specification order and applies phase corrections to ensure " |
| 169 | + "correct voltage polarity.", |
88 | 170 | ) |
89 | 171 |
|
90 | 172 | @cached_property |
|
0 commit comments