How to resolve the algorithm Percolation/Bond percolation step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Percolation/Bond percolation step by step in the Java programming language

Table of Contents

Problem Statement

Given an

M × N

{\displaystyle M\times N}

rectangular array of cells numbered

c e l l

[ 0.. M − 1 , 0.. N − 1 ]

{\displaystyle \mathrm {cell} [0..M-1,0..N-1]}

, assume

M

{\displaystyle M}

is horizontal and

N

{\displaystyle N}

is downwards. Each

c e l l

[ m , n ]

{\displaystyle \mathrm {cell} [m,n]}

is bounded by (horizontal) walls

h w a l l

[ m , n ]

{\displaystyle \mathrm {hwall} [m,n]}

and

h w a l l

[ m + 1 , n ]

{\displaystyle \mathrm {hwall} [m+1,n]}

; (vertical) walls

v w a l l

[ m , n ]

{\displaystyle \mathrm {vwall} [m,n]}

and

v w a l l

[ m , n + 1 ]

{\displaystyle \mathrm {vwall} [m,n+1]}

Assume that the probability of any wall being present is a constant

p

{\displaystyle p}

where Except for the outer horizontal walls at

m

0

{\displaystyle m=0}

and

m

M

{\displaystyle m=M}

which are always present. Simulate pouring a fluid onto the top surface (

n

0

{\displaystyle n=0}

) where the fluid will enter any empty cell it is adjacent to if there is no wall between where it currently is and the cell on the other side of the (missing) wall.
The fluid does not move beyond the horizontal constraints of the grid. The fluid may move “up” within the confines of the grid of cells. If the fluid reaches a bottom cell that has a missing bottom wall then the fluid can be said to 'drip' out the bottom at that point. Given

p

{\displaystyle p}

repeat the percolation

t

{\displaystyle t}

times to estimate the proportion of times that the fluid can percolate to the bottom for any given

p

{\displaystyle p}

. Show how the probability of percolating through the random grid changes with

p

{\displaystyle p}

going from

0.0

{\displaystyle 0.0}

to

1.0

{\displaystyle 1.0}

in

0.1

{\displaystyle 0.1}

increments and with the number of repetitions to estimate the fraction at any given

p

{\displaystyle p}

as

t

100

{\displaystyle t=100}

. Use an

M

10 , N

10

{\displaystyle M=10,N=10}

grid of cells for all cases. Optionally depict fluid successfully percolating through a grid graphically. Show all output on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Percolation/Bond percolation step by step in the Java programming language

Percolation on a 2D grid

This Java program simulates a percolation experiment on a 2D grid. Percolation is a statistical concept used to describe the behavior of fluids flowing through a porous material. In this program, the grid represents the porous material, and the fluid is water.

Grid Representation

  • The grid is represented as an array of integers, where each integer represents a square on the grid.
  • Each square can have three possible states:
    • FILL, indicating that the square is filled with water.
    • RIGHT_WALL, indicating that the square has a wall on its right side.
    • LOWER_WALL, indicating that the square has a wall on its bottom side.

Percolation Experiment

  • The program starts by creating a COL_COUNT x ROW_COUNT grid.
  • Each square in the grid is randomly assigned a value of FILL, RIGHT_WALL, or LOWER_WALL with a given probability.
  • The program then simulates water flowing through the grid. Water can flow through a square if it is not blocked by a wall.
  • The program checks if water can flow from the top of the grid to the bottom. If it can, the grid is said to percolate.

Output

  • The program prints out the grid before and after the percolation experiment.
  • It also prints out the proportion of grids that percolate for different probabilities of filling.

Detailed Explanation

  • The main method sets up the grid, runs the percolation experiment, and prints out the results.
  • The makeGrid method creates a new grid and randomly assigns values to each square.
  • The showGrid method prints out the grid.
  • The fill method simulates water flowing through the grid. It returns true if water can flow from the top of the grid to the bottom.
  • The percolate method runs the percolation experiment and returns true if the grid percolates.

Example Output

Sample percolation with a 10 x 10 grid:
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
|    |  []|  []|  []|  []|  []|  []|  []|    |
+---+---+---+---+---+---+---+---+---+---+
Using 10,000 repetitions for each probability p:
p = 0.1:   0.0000
p = 0.2:   0.0001
p = 0.3:   0.0007
p = 0.4:   0.0064
p = 0.5:   0.0591
p = 0.6:   0.3151
p = 0.7:   0.7724
p = 0.8:   0.9574
p = 0.9:   0.9991

This output shows that the grid percolates with a probability of 0.591 when the probability of filling a square is 0.5.

Source code in the java programming language

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;

public final class PercolationBond {

