{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "cfd89770-b82f-4d57-b009-e96ad4b4ab04", "metadata": {}, "source": [ "# Example-06: Sextupole element factory" ] }, { "cell_type": "markdown", "id": "3dfa901a-d80c-49fd-aaca-982ec3b3f915", "metadata": {}, "source": [ "In this example sextupole factory is illustrated. \n", "\n", "The sextupole 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}{2!} k_n \\left(\\frac{q_x^3}{3} - q_x q_y^2 \\right) - \\frac{1}{2!} k_s \\left(\\frac{q_y^3}{3} - q_x^2 q_y\\right))\\\\\n", "& \\varphi = 0 \\\\\n", "& t = h = 0 \\\\\n", "\\end{align}\n", "$\n", "\n", "The constructed element signature is:\n", "\n", "```python\n", "def sextupole(qsps:Array, length:Array, kn:Array, ks:Array) -> Array:\n", " ...\n", "```\n", "\n", "Note, both `kn` and `ks` should be passed on invocation." ] }, { "cell_type": "code", "execution_count": 1, "id": "0aa96d72-1dc3-4a49-b0db-88f7d0ee8300", "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.sextupole import sextupole_factory\n", "\n", "jax.numpy.set_printoptions(linewidth=256, precision=12)" ] }, { "cell_type": "code", "execution_count": 2, "id": "d9976ec2-bbd8-45c2-9a5c-14c9cc500d5f", "metadata": {}, "outputs": [], "source": [ "# Set data type\n", "\n", "jax.config.update(\"jax_enable_x64\", True)" ] }, { "cell_type": "code", "execution_count": 3, "id": "57e81c8c-0ed1-44b3-8f8b-2705219a52aa", "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": "a5793c8c-88d2-41da-a812-6be61b04ba4f", "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": "71ca2243-b0dd-412d-b99c-73ea5b5a3b6a", "metadata": {}, "outputs": [], "source": [ "# Define generic sextupole element\n", "\n", "gamma = 10**3\n", "element = jit(sextupole_factory(beta=beta(gamma), gamma=gamma, order=2**1, iterations=100))" ] }, { "cell_type": "code", "execution_count": 6, "id": "3d76fdc0-fb94-4a60-94dc-6ff5d8754dd7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-0.009839311476 0.005305274154 0.000999691938 0.000595944971 0.002048184556 -0.0001 ]\n", "[-0.009839311476 0.005305274154 0.000999691938 0.000595944971 0.002048184556 -0.0001 ]\n", "True\n" ] } ], "source": [ "# Compare with PTC\n", "\n", "length = jax.numpy.float64(0.2)\n", "kn = jax.numpy.float64(-50.0)\n", "ks = jax.numpy.float64(+75.0)\n", "\n", "print(res := element(qsps, length, kn, ks))\n", "print(ref := ptc(qsps, 'sextupole', {'l': float(length), 'k2': float(kn), 'k2s': float(ks)}, gamma=gamma))\n", "print(jax.numpy.allclose(res, ref))" ] }, { "cell_type": "code", "execution_count": 7, "id": "a11ec1ce-a49b-4b47-aba7-8d458fe43ac8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 9.977539230851e-01 -1.999683876014e-02 0.000000000000e+00 1.998755038515e-01 -1.333336463994e-03 -1.588878813703e-04]\n", " [-1.999528286417e-02 1.002381087687e+00 0.000000000000e+00 -1.333283393256e-03 2.001711272451e-01 -3.042893998520e-04]\n", " [ 3.567744778423e-05 1.067886761186e-05 1.000000000000e+00 -1.582182316560e-04 -3.046376164518e-04 8.131286746188e-07]\n", " [-2.075140977962e-02 -2.000692596676e-01 0.000000000000e+00 9.979602547484e-01 -2.001825891400e-02 2.884097120735e-05]\n", " [-2.000275114867e-01 2.345269216804e-02 0.000000000000e+00 -2.001650181701e-02 1.002174743629e+00 1.451547319575e-05]\n", " [ 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00]]\n", "\n", "[ 5.960059282262e-04 2.048394055811e-03 -2.275669201308e-06 -2.198365452755e-03 5.184991611235e-03 0.000000000000e+00]\n", "\n" ] } ], "source": [ "# Differentiability\n", "\n", "print(jacrev(element)(qsps, length, kn, ks))\n", "print()\n", "\n", "print(jacrev(element, 1)(qsps, length, kn, ks))\n", "print()" ] } ], "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 }