Skip to content

Commit

Permalink
Merge pull request #68 from jbisits/jib-patches
Browse files Browse the repository at this point in the history
Specify `NamedTuple` type in struct +`show` fixes
  • Loading branch information
jbisits authored Nov 15, 2024
2 parents 41c7047 + 4031922 commit 78cc878
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StaircaseShenanigans"
uuid = "c2bb06a8-94f3-4279-b990-30bf3ab8ba6f"
authors = ["Josef Bisits <[email protected]>"]
version = "0.4.2"
version = "0.4.3"

[deps]
GibbsSeaWater = "9a22fb26-0b63-4589-b28e-8f9d0b5c3d05"
Expand Down
2 changes: 1 addition & 1 deletion examples/single_interface_periodic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ model_setup = (;architecture, diffusivities, domain_extent, resolution, eos)
depth_of_interface = -0.5
salinity = [34.58, 34.70]
temperature = [-1.5, 0.5]
interface_ics = PeriodoicSingleInterfaceICs(eos, depth_of_interface, salinity, temperature, BackgroundTanh())
interface_ics = PeriodoicSingleInterfaceICs(eos, depth_of_interface, salinity, temperature, BackgroundTanh(tanh_background, 100, NamedTuple()))
tracer_noise = TracerNoise(1e-6, 1e-6)

## setup model
Expand Down
1 change: 1 addition & 0 deletions src/StaircaseShenanigans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import SeawaterPolynomials.SecondOrderSeawaterPolynomials: RoquetSeawaterPolynom
abstract type AbstractStaircaseModel end
abstract type AbstractInitialConditions end
abstract type AbstractNoise end
abstract type BackgroundFunction end

export StaircaseDNS, PeriodicStaircaseDNS, DNSModel, SDNS_simulation_setup

