-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffusion.py
More file actions
196 lines (142 loc) · 5.74 KB
/
diffusion.py
File metadata and controls
196 lines (142 loc) · 5.74 KB
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
186
187
188
189
190
191
192
193
194
195
196
import numpy as np
import matplotlib.pyplot as plt
import sys
from scipy.optimize import curve_fit
def func(x, a, c, d):
return a*np.exp(-c*x)+d
def power_law(x, a, b):
return a * np.power(x, b)
def update_grid(phi_grid: np.ndarray, sigma, k, D, dx, dt, grid_size) -> np.ndarray:
"""Perform a single sweep, updating phi at each point."""
xcoords, ycoords = np.indices([grid_size, grid_size])
centre = [phi_grid.shape[0]/2, phi_grid.shape[1]/2]
r = np.sqrt((xcoords - centre[0])**2 + (ycoords - centre[1])**2)
rho = np.exp(-1 * r**2 / sigma**2)
phi_grid += dt * (D * ((
np.roll(phi_grid, 1, axis=1) +
np.roll(phi_grid, -1, axis=1) +
np.roll(phi_grid, 1, axis=0) +
np.roll(phi_grid, -1, axis=0) -
4 * phi_grid) / dx**2) +
rho -
k * phi_grid)
return phi_grid
def update_grid_v(phi_grid, sigma, k, D, dx, dt, grid_size, v0):
"""Perform a single sweep, updating phi at each point."""
# add rho.
xcoords, ycoords = np.indices([grid_size, grid_size])
centre = [grid_size/2, grid_size/2]
r = np.sqrt((xcoords - centre[0])**2 + (ycoords - centre[1])**2)
rho = np.exp(-1 * r**2 / sigma**2)
# add drift velocity.
vx = -v0 * np.sin(2 * np.pi * ycoords[0] / grid_size)
vx = np.tile(vx, [grid_size, 1])
d_phi_dx = (
np.roll(phi_grid, 1, axis=0) -
np.roll(phi_grid, -1, axis=0)) / (2 * dx)
# perform the grid update.
phi_grid += dt * (D * ((
np.roll(phi_grid, 1, axis=1) +
np.roll(phi_grid, -1, axis=1) +
np.roll(phi_grid, 1, axis=0) +
np.roll(phi_grid, -1, axis=0) -
(4 * phi_grid)) / dx**2) +
rho -
(k * phi_grid) -
(d_phi_dx * vx))
return phi_grid
def animation(phi_grid, sigma, k, D, dx, dt, grid_size):
fig, ax = plt.subplots()
im = ax.imshow(phi_grid, animated=True)
cbar = fig.colorbar(im, ax=ax)
# choose a large range so that it will likely converge before then, but will never
# continue forever.
for i in range(1000000):
# move one step forward in the simulation, updating phi at every point.
phi_grid = update_grid(phi_grid, sigma, k, D, dx, dt, grid_size)
# every 50 sweeps update the animation.
if i % 50 == 0:
plt.cla()
cbar.remove()
im = ax.imshow(phi_grid, interpolation='bilinear', animated=True)
cbar = fig.colorbar(im, ax=ax)
plt.draw()
plt.pause(0.00001)
def task3(phi_grid, sigma, k, D, dx, dt, grid_size):
"""Average value over time"""
aver = []
t = []
# choose a large range so that it will likely converge before then, but will never
# continue forever.
for i in range(10000):
# move one step forward in the simulation, updating phi at every point.
phi_grid = update_grid(phi_grid, sigma, k, D, dx, dt, grid_size)
if i % 50 == 0:
aver.append(np.average(phi_grid))
t.append(i)
plt.plot(t, aver)
plt.show()
def task4(phi_grid, sigma, k, D, dx, dt, grid_size):
"""Radial distribution"""
# choose a large range so that it will likely converge before then, but will never
# continue forever.
for i in range(10000):
# move one step forward in the simulation, updating phi at every point.
phi_grid = update_grid(phi_grid, sigma, k, D, dx, dt, grid_size)
if i % 50 == 0 and i > 2000:
xcoords, ycoords = np.indices([grid_size, grid_size])
centre = [phi_grid.shape[0]/2, phi_grid.shape[1]/2]
r = np.sqrt((xcoords - centre[0])**2 + (ycoords - centre[1])**2)
x = r.flatten()
popt, pcov = curve_fit(func, x, phi_grid.flatten())
plt.plot(x, func(x, *popt), label="exponential")
popt, pcov = curve_fit(power_law, x, phi_grid.flatten(), p0=[40,-0.5])
plt.plot(x, power_law(x, *popt), label="power law")
plt.plot(x, phi_grid.flatten(), label="data")
plt.legend()
plt.ylabel("$\phi$")
plt.xlabel("Radial distance from centre")
plt.show()
break
def task5(phi_grid, sigma, k, D, dx, dt, grid_size, v0):
fig, ax = plt.subplots()
im = ax.imshow(phi_grid, animated=True)
cbar = fig.colorbar(im, ax=ax)
# choose a large range so that it will likely converge before then, but will never
# continue forever.
for i in range(1000000):
# move one step forward in the simulation, updating phi at every point.
phi_grid = update_grid_v(phi_grid, sigma, k, D, dx, dt, grid_size, v0)
# every 50 sweeps update the animation.
if i % 50 == 0:
plt.cla()
cbar.remove()
im = ax.imshow(phi_grid, interpolation='bilinear', animated=True)
cbar = fig.colorbar(im, ax=ax)
plt.draw()
plt.pause(0.00001)
def main():
"""Evaluate command line args to choose a function.
"""
mode = sys.argv[1]
grid_size = 50
dx = 1
# choose the simulation parameters.
sigma = 10
k = 0.01
D = 1
phi0 = 0.5
# create custom phi_grid with base value of phi0 with noise between 0.1 and -0.1.
phi_grid = float(phi0) + (np.random.rand(grid_size, grid_size) * 0.2) - 0.1
dt = float(sys.argv[2])
if mode == "vis":
animation(phi_grid, sigma, k, D, dx, dt, grid_size)
elif mode == "3":
task3(phi_grid, sigma, k, D, dx, dt, grid_size)
elif mode == "4":
task4(phi_grid, sigma, k, D, dx, dt, grid_size)
elif mode == "5":
v0 = float(sys.argv[3])
task5(phi_grid, sigma, k, D, dx, dt, grid_size, v0)
if __name__=="__main__":
main()