Skip to content
Snippets Groups Projects
Select Git revision
  • daba57f22287f3c4d89c140aff22ef97c9a78b9d
  • master default protected
  • taxon-oriented
  • marco/atkis-extract-vector-maps
  • development
  • phase-oriented
  • atkis
  • marco/atkis-skylark-speed-opt
  • marco/master-skylark-speed-opt
  • marco/atkis-fix-unit-tests
  • marco/aquacrop-fix-api-use
  • new-data
  • fix-almass-crop-model
  • precompile-statements
  • precompile-tools
  • tmp-faster-loading
  • skylark
  • testsuite
  • code-review
  • v0.8
  • v0.7.0
  • v0.6.1
  • v0.6.0
  • v0.5.5
  • v0.5.4
  • v0.5.3
  • v0.5.2
  • v0.2
  • v0.3.0
  • v0.4.1
  • v0.5
31 results

crops.md

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    simulation.jl 2.94 KiB
    ### Persephone - a socio-economic-ecological model of European agricultural landscapes.
    ###
    ### This file includes the core functions for initialising and running simulations.
    ###
    
    """
    The file that stores all default parameters.
    """
    const PARAMFILE = "src/parameters.toml"
    ## (DO NOT CHANGE THIS VALUE! Instead, specify simulation-specific configuration files
    ## by using the "--configfile" commandline argument, or when invoking simulate().) 
    
    """
        initialise(config=PARAMFILE, seed=nothing)
    
    Initialise the model: read in parameters, create the output data directory,
    and instantiate the AgentBasedModel object. Optionally allows specifying the
    configuration file and overriding the `seed` parameter.
    """
    function initialise(config::String=PARAMFILE, seed::Union{Int64,Nothing}=nothing)
        @info "Simulation run started at $(Dates.now())."
        settings = getsettings(config, seed)
        Random.seed!(settings["core"]["seed"])
        events = Vector{FarmEvent}()
        dataoutputs = Vector{DataOutput}()
        landscape = initlandscape(settings["core"]["landcovermap"], settings["core"]["farmfieldsmap"])
        space = GridSpace(size(landscape), periodic=false)
        properties = Dict{Symbol,Any}(:settings=>settings,
                                      :date=>settings["core"]["startdate"],
                                      :landscape=>landscape,
                                      :dataoutputs=>dataoutputs,
                                      :events=>events)
        @debug "Setting up model."
        model = AgentBasedModel(Union{Farmer,Animal,FarmPlot}, space, properties=properties,
                                rng=Random.Xoshiro(settings["core"]["seed"]), warn=false)
        setupdatadir(model)
        initfarms!(model)
        initfields!(model)
        initnature!(model)
        model
    end
    
    """
        stepsimulation!(model)
    
    Execute one update of the model.
    """
    function stepsimulation!(model::AgentBasedModel)
        @info "Simulating day $(model.date)."
        for a in Schedulers.ByType((Farmer,FarmPlot,Animal), true)(model)
            #The animal may have been killed, so we need a try/catch
            try stepagent!(model[a], model) catch keyerror end
        end
        updateevents!(model)
        outputdata(model)
        model.date += Day(1)
    end
    
    """
        finalise(model)
    
    Wrap up the simulation. Currently doesn't do anything except print some information.
    """
    function finalise(model::AgentBasedModel)
        @info "Simulated $(model.date-@param(core.startdate))."
        @info "Simulation run completed at $(Dates.now()),\nwrote output to $(@param(core.outdir))."
        #XXX is there anything to do here?
        #genocide!(model)
    end
    
    """
        simulate(config=PARAMFILE, seed=nothing)
    
    Carry out a complete simulation run, optionally specifying a configuration file
    and a seed for the RNG.
    """
    function simulate(config::String=PARAMFILE, seed::Union{Int64,Nothing}=nothing)
        model = initialise(config, seed)
        runtime = Dates.value(@param(core.enddate)-@param(core.startdate))+1
        step!(model, dummystep, stepsimulation!, runtime)
        finalise(model)
    end