In [1]:
from pythonpic.classes.species import Species
from pythonpic.classes.grid import Grid
from pythonpic.algorithms import density_profiles
import matplotlib.pyplot as plt
import numpy as np
import scipy.integrate
%matplotlib inline
In [68]:
FDENS = density_profiles.FDENS
In [50]:
Out[50]:
In [2]:
g = Grid(1, 100, 50)
moat_left = g.L/4
ramp_length = g.L/4
plasma_length = g.L/2
N = 1000
dense_x, dense_dx = np.linspace(0, g.L, N*1000, retstep=True)
y = FDENS(dense_x, moat_left, ramp_length, plasma_length, N) * dense_dx
dense_x_indices = (dense_x // g.dx).astype(int)
print(dense_x_indices)
y_grid = np.bincount(dense_x_indices, y)
print(y_grid)
# y_grid = np.array([scipy.integrate.quad(lambda x: FDENS(x, moat_left, ramp_length, plasma_length, N), x, x+g.dx) for x in g.x])
# y_grid[1:] += y_grid[:-1] % 1
# y_grid = np.floor(y_grid)
In [75]:
integrated = scipy.integrate.cumtrapz(y, dense_x).astype(int)
# plt.plot(dense_x[1:], integrated)
plt.plot(dense_x, y)
plt.plot(g.x, y_grid)
print(y_grid.sum()*g.dx - N)
print(y_grid)
In [5]:
indices = (integrated[1:] - integrated[:-1]) == 1
plt.plot(dense_x[:-2], indices)
Out[5]:
In [29]:
s = Species(1, 1, N)
s.x = dense_x[0:][indices]
plt.figure(figsize=(10,10))
plt.plot(dense_x, y, label="Distribution function")
plt.scatter(s.x, s.x, c="g", alpha=0.1, label="Particles")
plt.xlim(0, g.L)
plt.hist(s.x, g.x, label="Particle density");
plt.xticks(g.x)
# plt.grid()
plt.gca().xaxis.set_ticklabels([])
plt.legend()
Out[29]:
In [30]:
plt.figure(figsize=(10,10))
quadratic_distribution_function = density_profiles.FDENS(dense_x, moat_left, ramp_length, plasma_length, N, 'quadratic')
plt.plot(dense_x, quadratic_distribution_function, label="Distribution function")
s.distribute_nonuniformly(g.L, moat_left, ramp_length, plasma_length, profile='quadratic')
plt.scatter(s.x, s.x, c="g", alpha=0.1, label="Particles")
plt.xlim(0, g.L)
plt.hist(s.x, g.x, label="Particle density");
plt.xticks(g.x)
plt.gca().xaxis.set_ticklabels([])
plt.legend()
Out[30]:
In [23]:
plt.figure(figsize=(10,10))
s = Species(1, 1, N)
exponential_distribution_function = density_profiles.FDENS(dense_x, moat_left, ramp_length, plasma_length, N, 'exponential')
plt.plot(dense_x, exponential_distribution_function, label="Distribution function")
s.distribute_nonuniformly(g.L, moat_left, ramp_length, plasma_length, profile='exponential')
plt.scatter(s.x, s.x, c="g", alpha=0.1, label="Particles")
plt.xlim(0, g.L)
plt.hist(s.x, g.x, label="Particle number in cell");
plt.xticks(g.x)
plt.gca().xaxis.set_ticklabels([])
plt.legend()
Out[23]:
In [26]:
plt.figure(figsize=(10,10))
s = Species(1, 1, N * 10)
exponential2_distribution_function = density_profiles.FDENS(dense_x, moat_left, ramp_length, plasma_length, N*10, 'exponential')
plt.plot(dense_x, exponential2_distribution_function, label="Distribution function")
s.distribute_nonuniformly(g.L, moat_left, ramp_length, plasma_length, profile='exponential')
plt.xlim(0, g.L)
plt.hist(s.x, g.x, label="Particle number in cell", alpha=0.5);
plt.scatter(s.x, s.x, c="g", alpha=0.1, label="Particles")
plt.xticks(g.x)
plt.gca().xaxis.set_ticklabels([])
plt.legend()
Out[26]:
In [10]:
plt.figure(figsize=(10,10))
N = 3757
s = Species(1, 1, N, "particles")
linear_distribution_function = density_profiles.FDENS(dense_x, moat_left, ramp_length, plasma_length, N, 'linear')
s.distribute_nonuniformly(g.L, moat_left, ramp_length, plasma_length, profile='linear')
print(s.N - s.x.size)
plt.plot(dense_x, linear_distribution_function, label="Distribution function")
plt.scatter(s.x, s.x, c="g", alpha=0.1, label="Particles")
plt.hist(s.x, g.x, label="Particle density", alpha=0.5);
plt.xlim(0, g.L)
plt.xticks(g.x)
plt.gca().xaxis.set_ticklabels([])
plt.legend()
Out[10]: