%config InlineBackend.figure_format = 'retina'
import numpy as np
import matplotlib.pyplot as plt
import math as mt
from numba.decorators import jit
from numba import f8,u8
import scipy.fftpack as sf
from IPython.display import display, clear_output
def es1(vtc,vtb,ppc,dx,nx,dt,nt,nb,vb):
np.seterr(divide='ignore', invalid='ignore')
nop=ppc*nx
lx=dx*nx
xx=dx*np.arange(nx)
tt=dt*np.arange(nt)
xp=np.zeros((2,nop))
vp=np.zeros((2,nop))
xp[0,:]=np.linspace(0,lx-lx/nop,nop)
xp[1,:]=np.linspace(0,lx-lx/nop,nop)
vp[0,:]=np.random.normal(0,vtc,nop)-vb*nb/(1-nb)
vp[1,:]=np.random.normal(0,vtb,nop)+vb
ds=np.zeros((2,nx))
kk=2*mt.pi/lx*np.r_[np.arange(nx/2),np.arange(-nx/2,0)]
Kl=(np.sin(0.5*kk*dx)/(0.5*dx))**2
kl=np.sin(kk*dx)/dx
esave=np.zeros((nt,nx))
print('initial condition')
plt.subplot(121);plt.plot(xp[0,:],vp[0,:],',');plt.plot(xp[1,:],vp[1,:],',')
plt.subplot(122);plt.hist(vp[0,:],bins=500,histtype='step',weights=(1-nb)*np.ones(nop)); plt.hist(vp[1,:], bins=500, histtype='step', weights=nb*np.ones(nop))
plt.show()
q =np.zeros(2)
q[0]=nx/nop*(1-nb)
q[1]=nx/nop*nb
for it in range(nt):
xp=xp+dt*vp
xp[xp>lx]=xp[xp>lx]-lx
xp[xp<0 ]=xp[xp<0 ]+lx
ds=np.zeros((2,nx))
ds=dens(dx,nx,nop,q,xp,ds)
#exfft=1j/kk*sf.fft(np.sum(ds,0))
#exfft[0]=0
exfft=1j*kl*sf.fft(np.sum(ds,0))/Kl
exfft[0]=0
ex=np.real(sf.ifft(exfft))
vp=acc(dx,nx,nop,xp,vp,ex,dt)
esave[it,:]=ex[:]
kmin=2*mt.pi/(dx*nx)*(-nx/2); kmax=2*mt.pi/(dx*nx)*(nx/2)
wmin=2*mt.pi/(dt*nt)*(-nt/2); wmax=2*mt.pi/(dt*nt)*(nt/2)
kaxis=np.linspace(kmin,kmax,nx); waxis=np.linspace(wmin,wmax,nt)
return locals()
@jit('f8[:,:](f8,u8,u8,f8[:,:],f8[:,:],f8[:],f8)')
def acc(dx,nx,nop,xp,vp,ex,dt):
for isp in range(2):
for ip in range(nop):
ixm=mt.floor(xp[isp,ip]/dx); ixp=ixm+1
wxp=xp[isp,ip]/dx-ixm; wxm=1-wxp
if ixp>nx-1: ixp=ixp-nx
vp[isp,ip]=vp[isp,ip]-dt*(wxm*ex[ixm]+wxp*ex[ixp])
return vp
@jit('f8[:,:](f8,u8,u8,f8[:],f8[:,:],f8[:,:])')
def dens(dx,nx,nop,q,xp,ds):
for isp in range(2):
for ip in range(nop):
ixm=mt.floor(xp[isp,ip]/dx); ixp=ixm+1
wxp=xp[isp,ip]/dx-ixm; wxm=1-wxp
if ixp>nx-1: ixp=ixp-nx
ds[isp,ixm]=ds[isp,ixm]+wxm
ds[isp,ixp]=ds[isp,ixp]+wxp
ds[isp,:]=ds[isp,:]*q[isp]
return ds
%%time
data=es1(0.05,0.05,400,0.1,256,0.1,512,0.5,0) #(vt,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.axis([kmin/4,kmax/4,wmin/4,wmax/4])
plt.clim(0,10)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/4,wmax/4])
plt.clim(0,1)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],wmin,wmax]))
plt.axis([0,tt[nt-1],wmin/4,wmax/4])
plt.clim(0,0.1)
plt.show()
%time data=es1(0.05,0.01,400,0.2,2**8,0.2,1024,0.01, 0.1) #(vtc,vtb,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.plot(kaxis,vb*kaxis,'w--')
plt.axis([kmin/4,kmax/4,wmin/4,wmax/4])
plt.clim(0,10)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/4,wmax/4])
plt.clim(0,1)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],wmin,wmax]))
plt.axis([0,tt[nt-1],wmin/4,wmax/4])
plt.clim(0,.1)
plt.show()
%time data=es1(0.05,0.01,400,0.2,2**8,0.2,1024,0.01, 0.25) #(vt,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.plot(kaxis,vb*kaxis,'w--')
plt.axis([kmin/2,kmax/2,wmin/2,wmax/2])
plt.clim(0,50)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/4,wmax/4])
plt.clim(0,1)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],wmin,wmax]))
plt.axis([0,tt[nt-1],wmin/2,wmax/2])
plt.show()
%time data=es1(0.05,0.01,400,0.2,2**8,0.2,1024,0.01, 0.5) #(vt,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.plot(kaxis,vb*kaxis,'w--')
plt.axis([kmin/4,kmax/4,wmin/4,wmax/4])
plt.clim(0,100)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/4,wmax/4])
plt.clim(0,10)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],kmin,kmax]))
plt.axis([0,tt[nt-1],kmin/2,kmax/2])
plt.clim(0,1)
plt.show()
%time data=es1(0.05,0.01,400,0.2,2**8,0.2,1024,0.1, 0.1) #(vt,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.plot(kaxis,vb*kaxis,'w--')
plt.axis([kmin,kmax,wmin/4,wmax/4])
plt.clim(0,10)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/4,wmax/4])
plt.clim(0,1)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],kmin,kmax]))
plt.axis([0,tt[nt-1],kmin/2,kmax/2])
plt.clim(0,.1)
plt.show()
%time data=es1(0.05,0.01,400,0.2,2**8,0.2,1024,0.1, 0.25) #(vt,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.plot(kaxis,vb*kaxis,'w--')
plt.axis([kmin/4,kmax/4,wmin/4,wmax/4])
plt.clim(0,100)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/4,wmax/4])
plt.clim(0,1)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],kmin,kmax]))
plt.axis([0,tt[nt-1],kmin,kmax])
plt.clim(0,0.5)
plt.show()
%time data=es1(0.05,0.01,400,0.2,2**8,0.2,1024,0.1, 0.5) #(vt,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.plot(kaxis,vb*kaxis,'w--')
plt.axis([kmin/2,kmax/2,wmin/4,wmax/4])
plt.clim(0,50)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/4,wmax/4])
plt.clim(0,1)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],kmin,kmax]))
plt.axis([0,tt[nt-1],kmin,kmax])
plt.clim(0,0.5)
plt.show()
%time data=es1(0.05,0.05,400,0.2,2**8,0.2,1024,0.5, 0.1) #(vt,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.axis([kmin/4,kmax/4,wmin/4,wmax/4])
#plt.clim(0,50)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/10,wmax/10])
#plt.clim(0,10)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],wmin,wmax]))
plt.axis([0,tt[nt-1],wmin/2,wmax/2])
plt.show()
%time data=es1(0.05,0.05,400,0.2,2**8,0.2,1024,0.5, 0.25) #(vt,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.axis([kmin/4,kmax/4,wmin/4,wmax/4])
#plt.clim(0,500)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/4,wmax/4])
#plt.clim(0,10)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],wmin,wmax]))
plt.axis([0,tt[nt-1],wmin/4,wmax/4])
plt.show()
%%time
data=es1(0.05,0.05,400,0.2,2**8,0.2,1024,0.5, 0.5) #(vt,nop,dx,nx,dt,nt,nb,vb)
locals().update(data)
plt.figure(figsize=(20,5))
plt.subplot(141)
plt.imshow(esave,origin='lower',aspect='auto')
plt.subplot(142)
fftex=sf.fftshift(sf.fft2(esave[:,::-1]))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([kmin,kmax,wmin,wmax]))
plt.plot(kaxis,np.sqrt(1+3*kaxis**2*vtc**2),'w--')
plt.axis([kmin/4,kmax/4,wmin/4,wmax/4])
#plt.clim(0,500)
plt.subplot(143)
fftex=sf.fftshift(sf.fft(esave,axis=0))
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,xx[nx-1],wmin,wmax]))
plt.axis([0,xx[nx-1],wmin/4,wmax/4])
#plt.clim(0,10)
plt.subplot(144)
fftex=sf.fftshift(sf.fft(esave.T,axis=0),axes=0)
plt.imshow(abs(fftex),origin='lower',aspect='auto',extent=([0,tt[nt-1],wmin,wmax]))
plt.axis([0,tt[nt-1],wmin/4,wmax/4])
plt.show()