Skip to content

Commit

Permalink
update examples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mastrof committed Dec 21, 2023
1 parent 499cc2c commit 67e9947
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 55 deletions.
15 changes: 8 additions & 7 deletions examples/Analysis/velocity_autocorrelations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ space = ContinuousSpace((L,L,L))

model = StandardABM(Microbe{3}, space, Δt; container=Vector)
n = 200
for i in 1:n
add_agent!(model; turn_rate, motility=RunTumble(speed=[U]))
add_agent!(model; turn_rate, motility=RunReverse(speed_forward=[U]))
add_agent!(model; turn_rate, motility=RunReverseFlick(speed_forward=[U]))
for Motility in (RunTumble, RunReverse, RunReverseFlick), i in 1:n
add_agent!(model; turn_rate, motility=Motility(speed=[U]))
end
ids_runtumble = 1:n
ids_runreverse = (1:n) .+ n
ids_runrevflick = (1:n) .+ 2n

nsteps = round(Int, 100τ_run / Δt)
adata = [:vel]
adf, = run!(model, nsteps; adata)

adf_runtumble = filter(:id => id -> model[id].motility isa RunTumble, adf; view=true)
adf_runrev = filter(:id => id -> model[id].motility isa RunReverse, adf; view=true)
adf_runrevflick = filter(:id => id -> model[id].motility isa RunReverseFlick, adf; view=true)
adf_runtumble = filter(:id => id -> id in ids_runtumble, adf; view=true)
adf_runrev = filter(:id => id -> id in ids_runreverse, adf; view=true)
adf_runrevflick = filter(:id => id -> id in ids_runrevflick, adf; view=true)
adfs = [adf_runtumble, adf_runrev, adf_runrevflick]

t = range(0, (nsteps-1)*Δt; step=Δt)
Expand Down
32 changes: 16 additions & 16 deletions test/model-creation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,23 @@ using LinearAlgebra: norm
rng = Xoshiro(123)
pos = rand(rng, SVector{D})
vel = random_velocity(rng, D)
speed = random_speed(rng, RunTumble())
#speed = random_speed(rng, RunTumble())
spd = rand(rng, speed(RunTumble()))
@test model[1] isa Microbe{D}
@test model[1].pos == pos
@test model[1].vel == vel
@test model[1].speed == speed
@test model[1].motility isa RunTumble
@test model[1].turn_rate == 1.0
@test model[1].radius == 0.0
@test model[1].rotational_diffusivity == 0.0
@test model[1].state == 0.0
@test position(model[1]) == pos
@test direction(model[1]) == vel
@test speed(model[1]) == spd
@test turnrate(model[1]) == 1.0
@test radius(model[1]) == 0.0
@test rotational_diffusivity(model[1]) == 0.0
@test state(model[1]) == 0.0
# add agent with kwproperties
motility = RunReverse(
speed_backward = [24.0],
motile_state = MotileState(Backward)
)
add_agent!(model; turn_rate = 0.55, motility)
@test model[2].turn_rate == 0.55
@test model[2].motility isa RunReverse
@test model[2].speed == 24.0
# add agent with predefined position
pos = SVector{D}(i/2D for i in 1:D)
Expand All @@ -63,21 +62,22 @@ using LinearAlgebra: norm
add_agent!(model)
m = model[1]
@test m isa T{D}
@test m.pos == rand(rng, SVector{D})
@test m.vel == random_velocity(rng, D)
@test m.speed == random_speed(rng, m.motility)
@test position(m) == rand(rng, SVector{D})
@test direction(m) == random_velocity(rng, D)
@test speed(m) == rand(rng, speed(motilepattern(m)))
@test issubset(
(:id, :pos, :vel, :speed, :motility,
:rotational_diffusivity, :radius, :state),
fieldnames(T)
)

φ(microbe, model) = turnrate(microbe) * cwbias(microbe, model)
if T == BrownBerg
@test turnrate(m, model) == m.turn_rate * exp(-m.gain*m.state)
@test φ(m, model) == m.turn_rate * exp(-m.gain*m.state)
elseif T == Brumley
@test turnrate(m, model) == (1+exp(-m.gain*m.state))*m.turn_rate/2
@test φ(m, model) == (1+exp(-m.gain*m.state))*m.turn_rate/2
elseif T == Celani
@test turnrate(m, model) == m.turn_rate * (1 - m.gain*m.state)
@test φ(m, model) == m.turn_rate * (1 - m.gain*m.state)
# when no concentration field is set, markovian variables are zero
@test m.markovian_variables == zeros(3)

Expand Down
4 changes: 2 additions & 2 deletions test/model-stepping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ using LinearAlgebra: norm
model = StandardABM(Microbe{D}, space, dt; rng, container)
pos = extent ./ 2
vel1 = random_velocity(model)
speed1 = random_speed(rng, RunTumble())
speed1 = rand(rng, speed(RunTumble()))
add_agent!(pos, model; vel=vel1, speed=speed1, turn_rate=0)
vel2 = random_velocity(model)
speed2 = random_speed(rng, RunReverse())
speed2 = rand(rng, speed(RunReverse()))
add_agent!(pos, model; vel=vel2, speed=speed2, turn_rate=Inf, motility=RunReverse())
run!(model, 1) # performs 1 microbe_step!
# x₁ = x₀ + vΔt
Expand Down
56 changes: 26 additions & 30 deletions test/motility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ using Distributions
using LinearAlgebra: norm

