Skip to content

Commit

Permalink
use the new SymmetryVector from Crystalline instead of old `SymVect…
Browse files Browse the repository at this point in the history
…or` from BandGraphs
  • Loading branch information
thchr committed Oct 18, 2024
1 parent 7ea093e commit 58b0e41
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 96 deletions.
5 changes: 3 additions & 2 deletions BandGraphs/ext/BandGraphsGraphMakieExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using BandGraphs

using LayeredLayouts: solve_positions, Zarate # must use https://github.com/thchr/LayeredLayouts.jl#equal_layers
using Crystalline
using Crystalline: AbstractSymmetryVector
using Graphs
using MetaGraphsNext

Expand All @@ -22,13 +23,13 @@ import BandGraphs: plot_flattened_bandgraph, make_vertices_dragable! # exported

## Unfolding a band graph `g` via `kg_trail`
function plot_flattened_bandgraph(
n::SymVector{D},
n::AbstractSymmetryVector{D},
lgirsd::Dict{String, <:AbstractVector{LGIrrep{D}}};
timereversal=true
) where D
# TODO: take a `SubductionTable` as input?

sgnum = num(group(first(first(n.lgirsv))))
sgnum = num(n)
@assert sgnum == num(group(first(first(values(lgirsd)))))

# compute subgraphs and partitions
Expand Down
3 changes: 1 addition & 2 deletions BandGraphs/src/BandGraphs.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module BandGraphs

using Crystalline
using Crystalline: irdim
using Crystalline: irdim, AbstractSymmetryVector
import JLD2
using PrettyTables
using LinearAlgebra
Expand All @@ -16,7 +16,6 @@ using BlockArrays: BlockArray, Block # for `assemble_adjacency`
export
Connection,
SubductionTable,
SymVector,
Partition,
SubGraph,
BandGraph,
Expand Down
2 changes: 1 addition & 1 deletion BandGraphs/src/separable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function findall_vertices(criterion, bandg :: BandGraph{D}) where D
end

