Cleanup and refactor materials library#3252
Open
oskooi wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
python/materials.py(1682 lines) is Meep's optical materials library — a flat, import-time-evaluated module that defines ~60mp.Mediumobjects (semiconductors, dielectrics, metals, birefringent crystals). It is a public API: users dofrom meep.materials import Si, Au, SiO2, ...and the names appear in tests, examples, tutorials, anddoc/docs/Materials.md.The file works but has accumulated problems that make it error-prone to extend:
import numpyis never used.metal_rangeis 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.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.__all__: hundreds of intermediate globals (X_frq1,X_gam1,X_sig1,X_susc,X_range) leak intomeep.materials's namespace.import *exports all of them.sigN = fN * plasma**2 / frqN**2~5× each.Mediumregistry.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)
Si,Au_JC_visible,SiO2_aniso, etc.)um_scaleandeV_um_scalestay at module top, before any material is built (docs instruct users to editum_scalein place — this must keep working since materials are built at import using its value.)mp.Mediummust produce numerically identicalepsilon(freq)to the current file (target: exact; tolerance ≤ 1e-12).## WARNING: unstable; field divergence may occurnotes (Ag_visible,Al_visible,Mo).