.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/fractals_chaos/dla/plot_01_dla_cluster.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_fractals_chaos_dla_plot_01_dla_cluster.py: Diffusion-limited aggregation =================================== Grows a cluster by releasing random walkers that stick on first contact, and measures its fractal dimension from the mass-radius relation N(r) ~ r^D. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.fractals_chaos import dla_cluster .. GENERATED FROM PYTHON SOURCE LINES 17-19 Grow and draw the cluster ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-27 .. code-block:: Python cluster = dla_cluster(12000, seed=0) fig, ax = plt.subplots(figsize=(6, 6)) ax.scatter(*cluster.T, c=np.arange(len(cluster)), cmap="viridis", s=0.5) ax.set_aspect("equal") ax.axis("off") ax.set_title(f"{len(cluster)} particles, colored by arrival time") .. image-sg:: /api/gallery/fractals_chaos/dla/images/sphx_glr_plot_01_dla_cluster_001.png :alt: 9323 particles, colored by arrival time :srcset: /api/gallery/fractals_chaos/dla/images/sphx_glr_plot_01_dla_cluster_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, '9323 particles, colored by arrival time') .. GENERATED FROM PYTHON SOURCE LINES 28-30 Mass-radius scaling ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-44 .. code-block:: Python r = np.sqrt((cluster**2).sum(axis=1)) radii = np.logspace(np.log10(5), np.log10(r.max() / 2), 12) mass = np.array([(r < rho).sum() for rho in radii]) slope = np.polyfit(np.log(radii), np.log(mass), 1)[0] print(f"mass-radius dimension D = {slope:.3f} (Witten and Sander: about 1.7)") fig, ax = plt.subplots() ax.loglog(radii, mass, "o", label="particles within radius r") ax.loglog(radii, mass[0] * (radii / radii[0]) ** slope, "--", label=f"slope {slope:.2f}") ax.loglog(radii, mass[0] * (radii / radii[0]) ** 2, ":", label="slope 2 (compact disk)") ax.set_xlabel("r") ax.set_ylabel("N(r)") ax.legend() .. image-sg:: /api/gallery/fractals_chaos/dla/images/sphx_glr_plot_01_dla_cluster_002.png :alt: plot 01 dla cluster :srcset: /api/gallery/fractals_chaos/dla/images/sphx_glr_plot_01_dla_cluster_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none mass-radius dimension D = 1.663 (Witten and Sander: about 1.7) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.771 seconds) .. _sphx_glr_download_api_gallery_fractals_chaos_dla_plot_01_dla_cluster.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_dla_cluster.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_dla_cluster.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_dla_cluster.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_