Skip to content

Commit c3d1b7b

Browse files
committed
Limit number of sources to 1000
1 parent 7f6b1b7 commit c3d1b7b

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG.md

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

1111
### Changed
12+
- Limited number of distinct sources to 1000. In cases where a complicated spatial dependence of the source is desired, a ``CustomFieldSource`` or a ``CustomCurrentSource`` can be used instead of multiple distinct sources.
1213

1314
### Fixed
1415
- Properly handle `.freqs` in `output_monitors` of adjoint plugin.

tests/test_components/test_simulation.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import tidy3d as td
88
from tidy3d.exceptions import SetupError, ValidationError, Tidy3dKeyError
99
from tidy3d.components import simulation
10-
from tidy3d.components.simulation import MAX_NUM_MEDIUMS
10+
from tidy3d.components.simulation import MAX_NUM_MEDIUMS, MAX_NUM_SOURCES
1111
from ..utils import assert_log_level, SIM_FULL, log_capture, run_emulated
1212
from tidy3d.constants import LARGE_NUMBER
1313

@@ -492,6 +492,7 @@ def test_validate_components_none():
492492

493493
assert SIM._structures_not_at_edges(val=None, values=SIM.dict()) is None
494494
assert SIM._validate_num_mediums(val=None) is None
495+
assert SIM._validate_num_sources(val=None) is None
495496
assert SIM._warn_monitor_mediums_frequency_range(val=None, values=SIM.dict()) is None
496497
assert SIM._warn_monitor_simulation_frequency_range(val=None, values=SIM.dict()) is None
497498
assert SIM._warn_grid_size_too_small(val=None, values=SIM.dict()) is None
@@ -1179,6 +1180,22 @@ def test_num_mediums():
11791180
)
11801181

11811182

1183+
def test_num_sources():
1184+
"""Make sure we error if too many sources supplied."""
1185+
1186+
src = td.PlaneWave(
1187+
source_time=td.GaussianPulse(freq0=2.5e14, fwidth=1e13),
1188+
center=(0, 0, 0),
1189+
size=(td.inf, td.inf, 0),
1190+
direction="+",
1191+
)
1192+
1193+
_ = td.Simulation(size=(5, 5, 5), run_time=1e-12, sources=[src] * MAX_NUM_SOURCES)
1194+
1195+
with pytest.raises(pydantic.ValidationError):
1196+
_ = td.Simulation(size=(5, 5, 5), run_time=1e-12, sources=[src] * (MAX_NUM_SOURCES + 1))
1197+
1198+
11821199
def _test_names_default():
11831200
"""makes sure default names are set"""
11841201

tidy3d/components/simulation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
# maximum number of mediums supported
5656
MAX_NUM_MEDIUMS = 65530
5757

58+
# maximum number of sources
59+
MAX_NUM_SOURCES = 1000
60+
5861
# maximum geometry count in a single structure
5962
MAX_GEOMETRY_COUNT = 100
6063

@@ -445,6 +448,22 @@ def _validate_num_mediums(cls, val):
445448

446449
return val
447450

451+
@pydantic.validator("sources", always=True)
452+
def _validate_num_sources(cls, val):
453+
"""Error if too many sources present."""
454+
455+
if val is None:
456+
return val
457+
458+
if len(val) > MAX_NUM_SOURCES:
459+
raise SetupError(
460+
f"Number of distinct sources exceeds the maximum allowed {MAX_NUM_SOURCES}. "
461+
"For a complex source setup, consider using 'CustomFieldSource' or "
462+
"'CustomCurrentSource' to combine multiple sources into one object."
463+
)
464+
465+
return val
466+
448467
@pydantic.validator("structures", always=True)
449468
def _validate_num_geometries(cls, val):
450469
"""Error if too many geometries in a single structure."""

0 commit comments

Comments
 (0)