{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# This file is part of nannos\n# License: GPLv3\n%matplotlib notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Convergence\n\nConvergence of the various FMM formulations.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nimport nannos as nn" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will study the convergence on a benchmark case from\n:cite:p:`Li1997`.\nFirst we define the main function that performs the simulation.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def checkerboard(nh, formulation):\n la = 1\n d = 2 * 1.25 * la\n Nx = 2**9\n Ny = 2**9\n lattice = nn.Lattice(([d, 0], [0, d]), discretization=(Nx, Ny))\n pw = nn.PlaneWave(wavelength=la, angles=(0, 0, 0))\n epsgrid = lattice.ones() * 2.25\n sq1 = lattice.square((0.25 * d, 0.25 * d), 0.5 * d)\n sq2 = lattice.square((0.75 * d, 0.75 * d), 0.5 * d)\n epsgrid[sq1] = 1\n epsgrid[sq2] = 1\n\n sup = lattice.Layer(\"Superstrate\", epsilon=2.25)\n sub = lattice.Layer(\"Substrate\", epsilon=1)\n st = lattice.Layer(\"Structured\", la)\n st.epsilon = epsgrid\n\n sim = nn.Simulation([sup, st, sub], pw, nh, formulation=formulation)\n order = (\n -1,\n -1,\n ) # this actually corresponds to order (0,-1) for the other unit cell in [Li1997]\n R, T = sim.diffraction_efficiencies(orders=True)\n t = sim.get_order(T, order)\n return R, T, t, sim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perform the simulation for different formulations and number\nof retained harmonics:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "NH = [100, 200, 300, 400, 600]\nformulations = [\"original\", \"tangent\", \"pol\", \"jones\"]\nnhs = {f: [] for f in formulations}\nts = {f: [] for f in formulations}\n\n\nfor nh in NH:\n print(\"============================\")\n print(\"number of harmonics = \", nh)\n print(\"============================\")\n\n for formulation in formulations:\n Ri, Ti, t, sim = checkerboard(nh, formulation=formulation)\n R = np.sum(Ri)\n T = np.sum(Ti)\n print(\"formulation = \", formulation)\n print(\"nh0 = \", nh)\n print(\"nh = \", sim.nh)\n print(\"t = \", t)\n print(\"R = \", R)\n print(\"T = \", T)\n print(\"R+T = \", R + T)\n print(\"-----------------\")\n nhs[formulation].append(sim.nh)\n ts[formulation].append(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot the results:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "markers = {\"original\": \"^\", \"tangent\": \"o\", \"jones\": \"s\", \"pol\": \"^\"}\ncolors = {\n \"original\": \"#d4b533\",\n \"tangent\": \"#d46333\",\n \"jones\": \"#3395d4\",\n \"pol\": \"#54aa71\",\n}\n\nfor formulation in formulations:\n plt.plot(\n nhs[formulation],\n ts[formulation],\n \"-\",\n color=colors[formulation],\n marker=markers[formulation],\n label=formulation,\n )\n plt.pause(0.1)\nplt.legend()\nplt.xlabel(\"number of Fourier harmonics $n_h$\")\nplt.ylabel(\"$T_{0,-1}$\")\nplt.ylim(0.1255, 0.129)\nplt.tight_layout()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import nannos.utils.jupyter\n%nannos_version_table" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 0 }