Skip to content

Commit

Permalink
Set minimum PML layers to 6
Browse files Browse the repository at this point in the history
  • Loading branch information
momchil-flex committed Nov 13, 2024
1 parent 2490f92 commit 2156051
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 56 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `@scalar_objective` decorator in `tidy3d.plugins.autograd` that wraps objective functions to ensure they return a scalar value and performs additional checks to ensure compatibility of objective functions with autograd. Used by default in `tidy3d.plugins.autograd.value_and_grad` as well as `tidy3d.plugins.autograd.grad`.
- Autograd support for simulations without adjoint sources in `run` as well as `run_async`, which will not attempt to run the simulation but instead return zero gradients. This can sometimes occur if the objective function gradient does not depend on some simulations, for example when using `min` or `max` in the objective.


### Changed
- `CustomMedium` design regions require far less data when performing inverse design by reducing adjoint field monitor size for dims with one pixel.
- Calling `.values` on `DataArray` no longer raises a `DeprecationWarning` during automatic differentiation
- Calling `.values` on `DataArray` no longer raises a `DeprecationWarning` during automatic differentiation.
- Minimum number of PML layers set to 6.

### Fixed
- Regression in local field projection leading to incorrect results for `far_field_approx=True`.
Expand Down
23 changes: 10 additions & 13 deletions tests/test_components/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_sim_nonuniform_small():
# tests when the nonuniform grid does not cover the simulation size

size_x = 18
num_layers_pml_x = 2
num_layers_pml_x = 6
grid_size_x = [2, 1, 3]
sim = td.Simulation(
center=(1, 0, 0),
Expand Down Expand Up @@ -153,15 +153,12 @@ def test_sim_nonuniform_small():
for dl in dls:
assert dl in grid_size_x

# tests that it gives exactly what we expect
assert np.all(bound_coords == np.array([-12, -10, -8, -6, -4, -2, 0, 1, 4, 7, 10, 13, 16]))


def test_sim_nonuniform_large():
# tests when the nonuniform grid extends beyond the simulation size

size_x = 18
num_layers_pml_x = 2
num_layers_pml_x = 6
grid_size_x = [2, 3, 4, 1, 2, 1, 3, 1, 2, 3, 4]
sim = td.Simulation(
center=(1, 0, 0),
Expand Down Expand Up @@ -230,9 +227,9 @@ def test_sim_symmetry_grid():
size=(11, 11, 11),
grid_spec=td.GridSpec(grid_x=grid_1d, grid_y=grid_1d, grid_z=grid_1d),
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=2),
y=td.Boundary.pml(num_layers=2),
z=td.Boundary.pml(num_layers=2),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
symmetry=(0, 1, -1),
run_time=1e-12,
Expand All @@ -257,20 +254,20 @@ def test_sim_pml_grid():
size=(4, 4, 4),
grid_spec=td.GridSpec.uniform(1.0),
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=2),
y=td.Boundary.absorber(num_layers=2),
z=td.Boundary.stable_pml(num_layers=2),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.absorber(num_layers=6),
z=td.Boundary.stable_pml(num_layers=6),
),
run_time=1e-12,
)

for dim in "xyz":
c = sim.grid.centers.dict()[dim]
assert np.all(c == np.array([-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5]))
assert np.all(c == np.arange(-7.5, 8, 1))

for dim in "xyz":
b = sim.grid.boundaries.dict()[dim]
assert np.all(b == np.array([-4, -3, -2, -1, 0, 1, 2, 3, 4]))
assert np.all(b == np.arange(-8, 8.5, 1))


