Skip to content

Commit d862808

Browse files
Gregory RobertsGregory Roberts
authored andcommitted
first round of review comments
1 parent 444720f commit d862808

File tree

7 files changed

+649
-348
lines changed

7 files changed

+649
-348
lines changed

tests/test_components/test_autograd_rf_box.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
SAVE_ADJ_LOC = 1
2626
LOCAL_GRADIENT = True
2727
VERBOSE = False
28-
SHOW_PRINT_STATEMENTS = False
28+
SHOW_PRINT_STATEMENTS = True
2929
USE_POLYSLAB_FOR_BOX = False
3030
NUMERICAL_RESULTS_DATA_DIR = (
3131
"./numerical_rf_box_polyslab_test/" if USE_POLYSLAB_FOR_BOX else "./numerical_rf_box_box_test/"
@@ -366,7 +366,7 @@ def run_and_process_fd(all_box_parameters, fd_step, objective):
366366
fd_grad = np.squeeze(all_fd_grad_parameters[argmin_convergence_test])
367367
else:
368368
fd_grad = np.squeeze(all_fd_grad_parameters)
369-
valid_mask = np.ones(fd_grad, dtype=bool)
369+
valid_mask = np.ones(fd_grad.shape, dtype=bool)
370370

371371
return fd_grad, valid_mask
372372

@@ -473,7 +473,6 @@ def test_finite_difference_2d_box_pec(rf_2d_test_parameters, rng, tmp_path, crea
473473
"test_number",
474474
)(rf_2d_test_parameters)
475475

476-
dim_um = 3 * mesh_wvl_um
477476
dim_um = 3 * mesh_wvl_um
478477

479478
thickness_box_placement_um = 0.6 * mesh_wvl_um # 1.2 * mesh_wvl_um
@@ -497,8 +496,6 @@ def test_finite_difference_2d_box_pec(rf_2d_test_parameters, rng, tmp_path, crea
497496
else:
498497
fd_step = np.array([mesh_cell_override_size])
499498

500-
all_perm = []
501-
502499
z_planes = np.array([0.0])
503500

504501
all_box_parameters, box_z_values = generate_boxes(
@@ -632,7 +629,7 @@ def test_finite_difference_2d_box_pec(rf_2d_test_parameters, rng, tmp_path, crea
632629
),
633630
indirect=["dir_name"],
634631
)
635-
def test_finite_difference_3d_pec(rf_3d_test_parameters, rng, tmp_path, create_directory):
632+
def test_finite_difference_3d_box_pec(rf_3d_test_parameters, rng, tmp_path, create_directory):
636633
"""Test a variety of autograd permittivity gradients for 3D PEC boxes by"""
637634
"""comparing them to numerical finite difference."""
638635

@@ -656,7 +653,6 @@ def test_finite_difference_3d_pec(rf_3d_test_parameters, rng, tmp_path, create_d
656653
"test_number",
657654
)(rf_3d_test_parameters)
658655

659-
dim_um = 3 * mesh_wvl_um
660656
dim_um = 3 * mesh_wvl_um
661657

662658
thickness_box_placement_um = 1.2 * mesh_wvl_um
@@ -680,8 +676,6 @@ def test_finite_difference_3d_pec(rf_3d_test_parameters, rng, tmp_path, create_d
680676
else:
681677
fd_step = np.array([mesh_cell_override_size])
682678

683-
all_perm = []
684-
685679
all_box_parameters = generate_boxes(
686680
lateral_dim_bounds=[-0.5 * dim_um, 0.5 * dim_um],
687681
mesh_cell_override_size=mesh_cell_override_size,

tests/test_components/test_autograd_rf_polyslab.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def run_and_process_fd(polyslab_parameters, fd_step, objective):
327327
fd_grad = np.squeeze(all_fd_grad_parameters[argmin_convergence_test])
328328
else:
329329
fd_grad = np.squeeze(all_fd_grad_parameters)
330-
valid_mask = np.ones(fd_grad, dtype=bool)
330+
valid_mask = np.ones(fd_grad.shape, dtype=bool)
331331

332332
return fd_grad, valid_mask
333333

@@ -398,6 +398,10 @@ def run_and_process_fd(polyslab_parameters, fd_step, objective):
398398

399399
test_number += 1
400400

401+
import sys
402+
403+
sys.stdout = sys.stderr
404+
401405

402406
@pytest.mark.numerical
403407
@pytest.mark.parametrize(
@@ -408,7 +412,7 @@ def run_and_process_fd(polyslab_parameters, fd_step, objective):
408412
),
409413
indirect=["dir_name"],
410414
)
411-
def test_finite_difference_2d_pec(rf_2d_test_parameters, rng, tmp_path, create_directory):
415+
def test_finite_difference_2d_polyslab_pec(rf_2d_test_parameters, rng, tmp_path, create_directory):
412416
"""Test a variety of autograd permittivity gradients for 2D `PolySlab` PEC by"""
413417
"""comparing them to numerical finite difference."""
414418

@@ -432,7 +436,6 @@ def test_finite_difference_2d_pec(rf_2d_test_parameters, rng, tmp_path, create_d
432436
"test_number",
433437
)(rf_2d_test_parameters)
434438

435-
dim_um = 1.5 * mesh_wvl_um
436439
dim_um = 1.5 * mesh_wvl_um
437440

438441
thickness_box_placement_um = 1.2 * mesh_wvl_um
@@ -456,8 +459,6 @@ def test_finite_difference_2d_pec(rf_2d_test_parameters, rng, tmp_path, create_d
456459
else:
457460
fd_step = np.array([mesh_cell_override_size])
458461

459-
all_perm = []
460-
461462
z_planes = np.linspace(-0.5 * thickness_box_placement_um, 0.5 * thickness_box_placement_um, 3)
462463

463464
polyslab = generate_polyslab(0.5 * dim_um, dim_um, rng)
@@ -485,16 +486,16 @@ def test_finite_difference_2d_pec(rf_2d_test_parameters, rng, tmp_path, create_d
485486

486487
obj, adj_grad = obj_val_and_grad([polyslab])
487488

488-
if test_number > 3:
489-
old_data = np.load(
490-
f"{NUMERICAL_RESULTS_DATA_DIR}/rf_polyslab_2d_vertex_fd_adj_data_test_{test_number}.npy"
491-
)
492-
fd_grad = old_data[SAVE_FD_LOC, :]
493-
valid_mask = np.ones(len(fd_grad)) > 0
494-
else:
495-
fd_grad, valid_mask = run_and_process_fd(
496-
polyslab_parameters=polyslab, fd_step=fd_step, objective=objective
497-
)
489+
# if test_number > 3:
490+
# old_data = np.load(
491+
# f"{NUMERICAL_RESULTS_DATA_DIR}/rf_polyslab_2d_vertex_fd_adj_data_test_{test_number}.npy"
492+
# )
493+
# fd_grad = old_data[SAVE_FD_LOC, :]
494+
# valid_mask = np.ones(len(fd_grad)) > 0
495+
# else:
496+
fd_grad, valid_mask = run_and_process_fd(
497+
polyslab_parameters=polyslab, fd_step=fd_step, objective=objective
498+
)
498499

499500
fd_grad = np.array(fd_grad)
500501
adj_grad = np.squeeze(np.array(adj_grad))
@@ -575,7 +576,7 @@ def test_finite_difference_2d_pec(rf_2d_test_parameters, rng, tmp_path, create_d
575576
),
576577
indirect=["dir_name"],
577578
)
578-
def test_finite_difference_3d_pec(rf_3d_test_parameters, rng, tmp_path, create_directory):
579+
def test_finite_difference_3d_polyslab_pec(rf_3d_test_parameters, rng, tmp_path, create_directory):
579580
"""Test a variety of autograd permittivity gradients for 3D PEC `PolySlab` by"""
580581
"""comparing them to numerical finite difference."""
581582

@@ -599,7 +600,6 @@ def test_finite_difference_3d_pec(rf_3d_test_parameters, rng, tmp_path, create_d
599600
"test_number",
600601
)(rf_3d_test_parameters)
601602

602-
dim_um = 1.5 * mesh_wvl_um
603603
dim_um = 1.5 * mesh_wvl_um
604604

605605
thickness_box_placement_um = 1.0 * mesh_wvl_um
@@ -623,8 +623,6 @@ def test_finite_difference_3d_pec(rf_3d_test_parameters, rng, tmp_path, create_d
623623
else:
624624
fd_step = np.array([mesh_cell_override_size])
625625

626-
all_perm = []
627-
628626
polyslab = generate_polyslab(0.5 * dim_um, dim_um, rng)
629627
polyslab_z_value = 0
630628
polyslab_z_thickness = polyslab_z_thickness_wvl * adj_wvl_um
@@ -651,11 +649,9 @@ def test_finite_difference_3d_pec(rf_3d_test_parameters, rng, tmp_path, create_d
651649

652650
obj, adj_grad = obj_val_and_grad([polyslab])
653651

654-
old_data = np.load(
655-
f"{NUMERICAL_RESULTS_DATA_DIR}/rf_polyslab_3d_vertex_fd_adj_data_test_{test_number}.npy"
652+
fd_grad, valid_mask = run_and_process_fd(
653+
polyslab_parameters=polyslab, fd_step=fd_step, objective=objective
656654
)
657-
fd_grad = old_data[SAVE_FD_LOC, :]
658-
valid_mask = np.ones(len(fd_grad)) > 0
659655

660656
fd_grad = np.array(fd_grad)
661657
adj_grad = np.squeeze(np.array(adj_grad))
@@ -693,7 +689,7 @@ def test_finite_difference_3d_pec(rf_3d_test_parameters, rng, tmp_path, create_d
693689

694690
if SAVE_FD_ADJ_DATA:
695691
np.save(
696-
f"{NUMERICAL_RESULTS_DATA_DIR}/rf_polyslab_3d_vertex_fd_adj_data_test_{test_number}_.npy",
692+
f"{NUMERICAL_RESULTS_DATA_DIR}/rf_polyslab_3d_vertex_fd_adj_data_test_{test_number}.npy",
697693
vertex_data,
698694
)
699695

@@ -723,5 +719,5 @@ def test_finite_difference_3d_pec(rf_3d_test_parameters, rng, tmp_path, create_d
723719
)
724720

725721
plt.savefig(
726-
f"{NUMERICAL_RESULTS_DATA_DIR}/rf_3d_polyslab_summary_plot_test_{test_number}_.png"
722+
f"{NUMERICAL_RESULTS_DATA_DIR}/rf_3d_polyslab_summary_plot_test_{test_number}.png"
727723
)

tests/test_components/test_geometry.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,3 +1298,136 @@ def test_cleanup_shapely_object():
12981298
orig_polygon = shapely.Polygon(exterior_coords)
12991299
new_polygon = cleanup_shapely_object(orig_polygon, tolerance_ratio=1e-12)
13001300
assert len(new_polygon.exterior.coords) == 0 # empty / collinear polygons should get deleted
1301+
1302+
1303+
def test_snap_coords_outside():
1304+
"""Test coordinate snapping for box geometries."""
1305+
num_coords = 101
1306+
coords_to_snap = np.linspace(-1, 1, num_coords)
1307+
1308+
coord_face_min = np.mean(coords_to_snap[0:2])
1309+
coord_face_max = np.mean(coords_to_snap[-2:])
1310+
1311+
snap_pt_min = td.Box._snap_coords_outside(
1312+
min_max_index=0, snap_coords_values=coords_to_snap, coord_normal_face=coord_face_min
1313+
)
1314+
snap_pt_max = td.Box._snap_coords_outside(
1315+
min_max_index=1, snap_coords_values=coords_to_snap, coord_normal_face=coord_face_max
1316+
)
1317+
1318+
assert snap_pt_min == coords_to_snap[0], "Unexpected snapping point minimum"
1319+
assert snap_pt_max == coords_to_snap[-1], "Unexpected snapping point maximum"
1320+
1321+
with AssertLogLevel("WARNING", contains_str="Unable to snap coordinates outside"):
1322+
snap_pt_min = td.Box._snap_coords_outside(
1323+
min_max_index=0,
1324+
snap_coords_values=coords_to_snap,
1325+
coord_normal_face=coords_to_snap[0] - 0.1,
1326+
)
1327+
1328+
assert snap_pt_min == coords_to_snap[0], "Unexpected snapping point minimum"
1329+
1330+
with AssertLogLevel("WARNING", contains_str="Unable to snap coordinates outside"):
1331+
snap_pt_min = td.Box._snap_coords_outside(
1332+
min_max_index=1,
1333+
snap_coords_values=coords_to_snap,
1334+
coord_normal_face=coords_to_snap[-1] + 0.1,
1335+
)
1336+
1337+
assert snap_pt_min == coords_to_snap[-1], "Unexpected snapping point maxium"
1338+
1339+
1340+
def test_singularity_correction_pec():
1341+
"""Tests singularity correction detection used for PEC gradients of `Box` geometries."""
1342+
1343+
check_3d_size = (1.0, 2.0, 3.0)
1344+
1345+
for axis in range(3):
1346+
is_2d, zero_dimension, do_singularity_correction = td.Box._check_singularity_correction_pec(
1347+
size=check_3d_size, axis_normal=axis
1348+
)
1349+
assert not is_2d, "Unexpectedly found 2D geometry"
1350+
assert not zero_dimension, "Unexpectedly found zero_dimension"
1351+
assert not do_singularity_correction, (
1352+
"Unexpectedly identifying need for singularity correction"
1353+
)
1354+
1355+
check_1d_error_size = (1.0, 0.0, 0.0)
1356+
1357+
for axis in range(3):
1358+
with AssertLogLevel(
1359+
"ERROR", contains_str="Derivative of PEC material with less than 2 dimensions"
1360+
):
1361+
is_2d, zero_dimension, do_singularity_correction = (
1362+
td.Box._check_singularity_correction_pec(size=check_1d_error_size, axis_normal=axis)
1363+
)
1364+
1365+
check_2d_size = 1.0
1366+
1367+
for index_2d in range(3):
1368+
check_2d_size = tuple(0.0 if idx == index_2d else 1.0 for idx in range(3))
1369+
1370+
for axis in range(3):
1371+
print(f"check 2d size = {check_2d_size}")
1372+
1373+
is_2d, zero_dimension, do_singularity_correction = (
1374+
td.Box._check_singularity_correction_pec(size=check_2d_size, axis_normal=axis)
1375+
)
1376+
1377+
print(f"is 2d = {is_2d}")
1378+
1379+
assert is_2d, "Expected 2D geometry detection"
1380+
assert zero_dimension == "xyz"[index_2d], "Wrong zero dimension identified"
1381+
1382+
if axis == index_2d:
1383+
assert not do_singularity_correction, (
1384+
"Unexpectedly detecting need for singularity correction"
1385+
)
1386+
else:
1387+
assert do_singularity_correction, (
1388+
"Unexpectedly not detecting need for singularity correction"
1389+
)
1390+
1391+
1392+
def test_trim_dims_and_bounds_edge():
1393+
"""Tests the dimension and bound trimming for 2D PEC gradient integrations in `Box`."""
1394+
1395+
for zero_dimension_idx in range(3):
1396+
box = td.Box(
1397+
center=(0.0, 0.0, 0.0),
1398+
size=tuple(0.0 if zero_dimension_idx == idx else 1.0 * (idx + 1) for idx in range(3)),
1399+
)
1400+
1401+
for axis_normal in range(3):
1402+
if axis_normal == zero_dimension_idx:
1403+
continue
1404+
1405+
dim_normal, dims_perp = box.pop_axis("xyz", axis=axis_normal)
1406+
bounds_normal, bounds_perp = box.pop_axis(np.array(box.bounds).T, axis=axis_normal)
1407+
bounds_perp = np.array(bounds_perp).T
1408+
1409+
trimmed_dims, trimmed_bounds = td.Box._trim_dims_and_bounds_edge(
1410+
dims_perp=dims_perp,
1411+
bounds_perp=bounds_perp,
1412+
zero_dimension="xyz"[zero_dimension_idx],
1413+
)
1414+
1415+
expected_trimmed_idx = 0
1416+
for idx in range(3):
1417+
if (idx == axis_normal) or (idx == zero_dimension_idx):
1418+
continue
1419+
1420+
expected_trimmed_idx = idx
1421+
1422+
expected_trimmed_dims = ("xyz"[expected_trimmed_idx],)
1423+
expected_trimmed_bounds = (
1424+
(-0.5 * (expected_trimmed_idx + 1),),
1425+
(0.5 * (expected_trimmed_idx + 1),),
1426+
)
1427+
1428+
assert np.all(np.array(expected_trimmed_dims) == np.array(trimmed_dims)), (
1429+
"Unexpected trimmed dims"
1430+
)
1431+
assert np.all(np.array(expected_trimmed_bounds) == np.array(trimmed_bounds)), (
1432+
"Unexpected trimmed bounds"
1433+
)

0 commit comments

Comments
 (0)