From 7ea038e8146ee60effe930b8391da2704b20d545 Mon Sep 17 00:00:00 2001 From: Thomas Christensen Date: Fri, 13 Sep 2024 20:40:55 +0200 Subject: [PATCH] Unify `Collection` and `IrrepCollection`, removing the latter --- README.md | 2 +- build/ParseIsotropy.jl | 8 +++---- src/Crystalline.jl | 4 ++-- src/irreps_reality.jl | 4 ++-- src/littlegroup_irreps.jl | 6 +++--- src/pointgroup.jl | 8 +++---- src/show.jl | 12 +++++------ src/types.jl | 39 ++++++++++++++++++++++++----------- src/types_symmetry_vectors.jl | 29 ++++++++------------------ src/wyckoff.jl | 4 ++-- test/show.jl | 8 +++---- 11 files changed, 64 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index a68b343d..c29cfe27 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ SpaceGroup{3} ⋕16 (P222) with 4 operations: # indexed by their k-point labels; then inspect the small irreps at the A point julia> lgirs = lgirreps(16, Val(3)) julia> lgirs["A"] -2-element IrrepCollection{LGIrrep{3}} for ⋕16 (P222) at A = [α, 0, 1/2]: +2-element Collection{LGIrrep{3}} for ⋕16 (P222) at A = [α, 0, 1/2]: A₁ ─┬───────────────────────────────────────────── ├─ 1: ──────────────────────────────── (x,y,z) │ 1 diff --git a/build/ParseIsotropy.jl b/build/ParseIsotropy.jl index 0928f66e..0cd4db81 100644 --- a/build/ParseIsotropy.jl +++ b/build/ParseIsotropy.jl @@ -1,7 +1,7 @@ module ParseIsotropy using Crystalline -using Crystalline: AbstractIrrep, IrrepCollection +using Crystalline: AbstractIrrep export parselittlegroupirreps @@ -264,7 +264,7 @@ end parselittlegroupirreps() = parselittlegroupirreps.(parseisoir(Complex)) function parselittlegroupirreps(irvec::Vector{SGIrrep3D{ComplexF64}}) - lgirsd = Dict{String, IrrepCollection{LGIrrep{3}}}() + lgirsd = Dict{String, Collection{LGIrrep{3}}}() curklab = nothing; accidx = Int[] for (idx, ir) in enumerate(irvec) # loop over distinct irreps (e.g., Γ1, Γ2, Γ3, Z1, Z2, ..., GP1) if curklab == klabel(ir) @@ -275,7 +275,7 @@ function parselittlegroupirreps(irvec::Vector{SGIrrep3D{ComplexF64}}) for (pos, kidx) in enumerate(accidx) # write all irreps of a specific k-point to a vector (e.g., Z1, Z2, ...) lgirs[pos] = littlegroupirrep(irvec[kidx]) end - push!(lgirsd, curklab=>IrrepCollection(lgirs)) + push!(lgirsd, curklab=>Collection(lgirs)) end curklab = klabel(ir) @@ -292,7 +292,7 @@ function parselittlegroupirreps(irvec::Vector{SGIrrep3D{ComplexF64}}) end lastklab = klabel(irvec[last(accidx)]) @assert lastklab == "Ω" - push!(lgirsd, lastklab=>IrrepCollection(lgirs)) + push!(lgirsd, lastklab=>Collection(lgirs)) return lgirsd end diff --git a/src/Crystalline.jl b/src/Crystalline.jl index 54a7b996..8d16850c 100644 --- a/src/Crystalline.jl +++ b/src/Crystalline.jl @@ -52,7 +52,7 @@ include("types.jl") # defines useful types for space group symmetry analysis export SymOperation, # types DirectBasis, ReciprocalBasis, Reality, REAL, PSEUDOREAL, COMPLEX, - IrrepCollection, + Collection, MultTable, LGIrrep, PGIrrep, SiteIrrep, KVec, RVec, BandRep, BandRepSet, @@ -74,7 +74,7 @@ export SymOperation, # types isspinful include("types_symmetry_vectors.jl") -export SymmetryVector, NewBandRep, Collection +export SymmetryVector, NewBandRep export irreps, multiplicities, occupation include("notation.jl") diff --git a/src/irreps_reality.jl b/src/irreps_reality.jl index 4bfd0538..785605fb 100644 --- a/src/irreps_reality.jl +++ b/src/irreps_reality.jl @@ -232,7 +232,7 @@ function realify(lgirs::AbstractVector{LGIrrep{D}}; verbose::Bool=false) where D end end - return IrrepCollection(lgirs′) + return Collection(lgirs′) end """ @@ -320,7 +320,7 @@ function realify(irs::AbstractVector{T}) where T<:AbstractIrrep # `AbstractIrrep`, which is probably not a great idea, but meh) ntuple(i->getfield(ir, 5+i), Val(T_extrafields))...)) end - return IrrepCollection(irs′) + return Collection(irs′) end # ---------------------------------------------------------------------------------------- # diff --git a/src/littlegroup_irreps.jl b/src/littlegroup_irreps.jl index ade5b071..12a0131d 100644 --- a/src/littlegroup_irreps.jl +++ b/src/littlegroup_irreps.jl @@ -47,7 +47,7 @@ littlegroups(sgnum::Integer, D::Integer) = littlegroups(sgnum, Val(D)) """ lgirreps(sgnum::Integer, D::Union{Val{Int}, Integer}=Val(3)) - -> Dict{String, IrrepCollection{LGIrrep{D}}} + -> Dict{String, Collection{LGIrrep{D}}} For given space group number `sgnum` and dimension `D`, return the associated little group (or "small") irreps (`LGIrrep{D}`s) at high-symmetry k-points, lines, and planes. @@ -88,11 +88,11 @@ function lgirreps(sgnum::Integer, Dᵛ::Val{D}=Val(3), Ps_list, τs_list, realities_list, cdmls_list = _load_lgirreps_data(sgnum, irs_jldfile) - lgirsd = Dict{String, IrrepCollection{LGIrrep{D}}}() + lgirsd = Dict{String, Collection{LGIrrep{D}}}() for (Ps, τs, realities, cdmls) in zip(Ps_list, τs_list, realities_list, cdmls_list) klab = klabel(first(cdmls)) lg = lgs[klab] - lgirsd[klab] = IrrepCollection( + lgirsd[klab] = Collection( [LGIrrep{D}(cdml, lg, P, τ, Reality(reality)) for (P, τ, reality, cdml) in zip(Ps, τs, realities, cdmls)]) end diff --git a/src/pointgroup.jl b/src/pointgroup.jl index fd4d4e45..8024f76b 100644 --- a/src/pointgroup.jl +++ b/src/pointgroup.jl @@ -137,7 +137,7 @@ function pgirreps(iuclab::String, ::Val{3}=Val(3); mulliken::Bool=false) matrices, realities, cdmls = _load_pgirreps_data(iuclab) pgirlabs = !mulliken ? cdmls : _mulliken.(Ref(iuclab), cdmls, false) - return IrrepCollection(PGIrrep{3}.(pgirlabs, Ref(pg), matrices, Reality.(realities))) + return Collection(PGIrrep{3}.(pgirlabs, Ref(pg), matrices, Reality.(realities))) end # 2D function pgirreps(iuclab::String, ::Val{2}; mulliken::Bool=false) @@ -152,7 +152,7 @@ function pgirreps(iuclab::String, ::Val{2}; mulliken::Bool=false) matrices, realities, cdmls = _load_pgirreps_data(iuclab) pgirlabs = !mulliken ? cdmls : _mulliken.(Ref(iuclab), cdmls, false) - return IrrepCollection(PGIrrep{2}.(pgirlabs, Ref(pg), matrices, Reality.(realities))) + return Collection(PGIrrep{2}.(pgirlabs, Ref(pg), matrices, Reality.(realities))) end # 1D function pgirreps(iuclab::String, ::Val{1}; mulliken::Bool=false) @@ -171,7 +171,7 @@ function pgirreps(iuclab::String, ::Val{1}; mulliken::Bool=false) end pgirlabs = !mulliken ? cdmls : _mulliken.(Ref(iuclab), cdmls, false) - return IrrepCollection(PGIrrep{1}.(pgirlabs, Ref(pg), matrices, REAL)) + return Collection(PGIrrep{1}.(pgirlabs, Ref(pg), matrices, REAL)) end pgirreps(iuclab::String, ::Val{D}; kws...) where D = _throw_invalid_dim(D) # if D ∉ (1,2,3) pgirreps(iuclab::String, D::Integer; kws...) = pgirreps(iuclab, Val(D); kws...) @@ -181,7 +181,7 @@ function pgirreps(pgnum::Integer, Dᵛ::Val{D}=Val(3); return pgirreps(iuc, Dᵛ; kws...) end function pgirreps(pgnum::Integer, D::Integer; kws...) - return pgirreps(pgnum, Val(D); kws...) :: IrrepCollection{<:PGIrrep} + return pgirreps(pgnum, Val(D); kws...) :: Collection{<:PGIrrep} end function ⊕(pgir1::PGIrrep{D}, pgir2::PGIrrep{D}) where D diff --git a/src/show.jl b/src/show.jl index 38ecee58..db84d8cc 100644 --- a/src/show.jl +++ b/src/show.jl @@ -334,12 +334,12 @@ function prettyprint_header(io::IO, irlab::AbstractString, nboxdelims::Integer=4 end # ---------------------------------------------------------------------------------------- # -# IrrepCollection +# Collection{<:AbstractIrrep} -function summary(io::IO, c::IrrepCollection{T}) where T - print(io, length(c), "-element IrrepCollection{", T, "}") +function summary(io::IO, c::Collection{T}) where T <: AbstractIrrep + print(io, length(c), "-element Collection{", T, "}") end -function show(io::IO, ::MIME"text/plain", c::IrrepCollection) +function show(io::IO, ::MIME"text/plain", c::Collection{T}) where T <: AbstractIrrep summary(io, c) isassigned(c, firstindex(c)) && _print_group_descriptor(io, group(first(c)); prefix=" for ") println(io, ":") @@ -352,8 +352,8 @@ function show(io::IO, ::MIME"text/plain", c::IrrepCollection) i ≠ length(c) && println(io) end end -function show(io::IO, c::IrrepCollection) - show(io, c.irs) +function show(io::IO, c::Collection{T}) where T <: AbstractIrrep + show(io, c.vs) g = group(first(c)) if position(g) !== nothing printstyled(io, " (", fullpositionlabel(g), ")"; color=:light_black) diff --git a/src/types.jl b/src/types.jl index 75e13b60..26a44200 100644 --- a/src/types.jl +++ b/src/types.jl @@ -781,21 +781,36 @@ end Base.position(siteir::SiteIrrep) = position(group(siteir)) # ---------------------------------------------------------------------------------------- # -# IrrepCollection +# Collection{T} # ---------------------------------------------------------------------------------------- # -struct IrrepCollection{T<:AbstractIrrep} <: AbstractVector{T} - irs :: Vector{T} -end -Base.size(c::IrrepCollection) = size(c.irs) -Base.IndexStyle(::Type{<:IrrepCollection}) = IndexLinear() -@propagate_inbounds Base.getindex(c::IrrepCollection, i::Int) = c.irs[i] -@propagate_inbounds function Base.setindex!( - c::IrrepCollection{T}, ir::T, i::Int) where T<:AbstractIrrep - c.irs[i] = ir +""" + Collection{T} <: AbstractVector{T} + +A wrapper around a `Vector{T}`, that allows custom printing and dispatch rules of custom +`T` (e.g., `AbstractIrrep` & `NewBandRep`). + +In Crystalline, it is assumed that all elements of the wrapped vector are associated with +the _same_ space or point group. Accordingly, if `T` implements [`dim`](@ref) or +[`num`](@ref), either must return the same element for each element of the `Collection` +(and is equal to `dim(::Collection)` and `num(::Collection)`). +""" +struct Collection{T} <: AbstractVector{T} + vs :: Vector{T} end -IrrepCollection(c::IrrepCollection) = c -Base.similar(c::IrrepCollection{T}) where T = IrrepCollection{T}(similar(c.irs)) + +# ::: AbstractArray interface ::: +Base.size(c::Collection) = size(c.vs) +Base.IndexStyle(::Type{<:Collection}) = IndexLinear() +@propagate_inbounds Base.getindex(c::Collection{T}, i::Integer) where T = c.vs[i]::T +@propagate_inbounds Base.setindex!(c::Collection{T}, v::T, i::Integer) where T = (c.vs[i]=v) +Base.similar(c::Collection{T}) where T = Collection{T}(similar(c.vs)) +Base.iterate(c::Collection) = iterate(c.vs) +Base.iterate(c::Collection, state) = iterate(c.vs, state) + +# ::: Interface ::: +dim(vs::Collection) = dim(first(vs)) +num(vs::Collection) = num(first(vs)) # ---------------------------------------------------------------------------------------- # # CharacterTable diff --git a/src/types_symmetry_vectors.jl b/src/types_symmetry_vectors.jl index 76282af7..32a65f1a 100644 --- a/src/types_symmetry_vectors.jl +++ b/src/types_symmetry_vectors.jl @@ -4,12 +4,13 @@ abstract type AbstractSymmetryVector{D} <: AbstractVector{Int} end # ::: API ::: """ - irreps(n::AbstractSymmetryVector) -> AbstractVector{<:IrrepCollection} + irreps(n::AbstractSymmetryVector{D}) -> AbstractVector{<:Collection{<:AbstractIrrep{D}}} Return the irreps referenced by `n`. -The returned value is an `AbstractVector` of `IrrepCollection`s, with irreps for distinct -groups and **k**-manifolds belonging to the same `IrrepCollection`. +The returned value is an `AbstractVector` of `Collection{<:AbstractIrrep}`s, with irreps for +distinct groups, usually associated with specific **k**-manifolds, belonging to the same +`Collection`. See also [`multiplicities(::AbstractSymmetryVector)`](@ref). """ @@ -68,7 +69,7 @@ end # SymmetryVector mutable struct SymmetryVector{D} <: AbstractSymmetryVector{D} - const lgirsv :: Vector{IrrepCollection{LGIrrep{D}}} + const lgirsv :: Vector{Collection{LGIrrep{D}}} const multsv :: Vector{Vector{Int}} occupation :: Int end @@ -151,26 +152,14 @@ function Base.convert(::Type{BandRep}, br::NewBandRep{D}) where D end # ---------------------------------------------------------------------------------------- # -# Collection -struct Collection{T} <: AbstractVector{T} - vs :: Vector{T} -end - -# ::: AbstractArray interface ::: -Base.size(c::Collection) = size(c.vs) -Base.getindex(c::Collection, i::Int) = c.vs[i] -Base.setindex!(c::Collection{T}, v::T, i::Int) where T = (c.vs[i]=v) -Base.similar(c::Collection{T}) where T = Collection{T}(similar(c.vs)) -Base.iterate(c::Collection) = iterate(c.vs) -Base.iterate(c::Collection, state) = iterate(c.vs, state) +# Collection{<:NewBandRep} # ::: Utilities ::: -num(brs::Collection{NewBandRep}) = num(first(brs).siteir) -irreplabels(brs::Collection{NewBandRep}) = irreplabels(first(brs).n) -klabels(brs::Collection{NewBandRep}) = klabels(first(brs).n) +irreplabels(brs::Collection{<:NewBandRep}) = irreplabels(first(brs).n) +klabels(brs::Collection{<:NewBandRep}) = klabels(first(brs).n) # ::: Conversion to BandRepSet ::: -function Base.convert(::Type{BandRepSet}, brs::Collection{NewBandRep{D}}) where D +function Base.convert(::Type{BandRepSet}, brs::Collection{<:NewBandRep}) sgnum = num(first(brs)) bandreps = convert.(Ref(BandRep), brs) kvs = [position(first(lgirs)) for lgirs in irreps(first(brs))] diff --git a/src/wyckoff.jl b/src/wyckoff.jl index 741f5203..0a7c2883 100644 --- a/src/wyckoff.jl +++ b/src/wyckoff.jl @@ -347,7 +347,7 @@ SiteGroup{2} ⋕16 (p6) at 2b = [1/3, 2/3] with 3 operations: {3⁻|0,1} julia> siteirs = siteirreps(siteg) -3-element IrrepCollection{SiteIrrep{2}} for ⋕16 (p6) at 2b = [1/3, 2/3]: +3-element Collection{SiteIrrep{2}} for ⋕16 (p6) at 2b = [1/3, 2/3]: Γ₁ ─┬───────────────────────────────────────────── ├─ 1: ────────────────────────────────── (x,y) │ 1 @@ -392,6 +392,6 @@ function siteirreps(siteg::SiteGroup{D}; mulliken::Bool=false) where D SiteIrrep{D}(label(pgir), siteg, pgir.matrices[Iᵖ²ᵍ], reality(pgir), pgir.iscorep, pglabel) end - return IrrepCollection(siteirs) + return Collection(siteirs) end mulliken(siteir::SiteIrrep) = _mulliken(siteir.pglabel, label(siteir), iscorep(siteir)) \ No newline at end of file diff --git a/test/show.jl b/test/show.jl index 141027c1..82afcd92 100644 --- a/test/show.jl +++ b/test/show.jl @@ -196,7 +196,7 @@ test_tp_show(sitegroup(sg, wps[end-1]), str) # LGIrrep # ------------------------------- str = """ -4-element IrrepCollection{LGIrrep{3}} for ⋕16 (P222) at Γ = [0, 0, 0]: +4-element Collection{LGIrrep{3}} for ⋕16 (P222) at Γ = [0, 0, 0]: Γ₁ ─┬───────────────────────────────────────────── ├─ 1: ──────────────────────────────── (x,y,z) │ 1 @@ -252,7 +252,7 @@ str = """ test_tp_show(lgirreps(16)["Γ"], str) str = """ -4-element IrrepCollection{LGIrrep{3}}: +4-element Collection{LGIrrep{3}}: #undef #undef #undef @@ -349,8 +349,8 @@ str = """ pgirs = pgirreps("6") pgirs′ = realify(pgirs) test_tp_show(pgirs′[end], str) -@test summary(pgirs) == "6-element IrrepCollection{PGIrrep{3}}" -@test summary(pgirs′) == "4-element IrrepCollection{PGIrrep{3}}" +@test summary(pgirs) == "6-element Collection{PGIrrep{3}}" +@test summary(pgirs′) == "4-element Collection{PGIrrep{3}}" # ------------------------------- # CharacterTable