From c5a5fa09b96fdafe3419304a1c8aa6e47430715d Mon Sep 17 00:00:00 2001 From: Riccardo Foffi Date: Mon, 26 Feb 2024 23:04:53 +0100 Subject: [PATCH] use `affect!` as ABM property --- src/api.jl | 1 - src/chemotaxis/brown-berg.jl | 6 +----- src/chemotaxis/brumley.jl | 6 +----- src/chemotaxis/celani.jl | 6 +----- src/chemotaxis/xie.jl | 6 +----- src/microbe_step.jl | 14 +++----------- src/microbes.jl | 7 ++++++- src/model.jl | 2 ++ test/model-creation.jl | 1 + test/model-stepping.jl | 10 +++------- 10 files changed, 19 insertions(+), 40 deletions(-) diff --git a/src/api.jl b/src/api.jl index 61d4fd7..898d0e9 100644 --- a/src/api.jl +++ b/src/api.jl @@ -1,6 +1,5 @@ export position, direction, speed, velocity, motilepattern, turnrate, rotational_diffusivity, radius, state, - tumblebias, distance, distancevector Base.position(m::AbstractMicrobe) = m.pos diff --git a/src/chemotaxis/brown-berg.jl b/src/chemotaxis/brown-berg.jl index 09f4beb..d1f51ee 100644 --- a/src/chemotaxis/brown-berg.jl +++ b/src/chemotaxis/brown-berg.jl @@ -1,4 +1,4 @@ -export BrownBerg, chemotaxis! +export BrownBerg """ BrownBerg{D} <: AbstractMicrobe{D} @@ -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 diff --git a/src/chemotaxis/brumley.jl b/src/chemotaxis/brumley.jl index 4c7eb51..0d4db6c 100644 --- a/src/chemotaxis/brumley.jl +++ b/src/chemotaxis/brumley.jl @@ -1,4 +1,4 @@ -export Brumley, chemotaxis! +export Brumley """ Brumley{D} <: AbstractMicrobe{D} @@ -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 diff --git a/src/chemotaxis/celani.jl b/src/chemotaxis/celani.jl index 09df18a..aaf03d8 100644 --- a/src/chemotaxis/celani.jl +++ b/src/chemotaxis/celani.jl @@ -1,4 +1,4 @@ -export Celani, chemotaxis! +export Celani """ Celani{D} <: AbstractMicrobe{D} @@ -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 diff --git a/src/chemotaxis/xie.jl b/src/chemotaxis/xie.jl index 8d418d0..32f01a4 100644 --- a/src/chemotaxis/xie.jl +++ b/src/chemotaxis/xie.jl @@ -1,4 +1,4 @@ -export Xie, chemotaxis! +export Xie """ Xie{D} <: AbstractMicrobe{D} @@ -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 diff --git a/src/microbe_step.jl b/src/microbe_step.jl index 728e0c6..1269a97 100644 --- a/src/microbe_step.jl +++ b/src/microbe_step.jl @@ -1,4 +1,4 @@ -export microbe_step!, microbe_pathfinder_step!, turnrate, affect! +export microbe_step!, microbe_pathfinder_step! """ microbe_step!(microbe, model) @@ -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 @@ -37,7 +37,7 @@ 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 @@ -45,11 +45,3 @@ function microbe_pathfinder_step!(microbe::AbstractMicrobe, 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 diff --git a/src/microbes.jl b/src/microbes.jl index c24f1b8..7f6975a 100644 --- a/src/microbes.jl +++ b/src/microbes.jl @@ -1,4 +1,4 @@ -export Microbe +export Microbe, chemotaxis!, tumblebias """ Microbe{D} <: AbstractMicrobe{D} @@ -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)))") diff --git a/src/model.jl b/src/model.jl index 8086530..af3b01d 100644 --- a/src/model.jl +++ b/src/model.jl @@ -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 @@ -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! ) diff --git a/test/model-creation.jl b/test/model-creation.jl index 2afaa42..ea32c0a 100644 --- a/test/model-creation.jl +++ b/test/model-creation.jl @@ -13,6 +13,7 @@ using LinearAlgebra: norm :concentration_gradient, :concentration_time_derivative, :compound_diffusivity, + :affect! )) end diff --git a/test/model-stepping.jl b/test/model-stepping.jl index e82314c..e8ddbe7 100644 --- a/test/model-stepping.jl +++ b/test/model-stepping.jl @@ -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)