Percolation Simulation - Part 1

Percolation simulations are easy!

Part 1

Part 1 - Part 2 - Part 3

We are going to write a 2d percolation simulation in Rust. For part 1, we are going to do the basics.

A 2D percolation simulation is a computational tool used to model the concept of percolation in a two-dimensional space.

Percolation theory studies the behaviour of connected clusters in a random graph, and it's often used in the context of material science, geology, physics, and more. In particular, it can describe how a fluid might flow through a porous medium, or how a disease might spread through a population.

In the most basic 2D percolation simulation, you might have a square grid (or array) where each cell can either be "open" or "blocked". An "open" cell represents a part of the material that fluid can flow through, while a "blocked" cell represents an impermeable part of the material.

The main point of interest in a percolation simulation is the existence of a "percolating cluster". This is a path of connected "open" cells that spans from one side of the grid to the opposite side. If such a path exists, the system is said to percolate.

Applications of percolation simulations are quite wide-ranging, and include areas like understanding the porous structure of rocks for oil extraction, studying how forest fires spread, modelling the spread of diseases in a population, understanding how electrical conductivity changes in composite materials, and more.

I assume you have already installed Rust. If not, go to Install Rust on the Rust-Lang website.

For this first part, we are going to create an array and populate it with the default value of zero in each cell, print the array out, change the values in the array and print the array out again.

Things to note:

  • We will set up constants for the height and width of the array. Note, in Rust the protocol is that the name given to a constant is always in uppercase.
  • When we change the values in the array, we want to leave a boarder - or halo - of zeros round the array.
  • Because we are going to print the array out more than once we will create a function to do this.
  • The Rust code for part 1

    fn print_array(array:&Vec>, height: usize, width: usize){
    //function to print out the array to the teminal
    	for i in 0..height{
    		for j in 0..width{
    			print!("{:0>3} ",array[i][j]);
    		}
    	println!("");
    	}
    // end print_array function
    }
       
    fn main() {
    //define array, constants and variables
    	const WIDTH: usize = 16;
    	const HEIGHT: usize  = 16;
       
    //define array and populat with zeros
    	let mut array = vec![vec![0; WIDTH]; HEIGHT];
    
    //print array
    	print_array(&array, HEIGHT, WIDTH);
            
    //populate array with consecutive numbers where 0 is rock and give it a rock boarder
    	for i in 1..HEIGHT-1{
    		for j in 1..WIDTH-1{
    			array[i][j]=(i*HEIGHT)+j;
    		}
    	}
       
    //print array
    	println!("");
    	print_array(&array, HEIGHT, WIDTH);
    
    //end of program
    }
                    

    The code output

    me@pi$ cargo run
    Compiling arraytest v0.1.0 (/home/me/rust/arraytest)
    Finished dev [unoptimized + debuginfo] target(s) in 0.30s
    Running `target/debug/arraytest`
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    000 017 018 019 020 021 022 023 024 025 026 027 028 029 030 000
    000 033 034 035 036 037 038 039 040 041 042 043 044 045 046 000
    000 049 050 051 052 053 054 055 056 057 058 059 060 061 062 000
    000 065 066 067 068 069 070 071 072 073 074 075 076 077 078 000
    000 081 082 083 084 085 086 087 088 089 090 091 092 093 094 000
    000 097 098 099 100 101 102 103 104 105 106 107 108 109 110 000
    000 113 114 115 116 117 118 119 120 121 122 123 124 125 126 000
    000 129 130 131 132 133 134 135 136 137 138 139 140 141 142 000
    000 145 146 147 148 149 150 151 152 153 154 155 156 157 158 000
    000 161 162 163 164 165 166 167 168 169 170 171 172 173 174 000
    000 177 178 179 180 181 182 183 184 185 186 187 188 189 190 000
    000 193 194 195 196 197 198 199 200 201 202 203 204 205 206 000
    000 209 210 211 212 213 214 215 216 217 218 219 220 221 222 000
    000 225 226 227 228 229 230 231 232 233 234 235 236 237 238 000
    000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
    me@pi$
                    

    Mission Accomplished

    And there we have it, step one complete, you have an array, you can modify the cells and you can print it out.

    Part 1 - Part 2 - Part 3