Skip to content

Commit

Permalink
Restrict number of time points for non-0D field time monitors
Browse files Browse the repository at this point in the history
  • Loading branch information
momchil-flex committed Mar 15, 2024
1 parent 5d1927c commit 5214434
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- `run_time` of the adjoint simulation is set more robustly based on the adjoint sources and the forward simulation `run_time` as `sim_fwd.run_time + c / fwdith_adj` where `c=10`.
- `FieldTimeMonitor` restriction to record at a maximum of 5000 time steps if the monitor is not zero-dimensional, to avoid creating unnecessarily large amounts of data.

### Fixed
- `tidy3d.plugins.design.Results` store the `BatchData` for batch runs in the `.batch_data` field.
Expand Down
2 changes: 1 addition & 1 deletion docs/faq
Submodule faq updated 225 files
2 changes: 1 addition & 1 deletion docs/notebooks
Submodule notebooks updated 64 files
+2 −2 .github/workflows/sync-to-proxy-repo.yaml
+3 −0 .gitignore
+4 −4 AdjointPlugin10YBranchLevelSet.ipynb
+23 −16 AdjointPlugin11CircuitMZI.ipynb
+62 −64 AdjointPlugin12LightExtractor.ipynb
+1,746 −0 AdjointPlugin13Metasurface.ipynb
+2,953 −0 AdjointPlugin14PreFab.ipynb
+10 −14 AdjointPlugin3InverseDesign.ipynb
+4 −2 AdjointPlugin5BoundaryGradients.ipynb
+11 −12 AdjointPlugin6GratingCoupler.ipynb
+5 −3 AdjointPlugin7Metalens.ipynb
+4 −2 AdjointPlugin8WaveguideBend.ipynb
+115 −327 AdjointPlugin9WDM.ipynb
+2,242 −0 AllDielectricStructuralColor.ipynb
+1 −1 BullseyeCavityPSO.ipynb
+1,945 −2,809 Design.ipynb
+4 −52 Dispersion.ipynb
+16 −16 Fitting.ipynb
+595 −1 GDSExport.ipynb
+8 −8 GeometryTransformations.ipynb
+1 −0 MIMResonator.ipynb
+8 −2 MetalHeaterPhaseShifter.ipynb
+215 −216 ParameterScan.ipynb
+19 −108 PhotonicCrystalWaveguidePolarizationFilter.ipynb
+2,996 −1 ResonanceFinder.ipynb
+242 −356 StripToSlotConverters.ipynb
+5 −1 ThermallyTunedRingResonator.ipynb
+113 −113 TimeModulationTutorial.ipynb
+3,113 −0 WaveguideGratingAntenna.ipynb
+157 −182 WaveguideToRingCoupling.ipynb
+73 −705 XarrayTutorial.ipynb
+104 −2,414 YJunction.ipynb
+3 −1 docs/case_studies/metamaterials_gratings_periodic.rst
+2 −2 docs/case_studies/photonic_opt.rst
+1 −0 docs/case_studies/pic.rst
+5 −3 docs/features/adjoint.rst
+2 −1 docs/features/medium.rst
+1 −1 docs/features/parameter_sweep.rst
+ img/adjoint_11.png
+ img/adjoint_12.png
+ img/adjoint_13.png
+ img/adjoint_3.png
+ img/adjoint_5.png
+ img/adjoint_6.png
+ img/adjoint_7.png
+ img/adjoint_8.png
+ img/adjoint_9.png
+ img/all_dielectric.png
+ img/mim.png
+ img/prefab_intro.png
+ img/prefab_target.png
+ img/thermally_tuned_waveguide.png
+ img/wga_schematic.png
+ img/wga_schematic_2.png
+0 −16 metadata/description_length_checker.py
+0 −681 metadata/metadata.txt
+0 −16 metadata/title_length_checker.py
+0 −30 metadata/update_notebook_metadata.py
+ misc/grating_coupler_history_no_penalty.pkl
+1 −1 misc/import_file_mapping.json
+ misc/logo.png
+22 −0 misc/my_medium.json
+ misc/prefab_base_sim.hdf5
+ misc/prefab_gc.gds
33 changes: 33 additions & 0 deletions tests/test_components/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,39 @@ def test_error_large_monitors(monitor):
sim_large.validate_pre_upload()


def test_error_max_time_monitor_steps():
"""Test if a time monitor with too many time steps causes pre upload error."""