	public static void main(String[] aArgs) {
		System.out.println("Sample percolation with a " + COL_COUNT + " x " + ROW_COUNT + " grid:");
		makeGrid(0.5);
		percolate();
		showGrid();
		
		System.out.println("Using 10,000 repetitions for each probability p:");
	    for ( int p = 1; p <= 9; p++ ) {
	        int percolationCount = 0;
	        double probability = p / 10.0;
	        for ( int i = 0; i < 10_000; i++ ) {
	            makeGrid(probability); 
	            if ( percolate() ) {
	            	percolationCount += 1;
	            }
	        }
	        final double percolationProportion = (double) percolationCount / 10_000;
	        System.out.println(String.format("%s%.1f%s%.4f", "p = ", probability, ":  ", percolationProportion));
	    }
	}
	
	private static void makeGrid(double aProbability) {	    
	    Arrays.fill(grid, 0);
	    for ( int i = 0; i < COL_COUNT; i++ ) {
	    	grid[i] = LOWER_WALL | RIGHT_WALL;
	    }

	    endOfRow = COL_COUNT;
	    for ( int i = 0; i < ROW_COUNT; i++ ) {
	        for ( int j = COL_COUNT - 1; j >= 1; j-- ) {	        	
	        	final boolean chance1 = RANDOM.nextDouble() < aProbability;
	        	final boolean chance2 = RANDOM.nextDouble() < aProbability;
	            grid[endOfRow++] = ( chance1 ? LOWER_WALL : 0 ) | ( chance2 ? RIGHT_WALL : 0 );
	        }
	        final boolean chance3 = RANDOM.nextDouble() < aProbability;
	        grid[endOfRow++] = RIGHT_WALL | ( chance3 ? LOWER_WALL : 0 );
	    }
	}
	
	private static void showGrid() {
		for ( int j = 0; j < COL_COUNT; j++ ) {
	    	System.out.print("+--");
	    }
	    System.out.println("+");
		
		for ( int i = 0; i < ROW_COUNT; i++ ) {
			System.out.print("|"); 
	        for ( int j = 0; j < COL_COUNT; j++ ) {
	            System.out.print( ( ( grid[i * COL_COUNT + j + COL_COUNT] & FILL ) != 0 ) ? "[]" : "  " );
	            System.out.print( ( ( grid[i * COL_COUNT + j + COL_COUNT] & RIGHT_WALL ) != 0 ) ? "|" : " " );
	        }
	        System.out.println();
		
	        for ( int j = 0; j < COL_COUNT; j++ ) {
	            System.out.print( ( ( grid[i * COL_COUNT + j + COL_COUNT] & LOWER_WALL) != 0 ) ? "+--" : "+  " );
	        }
	        System.out.println("+");
		}
		
		System.out.print(" "); 
		for ( int j = 0; j < COL_COUNT; j++ ) {
            System.out.print( ( ( grid[ROW_COUNT * COL_COUNT + j + COL_COUNT] & FILL ) != 0 ) ? "[]" : "  " );
            System.out.print( ( ( grid[ROW_COUNT * COL_COUNT + j + COL_COUNT] & RIGHT_WALL ) != 0 ) ? "|" : " " );
        }
        System.out.println(System.lineSeparator());
	}
	
	private static boolean fill(int aGridIndex) {
	    if ( ( grid[aGridIndex] & FILL ) != 0 ) {
	    	return false;
	    }	    
	    grid[aGridIndex] |= FILL;
	    
	    if ( aGridIndex >= endOfRow ) {
	    	return true;
	    }	    
	    return ( ( ( grid[aGridIndex]             & LOWER_WALL ) == 0 ) && fill(aGridIndex + COL_COUNT) ) ||
	           ( ( ( grid[aGridIndex]             & RIGHT_WALL ) == 0 ) && fill(aGridIndex + 1) )         ||
	           ( ( ( grid[aGridIndex - 1]         & RIGHT_WALL ) == 0 ) && fill(aGridIndex - 1) )         ||
	           ( ( ( grid[aGridIndex - COL_COUNT] & LOWER_WALL ) == 0 ) && fill(aGridIndex - COL_COUNT) );
	}

	private static boolean percolate() {
		int i = 0;
	    while ( i < COL_COUNT && ! fill(COL_COUNT + i) ) {
	    	i++;
	    }
	    return i < COL_COUNT;
	}
		
	private static final int ROW_COUNT = 10;
	private static final int COL_COUNT = 10;
	
	private static int endOfRow = COL_COUNT;
	private static int[] grid = new int[COL_COUNT * ( ROW_COUNT + 2 )];

	private static final int FILL = 1;
	private static final int RIGHT_WALL = 2;
	private static final int LOWER_WALL = 4;

	private static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current();
	
}


  

You may also check:How to resolve the algorithm Program name step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Brainf*** programming language
You may also check:How to resolve the algorithm Strip control codes and extended characters from a string step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm HTTP step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Combinations step by step in the Picat programming language