# ocelot.cpbd.high_order¶

## Module Contents¶

### Functions¶

 t_nnn_transport(L, h, k1, k2, energy=0) here is used the following set of variables: t_nnn_mad(L, h, k1, k2, energy=0) Universal function to return second order matrix. t_nnn(L, h, k1, k2, energy=0) fringe_ent(h, k1, e, h_pole=0.0, gap=0.0, fint=0.0) Calculate first and second order matrices for edge focusing element. fringe_ext(h, k1, e, h_pole=0.0, gap=0.0, fint=0.0) Calculate first and second order matrices for edge focusing element. H23(vec_x, h, k1, k2, beta=1.0, g_inv=0.0) {x, px}, {y, py}, {sigma, ps} verlet(vec_x, step, h, k1, k2, beta=1.0, g_inv=0.0) q_{n+1} = q_{n} + h * dH(p_{n}, q_{n+1})/dp verlet1O(vec_x, step, h, k1, k2, beta=1.0, g_inv=0.0) q_{n+1} = q_{n} + h * dH(p_{n}, q_{n+1})/dp sym_map(z, X, h, k1, k2, energy=0.0) moments_py(bx, by, Bx, By, Bz, dzk) mx = v/(dz/dt)*e/p*(y’*Bz - By*(1.+x’^2) + x’*y’*Bx)*dz rk_track_in_field(y0, s_stop, N, energy, mag_field, s_start=0.0) Runge-Kutta solver of the exact trajectory equations in the fixed coordinate system {X, Y, Z]}. rk_field(rparticles, s_start, s_stop, N, energy, mag_field, long_dynamics=True) Method to track particles through mag field scipy_track_in_field(y0, l, N, energy, mag_field) rk_field_scipy(y0, l, N, energy, mag_field) track_und_weave(y0, z, kz, kx, Kx, energy) track_und_weave_openmp(u, l, N, kz, kx, Kx, energy) arcline(SREin, Delta_S, dS, R_vect) Martin Dohlus DESY, 2015
ocelot.cpbd.high_order.__author__ = Sergey Tomin
ocelot.cpbd.high_order._logger
ocelot.cpbd.high_order.nb_flag = True
ocelot.cpbd.high_order.__MAD__ = True

differential equation:

t_nnn’’ + kx**2*t_nnn = f_nnn

here index nnn means (1,2,3,4,5,6) = (x, x’, y, y’, s, delta)

# h = 1/r0 - curvature # k1 = 1/Bro*dBy/dx - gradient # k2 = 1/Bro*d^2(By)/dx^2 - sextupole component # h1 - derivative of h # h11 - derivative of h1 # cx1, sx1, cy1 … - derivative of cx, sx, cy, … and so on # cx = cos(kx*s) # sx = sin(kx*s)/kx # dx = h/kx2*(1. - cx) # cy = cos(ky*s) # sy = sin(ky*s)/ky

Defenition: Brown -> OCELOT n = ky**2/h**2 beta = K2/h**3

# Green’s function for X is Gx(t, tau) = 1/kx*sin(kx*(t - tau))