Expand Down
17 changes: 10 additions & 7 deletions src/staircase_initial_conditions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function STSingleInterfaceInitialConditions(eos::BoussinesqEquationOfState, dept

end
const SingleInterfaceICs = STSingleInterfaceInitialConditions # alias

Base.summary(ics::SingleInterfaceICs) = "Single S-T interface at z = $(ics.depth_of_interface)"
"""
struct PeriodicSTSingleInterfaceInitialConditions
Sets a `BackgroundField` according to `background_State` and uses a triply periodic domain
Expand All @@ -68,6 +68,7 @@ function PeriodicSTSingleInterfaceInitialConditions(eos::BoussinesqEquationOfSta

end
const PeriodoicSingleInterfaceICs = PeriodicSTSingleInterfaceInitialConditions # alias
Base.summary(ics::PeriodoicSingleInterfaceICs) = "Single S-T interface at z = $(ics.depth_of_interface) on triply periodic domain with $(summary(ics.background_state)) state"

"Container for initial conditions that have well mixed layers seperated by sharp step interfaces."
struct STStaircaseInitialConditions{T} <: AbstractStaircaseInitialConditions
Expand All @@ -92,6 +93,8 @@ function STStaircaseInitialConditions(model, number_of_interfaces, depth_of_inte

end
const StaircaseICs = STStaircaseInitialConditions # alias
Base.summary(ics::StaircaseICs) = "Multiple S-T interfaces at z = $(ics.depth_of_interfaces)"

"""
function compute_R_ρ(salinity, temperature, depth_of_interfaces, eos)
Compute the density ratio, ``R_{\rho}``, at a diffusive interface with a non-linear equation of state
Expand Down Expand Up @@ -154,7 +157,7 @@ function Base.show(io::IO, sics::AbstractStaircaseInitialConditions)
println(io, "┣━━━━ salinity_values: $(sics.salinity_values)")
println(io, "┣━ temperature_values: $(sics.temperature_values)")
println(io, "┣━━━━━━━━━━━━━━━━ R_ρ: $(round.(sics.R_ρ; digits = 2))")
println(io, "┗━━━ background_state: $(typeof(sics.background_state))")
println(io, "┗━━━ background_state: $(summary(sics.background_state))")
elseif sics isa STSingleInterfaceInitialConditions
println(io, "STSingleInterfaceInitialConditions")
println(io, "┣━ depth_of_interface: $(sics.depth_of_interface)")
Expand All @@ -171,10 +174,10 @@ function Base.show(io::IO, sics::AbstractStaircaseInitialConditions)
print(io, "┗━━━━━━━━━━━━━━━━━━ R_ρ: $(round.(sics.R_ρ; digits = 2))")
elseif sics isa SmoothSTStaircaseInitialConditions
println(io, "STStaircaseInitialConditions")
println(io, "┣━━━━ number_of_interfaces: $(sics.number_of_interfaces)")
println(io, "┣━━━━━ depth_of_interfaces: $(sics.depth_of_interfaces)")
println(io, "┣━━━━ salinity_values: $(sics.salinity_values)")
println(io, "┣━ temperature_values: $(sics.temperature_values)")
print(io, "┗━ smoothing_funciton: $(sics.smoothing_funciton)")
println(io, "┣━ number_of_interfaces: $(sics.number_of_interfaces)")
println(io, "┣━━ depth_of_interfaces: $(sics.depth_of_interfaces)")
println(io, "┣━━━━━━ salinity_values: $(sics.salinity_values)")
println(io, "┣━━━ temperature_values: $(sics.temperature_values)")
print(io, "┗━━━ smoothing_funciton: $(sics.smoothing_funciton)")
end
end
6 changes: 3 additions & 3 deletions src/staircase_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ struct StaircaseDNS{NHM <: NonhydrostaticModel,
end
function Base.show(io::IO, sdns::StaircaseDNS)
println(io, "StaircaseDirectNumericalSimulation")
println(io, "┣━━━━━━━━━━━━━━━━ model: $(summary(sdns.model))")
println(io, "┣━━━ initial_conditions: $(typeof(sdns.initial_conditions))")
print(io, "┗━━━━━━━━ initial_noise: $(typeof(sdns.initial_noise))")
println(io, "┣━━━━━━━━━━━━━━ model: $(summary(sdns.model))")
println(io, "┣━ initial_conditions: $(summary(sdns.initial_conditions))")
print(io, "┗━━━━━━ initial_noise: $(summary(sdns.initial_noise))")
end
"""
function StaircaseDNS(model, initial_conditions; initial_noise = nothing)
Expand Down
19 changes: 12 additions & 7 deletions src/staircase_noise.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Base.iterate(noise::AbstractNoise, state = 1) =
state > length(fieldnames(noise)) ? nothing : (getfield(noise, state), state + 1)

"""
struct VelocityNoise{T}
Container for the magnitudes of normally distributed noise added to velocity fields.
Expand All @@ -20,20 +23,22 @@ struct TracerNoise{T} <: AbstractNoise
"Noise magnitude for `T` tracer field"
T_magnitude :: T
end
Base.iterate(noise::AbstractNoise, state = 1) =
state > length(fieldnames(noise)) ? nothing : (getfield(noise, state), state + 1)
"Convenience for all noise at same magnitude `c`, default behaviour is 1e-4"
TracerNoise(c::Float64=1e-4) = TracerNoise(c, c)
Base.summary(noise::TracerNoise) = "Random noise magnitude in S and T fields $(noise.S_magnitude) and $(noise.T_magnitude) respectively"

function Base.show(io::IO, noise::AbstractNoise)
if noise isa VelocityNoise
println(io, "VelocityNoise")
println(io, "┣━ u_noise_magnitude: $(noise.u_magnitude)")
println(io, "┣━ v_noise_magnitude: $(noise.v_magnitude)")
print(io, "┗━ w_noise_magnitude: $(noise.w_magnitude)")
println(io, "┣━ u_noise_magnitude: $(noise.u_magnitude)")
println(io, "┣━ v_noise_magnitude: $(noise.v_magnitude)")
print(io, "┗━ w_noise_magnitude: $(noise.w_magnitude)")
elseif noise isa TracerNoise
println(io, "TracerNoise")
println(io, "┣━ S_noise_magnitude: $(noise.S_magnitude)")
print(io, "┗━ T_noise_magnitude: $(noise.T_magnitude)")
println(io, "┣━ S_noise_magnitude: $(noise.S_magnitude)")
print(io, "┗━ T_noise_magnitude: $(noise.T_magnitude)")
end
end
"Convenience for all noise at same magnitude `c`, default behaviour is 1e-4"
VelocityNoise(c::Float64=1e-4) = VelocityNoise(c, c, c)
Base.summary(noise::VelocityNoise) = "Random noise magnitude in u, v and w fields $(noise.u_magnitude), $(noise.v_magnitude), and $(noise.w_magnitude) respectively"
36 changes: 20 additions & 16 deletions src/staircase_restoring.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
abstract type BackgroundFunction end

Oceananigans.BackgroundField(bf::BackgroundFunction) =
BackgroundField(bf.func, parameters = bf.parameters)
"""
Expand All @@ -12,16 +10,10 @@ mutable struct BackgroundTanh{F, T} <: BackgroundFunction
"Scale the steepness of the `tanh` change"
scale :: T
"Parameters for the tanh background field"
parameters :: Any
parameters :: NamedTuple
end
BackgroundTanh() = BackgroundTanh(tanh_background, 100, NamedTuple())
BackgroundTanh(scale) = BackgroundTanh(tanh_background, scale, NamedTuple())
function Base.show(io, bt::BackgroundTanh)
println(io, "BackgroundTanh")
println(io, "┣━━━ function: $(bt.func)")
println(io, "┣━━━━━━ scale: $(bt.scale)")
print(io, "┗━ parameters: $(bt.parameters)")
end
"""
mutable struct BackgroundLinear{F, P}
Container for a linear background field.
Expand All @@ -30,14 +22,25 @@ mutable struct BackgroundLinear{F} <: BackgroundFunction
"Linear function"
func :: F
"Parameters for the tanh background field"
parameters :: Any
parameters :: NamedTuple
end
BackgroundLinear() = BackgroundLinear(linear_background, NamedTuple())
function Base.show(io, bl::BackgroundLinear)
println(io, "BackgroundLinear")
println(io, "┣━━━ function: $(bl.func)")
print(io, "┗━ parameters: $(bl.parameters)")

function Base.show(io::IO, bf::BackgroundFunction)
if bf isa BackgroundTanh
println(io, "BackgroundTanh")
println(io, "┣━━━ function: $(bf.func)")
println(io, "┣━━━━━━ scale: $(bf.scale)")
print(io, "┗━ parameters: $(bf.parameters)")
elseif bf isa BackgroundLinear
println(io, "BackgroundLinear")
println(io, "┣━━━ function: $(bf.func)")
print(io, "┗━ parameters: $(bf.parameters)")
end
end
Base.summary(bt::BackgroundTanh) = "$(bt.func)"
Base.summary(bl::BackgroundLinear) = "$(bl.func)"

"""
function S_and_T_background_fields(initial_conditions)
Set background fields for the `S` and `T` tracer fields where the domain is triply periodic.
Expand All @@ -53,9 +56,9 @@ function S_and_T_background_fields(ics::PeriodicSTSingleInterfaceInitialConditio
end
"Sets a background state that is hyperbolic tangent. There is also a method to save an
`Array` of this backgorund state to output."
tanh_background(x, y, z, t, p) = p.Cₗ - 0.5 * p.ΔC * (1 + tanh(p.D * (z - p.z_interface) / p.Lz))
@inline tanh_background(x, y, z, t, p) = p.Cₗ - 0.5 * p.ΔC * (1 + tanh(p.D * (z - p.z_interface) / p.Lz))
tanh_background(z, ΔC, Cₗ, Lz, z_interface, D) = Cₗ - 0.5 * ΔC * (1 + tanh(D * (z - z_interface) / Lz))
linear_background(x, y, z, t, p) = p.Cᵤ - p.ΔC * z / p.Lz
@inline linear_background(x, y, z, t, p) = p.Cᵤ - p.ΔC * z / p.Lz
linear_background(z, ΔC, Cᵤ, Lz) = Cᵤ - ΔC * z / Lz

function get_parameters!(ics::PeriodicSTSingleInterfaceInitialConditions, tracer::Symbol, Lz)
Expand Down Expand Up @@ -132,6 +135,7 @@ function save_background_state!(simulation, model, initial_conditions::Periodoic

return nothing
end

# The following functions are to be used as `BoundaryConditions` so that tracers can
# re-enter the domain with the initial gradient added effectively allowing the gradient to
# be maintained. Another option is to add background tracer fields and only evolve the anomaly
Expand Down

0 comments on commit 78cc878

Please sign in to comment.