Skip to content

Commit

Permalink
siteselector cell iter will continue until found
Browse files Browse the repository at this point in the history
  • Loading branch information
pablosanjose committed Oct 21, 2023
1 parent db3b60a commit 743016a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
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

0 comments on commit 743016a

Please sign in to comment.