Skip to content

Cleanup and refactor materials library#3252

Open
oskooi wants to merge 1 commit into
NanoComp:masterfrom
oskooi:materials_library_fixes
Open

Cleanup and refactor materials library#3252
oskooi wants to merge 1 commit into
NanoComp:masterfrom
oskooi:materials_library_fixes

Conversation

@oskooi

@oskooi oskooi commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Context

python/materials.py (1682 lines) is Meep's optical materials library — a flat, import-time-evaluated module that defines ~60 mp.Medium objects (semiconductors, dielectrics, metals, birefringent crystals). It is a public API: users do from meep.materials import Si, Au, SiO2, ... and the names appear in tests, examples, tutorials, and doc/docs/Materials.md.

The file works but has accumulated problems that make it error-prone to extend:

  • Dead code: import numpy is never used.
  • Typo: line 813 comment reads "chroimium" (should be "chromium").
  • Order-dependent shadowing bug (latent): metal_range is reassigned 3× at module scope (lines 294, 338, 380). The 8 Rakic metals defined after Cu — Al, Be, Cr, Ni, Pd, Pt, Ti, W — silently inherit Cu's range because that was the last assignment. Reordering or inserting a metal would silently change valid ranges.
  • Intra-material variable shadowing: birefringent crystals reuse the same globals for the ordinary and extraordinary axes (e.g. BaB2O4_frq1, LiNbO3_frq1, Al2O3_frq1, SiO2_frq1, etc., are each assigned twice). Correct only because susceptibility lists are built eagerly between the two blocks — fragile and confusing.
  • Namespace pollution / no __all__: hundreds of intermediate globals (X_frq1, X_gam1, X_sig1, X_susc, X_range) leak into meep.materials's namespace. import * exports all of them.
  • Massive boilerplate: each material repeats the same ~10-25 lines. Metals additionally repeat the Rakic formula sigN = fN * plasma**2 / frqN**2 ~5× each.
  • No programmatic lookup: materials can only be selected by hand-typed name; there is no name→Medium registry.

Goal: refactor for readability and safety while keeping the public API and the exact numerical output identical, and add an additive registry for name-based lookup.

Hard constraints (must not change)

  • Every existing material name stays (E.g. Si, Au_JC_visible, SiO2_aniso, etc.)
  • Public constants um_scale and eV_um_scale stay at module top, before any material is built (docs instruct users to edit um_scale in place — this must keep working since materials are built at import using its value.)
  • Every mp.Medium must produce numerically identical epsilon(freq) to the current file (target: exact; tolerance ≤ 1e-12).
  • Preserve all reference comments (papers, refractiveindex.info URLs, wavelength ranges), and ## WARNING: unstable; field divergence may occur notes (Ag_visible, Al_visible, Mo).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant