Skip to content

Slanted prism: intersect_line_with_prism tests side faces as parallelograms, so subpixel fill fractions next to a sidewall depend on geometry far away #101

Open

Description

@Andeloth

Summary

For a prism with sidewall_angle != 0, intersect_line_with_prism treats each side face as the parallelogram spanned by the bottom edge and the top offset of the edge's first vertex, vps_top[nv] - vps_bottom[nv]. The real face has corners vps_bottom[nv], vps_bottom[nvp1], vps_top[nvp1], vps_top[nv], and the two top offsets differ wherever the edge ends at a corner. There the parallelogram sticks out beyond the face, and a line passing just outside the upper part of the neighbouring sidewall gets a crossing with a face it never meets.

intersect_line_segment_with_prism decides whether a segment starts inside from the parity of the crossings before it, so one spurious crossing turns a line in the cladding into a line inside the prism. Whether that shows depends on the rest of the line:

  • With no other crossing beyond the segment, the na == -1 early exit returns 0, which happens to be right.
  • With one, the line counts as inside.

So box_overlap_with_object gives wrong fill fractions in the voxels cut by the upper part of a sidewall. So does Meep's subpixel smoothing, which integrates them along x-lines. Adding or removing geometry anywhere further along the same lines changes the result. point_in_prism is not affected: it interpolates the cross-section from each vertex's own offset.

Reproducer (Meep 1.34.0, libctl 4.7.1 from conda-forge)

Two slanted ridges that are the same solid within 3 µm of the probed voxels: a straight one, and the same ridge with a step that widens it on the −y side from x = 3 on.

import math
import numpy as np
import meep as mp

mp.verbosity(0)

H, ANGLE, W = 0.3, math.radians(35), 1.62     # ridge height, sidewall angle (35 deg from vertical), base width

# A straight slanted ridge along x, and the identical ridge with a step that widens it on the -y side from x = 3 on.
# Within a few um of x = 0 the two are the same solid.
straight = [(-5, -W / 2), (5, -W / 2), (5, W / 2), (-5, W / 2)]
with_step = [(-5, -W / 2), (3, -W / 2), (3, -1.5), (5, -1.5), (5, W / 2), (-5, W / 2)]

def smoothed_eps(xy):
    """Subpixel-smoothed epsilon of a small cell around x = 0 (voxel values, no interpolation), and voxel centres."""
    prism = mp.Prism([mp.Vector3(*v) for v in xy], height=H, sidewall_angle=ANGLE, material=mp.Medium(epsilon=4))
    cell, center = mp.Vector3(0.2, 3, 0.6), mp.Vector3(0, 0, 0.15)
    sim = mp.Simulation(cell_size=cell, geometry_center=center, resolution=30, geometry=[prism],
                        eps_averaging=True, subpixel_tol=1e-3, subpixel_maxeval=10000)
    sim.init_sim()
    eps = sim.get_array(center=center, size=cell, component=mp.Dielectric)
    x, y, z, _ = sim.get_array_metadata(center=center, size=cell)
    return eps, np.asarray(x), np.asarray(y), np.asarray(z)

eps_straight, x, y, z = smoothed_eps(straight)
eps_step, _, _, _ = smoothed_eps(with_step)
ix, iz = len(x) // 2, int(np.argmin(np.abs(z - 0.27)))     # a row of voxels near the top of the ridge
print(f"voxel row at x = {x[ix]:.3f}, z = {z[iz]:.3f}; the sidewalls there are at y = +-{W / 2 - z[iz] * math.tan(ANGLE):.3f}")
print("     y   straight  with step")
for k in np.flatnonzero((y > -0.8) & (y < -0.45)):
    print(f"{y[k]:+.3f}   {eps_straight[ix, k, iz]:.3f}     {eps_step[ix, k, iz]:.3f}")
print(f"largest difference anywhere in the cell: {np.abs(eps_step - eps_straight).max():.3f}")

The subpixel settings only keep the run short (about 20 s); the default settings are much slower on slanted sidewalls, but they go through the same box_overlap_with_object code.

Output

voxel row at x = 0.017, z = 0.267; the sidewalls there are at y = +-0.623
     y   straight  with step
-0.783   1.000     1.000
-0.750   1.000     1.000
-0.717   1.000     1.000
-0.683   1.000     1.067
-0.650   1.155     1.778
-0.617   2.174     4.000
-0.583   3.949     4.000
-0.550   4.000     4.000
-0.517   4.000     4.000
-0.483   4.000     4.000
-0.450   4.000     4.000
largest difference anywhere in the cell: 2.321

