Codice: Seleziona tutto
import numpy as np; np.random.seed(2)
import matplotlib.pyplot as plt
v = 1e-9; d = 1e-9; dt = d / v # dt = 1 secondo
l = 15 * d; p = 0.002; ratio = l*p/d;
T = 1e3; num_particles = int(1e4);
N = int(T / dt)
positions = np.zeros((num_particles, N, 2))
angles = np.random.uniform(0, 2 * np.pi, (num_particles, N))
for i in range(1, N):
x_new = positions[:, i - 1, 0] + d * np.cos(angles[:, i])
y_new = positions[:, i - 1, 1] + d * np.sin(angles[:, i])
x_cross = np.abs(x_new - np.round(x_new / l) * l) < d/2
y_cross = np.abs(y_new - np.round(y_new / l) * l) < d/2
transmit_x = np.random.rand(num_particles) < p
transmit_y = np.random.rand(num_particles) < p
x_new[x_cross & ~transmit_x] = positions[x_cross & ~transmit_x, i - 1, 0] - d * np.cos(angles[x_cross & ~transmit_x, i])
y_new[y_cross & ~transmit_y] = positions[y_cross & ~transmit_y, i - 1, 1] - d * np.sin(angles[y_cross & ~transmit_y, i])
positions[:, i, 0] = x_new
positions[:, i, 1] = y_new
msd = np.mean(positions[:, :, 0]**2 + positions[:, :, 1]**2, axis=0)
times = np.arange(N) * dt
plt.plot(times, msd * 1e12)
plt.xlabel("Time (s)"); plt.ylabel("Mean Squared Displacement (μm²)")
plt.text(times[-1] * 0.05, max(msd * 1e12) * 0.95, f"Ratio l*p/d = {ratio:.2f}",
fontsize=12, bbox=dict(facecolor='white', alpha=0.8))
plt.title("Mean Squared Displacement vs Time")
plt.grid()
plt.show()