Skip to content

Commit 483d999

Browse files
committed
fixing docstrings and cleaning up composite current integral
1 parent 3332097 commit 483d999

File tree

4 files changed

+31
-63
lines changed

4 files changed

+31
-63
lines changed

tests/test_components/test_microwave.py

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import pydantic.v1 as pd
1111
import pytest
1212
import xarray as xr
13-
from shapely.geometry import LineString, Polygon
14-
from shapely.plotting import plot_line, plot_polygon
1513

1614
import tidy3d as td
1715
import tidy3d.components.microwave.path_integrals.current_spec
@@ -30,15 +28,14 @@
3028
make_voltage_integral,
3129
)
3230
from tidy3d.components.mode.mode_solver import ModeSolver
33-
from tidy3d.components.types import Ax, Shapely
3431
from tidy3d.constants import EPSILON_0
3532
from tidy3d.exceptions import SetupError, ValidationError
3633

3734
from ..test_data.test_monitor_data import make_directivity_data
3835

3936
mm = 1e3
4037

41-
MAKE_PLOTS = True
38+
MAKE_PLOTS = False
4239
if MAKE_PLOTS:
4340
# Interactive plotting for debugging
4441
from matplotlib import use
@@ -237,24 +234,6 @@ def make_mw_sim(
237234
return sim
238235

239236

240-
def plot_auto_path_spec(
241-
path_spec: td.CompositeCurrentIntegralSpec,
242-
geoms: list[Shapely],
243-
ax: Ax = None,
244-
) -> Ax:
245-
"""Helper to plot composite path specifications along with the Shapely geometries used to generate them."""
246-
if ax is None:
247-
_, ax = plt.subplots(1, 1, tight_layout=True, figsize=(15, 15))
248-
249-
for geom in geoms:
250-
if isinstance(geom, Polygon):
251-
plot_polygon(geom, ax=ax)
252-
elif isinstance(geom, LineString):
253-
plot_line(geom, ax=ax)
254-
i_integral = make_current_integral(path_spec)
255-
i_integral.plot(x=i_integral.center[0], ax=ax)
256-
257-
258237
def test_inductance_formulas():
259238
"""Run the formulas for inductance and compare to precomputed results."""
260239
bar_size = (1000e4, 1e4, 1e4) # case from reference
@@ -399,9 +378,7 @@ def test_composite_current_integral_validation():
399378

400379
current_spec = td.CurrentIntegralAxisAlignedSpec(center=(1, 2, 3), size=(0, 1, 1), sign="-")
401380
voltage_spec = td.VoltageIntegralAxisAlignedSpec(center=(1, 2, 3), size=(0, 0, 1), sign="-")
402-
path_spec = td.CompositeCurrentIntegralSpec(
403-
center=(1, 2, 3), size=(0, 1, 1), path_specs=[current_spec], sum_spec="sum"
404-
)
381+
path_spec = td.CompositeCurrentIntegralSpec(path_specs=[current_spec], sum_spec="sum")
405382

406383
with pytest.raises(pd.ValidationError):
407384
path_spec.updated_copy(path_specs=[])
@@ -519,9 +496,7 @@ def test_path_integral_factory_current_validation():
519496
custom_2d_spec = td.CustomCurrentIntegral2DSpec(
520497
vertices=[(0, 1), (0, 4), (3, 4), (3, 1)], axis=1, position=2
521498
)
522-
composite_spec = td.CompositeCurrentIntegralSpec(
523-
center=(1, 2, 3), size=(0, 1, 1), path_specs=[axis_aligned_spec], sum_spec="sum"
524-
)
499+
composite_spec = td.CompositeCurrentIntegralSpec(path_specs=[axis_aligned_spec], sum_spec="sum")
525500

526501
# Test successful creation with axis-aligned spec
527502
current_integral = make_current_integral(axis_aligned_spec)
@@ -538,7 +513,6 @@ def test_path_integral_factory_current_validation():
538513
# Test successful creation with composite spec
539514
current_integral = make_current_integral(composite_spec)
540515
assert current_integral is not None
541-
assert current_integral.center == (1, 2, 3)
542516
assert len(current_integral.path_specs) == 1
543517

544518
# Test ValidationError with unsupported type
@@ -613,7 +587,7 @@ def mock_make_voltage_integral(path_spec):
613587

614588

615589
def test_path_integral_factory_composite_current():
616-
"""Test make_current_integral with CompositeCurrentIntegralSpec and integration."""
590+
"""Test make_current_integral with CompositeCurrentIntegralSpec."""
617591

618592
# Create base specs for the composite
619593
axis_aligned_spec1 = td.CurrentIntegralAxisAlignedSpec(
@@ -625,22 +599,17 @@ def test_path_integral_factory_composite_current():
625599

626600
# Test creation of CompositeCurrentIntegralSpec
627601
composite_spec = td.CompositeCurrentIntegralSpec(
628-
center=(1.5, 2, 3),
629-
size=(2, 1, 1),
630602
path_specs=[axis_aligned_spec1, axis_aligned_spec2],
631603
sum_spec="sum",
632604
)
633605

634606
# Test successful creation with composite spec
635607
current_integral = make_current_integral(composite_spec)
636608
assert current_integral is not None
637-
assert current_integral.center == (1.5, 2, 3)
638609
assert len(current_integral.path_specs) == 2
639610

640611
# Test with different sum_spec options
641612
composite_spec_split = td.CompositeCurrentIntegralSpec(
642-
center=(1.5, 2, 3),
643-
size=(2, 1, 1),
644613
path_specs=[axis_aligned_spec1, axis_aligned_spec2],
645614
sum_spec="split",
646615
)

tests/test_plugins/test_microwave.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,8 @@ def test_composite_current_integral_warnings():
543543
coords = {"f": f, "mode_index": mode_index}
544544
values = np.ones((3, 5))
545545

546-
path_spec = (
547-
tidy3d.components.microwave.path_integrals.current_spec.CurrentIntegralAxisAlignedSpec(
548-
center=(0, 0, 0), size=(2, 2, 0), sign="+"
549-
)
550-
)
551-
composite_integral = mw.CompositeCurrentIntegral(
552-
center=(0, 0, 0), size=(4, 4, 0), path_specs=[path_spec], sum_spec="split"
553-
)
546+
path_spec = td.CurrentIntegralAxisAlignedSpec(center=(0, 0, 0), size=(2, 2, 0), sign="+")
547+
composite_integral = mw.CompositeCurrentIntegral(path_specs=[path_spec], sum_spec="split")
554548

555549
phase_diff = FreqModeDataArray(np.angle(values), coords=coords)
556550
with AssertLogLevel(None):
@@ -808,7 +802,7 @@ def test_composite_current_integral_compute_current():
808802

809803
# Test with sum_spec="sum"
810804
composite_integral_sum = mw.CompositeCurrentIntegral(
811-
center=(0, 0, 0), size=(1, 1, 0), path_specs=[path_spec1, path_spec2], sum_spec="sum"
805+
path_specs=[path_spec1, path_spec2], sum_spec="sum"
812806
)
813807

814808
current_sum = composite_integral_sum.compute_current(SIM_Z_DATA["field"])
@@ -817,7 +811,7 @@ def test_composite_current_integral_compute_current():
817811

818812
# Test with sum_spec="split"
819813
composite_integral_split = mw.CompositeCurrentIntegral(
820-
center=(0, 0, 0), size=(1, 1, 0), path_specs=[path_spec1, path_spec2], sum_spec="split"
814+
path_specs=[path_spec1, path_spec2], sum_spec="split"
821815
)
822816

823817
current_split = composite_integral_split.compute_current(SIM_Z_DATA["field"])
@@ -833,9 +827,7 @@ def test_composite_current_integral_time_domain_error():
833827

834828
path_spec = mw.CurrentIntegralAxisAligned(center=(0, 0, 0), size=(0.5, 0.5, 0), sign="+")
835829

836-
composite_integral = mw.CompositeCurrentIntegral(
837-
center=(0, 0, 0), size=(1, 1, 0), path_specs=[path_spec], sum_spec="split"
838-
)
830+
composite_integral = mw.CompositeCurrentIntegral(path_specs=[path_spec], sum_spec="split")
839831

840832
# Should raise DataError for time domain data with split sum_spec
841833
with pytest.raises(
@@ -851,9 +843,7 @@ def test_composite_current_integral_phase_consistency_warnings():
851843
# Create a composite integral for testing
852844
path_spec = mw.CurrentIntegralAxisAligned(center=(0, 0, 0), size=(0.5, 0.5, 0), sign="+")
853845

854-
composite_integral = mw.CompositeCurrentIntegral(
855-
center=(0, 0, 0), size=(1, 1, 0), path_specs=[path_spec], sum_spec="split"
856-
)
846+
composite_integral = mw.CompositeCurrentIntegral(path_specs=[path_spec], sum_spec="split")
857847

858848
# Test _check_phase_sign_consistency with consistent data
859849
f = [2e9, 3e9, 4e9]
@@ -970,7 +960,7 @@ def test_composite_current_integral_freq_mode_data():
970960

971961
# Test with sum_spec="sum" - should work with FreqModeDataArray
972962
composite_integral_sum = mw.CompositeCurrentIntegral(
973-
center=(0, 0, 0), size=(1, 1, 0), path_specs=[path_spec1, path_spec2], sum_spec="sum"
963+
path_specs=[path_spec1, path_spec2], sum_spec="sum"
974964
)
975965

976966
# Use mode data which provides FreqModeDataArray
@@ -984,7 +974,7 @@ def test_composite_current_integral_freq_mode_data():
984974

985975
# Test with sum_spec="split" - should also work with FreqModeDataArray
986976
composite_integral_split = mw.CompositeCurrentIntegral(
987-
center=(0, 0, 0), size=(1, 1, 0), path_specs=[path_spec1, path_spec2], sum_spec="split"
977+
path_specs=[path_spec1, path_spec2], sum_spec="split"
988978
)
989979

990980
current_split = composite_integral_split.compute_current(SIM_Z_DATA["mode"])

tidy3d/components/microwave/path_integrals/current_spec.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88
import pydantic.v1 as pd
99

10-
from tidy3d.components.base import cached_property
10+
from tidy3d.components.base import Tidy3dBaseModel, cached_property
1111
from tidy3d.components.geometry.base import Box, Geometry
1212
from tidy3d.components.microwave.path_integrals.base_spec import (
1313
AbstractAxesRH,
@@ -269,10 +269,10 @@ def plot(
269269
return ax
270270

271271

272-
class CompositeCurrentIntegralSpec(Box):
272+
class CompositeCurrentIntegralSpec(Tidy3dBaseModel):
273273
"""Specification for a composite current integral.
274274
275-
This class is used to set up a CompositeCurrentIntegral, which combines
275+
This class is used to set up a ``CompositeCurrentIntegral``, which combines
276276
multiple current integrals. It does not perform any integration itself.
277277
"""
278278

tidy3d/plugins/microwave/custom_path_integrals.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import xarray as xr
99

10+
from tidy3d.components.base import cached_property
1011
from tidy3d.components.data.data_array import FreqDataArray, FreqModeDataArray
1112
from tidy3d.components.data.monitor_data import FieldTimeData
1213
from tidy3d.components.microwave.path_integrals.base_spec import CustomPathIntegral2DSpec
@@ -17,9 +18,9 @@
1718
from tidy3d.components.microwave.path_integrals.voltage_spec import CustomVoltageIntegral2DSpec
1819
from tidy3d.exceptions import DataError
1920
from tidy3d.log import log
20-
21-
from .path_integrals import (
21+
from tidy3d.plugins.microwave.path_integrals import (
2222
AxisAlignedPathIntegral,
23+
CurrentIntegralAxisAligned,
2324
CurrentIntegralResultTypes,
2425
IntegralResultTypes,
2526
MonitorDataTypes,
@@ -167,6 +168,18 @@ def compute_current(self, em_field: MonitorDataTypes) -> CurrentIntegralResultTy
167168
class CompositeCurrentIntegral(CompositeCurrentIntegralSpec):
168169
"""Current integral comprising one or more disjoint paths"""
169170

171+
@cached_property
172+
def current_integrals(
173+
self,
174+
) -> tuple[Union[CurrentIntegralAxisAligned, CustomCurrentIntegral2D], ...]:
175+
""" "Collection of closed current path integrals."""
176+
from tidy3d.components.microwave.path_integrals.path_integral_factory import (
177+
make_current_integral,
178+
)
179+
180+
current_integrals = [make_current_integral(path_spec) for path_spec in self.path_specs]
181+
return current_integrals
182+
170183
def compute_current(self, em_field: MonitorDataTypes) -> IntegralResultTypes:
171184
"""Compute current flowing in loop defined by the outer edge of a rectangle."""
172185
if isinstance(em_field, FieldTimeData) and self.sum_spec == "split":
@@ -175,11 +188,7 @@ def compute_current(self, em_field: MonitorDataTypes) -> IntegralResultTypes:
175188
"Either switch the sum_spec to 'sum' or supply frequency domain data."
176189
)
177190

178-
from tidy3d.components.microwave.path_integrals.path_integral_factory import (
179-
make_current_integral,
180-
)
181-
182-
current_integrals = [make_current_integral(path_spec) for path_spec in self.path_specs]
191+
current_integrals = self.current_integrals
183192

184193
# Calculate currents from each path integral and store in dataarray with path index dimension
185194
path_currents = []

0 commit comments

Comments
 (0)