1 Introduction

The numerical method discussed in this work has received different names in the literature throughout the years. Names such as Full Morphology [1, 2], Pore-morphology-based Simulation [3,4,5,6], Pore Morphology [7, 8], Morphological [9] Approach, and Capillary Drainage Transform [10, 11] are found in the literature for Young–Laplace equation-based algorithms. Hereafter, the technique will be referred to as the Young–Laplace Method (YLM). This choice intends to clearly state the physical principle behind the method, which is related to the Young–Laplace equation (Eq. (1)), which fundamentally computes capillary forces that are involved in capillary-driven displacements.

The YLM was independently proposed in the late 1990s and initial 2000s by Hazlett [12], Magnani [13], and Magnani et al. [14]. Nonetheless, most of the cited literature in the area recognized Hazlett [12] as the sole creator of the method. Based on the hypothesis that the fluid invasion in a porous medium is exclusively limited by the capillary pressure, they proposed a sequence of steps to simulate both imbibition and drainage processes, even if no name was given to this new method. The YLM is capable of determining the spatial distribution of two immiscible fluids inside any porous geometry for a value of capillary pressure. This result is obtained under the assumption that the fluid distribution only depends on the mechanical equilibrium between the applied pressure difference and capillary forces. With this approach, dynamic effects are neglected and the fluid invasion is thought of as being a quasi-static process. This is a strong hypothesis that allows for a very efficient computational method and has a great impact on the practical applicability of the technique.

Despite the applicability limitations, a renewed interest in the method has emerged in recent years. This can be attested by the fact that more than ever, manuscripts applying YLM were published in these past years. A possible reason for this greater interest is the expansion in access to 3D imaging tools (e.g., 3D microtomography (µCT)) and also in the acquisition of physical properties by image analysis, for example, in digital petrophysics. Since the method makes no assumption regarding the geometry and it has a low computational cost, it is an excellent tool for the first analysis of digital rock images.

Most of the important applications were published only 10 years after the method was created when the use of digital rock images obtained by µCT started to gain popularity. Vogel et al. [2] compared the use of YLM, Lattice–Boltzmann Method (LBM), and Pore Network Modeling (PNM) to compute the capillary curve. The authors used µCT images of a borosilicate cylinder with a size of 983 voxels. It was found that the computational cost of the YLM was similar to the PNM, which in turn was thousands of times lower than that of LBM. It is important to emphasize that both the LBM and the PNM can determine transport properties of the flow like permeability, whereas as said, the YLM is limited to the quasi-static circumstances. Regarding the capillary curve, all mentioned methods showed equivalent results. Schulz et al. [1] applied the YLM to study the gas diffusion layer inside a polymer electrolyte fuel cell as the presence of water in reaction sites hinders the efficiency of the cell. Shikhov and Arns [6] employed the YLM to simulate three capillary pressure experiments: Porous Plate, Centrifuge Multi-speed, and Mercury Intrusion Porosimetry. Obtained results revealed that heterogeneities play a significant role in the experimental results, and they concluded that the numerical simulations using YLM can help to better delineate laboratory experiments.

The determination of relative permeability employing the YLM is another interesting application that emerged in recent years [7, 9, 15, 16]. Although limited to quasi-static phenomena, the method is combined with dynamical methods to assess this property. In this approach, the YLM computes the space distribution of the fluids in an interest capillary pressure then the distribution is used as input data in a dynamical method (like LBM) to determine the flow rate in the percolating phase.

The lack of a proper and uniform name is a minor difficulty one will face trying to apply the YLM. A greater obstacle is the absence of detailed information regarding its implementation. Except for a few papers [4, 14], most works are restricted to describing the general characteristics of the method, even when relevant changes are proposed [9]. Frequently, critical computational aspects that can impact computational efficiency are not properly discussed. This prevents the comparison of obtained results with different implementations, difficult to replicate computational simulations for improving the algorithm, and makes unclear whether new developments affect computational efficiency.

