Skip to content

Commit c168ee5

Browse files
committed
Option to save velocities + homogensise .nc functions
1 parent 360b964 commit c168ee5

File tree

3 files changed

+71
-28
lines changed

3 files changed

+71
-28
lines changed

examples/twolayer_example.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ set_two_layer_initial_conditions!(dns)
2929
Δt = 1e-4
3030
stop_time = 60
3131
save_schedule = 0.5 # seconds
32-
simulation = DNS_simulation_setup(dns, Δt, stop_time, save_schedule)
32+
simulation = DNS_simulation_setup(dns, Δt, stop_time, save_schedule, save_velocities = true)
3333

3434
## Run the simulation
3535
run!(simulation)

src/output_utils.jl

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ function field_ts_timemean(field_ts::FieldTimeSeries)
127127

128128
end
129129
"""
130-
function predicted_maximum_density()
131-
Compute the predicted maximum density of water that can form along the mixing line.
130+
function predicted_maximum_density
131+
Compute the predicted maximum density of water that can form along the mixing line between
132+
the salinity and temperature of the upper and lower layers.
132133
"""
133134
function predicted_maximum_density!(simulation::Simulation, dns::TwoLayerDNS; reference_pressure = 0)
134135

@@ -137,8 +138,15 @@ function predicted_maximum_density!(simulation::Simulation, dns::TwoLayerDNS; re
137138
S_mix = range(Sᵘ, Sˡ, step = 0.000001)
138139
T_mix = @. Tᵘ + slope * (Sᵘ - S_mix)
139140
ρ_mix_max = maximum(gsw_rho.(S_mix, T_mix, reference_pressure))
140-
NCDataset(simulation.output_writers[:outputs].filepath, "a") do ds
141-
ds.attrib["Predicted maximum density"] = ρ_mix_max
141+
file_type = find_file_type(simulation.output_writers[:outputs].filepath)
142+
if isequal(file_type, ".nc")
143+
NCDataset(simulation.output_writers[:outputs].filepath, "a") do ds
144+
ds.attrib["Predicted maximum density"] = ρ_mix_max
145+
end
146+
elseif isequal(file_type, ".jld2")
147+
jldopen(simulation.output_writers[:outputs].filepath, "a+") do f
148+
f["Predicted maximum density"] = ρ_mix_max
149+
end
142150
end
143151

144152
return nothing
@@ -208,15 +216,15 @@ function kolmogorov_and_batchelor_scale!(file::AbstractString)
208216
file_type = find_file_type(file)
209217
if isequal(file_type, ".nc")
210218

211-
ϵ = Raster(file, lazy = true, name = )
212-
ds = NCDataset(file, "a")
213-
ν_str = ds.attrib["ν"]
214-
ν = parse(Float64, ν_str[1:findfirst('m', ν_str)-1])
215-
Sc = ds.attrib["Sc"]
216-
η_min = minimum_η(ϵ; ν)
217-
ds.attrib["η (min)"] = η_min # minimum space and time Kolmogorov scale
218-
ds.attrib["λ_B"] = η_min / sqrt(Sc) # minimum space and time Batchelor scale
219-
close(ds)
219+
NCDataset(file, "a") do ds
220+
ϵ = Raster(file, lazy = true, name = )
221+
ν_str = ds.attrib["ν"]
222+
ν = parse(Float64, ν_str[1:findfirst('m', ν_str)-1])
223+
Sc = ds.attrib["Sc"]
224+
η_min = minimum_η(ϵ; ν)
225+
ds.attrib["η (min)"] = η_min # minimum space and time Kolmogorov scale
226+
ds.attrib["λ_B"] = η_min / sqrt(Sc) # minimum space and time Batchelor scale
227+
end
220228

221229
elseif isequal(file_type, ".jld2")
222230

@@ -234,6 +242,35 @@ function kolmogorov_and_batchelor_scale!(file::AbstractString)
234242

235243
return nothing
236244

245+
end
246+
"""
247+
function batchelor_scale!
248+
Compute and append the minimum space-time Batchelor scale and append it to save output.
249+
"""
250+
function batchelor_scale!(file::AbstractString)
251+
252+
file_type = find_file_type(file)
253+
if isequal(file_type, ".nc")
254+
255+
NCDataset(file, "a") do ds
256+
Sc = ds.attrib["Sc"]
257+
η_min = minimum(ds["η_space"][:])
258+
ds.attrib["λ_B"] = η_min / sqrt(Sc) # minimum space and time Batchelor scale
259+
end
260+
261+
elseif isequal(file_type, ".jld2")
262+
263+
Sc = load(file, "Non_dimensional_numbers")["Sc"]
264+
η = FieldTimeSeries(simulation.output_writers[:outputs].filepath, "η_space")
265+
min_η = minimum(η)
266+
jldopen(file, "a+") do f
267+
f["minimum_batchelor_scale"] = min_η / sqrt(Sc)
268+
end
269+
270+
end
271+
272+
return nothing
273+
237274
end
238275
"""
239276
function non_dimensional_numbers(simulation::Simulation, dns::TwoLayerDNS)
@@ -263,16 +300,16 @@ function non_dimensional_numbers!(simulation::Simulation, dns::TwoLayerDNS)
263300

264301
if simulation.output_writers[:outputs] isa NetCDFOutputWriter
265302

266-
ds = NCDataset(simulation.output_writers[:outputs].filepath, "a")
267-
ds.attrib["EOS"] = summary(model.buoyancy.model.equation_of_state.seawater_polynomial)
268-
ds.attrib["Reference density"] = "$(model.buoyancy.model.equation_of_state.reference_density)kgm⁻³"
269-
ds.attrib["ν"] = "$(model.closure.ν) m²s⁻¹"
270-
ds.attrib["κₛ"] = "$(model.closure.κ.S) m²s⁻¹"
271-
ds.attrib["κₜ"] = "$(model.closure.κ.T) m²s⁻¹"
272-
for key keys(nd_nums)
273-
ds.attrib[key] = nd_nums[key]
303+
NCDataset(simulation.output_writers[:outputs].filepath, "a") do ds
304+
ds.attrib["EOS"] = summary(model.buoyancy.model.equation_of_state.seawater_polynomial)
305+
ds.attrib["Reference density"] = "$(model.buoyancy.model.equation_of_state.reference_density)kgm⁻³"
306+
ds.attrib["ν"] = "$(model.closure.ν) m²s⁻¹"
307+
ds.attrib["κₛ"] = "$(model.closure.κ.S) m²s⁻¹"
308+
ds.attrib["κₜ"] = "$(model.closure.κ.T) m²s⁻¹"
309+
for key keys(nd_nums)
310+
ds.attrib[key] = nd_nums[key]
311+
end
274312
end
275-
close(ds)
276313

277314
else
278315

src/twolayerdns.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ the course of a simulation;
152152
- `diffusive_cfl` maximum diffusive cfl value used to determine the adaptive timestep size;
153153
- `max_change` maximum change in the timestep size;
154154
- `max_Δt` the maximum timestep;
155-
- `density_reference_pressure` for the seawater density calculation.
155+
- `density_reference_pressure` for the seawater density calculation;
156+
- `save_velocities` defaults to `false`, if `true` model velocities will be saved to output.
156157
"""
157158
function DNS_simulation_setup(dns::TwoLayerDNS, Δt::Number,
158159
stop_time::Number, save_schedule::Number,
@@ -161,7 +162,8 @@ function DNS_simulation_setup(dns::TwoLayerDNS, Δt::Number,
161162
diffusive_cfl = 0.75,
162163
max_change = 1.2,
163164
max_Δt = 1e-1,
164-
density_reference_pressure = 0)
165+
density_reference_pressure = 0,
166+
save_velocities = false)
165167

166168
model = dns.model
167169
simulation = Simulation(model; Δt, stop_time)
@@ -193,11 +195,15 @@ function DNS_simulation_setup(dns::TwoLayerDNS, Δt::Number,
193195
"units" => "kgm⁻³"),
194196
"η_space" => Dict("longname" => "Minimum (in space) Kolmogorov length"),
195197
"κᵥ" => Dict("longname" => "Inferred vertical diffusivity",
196-
"units" => "m²s⁻¹")
197-
)
198+
"units" => "m²s⁻¹"))
198199

199200
# outputs to be saved during the simulation
200-
outputs = (S = S, T = T, η_space = η_space, σ = σ, κᵥ = κᵥ)
201+
outputs = Dict("S" => S, "T" => T, "η_space" => η_space, "σ" => σ, "κᵥ" => κᵥ)
202+
if save_velocities
203+
u, v = model.velocities.u, model.velocities.v
204+
velocities = Dict("u" => u, "v" => v, "w" => w)
205+
merge!(outputs, velocities)
206+
end
201207

202208
filename = form_filename(dns, stop_time, output_writer)
203209
simulation.output_writers[:outputs] = output_writer == :netcdf ?

0 commit comments

Comments
 (0)