Skip to content

Instantly share code, notes, and snippets.

@sirpengi sirpengi/output.txt

Last active Aug 29, 2015
Embed
What would you like to do?
some sandpile thing
from tkinter import *
import numpy as np
np.set_printoptions(threshold=np.nan)
import random
import time
import pylab
import collections
class sandPile:
def __init__(self, L, rand):
self.L = L
#random.seed(210)
self.random = random # Determines whether or not the sand pile is randomized
self.sandArray = np.empty((self.L, self.L)) # Empty sand array
self.avalancheArray = np.zeros((self.L, self.L)) # Tracks whether sand pile at location was involved in an avalanche
self.avalancheSize = []
if rand: # Intializes a random sand pile
for i in range(len(self.sandArray)):
for j in range (len(self.sandArray)):
self.sandArray[i,j] = random.randint(0,3)
else: # Inializes a pile with each cell at the threshold value
for i in range(len(self.sandArray)):
for j in range(len(self.sandArray)):
self.sandArray[i,j] = 0
def addRandSand(self):
i = random.randint(0, self.L -1) # Selects a random site for the sand to be added to
j = random.randint(0, self.L - 1)
self.addSand(i, j)
def addSand(self, i, j):
critical = False # Tracks whether cell that had sand added to it was critical
self.avalancheArray = np.zeros((self.L, self.L)) # Clears the avalanche array
if self.sandArray[i,j] == 3: # If the cell is at the threshold value, it is critical
critical = True
self.sandArray[i,j] += 1 # Adds the sand to the cell
self.relax() # Relax the array
return critical # Returns whether or not the cell was critical
def relax(self):
counter = 1
while counter > 0:
counter = 0
for i in range(len(self.sandArray)):
for j in range(len(self.sandArray)):
if self.sandArray[i,j] > 3:
self.avalancheArray[i,j] =1 # We use >= 4 instead of == 4 in case the dynamics of the system
if i - 1 >= 0: # cause a build-up of more than a 4 grains before the system
self.sandArray[i-1,j] += 1 # can relax
if i + 1 <= self.L - 1:
self.sandArray[i+1,j] += 1 # Each time the while loop is run, the nearest neighbors of the
if j - 1 >= 0: # they're marked with the avalanche Array as having been involved
self.sandArray[i,j-1] += 1 # in the avalanche
if j + 1 <= self.L - 1:
self.sandArray[i,j+1] += 1
counter += 1 # Counter determines whether any activity occured
self.sandArray[i,j] -= 4 # while passing through the array to determine whether
# or not to continue relaxation
size = 0
for i in range(len(self.avalancheArray)):
for j in range(len(self.avalancheArray)):
if self.avalancheArray[i,j] > 0:
size += 1
self.avalancheSize.append(size)
def draw(self, canvas, size):
for i in range(len(self.sandArray)):
for j in range(len(self.sandArray)):
if self.sandArray[j,i] == 0:
canvas.create_rectangle(size*i, size*j, size*(i + 1), \
size*(j+1), fill="white", outline="white")
elif self.sandArray[j,i] == 1:
canvas.create_rectangle(size*i, size*j, size*(i+1), \
size*(j+1), fill="gray", outline="gray")
elif self.sandArray[j,i] == 2:
canvas.create_rectangle(size*i, size*j, size*(i+1), \
size*(j+1), fill="light gray", outline="light gray")
elif self.sandArray[j,i] > 4:
canvas.create_rectangle(size*i, size*j, size*(i+1), \
size*(j+1), fill="green", outline="green")
elif self.sandArray[j,i] == 4:
canvas.create_rectangle(size*i, size*j, size*(i+1), \
size*(j+1), fill="red", outline="red")
else:
canvas.create_rectangle(size*i, size*j, size*(i+1), \
size*(j+1), fill="black", outline="black")
def drawavalanche(self, canvas, size):
for i in range(len(self.avalancheArray)):
for j in range(len(self.avalancheArray)):
if self.avalancheArray[j,i] == 1:
canvas.create_rectangle(size*i, size*j, size*(i + 1), \
size*(j+1), fill="blue", outline="blue")
def animate(self, pausetime, totalSand, canvas, size):
sand = 1
while sand <= totalSand:
self.addSand()
canvas.delete('all')
self.draw(canvas,10)
self.drawavalanche(canvas,10)
canvas.update()
sand += 1
print(sand)
def simulate(self, totalSand, size):
sand = 1
while sand <= totalSand:
self.addRandSand()
sand += 1
if sand % 1000 == 0:
print(sand)
def reset(self):
self.avalanchSize = []
def plot(self):
bins = np.logspace(0,3, 10)
print (bins)
numberArray = np.zeros(len(bins))
for s in self.avalancheSize:
for j in range(len(bins)-1):
if bins[j] <= s < bins[j+1]:
numberArray[j] += 1
pylab.loglog(bins, numberArray)
#print(numberArray)
s = 15
sand = sandPile(s, True)
width = sand.L*s
height = sand.L*s
tk = Tk()
tk.wm_geometry(str(width)+'x'+str(height))
canvas = Canvas(tk, width=width, height=height)
canvas.pack()
print(sand.sandArray)
"""
for i in range(50):
print("STEP: {0}".format(i))
sand.addSand(2, 2)
print(sand.sandArray)
"""
sand.simulate(10000, 10)
sand.plot()
pylab.show()
"""
#sand.animate(0, 3000, canvas, 10)
sand.simulate(1000, 10)
sand.reset()
sand.simulate(5000, 10)
sand.plot()
pylab.show()
#mainloop()
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.