-
Notifications
You must be signed in to change notification settings - Fork 58
feat: add validation for grid spacing in AutoGrid #2453
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -541,3 +541,41 @@ def test_uniform_grid_dl_validation(dl, expect_exception): | |
grid_spec=td.GridSpec.uniform(dl=dl), | ||
run_time=1e-12, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("freq0", "expect_error"), | ||
[ | ||
(3e21, True), # dl<1e-6 → fail | ||
(1e14, False), # dl>1e-7 → pass | ||
], | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you just make the simulation size 1e7 smaller wouldn't that avoid the long runtime of the test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it reduces the runtime. Thanks! |
||
def test_autogrid_min_spacing(freq0, expect_error): | ||
""" | ||
Test that Simulation.post_init catches too‐fine spacing produced by AutoGrid | ||
(i.e. min grid spacing < 1e-7 µm). | ||
""" | ||
L = 10.0 | ||
buffer = 1.0 | ||
source = td.PointDipole( | ||
center=(-L / 2 + buffer, 0, 0), | ||
source_time=td.GaussianPulse(freq0=freq0, fwidth=freq0 / 10.0), | ||
polarization="Ez", | ||
) | ||
|
||
kwargs = dict( | ||
size=(L, L, L), | ||
sources=[source], | ||
grid_spec=td.GridSpec( | ||
grid_x=td.AutoGrid(min_steps_per_wvl=10), | ||
grid_y=td.AutoGrid(min_steps_per_wvl=10), | ||
grid_z=td.AutoGrid(min_steps_per_wvl=10), | ||
), | ||
run_time=1e-11, | ||
) | ||
|
||
if expect_error: | ||
with pytest.raises(SetupError): | ||
_ = td.Simulation(**kwargs) | ||
else: | ||
_ = td.Simulation(**kwargs) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,6 +228,29 @@ def boundaries_for_zero_dims(cls, val, values): | |
return boundaries_for_zero_dims | ||
|
||
|
||
def _validate_min_grid_spacing(grid, threshold=1e-7, msg_prefix=""): | ||
""" | ||
Ensure that the smallest grid spacing in any direction of the simulation grid | ||
is not below `threshold` (µm). | ||
""" | ||
# grab the 1D cell‐boundary arrays | ||
xs, ys, zs = grid.boundaries.x, grid.boundaries.y, grid.boundaries.z | ||
dx = np.diff(xs) | ||
dy = np.diff(ys) | ||
dz = np.diff(zs) | ||
# ignore zero‐length dims | ||
min_dx = dx[dx > 0].min() if np.any(dx > 0) else np.inf | ||
min_dy = dy[dy > 0].min() if np.any(dy > 0) else np.inf | ||
min_dz = dz[dz > 0].min() if np.any(dz > 0) else np.inf | ||
overall = min(min_dx, min_dy, min_dz) | ||
if (overall < threshold) and not np.isclose(overall, threshold, rtol=1e-6): | ||
raise SetupError( | ||
f"{msg_prefix} grid spacing is {overall:.2e} µm. " | ||
"Please check your units! For more info on Tidy3D units, see: " | ||
"https://docs.flexcompute.com/projects/tidy3d/en/latest/faq/docs/faq/What-are-the-units-used-in-the-simulation.html" | ||
) | ||
|
||
|
||
class AbstractYeeGridSimulation(AbstractSimulation, ABC): | ||
""" | ||
Abstract class for a simulation involving electromagnetic fields defined on a Yee grid. | ||
|
@@ -3596,6 +3619,9 @@ def _post_init_validators(self) -> None: | |
self._validate_custom_source_time() | ||
self._validate_mode_object_bends() | ||
self._warn_mode_object_pml() | ||
_validate_min_grid_spacing( | ||
self.grid, threshold=1e-7, msg_prefix=f"'{self.__class__.__name__}'" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's do 1e-6, even that sounds too large, but to be consistent with the uniform grid validator. Or maybe we should increase the threshold for both? I don't know if I missed anything in the other discussion but wouldn't even 1e-5 be more than reasonable (hard to imagine any simulation that needs to go even below 1e-4). @yaugenst ? |
||
|
||
def _warn_mode_object_pml(self) -> None: | ||
"""Warn if any mode objects have large pml.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1e-6 for both