-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmultilayerqg.jl
667 lines (511 loc) · 21.6 KB
/
multilayerqg.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
module MultilayerQG
export
fwdtransform!,
invtransform!,
streamfunctionfrompv!,
pvfromstreamfunction!,
updatevars!,
set_q!,
set_ψ!,
energies,
fluxes
using
FFTW,
CUDA,
LinearAlgebra,
StaticArrays,
Reexport
@reexport using FourierFlows
using LinearAlgebra: mul!, ldiv!
using FFTW: rfft, irfft
using FourierFlows: parsevalsum, parsevalsum2, superzeros, plan_flows_rfft
nothingfunction(args...) = nothing
"""
Problem(; parameters...)
Construct a multi-layer QG problem.
"""
function Problem(nlayers::Int, # number of fluid layers
dev = CPU();
# Numerical parameters
nx = 128,
Lx = 2π,
ny = nx,
Ly = Lx,
dt = 0.01,
# Physical parameters
f₀ = 1.0, # Coriolis parameter
β = 0.0, # y-gradient of Coriolis parameter
g = 1.0, # gravitational constant
U = zeros(nlayers), # imposed zonal flow U(y) in each layer
H = 1/nlayers * ones(nlayers), # rest fluid height of each layer
ρ = Array{Float64}(1:nlayers), # density of each layer
eta = nothing, # topographic PV
# Bottom Drag and/or (hyper)-viscosity
μ = 0,
ν = 0,
nν = 1,
# Timestepper and equation options
stepper = "RK4",
calcFq = nothingfunction,
stochastic = false,
linear = false,
T = Float64)
# topographic PV
eta === nothing && (eta = zeros(dev, T, (nx, ny)))
grid = TwoDGrid(dev, nx, Lx, ny, Ly; T=T)
params = Params(nlayers, g, f₀, β, ρ, H, U, eta, μ, ν, nν, grid, calcFq=calcFq, dev=dev)
vars = calcFq == nothingfunction ? Vars(dev, grid, params) : (stochastic ? StochasticForcedVars(dev, grid, params) : ForcedVars(dev, grid, params))
eqn = linear ? LinearEquation(dev, params, grid) : Equation(dev, params, grid)
FourierFlows.Problem(eqn, stepper, dt, grid, vars, params, dev)
end
abstract type BarotropicParams <: AbstractParams end
struct Params{T, Aphys3D, Aphys2D, Aphys1D, Atrans4D, Trfft} <: AbstractParams
# prescribed params
nlayers :: Int # Number of fluid layers
g :: T # Gravitational constant
f₀ :: T # Constant planetary vorticity
β :: T # Planetary vorticity y-gradient
ρ :: Aphys3D # Array with density of each fluid layer
H :: Aphys3D # Array with rest height of each fluid layer
U :: Aphys3D # Array with imposed constant zonal flow U(y) in each fluid layer
eta :: Aphys2D # Array containing topographic PV
μ :: T # Linear bottom drag
ν :: T # Viscosity coefficient
nν :: Int # Hyperviscous order (nν=1 is plain old viscosity)
calcFq! :: Function # Function that calculates the forcing on QGPV q
# derived params
g′ :: Aphys1D # Array with the reduced gravity constants for each fluid interface
Qx :: Aphys3D # Array containing x-gradient of PV due to eta in each fluid layer
Qy :: Aphys3D # Array containing y-gradient of PV due to β, U, and eta in each fluid layer
S :: Atrans4D # Array containing coeffients for getting PV from streamfunction
S⁻¹ :: Atrans4D # Array containing coeffients for inverting PV to streamfunction
rfftplan :: Trfft # rfft plan for FFTs
end
struct SingleLayerParams{T, Aphys3D, Aphys2D, Trfft} <: BarotropicParams
# prescribed params
β :: T # Planetary vorticity y-gradient
U :: Aphys3D # Imposed constant zonal flow U(y)
eta :: Aphys2D # Array containing topographic PV
μ :: T # Linear bottom drag
ν :: T # Viscosity coefficient
nν :: Int # Hyperviscous order (nν=1 is plain old viscosity)
calcFq! :: Function # Function that calculates the forcing on QGPV q
# derived params
Qx :: Aphys3D # Array containing x-gradient of PV due to eta
Qy :: Aphys3D # Array containing meridional PV gradient due to β, U, and eta
rfftplan :: Trfft # rfft plan for FFTs
end
function convert_U_to_U3D(dev, nlayers, grid, U::AbstractArray{TU, 1}) where TU
T = eltype(grid)
if length(U) == nlayers
U_2D = zeros(dev, T, (1, nlayers))
U_2D[:] = U
U_2D = repeat(U_2D, outer=(grid.ny, 1))
else
U_2D = zeros(dev, T, (grid.ny, 1))
U_2D[:] = U
end
U_3D = zeros(dev, T, (1, grid.ny, nlayers))
@views U_3D[1, :, :] = U_2D
return U_3D
end
function convert_U_to_U3D(dev, nlayers, grid, U::AbstractArray{TU, 2}) where TU
T = eltype(grid)
U_3D = zeros(dev, T, (1, grid.ny, nlayers))
@views U_3D[1, :, :] = U
return U_3D
end
function convert_U_to_U3D(dev, nlayers, grid, U::Number)
T = eltype(grid)
A = ArrayType(dev)
U_3D = reshape(repeat([T(U)], outer=(grid.ny, 1)), (1, grid.ny, nlayers))
return A(U_3D)
end
function Params(nlayers, g, f₀, β, ρ, H, U, eta, μ, ν, nν, grid; calcFq=nothingfunction, effort=FFTW.MEASURE, dev::Device=CPU()) where TU
T = eltype(grid)
A = ArrayType(dev)
ny, nx = grid.ny , grid.nx
nkr, nl = grid.nkr, grid.nl
kr, l = grid.kr , grid.l
U = convert_U_to_U3D(dev, nlayers, grid, U)
Uyy = real.(ifft(-l.^2 .* fft(U)))
Uyy = repeat(Uyy, outer=(nx, 1, 1))
etah = rfft(A(eta))
etax = irfft(im * kr .* etah, nx)
etay = irfft(im * l .* etah, nx)
Qx = zeros(dev, T, (nx, ny, nlayers))
@views @. Qx[:, :, nlayers] += etax
Qy = zeros(dev, T, (nx, ny, nlayers))
Qy = T(β) .- Uyy # T(β) is needed to ensure that Qy remains same type as U
@views @. Qy[:, :, nlayers] += etay
rfftplanlayered = plan_flows_rfft(A{T, 3}(undef, grid.nx, grid.ny, nlayers), [1, 2]; flags=effort)
if nlayers==1
return SingleLayerParams(T(β), U, eta, T(μ), T(ν), nν, calcFq, Qx, Qy, rfftplanlayered)
else # if nlayers≥2
ρ = reshape(T.(ρ), (1, 1, nlayers))
H = reshape(T.(H), (1, 1, nlayers))
g′ = T(g) * (ρ[2:nlayers] - ρ[1:nlayers-1]) ./ ρ[2:nlayers] # reduced gravity at each interface
Fm = @. T(f₀^2 / (g′ * H[2:nlayers]))
Fp = @. T(f₀^2 / (g′ * H[1:nlayers-1]))
typeofSkl = SArray{Tuple{nlayers, nlayers}, T, 2, nlayers^2} # StaticArrays of type T and dims = (nlayers x nlayers)
S = Array{typeofSkl, 2}(undef, (nkr, nl))
calcS!(S, Fp, Fm, nlayers, grid)
S⁻¹ = Array{typeofSkl, 2}(undef, (nkr, nl))
calcS⁻¹!(S⁻¹, Fp, Fm, nlayers, grid)
S, S⁻¹, Fp, Fm = A(S), A(S⁻¹), A(Fp), A(Fm) # convert to appropriate ArrayType
CUDA.@allowscalar @views Qy[:, :, 1] = @. Qy[:, :, 1] - Fp[1] * (U[:, :, 2] - U[:, :, 1])
for j = 2:nlayers-1
CUDA.@allowscalar @views Qy[:, :, j] = @. Qy[:, :, j] - Fp[j] * (U[:, :, j+1] - U[:, :, j]) + Fm[j-1] * (U[:, :, j-1] - U[:, :, j])
end
CUDA.@allowscalar @views Qy[:, :, nlayers] = @. Qy[:, :, nlayers] - Fm[nlayers-1] * (U[:, :, nlayers-1] - U[:, :, nlayers])
return Params(nlayers, T(g), T(f₀), T(β), A(ρ), A(H), U, eta, T(μ), T(ν), nν, calcFq, A(g′), Qx, Qy, S, S⁻¹, rfftplanlayered)
end
end
numberoflayers(params) = params.nlayers
numberoflayers(::SingleLayerParams) = 1
# ---------
# Equations
# ---------
function hyperdissipation(dev, params, grid)
T = eltype(grid)
L = ArrayType(dev){T}(undef, (grid.nkr, grid.nl, numberoflayers(params)))
@. L = - params.ν * grid.Krsq^params.nν
@views @. L[1, 1, :] = 0
return L
end
function LinearEquation(dev, params, grid)
L = hyperdissipation(dev, params, grid)
return FourierFlows.Equation(L, calcNlinear!, grid)
end
function Equation(dev, params, grid)
L = hyperdissipation(dev, params, grid)
return FourierFlows.Equation(L, calcN!, grid)
end
# ----
# Vars
# ----
struct Vars{Aphys, Atrans, F, P} <: AbstractVars
q :: Aphys
ψ :: Aphys
u :: Aphys
v :: Aphys
qh :: Atrans
ψh :: Atrans
uh :: Atrans
vh :: Atrans
Fqh :: F
prevsol :: P
end
const ForcedVars = Vars{<:AbstractArray, <:AbstractArray, <:AbstractArray, Nothing}
const StochasticForcedVars = Vars{<:AbstractArray, <:AbstractArray, <:AbstractArray, <:AbstractArray}
"""
Vars(dev, grid, params)
Returns the vars for unforced multi-layer QG problem with `grid` and `params`.
"""
function Vars(dev::Dev, grid, params) where Dev
T = eltype(grid)
nlayers = numberoflayers(params)
@devzeros Dev T (grid.nx, grid.ny, nlayers) q ψ u v
@devzeros Dev Complex{T} (grid.nkr, grid.nl, nlayers) qh ψh uh vh
return Vars(q, ψ, u, v, qh, ψh, uh, vh, nothing, nothing)
end
"""
ForcedVars(dev, grid, params)
Returns the vars for forced multi-layer QG problem with `grid` and `params`.
"""
function ForcedVars(dev::Dev, grid, params) where Dev
T = eltype(grid)
nlayers = numberoflayers(params)
@devzeros Dev T (grid.nx, grid.ny, nlayers) q ψ u v
@devzeros Dev Complex{T} (grid.nkr, grid.nl, nlayers) qh ψh uh vh Fqh
return Vars(q, ψ, u, v, qh, ψh, uh, vh, Fqh, nothing)
end
"""
StochasticForcedVars(dev, rid, params)
Returns the vars for forced multi-layer QG problem with `grid` and `params`.
"""
function StochasticForcedVars(dev::Dev, grid, params) where Dev
T = eltype(grid)
nlayers = numberoflayers(params)
@devzeros Dev T (grid.nx, grid.ny, nlayers) q ψ u v
@devzeros Dev Complex{T} (grid.nkr, grid.nl, nlayers) qh ψh uh vh Fqh prevsol
return Vars(q, ψ, u, v, qh, ψh, uh, vh, Fqh, prevsol)
end
fwdtransform!(varh, var, params::AbstractParams) = mul!(varh, params.rfftplan, var)
invtransform!(var, varh, params::AbstractParams) = ldiv!(var, params.rfftplan, varh)
function streamfunctionfrompv!(ψh, qh, params, grid)
for j=1:grid.nl, i=1:grid.nkr
CUDA.@allowscalar @views ψh[i, j, :] .= params.S⁻¹[i, j] * qh[i, j, :]
end
end
function pvfromstreamfunction!(qh, ψh, params, grid)
for j=1:grid.nl, i=1:grid.nkr
CUDA.@allowscalar @views qh[i, j, :] .= params.S[i, j] * ψh[i, j, :]
end
end
function streamfunctionfrompv!(ψh, qh, params::SingleLayerParams, grid)
@. ψh = -grid.invKrsq * qh
end
function pvfromstreamfunction!(qh, ψh, params::SingleLayerParams, grid)
@. qh = -grid.Krsq * ψh
end
"""
calcS!(S, Fp, Fm, nlayers, grid)
Constructs the array S, which consists of nlayer x nlayer static arrays S_kl that relate
the q's and ψ's at every wavenumber: q̂_{k, l} = S_kl * ψ̂_{k, l}.
"""
function calcS!(S, Fp, Fm, nlayers, grid)
F = Matrix(Tridiagonal(Fm, -([Fp; 0] + [0; Fm]), Fp))
for n=1:grid.nl, m=1:grid.nkr
CUDA.@allowscalar k² = grid.Krsq[m, n]
Skl = SMatrix{nlayers, nlayers}(- k² * I + F)
S[m, n] = Skl
end
return nothing
end
"""
calcS⁻¹!(S, Fp, Fm, nlayers, grid)
Constructs the array S⁻¹, which consists of nlayer x nlayer static arrays (S_kl)⁻¹ that
relate the q's and ψ's at every wavenumber: ψ̂_{k, l} = (S_kl)⁻¹ * q̂_{k, l}.
"""
function calcS⁻¹!(S⁻¹, Fp, Fm, nlayers, grid)
T = eltype(grid)
F = Matrix(Tridiagonal(Fm, -([Fp; 0] + [0; Fm]), Fp))
for n=1:grid.nl, m=1:grid.nkr
CUDA.@allowscalar k² = grid.Krsq[m, n] == 0 ? 1 : grid.Krsq[m, n]
Skl = - k² * I + F
S⁻¹[m, n] = SMatrix{nlayers, nlayers}(I / Skl)
end
S⁻¹[1, 1] = SMatrix{nlayers, nlayers}(zeros(T, (nlayers, nlayers)))
return nothing
end
# -------
# Solvers
# -------
function calcN!(N, sol, t, clock, vars, params, grid)
nlayers = numberoflayers(params)
calcN_advection!(N, sol, vars, params, grid)
@views @. N[:, :, nlayers] += params.μ * grid.Krsq * vars.ψh[:, :, nlayers] # bottom linear drag
addforcing!(N, sol, t, clock, vars, params, grid)
return nothing
end
function calcNlinear!(N, sol, t, clock, vars, params, grid)
nlayers = numberoflayers(params)
calcN_linearadvection!(N, sol, vars, params, grid)
@views @. N[:, :, nlayers] += params.μ * grid.Krsq * vars.ψh[:, :, nlayers] # bottom linear drag
addforcing!(N, sol, t, clock, vars, params, grid)
return nothing
end
"""
calcN_advection!(N, sol, vars, params, grid)
Calculates the advection term.
"""
function calcN_advection!(N, sol, vars, params, grid)
@. vars.qh = sol
streamfunctionfrompv!(vars.ψh, vars.qh, params, grid)
@. vars.uh = -im * grid.l * vars.ψh
@. vars.vh = im * grid.kr * vars.ψh
invtransform!(vars.u, vars.uh, params)
@. vars.u += params.U # add the imposed zonal flow U
uQx, uQxh = vars.q, vars.uh # use vars.q and vars.uh as scratch variables
@. uQx = vars.u * params.Qx # (U+u)*∂Q/∂x
fwdtransform!(uQxh, uQx, params)
@. N = - uQxh # -\hat{(U+u)*∂Q/∂x}
invtransform!(vars.v, vars.vh, params)
vQy, vQyh = vars.q, vars.vh # use vars.q and vars.vh as scratch variables
@. vQy = vars.v * params.Qy # v*∂Q/∂y
fwdtransform!(vQyh, vQy, params)
@. N -= vQyh # -\hat{v*∂Q/∂y}
invtransform!(vars.q, vars.qh, params)
uq , vq = vars.u , vars.v # use vars.u and vars.v as scratch variables
uqh, vqh = vars.uh, vars.vh # use vars.uh and vars.vh as scratch variables
@. uq *= vars.q # (U+u)*q
@. vq *= vars.q # v*q
fwdtransform!(uqh, uq, params)
fwdtransform!(vqh, vq, params)
@. N -= im * grid.kr * uqh + im * grid.l * vqh # -\hat{∂[(U+u)q]/∂x} - \hat{∂[vq]/∂y}
return nothing
end
"""
calcN_linearadvection!(N, sol, vars, params, grid)
Calculates the advection term of the linearized equations.
"""
function calcN_linearadvection!(N, sol, vars, params, grid)
@. vars.qh = sol
streamfunctionfrompv!(vars.ψh, vars.qh, params, grid)
@. vars.uh = -im * grid.l * vars.ψh
@. vars.vh = im * grid.kr * vars.ψh
invtransform!(vars.u, vars.uh, params)
@. vars.u += params.U # add the imposed zonal flow U
uQx, uQxh = vars.q, vars.uh # use vars.q and vars.uh as scratch variables
@. uQx = vars.u * params.Qx # (U+u)*∂Q/∂x
fwdtransform!(uQxh, uQx, params)
@. N = - uQxh # -\hat{(U+u)*∂Q/∂x}
invtransform!(vars.v, vars.vh, params)
vQy, vQyh = vars.q, vars.vh # use vars.q and vars.vh as scratch variables
@. vQy = vars.v * params.Qy # v*∂Q/∂y
fwdtransform!(vQyh, vQy, params)
@. N -= vQyh # -\hat{v*∂Q/∂y}
invtransform!(vars.q, vars.qh, params)
@. vars.u = params.U
Uq , Uqh = vars.u , vars.uh # use vars.u and vars.uh as scratch variables
@. Uq *= vars.q # U*q
fwdtransform!(Uqh, Uq, params)
@. N -= im * grid.kr * Uqh # -\hat{∂[U*q]/∂x}
return nothing
end
addforcing!(N, sol, t, clock, vars::Vars, params, grid) = nothing
function addforcing!(N, sol, t, clock, vars::ForcedVars, params, grid)
params.calcFq!(vars.Fqh, sol, t, clock, vars, params, grid)
@. N += vars.Fqh
return nothing
end
# ----------------
# Helper functions
# ----------------
"""
updatevars!(vars, params, grid, sol)
updatevars!(prob)
Update all problem variables using `sol`.
"""
function updatevars!(vars, params, grid, sol)
@. vars.qh = sol
streamfunctionfrompv!(vars.ψh, vars.qh, params, grid)
@. vars.uh = -im * grid.l * vars.ψh
@. vars.vh = im * grid.kr * vars.ψh
invtransform!(vars.q, deepcopy(vars.qh), params)
invtransform!(vars.ψ, deepcopy(vars.ψh), params)
invtransform!(vars.u, deepcopy(vars.uh), params)
invtransform!(vars.v, deepcopy(vars.vh), params)
return nothing
end
updatevars!(prob) = updatevars!(prob.vars, prob.params, prob.grid, prob.sol)
"""
set_q!(sol, params, vars, grid, q)
set_q!(prob)
Set the solution `prob.sol` as the transform of `q` and updates variables.
"""
function set_q!(sol, params, vars, grid, q)
A = typeof(vars.q)
fwdtransform!(vars.qh, A(q), params)
@. vars.qh[1, 1, :] = 0
@. sol = vars.qh
updatevars!(vars, params, grid, sol)
return nothing
end
function set_q!(sol, params::SingleLayerParams, vars, grid, q::AbstractArray{T, 2}) where T
A = typeof(vars.q[:, :, 1])
q_3D = vars.q
@views q_3D[:, :, 1] = A(q)
set_q!(sol, params, vars, grid, q_3D)
return nothing
end
set_q!(prob, q) = set_q!(prob.sol, prob.params, prob.vars, prob.grid, q)
"""
set_ψ!(params, vars, grid, sol, ψ)
set_ψ!(prob)
Set the solution `prob.sol` to correspond to the transform of streamfunction `ψ` and
updates variables.
"""
function set_ψ!(sol, params, vars, grid, ψ)
A = typeof(vars.ψ)
fwdtransform!(vars.ψh, A(ψ), params)
pvfromstreamfunction!(vars.qh, vars.ψh, params, grid)
invtransform!(vars.q, vars.qh, params)
set_q!(sol, params, vars, grid, vars.q)
return nothing
end
function set_ψ!(sol, params::SingleLayerParams, vars, grid, ψ::AbstractArray{T, 2}) where T
A = typeof(vars.ψ[:, :, 1])
ψ_3D = vars.ψ
@views ψ_3D[:, :, 1] = A(ψ)
set_ψ!(sol, params, vars, grid, ψ_3D)
return nothing
end
set_ψ!(prob, ψ) = set_ψ!(prob.sol, prob.params, prob.vars, prob.grid, ψ)
"""
energies(vars, params, grid, sol)
energies(prob)
Returns the kinetic energy of each fluid layer KE``_1, ...,`` KE``_{n}``, and the
potential energy of each fluid interface PE``_{3/2}, ...,`` PE``_{n-1/2}``, where ``n``
is the number of layers in the fluid. (When ``n=1``, only the kinetic energy is returned.)
The kinetic energy at the ``j``-th fluid layer is
```math
\\textrm{KE}_j = \\frac{H_j}{H} \\int \\frac1{2} |\\boldsymbol{\\nabla} \\psi_j|^2 \\frac{\\mathrm{d}^2 \\boldsymbol{x}}{L_x L_y} \\ , \\quad j = 1, \\dots, n \\ ,
```
while the potential energy that corresponds to the interface ``j+1/2`` (i.e., interface between the ``j``-th and ``(j+1)``-th fluid layer) is
```math
\\textrm{PE}_{j+1/2} = \\int \\frac1{2} \\frac{f_0^2}{g'_{j+1/2}} (\\psi_j - \\psi_{j+1})^2 \\frac{\\mathrm{d}^2 \\boldsymbol{x}}{L_x L_y} \\ , \\quad j = 1, \\dots, n-1 \\ .
```
"""
function energies(vars, params, grid, sol)
nlayers = numberoflayers(params)
KE, PE = zeros(nlayers), zeros(nlayers-1)
@. vars.qh = sol
streamfunctionfrompv!(vars.ψh, vars.qh, params, grid)
abs²∇𝐮h = vars.uh # use vars.uh as scratch variable
@. abs²∇𝐮h = grid.Krsq * abs2(vars.ψh)
for j = 1:nlayers
CUDA.@allowscalar KE[j] = 1 / (2 * grid.Lx * grid.Ly) * parsevalsum(abs²∇𝐮h[:, :, j], grid) * params.H[j] / sum(params.H)
end
for j = 1:nlayers-1
CUDA.@allowscalar PE[j] = 1 / (2 * grid.Lx * grid.Ly) * params.f₀^2 / params.g′[j] * parsevalsum(abs2.(vars.ψh[:, :, j+1] .- vars.ψh[:, :, j]), grid)
end
return KE, PE
end
function energies(vars, params::SingleLayerParams, grid, sol)
@. vars.qh = sol
streamfunctionfrompv!(vars.ψh, vars.qh, params, grid)
abs²∇𝐮h = vars.uh # use vars.uh as scratch variable
@. abs²∇𝐮h = grid.Krsq * abs2(vars.ψh)
return 1 / (2 * grid.Lx * grid.Ly) * parsevalsum(abs²∇𝐮h, grid)
end
energies(prob) = energies(prob.vars, prob.params, prob.grid, prob.sol)
"""
fluxes(vars, params, grid, sol)
fluxes(prob)
Returns the lateral eddy fluxes within each fluid layer, lateralfluxes``_1,...,``lateralfluxes``_n``
and also the vertical eddy fluxes at each fluid interface
verticalfluxes``_{3/2}``, ...,`` verticalfluxes``_{n-1/2}, where ``n`` is the number of layers in the fluid.
(When ``n=1``, only the lateral fluxes are returned.)
The lateral eddy fluxes whithin the ``j``-th fluid layer are
```math
\\textrm{lateralfluxes}_j = \\frac{H_j}{H} \\int U_j \\, \\upsilon_j \\, \\partial_y u_j
\\frac{\\mathrm{d}^2 \\boldsymbol{x}}{L_x L_y} \\ , \\quad j = 1, \\dots, n \\ ,
```
while the vertical eddy fluxes at the ``j+1/2``-th fluid interface (i.e., interface between
the ``j``-th and ``(j+1)``-th fluid layer) are
```math
\\textrm{verticalfluxes}_{j+1/2} = \\int \\frac{f_0^2}{g'_{j+1/2} H} (U_j - U_{j+1}) \\,
\\upsilon_{j+1} \\, \\psi_{j} \\frac{\\mathrm{d}^2 \\boldsymbol{x}}{L_x L_y} \\ , \\quad
j = 1 , \\dots , n-1.
```
"""
function fluxes(vars, params, grid, sol)
nlayers = numberoflayers(params)
lateralfluxes, verticalfluxes = zeros(nlayers), zeros(nlayers-1)
updatevars!(vars, params, grid, sol)
∂u∂yh = vars.uh # use vars.uh as scratch variable
∂u∂y = vars.u # use vars.u as scratch variable
@. ∂u∂yh = im * grid.l * vars.uh
invtransform!(∂u∂y, ∂u∂yh, params)
lateralfluxes = (sum(@. params.H * params.U * vars.v * ∂u∂y; dims=(1, 2)))[1, 1, :]
lateralfluxes *= grid.dx * grid.dy / (grid.Lx * grid.Ly * sum(params.H))
for j = 1:nlayers-1
CUDA.@allowscalar verticalfluxes[j] = sum(@views @. params.f₀^2 / params.g′[j] * (params.U[: ,:, j] - params.U[:, :, j+1]) * vars.v[:, :, j+1] * vars.ψ[:, :, j] ; dims=(1, 2))[1]
CUDA.@allowscalar verticalfluxes[j] *= grid.dx * grid.dy / (grid.Lx * grid.Ly * sum(params.H))
end
return lateralfluxes, verticalfluxes
end
function fluxes(vars, params::SingleLayerParams, grid, sol)
updatevars!(vars, params, grid, sol)
∂u∂yh = vars.uh # use vars.uh as scratch variable
∂u∂y = vars.u # use vars.u as scratch variable
@. ∂u∂yh = im * grid.l * vars.uh
invtransform!(∂u∂y, ∂u∂yh, params)
lateralfluxes = (sum(@. params.U * vars.v * ∂u∂y; dims=(1, 2)))[1, 1, :]
lateralfluxes *= grid.dx * grid.dy / (grid.Lx * grid.Ly)
return lateralfluxes
end
fluxes(prob) = fluxes(prob.vars, prob.params, prob.grid, prob.sol)
end # module