-
Notifications
You must be signed in to change notification settings - Fork 33
/
block2d_rotated_ca_demo.py
60 lines (53 loc) · 2.01 KB
/
block2d_rotated_ca_demo.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
import cellpylib as cpl
import numpy as np
"""
2D Block CA from
https://writings.stephenwolfram.com/2023/02/computational-foundations-for-the-second-law-of-thermodynamics/
"""
initial_conditions = np.loadtxt('block2d_rotated_initial_conditions.txt', dtype=int)
initial_conditions = np.array([initial_conditions])
def make_block2d_rule():
base_rules = {
((0, 0), (0, 0)): ((0, 0), (0, 0)),
((0, 0), (0, 2)): ((2, 0), (0, 0)),
((2, 0), (0, 0)): ((0, 0), (0, 2)),
((0, 0), (2, 0)): ((0, 2), (0, 0)),
((0, 2), (0, 0)): ((0, 0), (2, 0)),
((0, 0), (2, 2)): ((2, 2), (0, 0)),
((2, 2), (0, 0)): ((0, 0), (2, 2)),
((0, 2), (0, 2)): ((2, 0), (2, 0)),
((2, 0), (2, 0)): ((0, 2), (0, 2)),
((0, 2), (2, 0)): ((2, 0), (0, 2)),
((2, 0), (0, 2)): ((0, 2), (2, 0)),
((0, 2), (2, 2)): ((2, 2), (2, 0)),
((2, 2), (2, 0)): ((0, 2), (2, 2)),
((2, 0), (2, 2)): ((2, 2), (0, 2)),
((2, 2), (0, 2)): ((2, 0), (2, 2)),
((2, 2), (2, 2)): ((2, 2), (2, 2)),
# wall rules
((0, 0), (1, 1)): ((0, 0), (1, 1)),
((0, 1), (1, 1)): ((0, 1), (1, 1)),
((0, 2), (1, 1)): ((2, 0), (1, 1)),
((2, 0), (1, 1)): ((0, 2), (1, 1)),
((2, 1), (1, 1)): ((2, 1), (1, 1)),
((2, 2), (1, 1)): ((2, 2), (1, 1)),
((1, 1), (1, 1)): ((1, 1), (1, 1)),
((1, 0), (0, 0)): ((1, 0), (0, 0)),
((1, 0), (0, 2)): ((1, 0), (0, 2)),
}
rules = {}
# add rotations
for r, v in base_rules.items():
rules[r] = v
for _ in range(3):
r = ((r[1][0], r[0][0]), (r[1][1], r[0][1]))
v = ((v[1][0], v[0][0]), (v[1][1], v[0][1]))
if r not in rules:
rules[r] = v
def _apply_rule(n, t):
n = tuple(tuple(i) for i in n)
return rules[n]
return _apply_rule
ca = cpl.evolve2d_block(initial_conditions, block_size=(2, 2),
timesteps=251, apply_rule=make_block2d_rule())
cpl.plot2d_animate(ca)