Skip to content
Snippets Groups Projects
Verified Commit d1e86a14 authored by Anton Akhmerov's avatar Anton Akhmerov
Browse files

improve and speed up drude animation

parent fd1ec3cb
No related branches found
No related tags found
1 merge request!108rework plots
Pipeline #60424 passed
```python tags=["initialize"] ```python tags=["initialize"]
from matplotlib import pyplot from matplotlib import pyplot
import matplotlib.animation as animation
from IPython.display import HTML
import numpy as np import numpy as np
from common import draw_classic_axes, configure_plotting from common import draw_classic_axes, configure_plotting
...@@ -61,21 +62,21 @@ Even under these simplistic assumptions, the trajectory of the electrons is hard ...@@ -61,21 +62,21 @@ Even under these simplistic assumptions, the trajectory of the electrons is hard
Due to the random scattering, each trajectory is different, and this is how several example trajectories look: Due to the random scattering, each trajectory is different, and this is how several example trajectories look:
```python ```python
%matplotlib inline # Use colors from the default color cycle
import matplotlib.pyplot as plt default_colors = pyplot.rcParams['axes.prop_cycle'].by_key()['color']
import numpy as np blue, red = default_colors[0], default_colors[3]
import matplotlib.animation as animation
from IPython.display import HTML
walkers = 20 # number of particles walkers = 20 # number of particles
tau = 1 # relaxation time tau = 1 # relaxation time
gamma = .3 # dissipation strength gamma = .3 # dissipation strength
a = 1 # acceleration a = 1 # acceleration
dt = .1 # infinitesimal dt = .1 # infinitesimal
T = 20 # simulation time T = 10 # simulation time
v = np.zeros((2, int(T // dt), walkers), dtype=float) # v = np.zeros((2, int(T // dt), walkers), dtype=float) #
# Select random time steps and scattering angles
np.random.seed(1)
scattering_events = np.random.binomial(1, dt/tau, size=v.shape[1:]) scattering_events = np.random.binomial(1, dt/tau, size=v.shape[1:])
angles = np.random.uniform(high=2*np.pi, size=scattering_events.shape) * scattering_events angles = np.random.uniform(high=2*np.pi, size=scattering_events.shape) * scattering_events
rotations = np.array( rotations = np.array(
...@@ -97,47 +98,33 @@ r = np.cumsum(v * dt, axis=1) ...@@ -97,47 +98,33 @@ r = np.cumsum(v * dt, axis=1)
scattering_positions = np.copy(r) scattering_positions = np.copy(r)
scattering_positions[:, ~scattering_events.astype(bool)] = np.nan scattering_positions[:, ~scattering_events.astype(bool)] = np.nan
fig = plt.figure() scatter_pts = scattering_positions
scatter_pts = scattering_positions[:, :100]
trace = r[:, :100]
nz_scatters = tuple((np.hstack(scatter_pts[0])[~np.isnan(np.hstack(scatter_pts[0]))], r_min = np.min(r.reshape(2, -1), axis=1) - 1
np.hstack(scatter_pts[1])[~np.isnan(np.hstack(scatter_pts[1]))])) r_max = np.max(r.reshape(2, -1), axis=1) + 1
plt.axis([min(nz_scatters[0])-1, fig = pyplot.figure(figsize=(9, 6))
max(nz_scatters[0])+1, ax = fig.add_subplot(1, 1, 1)
min(nz_scatters[1])-1, ax.axis("off")
max(nz_scatters[1])+1]) ax.set(xlim=(r_min[0], r_max[0]), ylim=(r_min[1], r_max[1]))
lines = [] trajectories = ax.plot([],[], lw=1, color=blue, alpha=0.5)[0]
scatterers = [] scatterers = ax.scatter([], [], s=20, c=red)
for index in range(walkers):
lobj = plt.plot([],[], lw=1, color='b', alpha=0.5)[0]
lines.append(lobj)
scatterers.append(plt.scatter([], [], s=10, c='r'))
def animate(i): def frame(i):
for lnum, line in enumerate(lines): concatenated_lines = np.concatenate(
line.set_data(trace[0][:i, lnum], trace[1][:i, lnum]) (r[:, :i], np.nan * np.ones((2, 1, walkers))),
data = np.stack((scatter_pts[0][:i,lnum], scatter_pts[1][:i, lnum])).T axis=1
scatterers[lnum].set_offsets(data) ).transpose(0, 2, 1).reshape(2, -1)
trajectories.set_data(*concatenated_lines)
scatterers.set_offsets(scatter_pts[:, :i].reshape(2, -1).T)
anim = animation.FuncAnimation(fig, animate, interval=100) anim = animation.FuncAnimation(fig, frame, interval=100)
def remove_axes(ax): pyplot.close()
ax.spines['bottom'].set_color('white')
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')
ax.spines['left'].set_color('white')
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
remove_axes(plt.gca());
plt.close();
HTML(anim.to_html5_video()) HTML(anim.to_html5_video())
``` ```
---
### Equations of motion ### Equations of motion
Our goal is finding the *electric current density* $j$. Our goal is finding the *electric current density* $j$.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment