Skip to content

Commit

Permalink
Use dispatch instead of manual branching (#90)
Browse files Browse the repository at this point in the history
* Use dispatch instead of manual branching

* Update motility.jl

* Update Project.toml

* Update motility.jl

* Update MicrobeAgents.jl

* Update motility.jl

* Update motility.jl

* Update motility.jl

* update to new LightSumTypes version

* bump patch version

* update benchmark

---------

Co-authored-by: Riccardo Foffi <[email protected]>
  • Loading branch information
Tortar and mastrof authored Sep 24, 2024
1 parent 0498af4 commit ded80e0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name = "MicrobeAgents"
uuid = "b17a4bac-8667-4a2d-84f7-1883ae0b8dbb"
authors = ["Riccardo Foffi <[email protected]> and contributors"]
version = "0.4.1"
version = "0.4.2"

[deps]
Agents = "46ada45e-f475-11e8-01d0-f70cc89e6671"
Autocorrelations = "b9118d5e-f165-4cbd-b2ae-b030566b7b26"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
LightSumTypes = "f56206fc-af4c-5561-a72a-43fe2ca5a923"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MeanSquaredDisplacement = "13c93d70-909c-440c-af92-39d48ffa2ba2"
MixedStructTypes = "3d69f371-6fa5-5add-b11c-3293622cad62"
Quaternions = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand All @@ -23,8 +23,8 @@ Autocorrelations = "0.1"
DataFrames = "1"
Distributions = "0.25"
GeometryBasics = "0.4"
LightSumTypes = "4"
MeanSquaredDisplacement = "0.2"
MixedStructTypes = "0.2"
Quaternions = "0.7"
StaticArrays = "1"
StatsBase = "0.34"
Expand Down
20 changes: 20 additions & 0 deletions benchmark/benchmarks_0.4.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# MicrobeAgents.jl benchmarks - 0.4.2

## Simple stepping
Type | Time (μs) | Allocs | Memory
--- | --- | --- | ---
Microbe{1} | 5.23e-01 | 1.10e+01 | 1.79e+02
Microbe{2} | 6.60e-01 | 1.50e+01 | 3.07e+02
Microbe{3} | 7.39e-01 | 1.50e+01 | 3.07e+02
BrownBerg{1} | 6.13e-01 | 1.70e+01 | 7.55e+02
BrownBerg{2} | 7.35e-01 | 2.10e+01 | 9.79e+02
BrownBerg{3} | 8.29e-01 | 2.10e+01 | 1.08e+03
Brumley{1} | 6.11e-01 | 1.65e+01 | 7.47e+02
Brumley{2} | 7.55e-01 | 2.05e+01 | 9.71e+02
Brumley{3} | 8.49e-01 | 2.05e+01 | 1.07e+03
Celani{1} | 5.47e-01 | 1.30e+01 | 3.71e+02
Celani{2} | 7.36e-01 | 1.70e+01 | 5.31e+02
Celani{3} | 8.00e-01 | 1.70e+01 | 5.63e+02
SonMenolascina{1} | 6.65e-01 | 1.60e+01 | 7.39e+02
SonMenolascina{2} | 8.02e-01 | 2.00e+01 | 9.64e+02
SonMenolascina{3} | 8.90e-01 | 2.00e+01 | 1.06e+03
2 changes: 1 addition & 1 deletion src/MicrobeAgents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export add_agent!, add_agent_own_pos!
export move_agent!, walk!, run!

using Distributions
using LightSumTypes
using LinearAlgebra
using MixedStructTypes
using Random
using Quaternions
using StaticArrays
Expand Down
4 changes: 2 additions & 2 deletions src/microbe_step.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function microbe_step!(microbe::AbstractMicrobe, model::AgentBasedModel)
# if the new motile state is a TurnState with duration 0
# immediately turn and transition to another state
new_motilestate = motilestate(microbe)
if kindof(new_motilestate) === :TurnState && iszero(duration(new_motilestate))
if variantof(new_motilestate) === TurnState && iszero(duration(new_motilestate))
turn!(microbe, model)
update_motilestate!(microbe, model)
end
Expand Down Expand Up @@ -53,7 +53,7 @@ function microbe_pathfinder_step!(microbe::AbstractMicrobe, model::AgentBasedMod
# if the new motile state is a TurnState with duration 0
# immediately turn and transition to another state
new_motilestate = motilestate(microbe)
if kindof(new_motilestate) === :TurnState && iszero(duration(new_motilestate))
if variantof(new_motilestate) === TurnState && iszero(duration(new_motilestate))
turn!(microbe, model)
update_motilestate!(microbe, model)
end
Expand Down
42 changes: 22 additions & 20 deletions src/motility.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@ export motilepattern, motilestate, state, states, transition_weights
export duration, speed, polar, azimuthal
export Arccos # from Agents

@compact_structs MotileState begin
@kwdef struct RunState
duration
speed
polar = nothing
azimuthal = nothing
end
@kwdef struct TurnState
duration
speed = [zero(duration)]
polar
azimuthal
end
end

function biased(s::MotileState)
if kindof(s) === :RunState
return true
else # TurnState
return false
end
struct RunState
duration
speed
polar
azimuthal
end
struct TurnState
duration
speed
polar
azimuthal
end
@sumtype MotileState(RunState, TurnState)
function RunState(; duration, speed, polar=nothing, azimuthal=nothing)
MotileState(RunState(duration, speed, polar, azimuthal))
end
function TurnState(; duration, speed=[zero(duration)], polar, azimuthal)
MotileState(TurnState(duration, speed, polar, azimuthal))
end

biased(s::MotileState) = biased(variant(s))
biased(::RunState) = true
biased(::TurnState) = false

# Base motile states
Run(duration::Real, speed) = RunState(; duration, speed)
Expand Down

2 comments on commit ded80e0

@mastrof
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/115907

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.2 -m "<description of version>" ded80e06b28b7fcc5f9d6f62978af9d3f1789760
git push origin v0.4.2

Please sign in to comment.