Csci 210 Lab: Percolation

(Laura Toma)
(adapted from Sedgewick & Wayne, Stanford)


In this lab you will design a tool to help investigate a phenomenon called percolation. Percolation concerns the movement of fluids through porous material.

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.

Program structure

The user will be able to execute two classes: Visualize and Estimate. Both of them take on the command line 4 arguments: N, p, T and {v or g}. The last argument stands for vertical or general percolation.

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 v
will 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 v
will 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:

Working with files in Java

In a nutshell, the code creates a new File, then opens the file with the Scanner class. Opening a scanner from a file must be checked for an exception to make sure everything went ok. Once it has been opened, then the Scanner can basically act as an interator.
File f = new File(fileName);
Scanner sc = null;
try {
     sc = new Scanner(f);
    } catch (FileNotFoundException e) {
     System.err.println("File not found!");
//here is how you would read a file of integers
while (sc.hasNextInt()) {
    int nextOne = sc.nextInt();
Last modified: Fri Nov 13 18:00:53 EST 2009