Note
Go to the end to download the full example code. or to run this example in your browser via Binder
Convergence#
Convergence of the various FMM formulations.
import time
import matplotlib.pyplot as plt
import nannos as nn
bk = nn.backend
We will study the convergence on a benchmark case from [Li1997]. First we define the main function that performs the simulation.
wavelength = 1
sq_size = 1.25 * wavelength
eps_diel = 2.25
def checkerboard_cellA(nh, formulation):
d = 2 * sq_size
Nx = 2**9
Ny = 2**9
lattice = nn.Lattice(([d, 0], [0, d]), discretization=(Nx, Ny))
pw = nn.PlaneWave(wavelength=wavelength, angles=(0, 0, 0))
epsgrid = lattice.ones() * eps_diel
sq1 = lattice.square((0.25 * d, 0.25 * d), sq_size)
sq2 = lattice.square((0.75 * d, 0.75 * d), sq_size)
epsgrid[sq1] = 1
epsgrid[sq2] = 1
sup = lattice.Layer("Superstrate", epsilon=eps_diel)
sub = lattice.Layer("Substrate", epsilon=1)
st = lattice.Layer("Structured", wavelength)
st.epsilon = epsgrid
sim = nn.Simulation([sup, st, sub], pw, nh, formulation=formulation)
# this actually corresponds to order (0,-1) for the other unit cell in [Li1997]
order = (-1, -1)
R, T = sim.diffraction_efficiencies(orders=True)
t = sim.get_order(T, order)
return t, sim
def checkerboard_cellB(nh, formulation):
d = sq_size * 2**0.5
Nx = 2**9
Ny = 2**9
lattice = nn.Lattice(([d, 0], [0, d]), discretization=(Nx, Ny))
pw = nn.PlaneWave(wavelength=wavelength, angles=(0, 45, 0))
epsgrid = lattice.ones() * eps_diel
sq = lattice.square((0.5 * d, 0.5 * d), sq_size, rotate=45)
epsgrid[sq] = 1
sup = lattice.Layer("Superstrate", epsilon=eps_diel)
sub = lattice.Layer("Substrate", epsilon=1)
st = lattice.Layer("Structured", wavelength)
st.epsilon = epsgrid
# st.plot()
sim = nn.Simulation([sup, st, sub], pw, nh, formulation=formulation)
order = (0, -1)
R, T = sim.diffraction_efficiencies(orders=True)
t = sim.get_order(T, order)
return t, sim
Perform the simulation for different formulations and number of retained harmonics:
def plot_cell(sim):
axin = plt.gca().inset_axes([0.77, 0.0, 0.25, 0.25])
lay = sim.get_layer_by_name("Structured")
lay.plot(ax=axin)
axin.set_axis_off()
NH = [100, 200, 300, 400, 600]
formulations = ["original", "tangent", "pol", "jones"]
for icell, cell_fun in enumerate([checkerboard_cellA, checkerboard_cellB]):
celltype = "A" if icell == 0 else "B"
print("============================")
print(f"cell type {celltype}")
print("============================")
nhs = {f: [] for f in formulations}
ts = {f: [] for f in formulations}
times = {f: [] for f in formulations}
for nh in NH:
print("number of harmonics = ", nh)
for formulation in formulations:
t0 = -time.time()
t, sim = cell_fun(nh, formulation=formulation)
t0 += time.time()
print("formulation = ", formulation)
print(f"number of harmonics: asked={nh}, actual={sim.nh}")
print(f"elapsed time = {t0}s")
print("T(0,-1) = ", t)
print("-----------------")
nhs[formulation].append(sim.nh)
ts[formulation].append(t)
times[formulation].append(t0)
#########################################################################
# Plot the results:
markers = {"original": "^", "tangent": "o", "jones": "s", "pol": "^"}
colors = {
"original": "#d4b533",
"tangent": "#d46333",
"jones": "#3395d4",
"pol": "#54aa71",
}
plt.figure()
for formulation in formulations:
plt.plot(
nhs[formulation],
ts[formulation],
"-",
color=colors[formulation],
marker=markers[formulation],
label=formulation,
)
plt.pause(0.1)
plt.legend(loc=5, ncols=2)
plt.xlabel("number of Fourier harmonics $n_h$")
plt.ylabel("$T_{0,-1}$")
# plt.ylim(0.1255, 0.129)
plt.title(f"cell {celltype}")
plot_cell(sim)
plt.tight_layout()
plt.figure()
for formulation in formulations:
plt.plot(
nhs[formulation],
times[formulation],
"-",
color=colors[formulation],
marker=markers[formulation],
label=formulation,
)
plt.pause(0.1)
plt.yscale("log")
plt.legend(ncols=2)
plt.xlabel("number of Fourier harmonics $n_h$")
plt.ylabel("CPU time (s)")
plt.title(f"cell {celltype}")
plot_cell(sim)
plt.tight_layout()
============================
cell type A
============================
number of harmonics = 100
formulation = original
number of harmonics: asked=100, actual=97
elapsed time = 0.2108933925628662s
T(0,-1) = 0.12583443545828166
-----------------
formulation = tangent
number of harmonics: asked=100, actual=97
elapsed time = 0.3744027614593506s
T(0,-1) = 0.12805228665328253
-----------------
formulation = pol
number of harmonics: asked=100, actual=97
elapsed time = 0.32265567779541016s
T(0,-1) = 0.1280522866532768
-----------------
formulation = jones
number of harmonics: asked=100, actual=97
elapsed time = 0.357330322265625s
T(0,-1) = 0.12829938643406547
-----------------
number of harmonics = 200
formulation = original
number of harmonics: asked=200, actual=197
elapsed time = 1.0313758850097656s
T(0,-1) = 0.12677369665660498
-----------------
formulation = tangent
number of harmonics: asked=200, actual=197
elapsed time = 1.2640438079833984s
T(0,-1) = 0.12843894955158572
-----------------
formulation = pol
number of harmonics: asked=200, actual=197
elapsed time = 1.283071517944336s
T(0,-1) = 0.12843894955157578
-----------------
formulation = jones
number of harmonics: asked=200, actual=197
elapsed time = 1.3252723217010498s
T(0,-1) = 0.12874832484252707
-----------------
number of harmonics = 300
formulation = original
number of harmonics: asked=300, actual=293
elapsed time = 3.203718900680542s
T(0,-1) = 0.12700879783997474
-----------------
formulation = tangent
number of harmonics: asked=300, actual=293
elapsed time = 3.5388376712799072s
T(0,-1) = 0.1285226252519324
-----------------
formulation = pol
number of harmonics: asked=300, actual=293
elapsed time = 3.5160303115844727s
T(0,-1) = 0.12852262525194116
-----------------
formulation = jones
number of harmonics: asked=300, actual=293
elapsed time = 3.655587673187256s
T(0,-1) = 0.12868178902298358
-----------------
number of harmonics = 400
formulation = original
number of harmonics: asked=400, actual=385
elapsed time = 6.821784257888794s
T(0,-1) = 0.12725054007633896
-----------------
formulation = tangent
number of harmonics: asked=400, actual=385
elapsed time = 7.350002288818359s
T(0,-1) = 0.12854946017234567
-----------------
formulation = pol
number of harmonics: asked=400, actual=385
elapsed time = 7.318820476531982s
T(0,-1) = 0.12854946017234517
-----------------
formulation = jones
number of harmonics: asked=400, actual=385
elapsed time = 7.299178600311279s
T(0,-1) = 0.12867166821042447
-----------------
number of harmonics = 600
formulation = original
number of harmonics: asked=600, actual=593
elapsed time = 23.057771682739258s
T(0,-1) = 0.1274294323989492
-----------------
formulation = tangent
number of harmonics: asked=600, actual=593
elapsed time = 23.978371620178223s
T(0,-1) = 0.12852617563412871
-----------------
formulation = pol
number of harmonics: asked=600, actual=593
elapsed time = 24.045851469039917s
T(0,-1) = 0.12852617563411178
-----------------
formulation = jones
number of harmonics: asked=600, actual=593
elapsed time = 24.258294343948364s
T(0,-1) = 0.12865899294972083
-----------------
============================
cell type B
============================
number of harmonics = 100
formulation = original
number of harmonics: asked=100, actual=97
elapsed time = 0.3473939895629883s
T(0,-1) = 0.12638413481716906
-----------------
formulation = tangent
number of harmonics: asked=100, actual=97
elapsed time = 0.5046625137329102s
T(0,-1) = 0.12821645033384216
-----------------
formulation = pol
number of harmonics: asked=100, actual=97
elapsed time = 0.4989192485809326s
T(0,-1) = 0.12821645033383577
-----------------
formulation = jones
number of harmonics: asked=100, actual=97
elapsed time = 0.546832799911499s
T(0,-1) = 0.12812319496479768
-----------------
number of harmonics = 200
formulation = original
number of harmonics: asked=200, actual=197
elapsed time = 1.2312908172607422s
T(0,-1) = 0.12700548284814972
-----------------
formulation = tangent
number of harmonics: asked=200, actual=197
elapsed time = 1.4501445293426514s
T(0,-1) = 0.12823255616464801
-----------------
formulation = pol
number of harmonics: asked=200, actual=197
elapsed time = 1.4535293579101562s
T(0,-1) = 0.12823255616467172
-----------------
formulation = jones
number of harmonics: asked=200, actual=197
elapsed time = 1.4893431663513184s
T(0,-1) = 0.1280877717602022
-----------------
number of harmonics = 300
formulation = original
number of harmonics: asked=300, actual=293
elapsed time = 3.404618501663208s
T(0,-1) = 0.1271855413568751
-----------------
formulation = tangent
number of harmonics: asked=300, actual=293
elapsed time = 3.7003421783447266s
T(0,-1) = 0.12825634085494542
-----------------
formulation = pol
number of harmonics: asked=300, actual=293
elapsed time = 3.6907877922058105s
T(0,-1) = 0.12825634085491608
-----------------
formulation = jones
number of harmonics: asked=300, actual=293
elapsed time = 3.7421393394470215s
T(0,-1) = 0.1280935976012433
-----------------
number of harmonics = 400
formulation = original
number of harmonics: asked=400, actual=385
elapsed time = 6.827123165130615s
T(0,-1) = 0.1273177587405062
-----------------
formulation = tangent
number of harmonics: asked=400, actual=385
elapsed time = 7.293874502182007s
T(0,-1) = 0.12825655476841114
-----------------
formulation = pol
number of harmonics: asked=400, actual=385
elapsed time = 7.292124509811401s
T(0,-1) = 0.12825655476840622
-----------------
formulation = jones
number of harmonics: asked=400, actual=385
elapsed time = 7.331882953643799s
T(0,-1) = 0.12808885995392583
-----------------
number of harmonics = 600
formulation = original
number of harmonics: asked=600, actual=593
elapsed time = 22.78738760948181s
T(0,-1) = 0.12750443255815355
-----------------
formulation = tangent
number of harmonics: asked=600, actual=593
elapsed time = 23.764607191085815s
T(0,-1) = 0.12824480750865824
-----------------
formulation = pol
number of harmonics: asked=600, actual=593
elapsed time = 24.001410484313965s
T(0,-1) = 0.12824480750859457
-----------------
formulation = jones
number of harmonics: asked=600, actual=593
elapsed time = 24.60649538040161s
T(0,-1) = 0.12808282495649406
-----------------
Total running time of the script: (4 minutes 53.901 seconds)
Estimated memory usage: 3305 MB