Skip to content

Commit e14ac64

Browse files
authored
Merge pull request #174 from jbisits/energeticcomputations
Add `KineticEnergy` + onfly `potential_energy`
2 parents 95acaa1 + 1c7ae56 commit e14ac64

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TwoLayerDirectNumericalShenanigans"
22
uuid = "40aaee9f-3595-48be-b36c-f1067009652f"
33
authors = ["Josef Bisits <[email protected]>"]
4-
version = "0.6.1"
4+
version = "0.6.2"
55

66
[deps]
77
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"

src/TwoLayerDirectNumericalShenanigans.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ module TwoLayerDirectNumericalShenanigans
33
using Oceananigans, Printf, Reexport, JLD2, NCDatasets, GibbsSeaWater
44
using Oceananigans: AbstractModel, Operators.ℑzᵃᵃᶜ
55
using Oceananigans: Models.seawater_density
6-
using SeawaterPolynomials: BoussinesqEquationOfState
6+
using SeawaterPolynomials: BoussinesqEquationOfState, ρ
77
using SeawaterPolynomials.TEOS10
88
using SeawaterPolynomials.SecondOrderSeawaterPolynomials
9-
import SeawaterPolynomials.ρ
109
using SpecialFunctions: erf
11-
using Oceanostics: KineticEnergyDissipationRate
10+
using Oceanostics: KineticEnergyDissipationRate, KineticEnergy
1211
using CUDA: allowscalar, CuArray
1312
import Base: show, iterate
1413

src/kernelfunctions.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
@inline tracer_perturbationᶜᶜᶜ(i, j, k, grid, tracer, tracer_mean) =
22
tracer[i, j, k] - tracer_mean[i, j, k]
33
# Only needed for testing
4+
"""
5+
function tracer_perturbation(model, tracer)
6+
Calculate the perturbation of `tracer` from the spatial `tracer` mean.
7+
"""
48
@inline function tracer_perturbation(model, tracer)
59

610
grid = model.grid
@@ -11,6 +15,10 @@ end
1115

1216
@inline vtf(i, j, k, grid, tracer, tracer_mean, w) =
1317
-ℑzᵃᵃᶜ(i, j, k, w) * tracer_perturbationᶜᶜᶜ(i, j, k, grid, tracer, tracer_mean)
18+
"""
19+
function vertical_tracer_flux(model, tracer)
20+
Calculate the vertical tracer flux of `tracer` from `model`.
21+
"""
1422
@inline function vertical_tracer_flux(model, tracer)
1523

1624
grid = model.grid
@@ -19,3 +27,21 @@ end
1927

2028
return KernelFunctionOperation{Center, Center, Center}(vtf, grid, tracer, tracer_mean, w)
2129
end
30+
"""
31+
function potential_energy(model)
32+
Calculate the potential energy
33+
```math
34+
Eₚ = g∫ᵥρzdV
35+
```
36+
for `model` during a simulation.
37+
"""
38+
@inline function potential_energy(model)
39+
40+
grid = model.grid
41+
ρ = seawater_density(model)
42+
Z = Oceananigans.Models.model_geopotential_height(model)
43+
g = tuple(model.buoyancy.model.gravitational_acceleration)
44+
45+
return KernelFunctionOperation{Center, Center, Center}(Ep, grid, ρ, Z, g)
46+
end
47+
@inline Ep(i, j, k, grid, ρ, Z, g) = g[1] * ρ[i, j, k] * Z[i, j, k]

src/twolayerdns.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ function save_computed_output!(simulation, tldns, save_schedule, save_file, outp
292292
ϵ = KineticEnergyDissipationRate(model)
293293
∫ϵ = Integral(ϵ)
294294
ϵ_maximum = Reduction(maximum!, ϵ, dims = (1, 2, 3))
295+
Eₖ = KineticEnergy(model)
296+
∫Eₖ = Integral(Eₖ)
297+
Eₚ = potential_energy(model)
298+
∫Eₚ = Integral(Eₚ)
295299

296300
# Horizontally integrated vertical temperature flux and vertical temperature gradient
297301
T = model.tracers.T
@@ -300,13 +304,16 @@ function save_computed_output!(simulation, tldns, save_schedule, save_file, outp
300304
∂T∂z = ∂z(T)
301305
∫ₐ∂T∂z = Integral(∂T∂z, dims = (1, 2))
302306

303-
computed_outputs = Dict("σ" => σ, "∫ϵ" => ∫ϵ, "ϵ_maximum" => ϵ_maximum, "∫ₐ∂T∂z" => ∫ₐ∂T∂z,
304-
"∫ₐw′T′" => ∫ₐw′T′)
307+
computed_outputs = Dict("σ" => σ, "∫ϵ" => ∫ϵ, "ϵ_maximum" => ϵ_maximum, "∫Eₖ" => ∫Eₖ,
308+
"∫Eₚ" => ∫Eₚ, "∫ₐ∂T∂z" => ∫ₐ∂T∂z, "∫ₐw′T′" => ∫ₐw′T′)
309+
305310
oa = Dict(
306311
"σ" => Dict("longname" => "Seawater potential density calculated using TEOS-10 at $(reference_gp_height)dbar",
307312
"units" => "kgm⁻³"),
308313
"ϵ_maximum" => Dict("longname" => "Maximum (in space) TKE dissipation"),
309314
"∫ϵ" => Dict("longname" => "Volume integrated turbulent kintetic energy dissipation"),
315+
"∫Eₖ" => Dict("longname" => "Volume integrated turbulent kinetic energy"),
316+
"∫Eₚ" => Dict("longname" => "Volume integrated potential energy (g∫ᵥρzdV)"),
310317
"∫ₐw′T′" => Dict("longname" => "Horizontally integrated vertical temeprature flux"),
311318
"∫ₐ∂T∂z" => Dict("longname" => "Horizontally integrated vertical temperature gradient")
312319
)

test/kernelfunction_test.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ profile_function = StepChange(transition_depth)
1313
tldns = TwoLayerDNS(model, profile_function, initial_conditions)
1414

1515
set_two_layer_initial_conditions!(tldns)
16+
17+
Eₚ_model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities)
18+
set!(Eₚ_model, T = -1.5, S = 34.568)
19+
ρ_model = Field(seawater_density(Eₚ_model))
20+
compute!(ρ_model)
21+
z_grid = Field(Oceananigans.Models.model_geopotential_height(Eₚ_model))
22+
compute!(z_grid)
23+
dV = xspacings(model.grid, Center()) * yspacings(model.grid, Center()) * zspacings(model.grid, Center())
24+
computed_Eₚ = model.buoyancy.model.gravitational_acceleration * ρ_model.data .* z_grid.data

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using TwoLayerDirectNumericalShenanigans, Test
22
using TwoLayerDirectNumericalShenanigans: perturb_tracer
33
using Oceananigans.Fields
4+
using Oceananigans: Models.seawater_density
45
using SeawaterPolynomials
56
using NCDatasets, JLD2
67
import SeawaterPolynomials.ρ
@@ -189,3 +190,11 @@ include("kernelfunction_test.jl")
189190
@test all(vtflux.data .≈ 0 )
190191

191192
end
193+
194+
@testset "Potential energy" begin
195+
196+
Eₚ = Field(TLDNS.potential_energy(Eₚ_model))
197+
compute!(Eₚ)
198+
@test all(Eₚ.data == computed_Eₚ)
199+
200+
end

0 commit comments

Comments
 (0)