Skip to content

Fix bug in interpolation of epsilon grid from Meep to MPB#3163

Merged
stevengj merged 2 commits intoNanoComp:masterfrom
oskooi:meep_mpb_interp_bug_fix
Mar 7, 2026
Merged

Fix bug in interpolation of epsilon grid from Meep to MPB#3163
stevengj merged 2 commits intoNanoComp:masterfrom
oskooi:meep_mpb_interp_bug_fix

Conversation

@oskooi
Copy link
Collaborator

@oskooi oskooi commented Mar 6, 2026

Fixes #1237.

Bug Analysis

The error "invalid dielectric function for MPB" is triggered by MPB's check_maxwell_dielectric() (in maxwell_eps.c:266), which validates that every grid point's inverse-epsilon tensor (eps_inv) is positive-definite.

Root cause (in mpb.cpp, the primary fix)

The meep_mpb_eps callback assembles a 3×3 eps_inv tensor at each MPB grid point by querying Meep's get_chi1inv for 6 independent components. The diagonal components each come from their natural Yee grid positions (Ex grid for chi1inv_xx, Ey grid for chi1inv_yy, Ez grid for chi1inv_zz), which are spatially offset by half a pixel from each other. The off-diagonal components were queried from only one grid — e.g., m01 = chi1inv(Ex, Y, p) used only the Ex grid.

Near a curved material boundary (like a Cylinder), the Ex grid point may sit right at the interface where subpixel averaging produces a large off-diagonal chi1inv_xy, while the Ey grid point (half a pixel away) is inside the high-epsilon material with a small chi1inv_yy. The assembled tensor then has m01² > m00 * m11, failing positive-definiteness.

Fix: Average each off-diagonal component from both relevant Yee grids:

  • m01 = 0.5 * (chi1inv(Ex,Y,p) + chi1inv(Ey,X,p))
  • m02 = 0.5 * (chi1inv(Ex,Z,p) + chi1inv(Ez,X,p))
  • m12 = 0.5 * (chi1inv(Ey,Z,p) + chi1inv(Ez,Y,p))

This produces off-diagonal values consistent with both corresponding diagonal values.

Secondary bug (in monitor.cpp, defensive six)

structure_chunk::get_chi1inv_at_pt has a frequency-dependent code path (entered whenever frequency != 0) that builds a full 3×3 chi1inv tensor using chi1inv[comp_list[com_it]][dir_int][idx] for all three components — but idx was computed via gv.index(c, iloc) for a single component c. On the Yee grid, the same flat index refers to a different spatial location for each component, so this reads wrong data. For non-dispersive materials (no susceptibilities or conductivity), this tensor path is a no-op that only introduces errors.

Fix: Skip the tensor assembly path when no frequency-dependent material properties exist, returning the direct chi1inv[c][d][idx] value instead.

@codecov-commenter
Copy link

codecov-commenter commented Mar 6, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.90%. Comparing base (f29a8c7) to head (371d22e).
⚠️ Report is 111 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3163      +/-   ##
==========================================
+ Coverage   73.81%   73.90%   +0.09%     
==========================================
  Files          18       18              
  Lines        5423     5454      +31     
==========================================
+ Hits         4003     4031      +28     
- Misses       1420     1423       +3     

see 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stevengj stevengj merged commit a53a3ed into NanoComp:master Mar 7, 2026
5 checks passed
@oskooi oskooi deleted the meep_mpb_interp_bug_fix branch March 7, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bad interpolation from Meep grid to MPB during eigenmode calculation

3 participants