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

generalise dispersal to multiple populations #116

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions src/kernel/formulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ where λ is a shape parameter.
Base.@kwdef struct ExponentialKernel{P} <: KernelFormulation
λ::P = Param(1.0, bounds=(0.0, 2.0))
end
(f::ExponentialKernel)(d) = exp(-d / f.λ)
(f::ExponentialKernel)(d) = exp.(-d ./ f.λ)

"""
GeometricKernel <: KernelFormulation
Expand All @@ -50,7 +50,7 @@ where α is a shape parameter.
Base.@kwdef struct GeometricKernel{P} <: KernelFormulation
α::P = Param(1.0, bounds=(-1000.0, 1000.0))
end
(f::GeometricKernel)(d) = (1 + d)^f.α * ((f.α + 1)*(f.α + 2)) / (2 * π)
(f::GeometricKernel)(d) = (1 + d) .^ f.α .* ((f.α .+ 1) .* (f.α .+ 2)) ./ (2 * π)

"""
GaussianKernel <: KernelFormulation
Expand All @@ -68,12 +68,12 @@ where α is a positive parameter.
Base.@kwdef struct GaussianKernel{P} <: KernelFormulation
α::P = Param(1.0, bounds=(0.0, 1000.0))
end
(f::GaussianKernel)(d) = 1 / (π * f.α^2) * exp(- d^2 / f.α^2)
(f::GaussianKernel)(d) = 1 ./ (π * f.α .^ 2) .* exp.(-d ^ 2 ./ f.α .^ 2)

"""
WeibullKernel <: KernelFormulation

WeibullKernel(α,β)
WeibullKernel(α, β)

Probability density function of distance ``d``.

Expand All @@ -87,4 +87,4 @@ Base.@kwdef struct WeibullKernel{A,B} <: KernelFormulation
α::A = Param(1.0, bounds=(0.0, 1000.0))
β::B = Param(2.0, bounds=(0.0, 1000.0))
end
(f::WeibullKernel)(d) = f.β / (2 * π * f.α^2) * d^(f.β - 2) * exp(- d^f.β / f.α^f.β)
(f::WeibullKernel)(d) = f.β ./ (2 * π .* f.α .^ 2) .* d .^ (f.β .- 2) * exp.(-d .^ f.β ./ f.α .^ f.β)
8 changes: 4 additions & 4 deletions src/kernel/inwards.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
InwardsPopulationDispersal <: NeighborhoodRule
InwardsDispersal <: NeighborhoodRule

InwardsPopulationDispersal(; kw...)
InwardsPopulationDispersal{R}(; kw...)
InwardsPopulationDispersal{R,W}(; kw...)
InwardsDispersal(; kw...)
InwardsDispersal{R}(; kw...)
InwardsDispersal{R,W}(; kw...)

Implements deterministic dispersal from populations in neighboring cells to the current
cell.
Expand Down
14 changes: 11 additions & 3 deletions src/kernel/kernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct DispersalKernel{R,N,L,T,H<:Stencil{R,N,L,T},K,F,C,D<:DistanceMethod} <: S
stencil
else
# Build the kernel matrix
newkernel = scale(buildkernel(stencil, formulation, distancemethod, cellsize))
newkernel = buildkernel(stencil, formulation, distancemethod, cellsize) |> scale
new{R,N,L,T,H,typeof(newkernel),F,C,D}(
stencil, newkernel, formulation, cellsize, distancemethod
)
Expand Down Expand Up @@ -88,7 +88,15 @@ formulation(stencil::DispersalKernel) = stencil.formulation
# SMatrix{S,S}(kernel)
# end
function buildkernel(stencil::Stencil{<:Any,<:Any,L}, f, dm, cellsize) where L
SVector{L}(Tuple(dispersalprob(f, dm, x, y, cellsize) for (x, y) in offsets(stencil)))
probs = map(offsets(stencil)) do (x, y)
dispersalprob(f, dm, x, y, cellsize)
end
return SVector{L}(probs)
end

scale(x) = x ./ sum(x)
function scale(xs)
s = sum(xs)
map(xs) do x
x ./ s
end
end
18 changes: 9 additions & 9 deletions src/kernel/outwards.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""
OutwardsPopulationDispersal <: SetNeighborhoodRule
OutwardsDispersal <: SetNeighborhoodRule

OutwardsPopulationDispersal(; kw...)
OutwardsPopulationDispersal{R}(; kw...)
OutwardsPopulationDispersal{R,W}(; kw...)
OutwardsDispersal(; kw...)
OutwardsDispersal{R}(; kw...)
OutwardsDispersal{R,W}(; kw...)

Implements deterministic dispersal from the current cell to populations in neighboring
cells.
Expand All @@ -29,10 +29,10 @@ is occupied.
Default is 1.0.
- `distancemethod`: [`DistanceMethod`](@ref) object for calculating distance between cells.
The default is [`CentroidToCentroid`](@ref).
- `maskbehavior`: The default setting is `IgnoreMaskEdges()`. Use `CheckMaskEdges()` to indicate that the grid is
masked, enabling the rule to perform boundary checking at mask edges. Not using
`CheckMaskEdges()` on a masked grid may result in the loss of individuals at the edges, but it comes
at a performance cost.
- `maskbehavior`: The default is `IgnoreMaskEdges()`. Use `CheckMaskEdges()`
to enabling the rule to perform boundary checking at mask edges.
Using `IgnoreMaskEdges()` on a masked grid may result in the loss of individuals at the edges,
while using `CheckMaskEdges()` should not. However, this comes at a performance cost.

Pass grid name `Symbol`s to `R` and `W` type parameters to use specific grids.
"""
Expand Down Expand Up @@ -69,7 +69,7 @@ end
target = I .+ offset
(target_mod, inbounds) = DynamicGrids.inbounds(data, target)
if inbounds && (isnothing(mask_data) || mask_data[target_mod...])
@inbounds propagules = N * k
@inbounds propagules = N .* k
@inbounds add!(data[W], propagules, target_mod...)
sum += propagules
end
Expand Down
Loading