ABMEv_runworld.jl 2.09 KB
Newer Older
1
"""
Victor's avatar
Victor committed
2
3
    function run!(w::World{A,S,T},alg::L,tend::Number;dt_saving=nothing,cb=(names = String[],agg =nothing))

4
5
Run `w` with algorithm `alg`, until `tend` is reached.
Returns a `Simulation` type.
Victor's avatar
Victor committed
6
- `worldall` stores the world every `p["dt_saving"]` time steps.
7
If `dt_saving` not specified, `sim` contains an array of two elements,
Victor's avatar
Victor committed
8
first corresponding to initial conditions and last corresponding to world in the last time step.
Victor's avatar
Victor committed
9
- `cb` correspond to callbacks function. Look at the documentation for more information
Victor's avatar
Victor committed
10
- the run stops if the number of agents reaches`p["NMax"]`.
11
"""
12
function run!(w::World{A,S,T},alg::L,tend::Number;
13
                dt_saving=nothing,
14
                cb=(names = String[],agg =nothing)) where {A,S,T,L<:AbstractAlg}
15
    _check_timedep(w.p)
Victor's avatar
Victor committed
16
    n=size(w);
Victor's avatar
Victor committed
17
    NMax = maxsize(w)
18
    t = .0
Victor's avatar
Victor committed
19
20
21
22
    i = 1;j=1;dt = 0.
    isnothing(dt_saving) ? dt_saving =  tend + 1. : nothing
    sim = Simulation(w,cb=cb)
    if A <: AbstractAgent{AA,Rates{true}} where {AA}
23
        update_rates!(w,alg)
Victor's avatar
Victor committed
24
25
    end
    while t<tend
26
27
        if dt < 0
            throw("We obtained negative time step dt = $dt at event $i")
Victor's avatar
Victor committed
28
        elseif size(w) == NMax
29
30
            @info "All individuals have died :("
            break
Victor's avatar
Victor committed
31
        elseif size(w) == 0
32
33
34
            @info "We have reached the maximum number of individuals allowed"
            break
        end
Victor's avatar
Victor committed
35
36
        if  t - get_tend(sim) >= dt_saving
            @info "saving world @ t = $(t)/ $(tend)"
37
            add_entry!(sim,w)
38
        end
39
        dt = updateWorld!(w,alg)
Victor's avatar
Victor committed
40
        t +=  dt
41
42
        i += 1
    end
Victor's avatar
Victor committed
43
    # Saving last time step
Victor's avatar
Victor committed
44
    add_entry!(sim,w)
Victor's avatar
Victor committed
45
    @info "simulation stopped at t=$(t), after $(i) generations"
Victor's avatar
Victor committed
46
    return sim
47
end
48
49

"""
Victor's avatar
Victor committed
50
    function _correct_timedep!(p::Dict)
51
52
53
54
55
56
57
58
59
60
61
62
63

checks time dependency of birth and death functions,
and overloads the function if not provided
"""
function _check_timedep(p::Dict)
    @unpack d,b = p
    if numargs(p["b"]) < 2
        throw(ArgumentError("Birth function needs `X` and `t` arguments"))
    end
    if numargs(p["d"]) < 3
        throw(ArgumentError("Death function needs `X`, `Y` and `t` arguments"))
    end
end