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

@testset "Model initialisation" begin
    model = initialise(TESTPARAMETERS)
    @test typeof(model.settings) == Dict{String, Any}
    @test model.date == Date(2022,2,1)
    @test typeof(model.landscape) == Matrix{Pixel}
    @test typeof(model.dataoutputs) == Vector{DataOutput}
    @test typeof(model.logger) == TeeLogger{Tuple{ConsoleLogger, ConsoleLogger}}
    @test length(model.dataoutputs) == 2
    @test model.events == Vector{FarmEvent}()
    @test nagents(model) == 2092+10+28
end

@testset "Parameter scanning" begin
    config = "paramscan.toml"
    testdirs = ["results_test_paramscan/seed_1_loglevel_warn_overwrite_ask",
                "results_test_paramscan/seed_1_loglevel_warn_overwrite_true",
                "results_test_paramscan/seed_2_loglevel_info_overwrite_true"]
    settings = Ps.getsettings(config)
    scanparams = settings["internal.scanparams"]
    @test length(scanparams) == 3
    @test sort(["core.overwrite", "core.loglevel", "core.seed"]) == sort(scanparams)
    scan = Ps.paramscan(settings, scanparams)
    outdirs = map(s -> s["core.outdir"], scan)
    @test length(outdirs) == 12
    #TODO while parallelisation doesn't work yet, initialising 12 models takes too
    # long to do every time the test suite is run. Therefore, I'm commenting out
    # some tests here, to be reinstated once parallelisation is implemented.
    #@test length(initialise(config)) == 12
    for dir in testdirs
        @test dir in outdirs
        #@test isdir(dir)
    end
    #rm("results_test_paramscan", force=true, recursive=true)
end

@testset "Model simulation" begin
    # The primary reason for this testset is to make sure that a complete
    # simulation will run through without errors. Thus, there are few tests.
    # Additionally, it makes sure that no part of the code uses the global
    # RNG, as this would compromise reproducibility. If one of the `rand()`
    # tests fail, that requirement has been violated somewhere.
    Random.seed!(1)
    rand1 = rand()
    Random.seed!(1)
    model = initialise(TESTPARAMETERS, 218)
    #XXX upstream problem with ArgParse (https://github.com/carlobaldassi/ArgParse.jl/issues/121) - should work again with Julia 1.10
    @test_broken rand() == rand1 
    Random.seed!(1)
    @test @param(core.seed) == 218
    @test_logs((:info, "Simulating day 2022-02-01."),
               (:info, "Simulated 59 days."),
               min_level=Logging.Debug, match_mode=:any,
               simulate!(Ps.withtestlogger(model)))
    @test model.date == Date(2022,4,1)
    @test rand() == rand1
end