f111 = -(h**3 + K2 - 2*h*ky**2)*cx**2 + 1./2.*h*kx**4*sx*2 - kx**3*cx*sx*h’ f112 = - 2 * kx * (h**3 + K2 - 2 * h * ky**2) * cx * sx - h * kx**2 * cx * sx + (cx**2 - kx**2 * sx**2)*h’ f116 = (2*h^2 -ky^2)* cx + 2 (h^3+K2-2 *h *ky^2) *cx dx-h^2*kx^2 sx^2 + (h *kx *cx *sx-dx kx^2*sx)* h’ f122 = 1/2* h* cx^2 + h^3* (-1-K2/h^3+(2 ky^2)/h^2) sx^2 + sx *sx h’ f126 = h^2 (2-ky^2/h^2) sx+h^2 *cx sx + 2 h^3 (-1-K2/h^3+(2 ky^2)/h^2) dx sx + (cx dx +h sx^2) h’ f166 = -h + dx*h^2*(2-ky^2/h^2) + dx^2*h^3*(-1-K2/h^3+(2 ky^2)/h^2) + 1/2*h*(dx’)^2 + dx*dx’* h’ f133 = -(1/2)* h*ky4*sy^2 - ky2*cy* sy*h’ + 1/2*cy^2*(2*K2 - h*ky^2+h’’) = |h’ = 0, h’’ = 0 | = -(1/2)* h*ky2 + cy**2*K2 f134 = h*ky2*cy*sy + (cy2 - sy2*ky2)*h’ + cy*sy*(2*k2 - h*ky2 + h’’) f144 = -(1/2)*h*cy2 + cy*sy*h’ + (sy2*(2*K2 - h*ky2 + h’’))/2 =|h’ = 0, h’’ = 0 | = -(1/2) *h +sy^2*K2

# Green’s function for Y is Gy(t, tau) = 1/ky*sin(ky*(t - tau))

f313 = 2*(K2 - ky2*h)*cx*cy + h*kx2*ky2*sx*sy + (kx^2 cy *sx-ky^2 *cx sy) h’ f314 = -h kx^2 cy sx+2 h^3 (K2/h^3-ky^2/h^2) cx sy+(cx cy+kx^2 sx sy) h’ f323 = 2 h^3 (K2/h^3-ky^2/h^2) cy sx-h ky^2 cx sy+(-cx cy-ky^2 sx sy) h’ f324 = h cx cy+2 h^3 (K2/h^3-ky^2/h^2) sx sy + (cy sx-cx sy) h’ f336 = ky^2 cy+2 h^3 (K2/h^3-ky^2/h^2) dx cy-h^2 ky^2 sx sy-(h cy sx+ky^2 dx sy) h’ f346 = h^2 cy sx+ky^2 sy+2 h^3 (K2/h^3-ky^2/h^2) dx sy-(-dx cy+h sx sy) h’

Integration:

I111 = Gx * cx**2 = ((2. + cx)*dx)/(3*h) I122 = Gx * sx**2 = dx**2/3./h**2 I112 = Gx * cx*sx = sx*dx/(3.*h) I11 = Gx * cx = s*sx/2. I116 = Gx * cx*dx = h/kx2*(cx - cx**2) = h/kx2*(I11 - I111) I12 = Gx * sx = 1./2./kx2*(sx - s*cx/2.) I126 = Gx * sx*dx = h/kx2*(sx - sx*cx) = h/kx2*(I12 - I112) I10 = Gx = dx/h I16 = Gx * dx = h/kx2*(dx/h - s*sx/2) I166 = Gx * dx**2 = h2/kx4*(1 - 2*cx + cx**2) = h2/kx4*(I10 - 2*I11 + I111) I144 = Gx * sy**2 = (sy2 - 2*dx/h)/(kx2 - 4*ky2) I133 = Gx * cy**2 = dx/h + ky2*(2*dx/h - sy2)/(kx2 - 4*ky2) I134 = Gx * cy*sy = (sy*cy - sx)/(kx2 - 4.*ky2) I313 = Gy * cx*cy = (kx2*cy*dx/h - 2*ky2*sx*sy)/(kx2 - 4 *ky2) I324 = Gy * sx*sy = (kx2*cy*dx/h - 2*ky2*sx*sy)/(kx2 - 4*ky2) I314 = Gy * cx*sy = (2*cy*sx - (1 + cx)*sy)/(kx2 - 4*ky2) I323 = Gy * sx*cy = ((1 - 2*ky2*dx/h)*sy - cy*sx)/ (kx2 - 4*ky2) I33 = Gy * cy = s*sy/2. I336 = Gy * dx*cy = h/kx2*(I33 - I313) I34 = Gy * sy = (sy - s*cy)/(2*ky2) I346 = Gy * dx*sy = h/kx2*(I34 - I314)

