Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix[invdes]: handle pixel size validation for simulations without sources #2161

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- NumPy 2.1 compatibility issue where `numpy.float64` values passed to xarray interpolation would raise TypeError.
- 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.
- New field `min_steps_per_sim_size` in `AutoGrid` that sets minimal number of grid steps per longest edge length of simulation domain.
- Validation error in inverse design plugin when simulation has no sources by adding a source existence check before validating pixel size.

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

Expand Down
20 changes: 20 additions & 0 deletions tests/test_plugins/test_invdes.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,23 @@ def test_validate_invdes_metric():
invdes = invdes.updated_copy(simulation=simulation.updated_copy(monitors=[monitor]))
with pytest.raises(ValueError, match="must return a real"):
invdes.updated_copy(metric=metric)


def test_pixel_size_warn_validator_no_sources(log_capture):
"""Test that pixel size validator handles simulations without sources."""

sourceless_sim = simulation.updated_copy(sources=[])

invdes = make_invdes()
invdes = invdes.updated_copy(simulation=sourceless_sim)

with AssertLogLevel(log_capture, "WARNING", contains_str="Cannot validate pixel size"):
region_coarse = invdes.design_region.updated_copy(pixel_size=1.0)
invdes = invdes.updated_copy(design_region=region_coarse)

invdes_multi = make_invdes_multi()
sourceless_sims = [sourceless_sim for _ in invdes_multi.simulations]
invdes_multi = invdes_multi.updated_copy(simulations=sourceless_sims)

with AssertLogLevel(log_capture, "WARNING", contains_str="Cannot validate pixel size"):
invdes_multi = invdes_multi.updated_copy(design_region=region_coarse)
7 changes: 7 additions & 0 deletions tidy3d/plugins/invdes/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ def check_pixel_size(sim_field_name: str):

def check_pixel_size_sim(sim: td.Simulation, pixel_size: float, index: int = None) -> None:
"""Check a pixel size compared to the simulation min wvl in material."""
if not sim.sources:
td.log.warning(
"Cannot validate pixel size in design region: simulation has no sources. "
"Please ensure the pixel size is appropriate for your target wavelength."
)
return

if pixel_size > PIXEL_SIZE_WARNING_THRESHOLD * sim.wvl_mat_min:
sim_string = f"simulations[{index}]" if index else "the simulation"

Expand Down
Loading