Skip to content

Draw perfect metals (ε = -∞) in dark red in plot2D - #3303

Merged
oskooi merged 7 commits into
NanoComp:masterfrom
oskooi:plot2D_metals_dark_grey
Sep 5, 2026
Merged

oskooi merged 7 commits into
NanoComp:masterfrom
oskooi:plot2D_metals_dark_grey

Conversation

@oskooi

@oskooi oskooi commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

Context

mp.metal and mp.perfect_electric_conductor are defined as Medium(epsilon=-inf) (python/meep.i:1825-1826), and that -inf survives as a large finite -1e20 all the way through geom_epsilon::chi1p1Simulation.get_epsilon_grid.

plot_eps — the function plot2D calls to draw the geometry (python/visualization.py:599-732) — passes that array straight to ax.imshow and, when colorbar=True, straight into mpl.colors.Normalize(np.amin(eps_data), np.amax(eps_data)). The consequences of this are:

  • Colorbar: vmin = -inf, so the colorbar is meaningless and every finite dielectric collapses onto one end of the scale.
  • Image: matplotlib's internal safe_masked_invalid masks the -inf pixels and draws them with the colormap's "bad" color, which defaults to fully transparent — metal renders as blank background rather than as a material (see first image below).
  • Contour path: levels=np.unique(eps_data) includes -inf as a contour level.

Changes

python/visualization.py

  • new pec_color: "darkred" key in default_eps_parameters (dropped by filter_dict before reaching imshow, like the other non-imshow dicts).
  • plot_eps now masks metallic pixels (~np.isfinite(eps) | (eps <= -mp.inf)) and paints them via cmap.set_bad(pec_color) on a copy.copy of the colormap, so imshow autoscales over the finite permittivities only.
  • Colorbar vmin/vmax now come from the unmasked values, with a guard for an all-metal plane.
  • Contour path uses np.unique(eps_data.compressed()) so -1e20 is no longer emitted as a contour level.
  • Fixed a latent aliasing bug: eps_parameters = default_eps_parameters handed out the module-level dict itself, which the new code would have mutated — now copy.deepcopy.
  • Added _get_colormap() (handles str or Colormap, matplotlib ≥3.5 or older) and used it in _add_colorbar; required because a Colormap instance is now passed there, and mpl.cm.get_cmap is removed in matplotlib 3.11. This also clears the deprecation warning the old cell emitted.

Verification

test_plot2D_pec (clim finite and equal to (1, 12), bad color = darkred, mask non-empty, defaults unmutated, user override honored) and test_plot2D_pec_contour.

Here is an example involving a cleaved multilayer stack in which the substrate is a perfect metal. Metallic/PEC regions render as a distinct dark red, and the color scale (image and colorbar) is driven only by the finite permittivity values.

edge_emitter_layout edge_emitter_layout

Comment thread python/visualization.py Outdated
Comment thread python/visualization.py Outdated
@oskooi
oskooi merged commit e003ad0 into NanoComp:master Sep 5, 2026
6 checks passed
@oskooi
oskooi deleted the plot2D_metals_dark_grey branch September 5, 2026 02:37
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.

2 participants