In this paper, we present a unified algorithm for the Young–Laplace Method where both drainage and imbibition processes are considered in a single framework. It incorporates several existing improvements for drainage simulation [4, 5], and it correctly describes the presence of ganglia (quantities of fluid entrapped in the pore space, like bubbles). A more detailed description of the improvements implemented in the works of Hilpert and Miller [4] and Schutz et al. [5] is done in Appendix 2. More than just a grouping of previous works, we propose a new and coherent mathematical formalism that simplifies the understanding of the method and sets the basis upon which new improvements can be proposed. Moreover, an efficient C +  + code using this new structure is made available using the GNU GPLv3 software.

The paper is organized as follows. In Sect. 2, we describe some basic physical concepts regarding YLM. Section 3 presents our unified algorithm for the YLM and its formal mathematical description using the notation of set operations. In Sect. 4, we discuss the YLM for the imbibition case, which, so far, is not a well-addressed question. We show that the correction from Hilpert and Miller [4] does not need to be applied in this case and also reveal how to apply Schulz et al.’s [5] variable contact angles for imbibition processes. Finally, Sect. 5 summarizes the conclusions of the paper.

2 Basic Concepts

The main purpose of the YLM is to find the spatial distribution of both the invading and displaced fluid inside a porous media, without employing dynamical equations. Since it is limited to the prediction of the equilibrium states of capillary pressure under this condition, it is commonly regarded as a quasi-static method.

YLM relies on the Young–Laplace equation to determine the interface liquid curvature in the non-wetting and wetting phases [17]. Additionally, the method employs an approximation that the two radii of curvature (\(R\)) are equal which, for a cylindrical capillary tube results on

$${P}_{{\text{c}}}={P}_{{\text{N}}}-{P}_{{\text{W}}}=\frac{2\sigma\; cos\theta }{{r}_{{\text{c}}}}$$
(1)

where \({P}_{{\text{c}}}\) is the capillary pressure, i.e., the difference between the pressure in the non-wetting (\({P}_{{\text{N}}}\)) and wetting (\({P}_{{\text{W}}}\)) phases, and \(\sigma\) is the interfacial tension. As shown in Fig. 1a, \(\theta\) is the wetting fluid contact angle, and \({r}_{{\text{c}}}\) is the capillary radius (\({r}_{c}=R cos\theta\)).

Fig. 1
figure 1

The non-wetting and wetting fluids are presented in blue and red, respectively. a Capillary rise of a wetting fluid in a cylindrical tube. The figure depicts the longitudinal section of the tube. b Two equilibrium states for different values of the capillary pressure (\({P}_{{\text{c}}}\)). \({{P}_{{\text{c}}}}^{({\text{I}})}<{{P}_{{\text{c}}}}^{({\text{II}})}\), resulting in a larger curvature radius in the upper case. c Comparison between equilibrium states obtained by YLM and LBM for a drainage process

As seen in Eq. (1), the importance of capillary pressure in multiphase fluid flows is evident as the capillary radius decreases, especially in strongly wetting conditions. Therefore, modeling multiphase flows in porous media needs to consider the influence of capillary effects, as both the fluid flow dynamics in micro- and nanopores and the thermodynamic properties of the phases involved can be altered [18,19,20,21]. Figure 1b shows how the regions occupied by each fluid are affected by the change in capillary or applied pressure for a capillary tube with a variable cross-section. In the upper section, the non-wetting fluid (blue) cannot access the narrower part of the channel because a low-pressure value corresponds to a large curvature radius. In the narrower section, the pressure difference is increased, and the new equilibrium is related to smaller radii. The main idea behind the YLM, as proposed by Hazlett [12] and Magnani [13] came from a simple idea. Considering a null contact angle and a given value of \({P}_{{\text{c}}}\), a region can only be occupied by the non-wetting fluid if it is possible to inscribe a sphere of radius \(R=2\sigma /{P}_{{\text{c}}}\) inside this region.

Figure 1b presents scenarios where the two fluids are statically in equilibrium. In the absence of fluid motion, the two fluids should remain in this configuration. If a dynamic process of invasion is carried out with a low capillary number (when capillary forces are dominant over the viscous ones), then we expect the system to pass through these configurations during the invasion process. Thus, finding these states is also necessary for understanding dynamic processes.

