Skip to content

Commit 54840fd

Browse files
committed
feat(FXC-4519): Added check to warn of duplicated voltage values
1 parent e3c3930 commit 54840fd

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
### Added
11+
- Added validation to `DCVoltageSource` that warns when duplicate voltage values are detected in the `voltage` array, including treating `0` and `-0` as the same value.
1112

1213
### Changed
1314

tests/test_components/test_heat_charge.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,25 @@ def test_heat_charge_bcs_validation(boundary_conditions):
903903
td.VoltageBC(source=td.SSACVoltageSource(voltage=np.array([td.inf, 0, 1]), amplitude=1e-2))
904904

905905

906+
def test_repeated_voltage_warning():
907+
"""Test that a warning is raised when repeated voltage values are present."""
908+
# No warning for unique values
909+
with AssertLogLevel(None):
910+
td.DCVoltageSource(voltage=[0, 1, 2, 3])
911+
912+
# Warning for repeated values
913+
with AssertLogLevel("WARNING"):
914+
td.DCVoltageSource(voltage=[1, 2, 2, 3])
915+
916+
# Warning for 0 and -0 (treated as duplicates)
917+
with AssertLogLevel("WARNING"):
918+
td.DCVoltageSource(voltage=[0.0, -0.0, 1, 2])
919+
920+
# Warning for multiple repeated values
921+
with AssertLogLevel("WARNING"):
922+
td.DCVoltageSource(voltage=[1, 1, 2, 2, 3])
923+
924+
906925
def test_freqs_validation():
907926
"""Test validation that freqs requires SSACVoltageSource."""
908927
solid_box_1 = td.Box(center=(0, 0, 0), size=(2, 2, 2))

tidy3d/components/spice/sources/dc.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323

2424
from typing import Literal, Optional
2525

26+
import numpy as np
2627
import pydantic.v1 as pd
2728

2829
from tidy3d.components.base import Tidy3dBaseModel
2930
from tidy3d.components.types import ArrayFloat1D
3031
from tidy3d.constants import AMP, VOLT
3132
from tidy3d.constants import inf as td_inf
33+
from tidy3d.log import log
3234

3335

3436
class DCVoltageSource(Tidy3dBaseModel):
@@ -73,6 +75,20 @@ def check_voltage(cls, val):
7375
raise ValueError(f"Voltages must be finite. Currently voltage={val}.")
7476
return val
7577

78+
@pd.validator("voltage")
79+
def check_repeated_voltage(cls, val):
80+
"""Warn if repeated voltage values are present, treating 0 and -0 as the same value."""
81+
# Normalize all zero values (both 0.0 and -0.0) to 0.0 so they are treated as duplicates
82+
normalized = np.where(val == 0, 0.0, val)
83+
unique_values = np.unique(normalized)
84+
if len(unique_values) < len(val):
85+
log.warning(
86+
"Duplicate voltage values detected in 'voltage' array. "
87+
f"Found {len(val)} values but only {len(unique_values)} are unique. "
88+
"Note: 0 and -0 are considered the same value."
89+
)
90+
return val
91+
7692

7793
class GroundVoltage(Tidy3dBaseModel):
7894
"""

0 commit comments

Comments
 (0)