blflow/blflow.py

82 lines
1.5 KiB
Python
Raw Permalink Normal View History

2015-01-28 12:01:23 +00:00
#!/usr/bin/python
# Boundary layer flow
2018-12-16 12:08:34 +00:00
import numpy as np
import matplotlib.pyplot as plt
2015-01-28 12:01:23 +00:00
2018-12-16 12:08:34 +00:00
def K(t):
"""
Forcing function as a function of time.
"""
return (1-np.exp(-0.1*t))*np.cos(t)
2015-01-28 12:01:23 +00:00
2018-12-16 12:08:34 +00:00
s = 10 # Shear wave number
n = 50 # Number of gridpoints in vertical direction
2015-01-28 12:01:23 +00:00
2018-12-16 12:08:34 +00:00
# Grid
y = np.linspace(0, 1, n)
2015-01-28 12:01:23 +00:00
2018-12-16 12:08:34 +00:00
dy = y[1]-y[0]
dt = 0.0005
2015-01-28 12:01:23 +00:00
2018-12-16 12:08:34 +00:00
l = (dt/(s**2*dy**2))
hnu = np.exp(-np.sqrt(1j)*s*y)
2015-01-28 12:01:23 +00:00
# fnu=(1-1j)/s
2018-12-16 12:08:34 +00:00
fnu = 0
2015-01-28 12:01:23 +00:00
def u_ex(tn):
2018-12-16 12:08:34 +00:00
"""
Exact harmonic solution of the velocity field
"""
return (((1-hnu)/(1-fnu))*np.exp(1j*(tn))/1j).real
def u_np1(un, tn, dt):
Kn = K(tn)
unp1 = un
unp1[0] = 0 # Velocity zero ver here
unp1[1:-1] = un[1:-1]+dt*Kn+l*(un[0:-2]-2*un[1:-1]+un[2:])
unp1[-1] = unp1[-2] # Approximate 'infinity' bc
2015-01-28 12:01:23 +00:00
return unp1
2018-12-16 12:08:34 +00:00
un0 = np.zeros(n, float)
t = 0
un = un0
2015-01-28 12:01:23 +00:00
2018-12-16 12:08:34 +00:00
f = plt.figure(1, figsize=(12, 6))
2015-01-28 12:01:23 +00:00
2018-12-16 12:08:34 +00:00
linefd, = plt.plot(un0, y)
linee, = plt.plot(un0, y)
2018-12-16 12:40:05 +00:00
plt.legend(('Finite difference', 'Periodic exact'),
loc='lower right')
2018-12-16 12:08:34 +00:00
plt.ylim(0, 1)
plt.xlim(-1.5, 1.5)
plt.ylabel('Distance from wall [m]')
plt.xlabel('Velocity [m/s]')
plt.grid(True)
plt.show(block=False)
i = 0
uold = un
2015-01-28 12:01:23 +00:00
2018-12-16 12:08:34 +00:00
try:
while(True):
t += dt
uold = un
un = u_np1(uold, t, dt)
2018-12-16 12:40:05 +00:00
if(i % 25 == 0):
2018-12-16 12:08:34 +00:00
linefd.set_xdata(un)
linee.set_xdata(u_ex(t))
f.canvas.draw_idle()
2018-12-16 12:40:05 +00:00
plt.pause(.000005)
if not plt.fignum_exists(1):
break
2018-12-16 12:08:34 +00:00
i += 1
except Exception:
pass
2015-01-28 12:01:23 +00:00
2018-12-16 12:08:34 +00:00
exit(0)