-
Notifications
You must be signed in to change notification settings - Fork 20
/
varying_f_varying_diff_hi_res.py
185 lines (135 loc) · 4.72 KB
/
varying_f_varying_diff_hi_res.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# import necessary libraries
import numpy as np
import matplotlib.pyplot as pl
# for the animation
import matplotlib.animation as animation
from matplotlib.colors import Normalize
from scipy.sparse import spdiags
import matplotlib as mpl
def get_laplacian(N):
"""Construct a sparse matrix that applies the 5-point discretization"""
N = N
e = np.ones(N**2)
e2 = ([1]*(N-1)+[0])*N
e3 = ([0]+[1]*(N-1))*N
L = spdiags([-4*e,e2,e3,e,e],[0,-1,1,-N,N],N**2,N**2)
return L
# ============ define relevant functions =============
# an efficient function to compute a mean over neighboring cells
def apply_laplacian(mat):
"""This function applies a discretized Laplacian
in periodic boundary conditions to a matrix
For more information see
https://en.wikipedia.org/wiki/Discrete_Laplace_operator#Implementation_via_operator_discretization
"""
# the cell appears 4 times in the formula to compute
# the total difference
neigh_mat = -4*mat.copy()
# Each direct neighbor on the lattice is counted in
# the discrete difference formula
neighbors = [
( 1.0, (-1, 0) ),
( 1.0, ( 0,-1) ),
( 1.0, ( 0, 1) ),
( 1.0, ( 1, 0) ),
]
# shift matrix according to demanded neighbors
# and add to this cell with corresponding weight
for weight, neigh in neighbors:
neigh_mat += weight * np.roll(mat, neigh, (0,1))
return neigh_mat
# Define the update formula for chemicals A and B
def update(A, B, DA, DB, f, k, delta_t, L=None):
"""Apply the Gray-Scott update formula"""
# compute the diffusion part of the update
if L is None:
diff_A = DA * apply_laplacian(A)
diff_B = DB * apply_laplacian(B)
else:
diff_A = DA * L.dot(A)
diff_B = DB * L.dot(B)
# Apply chemical reaction
reaction = A*B**2
diff_A -= reaction
diff_B += reaction
# Apply birth/death
diff_A += f * (1-A)
diff_B -= (k+f) * B
A += diff_A * delta_t
B += diff_B * delta_t
return A, B
def get_initial_A_and_B(N, random_influence = 0.2):
"""get the initial chemical concentrations"""
# get initial homogeneous concentrations
A = (1-random_influence) * np.ones((N,N))
B = np.zeros((N,N))
# put some noise on there
A += random_influence * np.random.random((N,N))
B += random_influence * np.random.random((N,N))
# get center and radius for initial disturbance
N2, r = N//2, 50
# apply initial disturbance
A[N2-r:N2+r, N2-r:N2+r] = 0.50
B[N2-r:N2+r, N2-r:N2+r] = 0.25
return A.flatten(), B.flatten()
def get_initial_artists(A, B):
"""return the matplotlib artists for animation"""
dpi = mpl.rcParams['figure.dpi']
figsize = N / float(dpi), N / float(dpi)
fig = pl.figure(figsize=figsize)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
imA = ax.imshow(A.reshape(N,N), animated=True,vmin=0,vmax=1,cmap='Greys')
return fig, imA
def updatefig(frame_id,updates_per_frame,*args):
"""Takes care of the matplotlib-artist update in the animation"""
# update x times before updating the frame
for u in range(updates_per_frame):
A, B = update(*args)
# update the frame
imA.set_array(A.reshape(N,N))
# renormalize the colors
#imA.set_norm(Normalize(vmin=np.amin(A),vmax=np.amax(A)))
#imB.set_norm(Normalize(vmin=np.amin(B),vmax=np.amax(B)))
# return the updated matplotlib objects
return imA,
# =========== define model parameters ==========
# grid size
N = 1000
L = get_laplacian(N)
# update in time
delta_t = 1.1
# Diffusion coefficients
DA = 0.16*1.3
DB = 0.08*1.3
D = (np.ones((N,N)) * np.linspace(0.4,1.3,N)[:,None])
DA = 0.16*D
DB = 0.08*D
DA = DA.flatten()
DB = DB.flatten()
# define birth/death rates
f = (np.ones((N,N)) * np.linspace(0.016,0.040,N)[None,:]).flatten()
f = np.ones((N,N))
for i in range(N):
for j in range(N):
r = np.sqrt((i-N/2)**2+(j-N/2)**2) / (N/2)
f[i,j] = r * (0.045-0.017)+0.016
f = f.flatten()
#f = 0.060
k = 0.062
# intialize the figures
A, B = get_initial_A_and_B(N)
# how many updates should be computed before a new frame is drawn
updates_per_frame = 30
# these are the arguments which have to passed to the update function
animation_arguments = (updates_per_frame, A, B, DA, DB, f, k, delta_t, L)
from progressbar import ProgressBar as PB
bar = PB()
for step in bar(range(30000)):
update(*animation_arguments[1:])
if step in [ 400, 1000, 4000, 8000, 15000, 29999 ]:
fig, imA = get_initial_artists(A, B)
fig.savefig('img/n_1000_hires_{:d}.png'.format(step))
# show the animation
#ani.save('img/gray_scott_varying_feed_rate.mp4', writer=writer)
# show the animation