simulation.jl 1.91 KB
Newer Older
1
2
3
4
5
6
using LightGraphs
using Test
using Revise,ABMEv
using UnPack
myspace = (GraphSpace(SimpleGraph(10,10)),RealSpace{1,Float64}())
myagents = [Agent(myspace,ancestors=true,rates=true) for i in 1:10]
7
8
d(X,Y,t) = gaussian(X[1],Y[1],1)
b(X,Y,t) = gaussian(X[1],0,1)
9
10
11
12
D = (Int64(1),Float64(1.))
mu = [1,1]
NMax = 100
p = Dict{String,Any}();@pack! p = d,b,D,mu,NMax
Victor's avatar
Victor committed
13
w0 = World(myagents,myspace,p,0.)
14

Victor's avatar
Victor committed
15
16
17
18
19
20
21
22
23
24
25
26
27
# Basic test
s = Simulation(w0)
typeof(s)
@test get_tend(s)  0.
@test get_size(s)  1

# adding an entry to sim
newa = give_birth(1,w0)
addAgent!(w0,newa)
@test isnothing(add_entry!(s,w0))
@test get_size(s) == 2

#TODO: try with callbacks
28
29
30
31
32
33
34
35
###################################
#######CALLBACKS###################
###################################

myspace = (RealSpace{1,Float64}(),)
sigma_K = .9;
sigma_a = .7;
K0 = 1000;
36
37
b(X,t) = gaussian(X[1],0.,sigma_K)
d(X,Y,t) = gaussian(X[1],Y[1],sigma_a)/K0
38
39
40
41
D = (1e-2,)
mu = [.1]
NMax = 10000
tend = 1.5
Victor's avatar
Victor committed
42
p = Dict{String,Any}();@pack! p = D,mu,NMax
43

44
## testing cb
45
myagents = [Agent(myspace,[0.],ancestors=true,rates=true) for i in 1:K0]
46
47
48
49
w0 = World(myagents,myspace,p,0.)
w1 = copy(w0)
cb = (names = ["gamma_div"], agg = Function[w -> var(Float64.(get_x(w,1)))])
eltype(cb.agg)
Victor's avatar
Victor committed
50
@time sim = run!(w1,Gillepsie(),tend,b,d,cb=cb,dt_saving = .1)
51

52
@test typeof(sim["gamma_div"]) <: Vector
53

54
55
56
@test typeof(get_world(sim,get_size(sim))) <: World
@test typeof(sim[2]) <: Vector

57
##testing t_saving_cb
58
myagents = [Agent(myspace,[0.],ancestors=true,rates=true) for i in 1:K0]
59
60
61
62
63
64
65
66
67
68
69
70
w0 = World(myagents,myspace,p,0.)
w1 = copy(w0)
cb = (names = ["gamma_div"], agg = Function[w -> var(Float64.(get_x(w,1)))])
eltype(cb.agg)
@time sim = run!(w1,Gillepsie(),tend,b,d,cb=cb,t_saving_cb = collect(1.0:0.1:1.5))
@test typeof(sim["gamma_div"]) <: Vector
@test get_size(sim) == length(sim["gamma_div"])
@test length(sim[1]) == 1000
@test length(sim[end]) > 1
@test length(sim[2]) == 1


71
## testing plot
72
73
using Plots
plot(get_tspan(sim),sim["gamma_div"])