function findall_separable_vertices(
criterion, n::SymVector{D}, subts, lgirsd; kws...) where D
criterion, n::AbstractSymmetryVector{D}, subts, lgirsd; kws...) where D
bandg = build_subgraphs(n, subts, lgirsd)
return findall_separable_vertices(criterion, bandg; kws...)
end
Expand Down
19 changes: 10 additions & 9 deletions BandGraphs/src/subgraphs.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function build_subgraphs(
n::SymVector{D},
n::AbstractSymmetryVector{D},
subductions,
lgirsd::Dict{String, <:AbstractVector{LGIrrep{D}}}
) where D
Expand All @@ -9,7 +9,7 @@ function build_subgraphs(

# maximal k-point partitions
partitions_max = Partition{D}[]
for (klab, nsₖ, lgirsₖ) in zip(n.klabs, n.mults, n.lgirsv)
for (klab, nsₖ, lgirsₖ) in zip(klabels(n), multiplicities(n), irreps(n))
kidx += 1
p_max = build_partition(klab, nsₖ, lgirsₖ, kidx, seen_irs)
push!(partitions_max, p_max)
Expand All @@ -28,7 +28,7 @@ function build_subgraphs(
any(p->p.klab==klab_max′, partitions_max) && continue

# now we have to find the entries of k′ ("monodromy" point) in k ("original" point)
idx = findfirst(==(klab_max), n.klabs)
idx = findfirst(==(klab_max), klabels(n))
if isnothing(idx)
# user might have intentionally left out parts of a symmetry vector; then this
# could fail to find the associated "original" irrep in which case it makes
Expand All @@ -39,13 +39,13 @@ function build_subgraphs(
else
kidx += 1
end
@assert replace.(s.irlabsᴳ, Ref(''=>"")) == label.(n.lgirsv[idx]) # verify assumption used below
@assert replace.(s.irlabsᴳ, Ref(''=>"")) == label.(irreps(n)[idx]) # verify assumption used below

lgirs_max′ = map(enumerate(n.lgirsv[idx])) do (i,lgir)
lgirs_max′ = map(enumerate(irreps(n)[idx])) do (i,lgir)
LGIrrep{D}(s.irlabsᴳ[i], lgir.g, lgir.matrices, lgir.translations, lgir.reality,
lgir.iscorep)
end
p_max = build_partition(klab_max′, n.mults[idx], lgirs_max′, kidx, seen_irs)
p_max = build_partition(klab_max′, multiplicities(n)[idx], lgirs_max′, kidx, seen_irs)
push!(partitions_max, p_max)
seen_irs = last(p_max.iridxs)

Expand Down Expand Up @@ -156,6 +156,7 @@ function build_subgraphs(
# with the generic point Ω and its trivial irrep Ω₁, in order to tie together every
# pair of monodromy-related irreps via Ω₁: we do this to ensure that band graphs
# which are connected in energy are also connected as graphs (via Ω₁, at least)
μ = occupation(n)
if !isempty(monodromy_pairs)
lgir_Ω = only(lgirsd["Ω"])
lg_Ω = group(lgir_Ω)
Expand All @@ -169,16 +170,16 @@ function build_subgraphs(
irlab_Ω′ = klab_Ω′ * ""

kidx += 1
global_iridxs = seen_irs .+ (1:n.μ)
global_iridxs = seen_irs .+ (1:occupation(n))
seen_irs = last(global_iridxs)

lg_Ω′ = LittleGroup{D}(lg_Ω.num, lg_Ω.kv, klab_Ω′, lg_Ω.operations)
lgir_Ω′ = LGIrrep{D}(irlab_Ω′, lg_Ω′, lgir_Ω.matrices, lgir_Ω.translations,
lgir_Ω.reality, lgir_Ω.iscorep)
p_Ω′ = Partition(klab_Ω′, fill(lgir_Ω′, n.μ), [1:n.μ], false, kidx, global_iridxs)
p_Ω′ = Partition(klab_Ω′, fill(lgir_Ω′, μ), [1:μ], false, kidx, global_iridxs)
push!(partitions_nonmax, p_Ω′)

A = zeros(Int, length(original_p_max.lgirs), n.μ)
A = zeros(Int, length(original_p_max.lgirs), μ)
col_start = 1
for (row, lgir) in enumerate(original_p_max.lgirs)
cols = col_start:(col_start+irdim(lgir)-1)
Expand Down
57 changes: 9 additions & 48 deletions BandGraphs/src/types.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
# ---------------------------------------------------------------------------------------- #

struct SymVector{D}
mults :: Vector{Vector{Int}}
lgirsv :: Vector{Vector{LGIrrep{D}}}
klabs :: Vector{String}
μ :: Int # connectivity
end
Crystalline.irreplabels(n::SymVector) = [label.(lgirs) for lgirs in n.lgirsv]

"""
SymVector(nv, irlabs_nv, lgirsd)
SymmetryVector(nv, irlabs_nv, lgirsd)
Build a structured representation of a symmetry vector `nv` whose entries correspond to the
irrep labels in `irlabs_nv`, mapping these labels to the full irrep information provided in
`lgirsd`.
Note that the sorting of labels in `lgirsd` and (`nv`, `irlabs_nv`) is allowed to differ.
"""
function SymVector(
function Crystalline.SymmetryVector(
nv::AbstractVector{<:Integer},
irlabs_nv::AbstractVector{String},
lgirsd::Dict{String, <:AbstractVector{LGIrrep{D}}}) where D
klabs = unique(klabel.(irlabs_nv))
Nk = length(klabs)
mults = [Int[] for _ in 1:Nk]
multsv = [Int[] for _ in 1:Nk]
lgirsv = [LGIrrep{D}[] for _ in 1:Nk]
j = 1
for (nᵢ, irlabᵢ) in zip(nv, irlabs_nv)
klabᵢ = klabel(irlabᵢ)
if klabᵢ != klabs[j]
j += 1
end
push!(mults[j], nᵢ)
push!(multsv[j], nᵢ)

# find associated irrep in `lgirsd[klabᵢ]`
lgirsⱼ = lgirsd[klabᵢ]
Expand All @@ -40,50 +33,18 @@ function SymVector(
push!(lgirsv[j], lgirsⱼ[something(iridxᵢ)])
end
end
lgirsv = [Collection(lgirs) for lgirs in lgirsv]
if length(nv) length(irlabs_nv)+1
error("n must contain its band connectivity")
end
μ = nv[end]
return SymVector(mults, lgirsv, string.(klabs), μ)
return SymmetryVector{D}(lgirsv, multsv, μ)
end
function _throw_failed_to_find_irrep(irlabᵢ, lgirsⱼ)
error("failed to find an irrep label \"$irlabᵢ\" in `lgirsd`; available irrep labels \
at the considered k-point were $(label.(lgirsⱼ))")
end

function Base.show(io :: IO, ::MIME"text/plain", n :: SymVector)
summary(io, n)
print(io, " over ", length(n.mults), " k-points:\n ")
show(io, n)
end

function Base.show(io :: IO, n :: SymVector)
print(io, "[")
for (i, (mults_k, lgirs_k)) in enumerate(zip(n.mults, n.lgirsv))
printstyled(io,
Crystalline.symvec2string(mults_k, label.(lgirs_k); braces=false);
color=iseven(i) ? :light_blue : :normal)
i length(n.mults) && print(io, ", ")
end
print(io, "]")
printstyled(io, " (", n.μ, " band", n.μ 1 ? "s" : "", ")"; color=:light_black)
end

Base.collect(n :: SymVector) = reduce(vcat, n.mults) # flatten `n` to a simple vector `nv`

@noinline function _throw_dissimilar_irreps(n₁, n₂)
error("cannot add symmetry vectors with different irreps:\n $(n₁.lgirsv)$(n₂.lgirsv)")
end
function Base.:+(n₁ :: SymVector{D}, n₂ :: SymVector{D}) where D
length(n₁.lgirsv) == length(n₂.lgirsv) || _throw_dissimilar_irreps(n₁, n₂)
for (lgirs₁, lgirs₂) in zip(n₁.lgirsv, n₂.lgirsv)
length(lgirs₁) == length(lgirs₂) || _throw_dissimilar_irreps(n₁, n₂)
for (lgir₁, lgir₂) in zip(lgirs₁, lgirs₂)
label(lgir₁) == label(lgir₂) || _throw_dissimilar_irreps(n₁, n₂)
end
end
return SymVector(n₁.mults + n₂.mults, n₁.lgirsv, n₁.klabs, n₁.μ + n₂.μ)
end
# ---------------------------------------------------------------------------------------- #

struct Partition{D}
Expand All @@ -94,7 +55,7 @@ struct Partition{D}
kidx :: Int # index in the block form (i.e., index of k-manifold)
iridxs :: UnitRange{Int} # _global_ irrep indices
end
Base.length(p::Partition) = length(irreplabels(p))
Base.length(p::Partition) = length(p.lgirs)
Crystalline.irreplabels(p::Partition) = label.(p.lgirs)
irdims(p::Partition) = Crystalline.irdim.(p.lgirs)

Expand Down Expand Up @@ -195,7 +156,7 @@ function BandGraph(
BandGraph{D}(Vector(subgraphs), Vector(partitions))
end

function occupation(bandg::BandGraph)
function Crystalline.occupation(bandg::BandGraph)
partitions = bandg.partitions
μ = sum(irdim, first(partitions).lgirs)
for p in @view partitions[2:end]
Expand Down
6 changes: 3 additions & 3 deletions BandGraphs/test/bandgraph_properties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ using Crystalline: irdim
lgirsd = lgirreps(sgnum, Dᵛ)
timereversal && realify!(lgirsd)
for _n in sb
n = SymVector(_n, sb.irlabs, lgirsd)
n = SymmetryVector(_n, sb.irlabs, lgirsd)
bandg = build_subgraphs(n, subts, lgirsd)
partitions, subgraphs = bandg.partitions, bandg.subgraphs

# band occupation & grand-sums of all subgraphs must be constant & equal
occupations = [sum(s.A) for s in subgraphs]
@test all(==(n.μ), occupations)
@test all(==(occupation(n)), occupations)
A = assemble_adjacency(bandg)
@test all(b -> iszero(b) || sum(b) == n.μ, A.blocks)
@test all(b -> iszero(b) || sum(b) == occupation(n), A.blocks)

# column-wise and row-wise sums of adjacency blocks must give corresponding
# irrep dimensions of columns and rows, respectively
Expand Down
2 changes: 1 addition & 1 deletion BandGraphs/test/preliminary_planegroup_work.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ for (j, _n) in enumerate(sb)
println(j)
_n[end] == 1 && continue # nothing interesting to draw for one-band configurations

n = SymVector(_n, brs.irlabs, lgirsd)
n = SymmetryVector(_n, brs.irlabs, lgirsd)
bandg = build_subgraphs(n, subts²ᴰ, lgirsd)

subgraphs_ps = permute_subgraphs(bandg.subgraphs);
Expand Down
4 changes: 2 additions & 2 deletions BandGraphs/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ timereversal && realify!(lgirsd)
subts = subduction_tables(sgnum, D; timereversal)
_n = sb[end]
#_n = brs[end-1]
n = SymVector(_n, brs.irlabs, lgirsd)
n = SymmetryVector(_n, brs.irlabs, lgirsd)

bandg = build_subgraphs(n, subts, lgirsd)
subgraphs, partitions = bandg.subgraphs, bandg.partitions
Expand Down Expand Up @@ -98,7 +98,7 @@ let sgnum = 96, D = 3 # 200
subts = subduction_tables(sgnum, D; timereversal)

for nv in sb
n = SymVector(nv, brs.irlabs, lgirsd);
n = SymmetryVector(nv, brs.irlabs, lgirsd);
bandg = build_subgraphs(n, subts, lgirsd)
subgraphs_ps = permute_subgraphs(bandg.subgraphs)
bandgp = BandGraphs.BandGraphPermutations(bandg.partitions, subgraphs_ps);
Expand Down
20 changes: 10 additions & 10 deletions BandGraphs/test/seperable-irreps-testbed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ criterion = lgir -> isspecial(lgir) && irdim(lgir) == 3 # KdotP.isweyl(lgir; tim
separable_degree = 2

sgnums = 1:MAX_SGNUM[D]
contendersd = Dictionary{Int, Vector{SymVector{D}}}()
contendersd = Dictionary{Int, Vector{SymmetryVector{D}}}()
lgirsdd = Dictionary{Int, Dict{String, Collection{LGIrrep{D}}}}()
subtsd = Dictionary{Int, Vector{SubductionTable{D}}}()
for sgnum in sgnums
Expand All @@ -32,7 +32,7 @@ for sgnum in sgnums
any(criterion, lgirs)
end
if has_relevant_lgirs
insert!(contendersd, sgnum, SymVector{D}[])
insert!(contendersd, sgnum, SymmetryVector{D}[])
insert!(lgirsdd, sgnum, lgirsd)
insert!(subtsd, sgnum, subduction_tables(sgnum, D; timereversal))
else
Expand All @@ -45,8 +45,8 @@ for sgnum in sgnums
for i in eachindex(sb)
μs[i] == 1 && continue # nothing interesting in 1-band cases

n = SymVector(sb[i], brs.irlabs, lgirsd)
for (lgirs, mults) in zip(n.lgirsv, n.mults)
n = SymmetryVector(sb[i], brs.irlabs, lgirsd)
for (lgirs, mults) in zip(irreps(n), multiplicities(n))
for (lgir, m) in zip(lgirs, mults)
if m 0 && criterion(lgir)
push!(contendersd[sgnum], n)
Expand All @@ -63,9 +63,9 @@ end


## --------------------------------------------------------------------------------------- #
separable_summary = Dictionary{Int, Vector{Tuple{Vector{LGIrrep{D}}, SymVector{D}}}}()
separable_details = Dictionary{Int, Vector{Tuple{SymVector{D}, Vector{Tuple{BandGraph{D}, BandGraph{D}, LGIrrep{D}}}}}}()
inseparable = Dictionary{Int, Vector{SymVector{D}}}()
separable_summary = Dictionary{Int, Vector{Tuple{Vector{LGIrrep{D}}, SymmetryVector{D}}}}()
separable_details = Dictionary{Int, Vector{Tuple{SymmetryVector{D}, Vector{Tuple{BandGraph{D}, BandGraph{D}, LGIrrep{D}}}}}}()
inseparable = Dictionary{Int, Vector{SymmetryVector{D}}}()

done = Set{Int}()
too_hard = Set{Int}()
Expand Down Expand Up @@ -99,13 +99,13 @@ for (sgnum, ns) in pairs(contendersd)
has_split, bandg_splits = tmp
if has_split
lgir = getindex.(bandg_splits, 3)
push!(get!(separable_summary, sgnum, Vector{SymVector{D}}()), (lgir, n))
push!(get!(separable_summary, sgnum, Vector{SymmetryVector{D}}()), (lgir,n))
push!(get!(separable_details, sgnum,
Vector{Tuple{SymVector{D}, typeof(bandg_splits)}}()),
Vector{Tuple{SymmetryVector{D}, typeof(bandg_splits)}}()),
(n, bandg_splits))
printstyled(" HAS SEPARABLE CONFIGURATIONS!!!\n", color=:green)
else
push!(get!(inseparable, sgnum, Vector{SymVector{D}}()), n)
push!(get!(inseparable, sgnum, Vector{SymmetryVector{D}}()), n)
end
catch e
printstyled(" ", e, "\n",
Expand Down
18 changes: 9 additions & 9 deletions BandGraphs/test/splittable-2d-irreps-in-planegroups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ D = 2
timereversal = false
criterion = (lgir) -> irdim(lgir) == 2

contenders = Vector{Pair{Int, SymVector{D}}}()
contenders = Vector{Pair{Int, SymmetryVector{D}}}()
lgirsdd = Dict{Int, Dict{String, Collection{LGIrrep{D}}}}()
subtsd = Dict{Int, Vector{SubductionTable{D}}}()
for sgnum in 1:MAX_SGNUM[D]
Expand All @@ -31,8 +31,8 @@ for sgnum in 1:MAX_SGNUM[D]
μs[i] == 1 && continue # nothing interesting in 1-band cases

_n = sb[i]
n = SymVector(_n, brs.irlabs, lgirsd)
for (lgirs, mults) in zip(n.lgirsv, n.mults)
n = SymmetryVector(_n, brs.irlabs, lgirsd)
for (lgirs, mults) in zip(irreps(n), multiplicities(n))
for (lgir, m) in zip(lgirs, mults)
if m 0 && criterion(lgir)
push!(contenders, sgnum => n)
Expand All @@ -51,22 +51,22 @@ end


## --------------------------------------------------------------------------------------- #
separable_summary = Dict{Int, Vector{Tuple{Vector{LGIrrep{D}}, SymVector{D}}}}()
separable_details = Dict{Int, Vector{Tuple{SymVector{D}, Vector{Tuple{BandGraph{D}, BandGraph{D}, LGIrrep{D}}}}}}()
inseparable = Dict{Int, Vector{SymVector{D}}}()
separable_summary = Dict{Int, Vector{Tuple{Vector{LGIrrep{D}}, SymmetryVector{D}}}}()
separable_details = Dict{Int, Vector{Tuple{SymmetryVector{D}, Vector{Tuple{BandGraph{D}, BandGraph{D}, LGIrrep{D}}}}}}()
inseparable = Dict{Int, Vector{SymmetryVector{D}}}()

for (i, (sgnum, n)) in enumerate(contenders)
subts = subtsd[sgnum]
lgirsd = lgirsdd[sgnum]
has_split, bandg_splits = findall_separable_vertices(criterion, n, subts, lgirsd)
if has_split
lgir = getindex.(bandg_splits, 3)
push!(get!(separable_summary, sgnum, Vector{SymVector{D}}()), (lgir, n))
push!(get!(separable_summary, sgnum, Vector{SymmetryVector{D}}()), (lgir, n))
push!(get!(separable_details, sgnum,
Vector{Tuple{SymVector{D}, typeof(bandg_splits)}}()),
Vector{Tuple{SymmetryVector{D}, typeof(bandg_splits)}}()),
(n, bandg_splits))
else
push!(get!(inseparable, sgnum, Vector{SymVector{D}}()), n)
push!(get!(inseparable, sgnum, Vector{SymmetryVector{D}}()), n)
end
end
separable_summary
Expand Down
2 changes: 1 addition & 1 deletion BandGraphs/test/subgroup-relationships.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ for sgnum in 1:230
checked_vectors = 0
n_permutations = BigInt(0)
for _n in sb
n = SymVector(_n, sb.irlabs, lgirsd)
n = SymmetryVector(_n, sb.irlabs, lgirsd)
any(irlab -> contains(string(n), irlab), irlabs) || continue
checked_vectors += 1
Expand Down
Loading

0 comments on commit 58b0e41

Please sign in to comment.