Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions tests/test_IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_simulation_preserve_types():
st = GaussianPulse(freq0=1.0, fwidth=1.0)

sim_all = Simulation(
size=(1.0, 1.0, 1.0),
size=(10.0, 10.0, 10.0),
grid_size=(1, 1, 1),
structures=[
Structure(geometry=Box(size=(1, 1, 1)), medium=Medium()),
Expand All @@ -40,7 +40,13 @@ def test_simulation_preserve_types():
],
sources=[
VolumeSource(size=(0, 0, 0), source_time=st, polarization="Ex"),
PlaneWave(size=(inf, inf, 0), source_time=st, direction="+", polarization="Ex"),
PlaneWave(
size=(inf, inf, 0),
center=(0, 0, 4.5),
source_time=st,
direction="+",
polarization="Ex",
),
GaussianBeam(
size=(inf, inf, 0),
source_time=st,
Expand Down
153 changes: 150 additions & 3 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@
from tidy3d.log import ValidationError, SetupError


def assert_log_level(caplog, log_level_expected):
"""ensure something got logged if log_level is not None.
note: I put this here rather than utils.py because if we import from utils.py,
it will validate the sims there and those get included in log.
"""

# get log output
logs = caplog.record_tuples

# there's a log but the log level is not None (problem)
if logs and not log_level_expected:
raise Exception

# we expect a log but none is given (problem)
if log_level_expected and not logs:
raise Exception

# both expected and got log, check the log levels match
if logs and log_level_expected:
for log in logs:
log_level = log[1]
if log_level == log_level_expected:
# log level was triggered, exit
return
raise Exception


def test_sim():
"""make sure a simulation can be initialized"""

Expand Down Expand Up @@ -116,7 +143,128 @@ def place_box(center_offset):
def test_sim_grid_size():

size = (1, 1, 1)
s = Simulation(size=size, grid_size=(1.0, 1.0, 1.0))
_ = Simulation(size=size, grid_size=(1.0, 1.0, 1.0))


@pytest.mark.parametrize("fwidth,log_level", [(0.001, None), (3, 30)])
def test_sim_frequency_range(caplog, fwidth, log_level):
# small fwidth should be inside range, large one should throw warning

size = (1, 1, 1)
medium = Medium(frequency_range=(2, 3))
box = Structure(geometry=Box(size=(0.1, 0.1, 0.1)), medium=medium)
src = VolumeSource(
source_time=GaussianPulse(freq0=2.4, fwidth=fwidth),
size=(0, 0, 0),
polarization="Ex",
)
_ = Simulation(size=(1, 1, 1), grid_size=(0.1, 0.1, 0.1), structures=[box], sources=[src])

assert_log_level(caplog, log_level)


@pytest.mark.parametrize("grid_size,log_level", [(0.001, None), (3, 30)])
def test_sim_grid_size(caplog, grid_size, log_level):
# small fwidth should be inside range, large one should throw warning

medium = Medium(permittivity=2, frequency_range=(2e14, 3e14))
box = Structure(geometry=Box(size=(0.1, 0.1, 0.1)), medium=medium)
src = VolumeSource(
source_time=GaussianPulse(freq0=2.5e14, fwidth=1e13),
size=(0, 0, 0),
polarization="Ex",
)
_ = Simulation(size=(1, 1, 1), grid_size=(0.1, 0.1, grid_size), structures=[box], sources=[src])

assert_log_level(caplog, log_level)


@pytest.mark.parametrize("box_size,log_level", [(0.001, None), (9.9, 30), (20, None)])
def test_sim_structure_gap(caplog, box_size, log_level):
"""Make sure the gap between a structure and PML is not too small compared to lambda0."""
medium = Medium(permittivity=2)
box = Structure(geometry=Box(size=(box_size, box_size, box_size)), medium=medium)
src = VolumeSource(
source_time=GaussianPulse(freq0=3e14, fwidth=1e13),
size=(0, 0, 0),
polarization="Ex",
)
sim = Simulation(
size=(10, 10, 10),
grid_size=(0.1, 0.1, 0.1),
structures=[box],
sources=[src],
pml_layers=[PML(num_layers=5), PML(num_layers=5), PML(num_layers=5)],
)
assert_log_level(caplog, log_level)


def test_sim_plane_wave_error():
""" "Make sure we error if plane wave is not intersecting homogeneous region of simulation."""

medium_bg = Medium(permittivity=2)
medium_air = Medium(permittivity=1)

box = Structure(geometry=Box(size=(0.1, 0.1, 0.1)), medium=medium_air)

box_transparent = Structure(geometry=Box(size=(0.1, 0.1, 0.1)), medium=medium_bg)

src = PlaneWave(
source_time=GaussianPulse(freq0=2.5e14, fwidth=1e13),
center=(0, 0, 0),
size=(inf, inf, 0),
direction="+",
polarization="Ex",
)

# with transparent box continue
_ = Simulation(
size=(1, 1, 1),
grid_size=(0.1, 0.1, 0.1),
medium=medium_bg,
structures=[box_transparent],
sources=[src],
)

# with non-transparent box, raise
with pytest.raises(SetupError):
_ = Simulation(
size=(1, 1, 1),
grid_size=(0.1, 0.1, 0.1),
medium=medium_bg,
structures=[box_transparent, box],
sources=[src],
)


@pytest.mark.parametrize(
"box_size,log_level",
[((0.1, 0.1, 0.1), None), ((1, 0.1, 0.1), 30), ((0.1, 1, 0.1), 30), ((0.1, 0.1, 1), 30)],
)
def test_sim_structure_extent(caplog, box_size, log_level):
"""Make sure we warn if structure extends exactly to simulation edges."""

box = Structure(geometry=Box(size=box_size), medium=Medium(permittivity=2))
sim = Simulation(size=(1, 1, 1), grid_size=(0.1, 0.1, 0.1), structures=[box])

assert_log_level(caplog, log_level)


def test_num_mediums():
"""Make sure we error if too many mediums supplied."""

structures = []
for i in range(200):
structures.append(
Structure(geometry=Box(size=(1, 1, 1)), medium=Medium(permittivity=i + 1))
)
sim = Simulation(size=(1, 1, 1), grid_size=(0.1, 0.1, 0.1), structures=structures)

with pytest.raises(SetupError):
structures.append(
Structure(geometry=Box(size=(1, 1, 1)), medium=Medium(permittivity=i + 2))
)
sim = Simulation(size=(1, 1, 1), grid_size=(0.1, 0.1, 0.1), structures=structures)


""" geometry """
Expand Down Expand Up @@ -259,7 +407,6 @@ def test_medium_dispersion_create():
def eps_compare(medium: Medium, expected: Dict, tol: float = 1e-5):

for freq, val in expected.items():
# print(f"Expected: {medium.eps_model(freq)}, got: {val}")
assert np.abs(medium.eps_model(freq) - val) < tol


Expand Down Expand Up @@ -340,7 +487,7 @@ def test_modes():
""" names """


def test_names_default():
def _test_names_default():
"""makes sure default names are set"""

sim = Simulation(
Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def prepend_tmp(path):


SIM_MONITORS = Simulation(
size=(2.0, 2.0, 2.0),
size=(10.0, 10.0, 10.0),
grid_size=(0.1, 0.1, 0.1),
monitors=[
FieldMonitor(size=(1, 1, 1), center=(0, 1, 0), freqs=[1, 2, 5, 7, 8], name="field_freq"),
Expand All @@ -52,7 +52,7 @@ def prepend_tmp(path):
)

SIM_FULL = Simulation(
size=(2.0, 2.0, 2.0),
size=(10.0, 10.0, 10.0),
grid_size=(0.1, 0.1, 0.1),
run_time=40e-11,
structures=[
Expand Down
Loading