Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClusterTimeSimulation #19

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ version = "0.1.0"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DocumentFormat = "ffa9a821-9c82-50df-894e-fbcef3ed31cd"
GeometryTypes = "4d00f742-c7ba-57c2-abde-4428a4b178cb"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand Down
41 changes: 19 additions & 22 deletions scripts/bench_init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,29 @@ using Flase
using BenchmarkTools

println("gridsize, fraction, time, bytes")
for gridsize in (10, 20, 100, 200, 400, 800, #=1600, 2000=#)
for gridsize in (10, 20, 100, 200, 400, 800) #=1600, 2000=#
for sheep_ration in (0.1, 0.5, 0.9)
t = @timed begin
v0 = 5.0
Dϕ = 4.0
world = World(
v0 = 1.,
n_dogs = 9,
boxsize = float(gridsize),
freedom_rate = 10.0,
business_rate = 10.0,
meanSleepyness = 10.0,
motion = BrownianMotion(
noise = v0^4 / Dϕ,
friction = Dϕ / v0^2
v0 = 5.0
Dϕ = 4.0
world = World(
v0 = 1.0,
n_dogs = 9,
boxsize = float(gridsize),
freedom_rate = 10.0,
business_rate = 10.0,
meanSleepyness = 10.0,
motion = BrownianMotion(noise = v0^4 / Dϕ, friction = Dϕ / v0^2),
sheeps = Flase.SparseSheeps(
gridsize, # gridsize
n_sheeps = sheep_ration * gridsize^2,
),
sheeps = Flase.SparseSheeps(
gridsize, # gridsize
n_sheeps = sheep_ration * gridsize^2,
)
)
simulation = FiniteSimulation(;
dt = 0.1,
end_time = 100.0,
world = world,
plotter = Flase.VoidPlotter()
simulation = FiniteSimulation(;
dt = 0.1,
end_time = 100.0,
world = world,
plotter = Flase.VoidPlotter(),
)
end
println("$gridsize, $sheep_ration, $(t.time), $(t.bytes)")
Expand Down
45 changes: 21 additions & 24 deletions scripts/bench_runsim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,31 @@ using Flase
using BenchmarkTools

@time begin
v0 = 5.0
Dϕ = 4.0
gridsize = 200
frac = 0.9
world = World(
v0 = 1.,
n_dogs = 9,
boxsize = float(gridsize),
freedom_rate = 10.0,
business_rate = 10.0,
meanSleepyness = 10.0,
motion = BrownianMotion(
noise = v0^4 / Dϕ,
friction = Dϕ / v0^2
v0 = 5.0
Dϕ = 4.0
gridsize = 200
frac = 0.9
world = World(
v0 = 1.0,
n_dogs = 9,
boxsize = float(gridsize),
freedom_rate = 10.0,
business_rate = 10.0,
meanSleepyness = 10.0,
motion = BrownianMotion(noise = v0^4 / Dϕ, friction = Dϕ / v0^2),
sheeps = Flase.BaseSheeps(
gridsize, # gridsize
n_sheeps = frac * gridsize^2,
),
sheeps = Flase.BaseSheeps(
gridsize, # gridsize
n_sheeps = frac * gridsize^2,
)
)
simulation = FiniteSimulation(;
dt = 0.1,
end_time = 100.0,
world = world,
plotter = Flase.VoidPlotter()
simulation = FiniteSimulation(;
dt = 0.1,
end_time = 100.0,
world = world,
plotter = Flase.VoidPlotter(),
)
end
t = @benchmark begin
Flase.runsim( simulation )
Flase.runsim(simulation)
end
display(t)
17 changes: 5 additions & 12 deletions scripts/movingDogs.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
using Flase

world = World(
v0 = 1.,
v0 = 1.0,
n_dogs = 120,
boxsize = 10.0,
motion = BrownianMotion(
noise = 0.5,
friction = 1.0
)
)
simulation = InfiniteSimulation(;
dt = 0.05,
world = world,
plotter = UnicodePlotter()
)
Flase.runsim( simulation )
motion = BrownianMotion(noise = 0.5, friction = 1.0),
)
simulation = InfiniteSimulation(; dt = 0.05, world = world, plotter = UnicodePlotter())
Flase.runsim(simulation)
45 changes: 21 additions & 24 deletions scripts/movingDogs_staticSheeps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,30 @@ using Flase
using BenchmarkTools

@time begin
v0 = 5.0
Dϕ = 4.0
gridsize = 10
frac = 0.1
world = World(
v0 = 1.,
n_dogs = 9,
boxsize = float(gridsize),
freedom_rate = 10.0,
business_rate = 10.0,
meanSleepyness = 10.0,
motion = BrownianMotion(
noise = v0^4 / Dϕ,
friction = Dϕ / v0^2
v0 = 5.0
Dϕ = 4.0
gridsize = 10
frac = 0.1
world = World(
v0 = 1.0,
n_dogs = 9,
boxsize = float(gridsize),
freedom_rate = 10.0,
business_rate = 10.0,
meanSleepyness = 10.0,
motion = BrownianMotion(noise = v0^4 / Dϕ, friction = Dϕ / v0^2),
sheeps = DenseSheeps(
gridsize, # gridsize
n_sheeps = frac * gridsize^2,
),
sheeps = DenseSheeps(
gridsize, # gridsize
n_sheeps = frac * gridsize^2,
)
)
simulation = FiniteSimulation(;
dt = 0.1,
end_time = 100.0,
world = world,
plotter = Flase.VoidPlotter()
simulation = FiniteSimulation(;
dt = 0.1,
end_time = 100.0,
world = world,
plotter = Flase.VoidPlotter(),
)
end
@benchmark begin
Flase.runsim( simulation )
Flase.runsim(simulation)
end samples = 1 evals = 1 gcsample = true
14 changes: 11 additions & 3 deletions src/measures/MeanQuadraticDistance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

#first we need a new structure

struct MQD
Base.@kwdef struct MQD
mqdNorm::Float64
end

MQD() = MQD(0)
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved

function MQD(sheeps::Sheeps)
mqdNorm = measure(MQD(), sheeps)
return mqdNorm
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
end

#then compute the function that takes imput from object sheep::Sheeps
Expand Down Expand Up @@ -55,6 +63,6 @@ end


function getMQD(mqd::MQD, sheeps::Sheeps, mqdNorm::Float64)
mqd_value = measure(mqd, sheeps)/mqdNorm
mqd_value = measure(mqd, sheeps) / mqdNorm
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
return mqd_value
end
end
9 changes: 5 additions & 4 deletions src/measures/MeanSquaredDisplacement.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"Type for calculating the mean squared displacement."
struct MSD
end
struct MSD end

function measure(::MSD, sheeps::Sheeps, world::World)
# get center of mass
Expand All @@ -14,7 +13,9 @@ function measure(::MSD, sheeps::Sheeps, world::World)
end

function getClusterRadius(sheeps::Sheeps, world::World)
clusterRadius = sqrt(sheeps.current_sheep[] / sheeps.capacity / π) * world.boxsize[] / size(sheeps.grid)[1]
clusterRadius =
sqrt(sheeps.current_sheep[] / sheeps.capacity / π) * world.boxsize[] /
size(sheeps.grid)[1]
return clusterRadius
end

Expand All @@ -24,4 +25,4 @@ function getMSD(msd::MSD, sheeps::Sheeps, world::World)
msdNorm = (R_cl * R_cl) / 2 + R_cl * R_item
msd_value = msdNorm / measure(msd, sheeps, world)
return msd_value
end
end
110 changes: 60 additions & 50 deletions src/simulations/ClusterTimeSimulation.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Distributions

struct ClusterTimeSimulation{P<:Plotter, F<:Number, W<:World } <: Simulation
struct ClusterTimeSimulation{P<:Plotter,F<:Number,W<:World} <: Simulation

time::Base.RefValue{F}
t_sheep_boredom::Base.RefValue{F}
Expand All @@ -11,67 +11,77 @@ struct ClusterTimeSimulation{P<:Plotter, F<:Number, W<:World } <: Simulation
world::W
plotter::P

end
end

ClusterTimeSimulation(;

msdThreshold::Float64 = 0.7,
mqdThreshold::Float64 = 0.1,
dt::F,
world::W,
condition::Int64,
time::F = convert(typeof(dt), 0) ,
t_sheep_boredom = rand(Exponential(world.meanSheepDiffusionTime / world.sheeps.current_sheep[])),
plotter::P = UnicodePlotter()
) where {F<:Number, W<:World,P<:Plotter} =
ClusterTimeSimulation{P,F,W}(Ref(time), Ref(t_sheep_boredom), condition, msdThreshold, mqdThreshold, dt, world, plotter)
time::F = convert(typeof(dt), 0),
t_sheep_boredom = rand(
Exponential(world.meanSheepDiffusionTime / world.sheeps.current_sheep[]),
),
plotter::P = UnicodePlotter(),
) where {F<:Number,W<:World,P<:Plotter} = ClusterTimeSimulation{P,F,W}(
Ref(time),
Ref(t_sheep_boredom),
condition,
msdThreshold,
mqdThreshold,
dt,
world,
plotter,
)


function runsim(sim::ClusterTimeSimulation)
mqd = MQD()
msd = MSD()
mqdNorm = measure(mqd, sim.world.sheeps)
mqd_value = getMQD(mqd, sim.world.sheeps, mqdNorm)
msd_value = getMSD(msd, sim.world.sheeps, sim.world)

p = plot(sim.plotter, sim.world, sim.time[])
io = IOBuffer()
if sim.condition == 0
while msd_value < sim.msdThreshold

println("I'm in 0")
measure(msd, sim.world.sheeps, sim.world)
iterate!(sim)
plot!(io, p, sim.plotter, sim.world, sim.time[])
msd_value = getMSD(msd, sim.world.sheeps, sim.world)

end
elseif sim.condition == 1
while mqd_value > sim.mqdThreshold

println("I'm in 1")
measure(mqd, sim.world.sheeps)
iterate!(sim)
plot!(io, p, sim.plotter, sim.world, sim.time[])
mqd_value = getMQD(mqd, sim.world.sheeps, mqdNorm)
mqd = MQD()
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
msd = MSD()
mqdNorm = MQD(sim.world.sheeps)
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
mqd_value = getMQD(mqd, sim.world.sheeps, mqdNorm)
msd_value = getMSD(msd, sim.world.sheeps, sim.world)

p = plot(sim.plotter, sim.world, sim.time[])
io = IOBuffer()
if sim.condition == 0
while msd_value < sim.msdThreshold

println("I'm in 0")
measure(msd, sim.world.sheeps, sim.world)
iterate!(sim)
plot!(io, p, sim.plotter, sim.world, sim.time[])
msd_value = getMSD(msd, sim.world.sheeps, sim.world)

end
elseif sim.condition == 1
while mqd_value > sim.mqdThreshold

println("I'm in 1")
measure(mqd, sim.world.sheeps)
iterate!(sim)
plot!(io, p, sim.plotter, sim.world, sim.time[])
mqd_value = getMQD(mqd, sim.world.sheeps, mqdNorm)
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved

end
elseif sim.condition == 2
while msd_value < sim.msdThreshold && mqd_value > sim.mqdThreshold

println("I'm in 2")
measure(mqd, sim.world.sheeps)
iterate!(sim)
plot!(io, p, sim.plotter, sim.world, sim.time[])
mqd_value = getMQD(mqd, sim.world.sheeps, mqdNorm)
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
msd_value = getMSD(msd, sim.world.sheeps, sim.world)

end
else
sim.condition == 3
error("Check breaking condition! It is $condition right now.")

end
elseif sim.condition == 2
while msd_value < sim.msdThreshold && mqd_value > sim.mqdThreshold

println("I'm in 2")
measure(mqd, sim.world.sheeps)
iterate!(sim)
plot!(io, p, sim.plotter, sim.world, sim.time[])
mqd_value = getMQD(mqd, sim.world.sheeps, mqdNorm)
msd_value = getMSD(msd, sim.world.sheeps, sim.world)

end
else sim.condition == 3
error("Check breaking condition! It is $condition right now.")

end

return sim.time[]
return sim.time[]

end
Loading