During drainage, increasing the pressure associated with the injected non-wetting fluid displaces the wetting fluid that initially fills the porous media. The increase occurs in small steps to allow the fluids to reach new equilibrium configurations. On the other hand, imbibition happens when the wetting fluid invades the region previously filled by the non-wetting fluid when the pressure of the latter is decreased. Using Fig. 1b as an illustration, during the drainage the system would go from states I to II, whereas for the imbibition, the wetting front evolution is the opposite.

Figure 1c shows a comparison between the equilibrium states of drainage predicted by the YLM and by the Shan–Chen multicomponent LBM (SC-LBM) [22,23,24,25]. Despite some small differences, the results from the YLM are pretty close to the ones from SC-LBM, showing that the method can predict the static equilibrium states of this stepwise invasion process.

In this study, we included the results of SC-LBM simulations as an indication of quality to show that YLM provides satisfactory results for the invasion process. Although we considered an idealized porous medium, the LBM (and its variations) has been considered a very powerful tool for predicting transport phenomena in more complex situations involving realistic 2D and 3D porous media [26,27,28,29,30,31,32,33,34,35].

3 Unified Algorithm

The YLM is applied to 2D or 3D segmented images; the algorithm described works in both cases without any adaptation. Nonetheless, to be coherent with the images used in the paper, we will use expressions related to the 2D case, such as “pixel” and “circle” rather than “voxel” and “sphere”.

Operations over images commonly require iterated loops over all its pixels. It occurs, for example, when determining the image regions where a circle can be inscribed without overlapping solid pixels, as illustrated in Fig. 2. A first approach is “to move” the circle over the whole image checking whether or not it overlaps solid pixels. Unfortunately, this leads to making operations of the order of \(\mathcal{O}({n}^{2})\) where \(n\) is the number of pixels in the image. It is something that is computationally unfeasible if it is applied to three-dimensional digital porous media. Additionally, the YLM requires these regions to be found for different circle radii (each radius is associated with a \({P}_{{\text{c}}}\) value by Eq. (1)), so it is essential that the algorithm does not demand all the computations to be remade for each new value. Thus, in order to obtain an efficient YLM algorithm, this question must be carefully addressed.

Fig. 2
figure 2

Determination of the pixels inside the porous region which can be occupied by a circle of 7 pixels in diameter (region delimited by the white line). The black pixels represent the solid region while the red pixels are the ones that could not be occupied by the circle

To the best of the author’s knowledge, there is not any work describing in detail how to implement the YLM efficiently. The solution is to use Mathematical Morphology (MM) algorithms [36] since they allow discovering the region occupied by a predetermined circle, with a very low computational cost operation called opening (a more detailed discussion of MM is addressed in Appendix 1). This operation is performed with the aid of the Euclidian Distance Transform (\(EDT\)), which only needs to be executed once for an image. The EDT can be computed using a very efficient and parallel algorithm proposed by Saito and Toriwaki [37], and the result is a map showing the distance of each pore pixel to the closest solid pixel. Then, when these regions are found, another algorithm is used to discover the regions connected to the source of the invading fluid.

Although improvements regarding drainage simulations were mentioned in previous sections, there are some significant issues that we have not yet addressed. Among them are (i) the compressible behavior of the displaced fluid, (ii) the use of these existing improvements in imbibition simulations, and (iii) the treatment of isolated bubbles of the invading fluid. All of these issues often play a significant role in practical problems.

Previous works [13, 14] devoted some attention to these questions. However, when trying to adapt the method to some selected cases, the authors created countless nomenclatures and notations, which added significant complexity to the algorithm. As described in Sect. 1, these are limiting factors for the wider adoption of the YLM. In this section, we develop a unified algorithm that combines these different solutions in a simpler approach.

3.1 Benchmark Geometry

