{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "556562f3-8ece-4517-8c93-ee5e2fc29131", "metadata": {}, "source": [ "# Example-10: Dipole element factory (cylindrical multipole)" ] }, { "cell_type": "markdown", "id": "f744ba48-1780-4a06-a57b-ab549fbbf6c3", "metadata": {}, "source": [ "In this example dipole factory is illustrated for dipole with multipole cylindrical multipoles. \n", "\n", "The dipole hamiltonian is:\n", "\n", "$\n", "\\begin{align}\n", "& H(q_x, q_y, q_s, p_x, p_y, p_s; s) = \\frac{p_s}{\\beta} - t(s)(q_x p_y - q_y p_x) - (1 + h(s) q_x) \\left(\\sqrt{P_s^2 - P_x^2 - P_y^2 - \\frac{1}{\\beta^2 \\gamma^2}} + a_s(q_x, q_y, q_s; s)\\right) \\\\\n", "& \\\\\n", "& P_s = p_s + 1/\\beta - \\varphi(q_x, q_y, q_s; s) \\\\\n", "& P_x = p_x - a_x(q_x, q_y, q_s; s) \\\\\n", "& P_y = p_y - a_y(q_x, q_y, q_s; s) \\\\\n", "\\\\\n", "& (a_x, a_y, a_s) = (0, 0, -\\frac{1}{1 + q_x/\\rho}\\left(\\frac{q_x}{ \\rho} + \\frac{q_x^2}{2 \\rho^2} \\right) + U(q_x, q_y; \\rho) )\\\\\n", "& \\varphi = 0 \\\\\n", "& t = 0 \\\\\n", "& h = \\frac{1}{\\rho} = \\frac{\\alpha}{l}\n", "\\end{align}\n", "$\n", "\n", "The constructed element signature is:\n", "\n", "```python\n", "def dipole(qsps:Array, length:Array, angle:Array, kq_n:Array, kq_s:Array, ks_n:Array, ks_s:Array, ko_n:Array, ko_s:Array) -> Array:\n", " ...\n", "```\n", "\n", "Note, no fringe effects are icluded.\n", "\n", "Cylindrical potential is precomputed for quadrupole, sextuipole and octupole components up to degree 10 in transverse coordinates." ] }, { "cell_type": "code", "execution_count": 1, "id": "206cff65-dbbe-4592-8927-fae0518a964a", "metadata": {}, "outputs": [], "source": [ "import jax\n", "from jax import jit\n", "from jax import jacrev\n", "\n", "from elementary.util import ptc\n", "from elementary.util import beta\n", "from elementary.dipole import dipole_factory\n", "\n", "jax.numpy.set_printoptions(linewidth=256, precision=12)" ] }, { "cell_type": "code", "execution_count": 2, "id": "a699f41b-1475-437e-93f1-b6e07400957d", "metadata": {}, "outputs": [], "source": [ "# Set data type\n", "\n", "jax.config.update(\"jax_enable_x64\", True)" ] }, { "cell_type": "code", "execution_count": 3, "id": "0ab982ad-6729-4515-8daa-07e15d6aaff3", "metadata": {}, "outputs": [], "source": [ "# Set device\n", "\n", "device, *_ = jax.devices('cpu')\n", "jax.config.update('jax_default_device', device)" ] }, { "cell_type": "code", "execution_count": 4, "id": "7a28ae6d-52c1-4a0d-aeb4-ee10bba7cd13", "metadata": {}, "outputs": [], "source": [ "# Set initial condition\n", "\n", "(q_x, q_y, q_s) = qs = jax.numpy.array([-0.01, 0.005, 0.001])\n", "(p_x, p_y, p_s) = ps = jax.numpy.array([0.001, 0.001, -0.0001])\n", "qsps = jax.numpy.hstack([qs, ps])" ] }, { "cell_type": "code", "execution_count": 5, "id": "06b4f75a-f769-4d5b-a5c6-897f1063abde", "metadata": {}, "outputs": [], "source": [ "# Define generic dipole element\n", "# Note, exact should be set to false\n", "\n", "gamma = 10**3\n", "element = jit(dipole_factory(exact=False, multipole=True, beta=beta(gamma), gamma=gamma, order=2**1, iterations=1E3))" ] }, { "cell_type": "code", "execution_count": 6, "id": "86425b83-95ed-4e40-a227-e79d2fbfd9e2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-0.017200420757 -0.003042779236 0.001519392779 -0.017822768071 -0.015620313086 -0.0001 ]\n", "[-0.017200420757 -0.003042779236 0.00151939277 -0.017822768072 -0.015620313086 -0.0001 ]\n", "True\n" ] } ], "source": [ "# Compare with PTC\n", "\n", "length = jax.numpy.float64(1.0)\n", "angle = jax.numpy.float64(0.05)\n", "\n", "kq_n = jax.numpy.float64(-2.0)\n", "kq_s = jax.numpy.float64(+1.5)\n", "ks_n = jax.numpy.float64(-50.0)\n", "ks_s = jax.numpy.float64(+75.0)\n", "ko_n = jax.numpy.float64(-100.0)\n", "ko_s = jax.numpy.float64(+500.0)\n", "\n", "print(res := element(qsps, length, angle, kq_n, kq_s, ks_n, ks_s, ko_n, ko_s))\n", "print(ref := ptc(qsps, 'sbend', {'l': float(length), 'angle': float(angle), 'knl': f'{{0.0,{float(kq_n*length)}, {float(ks_n*length)}, {float(ko_n*length)}}}', 'ksl': f'{{0.0,{float(kq_s*length)}, {float(ks_s*length)}, {float(ko_s*length)}}}', 'kill_ent_fringe': 'true', 'kill_exi_fringe': 'true'}, gamma=gamma))\n", "print(jax.numpy.allclose(res, ref))" ] } ], "metadata": { "colab": { "collapsed_sections": [ "myt0_gMIOq7b", "5d97819c" ], "name": "03_frequency.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.1" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 5 }