alexysong / inkstone

Efficient electromagnetic solver based on rigorous coupled-wave analysis for 3D and 2D multi-layered structures with in-plane periodicity, such as gratings, photonic-crystal slabs, metasurfaces, surface-emitting lasers, nano-antennas, and more.
GNU Affero General Public License v3.0
44 stars 16 forks source link

Adding Multiple Patterns To Layers Sometimes Removes Prior Patterns From Simulation #7

Closed ApexDavid closed 7 months ago

ApexDavid commented 8 months ago

I found that adding multiple patterns to a layer would, inconsistently, remove prior ones. They are not literally removed, as they are still listed within the Layer object, but they no longer impact the simulation. It also appears to depend on the order in which the objects are added.

I've written a simple script that adds 4 rectangle patterns to a layer in a simulator. When UPPER RIGHT is added first, all four patterns appear in ReconstructLayer. If UPPER RIGHT is added last, it is the only one to appear. Nothing else is changed aside from moving that AddPatternRectangle from the beginning to the end.

SetUpperRightFirst

SetUpperRightLast

sim = Inkstone()
sim.SetNumG(151)
sim.SetLattice(((2,0),(0,2)))

sim.AddMaterial(name = "si", epsilon = 3.4699**2)
sim.AddLayer(name = 'input', thickness = 0, material_background = 'si')
sim.AddLayer(name = 'slab', thickness = 6, material_background = 'vacuum')
sim.AddLayer(name = 'output', thickness = 0, material_background = 'vacuum')

sim.SetExcitation(
    theta=0,
    phi=0,
    s_amplitude=1,
    p_amplitude=0
)

width = 0.3
offset = 0.5

# Add square in -x +y corner
sim.AddPatternRectangle(
    layer = 'slab',
    material = 'si',
    center = [-offset,offset],
    side_lengths = (width,width),
    angle = 0,
    pattern_name="UPPER LEFT"
)

# Add square in +x -y corner
sim.AddPatternRectangle(
    layer = 'slab',
    material = 'si',
    center = [offset,-offset],
    side_lengths = (width,width),
    angle = 0,
    pattern_name="LOWER RIGHT"
)

# Add square in -x -y corner
sim.AddPatternRectangle(
    layer = 'slab',
    material = 'si',
    center = [-offset,-offset],
    side_lengths = (width,width),
    angle = 0,
    pattern_name="LOWER LEFT"
)

# Add square in +x +y corner
# Adding this rectangle first 'fixes' the issue.
sim.AddPatternRectangle(
    layer = 'slab',
    material = 'si',
    center = [offset,offset],
    side_lengths = (width,width),
    angle = 0,
    pattern_name="UPPER RIGHT"
)

print(sim.layers["slab"].patterns)

xx, yy, eps, mu = sim.ReconstructLayer('slab',100,100)
plt.pcolormesh(xx,yy,np.real(eps[:,:,0,0]))
plt.show()
alexysong commented 7 months ago

Fixed in 491ebe2a9d9aa11e1a4ac95bfc68923f91a56cab