|
| 1 | +"""Tests for adjoint monitor sizing on planar simulations.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +import tidy3d as td |
| 9 | + |
| 10 | +SIM_FIELDS_KEYS = [("dummy", 0, "geometry")] |
| 11 | + |
| 12 | +POLY_VERTS_2D: np.ndarray = np.array( |
| 13 | + [ |
| 14 | + (0.0, 0.0), |
| 15 | + (3.0, 0.0), |
| 16 | + (4.0, 2.0), |
| 17 | + (2.0, 4.0), |
| 18 | + (0.0, 3.0), |
| 19 | + ], |
| 20 | + dtype=float, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _make_2d_simulation(structure: td.Structure) -> td.Simulation: |
| 25 | + return td.Simulation( |
| 26 | + size=(4.0, 4.0, 0.0), |
| 27 | + run_time=1e-12, |
| 28 | + grid_spec=td.GridSpec.uniform(dl=0.2), |
| 29 | + boundary_spec=td.BoundarySpec.pml(x=True, y=True, z=False), |
| 30 | + structures=[structure], |
| 31 | + sources=[], |
| 32 | + monitors=[ |
| 33 | + td.FieldMonitor(center=(0, 0, 0), size=(0, 0, 0), freqs=[2e14], name="ref"), |
| 34 | + ], |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def _make_tetra_mesh() -> td.TriangleMesh: |
| 39 | + # Reuse the same tetra mesh everywhere (matches the earlier 2D test geometry). |
| 40 | + vertices = np.array( |
| 41 | + [ |
| 42 | + (1.0, 0.0, -0.1), |
| 43 | + (-1.0, 0.0, 0.1), |
| 44 | + (0.0, 1.0, 0.1), |
| 45 | + (0.0, -1.0, 0.1), |
| 46 | + ], |
| 47 | + dtype=float, |
| 48 | + ) |
| 49 | + faces = np.array( |
| 50 | + [ |
| 51 | + (0, 1, 2), |
| 52 | + (0, 1, 3), |
| 53 | + (0, 2, 3), |
| 54 | + (1, 2, 3), |
| 55 | + ], |
| 56 | + dtype=int, |
| 57 | + ) |
| 58 | + return td.TriangleMesh.from_vertices_faces(vertices, faces) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize( |
| 62 | + "center_z, expected_size", |
| 63 | + [ |
| 64 | + (0.0, (1.0, 1.0, 0.0)), |
| 65 | + (0.25, (2 * np.sqrt(0.5**2 - 0.25**2),) * 2 + (0.0,)), |
| 66 | + ], |
| 67 | +) |
| 68 | +def test_adjoint_monitors_use_plane_bounds_sphere(center_z, expected_size): |
| 69 | + structure = td.Structure( |
| 70 | + geometry=td.Sphere(radius=0.5, center=(0, 0, center_z)), medium=td.Medium() |
| 71 | + ) |
| 72 | + sim = _make_2d_simulation(structure) |
| 73 | + |
| 74 | + monitors_field, monitors_eps = sim._make_adjoint_monitors(SIM_FIELDS_KEYS) |
| 75 | + |
| 76 | + assert monitors_field[0].size == pytest.approx(expected_size) |
| 77 | + assert monitors_field[0].center == pytest.approx((0.0, 0.0, 0.0)) |
| 78 | + assert monitors_eps[0].size == pytest.approx(expected_size) |
| 79 | + |
| 80 | + |
| 81 | +def test_adjoint_monitors_use_plane_bounds_mesh(): |
| 82 | + mesh = _make_tetra_mesh() |
| 83 | + structure = td.Structure(geometry=mesh, medium=td.Medium()) |
| 84 | + sim = _make_2d_simulation(structure) |
| 85 | + |
| 86 | + monitors_field, monitors_eps = sim._make_adjoint_monitors(SIM_FIELDS_KEYS) |
| 87 | + |
| 88 | + assert monitors_field[0].size == pytest.approx((0.5, 1.0, 0.0)) |
| 89 | + assert monitors_field[0].center == pytest.approx((0.25, 0.0, 0.0)) |
| 90 | + assert monitors_eps[0].size == pytest.approx((0.5, 1.0, 0.0)) |
| 91 | + |
| 92 | + |
| 93 | +def test_adjoint_monitors_use_plane_bounds_mesh_disjoint_components(): |
| 94 | + """ |
| 95 | + Disjoint mesh components: adjoint-plane monitor should use the union of |
| 96 | + all intersection bounds (not just one component). |
| 97 | + """ |
| 98 | + |
| 99 | + # Two identical tetrahedra, separated in x, symmetric about the origin. |
| 100 | + # Each component spans: |
| 101 | + # x: [-2, -1] and [1, 2] |
| 102 | + # y: [-1, 1] for both |
| 103 | + vertices = np.array( |
| 104 | + [ |
| 105 | + # Left component (x in [-2, -1]) |
| 106 | + (-2.0, 0.0, 1.0), # 0 |
| 107 | + (-1.0, 0.0, -1.0), # 1 |
| 108 | + (-2.0, 1.0, -1.0), # 2 |
| 109 | + (-2.0, -1.0, -1.0), # 3 |
| 110 | + # Right component (x in [1, 2]) |
| 111 | + (2.0, 0.0, 1.0), # 4 |
| 112 | + (1.0, 0.0, -1.0), # 5 |
| 113 | + (2.0, 1.0, -1.0), # 6 |
| 114 | + (2.0, -1.0, -1.0), # 7 |
| 115 | + ], |
| 116 | + dtype=float, |
| 117 | + ) |
| 118 | + |
| 119 | + # Faces for each tetrahedron (same connectivity, offset by +4 for right) |
| 120 | + faces = np.array( |
| 121 | + [ |
| 122 | + (0, 1, 2), |
| 123 | + (0, 1, 3), |
| 124 | + (0, 2, 3), |
| 125 | + (1, 2, 3), |
| 126 | + (4, 5, 6), |
| 127 | + (4, 5, 7), |
| 128 | + (4, 6, 7), |
| 129 | + (5, 6, 7), |
| 130 | + ], |
| 131 | + dtype=int, |
| 132 | + ) |
| 133 | + |
| 134 | + mesh = td.TriangleMesh.from_vertices_faces(vertices, faces) |
| 135 | + structure = td.Structure(geometry=mesh, medium=td.Medium()) |
| 136 | + sim = _make_2d_simulation(structure) |
| 137 | + |
| 138 | + monitors_field, monitors_eps = sim._make_adjoint_monitors(SIM_FIELDS_KEYS) |
| 139 | + |
| 140 | + # Union across both components: |
| 141 | + # x spans [-2, 2] -> size 4 |
| 142 | + # y spans [-0.5, 0.5] -> size 1 (note we are interested in z=0 plane, mid y between +-1 and 0) |
| 143 | + # z size is 0 for a 2D plane monitor |
| 144 | + assert monitors_field[0].size == pytest.approx((4.0, 1.0, 0.0)) |
| 145 | + assert monitors_field[0].center == pytest.approx((0.0, 0.0, 0.0)) |
| 146 | + assert monitors_eps[0].size == pytest.approx((4.0, 1.0, 0.0)) |
| 147 | + |
| 148 | + |
| 149 | +def _make_3d_simulation(structure: td.Structure) -> td.Simulation: |
| 150 | + return td.Simulation( |
| 151 | + size=(4.0, 4.0, 4.0), |
| 152 | + run_time=1e-12, |
| 153 | + grid_spec=td.GridSpec.uniform(dl=0.2), |
| 154 | + boundary_spec=td.BoundarySpec.pml(x=True, y=True, z=True), |
| 155 | + structures=[structure], |
| 156 | + sources=[], |
| 157 | + monitors=[ |
| 158 | + td.FieldMonitor(center=(0, 0, 0), size=(0, 0, 0), freqs=[2e14], name="ref"), |
| 159 | + ], |
| 160 | + ) |
| 161 | + |
| 162 | + |
| 163 | +@pytest.mark.parametrize( |
| 164 | + "geometry", |
| 165 | + [ |
| 166 | + td.Sphere(radius=0.5, center=(0.3, -0.2, 0.1)), |
| 167 | + td.Cylinder(radius=0.3, length=0.8, center=(-0.5, 0.4, -0.1), axis=2), |
| 168 | + td.Box(center=(0.2, 0.1, -0.3), size=(0.6, 0.8, 0.4)), |
| 169 | + _make_tetra_mesh(), |
| 170 | + td.PolySlab(vertices=POLY_VERTS_2D, axis=2, slab_bounds=(-1, 1)), |
| 171 | + ], |
| 172 | + ids=["sphere", "cylinder", "box", "mesh", "polyslab"], |
| 173 | +) |
| 174 | +def test_adjoint_monitors_3d_use_geometry_bounding_box(geometry): |
| 175 | + structure = td.Structure(geometry=geometry, medium=td.Medium()) |
| 176 | + sim = _make_3d_simulation(structure) |
| 177 | + |
| 178 | + monitors_field, monitors_eps = sim._make_adjoint_monitors(SIM_FIELDS_KEYS) |
| 179 | + |
| 180 | + expected_box = geometry.bounding_box |
| 181 | + |
| 182 | + assert monitors_field[0].size == pytest.approx(tuple(expected_box.size)) |
| 183 | + assert monitors_field[0].center == pytest.approx(tuple(expected_box.center)) |
| 184 | + assert monitors_eps[0].size == pytest.approx(tuple(expected_box.size)) |
| 185 | + assert monitors_eps[0].center == pytest.approx(tuple(expected_box.center)) |
0 commit comments