Skip to content

Commit

Permalink
boundary spec deprecation warning and updated tests to always set bou…
Browse files Browse the repository at this point in the history
…ndaries explicitly
  • Loading branch information
shashwat-sh authored and momchil-flex committed Dec 29, 2022
1 parent 892888b commit 3c6399d
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 18 deletions.
23 changes: 20 additions & 3 deletions tests/test_components/test_grid.py
Expand Up @@ -211,7 +211,12 @@ def test_sim_nonuniform_large():

def test_sim_grid():

sim = td.Simulation(size=(4, 4, 4), grid_spec=td.GridSpec.uniform(1.0), run_time=1e-12)
sim = td.Simulation(
size=(4, 4, 4),
grid_spec=td.GridSpec.uniform(1.0),
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

for c in sim.grid.centers.dict(exclude={TYPE_TAG_STR}).values():
assert np.all(c == np.array([-1.5, -0.5, 0.5, 1.5]))
Expand Down Expand Up @@ -272,7 +277,12 @@ def test_sim_pml_grid():

def test_sim_discretize_vol():

sim = td.Simulation(size=(4, 4, 4), grid_spec=td.GridSpec.uniform(1.0), run_time=1e-12)
sim = td.Simulation(
size=(4, 4, 4),
grid_spec=td.GridSpec.uniform(1.0),
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

vol = td.Box(size=(1.9, 1.9, 1.9))

Expand All @@ -289,7 +299,12 @@ def test_sim_discretize_vol():

def test_sim_discretize_plane():

sim = td.Simulation(size=(4, 4, 4), grid_spec=td.GridSpec.uniform(1.0), run_time=1e-12)
sim = td.Simulation(
size=(4, 4, 4),
grid_spec=td.GridSpec.uniform(1.0),
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

plane = td.Box(size=(6, 6, 0))

Expand All @@ -312,13 +327,15 @@ def test_grid_auto_uniform():
grid_spec=td.GridSpec.uniform(0.1),
run_time=1e-12,
medium=td.Medium(permittivity=4),
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

sim_auto = td.Simulation(
size=(4, 4, 4),
grid_spec=td.GridSpec.auto(wavelength=2.4, min_steps_per_wvl=12),
run_time=1e-12,
medium=td.Medium(permittivity=4),
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

bounds_uniform = sim_uniform.grid.boundaries.to_list
Expand Down
68 changes: 63 additions & 5 deletions tests/test_components/test_simulation.py
Expand Up @@ -91,6 +91,15 @@ def test_sim_init():
sim.epsilon(m)


# TODO: remove for 2.0
def test_deprecation_defaults(caplog):
"""Make sure deprecation warnings thrown if defaults used."""
s = td.Simulation(
size=(1, 1, 1), run_time=1e-12, grid_spec=td.GridSpec.uniform(dl=0.1), boundary_spec=None
)
assert_log_level(caplog, 30)


def test_sim_bounds():
"""make sure bounds are working correctly"""

Expand All @@ -111,6 +120,7 @@ def place_box(center_offset):
geometry=td.Box(size=(1, 1, 1), center=shifted_center), medium=td.Medium()
)
],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

# create all permutations of squares being shifted 1, -1, or zero in all three directions
Expand Down Expand Up @@ -141,11 +151,20 @@ def test_sim_size():
grid_spec = td.GridSpec(grid_x=mesh1d, grid_y=mesh1d, grid_z=mesh1d)

with pytest.raises(SetupError):
s = td.Simulation(size=(1, 1, 1), grid_spec=grid_spec, run_time=1e-12)
s = td.Simulation(
size=(1, 1, 1),
grid_spec=grid_spec,
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)
s._validate_size()

with pytest.raises(SetupError):
s = td.Simulation(size=(1, 1, 1), run_time=1e-7)
s = td.Simulation(
size=(1, 1, 1),
run_time=1e-7,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)
s._validate_size()


Expand All @@ -161,6 +180,7 @@ def _test_monitor_size():
)
],
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)
s.validate_pre_upload()

Expand All @@ -184,6 +204,7 @@ def test_monitor_medium_frequency_range(caplog, freq, log_level):
monitors=[mnt],
sources=[src],
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)
assert_log_level(caplog, log_level)

Expand All @@ -199,7 +220,13 @@ def test_monitor_simulation_frequency_range(caplog, fwidth, log_level):
polarization="Ex",
)
mnt = td.FieldMonitor(size=(0, 0, 0), name="freq", freqs=[1.5])
sim = td.Simulation(size=(1, 1, 1), monitors=[mnt], sources=[src], run_time=1e-12)
sim = td.Simulation(
size=(1, 1, 1),
monitors=[mnt],
sources=[src],
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)
assert_log_level(caplog, log_level)


Expand Down Expand Up @@ -489,6 +516,7 @@ def test_large_grid_size(caplog, grid_size, log_level):
structures=[box],
sources=[src],
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

assert_log_level(caplog, log_level)
Expand Down Expand Up @@ -543,6 +571,7 @@ def test_sim_plane_wave_error():
structures=[box_transparent],
sources=[src],
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

# with non-transparent box, raise
Expand All @@ -552,6 +581,7 @@ def test_sim_plane_wave_error():
medium=medium_bg,
structures=[box_transparent, box],
sources=[src],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)


Expand Down Expand Up @@ -611,6 +641,7 @@ def test_sim_monitor_homogeneous():
sources=[src],
run_time=1e-12,
monitors=[monitor],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

# with non-transparent box, raise
Expand All @@ -622,6 +653,7 @@ def test_sim_monitor_homogeneous():
sources=[src],
monitors=[monitor],
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

