Skip to content

Commit cba047c

Browse files
authored
Merge pull request #155 from jbisits/functionrename
Rename `DNS` - > `DNSModel`, `TLDNS_simulation_setup` + update README.md + change default behaviour of `overwrite_existing`
2 parents 4cccff7 + 0154aa9 commit cba047c

File tree

8 files changed

+82
-86
lines changed

8 files changed

+82
-86
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.4.5"
4+
version = "0.5.0"
55

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://github.com/jbisits/TwoLayerDirectNumericalShenanigans.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/jbisits/TwoLayerDirectNumericalShenanigans.jl/actions/workflows/CI.yml?query=branch%3Amain)
44

5-
A Julia package (unregistered) to explore the cabbeling instability using Direct Numerical Simulation experiments built with [Oceananigans.jl](https://github.com/CliMA/Oceananigans.jl).
5+
A Julia package (unregistered) to explore two layer systems using Direct Numerical Simulation experiments built with [Oceananigans.jl](https://github.com/CliMA/Oceananigans.jl).
66

77
To add the package (assuming julia is already installed):
88

examples/twolayer_example.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,31 @@ diffusivities = (ν = 1e-4, κ = (S = 1e-5, T = 1e-5))
66
resolution = (Nx = 10, Ny = 10, Nz = 100)
77

88
## Setup the model
9-
model = DNS(architecture, DOMAIN_EXTENT, resolution, diffusivities;
10-
reference_density = REFERENCE_DENSITY)
9+
model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities;
10+
reference_density = REFERENCE_DENSITY)
1111

1212
## set initial conditions
1313
T₀ᵘ = -1.5
1414
S₀ᵘ = (stable = 34.551, cabbeling = 34.58, unstable = 34.59)
1515
cabbeling = CabbelingUpperLayerInitialConditions(S₀ᵘ.cabbeling, T₀ᵘ)
1616
initial_conditions = TwoLayerInitialConditions(cabbeling)
1717
transition_depth = find_depth(model, INTERFACE_LOCATION)
18-
profile_function = StepChange(transition_depth)#HyperbolicTangent(INTERFACE_LOCATION, 1.0)
18+
profile_function = StepChange(transition_depth)
1919
tracer_perturbation_depth = find_depth(model, INTERFACE_LOCATION / 2)
2020
tracer_perturbation = SalinityGaussianProfile(tracer_perturbation_depth, 0.0, 1.5)
2121
noise_depth = find_depth(model, INTERFACE_LOCATION)
2222
initial_noise = SalinityNoise(noise_depth, 1e-2)
2323

24-
dns = TwoLayerDNS(model, profile_function, initial_conditions; initial_noise)
24+
tldns = TwoLayerDNS(model, profile_function, initial_conditions; initial_noise)
2525

26-
set_two_layer_initial_conditions!(dns)
26+
set_two_layer_initial_conditions!(tldns)
2727

2828
## build the simulation
2929
Δt = 1e-4
3030
stop_time = 60
3131
save_schedule = 0.5 # seconds
32-
output_path = joinpath(@__DIR__, "outputs/")
33-
simulation = DNS_simulation_setup(dns, Δt, stop_time, save_schedule,
34-
overwrite_existing = true; output_path)
32+
output_path = joinpath(@__DIR__, "outputs")
33+
simulation = TLDNS_simulation_setup(tldns, Δt, stop_time, save_schedule; output_path)
3534

3635
## Run the simulation
3736
run!(simulation)

src/TwoLayerDirectNumericalShenanigans.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract type AbstractProfileFunction end
2828
"Abstract supertype for dns initial conditions."
2929
abstract type AbstractInitialConditions end
3030

31-
export TwoLayerDNS, DNS, DNS_simulation_setup
31+
export TwoLayerDNS, DNSModel, TLDNS_simulation_setup
3232

3333
export
3434
StableUpperLayerInitialConditions,

src/twolayerdns.jl

Lines changed: 63 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ Base.iterate(tldns::TwoLayerDNS, state = 1) =
3333
state > length(fieldnames(TwoLayerDNS)) ? nothing :
3434
(getfield(tldns, state), state + 1)
3535
"""
36-
function DNS(architecture, domain_extent::NamedTuple, resolution::NamedTuple,
37-
diffusivities::NamedTuple)
38-
Setup a Direct Numerical Simulation on `architecture` (`CPU()` or `GPU()`) over the
36+
function DNSModel(architecture, domain_extent::NamedTuple, resolution::NamedTuple,
37+
diffusivities::NamedTuple)
38+
Setup a Direct Numerical Simulation [`Model`](https://clima.github.io/OceananigansDocumentation/dev/appendix/library/#Oceananigans.Models.NonhydrostaticModels.NonhydrostaticModel-Tuple{})
39+
on `architecture` (`CPU()` or `GPU()`) over the
3940
`domain_extent` with `resolution` and scalar diffusivities for momentum
4041
(kinematic viscosity `ν`) and the temperature and salinity tracers (`κₜ` and `κₛ`).
4142
To evolve the temperature and salinity tracers, the [polynomial approximation to the
@@ -66,15 +67,15 @@ the rate `stretching`, if `false` uniform grid spacing is used;
6667
- `refinement = 1.2` spacing near the surface in the `z` dimension;
6768
- `stretching = 100` rate of stretching at the bottom of grid in the `z` dimension.
6869
"""
69-
function DNS(architecture, domain_extent::NamedTuple, resolution::NamedTuple,
70-
diffusivities::NamedTuple;
71-
linear_eos = false,
72-
α = 1.67e-4,
73-
β = 7.80e-4,
74-
reference_density = nothing,
75-
zgrid_stretching = false,
76-
refinement = 1.05,
77-
stretching = 40)
70+
function DNSModel(architecture, domain_extent::NamedTuple, resolution::NamedTuple,
71+
diffusivities::NamedTuple;
72+
linear_eos = false,
73+
α = 1.67e-4,
74+
β = 7.80e-4,
75+
reference_density = nothing,
76+
zgrid_stretching = false,
77+
refinement = 1.05,
78+
stretching = 40)
7879

7980
Lx, Ly, Lz = domain_extent.Lx, domain_extent.Ly, domain_extent.Lz
8081
Nx, Ny, Nz = resolution.Nx, resolution.Ny, resolution.Nz
@@ -127,11 +128,11 @@ function grid_stretching(Lz::Number, Nz::Number, refinement::Number, stretching:
127128

128129
end
129130
"""
130-
function DNS_simulation_setup(dns::TwoLayerDNS, Δt::Number, stop_time::Number,
131-
save_schedule::Number; cfl = 0.75, diffusive_cfl = 0.75,
132-
max_change = 1.2, max_Δt = 1e-1)
133-
Setup a DNS from `initial_conditions` that are of type `TwoLayerInitialConditions`.
134-
Important non-dimensional numnbers that are part of this experiment are computed and saved
131+
function TLDNS_simulation_setup(tldns::TwoLayerDNS, Δt::Number, stop_time::Number,
132+
save_schedule::Number; cfl = 0.75, diffusive_cfl = 0.75,
133+
max_change = 1.2, max_Δt = 1e-1)
134+
Setup a `Simulation` of `tldns` from `initial_conditions` that are of type `TwoLayerInitialConditions`.
135+
Important non-dimensional numbers that are part of this experiment are computed and saved
135136
to the simulation output file.
136137
137138
## Function arguments:
@@ -143,7 +144,7 @@ the course of a simulation;
143144
- `savefile` name of the file to save the data to,
144145
- `save_schedule` number (representing time in seconds) at which to save model output, e.g.,
145146
`save_schedule = 1` saves output every second;
146-
- `output_writer` a `Symbol` (either `:netcdf` or `:jld2`) choosing whether to save data in
147+
- `save_file` a `Symbol` (either `:netcdf` or `:jld2`) choosing whether to save data in
147148
`NetCDF` format (`.nc`) ot `JLD2` format ('.jld2).
148149
149150
## Keyword arguments:
@@ -158,23 +159,23 @@ there is no `Checkpointer`.
158159
- `max_Δt` the maximum timestep;
159160
- `density_reference_gp_height` for the seawater density calculation;
160161
- `save_velocities` defaults to `false`, if `true` model velocities will be saved to output;
161-
- `overwrite_existing` whether the output overwrites a file of the same name, default is
162-
`false`.
162+
- `overwrite_saved_output` whether the output overwrites a file of the same name, default is
163+
`true`.
163164
"""
164-
function DNS_simulation_setup(dns::TwoLayerDNS, Δt::Number,
165-
stop_time::Number, save_schedule::Number,
166-
output_writer::Symbol=:netcdf;
167-
output_path = SIMULATION_PATH,
168-
checkpointer_time_interval = nothing,
169-
cfl = 0.75,
170-
diffusive_cfl = 0.75,
171-
max_change = 1.2,
172-
max_Δt = 1e-1,
173-
density_reference_gp_height = 0,
174-
overwrite_existing = false,
175-
save_velocities = false)
176-
177-
model = dns.model
165+
function TLDNS_simulation_setup(tldns::TwoLayerDNS, Δt::Number,
166+
stop_time::Number, save_schedule::Number;
167+
save_file = :netcdf,
168+
output_path = SIMULATION_PATH,
169+
checkpointer_time_interval = nothing,
170+
cfl = 0.75,
171+
diffusive_cfl = 0.75,
172+
max_change = 1.2,
173+
max_Δt = 1e-1,
174+
density_reference_gp_height = 0,
175+
overwrite_saved_output = nothing,
176+
save_velocities = false)
177+
178+
model = tldns.model
178179
simulation = Simulation(model; Δt, stop_time)
179180

180181
# time step adjustments
@@ -186,62 +187,58 @@ function DNS_simulation_setup(dns::TwoLayerDNS, Δt::Number,
186187

187188
# Custom saved output
188189
# Potential density
189-
σ = seawater_density(model, geopotential_height = 0)
190+
σ = seawater_density(model, geopotential_height = density_reference_gp_height)
190191

191192
# Inferred vertical temperature diffusivity
192-
T_mean = Average(T)
193-
T′ = T - Field(T_mean)
194-
w′ = wᶜᶜᶜ(model)
195-
w′T′ = Field(w′ * T′)
196-
∫ₐw′T′ = Integral(w′T′, dims = (1, 2))
197-
T_gradient = ∂z(T)
198-
∫ₐT_gradient = Integral(T_gradient, dims = (1, 2))
193+
# T_mean = Average(T)
194+
# T′ = T - Field(T_mean)
195+
# w′ = wᶜᶜᶜ(model)
196+
# w′T′ = Field(w′ * T′)
197+
# ∫ₐw′T′ = Integral(w′T′, dims = (1, 2))
198+
# T_gradient = ∂z(T)
199+
# ∫ₐT_gradient = Integral(T_gradient, dims = (1, 2))
199200

200201
ϵ = KineticEnergyDissipationRate(model)
201202
# Volume integrated TKE dissipation
202203
∫ϵ = Integral(ϵ)
203204
# Minimum in space Kolmogorov length scale
204205
η_space(model) = (model.closure.ν^3 / maximum(ϵ))^(1/4)
205206

206-
# Volume integrated TKE dissipation
207-
∫ϵ = Integral(ϵ)
208-
209207
# Dimensions and attributes for custom saved output
210208
dims = Dict("η_space" => ())
211209
oa = Dict(
212210
"σ" => Dict("longname" => "Seawater potential density calculated using TEOS-10 at $(density_reference_gp_height)dbar",
213211
"units" => "kgm⁻³"),
214212
"η_space" => Dict("longname" => "Minimum (in space) Kolmogorov length"),
215-
"∫ₐw′T′" => Dict("longname" => "Horizontally integrated vertical temperature flux (w′T′)"),
216-
"∫ₐT_gradient" => Dict("longname" => "Horizontally integrated vertical temperature gradient (∂T/∂z)"),
213+
# "∫ₐw′T′" => Dict("longname" => "Horizontally integrated vertical temperature flux (w′T′)"),
214+
# "∫ₐT_gradient" => Dict("longname" => "Horizontally integrated vertical temperature gradient (∂T/∂z)"),
217215
"∫ϵ" => Dict("longname" => "Volume integrated turbulent kintetic energy dissipation")
218216
)
219217

220218
# outputs to be saved during the simulation
221-
outputs = Dict("S" => S, "T" => T, "η_space" => η_space, "σ" => σ, "∫ϵ" => ∫ϵ,
222-
"∫ₐw′T′" => ∫ₐw′T′, "∫ₐT_gradient" => ∫ₐT_gradient)
219+
outputs = Dict("S" => S, "T" => T, "η_space" => η_space, "σ" => σ, "∫ϵ" => ∫ϵ)#=,
220+
"∫ₐw′T′" => ∫ₐw′T′, "∫ₐT_gradient" => ∫ₐT_gradient)=#
223221
if save_velocities
224222
u, v, w = model.velocities
225223
velocities = Dict("u" => u, "v" => v, "w" => w)
226224
merge!(outputs, velocities)
227225
end
228226

229-
filename = form_filename(dns, stop_time, output_writer, output_path)
230-
simulation.output_writers[:outputs] = output_writer == :netcdf ?
231-
NetCDFOutputWriter(model, outputs;
232-
filename,
233-
overwrite_existing,
227+
filename = form_filename(tldns, stop_time, save_file, output_path)
228+
simulation.output_writers[:outputs] = save_file == :netcdf ?
229+
NetCDFOutputWriter(model, outputs; filename,
230+
overwrite_existing = overwrite_saved_output,
234231
schedule = TimeInterval(save_schedule),
235232
dimensions = dims,
236233
output_attributes = oa
237234
) :
238235
JLD2OutputWriter(model, outputs;
239236
filename,
240237
schedule = TimeInterval(save_schedule),
241-
overwrite_existing)
238+
overwrite_existing = overwrite_saved_output)
242239

243-
non_dimensional_numbers!(simulation, dns)
244-
predicted_maximum_density!(simulation, dns)
240+
non_dimensional_numbers!(simulation, tldns)
241+
predicted_maximum_density!(simulation, tldns)
245242

246243
# progress reporting
247244
simulation.callbacks[:progress] = Callback(simulation_progress, IterationInterval(100))
@@ -252,26 +249,26 @@ function DNS_simulation_setup(dns::TwoLayerDNS, Δt::Number,
252249

253250
end
254251
"""
255-
function form_filename(dns::TwoLayerDNS, stop_time::Number, output_writer::Symbol, output_path::AbstractString)
252+
function form_filename(tldns::TwoLayerDNS, stop_time::Number, save_file, output_path)
256253
Create a filename for saved output based on the `profile_function`,`initial_conditions`,
257254
`tracer_perturbation` and length of the simulation.
258255
"""
259-
function form_filename(dns::TwoLayerDNS, stop_time::Number, output_writer::Symbol, output_path::AbstractString)
256+
function form_filename(tldns::TwoLayerDNS, stop_time::Number, save_file, output_path)
260257

261-
pf_string = lowercase(string(typeof(dns.profile_function))[1:findfirst('{', string(typeof(dns.profile_function))) - 1])
262-
ic_type = typeof(dns.initial_conditions)
258+
pf_string = lowercase(string(typeof(tldns.profile_function))[1:findfirst('{', string(typeof(tldns.profile_function))) - 1])
259+
ic_type = typeof(tldns.initial_conditions)
263260
ic_string = ic_type <: StableTwoLayerInitialConditions ? "stable" :
264261
ic_type <: CabbelingTwoLayerInitialConditions ?
265262
"cabbeling" : ic_type <: UnstableTwoLayerInitialConditions ?
266263
"unstable" : ic_type <: IsohalineTwoLayerInitialConditions ?
267264
"isohaline" : "isothermal"
268265

269-
tp_string = lowercase(string(typeof(dns.tracer_perturbation)))
266+
tp_string = lowercase(string(typeof(tldns.tracer_perturbation)))
270267
tp_find = isnothing(findfirst('{', tp_string)) ? length(tp_string) :
271268
findfirst('{', tp_string) - 1
272269
stop_time_min = stop_time / 60 1 ? string(round(Int, stop_time / 60)) :
273270
string(round(stop_time / 60; digits = 2))
274-
filetype = output_writer == :netcdf ? ".nc" : ".jld2"
271+
filetype = save_file == :netcdf ? ".nc" : ".jld2"
275272
savefile = ic_string *"_"* pf_string *"_"* tp_string[1:tp_find] *"_"* stop_time_min * "min" * filetype
276273

277274
# make a simulation directory if one is not present
@@ -284,11 +281,11 @@ function form_filename(dns::TwoLayerDNS, stop_time::Number, output_writer::Symbo
284281

285282
end
286283
"""
287-
function checkpointer_setup!(simulation, model, checkpointer_time_interval)
284+
function checkpointer_setup!(simulation, model, output_path, checkpointer_time_interval)
288285
Setup a `Checkpointer` at `checkpointer_time_interval` for a `simulation`
289286
"""
290-
function checkpointer_setup!(simulation::Simulation, model::Oceananigans.AbstractModel,
291-
output_path::AbstractString, checkpointer_time_interval::Number)
287+
function checkpointer_setup!(simulation, model, output_path,
288+
checkpointer_time_interval::Number)
292289

293290
dir = output_path == SIMULATION_PATH ? CHECKPOINT_PATH :
294291
joinpath(output_path, "model_checkpoints/")

test/initialconditions_test.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Need to write these to check things are set at the right level in the right field
22
diffusivities == 1e-4, κ = (S = 1e-5, T = 1e-5))
33
resolution = (Nx = 10, Ny = 10, Nz = 500)
4-
model = DNS(architecture, DOMAIN_EXTENT, resolution, diffusivities;
5-
reference_density = REFERENCE_DENSITY)
4+
model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities;
5+
reference_density = REFERENCE_DENSITY)
66

77
## set initial conditions, currently there are four options available in this submodule
88
initial_conditions = StableTwoLayerInitialConditions(0, 0, 0, 0, 0, 0)

test/kernelfunction_test.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ using GibbsSeaWater
77
diffusivities == 1e-4, κ = (S = 1e-5, T = 1e-5))
88
resolution = (Nx = 10, Ny = 10, Nz = 100)
99

10-
model = DNS(architecture, DOMAIN_EXTENT, resolution, diffusivities;
11-
reference_density = REFERENCE_DENSITY)
10+
model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities;
11+
reference_density = REFERENCE_DENSITY)
1212

