#HPP Lattice Gas Model #for NoMM by Emily Royall #!/usr/bin/python import matplotlib matplotlib.use('TkAgg') #calls Tk backend; Agg= Anti Grain Geometry, #A C++ library to make a raster. import pylab as PL import random as RD import scipy as SP RD.seed() #seed sets integer starting value for a random lattice #initialize window width = 50 height = 50 initProb = 0.1 #initial probability of changing state maxState = 6 def init(): global time, config, nextConfig #global variables in python are those referenced outside a function #calling global explicitly means using global variables in local function #declare these variables as global because they will be assigned new #values in the def(init) function time = 0 config = SP.zeros([height, width]) #initial config is zero for all cells # [] = list for x in xrange(width): #for all columns in matrix for y in xrange(height): #for the rows values of each x column if RD.random() < initProb: state = maxState #state is = 6 if the random value assigned to the site # is less than the probability of changing state else: state = 0 config[y, x] = state #otherwise, the state of the site is zero #update current state value with config[y,x] nextConfig = SP.zeros([height, width]) #update nextConfig with zeros def draw(): PL.cla() #clears axis PL.pcolor(config, vmin = 0, vmax = maxState, cmap = PL.cm.binary) #.cm = colormap binary = black and white. PL.axis('image') PL.title('t = ' + str(time)) #have title be t = the current time variable as a string def step(): global time, config, nextConfig time += 1 #increment time for x in xrange(width): #for all columns in matrix (defined as 100) for y in xrange(height): #for the row values of each x column state = config[y, x] #set current state for all cells as the y and x parameters of config variable #recall config is set as SP.zeros([h,w]) in init if state == 0: num = 0 for dx in xrange(-1, 2): #for differential in x evaluated over interval -1,2 for dy in xrange(-1, 2): #for differential in y evaluated over interval -1,2 if config[(y+dy)%height, (x+dx)%width] == maxState: #if the current config is numerically equal to maxState #% = substitutes values into height and width variables num += 1 #increment by 1 if RD.random() * 3 < num: state = maxState else: state = 0 else: state -= 1 # calculates "1 - state" and assigns the result to "state" nextConfig[y, x] = state #update state with nextConfig[y, x] values config, nextConfig = nextConfig, config #current config becomes next config, and next config becomes current import pycxsimulator pycxsimulator.GUI().start(func=[init,draw,step])