Skip to content
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 @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- If `ModeSpec.angle_rotation=True` for a mode object, validate that the structure rotation can be successfully done. Also, error if the medium cannot be rotated (e.g. anisotropic or custom medium), which would previously have just produced wrong results.
- Characteristic impedance calculations in the `ImpedanceCalculator` using definitions that rely on flux, which were giving incorrect results for lossy transmission lines.
- Fixed handling of symmetry when creating adjoint field sources and added warning when broken up adjoint simulations do not have the same symmetry as the forward simulation.
- Validation for `CustomGridBoundaries`, which was previously allowing unsorted arrays and arrays with less than two entries.

### Changed
- Relaxed bounds checking of path integrals during `WavePort` validation.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_components/test_grid_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,13 @@ def test_uniform_grid_dl_validation(dl, expect_exception):
grid_spec=td.GridSpec.uniform(dl=dl),
run_time=1e-12,
)


def test_custom_grid_boundary_validation():
"""Tests that the 'coords' is at least length 2 and sorted in ascending order."""

with pytest.raises(pydantic.ValidationError):
_ = td.CustomGridBoundaries(coords=[10])

with pytest.raises(pydantic.ValidationError):
_ = td.CustomGridBoundaries(coords=[9, 10, 9, 10, 11, 9, 8])
17 changes: 17 additions & 0 deletions tidy3d/components/grid/grid_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,23 @@ def estimated_min_dl(

return min(np.diff(self.coords))

@pd.validator("coords", always=True)
def _validate_coords(cls, val):
"""
Ensure 'coords' is sorted and has at least 2 entries.
"""
if len(val) < 2:
raise SetupError("You must supply at least 2 entries for 'coords'.")
# Ensure coords is sorted
positive_diff = np.diff(val) > 0
if not np.all(positive_diff):
violations = np.where(np.diff(val) <= 0)[0] + 1
raise SetupError(
"'coords' must be strictly increasing (sorted in ascending order). "
f"The entries at the following indices violated this requirement: {violations}."
)
return val


class CustomGrid(GridSpec1d):
"""Custom 1D grid supplied as a list of grid cell sizes centered on the simulation center.
Expand Down