-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathKSanimate.py
72 lines (63 loc) · 1.75 KB
/
KSanimate.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
import numpy as np
from KS import KS
import matplotlib.pyplot as plt
import matplotlib.animation as animation
L = 16 # domain is 0 to 2.*np.pi*L
N = 128 # number of collocation points
dt = 0.5 # time step
diffusion = 1.0
ks = KS(L=L,diffusion=diffusion,N=N,dt=dt) # instantiate model
# define initial condition
#u = np.cos(x/L)*(1.0+np.sin(x/L)) # smooth IC
u = 0.01*np.random.normal(size=N) # noisy IC
# remove zonal mean
u = u - u.mean()
# spectral space variable.
ks.xspec[0] = np.fft.rfft(u)
# time stepping loop.
nmin = 1000; nmax = 5000
uu = []; tt = []
vspec = np.zeros(ks.xspec.shape[1], np.float)
x = np.arange(N)
fig, ax = plt.subplots()
line, = ax.plot(x, ks.x.squeeze())
ax.set_xlim(0,N-1)
ax.set_ylim(-3,3)
#Init only required for blitting to give a clean slate.
def init():
global line
line.set_ydata(np.ma.array(x, mask=True))
return line,
# spinup
for n in range(nmin):
ks.advance()
def updatefig(n):
global tt,uu,vspec
ks.advance()
vspec += np.abs(ks.xspec.squeeze())**2
u = ks.x.squeeze()
line.set_ydata(u)
print(n,u.min(),u.max())
uu.append(u); tt.append(n*dt)
return line,
ani = animation.FuncAnimation(fig, updatefig, np.arange(1,nmax+1), init_func=init,
interval=25, blit=True, repeat=False)
plt.show()
plt.figure()
# make contour plot of solution, plot spectrum.
ncount = len(uu)
vspec = vspec/ncount
uu = np.array(uu); tt = np.array(tt)
print(tt.min(), tt.max())
nplt = 200
plt.contourf(x,tt[:nplt],uu[:nplt],31,extend='both')
plt.xlabel('x')
plt.ylabel('t')
plt.colorbar()
plt.title('chaotic solution of the K-S equation')
plt.figure()
plt.loglog(ks.wavenums,vspec)
plt.title('variance spectrum')
plt.ylim(0.001,10000)
plt.xlim(0,100)
plt.show()