ocelot.cpbd.high_order.t_nnn_transport(L, h, k1, k2, energy=0)

here is used the following set of variables: x, dx/ds, y, dy/ds, delta_l, dp/p0 :param L: :param angle: :param k1: :param k2: :return:

ocelot.cpbd.high_order.t_nnn_mad(L, h, k1, k2, energy=0)

Universal function to return second order matrix. Here are used the following set of variables: x, px/p0, y, py/p0, tau, dE/(p0c). See https://nbviewer.jupyter.org/github/ocelot-collab/ocelot/blob/master/demos/ipython_tutorials/2_tracking.ipynb

Parameters
• L – length in [m]

• h – curvature (1/r) of the bend in [1/m]

• k1 – strength of quadrupole component in [1/m^2],

• k2 – strength of sextupole component in [1/m^3],

• energy – energy in [GeV]

Returns

second order matrix, numpy array with shape = (6, 6, 6)

ocelot.cpbd.high_order.t_nnn(L, h, k1, k2, energy=0)
ocelot.cpbd.high_order.fringe_ent(h, k1, e, h_pole=0.0, gap=0.0, fint=0.0)

Calculate first and second order matrices for edge focusing element. Note: the first order (R) is shown here in the sake of completeness but it is duplicated

in r_matrix.py module and used from there in optics.py

Parameters
• h – curvature (1/r) of the bend in [1/m]

• k1 – strength of quadrupole component of the dipole in [1/m^2],

• e – the angle of inclination of the entrance face [rad],

• h_pole – the curvature (1/r) of the entrance face

• gap – the magnet gap [m], NOTE in MAD and ELEGANT: HGAP = gap/2

• fint – fringe field integral

Returns

R, T. First and second order matrices (6, 6), (6, 6, 6)

ocelot.cpbd.high_order.fringe_ext(h, k1, e, h_pole=0.0, gap=0.0, fint=0.0)

Calculate first and second order matrices for edge focusing element. Note: the first order (R) is shown here in the sake of completeness but it is duplicated

in r_matrix.py module and used from there in optics.py

Parameters
• h – curvature (1/r) of the bend in [1/m]

• k1 – strength of quadrupole component of the dipole in [1/m^2],

• e – the angle of inclination of the exit face [rad],

• h_pole – the curvature (1/r) of the exit face

• gap – the magnet gap [m], NOTE in MAD and ELEGANT: HGAP = gap/2

• fint – fringe field integral

Returns

R, T. First and second order matrices (6, 6), (6, 6, 6)

ocelot.cpbd.high_order.H23(vec_x, h, k1, k2, beta=1.0, g_inv=0.0)

{x, px}, {y, py}, {sigma, ps} sigma = s - ct ps = (E - E0)/(p0*c) H2 = (px**2 + py**2)/2 + (h**2 + k1)*x**2/2 - (k1*y**2)/2 - (h*pt*x)/beta H3 = (h*x - ps/beta)*(px**2 + py**2)/2 + (2*h*k1 + k2)*(x**3)/6 - (h*k1 + k2)*(x*y**2)/2 - ps/(beta*gamma**2*(1. + beta)) H23 = H2 + H3 :param vec_x: [x, px, y, py, sigma, psigma] :param h: curvature :param k1: quadrupole strength :param k2: sextupole strength :param beta: = 1, velocity :param g_inv: 1/gamma, by default 0. :return: [x’, px’, y’, py’, sigma’, psigma’]

ocelot.cpbd.high_order.verlet(vec_x, step, h, k1, k2, beta=1.0, g_inv=0.0)

q_{n+1} = q_{n} + h * dH(p_{n}, q_{n+1})/dp p_{n+1} = p_{n} - h * dH(p_{n}, q_{n+1})/dq

ocelot.cpbd.high_order.verlet1O(vec_x, step, h, k1, k2, beta=1.0, g_inv=0.0)