1313
z = znodes(model.grid, Center())
1414

test/runtests.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ include("initialconditions_test.jl")
6363

6464
for tb tracer_profile_perturbations
6565

66-
model = DNS(architecture, DOMAIN_EXTENT, resolution, diffusivities;
66+
model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities;
6767
reference_density = REFERENCE_DENSITY)
6868
dns = TwoLayerDNS(model, profile_function, initial_conditions, tracer_perturbation = tb)
6969
set_two_layer_initial_conditions!(dns)
@@ -80,7 +80,7 @@ end
8080

8181
for tb tracer_blob_perturbations
8282

83-
model = DNS(architecture, DOMAIN_EXTENT, resolution, diffusivities;
83+
model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities;
8484
reference_density = REFERENCE_DENSITY)
8585
dns = TwoLayerDNS(model, profile_function, initial_conditions, tracer_perturbation = tb)
8686
set_two_layer_initial_conditions!(dns)
@@ -94,7 +94,7 @@ end
9494

9595
for tn tracer_noise_perturbations
9696

97-
model = DNS(architecture, DOMAIN_EXTENT, resolution, diffusivities;
97+
model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities;
9898
reference_density = REFERENCE_DENSITY)
9999
dns = TwoLayerDNS(model, profile_function, initial_conditions, initial_noise = tn)
100100
set_two_layer_initial_conditions!(dns)
@@ -104,7 +104,7 @@ end
104104
end
105105
for tnv tracer_noise_perturbations_vec
106106

107-
model = DNS(architecture, DOMAIN_EXTENT, resolution, diffusivities;
107+
model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities;
108108
reference_density = REFERENCE_DENSITY)
109109
dns = TwoLayerDNS(model, profile_function, initial_conditions, initial_noise = tnv)
110110
set_two_layer_initial_conditions!(dns)
@@ -117,7 +117,7 @@ end
117117

118118
@testset "Step change" begin
119119

120-
model = DNS(architecture, DOMAIN_EXTENT, resolution, diffusivities;
120+
model = DNSModel(architecture, DOMAIN_EXTENT, resolution, diffusivities;
121121
reference_density = REFERENCE_DENSITY)
122122
profile_function = StepChange(z[depth_idx])
123123
initial_conditions = TwoLayerInitialConditions(34.551, -1.5, 34.7, 0.5)

0 commit comments

Comments
 (0)