Skip to content
Snippets Groups Projects
Commit f3a82bb7 authored by Marco Matthies's avatar Marco Matthies
Browse files

Rework `initfields!()`, remove `initfields_fill_with!()`

New `make_cropstate` helper function for switchable crop models.
parent 2efdb8ad
No related branches found
No related tags found
No related merge requests found
...@@ -14,9 +14,9 @@ using Persefone: ...@@ -14,9 +14,9 @@ using Persefone:
m, m,
SimulationModel, SimulationModel,
fertiliser, fertiliser,
initfields_fill_with!,
maxtemp, maxtemp,
mintemp mintemp
import Persefone: import Persefone:
stepagent!, stepagent!,
croptype, croptype,
...@@ -24,6 +24,7 @@ import Persefone: ...@@ -24,6 +24,7 @@ import Persefone:
cropheight, cropheight,
cropcover, cropcover,
cropyield cropyield
using Dates: Date, month, monthday using Dates: Date, month, monthday
using CSV: CSV using CSV: CSV
...@@ -166,22 +167,6 @@ function readcropparameters(generalcropfile::String, growthfile::String) ...@@ -166,22 +167,6 @@ function readcropparameters(generalcropfile::String, growthfile::String)
croptypes croptypes
end end
"""
initfields!(model)
Initialise the model with its farm plots.
"""
function initfields!(model::SimulationModel)
initfields_fill_with!(model) do model, x, y
month(model.date) < 3 ? phase = ALMaSS.janfirst : phase = ALMaSS.marchfirst
FarmPlot(length(model.farmplots) + 1, [(x,y)],
CropState(model.crops["natural grass"],
phase,
0.0, 0.0m, 0.0, 0.0, Vector{EventType}())
)
end
end
""" """
stepagent!(farmplot, model) stepagent!(farmplot, model)
......
### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe. ### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe.
### ###
### Crop model helper functions. ### Functions for switchable crop models.
### ###
function initcropmodel(cropmodel::AbstractString, cropfile::AbstractString, growthfile::AbstractString) function initcropmodel(cropmodel::AbstractString, cropfile::AbstractString, growthfile::AbstractString)
...@@ -19,12 +19,58 @@ function initcropmodel(cropmodel::AbstractString, cropfile::AbstractString, grow ...@@ -19,12 +19,58 @@ function initcropmodel(cropmodel::AbstractString, cropfile::AbstractString, grow
return crops, Tcroptype, Tcropstate return crops, Tcroptype, Tcropstate
end end
"""
initfields!(model)
Initialise the model with its farm plots.
"""
function initfields!(model::SimulationModel, cropmodel::AbstractString) function initfields!(model::SimulationModel, cropmodel::AbstractString)
n = 0
convertid = Dict{Int64,Int64}()
width, height = size(model.landscape)
for x in 1:width
for y in 1:height
# for each pixel, we need to extract the field ID given by the map input
# file, and convert it into the internal object ID used by Agents.jl,
# creating a new agent object if necessary
rawid = model.landscape[x,y].fieldid
(ismissing(rawid)) && continue
if rawid in keys(convertid)
objectid = convertid[rawid]
model.landscape[x,y].fieldid = objectid
push!(model.farmplots[objectid].pixels, (x,y))
else
cropstate = make_cropstate(model, cropmodel)
fp = FarmPlot(
length(model.farmplots) + 1,
[(x, y)],
cropstate
)
push!(model.farmplots, fp)
model.landscape[x,y].fieldid = fp.id
convertid[rawid] = fp.id
n += 1
end
end
end
@info "Initialised $n farm plots."
end
function make_cropstate(model::SimulationModel, cropmodel::AbstractString)
if cropmodel == "almass" if cropmodel == "almass"
ALMaSS.initfields!(model) phase = (month(model.date) < 3 ? ALMaSS.janfirst : ALMaSS.marchfirst)
cs = ALMaSS.CropState(
model.crops["natural grass"],
phase,
0.0, 0.0m, 0.0, 0.0, Vector{EventType}()
)
elseif cropmodel == "simple" elseif cropmodel == "simple"
SimpleCrop.initfields!(model) cs = SimpleCrop.CropState(
model.crops["natural grass"],
0.0m
)
else else
error("initfields! for crop model '$cropmodel'") error("Unhandled crop model '$cropmodel' in make_cropstate")
end end
return cs
end end
...@@ -15,40 +15,6 @@ cropheight(f::FarmPlot{T}) where {T} = cropheight(f.crop_state) ...@@ -15,40 +15,6 @@ cropheight(f::FarmPlot{T}) where {T} = cropheight(f.crop_state)
cropcover(f::FarmPlot{T}) where {T} = cropcover(f.crop_state) cropcover(f::FarmPlot{T}) where {T} = cropcover(f.crop_state)
cropyield(f::FarmPlot{T}) where {T} = cropyield(f.crop_state) cropyield(f::FarmPlot{T}) where {T} = cropyield(f.crop_state)
"""
initfields_fill_with!(make_farmplot_fn, model)
Initialise the model with its farm plots, using the
`make_farmplot_fn(model, x, y)` function to create new farmplots.
"""
function initfields_fill_with!(make_farmplot_fn::Function, model::SimulationModel)
n = 0
convertid = Dict{Int64,Int64}()
width, height = size(model.landscape)
for x in 1:width
for y in 1:height
# for each pixel, we need to extract the field ID given by the map input
# file, and convert it into the internal object ID used by Agents.jl,
# creating a new agent object if necessary
rawid = model.landscape[x,y].fieldid
(ismissing(rawid)) && continue
if rawid in keys(convertid)
objectid = convertid[rawid]
model.landscape[x,y].fieldid = objectid
push!(model.farmplots[objectid].pixels, (x,y))
else
fp = make_farmplot_fn(model, x, y)
push!(model.farmplots, fp)
model.landscape[x,y].fieldid = fp.id
convertid[rawid] = fp.id
n += 1
end
end
end
@info "Initialised $n farm plots."
end
## UTILITY FUNCTIONS ## UTILITY FUNCTIONS
""" """
......
...@@ -4,8 +4,8 @@ using Persefone: ...@@ -4,8 +4,8 @@ using Persefone:
FarmPlot, FarmPlot,
Length, Length,
m, m,
SimulationModel, SimulationModel
initfields_fill_with!
import Persefone: import Persefone:
stepagent!, stepagent!,
croptype, croptype,
...@@ -41,15 +41,4 @@ function stepagent!(farmplot::FarmPlot{CropState}, model::SimulationModel) ...@@ -41,15 +41,4 @@ function stepagent!(farmplot::FarmPlot{CropState}, model::SimulationModel)
# TODO: do something simple # TODO: do something simple
end end
"""
initfields!(model)
Initialise the model with its farm plots.
"""
function initfields!(model::SimulationModel)
initfields_fill_with!(model) do model, x, y
FarmPlot(length(model.farmplots) + 1, [(x,y)], CropState(model.crops["natural grass"], 0.0m))
end
end
end # module SimpleCrop end # module SimpleCrop
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
@testset "Landscape initialisation" begin @testset "Landscape initialisation" begin
model = inittestmodel(false) model = inittestmodel(false)
# these tests are specific to the Jena maps # these tests are specific to the Jena maps
@test_logs (:info, "Initialised 2092 farm plots.") match_mode=:any Ps.ALMaSS.initfields!(model) cropmodel = "almass"
@test_logs (:info, "Initialised 2092 farm plots.") match_mode=:any Ps.initfields!(model, cropmodel)
@test size(model.landscape) == (1754, 1602) @test size(model.landscape) == (1754, 1602)
@test Ps.landcover((100,100), model) == Ps.forest @test Ps.landcover((100,100), model) == Ps.forest
@test Ps.landcover((300,1), model) == Ps.soil @test Ps.landcover((300,1), model) == Ps.soil
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment