Skip to content

Commit

Permalink
optimizations in sites_to_orbs
Browse files Browse the repository at this point in the history
  • Loading branch information
pablosanjose committed Jan 22, 2024
1 parent 7a70bef commit 80604c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
58 changes: 26 additions & 32 deletions src/slices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,53 +339,47 @@ sites_to_orbs(c::CellSites, g) = sites_to_orbs(c, blockstructure(g))

function sites_to_orbs(cs::CellSites, os::OrbitalBlockStructure)
sites = siteindices(cs)
orbinds, groups = orbinds_and_groups(sites, os)
return CellIndices(cell(cs), orbinds, OrbitalLikeGrouped(dictionary(groups)))
groups = _groups(sites, os) # sites, orbranges
orbinds = _orbinds(sites, groups, os)
return CellIndices(cell(cs), orbinds, OrbitalLikeGrouped(Dictionary(groups...)))
end

function sites_to_orbs_flat(cs::CellSites, os::OrbitalBlockStructure)
sites = siteindices(cs)
orbinds = orbinds_only(sites, os)
orbinds = _orbinds(sites, os)
return CellIndices(cell(cs), orbinds, OrbitalLike())
end

function orbinds_and_groups(sites, os)
orbinds = Int[]
groups = Pair{Int,UnitRange{Int}}[]
for i in sites
rng = flatrange(os, i)
append!(orbinds, rng)
push!(groups, i => rng) # site index => orb range
_groups(i::Integer, os) = [i], [flatrange(os, i)]
_groups(::Colon, os) = _groups(siterange(os), os)

function _groups(sites, os)
siteinds = Int[]
orbranges = UnitRange{Int}[]
sizehint!(siteinds, length(sites))
sizehint!(orbranges, length(sites))
for site in sites
rng = flatrange(os, site)
push!(siteinds, site)
push!(orbranges, rng)
end
return orbinds, groups
end

function orbinds_and_groups(i::Integer, os)
orbinds = flatrange(os, i) # a UnitRange
groups = (i => orbinds,)
return orbinds, groups
return siteinds, orbranges
end

function orbinds_and_groups(::Colon, os)
orbinds = flatrange(os, :) # a UnitRange
groups = Pair{Int,UnitRange{Int}}[]
for i in siterange(os)
push!(groups, i => flatrange(os, i))
end
return orbinds, groups
end
_orbinds(sites::Union{Integer,Colon,AbstractUnitRange}, _, os) = flatrange(os, sites)

function orbinds_and_groups(sites::AbstractUnitRange, os)
orbinds = flatrange(os, sites) # a UnitRange
groups = Pair{Int,UnitRange{Int}}[]
for i in sites
push!(groups, i => flatrange(os, i))
function _orbinds(_, (sites, orbrngs), os) # reuse precomputed groups
orbinds = Int[]
sizehint!(orbinds, length(sites))
for rng in orbrngs
append!(orbinds, rng)
end
return orbinds, groups
return orbinds
end

function orbinds_only(sites, os)
function _orbinds(sites, os)
orbinds = Int[]
sizehint!(orbinds, length(sites))
for i in sites
rng = flatrange(os, i)
append!(orbinds, rng)
Expand Down
6 changes: 3 additions & 3 deletions src/solvers/green/schur.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function SchurFactorsSolver(h::Hamiltonian{T,<:Any,1}, shift = one(Complex{T}))
iG, (p, pd) = store_diagonal_ptrs(fh0)
ptrs = (p, pd, pd[sinds])
workspace = SchurWorkspace{Complex{T}}(size(L), length(linds), length(rinds))
return SchurFactorsSolver(shift, hm, h0, hp, l_leq_r, iG, ptrs, linds, rinds, sinds, L, R, R´L´, workspace)
return SchurFactorsSolver(T(shift), hm, h0, hp, l_leq_r, iG, ptrs, linds, rinds, sinds, L, R, R´L´, workspace)
end

function SchurWorkspace{C}((n, d), l, r) where {C}
Expand Down Expand Up @@ -341,9 +341,9 @@ end

#region ## apply ##

function apply(s::GS.Schur, h::AbstractHamiltonian1D{T}, contacts::Contacts) where {T}
function apply(s::GS.Schur, h::AbstractHamiltonian1D, contacts::Contacts)
= hamiltonian(h)
fsolver = SchurFactorsSolver(h´, T(s.shift))
fsolver = SchurFactorsSolver(h´, s.shift)
h0 = unitcell_hamiltonian(h)
boundary = round(only(s.boundary))
rsites = stored_cols(h[unflat(1)])
Expand Down

0 comments on commit 80604c6

Please sign in to comment.