We adopted the benchmark geometry of Mohammadmoradi and Kantzas [7]. Our benchmark is shown in Fig. 3. We have restricted its application to porous media with a simplified 2D geometry to test and exemplify the algorithm’s performance in several situations found in, for instance, water/oil-filled rocks: ganglia, dead-end pores, isolated pores, and channels with different shapes and sizes. However, we emphasize the YLM is applied to 2D and 3D cases without any modifications. The wetting (W) and non-wetting (N) fluids are represented by red and blue colors, respectively. The top figure represents the initial state for the drainage, and the bottom figure is the one for the imbibition. In both cases, the inlet is positioned on the left side and the outlet on the right side of the medium. Thus, the dark blue and dark red squares are the source or sink of each fluid, depending on the case.

Fig. 3
figure 3

The benchmark geometry used to analyze and demonstrate the unified algorithm. The top and bottom figures represent the initial fluids distribution for drainage and imbibition, respectively. The wetting and non-wetting fluids are represented, respectively, by red and blue colors. In both cases, the inlet is positioned on the left side and the outlet on the right side of the medium. Thus, the dark blue and dark red squares are the source or sink of each fluid, depending on the case

A semi-permeable membrane was added in different locations in each case. In the drainage process, this membrane is positioned just before the outlet to ensure that the maximum number of pores will be invaded before the fluid finally percolates. For the imbibition process, the membrane is located after the inlet to prevent the invading fluid from filling the whole geometry in a single pressure step.

Figure 4a, b show equilibrium states for drainage and imbibition, respectively, for a \(0^\circ\)-contact angle. The case for a \(45^\circ\)-contact angle is shown in Fig. 4c, d. Each case is represented by a two-letter code. The first relates to the wettability of the invading fluid: non-wetting (N) or wetting (W). The second one indicates if the displaced fluid is incompressible (I) or compressible (C). Thus, the four cases of interest are expressed as NC, NI, WI, and WC. During drainage, the \({P}_{{\text{c}}}\) increases up to the point that the capillary diameter reaches a pixel length (Eq. (1)). In the imbibition the opposite occurs, i.e., initially, the \({P}_{{\text{c}}}\) is high, and as the pressure decreases, the wetting fluid starts to occupy the available channels. For all four cases, the algorithm provided physically consistent results. Each row in the figures states a specific value of the \({P}_{{\text{c}}}\), and they resulted from a sequence of operations applied over the regions of interest of the benchmark images (Fig. 3). These operations are described in the next section.

Fig. 4
figure 4

Selected equilibrium states for the drainage (a and c) or imbibition (b and d) using the unified YLM algorithm in the benchmark geometries with a contact angle of 0° (a and b) or 45° (c and d). Each case is represented by a two-letter code. The first relates to the wettability of the invading fluid: non-wetting (N) or wetting (W). The second one indicates if the displaced fluid is incompressible (I) or compressible (C)

3.2 Algorithm’s Description

The first step in building this framework is to identify the minimal set of regions of interest over which the operations will be performed. Since each of these regions is represented computationally by a matrix, the use of too many of them results in a high computational cost, and it is time-consuming. While Hazlett [12] defined these regions vaguely, Magnani [13] and Magnani et al. [14] used a non-necessary large number of regions. Hilpert and Miler [4] presented a clear definition while providing simple operators to manipulate these regions based on the Mathematical Morphology formalism. Thus, in this present work, we keep their definitions, where the current state is defined by three regions: the invading fluid, the displaced fluid, and the solid. Additionally, some pixels must be labeled as the source and sink of the invading and displaceable fluids.

Figure 5 shows a schematic picture of the YLM algorithm. We use image analysis notation based on set operations. The sequence and number of proposed operations were designed to allow a unified algorithm for the four cases of interest (NC, NI, WI, and WC). Each column of the figure represents each of these cases. For a better organization, the operations are grouped into four steps, with the last step being performed solely for the two incompressible cases (NI and WI).

Fig. 5
figure 5

