-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavier_stokes_2d.py
286 lines (231 loc) · 6.84 KB
/
navier_stokes_2d.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import numpy as np
import scipy.sparse.linalg as splinalg
from scipy import interpolate
import matplotlib.pyplot as plt
# Optional
import cmasher as cmr
from tqdm import tqdm
#Taken from
#https://medium.com/@zakariatemouch/exploring-fluid-dynamics-using-python-a-numerical-approach-with-navier-stokes-equations-0a00ddae6822
import matplotlib
matplotlib.use('TkAgg')
DOMAIN_SIZE = 1.0
N_POINTS = 41
N_TIME_STEPS = 100
TIME_STEP_LENGTH = 0.1
KINEMATIC_VISCOSITY = 0.0001
MAX_ITER_CG = None
def forcing_function(time, point):
time_decay = np.maximum(
2.0 - 0.5 * time,
0.0,
)
forced_value = (
time_decay
*
np.where(
(
(point[0] > 0.4)
&
(point[0] < 0.6)
&
(point[1] > 0.1)
&
(point[1] < 0.3)
),
np.array([0.0, 1.0]),
np.array([0.0, 0.0]),
)
)
return forced_value
def main():
element_length = DOMAIN_SIZE / (N_POINTS - 1)
scalar_shape = (N_POINTS, N_POINTS)
scalar_dof = N_POINTS ** 2
vector_shape = (N_POINTS, N_POINTS, 2)
vector_dof = N_POINTS ** 2 * 2
x = np.linspace(0.0, DOMAIN_SIZE, N_POINTS)
y = np.linspace(0.0, DOMAIN_SIZE, N_POINTS)
# Using "ij" indexing makes the differential operators more logical.
X, Y = np.meshgrid(x, y, indexing="ij")
coordinates = np.concatenate(
(
X[..., np.newaxis],
Y[..., np.newaxis],
),
axis=-1,
)
forcing_function_vectorized = np.vectorize(
pyfunc=forcing_function,
signature="(),(d)->(d)",
)
def partial_derivative_x(field):
diff = np.zeros_like(field)
diff[1:-1, 1:-1] = (
(
field[2:, 1:-1]
-
field[0:-2, 1:-1]
) / (
2 * element_length
)
)
return diff
def partial_derivative_y(field):
diff = np.zeros_like(field)
diff[1:-1, 1:-1] = (
(
field[1:-1, 2:]
-
field[1:-1, 0:-2]
) / (
2 * element_length
)
)
return diff
def laplace(field):
diff = np.zeros_like(field)
diff[1:-1, 1:-1] = (
(
field[0:-2, 1:-1]
+
field[1:-1, 0:-2]
- 4 *
field[1:-1, 1:-1]
+
field[2:, 1:-1]
+
field[1:-1, 2:]
) / (
element_length ** 2
)
)
return diff
def divergence(vector_field):
divergence_applied = (
partial_derivative_x(vector_field[..., 0])
+
partial_derivative_y(vector_field[..., 1])
)
return divergence_applied
def gradient(field):
gradient_applied = np.concatenate(
(
partial_derivative_x(field)[..., np.newaxis],
partial_derivative_y(field)[..., np.newaxis],
),
axis=-1,
)
return gradient_applied
def curl_2d(vector_field):
curl_applied = (
partial_derivative_x(vector_field[..., 1])
-
partial_derivative_y(vector_field[..., 0])
)
return curl_applied
def advect(field, vector_field):
backtraced_positions = np.clip(
(
coordinates
-
TIME_STEP_LENGTH
*
vector_field
),
0.0,
DOMAIN_SIZE,
)
advected_field = interpolate.interpn(
points=(x, y),
values=field,
xi=backtraced_positions,
)
return advected_field
def diffusion_operator(vector_field_flattened):
vector_field = vector_field_flattened.reshape(vector_shape)
diffusion_applied = (
vector_field
-
KINEMATIC_VISCOSITY
*
TIME_STEP_LENGTH
*
laplace(vector_field)
)
return diffusion_applied.flatten()
def poisson_operator(field_flattened):
field = field_flattened.reshape(scalar_shape)
poisson_applied = laplace(field)
return poisson_applied.flatten()
plt.style.use("dark_background")
plt.figure(figsize=(5, 5), dpi=160)
velocities_prev = np.zeros(vector_shape)
time_current = 0.0
for i in tqdm(range(N_TIME_STEPS)):
time_current += TIME_STEP_LENGTH
forces = forcing_function_vectorized(
time_current,
coordinates,
)
# (1) Apply Forces
velocities_forces_applied = (
velocities_prev
+
TIME_STEP_LENGTH
*
forces
)
# (2) Nonlinear convection (=self-advection)
velocities_advected = advect(
field=velocities_forces_applied,
vector_field=velocities_forces_applied,
)
# (3) Diffuse
velocities_diffused = splinalg.cg(
A=splinalg.LinearOperator(
shape=(vector_dof, vector_dof),
matvec=diffusion_operator,
),
b=velocities_advected.flatten(),
maxiter=MAX_ITER_CG,
)[0].reshape(vector_shape)
# (4.1) Compute a pressure correction
pressure = splinalg.cg(
A=splinalg.LinearOperator(
shape=(scalar_dof, scalar_dof),
matvec=poisson_operator,
),
b=divergence(velocities_diffused).flatten(),
maxiter=MAX_ITER_CG,
)[0].reshape(scalar_shape)
# (4.2) Correct the velocities to be incompressible
velocities_projected = (
velocities_diffused
-
gradient(pressure)
)
# Advance to next time step
velocities_prev = velocities_projected
# Plot
curl = curl_2d(velocities_projected)
plt.contourf(
X,
Y,
curl,
cmap=cmr.redshift,
levels=100,
)
plt.quiver(
X,
Y,
velocities_projected[..., 0],
velocities_projected[..., 1],
color="dimgray",
)
plt.draw()
plt.pause(2)
plt.clf()
plt.show()
if __name__ == "__main__":
main()