@testset "Motility" begin
@test AbstractMotilityOneStep <: AbstractMotility
@test AbstractMotilityTwoStep <: AbstractMotility
@test ~(AbstractMotilityOneStep <: AbstractMotilityTwoStep)
@test MotilityOneStep <: AbstractMotility
@test MotilityTwoStep <: AbstractMotility
@test !(MotilityOneStep <: MotilityTwoStep)

@test fieldnames(RunTumble) == (:speed, :polar, :azimuthal)
@test fieldnames(MotilityOneStep) == (:speed, :polar, :azimuthal)
rt = RunTumble()
# type hierarchy
@test rt isa AbstractMotilityOneStep
@test rt isa MotilityOneStep
# default values
@test rt.speed == (30.0,)
@test rt.polar == Uniform(-π, π)
Expand All @@ -20,54 +20,50 @@ using LinearAlgebra: norm
@test rt.speed == Normal(5, 0.1)
@test rt.polar == [-0.1, 0.1]
@test rt.azimuthal == (π,)
# base constructor without keywords
rt2 = RunTumble(Normal(5,0.1), [-0.1, 0.1], (π,))
@test rt2.speed == rt.speed && rt2.polar == rt.polar && rt2.azimuthal == rt.azimuthal

@test fieldnames(RunReverse) == (
:speed_forward, :polar_forward, :azimuthal_forward,
@test fieldnames(MotilityTwoStep) == (
:speed, :polar, :azimuthal,
:speed_backward, :polar_backward, :azimuthal_backward,
:motile_state
)
# default values
rr = RunReverse()
@test rr isa AbstractMotilityTwoStep
@test rr.speed_forward == (30.0,)
@test rr.polar_forward == (π,)
@test rr.azimuthal_forward == Arccos()
@test rr.speed_backward == rr.speed_forward
@test rr.polar_backward == rr.polar_forward
@test rr.azimuthal_backward == rr.azimuthal_forward
@test rr isa MotilityTwoStep
@test rr.speed == (30.0,)
@test rr.polar == (π,)
@test rr.azimuthal == Arccos()
@test rr.speed_backward == rr.speed
@test rr.polar_backward == rr.polar
@test rr.azimuthal_backward == rr.azimuthal
@test rr.motile_state.state == Forward
# field overload
@test rr.state == Forward
# keywords
rr = RunReverse(; azimuthal_backward = (-π/4,0/4))
@test rr.azimuthal_backward == (-π/4,0/4)
# backward distributions follow forward if unspecified
rr = RunReverse(; speed_forward = (45,))
@test rr.speed_backward == rr.speed_forward == (45,)
rr = RunReverse(; speed = (45,))
@test rr.speed_backward == rr.speed == (45,)

@test fieldnames(RunReverseFlick) == fieldnames(RunReverse)
# default values
rrf = RunReverseFlick()
@test rrf isa AbstractMotilityTwoStep
@test rrf.speed_forward == (30.0,)
@test rrf.polar_forward == (π,)
@test rrf.azimuthal_forward == Arccos()
@test rrf.speed_backward == rrf.speed_forward
@test rrf isa MotilityTwoStep
@test rrf.speed == (30.0,)
@test rrf.polar == (π,)
@test rrf.azimuthal == Arccos()
@test rrf.speed_backward == rrf.speed
@test rrf.polar_backward == (-π/2, π/2)
@test rrf.azimuthal_backward == rrf.azimuthal_forward
@test rrf.azimuthal_backward == rrf.azimuthal
@test rrf.motile_state.state == Forward
# field overload
@test rrf.state == Forward
# keywords
rrf = RunReverseFlick(; azimuthal_backward = (-π/4,0/4))
@test rrf.azimuthal_backward == (-π/4,0/4)
# polar distributions are independent in run reverse flick
rrf = RunReverseFlick(; speed_forward=(45,), polar_forward=(3π,))
@test rrf.speed_backward == rrf.speed_forward == (45,)
@test rrf.polar_forward == (3π,) && rrf.polar_backward == (-π/2/2)
rrf = RunReverseFlick(; speed=(45,), polar=(3π,))
@test rrf.speed_backward == rrf.speed == (45,)
@test rrf.polar == (3π,) && rrf.polar_backward == (-π/2/2)

@testset "Motile state" begin
@test instances(TwoState) == (Forward, Backward)
Expand Down Expand Up @@ -105,7 +101,7 @@ using LinearAlgebra: norm
@test u₁ == u₂

model = StandardABM(Microbe{2}, ContinuousSpace((1,1)))
rr = RunReverse(speed_forward=[45], speed_backward=[35]) # initialized to Forward
rr = RunReverse(speed=[45], speed_backward=[35]) # initialized to Forward
add_agent!(model; motility = rr)
@test random_speed(model[1], model) == 45
switch!(model[1].motility) # now Backward
Expand Down

0 comments on commit 67e9947

Please sign in to comment.