Skip to content

Faster initialization #156

@ffreyer

Description

@ffreyer

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions