From 61ef43b8cfcccfc38ab566be5cceb4264072abe5 Mon Sep 17 00:00:00 2001 From: momchil Date: Tue, 12 Nov 2024 14:10:21 +0100 Subject: [PATCH] Set minimum PML layers to 6 --- CHANGELOG.md | 4 +- tests/test_components/test_grid.py | 23 +++++------ tests/test_components/test_grid_spec.py | 18 ++++---- tests/test_components/test_simulation.py | 52 ++++++++++++------------ tests/test_components/test_viz.py | 2 +- tidy3d/components/boundary.py | 16 +++++--- 6 files changed, 59 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc94d0da5..afc38f71d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/tests/test_components/test_grid.py b/tests/test_components/test_grid.py index 2ca7e613e..587a09ddb 100644 --- a/tests/test_components/test_grid.py +++ b/tests/test_components/test_grid.py @@ -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), @@ -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), @@ -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, @@ -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(): diff --git a/tests/test_components/test_grid_spec.py b/tests/test_components/test_grid_spec.py index 2b4d8c471..1753fb9c2 100644 --- a/tests/test_components/test_grid_spec.py +++ b/tests/test_components/test_grid_spec.py @@ -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, @@ -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, @@ -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, diff --git a/tests/test_components/test_simulation.py b/tests/test_components/test_simulation.py index 096afd141..c5ba6e219 100644 --- a/tests/test_components/test_simulation.py +++ b/tests/test_components/test_simulation.py @@ -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, ) @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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() diff --git a/tests/test_components/test_viz.py b/tests/test_components/test_viz.py index d2641a484..6ed6a473b 100644 --- a/tests/test_components/test_viz.py +++ b/tests/test_components/test_viz.py @@ -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), diff --git a/tidy3d/components/boundary.py b/tidy3d/components/boundary.py index 76441f9f3..97c51a4ea 100644 --- a/tidy3d/components/boundary.py +++ b/tidy3d/components/boundary.py @@ -260,10 +260,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.", + gt=5, ) parameters: AbsorberParams = pd.Field( ..., @@ -376,10 +377,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.", + gt=5, ) parameters: PMLParams = pd.Field( @@ -413,8 +415,11 @@ class StablePML(AbsorberSpec): * `Introduction to perfectly matched layer (PML) tutorial `__ """ - 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.", + gt=5, ) parameters: PMLParams = pd.Field( @@ -463,10 +468,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.", + gt=5, ) parameters: AbsorberParams = pd.Field(