From 3aecf3f0e832697a6a2721b8712df356e240b129 Mon Sep 17 00:00:00 2001 From: Daniel Vedder <daniel.vedder@idiv.de> Date: Sat, 21 Jan 2023 15:07:21 +0100 Subject: [PATCH] Wrote simulation tests There's still at least one bug with the RNG --- test/io_tests.jl | 4 ++-- test/landscape_tests.jl | 4 ++-- test/nature_tests.jl | 9 +++++---- test/simulation_tests.jl | 29 ++++++++++++++++++++++++++++- test/test_parameters.toml | 6 +++--- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/test/io_tests.jl b/test/io_tests.jl index 475c1da..a9dd780 100644 --- a/test/io_tests.jl +++ b/test/io_tests.jl @@ -8,7 +8,7 @@ space = GridSpace((10,10), periodic=false) model = AgentBasedModel(Animal, space, properties=properties, warn=false) @test @param(core.configfile) == TESTPARAMETERS - @test @param(core.startdate) == Date(2022, 1, 1) + @test @param(core.startdate) == Date(2022, 2, 1) @test @param(nature.targetspecies) == ["Wolpertinger", "Wyvern"] @param(core.enddate) = Date(2022,1,3) @test @param(core.enddate) == Date(2022,1,3) @@ -22,7 +22,7 @@ end model = AgentBasedModel(Animal, space, properties=properties, warn=false) # test that the output directory is created with all files outdir = @param(core.outdir) - logstring = "Setting up output directory results_testsuite_$(Dates.today())_s1" + logstring = "Setting up output directory results_testsuite_$(Dates.today())_s1." @test_logs (:debug, logstring) min_level=Logging.Debug Ps.setupdatadir(model) @test isdir(outdir) @test isfile(joinpath(outdir, @param(core.landcovermap))) diff --git a/test/landscape_tests.jl b/test/landscape_tests.jl index 24f0481..b02ee20 100644 --- a/test/landscape_tests.jl +++ b/test/landscape_tests.jl @@ -61,11 +61,11 @@ end @test model.landscape[1,1].events == [Ps.tillage, Ps.sowing] @test model.landscape[2,1].events == [Ps.tillage] @test model.landscape[2,2].events == [Ps.sowing] - @test_logs (:info, "Simulating day 2022-01-01.") match_mode=:any stepsimulation!(model) + @test_logs (:info, "Simulating day 2022-02-01.") match_mode=:any stepsimulation!(model) @test model.landscape[1,1].events == [Ps.sowing] @test model.landscape[2,1].events == [] @test model.landscape[2,2].events == [Ps.sowing] - @test_logs (:info, "Simulating day 2022-01-02.") match_mode=:any stepsimulation!(model) + @test_logs (:info, "Simulating day 2022-02-02.") match_mode=:any stepsimulation!(model) @test model.landscape[1,1].events == [] @test model.landscape[2,1].events == [] @test model.landscape[2,2].events == [] diff --git a/test/nature_tests.jl b/test/nature_tests.jl index b8d4937..661ea57 100644 --- a/test/nature_tests.jl +++ b/test/nature_tests.jl @@ -68,8 +68,8 @@ end @testset "Species macros" begin # create a model landscape and a test species + #TODO test @rand model = smalltestlandscape(Union{Animal,Farmer,FarmPlot}) - @species Mermaid begin ageofmaturity = 2 pesticidemortality = 1.0 @@ -85,7 +85,8 @@ end end end @phase drought begin - @debug "$(Persephone.animalid(animal)) is trying to survive a drought." + n = sum(1 for a in @neighbours(0)) + @debug "$(Persephone.animalid(animal)) is experiencing drought with $n neighbour(s)." @respond Persephone.sowing @setphase(life) end end @@ -110,8 +111,8 @@ end @test model[1].age == 1 @test model[2].traits["phase"] == "drought" createevent!(model, [pond], Ps.sowing) - @test_logs((:debug, "Mermaid 1 is trying to survive a drought."), - (:debug, "Mermaid 2 is trying to survive a drought."), + @test_logs((:debug, "Mermaid 1 is experiencing drought with 1 neighbour(s)."), + (:debug, "Mermaid 2 is experiencing drought with 1 neighbour(s)."), min_level=Logging.Debug, match_mode=:any, stepsimulation!(model)) @test_logs((:debug, "Mermaid 1 is swimming happily in its pond."), diff --git a/test/simulation_tests.jl b/test/simulation_tests.jl index 4ef393c..d4a2d0b 100644 --- a/test/simulation_tests.jl +++ b/test/simulation_tests.jl @@ -3,4 +3,31 @@ ### These are the tests for the core simulation functions. ### -#TODO +@testset "Model initialisation" begin + model = initialise(TESTPARAMETERS) + @test typeof(model.settings) == Dict{String, Dict{String, Any}} + @test model.date == Date(2022,2,1) + @test typeof(model.landscape) == Matrix{Pixel} + @test typeof(model.dataoutputs) == Vector{DataOutput} + @test length(model.dataoutputs) == 2 + @test model.events == Vector{FarmEvent}() + @test nagents(model) == 2092+10+28 +end + +@testset "Model simulation" begin + Random.seed!(1) + @test isapprox(rand(), 0.07337, atol=0.0001) + model = simulate(TESTPARAMETERS, 218) + @test @param(core.seed) == 218 + @test model.date == Date(2022,4,1) + nwol = sum((typeof(a)==Animal && a.traits["name"]=="Wolpertinger") for a in allagents(model)) + nwyv = sum((typeof(a)==Animal && a.traits["name"]=="Wyvern") for a in allagents(model)) + #FIXME these still fail - although it might not be that clever to rely on random model outcomes + @test nwol == 32 + @test nwyv == 9 + # To retain reproducibility, the model code must never use the global RNG. + # If this next test fails, that requirement has probably been violated somewhere. + #FIXME it does fail... Might it be called by some dependency? + @test isapprox(rand(), 0.34924, atol=0.0001) +end + diff --git a/test/test_parameters.toml b/test/test_parameters.toml index 0e3e021..3e5e046 100644 --- a/test/test_parameters.toml +++ b/test/test_parameters.toml @@ -9,11 +9,11 @@ landcovermap = "landcover_jena.tif" # location of the landcover map farmfieldsmap = "fields_jena.tif" # location of the field geometry map outdir = "results_testsuite" # location and name of the output folder overwrite = true # overwrite the output directory? (true/false/"ask") -loglevel = "debug" # verbosity level: "debug", "info", "quiet" +loglevel = "quiet" # verbosity level: "debug", "info", "quiet" seed = 1 # seed value for the RNG (0 -> random value) # dates to start and end the simulation -startdate = 2022-01-01 -enddate = 2022-01-02 +startdate = 2022-02-01 +enddate = 2022-03-31 [farm] farmmodel = "FieldManager" # which version of the farm model to use (not yet implemented) -- GitLab