The percolation problem solver in a 2D domain
Description
This code simulates the percolation of a fluid through a porous medium, where the porosity is defined on a 2D grid (refer to Grid.cs class), and cell values may be either 0 ('closed' cell), 1 ('opened' pore), or 2 ('filled' by the fluid). The fluid penetrates the grid from its top side, and the system is considered percolated if one of the cells from the top side of the domain has a continuous path through the 'filled' grid's cells down to one of the cells of the bottom side of the domain.
The core class PercolationSolver.cs randomly opens the remaining 'closed' cells of the grid and builds a dynamic connectivity structure between all opened cells connected to the top side of the domain. The connectivity is resolved using either QuickFind, QuickUnion or WeightedQuickUnion algorithms implemented in the 'unionfind' package.
The source code is provided in both C# and Java languages. The compiled executables can be launched via Windows batch scripts: run.bat for the C#-version or runj.bat for the Java version.
The code is partially parallelized using Parallel.For
and .AsParallel()
in C#, and via parallel streams in Java.
Although written in C#, the key concepts of this code are inspired by Robert Sedgewick's works.
Demo
If the program is run with the '-image' command line argument, it saves cell values at every iteration as PPM images into the 'saves/*.ppm' directory. Closed cells are black, opened cells are gray, and cells filled by the fluid are white. When collected together, the frames look like this:
-res 20 -image |
-res 220 -image |
---|---|
![]() |
![]() |
The command-line argument '-console' enables pseudo-graphical rendering of grid data in console text mode. Vertical flickering occurs due to the automatic scrolling of the console text when new lines are printed. However, each previous frame can be observed by sliding the console scrollbar:
-res 20 -console
Usage
You can refer to the run.bat batch file to launch the C# app from the Windows console terminal, or you can refer to the runj.bat batch file to launch the Java JAR file, or you can launch the executable manually:
Language | Platform | Launch command |
---|---|---|
C# | Windows | asd_2_wquf_apps.exe -res 20 -console -image -ll |
C# | Linux/Mac | dotnet .\asd_2_wquf_apps.dll -res 20 -console -image -ll |
Java | Windows/Linux/Mac | java -jar .\asd_2_wquf_apps.jar -res 20 -console -image -ll |
The command line arguments have the following meanings:
Argument | Description |
---|---|
-res N |
represents the grid resolution (its rows and columns count) as N by N |
-console |
specifies to write the grid data into the console in an ASCII-art manner |
-image |
indicates whether to save the grid data into PPM files in the 'saves' directory |
-ll |
try to launch this application on multiple processor threads |
So, if you want to print only the cell data to the console, type:
asd_2_wquf_apps.exe -res 20 -console -ll
.
If you want to save only the cell data as PPM images, type:
asd_2_wquf_apps.exe -res 20 -image -ll
.
License
Published under the MIT License.