#!/usr/bin/env pythonfrom scipy import *
def printbinarymatrix(A):
m,n = A.shape
s = '\n'.join([ \
''.join([( ' 'if i < 0.5else'*' ) for i in row]) for row in A])
s = '\n'.join(['-'*(n+2),s,'-'*(n+2)])
print s
returndef calc_percolation_depth(A, i0=None):
""" Given a matrix A of {0,1}'s, find the farthest across the matrix we can walk on 0-entries, when using a von Neumann neighborhood, if we start at the left side in row i0. If i0 == None, return the maximum over all initial rows. """
upper_bound = A.shape[1]
if i0 == None:
returnmax([calc_percolation_depth(A,i) for i in xrange(upper_bound)])
neighbors = [(1,0), (-1,0), (0,1), (0,-1)]
# to track which sites we have visited ...
visited = 0*A
defnext(i,j):
if i < 0 or i >= A.shape[0]:
return0if j >= upper_bound:
return upper_bound
if j < 0 or j > A.shape[1]:
return0if visited[i,j] == 1:
return0
visited[i,j] = 1if A[i,j] > 0.5:
return j
returnmax(j,max([next(i+dx,j+dy) for (dx,dy) in neighbors]))
returnnext(i0,0)
def test():
p = 0.45
N = 12
A = floor(rand(N,N) + p)
assertset([0,1]) == set(map(int,A.flatten()))
print"Here we have a %dx%d matrix, with fill fraction %f"%(N,N,p)
printbinarymatrix(A)
print"If we start on the left side, how far toward the right can we move on blanks?"for i in range(N):
d = calc_percolation_depth(A,i)
print"%2d steps on row %d"%(d,i)
print"Max depth: ", calc_percolation_depth(A)
print"Now, double-check with your own eyes. Are all of these correct?"
test()
+------------+
|* * * ** * |
|*** ** *|
| * * * |
|*** * *** |
| ** *** |
|* * ***|
| * * * * *|
| *** * *|
|** ****** |
|** *** *|
| *** * *** |
|* ** *** * |
+------------+
If we start on the left side, how far toward the right can we move on 0's?
0 steps on row 0
0 steps on row 1
6 steps on row 2
0 steps on row 3
9 steps on row 4
0 steps on row 5
9 steps on row 6
9 steps on row 7
0 steps on row 8
0 steps on row 9
1 steps on row 10
0 steps on row 11
Max depth: 9
Now, double-check with your own eyes. Are all of these correct?
For what p would the water stop flowing?
Can we walk from a random site from the left edge to the right edge?
plot the max path length as a function of p
Cluster-size distribution and finite-size effects...
Percolation threshold for site-percolation on a von-Neumann neighborhood... p_c = 0.5927... (no exact formula is known)
"""A simple and fast implementation of conway'sgame-of-life using convolutionsBug: Currently does not play well with Canopy. Have not figured out why.Timothy Reluga2016.03.25"""import numpy as np
import time
from scipy.signal import convolve, fftconvolve
import matplotlib.pyplot as plt
#print plt.get_backend()def conway(state):
""" Conway's game of life state transition """
kernel = np.array([[1,1,1],[1,0,1],[1,1,1]])
# computes sums around each pixel#sums = convolve(state, kernel, mode='same') # slow normal convolutions
sums = fftconvolve(state, kernel, mode='same').round() # fast convolutions
newstate = np.zeros(sums.shape)
newstate[np.where((sums == 2) & (state == 1))] = 1
newstate[np.where((sums == 3) & (state == 1))] = 1
newstate[np.where((sums == 3) & (state == 0))] = 1# return new statereturn newstate
def main():
p = 0.1
sleeptime = 0.05# set up board
m,n = 150,200
A = np.floor(p+np.random.random(m*n)).reshape((m, n))
# plot each frame
plt.ion()
plt.figure(1, figsize=(9,9))
img_plot = plt.imshow(A, interpolation="nearest", cmap = plt.cm.gray)
plt.show(block=False)
whileTrue:
A = conway(A)
img_plot.set_data(A)
plt.draw()
time.sleep(sleeptime)
if__name__ == "__main__":
main()
Understanding Navier--Stokes in terms of cellular automata
The Navier-Stokes equations are of central importance in applied-mathematics -- such importance that a prize of 1 million dollars has been offered to anybody who can show that they fully describe the motion of a fluid, and never break-down in finite time.
HHP and FHP models of fluids and Navier stokes as lattice gasses ( see paper notes)