Skip to content

Commit

Permalink
Merge pull request #174 from jbisits/energeticcomputations
Browse files Browse the repository at this point in the history
Add `KineticEnergy` + onfly `potential_energy`
  • Loading branch information
jbisits authored Nov 25, 2023
2 parents 95acaa1 + 1c7ae56 commit e14ac64
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TwoLayerDirectNumericalShenanigans"
uuid = "40aaee9f-3595-48be-b36c-f1067009652f"
authors = ["Josef Bisits <[email protected]>"]
version = "0.6.1"
version = "0.6.2"

[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand Down
5 changes: 2 additions & 3 deletions src/TwoLayerDirectNumericalShenanigans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ module TwoLayerDirectNumericalShenanigans
using Oceananigans, Printf, Reexport, JLD2, NCDatasets, GibbsSeaWater
using Oceananigans: AbstractModel, Operators.ℑzᵃᵃᶜ
using Oceananigans: Models.seawater_density
using SeawaterPolynomials: BoussinesqEquationOfState
using SeawaterPolynomials: BoussinesqEquationOfState, ρ
using SeawaterPolynomials.TEOS10
using SeawaterPolynomials.SecondOrderSeawaterPolynomials
import SeawaterPolynomials.ρ
using SpecialFunctions: erf
using Oceanostics: KineticEnergyDissipationRate
using Oceanostics: KineticEnergyDissipationRate, KineticEnergy
using CUDA: allowscalar, CuArray
import Base: show, iterate

Expand Down
26 changes: 26 additions & 0 deletions src/kernelfunctions.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@inline tracer_perturbationᶜᶜᶜ(i, j, k, grid, tracer, tracer_mean) =
tracer[i, j, k] - tracer_mean[i, j, k]
# Only needed for testing
"""
function tracer_perturbation(model, tracer)
Calculate the perturbation of `tracer` from the spatial `tracer` mean.
"""
@inline function tracer_perturbation(model, tracer)

grid = model.grid
Expand All @@ -11,6 +15,10 @@ end

@inline vtf(i, j, k, grid, tracer, tracer_mean, w) =
-ℑzᵃᵃᶜ(i, j, k, w) * tracer_perturbationᶜᶜᶜ(i, j, k, grid, tracer, tracer_mean)
"""
function vertical_tracer_flux(model, tracer)
Calculate the vertical tracer flux of `tracer` from `model`.
"""
@inline function vertical_tracer_flux(model, tracer)

grid = model.grid
Expand All @@ -19,3 +27,21 @@ end

return KernelFunctionOperation{Center, Center, Center}(vtf, grid, tracer, tracer_mean, w)
end
"""
function potential_energy(model)
Calculate the potential energy
```math
Eₚ = g∫ᵥρzdV
```
for `model` during a simulation.
"""
@inline function potential_energy(model)

grid = model.grid
ρ = seawater_density(model)
Z = Oceananigans.Models.model_geopotential_height(model)
g = tuple(model.buoyancy.model.gravitational_acceleration)

return KernelFunctionOperation{Center, Center, Center}(Ep, grid, ρ, Z, g)
end
@inline Ep(i, j, k, grid, ρ, Z, g) = g[1] * ρ[i, j, k] * Z[i, j, k]
11 changes: 9 additions & 2 deletions src/twolayerdns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ function save_computed_output!(simulation, tldns, save_schedule, save_file, outp
ϵ = KineticEnergyDissipationRate(model)
∫ϵ = Integral(ϵ)
ϵ_maximum = Reduction(maximum!, ϵ, dims = (1, 2, 3))
Eₖ = KineticEnergy(model)
∫Eₖ = Integral(Eₖ)
Eₚ = potential_energy(model)
∫Eₚ = Integral(Eₚ)

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

computed_outputs = Dict("σ" => σ, "∫ϵ" => ∫ϵ, "ϵ_maximum" => ϵ_maximum, "∫ₐ∂T∂z" => ∫ₐ∂T∂z,
"∫ₐw′T′" => ∫ₐw′T′)
computed_outputs = Dict("σ" => σ, "∫ϵ" => ∫ϵ, "ϵ_maximum" => ϵ_maximum, "∫Eₖ" => ∫Eₖ,
"∫Eₚ" => ∫Eₚ, "∫ₐ∂T∂z" => ∫ₐ∂T∂z, "∫ₐw′T′" => ∫ₐw′T′)

oa = Dict(
"σ" => Dict("longname" => "Seawater potential density calculated using TEOS-10 at $(reference_gp_height)dbar",
"units" => "kgm⁻³"),
"ϵ_maximum" => Dict("longname" => "Maximum (in space) TKE dissipation"),
"∫ϵ" => Dict("longname" => "Volume integrated turbulent kintetic energy dissipation"),
"∫Eₖ" => Dict("longname" => "Volume integrated turbulent kinetic energy"),
"∫Eₚ" => Dict("longname" => "Volume integrated potential energy (g∫ᵥρzdV)"),
"∫ₐw′T′" => Dict("longname" => "Horizontally integrated vertical temeprature flux"),
"∫ₐ∂T∂z" => Dict("longname" => "Horizontally integrated vertical temperature gradient")
)
Expand Down
9 changes: 9 additions & 0 deletions test/kernelfunction_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ profile_function = StepChange(transition_depth)
tldns = TwoLayerDNS(model, profile_function, initial_conditions)

set_two_layer_initial_conditions!(tldns)

Eₚ_model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities)
set!(Eₚ_model, T = -1.5, S = 34.568)
ρ_model = Field(seawater_density(Eₚ_model))
compute!(ρ_model)
z_grid = Field(Oceananigans.Models.model_geopotential_height(Eₚ_model))
compute!(z_grid)
dV = xspacings(model.grid, Center()) * yspacings(model.grid, Center()) * zspacings(model.grid, Center())
computed_Eₚ = model.buoyancy.model.gravitational_acceleration * ρ_model.data .* z_grid.data
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using TwoLayerDirectNumericalShenanigans, Test
using TwoLayerDirectNumericalShenanigans: perturb_tracer
using Oceananigans.Fields
using Oceananigans: Models.seawater_density
using SeawaterPolynomials
using NCDatasets, JLD2
import SeawaterPolynomials.ρ
Expand Down Expand Up @@ -189,3 +190,11 @@ include("kernelfunction_test.jl")
@test all(vtflux.data .≈ 0 )

end

@testset "Potential energy" begin

Eₚ = Field(TLDNS.potential_energy(Eₚ_model))
compute!(Eₚ)
@test all(Eₚ.data == computed_Eₚ)

end

0 comments on commit e14ac64

Please sign in to comment.