Skip to content

Commit efb87ae

Browse files
committed
Warn if time monitor start time is after simulation run time
1 parent c21dde3 commit efb87ae

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

tests/test_components/test_simulation.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,27 @@ def test_error_large_monitors():
18331833
s.validate_pre_upload()
18341834

18351835

1836+
@pytest.mark.parametrize("start, log_level", [(1e-12, None), (1, "WARNING")])
1837+
def test_warn_time_monitor_outside_run_time(log_capture, start, log_level):
1838+
"""Make sure we get a warning if the mode monitor grid is too large."""
1839+
1840+
sim = td.Simulation(
1841+
size=(2.0, 2.0, 2.0),
1842+
grid_spec=td.GridSpec.uniform(dl=0.1),
1843+
run_time=1e-12,
1844+
sources=[
1845+
td.ModeSource(
1846+
size=(0.1, 0.1, 0),
1847+
direction="+",
1848+
source_time=td.GaussianPulse(freq0=1e12, fwidth=0.1e12),
1849+
)
1850+
],
1851+
monitors=[td.FieldTimeMonitor(size=(td.inf, 0, td.inf), start=start, name="test")],
1852+
)
1853+
with AssertLogLevel(log_capture, log_level_expected=log_level, contains_str="start time"):
1854+
sim.validate_pre_upload()
1855+
1856+
18361857
def test_dt():
18371858
"""make sure dt is reduced when there is a medium with eps_inf < 1."""
18381859
sim = td.Simulation(

tidy3d/components/simulation.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from .source import CustomCurrentSource, CustomSourceTime, ContinuousWave
3030
from .source import TFSF, Source, ModeSource
3131
from .monitor import MonitorType, Monitor, FreqMonitor, SurfaceIntegrationMonitor
32-
from .monitor import AbstractModeMonitor, FieldMonitor
32+
from .monitor import AbstractModeMonitor, FieldMonitor, TimeMonitor
3333
from .monitor import PermittivityMonitor, DiffractionMonitor, AbstractFieldProjectionMonitor
3434
from .monitor import FieldProjectionAngleMonitor, FieldProjectionKSpaceMonitor
3535
from .data.dataset import Dataset
@@ -1065,7 +1065,7 @@ def validate_pre_upload(self, source_required: bool = True) -> None:
10651065
self._validate_modes_size()
10661066
self._validate_datasets_not_none()
10671067
self._validate_tfsf_structure_intersections()
1068-
# self._validate_run_time()
1068+
self._warn_time_monitors_outside_run_time()
10691069
_ = self.volumetric_structures
10701070
log.end_capture(self)
10711071
if source_required and len(self.sources) == 0:
@@ -1282,6 +1282,21 @@ def _validate_tfsf_structure_intersections(self) -> None:
12821282
f" '{'xyz'[source.injection_axis]}'."
12831283
)
12841284

1285+
def _warn_time_monitors_outside_run_time(self) -> None:
1286+
"""Warn if time monitors start after the simulation run_time.
1287+
TODO: (remove this comment later) this is done as a pre-upload validator in view of a
1288+
planned change to allow ``run_time`` to accept a ``RunTimeSpec`` which would automatically
1289+
determine a run time based on simulation details. Then, we would have to access the
1290+
dynamically computed run_time e.g. through a ``_run_time`` cached property.
1291+
"""
1292+
with log as consolidated_logger:
1293+
for monitor in self.monitors:
1294+
if isinstance(monitor, TimeMonitor) and monitor.start > self.run_time:
1295+
consolidated_logger.warning(
1296+
f"Monitor {monitor.name} has a start time {monitor.start:1.2e}s exceeding"
1297+
f"the simulation run time {self.run_time:1.2e}s. No data will be recorded."
1298+
)
1299+
12851300
""" Accounting """
12861301

12871302
# candidate for removal in 3.0

0 commit comments

Comments
 (0)