In [115]:
from sympy import sin, cos, tan, atan2, sqrt, Matrix, var, init_printing, simplify, pprint
init_printing(use_unicode=True)
var('tanLambda,d0,phi0,z0', real = True)
var('ptinv', real= True, positive = True)
var('x,y,z,p_x,p_y,p_z', real = True)
var('alpha, P', real = True)
var('T,V,D', real = True, positive = True)
_T = p_x**2 + p_y**2
_P = p_x * y - p_y * x
_V = x**2 + y**2
_D = sqrt(_T + alpha *(2*_P + _V * alpha))
_ptinv = 1 / sqrt(_T)
_tanLambda = p_z / sqrt(_T)
_phi0 = atan2(p_y - x * alpha, p_x + y * alpha)
_z0 = z - p_z * atan2(p_x * x * alpha + p_y * y * alpha, _T + alpha * _P) / alpha
_d0 = (2 * _P + alpha * _V) / (_T + _D )
X = Matrix([
_tanLambda, _ptinv, _d0, _phi0, _z0
])
Y = Matrix([x, y, z, p_x, p_y, p_z])
J = X.jacobian(Y)
J.simplify()
A = J[:, :3]
B = J[:, 3:]
In [116]:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# Position of the particle
pos_x = 3.0
pos_y = 2.0
pos_z = 1.0
# Momentum of the particle at this position
mom_x = 1.0
mom_y = 2.0
mom_z = 3.0
# Aplha containing units, magnetic field and charge
draw_alpha = 1.0
mom_tinv = 1.0 / np.sqrt(mom_x**2 + mom_y**2)
fig = plt.figure()
ax = fig.gca(projection='3d')
t = np.linspace(0, 10, 100)
draw_mom_z = mom_z * np.ones(len(t))
draw_mom_y = np.cos(draw_alpha * t) / mom_tinv
draw_mom_x = -np.sin(draw_alpha * t) / mom_tinv
draw_z = pos_z + draw_mom_z * t
draw_x = pos_x + np.cos(draw_alpha * t) / (draw_alpha * mom_tinv)
draw_y = pos_y + np.sin(draw_alpha * t) / (draw_alpha * mom_tinv)
ax.plot(draw_x, draw_y, draw_z, label='Helix')
ax.legend()
plt.show()
for i in range(0, 10):
print(X.subs([(p_x, draw_mom_x[i]),(p_y, draw_mom_y[i]), (p_z, draw_mom_z[i]), (x, draw_x[i]), (y, draw_y[i]), (z, draw_z[i]), (alpha, 1.0)]))
In [122]:
# Without magnetic field or zero charge
fig = plt.figure()
ax = fig.gca(projection='3d')
t = np.linspace(0, 10, 100)
draw_mom_z = mom_z * np.ones(len(t))
draw_mom_y = mom_y * np.ones(len(t))
draw_mom_x = mom_x * np.ones(len(t))
draw_z = pos_z + draw_mom_z * t
draw_x = pos_x + draw_mom_x * t
draw_y = pos_y + draw_mom_y * t
ax.plot(draw_x, draw_y, draw_z, label='Helix')
ax.legend()
plt.show()
X_zero = X.subs([(alpha, 0.0)])
X_zero[4] = z - p_z * (p_x * x + p_y * y) / _T
X_zero
for i in range(0, 10):
print(X_zero.subs([(p_x, draw_mom_x[i]),(p_y, draw_mom_y[i]), (p_z, draw_mom_z[i]), (x, draw_x[i]), (y, draw_y[i]), (z, draw_z[i])]))
In [114]:
_P2 = 2 * p_x * y - 2 * p_y * x
_P2A = 2 * alpha * p_x * y - 2 * alpha * p_y * x
_VAA = alpha **2 * x**2 + alpha **2 * y**2
_DE = sqrt(T + alpha *(2*P + V * alpha))
subs = [(_D, D), (_P, P),(_V, V), (_T, T), (_P2, 2 * P), (_P2A, 2 * alpha * P), (_VAA, alpha ** 2 * V), (_DE, D)]
A = A.subs(subs)
A
Out[114]:
In [95]:
B = B.subs(subs)
B
Out[95]:
In [96]:
X = X.subs(subs)
X
Out[96]:
In [97]:
c0 = X - A * Matrix([x,y,z]) - B * Matrix([p_x, p_y, p_z])
c0
Out[97]:
In [ ]: