.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/basic/plot_convergence.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_basic_plot_convergence.py: Convergence =========== Convergence of the various FMM formulations. .. GENERATED FROM PYTHON SOURCE LINES 15-24 .. code-block:: Python import time import matplotlib.pyplot as plt import nannos as nn bk = nn.backend .. GENERATED FROM PYTHON SOURCE LINES 25-28 We will study the convergence on a benchmark case from :cite:p:`Li1997`. First we define the main function that performs the simulation. .. GENERATED FROM PYTHON SOURCE LINES 28-82 .. code-block:: Python 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 .. GENERATED FROM PYTHON SOURCE LINES 83-85 Perform the simulation for different formulations and number of retained harmonics: .. GENERATED FROM PYTHON SOURCE LINES 85-174 .. code-block:: Python 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() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/basic/images/sphx_glr_plot_convergence_001.png :alt: cell A :srcset: /examples/basic/images/sphx_glr_plot_convergence_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/basic/images/sphx_glr_plot_convergence_002.png :alt: cell A :srcset: /examples/basic/images/sphx_glr_plot_convergence_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/basic/images/sphx_glr_plot_convergence_003.png :alt: cell B :srcset: /examples/basic/images/sphx_glr_plot_convergence_003.png :class: sphx-glr-multi-img * .. image-sg:: /examples/basic/images/sphx_glr_plot_convergence_004.png :alt: cell B :srcset: /examples/basic/images/sphx_glr_plot_convergence_004.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none ============================ cell type A ============================ number of harmonics = 100 formulation = original number of harmonics: asked=100, actual=97 elapsed time = 0.21494340896606445s T(0,-1) = 0.12583443545828166 ----------------- formulation = tangent number of harmonics: asked=100, actual=97 elapsed time = 0.39380860328674316s T(0,-1) = 0.12805228665328253 ----------------- formulation = pol number of harmonics: asked=100, actual=97 elapsed time = 0.3573622703552246s T(0,-1) = 0.1280522866532768 ----------------- formulation = jones number of harmonics: asked=100, actual=97 elapsed time = 0.37837839126586914s T(0,-1) = 0.12829938643406547 ----------------- number of harmonics = 200 formulation = original number of harmonics: asked=200, actual=197 elapsed time = 1.0715365409851074s T(0,-1) = 0.12677369665660498 ----------------- formulation = tangent number of harmonics: asked=200, actual=197 elapsed time = 1.2821485996246338s T(0,-1) = 0.12843894955158572 ----------------- formulation = pol number of harmonics: asked=200, actual=197 elapsed time = 1.3130955696105957s T(0,-1) = 0.12843894955157578 ----------------- formulation = jones number of harmonics: asked=200, actual=197 elapsed time = 1.3471274375915527s T(0,-1) = 0.12874832484252707 ----------------- number of harmonics = 300 formulation = original number of harmonics: asked=300, actual=293 elapsed time = 3.2607803344726562s T(0,-1) = 0.12700879783997474 ----------------- formulation = tangent number of harmonics: asked=300, actual=293 elapsed time = 3.6047160625457764s T(0,-1) = 0.1285226252519324 ----------------- formulation = pol number of harmonics: asked=300, actual=293 elapsed time = 3.5516562461853027s T(0,-1) = 0.12852262525194116 ----------------- formulation = jones number of harmonics: asked=300, actual=293 elapsed time = 3.636275291442871s T(0,-1) = 0.12868178902298358 ----------------- number of harmonics = 400 formulation = original number of harmonics: asked=400, actual=385 elapsed time = 6.854674577713013s T(0,-1) = 0.12725054007633896 ----------------- formulation = tangent number of harmonics: asked=400, actual=385 elapsed time = 7.398108959197998s T(0,-1) = 0.12854946017234567 ----------------- formulation = pol number of harmonics: asked=400, actual=385 elapsed time = 7.309309005737305s T(0,-1) = 0.12854946017234517 ----------------- formulation = jones number of harmonics: asked=400, actual=385 elapsed time = 7.3898704051971436s T(0,-1) = 0.12867166821042447 ----------------- number of harmonics = 600 formulation = original number of harmonics: asked=600, actual=593 elapsed time = 23.1548171043396s T(0,-1) = 0.1274294323989492 ----------------- formulation = tangent number of harmonics: asked=600, actual=593 elapsed time = 24.253308534622192s T(0,-1) = 0.12852617563412871 ----------------- formulation = pol number of harmonics: asked=600, actual=593 elapsed time = 24.071086168289185s T(0,-1) = 0.12852617563411178 ----------------- formulation = jones number of harmonics: asked=600, actual=593 elapsed time = 24.374499559402466s T(0,-1) = 0.12865899294972083 ----------------- ============================ cell type B ============================ number of harmonics = 100 formulation = original number of harmonics: asked=100, actual=97 elapsed time = 0.3592796325683594s T(0,-1) = 0.12638413481716906 ----------------- formulation = tangent number of harmonics: asked=100, actual=97 elapsed time = 0.5035815238952637s T(0,-1) = 0.12821645033384216 ----------------- formulation = pol number of harmonics: asked=100, actual=97 elapsed time = 0.533735990524292s T(0,-1) = 0.12821645033383577 ----------------- formulation = jones number of harmonics: asked=100, actual=97 elapsed time = 0.5749943256378174s T(0,-1) = 0.12812319496479768 ----------------- number of harmonics = 200 formulation = original number of harmonics: asked=200, actual=197 elapsed time = 1.2429182529449463s T(0,-1) = 0.12700548284814972 ----------------- formulation = tangent number of harmonics: asked=200, actual=197 elapsed time = 1.4730887413024902s T(0,-1) = 0.12823255616464801 ----------------- formulation = pol number of harmonics: asked=200, actual=197 elapsed time = 1.460988998413086s T(0,-1) = 0.12823255616467172 ----------------- formulation = jones number of harmonics: asked=200, actual=197 elapsed time = 1.5093660354614258s T(0,-1) = 0.1280877717602022 ----------------- number of harmonics = 300 formulation = original number of harmonics: asked=300, actual=293 elapsed time = 3.42385196685791s T(0,-1) = 0.1271855413568751 ----------------- formulation = tangent number of harmonics: asked=300, actual=293 elapsed time = 3.727818489074707s T(0,-1) = 0.12825634085494542 ----------------- formulation = pol number of harmonics: asked=300, actual=293 elapsed time = 3.724989891052246s T(0,-1) = 0.12825634085491608 ----------------- formulation = jones number of harmonics: asked=300, actual=293 elapsed time = 3.7718183994293213s T(0,-1) = 0.1280935976012433 ----------------- number of harmonics = 400 formulation = original number of harmonics: asked=400, actual=385 elapsed time = 6.924372911453247s T(0,-1) = 0.1273177587405062 ----------------- formulation = tangent number of harmonics: asked=400, actual=385 elapsed time = 7.3615710735321045s T(0,-1) = 0.12825655476841114 ----------------- formulation = pol number of harmonics: asked=400, actual=385 elapsed time = 7.388616323471069s T(0,-1) = 0.12825655476840622 ----------------- formulation = jones number of harmonics: asked=400, actual=385 elapsed time = 7.488894462585449s T(0,-1) = 0.12808885995392583 ----------------- number of harmonics = 600 formulation = original number of harmonics: asked=600, actual=593 elapsed time = 23.081909894943237s T(0,-1) = 0.12750443255815355 ----------------- formulation = tangent number of harmonics: asked=600, actual=593 elapsed time = 24.58843183517456s T(0,-1) = 0.12824480750865824 ----------------- formulation = pol number of harmonics: asked=600, actual=593 elapsed time = 23.93336820602417s T(0,-1) = 0.12824480750859457 ----------------- formulation = jones number of harmonics: asked=600, actual=593 elapsed time = 24.058025360107422s T(0,-1) = 0.12808282495649406 ----------------- .. rst-class:: sphx-glr-timing **Total running time of the script:** (4 minutes 56.068 seconds) **Estimated memory usage:** 3247 MB .. _sphx_glr_download_examples_basic_plot_convergence.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/nannos/nannos.gitlab.io/doc?filepath=notebooks/examples/basic/plot_convergence.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_convergence.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_convergence.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_convergence.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_