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

foreach_cell iteration should continue until at least one site is found #222

Merged
merged 1 commit into from
Oct 21, 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
14 changes: 10 additions & 4 deletions src/apply.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,19 @@ function recursive_push!(v::Vector{Pair{T,T}}, (xs, ys)::Pair) where {T}
return v
end

# for cells = function
# for cells::Function
function recursive_push!(v::Vector{SVector{L,Int}}, fcell::Function) where {L}
iter = BoxIterator(zero(SVector{L,Int}))
keepgoing = true
for cell in iter
fcell(cell) || continue
acceptcell!(iter, cell)
push!(v, cell)
found = fcell(cell)
if found || keepgoing
acceptcell!(iter, cell)
if found
push!(v, cell)
keepgoing = false
end
end
end
return v
end
Expand Down
8 changes: 6 additions & 2 deletions src/selectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ isonsite(dr) = iszero(dr)
function foreach_cell(f, sel::AppliedSiteSelector)
lat = lattice(sel)
cells_list = cells(sel)
if isempty(cells_list) # no cells specified
if isempty(cells_list) # no cells specified
iter = BoxIterator(zerocell(lat))
keepgoing = true # will search until we find at least one
for cell in iter
found = f(cell)
found && acceptcell!(iter, cell)
if found || keepgoing
acceptcell!(iter, cell)
found && (keepgoing = false)
end
end
else
for cell in cells_list
Expand Down
7 changes: 5 additions & 2 deletions test/test_lattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ end
@test ls2[2] isa Tuple{SVector{2, Int}, Int}
ls = ls1[sublats = :B]
@test isempty(ls)
ls = lat[cells = n -> norm(n) < 4]
@test all(n -> norm(n) < 4, Quantica.cells(ls))
ls = lat[cells = n -> 5 < norm(n) < 10]
@test !isempty(Quantica.cells(ls)) && all(n -> 5 < norm(n) < 10, Quantica.cells(ls))
ls = lat[region = r -> 5 < norm(r) < 10]
@test !isempty(Quantica.cells(ls)) && all(r -> 5 < norm(r) < 10, Quantica.sites(ls))

end
Loading