.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/monte_carlo/04_metropolis_equilibration.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_gallery_monte_carlo_04_metropolis_equilibration.py: Equilibrating an RSA configuration with Metropolis moves ========================================================= Random sequential addition (RSA) creates a valid hard-sphere packing, but it is irreversible and does not sample the equilibrium hard-sphere ensemble. This example uses that RSA configuration only as an initial state for single-particle Metropolis moves. The Metropolis sampler keeps every radius, class label, and the particle count fixed; it only changes sphere centres. For a production equilibrium calculation, check that observables have reached stationarity and estimate their uncertainty from independent samples. The acceptance rate printed below helps tune the proposal displacement. .. GENERATED FROM PYTHON SOURCE LINES 15-20 .. code-block:: Python from PackLab import monte_carlo, samplers from PackLab.units import ureg import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 21-23 Create a convenient non-overlapping initial state with RSA ----------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-39 .. code-block:: Python domain = monte_carlo.PackingDomain( 5 * ureg.micrometer, 5 * ureg.micrometer, 5 * ureg.micrometer, use_periodic_boundaries=True, ) radius_sampler = samplers.ConstantRadiusSampler(150 * ureg.nanometer, bins=1) rsa_options = monte_carlo.RSAOptions() rsa_options.random_seed = 12 rsa_options.maximum_attempts = 50_000 rsa_options.target_packing_fraction = 0.08 initial_result = monte_carlo.RSASimulator(domain, radius_sampler, rsa_options).run() .. GENERATED FROM PYTHON SOURCE LINES 40-42 Equilibrate with fixed-volume Metropolis sampling ------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 42-63 .. code-block:: Python metropolis_options = monte_carlo.MetropolisOptions() metropolis_options.random_seed = 34 metropolis_options.number_of_sweeps = 500 metropolis_options.maximum_displacement = 50 * ureg.nanometer simulator = monte_carlo.MetropolisSimulator( domain, initial_result.sphere_configuration, metropolis_options, ) equilibrated_result = simulator.run() statistics = simulator.statistics print(f"Metropolis acceptance rate: {statistics.acceptance_rate:.3f}") print(f"Completed sweeps: {statistics.completed_sweeps}") print(f"Packing fraction: {equilibrated_result.statistics.packing_fraction_geometry:.3f}") figure = equilibrated_result.plot_slice_2d(show=False) _ = figure.suptitle("Hard-sphere configuration after Metropolis equilibration") plt.show() .. image-sg:: /gallery/monte_carlo/images/sphx_glr_04_metropolis_equilibration_001.png :alt: Hard-sphere configuration after Metropolis equilibration, 2D slice at z≈0.00 meter, thickness 0.00 meter | showing 61 spheres :srcset: /gallery/monte_carlo/images/sphx_glr_04_metropolis_equilibration_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Metropolis acceptance rate: 0.910 Completed sweeps: 500 Packing fraction: 0.080 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.542 seconds) .. _sphx_glr_download_gallery_monte_carlo_04_metropolis_equilibration.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 04_metropolis_equilibration.ipynb <04_metropolis_equilibration.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 04_metropolis_equilibration.py <04_metropolis_equilibration.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 04_metropolis_equilibration.zip <04_metropolis_equilibration.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_