In [1]:
# -*- coding: utf-8 -*-

import meep as mp
import math
import numpy as np
import matplotlib.pyplot as plt

resolution = 50        # pixels/μm

dpml = 1.0             # PML thickness
dsub = 3.0             # substrate thickness
dpad = 3.0             # padding between grating and PML
gp = 10.0              # grating period
gh = 0.5               # grating height
gdc = 0.5              # grating duty cycle

sx = dpml+dsub+gh+dpad+dpml
sy = gp

cell_size = mp.Vector3(sx,sy,0)
pml_layers = [mp.PML(thickness=dpml,direction=mp.X)]

wvl_min = 0.4           # min wavelength
wvl_max = 0.6           # max wavelength
fmin = 1/wvl_max        # min frequency
fmax = 1/wvl_min        # max frequency
fcen = 0.5*(fmin+fmax)  # center frequency
df = fmax-fmin          # frequency width

src_pt = mp.Vector3(-0.5*sx+dpml+0.5*dsub)
sources = [mp.Source(mp.GaussianSource(fcen, fwidth=df), component=mp.Ez, center=src_pt, size=mp.Vector3(y=sy))]

k_point = mp.Vector3(0,0,0)

glass = mp.Medium(index=1.5)

symmetries=[mp.Mirror(mp.Y)]

sim = mp.Simulation(resolution=resolution,
                    cell_size=cell_size,
                    boundary_layers=pml_layers,
                    k_point=k_point,
                    default_material=glass,
                    sources=sources,
                    symmetries=symmetries)

nfreq = 21
mon_pt = mp.Vector3(0.5*sx-dpml-0.5*dpad)
flux_mon = sim.add_flux(fcen, df, nfreq, mp.FluxRegion(center=mon_pt, size=mp.Vector3(y=sy)))

sim.run(until_after_sources=mp.stop_when_fields_decayed(50, mp.Ez, mon_pt, 1e-9))

input_flux = mp.get_fluxes(flux_mon)

sim.reset_meep()

geometry = [mp.Block(material=glass, size=mp.Vector3(dpml+dsub,mp.inf,mp.inf), center=mp.Vector3(-0.5*sx+0.5*(dpml+dsub))),
            mp.Block(material=glass, size=mp.Vector3(gh,gdc*gp,mp.inf), center=mp.Vector3(-0.5*sx+dpml+dsub+0.5*gh))]

sim = mp.Simulation(resolution=resolution,
                    cell_size=cell_size,
                    boundary_layers=pml_layers,
                    geometry=geometry,
                    k_point=k_point,
                    sources=sources,
                    symmetries=symmetries)

mode_mon = sim.add_flux(fcen, df, nfreq, mp.FluxRegion(center=mon_pt, size=mp.Vector3(y=sy)))

sim.run(until_after_sources=mp.stop_when_fields_decayed(50, mp.Ez, mon_pt, 1e-9))

freqs = mp.get_eigenmode_freqs(mode_mon)

nmode = 10
res = sim.get_eigenmode_coefficients(mode_mon, range(1,nmode+1), eig_parity=mp.ODD_Z+mp.EVEN_Y)
coeffs = res.alpha
kdom = res.kdom

mode_wvl = []
mode_angle = []
mode_tran = []

for nm in range(nmode):
  for nf in range(nfreq):
    mode_wvl.append(1/freqs[nf])
    mode_angle.append(math.degrees(math.acos(kdom[nm*nfreq+nf].x/freqs[nf])))
    tran = abs(coeffs[nm,nf,0])**2/input_flux[nf]
    mode_tran.append(0.5*tran if nm != 0 else tran)
    print("grating{}:, {:.5f}, {:.2f}, {:.8f}".format(nm,mode_wvl[-1],mode_angle[-1],mode_tran[-1]))

