{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "556562f3-8ece-4517-8c93-ee5e2fc29131", "metadata": {}, "source": [ "# Example-07: Octupole element factory" ] }, { "cell_type": "markdown", "id": "f0a92d9b-6834-45ee-861d-9abbc6d78916", "metadata": {}, "source": [ "In this example octupole factory is illustrated. \n", "\n", "The octupole 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}{3!} k_n \\left(\\frac{q_x^4}{4} - 3 q_x^2 q_y^2 + \\frac{q_y^4}{4}\\right) - \\frac{1}{3!} k_s \\left(q_x q_y^3 - q_x^3 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 octupole(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": "5a1ab87e-1c63-4f5b-a8bb-d090991eea4e", "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.octupole import octupole_factory\n", "\n", "jax.numpy.set_printoptions(linewidth=256, precision=12)" ] }, { "cell_type": "code", "execution_count": 2, "id": "2c376344-aa3b-4536-938c-d68a7dec2bdb", "metadata": {}, "outputs": [], "source": [ "# Set data type\n", "\n", "jax.config.update(\"jax_enable_x64\", True)" ] }, { "cell_type": "code", "execution_count": 3, "id": "c49d181e-8942-4559-962d-d36f73a98507", "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": "14f9e09d-9863-4486-88e9-d246e0d7f43e", "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": "51b5e6a8-fafe-4b8e-9e29-98c3c851352d", "metadata": {}, "outputs": [], "source": [ "# Define generic octupole element\n", "\n", "gamma = 10**3\n", "element = jit(octupole_factory(beta=beta(gamma), gamma=gamma, order=2**1, iterations=100))" ] }, { "cell_type": "code", "execution_count": 6, "id": "c56b9b37-7ece-4fc7-8d81-882ebb5e53d5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-0.009797768862 0.005199205032 0.000999798525 0.001022114248 0.000992148146 -0.0001 ]\n", "[-0.009797768862 0.005199205032 0.000999798525 0.001022114248 0.000992148146 -0.0001 ]\n", "True\n" ] } ], "source": [ "# Compare with PTC\n", "\n", "length = jax.numpy.float64(0.2)\n", "kn = jax.numpy.float64(-100.0)\n", "ks = jax.numpy.float64(+500.0)\n", "\n", "print(res := element(qsps, length, kn, ks))\n", "print(ref := ptc(qsps, 'octupole', {'l': float(length), 'k3': float(kn), 'k3s': float(ks)}, gamma=gamma))\n", "print(jax.numpy.allclose(res, ref))" ] }, { "cell_type": "code", "execution_count": 7, "id": "ea2129f1-8127-46a8-ae2c-54d532a41bb5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 9.995697830497e-01 4.656632792258e-04 0.000000000000e+00 1.999915493370e-01 3.093502378623e-05 -2.022535230983e-04]\n", " [ 4.656649771080e-04 1.000430351571e+00 0.000000000000e+00 3.093510854460e-05 2.000492660372e-01 -1.992851672412e-04]\n", " [-2.659897270538e-08 -9.006312695687e-07 1.000000000000e+00 -2.022530833439e-04 -1.992853937288e-04 6.030721251410e-07]\n", " [-4.326806733311e-03 4.608973281245e-03 0.000000000000e+00 9.995646349961e-01 4.562292899969e-04 -1.639276538502e-08]\n", " [ 4.609037834460e-03 4.329471623185e-03 0.000000000000e+00 4.562327538309e-04 1.000435498762e+00 -8.939569416383e-07]\n", " [ 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 0.000000000000e+00 1.000000000000e+00]]\n", "\n", "[ 1.022217507094e-03 9.922483777699e-04 -1.014842745316e-06 1.106308301324e-04 -3.477926503128e-05 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 }