diff --git a/test.sh b/run.sh similarity index 100% rename from test.sh rename to run.sh diff --git a/src/Persephone.jl b/src/Persephone.jl index 2470a0c253c1406ef1c41225519565d393e3b5ff..5ab364d48cfd30a2d10e34e516e8935dc6d52b41 100644 --- a/src/Persephone.jl +++ b/src/Persephone.jl @@ -23,6 +23,7 @@ using TOML ## define exported functions and variables +#XXX export all types? export param, simulate, diff --git a/src/core/simulation.jl b/src/core/simulation.jl index 6440f5792126fba23de45fa625b6451b07ed02c6..b94b8d69aea70865b0780830d664eaa5977f2eb5 100644 --- a/src/core/simulation.jl +++ b/src/core/simulation.jl @@ -22,7 +22,7 @@ function initialise(config::String=PARAMFILE) properties = Dict{Symbol,Any}(:date=>param("core.startdate"), :landscape=>landscape, :events=>events) - @debug "Setting up model now" + @debug "Setting up model." model = AgentBasedModel(Union{Farmer,Animal,FarmPlot}, space, properties=properties, rng=Random.Xoshiro(param("core.seed")), warn=false) # initialise submodels diff --git a/src/crop/crops.jl b/src/crop/crops.jl index fd1bef711e2d949804a9c1c6e1258716d98e39fe..ec4520fb6d71aff653bafd6e6870ad91d72153a2 100644 --- a/src/crop/crops.jl +++ b/src/crop/crops.jl @@ -59,7 +59,7 @@ function initfields!(model::AgentBasedModel) @debug "Initialised $n farm plots." end -#XXX only needed during development, can be deleted again +#XXX only needed during development, can be deleted again? function countfields(model::AgentBasedModel) ids::Vector{Int64} = [] width, height = size(model.landscape) diff --git a/test/io_tests.jl b/test/io_tests.jl new file mode 100644 index 0000000000000000000000000000000000000000..c7d10ceba58f7060c186f226a34e0c0fb71427df --- /dev/null +++ b/test/io_tests.jl @@ -0,0 +1,6 @@ +### Persephone - a socio-economic-ecological model of European agricultural landscapes. +### +### This tests the core input and output functions. +### + +#TODO diff --git a/test/landscape_tests.jl b/test/landscape_tests.jl index 308603c3ca217d2027c7c0b108ec538c5b8c350b..be848eabcd10aaabd70d8c9203c240456614d6d3 100644 --- a/test/landscape_tests.jl +++ b/test/landscape_tests.jl @@ -3,4 +3,50 @@ ### These are the tests for the core landscape functions. ### -#TODO +@testset "Landscape initialisation" begin + # initialise the landscape part of the model + landscape = Persephone.initlandscape() + space = GridSpace(size(landscape), periodic=false) + properties = Dict{Symbol,Any}(:landscape=>landscape) + model = AgentBasedModel(Persephone.FarmPlot, space, properties=properties, + rng=Random.Xoshiro(param("core.seed")), warn=false) + Persephone.initfields!(model) + # these tests are specific to the Jena maps + @test size(model.landscape) == (1754, 1602) + @test Persephone.landcover(model, (100,100)) == Persephone.forest + @test Persephone.landcover(model, (300,1)) == Persephone.soil + @test Persephone.landcover(model, (500,1)) == Persephone.nodata + @test Persephone.landcover(model, (400,400)) == Persephone.grass + @test Persephone.landcover(model, (800,800)) == Persephone.agriculture + @test Persephone.landcover(model, (1100,1100)) == Persephone.builtup + @test Persephone.countfields(model) == 2092 + @test Persephone.averagefieldsize(model) == 5.37 + @test count(f -> ismissing(f.fieldid), model.landscape) == 1685573 + @test length(Persephone.farmplot(model, (800,800)).pixels) == 4049 +end + +@testset "Event system" begin + # initialise a basic model landscape + landscape = Persephone.initlandscape() + space = GridSpace(size(landscape), periodic=false) + properties = Dict{Symbol,Any}(:date=>param("core.startdate"), + :landscape=>landscape, + :events=>Vector{Persephone.FarmEvent}()) + model = AgentBasedModel(Union{Persephone.Farmer,Persephone.Animal,Persephone.FarmPlot}, + space, properties=properties, + rng=Random.Xoshiro(param("core.seed")), warn=false) + # create some events and see whether they show up on the map and disappear as they should + Persephone.createevent!(model, [(1,1), (1,2), (1,3), (2,1), (2,3)], Persephone.tillage) + Persephone.createevent!(model, [(1,1), (1,2), (1,3), (2,2)], Persephone.sowing, 2) + @test model.landscape[1,1].events == [Persephone.tillage, Persephone.sowing] + @test model.landscape[2,1].events == [Persephone.tillage] + @test model.landscape[2,2].events == [Persephone.sowing] + stepsimulation!(model) + @test model.landscape[1,1].events == [Persephone.sowing] + @test model.landscape[2,1].events == [] + @test model.landscape[2,2].events == [Persephone.sowing] + stepsimulation!(model) + @test model.landscape[1,1].events == [] + @test model.landscape[2,1].events == [] + @test model.landscape[2,2].events == [] +end diff --git a/test/runtests.jl b/test/runtests.jl old mode 100755 new mode 100644 index bd92cde6651f6c39c24eed61ff8291c9c7f8d20c..5af8e83a7c9962701d96ab17d9fedbf11b2e448c --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,9 +8,13 @@ Pkg.activate("..") using Persephone using Test +using Random +using Agents @testset "Persephone Tests" begin + Persephone.initsettings("test_parameters.toml") @testset "Core model" begin + include("io_tests.jl") include("landscape_tests.jl") include("simulation_tests.jl") end @@ -23,4 +27,5 @@ using Test @testset "Farm model" begin include("farm_tests.jl") end + rm(param("core.outdir"), force=true, recursive=true) end