-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_barotropicqg.jl
345 lines (262 loc) · 10.2 KB
/
test_barotropicqg.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""
test_bqg_rossbywave(stepper, dt, nsteps, dev; kwargs...)
Evolves a Rossby wave and compares with the analytic solution.
"""
function test_bqg_rossbywave(stepper, dt, nsteps, dev::Device=CPU())
nx = 64
Lx = 2π
β = 2.0
μ = 0.0
ν = 0.0
T = Float64
# the following if statement is called so that all the cases of
# Problem() fuction are tested
if stepper=="ForwardEuler"
eta = zeros(dev, T, (nx, nx))
else
eta(x, y) = 0*x
end
prob = BarotropicQG.Problem(dev; nx=nx, Lx=Lx, eta=eta, β=β, μ=μ, ν=ν, stepper=stepper, dt=dt)
sol, cl, v, p, g = prob.sol, prob.clock, prob.vars, prob.params, prob.grid
x, y = gridpoints(g)
# the Rossby wave initial condition
ampl = 1e-2
kwave = 3 * 2π/g.Lx
lwave = 2 * 2π/g.Ly
ω = -p.β*kwave/(kwave^2 + lwave^2)
ζ0 = @. ampl*cos(kwave*x)*cos(lwave*y)
ζ0h = rfft(ζ0)
BarotropicQG.set_zeta!(prob, ζ0)
stepforward!(prob, nsteps)
dealias!(sol, g)
BarotropicQG.updatevars!(prob)
ζ_theory = @. ampl*cos(kwave*(x - ω/kwave*cl.t)) * cos(lwave*y)
return isapprox(ζ_theory, v.zeta, rtol=g.nx*g.ny*nsteps*1e-12)
end
"""
test_bqg_stochasticforcingbudgets(dev; kwargs...)
Tests if the energy budget is closed for BarotropicQG problem with stochastic forcing.
"""
function test_bqg_stochasticforcingbudgets(dev::Device=CPU(); n=256, dt=0.01, L=2π, ν=1e-7, nν=2, μ=1e-1, T=Float64)
n, L = 256, 2π
ν, nν = 1e-7, 2
μ = 1e-1
dt, tf = 0.005, 0.1/μ
nt = round(Int, tf/dt)
gr = TwoDGrid(dev, n, L)
x, y = gridpoints(gr)
# Forcing
kf, dkf = 12.0, 2.0
ε = 0.1
CUDA.@allowscalar Kr = ArrayType(dev)([ gr.kr[i] for i=1:gr.nkr, j=1:gr.nl ])
forcingcovariancespectrum = zeros(dev, T, (gr.nkr, gr.nl))
@. forcingcovariancespectrum = exp( -(sqrt(gr.Krsq) - kf)^2 / (2 * dkf^2) )
CUDA.@allowscalar @. forcingcovariancespectrum[gr.Krsq .< 2^2] = 0
CUDA.@allowscalar @. forcingcovariancespectrum[gr.Krsq .> 20^2] = 0
CUDA.@allowscalar @. forcingcovariancespectrum[Kr .< 2π/L] = 0
ε0 = parsevalsum(forcingcovariancespectrum .* gr.invKrsq / 2, gr) / (gr.Lx * gr.Ly)
forcingcovariancespectrum .= ε / ε0 * forcingcovariancespectrum
Random.seed!(1234)
function calcFq!(Fqh, sol, t, cl, v, p, g)
eta = ArrayType(dev)(exp.(2π*im*rand(T, size(sol)))/sqrt(cl.dt))
CUDA.@allowscalar eta[1, 1] = 0
@. Fqh = eta * sqrt(forcingcovariancespectrum)
return nothing
end
prob = BarotropicQG.Problem(dev; nx=n, Lx=L, ν=ν, nν=nν, μ=μ, dt=dt,
stepper="RK4", calcFq=calcFq!, stochastic=true)
sol, cl, v, p, g = prob.sol, prob.clock, prob.vars, prob.params, prob.grid
BarotropicQG.set_zeta!(prob, 0*x)
E = Diagnostic(BarotropicQG.energy, prob, nsteps=nt)
D = Diagnostic(BarotropicQG.dissipation, prob, nsteps=nt)
R = Diagnostic(BarotropicQG.drag, prob, nsteps=nt)
W = Diagnostic(BarotropicQG.work, prob, nsteps=nt)
diags = [E, D, W, R]
# Step forward
stepforward!(prob, diags, nt)
BarotropicQG.updatevars!(prob)
E, D, W, R = diags
t = round(μ*cl.t, digits=2)
i₀ = 1
dEdt = (E[(i₀+1):E.i] - E[i₀:E.i-1])/cl.dt
ii = (i₀):E.i-1
ii2 = (i₀+1):E.i
# dEdt = W - D - R?
# If the Ito interpretation was used for the work
# then we need to add the drift term
# total = W[ii2]+ε - D[ii] - R[ii] # Ito
total = W[ii2] - D[ii] - R[ii] # Stratonovich
residual = dEdt - total
isapprox(mean(abs.(residual)), 0, atol=1e-4)
end
"""
test_bqg_deterministicforcingbudgets(dev; kwargs...)
Tests if the energy budget is closed for BarotropicQG problem with deterministic forcing.
"""
function test_bqg_deterministicforcingbudgets(dev::Device=CPU(); n=256, dt=0.01, L=2π, ν=1e-7, nν=2, μ=1e-1)
n, L = 256, 2π
ν, nν = 1e-7, 2
μ = 1e-1
dt, tf = 0.005, 0.1/μ
nt = round(Int, tf/dt)
gr = TwoDGrid(dev, n, L)
x, y = gridpoints(gr)
k₀, l₀ = 2π/gr.Lx, 2π/gr.Ly
# Forcing = 0.01cos(4x)cos(5y)cos(2t)
f = @. 0.01 * cos(4k₀*x) * cos(5l₀*y)
fh = rfft(f)
function calcFq!(Fqh, sol, t, cl, v, p, g)
cos2t = cos(2*t)
@. Fqh = fh*cos2t
return nothing
end
prob = BarotropicQG.Problem(dev; nx=n, Lx=L, ν=ν, nν=nν, μ=μ, dt=dt,
stepper="RK4", calcFq=calcFq!, stochastic=false)
sol, cl, v, p, g = prob.sol, prob.clock, prob.vars, prob.params, prob.grid
BarotropicQG.set_zeta!(prob, 0*x)
E = Diagnostic(BarotropicQG.energy, prob, nsteps=nt)
D = Diagnostic(BarotropicQG.dissipation, prob, nsteps=nt)
R = Diagnostic(BarotropicQG.drag, prob, nsteps=nt)
W = Diagnostic(BarotropicQG.work, prob, nsteps=nt)
diags = [E, D, W, R]
# Step forward
stepforward!(prob, diags, round(Int, nt))
BarotropicQG.updatevars!(prob)
E, D, W, R = diags
t = round(μ*cl.t, digits=2)
i₀ = 1
dEdt = (E[(i₀+1):E.i] - E[i₀:E.i-1])/cl.dt
ii = (i₀):E.i-1
ii2 = (i₀+1):E.i
# dEdt = W - D - R?
total = W[ii2] - D[ii] - R[ii]
residual = dEdt - total
return isapprox(mean(abs.(residual)), 0, atol=1e-8)
end
"""
test_bqg_nonlinearadvection(dt, stepper, dev; kwargs...)
Tests the advection term in the BarotropicQG module by timestepping a
test problem with timestep dt and timestepper identified by the string stepper.
The test problem is derived by picking a solution ζf (with associated
streamfunction ψf) for which the advection term J(ψf, ζf) is non-zero. Next, a
forcing Ff is derived according to Ff = ∂ζf/∂t + J(ψf, ζf) - ν∇²ζf. One solution
to the vorticity equation forced by this Ff is then ζf. (This solution may not
be realized, at least at long times, if it is unstable.)
"""
function test_bqg_advection(dt, stepper, dev::Device=CPU(); n=128, L=2π, ν=1e-2, nν=1, μ=0.0)
n, L = 128, 2π
ν, nν = 1e-2, 1
μ = 0.0
tf = 1.0
nt = round(Int, tf/dt)
gr = TwoDGrid(dev, n, L)
x, y = gridpoints(gr)
psif = @. sin(2x) * cos(2y) + 2sin(x) * cos(3y)
qf = @. -8sin(2x) * cos(2y) - 20sin(x) * cos(3y)
Ff = @. -(
ν*( 64sin(2x) * cos(2y) + 200sin(x) * cos(3y) )
+ 8 * ( cos(x) * cos(3y) * sin(2x) * sin(2y) - 3cos(2x) * cos(2y) * sin(x) * sin(3y) )
)
Ffh = rfft(Ff)
# Forcing
function calcFq!(Fqh, sol, t, cl, v, p, g)
Fqh .= Ffh
return nothing
end
prob = BarotropicQG.Problem(dev; nx=n, Lx=L, ν=ν, nν=nν, μ=μ, dt=dt, stepper=stepper, calcFq=calcFq!)
sol, cl, v, p, g = prob.sol, prob.clock, prob.vars, prob.params, prob.grid
BarotropicQG.set_zeta!(prob, qf)
# Step forward
stepforward!(prob, round(Int, nt))
BarotropicQG.updatevars!(prob)
return isapprox(v.q, qf, rtol=rtol_barotropicQG)
end
"""
test_bqg_formstress(dt, stepper, dev; kwargs...)
Tests the form stress term that forces the domain-averaged zonal flow U(t).
"""
function test_bqg_formstress(dt, stepper, dev::Device=CPU(); n=128, L=2π, ν=0.0, nν=1, μ=0.0)
n, L = 128, 2π
ν, nν = 1e-2, 1
μ = 0.0
tf = 1
nt = 1
gr = TwoDGrid(dev, n, L)
x, y = gridpoints(gr)
ζ₀ = @. -20 * sin(10x) * cos(10y)
topoPV(x, y) = @. cos(10x) * cos(10y)
F(t) = 0 #no forcing
answer = 0.25 # this is what <v*eta> should be
prob = BarotropicQG.Problem(dev; nx=n, Lx=L, ν=ν, nν=nν, μ=μ, dt=dt, stepper=stepper, eta=topoPV, calcFU=F)
BarotropicQG.set_zeta!(prob, ζ₀)
BarotropicQG.updatevars!(prob)
# Step forward
stepforward!(prob, nt)
return CUDA.@allowscalar isapprox(prob.timestepper.N[1, 1], answer, rtol=rtol_barotropicQG)
end
"""
test_bqg_energyenstrophy(dev)
Tests the energy and enstrophy function for a BarotropicQG problem.
"""
function test_bqg_energyenstrophy(dev::Device=CPU())
nx, Lx = 64, 2π
ny, Ly = 64, 3π
g = TwoDGrid(dev, nx, Lx, ny, Ly)
k₀, l₀ = 2π/Lx, 2π/Ly # fundamental wavenumbers
x, y = gridpoints(g)
energy_calc = 29/9
enstrophy_calc = 2701/162
eta = @. cos(10k₀*x) * cos(10l₀*y)
psi0 = @. sin(2k₀*x) * cos(2l₀*y) + 2sin(k₀*x) * cos(3l₀*y)
zeta0 = @. - ((2k₀)^2+(2l₀)^2) * sin(2k₀*x) * cos(2l₀*y) - (k₀^2+(3l₀)^2) * 2sin(k₀*x) * cos(3l₀*y)
prob = BarotropicQG.Problem(dev; nx=nx, Lx=Lx, ny=ny, Ly=Ly, eta = eta, stepper="ForwardEuler")
sol, cl, v, p, g = prob.sol, prob.clock, prob.vars, prob.params, prob.grid
BarotropicQG.set_zeta!(prob, zeta0)
BarotropicQG.updatevars!(prob)
energyzeta0 = BarotropicQG.energy(prob)
enstrophyzeta0 = BarotropicQG.enstrophy(prob)
return isapprox(energyzeta0, energy_calc, rtol=rtol_barotropicQG) && isapprox(enstrophyzeta0, enstrophy_calc, rtol=rtol_barotropicQG) &&
BarotropicQG.addforcing!(prob.timestepper.N, sol, cl.t, cl, v, p, g)==nothing
end
"""
test_bqg_energyenstrophy(dev)
Tests the mean flow U(t) energy and enstrophy function for a BarotropicQG problem.
"""
function test_bqg_meanenergyenstrophy(dev::Device=CPU())
nx, Lx = 64, 2π
ny, Ly = 96, 3π
g = TwoDGrid(dev, nx, Lx, ny, Ly)
k₀, l₀ = 2π/Lx, 2π/Ly # fundamental wavenumbers
x, y = gridpoints(g)
calcFU(t) = 0.0
eta(x, y) = @. cos(10x) * cos(10y)
psi0 = @. sin(2k₀*x) * cos(2l₀*y) + 2sin(k₀*x) * cos(3l₀*y)
zeta0 = @. - ((2k₀)^2+(2l₀)^2) * sin(2k₀*x) * cos(2l₀*y) - (k₀^2+(3l₀)^2) * 2sin(k₀*x) * cos(3l₀*y)
β = 10.0
U = 1.2
energy_calc = 29/9
enstrophy_calc = 2701/162
prob = BarotropicQG.Problem(dev; nx=nx, Lx=Lx, ny=ny, Ly=Ly, β=β, eta=eta, calcFU = calcFU,
stepper="ForwardEuler")
BarotropicQG.set_zeta!(prob, zeta0)
BarotropicQG.set_U!(prob, U)
BarotropicQG.updatevars!(prob)
energyU = BarotropicQG.meanenergy(prob)
enstrophyU = BarotropicQG.meanenstrophy(prob)
energyzeta0 = BarotropicQG.energy(prob)
enstrophyzeta0 = BarotropicQG.enstrophy(prob)
return (isapprox(energyU, 0.5*U^2, rtol=rtol_barotropicQG) &&
isapprox(enstrophyU, β*U, rtol=rtol_barotropicQG) &&
isapprox(energyzeta0, energy_calc, rtol=rtol_barotropicQG) &&
isapprox(enstrophyzeta0, enstrophy_calc, rtol=rtol_barotropicQG)
)
end
"""
test_bqg_problemtype(dev, T)
Tests the BarotropicQG problem constructor for different DataType `T`.
"""
function test_bqg_problemtype(dev, T)
prob = BarotropicQG.Problem(dev; T=T)
A = ArrayType(dev)
return (typeof(prob.sol)<:A{Complex{T}, 2} && typeof(prob.grid.Lx)==T && eltype(prob.grid.x)==T && typeof(prob.vars.u)<:A{T, 2})
end