Skip to content

Commit

Permalink
for PML or absorbers along a zero dim, raise error instead of warning
Browse files Browse the repository at this point in the history
- update changelog
  • Loading branch information
shashwat-sh authored and momchil-flex committed Oct 30, 2023
1 parent 11b59e7 commit 1e5d85e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 15 additions & 14 deletions tests/test_components/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -470,27 +470,27 @@ 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,
sources=[src],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(),
y=td.Boundary.stable_pml(),
z=td.Boundary.pec(),
z=td.Boundary.periodic(),
),
)

Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions tests/test_plugins/test_adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
),
)


Expand Down
11 changes: 5 additions & 6 deletions tidy3d/components/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1e5d85e

Please sign in to comment.