Skip to content

Commit 1e5d85e

Browse files
shashwat-shmomchil-flex
authored andcommitted
for PML or absorbers along a zero dim, raise error instead of warning
- update changelog
1 parent 11b59e7 commit 1e5d85e

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
- Update versions of `boto3`, `requests`, and `click`.
1919
- python 3.7 no longer tested nor supported.
2020
- Remove warning that monitors now have `colocate=True` by default.
21+
- 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.
2122

2223
### Fixed
2324
- If no adjoint sources for one simulation in an objective function, make a mock source with zero amplitude and warn user.

tests/test_components/test_simulation.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def test_validate_plane_wave_boundaries(log_capture):
461461

462462
def test_validate_zero_dim_boundaries(log_capture):
463463

464-
# zero-dim simulation with an absorbing boundary in that direction should warn
464+
# zero-dim simulation with an absorbing boundary in that direction should error
465465
src = td.PlaneWave(
466466
source_time=td.GaussianPulse(freq0=2.5e14, fwidth=1e13),
467467
center=(0, 0, 0),
@@ -470,27 +470,27 @@ def test_validate_zero_dim_boundaries(log_capture):
470470
pol_angle=0.0,
471471
)
472472

473-
td.Simulation(
474-
size=(1, 1, 0),
475-
run_time=1e-12,
476-
sources=[src],
477-
boundary_spec=td.BoundarySpec(
478-
x=td.Boundary.periodic(),
479-
y=td.Boundary.periodic(),
480-
z=td.Boundary.pml(),
481-
),
482-
)
483-
assert_log_level(log_capture, "WARNING")
473+
with pytest.raises(pydantic.ValidationError):
474+
td.Simulation(
475+
size=(1, 1, 0),
476+
run_time=1e-12,
477+
sources=[src],
478+
boundary_spec=td.BoundarySpec(
479+
x=td.Boundary.periodic(),
480+
y=td.Boundary.periodic(),
481+
z=td.Boundary.pml(),
482+
),
483+
)
484484

485-
# zero-dim simulation with an absorbing boundary any other direction should not warn
485+
# zero-dim simulation with an absorbing boundary any other direction should not error
486486
td.Simulation(
487487
size=(1, 1, 0),
488488
run_time=1e-12,
489489
sources=[src],
490490
boundary_spec=td.BoundarySpec(
491491
x=td.Boundary.pml(),
492492
y=td.Boundary.stable_pml(),
493-
z=td.Boundary.pec(),
493+
z=td.Boundary.periodic(),
494494
),
495495
)
496496

@@ -611,6 +611,7 @@ def test_plot_1d_sim():
611611
size=(0, 0, 1),
612612
grid_spec=grid_spec,
613613
run_time=1e-13,
614+
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
614615
)
615616
_ = s.plot(y=0)
616617
plt.close()

tests/test_plugins/test_adjoint.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,11 @@ def test_structure_overlaps():
849849
grid_spec=td.GridSpec(wavelength=1.0),
850850
run_time=1e-12,
851851
sources=(src,),
852+
boundary_spec=td.BoundarySpec(
853+
x=td.Boundary.pml(),
854+
y=td.Boundary.periodic(),
855+
z=td.Boundary.pml(),
856+
),
852857
)
853858

854859

tidy3d/components/simulation.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,17 +383,16 @@ def tfsf_with_symmetry(cls, val, values):
383383

384384
@pydantic.validator("boundary_spec", always=True)
385385
def boundaries_for_zero_dims(cls, val, values):
386-
"""Warn if an absorbing boundary is used along a zero dimension."""
386+
"""Error if an absorbing boundary is used along a zero dimension."""
387387
boundaries = val.to_list
388388
size = values.get("size")
389389
for dim, (boundary, size_dim) in enumerate(zip(boundaries, size)):
390390
num_absorbing_bdries = sum(isinstance(bnd, AbsorberSpec) for bnd in boundary)
391391
if num_absorbing_bdries > 0 and size_dim == 0:
392-
log.warning(
393-
f"If the simulation is intended to be 2D in the plane normal to the "
394-
f"{'xyz'[dim]} axis, using a PML or absorbing boundary along that axis "
395-
f"is incorrect. Consider using a 'Periodic' boundary along {'xyz'[dim]}.",
396-
custom_loc=["boundary_spec", "xyz"[dim]],
392+
raise SetupError(
393+
f"The simulation has zero size along the {'xyz'[dim]} axis, so "
394+
"using a PML or absorbing boundary along that axis is incorrect. "
395+
f"Use either 'Periodic' or 'BlochBoundary' along {'xyz'[dim]}."
397396
)
398397
return val
399398

0 commit comments

Comments
 (0)