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

Fixing boundary checks on masked grids #114

Merged
merged 41 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
4a92d6e
Trying masked OutwardsDispersal
nicolasmerino41 Jun 13, 2024
ca57619
Update outwards.jl
nicolasmerino41 Jun 13, 2024
77fd5e1
Update index.md
nicolasmerino41 Jun 13, 2024
aa7ef4e
Update outwards
nicolasmerino41 Jun 13, 2024
dd042d8
Update outwards.jl
nicolasmerino41 Jun 13, 2024
f6c5719
Update outwards.jl
nicolasmerino41 Jun 13, 2024
aa7f88c
Update outwards.jl
nicolasmerino41 Jun 13, 2024
b8be33f
Update src/kernel/outwards.jl
nicolasmerino41 Jun 13, 2024
5dd7c2c
Update src/kernel/outwards.jl
nicolasmerino41 Jun 13, 2024
a6876ba
Update outwards.jl
nicolasmerino41 Jun 13, 2024
8595100
Irrelevant update
nicolasmerino41 Jun 13, 2024
be564b8
Update outwards.jl
nicolasmerino41 Jun 13, 2024
5144f92
Update outwards.jl
nicolasmerino41 Jun 13, 2024
8bcfefb
Update outwards.jl
nicolasmerino41 Jun 13, 2024
f7538d6
Update outwards.jl
nicolasmerino41 Jun 13, 2024
9ae4999
Updated OutwardsDispersal struct
nicolasmerino41 Jun 13, 2024
84508de
Update src/kernel/outwards.jl
nicolasmerino41 Jun 14, 2024
211ccb4
Update outwards and testing
nicolasmerino41 Jun 14, 2024
bd5098d
Small typo fix
nicolasmerino41 Jun 14, 2024
0e921bf
Update src/kernel/outwards.jl
nicolasmerino41 Jun 17, 2024
6850dc2
If/else block for mask identification
nicolasmerino41 Jun 17, 2024
d7eff3b
If/else mask identification
nicolasmerino41 Jun 17, 2024
1af845a
remove target
nicolasmerino41 Jun 17, 2024
607885d
Update outwards.jl
nicolasmerino41 Jun 17, 2024
00421ae
Back to old version
nicolasmerino41 Jun 17, 2024
330ff3f
Update outwards.jl
nicolasmerino41 Jun 17, 2024
e112673
Update outwards.jl
nicolasmerino41 Jun 17, 2024
c0d7394
Update outwards.jl
nicolasmerino41 Jun 17, 2024
25fd24e
Update outwards.jl
nicolasmerino41 Jun 17, 2024
1f50575
Update outwards.jl
nicolasmerino41 Jun 17, 2024
d9e034b
Update outwards.jl
nicolasmerino41 Jun 17, 2024
2001258
Update outwards.jl
nicolasmerino41 Jun 17, 2024
e268d79
Update outwards.jl
nicolasmerino41 Jun 17, 2024
f5e4777
Update outwards.jl
nicolasmerino41 Jun 17, 2024
0aeefc0
Performance fix
nicolasmerino41 Jun 17, 2024
2777e96
Update outwards.jl
nicolasmerino41 Jun 17, 2024
ad744de
Update outwards.jl
nicolasmerino41 Jun 17, 2024
ce4d5bc
Irrelevant
nicolasmerino41 Jun 18, 2024
68f045b
Back to slow bound-checking
nicolasmerino41 Jun 18, 2024
03ff760
Update doc
nicolasmerino41 Jun 18, 2024
421f465
Renaming
nicolasmerino41 Jun 19, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*.jl.*.cov
*.jl.mem
docs/build
*.json
Manifest.toml
24 changes: 20 additions & 4 deletions src/kernel/outwards.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,37 @@ is occupied.

Pass grid name `Symbol`s to `R` and `W` type parameters to use specific grids.
"""
# Updated the OutwardsDispersal rule to include an optional mask
struct OutwardsDispersal{R,W,S<:Stencils.AbstractKernelStencil} <: SetNeighborhoodRule{R,W}
stencil::S
mask::Union{Nothing, Array{Bool}} # Add an optional mask parameter
nicolasmerino41 marked this conversation as resolved.
Show resolved Hide resolved
end

function OutwardsDispersal{R,W}(stencil::S; mask=nothing) where {R,W,S<:Stencils.AbstractKernelStencil}
OutwardsDispersal{R,W,S}(stencil, mask)
end

function OutwardsDispersal{R,W}(; kw...) where {R,W}
OutwardsDispersal{R,W}(DispersalKernel(; kw...))
end

@inline function applyrule!(data, rule::OutwardsDispersal{R,W}, N, I) where {R,W}
N == zero(N) && return nothing
# Check if the current cell is masked, skip if it is
if rule.mask !== nothing && !rule.mask[I...]
nicolasmerino41 marked this conversation as resolved.
Show resolved Hide resolved
return nothing
end
sum = zero(N)
for (offset, k) in zip(offsets(rule), kernel(rule))
@inbounds propagules = N * k
@inbounds add!(data[W], propagules, I .+ offset...)
sum += propagules
target = I .+ offset
# Check if the target cell is within bounds and not masked
(target_mod, inbounds) = _inbounds(Reflect(), size(data[1]), target...)
Copy link
Member

@rafaqz rafaqz Jun 13, 2024

Choose a reason for hiding this comment

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

I think inbounds(data, i) does exactly this for you

Copy link
Author

Choose a reason for hiding this comment

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

True! And it's better cause I don't need to specify the boundary condition

Copy link
Member

Choose a reason for hiding this comment

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

Yes we try to keep anything related to boundary conditions, aux data, masks etc out of rules and in the data object. Then we can swap them around without changing all our code.

if inbounds && (rule.mask === nothing || rule.mask[target_mod...])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if inbounds && (rule.mask === nothing || rule.mask[target_mod...])
if inbounds && (isnothing(rule.mask) || mask(data)[target_mod...])

@inbounds propagules = N * k
@inbounds add!(data[W], propagules, target_mod...)
sum += propagules
end
end
@inbounds sub!(data[W], sum, I...)
return nothing
end
end
Loading