-
Notifications
You must be signed in to change notification settings - Fork 30
/
twodturb_mcwilliams1984.jl
98 lines (78 loc) · 2.62 KB
/
twodturb_mcwilliams1984.jl
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
using FourierFlows, PyPlot, JLD2, Printf, Random, FFTW
using Random: seed!
import GeophysicalFlows.TwoDTurb
import GeophysicalFlows.TwoDTurb: energy, enstrophy
import GeophysicalFlows: peakedisotropicspectrum
dev = CPU() # Device (CPU/GPU)
# Parameters
n = 256
L = 2π
nν = 2
ν = 0.0
dt = 1e-2
nsteps = 5000
nsubs = 200
# Files
filepath = "."
plotpath = "./plots_McWilliams1984"
plotname = "snapshots"
filename = joinpath(filepath, "McWilliams1984.jld2")
# File management
if isfile(filename); rm(filename); end
if !isdir(plotpath); mkdir(plotpath); end
# Initialize problem
prob = TwoDTurb.Problem(; nx=n, Lx=L, ny=n, Ly=L, ν=ν, nν=nν, dt=dt, stepper="FilteredRK4", dev=dev)
sol, cl, vs, gr, filter = prob.sol, prob.clock, prob.vars, prob.grid, prob.timestepper.filter
x, y = gridpoints(gr)
# Initial condition closely following pyqg barotropic example
# that reproduces the results of the paper by McWilliams (1984)
seed!(1234)
k0, E0 = 6, 0.5
zetai = peakedisotropicspectrum(gr, k0, E0, mask=filter)
TwoDTurb.set_zeta!(prob, zetai)
# Create Diagnostic -- energy and enstrophy are functions imported at the top.
E = Diagnostic(energy, prob; nsteps=nsteps)
Z = Diagnostic(enstrophy, prob; nsteps=nsteps)
diags = [E, Z] # A list of Diagnostics types passed to "stepforward!" will
# be updated every timestep.
# Create Output
get_sol(prob) = Array(prob.sol) # extracts the Fourier-transformed solution
get_u(prob) = Array(irfft(im*gr.l.*gr.invKrsq.*sol, gr.nx))
out = Output(prob, filename, (:sol, get_sol), (:u, get_u))
saveproblem(out)
function plot_output(prob, fig, axs; drawcolorbar=false)
# Plot the vorticity field and the evolution of energy and enstrophy.
TwoDTurb.updatevars!(prob)
sca(axs[1])
pcolormesh(x, y, vs.zeta)
clim(-40, 40)
axis("off")
axis("square")
if drawcolorbar==true
colorbar()
end
sca(axs[2])
cla()
plot(E.t[1:E.i], E.data[1:E.i]/E.data[1])
plot(Z.t[1:Z.i], Z.data[1:E.i]/Z.data[1])
xlabel(L"t")
ylabel(L"\Delta E, \, \Delta Z")
pause(0.01)
end
# Step forward
fig, axs = subplots(ncols=2, nrows=1, figsize=(12, 4))
plot_output(prob, fig, axs; drawcolorbar=true)
startwalltime = time()
while cl.step < nsteps
stepforward!(prob, diags, nsubs)
saveoutput(out)
# Message
log = @sprintf("step: %04d, t: %d, ΔE: %.4f, ΔZ: %.4f, τ: %.2f min",
cl.step, cl.t, E.data[E.i]/E.data[1], Z.data[Z.i]/Z.data[1], (time()-startwalltime)/60)
println(log)
plot_output(prob, fig, axs; drawcolorbar=false)
end
println("finished")
plot_output(prob, fig, axs; drawcolorbar=false)
savename = @sprintf("%s_%09d.png", joinpath(plotpath, plotname), cl.step)
savefig(savename, dpi=240)