In [1]:
%matplotlib inline
%load_ext autoreload
%autoreload 2

In [2]:
import numpy as np
from pic1d2v import *
import matplotlib.pyplot as plt
from matplotlib import animation
from collections import namedtuple
norm = lambda x: np.max(np.abs(x))

Single particle motion in a uniform $B$ field


In [3]:
nt = 100
nx = 512
L  = 2*np.pi
dx = L/nx
dt = 0.5
B0 = 0.5
wc = -B0
t_vals = np.linspace(0, nt*dt, nt+1)
N_e = 1
N_i = nx

init_pos = L/2.

electron = Species(-1., 1., N_e,
                   np.array((init_pos,)),
                   -np.ones(N_e), np.zeros(N_e))
ion      = Species(1./nx, -1., nx,
                   np.linspace(0, L, nx+1)[:-1],
                   np.zeros(N_i), np.zeros(N_i))

species = [electron, ion]
colors  = ['b']

xp, vx, vy, E, phi, rho = pic(species, nx, dx, nt, dt, L, B0, 
                              solver_method="FFT",
                              weight_method="CIC",
                              interp_method="CIC")

In [4]:
fig = plt.figure()
p1 = fig.add_subplot(1, 1, 1)
p1.set_xlabel('x'); p1.set_ylabel('$v_x$')
imgs = []   
s = 1
x_vals = np.linspace(0,L,nx+1)[:-1]
for i in range(nt+1):
    imgs.append((p1.scatter(vx[i,:N_e], vy[i,:N_e], color=colors[0]),
                 #p1.plot(x_vals, phi[i], color='g')[0],
                ))


im_ani = animation.ArtistAnimation(fig, imgs, 
                                   interval=50, repeat_delay=3000,
                                   blit=True)
im_ani.save("1d2v-sing.mp4", writer="mencoder")



In [5]:
plt.plot(vx[:,0])
vx0 = -1*np.cos(wc*dt/2)
# Recall that the init half step back causes a small
# phase shift in the result
expected = vx0*np.cos(wc*t_vals+dt/2.)
plt.plot(expected)
print norm(expected-vx[:,0])


0.00780233277067

In [6]:
plt.plot(vy[:,0])
expected = np.sin(wc*t_vals + dt/2.)
plt.plot(expected)
print norm(expected-vy[:,0])


1.54876111935e-14

In [6]: