#! /usr/bin/env python from numpy import * import argparse from matplotlib import pyplot as plt from matplotlib import animation def setup_box(args): """ HPP Lattice Grid Cellular Automata """ x = args.x y = args.y #setup parameters for a box in the middle of the grid midx = int(x/2) midy = int(y/2) boxx = int(midx/4) boxy = int(midy/4) #setup a grid array by specified dimensions and seed with random values n = random.rand(x,y) n_int = zeros([x,y]) #find middle of array and set #step throuugh array and set those random values to 0,1 for x, b in enumerate(n): for y,c in enumerate(n[x]): #new way random number, convert to integer n_int[x][y] = int(n[x][y]*15) #old way #if n[x][y] > 0.5: n[x][y] = 0b1111 #else: n[x][y] = 0 #fill a central box if ((x >= midx-boxx) and (x <= midx+boxx)) and \ ((y >= midy-boxy) and (y <= midy+boxy)): n_int[x][y] = 0b1111 return n_int def test(args): # initialize firs state n = setup_box(args) n = n.astype(int) n_temp = zeros_like(n) print n timesteps = 20 n_stepped = [] n_stepped.append(n) for x,b in enumerate(n): for y,c in enumerate(n[x]): #check collissions if (n[x][y]) == 0b0101: n[x][y] << 1 elif (n[x][y]) == 0b1010: n[x][y] >> 1 else: n[x][y] = n[x][y] def propogate(n): #now propogate velocities for x,b in enumerate(n): for y,c in enumerate(n[x]): #now step through, mask and check values n_temp[x][y] = n[x][y-1] ^ n[x][y] + n[x-1][y] ^ n[x][y] print n_temp return n_temp for x in range(timesteps): #test for animation, just append time steps to a list n_stepped.append(propogate(n)) fig = plt.figure() def animate(i): plotimage = plt.imshow(n_stepped[i], interpolation="none") anim = animation.FuncAnimation(fig, animate, frames=len(n_stepped), interval=2, blit=False) #anim.save('test.gif', writer='imagemagick', fps=5) anim.save('test.mp4', writer='ffmpeg', fps=5) plt.show() if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-x", "--x", type=int, default=10, help="x-grid size") parser.add_argument("-y", "--y", type=int, default=10, help="y-grid size") parser.add_argument("-b", "--b", choices="ip", help="i - infinite edges, p - periodic edges") args = parser.parse_args() test(args)