From 1e5d85e276f5cd1ef6a13b6bb1e436a021e2ade4 Mon Sep 17 00:00:00 2001 From: Shashwat Sharma Date: Mon, 30 Oct 2023 12:12:33 -0400 Subject: [PATCH] for PML or absorbers along a zero dim, raise error instead of warning - update changelog --- CHANGELOG.md | 1 + tests/test_components/test_simulation.py | 29 ++++++++++++------------ tests/test_plugins/test_adjoint.py | 5 ++++ tidy3d/components/simulation.py | 11 ++++----- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee9281bea..35d341597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update versions of `boto3`, `requests`, and `click`. - python 3.7 no longer tested nor supported. - Remove warning that monitors now have `colocate=True` by default. +- If `PML` or any absorbing boundary condition is used along a direction where the `Simulation` size is zero, an error will be raised, rather than just a warning. ### Fixed - If no adjoint sources for one simulation in an objective function, make a mock source with zero amplitude and warn user. diff --git a/tests/test_components/test_simulation.py b/tests/test_components/test_simulation.py index 426fd7b62..d0a99b6fc 100644 --- a/tests/test_components/test_simulation.py +++ b/tests/test_components/test_simulation.py @@ -461,7 +461,7 @@ def test_validate_plane_wave_boundaries(log_capture): def test_validate_zero_dim_boundaries(log_capture): - # zero-dim simulation with an absorbing boundary in that direction should warn + # zero-dim simulation with an absorbing boundary in that direction should error src = td.PlaneWave( source_time=td.GaussianPulse(freq0=2.5e14, fwidth=1e13), center=(0, 0, 0), @@ -470,19 +470,19 @@ def test_validate_zero_dim_boundaries(log_capture): pol_angle=0.0, ) - td.Simulation( - size=(1, 1, 0), - run_time=1e-12, - sources=[src], - boundary_spec=td.BoundarySpec( - x=td.Boundary.periodic(), - y=td.Boundary.periodic(), - z=td.Boundary.pml(), - ), - ) - assert_log_level(log_capture, "WARNING") + with pytest.raises(pydantic.ValidationError): + td.Simulation( + size=(1, 1, 0), + run_time=1e-12, + sources=[src], + boundary_spec=td.BoundarySpec( + x=td.Boundary.periodic(), + y=td.Boundary.periodic(), + z=td.Boundary.pml(), + ), + ) - # zero-dim simulation with an absorbing boundary any other direction should not warn + # zero-dim simulation with an absorbing boundary any other direction should not error td.Simulation( size=(1, 1, 0), run_time=1e-12, @@ -490,7 +490,7 @@ def test_validate_zero_dim_boundaries(log_capture): boundary_spec=td.BoundarySpec( x=td.Boundary.pml(), y=td.Boundary.stable_pml(), - z=td.Boundary.pec(), + z=td.Boundary.periodic(), ), ) @@ -611,6 +611,7 @@ def test_plot_1d_sim(): size=(0, 0, 1), grid_spec=grid_spec, run_time=1e-13, + boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()), ) _ = s.plot(y=0) plt.close() diff --git a/tests/test_plugins/test_adjoint.py b/tests/test_plugins/test_adjoint.py index 8d7f2c05b..a03cfbfa9 100644 --- a/tests/test_plugins/test_adjoint.py +++ b/tests/test_plugins/test_adjoint.py @@ -849,6 +849,11 @@ def test_structure_overlaps(): grid_spec=td.GridSpec(wavelength=1.0), run_time=1e-12, sources=(src,), + boundary_spec=td.BoundarySpec( + x=td.Boundary.pml(), + y=td.Boundary.periodic(), + z=td.Boundary.pml(), + ), ) diff --git a/tidy3d/components/simulation.py b/tidy3d/components/simulation.py index fdf3290e9..8b3fbae01 100644 --- a/tidy3d/components/simulation.py +++ b/tidy3d/components/simulation.py @@ -383,17 +383,16 @@ def tfsf_with_symmetry(cls, val, values): @pydantic.validator("boundary_spec", always=True) def boundaries_for_zero_dims(cls, val, values): - """Warn if an absorbing boundary is used along a zero dimension.""" + """Error if an absorbing boundary is used along a zero dimension.""" boundaries = val.to_list size = values.get("size") for dim, (boundary, size_dim) in enumerate(zip(boundaries, size)): num_absorbing_bdries = sum(isinstance(bnd, AbsorberSpec) for bnd in boundary) if num_absorbing_bdries > 0 and size_dim == 0: - log.warning( - f"If the simulation is intended to be 2D in the plane normal to the " - f"{'xyz'[dim]} axis, using a PML or absorbing boundary along that axis " - f"is incorrect. Consider using a 'Periodic' boundary along {'xyz'[dim]}.", - custom_loc=["boundary_spec", "xyz"[dim]], + raise SetupError( + f"The simulation has zero size along the {'xyz'[dim]} axis, so " + "using a PML or absorbing boundary along that axis is incorrect. " + f"Use either 'Periodic' or 'BlochBoundary' along {'xyz'[dim]}." ) return val