q_{n+1} = q_{n} + h * dH(p_{n}, q_{n+1})/dp p_{n+1} = p_{n} - h * dH(p_{n}, q_{n+1})/dq

ocelot.cpbd.high_order.sym_map(z, X, h, k1, k2, energy=0.0)
ocelot.cpbd.high_order.moments_py(bx, by, Bx, By, Bz, dzk)

mx = v/(dz/dt)*e/p*(y’*Bz - By*(1.+x’^2) + x’*y’*Bx)*dz my = -v/(dz/dt)*e/p*(x’*Bz - Bx*(1.+y’^2) + x’*y’*By)*dz :param bx: x’ = dx/dz :param by: y’ = dy/dz :param Bx: horizontal component of mag field :param By: vertical component of mag field :param Bz: longitudinal component of mag field :param dzk: dz*e/p; dz - step, e - charge, p - momentum :return: mx, my

ocelot.cpbd.high_order.moments
ocelot.cpbd.high_order.rk_track_in_field(y0, s_stop, N, energy, mag_field, s_start=0.0)

Runge-Kutta solver of the exact trajectory equations in the fixed coordinate system {X, Y, Z]}. X, Y - transverse coordinates, Z - longitudinal.

Trajectory equations: x’’ = v/(dz/dt)*e/p*(y’*Bz - By*(1.+x’^2) + x’*y’*Bx) y’’ = -v/(dz/dt)*e/p*(x’*Bz - Bx*(1.+y’^2) + x’*y’*By) v/(dz/dt) = sqrt(1 + y’^2 + x’^2) z’ is not needed in the initial conditions and z’ is not used for SR calculation

therefore z’ can be arbitrary defined in the returning array. z’ = dE/(ps*c)

Parameters
• y0 – array n*6; initial coordinates of n particles [x0, x0’=dx/dz, y0, y0=dy/dz, 0, dE0/pc, x1, x1’, y1, y1’, 0, dE1/pc, … xn, xn’, yn, yn’, 0, dEn/pc]

• s_stop – longitudinal coordinate of stop

• N – number of steps

• energy – energy of particle in [GeV]

• mag_field – function. Bx, By, Bz = mag_field(X, Y, Z)

• s_start – starting longitudinal coordinate

Returns

array with length N*9- coordinates and magnetic fields on the trajectory [x, x’, y, y’, z, dE/pc, Bx, By, Bz, … xn, xn’, yn, yn’, zn, zn’, Bxn, Byn, Bzn]

ocelot.cpbd.high_order.rk_field(rparticles, s_start, s_stop, N, energy, mag_field, long_dynamics=True)

Method to track particles through mag field

Parameters
• rparticles – initial coordinates of the particles shape (6xN), N number of particles (e.g. ParticleArray.rparticles)

• s_start

• s_stop

• N – number of points on the trajectory

• energy – in GeV

• mag_field – must be function e.g. lambda x, y, z: (Bx, By, Bz)

• long_dynamics – True, if True, includes longitudinal dynamics, otherwise only transverse

Returns

ocelot.cpbd.high_order.scipy_track_in_field(y0, l, N, energy, mag_field)
ocelot.cpbd.high_order.rk_field_scipy(y0, l, N, energy, mag_field)
ocelot.cpbd.high_order.track_und_weave(y0, z, kz, kx, Kx, energy)
ocelot.cpbd.high_order.track_und_weave_openmp(u, l, N, kz, kx, Kx, energy)
ocelot.cpbd.high_order.arcline(SREin, Delta_S, dS, R_vect)

Martin Dohlus DESY, 2015

Parameters
• SREin – trajectory. traj[0,:] - longitudinal coordinate (s), traj[1,:],traj[2,:],traj[3,:] - rectangular coordinates, (x, y, z) traj[4,:],traj[5,:],traj[6,:] - tangential unit vectors, (x’, y’, z’)

• Delta_S

• dS