-
Notifications
You must be signed in to change notification settings - Fork 0
/
P8.py
67 lines (63 loc) · 1.7 KB
/
P8.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
"""
Problem 8
Avalanches
"""
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import itertools as itr
def CSand(Sand, CRITVAL):
SandX = Sand[1:, :] - Sand[:-1, :]
SandY = Sand[:, 1:] - Sand[:, :-1]
MaxTotalX = np.unravel_index(np.argmax(np.abs(SandX)), SandX.shape)
MaxTotalY = np.unravel_index(np.argmax(np.abs(SandY)), SandY.shape)
if abs(SandX[MaxTotalX]) >= abs(SandY[MaxTotalY]):
MaxTotCoord = MaxTotalX
Dir = 0
else:
MaxTotCoord = MaxTotalY
Dir = 1
Value = [SandX, SandY][Dir][MaxTotCoord]
PosNeg = np.sign(Value) == 1
#print MaxTotCoord, Dir, PosNeg, Value
#print Sand
#raw_input()
return abs(Value)>CRITVAL, [abs(Value),list(MaxTotCoord),Dir,PosNeg]
Asize = []
CritStatic = 7
CritDynamic = 6
for i in tqdm(xrange(10000)):
Sand = np.zeros((10,10))
check = True
size = 0
while True:
i, j = np.random.randint(10, size=2)
Sand[i, j] += 1
Av, [V, C, D, P] = CSand(Sand, CritStatic)
while Av:
size += V/2
#print V, C, D, P
C[D] += P
#print C
P = P*2 - 1
#print P
Sand[C[0], C[1]] -= V/2
#print Sand
C[D] -= P
#print C
Sand[C[0], C[1]] += V/2
#print Sand
#raw_input()
Av, [V, C, D, P] = CSand(Sand, CritDynamic)
if not Av:
break
else:
continue
Asize.append(size)
break
plt.hist(Asize)
plt.grid()
plt.xlabel('Avalanche Size')
plt.ylabel('Number of Occurences')
plt.title('Model of Avalanche Occurrence')
plt.savefig('P8.png', format='png', dpi=300)