Skip to content
Snippets Groups Projects
Commit 855124ec authored by xo30xoqa's avatar xo30xoqa
Browse files

Rewrote initmodel() and stepsimulation!() functions

parent d2a260f8
No related branches found
No related tags found
No related merge requests found
...@@ -24,7 +24,7 @@ mutable struct SimulationModel ...@@ -24,7 +24,7 @@ mutable struct SimulationModel
crops::Dict{String,CropType} crops::Dict{String,CropType}
farmers::Vector{Farmer} farmers::Vector{Farmer}
farmplots::Vector{FarmPlot} farmplots::Vector{FarmPlot}
animals::Vector{Animal} animals::Vector{Union{Animals,Nothing}}
migrants::Vector{Pair{Animal,Date}} migrants::Vector{Pair{Animal,Date}}
events::Vector{FarmEvent} events::Vector{FarmEvent}
end end
...@@ -85,7 +85,6 @@ a helper function for `initialise()`. ...@@ -85,7 +85,6 @@ a helper function for `initialise()`.
""" """
function initmodel(settings::Dict{String, Any}) function initmodel(settings::Dict{String, Any})
#TODO catch exceptions and print them to the log file #TODO catch exceptions and print them to the log file
#TODO remove Agents.jl-related code, reimplement this more cleanly (#72)
@debug "Initialising model object." @debug "Initialising model object."
createdatadir(settings["core.outdir"], settings["core.overwrite"]) createdatadir(settings["core.outdir"], settings["core.overwrite"])
logger = modellogger(settings["core.loglevel"], settings["core.outdir"]) logger = modellogger(settings["core.loglevel"], settings["core.outdir"])
...@@ -97,20 +96,20 @@ function initmodel(settings::Dict{String, Any}) ...@@ -97,20 +96,20 @@ function initmodel(settings::Dict{String, Any})
settings["core.enddate"]) settings["core.enddate"])
crops = readcropparameters(settings["crop.cropfile"], crops = readcropparameters(settings["crop.cropfile"],
settings["crop.growthfile"]) settings["crop.growthfile"])
space = GridSpace(size(landscape), periodic=false) model = SimulationModel(settings,
properties = Dict{Symbol,Any}(:settings=>settings, StableRNG(settings["core.seed"]),
:logger=>logger, logger,
:date=>settings["core.startdate"], Vector{DataOutput}(),
:landscape=>landscape, Dict{String, DataFrame}(),
:weather=>weather, settings["core.startdate"],
:crops=>crops, landscape,
:migrants=>Vector{Pair{Animal, Date}}(), weather,
:dataoutputs=>Vector{DataOutput}(), crops,
:datatables=>Dict{String, DataFrame}(), Vector{Farmer}(),
:events=>Vector{FarmEvent}()) Vector{FarmPlot}(),
model = AgentBasedModel(Union{Farmer,Animal,FarmPlot}, Vector{Union{Animals,Nothing}}(),
space, properties=properties, Vector{Pair{Animal, Date}}(),
rng=StableRNG(settings["core.seed"]), warn=false) Vector{FarmEvent}())
saveinputfiles(model) saveinputfiles(model)
initfields!(model) initfields!(model)
initfarms!(model) initfarms!(model)
...@@ -156,19 +155,15 @@ function stepsimulation!(model::AgentBasedModel) ...@@ -156,19 +155,15 @@ function stepsimulation!(model::AgentBasedModel)
#TODO catch exceptions and print them to the log file #TODO catch exceptions and print them to the log file
with_logger(model.logger) do with_logger(model.logger) do
@info "Simulating day $(model.date)." @info "Simulating day $(model.date)."
#TODO remove Agents.jl-related code, reimplement this more cleanly (#72) #XXX move the two loops into the relevant submodels?
for a in Schedulers.ByType((Farmer,FarmPlot,Animal), true)(model) for f in model.farmers
try #The animal may have been killed stepagent!(f, model)
stepagent!(model[a], model) end
catch exc for p in model.farmplots
# check if the KeyError comes from the `model[a]` or the function call stepagent!(p, model)
#FIXME this also silences KeyErrors caused in the species code (e.g.
# by accessing dead mates) - will be fixed once I reorganise the code
isa(exc, KeyError) && isa(exc.key, Int) ? continue : throw(exc)
end
end end
updateevents!(model)
updatenature!(model) updatenature!(model)
updateevents!(model)
outputdata(model) outputdata(model)
model.date += Day(1) model.date += Day(1)
model model
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment