{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "5cb92984-d64b-49ae-ad70-12261f26ddd0", "metadata": {}, "source": [ "# Example-18: Inverse tracking" ] }, { "cell_type": "code", "execution_count": 1, "id": "ba56d8c7-c426-4c33-8601-e7e853f575eb", "metadata": {}, "outputs": [], "source": [ "import torch\n", "\n", "from model.library.drift import Drift\n", "from model.library.quadrupole import Quadrupole\n", "from model.library.sextupole import Sextupole\n", "from model.library.octupole import Octupole\n", "from model.library.multipole import Multipole\n", "from model.library.dipole import Dipole\n", "from model.library.corrector import Corrector\n", "from model.library.gradient import Gradient\n", "from model.library.linear import Linear\n", "from model.library.bpm import BPM\n", "from model.library.marker import Marker\n", "from model.library.line import Line" ] }, { "cell_type": "code", "execution_count": 2, "id": "7e5f97a8-0dcc-4a56-b5f2-92a83d5d027e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0005, -0.0005, 0.0060, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0., 0., 0., 0.], dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0005, -0.0005, 0.0060, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0., 0., 0., 0.], dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0005, -0.0005, 0.0060, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-8.6736e-19, 2.1684e-19, 0.0000e+00, -4.3368e-19],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-2.7015e-10, -5.0000e-04, 6.0000e-03, 1.0000e-03],\n", " dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-2.6021e-18, 2.1684e-19, 8.6736e-19, -4.3368e-19],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Drift\n", "\n", "MF = Drift('MAG', length=1.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=False)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Drift('MAG', length=1.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Drift('MAG', length=1.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Drift('MAG', length=1.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True, insertion=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 3, "id": "15d0295c-1338-4cce-b921-4965cd9b27fc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-0.0002, -0.0043, 0.0075, 0.0095], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 6.5052e-19, 0.0000e+00, 3.4694e-18, -8.6736e-19],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-0.0002, -0.0043, 0.0075, 0.0095], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-8.6736e-19, -7.0473e-18, 6.0715e-18, -2.1684e-18],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0046, 0.0135, 0.0046, -0.0031], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-9.5410e-18, 1.1926e-18, 8.6736e-19, -7.8063e-18],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0005, -0.0005, 0.0055, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-7.8063e-18, -3.9031e-18, -2.6021e-18, -8.6736e-19],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Quadrupole\n", "\n", "MF = Quadrupole('MAG', length=0.5, kn=3.0, ks=-1.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=False)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Quadrupole('MAG', length=0.5, kn=3.0, ks=-1.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Quadrupole('MAG', length=0.5, kn=3.0, ks=-1.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Quadrupole('MAG', length=0.5, kn=3.0, ks=-1.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True, insertion=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 4, "id": "43136280-898c-4273-92f1-e74d95ab942d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0009, -0.0005, 0.0053, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0., 0., 0., 0.], dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0009, -0.0005, 0.0053, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0., 0., 0., 0.], dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0009, -0.0006, 0.0053, 0.0011], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-8.6736e-19, 1.0842e-19, -8.6736e-19, -4.3368e-19],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0007, -0.0006, 0.0053, 0.0011], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-8.6736e-19, 1.0842e-19, -8.6736e-19, 0.0000e+00],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Sextupole\n", "\n", "MF = Sextupole('MAG', length=0.25, ms=10.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=False)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Sextupole('MAG', length=0.25, ms=10.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Sextupole('MAG', length=0.25, ms=10.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Sextupole('MAG', length=0.25, ms=10.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True, insertion=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 5, "id": "25457662-44b4-4600-b7d2-61d59644a7b2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0009, -0.0005, 0.0052, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0., 0., 0., 0.], dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0009, -0.0005, 0.0052, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0., 0., 0., 0.], dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0009, -0.0005, 0.0052, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-8.6736e-19, 1.0842e-19, 0.0000e+00, 0.0000e+00],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0008, -0.0005, 0.0052, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-8.6736e-19, 0.0000e+00, 8.6736e-19, 0.0000e+00],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Octupole\n", "\n", "MF = Octupole('MAG', length=0.25, mo=25.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=False)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Octupole('MAG', length=0.25, mo=25.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Octupole('MAG', length=0.25, mo=25.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Octupole('MAG', length=0.25, mo=25.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True, insertion=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 6, "id": "c39b66b7-dc38-4e37-80db-126a0f625844", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0006, -0.0024, 0.0057, 0.0048], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 7.5894e-18, -4.3368e-19, -2.0817e-17, -2.8189e-18],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0006, -0.0024, 0.0057, 0.0048], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 7.3726e-18, -3.7947e-18, -2.0817e-17, -1.9516e-18],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0019, 0.0072, 0.0051, -0.0005], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-4.4235e-17, 3.0358e-18, 1.6480e-17, -4.1200e-18],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0007, -0.0006, 0.0053, 0.0011], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-3.7297e-17, 8.6736e-19, 9.5410e-18, 0.0000e+00],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Multipole\n", "\n", "MF = Multipole('MAG', length=0.25, kn=3.0, ks=-1.0, ms=10.0, mo=25.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=False)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Multipole('MAG', length=0.25, kn=3.0, ks=-1.0, ms=10.0, mo=25.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Multipole('MAG', length=0.25, kn=3.0, ks=-1.0, ms=10.0, mo=25.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Multipole('MAG', length=0.25, kn=3.0, ks=-1.0, ms=10.0, mo=25.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True, insertion=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 7, "id": "97072a39-c1d4-41e9-8b10-7054c1af59f5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([-0.0116, -0.0136, 0.0722, 0.1266], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([ 4.0549e-17, -4.4452e-17, -1.2663e-16, 2.5045e-16],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([ 0.0306, 0.0215, -0.0687, -0.1470], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([ 3.6689e-16, -8.8438e-16, -2.6489e-15, 4.6872e-15],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([-0.0021, -0.0006, 0.0069, -0.0007], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([ 3.7297e-17, 3.1550e-17, -1.0408e-17, 2.1250e-17],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([-0.0097, -0.0101, 0.0171, 0.0200], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([ 1.5400e-10, -2.2230e-10, -4.3635e-10, 5.0327e-10],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([ 0.0304, 0.0215, -0.0691, -0.1473], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([-6.6071e-09, -4.3597e-10, 9.8294e-09, -1.9903e-08],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([-0.0021, -0.0006, 0.0069, -0.0008], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, -0.0010], dtype=torch.float64)\n", "tensor([-1.6908e-12, 1.4334e-11, 2.0711e-11, -3.7469e-11],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Dipole\n", "\n", "MF = Dipole('MAG', length=2.0, angle=0.1, e1=0.01, e2=0.05, kn=3.0, ks=-1.0, ms=10.0, mo=25.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=False)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, -0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Dipole('MAG', length=2.0, angle=0.1, e1=0.01, e2=0.05, kn=3.0, ks=-1.0, ms=10.0, mo=25.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=False)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, -0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Dipole('MAG', length=2.0, angle=0.1, e1=0.01, e2=0.05, kn=3.0, ks=-1.0, ms=10.0, mo=25.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=False, insertion=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, -0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Dipole('MAG', length=2.0, angle=0.1, e1=0.01, e2=0.05, kn=1.0, ks=-1.0, ms=5.0, mo=10.0, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, -0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Dipole('MAG', length=2.0, angle=0.1, e1=0.01, e2=0.05, kn=3.0, ks=-1.0, ms=10.0, mo=25.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, -0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Dipole('MAG', length=2.0, angle=0.1, e1=0.01, e2=0.05, kn=3.0, ks=-1.0, ms=10.0, mo=25.0, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001, ns=10, exact=True, insertion=True)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, -0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 8, "id": "71912186-90e0-4906-b9a0-49c30c287738", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0.0010, 0.0095, 0.0050, 0.0510], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0000e+00, -4.3368e-19, 0.0000e+00, 8.6736e-19],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0.0011, 0.0095, 0.0055, 0.0510], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 8.6736e-19, -6.5052e-19, 0.0000e+00, 1.4962e-17],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Corrector\n", "\n", "MF = Corrector('MAG', cx=0.01, cy=0.05, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Corrector('MAG', cx=0.01, cy=0.05, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 9, "id": "791ff0ad-e469-457c-b98c-38861f60c9b1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0.0010, 0.0019, 0.0050, 0.0020], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0., 0., 0., 0.], dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0031, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 6.5052e-19, 1.0842e-18, -1.7347e-18, -2.1684e-19],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Gradient\n", "\n", "MF = Gradient('MAG', kn=0.1, ks=0.5, dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Gradient('MAG', kn=0.1, ks=0.5, dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 10, "id": "567cd2b1-562b-4d4d-a9f2-a7abe30b2779", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0056, 0.0107, 0.0026, -0.0038], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-2.8189e-18, 0.0000e+00, 5.2042e-18, 2.1684e-18],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-0.0009, -0.0046, 0.0097, 0.0083], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 8.6736e-19, 2.9273e-18, -1.7347e-18, -8.6736e-19],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Linear\n", "\n", "# Note, gives correct inverse for zero vector\n", "\n", "length = 1.0\n", "kn = - 2.0\n", "ks = + 1.5\n", "dp = 0.001\n", "Q = Quadrupole('Q', length, kn, ks, dp)\n", "\n", "state = torch.tensor([0.0, 0.0, 0.0, 0.0], dtype=torch.float64)\n", "\n", "matrix = torch.func.jacrev(lambda state, dp: Q(state, data={**Q.data(), **{'dp': dp}}), 0)(state, Q.dp)\n", "vector = torch.zeros_like(state)\n", "\n", "\n", "MF = Linear('MAG', (dp*vector).tolist(), matrix.tolist(), dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "\n", "MF = Linear('MAG', (dp*vector).tolist(), matrix.tolist(), dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 11, "id": "3ad43cda-127a-4f67-b0b8-c02fede912cd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([0., 0., 0., 0.], dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 4.3368e-19, 2.1684e-19, -1.7347e-18, -4.3368e-19],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Marker\n", "\n", "MF = Marker('MAG', dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = Marker('MAG', dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 12, "id": "45a8ed92-0026-48cc-8f92-2075241a996c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0011, -0.0005, 0.0047, 0.0011], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-2.1684e-19, 1.0842e-19, 0.0000e+00, 0.0000e+00],\n", " dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0054, 0.0010], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([ 2.1684e-19, 0.0000e+00, 0.0000e+00, -2.1684e-19],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# BPM\n", "\n", "xx = torch.tensor(+0.05, dtype=torch.float64)\n", "xy = torch.tensor(+0.01, dtype=torch.float64)\n", "yx = torch.tensor(+0.05, dtype=torch.float64)\n", "yy = torch.tensor(-0.06, dtype=torch.float64)\n", "\n", "MF = BPM('MAG', dp=0.001, dx=0.0, dy=0.0, dz=0.0, wx=0.0, wy=0.0, wz=0.0)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data={**MF.data(), **{'xx': xx, 'xy': xy, 'yx': yx, 'yy': yy}}, alignment=True))\n", "print(local := MI(local, data={**MI.data(), **{'xx': xx, 'xy': xy, 'yx': yx, 'yy': yy}}, alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "MF = BPM('MAG', dp=0.001, dx=0.01, dy=0.01, dz=-0.01, wx=0.001, wy=-0.001, wz=0.001)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data={**MF.data(), **{'xx': xx, 'xy': xy, 'yx': yx, 'yy': yy}}, alignment=True))\n", "print(local := MI(local, data={**MI.data(), **{'xx': xx, 'xy': xy, 'yx': yx, 'yy': yy}}, alignment=True))\n", "print(local - state)\n", "print()" ] }, { "cell_type": "code", "execution_count": 13, "id": "e896c00b-0bbe-49c1-b20e-cc1477c67ea8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-0.0043, -0.0001, 0.0121, -0.0014], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([5.5793e-16, 1.2804e-16, 1.7347e-18, 8.6736e-19], dtype=torch.float64)\n", "\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-0.0380, -0.0058, 0.0121, -0.0015], dtype=torch.float64)\n", "tensor([ 0.0010, -0.0005, 0.0050, 0.0010], dtype=torch.float64)\n", "tensor([-9.8145e-13, -1.4483e-13, 1.8322e-12, -7.0449e-13],\n", " dtype=torch.float64)\n", "\n" ] } ], "source": [ "# Line\n", "\n", "QF = Quadrupole('QF', 1.0, +0.25)\n", "QD = Quadrupole('QD', 1.0, -0.20)\n", "SF = Sextupole('SF', 0.25)\n", "SD = Sextupole('SD', 0.25)\n", "DR = Drift('DR', 0.25)\n", "BM = Dipole('BM', 3.50, torch.pi/8.0)\n", "\n", "MF = Line('FODO', \n", " [QF, DR, SF, DR, BM, DR, SD, DR, QD, DR, SD, DR, BM, DR, SF, DR], \n", " propagate=True, \n", " dp=0.001, \n", " exact=False, \n", " output=False,\n", " matrix = False)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\n", "print()\n", "\n", "\n", "MF = Line('FODO', \n", " [QF, DR, SF, DR, BM, DR, SD, DR, QD, DR, SD, DR, BM, DR, SF, DR], \n", " propagate=True, \n", " dp=0.001, \n", " exact=True, \n", " output=False,\n", " matrix = False)\n", "MI = MF.inverse()\n", "\n", "state = torch.tensor([0.001, -0.0005, 0.005, 0.001], dtype=torch.float64)\n", "\n", "print(local := state)\n", "print(local := MF(local, data=MF.data(), alignment=True))\n", "print(local := MI(local, data=MI.data(), alignment=True))\n", "print(local - state)\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 }