Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
landscape_tests.jl 3.31 KiB
### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe.
###
### These are the tests for the landscape and weather functions.
###

@testset "Landscape initialisation" begin
    model = inittestmodel(false)
    # these tests are specific to the Jena maps
    @test_logs (:info, "Initialised 2092 farm plots.") match_mode=:any Ps.initfields!(model)
    @test size(model.landscape) == (1754, 1602)
    @test Ps.landcover((100,100), model) == Ps.forest
    @test Ps.landcover((300,1), model) == Ps.soil
    @test Ps.landcover((500,1), model) == Ps.nodata
    @test Ps.landcover((400,400), model) == Ps.grass
    @test Ps.landcover((800,800), model) == Ps.agriculture
    @test Ps.landcover((1100,1100), model) == Ps.builtup
    @test Ps.averagefieldsize(model) == 5.37
    @test count(f -> ismissing(f.fieldid), model.landscape) == 1685573
    @test length(Ps.farmplot((800,800), model).pixels) == 4049               
end

@testset "Event system" begin
    model = inittestmodel()
    createevent!(model, [(1,1), (1,2), (1,3), (2,1), (2,3)], Ps.tillage)
    createevent!(model, [(1,1), (1,2), (1,3), (2,2)], Ps.sowing, 2)
    @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-02-01."),
               match_mode=:any,
               stepsimulation!(Ps.withtestlogger(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-02-02."),
               match_mode=:any,
               stepsimulation!(Ps.withtestlogger(model)))
    @test model.landscape[1,1].events == []
    @test model.landscape[2,1].events == []
    @test model.landscape[2,2].events == []
end

@testset "Landscape functions" begin
    model = inittestmodel()
    @test Ps.directionto((2,3), model, Ps.agriculture) == (0,0)
    @test Ps.directionto((2,3), model, Ps.forest) == (-1,2)
    @test Ps.directionto((2,3), model, Ps.grass) == (1,2)
    @test Ps.directionto((2,3), model, Ps.water) == (4,1)
    @test Ps.directionto((2,3), model, Ps.soil) == nothing
    @test Ps.distanceto((2,3), model, Ps.agriculture) == 0m
    @test Ps.distanceto((2,3), model, Ps.forest) == 20m
    @test Ps.distanceto((2,3), model, Ps.grass) == 20m
    @test Ps.distanceto((2,3), model, Ps.water) == 40m
    @test Ps.distanceto((2,3), model, Ps.soil) == Inf
    @test Ps.distancetoedge((1,1), model) == 40m
    @test Ps.distancetoedge((4,4), model) == 10m
    @test Ps.distancetoedge((6,6), model) == 20m
end

@testset "Weather initialisation" begin
    # these tests are specific to the Jena weather file
    model = inittestmodel()
    @test_logs((:warn, "There are missing days in the weather input file."),
               Ps.initweather(TESTSETTINGS["world.weatherfile"],
                              Date("2022-01-01"), Date("2023-12-31")))
    @test length(keys(model.weather)) == 59
    @test ismissing(Ps.windspeed(model))
    @test Ps.precipitation(model) == 1.3
    @test ismissing(Ps.sunshine(model))
    stepsimulation!(model)
    @test Ps.meantemp(model) == 5.1
    @test Ps.maxtemp(model) == 7.5
    @test Ps.mintemp(model) == 0.1
    stepsimulation!(model)
    @test Ps.vapourpressure(model) == 7.7
end