sim = td.Simulation(
size=(5, 5, 5),
run_time=1e-12,
grid_spec=td.GridSpec.uniform(dl=0.01),
sources=[
td.ModeSource(
size=(0.1, 0.1, 0),
direction="+",
source_time=td.GaussianPulse(freq0=2e14, fwidth=0.1e14),
)
],
)

# simulation with a 0D time monitor should not error
monitor = td.FieldTimeMonitor(center=(0, 0, 0), size=(0, 0, 0), name="time")
sim = sim.updated_copy(monitors=[monitor])
sim.validate_pre_upload()

# 1D monitor should error
with pytest.raises(SetupError):
monitor = monitor.updated_copy(size=(1, 0, 0))
sim = sim.updated_copy(monitors=[monitor])
sim.validate_pre_upload()

# setting a large enough interval should again not error
monitor = monitor.updated_copy(interval=20)
sim = sim.updated_copy(monitors=[monitor])
sim.validate_pre_upload()


def test_monitor_num_cells():
"""Test the computation of number of cells in monitor."""
sim = td.Simulation(
Expand Down
23 changes: 21 additions & 2 deletions tidy3d/components/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from .source import CustomCurrentSource, CustomSourceTime, ContinuousWave
from .source import TFSF, Source, ModeSource
from .monitor import MonitorType, Monitor, FreqMonitor, SurfaceIntegrationMonitor
from .monitor import AbstractModeMonitor, FieldMonitor, TimeMonitor
from .monitor import AbstractModeMonitor, FieldMonitor, TimeMonitor, FieldTimeMonitor
from .monitor import PermittivityMonitor, DiffractionMonitor, AbstractFieldProjectionMonitor
from .monitor import FieldProjectionAngleMonitor, FieldProjectionKSpaceMonitor
from .lumped_element import LumpedElementType, LumpedResistor
Expand Down Expand Up @@ -71,11 +71,14 @@
# maximum number of sources
MAX_NUM_SOURCES = 1000

# maximum numbers of simulation parameters
# restrictions on simulation number of cells and number of time steps
MAX_TIME_STEPS = 1e7
WARN_TIME_STEPS = 1e6
MAX_GRID_CELLS = 20e9
MAX_CELLS_TIMES_STEPS = 1e16

# monitor warnings and restrictions
MAX_TIME_MONITOR_STEPS = 5000 # does not apply to 0D monitors
WARN_MONITOR_DATA_SIZE_GB = 10
MAX_MONITOR_INTERNAL_DATA_SIZE_GB = 50
MAX_SIMULATION_DATA_SIZE_GB = 50
Expand Down Expand Up @@ -1743,6 +1746,7 @@ def validate_pre_upload(self, source_required: bool = True) -> None:
self._validate_datasets_not_none()
self._validate_tfsf_structure_intersections()
self._warn_time_monitors_outside_run_time()
self._validate_time_monitors_num_steps()
_ = self.volumetric_structures
log.end_capture(self)
if source_required and len(self.sources) == 0:
Expand Down Expand Up @@ -1854,6 +1858,21 @@ def warn_mode_size(monitor: AbstractModeMonitor, msg_header: str, custom_loc: Li
custom_loc = ["monitors", mnt_ind]
warn_mode_size(monitor=monitor, msg_header=msg_header, custom_loc=custom_loc)

def _validate_time_monitors_num_steps(self) -> None:
"""Raise an error if non-0D time monitors have too many time steps."""
for monitor in self.monitors:
if not isinstance(monitor, FieldTimeMonitor) or len(monitor.zero_dims) == 3:
continue
num_time_steps = monitor.num_steps(self.tmesh)
if num_time_steps > MAX_TIME_MONITOR_STEPS:
raise SetupError(
f"Time monitor '{monitor.name}' records at {num_time_steps} time steps, which "
f"is larger than the maximum allowed value of {MAX_TIME_MONITOR_STEPS} when "
"the monitor is not zero-dimensional. Change the geometry to a point monitor, "
"or use 'start', 'stop', and 'interval' to reduce the number of time steps "
"at which the monitor stores data."
)

@cached_property
def monitors_data_size(self) -> Dict[str, float]:
"""Dictionary mapping monitor names to their estimated storage size in bytes."""
Expand Down

0 comments on commit 5214434

Please sign in to comment.