We will model the system by an N-by-N grid of sites. Each site is either blocked or open. Open sites can be full or empty; blocked sites are always empty. Initially all sites are empty. The process starts by filling the open sites in the top row of cells. We want to model the fluid as it moves through the grid. We say that a system percolates if, at the end of the movement, there is a full site on the bottom row. If all bottom cells are empty, the system does not percolate.
Let's talk about modeling the fluid moving through the grid. The first version that we'll consider is vertical percolation. Imagine the fluid moving straight down (not sideways, and not up). So an open cell gets filled if it can be connected to a full cell in the top row via a chain of neighboring up cells. You can model this process as a row-by-row traversal of the grid, where each open cell on the row gets filled if the cell immediately above it is full.
General percolation With general percolation, we allow any path starting at the top and ending at the bottom (not just a vertical one). From any cell, the water can flow down, left, right, but also up. To compute all such paths, a row-by-row traversal like above is not enough; you will need to use recursion.
So, given a grid with open and blocked cells, you want to compute whether it percolates, either with vertical or with general percolation. You also want to visualize the grid after you let the fluid go through. This is only one part.
The other part is to help researchers study when a system percolates and when it does not. Researchers are interested in the following question: if the sites are independently set to be open with probability p, and therefore blocked with probability 1-p, what is the probability that the system percolates?
For this, you'll write code to set up a set of random grids (as specified above), compute whether they percolate, keep track of how many of them percolate, and display the probability. The probability p, the size of the grid N, and the number of trials should be specified by the user on the command line.
Class Visualize will generate T random grids of size N-by-N with vacancy probability p, and will visualize each grid (after you let the fluid go through). For example,
java Visualize 20 .8 1 vwill generate 1 grid of size 20-by-20 with vacancy probability .8, will simulate vertical percolation and will render the resulting grid. This is the tool used to view the percolation process.
Class Estimate will generate T random grids of size N-by-N with vacancy probability p, and will estimate the percolation probability. For example,
java Estimate 20 .8 100 vwill generate 100 grid of size 20-by-20 with vacancy probability .8, will simulate vertical percolation and will print the probability that a grid percolates. This is the tool used to compute the percolation probability.
In addition to these two classes, you will have a class Percolation to handle percolation.
Class Percolation should have the following:
5 5 0 1 1 0 1 0 0 1 1 1 1 1 0 1 1 1 0 0 0 1 0 1 1 1 1
java Percolates v testGrid.txt 5 5 0 1 1 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 percolateswith the filled grid renderd.
File f = new File(fileName); Scanner sc = null; try { sc = new Scanner(f); } catch (FileNotFoundException e) { System.err.println("File not found!"); System.exit(0); } //here is how you would read a file of integers while (sc.hasNextInt()) { int nextOne = sc.nextInt(); }Enjoy!