{ "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# Tangent field\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import importlib\nimport time\n\nimport matplotlib.pyplot as plt\n\nimport nannos as nn\nfrom nannos.formulations.tangent import get_tangent_field" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will generate a field tangent to the material interface\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "nh = 1500\nlattice = nn.Lattice(([1, 0], [0, 1]), discretization=2**9)\n\nx, y = lattice.grid\ncirc = lattice.circle((0.3, 0.3), 0.25)\nrect = lattice.rectangle((0.7, 0.7), (0.2, 0.5))\ngrid = lattice.ones() * (3 + 0.01j)\ngrid[circ] = 1\ngrid[rect] = 1\n\nst = lattice.Layer(\"pat\", thickness=1, epsilon=grid)\nlays = [lattice.Layer(\"sup\"), st, lattice.Layer(\"sub\")]\npw = nn.PlaneWave(wavelength=1 / 1.2)\nsim = nn.Simulation(lays, pw, nh)\n\ndsp = 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "FFT version\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t0 = -time.time()\nt = get_tangent_field(grid, sim.harmonics, normalize=False, type=\"fft\")\nt0 += time.time()\nprint(f\"Elapsed time {t0:.4f}s\")\n\n\nplt.figure()\nst.plot()\nplt.quiver(\n x[::dsp, ::dsp],\n y[::dsp, ::dsp],\n t[0][::dsp, ::dsp],\n t[1][::dsp, ::dsp],\n scale=20,\n)\nplt.axis(\"scaled\")\n_ = plt.axis(\"off\")\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Optimized version\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t0 = -time.time()\ntopt = get_tangent_field(grid, sim.harmonics, normalize=False, type=\"opt\", maxiter=1)\nt0 += time.time()\nprint(f\"Elapsed time {t0:.4f}s\")\n\nplt.figure()\nst.plot()\nplt.quiver(\n x[::dsp, ::dsp],\n y[::dsp, ::dsp],\n topt[0][::dsp, ::dsp],\n topt[1][::dsp, ::dsp],\n scale=20,\n)\nplt.axis(\"scaled\")\n_ = plt.axis(\"off\")\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Optimized version (normalized)\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t0 = -time.time()\ntopt = get_tangent_field(grid, sim.harmonics, normalize=True, type=\"opt\", maxiter=1)\nt0 += time.time()\nprint(f\"Elapsed time {t0:.4f}s\")\n\nplt.figure()\nst.plot()\nplt.quiver(\n x[::dsp, ::dsp],\n y[::dsp, ::dsp],\n topt[0][::dsp, ::dsp],\n topt[1][::dsp, ::dsp],\n scale=20,\n)\nplt.axis(\"scaled\")\n_ = plt.axis(\"off\")\nplt.show()" ] }, { "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.12.5" } }, "nbformat": 4, "nbformat_minor": 0 }