Updating the protolist generator to be more synchronized with th eonline model
This commit is contained in:
1 parent
ce38b4ff9f
commit
3457c7cb43
5 files changed
+28906
-29032
No files matched your search
Binary file not shown.
+28659
-28761
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,147 @@
|
||||
"""
|
||||
particle_transport.py
|
||||
|
||||
Transport a (position, direction) pair along a circular arc, as a charged
|
||||
particle would move in a uniform magnetic field B.
|
||||
|
||||
Physics
|
||||
-------
|
||||
A charged particle in a uniform field B moves on a circle of radius R lying
|
||||
in the plane perpendicular to B, with velocity always tangent to the circle.
|
||||
|
||||
Field orientation
|
||||
------------------
|
||||
- tilt = 0 -> B is as "vertical" as possible: take world-Y, project it into
|
||||
the plane perpendicular to the input direction (it must be
|
||||
perpendicular to the velocity), and normalize. That's B.
|
||||
- tilt = 90 -> B is "horizontal": the same vertical reference, rotated 90
|
||||
degrees around the input direction, i.e. tipped into the
|
||||
horizontal plane.
|
||||
- general tilt -> the vertical reference rotated by `tilt` degrees around the
|
||||
input direction.
|
||||
|
||||
Geometry
|
||||
--------
|
||||
Given unit velocity v0, field axis B (unit, perpendicular to v0), and
|
||||
radius R:
|
||||
|
||||
r0_hat = normalize(v0 x B) # unit vector from circle center to particle
|
||||
center = position - R * r0_hat
|
||||
|
||||
Rotating (position - center) and v0 by the same angle around B propagates
|
||||
the particle along the arc while preserving both the radius and the
|
||||
tangency of velocity to the circle, for any angle.
|
||||
|
||||
`bend_sign` flips which way the particle curves (equivalent to flipping the
|
||||
sign of charge*|B|); default +1.
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def normalize(v):
|
||||
v = np.asarray(v, dtype=float)
|
||||
n = np.linalg.norm(v)
|
||||
if n < 1e-12:
|
||||
raise ValueError("Cannot normalize a zero-length vector.")
|
||||
return v / n
|
||||
|
||||
|
||||
def rotate_vector(v, axis, angle_rad):
|
||||
"""Rotate vector v by angle_rad (radians) around unit vector `axis`,
|
||||
using Rodrigues' rotation formula."""
|
||||
axis = normalize(axis)
|
||||
v = np.asarray(v, dtype=float)
|
||||
cos_a = np.cos(angle_rad)
|
||||
sin_a = np.sin(angle_rad)
|
||||
return (
|
||||
v * cos_a
|
||||
+ np.cross(axis, v) * sin_a
|
||||
+ axis * np.dot(axis, v) * (1 - cos_a)
|
||||
)
|
||||
|
||||
|
||||
def _field_axis(v0, tilt_rad):
|
||||
"""Build the unit field axis B: world-Y projected perpendicular to v0,
|
||||
then rotated around v0 by tilt_rad."""
|
||||
y_axis = np.array([0.0, 1.0, 0.0])
|
||||
y_perp = y_axis - np.dot(y_axis, v0) * v0
|
||||
|
||||
if np.linalg.norm(y_perp) < 1e-9:
|
||||
# v0 is (anti)parallel to Y -> "vertical" is undefined; fall back to
|
||||
# an arbitrary reference perpendicular to v0.
|
||||
helper = np.array([1.0, 0.0, 0.0])
|
||||
y_perp = helper - np.dot(helper, v0) * v0
|
||||
if np.linalg.norm(y_perp) < 1e-9:
|
||||
helper = np.array([0.0, 0.0, 1.0])
|
||||
y_perp = helper - np.dot(helper, v0) * v0
|
||||
|
||||
vertical_ref = normalize(y_perp)
|
||||
|
||||
if abs(tilt_rad) < 1e-12:
|
||||
return vertical_ref
|
||||
return normalize(rotate_vector(vertical_ref, v0, tilt_rad))
|
||||
|
||||
|
||||
def transport_particle(pointing, position, angle_deg, radius, tilt_deg=0.0,
|
||||
bend_sign=1.0):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
pointing : array-like (3,) unit velocity/direction vector at the start
|
||||
position : array-like (3,) starting position
|
||||
angle_deg : float arc angle to sweep, degrees
|
||||
radius : float radius of the circular arc (R > 0)
|
||||
tilt_deg : float field tilt, degrees. 0 = vertical field,
|
||||
90 = horizontal field (see module docstring)
|
||||
bend_sign : float +1 or -1, flips the curving direction
|
||||
(equivalent to flipping charge * field sign)
|
||||
|
||||
Returns
|
||||
-------
|
||||
new_pointing : np.ndarray (3,) unit direction after sweeping angle_deg
|
||||
new_position : np.ndarray (3,) position after sweeping angle_deg
|
||||
"""
|
||||
v0 = normalize(pointing)
|
||||
p0 = np.asarray(position, dtype=float)
|
||||
theta = np.radians(angle_deg)
|
||||
tilt = np.radians(tilt_deg)
|
||||
|
||||
B = _field_axis(v0, tilt)
|
||||
|
||||
r0_hat = bend_sign * normalize(np.cross(v0, B))
|
||||
center = p0 - radius * r0_hat
|
||||
|
||||
offset0 = radius * r0_hat
|
||||
new_offset = rotate_vector(offset0, B, theta)
|
||||
new_position = center + new_offset
|
||||
new_pointing = normalize(rotate_vector(v0, B, theta))
|
||||
|
||||
return new_pointing, new_position
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Sanity check: moving +X, vertical field (tilt=0), radius=2, 90 deg arc.
|
||||
v0 = [1, 0, 0]
|
||||
p0 = [0, 0, 0]
|
||||
R = 2.0
|
||||
|
||||
new_v, new_p = transport_particle(v0, p0, angle_deg=90, radius=R, tilt_deg=0)
|
||||
print("tilt=0, 90 deg arc:")
|
||||
print(" new pointing:", new_v)
|
||||
print(" new position:", new_p)
|
||||
|
||||
# Verify: distance from arc center should stay == R for any angle.
|
||||
B = _field_axis(normalize(v0), 0.0)
|
||||
r0_hat = normalize(np.cross(normalize(v0), B))
|
||||
center = np.asarray(p0, dtype=float) - R * r0_hat
|
||||
for ang in [0, 30, 90, 180, 270, 360]:
|
||||
_, pos = transport_particle(v0, p0, angle_deg=ang, radius=R, tilt_deg=0)
|
||||
dist = np.linalg.norm(pos - center)
|
||||
print(f" angle={ang:>3} dist_from_center={dist:.6f} (should be {R})")
|
||||
|
||||
print()
|
||||
print("tilt=90 (horizontal field), 90 deg arc:")
|
||||
new_v2, new_p2 = transport_particle(v0, p0, angle_deg=90, radius=R, tilt_deg=90)
|
||||
print(" new pointing:", new_v2)
|
||||
print(" new position:", new_p2)
|
||||
@@ -498,26 +498,27 @@ class BeamlineEditor(QtWidgets.QMainWindow, Ui_BeamlineGUI):
|
||||
Ref='relative'
|
||||
line.append(self.elementDB[ele],sRef=sRef,Ref=Ref)
|
||||
self.elementDB[ele].Prefix=name
|
||||
# check for branching point
|
||||
if type == 'Dipole' and self.elementDB[ele].branching:
|
||||
doBranching = False
|
||||
if not self.flattenBranchID is None:
|
||||
if ele in self.flattenBranchID:
|
||||
idx = self.flattenBranchID.index(ele)
|
||||
doBranching = self.flattenSwitches[idx]
|
||||
else:
|
||||
if name in self.flattenBranchNames :
|
||||
idx = self.flattenBranchNames.index(name)
|
||||
doBranching = self.flattenSwitches[idx]
|
||||
if doBranching:
|
||||
print(' Branching at element:', name + '-' + self.elementDB[ele].Name, 'with angle:', self.elementDB[ele].design_angle)
|
||||
self.elementDB[ele].angle = self.elementDB[ele].design_angle
|
||||
line.hasBranchPoint = True
|
||||
secondBranch=True
|
||||
break
|
||||
else:
|
||||
self.elementDB[ele].angle=0
|
||||
line.hasBranchPoint = False
|
||||
# check for branching point and dipoles
|
||||
if type == 'Dipole':
|
||||
self.elementDB[ele].angle=self.elementDB[ele].design_angle
|
||||
if self.elementDB[ele].branching:
|
||||
doBranching = False
|
||||
if not self.flattenBranchID is None:
|
||||
if ele in self.flattenBranchID:
|
||||
idx = self.flattenBranchID.index(ele)
|
||||
doBranching = self.flattenSwitches[idx]
|
||||
else:
|
||||
if name in self.flattenBranchNames :
|
||||
idx = self.flattenBranchNames.index(name)
|
||||
doBranching = self.flattenSwitches[idx]
|
||||
if doBranching:
|
||||
print(' Branching at element:', name + '-' + self.elementDB[ele].Name, 'with angle:', self.elementDB[ele].design_angle)
|
||||
line.hasBranchPoint = True
|
||||
secondBranch=True
|
||||
break
|
||||
else:
|
||||
self.elementDB[ele].angle=0
|
||||
line.hasBranchPoint = False
|
||||
if secondBranch:
|
||||
#print("There should be now following the second branch from container:", name)
|
||||
if tree.secondary:
|
||||
|
||||
+79
-251
@@ -1,6 +1,7 @@
|
||||
from onlinemodel.core.apptemplate import ApplicationTemplate
|
||||
import math
|
||||
import numpy as np
|
||||
from beamline_editor.particle_transport import transport_particle
|
||||
|
||||
class ProtoListe(ApplicationTemplate):
|
||||
# any calculation for the explicit layout of the machine
|
||||
@@ -31,101 +32,13 @@ class ProtoListe(ApplicationTemplate):
|
||||
#self.SF.writeFacility(self)
|
||||
|
||||
def setRef(self,sin=0,zin=0,xin=0,yin=1.2,exin=0,eyin=0,ezin=1):
|
||||
self.s=sin
|
||||
self.z=zin
|
||||
self.x=xin
|
||||
self.y=yin
|
||||
# vector of direction
|
||||
self.ex=exin
|
||||
self.ey=eyin
|
||||
self.ez=ezin
|
||||
|
||||
self.vertx=0
|
||||
self.verty=0
|
||||
self.vertz=0
|
||||
self.verts=0
|
||||
|
||||
# some stuff to make it easier to generate the holy list.
|
||||
self.p1 = np.array([sin,xin,yin,zin]) # starting point
|
||||
self.pv = np.array([0,0,0,0]) # center point/vertex point
|
||||
self.p2 = np.array([0,0,0,0]) # end point
|
||||
self.ev1 = np.array([1,exin,eyin,ezin])
|
||||
self.ev2 = np.array([1,exin,eyin,ezin])
|
||||
self.ev = np.array([0,0,0])
|
||||
|
||||
|
||||
def advance(self, L, angle=0, tilt=0): # this is independent on the new coordinate system for the holy list
|
||||
self.vertx = 0
|
||||
self.verty = 0
|
||||
self.vertz = 0
|
||||
self.verts = self.s
|
||||
self.s = self.s + L
|
||||
if angle == 0:
|
||||
self.x = self.x + L * self.ex
|
||||
self.y = self.y + L * self.ey
|
||||
self.z = self.z + L * self.ez
|
||||
else:
|
||||
# save initial coordinates
|
||||
xtmp = self.x
|
||||
ytmp = self.y
|
||||
ztmp = self.z
|
||||
# radius
|
||||
R = L / angle
|
||||
# rotation vector - default around y axis
|
||||
Rproj = self.ey # R dot e = Ry*ey=ey
|
||||
Rx = -Rproj * self.ex
|
||||
Ry = 1 - Rproj * self.ey
|
||||
Rz = -Rproj * self.ez
|
||||
# Orthonormal vector to direction of propagation and rotation axis
|
||||
Ox = Ry * self.ez - Rz * self.ey
|
||||
Oy = Rz * self.ex - Rx * self.ez
|
||||
Oz = Rx * self.ey - Ry * self.ex
|
||||
Rnorm = math.sqrt(Rx * Rx + Ry * Ry + Rz * Rz)
|
||||
Onorm = math.sqrt(Ox * Ox + Oy * Oy + Oz * Oz)
|
||||
Rx = Rx / Rnorm
|
||||
Ry = Ry / Rnorm
|
||||
Rz = Rz / Rnorm
|
||||
Ox = Ox / Onorm
|
||||
Oy = Oy / Onorm
|
||||
Oz = Oz / Onorm
|
||||
# Rot Vector is the axis of rotation and Ort vector pointing to the origin of the rotation
|
||||
Ortx = Ox * math.cos(tilt) - Rx * math.sin(tilt)
|
||||
Orty = Oy * math.cos(tilt) - Ry * math.sin(tilt)
|
||||
Ortz = Oz * math.cos(tilt) - Rz * math.sin(tilt)
|
||||
# the origin of the rotation
|
||||
X0 = self.x + R * Ortx
|
||||
Y0 = self.y + R * Orty
|
||||
Z0 = self.z + R * Ortz
|
||||
# rotation
|
||||
|
||||
self.vertx = X0 - R * (Ortx * math.cos(angle * 0.5) - self.ex * math.sin(angle * 0.5)) / math.cos(angle * 0.5)
|
||||
self.verty = Y0 - R * (Orty * math.cos(angle * 0.5) - self.ey * math.sin(angle * 0.5)) / math.cos(angle * 0.5)
|
||||
self.vertz = Z0 - R * (Ortz * math.cos(angle * 0.5) - self.ez * math.sin(angle * 0.5)) / math.cos(angle * 0.5)
|
||||
self.x = X0 - R * (Ortx * math.cos(angle) - self.ex * math.sin(angle))
|
||||
self.y = Y0 - R * (Orty * math.cos(angle) - self.ey * math.sin(angle))
|
||||
self.z = Z0 - R * (Ortz * math.cos(angle) - self.ez * math.sin(angle))
|
||||
self.ex = (self.ex * math.cos(angle) + Ortx * math.sin(angle))
|
||||
self.ey = (self.ey * math.cos(angle) + Orty * math.sin(angle))
|
||||
self.ez = (self.ez * math.cos(angle) + Ortz * math.sin(angle))
|
||||
# calculate vertex
|
||||
ds = (self.vertx - xtmp) * (self.vertx - xtmp) + (self.verty - ytmp) * (self.verty - ytmp) + (
|
||||
self.vertz - ztmp) * (self.vertz - ztmp)
|
||||
self.verts = self.verts + math.sqrt(ds)
|
||||
|
||||
|
||||
def swap(self):
|
||||
xtmp=self.x
|
||||
ytmp=self.y
|
||||
ztmp=self.z
|
||||
stmp=self.s
|
||||
self.x=self.vertx
|
||||
self.y=self.verty
|
||||
self.z=self.vertz
|
||||
self.s=self.verts
|
||||
self.vertx=xtmp
|
||||
self.verty=ytmp
|
||||
self.vertz=ztmp
|
||||
self.verts=stmp
|
||||
self.p1 = np.array([sin, xin, yin, zin], dtype=float) # starting point
|
||||
self.pv = np.array([0., 0., 0., 0.], dtype=float) # center point/vertex point
|
||||
self.p2 = self.p1.copy()
|
||||
self.ev1 = np.array([1., exin, eyin, ezin], dtype=float)
|
||||
self.ev2 = np.array([1., exin, eyin, ezin], dtype=float)
|
||||
|
||||
|
||||
def getDomain(self,name):
|
||||
@@ -173,14 +86,16 @@ class ProtoListe(ApplicationTemplate):
|
||||
# self.p1 = self.pv - 0.5 * elelength*self.ev1
|
||||
# self.p2 = self.pv + 0.5 * elelength*self.ev1
|
||||
|
||||
|
||||
angh=np.arctan2(self.ev[0],self.ev[2])*180/np.pi
|
||||
rh = np.sqrt(self.ev[2]*self.ev[2]+self.ev[0]*self.ev[0])
|
||||
angv=np.arctan2(self.ev[1],rh)*180/np.pi
|
||||
angh = np.arctan2(self.ev1[1], self.ev1[3]) * 180 / np.pi
|
||||
rh = np.sqrt(self.ev1[3] * self.ev1[3] + self.ev1[1] * self.ev1[1])
|
||||
angv = np.arctan2(self.ev1[2], rh) * 180 / np.pi
|
||||
|
||||
|
||||
pos_s=self.s
|
||||
pos_z=self.z
|
||||
pos_s = self.pv[0]
|
||||
pos_x = self.pv[1]
|
||||
pos_y = self.pv[2]
|
||||
pos_z = self.pv[3]
|
||||
|
||||
|
||||
if name in self.order:
|
||||
return
|
||||
@@ -207,7 +122,7 @@ class ProtoListe(ApplicationTemplate):
|
||||
|
||||
|
||||
info={'Domain': domain, 'Prefix': prefix, 'Suffix': suffix, 'Index': index,
|
||||
's (m)': pos_s, 'z (m)': pos_z, 'x (m)': self.x, 'y (m)': self.y, 'L (m)': elelength,
|
||||
's (m)': pos_s, 'z (m)': pos_z, 'x (m)': pos_x, 'y (m)': pos_y, 'L (m)': elelength,
|
||||
'Reference': reference, 'Group': group, 'Baugruppe': baugruppe, 'Variante': 'A', 'Roll': roll,
|
||||
'PS-Ch.': psch,'P1':self.p1+ds,'PV':self.pv+ds,'P2':self.p2+ds,'EV1':self.ev1,'EV2':self.ev2,
|
||||
'Yaw': angh, 'Pitch': angv, 'Alignment': align}
|
||||
@@ -234,25 +149,24 @@ class ProtoListe(ApplicationTemplate):
|
||||
info = {'Domain': domain, 'Prefix': ele.Name, 'Suffix': '', 'Index': '',
|
||||
's (m)': self.s, 'z (m)': self.z, 'x (m)': self.x, 'y (m)': self.y, 'L (m)': 0,
|
||||
'Reference': 'Start', 'Group': 'Section', 'Baugruppe': 'Marker', 'Variante': 'A', 'Roll': 0,
|
||||
'PS-Ch.': 0, 'P1': self.p1, 'PV': self.p1, 'P2': self.p1, 'EV1': self.ev1,
|
||||
'EV2': self.ev1, 'Yaw': angh, 'Pitch': angv, 'Alignment': align}
|
||||
'PS-Ch.': 0, 'P1': self.p1, 'PV': self.pv, 'P2': self.p2, 'EV1': self.ev1,
|
||||
'EV2': self.ev2, 'Yaw': angh, 'Pitch': angv, 'Alignment': align}
|
||||
self.info[ele.Name] = info
|
||||
self.order.append(ele.Name)
|
||||
return
|
||||
|
||||
def writeDrift(self,s):
|
||||
self.p1 = self.p1 + s * self.ev1 # no change in vector - should replace advance in the future
|
||||
self.advance(s,0)
|
||||
|
||||
|
||||
def writeAlignment(self,ele):
|
||||
# print info of the element and advances the layout position over the reserved length
|
||||
if 'dx' in ele.__dict__:
|
||||
self.x=self.x+ele.dx
|
||||
self.p1[1] += ele.dx
|
||||
if 'dy' in ele.__dict__:
|
||||
self.y=self.y+ele.dy
|
||||
self.p1[2] += ele.dy
|
||||
self.advance(ele.getResLength(),0)
|
||||
self.writeDrift(ele.getResLength())
|
||||
|
||||
|
||||
|
||||
def writeMarker(self,ele):
|
||||
@@ -260,9 +174,9 @@ class ProtoListe(ApplicationTemplate):
|
||||
name = ele.Prefix + '-' + ele.Name
|
||||
domain = self.getDomain(name)
|
||||
align = 'Entrance'
|
||||
angh = np.arctan2(self.ev[0], self.ev[2]) * 180 / np.pi
|
||||
rh = np.sqrt(self.ev[2] * self.ev[2] + self.ev[0] * self.ev[0])
|
||||
angv = np.arctan2(self.ev[1], rh) * 180 / np.pi
|
||||
angh = np.arctan2(self.ev1[1], self.ev1[3]) * 180 / np.pi
|
||||
rh = np.sqrt(self.ev1[3] * self.ev1[3] + self.ev1[1] * self.ev1[1])
|
||||
angv = np.arctan2(self.ev1[2], rh) * 180 / np.pi
|
||||
if ele.Group == 'Start':
|
||||
group = 'Section'
|
||||
bg ='Marker'
|
||||
@@ -278,31 +192,25 @@ class ProtoListe(ApplicationTemplate):
|
||||
else:
|
||||
return
|
||||
info = {'Domain': domain, 'Prefix': prefix, 'Suffix': suffix, 'Index': index,
|
||||
's (m)': self.s, 'z (m)': self.z, 'x (m)': self.x, 'y (m)': self.y, 'L (m)': 0,
|
||||
's (m)': self.p1[0], 'z (m)': self.p1[3], 'x (m)': self.p1[1], 'y (m)': self.p1[2], 'L (m)': 0,
|
||||
'Reference': 'Start', 'Group': group, 'Baugruppe': bg, 'Variante': 'A', 'Roll': 0,
|
||||
'PS-Ch.': 0, 'P1': self.p1, 'PV': self.p1, 'P2': self.p1, 'EV1': self.ev1,
|
||||
'EV2': self.ev1, 'Yaw': angh, 'Pitch': angv, 'Alignment': align}
|
||||
'PS-Ch.': 0, 'P1': self.p1, 'PV': self.pv, 'P2': self.p2, 'EV1': self.ev1,
|
||||
'EV2': self.ev2, 'Yaw': angh, 'Pitch': angv, 'Alignment': align}
|
||||
self.info[ele.Name] = info
|
||||
self.order.append(ele.Name)
|
||||
return
|
||||
|
||||
|
||||
if ele.Group == 'Marker':
|
||||
if 'MREF' in ele.Name:
|
||||
self.write((ele.Name, ele.LengthRes, 'Start', ele.Group, ele.Tag, ele.Baugruppe, 0, 0))
|
||||
if ele.Group=='Start':
|
||||
self.writeLine(ele,[])
|
||||
return
|
||||
|
||||
|
||||
def writeVacuum(self,ele): # all components, which have a well defined vacuum vessel.
|
||||
self.p2 = self.p1 + ele.LengthRes*self.ev1
|
||||
self.pv = self.p1
|
||||
self.ev2 = self.ev1
|
||||
self.ev = self.ev1[1:4]
|
||||
|
||||
name = ele.Prefix+'-'+ele.Name
|
||||
self.write((name,ele.LengthRes,'Start',ele.Group,ele.Tag,ele.Baugruppe,0,0))
|
||||
self.p1 = self.p2 # advance position
|
||||
self.advance(ele.LengthRes,0)
|
||||
|
||||
|
||||
def writeRF(self,ele):
|
||||
self.writeVacuum(ele)
|
||||
@@ -352,17 +260,16 @@ class ProtoListe(ApplicationTemplate):
|
||||
if channels <0:
|
||||
channels = 0
|
||||
# print info of the element and advances the layout position over the reserved length
|
||||
reference='Center'
|
||||
if 'MBN' in ele.Name:
|
||||
reference='Vertex'
|
||||
|
||||
self.advance(ele.sRef+ele.Length*0.5,0) # propagation to the center point
|
||||
self.p1 = self.p1 + ele.sRef*self.ev1
|
||||
self.pv = self.p1 + 0.5 * ele.Length*self.ev1
|
||||
self.p2 = self.p1 + ele.Length*self.ev1
|
||||
self.ev2 = self.ev1
|
||||
self.ev = self.ev1[1:4]
|
||||
name = ele.Prefix+'-'+ele.Name
|
||||
self.write((name,ele.Length,'Center',ele.Group,ele.Tag,ele.Baugruppe,ele.Tilt,channels))
|
||||
self.write((name,ele.Length,reference,ele.Group,ele.Tag,ele.Baugruppe,ele.Tilt,channels))
|
||||
self.p1 = self.p2 + (ele.LengthRes-ele.sRef-ele.Length)*self.ev1
|
||||
self.advance(ele.LengthRes-ele.sRef-ele.Length*0.5,0) # residual space including half the magnet length
|
||||
|
||||
|
||||
|
||||
@@ -370,140 +277,61 @@ class ProtoListe(ApplicationTemplate):
|
||||
|
||||
def writeBend(self,ele):
|
||||
# print info of the element and advances the layout position over the reserved length
|
||||
if ele.design_angle==0:
|
||||
self.writeMagnets(ele)
|
||||
return
|
||||
|
||||
self.p1 = self.p1 + ele.sRef * self.ev1 # entrance of magnet.
|
||||
|
||||
# calculate the vertex, where we need the design angle and not the actual angle.
|
||||
angletmp = ele.angle
|
||||
ele.angle = ele.design_angle # for vertex we need to calculate with the design angle.
|
||||
angle = np.pi * ele.design_angle / 180.
|
||||
L = ele.getLength() # Full path length
|
||||
tilt = ele.Tilt
|
||||
R = L / angle
|
||||
|
||||
ev1 = self.ev1[1:4].copy()
|
||||
p1 = self.p1[1:4].copy()
|
||||
|
||||
ev2, p2 = transport_particle(ev1, p1, ele.angle, np.abs(R), -tilt, np.sign(R))
|
||||
self.ev2[1:4] = ev2.copy()
|
||||
self.ev2[0] = 1
|
||||
self.p2[1:4] = p2.copy()
|
||||
self.p2[0] = self.p1[0] + L
|
||||
|
||||
if np.abs(tilt) > 0.001:
|
||||
print(ele.Prefix,ele.Name,tilt,ele.design_angle,angletmp)
|
||||
print(self.ev1)
|
||||
print(self.ev2)
|
||||
|
||||
|
||||
channels = 1
|
||||
if 'PSChannels' in ele.__dict__:
|
||||
channels = ele['PSChannels']
|
||||
name = ele.Prefix+'-'+ele.Name
|
||||
if ele.Baugruppe[0:3]=='AFP':
|
||||
channels=0
|
||||
|
||||
self.advance(ele.sRef,0)
|
||||
self.p1 = self.p1 + ele.sRef*self.ev1
|
||||
if (ele.design_angle==0):
|
||||
self.pv = self.p1 + 0.5*ele.getLength()*self.ev1
|
||||
self.p2 = self.p1 + ele.getLength()*self.ev1
|
||||
self.ev2 = self.ev1
|
||||
self.ev= self.ev1[1:4]
|
||||
self.advance(0.5*ele.getLength(),0)
|
||||
if ele.Name not in self.dipole.keys():
|
||||
self.dipole[name] = {'Angle': ele.design_angle, 'Alignment': 'Entrance', 'Radius': '',
|
||||
'Transverse Wander': 0, 'Shift': 0, 'Length': ele.getLength()}
|
||||
self.write((name, ele.getLength(), 'Vertex', ele.Group, ele.Tag, ele.Baugruppe,
|
||||
ele.Tilt , channels))
|
||||
self.p1 = self.p2+(ele.getResLength()-ele.sRef-ele.getLength())*self.ev1
|
||||
self.advance(ele.getResLength()-ele.sRef-0.5*ele.getLength(), 0)
|
||||
self.pv = self.p1 + np.tan(0.5 * angle) * R * self.ev1 # advancing to the vertex
|
||||
self.write((name, ele.Length, 'Vertex', ele.Group, ele.Tag, ele.Baugruppe, ele.Tilt, channels))
|
||||
|
||||
# now do the true transport:
|
||||
ele.angle = angletmp
|
||||
print(ele.Prefix,ele.Name,ele.angle)
|
||||
if np.abs(ele.angle) < 1e-5:
|
||||
self.p1 = self.p1 + (ele.getResLength() - ele.sRef) * self.ev1
|
||||
self.ev2 = self.ev1.copy() # using initial vector since there is no net deflection
|
||||
else:
|
||||
# print('Bending magnet', ele.Name,ele.angle,ele.design_angle,ele.sRef)
|
||||
xtmp=self.x
|
||||
ytmp=self.y
|
||||
ztmp=self.z
|
||||
stmp=self.s
|
||||
extmp=self.ex
|
||||
eytmp=self.ey
|
||||
eztmp=self.ez
|
||||
if not ele.branching:
|
||||
ele.angle = ele.design_angle
|
||||
angletmp=ele.angle
|
||||
ele.angle=ele.design_angle
|
||||
angrad = np.pi* ele.angle / 180.
|
||||
|
||||
# ignoring tilt:
|
||||
R = ele.getLength()/angrad
|
||||
M = np.zeros((4,4))
|
||||
|
||||
M[0,0]=1
|
||||
M[2,2]= 1
|
||||
M[1, 1] = np.cos(angrad)
|
||||
M[3, 3] = np.cos(angrad)
|
||||
M[1, 3] = np.sin(angrad)
|
||||
M[3, 1] = -np.sin(angrad)
|
||||
|
||||
self.ev2 = np.matmul(M,self.ev1)
|
||||
radtilt = ele.Tilt*np.pi/180.
|
||||
if not ele.Tilt == 0:
|
||||
vec1 = np.array(self.ev1[1:])
|
||||
vec2 = np.array(self.ev2[1:])
|
||||
proj = np.sum(vec1*vec2)
|
||||
vecper = vec2 - proj*vec1
|
||||
vecpar = vec2 - vecper
|
||||
veccro = np.cross(vec1,vecper)
|
||||
self.ev2[1:] = vecpar+np.cos(radtilt)*vecpar-np.sin(radtilt)*veccro
|
||||
|
||||
Ld = R * np.sin(angrad*0.5)
|
||||
T = Ld/np.cos(angrad*0.5)
|
||||
self.pv = self.p1 + T * self.ev1
|
||||
self.p2 = self.pv + T * self.ev2
|
||||
self.ev = (1-ele.e1) * self.ev1[1:4] + (1-ele.e2) * self.ev2[1:4]
|
||||
|
||||
# calculate shifts of dipoles for good field region
|
||||
R = ele.getLength() / angrad;
|
||||
dR = R * (1 - math.cos(angrad)) # beam wander
|
||||
shift = 0.5
|
||||
if (ele.e1 == 0.5):
|
||||
dR = R * (1 - math.cos(angrad * 0.5))
|
||||
shift = -0.5
|
||||
vec1 = np.array(self.ev1[1:])
|
||||
vec2 = np.array(self.ev2[1:])
|
||||
vnorm = np.cross(vec1, vec2)
|
||||
vrot = (1 - ele.e1) * vec1 + (1 - ele.e2) * vec2
|
||||
vshift = np.cross(vnorm, vrot)
|
||||
vlen = np.sqrt(np.sum(vshift*vshift))
|
||||
# ###############################
|
||||
# disable shift in holy list!!!!
|
||||
#
|
||||
vlen = 0
|
||||
|
||||
if vlen > 0:
|
||||
vshift = vshift /vlen
|
||||
eshift = np.sign(angrad)*np.array([0,vshift[0],vshift[1],vshift[2]])*shift*dR
|
||||
self.p1 += eshift
|
||||
self.pv += eshift
|
||||
self.p2 += eshift
|
||||
|
||||
# advance according to design angle
|
||||
self.advance(ele.getLength(), angrad, radtilt)
|
||||
self.swap()
|
||||
|
||||
if ele.Name not in self.dipole.keys():
|
||||
if ele.e2 == 1:
|
||||
align = 'Entrance'
|
||||
elif ele.e1 ==1:
|
||||
align = 'Exit'
|
||||
else:
|
||||
align = 'Middle'
|
||||
self.dipole[name] = {'Angle': ele.design_angle, 'Alignment': align, 'Radius': np.abs(R),
|
||||
'Transverse Wander': np.abs(dR), 'Shift': shift*dR, 'Length': ele.getLength()}
|
||||
|
||||
self.write((name, ele.Length, 'Vertex', ele.Group, ele.Tag, ele.Baugruppe, ele.Tilt , channels))
|
||||
self.swap()
|
||||
|
||||
if vlen > 0:
|
||||
self.p1 -= eshift
|
||||
self.pv -= eshift
|
||||
self.p2 -= eshift
|
||||
|
||||
# restore position before dipole and then advance according to given angle (this matters for branching point dipoles
|
||||
self.x=xtmp
|
||||
self.y=ytmp
|
||||
self.z=ztmp
|
||||
self.s=stmp
|
||||
self.ex=extmp
|
||||
self.ey=eytmp
|
||||
self.ez=eztmp
|
||||
|
||||
ele.angle=angletmp
|
||||
if ele.angle == 0:
|
||||
self.p1 = self.p1 + ele.getLength() *self.ev1
|
||||
else:
|
||||
ps = self.p1[0]+ele.getLength()
|
||||
self.p1 = self.p2
|
||||
self.p1[0]=ps
|
||||
self.ev1 = self.ev2
|
||||
angrad=np.pi*ele.angle/180.
|
||||
Lpath=ele.getLength()
|
||||
self.advance(Lpath,angrad,radtilt)
|
||||
self.advance(ele.getResLength()-ele.sRef-Lpath,0)
|
||||
self.p1 = self.p1 + (ele.getResLength()-ele.sRef-Lpath)*self.ev1
|
||||
self.p1 = self.p2 + (ele.getResLength() - ele.sRef - ele.getLength()) * self.ev2
|
||||
self.ev1 = self.ev2.copy() # get new pointing
|
||||
|
||||
if np.abs(tilt) > 0.001:
|
||||
print('Nocheinmal - following the arc')
|
||||
print(ele.Prefix,ele.Name,tilt,ele.design_angle)
|
||||
print(self.ev1)
|
||||
print(self.ev2)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user