-
Notifications
You must be signed in to change notification settings - Fork 0
/
lattice-serial.py
79 lines (69 loc) · 2.12 KB
/
lattice-serial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import numpy as np
import random as r
import matplotlib.pyplot as plt
import sys
import math
# simlulation parameters
SIZE_X = 128
SIZE_Y = 128
SUBGRIDSIZE = 4
MAX_P = float(SUBGRIDSIZE*SUBGRIDSIZE*4)
DENSITY = 0.50
lattice = np.zeros((SIZE_Y, SIZE_X), dtype=np.byte)
buffer_lattice = np.zeros((SIZE_Y, SIZE_X), dtype=np.byte)
def init_lattice():
n = 0
for y in range(SIZE_Y):
for x in range(SIZE_X):
for d in range(3): # Looping over direction states.
if r.random() < DENSITY:
n += 1
lattice[y][x] += pow( 2, d )
add_square()
print("Created "+str(n)+" particles out of "+str(4*SIZE_X*SIZE_Y))
print("Expecting around "+str(4*SIZE_X*SIZE_Y*DENSITY))
def add_square():
SSIZE = 0.15
l = int(0.5*(SIZE_X - SSIZE*SIZE_X))
for x in range( l, l+int(SSIZE*SIZE_X) ):
for y in range( l, l+int(SSIZE*SIZE_X) ):
lattice[y][x] = 15
buffer_lattice[y][x] = 15
def nthBit( n, i):
mask = [1, 2, 4, 8]
return ( n & mask[i] ) != 0
def setBufferBit(y, x, n, v):
y %= SIZE_Y
x %= SIZE_X
num = buffer_lattice[y][x]
buffer_lattice[y][x] ^= (-v ^ num) & (1 << n)
def propagate():
for y in range(SIZE_Y):
for x in range(SIZE_X):
n = lattice[y][x]
if nthBit(n, 0): #Up
setBufferBit( y-1, x, 0, 1 )
if nthBit(n, 1): #Right
setBufferBit( y, x+1, 1, 1 )
if nthBit(n, 2): #Down
setBufferBit( y+1, x, 2, 1 )
if nthBit(n, 3): #Left
setBufferBit( y, x-1, 3, 1 )
lattice[y][x] = 0
def resolveCollisions():
for y in range(SIZE_Y):
for x in range(SIZE_X):
n = lattice[y][x]
if n == 10:
lattice[y][x] = 5
elif n == 5:
lattice[y][x] = 10
def update():
global lattice, buffer_lattice
propagate()
buffer_lattice, lattice = lattice, buffer_lattice
resolveCollisions()
init_lattice()
for i in range(1,500):
plt.imsave('temp/'+str(i)+'.jpg',lattice)
update()