The Algorithm structure is shown in four steps. Set operations represent the algorithm structure for each of the four cases. The symbols are: porous region (\(P\)), structuring element (\(D^{\prime}\)), 1st step set (\({A}_{{\text{i}}}\)), 2nd step operations (\({N}_{{\text{i}}}^{({\text{k}})}\) or \({W}_{{\text{i}}}^{({\text{k}})}\), it depends on the case), filtering process at the invading fluid source (\({F}_{{I}_{{\text{v}}}}\)), distribution of the invading fluid at the previous pressure step (\({I}_{{\text{i}}-1}\)), 3rd step set (\({B}_{{\text{i}}}\)), 4th step operations (\({C}_{{\text{i}}}^{({\text{k}})}\)), filtering process at the displaced fluid sink (\({F}_{{O}_{{\text{v}}}}\)), and distribution of the displaced fluid at the previous pressure step (\({O}_{{\text{i}}-1}\))

Common to all cases, the first step (Fig. 5) erodes the porous region (\(P\)) using a circle as a structuring element (\(D{\prime}\)), resulting in the \({A}_{i}\) set. The second one consists of a group of four operations (\({N}_{i}^{(k)}\) for NC and NI; \({W}_{i}^{(k)}\) for WC and WI) that expands the previous region excluding parts not connected to the input fluid source. The symbol \({F}_{{I}_{{\text{v}}}}\) denotes the operation or filtering process that guarantees that only the sections connected to the invading fluid source (\({I}_{{\text{v}}}\)) should remain, and \({I}_{{\text{i}}-1}\) indicates the distribution of the invading fluid at the previous pressure step. The operations order depends on the type of fluid, as shown in Fig. 5.

After identifying the regions occupied by the invading fluid, associated with a \({P}_{{\text{c}}}\), the regions originally disconnected (ganglia) are included in the third step, indicated by \({B}_{{\text{i}}}\) (Fig. 5). If the displacement fluid is fully compressible (NC/WC), this is the last stage for the current \({P}_{{\text{c}}}\). However, if the displaced fluid is incompressible (NI and WI), it is still necessary to exclude its parts that are not connected to the sink. It happens as the fluid has no place to flow to, and it cannot be compressed; then, this portion of the fluid remains trapped in its position in the structure. The fourth step (Fig. 5) operation is indicated by the \({C}_{{\text{i}}}^{(k)}\). In a similar way to \({F}_{{I}_{{\text{v}}}}\) and \({I}_{{\text{i}}-1}\), \({F}_{{O}_{{\text{v}}}}\), is the filtering process that guarantees that only the sections connected to the displaced fluid sink (\({O}_{{\text{v}}}\)) should remain, and \({O}_{{\text{i}}-1}\) indicates the distribution of the displaced fluid at the previous pressure step.

The results of each step in Fig. 5 performed by the YLM algorithm are exemplified and commented in Figs. 6 (NC and WC cases) and 7 (NI and WI cases) for one of the pressure steps. As for the input data, no special requirement was imposed on the initial fluids distribution or by the geometry, the algorithm is rather general.

Fig. 6
figure 6

Image analysis operations to determine the distribution of fluids in one pressure step. a Drainage: the invading fluid is non-wetting and the displaced fluid is fully compressible (NC). b Imbibition: the invading fluid is wetting, and the displaced fluid is fully compressible (WC). The fluid source \({I}_{{\text{v}}}\) is at the extreme left and the fluid sink \({O}_{{\text{v}}}\) at the extreme right. The beige pixels represent the input data of the algorithm: porous region and initial (or previous pressure step) distribution of the invading fluid. The purple pixels indicate the operations performed to find the final image that represents the obtained result for the one-step drainage or imbibition

Fig. 7
figure 7

Image analysis operations to determine the distribution of fluids in one pressure step. a Drainage: the invading fluid is non-wetting and the displaced fluid is fully incompressible (NI). b Imbibition: the invading fluid is wetting, and the displaced fluid is fully incompressible (WI). The fluid source \({I}_{{\text{v}}}\) is at the extreme left and the fluid sink \({O}_{{\text{v}}}\) at the extreme right. The beige pixels represent the input data of the algorithm: porous region and initial (or previous pressure step) distribution of the invading fluid. The purple pixels indicate the operations performed to find the final image that represents the obtained result for the one-step drainage or imbibition

