Skip to content

Commit 56b4603

Browse files
tylerflexmomchil-flex
authored andcommitted
better interval description and warning if unset
1 parent 93d0fcc commit 56b4603

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
- Warnings for too many frequencies in monitors; too many modes requested in a ``ModeSpec``; too many number of grid points in a mode monitor or mode source.
2020

2121
### Changed
22+
- Time domain monitors warn about data usage if all defaults used in time sampler specs.
2223

2324
### Fixed
2425
- Faster sorting of modes in `ModeSolverData.overlap_sort` by avoiding excessive data copying.

tests/test_components/test_monitor.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,28 @@ def test_stop_start():
1212
td.FluxTimeMonitor(size=(1, 1, 0), name="f", start=2, stop=1)
1313

1414

15+
# interval, start, stop, log_out
16+
time_sampling_tests = [
17+
(None, 0.0, None, "WARNING"), # all defaults
18+
(1, 0.0, None, None), # interval set (=1)
19+
(2, 0.0, None, None), # interval set (=2)
20+
(None, 1e-12, None, None), # start specified
21+
(None, 0.0, 5e-12, None), # stop specified
22+
]
23+
24+
25+
@pytest.mark.parametrize("interval, start, stop, log_desired", time_sampling_tests)
26+
def test_monitor_interval_warn(log_capture, interval, start, stop, log_desired):
27+
"""Assert time monitor interval warning handled as expected."""
28+
29+
mnt = td.FluxTimeMonitor(size=(1, 1, 0), name="f", interval=interval, stop=stop, start=start)
30+
assert_log_level(log_capture, log_desired)
31+
32+
# make sure it got set to either 1 (undefined) or the specified value
33+
mnt_interval = interval if interval else 1
34+
assert mnt.interval == mnt_interval
35+
36+
1537
def test_time_inds():
1638
M = td.FluxTimeMonitor(size=(1, 1, 0), name="f", start=0, stop=1)
1739
assert M.time_inds(tmesh=[]) == (0, 0)

tidy3d/components/monitor.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,40 @@ class TimeMonitor(Monitor, ABC):
192192
)
193193

194194
interval: pydantic.PositiveInt = pydantic.Field(
195-
1,
195+
None,
196196
title="Time interval",
197-
description="Number of time step intervals between monitor recordings.",
197+
description="Sampling rate of the monitor: number of time steps between each measurement. "
198+
"Set ``inverval`` to 1 for the highest possible resolution in time. "
199+
"Higher integer values downsample the data by measuring every ``interval`` time steps. "
200+
"This can be useful for reducing data storage as needed by the application.",
198201
)
199202

203+
@pydantic.validator("interval", always=True)
204+
def _warn_interval_default(cls, val, values):
205+
"""If all defaults used for time sampler, warn and set ``interval=1`` internally."""
206+
207+
if val is None:
208+
start = values.get("start")
209+
stop = values.get("stop")
210+
if start == 0.0 and stop is None:
211+
log.warning(
212+
"The monitor 'interval' field was left as its default value, "
213+
"which will set it to 1 internally. "
214+
"A value of 1 means that the data will be sampled at every time step, "
215+
"which may potentially produce more data than desired, "
216+
"depending on the use case. "
217+
"To reduce data storage, one may downsample the data by setting 'interval > 1'"
218+
" or by choosing alternative 'start' and 'stop' values for the time sampling. "
219+
"If you intended to use the highest resolution time sampling, "
220+
"you may suppress this warning "
221+
"by explicitly setting 'interval=1' in the monitor."
222+
)
223+
224+
# set 'interval = 1' for backwards compatibility
225+
val = 1
226+
227+
return val
228+
200229
@pydantic.validator("stop", always=True, allow_reuse=True)
201230
def stop_greater_than_start(cls, val, values):
202231
"""Ensure sure stop is greater than or equal to start."""

0 commit comments

Comments
 (0)