<jupyter_start><jupyter_text>Image-based invasion percoalationThis notebook illustrates the use of the image-based invasion percolation algorithm decribed [here](https://arxiv.org/abs/2101.03240). Import necessary packages and functions<jupyter_code>import porespy as ps
import numpy as np
import matplotlib.pyplot as plt
from edt import edt
import imageio
from copy import copy
ps.visualization.set_mpl_style()<jupyter_output><empty_output><jupyter_text>Generate test image<jupyter_code>np.random.seed(5)
im = ps.generators.blobs(shape=[500, 500], porosity=0.6, blobiness=2)<jupyter_output><empty_output><jupyter_text>Also generate an image defining the inlets<jupyter_code>bd = np.zeros_like(im, dtype=bool)
bd[:, 0] = 1
bd *= im
im = ps.filters.trim_disconnected_blobs(im=im, inlets=bd)
dt = edt(im)<jupyter_output><empty_output><jupyter_text>Apply IBIP to image<jupyter_code>out = ps.filters.ibip(im=im, inlets=bd, maxiter=15000)
inv_seq, inv_size = out.inv_sequence, out.inv_sizes<jupyter_output><empty_output><jupyter_text>Do some post-processing Convert invasion sequence values to saturation, and identify trapped regions:<jupyter_code>inv_satn = ps.filters.seq_to_satn(seq=inv_seq)
inv_seq_trapping = ps.filters.find_trapped_regions(seq=inv_seq, bins=None,
                                                   return_mask=False)
inv_satn_trapping = ps.filters.seq_to_satn(seq=inv_seq_trapping)<jupyter_output><empty_output><jupyter_text>Run standard MIO for comparison:Compute the MIO result, convert to sequation and saturation using the functions in ``porespy.tools``, and also find trapped regions.<jupyter_code>sizes = np.arange(int(dt.max())+1, 0, -1)
mio = ps.filters.porosimetry(im=im, inlets=bd, sizes=sizes, mode='mio')
mio_seq = ps.filters.size_to_seq(mio)
mio_seq[im*(mio_seq == 0)] = -1  # Adjust to set uninvaded to -1
mio_satn = ps.filters.seq_to_satn(mio_seq)
mio_seq_trapping = ps.filters.find_trapped_regions(seq=mio_seq, bins=None,
                                                   return_mask=False)<jupyter_output><empty_output><jupyter_text>Comparing the invasion configurations between the MIO and IBIP algorithms below suggests that the methods produce similar results when trapping is turned off.  In fact, it can be shown that the MIO result is a subset of the IBIP result.  Compare MIO and IBIP at Equal SaturationsThe comparison between the two methods can only be done when trapping is neglected.  The difference in the trapped defending phase from each method is discussed further below.  The images below show the saturation fields for both IBIP and MIO.> Saturation field means that each voxel in the image contains the value of the saturation at the point is was invaded.  In other words, the operation ``satn <= s`` will yeild and image containing the fluid configuration at saturation equal to ``s``.<jupyter_code>fig, ax = plt.subplots(1, 2, figsize=[8, 4])
ax[0].imshow(inv_satn/im, origin='lower', interpolation='none')
ax[1].imshow(mio_satn/im, origin='lower', interpolation='none');<jupyter_output><empty_output><jupyter_text>The saturation fields can be compared directly by seeing which voxels are filled at each saturation in both methods.  We must first find all the values of saturation that were obtained in the MIO result, which consists of discrete jumps (i.e. from ``s=0.33`` to ``s=0.49``) due to the pressure based nature of the simulations.  Once we have a list of all unique saturation values, we can threshold the saturation fields from both simulations at these values and compare the configurations.<jupyter_code>inv_satn_t = np.around(inv_satn, decimals=4)
mio_satn_t = np.around(mio_satn, decimals=4)
satns = np.unique(mio_satn_t)[1:]
err = []
diff = np.zeros_like(im, dtype=float)
for s in satns:
    ip_mask = (inv_satn_t <= s) * (inv_satn_t > 0)
    mio_mask = (mio_satn_t <= s) * (mio_satn_t > 0)
    diff[(mio_mask == 1)*(ip_mask == 0)*(im == 1)] = 1
    diff[(mio_mask == 0)*(ip_mask == 1)*(im == 1)] = -1
    err.append((mio_mask != ip_mask).sum())<jupyter_output><empty_output><jupyter_text>In the figure below we plot the differences between the two results at equal saturation. On the left is the fluid configurations of each method overlaid on top of each other.  Any voxels that are not green indicate locations where the two results disagree.  There are very few of them and almost impossible to see (e.g. near [x, y]=[250, 50]), so the plot on the right shows the count of mis-matching voxels as a function of saturation.  The total count never exceeds 70, which is a very small fraction of a 500 x 500 image.  The differences are due to the numerical precision of the saturation values.<jupyter_code>fig, ax = plt.subplots(1, 2, figsize=[8, 4])
ax[0].imshow(diff/im, origin='lower')
ax[1].plot(satns, err, 'o-');<jupyter_output><empty_output><jupyter_text>Comparing Capillary Pressure CurvesConverting the invasion configurations to capillary pressure curves is possible using functions provided in the ``metrics`` module.<jupyter_code>d = ps.metrics.pc_curve_from_ibip(im=im, seq=inv_seq, sizes=inv_size, voxel_size=1e-5);
e = ps.metrics.pc_curve(im=im, sizes=mio, voxel_size=1e-5);
fig, ax = plt.subplots()
ax.plot(np.log10(d.pc), d.snwp, 'g-', linewidth=2, label='IBIP')
ax.step(np.array(np.log10(e.pc)), e.snwp, 'r--', where='post', markersize=20, linewidth=3, alpha=0.6, label='MIO')
plt.xlabel('log(Capillary Pressure [Pa])')
plt.ylabel('Non-wetting Phase Saturation')
plt.legend()
ax.xaxis.grid(True, which='both')<jupyter_output><empty_output><jupyter_text>The above capillary pressure curves are quite interesting. The MIO curve in red is the envelop of peak pressures obtained by the IPIP curve in green. The red line corresponds to the normal 'porosimetry' experiment where increasingly high pressures are applied (x-axis) and larger fractions of the void space are filled (y-axis).  Each verticle jump in the red curve corresponds to a new region of the image being penetrated because the breakthrough pressure of a constriction was exceed.  All the void space beyond this constriction is then filled at once.  The green line is the result of IBIP and corresponds to volume controlled injection, where fluid is injected at some constant rate.  Every data point has a slightly higher saturation than the previous so there are no plateaus of constant saturation.  However, the capillary pressure (x-axis) fluctuates up and down seemlingly randomly.  This is because the advancing menisci squeeze through small constrictions (higher pressure), then enter into a larger region that was sheilded by the constriction to they expand (lower pressure).  This process repeats itself through the entire simulation.    Compare results with trappingOne of the key benefits of the IBIP algorithm is that the fluid invades incrememtally instead of in steps.  This means the appearance of trapped defending phase is immediately noted.  By contrast, the MIO approach computes large changes in saturation, so any trapping that may occur between these large jumps is not resolved.  Therefore, only the IBIP method can correctly identify trapped defending phase.  This is illustrated below:<jupyter_code>cmap = copy(plt.cm.turbo)
cmap.set_over(color='white')
cmap.set_under(color='grey')
fig, ax = plt.subplots(1, 3, figsize=[8, 4])
ax[0].imshow((inv_seq_trapping > 0)/im, vmin=1e-3, vmax=2,
             cmap=cmap, origin='lower', interpolation='none')
ax[0].axis('off')
ax[1].imshow((mio_seq_trapping > 0)/im, vmin=1e-3, vmax=1,
             cmap=cmap, origin='lower', interpolation='none')
ax[1].axis('off')
temp = (mio_seq_trapping > 0)*2.0 - (inv_seq_trapping > 0)*1.0
ax[2].imshow(temp/im, vmin=1e-3, vmax=2.0, 
             cmap=cmap, origin='lower', interpolation='none')
ax[2].axis('off');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09794871794871794, 'fraction_numerical': 0.015128205128205128, 'mean_word_length': 5.265863453815261, 'pattern_counts': {'":': 0, '<': 48, '<?xml version=': 0, '>': 52, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '85426', 'license': 'MIT', 'n_tokens_mistral': 2576, 'n_tokens_neox': 2410, 'n_words': 850, 'path': 'examples/filters/tutorials/using_ibip.ipynb', 'repo_name': 'xu-kai-xu/porespy', 'size': '7800'}