Skip to content

Commit 405aef2

Browse files
committed
fix[invdes]: handle pixel size validation for simulations without sources
1 parent ba05f6a commit 405aef2

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- NumPy 2.1 compatibility issue where `numpy.float64` values passed to xarray interpolation would raise TypeError.
1818
- Support for quasi-uniform grid specifications via `QuasiUniformGrid` that subclasses from `GridSpec1d`. The grids are almost uniform, but can adjust locally to the edge of structure bounding boxes, and snapping points.
1919
- New field `min_steps_per_sim_size` in `AutoGrid` that sets minimal number of grid steps per longest edge length of simulation domain.
20+
- Validation error in inverse design plugin when simulation has no sources by adding a source existence check before validating pixel size.
2021

2122
## [2.8.0rc1] - 2024-12-17
2223

tests/test_plugins/test_invdes.py

+20
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,23 @@ def test_validate_invdes_metric():
625625
invdes = invdes.updated_copy(simulation=simulation.updated_copy(monitors=[monitor]))
626626
with pytest.raises(ValueError, match="must return a real"):
627627
invdes.updated_copy(metric=metric)
628+
629+
630+
def test_pixel_size_warn_validator_no_sources(log_capture):
631+
"""Test that pixel size validator handles simulations without sources."""
632+
633+
sourceless_sim = simulation.updated_copy(sources=[])
634+
635+
invdes = make_invdes()
636+
invdes = invdes.updated_copy(simulation=sourceless_sim)
637+
638+
with AssertLogLevel(log_capture, "WARNING", contains_str="Cannot validate pixel size"):
639+
region_coarse = invdes.design_region.updated_copy(pixel_size=1.0)
640+
invdes = invdes.updated_copy(design_region=region_coarse)
641+
642+
invdes_multi = make_invdes_multi()
643+
sourceless_sims = [sourceless_sim for _ in invdes_multi.simulations]
644+
invdes_multi = invdes_multi.updated_copy(simulations=sourceless_sims)
645+
646+
with AssertLogLevel(log_capture, "WARNING", contains_str="Cannot validate pixel size"):
647+
invdes_multi = invdes_multi.updated_copy(design_region=region_coarse)

tidy3d/plugins/invdes/validators.py

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ def check_pixel_size(sim_field_name: str):
3333

3434
def check_pixel_size_sim(sim: td.Simulation, pixel_size: float, index: int = None) -> None:
3535
"""Check a pixel size compared to the simulation min wvl in material."""
36+
if not sim.sources:
37+
td.log.warning(
38+
"Cannot validate pixel size in design region: simulation has no sources. "
39+
"Please ensure the pixel size is appropriate for your target wavelength."
40+
)
41+
return
42+
3643
if pixel_size > PIXEL_SIZE_WARNING_THRESHOLD * sim.wvl_mat_min:
3744
sim_string = f"simulations[{index}]" if index else "the simulation"
3845

0 commit comments

Comments
 (0)