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

Faster initialization #156

Open
ffreyer opened this issue Jan 27, 2022 · 0 comments
Open

Faster initialization #156

ffreyer opened this issue Jan 27, 2022 · 0 comments

Comments

@ffreyer
Copy link
Collaborator

ffreyer commented Jan 27, 2022

function Base.push!(cache::LatticeIteratorCache, key::Dir2SrcTrg, lattice::AbstractLattice, ϵ=1e-6)
if !haskey(cache.cache, key)
_positions = positions(lattice)
wrap = generate_combinations(lattice_vectors(lattice))
directions = Vector{Float64}[]
# (src, trg), first index is dir, second index irrelevant
bonds = [Tuple{Int64, Int64}[] for _ in 1:length(lattice)]
for origin in 1:length(lattice)
for (trg, p) in enumerate(_positions)
d = _positions[origin] .- p .+ wrap[1]
for v in wrap[2:end]
new_d = _positions[origin] .- p .+ v
if directed_norm(new_d, ϵ) + ϵ < directed_norm(d, ϵ)
d .= new_d
end
end
# The rounding will allow us to use == here
idx = findfirst(dir -> isapprox(dir, d, atol=ϵ), directions)
if idx === nothing
push!(directions, d)
if length(bonds) < length(directions)
push!(bonds, Tuple{Int64, Int64}[])
end
push!(bonds[length(directions)], (origin, trg))
else
push!(bonds[idx], (origin, trg))
end
end
end
temp = sortperm(directions, by = v -> directed_norm(v, ϵ))
cache.cache[key] = bonds[temp]
end
nothing
end

function directions(lattice::AbstractLattice, ϵ = 1e-6)
_positions = positions(lattice)
wrap = generate_combinations(lattice_vectors(lattice))
directions = Vector{Float64}[]
for origin in 1:length(lattice)
for (trg, p) in enumerate(_positions)
d = _positions[origin] .- p .+ wrap[1]
for v in wrap[2:end]
new_d = _positions[origin] .- p .+ v
if directed_norm(new_d, ϵ) + ϵ < directed_norm(d, ϵ)
d .= new_d
end
end
idx = findfirst(dir -> isapprox(dir, d, atol=ϵ), directions)
if idx === nothing
push!(directions, d)
end
end
end
# temp = sortperm(directions, by=norm)
# directions[temp]
sort!(directions, by = v -> directed_norm(v, ϵ))
end

These take up a lot of time when generating files. Maybe there is a better way to do this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant