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
45using OceanBioME
56using OceanBioME: BoxModel
6- import OceanBioME. BoxModels: update_boxmodel_state!
77using JLD2
88using Plots
99
@@ -18,46 +18,49 @@ biogeochemistry = PhytoplanktonZooplankton()
1818# biogeochemistry = PhytoplanktonZooplankton(phytoplankton_growth_rate = 0.5)
1919
2020model = BoxModel (; biogeochemistry)
21- model. Δt = 0.05
22- model. stop_time = 50
2321
2422# Set the initial conditions
2523set! (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 ))
5659end
5760
5861mp4 (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 );
6366plot (plt1, plt2, layout = (1 ,2 ))
0 commit comments