Skip to content

Commit

Permalink
generalized validator to both current integral types
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarek-flex committed Sep 13, 2024
1 parent 83b7b15 commit 48338e9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/test_plugins/smatrix/test_terminal_component_modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
16 changes: 15 additions & 1 deletion tidy3d/plugins/microwave/custom_path_integrals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "-"
8 changes: 5 additions & 3 deletions tidy3d/plugins/smatrix/component_modelers/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."
)
3 changes: 1 addition & 2 deletions tidy3d/plugins/smatrix/ports/wave.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from ....exceptions import ValidationError
from ...microwave import (
CurrentIntegralTypes,
CustomCurrentIntegral2D,
ImpedanceCalculator,
VoltageIntegralTypes,
)
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 48338e9

Please sign in to comment.