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

Deprecate UnremovableABM #60

Merged
merged 6 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ for D in dimensions
evals = 10,
samples = 1000,
setup = (
model = UnremovableABM(S, $T, dt),
model = StandardABM(S, $T, dt; container=Vector),
S = ContinuousSpace(SVector{$D}(10.0 for _ in $D)),
dt = 0.1
)
Expand All @@ -45,7 +45,7 @@ open("benchmarks.md", "a") do io
for D in dimensions
space = ContinuousSpace(ntuple(_ -> 10.0, D))
dt = 0.1
model = UnremovableABM(T{D}, space, dt)
model = StandardABM(T{D}, space, dt; container=Vector)
add_agent!(model; turn_rate = 1/dt) # reorients at each step
nsteps = 1000
b = @benchmark run!($model, $(nsteps))
Expand Down
2 changes: 1 addition & 1 deletion docs/src/chemotaxis.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ properties = Dict(
:concentration_field => concentration_field,
:concentration_gradient => concentration_gradient
)
model = UnremovableABM(BrownBerg{2}, space, Δt; periodic, properties)
model = StandardABM(BrownBerg{2}, space, Δt; periodic, properties, container=Vector)
for i in 1:n
add_agent!(model)
end
Expand Down
7 changes: 3 additions & 4 deletions docs/src/firststeps.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ MicrobeAgents.jl exploits the `AgentBasedModel` interface from Agents.jl.
While the standard Agents.jl syntax will always work, it is typically more
convenient to use the method extensions provided by MicrobeAgents.jl, which
also includes some default parameters required by the simulations.
Both `StandardABM` and `UnremovableABM` are supported.
Whenever removal of microbes during the simulation is not needed,
`UnremovableABM` is the recommended choice.
it is recommended to call `StandardABM` with the `container=Vector`
keyword argument to improve performance.
```@docs
UnremovableABM
StandardABM
```

Expand All @@ -104,7 +103,7 @@ object.
extent = (1000.0, 500.0) # size of 2D simulation domain
space = ContinuousSpace(extent)
dt = 0.1 # integration timestep
model = UnremovableABM(Microbe{2}, space, dt)
model = StandardABM(Microbe{2}, space, dt)
```

Now bacteria can be added with the `add_agent!` function.
Expand Down
6 changes: 3 additions & 3 deletions docs/src/randomwalks.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ dt = 0.1
n = 10
nsteps = 600

model = UnremovableABM(Microbe{1}, space, dt)
model = StandardABM(Microbe{1}, space, dt; container=Vector)
foreach(_ -> add_agent!((0,), model), 1:n)
```
By default, `UnremovableABM` will create a periodic domain; this will
By default, `ContinuousSpace` will create a periodic domain; this will
allow us to mimic an "infinite" system.
All the microbes have been initialized from position 0, without
specifying any further property, so they will be initialized with a random
Expand Down Expand Up @@ -62,7 +62,7 @@ space = ContinuousSpace((L,L))
dt = 0.1
nsteps = 600

