-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChargedParticle.py
140 lines (109 loc) · 4.32 KB
/
ChargedParticle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# https://flothesof.github.io/charged-particle-trajectories-E-and-B-fields.html
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import ode
from mpl_toolkits.mplot3d import Axes3D
from base import plot3d
from GaussPlot import gauss_1d, gauss_1d_skew
class Particle (object):
def __init__(self, p0=[0, 0, 0], v0=[1, 1, 1]):
self.init_dat = p0 + v0
self.q = 10.0
self.m = 1.0
self.t0 = 0
def init_solver(self):
self.solver = ode(self.newton_method).set_integrator('dopri5')
self.solver.set_initial_value(self.init_dat, self.t0)
def newton_method(self, t, dat):
"""
Computes the derivative of the state vector y according to the equation of motion:
Y is the state vector (x, y, z, u, v, w) === (position, velocity).
returns dY/dt.
"""
x, y, z = dat[:3]
u, v, w = dat[3:]
alpha = self.q / self.m * self.b_field(dat[0:3])
u1 = alpha * v
v1 = -alpha * u
w1 = w
return np.array([u, v, w, u1, v1, w1])
def e_filed(self, xyz):
x, y, z = xyz
return 10 * np.sign(np.sin(2 * np.pi * x / 25))
def b_field(self, xyz):
x, y, z = xyz
return 1
class ChParticle (plot3d):
def __init__(self):
plot3d.__init__(self)
self.solver = ode(self.newton).set_integrator('dopri5')
self.solver1 = ode(self.newton1).set_integrator('dopri5')
self.solver2 = ode(self.newton2).set_integrator('dopri5')
def e_of_x(self, x):
return 10 * np.sign(np.sin(2 * np.pi * x / 25))
def b_xyz(self, x=1, y=1, z=1):
return 2 * x + np.sin(2 * np.pi * y / 25) + np.sin(2 * np.pi * z / 25)
def newton(self, t, Y, q, m):
"""
Computes the derivative of the state vector y according to the equation of motion:
Y is the state vector (x, y, z, u, v, w) === (position, velocity).
returns dY/dt.
"""
x, y, z = Y[0], Y[1], Y[2]
u, v, w = Y[3], Y[4], Y[5]
alpha = q / m * self.b_xyz(x, y, z)
return np.array([u, v, w, 0.5, alpha * w + self.e_of_x(x), -alpha * v])
def newton1(self, t, Y, q, m, B):
"""
Computes the derivative of the state vector y according to the equation of motion:
Y is the state vector (x, y, z, u, v, w) === (position, velocity).
returns dY/dt.
"""
x, y, z = Y[0], Y[1], Y[2]
u, v, w = Y[3], Y[4], Y[5]
alpha = q / m * B
return np.array([u, v, w, 0, alpha * w, -alpha * v])
def newton2(self, t, Y, q, m, B, E):
"""
Computes the derivative of the state vector y according to the equation of motion:
Y is the state vector (x, y, z, u, v, w) === (position, velocity).
returns dY/dt.
"""
x, y, z = Y[0], Y[1], Y[2]
u, v, w = Y[3], Y[4], Y[5]
alpha = q / m
return np.array([u, v, w, 0, alpha * B * w + E, -alpha * B * v])
def compute_trajectory(self, m=1, q=1):
r = ode(self.newton1).set_integrator('dopri5')
r.set_initial_value(initial_conditions,
t0).set_f_params(m, q, 1.0, 10.)
positions = []
t1 = 200
dt = 0.05
while r.successful() and r.t < t1:
r.set_f_params(m, q, 1.0, self.e_of_x(r.y[0]))
r.integrate(r.t + dt)
positions.append(r.y[:3])
return np.array(positions)
if __name__ == '__main__':
obj = ChParticle()
t0 = 0
x0 = np.array([0, 0, 0])
v0 = np.array([1, 1, 0])
initial_conditions = np.concatenate((x0, v0))
obj.solver.set_initial_value(initial_conditions, t0)
obj.solver.set_f_params(-1.0, 1.0)
obj.solver1.set_initial_value(initial_conditions, t0)
obj.solver1.set_f_params(1.0, 1.0, 1.0)
obj.solver2.set_initial_value(initial_conditions, t0)
obj.solver2.set_f_params(1.0, 1.0, 1.0, 10.0)
pos = []
t1 = 10
dt = 0.005
while obj.solver.successful() and obj.solver.t < t1:
print(obj.solver.t, *obj.solver.y)
obj.solver.integrate(obj.solver.t + dt)
pos.append(obj.solver.y[:3])
pos = np.array(pos)
obj.axs.plot3D(pos[:, 0], pos[:, 1], pos[:, 2])
plt.show()