.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/md/lj_fluid/plot_07_einstein_mean_squared_displacement.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_md_lj_fluid_plot_07_einstein_mean_squared_displacement.py: Einstein's diffusion law: mean-squared displacement grows linearly in time ============================================================================= Einstein's 1905 analysis of Brownian motion showed that a particle performing a random walk wanders off, on average, not in proportion to time but to its square root: the mean-squared displacement grows linearly, .. math:: \langle |\vec r(t) - \vec r(0)|^2 \rangle = 6Dt \quad (t\to\infty,\ 3\text{D}), with the self-diffusion coefficient :math:`D` as the slope. In a simulated Lennard-Jones liquid every atom performs such a random walk, kicked about by its neighbors. This example unwraps the periodic trajectory (:func:`~chemistrykit.md.systems.transport.unwrap_trajectory`), computes the time-origin-averaged mean-squared displacement (:func:`~chemistrykit.md.systems.transport.mean_squared_displacement`), and reads :math:`D` from the linear regime (:func:`~chemistrykit.md.systems.transport.einstein_diffusion_coefficient`). .. GENERATED FROM PYTHON SOURCE LINES 25-59 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from chemistrykit.md.systems.lj_fluid import LJFluid from chemistrykit.md.systems.thermostats import VelocityRescalingThermostat from chemistrykit.md.systems.transport import einstein_diffusion_coefficient, mean_squared_displacement T, dt, sample_every = 1.0, 0.005, 4 fluid = LJFluid.from_lattice(n_per_side=6, density=0.7, temperature=T, cutoff=2.5, rng=0) fluid.run(dt=dt, n_steps=2000, thermostat=VelocityRescalingThermostat(T, interval=10), sample_every=2000) result = fluid.run(dt=dt, n_steps=4000, sample_every=sample_every) max_lag = 500 msd = mean_squared_displacement(result.positions, box_length=result.box_length, max_lag=max_lag) lag_t = np.arange(max_lag + 1) * dt * sample_every D = einstein_diffusion_coefficient(lag_t, msd, fit_from=2.0) fig, axes = plt.subplots(1, 2, figsize=(11, 4.5)) axes[0].plot(lag_t, msd, color="steelblue", label="simulated MSD") axes[0].plot(lag_t, 6.0 * D * lag_t, color="black", linestyle="--", label=f"6Dt, D = {D:.3f}") axes[0].set_xlabel("t") axes[0].set_ylabel(r"$\langle|\Delta \vec r|^2\rangle$") axes[0].set_title("Mean-squared displacement") axes[0].legend() axes[1].loglog(lag_t[1:], msd[1:], color="steelblue", label="simulated MSD") v2 = 3.0 * T # = 3 k_B T / m axes[1].loglog(lag_t[1:40], v2 * lag_t[1:40] ** 2, color="gray", linestyle=":", label=r"ballistic $\langle v^2\rangle t^2$") axes[1].loglog(lag_t[40:], 6.0 * D * lag_t[40:], color="black", linestyle="--", label="diffusive 6Dt") axes[1].set_xlabel("t") axes[1].set_title("Log-log: ballistic, then diffusive") axes[1].legend() fig.tight_layout() .. image-sg:: /api/gallery/md/lj_fluid/images/sphx_glr_plot_07_einstein_mean_squared_displacement_001.png :alt: Mean-squared displacement, Log-log: ballistic, then diffusive :srcset: /api/gallery/md/lj_fluid/images/sphx_glr_plot_07_einstein_mean_squared_displacement_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 60-63 At very short times an atom simply flies freely (MSD grows as :math:`t^2`); after a few collisions its motion becomes a random walk and the MSD settles onto Einstein's straight line: .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: Python print(f"self-diffusion coefficient (reduced units): D = {D:.4f}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none self-diffusion coefficient (reduced units): D = 0.0949 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.587 seconds) .. _sphx_glr_download_api_gallery_md_lj_fluid_plot_07_einstein_mean_squared_displacement.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_07_einstein_mean_squared_displacement.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_07_einstein_mean_squared_displacement.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_07_einstein_mean_squared_displacement.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_