Skip to content

Commit 7b12848

Browse files
update box model
Updated PZ_box.jl to work with new box model interface
1 parent 713b959 commit 7b12848

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

Manifest.toml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Project4/PZ_box.jl

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# This script runs the phytoplankton-zooplankton (PZ) model as a 0-D box model using OceanBioME
22

33
# Import some required modules
4+
using Oceananigans
45
using OceanBioME
56
using OceanBioME: BoxModel
6-
import OceanBioME.BoxModels: update_boxmodel_state!
77
using JLD2
88
using Plots
99

@@ -18,46 +18,49 @@ biogeochemistry = PhytoplanktonZooplankton()
1818
#biogeochemistry = PhytoplanktonZooplankton(phytoplankton_growth_rate = 0.5)
1919

2020
model = BoxModel(; biogeochemistry)
21-
model.Δt = 0.05
22-
model.stop_time = 50
2321

2422
# Set the initial conditions
2523
set!(model, P = 0.1, Z = 0.1)
2624

25+
simulation = Simulation(model; Δt = 0.05, stop_time = 50)
26+
27+
simulation.output_writers[:fields] = JLD2Writer(model, model.fields; filename = "box_pz.jld2", schedule = IterationInterval(1), overwrite_existing = true)
28+
29+
prog(sim) = @info "$(prettytime(time(sim))) in $(prettytime(simulation.run_wall_time))"
30+
31+
simulation.callbacks[:progress] = Callback(prog, IterationInterval(1000))
32+
2733
# ## Run the model and save the output every save_interval timesteps (should only take a few seconds)
28-
@info "Running the model..."
29-
run!(model, save_interval = 1, feedback_interval = Inf, save = SaveBoxModel("box_pz.jld2"))
34+
@info "Running the simulation..."
35+
run!(simulation)
3036

3137
# Now, read the saved output and plot the results
3238

33-
vars = (:P, :Z)
34-
file = jldopen("box_pz.jld2")
35-
times = parse.(Float64, keys(file["values"]))
36-
37-
timeseries = NamedTuple{vars}(ntuple(t -> zeros(length(times)), length(vars)))
39+
# Now, read the data and plot the results. This is saved as a Field with metadata
40+
P_field = FieldTimeSeries("box_pz.jld2", "P")
41+
Z_field = FieldTimeSeries("box_pz.jld2", "Z")
3842

39-
for (idx, time) in enumerate(times)
40-
values = file["values/$time"]
41-
for tracer in vars
42-
getproperty(timeseries, tracer)[idx] = values[tracer]
43-
end
44-
end
43+
# Extract the data at the single grid point corresponding to the box model
44+
P=P_field.data[1,1,1,:]
45+
Z=Z_field.data[1,1,1,:]
4546

46-
close(file)
47+
# Extract the time from one of the variables
48+
t=P_field.times
4749

48-
anim = @animate for i=1:5:length(times)
49-
plt1 = plot(times[1:i], timeseries.P[1:i], linewidth = 2, xlabel = "time", ylabel = "P, Z", legend = :outertopleft, label = "P", linecolor = :blue);
50-
scatter!(plt1, [times[i]], [timeseries.P[i]], markercolor = :blue, label = :none)
51-
plot!(plt1, times[1:i], timeseries.Z[1:i], linewidth = 2, label="Z", linecolor = :red, xlimits = (0, maximum(times)), ylimits = (0, max(maximum(timeseries.P),maximum(timeseries.Z))));
52-
scatter!(plt1, [times[i]], [timeseries.Z[i]], markercolor = :red, label = :none)
53-
plt2 = plot(timeseries.P[1:i], timeseries.Z[1:i], linewidth = 2, xlabel="P", ylabel="Z", xlimits = (0, maximum(timeseries.P)), ylimits = (0,maximum(timeseries.Z)), linecolor = :black, legend = :none);
54-
scatter!(plt2, [timeseries.P[i]], [timeseries.Z[i]], markercolor = :black, label = :none)
50+
# Make an animated plot of the evolution of the PZ model
51+
anim = @animate for i=1:5:length(P_field.times)
52+
plt1 = plot(t[1:i], P[1:i], linewidth = 2, xlabel = "time", ylabel = "P, Z", legend = :outertopleft, label = "P", linecolor = :blue);
53+
scatter!(plt1, [t[i]], [P[i]], markercolor = :blue, label = :none)
54+
plot!(plt1, t[1:i], Z[1:i], linewidth = 2, label="Z", linecolor = :red, xlimits = (0, maximum(P_field.times)), ylimits = (0, max(maximum(P),maximum(Z))));
55+
scatter!(plt1, [t[i]], [Z[i]], markercolor = :red, label = :none)
56+
plt2 = plot(P[1:i], Z[1:i], linewidth = 2, xlabel="P", ylabel="Z", xlimits = (0, maximum(P)), ylimits = (0,maximum(Z)), linecolor = :black, legend = :none);
57+
scatter!(plt2, [P[i]], [Z[i]], markercolor = :black, label = :none)
5558
plot(plt1, plt2, layout = (1,2))
5659
end
5760

5861
mp4(anim, "PZ_box.mp4", fps = 20) # hide
5962

60-
plt1 = plot(times, timeseries.P, linewidth = 2, xlabel = "time", ylabel = "P, Z", legend = :outertopleft, label = "P");
61-
plot!(plt1, times,timeseries.Z, linewidth = 2, label="Z");
62-
plt2 = plot(timeseries.P, timeseries.Z, linewidth = 2, xlabel="P", ylabel="Z", linecolor = :black, legend = :none);
63+
plt1 = plot(t, P, linewidth = 2, xlabel = "time", ylabel = "P, Z", legend = :outertopleft, label = "P");
64+
plot!(plt1, t, Z, linewidth = 2, label="Z");
65+
plt2 = plot(P, Z, linewidth = 2, xlabel="P", ylabel="Z", linecolor = :black, legend = :none);
6366
plot(plt1, plt2, layout = (1,2))

0 commit comments

Comments
 (0)