.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/random_media/random_spheres.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_auto_examples_random_media_random_spheres.py: Random nonoverlapping spheres ============================= Generate a reproducible collection of identical spheres in a uniform background. Sphere centres are proposed uniformly within the part of the box that keeps each sphere fully inside. Overlapping proposals are rejected; the generator raises an error if it cannot place every requested sphere within its attempt limit. This is sequential placement, rather than an equilibrium hard-sphere model. The plotted medium samples material at voxel centres. Choose a spacing fine enough to resolve each sphere, and use small refractive index contrast for Born scattering. .. GENERATED FROM PYTHON SOURCE LINES 14-47 .. image-sg:: /auto_examples/random_media/images/sphx_glr_random_spheres_001.png :alt: Refractive index n: voxels :srcset: /auto_examples/random_media/images/sphx_glr_random_spheres_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Generated 12 nonoverlapping spheres with radius 40 nm. | .. code-block:: Python import matplotlib.pyplot as plt from bornsim import Grid from bornsim.medium.random_spheres import RandomSphereMedium from bornsim.units import ureg grid = Grid( shape=(16, 16, 16), spacing=25 * ureg.nanometer, ) medium = RandomSphereMedium( background_refractive_index=1.33, sphere_refractive_index=1.35, radius=40 * ureg.nanometer, sphere_count=12, ) volume = medium.to_volume( grid=grid, seed=42, ) figure = volume.plot_3d( backend="matplotlib", mode="voxels", field="refractive_index", ) print(f"Generated {medium.sphere_count} nonoverlapping spheres with radius {medium.radius:~P}.") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.583 seconds) .. _sphx_glr_download_auto_examples_random_media_random_spheres.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: random_spheres.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: random_spheres.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: random_spheres.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_