Skip to content

Field visualization flipped relative to eps preview (origin='lower' vs origin='upper' in viz.field) #77

Description

@4ndrebar

Description

When using legume.viz.field, the field visualization appears vertically flipped relative to the expected layout (as seen in the structure or EPS preview).

Expected vs. Actual

This discrepancy is especially visible when the structure is not vertically symmetric. Here's a comparison:

Expected (matches geometry):
Expected field

Actual (flipped vertically):
Actual field

Minimal working example

import numpy as np
import legume

# Initialize the lattice and the PhC
def wg(
    phc_periods: int = 8,
    index: int = 3,
    lattice_constant: float = 337,
    radius: float = 92,
    stack_height: float = 450,
    background_index: float = 1.45,
    semiconductor_index: float = 3.34,
):
    _radius = radius / lattice_constant
    _stack_height = stack_height / lattice_constant
    cell_center = (0, -(phc_periods) * np.sqrt(3) / 4)
    lattice = legume.Lattice([1, 0], [0, -2 * cell_center[1]])
    phc = legume.PhotCryst(lattice, eps_l=background_index**2, eps_u=background_index**2)

    top_phc = [
        legume.Circle(
            eps=background_index**2,
            x_cent=0.5 * ((period + 1) % 2),
            y_cent=period * np.sqrt(3) / 2 + cell_center[1],
            r=_radius
        )
        for period in range(phc_periods) if period != index
    ]

    phc.add_layer(d=_stack_height, eps_b=semiconductor_index**2)
    phc.add_shape(top_phc)
    return phc

phc = wg()
path = phc.lattice.bz_path([np.array([np.pi / 2, 0]), np.array([np.pi, 0])], [2])

gme = legume.GuidedModeExp(phc, gmax=4.01)
options = {
    'gmode_inds': [2, 4],
    'numeig': 2,
    'verbose': False
}
gme.run(kpoints=path['kpoints'], **options)

# Field visualization
_ = legume.viz.field(
    gme,
    field='e',
    kind=2,
    mind=0,
    z=0.5,
    component='y',
    val='abs'
)

Proposed fix

To correct the flipped field image, I locally modified line 1294 in viz.py:

Original:
origin='lower'

Modified:
origin='upper'

This change makes the field plot align with the EPS geometry.

Environment

legume version: legume.version = 1.0.2

Python version: python --version = 3.13.5

Full Conda environment: conda_env.txt

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