{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "75b1ed31-93e5-4288-88c1-e354236abf19", "metadata": {}, "source": [ "# Example-16: Custom (element)" ] }, { "cell_type": "code", "execution_count": 1, "id": "05dfd677-0519-49da-924a-73f697af4e25", "metadata": {}, "outputs": [], "source": [ "# In this example steps to build element with custom transformatiion are shown\n", "# This element can be used to wrap a custom transformation and used it with other elements\n", "# Construction requires transformation to be passed as a parameter\n", "# This transformation is expected to have certain signature\n", "# The first parameter is state (qx, px, qy, py) followed by other tensor knobs\n", "# These knobs are passed simular to deviation parameters in standart elements" ] }, { "cell_type": "code", "execution_count": 2, "id": "417812bd-a1bb-4fa9-9be8-dbbd84b5a0e5", "metadata": {}, "outputs": [], "source": [ "import torch\n", "\n", "from model.library.transformations import quadrupole\n", "from model.library.quadrupole import Quadrupole\n", "from model.library.custom import Custom" ] }, { "cell_type": "code", "execution_count": 3, "id": "f56c4bcd-818e-4c8b-91a4-9743d71a6b68", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([-0.0101, -0.0189, -0.0173, -0.0385], dtype=torch.float64)\n", "tensor([-0.0101, -0.0189, -0.0173, -0.0385], dtype=torch.float64)\n" ] } ], "source": [ "# Build custom element using a transformation\n", "\n", "state = torch.tensor([0.01, -0.005, -0.005, 0.001], dtype=torch.float64)\n", "\n", "kn = torch.tensor(5.0, dtype=torch.float64)\n", "ks = torch.tensor(1.0, dtype=torch.float64)\n", "dp = torch.tensor(0.001, dtype=torch.float64)\n", "length = torch.tensor(1.0, dtype=torch.float64)\n", "\n", "print(quadrupole(state, kn, ks, dp, length))\n", "\n", "C = Custom('C', quadrupole, ['kn', 'ks', 'dp', 'length'], length=1.0, angle=0.0, dp=dp.item())\n", "\n", "print(C(state, data={**C.data(), **{'kn': kn, 'ks': ks, 'dp': dp, 'length': length}}, alignment=False))" ] }, { "cell_type": "code", "execution_count": 4, "id": "dc1a5880-bcc5-4f9f-aeaa-5f863c1edbda", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([-0.0149, -0.0080, -0.0454, -0.1485], dtype=torch.float64)\n", "tensor([-0.0149, -0.0080, -0.0454, -0.1485], dtype=torch.float64)\n", "tensor([-0.0149, -0.0080, -0.0454, -0.1485], dtype=torch.float64)\n" ] } ], "source": [ "# Use regular element step\n", "\n", "state = torch.tensor([0.01, -0.005, -0.005, 0.001], dtype=torch.float64)\n", "\n", "kn = torch.tensor(5.0, dtype=torch.float64)\n", "ks = torch.tensor(1.0, dtype=torch.float64)\n", "dp = torch.tensor(0.001, dtype=torch.float64)\n", "dl = torch.tensor(0.0, dtype=torch.float64)\n", "\n", "Q = Quadrupole('Q', 1.0, 5.0, 1.0, dp=0.001, dx=-0.001)\n", "print(Q(state, data={**Q.data(), **{'kn': kn, 'ks': ks, 'dp': dp, 'dl': dl}}, alignment=False))\n", "\n", "print(Q._step(state, kn, ks, dp, dl))\n", "\n", "C = Custom('C', Q._step, ['kn', 'ks', 'dp', 'dl'], length=1.0, angle=0.0, dp=0.001, dx=-0.001)\n", "print(C(state, data={**C.data(), **{'kn': kn, 'ks': ks, 'dp': dp, 'dl': dl}}, alignment=False))" ] }, { "cell_type": "code", "execution_count": 5, "id": "ab3c99c7-74ee-4f6a-9ab6-ff3073978df5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([-0.0168, -0.0074, -0.0441, -0.1447], dtype=torch.float64)\n", "tensor([-0.0168, -0.0074, -0.0441, -0.1447], dtype=torch.float64)\n" ] } ], "source": [ "# Alignment support\n", "\n", "print(Q(state, data={**Q.data(), **{'kn': kn, 'ks': ks, 'dp': dp, 'dl': dl}}, alignment=True))\n", "print(C(state, data={**C.data(), **{'kn': kn, 'ks': ks, 'dp': dp, 'dl': dl}}, alignment=True))" ] } ], "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 }