Skip to content

PEC gradient support for Box and PolySlab geometries #2724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
841 changes: 841 additions & 0 deletions tests/test_components/test_autograd_rf_box.py

Large diffs are not rendered by default.

706 changes: 706 additions & 0 deletions tests/test_components/test_autograd_rf_polyslab.py

Large diffs are not rendered by default.

133 changes: 133 additions & 0 deletions tests/test_components/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,3 +1298,136 @@ def test_cleanup_shapely_object():
orig_polygon = shapely.Polygon(exterior_coords)
new_polygon = cleanup_shapely_object(orig_polygon, tolerance_ratio=1e-12)
assert len(new_polygon.exterior.coords) == 0 # empty / collinear polygons should get deleted


def test_snap_coords_outside():
"""Test coordinate snapping for box geometries."""
num_coords = 101
coords_to_snap = np.linspace(-1, 1, num_coords)

coord_face_min = np.mean(coords_to_snap[0:2])
coord_face_max = np.mean(coords_to_snap[-2:])

snap_pt_min = td.Box._snap_coords_outside(
min_max_index=0, snap_coords_values=coords_to_snap, coord_normal_face=coord_face_min
)
snap_pt_max = td.Box._snap_coords_outside(
min_max_index=1, snap_coords_values=coords_to_snap, coord_normal_face=coord_face_max
)

assert snap_pt_min == coords_to_snap[0], "Unexpected snapping point minimum"
assert snap_pt_max == coords_to_snap[-1], "Unexpected snapping point maximum"

with AssertLogLevel("WARNING", contains_str="Unable to snap coordinates outside"):
snap_pt_min = td.Box._snap_coords_outside(
min_max_index=0,
snap_coords_values=coords_to_snap,
coord_normal_face=coords_to_snap[0] - 0.1,
)

assert snap_pt_min == coords_to_snap[0], "Unexpected snapping point minimum"

with AssertLogLevel("WARNING", contains_str="Unable to snap coordinates outside"):
snap_pt_min = td.Box._snap_coords_outside(
min_max_index=1,
snap_coords_values=coords_to_snap,
coord_normal_face=coords_to_snap[-1] + 0.1,
)

assert snap_pt_min == coords_to_snap[-1], "Unexpected snapping point maxium"


def test_singularity_correction_pec():
"""Tests singularity correction detection used for PEC gradients of `Box` geometries."""

check_3d_size = (1.0, 2.0, 3.0)

for axis in range(3):
is_2d, zero_dimension, do_singularity_correction = td.Box._check_singularity_correction_pec(
size=check_3d_size, axis_normal=axis
)
assert not is_2d, "Unexpectedly found 2D geometry"
assert not zero_dimension, "Unexpectedly found zero_dimension"
assert not do_singularity_correction, (
"Unexpectedly identifying need for singularity correction"
)

check_1d_error_size = (1.0, 0.0, 0.0)

for axis in range(3):
with AssertLogLevel(
"ERROR", contains_str="Derivative of PEC material with less than 2 dimensions"
):
is_2d, zero_dimension, do_singularity_correction = (
td.Box._check_singularity_correction_pec(size=check_1d_error_size, axis_normal=axis)
)

check_2d_size = 1.0

for index_2d in range(3):
check_2d_size = tuple(0.0 if idx == index_2d else 1.0 for idx in range(3))

for axis in range(3):
print(f"check 2d size = {check_2d_size}")

is_2d, zero_dimension, do_singularity_correction = (
td.Box._check_singularity_correction_pec(size=check_2d_size, axis_normal=axis)
)

print(f"is 2d = {is_2d}")

assert is_2d, "Expected 2D geometry detection"
assert zero_dimension == "xyz"[index_2d], "Wrong zero dimension identified"

if axis == index_2d:
assert not do_singularity_correction, (
"Unexpectedly detecting need for singularity correction"
)
else:
assert do_singularity_correction, (
"Unexpectedly not detecting need for singularity correction"
)


def test_trim_dims_and_bounds_edge():
"""Tests the dimension and bound trimming for 2D PEC gradient integrations in `Box`."""

for zero_dimension_idx in range(3):
box = td.Box(
center=(0.0, 0.0, 0.0),
size=tuple(0.0 if zero_dimension_idx == idx else 1.0 * (idx + 1) for idx in range(3)),
)

for axis_normal in range(3):
if axis_normal == zero_dimension_idx:
continue

dim_normal, dims_perp = box.pop_axis("xyz", axis=axis_normal)
bounds_normal, bounds_perp = box.pop_axis(np.array(box.bounds).T, axis=axis_normal)
bounds_perp = np.array(bounds_perp).T

trimmed_dims, trimmed_bounds = td.Box._trim_dims_and_bounds_edge(
dims_perp=dims_perp,
bounds_perp=bounds_perp,
zero_dimension="xyz"[zero_dimension_idx],
)

expected_trimmed_idx = 0
for idx in range(3):
if (idx == axis_normal) or (idx == zero_dimension_idx):
continue

expected_trimmed_idx = idx

expected_trimmed_dims = ("xyz"[expected_trimmed_idx],)
expected_trimmed_bounds = (
(-0.5 * (expected_trimmed_idx + 1),),
(0.5 * (expected_trimmed_idx + 1),),
)

assert np.all(np.array(expected_trimmed_dims) == np.array(trimmed_dims)), (
"Unexpected trimmed dims"
)
assert np.all(np.array(expected_trimmed_bounds) == np.array(trimmed_bounds)), (
"Unexpected trimmed bounds"
)
Loading
Loading