From 65f41fd47c1579d0a21f908e4f0d2ee24cd549e3 Mon Sep 17 00:00:00 2001 From: jbisits Date: Fri, 15 Nov 2024 10:39:32 +1100 Subject: [PATCH 1/4] Fixes for showing --- examples/single_interface_periodic.jl | 2 +- src/staircase_initial_conditions.jl | 2 +- src/staircase_restoring.jl | 34 ++++++++++++++++----------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/examples/single_interface_periodic.jl b/examples/single_interface_periodic.jl index 0c47cf5..e048632 100644 --- a/examples/single_interface_periodic.jl +++ b/examples/single_interface_periodic.jl @@ -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 diff --git a/src/staircase_initial_conditions.jl b/src/staircase_initial_conditions.jl index 1afbeca..2cb6a61 100644 --- a/src/staircase_initial_conditions.jl +++ b/src/staircase_initial_conditions.jl @@ -154,7 +154,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)") diff --git a/src/staircase_restoring.jl b/src/staircase_restoring.jl index a5ce066..a02c6a7 100644 --- a/src/staircase_restoring.jl +++ b/src/staircase_restoring.jl @@ -12,16 +12,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. @@ -30,14 +24,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. @@ -53,9 +58,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) @@ -129,6 +134,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 From 7c95b4ba3805f28785ce4d91a947c1facb140140 Mon Sep 17 00:00:00 2001 From: jbisits Date: Fri, 15 Nov 2024 11:38:50 +1100 Subject: [PATCH 2/4] Nice summaries --- src/StaircaseShenanigans.jl | 1 + src/staircase_initial_conditions.jl | 5 ++++- src/staircase_model.jl | 4 ++-- src/staircase_noise.jl | 9 +++++++-- src/staircase_restoring.jl | 2 -- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/StaircaseShenanigans.jl b/src/StaircaseShenanigans.jl index 3a57503..7fa24de 100644 --- a/src/StaircaseShenanigans.jl +++ b/src/StaircaseShenanigans.jl @@ -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 diff --git a/src/staircase_initial_conditions.jl b/src/staircase_initial_conditions.jl index 2cb6a61..7cca8b2 100644 --- a/src/staircase_initial_conditions.jl +++ b/src/staircase_initial_conditions.jl @@ -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 @@ -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 @@ -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 diff --git a/src/staircase_model.jl b/src/staircase_model.jl index 39adfdb..1aaa69c 100644 --- a/src/staircase_model.jl +++ b/src/staircase_model.jl @@ -11,8 +11,8 @@ 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, "┣━━━ initial_conditions: $(summary(sdns.initial_conditions))") + print(io, "┗━━━━━━━━ initial_noise: $(summary(sdns.initial_noise))") end """ function StaircaseDNS(model, initial_conditions; initial_noise = nothing) diff --git a/src/staircase_noise.jl b/src/staircase_noise.jl index 4112987..9409724 100644 --- a/src/staircase_noise.jl +++ b/src/staircase_noise.jl @@ -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. @@ -20,8 +23,9 @@ 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 @@ -37,3 +41,4 @@ function Base.show(io::IO, noise::AbstractNoise) 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" diff --git a/src/staircase_restoring.jl b/src/staircase_restoring.jl index a02c6a7..d893862 100644 --- a/src/staircase_restoring.jl +++ b/src/staircase_restoring.jl @@ -1,5 +1,3 @@ -abstract type BackgroundFunction end - Oceananigans.BackgroundField(bf::BackgroundFunction) = BackgroundField(bf.func, parameters = bf.parameters) """ From bb5c5908d225e264949680e0b42d75df0d2dc933 Mon Sep 17 00:00:00 2001 From: jbisits Date: Fri, 15 Nov 2024 11:41:13 +1100 Subject: [PATCH 3/4] Homogenise print statements --- src/staircase_initial_conditions.jl | 10 +++++----- src/staircase_model.jl | 6 +++--- src/staircase_noise.jl | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/staircase_initial_conditions.jl b/src/staircase_initial_conditions.jl index 7cca8b2..4a84bd0 100644 --- a/src/staircase_initial_conditions.jl +++ b/src/staircase_initial_conditions.jl @@ -174,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 diff --git a/src/staircase_model.jl b/src/staircase_model.jl index 1aaa69c..d0406d0 100644 --- a/src/staircase_model.jl +++ b/src/staircase_model.jl @@ -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: $(summary(sdns.initial_conditions))") - print(io, "┗━━━━━━━━ initial_noise: $(summary(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) diff --git a/src/staircase_noise.jl b/src/staircase_noise.jl index 9409724..4d319a1 100644 --- a/src/staircase_noise.jl +++ b/src/staircase_noise.jl @@ -30,13 +30,13 @@ Base.summary(noise::TracerNoise) = "Random noise magnitude in S and T fields $(n 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" From 4031922216e97cf28a26b05131b0ac7762ae2004 Mon Sep 17 00:00:00 2001 From: jbisits Date: Fri, 15 Nov 2024 11:46:13 +1100 Subject: [PATCH 4/4] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 49cf259..efdf81a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "StaircaseShenanigans" uuid = "c2bb06a8-94f3-4279-b990-30bf3ab8ba6f" authors = ["Josef Bisits "] -version = "0.4.2" +version = "0.4.3" [deps] GibbsSeaWater = "9a22fb26-0b63-4589-b28e-8f9d0b5c3d05"