tran_max = round(max(mode_tran),1)
-----------
Initializing structure...
field decay(t = 50.01): 0.10609306658233111 / 0.10609306658233111 = 1.0
field decay(t = 100.01): 8.493197174525773e-20 / 0.10609306658233111 = 8.005421511626135e-19
run 0 finished at t = 100.01 (10001 timesteps)
-----------
Initializing structure...
field decay(t = 50.01): 0.10313983544158939 / 0.10313983544158939 = 1.0
field decay(t = 100.01): 8.275841626039517e-06 / 0.10313983544158939 = 8.023904237006785e-05
field decay(t = 150.02): 7.578862246277832e-06 / 0.10313983544158939 = 7.348142658778942e-05
field decay(t = 200.03): 2.6331983132012556e-06 / 0.10313983544158939 = 2.55303714799167e-05
field decay(t = 250.04): 1.0595609940381617e-06 / 0.10313983544158939 = 1.0273052981921975e-05
field decay(t = 300.04): 4.182093600426132e-07 / 0.10313983544158939 = 4.054780175400371e-06
field decay(t = 350.05): 1.7897453529965382e-07 / 0.10313983544158939 = 1.7352610127152227e-06
field decay(t = 400.06): 7.323581231103283e-08 / 0.10313983544158939 = 7.100633038386808e-07
field decay(t = 450.07): 2.9341078575718368e-08 / 0.10313983544158939 = 2.8447862506368783e-07
field decay(t = 500.08): 1.184153513314788e-08 / 0.10313983544158939 = 1.1481049084913492e-07
field decay(t = 550.08): 4.99840667036108e-09 / 0.10313983544158939 = 4.846242626780028e-08
field decay(t = 600.09): 2.3507305572060455e-09 / 0.10313983544158939 = 2.2791684194002052e-08
field decay(t = 650.1): 1.1816026525465915e-09 / 0.10313983544158939 = 1.1456317023268492e-08
field decay(t = 700.11): 3.9576427414058525e-10 / 0.10313983544158939 = 3.837162163834518e-09
field decay(t = 750.12): 1.4213834548368875e-10 / 0.10313983544158939 = 1.378112975215916e-09
field decay(t = 800.13): 8.16132061585537e-11 / 0.10313983544158939 = 7.912869533786803e-10
run 0 finished at t = 800.13 (80013 timesteps)
grating0:, 0.60000, 0.00, 0.06566064
grating0:, 0.58537, 0.00, 0.05057571
grating0:, 0.57143, 0.00, 0.03752612
grating0:, 0.55814, 0.00, 0.02620881
grating0:, 0.54545, 0.00, 0.01693912
grating0:, 0.53333, 0.00, 0.00973341
grating0:, 0.52174, 0.00, 0.00458582
grating0:, 0.51064, 0.00, 0.00152102
grating0:, 0.50000, 0.00, 0.00059451
grating0:, 0.48980, 0.00, 0.00181994
grating0:, 0.48000, 0.00, 0.00521963
grating0:, 0.47059, 0.00, 0.01065881
grating0:, 0.46154, 0.00, 0.01826748
grating0:, 0.45283, 0.00, 0.02799911
grating0:, 0.44444, 0.00, 0.03976392
grating0:, 0.43636, 0.00, 0.05353118
grating0:, 0.42857, 0.00, 0.06923018
grating0:, 0.42105, 0.00, 0.08678683
grating0:, 0.41379, 0.00, 0.10606991
grating0:, 0.40678, 0.00, 0.12727449
grating0:, 0.40000, 0.00, 0.15011932
grating1:, 0.60000, 3.44, 0.36441833
grating1:, 0.58537, 3.36, 0.37026888
grating1:, 0.57143, 3.28, 0.37531227
grating1:, 0.55814, 3.20, 0.37950073
grating1:, 0.54545, 3.13, 0.38292703
grating1:, 0.53333, 3.06, 0.38544932
grating1:, 0.52174, 2.99, 0.38707714
grating1:, 0.51064, 2.93, 0.38788219
grating1:, 0.50000, 2.87, 0.38779242
grating1:, 0.48980, 2.81, 0.38676394
grating1:, 0.48000, 2.75, 0.38487068
grating1:, 0.47059, 2.70, 0.38213290
grating1:, 0.46154, 2.65, 0.37846383
grating1:, 0.45283, 2.60, 0.37394214
grating1:, 0.44444, 2.55, 0.36858855
grating1:, 0.43636, 2.50, 0.36237877
grating1:, 0.42857, 2.46, 0.35537418
grating1:, 0.42105, 2.41, 0.34760885
grating1:, 0.41379, 2.37, 0.33907896
grating1:, 0.40678, 2.33, 0.32983916
grating1:, 0.40000, 2.29, 0.31995793
grating2:, 0.60000, 6.89, 0.00060264
grating2:, 0.58537, 6.72, 0.00061980
grating2:, 0.57143, 6.56, 0.00066675
grating2:, 0.55814, 6.41, 0.00070498
grating2:, 0.54545, 6.26, 0.00073362
grating2:, 0.53333, 6.12, 0.00077073
grating2:, 0.52174, 5.99, 0.00081400
grating2:, 0.51064, 5.86, 0.00084271
grating2:, 0.50000, 5.74, 0.00087413
grating2:, 0.48980, 5.62, 0.00092390
grating2:, 0.48000, 5.51, 0.00094605
grating2:, 0.47059, 5.40, 0.00097020
grating2:, 0.46154, 5.30, 0.00101890
grating2:, 0.45283, 5.20, 0.00104340
grating2:, 0.44444, 5.10, 0.00107227
grating2:, 0.43636, 5.01, 0.00109732
grating2:, 0.42857, 4.92, 0.00113048
grating2:, 0.42105, 4.83, 0.00115295
grating2:, 0.41379, 4.75, 0.00119091
grating2:, 0.40678, 4.67, 0.00121934
grating2:, 0.40000, 4.59, 0.00121934
grating3:, 0.60000, 10.37, 0.04032187
grating3:, 0.58537, 10.11, 0.04096864
grating3:, 0.57143, 9.87, 0.04150771
grating3:, 0.55814, 9.64, 0.04191451
grating3:, 0.54545, 9.42, 0.04230647
grating3:, 0.53333, 9.21, 0.04255648
grating3:, 0.52174, 9.01, 0.04268330
grating3:, 0.51064, 8.81, 0.04277436
grating3:, 0.50000, 8.63, 0.04276314
grating3:, 0.48980, 8.45, 0.04260564
grating3:, 0.48000, 8.28, 0.04237879
grating3:, 0.47059, 8.12, 0.04209800
grating3:, 0.46154, 7.96, 0.04166668
grating3:, 0.45283, 7.81, 0.04115689
grating3:, 0.44444, 7.66, 0.04057382
grating3:, 0.43636, 7.52, 0.03987212
grating3:, 0.42857, 7.39, 0.03909019
grating3:, 0.42105, 7.26, 0.03823992
grating3:, 0.41379, 7.13, 0.03728341
grating3:, 0.40678, 7.01, 0.03625078
grating3:, 0.40000, 6.89, 0.03516630
grating4:, 0.60000, 13.89, 0.00062308
grating4:, 0.58537, 13.54, 0.00063845
grating4:, 0.57143, 13.21, 0.00068368
grating4:, 0.55814, 12.90, 0.00072240
grating4:, 0.54545, 12.60, 0.00075041
grating4:, 0.53333, 12.32, 0.00078515
grating4:, 0.52174, 12.05, 0.00082905
grating4:, 0.51064, 11.79, 0.00085870
grating4:, 0.50000, 11.54, 0.00088811
grating4:, 0.48980, 11.30, 0.00093827
grating4:, 0.48000, 11.07, 0.00096058
grating4:, 0.47059, 10.85, 0.00098445
grating4:, 0.46154, 10.64, 0.00103279
grating4:, 0.45283, 10.44, 0.00105781
grating4:, 0.44444, 10.24, 0.00108599
grating4:, 0.43636, 10.05, 0.00111000
grating4:, 0.42857, 9.87, 0.00114322
grating4:, 0.42105, 9.70, 0.00116469
grating4:, 0.41379, 9.53, 0.00120199
grating4:, 0.40678, 9.36, 0.00123025
grating4:, 0.40000, 9.21, 0.00122872
grating5:, 0.60000, 17.46, 0.01434617
grating5:, 0.58537, 17.02, 0.01458357
grating5:, 0.57143, 16.60, 0.01476756
grating5:, 0.55814, 16.20, 0.01486971
grating5:, 0.54545, 15.83, 0.01502474
grating5:, 0.53333, 15.47, 0.01509725
grating5:, 0.52174, 15.12, 0.01510229
grating5:, 0.51064, 14.79, 0.01513732
grating5:, 0.50000, 14.48, 0.01513738
grating5:, 0.48980, 14.18, 0.01504842
grating5:, 0.48000, 13.89, 0.01495349
grating5:, 0.47059, 13.61, 0.01487265
grating5:, 0.46154, 13.34, 0.01470122
grating5:, 0.45283, 13.09, 0.01451304
grating5:, 0.44444, 12.84, 0.01431250
grating5:, 0.43636, 12.60, 0.01405324
grating5:, 0.42857, 12.37, 0.01377062
grating5:, 0.42105, 12.15, 0.01347551
grating5:, 0.41379, 11.94, 0.01312754
grating5:, 0.40678, 11.74, 0.01275200
grating5:, 0.40000, 11.54, 0.01237396
grating6:, 0.60000, 21.10, 0.00065868
grating6:, 0.58537, 20.56, 0.00067104
grating6:, 0.57143, 20.05, 0.00071263
grating6:, 0.55814, 19.57, 0.00075198
grating6:, 0.54545, 19.10, 0.00077912
grating6:, 0.53333, 18.66, 0.00080894
grating6:, 0.52174, 18.24, 0.00085387
grating6:, 0.51064, 17.84, 0.00088503
grating6:, 0.50000, 17.46, 0.00091058
grating6:, 0.48980, 17.09, 0.00096069
grating6:, 0.48000, 16.74, 0.00098339
grating6:, 0.47059, 16.40, 0.00100748
grating6:, 0.46154, 16.08, 0.00105476
grating6:, 0.45283, 15.77, 0.00108059
grating6:, 0.44444, 15.47, 0.00110807
grating6:, 0.43636, 15.18, 0.00112967
grating6:, 0.42857, 14.90, 0.00116340
grating6:, 0.42105, 14.63, 0.00118366
grating6:, 0.41379, 14.38, 0.00121941
grating6:, 0.40678, 14.13, 0.00124712
grating6:, 0.40000, 13.89, 0.00124315
grating7:, 0.60000, 24.83, 0.00712106
grating7:, 0.58537, 24.19, 0.00725596
grating7:, 0.57143, 23.58, 0.00735079
grating7:, 0.55814, 23.00, 0.00736618
grating7:, 0.54545, 22.45, 0.00746403
grating7:, 0.53333, 21.92, 0.00749527
grating7:, 0.52174, 21.42, 0.00746526
grating7:, 0.51064, 20.94, 0.00748585
grating7:, 0.50000, 20.49, 0.00749678
grating7:, 0.48980, 20.05, 0.00742615
grating7:, 0.48000, 19.63, 0.00736557
grating7:, 0.47059, 19.23, 0.00734406
grating7:, 0.46154, 18.85, 0.00724584
grating7:, 0.45283, 18.48, 0.00714626
grating7:, 0.44444, 18.13, 0.00705266
grating7:, 0.43636, 17.79, 0.00691768
grating7:, 0.42857, 17.46, 0.00677418
grating7:, 0.42105, 17.14, 0.00663497
grating7:, 0.41379, 16.84, 0.00645740
grating7:, 0.40678, 16.54, 0.00626388
grating7:, 0.40000, 16.26, 0.00608317
grating8:, 0.60000, 28.69, 0.00071146
grating8:, 0.58537, 27.92, 0.00071967
grating8:, 0.57143, 27.20, 0.00075394
grating8:, 0.55814, 26.52, 0.00079416
grating8:, 0.54545, 25.87, 0.00082087
grating8:, 0.53333, 25.26, 0.00084132
grating8:, 0.52174, 24.67, 0.00088698
grating8:, 0.51064, 24.11, 0.00092106
grating8:, 0.50000, 23.58, 0.00094015
grating8:, 0.48980, 23.07, 0.00098910
grating8:, 0.48000, 22.58, 0.00101255
grating8:, 0.47059, 22.12, 0.00103723
grating8:, 0.46154, 21.67, 0.00108226
grating8:, 0.45283, 21.24, 0.00110965
grating8:, 0.44444, 20.83, 0.00113597
grating8:, 0.43636, 20.43, 0.00115414
grating8:, 0.42857, 20.05, 0.00118921
grating8:, 0.42105, 19.68, 0.00120781
grating8:, 0.41379, 19.33, 0.00124119
grating8:, 0.40678, 18.99, 0.00126834
grating8:, 0.40000, 18.66, 0.00126119
grating9:, 0.60000, 32.68, 0.00405749
grating9:, 0.58537, 31.79, 0.00416387
grating9:, 0.57143, 30.95, 0.00423625
grating9:, 0.55814, 30.15, 0.00421224
grating9:, 0.54545, 29.40, 0.00429611
grating9:, 0.53333, 28.69, 0.00432324
grating9:, 0.52174, 28.01, 0.00427886
grating9:, 0.51064, 27.36, 0.00429394
grating9:, 0.50000, 26.74, 0.00432027
grating9:, 0.48980, 26.16, 0.00425827
grating9:, 0.48000, 25.59, 0.00420924
grating9:, 0.47059, 25.06, 0.00421678
grating9:, 0.46154, 24.54, 0.00415124
grating9:, 0.45283, 24.05, 0.00408829
grating9:, 0.44444, 23.58, 0.00404009
grating9:, 0.43636, 23.12, 0.00395897
grating9:, 0.42857, 22.69, 0.00387386
grating9:, 0.42105, 22.27, 0.00380207
grating9:, 0.41379, 21.86, 0.00369822
grating9:, 0.40678, 21.48, 0.00358005
grating9:, 0.40000, 21.10, 0.00348359
In [2]:
plt.figure(dpi=200)
plt.pcolormesh(np.reshape(mode_wvl,(nmode,nfreq)),
               np.reshape(mode_angle,(nmode,nfreq)),
               np.reshape(mode_tran,(nmode,nfreq)),
               cmap='Blues',
               shading='flat',
               vmin=0,
               vmax=tran_max)
plt.axis([min(mode_wvl), max(mode_wvl), min(mode_angle), max(mode_angle)])
plt.xlabel("wavelength (μm)")
plt.ylabel("diffraction angle (degrees)")
plt.xticks([t for t in np.linspace(wvl_min,wvl_max,3)])
plt.yticks([t for t in range(0,35,5)])
plt.title("transmittance of diffraction orders")
cbar = plt.colorbar()
cbar.set_ticks([t for t in np.arange(0,tran_max+0.1,0.1)])
cbar.set_ticklabels(["{:.1f}".format(t) for t in np.arange(0,tran_max+0.1,0.1)])