The voxel at y = −0.617 is cut by the sidewall at y = −0.623, about half inside. On the straight ridge it is 2.174. With the step 3 µm away it becomes 4.000, solid ridge, and its neighbours move by up to 0.6. The largest change in the cell is 2.32, on an index contrast of ε = 4 to 1.

Cause

utils/geom.c#L3339-L3358 on current master:

    vector3 v1 = vector3_minus(vps_bottom[nvp1], vps_bottom[nv]);
    vector3 v2 = vector3_minus(vps_top[nv], vps_bottom[nv]);
    ...
    //             pp + s*dp = o + t*v1 + u*v2
    // where o is vps_bottom[nv], v1 is vps_bottom[nvp1]-vps_bottom[nv],
    // v2 is vps_top[nv]-vps_bottom[nv], and 0 <= t <= 1, 0 <= u <= 1.
    ...
    if (tus.x < -tus_tolerance || tus.x > 1+tus_tolerance || tus.y < -tus_tolerance || tus.y > 1+tus_tolerance) continue;

Take the left end of the straight ridge in the reproducer, the edge from (−5, W/2) to (−5, −W/2), with d = H tan 35° = 0.21. At the top, the true face runs from (−5 + d, W/2 − d) to (−5 + d, −W/2 + d). The parallelogram's top edge runs from (−5 + d, W/2 − d) to (−5 + d, −W/2 − d).

The extra strip, y from −W/2 − d to −W/2 + d at the top and narrowing to nothing at the bottom, lies outside the ridge, just beyond its −y sidewall. Every x-line through it gets a crossing at x ≈ −5.

intersect_line_segment_with_prism then takes the inside state at the segment from the number of crossings before it:

  for (ns = 0; na == -1 && ns < num_intersections; ns++)
    if (slist[ns] > a) na = ns;

  if (na == -1) goto done;

  {
    int inside = ((na % 2) == 0 ? 0 : 1);

On the straight ridge, a line through the strip has only the spurious crossing, so na == -1 and the segment gets 0. With the step, the same line also crosses the step at x = 3 and the end at x = 5. The segment at x = 0 then sits after one crossing, and it counts as inside.

The same kind of overhang occurs at every corner of a slanted polygon, on whichever side the first vertex's offset points. A waveguide that is straight here and bends 50 µm further on is enough to trigger it.

normal_to_prism builds the same parallelogram (v2p = vps_diff_to_top[nv]) for min_distance_to_quadrilateral. Near a corner it can therefore also take its normal from the overhang. In our tests, fixing the line intersections alone removed every difference in ε, but the same change would apply there.

This is separate from #99: in the device below, the #99 fix on its own left the error unchanged.

Suggested fix

Test the actual face, for example as the two triangles sharing the diagonal from vps_bottom[nv] to vps_top[nvp1]:

--- a/utils/geom.c
+++ b/utils/geom.c
@@ -3336,26 +3336,31 @@
     int nvp1 = nv + 1;
     if (nvp1 == num_vertices) nvp1 = 0;
 
-    // checks if dp is parallel to the plane of the prism side face under consideration
-    vector3 v1 = vector3_minus(vps_bottom[nvp1], vps_bottom[nv]);
-    vector3 v2 = vector3_minus(vps_top[nv], vps_bottom[nv]);
-    double dot_tolerance = 1e-6;
-    if (fabs(vector3_dot(dp, vector3_cross(v1, v2))) <= dot_tolerance) continue;
-
-    // to find the intersection point pp + s*dp between the line and the
-    // prism side face, we will solve the vector equation
-    //             pp + s*dp = o + t*v1 + u*v2
-    // where o is vps_bottom[nv], v1 is vps_bottom[nvp1]-vps_bottom[nv],
-    // v2 is vps_top[nv]-vps_bottom[nv], and 0 <= t <= 1, 0 <= u <= 1.
-    matrix3x3 M;
-    M.c0 = v1;
-    M.c1 = v2;
-    M.c2 = vector3_scale(-1, dp);
-    vector3 RHS = vector3_minus(pp, vps_bottom[nv]);
-    vector3 tus = matrix3x3_vector3_mult(matrix3x3_inverse(M), RHS);
-    if (tus.x < -tus_tolerance || tus.x > 1+tus_tolerance || tus.y < -tus_tolerance || tus.y > 1+tus_tolerance) continue;
-    double s = tus.z;
-    if (num_intersections++ < slist_len) slist[num_intersections-1] = s;
+    // The side face is the quadrilateral vps_bottom[nv], vps_bottom[nvp1], vps_top[nvp1],
+    // vps_top[nv]. The parallelogram spanned by the bottom edge and vps_top[nv]-vps_bottom[nv]
+    // overhangs it wherever vps_top[nvp1]-vps_bottom[nvp1] differs, i.e. at every corner of a
+    // slanted prism, so test the face as two triangles sharing the diagonal
+    // vps_bottom[nv]-vps_top[nvp1].
+    vector3 o = vps_bottom[nv];
+    vector3 corners[2][2] = {{vector3_minus(vps_bottom[nvp1], o), vector3_minus(vps_top[nvp1], o)},
+                             {vector3_minus(vps_top[nvp1], o), vector3_minus(vps_top[nv], o)}};
+    int tri;
+    for (tri = 0; tri < 2; tri++) {
+      vector3 v1 = corners[tri][0], v2 = corners[tri][1];
+      double dot_tolerance = 1e-6;
+      if (fabs(vector3_dot(dp, vector3_cross(v1, v2))) <= dot_tolerance) continue;
+      matrix3x3 M;
+      M.c0 = v1;
+      M.c1 = v2;
+      M.c2 = vector3_scale(-1, dp);
+      vector3 RHS = vector3_minus(pp, o);
+      vector3 tus = matrix3x3_vector3_mult(matrix3x3_inverse(M), RHS);
+      if (tus.x < -tus_tolerance || tus.y < -tus_tolerance || tus.x + tus.y > 1 + tus_tolerance) continue;
+      // a hit on the shared diagonal (tus.y = 0 in the second triangle) was counted by the first
+      if (tri == 1 && tus.y <= tus_tolerance) continue;
+      double s = tus.z;
+      if (num_intersections++ < slist_len) slist[num_intersections-1] = s;
+    }
   }
 
   // identify intersections with prism ceiling and floor faces

