{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "556562f3-8ece-4517-8c93-ee5e2fc29131", "metadata": {}, "source": [ "# Example-05: Quadrupole element factory" ] }, { "cell_type": "markdown", "id": "d9982b6c-a41e-42c6-b005-3b1788e714cf", "metadata": {}, "source": [ "In this example quadrupole factory is illustrated. \n", "\n", "The quadrupole 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(q_x^2 + q_y^2 \\right) + k_s q_x q_y)\\\\\n", "& \\varphi = 0 \\\\\n", "& t = h = 0 \\\\\n", "\\end{align}\n", "$\n", "\n", "The constructed element signature is:\n", "\n", "```python\n", "def quadrupole(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": "7d852ca1-4e33-4983-8ec3-5b7d67f63674", "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.quadrupole import quadrupole_factory\n", "\n", "jax.numpy.set_printoptions(linewidth=256, precision=12)" ] }, { "cell_type": "code", "execution_count": 2, "id": "4e720ace-fceb-4f37-907f-999ff99c1aec", "metadata": {}, "outputs": [], "source": [ "# Set data type\n", "\n", "jax.config.update(\"jax_enable_x64\", True)" ] }, { "cell_type": "code", "execution_count": 3, "id": "0e8314ca-3244-4989-9bbb-e8e0fb9347dc", "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": "b9ee3851-7d8d-47cc-a1df-efa850ab6852", "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": "42328cbf-7250-42c2-8721-c20ba29e644e", "metadata": {}, "outputs": [], "source": [ "# Define generic quadrupole element\n", "\n", "gamma = 10**3\n", "element = jit(quadrupole_factory(beta=beta(gamma), gamma=gamma, order=2**1, iterations=100))" ] }, { "cell_type": "code", "execution_count": 6, "id": "0c974d8d-a443-4a3a-be4d-a5f65ef8d1b1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-0.017335308274 -0.005444917174 0.000881617913 -0.020606852812 -0.020050030434 -0.0001 ]\n", "[-0.017335308274 -0.005444917174 0.000881617913 -0.020606852812 -0.020050030434 -0.0001 ]\n", "True\n" ] } ], "source": [ "# Compare with PTC\n", "\n", "length = jax.numpy.float64(1.0)\n", "kn = jax.numpy.float64(-2.0)\n", "ks = jax.numpy.float64(+1.5)\n", "\n", "print(res := element(qsps, length, kn, ks))\n", "print(ref := ptc(qsps, 'quadrupole', {'l': float(length), 'k1': float(kn), 'k1s': float(ks)}, gamma=gamma))\n", "print(jax.numpy.allclose(res, ref))" ] }, { "cell_type": "code", "execution_count": 7, "id": "9d96dbbf-4849-4fe3-bc29-22896e2201a6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 2.279575304234e+00 7.632479527605e-01 0.000000000000e+00 1.388655261371e+00 2.520951472455e-01 9.491025360004e-03]\n", " [ 7.636609190032e-01 2.438324623808e-01 0.000000000000e+00 2.522344406377e-01 7.165998020053e-01 9.548555445061e-03]\n", " [ 2.540182886809e-02 -1.691652080606e-03 1.000000000000e+00 1.634207518714e-02 9.283955945891e-03 2.542783581185e-04]\n", " [ 3.154287926464e+00 1.578288131763e+00 0.000000000000e+00 2.279171804490e+00 7.632559539870e-01 9.596399325345e-03]\n", " [ 1.578258960753e+00 -1.054777136190e+00 0.000000000000e+00 7.631214548048e-01 2.439057831974e-01 -2.972554738619e-03]\n", " [ 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00]]\n", "\n", "[-0.020617438835 -0.020060330409 -0.000413662569 -0.042837992308 -0.015113128063 0. ]\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 }