/
particle_stepper.py
70 lines (51 loc) · 2.34 KB
/
particle_stepper.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
"""
Particle stepper
================
An example of PlasmaPy's particle stepper class, currently in need of a rewrite
for speed. Currently disabled from running.
"""
import numpy as np
from astropy import units as u
from plasmapy.classes import Plasma, Species
############################################################
# Initialize a plasma. This will be a source of electric and magnetic
# fields for our particles to move in.
plasma = Plasma(domain_x=np.linspace(-1, 1, 10) * u.m,
domain_y=np.linspace(-1, 1, 10) * u.m,
domain_z=np.linspace(-1, 1, 10) * u.m)
############################################################
# Initialize the fields. We'll take $\vec{B}$ in the $\hat{x}$ direction
# and $E$ in the $\hat{y}$ direction, which gets us an $E \times B$ drift
# in $\hat{z}$.
B0 = 4 * u.T
plasma.magnetic_field[0, :, :, :] = np.ones((10, 10, 10)) * B0
E0 = 2 * u.V / u.m
plasma.electric_field[1, :, :, :] = np.ones((10, 10, 10)) * E0
############################################################
# Initialize the particle. We'll take one proton `p` with a timestep of
# $10^{-10}s$ and run it for 10000 iterations.
species = Species(plasma, 'p', 1, 1, 1e-10 * u.s, 10000)
############################################################
# Initialize the particle's velocity. We'll limit ourselves to one in the
# $\hat{x}$ direction, parallel to the magnetic field $\vec{B}$ - that
# way, it won't turn in the $\hat{z}$ direction.
V0 = 1 * (u.m / u.s)
species.v[0][0] = V0
############################################################
# Run the pusher and plot the trajectory versus time.
species.run()
species.plot_time_trajectories()
############################################################
# Plot the shape of the trajectory in 3D.
species.plot_trajectories()
############################################################
# As a test, we calculate the mean velocity in the z direction from the
# velocity and position
vmean = species.velocity_history[:, :, 2].mean()
print(f"The calculated drift velocity is {vmean:.4f} to compare with the"
f"theoretical E0/B0 = {E0/B0:.4f}")
############################################################
# and from position:
Vdrift = species.position_history[-1, 0, 2] / (species.NT * species.dt)
normdrift = Vdrift
print(f"The calculated drift velocity from position is {normdrift:.4f}")