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 A
  • cell A
  • cell B
  • cell B
============================
cell type A
============================
number of harmonics =  100
formulation =  original
number of harmonics: asked=100, actual=97
elapsed time = 0.16234850883483887s
T(0,-1) =  0.12583443545828732
-----------------
formulation =  tangent
number of harmonics: asked=100, actual=97
elapsed time = 0.38002657890319824s
T(0,-1) =  0.1280522866532793
-----------------
formulation =  pol
number of harmonics: asked=100, actual=97
elapsed time = 0.29866957664489746s
T(0,-1) =  0.128052286653268
-----------------
formulation =  jones
number of harmonics: asked=100, actual=97
elapsed time = 0.32566332817077637s
T(0,-1) =  0.12829938643405658
-----------------
number of harmonics =  200
formulation =  original
number of harmonics: asked=200, actual=197
elapsed time = 1.294370412826538s
T(0,-1) =  0.1267736966566059
-----------------
formulation =  tangent
number of harmonics: asked=200, actual=197
elapsed time = 1.489173173904419s
T(0,-1) =  0.1284389495515869
-----------------
formulation =  pol
number of harmonics: asked=200, actual=197
elapsed time = 1.4316017627716064s
T(0,-1) =  0.12843894955158688
-----------------
formulation =  jones
number of harmonics: asked=200, actual=197
elapsed time = 1.475632905960083s
T(0,-1) =  0.12874832484252968
-----------------
number of harmonics =  300
formulation =  original
number of harmonics: asked=300, actual=293
elapsed time = 3.243748188018799s
T(0,-1) =  0.12700879783995656
-----------------
formulation =  tangent
number of harmonics: asked=300, actual=293
elapsed time = 3.5353994369506836s
T(0,-1) =  0.12852262525193675
-----------------
formulation =  pol
number of harmonics: asked=300, actual=293
elapsed time = 3.5288069248199463s
T(0,-1) =  0.12852262525193467
-----------------
formulation =  jones
number of harmonics: asked=300, actual=293
elapsed time = 3.630852222442627s
T(0,-1) =  0.12868178902297692
-----------------
number of harmonics =  400
formulation =  original
number of harmonics: asked=400, actual=385
elapsed time = 8.174145698547363s
T(0,-1) =  0.12725054007633035
-----------------
formulation =  tangent
number of harmonics: asked=400, actual=385
elapsed time = 8.266616106033325s
T(0,-1) =  0.12854946017234484
-----------------
formulation =  pol
number of harmonics: asked=400, actual=385
elapsed time = 8.598104000091553s
T(0,-1) =  0.12854946017234717
-----------------
formulation =  jones
number of harmonics: asked=400, actual=385
elapsed time = 8.470607995986938s
T(0,-1) =  0.12867166821042042
-----------------
number of harmonics =  600
formulation =  original
number of harmonics: asked=600, actual=593
elapsed time = 30.996415853500366s
T(0,-1) =  0.1274294323989178
-----------------
formulation =  tangent
number of harmonics: asked=600, actual=593
elapsed time = 30.444187879562378s
T(0,-1) =  0.12852617563412577
-----------------
formulation =  pol
number of harmonics: asked=600, actual=593
elapsed time = 32.32937836647034s
T(0,-1) =  0.12852617563411897
-----------------
formulation =  jones
number of harmonics: asked=600, actual=593
elapsed time = 32.36756253242493s
T(0,-1) =  0.12865899294967859
-----------------
============================
cell type B
============================
number of harmonics =  100
formulation =  original
number of harmonics: asked=100, actual=97
elapsed time = 0.2957303524017334s
T(0,-1) =  0.12638413481714725
-----------------
formulation =  tangent
number of harmonics: asked=100, actual=97
elapsed time = 0.4401991367340088s
T(0,-1) =  0.12821645033384166
-----------------
formulation =  pol
number of harmonics: asked=100, actual=97
elapsed time = 0.46117520332336426s
T(0,-1) =  0.12821645033383877
-----------------
formulation =  jones
number of harmonics: asked=100, actual=97
elapsed time = 0.4781177043914795s
T(0,-1) =  0.12812319496479274
-----------------
number of harmonics =  200
formulation =  original
number of harmonics: asked=200, actual=197
elapsed time = 1.1963269710540771s
T(0,-1) =  0.12700548284814542
-----------------
formulation =  tangent
number of harmonics: asked=200, actual=197
elapsed time = 1.6700668334960938s
T(0,-1) =  0.12823255616464457
-----------------
formulation =  pol
number of harmonics: asked=200, actual=197
elapsed time = 1.4159190654754639s
T(0,-1) =  0.12823255616465107
-----------------
formulation =  jones
number of harmonics: asked=200, actual=197
elapsed time = 1.4853930473327637s
T(0,-1) =  0.12808777176019862
-----------------
number of harmonics =  300
formulation =  original
number of harmonics: asked=300, actual=293
elapsed time = 3.9687180519104004s
T(0,-1) =  0.12718554135686563
-----------------
formulation =  tangent
number of harmonics: asked=300, actual=293
elapsed time = 4.29416036605835s
T(0,-1) =  0.12825634085494536
-----------------
formulation =  pol
number of harmonics: asked=300, actual=293
elapsed time = 4.139264822006226s
T(0,-1) =  0.12825634085494877
-----------------
formulation =  jones
number of harmonics: asked=300, actual=293
elapsed time = 4.333812952041626s
T(0,-1) =  0.12809359760124372
-----------------
number of harmonics =  400
formulation =  original
number of harmonics: asked=400, actual=385
elapsed time = 8.171222925186157s
T(0,-1) =  0.12731775874049386
-----------------
formulation =  tangent
number of harmonics: asked=400, actual=385
elapsed time = 8.840601205825806s
T(0,-1) =  0.12825655476841305
-----------------
formulation =  pol
number of harmonics: asked=400, actual=385
elapsed time = 8.926902770996094s
T(0,-1) =  0.1282565547684097
-----------------
formulation =  jones
number of harmonics: asked=400, actual=385
elapsed time = 9.400936603546143s
T(0,-1) =  0.12808885995391947
-----------------
number of harmonics =  600
formulation =  original
number of harmonics: asked=600, actual=593
elapsed time = 31.018673419952393s
T(0,-1) =  0.1275044325581457
-----------------
formulation =  tangent
number of harmonics: asked=600, actual=593
elapsed time = 32.60936188697815s
T(0,-1) =  0.1282448075086175
-----------------
formulation =  pol
number of harmonics: asked=600, actual=593
elapsed time = 35.93264150619507s
T(0,-1) =  0.12824480750861617
-----------------
formulation =  jones
number of harmonics: asked=600, actual=593
elapsed time = 36.11417269706726s
T(0,-1) =  0.1280828249565236
-----------------

Total running time of the script: (6 minutes 23.015 seconds)

Estimated memory usage: 4762 MB

Gallery generated by Sphinx-Gallery