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

Use dispatch instead of manual branching #90

Merged
merged 11 commits into from
Sep 24, 2024
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
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 <rfoffi@ethz.ch> 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 @@
# 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 @@
# 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))

Check warning on line 56 in src/microbe_step.jl

View check run for this annotation

Codecov / codecov/patch

src/microbe_step.jl#L56

Added line #L56 was not covered by tests
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 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

Check warning on line 31 in src/motility.jl

View check run for this annotation

Codecov / codecov/patch

src/motility.jl#L31

Added line #L31 was not covered by tests

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