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.

import porespy as ps
import matplotlib.pyplot as plt
import numpy as np
import inspect
b = inspect.signature(ps.generators.rsa)
print(b)
(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)

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:

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');
../../../_images/0eebb443afc7057d045f125d3cf9b89b1c55b3f968a7ba7723e59adadcab7fa4.png

Now let’s fill up the remaining space with as many smaller spheres as possible:

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');
../../../_images/8ce98fb2417c9aebbd1332c3b926719fcbfdafe253a0b6a5e6ee10ff9bada64d.png

mode#

Spheres can either be fully contained within the image or be truncated at the edges by specifying mode:

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}');
../../../_images/477ae6ddff451479842890fc87af9f18c9fdc85a35aa191d4dac867b3251d8bf.png

clearance#

Spheres can be made to partially overlap, the so called ‘cherry pit’ model, or to have some clearance:

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}');
../../../_images/14023dffb2fcc18ce766ccf848298351a9ab4b4df4b22033139b2047dfeb2d8a.png

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:

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');
/tmp/ipykernel_7783/2892074603.py:2: DeprecationWarning: Call to deprecated function (or staticmethod) RSA. (This function has been renamed to rsa (lowercase to meet pep8)
  im4 = ps.generators.RSA(im_or_shape=im3, r=5, clearance=5, return_spheres=True)
../../../_images/8571b36609736ac19c8ac8ca3301be72a946f4e5c3c78dced52db220052a7866.png

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:

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');
../../../_images/4b647c67d4090ce5745bd074978e77c0bdc6c5c3d4dad9163d2c7bde60be2e20.png

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:

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);
../../../_images/ceb80928289472492aa3c5777a5fd976f8958275995282414de05c6542d72243.png