<jupyter_start><jupyter_text>Simple Brownian motionWe will in this notebook simulate a simple bulk 1D Brownian motion and compute the Mean Square Displacement (MSD) in order to image the Einstein's theory. Since this notebook is only for demonstration, we will here make all the particle start at the same initial position $x = 0$.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
### Just some matplotlib tweaks
import matplotlib as mpl

mpl.rcParams["xtick.direction"] = "in"
mpl.rcParams["ytick.direction"] = "in"
mpl.rcParams["lines.markeredgecolor"] = "k"
mpl.rcParams["lines.markeredgewidth"] = 1.5
mpl.rcParams["figure.dpi"] = 200
from matplotlib import rc
rc('font', family='serif')
rc('text', usetex=True)
rc('xtick', labelsize='medium')
rc('ytick', labelsize='medium')
rc("axes", labelsize = "large")
def cm2inch(value):
    return value/2.54
a = 10e-6 # particle radius
kt = 4e-21 # Boltzman constant multiplied by the temperature
eta = 1e-3 # fluid viscosity (here water)
D = kt / (6 * np.pi * eta * a) # Diffusion coefficient
N = 100# number of particles trajectory simulated
L = 100000 # number of time step realised
dt = 0.1 # time step
# Generation of displacement using a white noise
Dx = np.sqrt(2*D)*np.random.normal(0, np.sqrt(dt), (L,N))
Xs = np.concatenate(([np.zeros(N)],np.cumsum(Dx, axis = 0)))
t = np.arange(len(Xs)) * dt
plt.figure()

for i in range(N):
    plt.plot(t, Xs[:,i]* 1e6) 

plt.xlabel("time (s)")
plt.ylabel("x position ($\mu$m)")
# Compute the MSD
from tqdm import trange
MSD = []
t_MSD = []
for i in trange(1,1000,60):
    msd = 0
    for j in range(N):
        x = Xs[:,j]
        msd += np.mean((x[i:] - x[:-i])**2)
        
    MSD.append(msd/N)
    t_MSD.append(i)
    
plt.plot(t[t_MSD],np.array(MSD)*1e12,"o", label = "Simulation")
plt.plot(t, 2*D*t * 1e12, label = "Theory")
plt.xlim((0,510))
plt.ylim((0,30))
plt.xlabel("time (s)")
plt.ylabel("MSD ($\mu \mathrm{m^2.s^{-1}} $)")
plt.legend()
fig, (ax1, ax2) = plt.subplots(2, 1, figsize = (cm2inch(16),cm2inch(8)))
for i in range(N):
    ax1.plot(t[:1000], Xs[:1000,i]* 1e6) 

ax1.set( ylabel="$x$ ($\mu$m)", yticks = np.arange(-6, 10, 2), ylim=(-6,7), xlim=(-2,100))

ax2.plot(t[t_MSD],np.array(MSD)*1e12,"o", label = "Simulation")
ax2.plot(t[t_MSD], 2*D*t[t_MSD] * 1e12, label = "Theory", zorder = - 1, color = "k")
ax2.set(xlabel="$t$ (s)", ylabel="MSD ($\mu \mathrm{m^2.s^{-1}} $)", xlim = (-2,100), ylim = (-0.2,4.5))
plt.legend(frameon = True)
plt.tight_layout(pad=0.4, w_pad=0.3, h_pad=1.0)
                               
plt.savefig("Brown_exemple.pdf")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18879508825786645, 'fraction_numerical': 0.045663852647735995, 'mean_word_length': 4.256048387096774, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '61598', 'license': 'MIT', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1046, 'n_words': 261, 'path': 'Preparation_talk/Brown/.ipynb_checkpoints/simple_Brownian-checkpoint.ipynb', 'repo_name': 'eXpensia/Confined-Brownian-Motion', 'size': '2606'}