mediums = td.Simulation.intersecting_media(monitor_n2f_vol, sim1.structures)
Expand Down Expand Up @@ -653,6 +685,7 @@ def test_sim_monitor_homogeneous():
sources=[src],
monitors=[monitor_n2f_vol_exclude],
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)


Expand Down Expand Up @@ -709,6 +742,7 @@ def test_proj_monitor_distance(caplog):
sources=[src],
run_time=1e-12,
monitors=[monitor_n2f_far],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)
assert_log_level(caplog, 30)

Expand All @@ -719,6 +753,7 @@ def test_proj_monitor_distance(caplog):
sources=[src],
run_time=1e-12,
monitors=[monitor_n2f],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

# proj_distance large but using approximations - don't warn
Expand All @@ -728,6 +763,7 @@ def test_proj_monitor_distance(caplog):
sources=[src],
run_time=1e-12,
monitors=[monitor_n2f_approx],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)


Expand Down Expand Up @@ -763,6 +799,7 @@ def test_diffraction_medium():
sources=[src],
run_time=1e-12,
monitors=[monitor],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

with pytest.raises(SetupError):
Expand All @@ -772,6 +809,7 @@ def test_diffraction_medium():
sources=[src],
monitors=[monitor],
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)


Expand All @@ -788,7 +826,13 @@ def test_sim_structure_extent(caplog, box_size, log_level):
polarization="Ex",
)
box = td.Structure(geometry=td.Box(size=box_size), medium=td.Medium(permittivity=2))
sim = td.Simulation(size=(1, 1, 1), structures=[box], sources=[src], run_time=1e-12)
sim = td.Simulation(
size=(1, 1, 1),
structures=[box],
sources=[src],
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

assert_log_level(caplog, log_level)

Expand All @@ -802,7 +846,13 @@ def test_num_mediums():
structures.append(
td.Structure(geometry=td.Box(size=(1, 1, 1)), medium=td.Medium(permittivity=i + 1))
)
sim = td.Simulation(size=(5, 5, 5), grid_spec=grid_spec, structures=structures, run_time=1e-12)
sim = td.Simulation(
size=(5, 5, 5),
grid_spec=grid_spec,
structures=structures,
run_time=1e-12,
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

with pytest.raises(SetupError):
structures.append(
Expand Down Expand Up @@ -861,6 +911,7 @@ def _test_names_default():
td.FluxMonitor(size=(0, 1, 1), center=(0, -0.5, 0), freqs=[1], name="mon2"),
td.FluxMonitor(size=(1, 0, 1), center=(0, -0.5, 0), freqs=[1], name="mon3"),
],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

for i, structure in enumerate(sim.structures):
Expand Down Expand Up @@ -888,6 +939,7 @@ def test_names_unique():
name="struct1",
),
],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

with pytest.raises(SetupError) as e:
Expand All @@ -910,6 +962,7 @@ def test_names_unique():
name="source1",
),
],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

with pytest.raises(SetupError) as e:
Expand All @@ -920,6 +973,7 @@ def test_names_unique():
td.FluxMonitor(size=(1, 1, 0), center=(0, -0.5, 0), freqs=[1], name="mon1"),
td.FluxMonitor(size=(0, 1, 1), center=(0, -0.5, 0), freqs=[1], name="mon1"),
],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)


Expand All @@ -936,6 +990,7 @@ def test_mode_object_syms():
run_time=1e-12,
symmetry=(1, -1, 0),
sources=[td.ModeSource(size=(2, 2, 0), direction="+", source_time=g)],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

# wrong mode monitor
Expand All @@ -949,6 +1004,7 @@ def test_mode_object_syms():
monitors=[
td.ModeMonitor(size=(2, 2, 0), name="mnt", freqs=[2], mode_spec=td.ModeSpec())
],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

# right mode source (centered on the symmetry)
Expand All @@ -959,6 +1015,7 @@ def test_mode_object_syms():
run_time=1e-12,
symmetry=(1, -1, 0),
sources=[td.ModeSource(center=(1, -1, 1), size=(2, 2, 0), direction="+", source_time=g)],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)

# right mode monitor (entirely in the main quadrant)
Expand All @@ -973,4 +1030,5 @@ def test_mode_object_syms():
center=(2, 0, 1), size=(2, 2, 0), name="mnt", freqs=[2], mode_spec=td.ModeSpec()
)
],
boundary_spec=td.BoundarySpec.all_sides(boundary=td.Periodic()),
)
3 changes: 3 additions & 0 deletions tests/test_data/test_data_arrays.py
Expand Up @@ -18,6 +18,7 @@
from tidy3d.components.monitor import MonitorType
from tidy3d.components.structure import Structure
from tidy3d.components.geometry import Box
from tidy3d.components.boundary import BoundarySpec, Periodic
from tidy3d import material_library
from tidy3d.constants import inf

Expand Down Expand Up @@ -101,6 +102,7 @@
sources=SOURCES,
monitors=MONITORS,
structures=STRUCTURES,
boundary_spec=BoundarySpec.all_sides(boundary=Periodic()),
)

SIM = Simulation(
Expand All @@ -111,6 +113,7 @@
sources=SOURCES,
monitors=MONITORS,
structures=STRUCTURES,
boundary_spec=BoundarySpec.all_sides(boundary=Periodic()),
)

""" Generate the data arrays (used in other test files) """
Expand Down
1 change: 1 addition & 0 deletions tests/utils.py
Expand Up @@ -53,6 +53,7 @@ def prepend_tmp(path):
name="mode",
),
],
boundary_spec=BoundarySpec.all_sides(boundary=Periodic()),
)

SIM_FULL = Simulation(
Expand Down

0 comments on commit 3c6399d

Please sign in to comment.