The capillary curve is then obtained by applying the following equation with different radii for the circle, whose values are given by the \({P}_{{\text{c}}}\) using the Young–Laplace Equation (Eq. (1)).

$$s\left(r\right)=\frac{\sum \left\{\left[{F}_{{{\text{I}}}_{{\text{v}}}}(P\ominus {D}^{\prime})\right]\oplus D\right\}}{\sum P}$$
(2)

where \(s(r)\) is the saturation of the invading fluid for a circle radius (\(r\), and \(D^{\prime}\equiv D^{\prime}(r)\)), and the summation symbol represents the operation of counting the pixels of each region, and the operator \({F}_{{I}_{v}}\), denotes the process of filtering the pixels connected with the invading fluid source.

4 Imbibition Case

As discussed earlier, the correction proposed by Hilpert and Miller [4] and the generalization proposed by Schulz et al. [5] to YLM were not applied to the imbibition case in other works. In this section, we show that there was no need to use Hilpert’s correction for imbibition and that the Schulz generalization can be applied to it, only if an appropriate membrane is used.

In drainage, larger channels drain first unless there is a narrow channel in the pathway to restrict them. The restriction is due to the higher \({P}_{{\text{c}}}\) required for draining these narrow channels. It is called the ink bottle effect. The approach proposed by Hilpert and Miller [4] is applied to correct an overestimated saturation due to ink bottle geometries inside the porous space after the opening process (see Fig. 13 in Appendix 2). However, the same effect cannot happen on imbibition because the smaller channels fill first. Therefore, Hilpert’s correction is unnecessary for imbibition.

The generalization of Schulz et al. [5] to other contact angles can be very useful in imbibition since wettability is an important factor. Nevertheless, as far as we know, it has never yet been described. Figure 8 shows that according to the Schulz process the dilation with \(R {\text{cos}}\theta\) results in a displaced fluid that moves backward, preventing the wetting fluid from entering the membrane and, subsequently, in the channel. The solution is straightforward: use an elongated membrane with a length greater than the difference in radii of the two circles,

$${L}_{{\text{m}}}>R-{R}^{\prime}=R^{\prime}\left(\frac{1}{{\text{cos}}\theta }-1\right)$$
(3)

where \({L}_{{\text{m}}}\) is the membrane length, \(R^{\prime}\) is the half channel width, and \(\theta\) is the contact angle as indicated in Fig. 8. Some selected simulation steps are shown in Fig. 4a, c for contact angles of \(0^\circ\) and \(45^\circ\), respectively.

Fig. 8
figure 8

Attempt to use the generalization by Schulz et al. [5] to simulate imbibition with a different wettability angle. Erosion for a circle radius and subsequent dilation by a circle of a larger radius is indicated in the same image

5 Conclusion

In this work, we presented an algorithm to simulate capillary-driven two-phase fluid flow in porous media by combining several previous works into a single unified framework. Following Hazlett [12], Magnani [13], and Magnani et al. [14], the algorithm uses the Mathematical Morphology formalism. We also incorporated improvements from Hilpert and Miller [4] and Schulz et al. [5]. In addition to the unified structure, we offer, for the first time in the literature, a complete and detailed description of the YLM algorithm and show how to apply it in the imbibition case.

This generalized framework allows the simulation of both drainage and imbibition problems with the displaced fluid being either fully compressible or incompressible. The unified algorithm is also capable of handling any geometry or initial fluid distributions. So, one possibility of application for this unified algorithm is predicting the hysteretic characteristic of the soil retention curve by drying and wetting cycles. However, it is not limited to this application.

Finally, we offer an open-source C +  + code for it, something that will help the YLM to gain even more space in different applications involving transport phenomena in digital porous media.

6 Computer Code Availability

The Young–Laplace Method unified algorithm is available as a C +  + source code, accompanied by Python 3 + example scripts. The source code is licensed under the GPL-3.0 License. The source code and examples can be downloaded from the data repository [38].