def test_sim_discretize_vol():
Expand Down
18 changes: 9 additions & 9 deletions tests/test_components/test_grid_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ def test_autogrid_2dmaterials():
structures=[box],
sources=[src],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.auto(),
run_time=1e-12,
Expand All @@ -192,9 +192,9 @@ def test_autogrid_2dmaterials():
structures=[box2],
sources=[src],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.auto(),
run_time=1e-12,
Expand All @@ -211,9 +211,9 @@ def test_autogrid_2dmaterials():
structures=[box, box2],
sources=[src],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.auto(),
run_time=1e-12,
Expand Down
52 changes: 26 additions & 26 deletions tests/test_components/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,9 @@ def test_sim_structure_gap(log_capture, box_size, log_level):
structures=[box],
sources=[src],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
run_time=1e-12,
)
Expand Down Expand Up @@ -2047,9 +2047,9 @@ def test_sim_volumetric_structures(log_capture, tmp_path):
structures=[struct],
sources=[src],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.uniform(dl=grid_dl),
run_time=1e-12,
Expand Down Expand Up @@ -2089,9 +2089,9 @@ def test_sim_volumetric_structures(log_capture, tmp_path):
sources=[src],
monitors=[monitor],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.uniform(dl=grid_dl),
run_time=1e-12,
Expand All @@ -2114,9 +2114,9 @@ def test_sim_volumetric_structures(log_capture, tmp_path):
sources=[src],
monitors=[monitor],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.uniform(dl=grid_dl),
run_time=1e-12,
Expand Down Expand Up @@ -2145,9 +2145,9 @@ def test_sim_volumetric_structures(log_capture, tmp_path):
structures=[below_half, box],
sources=[src],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.uniform(dl=grid_dl),
run_time=1e-12,
Expand All @@ -2161,9 +2161,9 @@ def test_sim_volumetric_structures(log_capture, tmp_path):
structures=[box, below],
sources=[src],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.uniform(dl=grid_dl),
run_time=1e-12,
Expand All @@ -2179,9 +2179,9 @@ def test_sim_volumetric_structures(log_capture, tmp_path):
sources=[src],
medium=box.medium,
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.uniform(dl=grid_dl),
run_time=1e-12,
Expand All @@ -2208,9 +2208,9 @@ def test_sim_volumetric_structures(log_capture, tmp_path):
structures=[struct],
sources=[src],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
y=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pml(num_layers=6),
z=td.Boundary.pml(num_layers=6),
),
grid_spec=td.GridSpec.uniform(dl=grid_dl),
run_time=1e-12,
Expand Down Expand Up @@ -2928,9 +2928,9 @@ def test_validate_low_num_cells_in_mode_objects():
grid_spec=td.GridSpec(wavelength=1.0),
sources=[mode_source],
boundary_spec=td.BoundarySpec(
x=td.Boundary.pml(num_layers=5),
x=td.Boundary.pml(num_layers=6),
y=td.Boundary.pec(),
z=td.Boundary.pml(num_layers=5),
z=td.Boundary.pml(num_layers=6),
),
)
sim2d._validate_num_cells_in_mode_objects()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_components/test_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_2d_boundary_plot():

# Simulation details
per_boundary = td.Boundary.periodic()
pml_boundary = td.Boundary.pml(num_layers=2)
pml_boundary = td.Boundary.pml(num_layers=6)

sim = td.Simulation(
size=(0, 1, 1),
Expand Down
18 changes: 13 additions & 5 deletions tidy3d/components/boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from .source import TFSF, GaussianBeam, ModeSource, PlaneWave
from .types import TYPE_TAG_STR, Axis, Complex

MIN_NUM_PML_LAYERS = 6


class BoundaryEdge(ABC, Tidy3dBaseModel):
"""Electromagnetic boundary condition at a domain edge."""
Expand Down Expand Up @@ -260,10 +262,11 @@ class PMLParams(AbsorberParams):
class AbsorberSpec(BoundaryEdge):
"""Specifies the generic absorber properties along a single dimension."""

num_layers: pd.NonNegativeInt = pd.Field(
num_layers: int = pd.Field(
...,
title="Number of Layers",
description="Number of layers of standard PML.",
ge=MIN_NUM_PML_LAYERS,
)
parameters: AbsorberParams = pd.Field(
...,
Expand Down Expand Up @@ -376,10 +379,11 @@ class PML(AbsorberSpec):
"""

num_layers: pd.NonNegativeInt = pd.Field(
num_layers: int = pd.Field(
12,
title="Number of Layers",
description="Number of layers of standard PML.",
ge=MIN_NUM_PML_LAYERS,
)

parameters: PMLParams = pd.Field(
Expand Down Expand Up @@ -413,8 +417,11 @@ class StablePML(AbsorberSpec):
* `Introduction to perfectly matched layer (PML) tutorial <https://www.flexcompute.com/fdtd101/Lecture-6-Introduction-to-perfectly-matched-layer/>`__
"""

num_layers: pd.NonNegativeInt = pd.Field(
40, title="Number of Layers", description="Number of layers of 'stable' PML."
num_layers: int = pd.Field(
40,
title="Number of Layers",
description="Number of layers of 'stable' PML.",
ge=MIN_NUM_PML_LAYERS,
)

parameters: PMLParams = pd.Field(
Expand Down Expand Up @@ -463,10 +470,11 @@ class Absorber(AbsorberSpec):
* `How to troubleshoot a diverged FDTD simulation <../../notebooks/DivergedFDTDSimulation.html>`_
"""

num_layers: pd.NonNegativeInt = pd.Field(
num_layers: int = pd.Field(
40,
title="Number of Layers",
description="Number of layers of absorber to add to + and - boundaries.",
ge=MIN_NUM_PML_LAYERS,
)

parameters: AbsorberParams = pd.Field(
Expand Down

0 comments on commit 2156051

Please sign in to comment.