<jupyter_start><jupyter_text>`rsa`This function implements 'random sequential addition' of spheres.  The main benefit of this approach is that sphere overlap can be prevented or controlled. The downside is that the function is a bit on the slow side, though efforts have been made to accelerate it using numba jit.<jupyter_code>import porespy as ps
import matplotlib.pyplot as plt
import numpy as np
import inspect
b = inspect.signature(ps.generators.rsa)
print(b)<jupyter_output>(im_or_shape: <built-in function array>, r: int, volume_fraction: int = 1, clearance: int = 0, n_max: int = 100000, mode: str = 'contained', return_spheres: bool = False, smooth: bool = True)<jupyter_text>`im_or_shape`The function can either add spheres an existing image, or create a new image of the given shape and spheres to that.  Let's start with an empty image, and set the void fraction to a low value:<jupyter_code>shape = [300, 300]
r = 15
im = ps.generators.rsa(im_or_shape=shape, r=r)
fig, ax = plt.subplots(1, 1, figsize=[4, 4])
ax.axis(False)
ax.imshow(im, origin='lower', interpolation='none');<jupyter_output><empty_output><jupyter_text>Now let's fill up the remaining space with as many smaller spheres as possible:<jupyter_code>r = 5
im = ps.generators.rsa(im_or_shape=im, r=r)
fig, ax = plt.subplots(1, 1, figsize=[4, 4])
ax.axis(False)
ax.imshow(im, origin='lower', interpolation='none');<jupyter_output><empty_output><jupyter_text>`mode`Spheres can either be fully contained within the image or be truncated at the edges by specifying mode:<jupyter_code>fig, ax = plt.subplots(1, 2, figsize=[8, 4])

r = 20
mode = 'contained'
im1 = ps.generators.rsa(im_or_shape=shape, r=r, mode=mode)
ax[0].imshow(im1, origin='lower', interpolation='none')
ax[0].axis(False)
ax[0].set_title(f'mode = {mode}')

mode = 'extended'
im2 = ps.generators.rsa(im_or_shape=shape, r=r, mode=mode)
ax[1].imshow(im2, origin='lower', interpolation='none')
ax[1].axis(False)
ax[1].set_title(f'mode = {mode}');<jupyter_output><empty_output><jupyter_text>`clearance`Spheres can be made to partially overlap, the so called 'cherry pit' model, or to have some clearance:<jupyter_code>fig, ax = plt.subplots(1, 2, figsize=[8, 4])

c = -4
im1 = ps.generators.rsa(im_or_shape=shape, r=r, clearance=c)
ax[0].imshow(im1, origin='lower', interpolation='none')
ax[0].axis(False)
ax[0].set_title(f'clearance = {c}')

c = 4
im2 = ps.generators.rsa(im_or_shape=shape, r=r, clearance=c)
ax[1].imshow(im2, origin='lower', interpolation='none')
ax[1].axis(False)
ax[1].set_title(f'clearance = {c}');<jupyter_output><empty_output><jupyter_text>When adding additional spheres to an existing image, the clearance only applies to new spheres.  To enforce clearance between the existing spheres you can dilate them by the amount of clearance desired (or erode them if overlap is desired).  In this case you'll want to set ``return_sphers=True`` to obtain an image of only the new spheres, which can be added to the original ones:<jupyter_code>im3 = ps.filters.fftmorphology(im=im2, strel=ps.tools.ps_disk(5), mode='dilation')
im4 = ps.generators.RSA(im_or_shape=im3, r=5, clearance=5, return_spheres=True)
im5 = im2 + im4
fig, ax = plt.subplots(1, 3, figsize=[10, 4])
ax[0].imshow(im2, origin='lower', interpolation='none')
ax[1].imshow(im4, origin='lower', interpolation='none')
ax[2].imshow(im5, origin='lower', interpolation='none');<jupyter_output><empty_output><jupyter_text>`volume_fraction` and `n_max`By default it will try to insert as many spheres as possible.  This can be controlled by setting the volume fraction or limiting the number of spheres inserted:<jupyter_code>fig, ax = plt.subplots(1, 2, figsize=[8, 4])

n_max = 5
im1 = ps.generators.rsa(shape, r=r, clearance=c, n_max=n_max)
ax[0].imshow(im1, origin='lower', interpolation='none')
ax[0].axis(False)
ax[0].set_title(f'n_max = {n_max}')

vf = 0.2
im2 = ps.generators.rsa(shape, r=r, clearance=c, volume_fraction=vf)
ax[1].axis(False)
ax[1].set_title(f'volume_fraction = {vf}')
ax[1].imshow(im2, origin='lower', interpolation='none');<jupyter_output><empty_output><jupyter_text>Note that this function returns the spheres as ``True`` which usually indicates the pore phase in porespy, so these will treated as images of holes.  If you intended to make an image of sphers, just invert the image:<jupyter_code>im1 = ~im1
im2 = ~im2
fig, ax = plt.subplots(1, 2, figsize=[8, 4])
ax[0].imshow(im1, origin='lower', interpolation='none')
ax[0].axis(False)
ax[1].imshow(im2, origin='lower', interpolation='none')
ax[1].axis(False);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14010928961748634, 'fraction_numerical': 0.023825136612021857, 'mean_word_length': 6.72972972972973, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '504717', 'license': 'MIT', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1620, 'n_words': 463, 'path': 'examples/generators/reference/rsa.ipynb', 'repo_name': 'xu-kai-xu/porespy', 'size': '4575'}