Skip to content

Commit

Permalink
use affect! as ABM property (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
mastrof authored Feb 26, 2024
1 parent bcac918 commit 58bb50b
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 40 deletions.
1 change: 0 additions & 1 deletion src/api.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export position, direction, speed, velocity, motilepattern,
turnrate, rotational_diffusivity, radius, state,
tumblebias,
distance, distancevector

Base.position(m::AbstractMicrobe) = m.pos
Expand Down
6 changes: 1 addition & 5 deletions src/chemotaxis/brown-berg.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export BrownBerg, chemotaxis!
export BrownBerg

"""
BrownBerg{D} <: AbstractMicrobe{D}
Expand Down Expand Up @@ -41,10 +41,6 @@ function chemotaxis!(microbe::BrownBerg, model)
return nothing
end # function

function affect!(microbe::BrownBerg, model)
chemotaxis!(microbe, model)
end

function tumblebias(microbe::BrownBerg)
g = microbe.gain
S = microbe.state
Expand Down
6 changes: 1 addition & 5 deletions src/chemotaxis/brumley.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export Brumley, chemotaxis!
export Brumley

"""
Brumley{D} <: AbstractMicrobe{D}
Expand Down Expand Up @@ -51,10 +51,6 @@ function chemotaxis!(microbe::Brumley, model)
return nothing
end # function

function affect!(microbe::Brumley, model)
chemotaxis!(microbe, model)
end

function tumblebias(microbe::Brumley)
Γ = microbe.gain
S = microbe.state
Expand Down
6 changes: 1 addition & 5 deletions src/chemotaxis/celani.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export Celani, chemotaxis!
export Celani

"""
Celani{D} <: AbstractMicrobe{D}
Expand Down Expand Up @@ -49,10 +49,6 @@ function chemotaxis!(microbe::Celani, model)
return nothing
end # function

function affect!(microbe::Celani, model)
chemotaxis!(microbe, model)
end

function tumblebias(microbe::Celani)
β = microbe.gain
S = microbe.state
Expand Down
6 changes: 1 addition & 5 deletions src/chemotaxis/xie.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export Xie, chemotaxis!
export Xie

"""
Xie{D} <: AbstractMicrobe{D}
Expand Down Expand Up @@ -80,10 +80,6 @@ function chemotaxis!(microbe::Xie, model; ε=1e-16)
return nothing
end

function affect!(microbe::Xie, model)
chemotaxis!(microbe, model)
end

function tumblebias(microbe::Xie)
S = state(microbe)
if state(motilepattern(microbe)) == Forward
Expand Down
14 changes: 3 additions & 11 deletions src/microbe_step.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export microbe_step!, microbe_pathfinder_step!, turnrate, affect!
export microbe_step!, microbe_pathfinder_step!

"""
microbe_step!(microbe, model)
Expand All @@ -16,7 +16,7 @@ function microbe_step!(microbe::AbstractMicrobe, model)
# reorient through rotational diffusion
rotational_diffusion!(microbe, model)
# update microbe state
affect!(microbe, model)
model.affect!(microbe, model)
# evaluate instantaneous turn rate
ω = turnrate(microbe) * tumblebias(microbe)
if rand(abmrng(model)) < ω * dt # if true reorient microbe
Expand All @@ -37,19 +37,11 @@ function microbe_pathfinder_step!(microbe::AbstractMicrobe, model)
# reorient through rotational diffusion
rotational_diffusion!(microbe, model)
# update microbe state
affect!(microbe, model)
model.affect!(microbe, model)
# evaluate instantaneous turn rate
ω = turnrate(microbe) * tumblebias(microbe)
if rand(abmrng(model)) < ω * dt # if true reorient microbe
turn!(microbe, model)
end
nothing
end

# no bias for generic non-chemotactic microbe
tumblebias(microbe::AbstractMicrobe) = 1.0
"""
affect!(microbe, model)
Can be used to arbitrarily update `microbe` state.
"""
affect!(microbe::AbstractMicrobe, model) = nothing
7 changes: 6 additions & 1 deletion src/microbes.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export Microbe
export Microbe, chemotaxis!, tumblebias

"""
Microbe{D} <: AbstractMicrobe{D}
Expand Down Expand Up @@ -26,6 +26,11 @@ and the default parameters
state::Float64 = 0.0
end

# fallback functions for default random behavior
chemotaxis!(microbe::AbstractMicrobe, model) = nothing
tumblebias(microbe::AbstractMicrobe) = 1.0


r2dig(x) = round(x, digits=2)
function Base.show(io::IO, ::MIME"text/plain", m::AbstractMicrobe{D}) where D
println(io, "$(typeof(m)) with $(typeof(motilepattern(m)))")
Expand Down
2 changes: 2 additions & 0 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ DEFAULT_ABM_PROPERTIES = Dict(
:concentration_time_derivative => (pos,model) -> 0.0, # scalar
# required by models of chemotaxis, default value is glutamate diffusivity
:compound_diffusivity => 608.0,
:affect! => chemotaxis!
)
```
By including these default properties, we make sure that all the chemotaxis models
Expand Down Expand Up @@ -97,4 +98,5 @@ DEFAULT_ABM_PROPERTIES = Dict(
:concentration_time_derivative => (pos,model) -> 0.0,
# required by models of chemotaxis, default value is glutamate diffusivity
:compound_diffusivity => 608.0,
:affect! => chemotaxis!
)
1 change: 1 addition & 0 deletions test/model-creation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using LinearAlgebra: norm
:concentration_gradient,
:concentration_time_derivative,
:compound_diffusivity,
:affect!
))
end

Expand Down
10 changes: 3 additions & 7 deletions test/model-stepping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ using LinearAlgebra: norm

# customize microbe affect! function
# decreases microbe state value by D at each step
MicrobeAgents.affect!(microbe::Microbe{D}, model) = (microbe.state -= D)
model = StandardABM(Microbe{D}, space, dt; container)
affect!(microbe::Microbe{D}, model) where D = (microbe.state -= D)
properties = Dict(:affect! => affect!)
model = StandardABM(Microbe{D}, space, dt; container, properties)
add_agent!(model)
run!(model, 1)
@test model[1].state == -D
# restore default affect! function
MicrobeAgents.affect!(microbe::Microbe{D}, model) = nothing
run!(model, 1)
# now the state has not changed from previous step
@test model[1].state == -D

# customize model_step! function
properties = Dict(:square_t => 0)
Expand Down

0 comments on commit 58bb50b

Please sign in to comment.