model = UnremovableABM(Microbe{2}, space, dt)
model = StandardABM(Microbe{2}, space, dt; container=Vector)
```
But we can now add bacteria with different motility patterns
(we import Distributions.jl to use a Normal distribution for the speed of
Expand Down
2 changes: 1 addition & 1 deletion examples/Analysis/msd_runtumble.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ U = 30.0 # μm/s
τ = 1.0 # s
turn_rate = 1 / τ

models = map(_ -> UnremovableABM(Microbe{3}, space, dt), θs)
models = map(_ -> StandardABM(Microbe{3}, space, dt; container=Vector), θs)
nmicrobes = 100
for (i,θ) in enumerate(θs)
motility = RunTumble(speed=[U], polar=[θ,-θ])
Expand Down
2 changes: 1 addition & 1 deletion examples/Analysis/run_distribution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ L = 1000
space = ContinuousSpace((L,L))
dt = 0.01

model = UnremovableABM(Microbe{2}, space, dt)
model = StandardABM(Microbe{2}, space, dt; container=Vector)
n = 500
Drot = 0.1
τ = 1.5
Expand Down
2 changes: 1 addition & 1 deletion examples/Analysis/velocity_autocorrelations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ turn_rate = 1 / τ_run # 1/s
L = 1e4 # μm
space = ContinuousSpace((L,L,L))

model = UnremovableABM(Microbe{3}, space, Δt)
model = StandardABM(Microbe{3}, space, Δt; container=Vector)
n = 200
for i in 1:n
add_agent!(model; turn_rate, motility=RunTumble(speed=[U]))
Expand Down
2 changes: 1 addition & 1 deletion examples/Chemotaxis/linear_ramp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ properties = Dict(
:concentration_field => concentration_field,
:concentration_gradient => concentration_gradient
)
model = UnremovableABM(BrownBerg{2}, space, Δt; properties)
model = StandardABM(BrownBerg{2}, space, Δt; properties)
for i in 1:n
add_agent!(model)
end
Expand Down
2 changes: 1 addition & 1 deletion examples/Chemotaxis/xie_2D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ properties = Dict(
)

rng = MersenneTwister(12)
model = UnremovableABM(Xie{2}, space, timestep; rng, properties)
model = StandardABM(Xie{2}, space, timestep; rng, properties)
foreach(_ -> add_agent!(model; chemotactic_precision=6.0), 1:300)

nsteps = 5000
Expand Down
2 changes: 1 addition & 1 deletion examples/Chemotaxis/xie_response-function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ properties = Dict(
)

model_step!(model) = model.t += 1
model = UnremovableABM(Xie{3}, space, timestep; properties, model_step!)
model = StandardABM(Xie{3}, space, timestep; properties, model_step!)
add_agent!(model; turn_rate_forward=0, motility=RunReverseFlick(motile_state=MotileState(Forward)))
add_agent!(model; turn_rate_backward=0, motility=RunReverseFlick(motile_state=MotileState(Backward)))

Expand Down
2 changes: 1 addition & 1 deletion examples/Encounters/sphere3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function setupmodel(R, L, n; dt=0.1, rng=Xoshiro(1))
:sphere => HyperSphere(extent./2, R),
:encounters => 0,
)
model = UnremovableABM(Microbe{3}, space, dt; properties, rng)
model = StandardABM(Microbe{3}, space, dt; properties, rng, container=Vector)
foreach(_ -> add_agent!(model; turn_rate=2.0), 1:n)
model → encounters!
abmproperties(model)[:old_positions] = map(m -> m.pos, allagents(model))
Expand Down
2 changes: 1 addition & 1 deletion examples/Pathfinder/chemotaxis_porous-medium.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ properties = Dict(
:C₂ => C₂,
)

model = UnremovableABM(BrownBerg{2}, space, dt; properties)
model = StandardABM(BrownBerg{2}, space, dt; properties, container=Vector)
pathfinder!(model, wm)
foreach(_ -> add_agent!((0.0, rand()*extent[2]), model), 1:nbacteria)
adata = [:pos]
Expand Down
2 changes: 1 addition & 1 deletion examples/Pathfinder/randomwalk_porous-medium.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bodies = [
]
wm = walkmap(bodies, extent, min_radius/25, 0)

model = UnremovableABM(BrownBerg{2}, space, dt)
model = StandardABM(BrownBerg{2}, space, dt; container=Vector)
pathfinder!(model, wm)
foreach(_ -> add_agent!(model), 1:nbacteria)
adata = [:pos]
Expand Down
2 changes: 1 addition & 1 deletion examples/RandomWalks/randomwalk1D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ n = 10
nsteps = 600

## abm setup
model = UnremovableABM(Microbe{1}, space, dt)
model = StandardABM(Microbe{1}, space, dt)
foreach(_ -> add_agent!((0,), model), 1:n)

## simulation
Expand Down
2 changes: 1 addition & 1 deletion examples/RandomWalks/randomwalk2D_motilepatterns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dt = 0.1
nsteps = 600

## abm setup
model = UnremovableABM(Microbe{2}, space, dt)
model = StandardABM(Microbe{2}, space, dt)
# add bacteria with different motile properties
add_agent!(model; motility=RunReverse(speed_forward=[55]), rotational_diffusivity=0.2)
add_agent!(model; motility=RunTumble(speed=Normal(30,6)), turn_rate=0.5)
Expand Down
2 changes: 1 addition & 1 deletion examples/RandomWalks/randomwalk3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dt = 0.1
nsteps = 600

## abm setup
model = UnremovableABM(Microbe{3}, space, dt)
model = StandardABM(Microbe{3}, space, dt)
# add bacteria with different motile properties
add_agent!(model; motility=RunReverse(speed_forward=[55]), rotational_diffusivity=0.2)
add_agent!(model; motility=RunTumble(speed=Normal(30,6)), turn_rate=0.5)
Expand Down
2 changes: 1 addition & 1 deletion src/MicrobeAgents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module MicrobeAgents

using Agents
export abmproperties, abmrng, abmspace, abmscheduler, spacesize
export StandardABM, UnremovableABM, ContinuousSpace
export StandardABM, ContinuousSpace
export add_agent!, add_agent_pos!
export move_agent!, walk!, run!

Expand Down
76 changes: 6 additions & 70 deletions src/model.jl
Original file line number Diff line number Diff line change
@@ -1,76 +1,9 @@
# extended from Agents.jl
export StandardABM, UnremovableABM, add_agent!
# exported from Agents.jl without extensions
export ContinuousSpace

"""
UnremovableABM(MicrobeType, space, timestep; kwargs...)
Extension of the `Agents.UnremovableABM` method for microbe types.
Implementation of `AgentBasedModel` where agents can only be added but not removed.
See `Agents.AgentBasedModel` for detailed information on the keyword arguments.

**Arguments**
- `MicrobeType`: a concrete subtype of `AbstractMicrobe{D}`,
with explicitly specified dimensionality `D`.
A list of available options can be obtained by running `subtypes(AbstractMicrobe)`.
- `space`: a `ContinuousSpace{D}` with _the same_ dimensionality `D` as MicrobeType
which specifies the spatial properties of the simulation domain.
- `timestep`: the integration timestep of the simulation.

**Keywords**
- `properties`: additional container of data to specify model-level properties.
MicrobeAgents.jl includes a set of default properties (detailed at the end).
- `agent_step!`: stepping function for each agent in the model
- `model_step!`: stepping function for the model
- `scheduler = Schedulers.fastest`
- `rng = Random.default_rng()`
- `spacing = minimum(extent)/20`
- `warn = true`

**Default `properties`**

When a model is created, a default set of properties is included in the model
(`MicrobeAgents.default_ABM_properties`):
```
DEFAULT_ABM_PROPERTIES = Dict(
:t => 0, # counter for timekeeping
:concentration_field => (pos,model) -> 0.0,
:concentration_gradient => (pos,model) -> zero.(pos),
:concentration_time_derivative => (pos,model) -> 0.0,
# required by models of chemotaxis, default value is glutamate diffusivity
:compound_diffusivity => 608.0,
)
```
By including these default properties, we make sure that all the chemotaxis models
will work even without extra user intervention.
All these properties can be overwritten by simply passing an equivalent key
to the `properties` dictionary when creating the model.
"""
function Agents.UnremovableABM(
T::Type{A}, space::ContinuousSpace{D}, timestep::Real;
agent_step! = microbe_step!,
model_step! = _ -> nothing,
scheduler = Schedulers.fastest,
properties = Dict(),
rng = Random.default_rng(),
warn = true
) where {D,A<:AbstractMicrobe{D}}
properties = Dict(
DEFAULT_ABM_PROPERTIES...,
properties...,
:timestep => timestep
)
UnremovableABM(T, space;
agent_step!, model_step!, scheduler, properties, rng, warn
)
end


"""
StandardABM(MicrobeType, space, timestep; kwargs...)
Extension of the `Agents.StandardABM` method for microbe types.
Implementation of `AgentBasedModel` where agents can be added and removed at any time.
If agents removal is not required, it is recommended to use `UnremovableABM` for better performance.
If agents removal is not required, it is recommended to use the
keyword argument `container = Vector` for better performance.
See `Agents.AgentBasedModel` for detailed information on the keyword arguments.

**Arguments**
Expand Down Expand Up @@ -107,9 +40,11 @@ function Agents.StandardABM(
T::Type{A}, space::ContinuousSpace{D}, timestep::Real;
agent_step! = microbe_step!,
model_step! = _ -> nothing,
container = Dict,
scheduler = Schedulers.fastest,
properties = Dict(),
rng = Random.default_rng(),
agents_first = true,
warn = true,
) where {D,A<:AbstractMicrobe{D}}
properties = Dict(
Expand All @@ -118,7 +53,8 @@ function Agents.StandardABM(
:timestep => timestep
)
StandardABM(T, space;
agent_step!, model_step!, scheduler, properties, rng, warn
agent_step!, model_step!, container,
scheduler, properties, rng, agents_first, warn
)
end

Expand Down
7 changes: 1 addition & 6 deletions src/neighborlists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,8 @@ function update_neighborlist!(microbe::AbstractMicrobe, model, listkey)
neighbor_list.xpositions[microbe.id] = microbe.pos
end


@inline function make_position_vector(model::UnremovableABM)
[position(a) for a in allagents(model)]
end
@inline function make_position_vector(model::StandardABM)
ids = sort(collect(allids(model)))
[position(model[i]) for i in ids]
map(position, allagents(model))
end
@inline function make_position_vector(x::AbstractVector)
[SVector{length(xᵢ)}(position(xᵢ)) for xᵢ in x]
Expand Down
9 changes: 3 additions & 6 deletions test/model-creation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ using LinearAlgebra: norm
for D in 1:3
timestep = 1
space = ContinuousSpace(ones(SVector{D}))
model_unremovable = UnremovableABM(Microbe{D}, space, timestep)
model_standard = StandardABM(Microbe{D}, space, timestep)
@test model_unremovable isa UnremovableABM
@test model_standard isa StandardABM
@test Set(keys(abmproperties(model_standard))) == Set((
model = StandardABM(Microbe{D}, space, timestep)
@test model isa StandardABM
@test Set(keys(abmproperties(model))) == Set((
:t, :timestep,
:concentration_field,
:concentration_gradient,
:concentration_time_derivative,
:compound_diffusivity,
))
@test abmproperties(model_standard) == abmproperties(model_unremovable)
end

@testset "Base Microbe type" begin
Expand Down
8 changes: 4 additions & 4 deletions test/model-stepping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ using Random
using LinearAlgebra: norm

@testset "Model stepping" begin
for D in 1:3, ABM in (StandardABM, UnremovableABM)
for D in 1:3, container in (Dict, Vector)
rng = Xoshiro(68)
dt = 1
extent = fill(300.0, SVector{D})
space = ContinuousSpace(extent)
model = ABM(Microbe{D}, space, dt; rng)
model = StandardABM(Microbe{D}, space, dt; rng, container)
pos = extent ./ 2
vel1 = random_velocity(model)
speed1 = random_speed(rng, RunTumble())
Expand All @@ -30,7 +30,7 @@ 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 = ABM(Microbe{D}, space, dt)
model = StandardABM(Microbe{D}, space, dt; container)
add_agent!(model)
run!(model, 1)
@test model[1].state == -D
Expand All @@ -42,7 +42,7 @@ using LinearAlgebra: norm

# customize model_step! function
model_step!(model) = model.t += 1
model = ABM(Microbe{D}, space, dt; model_step!)
model = StandardABM(Microbe{D}, space, dt; model_step!, container)
run!(model, 6)
@test model.t == 6
end
Expand Down
Loading