With this patch on top of #99, the two ridges in the reproducer give the same ε everywhere:

voxel row at x = 0.017, z = 0.267; the sidewalls there are at y = +-0.623
     y   straight  with step
-0.783   1.000     1.000
-0.750   1.000     1.000
-0.717   1.000     1.000
-0.683   1.000     1.000
-0.650   1.155     1.155
-0.617   2.140     2.140
-0.583   3.933     3.933
-0.550   4.000     4.000
-0.517   4.000     4.000
-0.483   4.000     4.000
-0.450   4.000     4.000
largest difference anywhere in the cell: 0.000

The straight ridge's sidewall voxel also moves, from 2.174 to 2.140, so the straight case was slightly off as well.

To test it, we built libctl 4.7.1 from the release tarball, with #99 and with this patch, and loaded it into Meep with LD_PRELOAD. (libmeep.so's RPATH takes precedence over LD_LIBRARY_PATH.) An unmodified build reproduced the conda-forge library's numbers exactly.

Effect on a real device

The device is a lithium-niobate rib waveguide in a 90° racetrack bend: Euler plus circular section, R = 100 µm, base width 1.62 µm, 0.3 µm high, 35° sidewalls. The ridge is one slanted prism of 582 vertices, including straight port extensions, simulated at 30 px/µm.

  • About 400 voxels per 4 µm of waveguide had ε wrong by up to 1.5.

  • The TE0 effective index at the input port came out as follows:

    Ridge TE0 n_eff at the input port
    libctl 4.7.1 (with or without Bug fix for prism cross section #99) 1.912094
    Same prism clipped to 20 µm around the port 1.911209
    With this patch, full or clipped 1.910998
    The same solid as an mp.Mesh 1.910998

    So libctl 4.7.1 puts n_eff 1.1 × 10⁻³ too high, about 0.7 rad of extra phase across the bend. With the patch, the prism and the mesh render identical ε at the input port, and the same ε to within 5 × 10⁻⁴ in a 4 µm window on the circular arc.

Workaround until a fix is released: build slanted sidewalls as an mp.Mesh, which Meep 1.34 supports and which is also much faster for large curved polygons.

Activity

  1. stevengj commented on Nov 11, 2026

    @stevengj
    Collaborator

    Can you submit a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions