From 48338e99ac746cfb5138f73441345ba4132a0d20 Mon Sep 17 00:00:00 2001 From: dmarek Date: Fri, 13 Sep 2024 09:07:53 -0400 Subject: [PATCH] generalized validator to both current integral types --- .../smatrix/terminal_component_modeler_def.py | 2 +- .../smatrix/test_terminal_component_modeler.py | 9 +++++++++ .../plugins/microwave/custom_path_integrals.py | 16 +++++++++++++++- .../smatrix/component_modelers/terminal.py | 8 +++++--- tidy3d/plugins/smatrix/ports/wave.py | 3 +-- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/tests/test_plugins/smatrix/terminal_component_modeler_def.py b/tests/test_plugins/smatrix/terminal_component_modeler_def.py index a622faf38f..5d281640dc 100644 --- a/tests/test_plugins/smatrix/terminal_component_modeler_def.py +++ b/tests/test_plugins/smatrix/terminal_component_modeler_def.py @@ -295,7 +295,7 @@ def make_port(center, direction, type, name) -> Union[CoaxialLumpedPort, WavePor radius=mean_radius, num_points=41, normal_axis=2, - clockwise=False, + clockwise=direction != "+", ), ) return port diff --git a/tests/test_plugins/smatrix/test_terminal_component_modeler.py b/tests/test_plugins/smatrix/test_terminal_component_modeler.py index 7289794938..4836a28327 100644 --- a/tests/test_plugins/smatrix/test_terminal_component_modeler.py +++ b/tests/test_plugins/smatrix/test_terminal_component_modeler.py @@ -553,3 +553,12 @@ def test_port_source_snapped_to_PML(tmp_path): with pytest.raises(SetupError): modeler._shift_value_signed(port) + + +def test_wave_port_validate_current_integral(tmp_path): + """Checks that the current integral direction validator runs correctly.""" + modeler = make_coaxial_component_modeler( + path_dir=str(tmp_path), port_types=(WavePort, WavePort) + ) + with pytest.raises(pydantic.ValidationError): + _ = modeler.updated_copy(direction="-", path="ports/0/") diff --git a/tidy3d/plugins/microwave/custom_path_integrals.py b/tidy3d/plugins/microwave/custom_path_integrals.py index 251e97a57a..5fcb981565 100644 --- a/tidy3d/plugins/microwave/custom_path_integrals.py +++ b/tidy3d/plugins/microwave/custom_path_integrals.py @@ -6,13 +6,14 @@ import numpy as np import pydantic.v1 as pd +import shapely import xarray as xr from ...components.base import cached_property from ...components.data.data_array import FreqDataArray, FreqModeDataArray, TimeDataArray from ...components.data.monitor_data import FieldData, FieldTimeData, ModeSolverData from ...components.geometry.base import Geometry -from ...components.types import ArrayFloat2D, Ax, Axis, Bound, Coordinate +from ...components.types import ArrayFloat2D, Ax, Axis, Bound, Coordinate, Direction from ...components.viz import add_ax_if_none from ...constants import MICROMETER, fp_eps from ...exceptions import DataError, SetupError @@ -396,3 +397,16 @@ def plot( arrowprops=ARROW_CURRENT, ) return ax + + @cached_property + def sign(self) -> Direction: + """Uses the ordering of the vertices to determine the direction of the current flow.""" + linestr = shapely.LineString(coordinates=self.vertices) + is_ccw = shapely.is_ccw(linestr) + # Invert statement when the vertices are given as (x, z) + if self.axis == 1: + is_ccw = not is_ccw + if is_ccw: + return "+" + else: + return "-" diff --git a/tidy3d/plugins/smatrix/component_modelers/terminal.py b/tidy3d/plugins/smatrix/component_modelers/terminal.py index 22a5d35463..ec806f299c 100644 --- a/tidy3d/plugins/smatrix/component_modelers/terminal.py +++ b/tidy3d/plugins/smatrix/component_modelers/terminal.py @@ -16,7 +16,7 @@ from ....components.types import Ax from ....components.viz import add_ax_if_none, equal_aspect from ....constants import C_0, OHM -from ....exceptions import ValidationError +from ....exceptions import Tidy3dError, ValidationError from ....web.api.container import BatchData from ..ports.base_lumped import AbstractLumpedPort from ..ports.base_terminal import AbstractTerminalPort, TerminalPortDataArray @@ -401,6 +401,8 @@ def _check_port_impedance_sign(self, Z_numpy: np.ndarray): port_Z = Z_numpy[:, port_idx, 0] signs = np.sign(np.real(port_Z)) if not np.all(signs == signs[0]): - raise ValueError( - f"Inconsistent sign of real part of Z detected for port {port_idx}." + raise Tidy3dError( + f"Inconsistent sign of real part of Z detected for port {port_idx}. " + "If you received this error, please create an issue in the Tidy3D " + "github repository." ) diff --git a/tidy3d/plugins/smatrix/ports/wave.py b/tidy3d/plugins/smatrix/ports/wave.py index 9a17c16431..f9c5e96925 100644 --- a/tidy3d/plugins/smatrix/ports/wave.py +++ b/tidy3d/plugins/smatrix/ports/wave.py @@ -17,7 +17,6 @@ from ....exceptions import ValidationError from ...microwave import ( CurrentIntegralTypes, - CustomCurrentIntegral2D, ImpedanceCalculator, VoltageIntegralTypes, ) @@ -196,7 +195,7 @@ def validate_current_integral_sign(cls, val, values): """ Validate that the sign of ``current_integral`` matches the port direction. """ - if val is None or isinstance(val, CustomCurrentIntegral2D): + if val is None: return val direction = values.get("direction")