diff --git a/AdvancedGaussianSources.ipynb b/AdvancedGaussianSources.ipynb new file mode 100644 index 00000000..428fe15b --- /dev/null +++ b/AdvancedGaussianSources.ipynb @@ -0,0 +1,1816 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "6b6287fd-21a5-4276-a199-b040bb171541", + "metadata": {}, + "source": [ + "# Advanced Gaussian sources\n", + "\n", + "Tidy3D natively offers sources like ModeSource, PlaneWave, GaussianBeam, etc. However, due to the python and open-source nature of Tidy3D, we can easily define arbitrary sources such as vortex beams. In this notebook, we demonstrate how to create these advanced sources in a way that is easily usable in multiple scenarios.\n", + "

\n", + "It can often be tedious to create easily usable customized simulation objects from scratch, as there are various details to consider. For instance, if one wants to create an easily-usable beam source from scratch, one must create functionality to automatically interpret the source plane into field coordinates, handle conversion for polar and azimuthal angles, handle different direction specifications, etc. This all becomes very complicated to build, so there is often a tradeoff between ease of construction and ease of use.\n", + "

\n", + "Fortunately, we can take advantage of the fact that Tidy3D's [Python frontend](https://github.com/flexcompute/tidy3d/tree/981c0ebb22ba4dd317818ebc3729edb1e0005f2f) is open source is open source to create complex Gaussian sources without needing to consider any of these extraneous functionalities. Namely, we use the Tidy3D [BeamProfile](https://github.com/flexcompute/tidy3d/blob/develop/tidy3d/components/beam.py) class to handle all extra usability considerations, so we only need to define the scalar field profile for the propagation direction. The BeamProfile object will then automatically create, interpolate, and rotate the field amplitudes according to the geometry of the source plane and specified angles.\n", + "

\n", + "This is just one way in which we can take advantage of the open source nature of Tidy3D, and these principles can be applied to any base object class that Tidy3D provides." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "0e1ed43f-e352-4b82-a2e8-c1d3fe23e7de", + "metadata": {}, + "outputs": [], + "source": [ + "# Standard python imports\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "\n", + "# Tidy3D import\n", + "import tidy3d as td\n", + "from tidy3d import web" + ] + }, + { + "cell_type": "markdown", + "id": "4b12ff8b-18e5-41a8-a62b-5165050f14b9", + "metadata": {}, + "source": [ + "If we look at how Tidy3D's GaussianBeam uses the abstract BeamProfile class, we see that the BeamProfile class already handles polarization angles, propagation angles, sourcetimes, direction, and can interpret user-defined source plane geometry. Thus we need only define the extra parameters our advanced Gaussian sources need on top of these. With each source, we will demonstrate its use after defining it as an object." + ] + }, + { + "cell_type": "markdown", + "id": "c1e27425-c9f0-47f5-84e1-77003ed6eb19", + "metadata": {}, + "source": [ + "## Hermite-Gaussian source\n", + "\n", + "Here we show how to create a Hermite-Gaussian mode in Tidy3D using our [CustomFieldSource](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.CustomFieldSource.html) object. We then validate the field profile change along the propagation direction.

\n", + "In cartesian coordinates, where $x$ is the propagation direction, the mode has field profile\n", + "\n", + "
$E_{l,m}(x,y,z)=E_0\\frac{\\omega_0}{\\omega(z)}H_l\\lparen\\frac{\\sqrt{2}x}{\\omega(z)}\\rparen H_m\\lparen\\frac{\\sqrt{2}y}{\\omega(z)}\\rparen\\exp\\lparen-\\frac{x^2+y^2}{\\omega^2(z)}\\rparen\\exp\\lparen-i\\frac{k(x^2+y^2)}{2R(z)}\\rparen\\exp\\lparen i\\psi(x)\\rparen\\exp\\lparen -ikz\\rparen$
\n", + "\n", + "where:\n", + "
  • $\\omega_0$ is the beam waist
  • \n", + "
  • $z_R=\\frac{\\pi n_\\epsilon\\omega_0^2}{\\lambda}$ is the Rayleigh length
  • \n", + "
  • $\\omega(z)=\\omega_0\\sqrt{1+\\frac{z^2}{z_R^2}}$ is the beam radius
  • \n", + "
  • $k=\\frac{2\\pi n}{\\lambda}$ is the wavenumber
  • \n", + "
  • $R(z)=z(1+\\frac{z_R^2}{z^2})$ is the wavefront curvature
  • \n", + "
  • $H_l,H_m$ are Hermite polynomials of orders $l$ and $m$
  • \n", + "
  • $\\psi(z)=(l+m+1)\\arctan\\lparen\\frac{z}{z_R}\\rparen$ is the phase function
  • " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "04a1703d-b15d-4894-8906-1ae849048656", + "metadata": {}, + "outputs": [], + "source": [ + "from scipy.special import hermite\n", + "from tidy3d.components.beam import BeamProfile\n", + "\n", + "\n", + "def HGM(\n", + " size,\n", + " m,\n", + " l,\n", + " source_time,\n", + " center=(0, 0, 0),\n", + " direction=\"+\",\n", + " pol_angle=0,\n", + " angle_phi=0,\n", + " angle_theta=0,\n", + " w0=1,\n", + " waist_distance=0,\n", + "):\n", + " freqs = [source_time.freq0]\n", + "\n", + " class HGM_obj(BeamProfile):\n", + " \"\"\"Component for constructing HGM beam data. The normal direction is implicitly\n", + " defined by the ``size`` parameter.\n", + " \"\"\"\n", + "\n", + " def scalar_field(self, points, background_n):\n", + " \"\"\"Scalar field for HGM beam.\n", + " Scalar field corresponding to the analytic beam in coordinate system such that the\n", + " propagation direction is z and the ``E``-field is entirely ``x``-polarized. The field is\n", + " computed on an unstructured array ``points`` of shape ``(3, ...)``.\n", + " \"\"\"\n", + " x, y, z = points\n", + " z += waist_distance\n", + "\n", + " zR = np.pi * background_n * w0**2 / (td.C_0 / np.array(self.freqs))\n", + " wz = w0 * np.sqrt(1 + z**2 / zR**2)\n", + " Rz = td.inf if np.any(z == 0) else z * (1 + zR**2 / z**2)\n", + " k = 2 * np.pi * np.array(self.freqs) / td.C_0 * background_n\n", + " psi_z = (l + m + 1) * np.arctan(z / zR)\n", + "\n", + " E = source_time.amplitude * w0 / wz\n", + " E *= hermite(l)(np.real(np.sqrt(2) * x / wz)) * hermite(m)(np.real(np.sqrt(2) * y / wz))\n", + " E *= np.exp(-(x**2 + y**2) / wz**2) * np.exp(-1j * k * (x**2 + y**2) / (2 * Rz))\n", + " E *= np.exp(1j * psi_z) * np.exp(-1j * k * z)\n", + "\n", + " return E\n", + "\n", + " HGM_obj = HGM_obj(\n", + " center=center,\n", + " size=size,\n", + " freqs=freqs,\n", + " angle_theta=angle_theta,\n", + " angle_phi=angle_phi,\n", + " pol_angle=pol_angle,\n", + " direction=direction,\n", + " )\n", + " field_data = HGM_obj.field_data\n", + " field_dataset = td.FieldDataset(\n", + " Ex=field_data.Ex,\n", + " Ey=field_data.Ey,\n", + " Ez=field_data.Ez,\n", + " Hx=field_data.Hx,\n", + " Hy=field_data.Hy,\n", + " Hz=field_data.Hz,\n", + " )\n", + " HGM_source = td.CustomFieldSource(\n", + " center=center, size=size, source_time=source_time, field_dataset=field_dataset\n", + " )\n", + " return HGM_source" + ] + }, + { + "cell_type": "markdown", + "id": "cef6fc6c-0be7-463a-956e-cec83322296f", + "metadata": {}, + "source": [ + "Create and run an example $m=2,l=1$ Hermite-Gaussian beam. To ensure that the BeamProfile object is working correctly, we specify extra angles and a non-unity beam waist:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "7d5d3f36-fb9e-489b-9d9a-e715d7ad131f", + "metadata": {}, + "outputs": [], + "source": [ + "freq0 = td.C_0 / 1.55\n", + "hgm = HGM(\n", + " size=(0, 4, 4),\n", + " m=2,\n", + " l=1,\n", + " w0=1.2,\n", + " pol_angle=np.pi / 24,\n", + " angle_phi=np.pi / 36,\n", + " source_time=td.GaussianPulse(freq0=freq0, fwidth=freq0 / 10),\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "47a6a1cd-10c3-4abd-8ca6-fb63a62f1197", + "metadata": {}, + "outputs": [], + "source": [ + "sim_size = (5.1, 5, 5)\n", + "sim_center = (sim_size[0] / 2 - 0.05, 0, 0)\n", + "\n", + "num_field_monitors = 61\n", + "field_monitor_xs = np.linspace(\n", + " sim_center[0] - sim_size[0] / 2 + 0.1, sim_size[0] - 0.1, num_field_monitors\n", + ")\n", + "field_monitors = []\n", + "for x in field_monitor_xs:\n", + " field_monitor = td.FieldMonitor(\n", + " center=(x, 0, 0), size=(0, sim_size[1], sim_size[2]), name=str(x), freqs=[freq0]\n", + " )\n", + " field_monitors.append(field_monitor)\n", + "\n", + "HGM_test_sim = td.Simulation(\n", + " center=sim_center,\n", + " size=sim_size,\n", + " structures=[],\n", + " sources=[hgm],\n", + " monitors=field_monitors,\n", + " run_time=1e-13,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "8e8b8e23-55a4-4ac8-9520-9f4baa0209e1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    16:42:01 EDT Created task 'HGM test' with task_id                               \n",
    +       "             'fdve-cec2fdd6-bb37-4688-be8e-2fe5d141cd30' and task_type 'FDTD'.  \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:01 EDT\u001b[0m\u001b[2;36m \u001b[0mCreated task \u001b[32m'HGM test'\u001b[0m with task_id \n", + "\u001b[2;36m \u001b[0m\u001b[32m'fdve-cec2fdd6-bb37-4688-be8e-2fe5d141cd30'\u001b[0m and task_type \u001b[32m'FDTD'\u001b[0m. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 View task using web UI at                                          \n",
    +       "             'https://tidy3d.simulation.cloud/workbench?taskId=fdve-cec2fdd6-bb3\n",
    +       "             7-4688-be8e-2fe5d141cd30'.                                         \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mView task using web UI at \n", + "\u001b[2;36m \u001b[0m\u001b]8;id=276143;https://tidy3d.simulation.cloud/workbench?taskId=fdve-cec2fdd6-bb37-4688-be8e-2fe5d141cd30\u001b\\\u001b[32m'https://tidy3d.simulation.cloud/workbench?\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=672000;https://tidy3d.simulation.cloud/workbench?taskId=fdve-cec2fdd6-bb37-4688-be8e-2fe5d141cd30\u001b\\\u001b[32mtaskId\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=276143;https://tidy3d.simulation.cloud/workbench?taskId=fdve-cec2fdd6-bb37-4688-be8e-2fe5d141cd30\u001b\\\u001b[32m=\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=210805;https://tidy3d.simulation.cloud/workbench?taskId=fdve-cec2fdd6-bb37-4688-be8e-2fe5d141cd30\u001b\\\u001b[32mfdve\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=276143;https://tidy3d.simulation.cloud/workbench?taskId=fdve-cec2fdd6-bb37-4688-be8e-2fe5d141cd30\u001b\\\u001b[32m-cec2fdd6-bb3\u001b[0m\u001b]8;;\u001b\\\n", + "\u001b[2;36m \u001b[0m\u001b]8;id=276143;https://tidy3d.simulation.cloud/workbench?taskId=fdve-cec2fdd6-bb37-4688-be8e-2fe5d141cd30\u001b\\\u001b[32m7-4688-be8e-2fe5d141cd30'\u001b[0m\u001b]8;;\u001b\\. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 Task folder: 'default'.                                            \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mTask folder: \u001b]8;id=405008;https://tidy3d.simulation.cloud/folders/ee7143f0-3861-47de-ae8f-8f55df7232f1\u001b\\\u001b[32m'default'\u001b[0m\u001b]8;;\u001b\\. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "43e426ca17fb4143991fdce94b96e69a", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    \n",
    +       "
    \n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:05 EDT Maximum FlexCredit cost: 0.025. Minimum cost depends on task       \n",
    +       "             execution details. Use 'web.real_cost(task_id)' to get the billed  \n",
    +       "             FlexCredit cost after a simulation run.                            \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:05 EDT\u001b[0m\u001b[2;36m \u001b[0mMaximum FlexCredit cost: \u001b[1;36m0.025\u001b[0m. Minimum cost depends on task \n", + "\u001b[2;36m \u001b[0mexecution details. Use \u001b[32m'web.real_cost\u001b[0m\u001b[32m(\u001b[0m\u001b[32mtask_id\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m to get the billed \n", + "\u001b[2;36m \u001b[0mFlexCredit cost after a simulation run. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:06 EDT status = success                                                   \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:06 EDT\u001b[0m\u001b[2;36m \u001b[0mstatus = success \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "07d747945c1d4fdbbe7f7a26aacd9fb8", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    \n",
    +       "
    \n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:07 EDT loading simulation from simulation_data.hdf5                       \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:07 EDT\u001b[0m\u001b[2;36m \u001b[0mloading simulation from simulation_data.hdf5 \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "hgm_sim_data = web.run(simulation=HGM_test_sim, task_name=\"HGM test\")" + ] + }, + { + "cell_type": "markdown", + "id": "23e278a7-fe24-4b06-9b87-d0cab75b499a", + "metadata": {}, + "source": [ + "Display field in propagation direction:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "58e9385b-a077-4044-9cec-763236d28a51", + "metadata": { + "editable": true, + "slideshow": { + "slide_type": "" + }, + "tags": [] + }, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "1d6efaa1ea2c48aa83955629851d003c", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(FloatSlider(value=0.05000000000000018, description='Ey', max=5.0, min=0.0500000000000001…" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import ipywidgets as widgets\n", + "\n", + "component = \"Ey\"\n", + "\n", + "field_slider = widgets.FloatSlider(\n", + " value=0,\n", + " min=field_monitor_xs[0],\n", + " max=field_monitor_xs[-1],\n", + " step=None,\n", + " description=component,\n", + " disabled=False,\n", + " continuous_update=True,\n", + " orientation=\"horizontal\",\n", + " readout=True,\n", + " readout_format=\".3f\",\n", + ")\n", + "\n", + "\n", + "def update_plot(x):\n", + " if x not in field_monitor_xs:\n", + " x = min(field_monitor_xs, key=lambda ll: abs(ll - x))\n", + " hgm_sim_data.plot_field(str(x), component, f=freq0)\n", + " plt.show()\n", + "\n", + "\n", + "widgets.interactive(update_plot, x=field_slider)" + ] + }, + { + "cell_type": "markdown", + "id": "7211088d-c600-47aa-9ee3-67abb636a2b0", + "metadata": {}, + "source": [ + "## Laguerre-Gaussian source\n", + "\n", + "Here we show how to create an Orbital Angular Momentum (OAM) beam in Tidy3D using our [CustomFieldSource](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.CustomFieldSource.html) object. We then validate the field profile change along the propagation direction.

    \n", + "In cylindrical coordinates, where $z$ is the propagation direction, the OAM has field profile\n", + "\n", + "
    $E_{l,p}(r,\\phi,z)=E_0\\sqrt{\\frac{2p!}{\\pi(p+|l|)!}} \\frac{1}{\\omega(z)} \\lparen\\frac{r\\sqrt{2}}{\\omega(z)}\\rparen^{|l|} \\exp\\lparen-\\frac{r^2}{\\omega^2(z)}\\rparen L_p^{|l|}\\lparen\\frac{2r^2}{\\omega^2(z)}\\rparen \\exp\\lparen-ik\\frac{r^2}{2R(z)}\\rparen \\exp\\lparen-il\\phi\\rparen \\exp\\lparen i\\psi(z)\\rparen$
    \n", + "\n", + "where:\n", + "
  • $\\omega_0$ is the beam waist
  • \n", + "
  • $z_R=\\frac{\\pi n_\\epsilon\\omega_0^2}{\\lambda}$ is the Rayleigh length
  • \n", + "
  • $\\omega(z)=\\omega_0\\sqrt{1+\\frac{z^2}{z_R^2}}$ is the beam radius
  • \n", + "
  • $R(z)=z(1+\\frac{z_R^2}{z^2})$ is the wavefront curvature
  • \n", + "
  • $k=\\frac{2\\pi n}{\\lambda}$ is the wavenumber
  • \n", + "
  • $L_p^{|l|}$ is the generalized Laguerre polynomial
  • \n", + "
  • $\\psi(z)=(|l| + 2p + 1)\\arctan\\lparen\\frac{z}{z_R}\\rparen$ is the phase function
  • " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ec84e602-efee-43d1-a39c-d58d12f5fa59", + "metadata": {}, + "outputs": [], + "source": [ + "from scipy.special import factorial, genlaguerre\n", + "\n", + "\n", + "def OAM(\n", + " size,\n", + " p,\n", + " l,\n", + " source_time,\n", + " center=(0, 0, 0),\n", + " direction=\"+\",\n", + " pol_angle=0,\n", + " angle_phi=0,\n", + " angle_theta=0,\n", + " w0=1,\n", + " waist_distance=0,\n", + "):\n", + " freqs = [source_time.freq0]\n", + "\n", + " class OAM_obj(BeamProfile):\n", + " \"\"\"Component for constructing OAM beam data. The normal direction is implicitly\n", + " defined by the ``size`` parameter.\n", + " \"\"\"\n", + "\n", + " def scalar_field(self, points, background_n):\n", + " \"\"\"Scalar field for OAM beam.\n", + " Scalar field corresponding to the analytic beam in coordinate system such that the\n", + " propagation direction is z and the ``E``-field is entirely ``x``-polarized. The field is\n", + " computed on an unstructured array ``points`` of shape ``(3, ...)``.\n", + " \"\"\"\n", + " x, y, z = points\n", + " z += waist_distance\n", + " r = np.sqrt(x**2 + y**2)\n", + " phi = np.arctan2(y, x)\n", + " absl = np.abs(l)\n", + "\n", + " zR = np.pi * background_n * w0**2 / (td.C_0 / np.array(self.freqs))\n", + " wz = w0 * np.sqrt(1 + z**2 / zR**2)\n", + " Rz = td.inf if np.any(z == 0) else z * (1 + zR**2 / z**2)\n", + " k = 2 * np.pi * np.array(self.freqs) / td.C_0 * background_n\n", + " psi_z = (np.abs(l) + 2 * p + 1) * np.arctan(z / zR)\n", + "\n", + " E = genlaguerre(p, absl)(2 * r**2 / wz**2) if p != 0 else 1\n", + " E *= (\n", + " source_time.amplitude\n", + " * np.sqrt(2 * factorial(p) / (np.pi * factorial(p + absl)))\n", + " / wz\n", + " )\n", + " E *= (r * np.sqrt(2) / wz) ** absl * np.exp(-(r**2) / wz**2)\n", + " E *= np.exp(-1j * k * r**2 / (2 * Rz)) * np.exp(-1j * l * phi) * np.exp(1j * psi_z)\n", + "\n", + " return E\n", + "\n", + " OAM_obj = OAM_obj(\n", + " center=center,\n", + " size=size,\n", + " freqs=freqs,\n", + " angle_theta=angle_theta,\n", + " angle_phi=angle_phi,\n", + " pol_angle=pol_angle,\n", + " direction=direction,\n", + " )\n", + " field_data = OAM_obj.field_data\n", + " field_dataset = td.FieldDataset(\n", + " Ex=field_data.Ex,\n", + " Ey=field_data.Ey,\n", + " Ez=field_data.Ez,\n", + " Hx=field_data.Hx,\n", + " Hy=field_data.Hy,\n", + " Hz=field_data.Hz,\n", + " )\n", + " OAM_source = td.CustomFieldSource(\n", + " center=center, size=size, source_time=source_time, field_dataset=field_dataset\n", + " )\n", + " return OAM_source" + ] + }, + { + "cell_type": "markdown", + "id": "21aaf9c2-bdc8-4fbc-a8fb-8f7150047470", + "metadata": {}, + "source": [ + "We will demonstrate this functionality by defining a $p=0,l=1$ OAM source that propagates in the $x$ direction:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "2576fe20-464d-44b6-a5b6-e5c157ca090a", + "metadata": {}, + "outputs": [], + "source": [ + "freq0 = td.C_0 / 1.55\n", + "oam = OAM(\n", + " size=(0, 4, 4),\n", + " p=0,\n", + " l=1,\n", + " # pol_angle=-np.pi/24,\n", + " # angle_theta=-np.pi/6,\n", + " source_time=td.GaussianPulse(freq0=freq0, fwidth=freq0 / 10),\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "71a6c80e-861d-405b-ac0a-966cc44552e3", + "metadata": {}, + "source": [ + "Create and run an example Laguerre-Gaussian (OAM) beam:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "13851ef1-bb22-4fa1-a2d3-ae8162549f62", + "metadata": {}, + "outputs": [], + "source": [ + "sim_size = (5.1, 5, 5)\n", + "sim_center = (sim_size[0] / 2 - 0.05, 0, 0)\n", + "\n", + "num_field_monitors = 61\n", + "field_monitor_xs = np.linspace(\n", + " sim_center[0] - sim_size[0] / 2 + 0.1, sim_size[0] - 0.1, num_field_monitors\n", + ")\n", + "field_monitors = []\n", + "for x in field_monitor_xs:\n", + " field_monitor = td.FieldMonitor(\n", + " center=(x, 0, 0), size=(0, sim_size[1], sim_size[2]), name=str(x), freqs=[freq0]\n", + " )\n", + " field_monitors.append(field_monitor)\n", + "\n", + "OAM_test_sim = td.Simulation(\n", + " center=sim_center,\n", + " size=sim_size,\n", + " structures=[],\n", + " sources=[oam],\n", + " monitors=field_monitors,\n", + " run_time=1e-13,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "a3005466-e0e6-4cd2-9017-7ae39f509dd2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    16:42:08 EDT Created task 'OAM test' with task_id                               \n",
    +       "             'fdve-e94f138b-e65b-4759-9108-50be8b9aef30' and task_type 'FDTD'.  \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:08 EDT\u001b[0m\u001b[2;36m \u001b[0mCreated task \u001b[32m'OAM test'\u001b[0m with task_id \n", + "\u001b[2;36m \u001b[0m\u001b[32m'fdve-e94f138b-e65b-4759-9108-50be8b9aef30'\u001b[0m and task_type \u001b[32m'FDTD'\u001b[0m. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 View task using web UI at                                          \n",
    +       "             'https://tidy3d.simulation.cloud/workbench?taskId=fdve-e94f138b-e65\n",
    +       "             b-4759-9108-50be8b9aef30'.                                         \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mView task using web UI at \n", + "\u001b[2;36m \u001b[0m\u001b]8;id=943547;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e94f138b-e65b-4759-9108-50be8b9aef30\u001b\\\u001b[32m'https://tidy3d.simulation.cloud/workbench?\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=890386;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e94f138b-e65b-4759-9108-50be8b9aef30\u001b\\\u001b[32mtaskId\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=943547;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e94f138b-e65b-4759-9108-50be8b9aef30\u001b\\\u001b[32m=\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=13105;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e94f138b-e65b-4759-9108-50be8b9aef30\u001b\\\u001b[32mfdve\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=943547;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e94f138b-e65b-4759-9108-50be8b9aef30\u001b\\\u001b[32m-e94f138b-e65\u001b[0m\u001b]8;;\u001b\\\n", + "\u001b[2;36m \u001b[0m\u001b]8;id=943547;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e94f138b-e65b-4759-9108-50be8b9aef30\u001b\\\u001b[32mb-4759-9108-50be8b9aef30'\u001b[0m\u001b]8;;\u001b\\. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 Task folder: 'default'.                                            \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mTask folder: \u001b]8;id=421943;https://tidy3d.simulation.cloud/folders/ee7143f0-3861-47de-ae8f-8f55df7232f1\u001b\\\u001b[32m'default'\u001b[0m\u001b]8;;\u001b\\. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "d0b9f4948dc84d9b947e8b6d0256dec6", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    \n",
    +       "
    \n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:10 EDT Maximum FlexCredit cost: 0.025. Minimum cost depends on task       \n",
    +       "             execution details. Use 'web.real_cost(task_id)' to get the billed  \n",
    +       "             FlexCredit cost after a simulation run.                            \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:10 EDT\u001b[0m\u001b[2;36m \u001b[0mMaximum FlexCredit cost: \u001b[1;36m0.025\u001b[0m. Minimum cost depends on task \n", + "\u001b[2;36m \u001b[0mexecution details. Use \u001b[32m'web.real_cost\u001b[0m\u001b[32m(\u001b[0m\u001b[32mtask_id\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m to get the billed \n", + "\u001b[2;36m \u001b[0mFlexCredit cost after a simulation run. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:11 EDT status = success                                                   \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:11 EDT\u001b[0m\u001b[2;36m \u001b[0mstatus = success \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "4b5cf604668f48b380e3eb0d284768c8", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    \n",
    +       "
    \n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:12 EDT loading simulation from simulation_data.hdf5                       \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:12 EDT\u001b[0m\u001b[2;36m \u001b[0mloading simulation from simulation_data.hdf5 \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "oam_sim_data = web.run(simulation=OAM_test_sim, task_name=\"OAM test\")" + ] + }, + { + "cell_type": "markdown", + "id": "a704cc24-2d21-4638-ad86-bc5e87a562e0", + "metadata": {}, + "source": [ + "Display field in propagation direction:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "23c71226-2795-4e70-9555-7c240f51966b", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "c1ccdcab2c494289beca5b600ed322e3", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(FloatSlider(value=0.05000000000000018, description='E', max=5.0, min=0.05000000000000018…" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import ipywidgets as widgets\n", + "\n", + "component = \"E\"\n", + "\n", + "field_slider = widgets.FloatSlider(\n", + " value=0,\n", + " min=field_monitor_xs[0],\n", + " max=field_monitor_xs[-1],\n", + " step=None,\n", + " description=component,\n", + " disabled=False,\n", + " continuous_update=True,\n", + " orientation=\"horizontal\",\n", + " readout=True,\n", + " readout_format=\".3f\",\n", + ")\n", + "\n", + "\n", + "def update_plot(x):\n", + " if x not in field_monitor_xs:\n", + " x = min(field_monitor_xs, key=lambda ll: abs(ll - x))\n", + " oam_sim_data.plot_field(str(x), component, f=freq0, val=\"abs^2\")\n", + " plt.show()\n", + "\n", + "\n", + "widgets.interactive(update_plot, x=field_slider)" + ] + }, + { + "cell_type": "markdown", + "id": "96eb9edb-7266-466a-b858-ad24566ad4df", + "metadata": {}, + "source": [ + "## Bessel beam source\n", + "\n", + "Here we show how to create an Bessel beam in Tidy3D using our [CustomFieldSource](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.CustomFieldSource.html) object. We then validate the field amplitude change along the propagation direction.

    \n", + "In cylindrical coordinates $(\\rho, \\phi, z)$, where $z$ is the propagation direction, the Bessel beam has field profile\n", + "\n", + "
    $U_\\beta(r)=J_m(k_T\\rho)e^{im\\phi}e^{-i\\beta z}$
    \n", + "\n", + "where:\n", + "
  • $k=\\frac{2\\pi n}{\\lambda}$ is the wavenumber
  • \n", + "
  • $J_m$ is the Bessel function of the first kind and $m^{th}$ order
  • \n", + "
  • $k_T=\\sqrt{k^2-\\beta^2}$
  • \n", + "
    \n", + "Source: Saleh & Teich, Fundamentals of Photonics" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "9793a27b-c702-412f-8afe-3015351acff3", + "metadata": {}, + "outputs": [], + "source": [ + "from scipy.special import jv\n", + "\n", + "\n", + "def Bessel(\n", + " size,\n", + " m,\n", + " beta,\n", + " source_time,\n", + " center=(0, 0, 0),\n", + " direction=\"+\",\n", + " pol_angle=0,\n", + " angle_phi=0,\n", + " angle_theta=0,\n", + " w0=1,\n", + " waist_distance=0,\n", + "):\n", + " if not isinstance(m, int):\n", + " raise Exception(\"Error: m must be an integer\")\n", + " freqs = [source_time.freq0]\n", + "\n", + " class Bessel_obj(BeamProfile):\n", + " \"\"\"Component for constructing Bessel beam data. The normal direction is implicitly\n", + " defined by the ``size`` parameter.\n", + " \"\"\"\n", + "\n", + " def scalar_field(self, points, background_n):\n", + " \"\"\"Scalar field for Bessel beam.\n", + " Scalar field corresponding to the analytic beam in coordinate system such that the\n", + " propagation direction is z and the ``E``-field is entirely ``x``-polarized. The field is\n", + " computed on an unstructured array ``points`` of shape ``(3, ...)``.\n", + " \"\"\"\n", + " x, y, z = points\n", + " z += waist_distance\n", + " rho = np.sqrt(x**2 + y**2)\n", + " phi = np.arctan2(y, x)\n", + "\n", + " k = 2 * np.pi * np.array(self.freqs) / td.C_0 * background_n\n", + " k_T = np.sqrt(k**2 - beta**2)\n", + "\n", + " E = source_time.amplitude * jv(m, k_T * rho)\n", + " E *= np.exp(1j * m * phi) * np.exp(-1j * beta * z)\n", + "\n", + " return E\n", + "\n", + " Bessel_obj = Bessel_obj(\n", + " center=center,\n", + " size=size,\n", + " freqs=freqs,\n", + " angle_theta=angle_theta,\n", + " angle_phi=angle_phi,\n", + " pol_angle=pol_angle,\n", + " direction=direction,\n", + " )\n", + " field_data = Bessel_obj.field_data\n", + " field_dataset = td.FieldDataset(\n", + " Ex=field_data.Ex,\n", + " Ey=field_data.Ey,\n", + " Ez=field_data.Ez,\n", + " Hx=field_data.Hx,\n", + " Hy=field_data.Hy,\n", + " Hz=field_data.Hz,\n", + " )\n", + " Bessel_source = td.CustomFieldSource(\n", + " center=center, size=size, source_time=source_time, field_dataset=field_dataset\n", + " )\n", + " return Bessel_source" + ] + }, + { + "cell_type": "markdown", + "id": "395121c5-498f-448c-9c18-a110a53275c6", + "metadata": {}, + "source": [ + "Create and run example a $m=0,\\beta=0$ Bessel beam:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "bb4a5088-43e1-4612-b966-01bc25955104", + "metadata": {}, + "outputs": [], + "source": [ + "freq0 = td.C_0 / 1.55\n", + "bessel = Bessel(\n", + " size=(0, 4, 4), m=0, beta=0, source_time=td.GaussianPulse(freq0=freq0, fwidth=freq0 / 10)\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "6983edb3-82b6-4c88-be92-8533c5c99714", + "metadata": {}, + "outputs": [], + "source": [ + "sim_size = (5.1, 5, 5)\n", + "sim_center = (sim_size[0] / 2 - 0.05, 0, 0)\n", + "\n", + "num_field_monitors = 61\n", + "field_monitor_xs = np.linspace(\n", + " sim_center[0] - sim_size[0] / 2 + 0.1, sim_size[0] - 0.1, num_field_monitors\n", + ")\n", + "field_monitors = []\n", + "for x in field_monitor_xs:\n", + " field_monitor = td.FieldMonitor(\n", + " center=(x, 0, 0), size=(0, sim_size[1], sim_size[2]), name=str(x), freqs=[freq0]\n", + " )\n", + " field_monitors.append(field_monitor)\n", + "\n", + "Bessel_test_sim = td.Simulation(\n", + " center=sim_center,\n", + " size=sim_size,\n", + " structures=[],\n", + " sources=[bessel],\n", + " monitors=field_monitors,\n", + " run_time=1e-13,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "df5e6e57-a468-426a-91a8-af296ae98d02", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    16:42:13 EDT Created task 'Bessel test' with task_id                            \n",
    +       "             'fdve-61433498-7c0d-4f97-8a04-b2c5b6d9d5b3' and task_type 'FDTD'.  \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:13 EDT\u001b[0m\u001b[2;36m \u001b[0mCreated task \u001b[32m'Bessel test'\u001b[0m with task_id \n", + "\u001b[2;36m \u001b[0m\u001b[32m'fdve-61433498-7c0d-4f97-8a04-b2c5b6d9d5b3'\u001b[0m and task_type \u001b[32m'FDTD'\u001b[0m. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 View task using web UI at                                          \n",
    +       "             'https://tidy3d.simulation.cloud/workbench?taskId=fdve-61433498-7c0\n",
    +       "             d-4f97-8a04-b2c5b6d9d5b3'.                                         \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mView task using web UI at \n", + "\u001b[2;36m \u001b[0m\u001b]8;id=408566;https://tidy3d.simulation.cloud/workbench?taskId=fdve-61433498-7c0d-4f97-8a04-b2c5b6d9d5b3\u001b\\\u001b[32m'https://tidy3d.simulation.cloud/workbench?\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=426938;https://tidy3d.simulation.cloud/workbench?taskId=fdve-61433498-7c0d-4f97-8a04-b2c5b6d9d5b3\u001b\\\u001b[32mtaskId\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=408566;https://tidy3d.simulation.cloud/workbench?taskId=fdve-61433498-7c0d-4f97-8a04-b2c5b6d9d5b3\u001b\\\u001b[32m=\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=610512;https://tidy3d.simulation.cloud/workbench?taskId=fdve-61433498-7c0d-4f97-8a04-b2c5b6d9d5b3\u001b\\\u001b[32mfdve\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=408566;https://tidy3d.simulation.cloud/workbench?taskId=fdve-61433498-7c0d-4f97-8a04-b2c5b6d9d5b3\u001b\\\u001b[32m-61433498-7c0\u001b[0m\u001b]8;;\u001b\\\n", + "\u001b[2;36m \u001b[0m\u001b]8;id=408566;https://tidy3d.simulation.cloud/workbench?taskId=fdve-61433498-7c0d-4f97-8a04-b2c5b6d9d5b3\u001b\\\u001b[32md-4f97-8a04-b2c5b6d9d5b3'\u001b[0m\u001b]8;;\u001b\\. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 Task folder: 'default'.                                            \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mTask folder: \u001b]8;id=509299;https://tidy3d.simulation.cloud/folders/ee7143f0-3861-47de-ae8f-8f55df7232f1\u001b\\\u001b[32m'default'\u001b[0m\u001b]8;;\u001b\\. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "b22604121f374ac19a8c6ada66273cbc", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    \n",
    +       "
    \n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:15 EDT Maximum FlexCredit cost: 0.025. Minimum cost depends on task       \n",
    +       "             execution details. Use 'web.real_cost(task_id)' to get the billed  \n",
    +       "             FlexCredit cost after a simulation run.                            \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:15 EDT\u001b[0m\u001b[2;36m \u001b[0mMaximum FlexCredit cost: \u001b[1;36m0.025\u001b[0m. Minimum cost depends on task \n", + "\u001b[2;36m \u001b[0mexecution details. Use \u001b[32m'web.real_cost\u001b[0m\u001b[32m(\u001b[0m\u001b[32mtask_id\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m to get the billed \n", + "\u001b[2;36m \u001b[0mFlexCredit cost after a simulation run. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:16 EDT status = success                                                   \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:16 EDT\u001b[0m\u001b[2;36m \u001b[0mstatus = success \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "662ca8877d1842b690c558fbed0cd9d0", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    \n",
    +       "
    \n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:17 EDT loading simulation from simulation_data.hdf5                       \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:17 EDT\u001b[0m\u001b[2;36m \u001b[0mloading simulation from simulation_data.hdf5 \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "bessel_sim_data = web.run(simulation=Bessel_test_sim, task_name=\"Bessel test\")" + ] + }, + { + "cell_type": "markdown", + "id": "3867781d-4279-4422-915e-cfbe9a9eeb6a", + "metadata": {}, + "source": [ + "Display field amplitude in propagation direction:" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "8e2f90a0-cf5d-42c5-a053-4bfc08c6c3d5", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "f9e45307f48444ba880bc20372fb3902", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(FloatSlider(value=0.05000000000000018, description='E', max=5.0, min=0.05000000000000018…" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import ipywidgets as widgets\n", + "\n", + "component = \"E\"\n", + "\n", + "field_slider = widgets.FloatSlider(\n", + " value=0,\n", + " min=field_monitor_xs[0],\n", + " max=field_monitor_xs[-1],\n", + " step=None,\n", + " description=component,\n", + " disabled=False,\n", + " continuous_update=True,\n", + " orientation=\"horizontal\",\n", + " readout=True,\n", + " readout_format=\".3f\",\n", + ")\n", + "\n", + "\n", + "def update_plot(x):\n", + " if x not in field_monitor_xs:\n", + " x = min(field_monitor_xs, key=lambda ll: abs(ll - x))\n", + " bessel_sim_data.plot_field(str(x), component, val=\"abs\", f=freq0)\n", + " plt.show()\n", + "\n", + "\n", + "widgets.interactive(update_plot, x=field_slider)" + ] + }, + { + "cell_type": "markdown", + "id": "83f68037-14d4-44d9-8878-0471febe3428", + "metadata": {}, + "source": [ + "## Hyper-geometric Gaussian source\n", + "\n", + "Here we show how to create a Hyper-geometric Gaussian (HGG) beam in Tidy3D using our [CustomFieldSource](https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.CustomFieldSource.html) object. We then validate the field profile change along the propagation direction.

    \n", + "In cylindrical coordinates, where $\\rho=\\frac{r}{\\omega_0}$ is the normalized radial coordinate and $Z=\\frac{z}{z_R}$ is the normalized longitude direction (the direction of propagation), the HGG has field profile\n", + "\n", + "
    $E_{p,m}\\lparen\\rho,\\phi,Z\\rparen=\\sqrt{\\frac{2^{p+|m|+1}}{\\pi\\Gamma(p+|m|+1)}}\\frac{\\Gamma\\lparen\\frac{p}{2}+|m|+1\\rparen}{\\Gamma(|m|+1)}i^{|m|+1}Z^{\\frac{p}{2}}\\lparen Z+i\\rparen^{-\\lparen\\frac{p}{2}+|m|+1\\rparen}\\rho^{|m|}\\exp\\lparen-\\frac{i\\rho^2}{Z+i}\\rparen e^{im\\phi} {_1}F_1\\lparen-\\frac{p}{2},|m|+1;\\frac{\\rho^2}{Z(Z+i)}\\rparen$
    \n", + "\n", + "where:\n", + "
  • $\\omega_0$ is the beam waist
  • \n", + "
  • $z_R=\\frac{\\pi n_\\epsilon\\omega_0^2}{\\lambda}$ is the Rayleigh length
  • \n", + "
  • $m$ is the rotational index
  • \n", + "
  • $p\\geq-|m|$
  • \n", + "
  • $\\Gamma(x)$ is the gamma function
  • \n", + "
  • ${_1}F_1(a,b;x)$ is a confluent hypergeometric function
  • " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "b5dec480-68fa-44a2-8665-ddd252ceacd7", + "metadata": {}, + "outputs": [], + "source": [ + "from scipy.special import gamma, hyp1f1\n", + "\n", + "\n", + "def HGG(\n", + " size,\n", + " p,\n", + " m,\n", + " source_time,\n", + " center=(0, 0, 0),\n", + " direction=\"+\",\n", + " pol_angle=0,\n", + " angle_phi=0,\n", + " angle_theta=0,\n", + " w0=1,\n", + " waist_distance=0,\n", + "):\n", + " if p < -np.abs(m):\n", + " raise Exception(\"Error: p must be greater than or equal to -|m|\")\n", + " freqs = [source_time.freq0]\n", + "\n", + " class HGG_obj(BeamProfile):\n", + " \"\"\"Component for constructing HGG beam data. The normal direction is implicitly\n", + " defined by the ``size`` parameter.\n", + " \"\"\"\n", + "\n", + " def scalar_field(self, points, background_n):\n", + " \"\"\"Scalar field for HGG beam.\n", + " Scalar field corresponding to the analytic beam in coordinate system such that the\n", + " propagation direction is z and the ``E``-field is entirely ``x``-polarized. The field is\n", + " computed on an unstructured array ``points`` of shape ``(3, ...)``.\n", + " \"\"\"\n", + " x, y, z = points\n", + " z += waist_distance\n", + "\n", + " r = np.sqrt(x**2 + y**2) / w0\n", + " phi = np.arctan2(y, x)\n", + " abs_m = np.abs(m)\n", + "\n", + " E = source_time.amplitude * np.sqrt(\n", + " 2 ** (p + abs_m + 1) / (np.pi * gamma(p + abs_m + 1))\n", + " )\n", + "\n", + " Z = z / (np.pi * background_n * w0**2 / (td.C_0 / np.array(self.freqs)))\n", + " if np.all(Z) == 0:\n", + " return E * r ** (p + abs_m) * np.exp(-(r**2) + 1j * m * phi)\n", + "\n", + " E *= gamma(p / 2 + abs_m + 1) / gamma(abs_m + 1) * 1j ** (abs_m + 1) * Z ** (p / 2)\n", + " E *= (Z + 1j) ** (-(p / 2 + abs_m + 1)) * r**abs_m * np.exp(-(1j * r**2) / (Z + 1j))\n", + " E *= np.exp(1j * m * phi) * hyp1f1(-p / 2, abs_m + 1, r**2 / (Z * (Z + 1j)))\n", + "\n", + " return E\n", + "\n", + " HGG_obj = HGG_obj(\n", + " center=center,\n", + " size=size,\n", + " freqs=freqs,\n", + " angle_theta=angle_theta,\n", + " angle_phi=angle_phi,\n", + " pol_angle=pol_angle,\n", + " direction=direction,\n", + " )\n", + " field_data = HGG_obj.field_data\n", + " field_dataset = td.FieldDataset(\n", + " Ex=field_data.Ex,\n", + " Ey=field_data.Ey,\n", + " Ez=field_data.Ez,\n", + " Hx=field_data.Hx,\n", + " Hy=field_data.Hy,\n", + " Hz=field_data.Hz,\n", + " )\n", + " HGG_source = td.CustomFieldSource(\n", + " center=center, size=size, source_time=source_time, field_dataset=field_dataset\n", + " )\n", + " return HGG_source" + ] + }, + { + "cell_type": "markdown", + "id": "3eed53a4-04cd-49a0-bcc4-069f78e740a2", + "metadata": {}, + "source": [ + "Create and run an example $p=1,m=2$ HGG beam:" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "dde4141e-ea33-4f36-949d-311ce5fbf606", + "metadata": {}, + "outputs": [], + "source": [ + "freq0 = td.C_0 / 1.55\n", + "hgg = HGG(size=(0, 4, 4), p=1, m=2, source_time=td.GaussianPulse(freq0=freq0, fwidth=freq0 / 10))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "8b307c51-4007-40aa-bac5-4f0ac7cf7d6a", + "metadata": {}, + "outputs": [], + "source": [ + "sim_size = (5.1, 5, 5)\n", + "sim_center = (sim_size[0] / 2 - 0.05, 0, 0)\n", + "\n", + "num_field_monitors = 61\n", + "field_monitor_xs = np.linspace(\n", + " sim_center[0] - sim_size[0] / 2 + 0.1, sim_size[0] - 0.1, num_field_monitors\n", + ")\n", + "field_monitors = []\n", + "for x in field_monitor_xs:\n", + " field_monitor = td.FieldMonitor(\n", + " center=(x, 0, 0), size=(0, sim_size[1], sim_size[2]), name=str(x), freqs=[freq0]\n", + " )\n", + " field_monitors.append(field_monitor)\n", + "\n", + "HGG_test_sim = td.Simulation(\n", + " center=sim_center,\n", + " size=sim_size,\n", + " structures=[],\n", + " sources=[hgg],\n", + " monitors=field_monitors,\n", + " run_time=1e-13,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "b611fea9-b5b1-4849-ba74-9c59c9c44395", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
    16:42:18 EDT Created task 'HGG test' with task_id                               \n",
    +       "             'fdve-e32c350e-4dcf-480a-9665-38865b9cda11' and task_type 'FDTD'.  \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:18 EDT\u001b[0m\u001b[2;36m \u001b[0mCreated task \u001b[32m'HGG test'\u001b[0m with task_id \n", + "\u001b[2;36m \u001b[0m\u001b[32m'fdve-e32c350e-4dcf-480a-9665-38865b9cda11'\u001b[0m and task_type \u001b[32m'FDTD'\u001b[0m. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 View task using web UI at                                          \n",
    +       "             'https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dc\n",
    +       "             f-480a-9665-38865b9cda11'.                                         \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mView task using web UI at \n", + "\u001b[2;36m \u001b[0m\u001b]8;id=62986;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[32m'https://tidy3d.simulation.cloud/workbench?\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=894741;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[32mtaskId\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=62986;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[32m=\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=322785;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[32mfdve\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=62986;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[32m-e32c350e-4dc\u001b[0m\u001b]8;;\u001b\\\n", + "\u001b[2;36m \u001b[0m\u001b]8;id=62986;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[32mf-480a-9665-38865b9cda11'\u001b[0m\u001b]8;;\u001b\\. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 Task folder: 'default'.                                            \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mTask folder: \u001b]8;id=234063;https://tidy3d.simulation.cloud/folders/ee7143f0-3861-47de-ae8f-8f55df7232f1\u001b\\\u001b[32m'default'\u001b[0m\u001b]8;;\u001b\\. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "546ccc90437045bb81c7a373e20aa2d6", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    \n",
    +       "
    \n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:20 EDT Maximum FlexCredit cost: 0.025. Minimum cost depends on task       \n",
    +       "             execution details. Use 'web.real_cost(task_id)' to get the billed  \n",
    +       "             FlexCredit cost after a simulation run.                            \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:20 EDT\u001b[0m\u001b[2;36m \u001b[0mMaximum FlexCredit cost: \u001b[1;36m0.025\u001b[0m. Minimum cost depends on task \n", + "\u001b[2;36m \u001b[0mexecution details. Use \u001b[32m'web.real_cost\u001b[0m\u001b[32m(\u001b[0m\u001b[32mtask_id\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m to get the billed \n", + "\u001b[2;36m \u001b[0mFlexCredit cost after a simulation run. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:21 EDT status = queued                                                    \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:21 EDT\u001b[0m\u001b[2;36m \u001b[0mstatus = queued \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 To cancel the simulation, use 'web.abort(task_id)' or              \n",
    +       "             'web.delete(task_id)' or abort/delete the task in the web UI.      \n",
    +       "             Terminating the Python script will not stop the job running on the \n",
    +       "             cloud.                                                             \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mTo cancel the simulation, use \u001b[32m'web.abort\u001b[0m\u001b[32m(\u001b[0m\u001b[32mtask_id\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m or \n", + "\u001b[2;36m \u001b[0m\u001b[32m'web.delete\u001b[0m\u001b[32m(\u001b[0m\u001b[32mtask_id\u001b[0m\u001b[32m)\u001b[0m\u001b[32m'\u001b[0m or abort/delete the task in the web UI. \n", + "\u001b[2;36m \u001b[0mTerminating the Python script will not stop the job running on the \n", + "\u001b[2;36m \u001b[0mcloud. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "30f9462ceee9414493f4130680656acf", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    16:42:30 EDT starting up solver                                                 \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:30 EDT\u001b[0m\u001b[2;36m \u001b[0mstarting up solver \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 running solver                                                     \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mrunning solver \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "46424f9850814e8aaf6c468e55943e59", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:32 EDT early shutoff detected at 92%, exiting.                            \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:32 EDT\u001b[0m\u001b[2;36m \u001b[0mearly shutoff detected at \u001b[1;36m92\u001b[0m%, exiting. \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    \n",
    +       "
    \n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
                 status = postprocess                                               \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m \u001b[0m\u001b[2;36m \u001b[0mstatus = postprocess \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "c4b25fe24b154abc953a8740aed7e36f", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:40 EDT status = success                                                   \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:40 EDT\u001b[0m\u001b[2;36m \u001b[0mstatus = success \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    16:42:42 EDT View simulation result at                                          \n",
    +       "             'https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dc\n",
    +       "             f-480a-9665-38865b9cda11'.                                         \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:42 EDT\u001b[0m\u001b[2;36m \u001b[0mView simulation result at \n", + "\u001b[2;36m \u001b[0m\u001b]8;id=707453;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[4;34m'https://tidy3d.simulation.cloud/workbench?\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=518924;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[4;34mtaskId\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=707453;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[4;34m=\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=745755;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[4;34mfdve\u001b[0m\u001b]8;;\u001b\\\u001b]8;id=707453;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[4;34m-e32c350e-4dc\u001b[0m\u001b]8;;\u001b\\\n", + "\u001b[2;36m \u001b[0m\u001b]8;id=707453;https://tidy3d.simulation.cloud/workbench?taskId=fdve-e32c350e-4dcf-480a-9665-38865b9cda11\u001b\\\u001b[4;34mf-480a-9665-38865b9cda11'\u001b[0m\u001b]8;;\u001b\\\u001b[4;34m.\u001b[0m \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "ed75d43054124131b48f3322dc9a1d6a", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    \n"
    +      ],
    +      "text/plain": []
    +     },
    +     "metadata": {},
    +     "output_type": "display_data"
    +    },
    +    {
    +     "data": {
    +      "text/html": [
    +       "
    \n",
    +       "
    \n" + ], + "text/plain": [ + "\n" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
    16:42:51 EDT loading simulation from simulation_data.hdf5                       \n",
    +       "
    \n" + ], + "text/plain": [ + "\u001b[2;36m16:42:51 EDT\u001b[0m\u001b[2;36m \u001b[0mloading simulation from simulation_data.hdf5 \n" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "hgg_sim_data = web.run(simulation=HGG_test_sim, task_name=\"HGG test\")" + ] + }, + { + "cell_type": "markdown", + "id": "40b05fb4-59ac-447b-8b47-d1999861c568", + "metadata": {}, + "source": [ + "Display field in propagation direction:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "496022b9-fc6b-4560-a34f-40697f7d49f5", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "92341339c3d64cca8fc6cf0f43df4854", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "interactive(children=(FloatSlider(value=0.05000000000000018, description='Ey', max=5.0, min=0.0500000000000001…" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import ipywidgets as widgets\n", + "\n", + "component = \"Ey\"\n", + "\n", + "field_slider = widgets.FloatSlider(\n", + " value=0,\n", + " min=field_monitor_xs[0],\n", + " max=field_monitor_xs[-1],\n", + " step=None,\n", + " description=component,\n", + " disabled=False,\n", + " continuous_update=True,\n", + " orientation=\"horizontal\",\n", + " readout=True,\n", + " readout_format=\".3f\",\n", + ")\n", + "\n", + "\n", + "def update_plot(x):\n", + " if x not in field_monitor_xs:\n", + " x = min(field_monitor_xs, key=lambda ll: abs(ll - x))\n", + " hgg_sim_data.plot_field(str(x), component, f=freq0)\n", + " plt.show()\n", + "\n", + "\n", + "widgets.interactive(update_plot, x=field_slider)" + ] + } + ], + "metadata": { + "description": "This notebook creates advanced gaussian beams that can be used by the interested user. In order to make these custom sources easy to use in multiple settings, we use Tidy3D's open-source BeamProfile object to build these sources. This also demonstrates how to take advantage of Tidy3D's open-source nature to save much complication.", + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "keywords": "gaussian, source, field, open-source, angular, hyper-geometric, bessel, hermite", + "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.11.8" + }, + "title": "Creating advanced Gaussian sources using Tidy3D's open-source BeamProfile object" + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/docs/features/sources.rst b/docs/features/sources.rst index b5c4d298..f76c7332 100644 --- a/docs/features/sources.rst +++ b/docs/features/sources.rst @@ -12,3 +12,4 @@ Sources are a fundamental component for input power in electromagnetic simulatio ../../TFSF ../../CustomFieldSource ../../BroadbandPlaneWaveWithConstantObliqueIncidentAngle + ../../AdvancedGaussianSources