From 1c0f44e3c262b9ce571a092050a532dea0be083a Mon Sep 17 00:00:00 2001 From: Daniel Vedder <daniel.vedder@idiv.de> Date: Sat, 26 Nov 2022 18:12:13 +0100 Subject: [PATCH] Model now saves per-species abundances each time step --- src/core/output.jl | 27 ++++++++++++++++++++++++++- src/core/simulation.jl | 1 + 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/core/output.jl b/src/core/output.jl index 75a37a3..de9047a 100644 --- a/src/core/output.jl +++ b/src/core/output.jl @@ -3,6 +3,9 @@ ### This file includes functions for saving the model output. ### +const LOGFILE = "simulation.log" +const POPFILE = "populations.csv" + ## Note: `setupdatadir()` was adapted from the GeMM model by Leidinger et al. ## (https://github.com/CCTB-Ecomods/gemm/blob/master/src/output.jl) @@ -29,7 +32,7 @@ function setupdatadir() elseif param("core.loglevel") == "quiet" loglevel = Logging.Warn end - logfile = open(joinpath(param("core.outdir"), "simulation.log"), "w+") + logfile = open(joinpath(param("core.outdir"), LOGFILE), "w+") simulationlogger = TeeLogger(ConsoleLogger(logfile, loglevel), ConsoleLogger(stdout, loglevel)) global_logger(simulationlogger) @@ -51,5 +54,27 @@ function setupdatadir() !(isfile(ffmap)) && Base.error("The map file $(ffmap) doesn't exist.") cp(lcmap, joinpath(param("core.outdir"), basename(lcmap)), force = true) cp(ffmap, joinpath(param("core.outdir"), basename(ffmap)), force = true) + # Create the data output file(s) + open(joinpath(param("core.outdir"), POPFILE), "w") do f + println(f, "Date;Species;Abundance") + end end +""" + savepopulationdata(model) + +Print a comma-separated set of lines to `populations.csv`, giving the +current date and population size for each animal species. +""" +function savepopulationdata(model::AgentBasedModel) + pops = Dict{String,Int}(s=>0 for s = param("nature.targetspecies")) + for a in allagents(model) + (typeof(a) != Animal) && continue + pops[a.species.name] += 1 + end + open(joinpath(param("core.outdir"), POPFILE), "a") do f + for p in keys(pops) + println(f, "$(model.date);$(p);$(pops[p])") + end + end +end diff --git a/src/core/simulation.jl b/src/core/simulation.jl index 48744d9..8e6f61a 100644 --- a/src/core/simulation.jl +++ b/src/core/simulation.jl @@ -44,6 +44,7 @@ function stepsimulation!(model::AgentBasedModel) for a in Schedulers.ByType((Farmer,Animal,CropPlot), true)(model) stepagent!(getindex(model, a), model) end + savepopulationdata(model) end """ -- GitLab